[FIX] Fix access to logfile by forked application
authorDmitry Bogatov <d.bogatov@samsung.com>
Thu, 14 Nov 2013 12:47:31 +0000 (16:47 +0400)
committerGerrit Code Review <gerrit@gerrit.vlan144.tizendev.org>
Mon, 18 Nov 2013 09:15:41 +0000 (09:15 +0000)
Change-Id: Ic6678130a8853dc3f946c9e6b92e6b7b4ab7d61a
Signed-off-by: Dmitry Bogatov <d.bogatov@samsung.com>
daemon/da_debug.c

index b4af13d2376c75ef3e9320662864a9550307ff1a..b98aab8954ca37acb1a7f355bfa25816eda81ef0 100644 (file)
@@ -28,7 +28,7 @@
  * - Samsung RnD Institute Russia
  *
  */
-
+#define _GNU_SOURCE
 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #define DEBUG_LOGFILE          "/tmp/daemonlog.da"
 
 #if DEBUG
+static inline void close_on_exec_dup(int old, int new)
+{
+       dup2(old, new);
+       fcntl(new, F_SETFD, fcntl(new, F_GETFD) | FD_CLOEXEC);
+}
+
 void initialize_log()
 {
-       int fd = open(DEBUG_LOGFILE, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC,
-                     0777);
+       int fd = open(DEBUG_LOGFILE, O_WRONLY | O_CREAT | O_TRUNC, 0777);
        if (fd > 0) {
-               dup2(fd, 1);
-               dup2(fd, 2);
+               close_on_exec_dup(fd, 1);
+               close_on_exec_dup(fd, 2);
+
                close(fd);
        } else {
                close(1);