diff options
Diffstat (limited to 'lib/utils_cmd.c')
-rw-r--r-- | lib/utils_cmd.c | 40 |
1 files changed, 24 insertions, 16 deletions
diff --git a/lib/utils_cmd.c b/lib/utils_cmd.c index 34fb390..7957ec1 100644 --- a/lib/utils_cmd.c +++ b/lib/utils_cmd.c | |||
@@ -18,18 +18,18 @@ | |||
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 | * |
@@ -42,7 +42,20 @@ | |||
42 | #include "common.h" | 42 | #include "common.h" |
43 | #include "utils.h" | 43 | #include "utils.h" |
44 | #include "utils_cmd.h" | 44 | #include "utils_cmd.h" |
45 | /* This variable must be global, since there's no way the caller | ||
46 | * can forcibly slay a dead or ungainly running program otherwise. | ||
47 | * Multithreading apps and plugins can initialize it (via CMD_INIT) | ||
48 | * in an async safe manner PRIOR to calling cmd_run() or cmd_run_array() | ||
49 | * for the first time. | ||
50 | * | ||
51 | * The check for initialized values is atomic and can | ||
52 | * occur in any number of threads simultaneously. */ | ||
53 | static pid_t *_cmd_pids = NULL; | ||
54 | |||
45 | #include "utils_base.h" | 55 | #include "utils_base.h" |
56 | |||
57 | #include "./maxfd.h" | ||
58 | |||
46 | #include <fcntl.h> | 59 | #include <fcntl.h> |
47 | 60 | ||
48 | #ifdef HAVE_SYS_WAIT_H | 61 | #ifdef HAVE_SYS_WAIT_H |
@@ -86,13 +99,7 @@ extern void die (int, const char *, ...) | |||
86 | void | 99 | void |
87 | cmd_init (void) | 100 | cmd_init (void) |
88 | { | 101 | { |
89 | #ifndef maxfd | 102 | long maxfd = mp_open_max(); |
90 | if (!maxfd && (maxfd = sysconf (_SC_OPEN_MAX)) < 0) { | ||
91 | /* possibly log or emit a warning here, since there's no | ||
92 | * guarantee that our guess at maxfd will be adequate */ | ||
93 | maxfd = DEFAULT_MAXFD; | ||
94 | } | ||
95 | #endif | ||
96 | 103 | ||
97 | /* if maxfd is unnaturally high, we force it to a lower value | 104 | /* if maxfd is unnaturally high, we force it to a lower value |
98 | * ( e.g. on SunOS, when ulimit is set to unlimited: 2147483647 this would cause | 105 | * ( e.g. on SunOS, when ulimit is set to unlimited: 2147483647 this would cause |
@@ -148,6 +155,7 @@ _cmd_open (char *const *argv, int *pfd, int *pfderr) | |||
148 | /* close all descriptors in _cmd_pids[] | 155 | /* close all descriptors in _cmd_pids[] |
149 | * This is executed in a separate address space (pure child), | 156 | * This is executed in a separate address space (pure child), |
150 | * so we don't have to worry about async safety */ | 157 | * so we don't have to worry about async safety */ |
158 | long maxfd = mp_open_max(); | ||
151 | for (i = 0; i < maxfd; i++) | 159 | for (i = 0; i < maxfd; i++) |
152 | if (_cmd_pids[i] > 0) | 160 | if (_cmd_pids[i] > 0) |
153 | close (i); | 161 | close (i); |
@@ -174,6 +182,7 @@ _cmd_close (int fd) | |||
174 | pid_t pid; | 182 | pid_t pid; |
175 | 183 | ||
176 | /* make sure the provided fd was opened */ | 184 | /* make sure the provided fd was opened */ |
185 | long maxfd = mp_open_max(); | ||
177 | if (fd < 0 || fd > maxfd || !_cmd_pids || (pid = _cmd_pids[fd]) == 0) | 186 | if (fd < 0 || fd > maxfd || !_cmd_pids || (pid = _cmd_pids[fd]) == 0) |
178 | return -1; | 187 | return -1; |
179 | 188 | ||
@@ -265,7 +274,6 @@ _cmd_fetch_output (int fd, output * op, int flags) | |||
265 | int | 274 | int |
266 | cmd_run (const char *cmdstring, output * out, output * err, int flags) | 275 | cmd_run (const char *cmdstring, output * out, output * err, int flags) |
267 | { | 276 | { |
268 | int fd, pfd_out[2], pfd_err[2]; | ||
269 | int i = 0, argc; | 277 | int i = 0, argc; |
270 | size_t cmdlen; | 278 | size_t cmdlen; |
271 | char **argv = NULL; | 279 | char **argv = NULL; |
@@ -369,10 +377,10 @@ cmd_file_read ( char *filename, output *out, int flags) | |||
369 | if ((fd = open(filename, O_RDONLY)) == -1) { | 377 | if ((fd = open(filename, O_RDONLY)) == -1) { |
370 | die( STATE_UNKNOWN, _("Error opening %s: %s"), filename, strerror(errno) ); | 378 | die( STATE_UNKNOWN, _("Error opening %s: %s"), filename, strerror(errno) ); |
371 | } | 379 | } |
372 | 380 | ||
373 | if(out) | 381 | if(out) |
374 | out->lines = _cmd_fetch_output (fd, out, flags); | 382 | out->lines = _cmd_fetch_output (fd, out, flags); |
375 | 383 | ||
376 | if (close(fd) == -1) | 384 | if (close(fd) == -1) |
377 | die( STATE_UNKNOWN, _("Error closing %s: %s"), filename, strerror(errno) ); | 385 | die( STATE_UNKNOWN, _("Error closing %s: %s"), filename, strerror(errno) ); |
378 | 386 | ||
@@ -382,12 +390,12 @@ cmd_file_read ( char *filename, output *out, int flags) | |||
382 | void | 390 | void |
383 | timeout_alarm_handler (int signo) | 391 | timeout_alarm_handler (int signo) |
384 | { | 392 | { |
385 | size_t i; | ||
386 | if (signo == SIGALRM) { | 393 | if (signo == SIGALRM) { |
387 | printf (_("%s - Plugin timed out after %d seconds\n"), | 394 | printf (_("%s - Plugin timed out after %d seconds\n"), |
388 | state_text(timeout_state), timeout_interval); | 395 | state_text(timeout_state), timeout_interval); |
389 | 396 | ||
390 | if(_cmd_pids) for(i = 0; i < maxfd; i++) { | 397 | long maxfd = mp_open_max(); |
398 | if(_cmd_pids) for(long int i = 0; i < maxfd; i++) { | ||
391 | if(_cmd_pids[i] != 0) kill(_cmd_pids[i], SIGKILL); | 399 | if(_cmd_pids[i] != 0) kill(_cmd_pids[i], SIGKILL); |
392 | } | 400 | } |
393 | 401 | ||