]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
selinux: use mallocinfo2() if it exists
authorLennart Poettering <lennart@poettering.net>
Mon, 3 May 2021 18:36:32 +0000 (20:36 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 4 May 2021 14:00:53 +0000 (16:00 +0200)
New glibc deprecated mallocinfo(), even newer glibc added mallocinfo2()
as replacement. Use it, if it exists.

Follow-up for 4b6f74f5a0943e0abfa8e6997811f8f7b7f00a15 and related
commits.

meson.build
src/basic/selinux-util.c

index b13817f0d21277a33b0f49b6c84d706074eae5fa..fed51389a8fac0c2290aaf93d9c7bdff7e048e74 100644 (file)
@@ -555,6 +555,7 @@ foreach ident : [
                                  #include <signal.h>
                                  #include <sys/wait.h>'''],
         ['mallinfo',          '''#include <malloc.h>'''],
+        ['mallinfo2',         '''#include <malloc.h>'''],
         ['execveat',          '''#include <unistd.h>'''],
         ['close_range',       '''#include <unistd.h>'''],
         ['epoll_pwait2',      '''#include <sys/epoll.h>'''],
index ee9e34ed47cb676fba1fc92449fecd9ec3aadca8..8c5544d46bbbda4efe954c4a1d9f5bb05ae0842a 100644 (file)
@@ -84,15 +84,24 @@ void mac_selinux_retest(void) {
 }
 
 #if HAVE_SELINUX
-#  if HAVE_MALLINFO
-static struct mallinfo mallinfo_nowarn(void) {
-        /* glibc has deprecated mallinfo(), but the replacement malloc_info() returns an XML blob ;=[ */
+#  if HAVE_MALLINFO2
+#    define HAVE_GENERIC_MALLINFO 1
+typedef struct mallinfo2 generic_mallinfo;
+static generic_mallinfo generic_mallinfo_get(void) {
+        return mallinfo2();
+}
+#  elif HAVE_MALLINFO
+#    define HAVE_GENERIC_MALLINFO 1
+typedef struct mallinfo generic_mallinfo;
+static generic_mallinfo generic_mallinfo_get(void) {
+        /* glibc has deprecated mallinfo(), let's suppress the deprecation warning if mallinfo2() doesn't
+         * exist yet. */
 DISABLE_WARNING_DEPRECATED_DECLARATIONS
         return mallinfo();
 REENABLE_WARNING
 }
 #  else
-#    warning "mallinfo() is missing, add mallinfo2() supported instead."
+#    define HAVE_GENERIC_MALLINFO 0
 #  endif
 
 static int open_label_db(void) {
@@ -100,8 +109,8 @@ static int open_label_db(void) {
         usec_t before_timestamp, after_timestamp;
         char timespan[FORMAT_TIMESPAN_MAX];
 
-#  if HAVE_MALLINFO
-        struct mallinfo before_mallinfo = mallinfo_nowarn();
+#  if HAVE_GENERIC_MALLINFO
+        generic_mallinfo before_mallinfo = generic_mallinfo_get();
 #  endif
         before_timestamp = now(CLOCK_MONOTONIC);
 
@@ -110,10 +119,10 @@ static int open_label_db(void) {
                 return log_enforcing_errno(errno, "Failed to initialize SELinux labeling handle: %m");
 
         after_timestamp = now(CLOCK_MONOTONIC);
-#  if HAVE_MALLINFO
-        struct mallinfo after_mallinfo = mallinfo_nowarn();
-        int l = after_mallinfo.uordblks > before_mallinfo.uordblks ? after_mallinfo.uordblks - before_mallinfo.uordblks : 0;
-        log_debug("Successfully loaded SELinux database in %s, size on heap is %iK.",
+#  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(timespan, sizeof(timespan), after_timestamp - before_timestamp, 0),
                   DIV_ROUND_UP(l, 1024));
 #  else