summaryrefslogtreecommitdiffstats
path: root/plugins/popen.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/popen.c')
-rw-r--r--plugins/popen.c97
1 files changed, 10 insertions, 87 deletions
diff --git a/plugins/popen.c b/plugins/popen.c
index 592263f..036bc60 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 */
44extern int timeout_interval;
45extern pid_t *childpid; 46extern pid_t *childpid;
46extern int *child_stderr_array; 47extern int *child_stderr_array;
47extern FILE *child_process; 48extern FILE *child_process;
@@ -49,9 +50,9 @@ extern FILE *child_process;
49FILE *spopen (const char *); 50FILE *spopen (const char *);
50int spclose (FILE *); 51int spclose (FILE *);
51#ifdef REDHAT_SPOPEN_ERROR 52#ifdef REDHAT_SPOPEN_ERROR
52RETSIGTYPE popen_sigchld_handler (int); 53void popen_sigchld_handler (int);
53#endif 54#endif
54RETSIGTYPE popen_timeout_alarm_handler (int); 55void popen_timeout_alarm_handler (int);
55 56
56#include <stdarg.h> /* ANSI C header file */ 57#include <stdarg.h> /* ANSI C header file */
57#include <fcntl.h> 58#include <fcntl.h>
@@ -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))
81int open_max (void); /* {Prog openmax} */
82static void err_sys (const char *, ...) __attribute__((noreturn,format(printf, 1, 2)));
83char *rtrim (char *, const char *);
84 80
85char *pname = NULL; /* caller can set this from argv[0] */ 81char *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 */
89static int maxfd; /* from our open_max(), {Prog openmax} */
90
91#ifdef REDHAT_SPOPEN_ERROR 83#ifdef REDHAT_SPOPEN_ERROR
92static volatile int childtermd = 0; 84static 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,17 +265,8 @@ spclose (FILE * fp)
273 return (1); 265 return (1);
274} 266}
275 267
276#ifdef OPEN_MAX
277static int openmax = OPEN_MAX;
278#else
279static 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
286RETSIGTYPE 269void
287popen_sigchld_handler (int signo) 270popen_sigchld_handler (int signo)
288{ 271{
289 if (signo == SIGCHLD) 272 if (signo == SIGCHLD)
@@ -291,7 +274,7 @@ popen_sigchld_handler (int signo)
291} 274}
292#endif 275#endif
293 276
294RETSIGTYPE 277void
295popen_timeout_alarm_handler (int signo) 278popen_timeout_alarm_handler (int signo)
296{ 279{
297 int fh; 280 int fh;
@@ -309,63 +292,3 @@ popen_timeout_alarm_handler (int signo)
309 exit (STATE_CRITICAL); 292 exit (STATE_CRITICAL);
310 } 293 }
311} 294}
312
313
314int
315open_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
334static void
335err_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
357char *
358rtrim (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}