Skip to contentSkip to conversation

ocfs2-tools: ocfs2_controld leaves std(in|out|err) unassigned

PATCH New Andrew Beekhof 2 participants 2 messages Nov 18, 2008 OCFS2

lore
Share & tools
lore
#0 lore raw

PATCH 2/3 ocfs2-tools: ocfs2_controld leaves std(in|out|err) unassigned

Leaving std(in|out|err) unassigned causes havoc when included code (in this case openais libraries) print to std(err|out). Specifically, this often leads to memory corruption since the output is sent to IPC sockets.

The safest approach is to reopen std(in|out|err) and have them point to /dev/null

ocfs2_controld/main.c +13 -0
@@ -1027,6 +1027,7 @@ static void lockfile(void)
 
  static void daemonize(void)
  {
+	int fd;
  	pid_t pid = fork();
  	if (pid < 0) {
  		perror("main: cannot fork");
@@ -1040,6 +1041,18 @@ static void daemonize(void)
  	close(0);
  	close(1);
  	close(2);
+	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);
+		}
+	}
  	openlog("ocfs2_controld", LOG_PID, LOG_DAEMON);
 
  	lockfile();
#1 lore raw

PATCH 2/3 ocfs2-tools: ocfs2_controld leaves std(in|out|err) unassigned

Quoted 9 lines · Andrew Beekhof

Leaving std(in|out|err) unassigned causes havoc when included code (in this case openais libraries) print to std(err|out). Specifically, this often leads to memory corruption since the output is sent to IPC sockets.

The safest approach is to reopen std(in|out|err) and have them point to /dev/null

Quoted 36 lines
ocfs2_controld/main.c +13 -0
@@ -1027,6 +1027,7 @@ static void lockfile(void)
 
  static void daemonize(void)
  {
+	int fd;
  	pid_t pid = fork();
  	if (pid < 0) {
  		perror("main: cannot fork");
@@ -1040,6 +1041,18 @@ static void daemonize(void)
  	close(0);
  	close(1);
  	close(2);
+	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);
+		}
+	}
  	openlog("ocfs2_controld", LOG_PID, LOG_DAEMON);
 
  	lockfile();

_______________________________________________
Ocfs2-devel mailing list
Ocfs2-devel at oss.oracle.com
http://oss.oracle.com/mailman/listinfo/ocfs2-devel

Signature · 8 lines
"Only a life lived for others is a life worth while."
							-Albert Einstein  

Joel Becker
Principal Software Developer
Oracle
E-mail: joel.becker at oracle.com
Phone: (650) 506-8127