fd-tracker: add pipe management wrappers to fd-tracker
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Fri, 22 Nov 2019 23:52:03 +0000 (18:52 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Sat, 23 Nov 2019 01:11:40 +0000 (20:11 -0500)
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 <jeremie.galarneau@efficios.com>
src/common/fd-tracker/utils.c
src/common/fd-tracker/utils.h

index ac4e882c7d3b3e818edb8e216ed8130d3456a07b..cd4418ed28895ae9438eb73f29c5598ebdd67da9 100644 (file)
  */
 
 #include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
 #include <common/fd-tracker/utils.h>
+#include <common/utils.h>
+
+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);
+}
index 86af3c6e1aadf695317a082160b37dbb2fd71874..e250f4af02212a0fd804c4c3880255e42c36da68 100644 (file)
@@ -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.
  */
This page took 0.026851 seconds and 4 git commands to generate.