From 6f88bc77617dbf8a919830ae070e5ca926755587 Mon Sep 17 00:00:00 2001 From: wm4 Date: Sun, 26 Oct 2014 01:18:55 +0200 Subject: osdep: add helper for creating a sane pipe() Or in other words, a pipe that has the CLOEXEC flag set. Needed since Linux' pipe2() is not in POSIX yet. --- osdep/io.c | 24 ++++++++++++++++++++---- osdep/io.h | 1 + 2 files changed, 21 insertions(+), 4 deletions(-) (limited to 'osdep') diff --git a/osdep/io.c b/osdep/io.c index 37ea37f058..00715288a9 100644 --- a/osdep/io.c +++ b/osdep/io.c @@ -43,22 +43,38 @@ bool mp_set_cloexec(int fd) } #ifdef __MINGW32__ -int mp_make_wakeup_pipe(int pipes[2]) +int mp_make_cloexec_pipe(int pipes[2]) { pipes[0] = pipes[1] = -1; return -1; } #else -// create a pipe, and set it to non-blocking (and also set FD_CLOEXEC) -int mp_make_wakeup_pipe(int pipes[2]) +int mp_make_cloexec_pipe(int pipes[2]) { if (pipe(pipes) != 0) { pipes[0] = pipes[1] = -1; return -1; } - for (int i = 0; i < 2; i++) { + for (int i = 0; i < 2; i++) mp_set_cloexec(pipes[i]); + return 0; +} +#endif + +#ifdef __MINGW32__ +int mp_make_wakeup_pipe(int pipes[2]) +{ + mp_make_cloexec_pipe(pipes); +} +#else +// create a pipe, and set it to non-blocking (and also set FD_CLOEXEC) +int mp_make_wakeup_pipe(int pipes[2]) +{ + if (mp_make_cloexec_pipe(pipes) < 0) + return -1; + + for (int i = 0; i < 2; i++) { int val = fcntl(pipes[i], F_GETFL) | O_NONBLOCK; fcntl(pipes[i], F_SETFL, val); } diff --git a/osdep/io.h b/osdep/io.h index ac1a0f90fa..c3fc0cf9cb 100644 --- a/osdep/io.h +++ b/osdep/io.h @@ -45,6 +45,7 @@ #endif bool mp_set_cloexec(int fd); +int mp_make_cloexec_pipe(int pipes[2]); int mp_make_wakeup_pipe(int pipes[2]); #ifdef _WIN32 -- cgit v1.2.3