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