diff options
Diffstat (limited to 'src/xine-utils/utils.c')
-rw-r--r-- | src/xine-utils/utils.c | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/xine-utils/utils.c b/src/xine-utils/utils.c index 75caaf09f..27c52c70d 100644 --- a/src/xine-utils/utils.c +++ b/src/xine-utils/utils.c @@ -35,9 +35,12 @@ #include <errno.h> #include <pwd.h> +#include <sys/types.h> #include <sys/time.h> +#include <sys/socket.h> #include <time.h> #include <unistd.h> +#include <fcntl.h> #if HAVE_EXECINFO_H #include <execinfo.h> @@ -56,6 +59,10 @@ #include <windows.h> #endif +#ifndef O_CLOEXEC +# define O_CLOEXEC 0 +#endif + typedef struct { const char language[16]; /* name of the locale */ const char encoding[16]; /* typical encoding */ @@ -717,3 +724,57 @@ char *xine_strcat_realloc (char **dest, char *append) strcat (*dest = newstr, append); return newstr; } + + +int _x_set_file_close_on_exec(int fd) +{ +#ifndef WIN32 + return fcntl(fd, F_SETFD, FD_CLOEXEC); +#else + return SetHandleInformation((HANDLE)_get_osfhandle(fd), HANDLE_FLAG_INHERIT, 0); +#endif +} + +int _x_set_socket_close_on_exec(int s) +{ +#ifndef WIN32 + return fcntl(s, F_SETFD, FD_CLOEXEC); +#else + return SetHandleInformation((HANDLE)s, HANDLE_FLAG_INHERIT, 0); +#endif +} + + +int xine_open_cloexec(const char *name, int flags) +{ + int fd = open(name, (flags | O_CLOEXEC)); + + if (fd >= 0) { + _x_set_file_close_on_exec(fd); + } + + return fd; +} + +int xine_create_cloexec(const char *name, int flags, mode_t mode) +{ + int fd = open(name, (flags | O_CREAT | O_CLOEXEC), mode); + + if (fd >= 0) { + _x_set_file_close_on_exec(fd); + } + + return fd; +} + +int xine_socket_cloexec(int domain, int type, int protocol) +{ + int s = socket(domain, type, protocol); + + if (s >= 0) { + _x_set_socket_close_on_exec(s); + } + + return s; +} + |