Commit | Line | Data |
---|---|---|
df038819 JG |
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> | |
c3e9c4c6 JG |
19 | #include <stdio.h> |
20 | #include <stdlib.h> | |
df038819 | 21 | #include <common/fd-tracker/utils.h> |
c3e9c4c6 JG |
22 | #include <common/utils.h> |
23 | ||
24 | static | |
25 | int open_pipe_cloexec(void *data, int *fds) | |
26 | { | |
e51dc192 | 27 | return utils_create_pipe_cloexec(fds); |
c3e9c4c6 JG |
28 | } |
29 | ||
30 | static | |
31 | int close_pipe(void *data, int *pipe) | |
32 | { | |
33 | utils_close_pipe(pipe); | |
34 | pipe[0] = pipe[1] = -1; | |
35 | return 0; | |
36 | } | |
df038819 JG |
37 | |
38 | int fd_tracker_util_close_fd(void *unused, int *fd) | |
39 | { | |
40 | return close(*fd); | |
41 | } | |
c3e9c4c6 JG |
42 | |
43 | int fd_tracker_util_pipe_open_cloexec(struct fd_tracker *tracker, | |
44 | const char *name, int *pipe) | |
45 | { | |
46 | int ret; | |
47 | const char *name_prefix; | |
48 | char *names[2]; | |
49 | ||
50 | name_prefix = name ? name : "Unknown pipe"; | |
51 | ret = asprintf(&names[0], "%s (read end)", name_prefix); | |
52 | if (ret < 0) { | |
53 | goto end; | |
54 | } | |
55 | ret = asprintf(&names[1], "%s (write end)", name_prefix); | |
56 | if (ret < 0) { | |
57 | goto end; | |
58 | } | |
59 | ||
60 | ret = fd_tracker_open_unsuspendable_fd(tracker, pipe, | |
61 | (const char **) names, 2, open_pipe_cloexec, NULL); | |
62 | free(names[0]); | |
63 | free(names[1]); | |
64 | end: | |
65 | return ret; | |
66 | } | |
67 | ||
68 | int fd_tracker_util_pipe_close(struct fd_tracker *tracker, int *pipe) | |
69 | { | |
70 | return fd_tracker_close_unsuspendable_fd(tracker, | |
71 | pipe, 2, close_pipe, NULL); | |
72 | } |