# HG changeset patch # User Paolo Bonzini # Date 1250840174 -7200 # Node ID c41f4ce30e3696cd9ecf6dd5ffae1b43459e739a # Parent 27f6d88f60b178199fe6a6bcf69ebfda3f260f81 popen-safer: test O_CLOEXEC at run-time. * lib/popen-safer.c: Test O_CLOEXEC at run-time. diff --git a/ChangeLog b/ChangeLog --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2009-08-21 Paolo Bonzini + + popen-safer: test O_CLOEXEC at run-time. + * lib/popen-safer.c: Test O_CLOEXEC at run-time. + 2009-08-21 Paolo Bonzini fcntl: move more flags to the header diff --git a/lib/popen-safer.c b/lib/popen-safer.c --- a/lib/popen-safer.c +++ b/lib/popen-safer.c @@ -27,18 +27,28 @@ #include "cloexec.h" -#ifndef O_CLOEXEC -# define O_CLOEXEC 0 -#endif - /* Like open (name, flags | O_CLOEXEC), although not necessarily atomic. FLAGS must not include O_CREAT. */ static int open_noinherit (char const *name, int flags) { - int fd = open (name, flags | O_CLOEXEC); - if (0 <= fd && !O_CLOEXEC && set_cloexec_flag (fd, true) != 0) + int fd; +#ifdef O_CLOEXEC + /* 0 = unknown, 1 = yes, -1 = no. */ + static int have_cloexec; + if (have_cloexec >= 0) + { + fd = open (name, flags | O_CLOEXEC); + if (have_cloexec == 0 && (0 <= fd || errno == EINVAL)) + have_cloexec = (0 <= fd ? 1 : -1); + if (have_cloexec == 1) + return fd; + } +#endif + + fd = open (name, flags); + if (0 <= fd && set_cloexec_flag (fd, true) != 0) { int saved_errno = errno; close (fd);