diff options
author | Chris Rankin <rankincj@yahoo.com> | 2011-10-01 14:14:27 +0100 |
---|---|---|
committer | Chris Rankin <rankincj@yahoo.com> | 2011-10-01 14:14:27 +0100 |
commit | 62a669d9f04e83debe729347617a935707badc7e (patch) | |
tree | 3d71d5520db6c429666abf1823d2d20b082af97d /src/xine-utils/utils.c | |
parent | 8ba50d7512e71ce2fa441f8e738459df21cec2ae (diff) | |
download | xine-lib-62a669d9f04e83debe729347617a935707badc7e.tar.gz xine-lib-62a669d9f04e83debe729347617a935707badc7e.tar.bz2 |
Mark simple file and socket descriptors as uninheritable.
This patch creates two utility functions:
int open_cloexec(pathname, flags)
int create_cloexec(pathname, flags, mode)
These return a file descriptor with the CLOEXEC flag set, to ensure
that the descriptor is not inherited across a fork/exec operation.
The sockets returned by:
_x_io_tcp_connect_ipv4()
_x_io_tcp_connect()
now also have their CLOEXEC flag set.
Diffstat (limited to 'src/xine-utils/utils.c')
-rw-r--r-- | src/xine-utils/utils.c | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/xine-utils/utils.c b/src/xine-utils/utils.c index 2f62c95a8..f5d5a0ff3 100644 --- a/src/xine-utils/utils.c +++ b/src/xine-utils/utils.c @@ -38,6 +38,7 @@ #include <sys/time.h> #include <time.h> #include <unistd.h> +#include <fcntl.h> #if HAVE_EXECINFO_H #include <execinfo.h> @@ -56,6 +57,10 @@ #include <windows.h> #endif +#ifndef O_CLOEXEC +# define O_CLOEXEC 0 +#endif + typedef struct { char *language; /* name of the locale */ char *encoding; /* typical encoding */ @@ -697,3 +702,37 @@ char *xine_strcat_realloc (char **dest, char *append) strcat (*dest = newstr, append); return newstr; } + + +static int set_close_on_execute(int fd) +{ +#ifndef WIN32 + return fcntl(fd, F_SETFD, FD_CLOEXEC); +#else + return SetHandleInformation((HANDLE)_get_osfhandle(fd), HANDLE_FLAG_INHERIT, 0); +#endif +} + + +int open_cloexec(const char *name, int flags) +{ + int fd = open(name, (flags | O_CLOEXEC)); + + if (fd >= 0) { + set_close_on_execute(fd); + } + + return fd; +} + +int create_cloexec(const char *name, int flags, mode_t mode) +{ + int fd = open(name, (flags | O_CREAT | O_CLOEXEC), mode); + + if (fd >= 0) { + set_close_on_execute(fd); + } + + return fd; +} + |