From: Jérémie Galarneau Date: Fri, 22 Nov 2019 23:52:03 +0000 (-0500) Subject: fd-tracker: add pipe management wrappers to fd-tracker X-Git-Tag: v2.12.0-rc1~225 X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=commitdiff_plain;h=c3e9c4c6febdea6b999db5ddc150782f3da5f78a fd-tracker: add pipe management wrappers to fd-tracker 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 --- 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. */