summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/analyze/analyze.c3
-rw-r--r--src/basic/alloc-util.c2
-rw-r--r--src/basic/conf-files.c2
-rw-r--r--src/basic/hashmap.c2
-rw-r--r--src/basic/hexdecoct.c2
-rw-r--r--src/basic/limits-util.c157
-rw-r--r--src/basic/limits-util.h10
-rw-r--r--src/basic/memory-util.c39
-rw-r--r--src/basic/memory-util.h53
-rw-r--r--src/basic/mempool.c1
-rw-r--r--src/basic/meson.build8
-rw-r--r--src/basic/namespace-util.c172
-rw-r--r--src/basic/namespace-util.h9
-rw-r--r--src/basic/process-util.c3
-rw-r--r--src/basic/process-util.h1
-rw-r--r--src/basic/sigbus.c2
-rw-r--r--src/basic/socket-util.c2
-rw-r--r--src/basic/sort-util.c27
-rw-r--r--src/basic/sort-util.h70
-rw-r--r--src/basic/stat-util.c46
-rw-r--r--src/basic/stat-util.h2
-rw-r--r--src/basic/stdio-util.h2
-rw-r--r--src/basic/strbuf.c2
-rw-r--r--src/basic/strv.c2
-rw-r--r--src/basic/terminal-util.c1
-rw-r--r--src/basic/util.c324
-rw-r--r--src/basic/util.h117
-rw-r--r--src/busctl/busctl.c2
-rw-r--r--src/cgtop/cgtop.c2
-rw-r--r--src/core/bpf-firewall.c1
-rw-r--r--src/core/dbus-cgroup.c1
-rw-r--r--src/core/execute.c2
-rw-r--r--src/core/job.c1
-rw-r--r--src/core/load-fragment.c1
-rw-r--r--src/core/machine-id-setup.c1
-rw-r--r--src/core/main.c1
-rw-r--r--src/core/manager.c2
-rw-r--r--src/core/namespace.c3
-rw-r--r--src/coredump/coredump-vacuum.c2
-rw-r--r--src/coredump/coredump.c2
-rw-r--r--src/fuzz/fuzz-dns-packet.c1
-rw-r--r--src/initctl/initctl.c4
-rw-r--r--src/journal/catalog.c3
-rw-r--r--src/journal/fsprg.c1
-rw-r--r--src/journal/journal-authenticate.c1
-rw-r--r--src/journal/journal-file.c2
-rw-r--r--src/journal/journal-vacuum.c2
-rw-r--r--src/journal/journalctl.c1
-rw-r--r--src/journal/journald-native.c1
-rw-r--r--src/journal/mmap-cache.c2
-rw-r--r--src/journal/test-compress-benchmark.c1
-rw-r--r--src/journal/test-compress.c2
-rw-r--r--src/libsystemd-network/dhcp-option.c6
-rw-r--r--src/libsystemd-network/dhcp6-option.c2
-rw-r--r--src/libsystemd-network/lldp-neighbor.c2
-rw-r--r--src/libsystemd-network/ndisc-router.c1
-rw-r--r--src/libsystemd-network/sd-dhcp-client.c2
-rw-r--r--src/libsystemd-network/sd-lldp.c2
-rw-r--r--src/libsystemd-network/sd-ndisc.c2
-rw-r--r--src/libsystemd-network/sd-radv.c2
-rw-r--r--src/libsystemd-network/test-dhcp-option.c2
-rw-r--r--src/libsystemd-network/test-dhcp6-client.c3
-rw-r--r--src/libsystemd/sd-bus/bus-container.c1
-rw-r--r--src/libsystemd/sd-bus/bus-introspect.c2
-rw-r--r--src/libsystemd/sd-bus/bus-kernel.c2
-rw-r--r--src/libsystemd/sd-bus/bus-match.c1
-rw-r--r--src/libsystemd/sd-bus/bus-message.c2
-rw-r--r--src/libsystemd/sd-bus/bus-socket.c2
-rw-r--r--src/libsystemd/sd-bus/sd-bus.c2
-rw-r--r--src/libsystemd/sd-bus/test-bus-match.c1
-rw-r--r--src/libsystemd/sd-bus/test-bus-server.c2
-rw-r--r--src/libsystemd/sd-device/device-enumerator.c2
-rw-r--r--src/libsystemd/sd-event/sd-event.c2
-rw-r--r--src/libsystemd/sd-hwdb/hwdb-util.c1
-rw-r--r--src/libsystemd/sd-netlink/local-addresses.c1
-rw-r--r--src/libsystemd/sd-netlink/netlink-message.c2
-rw-r--r--src/libsystemd/sd-resolve/sd-resolve.c4
-rw-r--r--src/libudev/libudev-list.c2
-rw-r--r--src/locale/localectl.c2
-rw-r--r--src/login/loginctl.c2
-rw-r--r--src/login/logind-core.c1
-rw-r--r--src/login/logind-user.c1
-rw-r--r--src/machine/machine-dbus.c1
-rw-r--r--src/machine/machinectl.c2
-rw-r--r--src/mount/mount-tool.c3
-rw-r--r--src/network/netdev/wireguard.c3
-rw-r--r--src/network/networkctl.c2
-rw-r--r--src/network/networkd-address.c2
-rw-r--r--src/nspawn/nspawn-mount.c2
-rw-r--r--src/nspawn/nspawn.c1
-rw-r--r--src/nss-mymachines/nss-mymachines.c2
-rw-r--r--src/portable/portable.c1
-rw-r--r--src/resolve/resolved-bus.c3
-rw-r--r--src/resolve/resolved-dns-dnssec.c2
-rw-r--r--src/resolve/resolved-dns-packet.c1
-rw-r--r--src/resolve/resolved-dns-trust-anchor.c3
-rw-r--r--src/resolve/resolved-mdns.c1
-rw-r--r--src/shared/ask-password-api.c2
-rw-r--r--src/shared/bitmap.c1
-rw-r--r--src/shared/bootspec.c1
-rw-r--r--src/shared/bpf-program.c2
-rw-r--r--src/shared/bus-unit-util.c2
-rw-r--r--src/shared/calendarspec.c1
-rw-r--r--src/shared/cgroup-show.c1
-rw-r--r--src/shared/efivars.c2
-rw-r--r--src/shared/format-table.c3
-rw-r--r--src/shared/json.c1
-rw-r--r--src/shared/logs-show.c1
-rw-r--r--src/shared/uid-range.c2
-rw-r--r--src/shared/utmp-wtmp.c2
-rw-r--r--src/systemctl/systemctl.c5
-rw-r--r--src/test/test-alloc-util.c2
-rw-r--r--src/test/test-fd-util.c2
-rw-r--r--src/test/test-prioq.c2
-rw-r--r--src/test/test-seccomp.c2
-rw-r--r--src/test/test-sigbus.c4
-rw-r--r--src/test/test-siphash24.c2
-rw-r--r--src/test/test-sleep.c2
-rw-r--r--src/test/test-stat-util.c1
-rw-r--r--src/test/test-unaligned.c2
-rw-r--r--src/test/test-unit-file.c2
-rw-r--r--src/test/test-util.c3
-rw-r--r--src/timedate/timedated.c2
-rw-r--r--src/tmpfiles/tmpfiles.c2
-rw-r--r--src/tty-ask-password-agent/tty-ask-password-agent.c2
-rw-r--r--src/udev/ata_id/ata_id.c2
-rw-r--r--src/udev/cdrom_id/cdrom_id.c2
-rw-r--r--src/udev/net/ethtool-util.c2
-rw-r--r--src/udev/net/link-config.c2
-rw-r--r--src/udev/scsi_id/scsi_serial.c2
-rw-r--r--src/udev/udevd.c1
131 files changed, 685 insertions, 573 deletions
diff --git a/src/analyze/analyze.c b/src/analyze/analyze.c
index 77adbed83d..f838daada8 100644
--- a/src/analyze/analyze.c
+++ b/src/analyze/analyze.c
@@ -36,11 +36,12 @@
#if HAVE_SECCOMP
# include "seccomp-util.h"
#endif
+#include "sort-util.h"
#include "special.h"
#include "strv.h"
#include "strxcpyx.h"
-#include "time-util.h"
#include "terminal-util.h"
+#include "time-util.h"
#include "unit-name.h"
#include "util.h"
#include "verbs.h"
diff --git a/src/basic/alloc-util.c b/src/basic/alloc-util.c
index ab7a42c4e2..b28fb95ccf 100644
--- a/src/basic/alloc-util.c
+++ b/src/basic/alloc-util.c
@@ -5,7 +5,7 @@
#include "alloc-util.h"
#include "macro.h"
-#include "util.h"
+#include "memory-util.h"
void* memdup(const void *p, size_t l) {
void *ret;
diff --git a/src/basic/conf-files.c b/src/basic/conf-files.c
index b70c6e50a8..d010fbb266 100644
--- a/src/basic/conf-files.c
+++ b/src/basic/conf-files.c
@@ -17,11 +17,11 @@
#include "missing.h"
#include "path-util.h"
#include "set.h"
+#include "sort-util.h"
#include "stat-util.h"
#include "string-util.h"
#include "strv.h"
#include "terminal-util.h"
-#include "util.h"
static int files_add(
Hashmap *h,
diff --git a/src/basic/hashmap.c b/src/basic/hashmap.c
index 24bdc33cec..c7bd7323a1 100644
--- a/src/basic/hashmap.c
+++ b/src/basic/hashmap.c
@@ -9,6 +9,7 @@
#include "fileio.h"
#include "hashmap.h"
#include "macro.h"
+#include "memory-util.h"
#include "mempool.h"
#include "process-util.h"
#include "random-util.h"
@@ -16,7 +17,6 @@
#include "siphash24.h"
#include "string-util.h"
#include "strv.h"
-#include "util.h"
#if ENABLE_DEBUG_HASHMAP
#include <pthread.h>
diff --git a/src/basic/hexdecoct.c b/src/basic/hexdecoct.c
index 8aedc0d583..a5660a0ff3 100644
--- a/src/basic/hexdecoct.c
+++ b/src/basic/hexdecoct.c
@@ -8,8 +8,8 @@
#include "alloc-util.h"
#include "hexdecoct.h"
#include "macro.h"
+#include "memory-util.h"
#include "string-util.h"
-#include "util.h"
char octchar(int x) {
return '0' + (x & 7);
diff --git a/src/basic/limits-util.c b/src/basic/limits-util.c
new file mode 100644
index 0000000000..fbf52e55f1
--- /dev/null
+++ b/src/basic/limits-util.c
@@ -0,0 +1,157 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
+
+#include "alloc-util.h"
+#include "cgroup-util.h"
+#include "limits-util.h"
+#include "memory-util.h"
+#include "parse-util.h"
+#include "process-util.h"
+#include "procfs-util.h"
+#include "string-util.h"
+
+uint64_t physical_memory(void) {
+ _cleanup_free_ char *root = NULL, *value = NULL;
+ uint64_t mem, lim;
+ size_t ps;
+ long sc;
+ int r;
+
+ /* We return this as uint64_t in case we are running as 32bit process on a 64bit kernel with huge amounts of
+ * memory.
+ *
+ * In order to support containers nicely that have a configured memory limit we'll take the minimum of the
+ * physically reported amount of memory and the limit configured for the root cgroup, if there is any. */
+
+ sc = sysconf(_SC_PHYS_PAGES);
+ assert(sc > 0);
+
+ ps = page_size();
+ mem = (uint64_t) sc * (uint64_t) ps;
+
+ r = cg_get_root_path(&root);
+ if (r < 0) {
+ log_debug_errno(r, "Failed to determine root cgroup, ignoring cgroup memory limit: %m");
+ return mem;
+ }
+
+ r = cg_all_unified();
+ if (r < 0) {
+ log_debug_errno(r, "Failed to determine root unified mode, ignoring cgroup memory limit: %m");
+ return mem;
+ }
+ if (r > 0) {
+ r = cg_get_attribute("memory", root, "memory.max", &value);
+ if (r < 0) {
+ log_debug_errno(r, "Failed to read memory.max cgroup attribute, ignoring cgroup memory limit: %m");
+ return mem;
+ }
+
+ if (streq(value, "max"))
+ return mem;
+ } else {
+ r = cg_get_attribute("memory", root, "memory.limit_in_bytes", &value);
+ if (r < 0) {
+ log_debug_errno(r, "Failed to read memory.limit_in_bytes cgroup attribute, ignoring cgroup memory limit: %m");
+ return mem;
+ }
+ }
+
+ r = safe_atou64(value, &lim);
+ if (r < 0) {
+ log_debug_errno(r, "Failed to parse cgroup memory limit '%s', ignoring: %m", value);
+ return mem;
+ }
+ if (lim == UINT64_MAX)
+ return mem;
+
+ /* Make sure the limit is a multiple of our own page size */
+ lim /= ps;
+ lim *= ps;
+
+ return MIN(mem, lim);
+}
+
+uint64_t physical_memory_scale(uint64_t v, uint64_t max) {
+ uint64_t p, m, ps, r;
+
+ assert(max > 0);
+
+ /* Returns the physical memory size, multiplied by v divided by max. Returns UINT64_MAX on overflow. On success
+ * the result is a multiple of the page size (rounds down). */
+
+ ps = page_size();
+ assert(ps > 0);
+
+ p = physical_memory() / ps;
+ assert(p > 0);
+
+ m = p * v;
+ if (m / p != v)
+ return UINT64_MAX;
+
+ m /= max;
+
+ r = m * ps;
+ if (r / ps != m)
+ return UINT64_MAX;
+
+ return r;
+}
+
+uint64_t system_tasks_max(void) {
+
+ uint64_t a = TASKS_MAX, b = TASKS_MAX;
+ _cleanup_free_ char *root = NULL;
+ int r;
+
+ /* Determine the maximum number of tasks that may run on this system. We check three sources to determine this
+ * limit:
+ *
+ * a) the maximum tasks value the kernel allows on this architecture
+ * b) the cgroups pids_max attribute for the system
+ * c) the kernel's configured maximum PID value
+ *
+ * And then pick the smallest of the three */
+
+ r = procfs_tasks_get_limit(&a);
+ if (r < 0)
+ log_debug_errno(r, "Failed to read maximum number of tasks from /proc, ignoring: %m");
+
+ r = cg_get_root_path(&root);
+ if (r < 0)
+ log_debug_errno(r, "Failed to determine cgroup root path, ignoring: %m");
+ else {
+ _cleanup_free_ char *value = NULL;
+
+ r = cg_get_attribute("pids", root, "pids.max", &value);
+ if (r < 0)
+ log_debug_errno(r, "Failed to read pids.max attribute of cgroup root, ignoring: %m");
+ else if (!streq(value, "max")) {
+ r = safe_atou64(value, &b);
+ if (r < 0)
+ log_debug_errno(r, "Failed to parse pids.max attribute of cgroup root, ignoring: %m");
+ }
+ }
+
+ return MIN3(TASKS_MAX,
+ a <= 0 ? TASKS_MAX : a,
+ b <= 0 ? TASKS_MAX : b);
+}
+
+uint64_t system_tasks_max_scale(uint64_t v, uint64_t max) {
+ uint64_t t, m;
+
+ assert(max > 0);
+
+ /* Multiply the system's task value by the fraction v/max. Hence, if max==100 this calculates percentages
+ * relative to the system's maximum number of tasks. Returns UINT64_MAX on overflow. */
+
+ t = system_tasks_max();
+ assert(t > 0);
+
+ m = t * v;
+ if (m / t != v) /* overflow? */
+ return UINT64_MAX;
+
+ return m / max;
+}
diff --git a/src/basic/limits-util.h b/src/basic/limits-util.h
new file mode 100644
index 0000000000..77895cbad4
--- /dev/null
+++ b/src/basic/limits-util.h
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
+#pragma once
+
+#include <inttypes.h>
+
+uint64_t physical_memory(void);
+uint64_t physical_memory_scale(uint64_t v, uint64_t max);
+
+uint64_t system_tasks_max(void);
+uint64_t system_tasks_max_scale(uint64_t v, uint64_t max);
diff --git a/src/basic/memory-util.c b/src/basic/memory-util.c
new file mode 100644
index 0000000000..3b078af20b
--- /dev/null
+++ b/src/basic/memory-util.c
@@ -0,0 +1,39 @@
+#include <unistd.h>
+
+#include "memory-util.h"
+
+size_t page_size(void) {
+ static thread_local size_t pgsz = 0;
+ long r;
+
+ if (_likely_(pgsz > 0))
+ return pgsz;
+
+ r = sysconf(_SC_PAGESIZE);
+ assert(r > 0);
+
+ pgsz = (size_t) r;
+ return pgsz;
+}
+
+bool memeqzero(const void *data, size_t length) {
+ /* Does the buffer consist entirely of NULs?
+ * Copied from https://github.com/systemd/casync/, copied in turn from
+ * https://github.com/rustyrussell/ccan/blob/master/ccan/mem/mem.c#L92,
+ * which is licensed CC-0.
+ */
+
+ const uint8_t *p = data;
+ size_t i;
+
+ /* Check first 16 bytes manually */
+ for (i = 0; i < 16; i++, length--) {
+ if (length == 0)
+ return true;
+ if (p[i])
+ return false;
+ }
+
+ /* Now we know first 16 bytes are NUL, memcmp with self. */
+ return memcmp(data, p + i, length) == 0;
+}
diff --git a/src/basic/memory-util.h b/src/basic/memory-util.h
new file mode 100644
index 0000000000..2d74b14a20
--- /dev/null
+++ b/src/basic/memory-util.h
@@ -0,0 +1,53 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
+#pragma once
+
+#include <inttypes.h>
+#include <stdbool.h>
+#include <string.h>
+#include <sys/types.h>
+
+#include "macro.h"
+
+size_t page_size(void) _pure_;
+#define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
+
+/* Normal memcpy requires src to be nonnull. We do nothing if n is 0. */
+static inline void memcpy_safe(void *dst, const void *src, size_t n) {
+ if (n == 0)
+ return;
+ assert(src);
+ memcpy(dst, src, n);
+}
+
+/* Normal memcmp requires s1 and s2 to be nonnull. We do nothing if n is 0. */
+static inline int memcmp_safe(const void *s1, const void *s2, size_t n) {
+ if (n == 0)
+ return 0;
+ assert(s1);
+ assert(s2);
+ return memcmp(s1, s2, n);
+}
+
+/* Compare s1 (length n1) with s2 (length n2) in lexicographic order. */
+static inline int memcmp_nn(const void *s1, size_t n1, const void *s2, size_t n2) {
+ return memcmp_safe(s1, s2, MIN(n1, n2))
+ ?: CMP(n1, n2);
+}
+
+#define memzero(x,l) \
+ ({ \
+ size_t _l_ = (l); \
+ void *_x_ = (x); \
+ _l_ == 0 ? _x_ : memset(_x_, 0, _l_); \
+ })
+
+#define zero(x) (memzero(&(x), sizeof(x)))
+
+bool memeqzero(const void *data, size_t length);
+
+#define eqzero(x) memeqzero(x, sizeof(x))
+
+static inline void *mempset(void *s, int c, size_t n) {
+ memset(s, c, n);
+ return (uint8_t*)s + n;
+}
diff --git a/src/basic/mempool.c b/src/basic/mempool.c
index 159c963377..22df42105b 100644
--- a/src/basic/mempool.c
+++ b/src/basic/mempool.c
@@ -5,6 +5,7 @@
#include "env-util.h"
#include "macro.h"
+#include "memory-util.h"
#include "mempool.h"
#include "process-util.h"
#include "util.h"
diff --git a/src/basic/meson.build b/src/basic/meson.build
index c62e4a3b59..30466ce946 100644
--- a/src/basic/meson.build
+++ b/src/basic/meson.build
@@ -79,6 +79,8 @@ basic_sources = files('''
khash.h
label.c
label.h
+ limits-util.c
+ limits-util.h
list.h
locale-util.c
locale-util.h
@@ -89,6 +91,8 @@ basic_sources = files('''
macro.h
memfd-util.c
memfd-util.h
+ memory-util.c
+ memory-util.h
mempool.c
mempool.h
missing.h
@@ -127,6 +131,8 @@ basic_sources = files('''
mkdir.h
mountpoint-util.c
mountpoint-util.h
+ namespace-util.c
+ namespace-util.h
nss-util.h
ordered-set.c
ordered-set.h
@@ -168,6 +174,8 @@ basic_sources = files('''
socket-label.c
socket-util.c
socket-util.h
+ sort-util.c
+ sort-util.h
sparse-endian.h
special.h
stat-util.c
diff --git a/src/basic/namespace-util.c b/src/basic/namespace-util.c
new file mode 100644
index 0000000000..67bdaa19d5
--- /dev/null
+++ b/src/basic/namespace-util.c
@@ -0,0 +1,172 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
+
+#include <fcntl.h>
+#include <linux/magic.h>
+
+#include "fd-util.h"
+#include "missing.h"
+#include "namespace-util.h"
+#include "process-util.h"
+#include "stat-util.h"
+#include "user-util.h"
+
+int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *userns_fd, int *root_fd) {
+ _cleanup_close_ int pidnsfd = -1, mntnsfd = -1, netnsfd = -1, usernsfd = -1;
+ int rfd = -1;
+
+ assert(pid >= 0);
+
+ if (mntns_fd) {
+ const char *mntns;
+
+ mntns = procfs_file_alloca(pid, "ns/mnt");
+ mntnsfd = open(mntns, O_RDONLY|O_NOCTTY|O_CLOEXEC);
+ if (mntnsfd < 0)
+ return -errno;
+ }
+
+ if (pidns_fd) {
+ const char *pidns;
+
+ pidns = procfs_file_alloca(pid, "ns/pid");
+ pidnsfd = open(pidns, O_RDONLY|O_NOCTTY|O_CLOEXEC);
+ if (pidnsfd < 0)
+ return -errno;
+ }
+
+ if (netns_fd) {
+ const char *netns;
+
+ netns = procfs_file_alloca(pid, "ns/net");
+ netnsfd = open(netns, O_RDONLY|O_NOCTTY|O_CLOEXEC);
+ if (netnsfd < 0)
+ return -errno;
+ }
+
+ if (userns_fd) {
+ const char *userns;
+
+ userns = procfs_file_alloca(pid, "ns/user");
+ usernsfd = open(userns, O_RDONLY|O_NOCTTY|O_CLOEXEC);
+ if (usernsfd < 0 && errno != ENOENT)
+ return -errno;
+ }
+
+ if (root_fd) {
+ const char *root;
+
+ root = procfs_file_alloca(pid, "root");
+ rfd = open(root, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY);
+ if (rfd < 0)
+ return -errno;
+ }
+
+ if (pidns_fd)
+ *pidns_fd = pidnsfd;
+
+ if (mntns_fd)
+ *mntns_fd = mntnsfd;
+
+ if (netns_fd)
+ *netns_fd = netnsfd;
+
+ if (userns_fd)
+ *userns_fd = usernsfd;
+
+ if (root_fd)
+ *root_fd = rfd;
+
+ pidnsfd = mntnsfd = netnsfd = usernsfd = -1;
+
+ return 0;
+}
+
+int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int userns_fd, int root_fd) {
+ if (userns_fd >= 0) {
+ /* Can't setns to your own userns, since then you could
+ * escalate from non-root to root in your own namespace, so
+ * check if namespaces equal before attempting to enter. */
+ _cleanup_free_ char *userns_fd_path = NULL;
+ int r;
+ if (asprintf(&userns_fd_path, "/proc/self/fd/%d", userns_fd) < 0)
+ return -ENOMEM;
+
+ r = files_same(userns_fd_path, "/proc/self/ns/user", 0);
+ if (r < 0)
+ return r;
+ if (r)
+ userns_fd = -1;
+ }
+
+ if (pidns_fd >= 0)
+ if (setns(pidns_fd, CLONE_NEWPID) < 0)
+ return -errno;
+
+ if (mntns_fd >= 0)
+ if (setns(mntns_fd, CLONE_NEWNS) < 0)
+ return -errno;
+
+ if (netns_fd >= 0)
+ if (setns(netns_fd, CLONE_NEWNET) < 0)
+ return -errno;
+
+ if (userns_fd >= 0)
+ if (setns(userns_fd, CLONE_NEWUSER) < 0)
+ return -errno;
+
+ if (root_fd >= 0) {
+ if (fchdir(root_fd) < 0)
+ return -errno;
+
+ if (chroot(".") < 0)
+ return -errno;
+ }
+
+ return reset_uid_gid();
+}
+
+int fd_is_network_ns(int fd) {
+ struct statfs s;
+ int r;
+
+ /* Checks whether the specified file descriptor refers to a network namespace. On old kernels there's no nice
+ * way to detect that, hence on those we'll return a recognizable error (EUCLEAN), so that callers can handle
+ * this somewhat nicely.
+ *
+ * This function returns > 0 if the fd definitely refers to a network namespace, 0 if it definitely does not
+ * refer to a network namespace, -EUCLEAN if we can't determine, and other negative error codes on error. */
+
+ if (fstatfs(fd, &s) < 0)
+ return -errno;
+
+ if (!is_fs_type(&s, NSFS_MAGIC)) {
+ /* On really old kernels, there was no "nsfs", and network namespace sockets belonged to procfs
+ * instead. Handle that in a somewhat smart way. */
+
+ if (is_fs_type(&s, PROC_SUPER_MAGIC)) {
+ struct statfs t;
+
+ /* OK, so it is procfs. Let's see if our own network namespace is procfs, too. If so, then the
+ * passed fd might refer to a network namespace, but we can't know for sure. In that case,
+ * return a recognizable error. */
+
+ if (statfs("/proc/self/ns/net", &t) < 0)
+ return -errno;
+
+ if (s.f_type == t.f_type)
+ return -EUCLEAN; /* It's possible, we simply don't know */
+ }
+
+ return 0; /* No! */
+ }
+
+ r = ioctl(fd, NS_GET_NSTYPE);
+ if (r < 0) {
+ if (errno == ENOTTY) /* Old kernels didn't know this ioctl, let's also return a recognizable error in that case */
+ return -EUCLEAN;
+
+ return -errno;
+ }
+
+ return r == CLONE_NEWNET;
+}
diff --git a/src/basic/namespace-util.h b/src/basic/namespace-util.h
new file mode 100644
index 0000000000..8c17ce91b2
--- /dev/null
+++ b/src/basic/namespace-util.h
@@ -0,0 +1,9 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
+#pragma once
+
+#include <sys/types.h>
+
+int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *userns_fd, int *root_fd);
+int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int userns_fd, int root_fd);
+
+int fd_is_network_ns(int fd);
diff --git a/src/basic/process-util.c b/src/basic/process-util.c
index 78ce43b944..86d5c3adb5 100644
--- a/src/basic/process-util.c
+++ b/src/basic/process-util.c
@@ -32,7 +32,9 @@
#include "ioprio.h"
#include "log.h"
#include "macro.h"
+#include "memory-util.h"
#include "missing.h"
+#include "namespace-util.h"
#include "process-util.h"
#include "raw-clone.h"
#include "rlimit-util.h"
@@ -42,7 +44,6 @@
#include "string-util.h"
#include "terminal-util.h"
#include "user-util.h"
-#include "util.h"
int get_process_state(pid_t pid) {
const char *p;
diff --git a/src/basic/process-util.h b/src/basic/process-util.h
index c85ea30ecc..9950723996 100644
--- a/src/basic/process-util.h
+++ b/src/basic/process-util.h
@@ -12,6 +12,7 @@
#include <sys/resource.h>
#include <sys/types.h>
+#include "alloc-util.h"
#include "format-util.h"
#include "ioprio.h"
#include "macro.h"
diff --git a/src/basic/sigbus.c b/src/basic/sigbus.c
index d5254eab9d..a065a45a8c 100644
--- a/src/basic/sigbus.c
+++ b/src/basic/sigbus.c
@@ -6,8 +6,8 @@
#include <sys/mman.h>
#include "macro.h"
+#include "memory-util.h"
#include "sigbus.h"
-#include "util.h"
#define SIGBUS_QUEUE_MAX 64
diff --git a/src/basic/socket-util.c b/src/basic/socket-util.c
index a8c9115dc2..3c156fd659 100644
--- a/src/basic/socket-util.c
+++ b/src/basic/socket-util.c
@@ -21,6 +21,7 @@
#include "format-util.h"
#include "log.h"
#include "macro.h"
+#include "memory-util.h"
#include "missing.h"
#include "parse-util.h"
#include "path-util.h"
@@ -31,7 +32,6 @@
#include "strv.h"
#include "user-util.h"
#include "utf8.h"
-#include "util.h"
#if ENABLE_IDN
# define IDN_FLAGS NI_IDN
diff --git a/src/basic/sort-util.c b/src/basic/sort-util.c
new file mode 100644
index 0000000000..5cf0d1d49b
--- /dev/null
+++ b/src/basic/sort-util.c
@@ -0,0 +1,27 @@
+#include "sort-util.h"
+#include "alloc-util.h"
+
+/* hey glibc, APIs with callbacks without a user pointer are so useless */
+void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
+ __compar_d_fn_t compar, void *arg) {
+ size_t l, u, idx;
+ const void *p;
+ int comparison;
+
+ assert(!size_multiply_overflow(nmemb, size));
+
+ l = 0;
+ u = nmemb;
+ while (l < u) {
+ idx = (l + u) / 2;
+ p = (const uint8_t*) base + idx * size;
+ comparison = compar(key, p, arg);
+ if (comparison < 0)
+ u = idx;
+ else if (comparison > 0)
+ l = idx + 1;
+ else
+ return (void *)p;
+ }
+ return NULL;
+}
diff --git a/src/basic/sort-util.h b/src/basic/sort-util.h
new file mode 100644
index 0000000000..e029f8646e
--- /dev/null
+++ b/src/basic/sort-util.h
@@ -0,0 +1,70 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
+#pragma once
+
+#include <stdlib.h>
+
+#include "macro.h"
+
+void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
+ __compar_d_fn_t compar, void *arg);
+
+#define typesafe_bsearch_r(k, b, n, func, userdata) \
+ ({ \
+ const typeof(b[0]) *_k = k; \
+ int (*_func_)(const typeof(b[0])*, const typeof(b[0])*, typeof(userdata)) = func; \
+ xbsearch_r((const void*) _k, (b), (n), sizeof((b)[0]), (__compar_d_fn_t) _func_, userdata); \
+ })
+
+/**
+ * Normal bsearch requires base to be nonnull. Here were require
+ * that only if nmemb > 0.
+ */
+static inline void* bsearch_safe(const void *key, const void *base,
+ size_t nmemb, size_t size, __compar_fn_t compar) {
+ if (nmemb <= 0)
+ return NULL;
+
+ assert(base);
+ return bsearch(key, base, nmemb, size, compar);
+}
+
+#define typesafe_bsearch(k, b, n, func) \
+ ({ \
+ const typeof(b[0]) *_k = k; \
+ int (*_func_)(const typeof(b[0])*, const typeof(b[0])*) = func; \
+ bsearch_safe((const void*) _k, (b), (n), sizeof((b)[0]), (__compar_fn_t) _func_); \
+ })
+
+/**
+ * Normal qsort requires base to be nonnull. Here were require
+ * that only if nmemb > 0.
+ */
+static inline void qsort_safe(void *base, size_t nmemb, size_t size, __compar_fn_t compar) {
+ if (nmemb <= 1)
+ return;
+
+ assert(base);
+ qsort(base, nmemb, size, compar);
+}
+
+/* A wrapper around the above, but that adds typesafety: the element size is automatically derived from the type and so
+ * is the prototype for the comparison function */
+#define typesafe_qsort(p, n, func) \
+ ({ \
+ int (*_func_)(const typeof(p[0])*, const typeof(p[0])*) = func; \
+ qsort_safe((p), (n), sizeof((p)[0]), (__compar_fn_t) _func_); \
+ })
+
+static inline void qsort_r_safe(void *base, size_t nmemb, size_t size, __compar_d_fn_t compar, void *userdata) {
+ if (nmemb <= 1)
+ return;
+
+ assert(base);
+ qsort_r(base, nmemb, size, compar, userdata);
+}
+
+#define typesafe_qsort_r(p, n, func, userdata) \
+ ({ \
+ int (*_func_)(const typeof(p[0])*, const typeof(p[0])*, typeof(userdata)) = func; \
+ qsort_r_safe((p), (n), sizeof((p)[0]), (__compar_d_fn_t) _func_, userdata); \
+ })
diff --git a/src/basic/stat-util.c b/src/basic/stat-util.c
index ea2bbc368b..2cd722c106 100644
--- a/src/basic/stat-util.c
+++ b/src/basic/stat-util.c
@@ -223,52 +223,6 @@ int fd_is_network_fs(int fd) {
return is_network_fs(&s);
}
-int fd_is_network_ns(int fd) {
- struct statfs s;
- int r;
-
- /* Checks whether the specified file descriptor refers to a network namespace. On old kernels there's no nice
- * way to detect that, hence on those we'll return a recognizable error (EUCLEAN), so that callers can handle
- * this somewhat nicely.
- *
- * This function returns > 0 if the fd definitely refers to a network namespace, 0 if it definitely does not
- * refer to a network namespace, -EUCLEAN if we can't determine, and other negative error codes on error. */
-
- if (fstatfs(fd, &s) < 0)
- return -errno;
-
- if (!is_fs_type(&s, NSFS_MAGIC)) {
- /* On really old kernels, there was no "nsfs", and network namespace sockets belonged to procfs
- * instead. Handle that in a somewhat smart way. */
-
- if (is_fs_type(&s, PROC_SUPER_MAGIC)) {
- struct statfs t;
-
- /* OK, so it is procfs. Let's see if our own network namespace is procfs, too. If so, then the
- * passed fd might refer to a network namespace, but we can't know for sure. In that case,
- * return a recognizable error. */
-
- if (statfs("/proc/self/ns/net", &t) < 0)
- return -errno;
-
- if (s.f_type == t.f_type)
- return -EUCLEAN; /* It's possible, we simply don't know */
- }
-
- return 0; /* No! */
- }
-
- r = ioctl(fd, NS_GET_NSTYPE);
- if (r < 0) {
- if (errno == ENOTTY) /* Old kernels didn't know this ioctl, let's also return a recognizable error in that case */
- return -EUCLEAN;
-
- return -errno;
- }
-
- return r == CLONE_NEWNET;
-}
-
int path_is_temporary_fs(const char *path) {
_cleanup_close_ int fd = -1;
diff --git a/src/basic/stat-util.h b/src/basic/stat-util.h
index 74fb7251b3..5aca17c04a 100644
--- a/src/basic/stat-util.h
+++ b/src/basic/stat-util.h
@@ -50,8 +50,6 @@ bool is_network_fs(const struct statfs *s) _pure_;
int fd_is_temporary_fs(int fd);
int fd_is_network_fs(int fd);
-int fd_is_network_ns(int fd);
-
int path_is_temporary_fs(const char *path);
/* Because statfs.t_type can be int on some architectures, we have to cast
diff --git a/src/basic/stdio-util.h b/src/basic/stdio-util.h
index dc67b6e761..c3b9448d4f 100644
--- a/src/basic/stdio-util.h
+++ b/src/basic/stdio-util.h
@@ -7,7 +7,7 @@
#include <sys/types.h>
#include "macro.h"
-#include "util.h"
+#include "memory-util.h"
#define snprintf_ok(buf, len, fmt, ...) \
((size_t) snprintf(buf, len, fmt, __VA_ARGS__) < (len))
diff --git a/src/basic/strbuf.c b/src/basic/strbuf.c
index 81f4f21ade..769b22aba0 100644
--- a/src/basic/strbuf.c
+++ b/src/basic/strbuf.c
@@ -5,8 +5,8 @@
#include <string.h>
#include "alloc-util.h"
+#include "sort-util.h"
#include "strbuf.h"
-#include "util.h"
/*
* Strbuf stores given strings in a single continuous allocated memory
diff --git a/src/basic/strv.c b/src/basic/strv.c
index 3a62f25ded..3700ce5b30 100644
--- a/src/basic/strv.c
+++ b/src/basic/strv.c
@@ -11,9 +11,9 @@
#include "escape.h"
#include "extract-word.h"
#include "fileio.h"
+#include "sort-util.h"
#include "string-util.h"
#include "strv.h"
-#include "util.h"
char *strv_find(char **l, const char *name) {
char **i;
diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c
index 0f38120729..4702e91732 100644
--- a/src/basic/terminal-util.c
+++ b/src/basic/terminal-util.c
@@ -32,6 +32,7 @@
#include "io-util.h"
#include "log.h"
#include "macro.h"
+#include "namespace-util.h"
#include "parse-util.h"
#include "path-util.h"
#include "proc-cmdline.h"
diff --git a/src/basic/util.c b/src/basic/util.c
index e577c93e60..ce3e321925 100644
--- a/src/basic/util.c
+++ b/src/basic/util.c
@@ -19,7 +19,6 @@
#include "alloc-util.h"
#include "btrfs-util.h"
#include "build.h"
-#include "cgroup-util.h"
#include "def.h"
#include "device-nodes.h"
#include "dirent-util.h"
@@ -52,20 +51,6 @@ int saved_argc = 0;
char **saved_argv = NULL;
static int saved_in_initrd = -1;
-size_t page_size(void) {
- static thread_local size_t pgsz = 0;
- long r;
-
- if (_likely_(pgsz > 0))
- return pgsz;
-
- r = sysconf(_SC_PAGESIZE);
- assert(r > 0);
-
- pgsz = (size_t) r;
- return pgsz;
-}
-
bool plymouth_running(void) {
return access("/run/plymouth/pid", F_OK) >= 0;
}
@@ -141,53 +126,6 @@ void in_initrd_force(bool value) {
saved_in_initrd = value;
}
-/* hey glibc, APIs with callbacks without a user pointer are so useless */
-void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
- __compar_d_fn_t compar, void *arg) {
- size_t l, u, idx;
- const void *p;
- int comparison;
-
- assert(!size_multiply_overflow(nmemb, size));
-
- l = 0;
- u = nmemb;
- while (l < u) {
- idx = (l + u) / 2;
- p = (const uint8_t*) base + idx * size;
- comparison = compar(key, p, arg);
- if (comparison < 0)
- u = idx;
- else if (comparison > 0)
- l = idx + 1;
- else
- return (void *)p;
- }
- return NULL;
-}
-
-bool memeqzero(const void *data, size_t length) {
- /* Does the buffer consist entirely of NULs?
- * Copied from https://github.com/systemd/casync/, copied in turn from
- * https://github.com/rustyrussell/ccan/blob/master/ccan/mem/mem.c#L92,
- * which is licensed CC-0.
- */
-
- const uint8_t *p = data;
- size_t i;
-
- /* Check first 16 bytes manually */
- for (i = 0; i < 16; i++, length--) {
- if (length == 0)
- return true;
- if (p[i])
- return false;
- }
-
- /* Now we know first 16 bytes are NUL, memcmp with self. */
- return memcmp(data, p + i, length) == 0;
-}
-
int on_ac_power(void) {
bool found_offline = false, found_online = false;
_cleanup_closedir_ DIR *d = NULL;
@@ -294,268 +232,6 @@ int container_get_leader(const char *machine, pid_t *pid) {
return 0;
}
-int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *userns_fd, int *root_fd) {
- _cleanup_close_ int pidnsfd = -1, mntnsfd = -1, netnsfd = -1, usernsfd = -1;
- int rfd = -1;
-
- assert(pid >= 0);
-
- if (mntns_fd) {
- const char *mntns;
-
- mntns = procfs_file_alloca(pid, "ns/mnt");
- mntnsfd = open(mntns, O_RDONLY|O_NOCTTY|O_CLOEXEC);
- if (mntnsfd < 0)
- return -errno;
- }
-
- if (pidns_fd) {
- const char *pidns;
-
- pidns = procfs_file_alloca(pid, "ns/pid");
- pidnsfd = open(pidns, O_RDONLY|O_NOCTTY|O_CLOEXEC);
- if (pidnsfd < 0)
- return -errno;
- }
-
- if (netns_fd) {
- const char *netns;
-
- netns = procfs_file_alloca(pid, "ns/net");
- netnsfd = open(netns, O_RDONLY|O_NOCTTY|O_CLOEXEC);
- if (netnsfd < 0)
- return -errno;
- }
-
- if (userns_fd) {
- const char *userns;
-
- userns = procfs_file_alloca(pid, "ns/user");
- usernsfd = open(userns, O_RDONLY|O_NOCTTY|O_CLOEXEC);
- if (usernsfd < 0 && errno != ENOENT)
- return -errno;
- }
-
- if (root_fd) {
- const char *root;
-
- root = procfs_file_alloca(pid, "root");
- rfd = open(root, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY);
- if (rfd < 0)
- return -errno;
- }
-
- if (pidns_fd)
- *pidns_fd = pidnsfd;
-
- if (mntns_fd)
- *mntns_fd = mntnsfd;
-
- if (netns_fd)
- *netns_fd = netnsfd;
-
- if (userns_fd)
- *userns_fd = usernsfd;
-
- if (root_fd)
- *root_fd = rfd;
-
- pidnsfd = mntnsfd = netnsfd = usernsfd = -1;
-
- return 0;
-}
-
-int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int userns_fd, int root_fd) {
- if (userns_fd >= 0) {
- /* Can't setns to your own userns, since then you could
- * escalate from non-root to root in your own namespace, so
- * check if namespaces equal before attempting to enter. */
- _cleanup_free_ char *userns_fd_path = NULL;
- int r;
- if (asprintf(&userns_fd_path, "/proc/self/fd/%d", userns_fd) < 0)
- return -ENOMEM;
-
- r = files_same(userns_fd_path, "/proc/self/ns/user", 0);
- if (r < 0)
- return r;
- if (r)
- userns_fd = -1;
- }
-
- if (pidns_fd >= 0)
- if (setns(pidns_fd, CLONE_NEWPID) < 0)
- return -errno;
-
- if (mntns_fd >= 0)
- if (setns(mntns_fd, CLONE_NEWNS) < 0)
- return -errno;
-
- if (netns_fd >= 0)
- if (setns(netns_fd, CLONE_NEWNET) < 0)
- return -errno;
-
- if (userns_fd >= 0)
- if (setns(userns_fd, CLONE_NEWUSER) < 0)
- return -errno;
-
- if (root_fd >= 0) {
- if (fchdir(root_fd) < 0)
- return -errno;
-
- if (chroot(".") < 0)
- return -errno;
- }
-
- return reset_uid_gid();
-}
-
-uint64_t physical_memory(void) {
- _cleanup_free_ char *root = NULL, *value = NULL;
- uint64_t mem, lim;
- size_t ps;
- long sc;
- int r;
-
- /* We return this as uint64_t in case we are running as 32bit process on a 64bit kernel with huge amounts of
- * memory.
- *
- * In order to support containers nicely that have a configured memory limit we'll take the minimum of the
- * physically reported amount of memory and the limit configured for the root cgroup, if there is any. */
-
- sc = sysconf(_SC_PHYS_PAGES);
- assert(sc > 0);
-
- ps = page_size();
- mem = (uint64_t) sc * (uint64_t) ps;
-
- r = cg_get_root_path(&root);
- if (r < 0) {
- log_debug_errno(r, "Failed to determine root cgroup, ignoring cgroup memory limit: %m");
- return mem;
- }
-
- r = cg_all_unified();
- if (r < 0) {
- log_debug_errno(r, "Failed to determine root unified mode, ignoring cgroup memory limit: %m");
- return mem;
- }
- if (r > 0) {
- r = cg_get_attribute("memory", root, "memory.max", &value);
- if (r < 0) {
- log_debug_errno(r, "Failed to read memory.max cgroup attribute, ignoring cgroup memory limit: %m");
- return mem;
- }
-
- if (streq(value, "max"))
- return mem;
- } else {
- r = cg_get_attribute("memory", root, "memory.limit_in_bytes", &value);
- if (r < 0) {
- log_debug_errno(r, "Failed to read memory.limit_in_bytes cgroup attribute, ignoring cgroup memory limit: %m");
- return mem;
- }
- }
-
- r = safe_atou64(value, &lim);
- if (r < 0) {
- log_debug_errno(r, "Failed to parse cgroup memory limit '%s', ignoring: %m", value);
- return mem;
- }
- if (lim == UINT64_MAX)
- return mem;
-
- /* Make sure the limit is a multiple of our own page size */
- lim /= ps;
- lim *= ps;
-
- return MIN(mem, lim);
-}
-
-uint64_t physical_memory_scale(uint64_t v, uint64_t max) {
- uint64_t p, m, ps, r;
-
- assert(max > 0);
-
- /* Returns the physical memory size, multiplied by v divided by max. Returns UINT64_MAX on overflow. On success
- * the result is a multiple of the page size (rounds down). */
-
- ps = page_size();
- assert(ps > 0);
-
- p = physical_memory() / ps;
- assert(p > 0);
-
- m = p * v;
- if (m / p != v)
- return UINT64_MAX;
-
- m /= max;
-
- r = m * ps;
- if (r / ps != m)
- return UINT64_MAX;
-
- return r;
-}
-
-uint64_t system_tasks_max(void) {
-
- uint64_t a = TASKS_MAX, b = TASKS_MAX;
- _cleanup_free_ char *root = NULL;
- int r;
-
- /* Determine the maximum number of tasks that may run on this system. We check three sources to determine this
- * limit:
- *
- * a) the maximum tasks value the kernel allows on this architecture
- * b) the cgroups pids_max attribute for the system
- * c) the kernel's configured maximum PID value
- *
- * And then pick the smallest of the three */
-
- r = procfs_tasks_get_limit(&a);
- if (r < 0)
- log_debug_errno(r, "Failed to read maximum number of tasks from /proc, ignoring: %m");
-
- r = cg_get_root_path(&root);
- if (r < 0)
- log_debug_errno(r, "Failed to determine cgroup root path, ignoring: %m");
- else {
- _cleanup_free_ char *value = NULL;
-
- r = cg_get_attribute("pids", root, "pids.max", &value);
- if (r < 0)
- log_debug_errno(r, "Failed to read pids.max attribute of cgroup root, ignoring: %m");
- else if (!streq(value, "max")) {
- r = safe_atou64(value, &b);
- if (r < 0)
- log_debug_errno(r, "Failed to parse pids.max attribute of cgroup root, ignoring: %m");
- }
- }
-
- return MIN3(TASKS_MAX,
- a <= 0 ? TASKS_MAX : a,
- b <= 0 ? TASKS_MAX : b);
-}
-
-uint64_t system_tasks_max_scale(uint64_t v, uint64_t max) {
- uint64_t t, m;
-
- assert(max > 0);
-
- /* Multiply the system's task value by the fraction v/max. Hence, if max==100 this calculates percentages
- * relative to the system's maximum number of tasks. Returns UINT64_MAX on overflow. */
-
- t = system_tasks_max();
- assert(t > 0);
-
- m = t * v;
- if (m / t != v) /* overflow? */
- return UINT64_MAX;
-
- return m / max;
-}
-
int version(void) {
puts("systemd " STRINGIFY(PROJECT_VERSION) " (" GIT_VERSION ")\n"
SYSTEMD_FEATURES);
diff --git a/src/basic/util.h b/src/basic/util.h
index dc33d66067..02fc31e69e 100644
--- a/src/basic/util.h
+++ b/src/basic/util.h
@@ -27,9 +27,6 @@
#include "macro.h"
#include "time-util.h"
-size_t page_size(void) _pure_;
-#define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
-
static inline const char* yes_no(bool b) {
return b ? "yes" : "no";
}
@@ -66,113 +63,8 @@ int prot_from_flags(int flags) _const_;
bool in_initrd(void);
void in_initrd_force(bool value);
-void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
- __compar_d_fn_t compar, void *arg);
-
-#define typesafe_bsearch_r(k, b, n, func, userdata) \
- ({ \
- const typeof(b[0]) *_k = k; \
- int (*_func_)(const typeof(b[0])*, const typeof(b[0])*, typeof(userdata)) = func; \
- xbsearch_r((const void*) _k, (b), (n), sizeof((b)[0]), (__compar_d_fn_t) _func_, userdata); \
- })
-
-/**
- * Normal bsearch requires base to be nonnull. Here were require
- * that only if nmemb > 0.
- */
-static inline void* bsearch_safe(const void *key, const void *base,
- size_t nmemb, size_t size, __compar_fn_t compar) {
- if (nmemb <= 0)
- return NULL;
-
- assert(base);
- return bsearch(key, base, nmemb, size, compar);
-}
-
-#define typesafe_bsearch(k, b, n, func) \
- ({ \
- const typeof(b[0]) *_k = k; \
- int (*_func_)(const typeof(b[0])*, const typeof(b[0])*) = func; \
- bsearch_safe((const void*) _k, (b), (n), sizeof((b)[0]), (__compar_fn_t) _func_); \
- })
-
-/**
- * Normal qsort requires base to be nonnull. Here were require
- * that only if nmemb > 0.
- */
-static inline void qsort_safe(void *base, size_t nmemb, size_t size, __compar_fn_t compar) {
- if (nmemb <= 1)
- return;
-
- assert(base);
- qsort(base, nmemb, size, compar);
-}
-
-/* A wrapper around the above, but that adds typesafety: the element size is automatically derived from the type and so
- * is the prototype for the comparison function */
-#define typesafe_qsort(p, n, func) \
- ({ \
- int (*_func_)(const typeof(p[0])*, const typeof(p[0])*) = func; \
- qsort_safe((p), (n), sizeof((p)[0]), (__compar_fn_t) _func_); \
- })
-
-static inline void qsort_r_safe(void *base, size_t nmemb, size_t size, __compar_d_fn_t compar, void *userdata) {
- if (nmemb <= 1)
- return;
-
- assert(base);
- qsort_r(base, nmemb, size, compar, userdata);
-}
-
-#define typesafe_qsort_r(p, n, func, userdata) \
- ({ \
- int (*_func_)(const typeof(p[0])*, const typeof(p[0])*, typeof(userdata)) = func; \
- qsort_r_safe((p), (n), sizeof((p)[0]), (__compar_d_fn_t) _func_, userdata); \
- })
-
-/* Normal memcpy requires src to be nonnull. We do nothing if n is 0. */
-static inline void memcpy_safe(void *dst, const void *src, size_t n) {
- if (n == 0)
- return;
- assert(src);
- memcpy(dst, src, n);
-}
-
-/* Normal memcmp requires s1 and s2 to be nonnull. We do nothing if n is 0. */
-static inline int memcmp_safe(const void *s1, const void *s2, size_t n) {
- if (n == 0)
- return 0;
- assert(s1);
- assert(s2);
- return memcmp(s1, s2, n);
-}
-
-/* Compare s1 (length n1) with s2 (length n2) in lexicographic order. */
-static inline int memcmp_nn(const void *s1, size_t n1, const void *s2, size_t n2) {
- return memcmp_safe(s1, s2, MIN(n1, n2))
- ?: CMP(n1, n2);
-}
-
int on_ac_power(void);
-#define memzero(x,l) \
- ({ \
- size_t _l_ = (l); \
- void *_x_ = (x); \
- _l_ == 0 ? _x_ : memset(_x_, 0, _l_); \
- })
-
-#define zero(x) (memzero(&(x), sizeof(x)))
-
-bool memeqzero(const void *data, size_t length);
-
-#define eqzero(x) memeqzero(x, sizeof(x))
-
-static inline void *mempset(void *s, int c, size_t n) {
- memset(s, c, n);
- return (uint8_t*)s + n;
-}
-
static inline void _reset_errno_(int *saved_errno) {
if (*saved_errno < 0) /* Invalidated by UNPROTECT_ERRNO? */
return;
@@ -237,15 +129,6 @@ static inline unsigned log2u_round_up(unsigned x) {
int container_get_leader(const char *machine, pid_t *pid);
-int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *userns_fd, int *root_fd);
-int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int userns_fd, int root_fd);
-
-uint64_t physical_memory(void);
-uint64_t physical_memory_scale(uint64_t v, uint64_t max);
-
-uint64_t system_tasks_max(void);
-uint64_t system_tasks_max_scale(uint64_t v, uint64_t max);
-
int version(void);
int str_verscmp(const char *s1, const char *s2);
diff --git a/src/busctl/busctl.c b/src/busctl/busctl.c
index a61fd2e706..c8125c6606 100644
--- a/src/busctl/busctl.c
+++ b/src/busctl/busctl.c
@@ -25,10 +25,10 @@
#include "path-util.h"
#include "pretty-print.h"
#include "set.h"
+#include "sort-util.h"
#include "strv.h"
#include "terminal-util.h"
#include "user-util.h"
-#include "util.h"
#include "verbs.h"
static enum {
diff --git a/src/cgtop/cgtop.c b/src/cgtop/cgtop.c
index ab3b979eaf..eb9ccd0cb0 100644
--- a/src/cgtop/cgtop.c
+++ b/src/cgtop/cgtop.c
@@ -25,11 +25,11 @@
#include "pretty-print.h"
#include "process-util.h"
#include "procfs-util.h"
+#include "sort-util.h"
#include "stdio-util.h"
#include "strv.h"
#include "terminal-util.h"
#include "unit-name.h"
-#include "util.h"
#include "virt.h"
typedef struct Group {
diff --git a/src/core/bpf-firewall.c b/src/core/bpf-firewall.c
index b9a611fd9e..2ecce300da 100644
--- a/src/core/bpf-firewall.c
+++ b/src/core/bpf-firewall.c
@@ -20,6 +20,7 @@
#include "bpf-program.h"
#include "fd-util.h"
#include "ip-address-access.h"
+#include "memory-util.h"
#include "missing_syscall.h"
#include "unit.h"
diff --git a/src/core/dbus-cgroup.c b/src/core/dbus-cgroup.c
index 7ab53a1c2a..4615aeaf66 100644
--- a/src/core/dbus-cgroup.c
+++ b/src/core/dbus-cgroup.c
@@ -13,6 +13,7 @@
#include "dbus-util.h"
#include "fd-util.h"
#include "fileio.h"
+#include "limits-util.h"
#include "path-util.h"
static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_cgroup_device_policy, cgroup_device_policy, CGroupDevicePolicy);
diff --git a/src/core/execute.c b/src/core/execute.c
index fb7564b9fe..49478305e2 100644
--- a/src/core/execute.c
+++ b/src/core/execute.c
@@ -65,6 +65,7 @@
#include "log.h"
#include "macro.h"
#include "manager.h"
+#include "memory-util.h"
#include "missing.h"
#include "mkdir.h"
#include "namespace.h"
@@ -91,7 +92,6 @@
#include "umask-util.h"
#include "unit.h"
#include "user-util.h"
-#include "util.h"
#include "utmp-wtmp.h"
#define IDLE_TIMEOUT_USEC (5*USEC_PER_SEC)
diff --git a/src/core/job.c b/src/core/job.c
index b2aa0c600f..81f5f9cb72 100644
--- a/src/core/job.c
+++ b/src/core/job.c
@@ -17,6 +17,7 @@
#include "parse-util.h"
#include "serialize.h"
#include "set.h"
+#include "sort-util.h"
#include "special.h"
#include "stdio-util.h"
#include "string-table.h"
diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c
index 084c61d982..805bdbca9b 100644
--- a/src/core/load-fragment.c
+++ b/src/core/load-fragment.c
@@ -36,6 +36,7 @@
#include "ioprio.h"
#include "ip-protocol-list.h"
#include "journal-util.h"
+#include "limits-util.h"
#include "load-fragment.h"
#include "log.h"
#include "missing.h"
diff --git a/src/core/machine-id-setup.c b/src/core/machine-id-setup.c
index aae548064e..9d3096e3ac 100644
--- a/src/core/machine-id-setup.c
+++ b/src/core/machine-id-setup.c
@@ -16,6 +16,7 @@
#include "macro.h"
#include "mkdir.h"
#include "mountpoint-util.h"
+#include "namespace-util.h"
#include "path-util.h"
#include "process-util.h"
#include "stat-util.h"
diff --git a/src/core/main.c b/src/core/main.c
index 47a976ad0f..c0fa33bab4 100644
--- a/src/core/main.c
+++ b/src/core/main.c
@@ -47,6 +47,7 @@
#include "ima-setup.h"
#include "killall.h"
#include "kmod-setup.h"
+#include "limits-util.h"
#include "load-fragment.h"
#include "log.h"
#include "loopback-setup.h"
diff --git a/src/core/manager.c b/src/core/manager.c
index f305dc6647..e60ee32b06 100644
--- a/src/core/manager.c
+++ b/src/core/manager.c
@@ -52,6 +52,7 @@
#include "log.h"
#include "macro.h"
#include "manager.h"
+#include "memory-util.h"
#include "missing.h"
#include "mkdir.h"
#include "parse-util.h"
@@ -77,7 +78,6 @@
#include "umask-util.h"
#include "unit-name.h"
#include "user-util.h"
-#include "util.h"
#include "virt.h"
#include "watchdog.h"
diff --git a/src/core/namespace.c b/src/core/namespace.c
index 4ee2b78653..582f6cb249 100644
--- a/src/core/namespace.c
+++ b/src/core/namespace.c
@@ -21,17 +21,18 @@
#include "mkdir.h"
#include "mount-util.h"
#include "mountpoint-util.h"
+#include "namespace-util.h"
#include "namespace.h"
#include "path-util.h"
#include "selinux-util.h"
#include "socket-util.h"
+#include "sort-util.h"
#include "stat-util.h"
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
#include "umask-util.h"
#include "user-util.h"
-#include "util.h"
#define DEV_MOUNT_OPTIONS (MS_NOSUID|MS_STRICTATIME|MS_NOEXEC)
diff --git a/src/coredump/coredump-vacuum.c b/src/coredump/coredump-vacuum.c
index 6ce5dfc71d..73fd420f3d 100644
--- a/src/coredump/coredump-vacuum.c
+++ b/src/coredump/coredump-vacuum.c
@@ -9,10 +9,10 @@
#include "fs-util.h"
#include "hashmap.h"
#include "macro.h"
+#include "memory-util.h"
#include "string-util.h"
#include "time-util.h"
#include "user-util.h"
-#include "util.h"
#define DEFAULT_MAX_USE_LOWER (uint64_t) (1ULL*1024ULL*1024ULL) /* 1 MiB */
#define DEFAULT_MAX_USE_UPPER (uint64_t) (4ULL*1024ULL*1024ULL*1024ULL) /* 4 GiB */
diff --git a/src/coredump/coredump.c b/src/coredump/coredump.c
index ecbb4bffc9..85e265c218 100644
--- a/src/coredump/coredump.c
+++ b/src/coredump/coredump.c
@@ -35,6 +35,7 @@
#include "log.h"
#include "macro.h"
#include "main-func.h"
+#include "memory-util.h"
#include "missing.h"
#include "mkdir.h"
#include "parse-util.h"
@@ -48,7 +49,6 @@
#include "strv.h"
#include "tmpfile-util.h"
#include "user-util.h"
-#include "util.h"
/* The maximum size up to which we process coredumps */
#define PROCESS_SIZE_MAX ((uint64_t) (2LLU*1024LLU*1024LLU*1024LLU))
diff --git a/src/fuzz/fuzz-dns-packet.c b/src/fuzz/fuzz-dns-packet.c
index c150c8184c..9bf95b9c76 100644
--- a/src/fuzz/fuzz-dns-packet.c
+++ b/src/fuzz/fuzz-dns-packet.c
@@ -1,6 +1,7 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include "fuzz.h"
+#include "memory-util.h"
#include "resolved-dns-packet.h"
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
diff --git a/src/initctl/initctl.c b/src/initctl/initctl.c
index 260dc2eb61..651e70c7fa 100644
--- a/src/initctl/initctl.c
+++ b/src/initctl/initctl.c
@@ -18,9 +18,9 @@
#include "initreq.h"
#include "list.h"
#include "log.h"
-#include "special.h"
-#include "util.h"
+#include "memory-util.h"
#include "process-util.h"
+#include "special.h"
#define SERVER_FD_MAX 16
#define TIMEOUT_MSEC ((int) (DEFAULT_EXIT_USEC/USEC_PER_MSEC))
diff --git a/src/journal/catalog.c b/src/journal/catalog.c
index ccee950d1d..21c0eaac9f 100644
--- a/src/journal/catalog.c
+++ b/src/journal/catalog.c
@@ -17,15 +17,16 @@
#include "fileio.h"
#include "hashmap.h"
#include "log.h"
+#include "memory-util.h"
#include "mkdir.h"
#include "path-util.h"
#include "siphash24.h"
+#include "sort-util.h"
#include "sparse-endian.h"
#include "strbuf.h"
#include "string-util.h"
#include "strv.h"
#include "tmpfile-util.h"
-#include "util.h"
const char * const catalog_file_dirs[] = {
"/usr/local/lib/systemd/catalog/",
diff --git a/src/journal/fsprg.c b/src/journal/fsprg.c
index 6d062de5f4..d60fc50068 100644
--- a/src/journal/fsprg.c
+++ b/src/journal/fsprg.c
@@ -32,6 +32,7 @@
#include "fsprg.h"
#include "gcrypt-util.h"
+#include "memory-util.h"
#define ISVALID_SECPAR(secpar) (((secpar) % 16 == 0) && ((secpar) >= 16) && ((secpar) <= 16384))
#define VALIDATE_SECPAR(secpar) assert(ISVALID_SECPAR(secpar));
diff --git a/src/journal/journal-authenticate.c b/src/journal/journal-authenticate.c
index 8a9ce8a0d0..a5ee10c3bc 100644
--- a/src/journal/journal-authenticate.c
+++ b/src/journal/journal-authenticate.c
@@ -10,6 +10,7 @@
#include "journal-authenticate.h"
#include "journal-def.h"
#include "journal-file.h"
+#include "memory-util.h"
static uint64_t journal_file_tag_seqnum(JournalFile *f) {
uint64_t r;
diff --git a/src/journal/journal-file.c b/src/journal/journal-file.c
index 56827f9f36..e0f06a21bb 100644
--- a/src/journal/journal-file.c
+++ b/src/journal/journal-file.c
@@ -22,10 +22,12 @@
#include "journal-def.h"
#include "journal-file.h"
#include "lookup3.h"
+#include "memory-util.h"
#include "parse-util.h"
#include "path-util.h"
#include "random-util.h"
#include "set.h"
+#include "sort-util.h"
#include "stat-util.h"
#include "string-util.h"
#include "strv.h"
diff --git a/src/journal/journal-vacuum.c b/src/journal/journal-vacuum.c
index 2778ce40c5..a932314e19 100644
--- a/src/journal/journal-vacuum.c
+++ b/src/journal/journal-vacuum.c
@@ -14,9 +14,9 @@
#include "journal-file.h"
#include "journal-vacuum.h"
#include "parse-util.h"
+#include "sort-util.h"
#include "string-util.h"
#include "time-util.h"
-#include "util.h"
#include "xattr-util.h"
struct vacuum_info {
diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
index 75b23c7144..9b813a14a0 100644
--- a/src/journal/journalctl.c
+++ b/src/journal/journalctl.c
@@ -50,6 +50,7 @@
#include "locale-util.h"
#include "log.h"
#include "logs-show.h"
+#include "memory-util.h"
#include "mkdir.h"
#include "pager.h"
#include "parse-util.h"
diff --git a/src/journal/journald-native.c b/src/journal/journald-native.c
index 221188db16..4efe34c664 100644
--- a/src/journal/journald-native.c
+++ b/src/journal/journald-native.c
@@ -19,6 +19,7 @@
#include "journald-syslog.h"
#include "journald-wall.h"
#include "memfd-util.h"
+#include "memory-util.h"
#include "parse-util.h"
#include "path-util.h"
#include "process-util.h"
diff --git a/src/journal/mmap-cache.c b/src/journal/mmap-cache.c
index 0dc453e709..7152564293 100644
--- a/src/journal/mmap-cache.c
+++ b/src/journal/mmap-cache.c
@@ -10,9 +10,9 @@
#include "list.h"
#include "log.h"
#include "macro.h"
+#include "memory-util.h"
#include "mmap-cache.h"
#include "sigbus.h"
-#include "util.h"
typedef struct Window Window;
typedef struct Context Context;
diff --git a/src/journal/test-compress-benchmark.c b/src/journal/test-compress-benchmark.c
index 7f13b611e6..1838f5b7d9 100644
--- a/src/journal/test-compress-benchmark.c
+++ b/src/journal/test-compress-benchmark.c
@@ -4,6 +4,7 @@
#include "compress.h"
#include "env-util.h"
#include "macro.h"
+#include "memory-util.h"
#include "parse-util.h"
#include "process-util.h"
#include "random-util.h"
diff --git a/src/journal/test-compress.c b/src/journal/test-compress.c
index 1b050b7052..0920833c12 100644
--- a/src/journal/test-compress.c
+++ b/src/journal/test-compress.c
@@ -9,11 +9,11 @@
#include "fd-util.h"
#include "fs-util.h"
#include "macro.h"
+#include "memory-util.h"
#include "path-util.h"
#include "random-util.h"
#include "tests.h"
#include "tmpfile-util.h"
-#include "util.h"
#if HAVE_XZ
# define XZ_OK 0
diff --git a/src/libsystemd-network/dhcp-option.c b/src/libsystemd-network/dhcp-option.c
index ad3f92546d..a68de4ff98 100644
--- a/src/libsystemd-network/dhcp-option.c
+++ b/src/libsystemd-network/dhcp-option.c
@@ -9,10 +9,10 @@
#include <string.h>
#include "alloc-util.h"
-#include "utf8.h"
-#include "strv.h"
-
#include "dhcp-internal.h"
+#include "memory-util.h"
+#include "strv.h"
+#include "utf8.h"
static int option_append(uint8_t options[], size_t size, size_t *offset,
uint8_t code, size_t optlen, const void *optval) {
diff --git a/src/libsystemd-network/dhcp6-option.c b/src/libsystemd-network/dhcp6-option.c
index a2aac9a793..017402c53b 100644
--- a/src/libsystemd-network/dhcp6-option.c
+++ b/src/libsystemd-network/dhcp6-option.c
@@ -14,10 +14,10 @@
#include "dhcp6-lease-internal.h"
#include "dhcp6-protocol.h"
#include "dns-domain.h"
+#include "memory-util.h"
#include "sparse-endian.h"
#include "strv.h"
#include "unaligned.h"
-#include "util.h"
typedef struct DHCP6StatusOption {
struct DHCP6Option option;
diff --git a/src/libsystemd-network/lldp-neighbor.c b/src/libsystemd-network/lldp-neighbor.c
index f6db62594d..9bae4a3c6e 100644
--- a/src/libsystemd-network/lldp-neighbor.c
+++ b/src/libsystemd-network/lldp-neighbor.c
@@ -7,9 +7,9 @@
#include "in-addr-util.h"
#include "lldp-internal.h"
#include "lldp-neighbor.h"
+#include "memory-util.h"
#include "missing.h"
#include "unaligned.h"
-#include "util.h"
static void lldp_neighbor_id_hash_func(const LLDPNeighborID *id, struct siphash *state) {
siphash24_compress(id->chassis_id, id->chassis_id_size, state);
diff --git a/src/libsystemd-network/ndisc-router.c b/src/libsystemd-network/ndisc-router.c
index 6935311b9a..c0938724e3 100644
--- a/src/libsystemd-network/ndisc-router.c
+++ b/src/libsystemd-network/ndisc-router.c
@@ -10,6 +10,7 @@
#include "alloc-util.h"
#include "dns-domain.h"
#include "hostname-util.h"
+#include "memory-util.h"
#include "missing.h"
#include "ndisc-internal.h"
#include "ndisc-router.h"
diff --git a/src/libsystemd-network/sd-dhcp-client.c b/src/libsystemd-network/sd-dhcp-client.c
index 7975cfecf5..97e1dd3702 100644
--- a/src/libsystemd-network/sd-dhcp-client.c
+++ b/src/libsystemd-network/sd-dhcp-client.c
@@ -24,10 +24,10 @@
#include "event-util.h"
#include "hostname-util.h"
#include "io-util.h"
+#include "memory-util.h"
#include "random-util.h"
#include "string-util.h"
#include "strv.h"
-#include "util.h"
#define MAX_CLIENT_ID_LEN (sizeof(uint32_t) + MAX_DUID_LEN) /* Arbitrary limit */
#define MAX_MAC_ADDR_LEN CONST_MAX(INFINIBAND_ALEN, ETH_ALEN)
diff --git a/src/libsystemd-network/sd-lldp.c b/src/libsystemd-network/sd-lldp.c
index 969fc71051..1f28c5731f 100644
--- a/src/libsystemd-network/sd-lldp.c
+++ b/src/libsystemd-network/sd-lldp.c
@@ -13,7 +13,9 @@
#include "lldp-internal.h"
#include "lldp-neighbor.h"
#include "lldp-network.h"
+#include "memory-util.h"
#include "socket-util.h"
+#include "sort-util.h"
#include "string-table.h"
#define LLDP_DEFAULT_NEIGHBORS_MAX 128U
diff --git a/src/libsystemd-network/sd-ndisc.c b/src/libsystemd-network/sd-ndisc.c
index 32c20b1e4a..5e711713d9 100644
--- a/src/libsystemd-network/sd-ndisc.c
+++ b/src/libsystemd-network/sd-ndisc.c
@@ -13,13 +13,13 @@
#include "fd-util.h"
#include "icmp6-util.h"
#include "in-addr-util.h"
+#include "memory-util.h"
#include "ndisc-internal.h"
#include "ndisc-router.h"
#include "random-util.h"
#include "socket-util.h"
#include "string-table.h"
#include "string-util.h"
-#include "util.h"
#define NDISC_TIMEOUT_NO_RA_USEC (NDISC_ROUTER_SOLICITATION_INTERVAL * NDISC_MAX_ROUTER_SOLICITATIONS)
diff --git a/src/libsystemd-network/sd-radv.c b/src/libsystemd-network/sd-radv.c
index 098e01fb82..08433adb25 100644
--- a/src/libsystemd-network/sd-radv.c
+++ b/src/libsystemd-network/sd-radv.c
@@ -18,12 +18,12 @@
#include "in-addr-util.h"
#include "io-util.h"
#include "macro.h"
+#include "memory-util.h"
#include "radv-internal.h"
#include "random-util.h"
#include "socket-util.h"
#include "string-util.h"
#include "strv.h"
-#include "util.h"
_public_ int sd_radv_new(sd_radv **ret) {
_cleanup_(sd_radv_unrefp) sd_radv *ra = NULL;
diff --git a/src/libsystemd-network/test-dhcp-option.c b/src/libsystemd-network/test-dhcp-option.c
index d84859c053..56bd690cb0 100644
--- a/src/libsystemd-network/test-dhcp-option.c
+++ b/src/libsystemd-network/test-dhcp-option.c
@@ -7,7 +7,7 @@
#include "dhcp-internal.h"
#include "dhcp-protocol.h"
#include "macro.h"
-#include "util.h"
+#include "memory-util.h"
struct option_desc {
uint8_t sname[64];
diff --git a/src/libsystemd-network/test-dhcp6-client.c b/src/libsystemd-network/test-dhcp6-client.c
index fa94b3cb75..00056aa9e2 100644
--- a/src/libsystemd-network/test-dhcp6-client.c
+++ b/src/libsystemd-network/test-dhcp6-client.c
@@ -18,9 +18,10 @@
#include "dhcp6-protocol.h"
#include "fd-util.h"
#include "macro.h"
+#include "memory-util.h"
#include "socket-util.h"
#include "tests.h"
-#include "util.h"
+#include "time-util.h"
#include "virt.h"
static struct ether_addr mac_addr = {
diff --git a/src/libsystemd/sd-bus/bus-container.c b/src/libsystemd/sd-bus/bus-container.c
index 2cfeefc2c3..40b0e8a947 100644
--- a/src/libsystemd/sd-bus/bus-container.c
+++ b/src/libsystemd/sd-bus/bus-container.c
@@ -7,6 +7,7 @@
#include "bus-internal.h"
#include "bus-socket.h"
#include "fd-util.h"
+#include "namespace-util.h"
#include "process-util.h"
#include "util.h"
diff --git a/src/libsystemd/sd-bus/bus-introspect.c b/src/libsystemd/sd-bus/bus-introspect.c
index bde97483e8..29a5ef715e 100644
--- a/src/libsystemd/sd-bus/bus-introspect.c
+++ b/src/libsystemd/sd-bus/bus-introspect.c
@@ -9,8 +9,8 @@
#include "bus-signature.h"
#include "fd-util.h"
#include "fileio.h"
+#include "memory-util.h"
#include "string-util.h"
-#include "util.h"
int introspect_begin(struct introspect *i, bool trusted) {
assert(i);
diff --git a/src/libsystemd/sd-bus/bus-kernel.c b/src/libsystemd/sd-bus/bus-kernel.c
index 34c8a9f8c8..b74cfa6ab8 100644
--- a/src/libsystemd/sd-bus/bus-kernel.c
+++ b/src/libsystemd/sd-bus/bus-kernel.c
@@ -31,7 +31,7 @@
#include "string-util.h"
#include "strv.h"
#include "user-util.h"
-#include "util.h"
+#include "memory-util.h"
void close_and_munmap(int fd, void *address, size_t size) {
if (size > 0)
diff --git a/src/libsystemd/sd-bus/bus-match.c b/src/libsystemd/sd-bus/bus-match.c
index 9642de10c3..266dd7f1df 100644
--- a/src/libsystemd/sd-bus/bus-match.c
+++ b/src/libsystemd/sd-bus/bus-match.c
@@ -10,6 +10,7 @@
#include "fd-util.h"
#include "fileio.h"
#include "hexdecoct.h"
+#include "sort-util.h"
#include "string-util.h"
#include "strv.h"
diff --git a/src/libsystemd/sd-bus/bus-message.c b/src/libsystemd/sd-bus/bus-message.c
index 700c57d362..11c4648f91 100644
--- a/src/libsystemd/sd-bus/bus-message.c
+++ b/src/libsystemd/sd-bus/bus-message.c
@@ -16,11 +16,11 @@
#include "fd-util.h"
#include "io-util.h"
#include "memfd-util.h"
+#include "memory-util.h"
#include "string-util.h"
#include "strv.h"
#include "time-util.h"
#include "utf8.h"
-#include "util.h"
static int message_append_basic(sd_bus_message *m, char type, const void *p, const void **stored);
diff --git a/src/libsystemd/sd-bus/bus-socket.c b/src/libsystemd/sd-bus/bus-socket.c
index df9b2631fd..89ac503ab8 100644
--- a/src/libsystemd/sd-bus/bus-socket.c
+++ b/src/libsystemd/sd-bus/bus-socket.c
@@ -18,6 +18,7 @@
#include "hexdecoct.h"
#include "io-util.h"
#include "macro.h"
+#include "memory-util.h"
#include "missing.h"
#include "path-util.h"
#include "process-util.h"
@@ -28,7 +29,6 @@
#include "string-util.h"
#include "user-util.h"
#include "utf8.h"
-#include "util.h"
#define SNDBUF_SIZE (8*1024*1024)
diff --git a/src/libsystemd/sd-bus/sd-bus.c b/src/libsystemd/sd-bus/sd-bus.c
index ba791db4f2..f7177963c2 100644
--- a/src/libsystemd/sd-bus/sd-bus.c
+++ b/src/libsystemd/sd-bus/sd-bus.c
@@ -32,12 +32,12 @@
#include "hexdecoct.h"
#include "hostname-util.h"
#include "macro.h"
+#include "memory-util.h"
#include "missing.h"
#include "parse-util.h"
#include "process-util.h"
#include "string-util.h"
#include "strv.h"
-#include "util.h"
#define log_debug_bus_message(m) \
do { \
diff --git a/src/libsystemd/sd-bus/test-bus-match.c b/src/libsystemd/sd-bus/test-bus-match.c
index 0949d8dee6..47014c403b 100644
--- a/src/libsystemd/sd-bus/test-bus-match.c
+++ b/src/libsystemd/sd-bus/test-bus-match.c
@@ -6,6 +6,7 @@
#include "bus-util.h"
#include "log.h"
#include "macro.h"
+#include "memory-util.h"
#include "tests.h"
static bool mask[32];
diff --git a/src/libsystemd/sd-bus/test-bus-server.c b/src/libsystemd/sd-bus/test-bus-server.c
index 0f1b9645bc..e38bcdcc76 100644
--- a/src/libsystemd/sd-bus/test-bus-server.c
+++ b/src/libsystemd/sd-bus/test-bus-server.c
@@ -9,7 +9,7 @@
#include "bus-util.h"
#include "log.h"
#include "macro.h"
-#include "util.h"
+#include "memory-util.h"
struct context {
int fds[2];
diff --git a/src/libsystemd/sd-device/device-enumerator.c b/src/libsystemd/sd-device/device-enumerator.c
index f643c6ea37..8f2764490a 100644
--- a/src/libsystemd/sd-device/device-enumerator.c
+++ b/src/libsystemd/sd-device/device-enumerator.c
@@ -8,9 +8,9 @@
#include "dirent-util.h"
#include "fd-util.h"
#include "set.h"
+#include "sort-util.h"
#include "string-util.h"
#include "strv.h"
-#include "util.h"
#define DEVICE_ENUMERATE_MAX_DEPTH 256
diff --git a/src/libsystemd/sd-event/sd-event.c b/src/libsystemd/sd-event/sd-event.c
index 04ba7e2574..eb98e49ca2 100644
--- a/src/libsystemd/sd-event/sd-event.c
+++ b/src/libsystemd/sd-event/sd-event.c
@@ -15,6 +15,7 @@
#include "hashmap.h"
#include "list.h"
#include "macro.h"
+#include "memory-util.h"
#include "missing.h"
#include "prioq.h"
#include "process-util.h"
@@ -23,7 +24,6 @@
#include "string-table.h"
#include "string-util.h"
#include "time-util.h"
-#include "util.h"
#define DEFAULT_ACCURACY_USEC (250 * USEC_PER_MSEC)
diff --git a/src/libsystemd/sd-hwdb/hwdb-util.c b/src/libsystemd/sd-hwdb/hwdb-util.c
index f8529670b3..dd9bf9e18d 100644
--- a/src/libsystemd/sd-hwdb/hwdb-util.c
+++ b/src/libsystemd/sd-hwdb/hwdb-util.c
@@ -13,6 +13,7 @@
#include "label.h"
#include "mkdir.h"
#include "path-util.h"
+#include "sort-util.h"
#include "strbuf.h"
#include "string-util.h"
#include "strv.h"
diff --git a/src/libsystemd/sd-netlink/local-addresses.c b/src/libsystemd/sd-netlink/local-addresses.c
index 5c37279bd2..751144539b 100644
--- a/src/libsystemd/sd-netlink/local-addresses.c
+++ b/src/libsystemd/sd-netlink/local-addresses.c
@@ -6,6 +6,7 @@
#include "local-addresses.h"
#include "macro.h"
#include "netlink-util.h"
+#include "sort-util.h"
static int address_compare(const struct local_address *a, const struct local_address *b) {
int r;
diff --git a/src/libsystemd/sd-netlink/netlink-message.c b/src/libsystemd/sd-netlink/netlink-message.c
index bf3ac95400..0dcc53be55 100644
--- a/src/libsystemd/sd-netlink/netlink-message.c
+++ b/src/libsystemd/sd-netlink/netlink-message.c
@@ -13,7 +13,7 @@
#include "netlink-types.h"
#include "netlink-util.h"
#include "socket-util.h"
-#include "util.h"
+#include "memory-util.h"
#define GET_CONTAINER(m, i) ((i) < (m)->n_containers ? (struct rtattr*)((uint8_t*)(m)->hdr + (m)->containers[i].offset) : NULL)
#define PUSH_CONTAINER(m, new) (m)->container_offsets[(m)->n_containers++] = (uint8_t*)(new) - (uint8_t*)(m)->hdr;
diff --git a/src/libsystemd/sd-resolve/sd-resolve.c b/src/libsystemd/sd-resolve/sd-resolve.c
index 36b9c8d019..fdad3bd697 100644
--- a/src/libsystemd/sd-resolve/sd-resolve.c
+++ b/src/libsystemd/sd-resolve/sd-resolve.c
@@ -19,11 +19,11 @@
#include "fd-util.h"
#include "io-util.h"
#include "list.h"
+#include "memory-util.h"
#include "missing.h"
+#include "process-util.h"
#include "resolve-private.h"
#include "socket-util.h"
-#include "util.h"
-#include "process-util.h"
#define WORKERS_MIN 1U
#define WORKERS_MAX 16U
diff --git a/src/libudev/libudev-list.c b/src/libudev/libudev-list.c
index 2737326187..00fd58b9ea 100644
--- a/src/libudev/libudev-list.c
+++ b/src/libudev/libudev-list.c
@@ -7,7 +7,7 @@
#include "alloc-util.h"
#include "libudev-list-internal.h"
-#include "util.h"
+#include "memory-util.h"
/**
* SECTION:libudev-list
diff --git a/src/locale/localectl.c b/src/locale/localectl.c
index 69f5667801..683b127ba1 100644
--- a/src/locale/localectl.c
+++ b/src/locale/localectl.c
@@ -16,13 +16,13 @@
#include "fileio.h"
#include "locale-util.h"
#include "main-func.h"
+#include "memory-util.h"
#include "pager.h"
#include "pretty-print.h"
#include "proc-cmdline.h"
#include "set.h"
#include "spawn-polkit-agent.h"
#include "strv.h"
-#include "util.h"
#include "verbs.h"
#include "virt.h"
diff --git a/src/login/loginctl.c b/src/login/loginctl.c
index 69850724da..b40f527233 100644
--- a/src/login/loginctl.c
+++ b/src/login/loginctl.c
@@ -19,6 +19,7 @@
#include "logs-show.h"
#include "macro.h"
#include "main-func.h"
+#include "memory-util.h"
#include "pager.h"
#include "parse-util.h"
#include "pretty-print.h"
@@ -33,7 +34,6 @@
#include "terminal-util.h"
#include "unit-name.h"
#include "user-util.h"
-#include "util.h"
#include "verbs.h"
static char **arg_property = NULL;
diff --git a/src/login/logind-core.c b/src/login/logind-core.c
index f3d8de4f14..2467da18ee 100644
--- a/src/login/logind-core.c
+++ b/src/login/logind-core.c
@@ -18,6 +18,7 @@
#include "conf-parser.h"
#include "device-util.h"
#include "fd-util.h"
+#include "limits-util.h"
#include "logind.h"
#include "parse-util.h"
#include "path-util.h"
diff --git a/src/login/logind-user.c b/src/login/logind-user.c
index 2b327cbe19..045b6f0e17 100644
--- a/src/login/logind-user.c
+++ b/src/login/logind-user.c
@@ -19,6 +19,7 @@
#include "fs-util.h"
#include "hashmap.h"
#include "label.h"
+#include "limits-util.h"
#include "logind-user.h"
#include "mkdir.h"
#include "parse-util.h"
diff --git a/src/machine/machine-dbus.c b/src/machine/machine-dbus.c
index 7a558df898..1ee878055e 100644
--- a/src/machine/machine-dbus.c
+++ b/src/machine/machine-dbus.c
@@ -29,6 +29,7 @@
#include "machine.h"
#include "missing_capability.h"
#include "mkdir.h"
+#include "namespace-util.h"
#include "os-util.h"
#include "path-util.h"
#include "process-util.h"
diff --git a/src/machine/machinectl.c b/src/machine/machinectl.c
index 8b97b4d8ce..78f5f2ff32 100644
--- a/src/machine/machinectl.c
+++ b/src/machine/machinectl.c
@@ -44,13 +44,13 @@
#include "rlimit-util.h"
#include "sigbus.h"
#include "signal-util.h"
+#include "sort-util.h"
#include "spawn-polkit-agent.h"
#include "stdio-util.h"
#include "string-table.h"
#include "strv.h"
#include "terminal-util.h"
#include "unit-name.h"
-#include "util.h"
#include "verbs.h"
#include "web-util.h"
diff --git a/src/mount/mount-tool.c b/src/mount/mount-tool.c
index bbbc91c38e..1fc8c954d9 100644
--- a/src/mount/mount-tool.c
+++ b/src/mount/mount-tool.c
@@ -22,13 +22,14 @@
#include "parse-util.h"
#include "path-util.h"
#include "pretty-print.h"
+#include "sort-util.h"
#include "spawn-polkit-agent.h"
#include "stat-util.h"
#include "strv.h"
+#include "terminal-util.h"
#include "unit-def.h"
#include "unit-name.h"
#include "user-util.h"
-#include "terminal-util.h"
enum {
ACTION_DEFAULT,
diff --git a/src/network/netdev/wireguard.c b/src/network/netdev/wireguard.c
index eefb543c4d..b1b2ae1bb9 100644
--- a/src/network/netdev/wireguard.c
+++ b/src/network/netdev/wireguard.c
@@ -13,6 +13,7 @@
#include "fd-util.h"
#include "fileio.h"
#include "hexdecoct.h"
+#include "memory-util.h"
#include "netlink-util.h"
#include "networkd-link.h"
#include "networkd-manager.h"
@@ -22,8 +23,8 @@
#include "resolve-private.h"
#include "string-util.h"
#include "strv.h"
-#include "wireguard.h"
#include "wireguard-netlink.h"
+#include "wireguard.h"
static void resolve_endpoints(NetDev *netdev);
diff --git a/src/network/networkctl.c b/src/network/networkctl.c
index 9452dabcde..86f6435e4f 100644
--- a/src/network/networkctl.c
+++ b/src/network/networkctl.c
@@ -26,6 +26,7 @@
#include "parse-util.h"
#include "pretty-print.h"
#include "socket-util.h"
+#include "sort-util.h"
#include "sparse-endian.h"
#include "stdio-util.h"
#include "string-table.h"
@@ -33,7 +34,6 @@
#include "strv.h"
#include "strxcpyx.h"
#include "terminal-util.h"
-#include "util.h"
#include "verbs.h"
static PagerFlags arg_pager_flags = 0;
diff --git a/src/network/networkd-address.c b/src/network/networkd-address.c
index 1a9ba4223c..42d61cc0e5 100644
--- a/src/network/networkd-address.c
+++ b/src/network/networkd-address.c
@@ -5,6 +5,7 @@
#include "alloc-util.h"
#include "conf-parser.h"
#include "firewall-util.h"
+#include "memory-util.h"
#include "missing_network.h"
#include "netlink-util.h"
#include "networkd-address.h"
@@ -15,7 +16,6 @@
#include "string-util.h"
#include "strv.h"
#include "utf8.h"
-#include "util.h"
#define ADDRESSES_PER_LINK_MAX 2048U
#define STATIC_ADDRESSES_PER_NETWORK_MAX 1024U
diff --git a/src/nspawn/nspawn-mount.c b/src/nspawn/nspawn-mount.c
index 13f50b2d37..12f557ee99 100644
--- a/src/nspawn/nspawn-mount.c
+++ b/src/nspawn/nspawn-mount.c
@@ -16,12 +16,12 @@
#include "path-util.h"
#include "rm-rf.h"
#include "set.h"
+#include "sort-util.h"
#include "stat-util.h"
#include "string-util.h"
#include "strv.h"
#include "tmpfile-util.h"
#include "user-util.h"
-#include "util.h"
CustomMount* custom_mount_add(CustomMount **l, size_t *n, CustomMountType t) {
CustomMount *c, *ret;
diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c
index 64a30a5b9a..c6cc82b12f 100644
--- a/src/nspawn/nspawn.c
+++ b/src/nspawn/nspawn.c
@@ -61,6 +61,7 @@
#include "mkdir.h"
#include "mount-util.h"
#include "mountpoint-util.h"
+#include "namespace-util.h"
#include "netlink-util.h"
#include "nspawn-cgroup.h"
#include "nspawn-def.h"
diff --git a/src/nss-mymachines/nss-mymachines.c b/src/nss-mymachines/nss-mymachines.c
index 486a658958..da003e6aa7 100644
--- a/src/nss-mymachines/nss-mymachines.c
+++ b/src/nss-mymachines/nss-mymachines.c
@@ -12,11 +12,11 @@
#include "hostname-util.h"
#include "in-addr-util.h"
#include "macro.h"
+#include "memory-util.h"
#include "nss-util.h"
#include "signal-util.h"
#include "string-util.h"
#include "user-util.h"
-#include "util.h"
NSS_GETHOSTBYNAME_PROTOTYPES(mymachines);
NSS_GETPW_PROTOTYPES(mymachines);
diff --git a/src/portable/portable.c b/src/portable/portable.c
index 01fd1a94a0..9d0d21c115 100644
--- a/src/portable/portable.c
+++ b/src/portable/portable.c
@@ -24,6 +24,7 @@
#include "set.h"
#include "signal-util.h"
#include "socket-util.h"
+#include "sort-util.h"
#include "string-table.h"
#include "strv.h"
#include "tmpfile-util.h"
diff --git a/src/resolve/resolved-bus.c b/src/resolve/resolved-bus.c
index 5b547ba7ee..4fc281e80a 100644
--- a/src/resolve/resolved-bus.c
+++ b/src/resolve/resolved-bus.c
@@ -4,12 +4,13 @@
#include "bus-common-errors.h"
#include "bus-util.h"
#include "dns-domain.h"
+#include "memory-util.h"
#include "missing_capability.h"
#include "resolved-bus.h"
#include "resolved-def.h"
#include "resolved-dns-synthesize.h"
-#include "resolved-dnssd.h"
#include "resolved-dnssd-bus.h"
+#include "resolved-dnssd.h"
#include "resolved-link-bus.h"
#include "user-util.h"
#include "utf8.h"
diff --git a/src/resolve/resolved-dns-dnssec.c b/src/resolve/resolved-dns-dnssec.c
index 3be18de841..a5ded5ada2 100644
--- a/src/resolve/resolved-dns-dnssec.c
+++ b/src/resolve/resolved-dns-dnssec.c
@@ -12,8 +12,10 @@
#include "fileio.h"
#include "gcrypt-util.h"
#include "hexdecoct.h"
+#include "memory-util.h"
#include "resolved-dns-dnssec.h"
#include "resolved-dns-packet.h"
+#include "sort-util.h"
#include "string-table.h"
#define VERIFY_RRS_MAX 256
diff --git a/src/resolve/resolved-dns-packet.c b/src/resolve/resolved-dns-packet.c
index 278546da84..a4959cd7c0 100644
--- a/src/resolve/resolved-dns-packet.c
+++ b/src/resolve/resolved-dns-packet.c
@@ -6,6 +6,7 @@
#include "alloc-util.h"
#include "dns-domain.h"
+#include "memory-util.h"
#include "resolved-dns-packet.h"
#include "set.h"
#include "string-table.h"
diff --git a/src/resolve/resolved-dns-trust-anchor.c b/src/resolve/resolved-dns-trust-anchor.c
index c5ec93b724..e5a27ca688 100644
--- a/src/resolve/resolved-dns-trust-anchor.c
+++ b/src/resolve/resolved-dns-trust-anchor.c
@@ -10,9 +10,10 @@
#include "fileio.h"
#include "hexdecoct.h"
#include "parse-util.h"
-#include "resolved-dns-trust-anchor.h"
#include "resolved-dns-dnssec.h"
+#include "resolved-dns-trust-anchor.h"
#include "set.h"
+#include "sort-util.h"
#include "string-util.h"
#include "strv.h"
diff --git a/src/resolve/resolved-mdns.c b/src/resolve/resolved-mdns.c
index 89c2497d32..67080cb01c 100644
--- a/src/resolve/resolved-mdns.c
+++ b/src/resolve/resolved-mdns.c
@@ -8,6 +8,7 @@
#include "fd-util.h"
#include "resolved-manager.h"
#include "resolved-mdns.h"
+#include "sort-util.h"
#define CLEAR_CACHE_FLUSH(x) (~MDNS_RR_CACHE_FLUSH & (x))
diff --git a/src/shared/ask-password-api.c b/src/shared/ask-password-api.c
index a3d61d5b9a..4122362b3c 100644
--- a/src/shared/ask-password-api.c
+++ b/src/shared/ask-password-api.c
@@ -31,6 +31,7 @@
#include "io-util.h"
#include "log.h"
#include "macro.h"
+#include "memory-util.h"
#include "missing.h"
#include "mkdir.h"
#include "process-util.h"
@@ -44,7 +45,6 @@
#include "tmpfile-util.h"
#include "umask-util.h"
#include "utf8.h"
-#include "util.h"
#define KEYRING_TIMEOUT_USEC ((5 * USEC_PER_MINUTE) / 2)
diff --git a/src/shared/bitmap.c b/src/shared/bitmap.c
index a4cd6451b0..a956a42cab 100644
--- a/src/shared/bitmap.c
+++ b/src/shared/bitmap.c
@@ -10,6 +10,7 @@
#include "bitmap.h"
#include "hashmap.h"
#include "macro.h"
+#include "memory-util.h"
struct Bitmap {
uint64_t *bitmaps;
diff --git a/src/shared/bootspec.c b/src/shared/bootspec.c
index 64b2574a18..f2b919b6bb 100644
--- a/src/shared/bootspec.c
+++ b/src/shared/bootspec.c
@@ -21,6 +21,7 @@
#include "parse-util.h"
#include "path-util.h"
#include "pe-header.h"
+#include "sort-util.h"
#include "stat-util.h"
#include "string-table.h"
#include "string-util.h"
diff --git a/src/shared/bpf-program.c b/src/shared/bpf-program.c
index 2c61e04132..40bc9645be 100644
--- a/src/shared/bpf-program.c
+++ b/src/shared/bpf-program.c
@@ -9,9 +9,9 @@
#include "bpf-program.h"
#include "fd-util.h"
#include "log.h"
+#include "memory-util.h"
#include "missing.h"
#include "path-util.h"
-#include "util.h"
int bpf_program_new(uint32_t prog_type, BPFProgram **ret) {
_cleanup_(bpf_program_unrefp) BPFProgram *p = NULL;
diff --git a/src/shared/bus-unit-util.c b/src/shared/bus-unit-util.c
index 3ea1bd29c9..d0bfd1894f 100644
--- a/src/shared/bus-unit-util.c
+++ b/src/shared/bus-unit-util.c
@@ -27,13 +27,13 @@
#include "rlimit-util.h"
#include "securebits-util.h"
#include "signal-util.h"
+#include "sort-util.h"
#include "string-util.h"
#include "syslog-util.h"
#include "terminal-util.h"
#include "unit-def.h"
#include "user-util.h"
#include "utf8.h"
-#include "util.h"
int bus_parse_unit_info(sd_bus_message *message, UnitInfo *u) {
assert(message);
diff --git a/src/shared/calendarspec.c b/src/shared/calendarspec.c
index b2285cebdc..e7ee90aa9c 100644
--- a/src/shared/calendarspec.c
+++ b/src/shared/calendarspec.c
@@ -18,6 +18,7 @@
#include "macro.h"
#include "parse-util.h"
#include "process-util.h"
+#include "sort-util.h"
#include "string-util.h"
#include "time-util.h"
diff --git a/src/shared/cgroup-show.c b/src/shared/cgroup-show.c
index 61df7511a1..91a243944c 100644
--- a/src/shared/cgroup-show.c
+++ b/src/shared/cgroup-show.c
@@ -20,6 +20,7 @@
#include "output-mode.h"
#include "path-util.h"
#include "process-util.h"
+#include "sort-util.h"
#include "string-util.h"
#include "terminal-util.h"
#include "unit-name.h"
diff --git a/src/shared/efivars.c b/src/shared/efivars.c
index 04c0a697b4..4f32163bba 100644
--- a/src/shared/efivars.c
+++ b/src/shared/efivars.c
@@ -21,11 +21,11 @@
#include "io-util.h"
#include "macro.h"
#include "parse-util.h"
+#include "sort-util.h"
#include "stdio-util.h"
#include "strv.h"
#include "time-util.h"
#include "utf8.h"
-#include "util.h"
#include "virt.h"
#if ENABLE_EFI
diff --git a/src/shared/format-table.c b/src/shared/format-table.c
index 7d529801a1..4e8fc808cd 100644
--- a/src/shared/format-table.c
+++ b/src/shared/format-table.c
@@ -8,14 +8,15 @@
#include "fileio.h"
#include "format-table.h"
#include "gunicode.h"
+#include "memory-util.h"
#include "pager.h"
#include "parse-util.h"
#include "pretty-print.h"
+#include "sort-util.h"
#include "string-util.h"
#include "terminal-util.h"
#include "time-util.h"
#include "utf8.h"
-#include "util.h"
#define DEFAULT_WEIGHT 100
diff --git a/src/shared/json.c b/src/shared/json.c
index 2a858d4cc4..55163a733b 100644
--- a/src/shared/json.c
+++ b/src/shared/json.c
@@ -18,6 +18,7 @@
#include "json-internal.h"
#include "json.h"
#include "macro.h"
+#include "memory-util.h"
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
diff --git a/src/shared/logs-show.c b/src/shared/logs-show.c
index 15ef0f19ff..5fb736f633 100644
--- a/src/shared/logs-show.c
+++ b/src/shared/logs-show.c
@@ -25,6 +25,7 @@
#include "log.h"
#include "logs-show.h"
#include "macro.h"
+#include "namespace-util.h"
#include "output-mode.h"
#include "parse-util.h"
#include "process-util.h"
diff --git a/src/shared/uid-range.c b/src/shared/uid-range.c
index 5fa7bd277e..7cb7d8a477 100644
--- a/src/shared/uid-range.c
+++ b/src/shared/uid-range.c
@@ -6,9 +6,9 @@
#include "alloc-util.h"
#include "macro.h"
+#include "sort-util.h"
#include "uid-range.h"
#include "user-util.h"
-#include "util.h"
static bool uid_range_intersect(UidRange *range, uid_t start, uid_t nr) {
assert(range);
diff --git a/src/shared/utmp-wtmp.c b/src/shared/utmp-wtmp.c
index ef9427fa7b..4b134b6c8a 100644
--- a/src/shared/utmp-wtmp.c
+++ b/src/shared/utmp-wtmp.c
@@ -16,12 +16,12 @@
#include "fd-util.h"
#include "hostname-util.h"
#include "macro.h"
+#include "memory-util.h"
#include "path-util.h"
#include "string-util.h"
#include "terminal-util.h"
#include "time-util.h"
#include "user-util.h"
-#include "util.h"
#include "utmp-wtmp.h"
int utmp_get_runlevel(int *runlevel, int *previous) {
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index 38897ecefb..7f6196328b 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -40,9 +40,9 @@
#include "glob-util.h"
#include "hexdecoct.h"
#include "hostname-util.h"
+#include "in-addr-util.h"
#include "initreq.h"
#include "install.h"
-#include "in-addr-util.h"
#include "io-util.h"
#include "journal-util.h"
#include "list.h"
@@ -51,6 +51,7 @@
#include "logs-show.h"
#include "macro.h"
#include "main-func.h"
+#include "memory-util.h"
#include "mkdir.h"
#include "pager.h"
#include "parse-util.h"
@@ -65,6 +66,7 @@
#include "sigbus.h"
#include "signal-util.h"
#include "socket-util.h"
+#include "sort-util.h"
#include "spawn-ask-password-agent.h"
#include "spawn-polkit-agent.h"
#include "special.h"
@@ -77,7 +79,6 @@
#include "unit-name.h"
#include "user-util.h"
#include "utf8.h"
-#include "util.h"
#include "utmp-wtmp.h"
#include "verbs.h"
#include "virt.h"
diff --git a/src/test/test-alloc-util.c b/src/test/test-alloc-util.c
index 40c32d7114..0f2897a376 100644
--- a/src/test/test-alloc-util.c
+++ b/src/test/test-alloc-util.c
@@ -4,7 +4,7 @@
#include "alloc-util.h"
#include "macro.h"
-#include "util.h"
+#include "memory-util.h"
static void test_alloca(void) {
static const uint8_t zero[997] = { };
diff --git a/src/test/test-fd-util.c b/src/test/test-fd-util.c
index 7a0a2ad7d8..713795814d 100644
--- a/src/test/test-fd-util.c
+++ b/src/test/test-fd-util.c
@@ -7,6 +7,7 @@
#include "fd-util.h"
#include "fileio.h"
#include "macro.h"
+#include "memory-util.h"
#include "path-util.h"
#include "process-util.h"
#include "random-util.h"
@@ -14,7 +15,6 @@
#include "string-util.h"
#include "tests.h"
#include "tmpfile-util.h"
-#include "util.h"
static void test_close_many(void) {
int fds[3];
diff --git a/src/test/test-prioq.c b/src/test/test-prioq.c
index 53c9e090a7..21d5b44fbb 100644
--- a/src/test/test-prioq.c
+++ b/src/test/test-prioq.c
@@ -6,7 +6,7 @@
#include "prioq.h"
#include "set.h"
#include "siphash24.h"
-#include "util.h"
+#include "sort-util.h"
#define SET_SIZE 1024*4
diff --git a/src/test/test-seccomp.c b/src/test/test-seccomp.c
index fbfeedd536..65f1aae5a7 100644
--- a/src/test/test-seccomp.c
+++ b/src/test/test-seccomp.c
@@ -13,6 +13,7 @@
#include "alloc-util.h"
#include "fd-util.h"
#include "macro.h"
+#include "memory-util.h"
#include "missing.h"
#include "nsflags.h"
#include "process-util.h"
@@ -21,7 +22,6 @@
#include "set.h"
#include "string-util.h"
#include "tests.h"
-#include "util.h"
#include "virt.h"
#if SCMP_SYS(socket) < 0 || defined(__i386__) || defined(__s390x__) || defined(__s390__)
diff --git a/src/test/test-sigbus.c b/src/test/test-sigbus.c
index d2666dd1d8..f753e3836e 100644
--- a/src/test/test-sigbus.c
+++ b/src/test/test-sigbus.c
@@ -1,15 +1,17 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
+#include <fcntl.h>
#include <sys/mman.h>
+#include <unistd.h>
#if HAVE_VALGRIND_VALGRIND_H
# include <valgrind/valgrind.h>
#endif
#include "fd-util.h"
+#include "memory-util.h"
#include "sigbus.h"
#include "tests.h"
-#include "util.h"
int main(int argc, char *argv[]) {
_cleanup_close_ int fd = -1;
diff --git a/src/test/test-siphash24.c b/src/test/test-siphash24.c
index 218200e480..6ae272fa94 100644
--- a/src/test/test-siphash24.c
+++ b/src/test/test-siphash24.c
@@ -1,7 +1,7 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
+#include "memory-util.h"
#include "siphash24.h"
-#include "util.h"
#define ITERATIONS 10000000ULL
diff --git a/src/test/test-sleep.c b/src/test/test-sleep.c
index 2a6d5e765a..618da6e40d 100644
--- a/src/test/test-sleep.c
+++ b/src/test/test-sleep.c
@@ -6,10 +6,10 @@
#include "fd-util.h"
#include "log.h"
+#include "memory-util.h"
#include "sleep-config.h"
#include "strv.h"
#include "tests.h"
-#include "util.h"
static void test_parse_sleep_config(void) {
const char *verb;
diff --git a/src/test/test-stat-util.c b/src/test/test-stat-util.c
index 0e2155e911..e3d89d6e26 100644
--- a/src/test/test-stat-util.c
+++ b/src/test/test-stat-util.c
@@ -9,6 +9,7 @@
#include "macro.h"
#include "missing.h"
#include "mountpoint-util.h"
+#include "namespace-util.h"
#include "path-util.h"
#include "stat-util.h"
#include "tmpfile-util.h"
diff --git a/src/test/test-unaligned.c b/src/test/test-unaligned.c
index 96d6b22be3..3d6f9a03b4 100644
--- a/src/test/test-unaligned.c
+++ b/src/test/test-unaligned.c
@@ -1,8 +1,8 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
+#include "memory-util.h"
#include "sparse-endian.h"
#include "unaligned.h"
-#include "util.h"
static uint8_t data[] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
diff --git a/src/test/test-unit-file.c b/src/test/test-unit-file.c
index f5578f9fc2..6b01529068 100644
--- a/src/test/test-unit-file.c
+++ b/src/test/test-unit-file.c
@@ -20,6 +20,7 @@
#include "install.h"
#include "load-fragment.h"
#include "macro.h"
+#include "memory-util.h"
#include "rm-rf.h"
#include "specifier.h"
#include "string-util.h"
@@ -28,7 +29,6 @@
#include "tests.h"
#include "tmpfile-util.h"
#include "user-util.h"
-#include "util.h"
static int test_unit_file_get_set(void) {
int r;
diff --git a/src/test/test-util.c b/src/test/test-util.c
index ffacd65669..1920cbc84a 100644
--- a/src/test/test-util.c
+++ b/src/test/test-util.c
@@ -8,6 +8,7 @@
#include "def.h"
#include "fileio.h"
#include "fs-util.h"
+#include "limits-util.h"
#include "missing_syscall.h"
#include "parse-util.h"
#include "process-util.h"
@@ -15,7 +16,7 @@
#include "rm-rf.h"
#include "string-util.h"
#include "tests.h"
-#include "util.h"
+#include "memory-util.h"
static void test_align_power2(void) {
unsigned long i, p2;
diff --git a/src/timedate/timedated.c b/src/timedate/timedated.c
index 324d4a41c7..b3f44bfaab 100644
--- a/src/timedate/timedated.c
+++ b/src/timedate/timedated.c
@@ -20,6 +20,7 @@
#include "hashmap.h"
#include "list.h"
#include "main-func.h"
+#include "memory-util.h"
#include "missing_capability.h"
#include "path-util.h"
#include "selinux-util.h"
@@ -29,7 +30,6 @@
#include "unit-def.h"
#include "unit-name.h"
#include "user-util.h"
-#include "util.h"
#define NULL_ADJTIME_UTC "0.0 0 0\n0\nUTC\n"
#define NULL_ADJTIME_LOCAL "0.0 0 0\n0\nLOCAL\n"
diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c
index 801e79b01d..ad73600241 100644
--- a/src/tmpfiles/tmpfiles.c
+++ b/src/tmpfiles/tmpfiles.c
@@ -53,6 +53,7 @@
#include "rm-rf.h"
#include "selinux-util.h"
#include "set.h"
+#include "sort-util.h"
#include "specifier.h"
#include "stat-util.h"
#include "stdio-util.h"
@@ -61,7 +62,6 @@
#include "strv.h"
#include "umask-util.h"
#include "user-util.h"
-#include "util.h"
/* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates
* them in the file system. This is intended to be used to create
diff --git a/src/tty-ask-password-agent/tty-ask-password-agent.c b/src/tty-ask-password-agent/tty-ask-password-agent.c
index fc165ffc01..d974d4451d 100644
--- a/src/tty-ask-password-agent/tty-ask-password-agent.c
+++ b/src/tty-ask-password-agent/tty-ask-password-agent.c
@@ -31,6 +31,7 @@
#include "io-util.h"
#include "macro.h"
#include "main-func.h"
+#include "memory-util.h"
#include "mkdir.h"
#include "path-util.h"
#include "pretty-print.h"
@@ -40,7 +41,6 @@
#include "string-util.h"
#include "strv.h"
#include "terminal-util.h"
-#include "util.h"
#include "utmp-wtmp.h"
static enum {
diff --git a/src/udev/ata_id/ata_id.c b/src/udev/ata_id/ata_id.c
index 6c2233e430..8ea2e1e327 100644
--- a/src/udev/ata_id/ata_id.c
+++ b/src/udev/ata_id/ata_id.c
@@ -26,8 +26,8 @@
#include "fd-util.h"
#include "libudev-util.h"
#include "log.h"
+#include "memory-util.h"
#include "udev-util.h"
-#include "util.h"
#define COMMAND_TIMEOUT_MSEC (30 * 1000)
diff --git a/src/udev/cdrom_id/cdrom_id.c b/src/udev/cdrom_id/cdrom_id.c
index aed24a3b9a..214fbaa042 100644
--- a/src/udev/cdrom_id/cdrom_id.c
+++ b/src/udev/cdrom_id/cdrom_id.c
@@ -21,9 +21,9 @@
#include <unistd.h>
#include "log.h"
+#include "memory-util.h"
#include "random-util.h"
#include "udev-util.h"
-#include "util.h"
/* device info */
static unsigned cd_cd_rom;
diff --git a/src/udev/net/ethtool-util.c b/src/udev/net/ethtool-util.c
index 55620926dc..c94977e7be 100644
--- a/src/udev/net/ethtool-util.c
+++ b/src/udev/net/ethtool-util.c
@@ -9,11 +9,11 @@
#include "ethtool-util.h"
#include "link-config.h"
#include "log.h"
+#include "memory-util.h"
#include "missing.h"
#include "socket-util.h"
#include "string-table.h"
#include "strxcpyx.h"
-#include "util.h"
static const char* const duplex_table[_DUP_MAX] = {
[DUP_FULL] = "full",
diff --git a/src/udev/net/link-config.c b/src/udev/net/link-config.c
index f1d36ccac0..438ff473ae 100644
--- a/src/udev/net/link-config.c
+++ b/src/udev/net/link-config.c
@@ -14,6 +14,7 @@
#include "fd-util.h"
#include "link-config.h"
#include "log.h"
+#include "memory-util.h"
#include "missing_network.h"
#include "naming-scheme.h"
#include "netlink-util.h"
@@ -26,7 +27,6 @@
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
-#include "util.h"
struct link_config_ctx {
LIST_HEAD(link_config, links);
diff --git a/src/udev/scsi_id/scsi_serial.c b/src/udev/scsi_id/scsi_serial.c
index c67d047475..7ca01858d1 100644
--- a/src/udev/scsi_id/scsi_serial.c
+++ b/src/udev/scsi_id/scsi_serial.c
@@ -21,11 +21,11 @@
#include <time.h>
#include <unistd.h>
+#include "memory-util.h"
#include "random-util.h"
#include "scsi.h"
#include "scsi_id.h"
#include "string-util.h"
-#include "util.h"
/*
* A priority based list of id, naa, and binary/ascii for the identifier
diff --git a/src/udev/udevd.c b/src/udev/udevd.c
index 0da82dca55..eab2b46250 100644
--- a/src/udev/udevd.c
+++ b/src/udev/udevd.c
@@ -46,6 +46,7 @@
#include "hashmap.h"
#include "io-util.h"
#include "libudev-device-internal.h"
+#include "limits-util.h"
#include "list.h"
#include "main-func.h"
#include "mkdir.h"