From b2b1ac768e10db47da5cbc072e920db1aebf2828 Mon Sep 17 00:00:00 2001
From: Mark Grondona <mgrondona@llnl.gov>
Date: Tue, 9 Dec 2003 21:11:32 +0000
Subject: [PATCH]  o new slurm env var set by slurmd for job steps:
 SLURM_LAUNCH_NODE_IPADDR  o more fixes for setenvpf() corner cases  o move
 tmpdir logic up so it is not executed unecessarily for every task.  o add
 getenvp() call to setenvpf.c

---
 src/slurmd/job.c      |  3 +--
 src/slurmd/mgr.c      | 24 ++++++++++++++++++++++++
 src/slurmd/setenvpf.c | 32 +++++++++++++++++++++++++++++++-
 src/slurmd/setenvpf.h |  3 ++-
 src/slurmd/smgr.c     | 26 ++++++++++++++++++--------
 src/slurmd/ulimits.c  | 20 +-------------------
 6 files changed, 77 insertions(+), 31 deletions(-)

diff --git a/src/slurmd/job.c b/src/slurmd/job.c
index d4d1c4ef17a..bad06be20f5 100644
--- a/src/slurmd/job.c
+++ b/src/slurmd/job.c
@@ -136,7 +136,6 @@ job_create(launch_tasks_request_msg_t *msg, slurm_addr *cli_addr)
 	memcpy(&io_addr,   cli_addr, sizeof(slurm_addr));
 	slurm_set_addr(&io_addr,   msg->io_port,   NULL); 
 
-
 #ifdef HAVE_ELAN
 	job->qsw_job = msg->qsw_job;
 #endif
@@ -320,7 +319,7 @@ _array_copy(int n, char **src)
 {
 	char **dst = xmalloc((n+1) * sizeof(char *));
 	dst[n] = NULL;
-	while (--n >= 0)
+	while ((--n >= 0) && (src[n] != NULL))
 		dst[n] = xstrdup(src[n]);
 	return dst;
 }
diff --git a/src/slurmd/mgr.c b/src/slurmd/mgr.c
index 904eefd1f9d..514d1f11edf 100644
--- a/src/slurmd/mgr.c
+++ b/src/slurmd/mgr.c
@@ -61,6 +61,7 @@
 #include "src/common/xsignal.h"
 #include "src/common/xstring.h"
 #include "src/common/xmalloc.h"
+#include "src/common/util-net.h"
 
 #include "src/slurmd/mgr.h"
 
@@ -126,6 +127,7 @@ static void _set_unexited_task_status(slurmd_job_t *job, int status);
 static int  _send_pending_exit_msgs(slurmd_job_t *job);
 
 static void _setargs(slurmd_job_t *job);
+static void _set_launch_ip_in_env(slurmd_job_t *, slurm_addr *cli);
 
 static void _random_sleep(slurmd_job_t *job);
 
@@ -157,6 +159,8 @@ mgr_launch_tasks(launch_tasks_request_msg_t *msg, slurm_addr *cli)
 
 	_setargs(job);
 
+	_set_launch_ip_in_env(job, cli);
+
 	if (_job_mgr(job) < 0)
 		return SLURM_ERROR;
 
@@ -1091,3 +1095,23 @@ _setargs(slurmd_job_t *job)
 	return;
 }
 
+static void
+_set_launch_ip_in_env(slurmd_job_t *job, slurm_addr *cli)
+{
+	char *p;
+	char addrbuf[INET_ADDRSTRLEN];
+
+	slurm_print_slurm_addr (cli, addrbuf, INET_ADDRSTRLEN);
+
+	/* 
+	 *  XXX: Eventually, need a function for slurm_addrs that
+	 *   returns just the IP address (not addr:port)
+	 */   
+
+	if ((p = strchr (addrbuf, ':')) != NULL)
+		*p = '\0';
+
+	setenvpf (&job->env, "SLURM_LAUNCH_NODE_IPADDR", "%s", addrbuf);
+	return;
+}
+
diff --git a/src/slurmd/setenvpf.c b/src/slurmd/setenvpf.c
index 46340ac787a..0e269c4c346 100644
--- a/src/slurmd/setenvpf.c
+++ b/src/slurmd/setenvpf.c
@@ -75,10 +75,23 @@ _find_name_in_env(char **env, const char *name)
 static char **
 _extend_env(char ***envp)
 {
+	char **ep;
 	size_t newcnt = (xsize (*envp) / sizeof (char *)) + 1;
+
 	*envp = xrealloc (*envp, newcnt * sizeof (char *));
+
 	(*envp)[newcnt - 1] = NULL;
-	return (&((*envp)[newcnt - 2]));
+	ep = &((*envp)[newcnt - 2]);
+
+	/* 
+	 *  Find last non-NULL entry
+	 */
+	while (*ep == NULL)
+		--ep;
+
+	info ("resized to %d, last = %s", newcnt, *ep);
+
+	return (++ep);
 }
 
 int 
@@ -133,3 +146,20 @@ unsetenvp(char **env, const char *name)
 	return;
 }
 
+char *
+getenvp(char **env, const char *name)
+{
+	size_t len = strlen(name);
+	char **ep;
+
+	if ((env == NULL) || (env[0] == '\0'))
+		return (NULL);
+
+	ep = _find_name_in_env (env, name);
+
+	if (*ep != NULL)
+		return (&(*ep)[len+1]);
+
+	return NULL;
+}
+
diff --git a/src/slurmd/setenvpf.h b/src/slurmd/setenvpf.h
index 85a16d9ac5d..c8667d936d7 100644
--- a/src/slurmd/setenvpf.h
+++ b/src/slurmd/setenvpf.h
@@ -27,6 +27,7 @@
 #define _SETENVPF_H
 
 int setenvpf(char ***envp, const char *name, const char *fmt, ...);
-int unsetenvp(char **envp, const char *name);
+int unsetenvp(char **env, const char *name);
+char * getenvp(char **env, const char *name);
 
 #endif
diff --git a/src/slurmd/smgr.c b/src/slurmd/smgr.c
index 0290f854ad9..6f98543f74c 100644
--- a/src/slurmd/smgr.c
+++ b/src/slurmd/smgr.c
@@ -77,6 +77,7 @@ static void  _session_mgr(slurmd_job_t *job);
 static int   _exec_all_tasks(slurmd_job_t *job);
 static void  _exec_task(slurmd_job_t *job, int i);
 static int   _become_user(slurmd_job_t *job);
+static void  _make_tmpdir(slurmd_job_t *job);
 static int   _child_exited(void);
 static void  _wait_for_all_tasks(slurmd_job_t *job);
 static int   _local_taskid(slurmd_job_t *job, pid_t pid);
@@ -169,6 +170,8 @@ _session_mgr(slurmd_job_t *job)
 		exit(5);
 	}
 
+	_make_tmpdir(job);
+
 	if (_exec_all_tasks(job) < 0) {
 		debug("exec_all_tasks failed");
 		exit(6);
@@ -511,7 +514,6 @@ static int
 _setup_env(slurmd_job_t *job, int taskid)
 {
 	task_info_t *t = job->task[taskid];
-	char *tmpdir;
 
 	if (setenvpf(&job->env, "SLURM_NODEID",       "%d", job->nodeid) < 0)
 		return -1;
@@ -519,17 +521,25 @@ _setup_env(slurmd_job_t *job, int taskid)
 		return -1;
 	if (setenvpf(&job->env, "SLURM_PROCID",       "%d", t->gid     ) < 0)
 		return -1;
-	if ((tmpdir = getenv("TMPDIR"))) {
-		int rc = mkdir(tmpdir, 0700);
-		if ((rc < 0) && (errno != EEXIST)) {
-			error("can not make TMPDIR %s: %m", tmpdir);
-			return -1;
-		}
-	}
 
 	return SLURM_SUCCESS;
 }
 
+static void
+_make_tmpdir(slurmd_job_t *job)
+{
+	char *tmpdir;
+
+	if (!(tmpdir = getenvp(job->env, "TMPDIR")))
+		return;
+
+	if ((mkdir(tmpdir, 0700) < 0) && (errno != EEXIST))
+		error ("Unable to create TMPDIR [%s]: %m", tmpdir);
+
+	return;
+}
+
+
 /*
  * Prepare task for parallel debugger attach
  */
diff --git a/src/slurmd/ulimits.c b/src/slurmd/ulimits.c
index ca3155f99cd..b8e6a0622f7 100644
--- a/src/slurmd/ulimits.c
+++ b/src/slurmd/ulimits.c
@@ -62,7 +62,6 @@ static struct userlim ulims[] =
  * Prototypes:
  *
  */
-static char * _getenvp(char **env, const char *name);
 static long   _get_env_val(char **env, const char *name);
 static int    _set_limit(char **env, struct userlim *ulim);
 
@@ -130,7 +129,7 @@ _get_env_val(char **env, const char *name)
 	xassert(env  != NULL);
 	xassert(name != NULL);
 
-	if(!(val = _getenvp(env, name))) 
+	if(!(val = getenvp(env, name))) 
 		return -2L;
 
 	retval = strtol(val, &p, 10);
@@ -143,21 +142,4 @@ _get_env_val(char **env, const char *name)
 	return retval;
 }
 
-static char *
-_getenvp(char **env, const char *name)
-{
-	size_t len = strlen(name);
-	char **ep;
-
-	if ((env == NULL) || (env[0] == '\0'))
-		return NULL;
-
-	for (ep = env; *ep != NULL; ++ep) {
-		if (!strncmp(*ep, name, len) && ((*ep)[len] == '=')) 
-			return &(*ep)[len+1];
-	}
-
-	return NULL;
-}
-
 
-- 
GitLab