server: call event_add_fd at the end of ctdb_set_child_logging()
authorStefan Metzmacher <metze@samba.org>
Thu, 7 Jan 2010 12:29:09 +0000 (13:29 +0100)
committerStefan Metzmacher <metze@samba.org>
Tue, 12 Jan 2010 11:20:15 +0000 (12:20 +0100)
metze

server/ctdb_logging.c

index 9d09aa2470428c322a9cbead6c2ee5aa6d66ecb9..56dcfa28576ae85567a96c9cb5e32274511ea9c5 100644 (file)
@@ -486,9 +486,9 @@ free_log:
 int ctdb_set_child_logging(struct ctdb_context *ctdb)
 {
        int p[2];
-       int ret;
+       int old_stdout, old_stderr;
 
-       if (ctdb->log->fd == 1) {
+       if (ctdb->log->fd == STDOUT_FILENO) {
                /* not needed for stdout logging */
                return 0;
        }
@@ -499,31 +499,39 @@ int ctdb_set_child_logging(struct ctdb_context *ctdb)
                return -1;
        }
 
+       /* We'll fail if stderr/stdout not already open; it's simpler. */
+       old_stdout = dup(STDOUT_FILENO);
+       old_stderr = dup(STDERR_FILENO);
+       if (dup2(p[1], STDOUT_FILENO) < 0 || dup2(p[1], STDERR_FILENO) < 0) {
+               int saved_errno = errno;
+               dup2(old_stdout, STDOUT_FILENO);
+               dup2(old_stderr, STDERR_FILENO);
+               close(old_stdout);
+               close(old_stderr);
+               close(p[0]);
+               close(p[1]);
+               errno = saved_errno;
+
+               printf(__location__ " dup2 failed: %s\n",
+                       strerror(errno));
+               return -1;
+       }
+       close(p[1]);
+       close(old_stdout);
+       close(old_stderr);
+
+       /* Is this correct for STDOUT and STDERR ? */
+       set_close_on_exec(STDOUT_FILENO);
+       set_close_on_exec(STDERR_FILENO);
+       set_close_on_exec(p[0]);
+
        event_add_fd(ctdb->ev, ctdb->log, p[0],
                     EVENT_FD_READ | EVENT_FD_AUTOCLOSE,
                     ctdb_log_handler, ctdb->log);
-       set_close_on_exec(p[0]);
        ctdb->log->pfd = p[0];
 
        DEBUG(DEBUG_NOTICE, (__location__ " Created PIPE FD:%d for logging\n", p[0]));
 
-       close(1);
-       close(2);
-       if (p[1] != 1) {
-               ret = dup2(p[1], 1);
-               if (ret == -1) {
-                       printf("dup2 failed: %s\n", strerror(errno));
-                       return -1;
-               }
-               close(p[1]);
-       }
-       /* also catch stderr of subcommands to the log */
-       ret = dup2(1, 2);
-       if (ret == -1) {
-               printf("dup2 failed: %s\n", strerror(errno));
-               return -1;
-       }
-
        return 0;
 }