diff --git a/common/log.c b/common/log.c index 39ffb779..07565668 100644 --- a/common/log.c +++ b/common/log.c @@ -73,12 +73,10 @@ internal_log_file_open(const char *fname) } } -#ifdef FD_CLOEXEC if (ret != -1) { - fcntl(ret, F_SETFD, FD_CLOEXEC); + g_file_set_cloexec(ret, 1); } -#endif return ret; } diff --git a/common/os_calls.c b/common/os_calls.c index e53f4df4..ba8eaf60 100644 --- a/common/os_calls.c +++ b/common/os_calls.c @@ -2249,6 +2249,49 @@ g_file_lock(int fd, int start, int len) #endif } +/*****************************************************************************/ +/* Gets the close-on-exec flag for a file descriptor */ +int +g_file_get_cloexec(int fd) +{ + int rv = 0; + int flags = fcntl(fd, F_GETFD); + if (flags >= 0 && (flags & FD_CLOEXEC) != 0) + { + rv = 1; + } + + return rv; +} + +/*****************************************************************************/ +/* Sets/clears the close-on-exec flag for a file descriptor */ +/* return boolean */ +int +g_file_set_cloexec(int fd, int status) +{ + int rv = 0; + int current_flags = fcntl(fd, F_GETFD); + if (current_flags >= 0) + { + int new_flags; + if (status) + { + new_flags = current_flags | FD_CLOEXEC; + } + else + { + new_flags = current_flags & ~FD_CLOEXEC; + } + if (new_flags != current_flags) + { + rv = (fcntl(fd, F_SETFD, new_flags) >= 0); + } + } + + return rv; +} + /*****************************************************************************/ /* Converts a hex mask to a mode_t value */ #if !defined(_WIN32) diff --git a/common/os_calls.h b/common/os_calls.h index 3892237f..e9dcb4d9 100644 --- a/common/os_calls.h +++ b/common/os_calls.h @@ -215,6 +215,8 @@ int g_file_write(int fd, const char *ptr, int len); int g_file_seek(int fd, int offset); int g_file_lock(int fd, int start, int len); int g_file_duplicate_on(int fd, int target_fd); +int g_file_get_cloexec(int fd); +int g_file_set_cloexec(int fd, int status); int g_chmod_hex(const char *filename, int flags); int g_umask_hex(int flags); int g_chown(const char *name, int uid, int gid);