diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Makefile.am | 2 | ||||
-rw-r--r-- | lib/maxfd.c | 26 | ||||
-rw-r--r-- | lib/maxfd.h | 9 | ||||
-rw-r--r-- | lib/utils_cmd.c | 15 |
4 files changed, 43 insertions, 9 deletions
diff --git a/lib/Makefile.am b/lib/Makefile.am index 01d73a64..1a47395d 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am | |||
@@ -7,7 +7,7 @@ noinst_LIBRARIES = libmonitoringplug.a | |||
7 | AM_CPPFLAGS = -DNP_STATE_DIR_PREFIX=\"$(localstatedir)\" \ | 7 | AM_CPPFLAGS = -DNP_STATE_DIR_PREFIX=\"$(localstatedir)\" \ |
8 | -I$(srcdir) -I$(top_srcdir)/gl -I$(top_srcdir)/intl -I$(top_srcdir)/plugins | 8 | -I$(srcdir) -I$(top_srcdir)/gl -I$(top_srcdir)/intl -I$(top_srcdir)/plugins |
9 | 9 | ||
10 | libmonitoringplug_a_SOURCES = utils_base.c utils_disk.c utils_tcp.c utils_cmd.c | 10 | libmonitoringplug_a_SOURCES = utils_base.c utils_disk.c utils_tcp.c utils_cmd.c maxfd.c |
11 | EXTRA_DIST = utils_base.h utils_disk.h utils_tcp.h utils_cmd.h parse_ini.h extra_opts.h | 11 | EXTRA_DIST = utils_base.h utils_disk.h utils_tcp.h utils_cmd.h parse_ini.h extra_opts.h |
12 | 12 | ||
13 | if USE_PARSE_INI | 13 | if USE_PARSE_INI |
diff --git a/lib/maxfd.c b/lib/maxfd.c new file mode 100644 index 00000000..529b3568 --- /dev/null +++ b/lib/maxfd.c | |||
@@ -0,0 +1,26 @@ | |||
1 | #include "./maxfd.h" | ||
2 | #include <errno.h> | ||
3 | |||
4 | long mp_open_max (void) { | ||
5 | long maxfd = 0L; | ||
6 | /* Try sysconf(_SC_OPEN_MAX) first, as it can be higher than OPEN_MAX. | ||
7 | * If that fails and the macro isn't defined, we fall back to an educated | ||
8 | * guess. There's no guarantee that our guess is adequate and the program | ||
9 | * will die with SIGSEGV if it isn't and the upper boundary is breached. */ | ||
10 | |||
11 | #ifdef _SC_OPEN_MAX | ||
12 | errno = 0; | ||
13 | if ((maxfd = sysconf (_SC_OPEN_MAX)) < 0) { | ||
14 | if (errno == 0) | ||
15 | maxfd = DEFAULT_MAXFD; /* it's indeterminate */ | ||
16 | else | ||
17 | die (STATE_UNKNOWN, _("sysconf error for _SC_OPEN_MAX\n")); | ||
18 | } | ||
19 | #elif defined(OPEN_MAX) | ||
20 | return OPEN_MAX | ||
21 | #else /* sysconf macro unavailable, so guess (may be wildly inaccurate) */ | ||
22 | return DEFAULT_MAXFD; | ||
23 | #endif | ||
24 | |||
25 | return(maxfd); | ||
26 | } | ||
diff --git a/lib/maxfd.h b/lib/maxfd.h new file mode 100644 index 00000000..45218d0f --- /dev/null +++ b/lib/maxfd.h | |||
@@ -0,0 +1,9 @@ | |||
1 | #ifndef _MAXFD_ | ||
2 | #define _MAXFD_ | ||
3 | |||
4 | #define DEFAULT_MAXFD 256 /* fallback value if no max open files value is set */ | ||
5 | #define MAXFD_LIMIT 8192 /* upper limit of open files */ | ||
6 | |||
7 | long mp_open_max (void); | ||
8 | |||
9 | #endif // _MAXFD_ | ||
diff --git a/lib/utils_cmd.c b/lib/utils_cmd.c index 883f1ec1..f66fd573 100644 --- a/lib/utils_cmd.c +++ b/lib/utils_cmd.c | |||
@@ -53,6 +53,9 @@ | |||
53 | static pid_t *_cmd_pids = NULL; | 53 | static pid_t *_cmd_pids = NULL; |
54 | 54 | ||
55 | #include "utils_base.h" | 55 | #include "utils_base.h" |
56 | |||
57 | #include "./maxfd.h" | ||
58 | |||
56 | #include <fcntl.h> | 59 | #include <fcntl.h> |
57 | 60 | ||
58 | #ifdef HAVE_SYS_WAIT_H | 61 | #ifdef HAVE_SYS_WAIT_H |
@@ -96,13 +99,7 @@ extern void die (int, const char *, ...) | |||
96 | void | 99 | void |
97 | cmd_init (void) | 100 | cmd_init (void) |
98 | { | 101 | { |
99 | #ifndef maxfd | 102 | long maxfd = mp_open_max(); |
100 | if (!maxfd && (maxfd = sysconf (_SC_OPEN_MAX)) < 0) { | ||
101 | /* possibly log or emit a warning here, since there's no | ||
102 | * guarantee that our guess at maxfd will be adequate */ | ||
103 | maxfd = DEFAULT_MAXFD; | ||
104 | } | ||
105 | #endif | ||
106 | 103 | ||
107 | /* if maxfd is unnaturally high, we force it to a lower value | 104 | /* if maxfd is unnaturally high, we force it to a lower value |
108 | * ( e.g. on SunOS, when ulimit is set to unlimited: 2147483647 this would cause | 105 | * ( e.g. on SunOS, when ulimit is set to unlimited: 2147483647 this would cause |
@@ -158,6 +155,7 @@ _cmd_open (char *const *argv, int *pfd, int *pfderr) | |||
158 | /* close all descriptors in _cmd_pids[] | 155 | /* close all descriptors in _cmd_pids[] |
159 | * This is executed in a separate address space (pure child), | 156 | * This is executed in a separate address space (pure child), |
160 | * so we don't have to worry about async safety */ | 157 | * so we don't have to worry about async safety */ |
158 | long maxfd = mp_open_max(); | ||
161 | for (i = 0; i < maxfd; i++) | 159 | for (i = 0; i < maxfd; i++) |
162 | if (_cmd_pids[i] > 0) | 160 | if (_cmd_pids[i] > 0) |
163 | close (i); | 161 | close (i); |
@@ -184,6 +182,7 @@ _cmd_close (int fd) | |||
184 | pid_t pid; | 182 | pid_t pid; |
185 | 183 | ||
186 | /* make sure the provided fd was opened */ | 184 | /* make sure the provided fd was opened */ |
185 | long maxfd = mp_open_max(); | ||
187 | if (fd < 0 || fd > maxfd || !_cmd_pids || (pid = _cmd_pids[fd]) == 0) | 186 | if (fd < 0 || fd > maxfd || !_cmd_pids || (pid = _cmd_pids[fd]) == 0) |
188 | return -1; | 187 | return -1; |
189 | 188 | ||
@@ -275,7 +274,6 @@ _cmd_fetch_output (int fd, output * op, int flags) | |||
275 | int | 274 | int |
276 | cmd_run (const char *cmdstring, output * out, output * err, int flags) | 275 | cmd_run (const char *cmdstring, output * out, output * err, int flags) |
277 | { | 276 | { |
278 | int fd, pfd_out[2], pfd_err[2]; | ||
279 | int i = 0, argc; | 277 | int i = 0, argc; |
280 | size_t cmdlen; | 278 | size_t cmdlen; |
281 | char **argv = NULL; | 279 | char **argv = NULL; |
@@ -397,6 +395,7 @@ timeout_alarm_handler (int signo) | |||
397 | printf (_("%s - Plugin timed out after %d seconds\n"), | 395 | printf (_("%s - Plugin timed out after %d seconds\n"), |
398 | state_text(timeout_state), timeout_interval); | 396 | state_text(timeout_state), timeout_interval); |
399 | 397 | ||
398 | long maxfd = mp_open_max(); | ||
400 | if(_cmd_pids) for(i = 0; i < maxfd; i++) { | 399 | if(_cmd_pids) for(i = 0; i < maxfd; i++) { |
401 | if(_cmd_pids[i] != 0) kill(_cmd_pids[i], SIGKILL); | 400 | if(_cmd_pids[i] != 0) kill(_cmd_pids[i], SIGKILL); |
402 | } | 401 | } |