]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Cache the isc_os_ncpu() result
authorOndrej Sury <ondrej@isc.org>
Mon, 12 Jul 2021 12:21:21 +0000 (14:21 +0200)
committerOndřej Surý <ondrej@isc.org>
Tue, 13 Jul 2021 07:12:04 +0000 (09:12 +0200)
It was discovered that on some platforms (f.e. Alpine Linux with MUSL)
the result of isc_os_ncpus() call differ when called before and after we
drop privileges.  This commit changes the isc_os_ncpus() call to cache
the result from the first call and thus always return the same value
during the runtime of the named.  The first call to isc_os_ncpus() is
made as soon as possible on the library initalization.

lib/isc/lib.c
lib/isc/os.c

index 2d6950ad6383f5c4ae2d247d13acb20d3a24c876..18e3284be072e974842eb5d54a9eb2188c7fe63d 100644 (file)
@@ -14,6 +14,7 @@
 #include <isc/bind9.h>
 #include <isc/lib.h>
 #include <isc/mem.h>
+#include <isc/os.h>
 #include <isc/tls.h>
 #include <isc/util.h>
 
@@ -45,6 +46,7 @@ isc__initialize(void) {
        isc__mem_initialize();
        isc__tls_initialize();
        isc__trampoline_initialize();
+       (void)isc_os_ncpus();
 }
 
 void
index 5ba4481d36484bf4dfc2f1009a88fae82588ad2a..6a75b6097664f5b675239d677291435e407f0f61 100644 (file)
@@ -9,7 +9,12 @@
  * information regarding copyright ownership.
  */
 
+#include <isc/once.h>
 #include <isc/os.h>
+#include <isc/util.h>
+
+static isc_once_t ncpus_once = ISC_ONCE_INIT;
+static unsigned int ncpus = 0;
 
 #ifdef HAVE_SYSCONF
 
@@ -46,10 +51,8 @@ sysctl_ncpus(void) {
 }
 #endif /* if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTLBYNAME) */
 
-unsigned int
-isc_os_ncpus(void) {
-       long ncpus = 0;
-
+static void
+ncpus_initialize(void) {
 #if defined(HAVE_SYSCONF)
        ncpus = sysconf_ncpus();
 #endif /* if defined(HAVE_SYSCONF) */
@@ -61,6 +64,13 @@ isc_os_ncpus(void) {
        if (ncpus <= 0) {
                ncpus = 1;
        }
+}
+
+unsigned int
+isc_os_ncpus(void) {
+       isc_result_t result = isc_once_do(&ncpus_once, ncpus_initialize);
+       RUNTIME_CHECK(result == ISC_R_SUCCESS);
+       INSIST(ncpus > 0);
 
-       return ((unsigned int)ncpus);
+       return (ncpus);
 }