From e7372d18a1a661d8c3dba9f51e1f17b5f94171a7 Mon Sep 17 00:00:00 2001 From: Daniel Lange Date: Wed, 10 Jan 2024 11:17:08 +0100 Subject: New upstream version 3.3.0 --- generic/fdstat_sysctl.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++ generic/fdstat_sysctl.h | 13 ++++++++ generic/gettime.c | 1 + generic/hostname.c | 1 + generic/uname.c | 1 + 5 files changed, 96 insertions(+) create mode 100644 generic/fdstat_sysctl.c create mode 100644 generic/fdstat_sysctl.h (limited to 'generic') diff --git a/generic/fdstat_sysctl.c b/generic/fdstat_sysctl.c new file mode 100644 index 0000000..ea374fb --- /dev/null +++ b/generic/fdstat_sysctl.c @@ -0,0 +1,80 @@ +/* +htop - generic/fdstat_sysctl.c +(C) 2022-2023 htop dev team +Released under the GNU GPLv2+, see the COPYING file +in the source distribution for its full text. +*/ + +#include "config.h" // IWYU pragma: keep + +#include "generic/fdstat_sysctl.h" + +#include +#include +#include + +#include // Shitty FreeBSD upstream headers +#include + + +static void Generic_getFileDescriptors_sysctl_internal( + const char* sysctlname_maxfiles, + const char* sysctlname_numfiles, + size_t size_header, + size_t size_entry, + double* used, + double* max +) { + *used = NAN; + *max = 65536; + + int max_fd, open_fd; + size_t len; + + len = sizeof(max_fd); + if (sysctlname_maxfiles && sysctlbyname(sysctlname_maxfiles, &max_fd, &len, NULL, 0) == 0) { + if (max_fd) { + *max = max_fd; + } else { + *max = NAN; + } + } + + len = sizeof(open_fd); + if (sysctlname_numfiles && sysctlbyname(sysctlname_numfiles, &open_fd, &len, NULL, 0) == 0) { + *used = open_fd; + return; + } + + // If no sysctl arc available, try to guess from the file table size at kern.file + // The size per entry differs per OS, thus skip if we don't know: + if (!size_entry) + return; + + len = 0; + if (sysctlbyname("kern.file", NULL, &len, NULL, 0) < 0) + return; + + if (len < size_header) + return; + + *used = (len - size_header) / size_entry; +} + +void Generic_getFileDescriptors_sysctl(double* used, double* max) { +#if defined(HTOP_DARWIN) + Generic_getFileDescriptors_sysctl_internal( + "kern.maxfiles", "kern.num_files", 0, 0, used, max); +#elif defined(HTOP_DRAGONFLY) + Generic_getFileDescriptors_sysctl_internal( + "kern.maxfiles", NULL, 0, sizeof(struct kinfo_file), used, max); +#elif defined(HTOP_FREEBSD) + Generic_getFileDescriptors_sysctl_internal( + "kern.maxfiles", "kern.openfiles", 0, 0, used, max); +#elif defined(HTOP_NETBSD) + Generic_getFileDescriptors_sysctl_internal( + "kern.maxfiles", NULL, 0, sizeof(struct kinfo_file), used, max); +#else +#error Unknown platform: Please implement proper way to query open/max file information +#endif +} diff --git a/generic/fdstat_sysctl.h b/generic/fdstat_sysctl.h new file mode 100644 index 0000000..107fcab --- /dev/null +++ b/generic/fdstat_sysctl.h @@ -0,0 +1,13 @@ +#ifndef HEADER_fdstat_sysctl +#define HEADER_fdstat_sysctl +/* +htop - generic/fdstat_sysctl.h +(C) 2022-2023 htop dev team +Released under the GNU GPLv2+, see the COPYING file +in the source distribution for its full text. +*/ + + +void Generic_getFileDescriptors_sysctl(double* used, double* max); + +#endif diff --git a/generic/gettime.c b/generic/gettime.c index b7c4885..209f523 100644 --- a/generic/gettime.c +++ b/generic/gettime.c @@ -4,6 +4,7 @@ htop - generic/gettime.c Released under the GNU GPLv2+, see the COPYING file in the source distribution for its full text. */ + #include "config.h" // IWYU pragma: keep #include "generic/gettime.h" diff --git a/generic/hostname.c b/generic/hostname.c index 69a4146..e327582 100644 --- a/generic/hostname.c +++ b/generic/hostname.c @@ -4,6 +4,7 @@ htop - generic/hostname.c Released under the GNU GPLv2+, see the COPYING file in the source distribution for its full text. */ + #include "config.h" // IWYU pragma: keep #include "generic/hostname.h" diff --git a/generic/uname.c b/generic/uname.c index 2a734dc..b5bb583 100644 --- a/generic/uname.c +++ b/generic/uname.c @@ -4,6 +4,7 @@ htop - generic/uname.c Released under the GNU GPLv2+, see the COPYING file in the source distribution for its full text. */ + #include "config.h" // IWYU pragma: keep #include "generic/uname.h" -- cgit v1.2.3