Commit | Line | Data |
---|---|---|
df038819 | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
df038819 | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: GPL-2.0-only |
df038819 | 5 | * |
df038819 JG |
6 | */ |
7 | ||
df038819 | 8 | #include <common/fd-tracker/utils.h> |
c3e9c4c6 | 9 | #include <common/utils.h> |
5c1f54d1 JG |
10 | #include <stdio.h> |
11 | #include <stdlib.h> | |
12 | #include <unistd.h> | |
c3e9c4c6 | 13 | |
5c1f54d1 | 14 | static int open_pipe_cloexec(void *data, int *fds) |
c3e9c4c6 | 15 | { |
e51dc192 | 16 | return utils_create_pipe_cloexec(fds); |
c3e9c4c6 JG |
17 | } |
18 | ||
5c1f54d1 | 19 | static int close_pipe(void *data, int *pipe) |
c3e9c4c6 JG |
20 | { |
21 | utils_close_pipe(pipe); | |
22 | pipe[0] = pipe[1] = -1; | |
23 | return 0; | |
24 | } | |
df038819 JG |
25 | |
26 | int fd_tracker_util_close_fd(void *unused, int *fd) | |
27 | { | |
28 | return close(*fd); | |
29 | } | |
c3e9c4c6 | 30 | |
5c1f54d1 JG |
31 | int fd_tracker_util_pipe_open_cloexec( |
32 | struct fd_tracker *tracker, const char *name, int *pipe) | |
c3e9c4c6 JG |
33 | { |
34 | int ret; | |
35 | const char *name_prefix; | |
36 | char *names[2]; | |
37 | ||
38 | name_prefix = name ? name : "Unknown pipe"; | |
39 | ret = asprintf(&names[0], "%s (read end)", name_prefix); | |
40 | if (ret < 0) { | |
41 | goto end; | |
42 | } | |
43 | ret = asprintf(&names[1], "%s (write end)", name_prefix); | |
44 | if (ret < 0) { | |
45 | goto end; | |
46 | } | |
47 | ||
5c1f54d1 | 48 | ret = fd_tracker_open_unsuspendable_fd(tracker, pipe, |
c3e9c4c6 JG |
49 | (const char **) names, 2, open_pipe_cloexec, NULL); |
50 | free(names[0]); | |
51 | free(names[1]); | |
52 | end: | |
53 | return ret; | |
54 | } | |
55 | ||
56 | int fd_tracker_util_pipe_close(struct fd_tracker *tracker, int *pipe) | |
57 | { | |
5c1f54d1 JG |
58 | return fd_tracker_close_unsuspendable_fd( |
59 | tracker, pipe, 2, close_pipe, NULL); | |
c3e9c4c6 | 60 | } |