From 3b58245624a7c23cc9810854e2d5e368af611bb7 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Tue, 6 May 2025 15:39:03 +0200 Subject: [PATCH 1/7] basic: Override glibc's sys/param.h header with an empty file Instead of unconditionally including sys/param.h in macro-fundamental.h which itself includes a bunch of other unnecessary headers, let's override it with an empty file to avoid it from overriding our MAX() macro. We can't make including it an error as it's included ( for seemingly no good reason) by . --- src/basic/include/sys/param.h | 8 ++++++++ src/basic/macro.h | 1 - src/basic/pidref.h | 2 ++ src/core/ipe-setup.c | 2 ++ src/fundamental/macro-fundamental.h | 7 ------- src/libsystemd/sd-journal/lookup3.c | 5 +---- src/network/test-network.c | 1 - src/shared/initreq.h | 11 ++--------- 8 files changed, 15 insertions(+), 22 deletions(-) create mode 100644 src/basic/include/sys/param.h diff --git a/src/basic/include/sys/param.h b/src/basic/include/sys/param.h new file mode 100644 index 0000000000..88cc47edf4 --- /dev/null +++ b/src/basic/include/sys/param.h @@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +/* sys/param.h from glibc unconditionally overrides the MIN() and MAX() macros which interferes with our own + * MAX() macro. It also includes a bunch of other headers transitively so we don't want to include + * sys/param.h in macro-fundamental.h unconditionally. We'd like to make including this file an error but + * unfortunately includes it. However, doesn't actually make use of anything from + * sys/param.h, so we override it with an empty file so it can't mess with our macros. */ diff --git a/src/basic/macro.h b/src/basic/macro.h index cf1c1a4617..9cee1868a5 100644 --- a/src/basic/macro.h +++ b/src/basic/macro.h @@ -5,7 +5,6 @@ #include #include #include -#include #include #include diff --git a/src/basic/pidref.h b/src/basic/pidref.h index 064b9d851a..59a4e39204 100644 --- a/src/basic/pidref.h +++ b/src/basic/pidref.h @@ -1,6 +1,8 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once +#include + #include "macro.h" #include "memory-util.h" diff --git a/src/core/ipe-setup.c b/src/core/ipe-setup.c index 5cc8b9bccc..392c9187e5 100644 --- a/src/core/ipe-setup.c +++ b/src/core/ipe-setup.c @@ -1,5 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include + #include "alloc-util.h" #include "conf-files.h" #include "copy.h" diff --git a/src/fundamental/macro-fundamental.h b/src/fundamental/macro-fundamental.h index 28fef06c99..597228f139 100644 --- a/src/fundamental/macro-fundamental.h +++ b/src/fundamental/macro-fundamental.h @@ -7,13 +7,6 @@ #include #include -/* This header unconditionally defines MAX() so include it here already so - * it won't override our own definition of MAX() that we define later in this - * file. */ -#if !SD_BOOT -#include -#endif - /* Temporarily disable some warnings */ #define DISABLE_WARNING_DEPRECATED_DECLARATIONS \ _Pragma("GCC diagnostic push"); \ diff --git a/src/libsystemd/sd-journal/lookup3.c b/src/libsystemd/sd-journal/lookup3.c index 8e3dac05af..a2015a7428 100644 --- a/src/libsystemd/sd-journal/lookup3.c +++ b/src/libsystemd/sd-journal/lookup3.c @@ -47,13 +47,10 @@ on 1 byte), but shoehorning those bytes into integers efficiently is messy. */ /* #define SELF_TEST 1 */ +#include /* attempt to define endianness */ #include /* defines uint32_t etc */ #include /* defines printf for tests */ -#include /* attempt to define endianness */ #include /* defines time_t for timings in the test */ -#ifdef linux -# include /* attempt to define endianness */ -#endif _Pragma("GCC diagnostic ignored \"-Wimplicit-fallthrough\"") diff --git a/src/network/test-network.c b/src/network/test-network.c index 89dd98f560..b98b9ecfb5 100644 --- a/src/network/test-network.c +++ b/src/network/test-network.c @@ -1,7 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #include -#include #include "sd-device.h" diff --git a/src/shared/initreq.h b/src/shared/initreq.h index c6a1050805..205a3be44a 100644 --- a/src/shared/initreq.h +++ b/src/shared/initreq.h @@ -6,7 +6,7 @@ #pragma once -#include +#include #if defined(__FreeBSD_kernel__) # define INIT_FIFO "/etc/.initctl" @@ -25,13 +25,6 @@ #define INIT_CMD_UNSETENV 7 #define INIT_CMD_CHANGECONS 12345 - -#ifdef MAXHOSTNAMELEN -# define INITRQ_HLEN MAXHOSTNAMELEN -#else -# define INITRQ_HLEN 64 -#endif - /* * This is what BSD 4.4 uses when talking to init. * Linux doesn't use this right now. @@ -39,7 +32,7 @@ struct init_request_bsd { char gen_id[8]; /* Beats me.. telnetd uses "fe" */ char tty_id[16]; /* Tty name minus /dev/tty */ - char host[INITRQ_HLEN]; /* Hostname */ + char host[HOST_NAME_MAX]; /* Hostname */ char term_type[16]; /* Terminal type */ int signal; /* Signal to send */ int pid; /* Process to send to */ From 3b7d3320477452111464c85387b43876f0030a4e Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Mon, 5 May 2025 16:03:20 +0200 Subject: [PATCH 2/7] macro: Reduce transitive includes --- src/basic/alloc-util.h | 23 +++++++++++++ src/basic/architecture.h | 1 + src/basic/arphrd-util.c | 1 + src/basic/assert-util.c | 1 + src/basic/capsule-util.c | 2 ++ src/basic/cgroup-util.h | 1 + src/basic/confidential-virt.h | 1 + src/basic/devnum-util.c | 1 + src/basic/devnum-util.h | 1 + src/basic/dirent-util.h | 1 + src/basic/env-util.h | 1 + src/basic/errno-list.c | 1 + src/basic/errno-util.h | 9 +++-- src/basic/fd-util.h | 1 + src/basic/hash-funcs.c | 1 + src/basic/hexdecoct.c | 1 + src/basic/ioprio-util.c | 3 ++ src/basic/iovec-wrapper.c | 3 ++ src/basic/label.c | 1 + src/basic/locale-util.h | 1 + src/basic/log.h | 2 ++ src/basic/macro.h | 45 ------------------------- src/basic/memory-util.h | 1 + src/basic/nulstr-util.h | 1 + src/basic/percent-util.h | 1 + src/basic/pidref.h | 1 + src/basic/ratelimit.c | 2 ++ src/basic/sha256.c | 1 + src/basic/sort-util.h | 1 + src/basic/static-destruct.h | 2 ++ src/basic/string-table.c | 2 ++ src/basic/string-util.h | 1 + src/basic/syslog-util.c | 1 + src/basic/time-util.h | 1 + src/boot/boot.c | 2 ++ src/boot/efi-string.c | 2 ++ src/boot/pe.c | 2 ++ src/boot/stub.c | 2 ++ src/core/kill.h | 1 + src/core/unit-dependency-atom.c | 1 + src/fundamental/macro-fundamental.h | 1 - src/import/import-compress.h | 2 ++ src/import/qcow2-util.c | 1 + src/libsystemd/sd-bus/bus-signature.c | 1 + src/libsystemd/sd-journal/journal-def.h | 2 ++ src/libsystemd/sd-json/json-util.c | 2 ++ src/libudev/libudev-device.c | 1 + src/libudev/libudev-enumerate.c | 1 + src/libudev/libudev-hwdb.c | 2 ++ src/libudev/libudev-queue.c | 1 + src/libudev/libudev.c | 1 + src/network/netdev/netdev-util.h | 2 ++ src/network/tc/tc-util.c | 1 + src/resolve/dns-type.h | 2 ++ src/shared/apparmor-util.h | 1 + src/shared/blkid-util.c | 2 ++ src/shared/bond-util.h | 1 + src/shared/bus-locator.c | 1 + src/shared/color-util.c | 1 + src/shared/condition.h | 1 + src/shared/copy.c | 1 + src/shared/coredump-util.h | 3 ++ src/shared/dev-setup.c | 1 + src/shared/idn-util.h | 1 + src/shared/import-util.h | 1 + src/shared/ipvlan-util.h | 1 + src/shared/libarchive-util.h | 2 ++ src/shared/libfido2-util.h | 2 ++ src/shared/open-file.h | 2 ++ src/shared/recovery-key.c | 2 ++ src/shared/selinux-util.h | 1 + src/shared/smbios11.c | 1 + src/test/test-dirent-util.c | 1 + src/test/test-fs-util.c | 1 + src/test/test-json.c | 1 + src/test/test-sizeof.c | 1 + 76 files changed, 128 insertions(+), 48 deletions(-) diff --git a/src/basic/alloc-util.h b/src/basic/alloc-util.h index f82530bdc2..900e798b57 100644 --- a/src/basic/alloc-util.h +++ b/src/basic/alloc-util.h @@ -137,6 +137,29 @@ static inline void *memdup_suffix0_multiply(const void *p, size_t need, size_t s return memdup_suffix0(p, size * need); } +static inline size_t GREEDY_ALLOC_ROUND_UP(size_t l) { + size_t m; + + /* Round up allocation sizes a bit to some reasonable, likely larger value. This is supposed to be + * used for cases which are likely called in an allocation loop of some form, i.e. that repetitively + * grow stuff, for example strv_extend() and suchlike. + * + * Note the difference to GREEDY_REALLOC() here, as this helper operates on a single size value only, + * and rounds up to next multiple of 2, needing no further counter. + * + * Note the benefits of direct ALIGN_POWER2() usage: type-safety for size_t, sane handling for very + * small (i.e. <= 2) and safe handling for very large (i.e. > SSIZE_MAX) values. */ + + if (l <= 2) + return 2; /* Never allocate less than 2 of something. */ + + m = ALIGN_POWER2(l); + if (m == 0) /* overflow? */ + return l; + + return m; +} + void* greedy_realloc(void **p, size_t need, size_t size); void* greedy_realloc0(void **p, size_t need, size_t size); void* greedy_realloc_append(void **p, size_t *n_p, const void *from, size_t n_from, size_t size); diff --git a/src/basic/architecture.h b/src/basic/architecture.h index 26aa9f405e..4d06e5c3a4 100644 --- a/src/basic/architecture.h +++ b/src/basic/architecture.h @@ -2,6 +2,7 @@ #pragma once #include +#include #include "macro.h" diff --git a/src/basic/arphrd-util.c b/src/basic/arphrd-util.c index 540d7d984e..2aa48182fc 100644 --- a/src/basic/arphrd-util.c +++ b/src/basic/arphrd-util.c @@ -7,6 +7,7 @@ #include #include "arphrd-util.h" +#include "assert-util.h" #include "macro.h" static const struct arphrd_name* lookup_arphrd(register const char *str, register GPERF_LEN_TYPE len); diff --git a/src/basic/assert-util.c b/src/basic/assert-util.c index f4cf97ad28..add9e92c52 100644 --- a/src/basic/assert-util.c +++ b/src/basic/assert-util.c @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include #include #include "assert-util.h" diff --git a/src/basic/capsule-util.c b/src/basic/capsule-util.c index d2d6b19448..83fcf7afbc 100644 --- a/src/basic/capsule-util.c +++ b/src/basic/capsule-util.c @@ -1,5 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include + #include "alloc-util.h" #include "capsule-util.h" #include "path-util.h" diff --git a/src/basic/cgroup-util.h b/src/basic/cgroup-util.h index 949de7f9e2..8434a094a0 100644 --- a/src/basic/cgroup-util.h +++ b/src/basic/cgroup-util.h @@ -2,6 +2,7 @@ #pragma once #include +#include #include #include #include diff --git a/src/basic/confidential-virt.h b/src/basic/confidential-virt.h index 097bbf7cfe..bbab123102 100644 --- a/src/basic/confidential-virt.h +++ b/src/basic/confidential-virt.h @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once +#include #include #include "errno-list.h" diff --git a/src/basic/devnum-util.c b/src/basic/devnum-util.c index ab4da865f2..7be34ca0ef 100644 --- a/src/basic/devnum-util.c +++ b/src/basic/devnum-util.c @@ -2,6 +2,7 @@ #include #include +#include #include "alloc-util.h" #include "chase.h" diff --git a/src/basic/devnum-util.h b/src/basic/devnum-util.h index 0efca56780..0e07df5e8c 100644 --- a/src/basic/devnum-util.h +++ b/src/basic/devnum-util.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "stdio-util.h" diff --git a/src/basic/dirent-util.h b/src/basic/dirent-util.h index f3007b6bc3..4c8d96788b 100644 --- a/src/basic/dirent-util.h +++ b/src/basic/dirent-util.h @@ -5,6 +5,7 @@ #include #include +#include "assert-util.h" #include "macro.h" #include "path-util.h" diff --git a/src/basic/env-util.h b/src/basic/env-util.h index 52771ecc81..14f2e71823 100644 --- a/src/basic/env-util.h +++ b/src/basic/env-util.h @@ -6,6 +6,7 @@ #include #include +#include "assert-util.h" #include "macro.h" #include "string.h" diff --git a/src/basic/errno-list.c b/src/basic/errno-list.c index 897f630307..677b906cc5 100644 --- a/src/basic/errno-list.c +++ b/src/basic/errno-list.c @@ -3,6 +3,7 @@ #include #include +#include "assert-util.h" #include "errno-list.h" #include "macro.h" diff --git a/src/basic/errno-util.h b/src/basic/errno-util.h index e7dcad3b44..2a5a5abb8a 100644 --- a/src/basic/errno-util.h +++ b/src/basic/errno-util.h @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once +#include #include #include @@ -42,6 +43,12 @@ static inline void _reset_errno_(int *saved_errno) { PROTECT_ERRNO; \ errno = ABS(value) +#define return_with_errno(r, err) \ + do { \ + errno = ABS(err); \ + return r; \ + } while (false) + static inline int negative_errno(void) { /* This helper should be used to shut up gcc if you know 'errno' is * negative. Instead of "return -errno;", use "return negative_errno();" @@ -103,8 +110,6 @@ static inline int errno_or_else(int fallback) { return ERRNO_IS_NEG_##name(-ABS(r)); \ } -assert_cc(INT_MAX <= INTMAX_MAX); - /* For send()/recv() or read()/write(). */ static inline bool ERRNO_IS_NEG_TRANSIENT(intmax_t r) { return IN_SET(r, diff --git a/src/basic/fd-util.h b/src/basic/fd-util.h index 59c43e8140..82dd9d31e7 100644 --- a/src/basic/fd-util.h +++ b/src/basic/fd-util.h @@ -2,6 +2,7 @@ #pragma once #include +#include #include #include #include diff --git a/src/basic/hash-funcs.c b/src/basic/hash-funcs.c index b122c300b8..6540fa5dfc 100644 --- a/src/basic/hash-funcs.c +++ b/src/basic/hash-funcs.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #include +#include #include "hash-funcs.h" #include "path-util.h" diff --git a/src/basic/hexdecoct.c b/src/basic/hexdecoct.c index 1d8e60330c..a08a4697ac 100644 --- a/src/basic/hexdecoct.c +++ b/src/basic/hexdecoct.c @@ -2,6 +2,7 @@ #include #include +#include #include #include diff --git a/src/basic/ioprio-util.c b/src/basic/ioprio-util.c index 3add1bddd0..49cd0f67e1 100644 --- a/src/basic/ioprio-util.c +++ b/src/basic/ioprio-util.c @@ -1,5 +1,8 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include + +#include "assert-util.h" #include "ioprio-util.h" #include "parse-util.h" #include "string-table.h" diff --git a/src/basic/iovec-wrapper.c b/src/basic/iovec-wrapper.c index 5cc3cc2f93..27b84d2cdb 100644 --- a/src/basic/iovec-wrapper.c +++ b/src/basic/iovec-wrapper.c @@ -1,5 +1,8 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include +#include + #include "alloc-util.h" #include "iovec-util.h" #include "iovec-wrapper.h" diff --git a/src/basic/label.c b/src/basic/label.c index 6bae653188..b922fd2f19 100644 --- a/src/basic/label.c +++ b/src/basic/label.c @@ -3,6 +3,7 @@ #include #include +#include "assert-util.h" #include "label.h" #include "macro.h" diff --git a/src/basic/locale-util.h b/src/basic/locale-util.h index 81fe8d1084..da84799cf1 100644 --- a/src/basic/locale-util.h +++ b/src/basic/locale-util.h @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once +#include #include #include #include diff --git a/src/basic/log.h b/src/basic/log.h index 845a458f3e..b0e8ef2989 100644 --- a/src/basic/log.h +++ b/src/basic/log.h @@ -1,12 +1,14 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once +#include #include #include #include #include #include +#include "assert-util.h" #include "macro.h" /* Some structures we reference but don't want to pull in headers for */ diff --git a/src/basic/macro.h b/src/basic/macro.h index 9cee1868a5..9391fc5e88 100644 --- a/src/basic/macro.h +++ b/src/basic/macro.h @@ -1,13 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once -#include -#include -#include -#include -#include -#include - #include "constants.h" #include "macro-fundamental.h" @@ -32,15 +25,6 @@ /* test harness */ #define EXIT_TEST_SKIP 77 -/* builtins */ -#if __SIZEOF_INT__ == 4 -#define BUILTIN_FFS_U32(x) __builtin_ffs(x); -#elif __SIZEOF_LONG__ == 4 -#define BUILTIN_FFS_U32(x) __builtin_ffsl(x); -#else -#error "neither int nor long are four bytes long?!?" -#endif - static inline uint64_t u64_multiply_safe(uint64_t a, uint64_t b) { if (_unlikely_(a != 0 && b > (UINT64_MAX / a))) return 0; /* overflow */ @@ -66,29 +50,6 @@ static inline unsigned long ALIGN_POWER2(unsigned long u) { return 1UL << (sizeof(u) * 8 - __builtin_clzl(u - 1UL)); } -static inline size_t GREEDY_ALLOC_ROUND_UP(size_t l) { - size_t m; - - /* Round up allocation sizes a bit to some reasonable, likely larger value. This is supposed to be - * used for cases which are likely called in an allocation loop of some form, i.e. that repetitively - * grow stuff, for example strv_extend() and suchlike. - * - * Note the difference to GREEDY_REALLOC() here, as this helper operates on a single size value only, - * and rounds up to next multiple of 2, needing no further counter. - * - * Note the benefits of direct ALIGN_POWER2() usage: type-safety for size_t, sane handling for very - * small (i.e. <= 2) and safe handling for very large (i.e. > SSIZE_MAX) values. */ - - if (l <= 2) - return 2; /* Never allocate less than 2 of something. */ - - m = ALIGN_POWER2(l); - if (m == 0) /* overflow? */ - return l; - - return m; -} - /* * container_of - cast a member of a structure out to the containing structure * @ptr: the pointer to the member. @@ -102,12 +63,6 @@ static inline size_t GREEDY_ALLOC_ROUND_UP(size_t l) { (type*)( (char *)UNIQ_T(A, uniq) - offsetof(type, member) ); \ }) -#define return_with_errno(r, err) \ - do { \ - errno = ABS(err); \ - return r; \ - } while (false) - #define PTR_TO_INT(p) ((int) ((intptr_t) (p))) #define INT_TO_PTR(u) ((void *) ((intptr_t) (u))) #define PTR_TO_UINT(p) ((unsigned) ((uintptr_t) (p))) diff --git a/src/basic/memory-util.h b/src/basic/memory-util.h index 443fc3a8ab..7a86cac5bc 100644 --- a/src/basic/memory-util.h +++ b/src/basic/memory-util.h @@ -2,6 +2,7 @@ #pragma once #include +#include #include #include #include diff --git a/src/basic/nulstr-util.h b/src/basic/nulstr-util.h index d6f2f58e9f..6a6c15a2e4 100644 --- a/src/basic/nulstr-util.h +++ b/src/basic/nulstr-util.h @@ -6,6 +6,7 @@ #include #include +#include "assert-util.h" #include "set.h" #define NULSTR_FOREACH(i, l) \ diff --git a/src/basic/percent-util.h b/src/basic/percent-util.h index e975d6e3e3..dd70d59dba 100644 --- a/src/basic/percent-util.h +++ b/src/basic/percent-util.h @@ -3,6 +3,7 @@ #include #include +#include #include "macro.h" diff --git a/src/basic/pidref.h b/src/basic/pidref.h index 59a4e39204..6084f9cf89 100644 --- a/src/basic/pidref.h +++ b/src/basic/pidref.h @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once +#include #include #include "macro.h" diff --git a/src/basic/ratelimit.c b/src/basic/ratelimit.c index df8c95c562..351fcac3bd 100644 --- a/src/basic/ratelimit.c +++ b/src/basic/ratelimit.c @@ -1,7 +1,9 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include #include +#include "assert-util.h" #include "macro.h" #include "ratelimit.h" diff --git a/src/basic/sha256.c b/src/basic/sha256.c index 1017a550fc..c4c2e85cec 100644 --- a/src/basic/sha256.c +++ b/src/basic/sha256.c @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include #include #include "alloc-util.h" diff --git a/src/basic/sort-util.h b/src/basic/sort-util.h index 9c818bd747..2820f2327c 100644 --- a/src/basic/sort-util.h +++ b/src/basic/sort-util.h @@ -3,6 +3,7 @@ #include +#include "assert-util.h" #include "macro.h" /* This is the same as glibc's internal __compar_d_fn_t type. glibc exports a public comparison_fn_t, for the diff --git a/src/basic/static-destruct.h b/src/basic/static-destruct.h index 2c7c75628f..f167d6413e 100644 --- a/src/basic/static-destruct.h +++ b/src/basic/static-destruct.h @@ -2,6 +2,8 @@ #pragma once +#include + #include "macro.h" #include "memory-util.h" diff --git a/src/basic/string-table.c b/src/basic/string-table.c index 8f2b4cb15b..30c912adb6 100644 --- a/src/basic/string-table.c +++ b/src/basic/string-table.c @@ -1,5 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include + #include "parse-util.h" #include "string-table.h" #include "string-util.h" diff --git a/src/basic/string-util.h b/src/basic/string-util.h index e00a5f5977..17fac1bfab 100644 --- a/src/basic/string-util.h +++ b/src/basic/string-util.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "alloc-util.h" #include "macro.h" diff --git a/src/basic/syslog-util.c b/src/basic/syslog-util.c index 0371922e39..18194f70d9 100644 --- a/src/basic/syslog-util.c +++ b/src/basic/syslog-util.c @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include #include #include "sd-id128.h" diff --git a/src/basic/time-util.h b/src/basic/time-util.h index 3f6e3c9f41..83d7e660ac 100644 --- a/src/basic/time-util.h +++ b/src/basic/time-util.h @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once +#include #include #include #include diff --git a/src/boot/boot.c b/src/boot/boot.c index c7f354506c..99d83f5462 100644 --- a/src/boot/boot.c +++ b/src/boot/boot.c @@ -1,5 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include + #include "bcd.h" #include "bootspec-fundamental.h" #include "console.h" diff --git a/src/boot/efi-string.c b/src/boot/efi-string.c index e8dbe297ed..4b9c3181c0 100644 --- a/src/boot/efi-string.c +++ b/src/boot/efi-string.c @@ -1,5 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include + #include "efi-string.h" #if SD_BOOT diff --git a/src/boot/pe.c b/src/boot/pe.c index 1a0feb633e..c5cd6a57ca 100644 --- a/src/boot/pe.c +++ b/src/boot/pe.c @@ -1,5 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include + #include "chid.h" #include "devicetree.h" #include "efi-firmware.h" diff --git a/src/boot/stub.c b/src/boot/stub.c index 4793391b06..9cc115a1f6 100644 --- a/src/boot/stub.c +++ b/src/boot/stub.c @@ -1,5 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include + #include "cpio.h" #include "device-path-util.h" #include "devicetree.h" diff --git a/src/core/kill.h b/src/core/kill.h index 97a295e1e5..ffa2280e60 100644 --- a/src/core/kill.h +++ b/src/core/kill.h @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once +#include #include #include diff --git a/src/core/unit-dependency-atom.c b/src/core/unit-dependency-atom.c index aba58612f7..06def183b9 100644 --- a/src/core/unit-dependency-atom.c +++ b/src/core/unit-dependency-atom.c @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include "assert-util.h" #include "unit-dependency-atom.h" static const UnitDependencyAtom atom_map[_UNIT_DEPENDENCY_MAX] = { diff --git a/src/fundamental/macro-fundamental.h b/src/fundamental/macro-fundamental.h index 597228f139..54b3e6a651 100644 --- a/src/fundamental/macro-fundamental.h +++ b/src/fundamental/macro-fundamental.h @@ -1,7 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once -#include #include #include #include diff --git a/src/import/import-compress.h b/src/import/import-compress.h index 6ad6cf7a67..160d9e62a6 100644 --- a/src/import/import-compress.h +++ b/src/import/import-compress.h @@ -1,6 +1,8 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once +#include + #if HAVE_BZIP2 #include #endif diff --git a/src/import/qcow2-util.c b/src/import/qcow2-util.c index fc34f30603..6c186f6128 100644 --- a/src/import/qcow2-util.c +++ b/src/import/qcow2-util.c @@ -3,6 +3,7 @@ #include #include "alloc-util.h" +#include "assert-util.h" #include "btrfs-util.h" #include "qcow2-util.h" #include "sparse-endian.h" diff --git a/src/libsystemd/sd-bus/bus-signature.c b/src/libsystemd/sd-bus/bus-signature.c index 78c743648b..16693bcad6 100644 --- a/src/libsystemd/sd-bus/bus-signature.c +++ b/src/libsystemd/sd-bus/bus-signature.c @@ -2,6 +2,7 @@ #include "sd-bus.h" +#include "assert-util.h" #include "bus-signature.h" #include "bus-type.h" diff --git a/src/libsystemd/sd-journal/journal-def.h b/src/libsystemd/sd-journal/journal-def.h index 02576077b0..28cccca21a 100644 --- a/src/libsystemd/sd-journal/journal-def.h +++ b/src/libsystemd/sd-journal/journal-def.h @@ -1,6 +1,8 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once +#include + #include "sd-id128.h" #include "macro.h" diff --git a/src/libsystemd/sd-json/json-util.c b/src/libsystemd/sd-json/json-util.c index 0fb5e87df7..a4beb54cba 100644 --- a/src/libsystemd/sd-json/json-util.c +++ b/src/libsystemd/sd-json/json-util.c @@ -1,5 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include + #include "alloc-util.h" #include "devnum-util.h" #include "env-util.h" diff --git a/src/libudev/libudev-device.c b/src/libudev/libudev-device.c index ae62138c8f..4ad1489307 100644 --- a/src/libudev/libudev-device.c +++ b/src/libudev/libudev-device.c @@ -22,6 +22,7 @@ #include "alloc-util.h" #include "device-private.h" #include "device-util.h" +#include "errno-util.h" #include "libudev-device-internal.h" #include "libudev-list-internal.h" #include "parse-util.h" diff --git a/src/libudev/libudev-enumerate.c b/src/libudev/libudev-enumerate.c index 270df0fa66..6afcc68256 100644 --- a/src/libudev/libudev-enumerate.c +++ b/src/libudev/libudev-enumerate.c @@ -16,6 +16,7 @@ #include "alloc-util.h" #include "device-enumerator-private.h" #include "device-util.h" +#include "errno-util.h" #include "libudev-device-internal.h" #include "libudev-list-internal.h" diff --git a/src/libudev/libudev-hwdb.c b/src/libudev/libudev-hwdb.c index 7144905e85..edc1c664a8 100644 --- a/src/libudev/libudev-hwdb.c +++ b/src/libudev/libudev-hwdb.c @@ -5,6 +5,8 @@ #include "sd-hwdb.h" #include "alloc-util.h" +#include "assert-util.h" +#include "errno-util.h" #include "hwdb-util.h" #include "libudev-list-internal.h" #include "log.h" diff --git a/src/libudev/libudev-queue.c b/src/libudev/libudev-queue.c index 1d5a024b79..c4aa78dd65 100644 --- a/src/libudev/libudev-queue.c +++ b/src/libudev/libudev-queue.c @@ -9,6 +9,7 @@ #include "libudev.h" #include "alloc-util.h" +#include "errno-util.h" #include "fd-util.h" #include "io-util.h" #include "udev-util.h" diff --git a/src/libudev/libudev.c b/src/libudev/libudev.c index a6004e3999..ae3260633a 100644 --- a/src/libudev/libudev.c +++ b/src/libudev/libudev.c @@ -9,6 +9,7 @@ #include "libudev.h" #include "alloc-util.h" +#include "errno-util.h" #include "fd-util.h" #include "log.h" #include "string-util.h" diff --git a/src/network/netdev/netdev-util.h b/src/network/netdev/netdev-util.h index 7c8c397d6c..089326c396 100644 --- a/src/network/netdev/netdev-util.h +++ b/src/network/netdev/netdev-util.h @@ -1,6 +1,8 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once +#include + #include "in-addr-util.h" #include "macro.h" diff --git a/src/network/tc/tc-util.c b/src/network/tc/tc-util.c index 3781182946..ce9b5ecdae 100644 --- a/src/network/tc/tc-util.c +++ b/src/network/tc/tc-util.c @@ -2,6 +2,7 @@ * Copyright © 2019 VMware, Inc. */ #include "alloc-util.h" +#include "assert-util.h" #include "extract-word.h" #include "fileio.h" #include "parse-util.h" diff --git a/src/resolve/dns-type.h b/src/resolve/dns-type.h index 657526b470..590e8f4008 100644 --- a/src/resolve/dns-type.h +++ b/src/resolve/dns-type.h @@ -1,6 +1,8 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once +#include + #include "macro.h" /* DNS record types, taken from diff --git a/src/shared/apparmor-util.h b/src/shared/apparmor-util.h index e88bf9d111..b1b511aed5 100644 --- a/src/shared/apparmor-util.h +++ b/src/shared/apparmor-util.h @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once +#include #include #include "dlfcn-util.h" diff --git a/src/shared/blkid-util.c b/src/shared/blkid-util.c index 377a277851..5446eb0f91 100644 --- a/src/shared/blkid-util.c +++ b/src/shared/blkid-util.c @@ -2,6 +2,8 @@ #if HAVE_BLKID +#include + #include "blkid-util.h" #include "string-util.h" diff --git a/src/shared/bond-util.h b/src/shared/bond-util.h index 557912c73f..84b18942ee 100644 --- a/src/shared/bond-util.h +++ b/src/shared/bond-util.h @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once +#include #include #include diff --git a/src/shared/bus-locator.c b/src/shared/bus-locator.c index 80d2b5371c..9ceb976b0b 100644 --- a/src/shared/bus-locator.c +++ b/src/shared/bus-locator.c @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include "assert-util.h" #include "bus-locator.h" #include "macro.h" diff --git a/src/shared/color-util.c b/src/shared/color-util.c index 9d714c0fff..e8258de95d 100644 --- a/src/shared/color-util.c +++ b/src/shared/color-util.c @@ -2,6 +2,7 @@ #include +#include "assert-util.h" #include "color-util.h" #include "macro.h" diff --git a/src/shared/condition.h b/src/shared/condition.h index 84b9273841..0644ec28f5 100644 --- a/src/shared/condition.h +++ b/src/shared/condition.h @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once +#include #include #include diff --git a/src/shared/copy.c b/src/shared/copy.c index fb006e38ed..2f153a9732 100644 --- a/src/shared/copy.c +++ b/src/shared/copy.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include diff --git a/src/shared/coredump-util.h b/src/shared/coredump-util.h index 4f54bb94c0..a72db4b4e5 100644 --- a/src/shared/coredump-util.h +++ b/src/shared/coredump-util.h @@ -1,6 +1,9 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once +#include +#include + #include "macro.h" typedef enum CoredumpFilter { diff --git a/src/shared/dev-setup.c b/src/shared/dev-setup.c index e0e0f7aac1..0ac195c86f 100644 --- a/src/shared/dev-setup.c +++ b/src/shared/dev-setup.c @@ -2,6 +2,7 @@ #include #include +#include #include #include "alloc-util.h" diff --git a/src/shared/idn-util.h b/src/shared/idn-util.h index 822800cc22..986f2cfd9e 100644 --- a/src/shared/idn-util.h +++ b/src/shared/idn-util.h @@ -8,6 +8,7 @@ # include #endif +#include #include #if HAVE_LIBIDN2 || HAVE_LIBIDN diff --git a/src/shared/import-util.h b/src/shared/import-util.h index 98b99e371f..91bc3083e4 100644 --- a/src/shared/import-util.h +++ b/src/shared/import-util.h @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once +#include #include #include "macro.h" diff --git a/src/shared/ipvlan-util.h b/src/shared/ipvlan-util.h index ca7b0b40f5..bc286d9d88 100644 --- a/src/shared/ipvlan-util.h +++ b/src/shared/ipvlan-util.h @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once +#include #include #include diff --git a/src/shared/libarchive-util.h b/src/shared/libarchive-util.h index 1efdacdd36..2695b18a7f 100644 --- a/src/shared/libarchive-util.h +++ b/src/shared/libarchive-util.h @@ -1,6 +1,8 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once +#include + #include "dlfcn-util.h" #if HAVE_LIBARCHIVE diff --git a/src/shared/libfido2-util.h b/src/shared/libfido2-util.h index 4a0d8f8cbe..18aec372a7 100644 --- a/src/shared/libfido2-util.h +++ b/src/shared/libfido2-util.h @@ -1,6 +1,8 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once +#include + #include "iovec-util.h" #include "macro.h" diff --git a/src/shared/open-file.h b/src/shared/open-file.h index fe38801001..d6cab52359 100644 --- a/src/shared/open-file.h +++ b/src/shared/open-file.h @@ -1,6 +1,8 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once +#include + #include "list.h" #include "macro.h" #include "memory-util.h" diff --git a/src/shared/recovery-key.c b/src/shared/recovery-key.c index 3233f05dfc..6c73370076 100644 --- a/src/shared/recovery-key.c +++ b/src/shared/recovery-key.c @@ -1,5 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include + #include "alloc-util.h" #include "memory-util.h" #include "random-util.h" diff --git a/src/shared/selinux-util.h b/src/shared/selinux-util.h index 4ed4a33198..979c3f5be9 100644 --- a/src/shared/selinux-util.h +++ b/src/shared/selinux-util.h @@ -6,6 +6,7 @@ #include #include +#include "assert-util.h" #include "label-util.h" #include "macro.h" diff --git a/src/shared/smbios11.c b/src/shared/smbios11.c index bea4f7f1ee..bd095992e9 100644 --- a/src/shared/smbios11.c +++ b/src/shared/smbios11.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "alloc-util.h" +#include "assert-util.h" #include "fileio.h" #include "macro.h" #include "smbios11.h" diff --git a/src/test/test-dirent-util.c b/src/test/test-dirent-util.c index 44e26e8e24..cb31edcdc6 100644 --- a/src/test/test-dirent-util.c +++ b/src/test/test-dirent-util.c @@ -4,6 +4,7 @@ #include #include #include +#include #include "alloc-util.h" #include "dirent-util.h" diff --git a/src/test/test-fs-util.c b/src/test/test-fs-util.c index 0670bb7f8e..e3c3149db1 100644 --- a/src/test/test-fs-util.c +++ b/src/test/test-fs-util.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #include +#include #include #include "alloc-util.h" diff --git a/src/test/test-json.c b/src/test/test-json.c index 8dd5746495..3fe2f26274 100644 --- a/src/test/test-json.c +++ b/src/test/test-json.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #include +#include #include "sd-json.h" diff --git a/src/test/test-sizeof.c b/src/test/test-sizeof.c index c3f1bdee89..fe90341dc8 100644 --- a/src/test/test-sizeof.c +++ b/src/test/test-sizeof.c @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include #include #include #include From c992250c2430110bc97a10216e7be80addcaff22 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Wed, 7 May 2025 11:15:12 +0200 Subject: [PATCH 3/7] basic: Move parts of audit-util.{c,h} to libaudit-util.{c,h} in shared/ --- src/basic/audit-util.c | 83 ------------------------- src/basic/audit-util.h | 36 ----------- src/core/audit-fd.c | 2 +- src/shared/condition.c | 2 +- src/shared/libaudit-util.c | 113 ++++++++++++++++++++++++++++++++++ src/shared/libaudit-util.h | 9 +++ src/shared/meson.build | 2 + src/sysusers/sysusers.c | 6 +- src/test/test-condition.c | 2 +- src/update-utmp/update-utmp.c | 6 +- 10 files changed, 137 insertions(+), 124 deletions(-) create mode 100644 src/shared/libaudit-util.c create mode 100644 src/shared/libaudit-util.h diff --git a/src/basic/audit-util.c b/src/basic/audit-util.c index 0d664b0d8b..9a9f2849ed 100644 --- a/src/basic/audit-util.c +++ b/src/basic/audit-util.c @@ -1,20 +1,10 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ -#include -#include -#include -#include -#include - #include "alloc-util.h" #include "audit-util.h" -#include "fd-util.h" #include "fileio.h" -#include "iovec-util.h" -#include "macro.h" #include "parse-util.h" #include "process-util.h" -#include "socket-util.h" #include "stat-util.h" #include "user-util.h" #include "virt.h" @@ -94,76 +84,3 @@ int audit_loginuid_from_pid(const PidRef *pid, uid_t *ret_uid) { return parse_uid(s, ret_uid); } - -static int try_audit_request(int fd) { - struct iovec iov; - struct msghdr mh; - ssize_t n; - - assert(fd >= 0); - - struct { - struct nlmsghdr hdr; - struct nlmsgerr err; - } _packed_ msg = { - .hdr.nlmsg_len = NLMSG_LENGTH(0), - .hdr.nlmsg_type = AUDIT_GET_FEATURE, - .hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK, - }; - iov = IOVEC_MAKE(&msg, msg.hdr.nlmsg_len); - mh = (struct msghdr) { - .msg_iov = &iov, - .msg_iovlen = 1, - }; - - if (sendmsg(fd, &mh, MSG_NOSIGNAL) < 0) - return -errno; - - iov.iov_len = sizeof(msg); - - n = recvmsg_safe(fd, &mh, 0); - if (n < 0) - return n; - if (n != NLMSG_LENGTH(sizeof(struct nlmsgerr))) - return -EIO; - - if (msg.hdr.nlmsg_type != NLMSG_ERROR) - return -EINVAL; - - return msg.err.error; -} - -bool use_audit(void) { - static int cached_use = -1; - int r; - - if (cached_use >= 0) - return cached_use; - - _cleanup_close_ int fd = socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC|SOCK_NONBLOCK, NETLINK_AUDIT); - if (fd < 0) { - cached_use = !ERRNO_IS_PRIVILEGE(errno) && !ERRNO_IS_NOT_SUPPORTED(errno); - if (cached_use) - log_debug_errno(errno, "Unexpected error while creating audit socket, proceeding with its use: %m"); - else - log_debug_errno(errno, "Won't talk to audit, because feature or privilege absent: %m"); - } else { - /* If we try and use the audit fd but get -ECONNREFUSED, it is because we are not in the - * initial user namespace, and the kernel does not have support for audit outside of the - * initial user namespace (see - * https://elixir.bootlin.com/linux/latest/C/ident/audit_netlink_ok). - * - * If we receive any other error, do not disable audit because we are not sure that the error - * indicates that audit will not work in general. */ - r = try_audit_request(fd); - if (r < 0) { - cached_use = r != -ECONNREFUSED; - log_debug_errno(r, cached_use ? - "Failed to make request on audit fd, ignoring: %m" : - "Won't talk to audit: %m"); - } else - cached_use = true; - } - - return cached_use; -} diff --git a/src/basic/audit-util.h b/src/basic/audit-util.h index 1ea5610e6d..98b1b3631a 100644 --- a/src/basic/audit-util.h +++ b/src/basic/audit-util.h @@ -1,16 +1,9 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once -#if HAVE_AUDIT -# include -#endif - #include #include -#include -#include "errno-util.h" -#include "log.h" #include "pidref.h" #define AUDIT_SESSION_INVALID UINT32_MAX @@ -18,35 +11,6 @@ int audit_session_from_pid(const PidRef *pid, uint32_t *ret_id); int audit_loginuid_from_pid(const PidRef *pid, uid_t *ret_uid); -bool use_audit(void); - static inline bool audit_session_is_valid(uint32_t id) { return id > 0 && id != AUDIT_SESSION_INVALID; } - -/* The wrappers for audit_open() and audit_close() are inline functions so that we don't get a spurious - * linkage to libaudit in libbasic, but we also don't need to create a separate source file for two very - * short functions. */ - -static inline int close_audit_fd(int fd) { -#if HAVE_AUDIT - if (fd >= 0) - audit_close(fd); -#else - assert(fd < 0); -#endif - return -EBADF; -} - -static inline int open_audit_fd_or_warn(void) { - int fd = -EBADF; - -#if HAVE_AUDIT - /* If the kernel lacks netlink or audit support, don't worry about it. */ - fd = audit_open(); - if (fd < 0) - return log_full_errno(ERRNO_IS_NOT_SUPPORTED(errno) ? LOG_DEBUG : LOG_WARNING, - errno, "Failed to connect to audit log, ignoring: %m"); -#endif - return fd; -} diff --git a/src/core/audit-fd.c b/src/core/audit-fd.c index 87b085c1bf..b09f3ac49a 100644 --- a/src/core/audit-fd.c +++ b/src/core/audit-fd.c @@ -7,7 +7,7 @@ #if HAVE_AUDIT # include -# include "audit-util.h" +# include "libaudit-util.h" # include "capability-util.h" static bool initialized = false; diff --git a/src/shared/condition.c b/src/shared/condition.c index 726f940489..63acf3906c 100644 --- a/src/shared/condition.c +++ b/src/shared/condition.c @@ -18,7 +18,6 @@ #include "alloc-util.h" #include "apparmor-util.h" #include "architecture.h" -#include "audit-util.h" #include "battery-util.h" #include "bitfield.h" #include "blockdev-util.h" @@ -44,6 +43,7 @@ #include "id128-util.h" #include "ima-util.h" #include "initrd-util.h" +#include "libaudit-util.h" #include "limits-util.h" #include "list.h" #include "macro.h" diff --git a/src/shared/libaudit-util.c b/src/shared/libaudit-util.c new file mode 100644 index 0000000000..e84684e581 --- /dev/null +++ b/src/shared/libaudit-util.c @@ -0,0 +1,113 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include +#include +#include +#include +#include + +#if HAVE_AUDIT +# include +#endif + +#include "fd-util.h" +#include "iovec-util.h" +#include "libaudit-util.h" +#include "log.h" +#include "socket-util.h" + +static int try_audit_request(int fd) { + struct iovec iov; + struct msghdr mh; + ssize_t n; + + assert(fd >= 0); + + struct { + struct nlmsghdr hdr; + struct nlmsgerr err; + } _packed_ msg = { + .hdr.nlmsg_len = NLMSG_LENGTH(0), + .hdr.nlmsg_type = AUDIT_GET_FEATURE, + .hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK, + }; + iov = IOVEC_MAKE(&msg, msg.hdr.nlmsg_len); + mh = (struct msghdr) { + .msg_iov = &iov, + .msg_iovlen = 1, + }; + + if (sendmsg(fd, &mh, MSG_NOSIGNAL) < 0) + return -errno; + + iov.iov_len = sizeof(msg); + + n = recvmsg_safe(fd, &mh, 0); + if (n < 0) + return n; + if (n != NLMSG_LENGTH(sizeof(struct nlmsgerr))) + return -EIO; + + if (msg.hdr.nlmsg_type != NLMSG_ERROR) + return -EINVAL; + + return msg.err.error; +} + +bool use_audit(void) { + static int cached_use = -1; + int r; + + if (cached_use >= 0) + return cached_use; + + _cleanup_close_ int fd = socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC|SOCK_NONBLOCK, NETLINK_AUDIT); + if (fd < 0) { + cached_use = !ERRNO_IS_PRIVILEGE(errno) && !ERRNO_IS_NOT_SUPPORTED(errno); + if (cached_use) + log_debug_errno(errno, "Unexpected error while creating audit socket, proceeding with its use: %m"); + else + log_debug_errno(errno, "Won't talk to audit, because feature or privilege absent: %m"); + } else { + /* If we try and use the audit fd but get -ECONNREFUSED, it is because we are not in the + * initial user namespace, and the kernel does not have support for audit outside of the + * initial user namespace (see + * https://elixir.bootlin.com/linux/latest/C/ident/audit_netlink_ok). + * + * If we receive any other error, do not disable audit because we are not sure that the error + * indicates that audit will not work in general. */ + r = try_audit_request(fd); + if (r < 0) { + cached_use = r != -ECONNREFUSED; + log_debug_errno(r, cached_use ? + "Failed to make request on audit fd, ignoring: %m" : + "Won't talk to audit: %m"); + } else + cached_use = true; + } + + return cached_use; +} + +int close_audit_fd(int fd) { +#if HAVE_AUDIT + if (fd >= 0) + audit_close(fd); +#else + assert(fd < 0); +#endif + return -EBADF; +} + +int open_audit_fd_or_warn(void) { + int fd = -EBADF; + +#if HAVE_AUDIT + /* If the kernel lacks netlink or audit support, don't worry about it. */ + fd = audit_open(); + if (fd < 0) + return log_full_errno(ERRNO_IS_NOT_SUPPORTED(errno) ? LOG_DEBUG : LOG_WARNING, + errno, "Failed to connect to audit log, ignoring: %m"); +#endif + return fd; +} diff --git a/src/shared/libaudit-util.h b/src/shared/libaudit-util.h new file mode 100644 index 0000000000..f00d816f78 --- /dev/null +++ b/src/shared/libaudit-util.h @@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +#include + +bool use_audit(void); + +int close_audit_fd(int fd); +int open_audit_fd_or_warn(void); diff --git a/src/shared/meson.build b/src/shared/meson.build index da1deffe4d..0a514dec7d 100644 --- a/src/shared/meson.build +++ b/src/shared/meson.build @@ -106,6 +106,7 @@ shared_sources = files( 'killall.c', 'label-util.c', 'libarchive-util.c', + 'libaudit-util.c', 'libcrypt-util.c', 'libfido2-util.c', 'libmount-util.c', @@ -332,6 +333,7 @@ libshared_name = 'systemd-shared-@0@'.format(shared_lib_tag) libshared_deps = [threads, libacl, + libaudit, libblkid, libcap, libcrypt, diff --git a/src/sysusers/sysusers.c b/src/sysusers/sysusers.c index 3d36b4ff64..4163994df2 100644 --- a/src/sysusers/sysusers.c +++ b/src/sysusers/sysusers.c @@ -2,8 +2,11 @@ #include +#if HAVE_AUDIT +# include +#endif + #include "alloc-util.h" -#include "audit-util.h" #include "build.h" #include "chase.h" #include "conf-files.h" @@ -18,6 +21,7 @@ #include "fs-util.h" #include "hashmap.h" #include "image-policy.h" +#include "libaudit-util.h" #include "libcrypt-util.h" #include "main-func.h" #include "memory-util.h" diff --git a/src/test/test-condition.c b/src/test/test-condition.c index 98418d7ac3..19377962b7 100644 --- a/src/test/test-condition.c +++ b/src/test/test-condition.c @@ -11,7 +11,6 @@ #include "alloc-util.h" #include "apparmor-util.h" #include "architecture.h" -#include "audit-util.h" #include "battery-util.h" #include "cgroup-util.h" #include "condition.h" @@ -26,6 +25,7 @@ #include "hostname-util.h" #include "id128-util.h" #include "ima-util.h" +#include "libaudit-util.h" #include "limits-util.h" #include "log.h" #include "macro.h" diff --git a/src/update-utmp/update-utmp.c b/src/update-utmp/update-utmp.c index 488c8c18ea..c39ea9b35d 100644 --- a/src/update-utmp/update-utmp.c +++ b/src/update-utmp/update-utmp.c @@ -5,14 +5,18 @@ #include #include +#if HAVE_AUDIT +# include +#endif + #include "sd-bus.h" #include "alloc-util.h" -#include "audit-util.h" #include "bus-error.h" #include "bus-locator.h" #include "bus-util.h" #include "format-util.h" +#include "libaudit-util.h" #include "log.h" #include "macro.h" #include "main-func.h" From 7227d5bfff7a765028841a9dc353374a910a5d5b Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Wed, 7 May 2025 11:26:36 +0200 Subject: [PATCH 4/7] terminal-util: Move various functions to ansi-color.c --- src/basic/ansi-color.c | 98 ++++++++++++++++++++++++++++++++ src/basic/ansi-color.h | 29 +++++++++- src/basic/meson.build | 1 + src/basic/terminal-util.c | 87 +--------------------------- src/basic/terminal-util.h | 20 ------- src/network/networkctl-journal.c | 1 + 6 files changed, 130 insertions(+), 106 deletions(-) create mode 100644 src/basic/ansi-color.c diff --git a/src/basic/ansi-color.c b/src/basic/ansi-color.c new file mode 100644 index 0000000000..18e25f3bb0 --- /dev/null +++ b/src/basic/ansi-color.c @@ -0,0 +1,98 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include "ansi-color.h" +#include "log.h" +#include "process-util.h" +#include "string-table.h" +#include "strv.h" +#include "terminal-util.h" + +static volatile int cached_color_mode = _COLOR_MODE_INVALID; +static volatile int cached_underline_enabled = -1; + +bool underline_enabled(void) { + + if (cached_underline_enabled < 0) { + + /* The Linux console doesn't support underlining, turn it off, but only there. */ + + if (colors_enabled()) + cached_underline_enabled = !streq_ptr(getenv("TERM"), "linux"); + else + cached_underline_enabled = false; + } + + return cached_underline_enabled; +} + +void reset_ansi_feature_caches(void) { + cached_color_mode = _COLOR_MODE_INVALID; + cached_underline_enabled = -1; +} + +ColorMode parse_systemd_colors(void) { + const char *e; + + e = getenv("SYSTEMD_COLORS"); + if (!e) + return _COLOR_MODE_INVALID; + + ColorMode m = color_mode_from_string(e); + if (m < 0) + return log_debug_errno(m, "Failed to parse $SYSTEMD_COLORS value '%s', ignoring: %m", e); + + return m; +} + +static ColorMode get_color_mode_impl(void) { + /* Returns the mode used to choose output colors. The possible modes are COLOR_OFF for no colors, + * COLOR_16 for only the base 16 ANSI colors, COLOR_256 for more colors, and COLOR_24BIT for + * unrestricted color output. */ + + /* First, we check $SYSTEMD_COLORS, which is the explicit way to change the mode. */ + ColorMode m = parse_systemd_colors(); + if (m >= 0) + return m; + + /* Next, check for the presence of $NO_COLOR; value is ignored. */ + if (getenv("NO_COLOR")) + return COLOR_OFF; + + /* If the above didn't work, we turn colors off unless we are on a TTY. And if we are on a TTY we + * turn it off if $TERM is set to "dumb". There's one special tweak though: if we are PID 1 then we + * do not check whether we are connected to a TTY, because we don't keep /dev/console open + * continuously due to fear of SAK, and hence things are a bit weird. */ + if (getpid_cached() == 1 ? getenv_terminal_is_dumb() : terminal_is_dumb()) + return COLOR_OFF; + + /* We failed to figure out any reason to *disable* colors. Let's see how many colors we shall use. */ + if (STRPTR_IN_SET(getenv("COLORTERM"), + "truecolor", + "24bit")) + return COLOR_24BIT; + + /* Note that the Linux console can only display 16 colors. We still enable 256 color mode + * even for PID1 output though (which typically goes to the Linux console), since the Linux + * console is able to parse the 256 color sequences and automatically map them to the closest + * color in the 16 color palette (since kernel 3.16). Doing 256 colors is nice for people who + * invoke systemd in a container or via a serial link or such, and use a true 256 color + * terminal to do so. */ + return COLOR_256; +} + +ColorMode get_color_mode(void) { + if (cached_color_mode < 0) + cached_color_mode = get_color_mode_impl(); + + return cached_color_mode; +} + + +static const char* const color_mode_table[_COLOR_MODE_MAX] = { + [COLOR_OFF] = "off", + [COLOR_16] = "16", + [COLOR_256] = "256", + [COLOR_24BIT] = "24bit", +}; + +DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(color_mode, ColorMode, COLOR_24BIT); diff --git a/src/basic/ansi-color.h b/src/basic/ansi-color.h index 5f1418109c..f3e55e8165 100644 --- a/src/basic/ansi-color.h +++ b/src/basic/ansi-color.h @@ -1,7 +1,34 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once -#include "terminal-util.h" +#include + +#include "macro.h" + +/* Limits the use of ANSI colors to a subset. */ +typedef enum ColorMode { + COLOR_OFF, /* No colors, monochrome output. */ + COLOR_16, /* Only the base 16 colors. */ + COLOR_256, /* Only 256 colors. */ + COLOR_24BIT, /* For truecolor or 24bit color support, no restriction. */ + _COLOR_MODE_MAX, + _COLOR_MODE_INVALID = -EINVAL, +} ColorMode; + +const char* color_mode_to_string(ColorMode m) _const_; +ColorMode color_mode_from_string(const char *s) _pure_; + +ColorMode get_color_mode(void); +static inline bool colors_enabled(void) { + /* Returns true if colors are considered supported on our stdout. */ + return get_color_mode() != COLOR_OFF; +} + +ColorMode parse_systemd_colors(void); + +bool underline_enabled(void); + +void reset_ansi_feature_caches(void); /* Regular colors */ #define ANSI_BLACK "\x1B[0;30m" /* Some type of grey usually. */ diff --git a/src/basic/meson.build b/src/basic/meson.build index 7cc29bd039..4588a9a6e8 100644 --- a/src/basic/meson.build +++ b/src/basic/meson.build @@ -4,6 +4,7 @@ basic_sources = files( 'MurmurHash2.c', 'af-list.c', 'alloc-util.c', + 'ansi-color.c', 'architecture.c', 'argv-util.c', 'arphrd-util.c', diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c index e59c8c2efe..acd2538952 100644 --- a/src/basic/terminal-util.c +++ b/src/basic/terminal-util.c @@ -60,8 +60,6 @@ static volatile unsigned cached_lines = 0; static volatile int cached_on_tty = -1; static volatile int cached_on_dev_null = -1; -static volatile int cached_color_mode = _COLOR_MODE_INVALID; -static volatile int cached_underline_enabled = -1; bool isatty_safe(int fd) { assert(fd >= 0); @@ -1433,10 +1431,10 @@ void reset_terminal_feature_caches(void) { cached_columns = 0; cached_lines = 0; - cached_color_mode = _COLOR_MODE_INVALID; - cached_underline_enabled = -1; cached_on_tty = -1; cached_on_dev_null = -1; + + reset_ansi_feature_caches(); } bool on_tty(void) { @@ -1753,72 +1751,6 @@ bool terminal_is_dumb(void) { return getenv_terminal_is_dumb(); } -static const char* const color_mode_table[_COLOR_MODE_MAX] = { - [COLOR_OFF] = "off", - [COLOR_16] = "16", - [COLOR_256] = "256", - [COLOR_24BIT] = "24bit", -}; - -DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(color_mode, ColorMode, COLOR_24BIT); - -static ColorMode parse_systemd_colors(void) { - const char *e; - - e = getenv("SYSTEMD_COLORS"); - if (!e) - return _COLOR_MODE_INVALID; - - ColorMode m = color_mode_from_string(e); - if (m < 0) - return log_debug_errno(m, "Failed to parse $SYSTEMD_COLORS value '%s', ignoring: %m", e); - - return m; -} - -static ColorMode get_color_mode_impl(void) { - /* Returns the mode used to choose output colors. The possible modes are COLOR_OFF for no colors, - * COLOR_16 for only the base 16 ANSI colors, COLOR_256 for more colors, and COLOR_24BIT for - * unrestricted color output. */ - - /* First, we check $SYSTEMD_COLORS, which is the explicit way to change the mode. */ - ColorMode m = parse_systemd_colors(); - if (m >= 0) - return m; - - /* Next, check for the presence of $NO_COLOR; value is ignored. */ - if (getenv("NO_COLOR")) - return COLOR_OFF; - - /* If the above didn't work, we turn colors off unless we are on a TTY. And if we are on a TTY we - * turn it off if $TERM is set to "dumb". There's one special tweak though: if we are PID 1 then we - * do not check whether we are connected to a TTY, because we don't keep /dev/console open - * continuously due to fear of SAK, and hence things are a bit weird. */ - if (getpid_cached() == 1 ? getenv_terminal_is_dumb() : terminal_is_dumb()) - return COLOR_OFF; - - /* We failed to figure out any reason to *disable* colors. Let's see how many colors we shall use. */ - if (STRPTR_IN_SET(getenv("COLORTERM"), - "truecolor", - "24bit")) - return COLOR_24BIT; - - /* Note that the Linux console can only display 16 colors. We still enable 256 color mode - * even for PID1 output though (which typically goes to the Linux console), since the Linux - * console is able to parse the 256 color sequences and automatically map them to the closest - * color in the 16 color palette (since kernel 3.16). Doing 256 colors is nice for people who - * invoke systemd in a container or via a serial link or such, and use a true 256 color - * terminal to do so. */ - return COLOR_256; -} - -ColorMode get_color_mode(void) { - if (cached_color_mode < 0) - cached_color_mode = get_color_mode_impl(); - - return cached_color_mode; -} - bool dev_console_colors_enabled(void) { _cleanup_free_ char *s = NULL; ColorMode m; @@ -1843,21 +1775,6 @@ bool dev_console_colors_enabled(void) { return !streq_ptr(s, "dumb"); } -bool underline_enabled(void) { - - if (cached_underline_enabled < 0) { - - /* The Linux console doesn't support underlining, turn it off, but only there. */ - - if (colors_enabled()) - cached_underline_enabled = !streq_ptr(getenv("TERM"), "linux"); - else - cached_underline_enabled = false; - } - - return cached_underline_enabled; -} - int vt_restore(int fd) { static const struct vt_mode mode = { diff --git a/src/basic/terminal-util.h b/src/basic/terminal-util.h index 0faf3c8761..3f6c376770 100644 --- a/src/basic/terminal-util.h +++ b/src/basic/terminal-util.h @@ -129,26 +129,6 @@ bool on_tty(void); bool getenv_terminal_is_dumb(void); bool terminal_is_dumb(void); -/* Limits the use of ANSI colors to a subset. */ -typedef enum ColorMode { - COLOR_OFF, /* No colors, monochrome output. */ - COLOR_16, /* Only the base 16 colors. */ - COLOR_256, /* Only 256 colors. */ - COLOR_24BIT, /* For truecolor or 24bit color support, no restriction. */ - _COLOR_MODE_MAX, - _COLOR_MODE_INVALID = -EINVAL, -} ColorMode; - -const char* color_mode_to_string(ColorMode m) _const_; -ColorMode color_mode_from_string(const char *s) _pure_; - -ColorMode get_color_mode(void); -static inline bool colors_enabled(void) { - /* Returns true if colors are considered supported on our stdout. */ - return get_color_mode() != COLOR_OFF; -} - -bool underline_enabled(void); bool dev_console_colors_enabled(void); int get_ctty_devnr(pid_t pid, dev_t *ret); diff --git a/src/network/networkctl-journal.c b/src/network/networkctl-journal.c index 58e67d9ebb..ca94410aee 100644 --- a/src/network/networkctl-journal.c +++ b/src/network/networkctl-journal.c @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include "ansi-color.h" #include "journal-internal.h" #include "log.h" #include "logs-show.h" From 2782dae50f62ec2404caa8859aeefd1209d5eb8a Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Wed, 7 May 2025 11:43:44 +0200 Subject: [PATCH 5/7] compress: Drop lz4 includes from compress.h The lz4 functions are only used in test-compress.c, so let's just put the declarations and includes in there instead of having everyone including compress.h pull in the lz4 headers. --- src/basic/compress.c | 5 ++++- src/basic/compress.h | 10 ---------- src/test/test-compress.c | 5 +++++ 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/basic/compress.c b/src/basic/compress.c index 97c1e03fbf..5f71eb6853 100644 --- a/src/basic/compress.c +++ b/src/basic/compress.c @@ -9,7 +9,9 @@ #include #if HAVE_LZ4 +#include #include +#include #endif #if HAVE_XZ @@ -47,8 +49,9 @@ static DLSYM_PROTOTYPE(LZ4F_decompress) = NULL; static DLSYM_PROTOTYPE(LZ4F_freeCompressionContext) = NULL; static DLSYM_PROTOTYPE(LZ4F_freeDecompressionContext) = NULL; static DLSYM_PROTOTYPE(LZ4F_isError) = NULL; +static DLSYM_PROTOTYPE(LZ4_compress_HC) = NULL; +/* These are used in test-compress.c so we don't make them static. */ DLSYM_PROTOTYPE(LZ4_compress_default) = NULL; -DLSYM_PROTOTYPE(LZ4_compress_HC) = NULL; DLSYM_PROTOTYPE(LZ4_decompress_safe) = NULL; DLSYM_PROTOTYPE(LZ4_decompress_safe_partial) = NULL; DLSYM_PROTOTYPE(LZ4_versionNumber) = NULL; diff --git a/src/basic/compress.h b/src/basic/compress.h index c2f336e9d7..dbd6bb3857 100644 --- a/src/basic/compress.h +++ b/src/basic/compress.h @@ -6,11 +6,6 @@ #include #include -#if HAVE_LZ4 -#include -#include -#endif - #include "dlfcn-util.h" typedef enum Compression { @@ -73,11 +68,6 @@ int decompress_stream_lz4(int fdf, int fdt, uint64_t max_size); int decompress_stream_zstd(int fdf, int fdt, uint64_t max_size); #if HAVE_LZ4 -extern DLSYM_PROTOTYPE(LZ4_compress_default); -extern DLSYM_PROTOTYPE(LZ4_decompress_safe); -extern DLSYM_PROTOTYPE(LZ4_decompress_safe_partial); -extern DLSYM_PROTOTYPE(LZ4_versionNumber); - int dlopen_lz4(void); #endif diff --git a/src/test/test-compress.c b/src/test/test-compress.c index 2d8732bddb..56a468ab88 100644 --- a/src/test/test-compress.c +++ b/src/test/test-compress.c @@ -229,6 +229,11 @@ _unused_ static void test_compress_stream(const char *compression, #endif #if HAVE_LZ4 +extern DLSYM_PROTOTYPE(LZ4_compress_default); +extern DLSYM_PROTOTYPE(LZ4_decompress_safe); +extern DLSYM_PROTOTYPE(LZ4_decompress_safe_partial); +extern DLSYM_PROTOTYPE(LZ4_versionNumber); + static void test_lz4_decompress_partial(void) { char buf[20000], buf2[100]; size_t buf_size = sizeof(buf), compressed; From 56c710ce372f997e6955fc2b6ae89886a13d9404 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Tue, 6 May 2025 14:20:43 +0200 Subject: [PATCH 6/7] alloc-util: Remove unneeded stdlib.h include --- src/basic/alloc-util.h | 1 - src/basic/build-path.c | 1 + src/home/home-util.c | 2 ++ 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/basic/alloc-util.h b/src/basic/alloc-util.h index 900e798b57..7cf10ef4f3 100644 --- a/src/basic/alloc-util.h +++ b/src/basic/alloc-util.h @@ -4,7 +4,6 @@ #include #include #include -#include #include #include "assert-util.h" diff --git a/src/basic/build-path.c b/src/basic/build-path.c index 5d93bded4d..0341074ced 100644 --- a/src/basic/build-path.c +++ b/src/basic/build-path.c @@ -2,6 +2,7 @@ #include #include +#include #include #include "build-path.h" diff --git a/src/home/home-util.c b/src/home/home-util.c index 2e5343f096..b695c5a49d 100644 --- a/src/home/home-util.c +++ b/src/home/home-util.c @@ -1,5 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include + #include "alloc-util.h" #include "dns-domain.h" #include "fd-util.h" From 5f142e01d00e8d3942d6a69a0aa8aed04a027a16 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Wed, 7 May 2025 21:43:39 +0200 Subject: [PATCH 7/7] strv: Move STRV_FOREACH() to strv-fundamental.h --- src/basic/strv.h | 1 + src/basic/virt.c | 1 + src/boot/boot.c | 1 + src/fundamental/string-util-fundamental.h | 6 ------ src/fundamental/strv-fundamental.h | 10 ++++++++++ src/sysupdate/sysupdate-pattern.c | 1 + 6 files changed, 14 insertions(+), 6 deletions(-) create mode 100644 src/fundamental/strv-fundamental.h diff --git a/src/basic/strv.h b/src/basic/strv.h index 08bcaea8b9..b7895cbf1b 100644 --- a/src/basic/strv.h +++ b/src/basic/strv.h @@ -9,6 +9,7 @@ #include "alloc-util.h" #include "hashmap.h" #include "memory-util.h" +#include "strv-fundamental.h" typedef enum ExtractFlags ExtractFlags; diff --git a/src/basic/virt.c b/src/basic/virt.c index e07cee72cf..5717a1d2b4 100644 --- a/src/basic/virt.c +++ b/src/basic/virt.c @@ -24,6 +24,7 @@ #include "stat-util.h" #include "string-table.h" #include "string-util.h" +#include "strv.h" #include "virt.h" enum { diff --git a/src/boot/boot.c b/src/boot/boot.c index 99d83f5462..0e24beff6a 100644 --- a/src/boot/boot.c +++ b/src/boot/boot.c @@ -29,6 +29,7 @@ #include "secure-boot.h" #include "shim.h" #include "smbios.h" +#include "strv-fundamental.h" #include "ticks.h" #include "tpm2-pcr.h" #include "uki.h" diff --git a/src/fundamental/string-util-fundamental.h b/src/fundamental/string-util-fundamental.h index b523b6c8bf..ff74a80a18 100644 --- a/src/fundamental/string-util-fundamental.h +++ b/src/fundamental/string-util-fundamental.h @@ -120,12 +120,6 @@ static inline void *memory_startswith(const void *p, size_t sz, const sd_char *t return (uint8_t*) p + n; } -#define _STRV_FOREACH(s, l, i) \ - for (typeof(*(l)) *s, *i = (l); (s = i) && *i; i++) - -#define STRV_FOREACH(s, l) \ - _STRV_FOREACH(s, l, UNIQ_T(i, UNIQ)) - static inline bool ascii_isdigit(sd_char a) { /* A pure ASCII, locale independent version of isdigit() */ return a >= '0' && a <= '9'; diff --git a/src/fundamental/strv-fundamental.h b/src/fundamental/strv-fundamental.h new file mode 100644 index 0000000000..3abcdc4b02 --- /dev/null +++ b/src/fundamental/strv-fundamental.h @@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +#include "macro-fundamental.h" + +#define _STRV_FOREACH(s, l, i) \ + for (typeof(*(l)) *s, *i = (l); (s = i) && *i; i++) + +#define STRV_FOREACH(s, l) \ + _STRV_FOREACH(s, l, UNIQ_T(i, UNIQ)) diff --git a/src/sysupdate/sysupdate-pattern.c b/src/sysupdate/sysupdate-pattern.c index 1266bc7295..82b00d3d3b 100644 --- a/src/sysupdate/sysupdate-pattern.c +++ b/src/sysupdate/sysupdate-pattern.c @@ -8,6 +8,7 @@ #include "path-util.h" #include "stdio-util.h" #include "string-util.h" +#include "strv.h" #include "sysupdate-instance.h" #include "sysupdate-pattern.h"