Benefits:

* Calling setsid() creates a new process group, which is probably
something you actually want with aisexec.

* Mapping 0/1/2 to /dev/null prevents printfs in the code from causing
problems (even if they're not "supposed" to be there).  For example, if
code at some point calls printf() - but file descriptor 1 is an IPC file
descriptor - bad things happen, I suspect.

Notes:

While POSIX defines /dev/null to exist and be a character device, a more
complete patch would check for those properties.

setsid() should probably have its return code checked, but I'm not sure
if doing so would upset things; does it matter (since openais doesn't
call setsid() now) if it fails?

-- Lon
Index: exec/main.c
===================================================================
--- exec/main.c	(revision 1480)
+++ exec/main.c	(working copy)
@@ -280,7 +280,7 @@
 
 static void aisexec_tty_detach (void)
 {
-	int lpc;
+	int lpc, fd;
 	struct rlimit oflimits;
 
 	/*
@@ -307,6 +307,24 @@
 			exit (0);
 			break;
 	}
+
+	/* Create new session */
+	setsid();
+
+	/* 
+	 * Map stdin/out/err to /dev/null.
+	 */
+	fd = open("/dev/null", O_RDWR);
+	if (fd >= 0) {
+		/* dup2 to 0 / 1 / 2 (stdin / stdout / stderr) */
+		dup2(fd, STDIN_FILENO);  /* 0 */
+		dup2(fd, STDOUT_FILENO); /* 1 */
+		dup2(fd, STDERR_FILENO); /* 2 */
+
+		/* Should be 0, but just in case it isn't... */
+		if (fd > 2) 
+			close(fd);
+	}
 }
 
 static void aisexec_setscheduler (void)
_______________________________________________
Openais mailing list
[email protected]
https://lists.linux-foundation.org/mailman/listinfo/openais

Reply via email to