From 43c27d8c6b9516f03920ca0a2933084cac0622d7 Mon Sep 17 00:00:00 2001
From: tewk <tewk@unknown>
Date: Sat, 29 Jun 2002 19:27:41 +0000
Subject: [PATCH] Added packstring_array routines to pack the user environment
 to send to the slurmds from the sruns

---
 src/common/pack.c                           | 56 +++++++++++++++++++++
 src/common/pack.h                           | 24 +++++++--
 src/common/slurm_protocol_defs.c            |  8 ++-
 src/common/slurm_protocol_defs.h            |  3 +-
 src/common/slurm_protocol_pack.c            |  4 +-
 testsuite/slurm_unit/slurmd/task_mgr-test.c |  3 +-
 6 files changed, 90 insertions(+), 8 deletions(-)

diff --git a/src/common/pack.c b/src/common/pack.c
index fad30e23a12..bd09dfbe53f 100644
--- a/src/common/pack.c
+++ b/src/common/pack.c
@@ -222,3 +222,59 @@ _unpackmem_malloc(char **valp, uint16_t *size_valp, void **bufp, int *lenp)
 		*valp = NULL;
 
 }
+
+/*
+ * Given a pointer to array of char * (char ** or char *[] ) and a size (size_val), convert 
+ * size_val to network byte order and store at 'bufp' followed by 
+ * the data at valp. Advance bufp and decrement lenp by 4 bytes 
+ * (size of size_val) then pack each of the size_val strings. 
+ */
+void
+_packstrarray(char **valp, uint16_t size_val, void **bufp, int *lenp)
+{
+	int i ;
+	uint16_t nl = htons(size_val);
+
+	memcpy(*bufp, &nl, sizeof(nl));
+	(size_t)*bufp += sizeof(nl);
+	*lenp -= sizeof(nl);
+
+	for ( i = 0 ; i < size_val ; i ++ )
+	{
+		packstr(valp[i],bufp,lenp) ;
+	}
+
+}
+
+/*
+ * Given 'bufp' pointing to a network byte order 32-bit integer
+ * (size) and a array of strings  Advance bufp and decrement 
+ * lenp by 4 bytes (size of memory  size records) plus the actual 
+ * buffer size.
+ * NOTE: valp is set to point into a newly created buffer, 
+ *	the caller is responsible for calling xfree on *valp
+ *	if non-NULL (set to NULL on zero size buffer value)
+ */
+void
+_unpackstrarray (char ***valp, uint16_t *size_valp, void **bufp, int *lenp)
+{
+	int i ;
+	uint16_t nl;
+	uint16_t uint16_tmp;
+
+	memcpy(&nl, *bufp, sizeof(nl));
+	*size_valp = ntohs(nl);
+	(size_t)*bufp += sizeof(nl);
+	*lenp -= sizeof(nl);
+
+	if (*size_valp > 0) {
+		*valp = xmalloc(sizeof ( char * ) * *size_valp ) ;
+		for ( i = 0 ; i < *size_valp ; i ++ )
+		{
+			unpackstr_xmalloc ( & (*valp)[i] , & uint16_tmp , bufp , lenp ) ;
+		}
+	}
+	else
+		*valp = NULL;
+
+}
diff --git a/src/common/pack.h b/src/common/pack.h
index 10acb515422..40b4449a39e 100644
--- a/src/common/pack.h
+++ b/src/common/pack.h
@@ -28,6 +28,9 @@ void	_unpack16(uint16_t *valp, void **bufp, int *lenp);
 void	_pack32array(uint32_t *valp, uint16_t size_val, void **bufp, int *lenp);
 void	_unpack32array( uint32_t **valp, uint16_t* size_val, void **bufp, int *lenp);
 
+void	_packstrarray(char **valp, uint16_t size_val, void **bufp, int *lenp);
+void	_unpackstrarray(char ***valp, uint16_t* size_val, void **bufp, int *lenp);
+
 void	_packmem(char *valp, uint16_t size_val, void **bufp, int *lenp);
 void	_unpackmem_ptr(char **valp, uint16_t *size_valp, void **bufp, int *lenp);
 void	_unpackmem_xmalloc(char **valp, uint16_t *size_valp, void **bufp, int *lenp);
@@ -86,6 +89,22 @@ void	_unpackmem_malloc(char **valp, uint16_t *size_valp, void **bufp, int *lenp)
 	_packmem(str,(uint16_t)_size,bufp,lenp);	\
 } while (0)				
 
+#define packstring_array(array,_size,bufp,lenp) do {	\
+	assert((bufp) != NULL && *(bufp) != NULL);	\
+        assert((lenp) != NULL);				\
+        assert(*(lenp) >= (sizeof(_size)+_size));	\
+	_packstrarray(array,(uint16_t)_size,bufp,lenp);	\
+} while (0)				
+
+#define unpackstring_array(valp,size_valp,bufp,lenp) do {	\
+	assert(valp != NULL);				\
+	assert(sizeof(size_valp) == sizeof(uint16_t *));\
+	assert((bufp) != NULL && *(bufp) != NULL);	\
+        assert((lenp) != NULL);				\
+        assert(*(lenp) >= sizeof(uint16_t));		\
+	_unpackstrarray(valp,(uint16_t *)size_valp,bufp,lenp);\
+} while (0)
+
 #define pack32_array(array,_size,bufp,lenp) packint_array(array,_size,bufp,lenp)
 #define packint_array(array,_size,bufp,lenp) do {	\
 	assert((bufp) != NULL && *(bufp) != NULL);	\
@@ -105,6 +124,7 @@ void	_unpackmem_malloc(char **valp, uint16_t *size_valp, void **bufp, int *lenp)
 } while (0)
 
 
+#define unpackstr_ptr		unpackmem_ptr
 #define unpackmem_ptr(valp,size_valp,bufp,lenp) do {	\
 	assert(valp != NULL);				\
 	assert(sizeof(size_valp) == sizeof(uint16_t *));\
@@ -114,9 +134,7 @@ void	_unpackmem_malloc(char **valp, uint16_t *size_valp, void **bufp, int *lenp)
 	_unpackmem_ptr(valp,(uint16_t *)size_valp,bufp,lenp);\
 } while (0)
 
-#define unpackstr_ptr		unpackmem_ptr
 #define unpackstr_malloc	unpackmem_malloc
-
 #define unpackmem_malloc(valp,size_valp,bufp,lenp) do {\
 	assert(valp != NULL);				\
 	assert(sizeof(size_valp) == sizeof(uint16_t *));\
@@ -126,6 +144,7 @@ void	_unpackmem_malloc(char **valp, uint16_t *size_valp, void **bufp, int *lenp)
 	_unpackmem_malloc(valp,(uint16_t *)size_valp,bufp,lenp);\
 } while (0)
 
+#define unpackstr_xmalloc	unpackmem_xmalloc
 #define unpackmem_xmalloc(valp,size_valp,bufp,lenp) do {\
 	assert(valp != NULL);				\
 	assert(sizeof(size_valp) == sizeof(uint16_t *));\
@@ -135,6 +154,5 @@ void	_unpackmem_malloc(char **valp, uint16_t *size_valp, void **bufp, int *lenp)
 	_unpackmem_xmalloc(valp,(uint16_t *)size_valp,bufp,lenp);\
 } while (0)
 
-#define unpackstr_xmalloc	unpackmem_xmalloc
 
 #endif /* _PACK_INCLUDED */
diff --git a/src/common/slurm_protocol_defs.c b/src/common/slurm_protocol_defs.c
index 645e0c2f6b5..fc578eb4ff3 100644
--- a/src/common/slurm_protocol_defs.c
+++ b/src/common/slurm_protocol_defs.c
@@ -233,12 +233,18 @@ void slurm_free_update_node_msg ( update_node_msg_t * msg )
 
 void slurm_free_launch_tasks_msg ( launch_tasks_msg_t * msg )
 {
+	int i ;
 	if ( msg )
 	{
 		if ( msg -> credentials )
 			xfree ( msg -> credentials );
 		if ( msg -> env )
-			xfree ( msg -> env );
+			for ( i = 0 ; i < msg -> envc ; i++ )
+			{
+				if ( msg -> env[i] )
+					xfree ( msg -> env[i] );
+			}
+			xfree ( msg -> env ) ;
 		if ( msg -> cwd )
 			xfree ( msg -> cwd );
 		if ( msg -> cmd_line )
diff --git a/src/common/slurm_protocol_defs.h b/src/common/slurm_protocol_defs.h
index 3e9db60db60..433b89ee2e6 100644
--- a/src/common/slurm_protocol_defs.h
+++ b/src/common/slurm_protocol_defs.h
@@ -185,7 +185,8 @@ typedef struct launch_tasks_msg
 	uint32_t gid ;
 	char * credentials ;
 	uint32_t tasks_to_launch ;
-	char * env ;
+	uint16_t envc ;
+	char ** env ;
 	char * cwd ;
 	char * cmd_line ;
 	slurm_addr * streams;
diff --git a/src/common/slurm_protocol_pack.c b/src/common/slurm_protocol_pack.c
index e3b60327cb0..8224d64a747 100644
--- a/src/common/slurm_protocol_pack.c
+++ b/src/common/slurm_protocol_pack.c
@@ -797,7 +797,7 @@ void pack_launch_tasks_msg ( launch_tasks_msg_t * msg , void ** buffer , uint32_
 	pack32 ( msg -> gid , buffer , length ) ;
 	packstr ( msg -> credentials , buffer , length ) ;
 	pack32 ( msg -> tasks_to_launch , buffer , length ) ;
-	packstr ( msg -> env , buffer , length ) ;
+	packstring_array ( msg -> env , msg -> envc , buffer , length ) ;
 	packstr ( msg -> cwd , buffer , length ) ;
 	packstr ( msg -> cmd_line , buffer , length ) ;
 	pack_slurm_addr_array ( msg -> streams , ( uint16_t ) msg -> tasks_to_launch , buffer , length ) ;
@@ -822,7 +822,7 @@ int unpack_launch_tasks_msg ( launch_tasks_msg_t ** msg_ptr , void ** buffer , u
 	unpack32 ( & msg -> gid , buffer , length ) ;
 	unpackstr_xmalloc ( & msg -> credentials , & uint16_tmp , buffer , length ) ;
 	unpack32 ( & msg -> tasks_to_launch , buffer , length ) ;
-	unpackstr_xmalloc ( & msg -> env , & uint16_tmp , buffer , length ) ;
+	unpackstring_array ( & msg -> env , & msg -> envc , buffer , length ) ;
 	unpackstr_xmalloc ( & msg -> cwd , & uint16_tmp , buffer , length ) ;
 	unpackstr_xmalloc ( & msg -> cmd_line , & uint16_tmp , buffer , length ) ;
 	unpack_slurm_addr_array ( & msg -> streams , & uint16_tmp , buffer , length ) ;
diff --git a/testsuite/slurm_unit/slurmd/task_mgr-test.c b/testsuite/slurm_unit/slurmd/task_mgr-test.c
index 60776e4b64e..e4f3fccda2a 100644
--- a/testsuite/slurm_unit/slurmd/task_mgr-test.c
+++ b/testsuite/slurm_unit/slurmd/task_mgr-test.c
@@ -11,7 +11,8 @@ int main ( int argc , char ** argv )
 	launch_tasks_msg . job_step_id = 2000 ; 
 	launch_tasks_msg . uid = 801 ;
 	launch_tasks_msg . gid = 802 ;
-	launch_tasks_msg . env = "";
+	launch_tasks_msg . envc = 0 ;
+	launch_tasks_msg . env = NULL ;
 	launch_tasks_msg . cmd_line = "./testme" ;
 	launch_tasks_msg . cwd = "." ;
 	
-- 
GitLab