Title: [opsview-base] [234] set ENV only in forked processes, not in nagios workers
- Revision
- 234
- Author
- aburzynski
- Date
- 2013-03-28 17:32:14 +0000 (Thu, 28 Mar 2013)
Log Message
set ENV only in forked processes, not in nagios workers
Modified Paths
Added Paths
Modified: trunk/Makefile
===================================================================
--- trunk/Makefile 2013-03-27 17:55:32 UTC (rev 233)
+++ trunk/Makefile 2013-03-28 17:32:14 UTC (rev 234)
@@ -617,6 +617,7 @@
cd ${NAGIOS} && patch -p0 < ../patches/nagios_tilda_in_commands_execute_via_shell.patch
cd ${NAGIOS} && patch -p0 < ../patches/nagios_bugfix_2650_2651.patch
cd ${NAGIOS} && patch -p1 < ../patches/nagios_code7_fix.patch
+ cd ${NAGIOS} && patch -p1 < ../patches/nagios_putenv_in_child_proc.patch
if [ $(KERNEL_NAME) = Linux ] ; then \
cd ${NAGIOS} && CFLAGS="${CFLAGS}" ./configure --with-nagios-user=$(NAGIOS_USER) --with-nagios-group=$(NAGIOS_GROUP) --with-command-group=$(NAGIOS_GROUP) --with-cgiurl=/cgi-bin --with-htmurl=/ --enable-libtap ; \
elif [ $(KERNEL_NAME) = Darwin ] ; then \
Added: trunk/patches/nagios_putenv_in_child_proc.patch
===================================================================
--- trunk/patches/nagios_putenv_in_child_proc.patch (rev 0)
+++ trunk/patches/nagios_putenv_in_child_proc.patch 2013-03-28 17:32:14 UTC (rev 234)
@@ -0,0 +1,100 @@
+diff -ur nagios-4.0.20130107/lib/runcmd.c nagios-4.0.20130107.env/lib/runcmd.c
+--- nagios-4.0.20130107/lib/runcmd.c 2013-03-28 17:30:23.000000000 +0000
++++ nagios-4.0.20130107.env/lib/runcmd.c 2013-03-28 16:13:25.000000000 +0000
+@@ -297,6 +297,7 @@
+ pid_t pid;
+
+ int i = 0;
++ int envc = 0;
+
+ if(!pids)
+ runcmd_init();
+@@ -379,8 +380,21 @@
+ if(pids[i] > 0)
+ close (i);
+
++ if ( env != NULL ) {
++ while ( *env != NULL ) {
++ putenv(*(env++));
++ envc++; /* count number of ENVs */
++ }
++ }
+ i = execvp(argv[0], argv);
+ fprintf(stderr, "execvp(%s, ...) failed. errno is %d: %s\n", argv[0], errno, strerror(errno));
++
++ /* free memory in child */
++ for ( i = 0; i < envc; i++ ) {
++ free(env[i]);
++ }
++ if ( env != NULL ) free(env);
++
+ if (!cmd2strv_errors)
+ free(argv[0]);
+ else
+diff -ur nagios-4.0.20130107/lib/worker.c nagios-4.0.20130107.env/lib/worker.c
+--- nagios-4.0.20130107/lib/worker.c 2013-03-28 17:30:23.000000000 +0000
++++ nagios-4.0.20130107.env/lib/worker.c 2013-03-28 17:29:30.000000000 +0000
+@@ -261,14 +261,6 @@
+ struct key_value *kv = &cp->request->kv[i];
+ /* skip environment macros */
+ if (kv->key_len == 3 && !strcmp(kv->key, "env")) {
+- char *eq = strchr(kv->value, '=');
+- if ( eq == NULL ) continue;
+- size_t index = eq - kv->value;
+- char * var = malloc(index+1);
+- strncpy(var, kv->value, index);
+- var[index] = '\0';
+- unsetenv(var);
+- free(var);
+ continue;
+ }
+ kvvec_addkv_wlen(&resp, kv->key, kv->key_len, kv->value, kv->value_len);
+@@ -455,8 +447,35 @@
+ int start_cmd(child_process *cp)
+ {
+ int pfd[2] = {-1, -1}, pfderr[2] = {-1, -1};
++ struct kvvec *kvv = cp->request;
++ int ei = 0;
++ int i;
++ char **env = (char **) malloc( ( kvv->kv_pairs + 1 ) * sizeof( char * ) );
++
++ if ( env == NULL ) {
++ exit_worker(EXIT_FAILURE, "Worker failed to allocate memory for ENV variables");
++ }
++
++ /* populate env in spawned children */
++ for (i = 0; i < kvv->kv_pairs; i++) {
++ struct key_value *kv = &kvv->kv[i];
++ char *key = kv->key;
++ char *value = kv->value;
++
++ if (!strcmp(key, "env")) {
++ // TODO: Should confirm if putenv is available
++ env[ei++] = strdup(value);
++ }
++ }
++ env[ei] = NULL;
++
++ cp->outstd.fd = runcmd_open(cp->cmd, pfd, pfderr, env);
++ // free memory (in parent)
++ for (i = 0; i < ei; i++) {
++ free(env[i]);
++ }
++ free(env);
+
+- cp->outstd.fd = runcmd_open(cp->cmd, pfd, pfderr, NULL);
+ if (cp->outstd.fd < 0) {
+ return -1;
+ }
+@@ -515,11 +534,6 @@
+ cp->timeout = (unsigned int)strtoul(value, &endptr, 0);
+ continue;
+ }
+- if (!strcmp(key, "env")) {
+- // TODO: Should confirm if putenv is available
+- putenv(value);
+- continue;
+- }
+ }
+
+ /* jobs without a timeout get a default of 60 seconds. */
_______________________________________________
Opsview-checkins mailing list
Opsview-checkins@lists.opsview.org
http://lists.opsview.org/lists/listinfo/opsview-checkins