]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
selinux: avoid probing memory status if debug logs are not enabled
authorLuca Boccassi <bluca@debian.org>
Fri, 10 Nov 2023 11:53:11 +0000 (11:53 +0000)
committerLuca Boccassi <bluca@debian.org>
Sat, 11 Nov 2023 12:29:52 +0000 (12:29 +0000)
Given we are optimizing the selinux paths, avoid doing these operations
unless the result is actually used

src/shared/selinux-util.c

index a38a56f43496e905f7d38c9057e1c54b45c542a0..d71e0f0f86832aabead572bae805b9ee9e85ea2a 100644 (file)
@@ -104,28 +104,36 @@ void mac_selinux_retest(void) {
 #if HAVE_SELINUX
 static int open_label_db(void) {
         struct selabel_handle *hnd;
-        usec_t before_timestamp, after_timestamp;
+        /* Avoid maybe-uninitialized false positives */
+        usec_t before_timestamp = USEC_INFINITY, after_timestamp = USEC_INFINITY;
+#  if HAVE_GENERIC_MALLINFO
+        generic_mallinfo before_mallinfo = {};
+#  endif
 
+        if (DEBUG_LOGGING) {
 #  if HAVE_GENERIC_MALLINFO
-        generic_mallinfo before_mallinfo = generic_mallinfo_get();
+                before_mallinfo = generic_mallinfo_get();
 #  endif
-        before_timestamp = now(CLOCK_MONOTONIC);
+                before_timestamp = now(CLOCK_MONOTONIC);
+        }
 
         hnd = selabel_open(SELABEL_CTX_FILE, NULL, 0);
         if (!hnd)
                 return log_enforcing_errno(errno, "Failed to initialize SELinux labeling handle: %m");
 
-        after_timestamp = now(CLOCK_MONOTONIC);
+        if (DEBUG_LOGGING) {
+                after_timestamp = now(CLOCK_MONOTONIC);
 #  if HAVE_GENERIC_MALLINFO
-        generic_mallinfo after_mallinfo = generic_mallinfo_get();
-        size_t l = LESS_BY((size_t) after_mallinfo.uordblks, (size_t) before_mallinfo.uordblks);
-        log_debug("Successfully loaded SELinux database in %s, size on heap is %zuK.",
-                  FORMAT_TIMESPAN(after_timestamp - before_timestamp, 0),
-                  DIV_ROUND_UP(l, 1024));
+                generic_mallinfo after_mallinfo = generic_mallinfo_get();
+                size_t l = LESS_BY((size_t) after_mallinfo.uordblks, (size_t) before_mallinfo.uordblks);
+                log_debug("Successfully loaded SELinux database in %s, size on heap is %zuK.",
+                          FORMAT_TIMESPAN(after_timestamp - before_timestamp, 0),
+                          DIV_ROUND_UP(l, 1024));
 #  else
-        log_debug("Successfully loaded SELinux database in %s.",
-                  FORMAT_TIMESPAN(after_timestamp - before_timestamp, 0));
+                log_debug("Successfully loaded SELinux database in %s.",
+                          FORMAT_TIMESPAN(after_timestamp - before_timestamp, 0));
 #  endif
+        }
 
         /* release memory after measurement */
         if (label_hnd)