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 --- plugins/common.h | 14 -------------- plugins/popen.c | 8 ++++---- plugins/runcmd.c | 6 ++++-- plugins/utils.c | 16 ---------------- plugins/utils.h | 2 -- 5 files changed, 8 insertions(+), 38 deletions(-) (limited to 'plugins') diff --git a/plugins/common.h b/plugins/common.h index 0f08e2f6..6bf4fca4 100644 --- a/plugins/common.h +++ b/plugins/common.h @@ -225,18 +225,4 @@ enum { # define __attribute__(x) /* do nothing */ #endif -/* 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 - #endif /* _COMMON_H_ */ diff --git a/plugins/popen.c b/plugins/popen.c index 723817d5..7703afc8 100644 --- a/plugins/popen.c +++ b/plugins/popen.c @@ -38,8 +38,9 @@ * *****************************************************************************/ -#include "common.h" -#include "utils.h" +#include "./common.h" +#include "./utils.h" +#include "../lib/maxfd.h" /* extern so plugin has pid to kill exec'd process on timeouts */ extern pid_t *childpid; @@ -177,8 +178,7 @@ spopen (const char *cmdstring) } argv[i] = NULL; - if(maxfd == 0) - maxfd = open_max(); + long maxfd = open_max(); if (childpid == NULL) { /* first time through */ if ((childpid = calloc ((size_t)maxfd, sizeof (pid_t))) == NULL) diff --git a/plugins/runcmd.c b/plugins/runcmd.c index 102191e4..98161421 100644 --- a/plugins/runcmd.c +++ b/plugins/runcmd.c @@ -88,8 +88,7 @@ extern void die (int, const char *, ...) * through this api and thus achieve async-safeness throughout the api */ void np_runcmd_init(void) { - if(maxfd == 0) - maxfd = open_max(); + long maxfd = open_max(); if(!np_pids) np_pids = calloc(maxfd, sizeof(pid_t)); } @@ -192,6 +191,7 @@ np_runcmd_open(const char *cmdstring, int *pfd, int *pfderr) /* close all descriptors in np_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(np_pids[i] > 0) close (i); @@ -219,6 +219,7 @@ np_runcmd_close(int fd) pid_t pid; /* make sure this fd was opened by popen() */ + long maxfd = open_max(); if(fd < 0 || fd > maxfd || !np_pids || (pid = np_pids[fd]) == 0) return -1; @@ -242,6 +243,7 @@ runcmd_timeout_alarm_handler (int signo) if (signo == SIGALRM) puts(_("CRITICAL - Plugin timed out while executing system call")); + long maxfd = open_max(); if(np_pids) for(i = 0; i < maxfd; i++) { if(np_pids[i] != 0) kill(np_pids[i], SIGKILL); } diff --git a/plugins/utils.c b/plugins/utils.c index b4214c61..71c0bdd8 100644 --- a/plugins/utils.c +++ b/plugins/utils.c @@ -804,19 +804,3 @@ char *sperfdata_int (const char *label, return data; } - -int -open_max (void) -{ - errno = 0; - if (maxfd > 0) - return(maxfd); - - if ((maxfd = sysconf (_SC_OPEN_MAX)) < 0) { - if (errno == 0) - maxfd = DEFAULT_MAXFD; /* it's indeterminate */ - else - die (STATE_UNKNOWN, _("sysconf error for _SC_OPEN_MAX\n")); - } - return(maxfd); -} diff --git a/plugins/utils.h b/plugins/utils.h index c76b3216..cb979ce7 100644 --- a/plugins/utils.h +++ b/plugins/utils.h @@ -106,8 +106,6 @@ char *sperfdata (const char *, double, const char *, char *, char *, char *sperfdata_int (const char *, int, const char *, char *, char *, int, int, int, int); -int open_max (void); - /* The idea here is that, although not every plugin will use all of these, most will or should. Therefore, for consistency, these very common options should have only these meanings throughout the overall suite */ -- cgit v1.2.3-74-g34f1 From 4295decfbf06adfa1bf019d28e9044971607b2d6 Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Sat, 23 Sep 2023 10:33:06 +0200 Subject: open_max is a library function now, it should be mp_open_max --- lib/maxfd.c | 2 +- lib/maxfd.h | 2 +- lib/utils_cmd.c | 8 ++++---- plugins/popen.c | 2 +- plugins/runcmd.c | 8 ++++---- 5 files changed, 11 insertions(+), 11 deletions(-) (limited to 'plugins') diff --git a/lib/maxfd.c b/lib/maxfd.c index dcd4d3db..529b3568 100644 --- a/lib/maxfd.c +++ b/lib/maxfd.c @@ -1,7 +1,7 @@ #include "./maxfd.h" #include -long open_max (void) { +long mp_open_max (void) { long maxfd = 0L; /* 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 diff --git a/lib/maxfd.h b/lib/maxfd.h index 0d734c5c..45218d0f 100644 --- a/lib/maxfd.h +++ b/lib/maxfd.h @@ -4,6 +4,6 @@ #define DEFAULT_MAXFD 256 /* fallback value if no max open files value is set */ #define MAXFD_LIMIT 8192 /* upper limit of open files */ -long open_max (void); +long mp_open_max (void); #endif // _MAXFD_ diff --git a/lib/utils_cmd.c b/lib/utils_cmd.c index 71da9d24..ef7053a5 100644 --- a/lib/utils_cmd.c +++ b/lib/utils_cmd.c @@ -89,7 +89,7 @@ extern void die (int, const char *, ...) void cmd_init (void) { - long maxfd = open_max(); + long maxfd = mp_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 @@ -145,7 +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(); + long maxfd = mp_open_max(); for (i = 0; i < maxfd; i++) if (_cmd_pids[i] > 0) close (i); @@ -172,7 +172,7 @@ _cmd_close (int fd) pid_t pid; /* make sure the provided fd was opened */ - long maxfd = open_max(); + long maxfd = mp_open_max(); if (fd < 0 || fd > maxfd || !_cmd_pids || (pid = _cmd_pids[fd]) == 0) return -1; @@ -385,7 +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(); + long maxfd = mp_open_max(); if(_cmd_pids) for(i = 0; i < maxfd; i++) { if(_cmd_pids[i] != 0) kill(_cmd_pids[i], SIGKILL); } diff --git a/plugins/popen.c b/plugins/popen.c index 7703afc8..b395f14a 100644 --- a/plugins/popen.c +++ b/plugins/popen.c @@ -178,7 +178,7 @@ spopen (const char *cmdstring) } argv[i] = NULL; - long maxfd = open_max(); + long maxfd = mp_open_max(); if (childpid == NULL) { /* first time through */ if ((childpid = calloc ((size_t)maxfd, sizeof (pid_t))) == NULL) diff --git a/plugins/runcmd.c b/plugins/runcmd.c index 98161421..bc0a4974 100644 --- a/plugins/runcmd.c +++ b/plugins/runcmd.c @@ -88,7 +88,7 @@ extern void die (int, const char *, ...) * through this api and thus achieve async-safeness throughout the api */ void np_runcmd_init(void) { - long maxfd = open_max(); + long maxfd = mp_open_max(); if(!np_pids) np_pids = calloc(maxfd, sizeof(pid_t)); } @@ -191,7 +191,7 @@ np_runcmd_open(const char *cmdstring, int *pfd, int *pfderr) /* close all descriptors in np_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(); + long maxfd = mp_open_max(); for (i = 0; i < maxfd; i++) if(np_pids[i] > 0) close (i); @@ -219,7 +219,7 @@ np_runcmd_close(int fd) pid_t pid; /* make sure this fd was opened by popen() */ - long maxfd = open_max(); + long maxfd = mp_open_max(); if(fd < 0 || fd > maxfd || !np_pids || (pid = np_pids[fd]) == 0) return -1; @@ -243,7 +243,7 @@ runcmd_timeout_alarm_handler (int signo) if (signo == SIGALRM) puts(_("CRITICAL - Plugin timed out while executing system call")); - long maxfd = open_max(); + long maxfd = mp_open_max(); if(np_pids) for(i = 0; i < maxfd; i++) { if(np_pids[i] != 0) kill(np_pids[i], SIGKILL); } -- cgit v1.2.3-74-g34f1