Merge pull request #1924 from RincewindsHat/compiler_warnings_4_1

Centralise and refactor maxfd related functionality
This commit is contained in:
Lorenz Kästle 2023-09-23 10:47:01 +02:00 committed by GitHub
commit 719e27ddc2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 54 additions and 59 deletions

View file

@ -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

26
lib/maxfd.c Normal file
View file

@ -0,0 +1,26 @@
#include "./maxfd.h"
#include <errno.h>
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
* 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. */
#ifdef _SC_OPEN_MAX
errno = 0;
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"));
}
#elif defined(OPEN_MAX)
return OPEN_MAX
#else /* sysconf macro unavailable, so guess (may be wildly inaccurate) */
return DEFAULT_MAXFD;
#endif
return(maxfd);
}

9
lib/maxfd.h Normal file
View file

@ -0,0 +1,9 @@
#ifndef _MAXFD_
#define _MAXFD_
#define DEFAULT_MAXFD 256 /* fallback value if no max open files value is set */
#define MAXFD_LIMIT 8192 /* upper limit of open files */
long mp_open_max (void);
#endif // _MAXFD_

View file

@ -53,6 +53,9 @@
static pid_t *_cmd_pids = NULL;
#include "utils_base.h"
#include "./maxfd.h"
#include <fcntl.h>
#ifdef HAVE_SYS_WAIT_H
@ -96,13 +99,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 = 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
@ -158,6 +155,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 = mp_open_max();
for (i = 0; i < maxfd; i++)
if (_cmd_pids[i] > 0)
close (i);
@ -184,6 +182,7 @@ _cmd_close (int fd)
pid_t pid;
/* make sure the provided fd was opened */
long maxfd = mp_open_max();
if (fd < 0 || fd > maxfd || !_cmd_pids || (pid = _cmd_pids[fd]) == 0)
return -1;
@ -275,7 +274,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;
@ -397,6 +395,7 @@ timeout_alarm_handler (int signo)
printf (_("%s - Plugin timed out after %d seconds\n"),
state_text(timeout_state), timeout_interval);
long maxfd = mp_open_max();
if(_cmd_pids) for(i = 0; i < maxfd; i++) {
if(_cmd_pids[i] != 0) kill(_cmd_pids[i], SIGKILL);
}

View file

@ -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_ */

View file

@ -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 = mp_open_max();
if (childpid == NULL) { /* first time through */
if ((childpid = calloc ((size_t)maxfd, sizeof (pid_t))) == NULL)

View file

@ -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 = mp_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 = mp_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 = mp_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 = mp_open_max();
if(np_pids) for(i = 0; i < maxfd; i++) {
if(np_pids[i] != 0) kill(np_pids[i], SIGKILL);
}

View file

@ -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);
}

View file

@ -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 */

View file

@ -9,7 +9,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: devel@monitoring-plugins.org\n"
"POT-Creation-Date: 2023-09-21 12:09+0200\n"
"POT-Creation-Date: 2023-09-22 15:36+0200\n"
"PO-Revision-Date: 2004-12-23 17:46+0100\n"
"Last-Translator: \n"
"Language-Team: Monitoring Plugin Development Team <devel@monitoring-plugins."
@ -4705,9 +4705,6 @@ msgstr "konnte keinen Speicher für '%s' reservieren\n"
msgid "failed malloc in xvasprintf\n"
msgstr "konnte keinen Speicher für '%s' reservieren\n"
msgid "sysconf error for _SC_OPEN_MAX\n"
msgstr ""
#, c-format
msgid ""
" %s (-h | --help) for detailed help\n"

View file

@ -10,7 +10,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: devel@monitoring-plugins.org\n"
"POT-Creation-Date: 2023-09-21 12:09+0200\n"
"POT-Creation-Date: 2023-09-22 15:36+0200\n"
"PO-Revision-Date: 2010-04-21 23:38-0400\n"
"Last-Translator: \n"
"Language-Team: Monitoring Plugin Development Team <devel@monitoring-plugins."
@ -4839,9 +4839,6 @@ msgstr "La fonction malloc à échoué dans strscat\n"
msgid "failed malloc in xvasprintf\n"
msgstr "La fonction malloc à échoué dans strscat\n"
msgid "sysconf error for _SC_OPEN_MAX\n"
msgstr ""
#, c-format
msgid ""
" %s (-h | --help) for detailed help\n"

View file

@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: devel@monitoring-plugins.org\n"
"POT-Creation-Date: 2023-09-21 12:09+0200\n"
"POT-Creation-Date: 2023-09-22 15:36+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@ -4532,9 +4532,6 @@ msgstr ""
msgid "failed malloc in xvasprintf\n"
msgstr ""
msgid "sysconf error for _SC_OPEN_MAX\n"
msgstr ""
#, c-format
msgid ""
" %s (-h | --help) for detailed help\n"