lib: clang-format
[monitoring-plugins.git] / lib / maxfd.c
blobc39d427cf078d4156946e8cd286193e6516b3b1f
1 #include "./maxfd.h"
2 #include <errno.h>
4 long 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. */
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"));
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
25 return (maxfd);