Add pipe creation to utils facility
[lttng-tools.git] / src / bin / lttng-sessiond / utils.c
CommitLineData
8e68d1c8 1/*
996b65c8 2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
1e307fab 3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8e68d1c8 4 *
d14d33bf
AM
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
8e68d1c8 8 *
d14d33bf
AM
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
8e68d1c8 13 *
d14d33bf
AM
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
8e68d1c8
DG
17 */
18
19#define _GNU_SOURCE
20#include <errno.h>
ef599319 21#include <fcntl.h>
8e68d1c8
DG
22#include <limits.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
8e68d1c8
DG
26#include <unistd.h>
27
db758600 28#include <common/error.h>
7272acf5 29
8e68d1c8
DG
30#include "utils.h"
31
54d01ffb
DG
32/*
33 * Write to writable pipe used to notify a thread.
34 */
35int notify_thread_pipe(int wpipe)
36{
37 int ret;
38
39 ret = write(wpipe, "!", 1);
40 if (ret < 0) {
7272acf5 41 PERROR("write poll pipe");
54d01ffb
DG
42 }
43
44 return ret;
45}
46
b082db07 47/*
050349bb 48 * Return pointer to home directory path using the env variable HOME.
b082db07 49 *
050349bb 50 * No home, NULL is returned.
b082db07
DG
51 */
52const char *get_home_dir(void)
53{
54 return ((const char *) getenv("HOME"));
55}
ef599319
DG
56
57/*
58 * Create a pipe in dst.
59 */
60int utils_create_pipe(int *dst)
61{
62 int ret;
63
64 if (dst == NULL) {
65 return -1;
66 }
67
68 ret = pipe(dst);
69 if (ret < 0) {
70 PERROR("create pipe");
71 }
72
73 return ret;
74}
75
76/*
77 * Create pipe and set CLOEXEC flag to both fd.
78 *
79 * Make sure the pipe opened by this function are closed at some point. Use
80 * utils_close_pipe().
81 */
82int utils_create_pipe_cloexec(int *dst)
83{
84 int ret, i;
85
86 if (dst == NULL) {
87 return -1;
88 }
89
90 ret = utils_create_pipe(dst);
91 if (ret < 0) {
92 goto error;
93 }
94
95 for (i = 0; i < 2; i++) {
96 ret = fcntl(dst[i], F_SETFD, FD_CLOEXEC);
97 if (ret < 0) {
98 PERROR("fcntl pipe cloexec");
99 goto error;
100 }
101 }
102
103error:
104 return ret;
105}
106
107/*
108 * Close both read and write side of the pipe.
109 */
110void utils_close_pipe(int *src)
111{
112 int i, ret;
113
114 if (src == NULL) {
115 return;
116 }
117
118 for (i = 0; i < 2; i++) {
119 /* Safety check */
120 if (src[i] < 0) {
121 continue;
122 }
123
124 ret = close(src[i]);
125 if (ret) {
126 PERROR("close pipe");
127 }
128 }
129}
This page took 0.034423 seconds and 4 git commands to generate.