summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2023-09-23 08:47:01 (GMT)
committerGitHub <noreply@github.com>2023-09-23 08:47:01 (GMT)
commit719e27ddc2f0b48bcd7fe5584b23e3ce83ddf291 (patch)
tree1ca6c622275cf8d57e0b6b6d7fb0da4cce8e9205
parentfe718dec117a7adb8a7107a4137a86c79b54d15f (diff)
parent4295decfbf06adfa1bf019d28e9044971607b2d6 (diff)
downloadmonitoring-plugins-719e27ddc2f0b48bcd7fe5584b23e3ce83ddf291.tar.gz
Merge pull request #1924 from RincewindsHat/compiler_warnings_4_1
Centralise and refactor maxfd related functionality
-rw-r--r--lib/Makefile.am2
-rw-r--r--lib/maxfd.c26
-rw-r--r--lib/maxfd.h9
-rw-r--r--lib/utils_cmd.c15
-rw-r--r--plugins/common.h14
-rw-r--r--plugins/popen.c8
-rw-r--r--plugins/runcmd.c6
-rw-r--r--plugins/utils.c16
-rw-r--r--plugins/utils.h2
-rw-r--r--po/de.po5
-rw-r--r--po/fr.po5
-rw-r--r--po/monitoring-plugins.pot5
12 files changed, 54 insertions, 59 deletions
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 01d73a6..1a47395 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -7,7 +7,7 @@ noinst_LIBRARIES = libmonitoringplug.a
7AM_CPPFLAGS = -DNP_STATE_DIR_PREFIX=\"$(localstatedir)\" \ 7AM_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
10libmonitoringplug_a_SOURCES = utils_base.c utils_disk.c utils_tcp.c utils_cmd.c 10libmonitoringplug_a_SOURCES = utils_base.c utils_disk.c utils_tcp.c utils_cmd.c maxfd.c
11EXTRA_DIST = utils_base.h utils_disk.h utils_tcp.h utils_cmd.h parse_ini.h extra_opts.h 11EXTRA_DIST = utils_base.h utils_disk.h utils_tcp.h utils_cmd.h parse_ini.h extra_opts.h
12 12
13if USE_PARSE_INI 13if USE_PARSE_INI
diff --git a/lib/maxfd.c b/lib/maxfd.c
new file mode 100644
index 0000000..529b356
--- /dev/null
+++ b/lib/maxfd.c
@@ -0,0 +1,26 @@
1#include "./maxfd.h"
2#include <errno.h>
3
4long 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 0000000..45218d0
--- /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
7long mp_open_max (void);
8
9#endif // _MAXFD_
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 @@
53static pid_t *_cmd_pids = NULL; 53static 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 *, ...)
96void 99void
97cmd_init (void) 100cmd_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)
275int 274int
276cmd_run (const char *cmdstring, output * out, output * err, int flags) 275cmd_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 }
diff --git a/plugins/common.h b/plugins/common.h
index 0f08e2f..6bf4fca 100644
--- a/plugins/common.h
+++ b/plugins/common.h
@@ -225,18 +225,4 @@ enum {
225# define __attribute__(x) /* do nothing */ 225# define __attribute__(x) /* do nothing */
226#endif 226#endif
227 227
228/* Try sysconf(_SC_OPEN_MAX) first, as it can be higher than OPEN_MAX.
229 * If that fails and the macro isn't defined, we fall back to an educated
230 * guess. There's no guarantee that our guess is adequate and the program
231 * will die with SIGSEGV if it isn't and the upper boundary is breached. */
232#define DEFAULT_MAXFD 256 /* fallback value if no max open files value is set */
233#define MAXFD_LIMIT 8192 /* upper limit of open files */
234#ifdef _SC_OPEN_MAX
235static long maxfd = 0;
236#elif defined(OPEN_MAX)
237# define maxfd OPEN_MAX
238#else /* sysconf macro unavailable, so guess (may be wildly inaccurate) */
239# define maxfd DEFAULT_MAXFD
240#endif
241
242#endif /* _COMMON_H_ */ 228#endif /* _COMMON_H_ */
diff --git a/plugins/popen.c b/plugins/popen.c
index 723817d..b395f14 100644
--- a/plugins/popen.c
+++ b/plugins/popen.c
@@ -38,8 +38,9 @@
38* 38*
39*****************************************************************************/ 39*****************************************************************************/
40 40
41#include "common.h" 41#include "./common.h"
42#include "utils.h" 42#include "./utils.h"
43#include "../lib/maxfd.h"
43 44
44/* extern so plugin has pid to kill exec'd process on timeouts */ 45/* extern so plugin has pid to kill exec'd process on timeouts */
45extern pid_t *childpid; 46extern pid_t *childpid;
@@ -177,8 +178,7 @@ spopen (const char *cmdstring)
177 } 178 }
178 argv[i] = NULL; 179 argv[i] = NULL;
179 180
180 if(maxfd == 0) 181 long maxfd = mp_open_max();
181 maxfd = open_max();
182 182
183 if (childpid == NULL) { /* first time through */ 183 if (childpid == NULL) { /* first time through */
184 if ((childpid = calloc ((size_t)maxfd, sizeof (pid_t))) == NULL) 184 if ((childpid = calloc ((size_t)maxfd, sizeof (pid_t))) == NULL)
diff --git a/plugins/runcmd.c b/plugins/runcmd.c
index 102191e..bc0a497 100644
--- a/plugins/runcmd.c
+++ b/plugins/runcmd.c
@@ -88,8 +88,7 @@ extern void die (int, const char *, ...)
88 * through this api and thus achieve async-safeness throughout the api */ 88 * through this api and thus achieve async-safeness throughout the api */
89void np_runcmd_init(void) 89void np_runcmd_init(void)
90{ 90{
91 if(maxfd == 0) 91 long maxfd = mp_open_max();
92 maxfd = open_max();
93 if(!np_pids) np_pids = calloc(maxfd, sizeof(pid_t)); 92 if(!np_pids) np_pids = calloc(maxfd, sizeof(pid_t));
94} 93}
95 94
@@ -192,6 +191,7 @@ np_runcmd_open(const char *cmdstring, int *pfd, int *pfderr)
192 /* close all descriptors in np_pids[] 191 /* close all descriptors in np_pids[]
193 * This is executed in a separate address space (pure child), 192 * This is executed in a separate address space (pure child),
194 * so we don't have to worry about async safety */ 193 * so we don't have to worry about async safety */
194 long maxfd = mp_open_max();
195 for (i = 0; i < maxfd; i++) 195 for (i = 0; i < maxfd; i++)
196 if(np_pids[i] > 0) 196 if(np_pids[i] > 0)
197 close (i); 197 close (i);
@@ -219,6 +219,7 @@ np_runcmd_close(int fd)
219 pid_t pid; 219 pid_t pid;
220 220
221 /* make sure this fd was opened by popen() */ 221 /* make sure this fd was opened by popen() */
222 long maxfd = mp_open_max();
222 if(fd < 0 || fd > maxfd || !np_pids || (pid = np_pids[fd]) == 0) 223 if(fd < 0 || fd > maxfd || !np_pids || (pid = np_pids[fd]) == 0)
223 return -1; 224 return -1;
224 225
@@ -242,6 +243,7 @@ runcmd_timeout_alarm_handler (int signo)
242 if (signo == SIGALRM) 243 if (signo == SIGALRM)
243 puts(_("CRITICAL - Plugin timed out while executing system call")); 244 puts(_("CRITICAL - Plugin timed out while executing system call"));
244 245
246 long maxfd = mp_open_max();
245 if(np_pids) for(i = 0; i < maxfd; i++) { 247 if(np_pids) for(i = 0; i < maxfd; i++) {
246 if(np_pids[i] != 0) kill(np_pids[i], SIGKILL); 248 if(np_pids[i] != 0) kill(np_pids[i], SIGKILL);
247 } 249 }
diff --git a/plugins/utils.c b/plugins/utils.c
index b4214c6..71c0bdd 100644
--- a/plugins/utils.c
+++ b/plugins/utils.c
@@ -804,19 +804,3 @@ char *sperfdata_int (const char *label,
804 804
805 return data; 805 return data;
806} 806}
807
808int
809open_max (void)
810{
811 errno = 0;
812 if (maxfd > 0)
813 return(maxfd);
814
815 if ((maxfd = sysconf (_SC_OPEN_MAX)) < 0) {
816 if (errno == 0)
817 maxfd = DEFAULT_MAXFD; /* it's indeterminate */
818 else
819 die (STATE_UNKNOWN, _("sysconf error for _SC_OPEN_MAX\n"));
820 }
821 return(maxfd);
822}
diff --git a/plugins/utils.h b/plugins/utils.h
index c76b321..cb979ce 100644
--- a/plugins/utils.h
+++ b/plugins/utils.h
@@ -106,8 +106,6 @@ char *sperfdata (const char *, double, const char *, char *, char *,
106char *sperfdata_int (const char *, int, const char *, char *, char *, 106char *sperfdata_int (const char *, int, const char *, char *, char *,
107 int, int, int, int); 107 int, int, int, int);
108 108
109int open_max (void);
110
111/* The idea here is that, although not every plugin will use all of these, 109/* The idea here is that, although not every plugin will use all of these,
112 most will or should. Therefore, for consistency, these very common 110 most will or should. Therefore, for consistency, these very common
113 options should have only these meanings throughout the overall suite */ 111 options should have only these meanings throughout the overall suite */
diff --git a/po/de.po b/po/de.po
index 0dedfc1..9ea32df 100644
--- a/po/de.po
+++ b/po/de.po
@@ -9,7 +9,7 @@ msgid ""
9msgstr "" 9msgstr ""
10"Project-Id-Version: PACKAGE VERSION\n" 10"Project-Id-Version: PACKAGE VERSION\n"
11"Report-Msgid-Bugs-To: devel@monitoring-plugins.org\n" 11"Report-Msgid-Bugs-To: devel@monitoring-plugins.org\n"
12"POT-Creation-Date: 2023-09-21 12:09+0200\n" 12"POT-Creation-Date: 2023-09-22 15:36+0200\n"
13"PO-Revision-Date: 2004-12-23 17:46+0100\n" 13"PO-Revision-Date: 2004-12-23 17:46+0100\n"
14"Last-Translator: \n" 14"Last-Translator: \n"
15"Language-Team: Monitoring Plugin Development Team <devel@monitoring-plugins." 15"Language-Team: Monitoring Plugin Development Team <devel@monitoring-plugins."
@@ -4705,9 +4705,6 @@ msgstr "konnte keinen Speicher für '%s' reservieren\n"
4705msgid "failed malloc in xvasprintf\n" 4705msgid "failed malloc in xvasprintf\n"
4706msgstr "konnte keinen Speicher für '%s' reservieren\n" 4706msgstr "konnte keinen Speicher für '%s' reservieren\n"
4707 4707
4708msgid "sysconf error for _SC_OPEN_MAX\n"
4709msgstr ""
4710
4711#, c-format 4708#, c-format
4712msgid "" 4709msgid ""
4713" %s (-h | --help) for detailed help\n" 4710" %s (-h | --help) for detailed help\n"
diff --git a/po/fr.po b/po/fr.po
index ec5651d..28deb94 100644
--- a/po/fr.po
+++ b/po/fr.po
@@ -10,7 +10,7 @@ msgid ""
10msgstr "" 10msgstr ""
11"Project-Id-Version: PACKAGE VERSION\n" 11"Project-Id-Version: PACKAGE VERSION\n"
12"Report-Msgid-Bugs-To: devel@monitoring-plugins.org\n" 12"Report-Msgid-Bugs-To: devel@monitoring-plugins.org\n"
13"POT-Creation-Date: 2023-09-21 12:09+0200\n" 13"POT-Creation-Date: 2023-09-22 15:36+0200\n"
14"PO-Revision-Date: 2010-04-21 23:38-0400\n" 14"PO-Revision-Date: 2010-04-21 23:38-0400\n"
15"Last-Translator: \n" 15"Last-Translator: \n"
16"Language-Team: Monitoring Plugin Development Team <devel@monitoring-plugins." 16"Language-Team: Monitoring Plugin Development Team <devel@monitoring-plugins."
@@ -4839,9 +4839,6 @@ msgstr "La fonction malloc à échoué dans strscat\n"
4839msgid "failed malloc in xvasprintf\n" 4839msgid "failed malloc in xvasprintf\n"
4840msgstr "La fonction malloc à échoué dans strscat\n" 4840msgstr "La fonction malloc à échoué dans strscat\n"
4841 4841
4842msgid "sysconf error for _SC_OPEN_MAX\n"
4843msgstr ""
4844
4845#, c-format 4842#, c-format
4846msgid "" 4843msgid ""
4847" %s (-h | --help) for detailed help\n" 4844" %s (-h | --help) for detailed help\n"
diff --git a/po/monitoring-plugins.pot b/po/monitoring-plugins.pot
index 2cd94b1..d019a7e 100644
--- a/po/monitoring-plugins.pot
+++ b/po/monitoring-plugins.pot
@@ -8,7 +8,7 @@ msgid ""
8msgstr "" 8msgstr ""
9"Project-Id-Version: PACKAGE VERSION\n" 9"Project-Id-Version: PACKAGE VERSION\n"
10"Report-Msgid-Bugs-To: devel@monitoring-plugins.org\n" 10"Report-Msgid-Bugs-To: devel@monitoring-plugins.org\n"
11"POT-Creation-Date: 2023-09-21 12:09+0200\n" 11"POT-Creation-Date: 2023-09-22 15:36+0200\n"
12"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" 12"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
13"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" 13"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
14"Language-Team: LANGUAGE <LL@li.org>\n" 14"Language-Team: LANGUAGE <LL@li.org>\n"
@@ -4532,9 +4532,6 @@ msgstr ""
4532msgid "failed malloc in xvasprintf\n" 4532msgid "failed malloc in xvasprintf\n"
4533msgstr "" 4533msgstr ""
4534 4534
4535msgid "sysconf error for _SC_OPEN_MAX\n"
4536msgstr ""
4537
4538#, c-format 4535#, c-format
4539msgid "" 4536msgid ""
4540" %s (-h | --help) for detailed help\n" 4537" %s (-h | --help) for detailed help\n"