Code

Fix cmd_run overwriting the environment
[nagiosplug.git] / lib / utils_cmd.c
index c4ceb97ed2d590f8a6388d107dcaefba0ae99966..0c853dcc687d99ba2479e43c40edfa5d37fa6f22 100644 (file)
@@ -1,42 +1,40 @@
-/****************************************************************************
- * Nagios run command utilities
- *
- * License: GPL
- * Copyright (c) 2005 nagios-plugins team
- *
- * $Id: utils_cmd.c 1434 2006-06-18 19:36:48Z opensides $
- *
- * Description :
- *
- * A simple interface to executing programs from other programs, using an
- * optimized and safe popen()-like implementation. It is considered safe
- * in that no shell needs to be spawned and the environment passed to the
- * execve()'d program is essentially empty.
- *
- *
- * The code in this file is a derivative of popen.c which in turn was taken
- * from "Advanced Programming for the Unix Environment" by W. Richard Stevens.
- *
- * Care has been taken to make sure the functions are async-safe. The one
- * function which isn't is cmd_init() which it doesn't make sense to
- * call twice anyway, so the api as a whole should be considered async-safe.
- *
- * License Information:
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
+/*****************************************************************************
+*
+* Nagios run command utilities
+*
+* License: GPL
+* Copyright (c) 2005-2006 Nagios Plugins Development Team
+*
+* Description :
+*
+* A simple interface to executing programs from other programs, using an
+* optimized and safe popen()-like implementation. It is considered safe
+* in that no shell needs to be spawned and the environment passed to the
+* execve()'d program is essentially empty.
+*
+* The code in this file is a derivative of popen.c which in turn was taken
+* from "Advanced Programming for the Unix Environment" by W. Richard Stevens.
+*
+* Care has been taken to make sure the functions are async-safe. The one
+* function which isn't is cmd_init() which it doesn't make sense to
+* call twice anyway, so the api as a whole should be considered async-safe.
+* 
+* 
+* This program is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+* 
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+* 
+* You should have received a copy of the GNU General Public License
+* along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*
+*
+*****************************************************************************/
 
 #define NAGIOSPLUG_API_C 1
 
 #include "common.h"
 #include "utils_cmd.h"
 #include "utils_base.h"
+#include <fcntl.h>
 
 #ifdef HAVE_SYS_WAIT_H
 # include <sys/wait.h>
 #endif
 
+/* used in _cmd_open to pass the environment to commands */
+extern char **environ;
+
 /** macros **/
 #ifndef WEXITSTATUS
 # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
@@ -123,7 +125,6 @@ cmd_init (void)
 static int
 _cmd_open (char *const *argv, int *pfd, int *pfderr)
 {
-       char *env[2];
        pid_t pid;
 #ifdef RLIMIT_CORE
        struct rlimit limit;
@@ -138,8 +139,7 @@ _cmd_open (char *const *argv, int *pfd, int *pfderr)
        if (!_cmd_pids)
                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)
                return -1;                                                                      /* errno set by the failing function */
@@ -170,7 +170,7 @@ _cmd_open (char *const *argv, int *pfd, int *pfderr)
                        if (_cmd_pids[i] > 0)
                                close (i);
 
-               execve (argv[0], argv, env);
+               execve (argv[0], argv, environ);
                _exit (STATE_UNKNOWN);
        }
 
@@ -376,3 +376,20 @@ cmd_run_array (char *const *argv, output * out, output * err, int flags)
 
        return _cmd_close (fd);
 }
+
+int
+cmd_file_read ( char *filename, output *out, int flags)
+{
+       int fd;
+       if(out)
+               memset (out, 0, sizeof(output));
+
+       if ((fd = open(filename, O_RDONLY)) == -1) {
+               die( STATE_UNKNOWN, _("Error opening %s: %s"), filename, strerror(errno) );
+       }
+       
+       if(out)
+               out->lines = _cmd_fetch_output (fd, out, flags);
+
+       return 0;
+}