Jelajahi Sumber

Fix cmd_run overwriting the environment

Some commands need the environment to function properly. One such
example is check_ssh and check_by_ssh when a SOCKS proxy is required.

This patch use setenv and extern char **environ to alter and pass the
new environment to the child process Those modules have been added to
Gnulib for portability.
Thomas Guyot-Sionnest 15 tahun lalu
induk
melakukan
cf2bcf6c7a
2 mengubah file dengan 6 tambahan dan 4 penghapusan
  1. 1 0
      NEWS
  2. 5 4
      lib/utils_cmd.c

+ 1 - 0
NEWS

@@ -29,6 +29,7 @@ This file documents the major additions and syntax changes between releases.
 	Fix translations when extra-opts aren't enabled (Jan Wagner - #2832884)
 	Fix translations when extra-opts aren't enabled (Jan Wagner - #2832884)
 	Fix parsing of multi-line strings in check_snmp (broken in 1.4.14) and enhance output in such case (#2832451)
 	Fix parsing of multi-line strings in check_snmp (broken in 1.4.14) and enhance output in such case (#2832451)
 	Fix detection of pst3 64-bit compile flags with Sun CC
 	Fix detection of pst3 64-bit compile flags with Sun CC
+	Fix cmd_run overwriting the environment, which would break some commands that needed it
 	WARNINGS
 	WARNINGS
 	Updated developer documentation to say that performance labels should not have an equals sign or
 	Updated developer documentation to say that performance labels should not have an equals sign or
 	single quote in the label
 	single quote in the label

+ 5 - 4
lib/utils_cmd.c

@@ -48,6 +48,9 @@
 # include <sys/wait.h>
 # include <sys/wait.h>
 #endif
 #endif
 
 
+/* used in _cmd_open to pass the environment to commands */
+extern char **environ;
+
 /** macros **/
 /** macros **/
 #ifndef WEXITSTATUS
 #ifndef WEXITSTATUS
 # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
 # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
@@ -122,7 +125,6 @@ cmd_init (void)
 static int
 static int
 _cmd_open (char *const *argv, int *pfd, int *pfderr)
 _cmd_open (char *const *argv, int *pfd, int *pfderr)
 {
 {
-	char *env[2];
 	pid_t pid;
 	pid_t pid;
 #ifdef RLIMIT_CORE
 #ifdef RLIMIT_CORE
 	struct rlimit limit;
 	struct rlimit limit;
@@ -137,8 +139,7 @@ _cmd_open (char *const *argv, int *pfd, int *pfderr)
 	if (!_cmd_pids)
 	if (!_cmd_pids)
 		CMD_INIT;
 		CMD_INIT;
 
 
-	env[0] = strdup ("LC_ALL=C");
-	env[1] = '\0';
+	setenv("LC_ALL", "C", 1);
 
 
 	if (pipe (pfd) < 0 || pipe (pfderr) < 0 || (pid = fork ()) < 0)
 	if (pipe (pfd) < 0 || pipe (pfderr) < 0 || (pid = fork ()) < 0)
 		return -1;									/* errno set by the failing function */
 		return -1;									/* errno set by the failing function */
@@ -169,7 +170,7 @@ _cmd_open (char *const *argv, int *pfd, int *pfderr)
 			if (_cmd_pids[i] > 0)
 			if (_cmd_pids[i] > 0)
 				close (i);
 				close (i);
 
 
-		execve (argv[0], argv, env);
+		execve (argv[0], argv, environ);
 		_exit (STATE_UNKNOWN);
 		_exit (STATE_UNKNOWN);
 	}
 	}