diff options
author | Sven Nierlein <sven@nierlein.de> | 2019-04-25 11:03:10 (GMT) |
---|---|---|
committer | Sven Nierlein <sven@nierlein.org> | 2019-05-24 12:51:10 (GMT) |
commit | e8325b39c47e6fbf7c8c1e31f9026870d9520af5 (patch) | |
tree | 1c55421a51808253cbe59348e45bd0cb580354c5 /plugins/popen.c | |
parent | 4131f2f268e7d771490ebeadbae50b4f95d69695 (diff) | |
download | monitoring-plugins-e8325b39c47e6fbf7c8c1e31f9026870d9520af5.tar.gz |
fix maxfd being zero
If _SC_OPEN_MAX is available then maxfd was zero initialized and never set to the value from sysconf.
This leads to segfaults with free(): invalid size introduced by commit 7cafb0e84550035fe671662c293122be975065ca.
Signed-off-by: Sven Nierlein <sven@nierlein.de>
Diffstat (limited to 'plugins/popen.c')
-rw-r--r-- | plugins/popen.c | 52 |
1 files changed, 3 insertions, 49 deletions
diff --git a/plugins/popen.c b/plugins/popen.c index 116d168..557fb44 100644 --- a/plugins/popen.c +++ b/plugins/popen.c | |||
@@ -78,14 +78,9 @@ RETSIGTYPE popen_timeout_alarm_handler (int); | |||
78 | 78 | ||
79 | #define min(a,b) ((a) < (b) ? (a) : (b)) | 79 | #define min(a,b) ((a) < (b) ? (a) : (b)) |
80 | #define max(a,b) ((a) > (b) ? (a) : (b)) | 80 | #define max(a,b) ((a) > (b) ? (a) : (b)) |
81 | static void err_sys (const char *, ...) __attribute__((noreturn,format(printf, 1, 2))); | ||
82 | char *rtrim (char *, const char *); | ||
83 | 81 | ||
84 | char *pname = NULL; /* caller can set this from argv[0] */ | 82 | char *pname = NULL; /* caller can set this from argv[0] */ |
85 | 83 | ||
86 | /*int *childerr = NULL;*//* ptr to array allocated at run-time */ | ||
87 | /*extern pid_t *childpid = NULL; *//* ptr to array allocated at run-time */ | ||
88 | |||
89 | #ifdef REDHAT_SPOPEN_ERROR | 84 | #ifdef REDHAT_SPOPEN_ERROR |
90 | static volatile int childtermd = 0; | 85 | static volatile int childtermd = 0; |
91 | #endif | 86 | #endif |
@@ -184,6 +179,9 @@ spopen (const char *cmdstring) | |||
184 | } | 179 | } |
185 | argv[i] = NULL; | 180 | argv[i] = NULL; |
186 | 181 | ||
182 | if(maxfd == 0) | ||
183 | maxfd = open_max(); | ||
184 | |||
187 | if (childpid == NULL) { /* first time through */ | 185 | if (childpid == NULL) { /* first time through */ |
188 | if ((childpid = calloc ((size_t)maxfd, sizeof (pid_t))) == NULL) | 186 | if ((childpid = calloc ((size_t)maxfd, sizeof (pid_t))) == NULL) |
189 | return (NULL); | 187 | return (NULL); |
@@ -296,47 +294,3 @@ popen_timeout_alarm_handler (int signo) | |||
296 | exit (STATE_CRITICAL); | 294 | exit (STATE_CRITICAL); |
297 | } | 295 | } |
298 | } | 296 | } |
299 | |||
300 | |||
301 | /* Fatal error related to a system call. | ||
302 | * Print a message and die. */ | ||
303 | |||
304 | #define MAXLINE 2048 | ||
305 | static void | ||
306 | err_sys (const char *fmt, ...) | ||
307 | { | ||
308 | int errnoflag = 1; | ||
309 | int errno_save; | ||
310 | char buf[MAXLINE]; | ||
311 | |||
312 | va_list ap; | ||
313 | |||
314 | va_start (ap, fmt); | ||
315 | /* err_doit (1, fmt, ap); */ | ||
316 | errno_save = errno; /* value caller might want printed */ | ||
317 | vsprintf (buf, fmt, ap); | ||
318 | if (errnoflag) | ||
319 | sprintf (buf + strlen (buf), ": %s", strerror (errno_save)); | ||
320 | strcat (buf, "\n"); | ||
321 | fflush (stdout); /* in case stdout and stderr are the same */ | ||
322 | fputs (buf, stderr); | ||
323 | fflush (NULL); /* flushes all stdio output streams */ | ||
324 | va_end (ap); | ||
325 | exit (1); | ||
326 | } | ||
327 | |||
328 | char * | ||
329 | rtrim (char *str, const char *tok) | ||
330 | { | ||
331 | int i = 0; | ||
332 | int j = sizeof (str); | ||
333 | |||
334 | while (str != NULL && i < j) { | ||
335 | if (*(str + i) == *tok) { | ||
336 | sprintf (str + i, "%s", "\0"); | ||
337 | return str; | ||
338 | } | ||
339 | i++; | ||
340 | } | ||
341 | return str; | ||
342 | } | ||