Merge pull request #2044 from RincewindsHat/fix/fedora-rpm-build
[monitoring-plugins.git] / plugins / popen.c
blob2b9824bc25eb5db2825668fba2d33d54634c2ca0
1 /*****************************************************************************
3 * Monitoring Plugins popen
5 * License: GPL
6 * Copyright (c) 2005-2024 Monitoring Plugins Development Team
8 * Description:
10 * A safe alternative to popen
12 * Provides spopen and spclose
14 * FILE * spopen(const char *);
15 * int spclose(FILE *);
17 * Code taken with little modification from "Advanced Programming for the Unix
18 * Environment" by W. Richard Stevens
20 * This is considered safe in that no shell is spawned, and the environment
21 * and path passed to the exec'd program are essentially empty. (popen create
22 * a shell and passes the environment to it).
25 * This program is free software: you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation, either version 3 of the License, or
28 * (at your option) any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with this program. If not, see <http://www.gnu.org/licenses/>.
39 *****************************************************************************/
41 #include "./common.h"
42 #include "./utils.h"
43 #include "../lib/maxfd.h"
45 /* extern so plugin has pid to kill exec'd process on timeouts */
46 extern pid_t *childpid;
47 extern int *child_stderr_array;
48 extern FILE *child_process;
50 FILE *spopen(const char * /*cmdstring*/);
51 int spclose(FILE * /*fp*/);
52 #ifdef REDHAT_SPOPEN_ERROR
53 void popen_sigchld_handler(int);
54 #endif
55 void popen_timeout_alarm_handler(int /*signo*/);
57 #include <stdarg.h> /* ANSI C header file */
58 #include <fcntl.h>
60 #include <limits.h>
61 #include <sys/resource.h>
63 #ifdef HAVE_SYS_WAIT_H
64 # include <sys/wait.h>
65 #endif
67 #ifndef WEXITSTATUS
68 # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
69 #endif
71 #ifndef WIFEXITED
72 # define WIFEXITED(stat_val) (((stat_val)&255) == 0)
73 #endif
75 /* 4.3BSD Reno <signal.h> doesn't define SIG_ERR */
76 #if defined(SIG_IGN) && !defined(SIG_ERR)
77 # define SIG_ERR ((Sigfunc *)-1)
78 #endif
80 char *pname = NULL; /* caller can set this from argv[0] */
82 #ifdef REDHAT_SPOPEN_ERROR
83 static volatile int childtermd = 0;
84 #endif
86 FILE *spopen(const char *cmdstring) {
87 #ifdef RLIMIT_CORE
88 /* do not leave core files */
89 struct rlimit limit;
90 getrlimit(RLIMIT_CORE, &limit);
91 limit.rlim_cur = 0;
92 setrlimit(RLIMIT_CORE, &limit);
93 #endif
95 char *env[2];
96 env[0] = strdup("LC_ALL=C");
97 env[1] = NULL;
99 /* if no command was passed, return with no error */
100 if (cmdstring == NULL)
101 return (NULL);
103 char *cmd = NULL;
104 /* make copy of command string so strtok() doesn't silently modify it */
105 /* (the calling program may want to access it later) */
106 cmd = malloc(strlen(cmdstring) + 1);
107 if (cmd == NULL)
108 return NULL;
109 strcpy(cmd, cmdstring);
111 /* This is not a shell, so we don't handle "???" */
112 if (strstr(cmdstring, "\""))
113 return NULL;
115 /* allow single quotes, but only if non-whitesapce doesn't occur on both sides */
116 if (strstr(cmdstring, " ' ") || strstr(cmdstring, "'''"))
117 return NULL;
119 int argc;
120 char **argv = NULL;
121 /* there cannot be more args than characters */
122 argc = strlen(cmdstring) + 1; /* add 1 for NULL termination */
123 argv = malloc(sizeof(char *) * argc);
125 if (argv == NULL) {
126 printf("%s\n", _("Could not malloc argv array in popen()"));
127 return NULL;
130 int i = 0;
131 char *str;
132 /* loop to get arguments to command */
133 while (cmd) {
134 str = cmd;
135 str += strspn(str, " \t\r\n"); /* trim any leading whitespace */
137 if (i >= argc - 2) {
138 printf("%s\n", _("CRITICAL - You need more args!!!"));
139 return (NULL);
142 if (strstr(str, "'") == str) { /* handle SIMPLE quoted strings */
143 str++;
144 if (!strstr(str, "'"))
145 return NULL; /* balanced? */
146 cmd = 1 + strstr(str, "'");
147 str[strcspn(str, "'")] = 0;
148 } else if (strcspn(str, "'") < strcspn(str, " \t\r\n")) {
149 /* handle --option='foo bar' strings */
150 char *tmp = str + strcspn(str, "'") + 1;
151 if (!strstr(tmp, "'"))
152 return NULL; /* balanced? */
153 tmp += strcspn(tmp, "'") + 1;
154 *tmp = 0;
155 cmd = tmp + 1;
156 } else {
157 if (strpbrk(str, " \t\r\n")) {
158 cmd = 1 + strpbrk(str, " \t\r\n");
159 str[strcspn(str, " \t\r\n")] = 0;
160 } else {
161 cmd = NULL;
165 if (cmd && strlen(cmd) == strspn(cmd, " \t\r\n"))
166 cmd = NULL;
168 argv[i++] = str;
170 argv[i] = NULL;
172 long maxfd = mp_open_max();
174 if (childpid == NULL) { /* first time through */
175 if ((childpid = calloc((size_t)maxfd, sizeof(pid_t))) == NULL)
176 return (NULL);
179 if (child_stderr_array == NULL) { /* first time through */
180 if ((child_stderr_array = calloc((size_t)maxfd, sizeof(int))) == NULL)
181 return (NULL);
184 int pfd[2];
185 if (pipe(pfd) < 0)
186 return (NULL); /* errno set by pipe() */
188 int pfderr[2];
189 if (pipe(pfderr) < 0)
190 return (NULL); /* errno set by pipe() */
192 #ifdef REDHAT_SPOPEN_ERROR
193 if (signal(SIGCHLD, popen_sigchld_handler) == SIG_ERR) {
194 usage4(_("Cannot catch SIGCHLD"));
196 #endif
198 pid_t pid;
199 if ((pid = fork()) < 0)
200 return (NULL); /* errno set by fork() */
202 if (pid == 0) { /* child */
203 close(pfd[0]);
204 if (pfd[1] != STDOUT_FILENO) {
205 dup2(pfd[1], STDOUT_FILENO);
206 close(pfd[1]);
208 close(pfderr[0]);
209 if (pfderr[1] != STDERR_FILENO) {
210 dup2(pfderr[1], STDERR_FILENO);
211 close(pfderr[1]);
213 /* close all descriptors in childpid[] */
214 for (i = 0; i < maxfd; i++)
215 if (childpid[i] > 0)
216 close(i);
218 execve(argv[0], argv, env);
219 _exit(0);
222 close(pfd[1]); /* parent */
223 if ((child_process = fdopen(pfd[0], "r")) == NULL)
224 return (NULL);
225 close(pfderr[1]);
227 childpid[fileno(child_process)] = pid; /* remember child pid for this fd */
228 child_stderr_array[fileno(child_process)] = pfderr[0]; /* remember STDERR */
229 return (child_process);
232 int spclose(FILE *fp) {
233 if (childpid == NULL)
234 return (1); /* popen() has never been called */
236 pid_t pid;
237 int fd = fileno(fp);
238 if ((pid = childpid[fd]) == 0)
239 return (1); /* fp wasn't opened by popen() */
241 childpid[fd] = 0;
242 if (fclose(fp) == EOF)
243 return (1);
245 #ifdef REDHAT_SPOPEN_ERROR
246 while (!childtermd)
247 ; /* wait until SIGCHLD */
248 #endif
250 int status;
251 while (waitpid(pid, &status, 0) < 0)
252 if (errno != EINTR)
253 return (1); /* error other than EINTR from waitpid() */
255 if (WIFEXITED(status))
256 return (WEXITSTATUS(status)); /* return child's termination status */
258 return (1);
261 #ifdef REDHAT_SPOPEN_ERROR
262 void popen_sigchld_handler(int signo) {
263 if (signo == SIGCHLD)
264 childtermd = 1;
266 #endif
268 void popen_timeout_alarm_handler(int signo) {
269 if (signo == SIGALRM) {
270 if (child_process != NULL) {
271 int fh = fileno(child_process);
272 if (fh >= 0) {
273 kill(childpid[fh], SIGKILL);
275 printf(_("CRITICAL - Plugin timed out after %d seconds\n"), timeout_interval);
276 } else {
277 printf("%s\n", _("CRITICAL - popen timeout received, but no child process"));
279 exit(STATE_CRITICAL);