From: Luca Boccassi Date: Fri, 10 Nov 2023 11:53:11 +0000 (+0000) Subject: selinux: avoid probing memory status if debug logs are not enabled X-Git-Tag: v255-rc2~39^2~3 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2c6a231b47c0e42f64d13efea461e5c061d9e605;p=thirdparty%2Fsystemd.git selinux: avoid probing memory status if debug logs are not enabled Given we are optimizing the selinux paths, avoid doing these operations unless the result is actually used --- diff --git a/src/shared/selinux-util.c b/src/shared/selinux-util.c index a38a56f4349..d71e0f0f868 100644 --- a/src/shared/selinux-util.c +++ b/src/shared/selinux-util.c @@ -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)