From 6bc33d63cfc0799b85a8e62c3657b5a13de5a78a Mon Sep 17 00:00:00 2001 From: Derek Foreman Date: Mon, 8 Jun 2015 11:37:31 -0500 Subject: [PATCH] log: Open log file CLOEXEC so child processes don't get the fd Reviewed-by: Pekka Paalanen Reviewed-by: Bryce Harrington Signed-off-by: Derek Foreman --- shared/os-compatibility.c | 22 ++++++++++++++-------- shared/os-compatibility.h | 3 +++ src/log.c | 7 ++++++- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/shared/os-compatibility.c b/shared/os-compatibility.c index b903347f..05999745 100644 --- a/shared/os-compatibility.c +++ b/shared/os-compatibility.c @@ -36,8 +36,8 @@ #include "os-compatibility.h" -static int -set_cloexec_or_close(int fd) +int +os_fd_set_cloexec(int fd) { long flags; @@ -46,16 +46,22 @@ set_cloexec_or_close(int fd) flags = fcntl(fd, F_GETFD); if (flags == -1) - goto err; + return -1; if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) - goto err; + return -1; - return fd; + return 0; +} -err: - close(fd); - return -1; +static int +set_cloexec_or_close(int fd) +{ + if (os_fd_set_cloexec(fd) != 0) { + close(fd); + return -1; + } + return fd; } int diff --git a/shared/os-compatibility.h b/shared/os-compatibility.h index a12ac0bd..06d25748 100644 --- a/shared/os-compatibility.h +++ b/shared/os-compatibility.h @@ -40,6 +40,9 @@ backtrace(void **buffer, int size) } #endif +int +os_fd_set_cloexec(int fd); + int os_socketpair_cloexec(int domain, int type, int protocol, int *sv); diff --git a/src/log.c b/src/log.c index 535bcba8..0302c8ca 100644 --- a/src/log.c +++ b/src/log.c @@ -36,6 +36,8 @@ #include "compositor.h" +#include "os-compatibility.h" + static FILE *weston_logfile = NULL; static int cached_tm_mday = -1; @@ -77,8 +79,11 @@ weston_log_file_open(const char *filename) { wl_log_set_handler_server(custom_handler); - if (filename != NULL) + if (filename != NULL) { weston_logfile = fopen(filename, "a"); + if (weston_logfile) + os_fd_set_cloexec(fileno(weston_logfile)); + } if (weston_logfile == NULL) weston_logfile = stderr;