X Tutup
Skip to content

Commit 4b6f74f

Browse files
keszybzpoettering
authored andcommitted
basic/selinux: work around mallinfo deprecation
Latest glibc has deprecated mallinfo(), so it might become unavailable at some point in the future. There is malloc_info(), but it returns XML, ffs. I think the information that we get from mallinfo() is quite useful, so let's use mallinfo() if available, and not otherwise.
1 parent d775d8e commit 4b6f74f

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,7 @@ foreach ident : [
532532
#include <unistd.h>
533533
#include <signal.h>
534534
#include <sys/wait.h>'''],
535+
['mallinfo', '''#include <malloc.h>'''],
535536
]
536537

537538
have = cc.has_function(ident[0], prefix : ident[1], args : '-D_GNU_SOURCE')

src/basic/macro.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@
9393
#endif
9494

9595
/* Temporarily disable some warnings */
96+
#define DISABLE_WARNING_DEPRECATED_DECLARATIONS \
97+
_Pragma("GCC diagnostic push"); \
98+
_Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
99+
96100
#define DISABLE_WARNING_FORMAT_NONLITERAL \
97101
_Pragma("GCC diagnostic push"); \
98102
_Pragma("GCC diagnostic ignored \"-Wformat-nonliteral\"")

src/basic/selinux-util.c

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,28 +84,42 @@ void mac_selinux_retest(void) {
8484
}
8585

8686
#if HAVE_SELINUX
87+
# if HAVE_MALLINFO
88+
static struct mallinfo mallinfo_nowarn(void) {
89+
/* glibc has deprecated mallinfo(), but the replacement malloc_info() returns an XML blob ;=[ */
90+
DISABLE_WARNING_DEPRECATED_DECLARATIONS
91+
return mallinfo();
92+
REENABLE_WARNING
93+
}
94+
# else
95+
# warning "mallinfo() is missing, add mallinfo2() supported instead."
96+
# endif
97+
8798
static int open_label_db(void) {
8899
struct selabel_handle *hnd;
89100
usec_t before_timestamp, after_timestamp;
90-
struct mallinfo before_mallinfo, after_mallinfo;
91101
char timespan[FORMAT_TIMESPAN_MAX];
92-
int l;
93102

94-
before_mallinfo = mallinfo();
103+
# if HAVE_MALLINFO
104+
struct mallinfo before_mallinfo = mallinfo_nowarn();
105+
# endif
95106
before_timestamp = now(CLOCK_MONOTONIC);
96107

97108
hnd = selabel_open(SELABEL_CTX_FILE, NULL, 0);
98109
if (!hnd)
99110
return log_enforcing_errno(errno, "Failed to initialize SELinux labeling handle: %m");
100111

101112
after_timestamp = now(CLOCK_MONOTONIC);
102-
after_mallinfo = mallinfo();
103-
104-
l = after_mallinfo.uordblks > before_mallinfo.uordblks ? after_mallinfo.uordblks - before_mallinfo.uordblks : 0;
105-
113+
# if HAVE_MALLINFO
114+
struct mallinfo after_mallinfo = mallinfo_nowarn();
115+
int l = after_mallinfo.uordblks > before_mallinfo.uordblks ? after_mallinfo.uordblks - before_mallinfo.uordblks : 0;
106116
log_debug("Successfully loaded SELinux database in %s, size on heap is %iK.",
107117
format_timespan(timespan, sizeof(timespan), after_timestamp - before_timestamp, 0),
108-
(l+1023)/1024);
118+
DIV_ROUND_UP(l, 1024));
119+
# else
120+
log_debug("Successfully loaded SELinux database in %s.",
121+
format_timespan(timespan, sizeof(timespan), after_timestamp - before_timestamp, 0));
122+
# endif
109123

110124
/* release memory after measurement */
111125
if (label_hnd)

0 commit comments

Comments
 (0)
X Tutup