X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=src%2Fcommon%2Futils.c;h=b345100ea0fa5ad6517d28c72cbc53a5bd87e391;hb=f82d9449a304561c5914b2c5f9471a640ca99248;hp=cfb6555a1d65fc061eb20c2c69326a91175f97ec;hpb=26fe59386444e0c932b5adcd5f9d6f9f114dd1e2;p=lttng-tools.git 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. */