Просмотр исходного кода

[PATCH] Replace freopen with open/dup2 when daemonizing

This patch replaces the existing freopen method of
forcing stdin/out/err to /dev/null with the more
usual system of open/dup2.

While I don't like posting patches I don't fully understand,
this patch seems to fix a problem where stdout/err get
assigned to a socket causing double logging output
on systemd.

Signed-off-by: Christine Caulfield <ccaulfie@redhat.com>
Reviewed-by: Jan Friesse <jfriesse@redhat.com>
Christine Caulfield 12 лет назад
Родитель
Сommit
8567887abb
1 измененных файлов с 6 добавлено и 9 удалено
  1. 6 9
      exec/main.c

+ 6 - 9
exec/main.c

@@ -398,7 +398,7 @@ static void priv_drop (void)
 
 static void corosync_tty_detach (void)
 {
-	FILE *r;
+	int devnull;
 
 	/*
 	 * Disconnect from TTY if this is not a debug run
@@ -424,16 +424,13 @@ static void corosync_tty_detach (void)
 	/*
 	 * Map stdin/out/err to /dev/null.
 	 */
-	r = freopen("/dev/null", "r", stdin);
-	if (r == NULL) {
+	devnull = open("/dev/null", O_RDWR);
+	if (devnull == -1) {
 		corosync_exit_error (COROSYNC_DONE_STD_TO_NULL_REDIR);
 	}
-	r = freopen("/dev/null", "a", stderr);
-	if (r == NULL) {
-		corosync_exit_error (COROSYNC_DONE_STD_TO_NULL_REDIR);
-	}
-	r = freopen("/dev/null", "a", stdout);
-	if (r == NULL) {
+
+	if (dup2(devnull, 0) < 0 || dup2(devnull, 1) < 0
+	    || dup2(devnull, 2) < 0) {
 		corosync_exit_error (COROSYNC_DONE_STD_TO_NULL_REDIR);
 	}
 }