diff options
Diffstat (limited to 'plugins/runcmd.c')
-rw-r--r-- | plugins/runcmd.c | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/plugins/runcmd.c b/plugins/runcmd.c index 102191e..ed49bb9 100644 --- a/plugins/runcmd.c +++ b/plugins/runcmd.c | |||
@@ -60,6 +60,8 @@ | |||
60 | # define SIG_ERR ((Sigfunc *)-1) | 60 | # define SIG_ERR ((Sigfunc *)-1) |
61 | #endif | 61 | #endif |
62 | 62 | ||
63 | #include "../lib/maxfd.h" | ||
64 | |||
63 | /* This variable must be global, since there's no way the caller | 65 | /* This variable must be global, since there's no way the caller |
64 | * can forcibly slay a dead or ungainly running program otherwise. | 66 | * can forcibly slay a dead or ungainly running program otherwise. |
65 | * Multithreading apps and plugins can initialize it (via NP_RUNCMD_INIT) | 67 | * Multithreading apps and plugins can initialize it (via NP_RUNCMD_INIT) |
@@ -88,8 +90,7 @@ extern void die (int, const char *, ...) | |||
88 | * through this api and thus achieve async-safeness throughout the api */ | 90 | * through this api and thus achieve async-safeness throughout the api */ |
89 | void np_runcmd_init(void) | 91 | void np_runcmd_init(void) |
90 | { | 92 | { |
91 | if(maxfd == 0) | 93 | long maxfd = mp_open_max(); |
92 | maxfd = open_max(); | ||
93 | if(!np_pids) np_pids = calloc(maxfd, sizeof(pid_t)); | 94 | if(!np_pids) np_pids = calloc(maxfd, sizeof(pid_t)); |
94 | } | 95 | } |
95 | 96 | ||
@@ -114,7 +115,7 @@ np_runcmd_open(const char *cmdstring, int *pfd, int *pfderr) | |||
114 | if(!np_pids) NP_RUNCMD_INIT; | 115 | if(!np_pids) NP_RUNCMD_INIT; |
115 | 116 | ||
116 | env[0] = strdup("LC_ALL=C"); | 117 | env[0] = strdup("LC_ALL=C"); |
117 | env[1] = '\0'; | 118 | env[1] = NULL; |
118 | 119 | ||
119 | /* make copy of command string so strtok() doesn't silently modify it */ | 120 | /* make copy of command string so strtok() doesn't silently modify it */ |
120 | /* (the calling program may want to access it later) */ | 121 | /* (the calling program may want to access it later) */ |
@@ -192,6 +193,7 @@ np_runcmd_open(const char *cmdstring, int *pfd, int *pfderr) | |||
192 | /* close all descriptors in np_pids[] | 193 | /* close all descriptors in np_pids[] |
193 | * This is executed in a separate address space (pure child), | 194 | * This is executed in a separate address space (pure child), |
194 | * so we don't have to worry about async safety */ | 195 | * so we don't have to worry about async safety */ |
196 | long maxfd = mp_open_max(); | ||
195 | for (i = 0; i < maxfd; i++) | 197 | for (i = 0; i < maxfd; i++) |
196 | if(np_pids[i] > 0) | 198 | if(np_pids[i] > 0) |
197 | close (i); | 199 | close (i); |
@@ -219,6 +221,7 @@ np_runcmd_close(int fd) | |||
219 | pid_t pid; | 221 | pid_t pid; |
220 | 222 | ||
221 | /* make sure this fd was opened by popen() */ | 223 | /* make sure this fd was opened by popen() */ |
224 | long maxfd = mp_open_max(); | ||
222 | if(fd < 0 || fd > maxfd || !np_pids || (pid = np_pids[fd]) == 0) | 225 | if(fd < 0 || fd > maxfd || !np_pids || (pid = np_pids[fd]) == 0) |
223 | return -1; | 226 | return -1; |
224 | 227 | ||
@@ -237,12 +240,12 @@ np_runcmd_close(int fd) | |||
237 | void | 240 | void |
238 | runcmd_timeout_alarm_handler (int signo) | 241 | runcmd_timeout_alarm_handler (int signo) |
239 | { | 242 | { |
240 | size_t i; | ||
241 | 243 | ||
242 | if (signo == SIGALRM) | 244 | if (signo == SIGALRM) |
243 | puts(_("CRITICAL - Plugin timed out while executing system call")); | 245 | puts(_("CRITICAL - Plugin timed out while executing system call")); |
244 | 246 | ||
245 | if(np_pids) for(i = 0; i < maxfd; i++) { | 247 | long maxfd = mp_open_max(); |
248 | if(np_pids) for(long int i = 0; i < maxfd; i++) { | ||
246 | if(np_pids[i] != 0) kill(np_pids[i], SIGKILL); | 249 | if(np_pids[i] != 0) kill(np_pids[i], SIGKILL); |
247 | } | 250 | } |
248 | 251 | ||