Skip to contentSkip to conversation

multipathd: Disassociate from /dev/console when daemonizing

PATCH New Ankit Jain 1 participant 1 message May 11, 2011 Device mapper

lore
Share & tools
lore

Ankit Jain

author
#0 lore raw

PATCH 1/1 multipathd: Disassociate from /dev/console when daemonizing

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 <[email protected]>
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 +11 -18
index e8eee9e..cb77c82 100644
@@ -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;
 }
 
Signature · 2 lines
Ankit Jain
SUSE Labs