multipathd: Disassociate from /dev/console when daemonizing
authorAnkit Jain <jankit@suse.de>
Wed, 11 May 2011 17:44:24 +0000 (23:14 +0530)
committerChristophe Varoqui <christophe.varoqui@opensvc.com>
Mon, 16 May 2011 18:10:32 +0000 (20:10 +0200)
Currently, multipathd redirects stdout/stderr to /dev/console,
to be able to show error messages when all else fails.

This patch redirects them to /dev/null instead, to make it a
well behaving daemon. This corresponds to FATE request
https://fate.novell.com/310684 .

This is based on -
git://git.kernel.org/pub/scm/linux/storage/multipath-tools/

----
commit a3dd997d1b0b55a934103e0b28648003675743e3
Author: Ankit Jain <jankit@suse.de>
Date:   Wed May 11 22:18:33 2011 +0530

    multipathd: Disassociate from /dev/console

    When daemonizing, redirect stdout/stderr to /dev/null
    instead of /dev/console. Also, chdir("/") before the redirection,
    so that any error can be reported.
    FATE#310684.

multipathd/main.c

index e8eee9e5ce54f3d0684b1f4107d778ba21e74cca..cb77c82f21270553bc6d0246914564b80eeb29f1 100644 (file)
@@ -1540,7 +1540,7 @@ static int
 daemonize(void)
 {
        int pid;
-       int in_fd, out_fd;
+       int dev_null_fd;
 
        if( (pid = fork()) < 0){
                fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
@@ -1556,31 +1556,24 @@ daemonize(void)
        else if (pid != 0)
                _exit(0);
 
-       in_fd = open("/dev/null", O_RDONLY);
-       if (in_fd < 0){
-               fprintf(stderr, "cannot open /dev/null for input : %s\n",
-                       strerror(errno));
-               _exit(0);
-       }
-       out_fd = open("/dev/console", O_WRONLY);
-       if (out_fd < 0){
-               fprintf(stderr, "cannot open /dev/console for output : %s\n",
+       if (chdir("/") < 0)
+               fprintf(stderr, "cannot chdir to '/', continuing\n");
+
+       dev_null_fd = open("/dev/null", O_RDWR);
+       if (dev_null_fd < 0){
+               fprintf(stderr, "cannot open /dev/null for input & output : %s\n",
                        strerror(errno));
                _exit(0);
        }
 
        close(STDIN_FILENO);
-       dup(in_fd);
+       dup(dev_null_fd);
        close(STDOUT_FILENO);
-       dup(out_fd);
+       dup(dev_null_fd);
        close(STDERR_FILENO);
-       dup(out_fd);
-
-       close(in_fd);
-       close(out_fd);
-       if (chdir("/") < 0)
-               fprintf(stderr, "cannot chdir to '/', continuing\n");
+       dup(dev_null_fd);
 
+       close(dev_null_fd);
        return 0;
 }