diff options
Diffstat (limited to 'lib/utils_cmd.c')
-rw-r--r-- | lib/utils_cmd.c | 398 |
1 files changed, 198 insertions, 200 deletions
diff --git a/lib/utils_cmd.c b/lib/utils_cmd.c index 7957ec14..35b83297 100644 --- a/lib/utils_cmd.c +++ b/lib/utils_cmd.c | |||
@@ -1,46 +1,45 @@ | |||
1 | /***************************************************************************** | 1 | /***************************************************************************** |
2 | * | 2 | * |
3 | * Monitoring run command utilities | 3 | * Monitoring run command utilities |
4 | * | 4 | * |
5 | * License: GPL | 5 | * License: GPL |
6 | * Copyright (c) 2005-2006 Monitoring Plugins Development Team | 6 | * Copyright (c) 2005-2024 Monitoring Plugins Development Team |
7 | * | 7 | * |
8 | * Description : | 8 | * Description : |
9 | * | 9 | * |
10 | * A simple interface to executing programs from other programs, using an | 10 | * A simple interface to executing programs from other programs, using an |
11 | * optimized and safe popen()-like implementation. It is considered safe | 11 | * optimized and safe popen()-like implementation. It is considered safe |
12 | * in that no shell needs to be spawned and the environment passed to the | 12 | * in that no shell needs to be spawned and the environment passed to the |
13 | * execve()'d program is essentially empty. | 13 | * execve()'d program is essentially empty. |
14 | * | 14 | * |
15 | * The code in this file is a derivative of popen.c which in turn was taken | 15 | * The code in this file is a derivative of popen.c which in turn was taken |
16 | * from "Advanced Programming for the Unix Environment" by W. Richard Stevens. | 16 | * from "Advanced Programming for the Unix Environment" by W. Richard Stevens. |
17 | * | 17 | * |
18 | * Care has been taken to make sure the functions are async-safe. The one | 18 | * Care has been taken to make sure the functions are async-safe. The one |
19 | * function which isn't is cmd_init() which it doesn't make sense to | 19 | * function which isn't is cmd_init() which it doesn't make sense to |
20 | * call twice anyway, so the api as a whole should be considered async-safe. | 20 | * call twice anyway, so the api as a whole should be considered async-safe. |
21 | * | 21 | * |
22 | * | 22 | * |
23 | * This program is free software: you can redistribute it and/or modify | 23 | * This program is free software: you can redistribute it and/or modify |
24 | * it under the terms of the GNU General Public License as published by | 24 | * it under the terms of the GNU General Public License as published by |
25 | * the Free Software Foundation, either version 3 of the License, or | 25 | * the Free Software Foundation, either version 3 of the License, or |
26 | * (at your option) any later version. | 26 | * (at your option) any later version. |
27 | * | 27 | * |
28 | * This program is distributed in the hope that it will be useful, | 28 | * This program is distributed in the hope that it will be useful, |
29 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 29 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
30 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 30 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
31 | * GNU General Public License for more details. | 31 | * GNU General Public License for more details. |
32 | * | 32 | * |
33 | * You should have received a copy of the GNU General Public License | 33 | * You should have received a copy of the GNU General Public License |
34 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 34 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
35 | * | 35 | * |
36 | * | 36 | * |
37 | *****************************************************************************/ | 37 | *****************************************************************************/ |
38 | 38 | ||
39 | #define NAGIOSPLUG_API_C 1 | 39 | #define NAGIOSPLUG_API_C 1 |
40 | 40 | ||
41 | /** includes **/ | 41 | /** includes **/ |
42 | #include "common.h" | 42 | #include "common.h" |
43 | #include "utils.h" | ||
44 | #include "utils_cmd.h" | 43 | #include "utils_cmd.h" |
45 | /* This variable must be global, since there's no way the caller | 44 | /* This variable must be global, since there's no way the caller |
46 | * can forcibly slay a dead or ungainly running program otherwise. | 45 | * can forcibly slay a dead or ungainly running program otherwise. |
@@ -59,115 +58,106 @@ static pid_t *_cmd_pids = NULL; | |||
59 | #include <fcntl.h> | 58 | #include <fcntl.h> |
60 | 59 | ||
61 | #ifdef HAVE_SYS_WAIT_H | 60 | #ifdef HAVE_SYS_WAIT_H |
62 | # include <sys/wait.h> | 61 | # include <sys/wait.h> |
63 | #endif | 62 | #endif |
64 | 63 | ||
65 | /* used in _cmd_open to pass the environment to commands */ | ||
66 | extern char **environ; | ||
67 | |||
68 | /** macros **/ | 64 | /** macros **/ |
69 | #ifndef WEXITSTATUS | 65 | #ifndef WEXITSTATUS |
70 | # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8) | 66 | # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8) |
71 | #endif | 67 | #endif |
72 | 68 | ||
73 | #ifndef WIFEXITED | 69 | #ifndef WIFEXITED |
74 | # define WIFEXITED(stat_val) (((stat_val) & 255) == 0) | 70 | # define WIFEXITED(stat_val) (((stat_val) & 255) == 0) |
75 | #endif | 71 | #endif |
76 | 72 | ||
77 | /* 4.3BSD Reno <signal.h> doesn't define SIG_ERR */ | 73 | /* 4.3BSD Reno <signal.h> doesn't define SIG_ERR */ |
78 | #if defined(SIG_IGN) && !defined(SIG_ERR) | 74 | #if defined(SIG_IGN) && !defined(SIG_ERR) |
79 | # define SIG_ERR ((Sigfunc *)-1) | 75 | # define SIG_ERR ((Sigfunc *)-1) |
80 | #endif | 76 | #endif |
81 | 77 | ||
82 | /** prototypes **/ | 78 | /** prototypes **/ |
83 | static int _cmd_open (char *const *, int *, int *) | 79 | static int _cmd_open(char *const *argv, int *pfd, int *pfderr) |
84 | __attribute__ ((__nonnull__ (1, 2, 3))); | 80 | __attribute__((__nonnull__(1, 2, 3))); |
85 | |||
86 | static int _cmd_fetch_output (int, output *, int) | ||
87 | __attribute__ ((__nonnull__ (2))); | ||
88 | 81 | ||
89 | static int _cmd_close (int); | 82 | static int _cmd_fetch_output(int fileDescriptor, output *cmd_output, int flags) __attribute__((__nonnull__(2))); |
90 | |||
91 | /* prototype imported from utils.h */ | ||
92 | extern void die (int, const char *, ...) | ||
93 | __attribute__ ((__noreturn__, __format__ (__printf__, 2, 3))); | ||
94 | 83 | ||
84 | static int _cmd_close(int fileDescriptor); | ||
95 | 85 | ||
96 | /* this function is NOT async-safe. It is exported so multithreaded | 86 | /* this function is NOT async-safe. It is exported so multithreaded |
97 | * plugins (or other apps) can call it prior to running any commands | 87 | * plugins (or other apps) can call it prior to running any commands |
98 | * through this api and thus achieve async-safeness throughout the api */ | 88 | * through this api and thus achieve async-safeness throughout the api */ |
99 | void | 89 | void cmd_init(void) { |
100 | cmd_init (void) | ||
101 | { | ||
102 | long maxfd = mp_open_max(); | 90 | long maxfd = mp_open_max(); |
103 | 91 | ||
104 | /* if maxfd is unnaturally high, we force it to a lower value | 92 | /* if maxfd is unnaturally high, we force it to a lower value |
105 | * ( e.g. on SunOS, when ulimit is set to unlimited: 2147483647 this would cause | 93 | * ( e.g. on SunOS, when ulimit is set to unlimited: 2147483647 this would cause |
106 | * a segfault when following calloc is called ... ) */ | 94 | * a segfault when following calloc is called ... ) */ |
107 | 95 | ||
108 | if ( maxfd > MAXFD_LIMIT ) { | 96 | if (maxfd > MAXFD_LIMIT) { |
109 | maxfd = MAXFD_LIMIT; | 97 | maxfd = MAXFD_LIMIT; |
110 | } | 98 | } |
111 | 99 | ||
112 | if (!_cmd_pids) | 100 | if (!_cmd_pids) { |
113 | _cmd_pids = calloc (maxfd, sizeof (pid_t)); | 101 | _cmd_pids = calloc(maxfd, sizeof(pid_t)); |
102 | } | ||
114 | } | 103 | } |
115 | 104 | ||
116 | |||
117 | /* Start running a command, array style */ | 105 | /* Start running a command, array style */ |
118 | static int | 106 | static int _cmd_open(char *const *argv, int *pfd, int *pfderr) { |
119 | _cmd_open (char *const *argv, int *pfd, int *pfderr) | ||
120 | { | ||
121 | pid_t pid; | ||
122 | #ifdef RLIMIT_CORE | 107 | #ifdef RLIMIT_CORE |
123 | struct rlimit limit; | 108 | struct rlimit limit; |
124 | #endif | 109 | #endif |
125 | 110 | ||
126 | int i = 0; | 111 | int i = 0; |
127 | 112 | ||
128 | if (!_cmd_pids) | 113 | if (!_cmd_pids) { |
129 | CMD_INIT; | 114 | CMD_INIT; |
115 | } | ||
130 | 116 | ||
131 | setenv("LC_ALL", "C", 1); | 117 | setenv("LC_ALL", "C", 1); |
132 | 118 | ||
133 | if (pipe (pfd) < 0 || pipe (pfderr) < 0 || (pid = fork ()) < 0) | 119 | pid_t pid; |
134 | return -1; /* errno set by the failing function */ | 120 | if (pipe(pfd) < 0 || pipe(pfderr) < 0 || (pid = fork()) < 0) { |
121 | return -1; /* errno set by the failing function */ | ||
122 | } | ||
135 | 123 | ||
136 | /* child runs exceve() and _exit. */ | 124 | /* child runs exceve() and _exit. */ |
137 | if (pid == 0) { | 125 | if (pid == 0) { |
138 | #ifdef RLIMIT_CORE | 126 | #ifdef RLIMIT_CORE |
139 | /* the program we execve shouldn't leave core files */ | 127 | /* the program we execve shouldn't leave core files */ |
140 | getrlimit (RLIMIT_CORE, &limit); | 128 | getrlimit(RLIMIT_CORE, &limit); |
141 | limit.rlim_cur = 0; | 129 | limit.rlim_cur = 0; |
142 | setrlimit (RLIMIT_CORE, &limit); | 130 | setrlimit(RLIMIT_CORE, &limit); |
143 | #endif | 131 | #endif |
144 | close (pfd[0]); | 132 | close(pfd[0]); |
145 | if (pfd[1] != STDOUT_FILENO) { | 133 | if (pfd[1] != STDOUT_FILENO) { |
146 | dup2 (pfd[1], STDOUT_FILENO); | 134 | dup2(pfd[1], STDOUT_FILENO); |
147 | close (pfd[1]); | 135 | close(pfd[1]); |
148 | } | 136 | } |
149 | close (pfderr[0]); | 137 | close(pfderr[0]); |
150 | if (pfderr[1] != STDERR_FILENO) { | 138 | if (pfderr[1] != STDERR_FILENO) { |
151 | dup2 (pfderr[1], STDERR_FILENO); | 139 | dup2(pfderr[1], STDERR_FILENO); |
152 | close (pfderr[1]); | 140 | close(pfderr[1]); |
153 | } | 141 | } |
154 | 142 | ||
155 | /* close all descriptors in _cmd_pids[] | 143 | /* close all descriptors in _cmd_pids[] |
156 | * This is executed in a separate address space (pure child), | 144 | * This is executed in a separate address space (pure child), |
157 | * so we don't have to worry about async safety */ | 145 | * so we don't have to worry about async safety */ |
158 | long maxfd = mp_open_max(); | 146 | long maxfd = mp_open_max(); |
159 | for (i = 0; i < maxfd; i++) | 147 | for (i = 0; i < maxfd; i++) { |
160 | if (_cmd_pids[i] > 0) | 148 | if (_cmd_pids[i] > 0) { |
161 | close (i); | 149 | close(i); |
150 | } | ||
151 | } | ||
162 | 152 | ||
163 | execve (argv[0], argv, environ); | 153 | execve(argv[0], argv, environ); |
164 | _exit (STATE_UNKNOWN); | 154 | _exit(STATE_UNKNOWN); |
165 | } | 155 | } |
166 | 156 | ||
167 | /* parent picks up execution here */ | 157 | /* parent picks up execution here */ |
168 | /* close children descriptors in our address space */ | 158 | /* close children descriptors in our address space */ |
169 | close (pfd[1]); | 159 | close(pfd[1]); |
170 | close (pfderr[1]); | 160 | close(pfderr[1]); |
171 | 161 | ||
172 | /* tag our file's entry in the pid-list and return it */ | 162 | /* tag our file's entry in the pid-list and return it */ |
173 | _cmd_pids[pfd[0]] = pid; | 163 | _cmd_pids[pfd[0]] = pid; |
@@ -175,93 +165,94 @@ _cmd_open (char *const *argv, int *pfd, int *pfderr) | |||
175 | return pfd[0]; | 165 | return pfd[0]; |
176 | } | 166 | } |
177 | 167 | ||
178 | static int | 168 | static int _cmd_close(int fileDescriptor) { |
179 | _cmd_close (int fd) | ||
180 | { | ||
181 | int status; | ||
182 | pid_t pid; | 169 | pid_t pid; |
183 | 170 | ||
184 | /* make sure the provided fd was opened */ | 171 | /* make sure the provided fd was opened */ |
185 | long maxfd = mp_open_max(); | 172 | long maxfd = mp_open_max(); |
186 | if (fd < 0 || fd > maxfd || !_cmd_pids || (pid = _cmd_pids[fd]) == 0) | 173 | if (fileDescriptor < 0 || fileDescriptor > maxfd || !_cmd_pids || |
174 | (pid = _cmd_pids[fileDescriptor]) == 0) { | ||
187 | return -1; | 175 | return -1; |
176 | } | ||
188 | 177 | ||
189 | _cmd_pids[fd] = 0; | 178 | _cmd_pids[fileDescriptor] = 0; |
190 | if (close (fd) == -1) | 179 | if (close(fileDescriptor) == -1) { |
191 | return -1; | 180 | return -1; |
181 | } | ||
192 | 182 | ||
193 | /* EINTR is ok (sort of), everything else is bad */ | 183 | /* EINTR is ok (sort of), everything else is bad */ |
194 | while (waitpid (pid, &status, 0) < 0) | 184 | int status; |
195 | if (errno != EINTR) | 185 | while (waitpid(pid, &status, 0) < 0) { |
186 | if (errno != EINTR) { | ||
196 | return -1; | 187 | return -1; |
188 | } | ||
189 | } | ||
197 | 190 | ||
198 | /* return child's termination status */ | 191 | /* return child's termination status */ |
199 | return (WIFEXITED (status)) ? WEXITSTATUS (status) : -1; | 192 | return (WIFEXITED(status)) ? WEXITSTATUS(status) : -1; |
200 | } | 193 | } |
201 | 194 | ||
202 | 195 | static int _cmd_fetch_output(int fileDescriptor, output *cmd_output, int flags) { | |
203 | static int | ||
204 | _cmd_fetch_output (int fd, output * op, int flags) | ||
205 | { | ||
206 | size_t len = 0, i = 0, lineno = 0; | ||
207 | size_t rsf = 6, ary_size = 0; /* rsf = right shift factor, dec'ed uncond once */ | ||
208 | char *buf = NULL; | ||
209 | int ret; | ||
210 | char tmpbuf[4096]; | 196 | char tmpbuf[4096]; |
211 | 197 | cmd_output->buf = NULL; | |
212 | op->buf = NULL; | 198 | cmd_output->buflen = 0; |
213 | op->buflen = 0; | 199 | ssize_t ret; |
214 | while ((ret = read (fd, tmpbuf, sizeof (tmpbuf))) > 0) { | 200 | while ((ret = read(fileDescriptor, tmpbuf, sizeof(tmpbuf))) > 0) { |
215 | len = (size_t) ret; | 201 | size_t len = (size_t)ret; |
216 | op->buf = realloc (op->buf, op->buflen + len + 1); | 202 | cmd_output->buf = realloc(cmd_output->buf, cmd_output->buflen + len + 1); |
217 | memcpy (op->buf + op->buflen, tmpbuf, len); | 203 | memcpy(cmd_output->buf + cmd_output->buflen, tmpbuf, len); |
218 | op->buflen += len; | 204 | cmd_output->buflen += len; |
219 | i++; | ||
220 | } | 205 | } |
221 | 206 | ||
222 | if (ret < 0) { | 207 | if (ret < 0) { |
223 | printf ("read() returned %d: %s\n", ret, strerror (errno)); | 208 | printf("read() returned %zd: %s\n", ret, strerror(errno)); |
224 | return ret; | 209 | return ret; |
225 | } | 210 | } |
226 | 211 | ||
227 | /* some plugins may want to keep output unbroken, and some commands | 212 | /* some plugins may want to keep output unbroken, and some commands |
228 | * will yield no output, so return here for those */ | 213 | * will yield no output, so return here for those */ |
229 | if (flags & CMD_NO_ARRAYS || !op->buf || !op->buflen) | 214 | if (flags & CMD_NO_ARRAYS || !cmd_output->buf || !cmd_output->buflen) { |
230 | return op->buflen; | 215 | return cmd_output->buflen; |
216 | } | ||
231 | 217 | ||
232 | /* and some may want both */ | 218 | /* and some may want both */ |
219 | char *buf = NULL; | ||
233 | if (flags & CMD_NO_ASSOC) { | 220 | if (flags & CMD_NO_ASSOC) { |
234 | buf = malloc (op->buflen); | 221 | buf = malloc(cmd_output->buflen); |
235 | memcpy (buf, op->buf, op->buflen); | 222 | memcpy(buf, cmd_output->buf, cmd_output->buflen); |
223 | } else { | ||
224 | buf = cmd_output->buf; | ||
236 | } | 225 | } |
237 | else | ||
238 | buf = op->buf; | ||
239 | 226 | ||
240 | op->line = NULL; | 227 | cmd_output->line = NULL; |
241 | op->lens = NULL; | 228 | cmd_output->lens = NULL; |
242 | i = 0; | 229 | size_t i = 0; |
243 | while (i < op->buflen) { | 230 | size_t ary_size = 0; /* rsf = right shift factor, dec'ed uncond once */ |
231 | size_t rsf = 6; | ||
232 | size_t lineno = 0; | ||
233 | while (i < cmd_output->buflen) { | ||
244 | /* make sure we have enough memory */ | 234 | /* make sure we have enough memory */ |
245 | if (lineno >= ary_size) { | 235 | if (lineno >= ary_size) { |
246 | /* ary_size must never be zero */ | 236 | /* ary_size must never be zero */ |
247 | do { | 237 | do { |
248 | ary_size = op->buflen >> --rsf; | 238 | ary_size = cmd_output->buflen >> --rsf; |
249 | } while (!ary_size); | 239 | } while (!ary_size); |
250 | 240 | ||
251 | op->line = realloc (op->line, ary_size * sizeof (char *)); | 241 | cmd_output->line = realloc(cmd_output->line, ary_size * sizeof(char *)); |
252 | op->lens = realloc (op->lens, ary_size * sizeof (size_t)); | 242 | cmd_output->lens = realloc(cmd_output->lens, ary_size * sizeof(size_t)); |
253 | } | 243 | } |
254 | 244 | ||
255 | /* set the pointer to the string */ | 245 | /* set the pointer to the string */ |
256 | op->line[lineno] = &buf[i]; | 246 | cmd_output->line[lineno] = &buf[i]; |
257 | 247 | ||
258 | /* hop to next newline or end of buffer */ | 248 | /* hop to next newline or end of buffer */ |
259 | while (buf[i] != '\n' && i < op->buflen) | 249 | while (buf[i] != '\n' && i < cmd_output->buflen) { |
260 | i++; | 250 | i++; |
251 | } | ||
261 | buf[i] = '\0'; | 252 | buf[i] = '\0'; |
262 | 253 | ||
263 | /* calculate the string length using pointer difference */ | 254 | /* calculate the string length using pointer difference */ |
264 | op->lens[lineno] = (size_t) & buf[i] - (size_t) op->line[lineno]; | 255 | cmd_output->lens[lineno] = (size_t)&buf[i] - (size_t)cmd_output->line[lineno]; |
265 | 256 | ||
266 | lineno++; | 257 | lineno++; |
267 | i++; | 258 | i++; |
@@ -270,135 +261,142 @@ _cmd_fetch_output (int fd, output * op, int flags) | |||
270 | return lineno; | 261 | return lineno; |
271 | } | 262 | } |
272 | 263 | ||
273 | 264 | int cmd_run(const char *cmdstring, output *out, output *err, int flags) { | |
274 | int | 265 | if (cmdstring == NULL) { |
275 | cmd_run (const char *cmdstring, output * out, output * err, int flags) | ||
276 | { | ||
277 | int i = 0, argc; | ||
278 | size_t cmdlen; | ||
279 | char **argv = NULL; | ||
280 | char *cmd = NULL; | ||
281 | char *str = NULL; | ||
282 | |||
283 | if (cmdstring == NULL) | ||
284 | return -1; | 266 | return -1; |
267 | } | ||
285 | 268 | ||
286 | /* initialize the structs */ | 269 | /* initialize the structs */ |
287 | if (out) | 270 | if (out) { |
288 | memset (out, 0, sizeof (output)); | 271 | memset(out, 0, sizeof(output)); |
289 | if (err) | 272 | } |
290 | memset (err, 0, sizeof (output)); | 273 | if (err) { |
274 | memset(err, 0, sizeof(output)); | ||
275 | } | ||
291 | 276 | ||
292 | /* make copy of command string so strtok() doesn't silently modify it */ | 277 | /* make copy of command string so strtok() doesn't silently modify it */ |
293 | /* (the calling program may want to access it later) */ | 278 | /* (the calling program may want to access it later) */ |
294 | cmdlen = strlen (cmdstring); | 279 | size_t cmdlen = strlen(cmdstring); |
295 | if ((cmd = malloc (cmdlen + 1)) == NULL) | 280 | char *cmd = NULL; |
281 | if ((cmd = malloc(cmdlen + 1)) == NULL) { | ||
296 | return -1; | 282 | return -1; |
297 | memcpy (cmd, cmdstring, cmdlen); | 283 | } |
284 | memcpy(cmd, cmdstring, cmdlen); | ||
298 | cmd[cmdlen] = '\0'; | 285 | cmd[cmdlen] = '\0'; |
299 | 286 | ||
300 | /* This is not a shell, so we don't handle "???" */ | 287 | /* This is not a shell, so we don't handle "???" */ |
301 | if (strstr (cmdstring, "\"")) return -1; | 288 | if (strstr(cmdstring, "\"")) { |
289 | return -1; | ||
290 | } | ||
302 | 291 | ||
303 | /* allow single quotes, but only if non-whitesapce doesn't occur on both sides */ | 292 | /* allow single quotes, but only if non-whitesapce doesn't occur on both sides */ |
304 | if (strstr (cmdstring, " ' ") || strstr (cmdstring, "'''")) | 293 | if (strstr(cmdstring, " ' ") || strstr(cmdstring, "'''")) { |
305 | return -1; | 294 | return -1; |
295 | } | ||
306 | 296 | ||
307 | /* each arg must be whitespace-separated, so args can be a maximum | 297 | /* each arg must be whitespace-separated, so args can be a maximum |
308 | * of (len / 2) + 1. We add 1 extra to the mix for NULL termination */ | 298 | * of (len / 2) + 1. We add 1 extra to the mix for NULL termination */ |
309 | argc = (cmdlen >> 1) + 2; | 299 | int argc = (cmdlen >> 1) + 2; |
310 | argv = calloc (sizeof (char *), argc); | 300 | char **argv = calloc((size_t)argc, sizeof(char *)); |
311 | 301 | ||
312 | if (argv == NULL) { | 302 | if (argv == NULL) { |
313 | printf ("%s\n", _("Could not malloc argv array in popen()")); | 303 | printf("%s\n", _("Could not malloc argv array in popen()")); |
314 | return -1; | 304 | return -1; |
315 | } | 305 | } |
316 | 306 | ||
317 | /* get command arguments (stupidly, but fairly quickly) */ | 307 | /* get command arguments (stupidly, but fairly quickly) */ |
308 | int i = 0; | ||
318 | while (cmd) { | 309 | while (cmd) { |
319 | str = cmd; | 310 | char *str = cmd; |
320 | str += strspn (str, " \t\r\n"); /* trim any leading whitespace */ | 311 | str += strspn(str, " \t\r\n"); /* trim any leading whitespace */ |
321 | 312 | ||
322 | if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */ | 313 | if (strstr(str, "'") == str) { /* handle SIMPLE quoted strings */ |
323 | str++; | 314 | str++; |
324 | if (!strstr (str, "'")) | 315 | if (!strstr(str, "'")) { |
325 | return -1; /* balanced? */ | 316 | return -1; /* balanced? */ |
326 | cmd = 1 + strstr (str, "'"); | ||
327 | str[strcspn (str, "'")] = 0; | ||
328 | } | ||
329 | else { | ||
330 | if (strpbrk (str, " \t\r\n")) { | ||
331 | cmd = 1 + strpbrk (str, " \t\r\n"); | ||
332 | str[strcspn (str, " \t\r\n")] = 0; | ||
333 | } | 317 | } |
334 | else { | 318 | cmd = 1 + strstr(str, "'"); |
319 | str[strcspn(str, "'")] = 0; | ||
320 | } else { | ||
321 | if (strpbrk(str, " \t\r\n")) { | ||
322 | cmd = 1 + strpbrk(str, " \t\r\n"); | ||
323 | str[strcspn(str, " \t\r\n")] = 0; | ||
324 | } else { | ||
335 | cmd = NULL; | 325 | cmd = NULL; |
336 | } | 326 | } |
337 | } | 327 | } |
338 | 328 | ||
339 | if (cmd && strlen (cmd) == strspn (cmd, " \t\r\n")) | 329 | if (cmd && strlen(cmd) == strspn(cmd, " \t\r\n")) { |
340 | cmd = NULL; | 330 | cmd = NULL; |
331 | } | ||
341 | 332 | ||
342 | argv[i++] = str; | 333 | argv[i++] = str; |
343 | } | 334 | } |
344 | 335 | ||
345 | return cmd_run_array (argv, out, err, flags); | 336 | return cmd_run_array(argv, out, err, flags); |
346 | } | 337 | } |
347 | 338 | ||
348 | int | 339 | int cmd_run_array(char *const *argv, output *out, output *err, int flags) { |
349 | cmd_run_array (char *const *argv, output * out, output * err, int flags) | ||
350 | { | ||
351 | int fd, pfd_out[2], pfd_err[2]; | ||
352 | |||
353 | /* initialize the structs */ | 340 | /* initialize the structs */ |
354 | if (out) | 341 | if (out) { |
355 | memset (out, 0, sizeof (output)); | 342 | memset(out, 0, sizeof(output)); |
356 | if (err) | 343 | } |
357 | memset (err, 0, sizeof (output)); | 344 | if (err) { |
345 | memset(err, 0, sizeof(output)); | ||
346 | } | ||
358 | 347 | ||
359 | if ((fd = _cmd_open (argv, pfd_out, pfd_err)) == -1) | 348 | int fd; |
360 | die (STATE_UNKNOWN, _("Could not open pipe: %s\n"), argv[0]); | 349 | int pfd_out[2]; |
350 | int pfd_err[2]; | ||
351 | if ((fd = _cmd_open(argv, pfd_out, pfd_err)) == -1) { | ||
352 | die(STATE_UNKNOWN, _("Could not open pipe: %s\n"), argv[0]); | ||
353 | } | ||
361 | 354 | ||
362 | if (out) | 355 | if (out) { |
363 | out->lines = _cmd_fetch_output (pfd_out[0], out, flags); | 356 | out->lines = _cmd_fetch_output(pfd_out[0], out, flags); |
364 | if (err) | 357 | } |
365 | err->lines = _cmd_fetch_output (pfd_err[0], err, flags); | 358 | if (err) { |
359 | err->lines = _cmd_fetch_output(pfd_err[0], err, flags); | ||
360 | } | ||
366 | 361 | ||
367 | return _cmd_close (fd); | 362 | return _cmd_close(fd); |
368 | } | 363 | } |
369 | 364 | ||
370 | int | 365 | int cmd_file_read(const char *filename, output *out, int flags) { |
371 | cmd_file_read ( char *filename, output *out, int flags) | ||
372 | { | ||
373 | int fd; | 366 | int fd; |
374 | if(out) | 367 | if (out) { |
375 | memset (out, 0, sizeof(output)); | 368 | memset(out, 0, sizeof(output)); |
369 | } | ||
376 | 370 | ||
377 | if ((fd = open(filename, O_RDONLY)) == -1) { | 371 | if ((fd = open(filename, O_RDONLY)) == -1) { |
378 | die( STATE_UNKNOWN, _("Error opening %s: %s"), filename, strerror(errno) ); | 372 | die(STATE_UNKNOWN, _("Error opening %s: %s"), filename, strerror(errno)); |
379 | } | 373 | } |
380 | 374 | ||
381 | if(out) | 375 | if (out) { |
382 | out->lines = _cmd_fetch_output (fd, out, flags); | 376 | out->lines = _cmd_fetch_output(fd, out, flags); |
377 | } | ||
383 | 378 | ||
384 | if (close(fd) == -1) | 379 | if (close(fd) == -1) { |
385 | die( STATE_UNKNOWN, _("Error closing %s: %s"), filename, strerror(errno) ); | 380 | die(STATE_UNKNOWN, _("Error closing %s: %s"), filename, strerror(errno)); |
381 | } | ||
386 | 382 | ||
387 | return 0; | 383 | return 0; |
388 | } | 384 | } |
389 | 385 | ||
390 | void | 386 | void timeout_alarm_handler(int signo) { |
391 | timeout_alarm_handler (int signo) | ||
392 | { | ||
393 | if (signo == SIGALRM) { | 387 | if (signo == SIGALRM) { |
394 | printf (_("%s - Plugin timed out after %d seconds\n"), | 388 | printf(_("%s - Plugin timed out after %d seconds\n"), state_text(timeout_state), |
395 | state_text(timeout_state), timeout_interval); | 389 | timeout_interval); |
396 | 390 | ||
397 | long maxfd = mp_open_max(); | 391 | long maxfd = mp_open_max(); |
398 | if(_cmd_pids) for(long int i = 0; i < maxfd; i++) { | 392 | if (_cmd_pids) { |
399 | if(_cmd_pids[i] != 0) kill(_cmd_pids[i], SIGKILL); | 393 | for (long int i = 0; i < maxfd; i++) { |
394 | if (_cmd_pids[i] != 0) { | ||
395 | kill(_cmd_pids[i], SIGKILL); | ||
396 | } | ||
397 | } | ||
400 | } | 398 | } |
401 | 399 | ||
402 | exit (timeout_state); | 400 | exit(timeout_state); |
403 | } | 401 | } |
404 | } | 402 | } |