diff options
author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2023-09-23 08:47:01 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-23 08:47:01 (GMT) |
commit | 719e27ddc2f0b48bcd7fe5584b23e3ce83ddf291 (patch) | |
tree | 1ca6c622275cf8d57e0b6b6d7fb0da4cce8e9205 /lib/utils_cmd.c | |
parent | fe718dec117a7adb8a7107a4137a86c79b54d15f (diff) | |
parent | 4295decfbf06adfa1bf019d28e9044971607b2d6 (diff) | |
download | monitoring-plugins-719e27ddc2f0b48bcd7fe5584b23e3ce83ddf291.tar.gz |
Merge pull request #1924 from RincewindsHat/compiler_warnings_4_1
Centralise and refactor maxfd related functionality
Diffstat (limited to 'lib/utils_cmd.c')
-rw-r--r-- | lib/utils_cmd.c | 15 |
1 files changed, 7 insertions, 8 deletions
diff --git a/lib/utils_cmd.c b/lib/utils_cmd.c index 883f1ec..f66fd57 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 | } |