From: Andreas Huber Date: Thu, 7 Dec 2017 13:35:43 +0000 (+0100) Subject: Fix leaked file descriptor X-Git-Tag: v3.3.5~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e7899cec59193c9d7fc7c1d96d250433d130eaf6;p=thirdparty%2Fccache.git Fix leaked file descriptor --- diff --git a/ccache.c b/ccache.c index 19df19502..fcd0f2154 100644 --- a/ccache.c +++ b/ccache.c @@ -3137,6 +3137,7 @@ static void setup_uncached_err(void) { int uncached_fd = dup(2); + set_cloexec_flag(uncached_fd); if (uncached_fd == -1) { cc_log("dup(2) failed: %s", strerror(errno)); failed(); diff --git a/ccache.h b/ccache.h index 413da7f32..2a0a095e6 100644 --- a/ccache.h +++ b/ccache.h @@ -184,6 +184,7 @@ char *x_readlink(const char *path); bool read_file(const char *path, size_t size_hint, char **data, size_t *size); char *read_text_file(const char *path, size_t size_hint); char *subst_env_in_string(const char *str, char **errmsg); +void set_cloexec_flag (int fd); // ---------------------------------------------------------------------------- // stats.c diff --git a/util.c b/util.c index 8b4783a0d..9d3bba8c8 100644 --- a/util.c +++ b/util.c @@ -51,10 +51,7 @@ init_log(void) if (logfile) { #ifndef _WIN32 int fd = fileno(logfile); - int flags = fcntl(fd, F_GETFD, 0); - if (flags >= 0) { - fcntl(fd, F_SETFD, flags | FD_CLOEXEC); - } + set_cloexec_flag(fd); #endif return true; } else { @@ -1187,6 +1184,7 @@ create_tmp_fd(char **fname) fatal("Failed to create temporary file for %s: %s", *fname, strerror(errno)); } + set_cloexec_flag(fd); #ifndef _WIN32 fchmod(fd, 0666 & ~get_umask()); @@ -1662,3 +1660,15 @@ subst_env_in_string(const char *str, char **errmsg) reformat(&result, "%s%.*s", result, (int)(q - p), p); return result; } + +void +set_cloexec_flag (int fd) +{ +#ifndef _WIN32 + int flags = fcntl(fd, F_GETFD, 0); + if (flags >= 0) { + fcntl(fd, F_SETFD, flags | FD_CLOEXEC); + } +#endif +} +