From: Mathieu Desnoyers Date: Tue, 16 Jul 2013 00:16:07 +0000 (-0400) Subject: Introduce utils_create_pipe_cloexec_nonblock() X-Git-Tag: v2.3.0-rc1~23 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=094f381cd5b1fa8588098b840bcc26bc974ea3d9;p=lttng-tools.git Introduce utils_create_pipe_cloexec_nonblock() Reviewed-by: Julien Desfossez Signed-off-by: Mathieu Desnoyers --- diff --git a/src/common/utils.c b/src/common/utils.c index cfb6555a1..b345100ea 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -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. */ diff --git a/src/common/utils.h b/src/common/utils.h index 06aef4f1a..7d8d70b5c 100644 --- a/src/common/utils.h +++ b/src/common/utils.h @@ -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);