diff options
Diffstat (limited to 'plugins/popen.c')
-rw-r--r-- | plugins/popen.c | 89 |
1 files changed, 6 insertions, 83 deletions
diff --git a/plugins/popen.c b/plugins/popen.c index 592263f..b395f14 100644 --- a/plugins/popen.c +++ b/plugins/popen.c | |||
@@ -14,7 +14,7 @@ | |||
14 | * FILE * spopen(const char *); | 14 | * FILE * spopen(const char *); |
15 | * int spclose(FILE *); | 15 | * int spclose(FILE *); |
16 | * | 16 | * |
17 | * Code taken with liitle modification from "Advanced Programming for the Unix | 17 | * Code taken with little modification from "Advanced Programming for the Unix |
18 | * Environment" by W. Richard Stevens | 18 | * Environment" by W. Richard Stevens |
19 | * | 19 | * |
20 | * This is considered safe in that no shell is spawned, and the environment | 20 | * This is considered safe in that no shell is spawned, and the environment |
@@ -38,10 +38,11 @@ | |||
38 | * | 38 | * |
39 | *****************************************************************************/ | 39 | *****************************************************************************/ |
40 | 40 | ||
41 | #include "common.h" | 41 | #include "./common.h" |
42 | #include "./utils.h" | ||
43 | #include "../lib/maxfd.h" | ||
42 | 44 | ||
43 | /* extern so plugin has pid to kill exec'd process on timeouts */ | 45 | /* extern so plugin has pid to kill exec'd process on timeouts */ |
44 | extern int timeout_interval; | ||
45 | extern pid_t *childpid; | 46 | extern pid_t *childpid; |
46 | extern int *child_stderr_array; | 47 | extern int *child_stderr_array; |
47 | extern FILE *child_process; | 48 | extern FILE *child_process; |
@@ -76,18 +77,9 @@ RETSIGTYPE popen_timeout_alarm_handler (int); | |||
76 | #define SIG_ERR ((Sigfunc *)-1) | 77 | #define SIG_ERR ((Sigfunc *)-1) |
77 | #endif | 78 | #endif |
78 | 79 | ||
79 | #define min(a,b) ((a) < (b) ? (a) : (b)) | ||
80 | #define max(a,b) ((a) > (b) ? (a) : (b)) | ||
81 | int open_max (void); /* {Prog openmax} */ | ||
82 | static void err_sys (const char *, ...) __attribute__((noreturn,format(printf, 1, 2))); | ||
83 | char *rtrim (char *, const char *); | ||
84 | 80 | ||
85 | char *pname = NULL; /* caller can set this from argv[0] */ | 81 | char *pname = NULL; /* caller can set this from argv[0] */ |
86 | 82 | ||
87 | /*int *childerr = NULL;*//* ptr to array allocated at run-time */ | ||
88 | /*extern pid_t *childpid = NULL; *//* ptr to array allocated at run-time */ | ||
89 | static int maxfd; /* from our open_max(), {Prog openmax} */ | ||
90 | |||
91 | #ifdef REDHAT_SPOPEN_ERROR | 83 | #ifdef REDHAT_SPOPEN_ERROR |
92 | static volatile int childtermd = 0; | 84 | static volatile int childtermd = 0; |
93 | #endif | 85 | #endif |
@@ -186,14 +178,14 @@ spopen (const char *cmdstring) | |||
186 | } | 178 | } |
187 | argv[i] = NULL; | 179 | argv[i] = NULL; |
188 | 180 | ||
181 | long maxfd = mp_open_max(); | ||
182 | |||
189 | if (childpid == NULL) { /* first time through */ | 183 | if (childpid == NULL) { /* first time through */ |
190 | maxfd = open_max (); /* allocate zeroed out array for child pids */ | ||
191 | if ((childpid = calloc ((size_t)maxfd, sizeof (pid_t))) == NULL) | 184 | if ((childpid = calloc ((size_t)maxfd, sizeof (pid_t))) == NULL) |
192 | return (NULL); | 185 | return (NULL); |
193 | } | 186 | } |
194 | 187 | ||
195 | if (child_stderr_array == NULL) { /* first time through */ | 188 | if (child_stderr_array == NULL) { /* first time through */ |
196 | maxfd = open_max (); /* allocate zeroed out array for child pids */ | ||
197 | if ((child_stderr_array = calloc ((size_t)maxfd, sizeof (int))) == NULL) | 189 | if ((child_stderr_array = calloc ((size_t)maxfd, sizeof (int))) == NULL) |
198 | return (NULL); | 190 | return (NULL); |
199 | } | 191 | } |
@@ -273,15 +265,6 @@ spclose (FILE * fp) | |||
273 | return (1); | 265 | return (1); |
274 | } | 266 | } |
275 | 267 | ||
276 | #ifdef OPEN_MAX | ||
277 | static int openmax = OPEN_MAX; | ||
278 | #else | ||
279 | static int openmax = 0; | ||
280 | #endif | ||
281 | |||
282 | #define OPEN_MAX_GUESS 256 /* if OPEN_MAX is indeterminate */ | ||
283 | /* no guarantee this is adequate */ | ||
284 | |||
285 | #ifdef REDHAT_SPOPEN_ERROR | 268 | #ifdef REDHAT_SPOPEN_ERROR |
286 | RETSIGTYPE | 269 | RETSIGTYPE |
287 | popen_sigchld_handler (int signo) | 270 | popen_sigchld_handler (int signo) |
@@ -309,63 +292,3 @@ popen_timeout_alarm_handler (int signo) | |||
309 | exit (STATE_CRITICAL); | 292 | exit (STATE_CRITICAL); |
310 | } | 293 | } |
311 | } | 294 | } |
312 | |||
313 | |||
314 | int | ||
315 | open_max (void) | ||
316 | { | ||
317 | if (openmax == 0) { /* first time through */ | ||
318 | errno = 0; | ||
319 | if ((openmax = sysconf (_SC_OPEN_MAX)) < 0) { | ||
320 | if (errno == 0) | ||
321 | openmax = OPEN_MAX_GUESS; /* it's indeterminate */ | ||
322 | else | ||
323 | err_sys (_("sysconf error for _SC_OPEN_MAX")); | ||
324 | } | ||
325 | } | ||
326 | return (openmax); | ||
327 | } | ||
328 | |||
329 | |||
330 | /* Fatal error related to a system call. | ||
331 | * Print a message and die. */ | ||
332 | |||
333 | #define MAXLINE 2048 | ||
334 | static void | ||
335 | err_sys (const char *fmt, ...) | ||
336 | { | ||
337 | int errnoflag = 1; | ||
338 | int errno_save; | ||
339 | char buf[MAXLINE]; | ||
340 | |||
341 | va_list ap; | ||
342 | |||
343 | va_start (ap, fmt); | ||
344 | /* err_doit (1, fmt, ap); */ | ||
345 | errno_save = errno; /* value caller might want printed */ | ||
346 | vsprintf (buf, fmt, ap); | ||
347 | if (errnoflag) | ||
348 | sprintf (buf + strlen (buf), ": %s", strerror (errno_save)); | ||
349 | strcat (buf, "\n"); | ||
350 | fflush (stdout); /* in case stdout and stderr are the same */ | ||
351 | fputs (buf, stderr); | ||
352 | fflush (NULL); /* flushes all stdio output streams */ | ||
353 | va_end (ap); | ||
354 | exit (1); | ||
355 | } | ||
356 | |||
357 | char * | ||
358 | rtrim (char *str, const char *tok) | ||
359 | { | ||
360 | int i = 0; | ||
361 | int j = sizeof (str); | ||
362 | |||
363 | while (str != NULL && i < j) { | ||
364 | if (*(str + i) == *tok) { | ||
365 | sprintf (str + i, "%s", "\0"); | ||
366 | return str; | ||
367 | } | ||
368 | i++; | ||
369 | } | ||
370 | return str; | ||
371 | } | ||