summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorSven Nierlein <sven@nierlein.de>2019-02-15 09:36:28 (GMT)
committerSven Nierlein <sven@nierlein.de>2019-02-15 09:36:28 (GMT)
commit7cafb0e84550035fe671662c293122be975065ca (patch)
tree5d80a8e80854edc41a72da668a89485505d7e5dc /lib
parent2962148d8bac3a65f1fb5e1a948e3f0a86a42a1a (diff)
downloadmonitoring-plugins-7cafb0e84550035fe671662c293122be975065ca.tar.gz
check_by_ssh: fix child process leak on timeoutsrefs/pull/1583/head
When check_by_ssh runs into a timeout it simply exits keeping all child processes running. Simply adopting the kill loop from runcmd_timeout_alarm_handler() fixes this. Signed-off-by: Sven Nierlein <sven@nierlein.de>
Diffstat (limited to 'lib')
-rw-r--r--lib/utils_base.c19
-rw-r--r--lib/utils_base.h5
-rw-r--r--lib/utils_cmd.c42
-rw-r--r--lib/utils_cmd.h13
4 files changed, 54 insertions, 25 deletions
diff --git a/lib/utils_base.c b/lib/utils_base.c
index 19a531f..fd7058d 100644
--- a/lib/utils_base.c
+++ b/lib/utils_base.c
@@ -37,6 +37,9 @@
37 37
38monitoring_plugin *this_monitoring_plugin=NULL; 38monitoring_plugin *this_monitoring_plugin=NULL;
39 39
40unsigned int timeout_state = STATE_CRITICAL;
41unsigned int timeout_interval = DEFAULT_SOCKET_TIMEOUT;
42
40int _np_state_read_file(FILE *); 43int _np_state_read_file(FILE *);
41 44
42void np_init( char *plugin_name, int argc, char **argv ) { 45void np_init( char *plugin_name, int argc, char **argv ) {
@@ -359,6 +362,22 @@ char *np_extract_value(const char *varlist, const char *name, char sep) {
359 return value; 362 return value;
360} 363}
361 364
365const char *
366state_text (int result)
367{
368 switch (result) {
369 case STATE_OK:
370 return "OK";
371 case STATE_WARNING:
372 return "WARNING";
373 case STATE_CRITICAL:
374 return "CRITICAL";
375 case STATE_DEPENDENT:
376 return "DEPENDENT";
377 default:
378 return "UNKNOWN";
379 }
380}
362 381
363/* 382/*
364 * Read a string representing a state (ok, warning... or numeric: 0, 1) and 383 * Read a string representing a state (ok, warning... or numeric: 0, 1) and
diff --git a/lib/utils_base.h b/lib/utils_base.h
index 42ae0c0..d7e7dff 100644
--- a/lib/utils_base.h
+++ b/lib/utils_base.h
@@ -61,6 +61,10 @@ void print_thresholds(const char *, thresholds *);
61int check_range(double, range *); 61int check_range(double, range *);
62int get_status(double, thresholds *); 62int get_status(double, thresholds *);
63 63
64/* Handle timeouts */
65extern unsigned int timeout_state;
66extern unsigned int timeout_interval;
67
64/* All possible characters in a threshold range */ 68/* All possible characters in a threshold range */
65#define NP_THRESHOLDS_CHARS "-0123456789.:@~" 69#define NP_THRESHOLDS_CHARS "-0123456789.:@~"
66 70
@@ -107,5 +111,6 @@ void np_state_write_string(time_t, char *);
107void np_init(char *, int argc, char **argv); 111void np_init(char *, int argc, char **argv);
108void np_set_args(int argc, char **argv); 112void np_set_args(int argc, char **argv);
109void np_cleanup(); 113void np_cleanup();
114const char *state_text (int);
110 115
111#endif /* _UTILS_BASE_ */ 116#endif /* _UTILS_BASE_ */
diff --git a/lib/utils_cmd.c b/lib/utils_cmd.c
index 7eb9a3a..795840d 100644
--- a/lib/utils_cmd.c
+++ b/lib/utils_cmd.c
@@ -40,6 +40,7 @@
40 40
41/** includes **/ 41/** includes **/
42#include "common.h" 42#include "common.h"
43#include "utils.h"
43#include "utils_cmd.h" 44#include "utils_cmd.h"
44#include "utils_base.h" 45#include "utils_base.h"
45#include <fcntl.h> 46#include <fcntl.h>
@@ -65,31 +66,6 @@ extern char **environ;
65# define SIG_ERR ((Sigfunc *)-1) 66# define SIG_ERR ((Sigfunc *)-1)
66#endif 67#endif
67 68
68/* This variable must be global, since there's no way the caller
69 * can forcibly slay a dead or ungainly running program otherwise.
70 * Multithreading apps and plugins can initialize it (via CMD_INIT)
71 * in an async safe manner PRIOR to calling cmd_run() or cmd_run_array()
72 * for the first time.
73 *
74 * The check for initialized values is atomic and can
75 * occur in any number of threads simultaneously. */
76static pid_t *_cmd_pids = NULL;
77
78/* Try sysconf(_SC_OPEN_MAX) first, as it can be higher than OPEN_MAX.
79 * If that fails and the macro isn't defined, we fall back to an educated
80 * guess. There's no guarantee that our guess is adequate and the program
81 * will die with SIGSEGV if it isn't and the upper boundary is breached. */
82#define DEFAULT_MAXFD 256 /* fallback value if no max open files value is set */
83#define MAXFD_LIMIT 8192 /* upper limit of open files */
84#ifdef _SC_OPEN_MAX
85static long maxfd = 0;
86#elif defined(OPEN_MAX)
87# define maxfd OPEN_MAX
88#else /* sysconf macro unavailable, so guess (may be wildly inaccurate) */
89# define maxfd DEFAULT_MAXFD
90#endif
91
92
93/** prototypes **/ 69/** prototypes **/
94static int _cmd_open (char *const *, int *, int *) 70static int _cmd_open (char *const *, int *, int *)
95 __attribute__ ((__nonnull__ (1, 2, 3))); 71 __attribute__ ((__nonnull__ (1, 2, 3)));
@@ -406,3 +382,19 @@ cmd_file_read ( char *filename, output *out, int flags)
406 382
407 return 0; 383 return 0;
408} 384}
385
386void
387timeout_alarm_handler (int signo)
388{
389 size_t i;
390 if (signo == SIGALRM) {
391 printf (_("%s - Plugin timed out after %d seconds\n"),
392 state_text(timeout_state), timeout_interval);
393
394 if(_cmd_pids) for(i = 0; i < maxfd; i++) {
395 if(_cmd_pids[i] != 0) kill(_cmd_pids[i], SIGKILL);
396 }
397
398 exit (timeout_state);
399 }
400}
diff --git a/lib/utils_cmd.h b/lib/utils_cmd.h
index ebaf15b..6f3aeb8 100644
--- a/lib/utils_cmd.h
+++ b/lib/utils_cmd.h
@@ -32,4 +32,17 @@ void cmd_init (void);
32#define CMD_NO_ARRAYS 0x01 /* don't populate arrays at all */ 32#define CMD_NO_ARRAYS 0x01 /* don't populate arrays at all */
33#define CMD_NO_ASSOC 0x02 /* output.line won't point to buf */ 33#define CMD_NO_ASSOC 0x02 /* output.line won't point to buf */
34 34
35/* This variable must be global, since there's no way the caller
36 * can forcibly slay a dead or ungainly running program otherwise.
37 * Multithreading apps and plugins can initialize it (via CMD_INIT)
38 * in an async safe manner PRIOR to calling cmd_run() or cmd_run_array()
39 * for the first time.
40 *
41 * The check for initialized values is atomic and can
42 * occur in any number of threads simultaneously. */
43static pid_t *_cmd_pids = NULL;
44
45RETSIGTYPE timeout_alarm_handler (int);
46
47
35#endif /* _UTILS_CMD_ */ 48#endif /* _UTILS_CMD_ */