From 0162cb2d4f7040e3b2d48095182f87ce565866a5 Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 5 Mar 2023 16:03:37 +0100 Subject: fixup! Rework maxfd/open_max to avoid unused variables --- lib/maxfd.c | 26 ++++++++++++++++++++++++++ lib/maxfd.h | 9 +++++++++ 2 files changed, 35 insertions(+) create mode 100644 lib/maxfd.c create mode 100644 lib/maxfd.h diff --git a/lib/maxfd.c b/lib/maxfd.c new file mode 100644 index 00000000..dcd4d3db --- /dev/null +++ b/lib/maxfd.c @@ -0,0 +1,26 @@ +#include "./maxfd.h" +#include + +long 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); +} diff --git a/lib/maxfd.h b/lib/maxfd.h new file mode 100644 index 00000000..0d734c5c --- /dev/null +++ b/lib/maxfd.h @@ -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 open_max (void); + +#endif // _MAXFD_ -- cgit v1.2.3-74-g34f1