From c0735a977e7f565398c9bf147463ec636c2388e6 Mon Sep 17 00:00:00 2001
From: Mark Grondona <mgrondona@llnl.gov>
Date: Thu, 10 Apr 2003 20:54:33 +0000
Subject: [PATCH]  o change slurmd daemonization to not close all fds

---
 src/common/daemonize.c | 28 ++++++++++++++++++++++++----
 src/slurmd/shm.c       |  5 ++++-
 src/slurmd/slurmd.c    | 31 +++++++++++++++++++++++++++++--
 3 files changed, 57 insertions(+), 7 deletions(-)

diff --git a/src/common/daemonize.c b/src/common/daemonize.c
index a4609103380..c0ab3733c29 100644
--- a/src/common/daemonize.c
+++ b/src/common/daemonize.c
@@ -71,15 +71,35 @@ daemon(int nochdir, int noclose)
 		default: _exit(0);      /* exit parent */
 	}
 
-	if(!nochdir && chdir("/") < 0)
-		fatal("chdir(/): %m");
+	if(!nochdir && chdir("/") < 0) {
+		error("chdir(/): %m");
+		return -1;
+	}
 
+	/* Close all file descriptors if requested
+	 */
 	if (!noclose) {
 		closeall(0);
 		open("/dev/null", O_RDWR);
-		dup(0);
-		dup(0);
+		dup2(0, STDOUT_FILENO);
+		dup2(0, STDERR_FILENO);
+	} else {
+		/* 
+		 * Otherwise, dup stdin, stdout, and stderr onto /dev/null
+		 */
+		int devnull = open("/dev/null", O_RDWR);
+		if (devnull < 0)
+			error("Unable to open /dev/null: %m");
+		if (dup2(devnull, STDIN_FILENO) < 0)
+			error("Unable to dup /dev/null onto stdin: %m");
+		if (dup2(devnull, STDOUT_FILENO) < 0)
+			error("Unable to dup /dev/null onto stdout: %m");
+		if (dup2(devnull, STDERR_FILENO) < 0)
+			error("Unable to dup /dev/null onto stderr: %m");
+		if (close(devnull) < 0)
+			error("Unable to close /dev/null: %m");
 	}
+
 	return 0;
 
 }
diff --git a/src/slurmd/shm.c b/src/slurmd/shm.c
index 0fd920223a1..f79fb59b7bc 100644
--- a/src/slurmd/shm.c
+++ b/src/slurmd/shm.c
@@ -983,6 +983,8 @@ _shm_reopen()
 		return SLURM_FAILURE;
 	}
 
+	debug3("successfully attached to slurmd shm");
+
 	/* 
 	 * Lock and unlock semaphore to ensure data is initialized 
 	 */
@@ -996,6 +998,7 @@ _shm_reopen()
 	}
 
 	_shm_unlock();
+	debug3("leaving shm_init()");
 
 	return retval;
 }
@@ -1046,7 +1049,7 @@ _shm_sane(void)
 
 	sem_getvalue(shm_lock, &val);
 
-	debug("shm lock val = %d, last accessed at %s", 
+	debug3("shm lock val = %d, last accessed at %s", 
 	      val, ctime(&st.st_atime));
 
 	if ((val == 0) && ((time(NULL) - st.st_atime) > 30))
diff --git a/src/slurmd/slurmd.c b/src/slurmd/slurmd.c
index 12edcb40159..66b61699406 100644
--- a/src/slurmd/slurmd.c
+++ b/src/slurmd/slurmd.c
@@ -138,13 +138,23 @@ main (int argc, char *argv[])
 	 */
 	if (_slurmd_init() < 0) {
 		error( "slurmd initialization failed" );
+		fflush( NULL );
 		exit(1);
 	}
 
+	debug3("slurmd initialization successful");
+
+	/* 
+	 * Become a daemon if desired.
+	 * Do not chdir("/") or close all fd's
+	 */
 	if (conf->daemonize) 
-		daemon(1,0);
+		daemon(1,1);
+
+	debug3("finished daemonize");
 
 	_kill_old_slurmd();
+
 	_create_msg_socket();
 
 	conf->pid = getpid();
@@ -550,6 +560,8 @@ _create_msg_socket()
 
 	conf->lfd = ld;
 
+	debug3("succesfully opened slurm listen port %d", conf->port);
+
 	return;
 }
 
@@ -584,6 +596,13 @@ _slurmd_init()
 		setrlimit(RLIMIT_NOFILE,&rlim);
 	}
 
+#ifndef NDEBUG
+	if (getrlimit(RLIMIT_CORE, &rlim) == 0) {
+		rlim.rlim_cur = rlim.rlim_max;
+		setrlimit(RLIMIT_CORE, &rlim);
+	}
+#endif /* !NDEBUG */
+
 	/*
 	 * Create a context for verifying slurm job credentials
 	 */
@@ -599,8 +618,15 @@ _slurmd_init()
 	/*
 	 * Cleanup shared memory if so configured
 	 */
-	if (conf->shm_cleanup)
+	if (conf->shm_cleanup) {
+		/* 
+		 * Need to kill any running slurmd's here so they do
+		 *  not fail to lock shared memory on exit
+		 */
+		_kill_old_slurmd(); 
+
 		shm_cleanup();
+	}
 
 	/*
 	 * Initialize slurmd shared memory
@@ -764,6 +790,7 @@ _usage()
 static int
 _set_slurmd_spooldir(void)
 {
+	debug3("initializing slurmd spool directory");
 	if ((mkdir(conf->spooldir, 0755) < 0) && (errno != EEXIST)) {
 		error("mkdir(%s): %m", conf->spooldir);
 		return SLURM_ERROR;
-- 
GitLab