fd-tracker: add pipe management wrappers to fd-tracker
[lttng-tools.git] / src / common / fd-tracker / utils.c
1 /*
2 * Copyright (C) 2018 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <common/fd-tracker/utils.h>
22 #include <common/utils.h>
23
24 static
25 int open_pipe_cloexec(void *data, int *fds)
26 {
27 int ret;
28
29 ret = utils_create_pipe_cloexec(fds);
30 if (ret < 0) {
31 goto end;
32 }
33 end:
34 return ret;
35 }
36
37 static
38 int close_pipe(void *data, int *pipe)
39 {
40 utils_close_pipe(pipe);
41 pipe[0] = pipe[1] = -1;
42 return 0;
43 }
44
45 int fd_tracker_util_close_fd(void *unused, int *fd)
46 {
47 return close(*fd);
48 }
49
50 int fd_tracker_util_pipe_open_cloexec(struct fd_tracker *tracker,
51 const char *name, int *pipe)
52 {
53 int ret;
54 const char *name_prefix;
55 char *names[2];
56
57 name_prefix = name ? name : "Unknown pipe";
58 ret = asprintf(&names[0], "%s (read end)", name_prefix);
59 if (ret < 0) {
60 goto end;
61 }
62 ret = asprintf(&names[1], "%s (write end)", name_prefix);
63 if (ret < 0) {
64 goto end;
65 }
66
67 ret = fd_tracker_open_unsuspendable_fd(tracker, pipe,
68 (const char **) names, 2, open_pipe_cloexec, NULL);
69 free(names[0]);
70 free(names[1]);
71 end:
72 return ret;
73 }
74
75 int fd_tracker_util_pipe_close(struct fd_tracker *tracker, int *pipe)
76 {
77 return fd_tracker_close_unsuspendable_fd(tracker,
78 pipe, 2, close_pipe, NULL);
79 }
This page took 0.043115 seconds and 4 git commands to generate.