From 7fd0e6f36d90a341e0d9b418f1cd64a3a5472a94 Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 5 Mar 2023 15:37:47 +0100 Subject: Rework maxfd/open_max to avoid unused variables --- lib/Makefile.am | 2 +- lib/utils_cmd.c | 15 +++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) (limited to 'lib') 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 AM_CPPFLAGS = -DNP_STATE_DIR_PREFIX=\"$(localstatedir)\" \ -I$(srcdir) -I$(top_srcdir)/gl -I$(top_srcdir)/intl -I$(top_srcdir)/plugins -libmonitoringplug_a_SOURCES = utils_base.c utils_disk.c utils_tcp.c utils_cmd.c +libmonitoringplug_a_SOURCES = utils_base.c utils_disk.c utils_tcp.c utils_cmd.c maxfd.c EXTRA_DIST = utils_base.h utils_disk.h utils_tcp.h utils_cmd.h parse_ini.h extra_opts.h if USE_PARSE_INI diff --git a/lib/utils_cmd.c b/lib/utils_cmd.c index 34fb3909..71da9d24 100644 --- a/lib/utils_cmd.c +++ b/lib/utils_cmd.c @@ -43,6 +43,9 @@ #include "utils.h" #include "utils_cmd.h" #include "utils_base.h" + +#include "./maxfd.h" + #include #ifdef HAVE_SYS_WAIT_H @@ -86,13 +89,7 @@ extern void die (int, const char *, ...) void cmd_init (void) { -#ifndef maxfd - if (!maxfd && (maxfd = sysconf (_SC_OPEN_MAX)) < 0) { - /* possibly log or emit a warning here, since there's no - * guarantee that our guess at maxfd will be adequate */ - maxfd = DEFAULT_MAXFD; - } -#endif + long maxfd = open_max(); /* if maxfd is unnaturally high, we force it to a lower value * ( e.g. on SunOS, when ulimit is set to unlimited: 2147483647 this would cause @@ -148,6 +145,7 @@ _cmd_open (char *const *argv, int *pfd, int *pfderr) /* close all descriptors in _cmd_pids[] * This is executed in a separate address space (pure child), * so we don't have to worry about async safety */ + long maxfd = open_max(); for (i = 0; i < maxfd; i++) if (_cmd_pids[i] > 0) close (i); @@ -174,6 +172,7 @@ _cmd_close (int fd) pid_t pid; /* make sure the provided fd was opened */ + long maxfd = open_max(); if (fd < 0 || fd > maxfd || !_cmd_pids || (pid = _cmd_pids[fd]) == 0) return -1; @@ -265,7 +264,6 @@ _cmd_fetch_output (int fd, output * op, int flags) int cmd_run (const char *cmdstring, output * out, output * err, int flags) { - int fd, pfd_out[2], pfd_err[2]; int i = 0, argc; size_t cmdlen; char **argv = NULL; @@ -387,6 +385,7 @@ timeout_alarm_handler (int signo) printf (_("%s - Plugin timed out after %d seconds\n"), state_text(timeout_state), timeout_interval); + long maxfd = open_max(); if(_cmd_pids) for(i = 0; i < maxfd; i++) { if(_cmd_pids[i] != 0) kill(_cmd_pids[i], SIGKILL); } -- cgit v1.2.3-74-g34f1