[PATCH v2 weston] log: Open log file CLOEXEC so child processes don't get the fd

Derek Foreman derekf at osg.samsung.com
Mon Jun 8 09:37:31 PDT 2015


Reviewed-by: Pekka Paalanen <pekka.paalanen at collabora.co.uk>
Signed-off-by: Derek Foreman <derekf at osg.samsung.com>
---

Nested the os_fd_set_cloexec() call so calling weston_log_file_open() multiple
times behaves as expected.

 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 611e7c8..d0ff1c8 100644
--- a/shared/os-compatibility.c
+++ b/shared/os-compatibility.c
@@ -33,8 +33,8 @@
 
 #include "os-compatibility.h"
 
-static int
-set_cloexec_or_close(int fd)
+int
+os_fd_set_cloexec(int fd)
 {
 	long flags;
 
@@ -43,16 +43,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 172bb7e..60ae7fd 100644
--- a/shared/os-compatibility.h
+++ b/shared/os-compatibility.h
@@ -38,6 +38,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);
 
 int
diff --git a/src/log.c b/src/log.c
index 99bbe18..0dae3b2 100644
--- a/src/log.c
+++ b/src/log.c
@@ -33,6 +33,8 @@
 
 #include "compositor.h"
 
+#include "os-compatibility.h"
+
 static FILE *weston_logfile = NULL;
 
 static int cached_tm_mday = -1;
@@ -74,8 +76,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;
-- 
2.1.4



More information about the wayland-devel mailing list