From c3e9c4c6febdea6b999db5ddc150782f3da5f78a Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Galarneau?= Date: Fri, 22 Nov 2019 18:52:03 -0500 Subject: [PATCH] fd-tracker: add pipe management wrappers to fd-tracker MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Add wrappers to allow the creation of a pipe the two ends of which are tracked by an fd-tracker. Signed-off-by: Jérémie Galarneau --- src/common/fd-tracker/utils.c | 55 +++++++++++++++++++++++++++++++++++ src/common/fd-tracker/utils.h | 7 +++++ 2 files changed, 62 insertions(+) diff --git a/src/common/fd-tracker/utils.c b/src/common/fd-tracker/utils.c index ac4e882c7..cd4418ed2 100644 --- a/src/common/fd-tracker/utils.c +++ b/src/common/fd-tracker/utils.c @@ -16,9 +16,64 @@ */ #include +#include +#include #include +#include + +static +int open_pipe_cloexec(void *data, int *fds) +{ + int ret; + + ret = utils_create_pipe_cloexec(fds); + if (ret < 0) { + goto end; + } +end: + return ret; +} + +static +int close_pipe(void *data, int *pipe) +{ + utils_close_pipe(pipe); + pipe[0] = pipe[1] = -1; + return 0; +} int fd_tracker_util_close_fd(void *unused, int *fd) { return close(*fd); } + +int fd_tracker_util_pipe_open_cloexec(struct fd_tracker *tracker, + const char *name, int *pipe) +{ + int ret; + const char *name_prefix; + char *names[2]; + + name_prefix = name ? name : "Unknown pipe"; + ret = asprintf(&names[0], "%s (read end)", name_prefix); + if (ret < 0) { + goto end; + } + ret = asprintf(&names[1], "%s (write end)", name_prefix); + if (ret < 0) { + goto end; + } + + ret = fd_tracker_open_unsuspendable_fd(tracker, pipe, + (const char **) names, 2, open_pipe_cloexec, NULL); + free(names[0]); + free(names[1]); +end: + return ret; +} + +int fd_tracker_util_pipe_close(struct fd_tracker *tracker, int *pipe) +{ + return fd_tracker_close_unsuspendable_fd(tracker, + pipe, 2, close_pipe, NULL); +} diff --git a/src/common/fd-tracker/utils.h b/src/common/fd-tracker/utils.h index 86af3c6e1..e250f4af0 100644 --- a/src/common/fd-tracker/utils.h +++ b/src/common/fd-tracker/utils.h @@ -28,6 +28,13 @@ struct lttng_poll_event; */ int fd_tracker_util_close_fd(void *, int *fd); +/* + * Create a pipe and track its underlying fds. + */ +int fd_tracker_util_pipe_open_cloexec(struct fd_tracker *tracker, + const char *name, int *pipe); +int fd_tracker_util_pipe_close(struct fd_tracker *tracker, int *pipe); + /* * Create a poll event and track its underlying fd, if applicable. */ -- 2.34.1