[stgt] [PATCH 09/15] Fix tgtd startup

Arne Redlich arne.redlich at googlemail.com
Tue Jun 9 18:22:30 CEST 2009


tgtd was deamonized and forked the logger daemon quite late during startup,
leading to the logger getting ahold of resources (IPC fd) it should not
have.

This also fixes the working dir and closes the std{in,out,err} file descriptors of
the logger daemon.

Signed-off-by: Arne Redlich <arne.redlich at googlemail.com>
---
 usr/log.c  |   20 ++++++++++++++++++++
 usr/tgtd.c |   28 ++++++++++++++--------------
 2 files changed, 34 insertions(+), 14 deletions(-)

diff --git a/usr/log.c b/usr/log.c
index 056314a..cf7e905 100644
--- a/usr/log.c
+++ b/usr/log.c
@@ -17,6 +17,7 @@
  * 02110-1301 USA
  */
 #include <ctype.h>
+#include <fcntl.h>
 #include <string.h>
 #include <stdarg.h>
 #include <stdio.h>
@@ -27,6 +28,7 @@
 #include <errno.h>
 #include <sys/shm.h>
 #include <sys/ipc.h>
+#include <sys/stat.h>
 #include <sys/types.h>
 #include <sys/wait.h>
 
@@ -340,6 +342,7 @@ int log_init(char *program_name, int size, int daemon, int debug)
 	if (daemon) {
 		struct sigaction sa_old;
 		struct sigaction sa_new;
+		int fd;
 
 		openlog(log_name, 0, LOG_DAEMON);
 		setlogmask (LOG_UPTO (LOG_DEBUG));
@@ -360,6 +363,23 @@ int log_init(char *program_name, int size, int daemon, int debug)
 			return 0;
 		}
 
+		fd = open("/dev/null", O_RDWR);
+		if (fd < 0) {
+			syslog(LOG_ERR, "failed to open /dev/null: %s\n",
+			       strerror(errno));
+			exit(1);
+		}
+
+		dup2(fd, 0);
+		dup2(fd, 1);
+		dup2(fd, 2);
+		setsid();
+		if (chdir("/") < 0) {
+			syslog(LOG_ERR, "failed to chdir to '/': %s\n",
+			       strerror(errno));
+			exit(1);
+		}
+
 		/* flush on daemon's crash */
 		sa_new.sa_handler = (void*)log_flush;
 		sigemptyset(&sa_new.sa_mask);
diff --git a/usr/tgtd.c b/usr/tgtd.c
index 945be31..3e0d400 100644
--- a/usr/tgtd.c
+++ b/usr/tgtd.c
@@ -348,19 +348,7 @@ int main(int argc, char **argv)
 		}
 	}
 
-	ep_fd = epoll_create(4096);
-	if (ep_fd < 0) {
-		fprintf(stderr, "can't create epoll fd, %m\n");
-		exit(1);
-	}
-
-	nr_lld = lld_init(&use_kernel, argv[optind]);
-	if (!nr_lld) {
-		fprintf(stderr, "No available low level driver!\n");
-		exit(1);
-	}
-
-	err = ipc_init();
+	err = log_init(program_name, LOG_SPACE_SIZE, is_daemon, is_debug);
 	if (err)
 		exit(1);
 
@@ -375,7 +363,19 @@ int main(int argc, char **argv)
 	if (err)
 		exit(1);
 
-	err = log_init(program_name, LOG_SPACE_SIZE, is_daemon, is_debug);
+	ep_fd = epoll_create(4096);
+	if (ep_fd < 0) {
+		fprintf(stderr, "can't create epoll fd, %m\n");
+		exit(1);
+	}
+
+	nr_lld = lld_init(&use_kernel, argv[optind]);
+	if (!nr_lld) {
+		fprintf(stderr, "No available low level driver!\n");
+		exit(1);
+	}
+
+	err = ipc_init();
 	if (err)
 		exit(1);
 
-- 
1.6.0.4



--
To unsubscribe from this list: send the line "unsubscribe stgt" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html



More information about the stgt mailing list