aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorGravatar Sven Nierlein <sven@nierlein.org> 2019-02-19 21:42:02 +0100
committerGravatar GitHub <noreply@github.com> 2019-02-19 21:42:02 +0100
commit931ed78b5dc062fff33652d87406f1547da5ddbe (patch)
tree9caf8031a15d7f046e77693c4002bf750bfef3d3 /lib
parent2813d08b92d08ba56ec22da00a23fff3a22ed74b (diff)
parent7cafb0e84550035fe671662c293122be975065ca (diff)
downloadmonitoring-plugins-931ed78b5dc062fff33652d87406f1547da5ddbe.tar.gz
Merge pull request #1583 from sni/fix_check_by_ssh_timeout_child_leak
check_by_ssh: fix child process leak on timeouts
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 19a531f5..fd7058da 100644
--- a/lib/utils_base.c
+++ b/lib/utils_base.c
@@ -37,6 +37,9 @@
monitoring_plugin *this_monitoring_plugin=NULL;
+unsigned int timeout_state = STATE_CRITICAL;
+unsigned int timeout_interval = DEFAULT_SOCKET_TIMEOUT;
+
int _np_state_read_file(FILE *);
void 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) {
return value;
}
+const char *
+state_text (int result)
+{
+ switch (result) {
+ case STATE_OK:
+ return "OK";
+ case STATE_WARNING:
+ return "WARNING";
+ case STATE_CRITICAL:
+ return "CRITICAL";
+ case STATE_DEPENDENT:
+ return "DEPENDENT";
+ default:
+ return "UNKNOWN";
+ }
+}
/*
* 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 42ae0c09..d7e7dffa 100644
--- a/lib/utils_base.h
+++ b/lib/utils_base.h
@@ -61,6 +61,10 @@ void print_thresholds(const char *, thresholds *);
int check_range(double, range *);
int get_status(double, thresholds *);
+/* Handle timeouts */
+extern unsigned int timeout_state;
+extern unsigned int timeout_interval;
+
/* All possible characters in a threshold range */
#define NP_THRESHOLDS_CHARS "-0123456789.:@~"
@@ -107,5 +111,6 @@ void np_state_write_string(time_t, char *);
void np_init(char *, int argc, char **argv);
void np_set_args(int argc, char **argv);
void np_cleanup();
+const char *state_text (int);
#endif /* _UTILS_BASE_ */
diff --git a/lib/utils_cmd.c b/lib/utils_cmd.c
index 7eb9a3a0..795840d3 100644
--- a/lib/utils_cmd.c
+++ b/lib/utils_cmd.c
@@ -40,6 +40,7 @@
/** includes **/
#include "common.h"
+#include "utils.h"
#include "utils_cmd.h"
#include "utils_base.h"
#include <fcntl.h>
@@ -65,31 +66,6 @@ extern char **environ;
# define SIG_ERR ((Sigfunc *)-1)
#endif
-/* This variable must be global, since there's no way the caller
- * can forcibly slay a dead or ungainly running program otherwise.
- * Multithreading apps and plugins can initialize it (via CMD_INIT)
- * in an async safe manner PRIOR to calling cmd_run() or cmd_run_array()
- * for the first time.
- *
- * The check for initialized values is atomic and can
- * occur in any number of threads simultaneously. */
-static pid_t *_cmd_pids = NULL;
-
-/* Try sysconf(_SC_OPEN_MAX) first, as it can be higher than OPEN_MAX.
- * If that fails and the macro isn't defined, we fall back to an educated
- * guess. There's no guarantee that our guess is adequate and the program
- * will die with SIGSEGV if it isn't and the upper boundary is breached. */
-#define DEFAULT_MAXFD 256 /* fallback value if no max open files value is set */
-#define MAXFD_LIMIT 8192 /* upper limit of open files */
-#ifdef _SC_OPEN_MAX
-static long maxfd = 0;
-#elif defined(OPEN_MAX)
-# define maxfd OPEN_MAX
-#else /* sysconf macro unavailable, so guess (may be wildly inaccurate) */
-# define maxfd DEFAULT_MAXFD
-#endif
-
-
/** prototypes **/
static int _cmd_open (char *const *, int *, int *)
__attribute__ ((__nonnull__ (1, 2, 3)));
@@ -406,3 +382,19 @@ cmd_file_read ( char *filename, output *out, int flags)
return 0;
}
+
+void
+timeout_alarm_handler (int signo)
+{
+ size_t i;
+ if (signo == SIGALRM) {
+ printf (_("%s - Plugin timed out after %d seconds\n"),
+ state_text(timeout_state), timeout_interval);
+
+ if(_cmd_pids) for(i = 0; i < maxfd; i++) {
+ if(_cmd_pids[i] != 0) kill(_cmd_pids[i], SIGKILL);
+ }
+
+ exit (timeout_state);
+ }
+}
diff --git a/lib/utils_cmd.h b/lib/utils_cmd.h
index ebaf15be..6f3aeb81 100644
--- a/lib/utils_cmd.h
+++ b/lib/utils_cmd.h
@@ -32,4 +32,17 @@ void cmd_init (void);
#define CMD_NO_ARRAYS 0x01 /* don't populate arrays at all */
#define CMD_NO_ASSOC 0x02 /* output.line won't point to buf */
+/* This variable must be global, since there's no way the caller
+ * can forcibly slay a dead or ungainly running program otherwise.
+ * Multithreading apps and plugins can initialize it (via CMD_INIT)
+ * in an async safe manner PRIOR to calling cmd_run() or cmd_run_array()
+ * for the first time.
+ *
+ * The check for initialized values is atomic and can
+ * occur in any number of threads simultaneously. */
+static pid_t *_cmd_pids = NULL;
+
+RETSIGTYPE timeout_alarm_handler (int);
+
+
#endif /* _UTILS_CMD_ */