diff options
Diffstat (limited to 'lib/utils_cmd.c')
-rw-r--r-- | lib/utils_cmd.c | 29 |
1 files changed, 17 insertions, 12 deletions
diff --git a/lib/utils_cmd.c b/lib/utils_cmd.c index 8b8e5708..f66fd573 100644 --- a/lib/utils_cmd.c +++ b/lib/utils_cmd.c | |||
@@ -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 |
@@ -118,10 +125,6 @@ _cmd_open (char *const *argv, int *pfd, int *pfderr) | |||
118 | 125 | ||
119 | int i = 0; | 126 | int i = 0; |
120 | 127 | ||
121 | /* if no command was passed, return with no error */ | ||
122 | if (argv == NULL) | ||
123 | return -1; | ||
124 | |||
125 | if (!_cmd_pids) | 128 | if (!_cmd_pids) |
126 | CMD_INIT; | 129 | CMD_INIT; |
127 | 130 | ||
@@ -152,6 +155,7 @@ _cmd_open (char *const *argv, int *pfd, int *pfderr) | |||
152 | /* close all descriptors in _cmd_pids[] | 155 | /* close all descriptors in _cmd_pids[] |
153 | * This is executed in a separate address space (pure child), | 156 | * This is executed in a separate address space (pure child), |
154 | * 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(); | ||
155 | for (i = 0; i < maxfd; i++) | 159 | for (i = 0; i < maxfd; i++) |
156 | if (_cmd_pids[i] > 0) | 160 | if (_cmd_pids[i] > 0) |
157 | close (i); | 161 | close (i); |
@@ -178,6 +182,7 @@ _cmd_close (int fd) | |||
178 | pid_t pid; | 182 | pid_t pid; |
179 | 183 | ||
180 | /* make sure the provided fd was opened */ | 184 | /* make sure the provided fd was opened */ |
185 | long maxfd = mp_open_max(); | ||
181 | 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) |
182 | return -1; | 187 | return -1; |
183 | 188 | ||
@@ -269,7 +274,6 @@ _cmd_fetch_output (int fd, output * op, int flags) | |||
269 | int | 274 | int |
270 | cmd_run (const char *cmdstring, output * out, output * err, int flags) | 275 | cmd_run (const char *cmdstring, output * out, output * err, int flags) |
271 | { | 276 | { |
272 | int fd, pfd_out[2], pfd_err[2]; | ||
273 | int i = 0, argc; | 277 | int i = 0, argc; |
274 | size_t cmdlen; | 278 | size_t cmdlen; |
275 | char **argv = NULL; | 279 | char **argv = NULL; |
@@ -391,6 +395,7 @@ timeout_alarm_handler (int signo) | |||
391 | printf (_("%s - Plugin timed out after %d seconds\n"), | 395 | printf (_("%s - Plugin timed out after %d seconds\n"), |
392 | state_text(timeout_state), timeout_interval); | 396 | state_text(timeout_state), timeout_interval); |
393 | 397 | ||
398 | long maxfd = mp_open_max(); | ||
394 | if(_cmd_pids) for(i = 0; i < maxfd; i++) { | 399 | if(_cmd_pids) for(i = 0; i < maxfd; i++) { |
395 | if(_cmd_pids[i] != 0) kill(_cmd_pids[i], SIGKILL); | 400 | if(_cmd_pids[i] != 0) kill(_cmd_pids[i], SIGKILL); |
396 | } | 401 | } |