Introduce utils_create_pipe_cloexec_nonblock()
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Tue, 16 Jul 2013 00:16:07 +0000 (20:16 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Tue, 16 Jul 2013 18:33:11 +0000 (14:33 -0400)
Reviewed-by: Julien Desfossez <julien.desfossez@efficios.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
src/common/utils.c
src/common/utils.h

index cfb6555a1d65fc061eb20c2c69326a91175f97ec..b345100ea0fa5ad6517d28c72cbc53a5bd87e391 100644 (file)
@@ -142,6 +142,48 @@ error:
        return ret;
 }
 
+/*
+ * Create pipe and set fd flags to FD_CLOEXEC and O_NONBLOCK.
+ *
+ * Make sure the pipe opened by this function are closed at some point. Use
+ * utils_close_pipe(). Using pipe() and fcntl rather than pipe2() to
+ * support OSes other than Linux 2.6.23+.
+ */
+LTTNG_HIDDEN
+int utils_create_pipe_cloexec_nonblock(int *dst)
+{
+       int ret, i;
+
+       if (dst == NULL) {
+               return -1;
+       }
+
+       ret = utils_create_pipe(dst);
+       if (ret < 0) {
+               goto error;
+       }
+
+       for (i = 0; i < 2; i++) {
+               ret = fcntl(dst[i], F_SETFD, FD_CLOEXEC);
+               if (ret < 0) {
+                       PERROR("fcntl pipe cloexec");
+                       goto error;
+               }
+               /*
+                * Note: we override any flag that could have been
+                * previously set on the fd.
+                */
+               ret = fcntl(dst[i], F_SETFL, O_NONBLOCK);
+               if (ret < 0) {
+                       PERROR("fcntl pipe nonblock");
+                       goto error;
+               }
+       }
+
+error:
+       return ret;
+}
+
 /*
  * Close both read and write side of the pipe.
  */
index 06aef4f1a33417b80e18672240ee500c648e5ce6..7d8d70b5c29cfad26260b172a5381ca8d6674069 100644 (file)
@@ -29,6 +29,7 @@
 char *utils_expand_path(const char *path);
 int utils_create_pipe(int *dst);
 int utils_create_pipe_cloexec(int *dst);
+int utils_create_pipe_cloexec_nonblock(int *dst);
 void utils_close_pipe(int *src);
 char *utils_strdupdelim(const char *begin, const char *end);
 int utils_set_fd_cloexec(int fd);
This page took 0.027252 seconds and 4 git commands to generate.