From b75c6c3c3fca9e64e8a002fb5e12c00241660abd Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Mon, 5 May 2025 17:00:24 +0200 Subject: [PATCH 1/5] meson: Make sure check-filesystems.sh runs from the build directory run_command()'s working directory is documented as undefined but it seems to be the current source directory as when the -ftime-trace clang option is enabled, -.json is written to src/basic/meson.build every time meson is run. Let's make sure the command is executed in the build directory so that any auxiliary files are written there as well. --- src/basic/meson.build | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/basic/meson.build b/src/basic/meson.build index a99f198a97..7cc29bd039 100644 --- a/src/basic/meson.build +++ b/src/basic/meson.build @@ -244,7 +244,13 @@ filesystem_includes = files( ) check_filesystems = find_program('check-filesystems.sh') -r = run_command([check_filesystems, cpp, files('filesystems-gperf.gperf')] + filesystem_includes, check: false) +r = run_command( + [ + 'env', '--chdir', meson.project_build_root(), + check_filesystems, cpp, files('filesystems-gperf.gperf') + ] + filesystem_includes, + check: false, +) if r.returncode() != 0 warning('Unknown filesystems defined in kernel headers:\n\n' + r.stdout()) endif From 76d62b63efbcf3eadb5d6579b1d4d7baa74bb0a4 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Mon, 5 May 2025 22:06:17 +0200 Subject: [PATCH 2/5] main-func: Reduce transitive includes Let's move some logic from _DEFINE_MAIN_FUNCTION() and other places in main-func.h into functions that we implement in main-func.c to allow moving some included headers from the header to the .c file. --- src/ac-power/ac-power.c | 1 + src/backlight/backlight.c | 1 + src/busctl/busctl.c | 1 + src/creds/creds.c | 1 + src/cryptsetup/cryptsetup.c | 1 + src/dissect/dissect.c | 1 + src/fstab-generator/fstab-generator.c | 2 ++ src/growfs/makefs.c | 1 + src/home/homework.c | 2 ++ src/hostname/hostnamed.c | 1 + src/import/importd.c | 1 + src/integritysetup/integritysetup.c | 1 + src/kernel-install/kernel-install.c | 1 + src/locale/localed.c | 1 + src/login/inhibit.c | 1 + src/login/user-runtime-dir.c | 1 + src/machine/machinectl.c | 1 + src/machine/machined.c | 1 + src/measure/measure.c | 1 + src/modules-load/modules-load.c | 1 + src/mount/mount-tool.c | 1 + src/notify/notify.c | 1 + src/nspawn/nspawn.c | 4 +++ src/nsresourced/nsresourcework.c | 1 + src/oom/oomctl.c | 1 + src/path/path.c | 1 + src/portable/portabled.c | 1 + src/ptyfwd/ptyfwd-tool.c | 1 + src/resolve/resolvectl.c | 4 +++ src/rfkill/rfkill.c | 1 + src/run/run.c | 2 ++ src/shared/generator.c | 1 + src/shared/main-func.c | 50 +++++++++++++++++++++++++++ src/shared/main-func.h | 48 ++++--------------------- src/shared/meson.build | 1 + src/sysext/sysext.c | 1 + src/systemctl/systemctl.c | 1 + src/sysupdate/sysupdate.c | 2 ++ src/sysupdate/updatectl.c | 1 + src/test/test-display-quota.c | 1 + src/timedate/timedated.c | 1 + src/timesync/wait-sync.c | 1 + src/udev/udevadm.c | 1 + src/userdb/userwork.c | 2 ++ src/varlinkctl/varlinkctl.c | 1 + src/veritysetup/veritysetup.c | 1 + src/vmspawn/vmspawn.c | 1 + src/volatile-root/volatile-root.c | 1 + 48 files changed, 114 insertions(+), 41 deletions(-) create mode 100644 src/shared/main-func.c diff --git a/src/ac-power/ac-power.c b/src/ac-power/ac-power.c index 6bb3eb1016..06f999dc80 100644 --- a/src/ac-power/ac-power.c +++ b/src/ac-power/ac-power.c @@ -8,6 +8,7 @@ #include "log.h" #include "main-func.h" #include "pretty-print.h" +#include "string-util.h" static bool arg_verbose = false; diff --git a/src/backlight/backlight.c b/src/backlight/backlight.c index 018cd35321..a39398a638 100644 --- a/src/backlight/backlight.c +++ b/src/backlight/backlight.c @@ -7,6 +7,7 @@ #include "sd-device.h" #include "alloc-util.h" +#include "argv-util.h" #include "device-util.h" #include "escape.h" #include "fileio.h" diff --git a/src/busctl/busctl.c b/src/busctl/busctl.c index d672000e55..8091a44705 100644 --- a/src/busctl/busctl.c +++ b/src/busctl/busctl.c @@ -3,6 +3,7 @@ #include #include "sd-bus.h" +#include "sd-daemon.h" #include "sd-json.h" #include "alloc-util.h" diff --git a/src/creds/creds.c b/src/creds/creds.c index 5caa45062e..7c2e49da4f 100644 --- a/src/creds/creds.c +++ b/src/creds/creds.c @@ -23,6 +23,7 @@ #include "missing_magic.h" #include "pager.h" #include "parse-argument.h" +#include "polkit-agent.h" #include "pretty-print.h" #include "process-util.h" #include "stat-util.h" diff --git a/src/cryptsetup/cryptsetup.c b/src/cryptsetup/cryptsetup.c index e1eadde498..c591cbeeeb 100644 --- a/src/cryptsetup/cryptsetup.c +++ b/src/cryptsetup/cryptsetup.c @@ -13,6 +13,7 @@ #include "sd-messages.h" #include "alloc-util.h" +#include "argv-util.h" #include "ask-password-api.h" #include "build.h" #include "cryptsetup-fido2.h" diff --git a/src/dissect/dissect.c b/src/dissect/dissect.c index 55232914ed..6ac292f140 100644 --- a/src/dissect/dissect.c +++ b/src/dissect/dissect.c @@ -11,6 +11,7 @@ #include "sd-device.h" #include "architecture.h" +#include "argv-util.h" #include "blockdev-util.h" #include "build.h" #include "chase.h" diff --git a/src/fstab-generator/fstab-generator.c b/src/fstab-generator/fstab-generator.c index 6f80289881..31c4c485c0 100644 --- a/src/fstab-generator/fstab-generator.c +++ b/src/fstab-generator/fstab-generator.c @@ -5,9 +5,11 @@ #include #include "alloc-util.h" +#include "argv-util.h" #include "bus-error.h" #include "bus-locator.h" #include "bus-unit-util.h" +#include "bus-util.h" #include "chase.h" #include "creds-util.h" #include "efi-loader.h" diff --git a/src/growfs/makefs.c b/src/growfs/makefs.c index c76e81d976..1e404bc7bf 100644 --- a/src/growfs/makefs.c +++ b/src/growfs/makefs.c @@ -11,6 +11,7 @@ #include "blockdev-util.h" #include "dissect-image.h" #include "fd-util.h" +#include "log.h" #include "main-func.h" #include "mkfs-util.h" #include "path-util.h" diff --git a/src/home/homework.c b/src/home/homework.c index 93a4e8dc28..8af0847c78 100644 --- a/src/home/homework.c +++ b/src/home/homework.c @@ -3,6 +3,8 @@ #include #include +#include "sd-daemon.h" + #include "blockdev-util.h" #include "bus-unit-util.h" #include "chown-recursive.h" diff --git a/src/hostname/hostnamed.c b/src/hostname/hostnamed.c index 962c5681e4..1bfd14b0d8 100644 --- a/src/hostname/hostnamed.c +++ b/src/hostname/hostnamed.c @@ -15,6 +15,7 @@ #include "bus-get-properties.h" #include "bus-log-control-api.h" #include "bus-polkit.h" +#include "bus-util.h" #include "constants.h" #include "daemon-util.h" #include "device-private.h" diff --git a/src/import/importd.c b/src/import/importd.c index a7de9ed247..6390b76d54 100644 --- a/src/import/importd.c +++ b/src/import/importd.c @@ -12,6 +12,7 @@ #include "bus-get-properties.h" #include "bus-log-control-api.h" #include "bus-polkit.h" +#include "bus-util.h" #include "common-signal.h" #include "constants.h" #include "daemon-util.h" diff --git a/src/integritysetup/integritysetup.c b/src/integritysetup/integritysetup.c index 56c02d4ad8..ece8c3c1a8 100644 --- a/src/integritysetup/integritysetup.c +++ b/src/integritysetup/integritysetup.c @@ -5,6 +5,7 @@ #include #include "alloc-util.h" +#include "argv-util.h" #include "cryptsetup-util.h" #include "fileio.h" #include "hexdecoct.h" diff --git a/src/kernel-install/kernel-install.c b/src/kernel-install/kernel-install.c index d4e65f5208..0a62d994bc 100644 --- a/src/kernel-install/kernel-install.c +++ b/src/kernel-install/kernel-install.c @@ -4,6 +4,7 @@ #include #include +#include "argv-util.h" #include "boot-entry.h" #include "build.h" #include "chase.h" diff --git a/src/locale/localed.c b/src/locale/localed.c index 3f742b8a62..24ec6263e5 100644 --- a/src/locale/localed.c +++ b/src/locale/localed.c @@ -14,6 +14,7 @@ #include "bus-message.h" #include "bus-polkit.h" #include "bus-unit-util.h" +#include "bus-util.h" #include "constants.h" #include "daemon-util.h" #include "kbd-util.h" diff --git a/src/login/inhibit.c b/src/login/inhibit.c index 276a3ba70d..df68eb7ef0 100644 --- a/src/login/inhibit.c +++ b/src/login/inhibit.c @@ -19,6 +19,7 @@ #include "log.h" #include "main-func.h" #include "pager.h" +#include "polkit-agent.h" #include "pretty-print.h" #include "process-util.h" #include "signal-util.h" diff --git a/src/login/user-runtime-dir.c b/src/login/user-runtime-dir.c index 451e181cd8..0b3e977d5c 100644 --- a/src/login/user-runtime-dir.c +++ b/src/login/user-runtime-dir.c @@ -14,6 +14,7 @@ #include "fs-util.h" #include "label-util.h" #include "limits-util.h" +#include "log.h" #include "main-func.h" #include "missing_magic.h" #include "missing_syscall.h" diff --git a/src/machine/machinectl.c b/src/machine/machinectl.c index 5a6052549d..4cd670748e 100644 --- a/src/machine/machinectl.c +++ b/src/machine/machinectl.c @@ -38,6 +38,7 @@ #include "hostname-util.h" #include "import-util.h" #include "in-addr-util.h" +#include "label-util.h" #include "locale-util.h" #include "log.h" #include "logs-show.h" diff --git a/src/machine/machined.c b/src/machine/machined.c index 364f9dfab6..658a7e4f20 100644 --- a/src/machine/machined.c +++ b/src/machine/machined.c @@ -11,6 +11,7 @@ #include "bus-locator.h" #include "bus-log-control-api.h" #include "bus-polkit.h" +#include "bus-util.h" #include "cgroup-util.h" #include "common-signal.h" #include "daemon-util.h" diff --git a/src/measure/measure.c b/src/measure/measure.c index b81bc2453c..fb18b1a9f7 100644 --- a/src/measure/measure.c +++ b/src/measure/measure.c @@ -16,6 +16,7 @@ #include "main-func.h" #include "memstream-util.h" #include "openssl-util.h" +#include "pager.h" #include "parse-argument.h" #include "parse-util.h" #include "pe-binary.h" diff --git a/src/modules-load/modules-load.c b/src/modules-load/modules-load.c index 59daeb5cb0..27ecb23e9b 100644 --- a/src/modules-load/modules-load.c +++ b/src/modules-load/modules-load.c @@ -8,6 +8,7 @@ #include "build.h" #include "conf-files.h" #include "constants.h" +#include "errno-util.h" #include "fd-util.h" #include "fileio.h" #include "log.h" diff --git a/src/mount/mount-tool.c b/src/mount/mount-tool.c index bda1e3a3be..de90f9d983 100644 --- a/src/mount/mount-tool.c +++ b/src/mount/mount-tool.c @@ -5,6 +5,7 @@ #include "sd-bus.h" #include "sd-device.h" +#include "argv-util.h" #include "build.h" #include "bus-error.h" #include "bus-locator.h" diff --git a/src/notify/notify.c b/src/notify/notify.c index acc8bc72af..6f924d719c 100644 --- a/src/notify/notify.c +++ b/src/notify/notify.c @@ -24,6 +24,7 @@ #include "parse-util.h" #include "pretty-print.h" #include "process-util.h" +#include "signal-util.h" #include "socket-util.h" #include "string-util.h" #include "strv.h" diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index dd9649d71e..bda38db105 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -16,6 +16,10 @@ #include #include +#if HAVE_SELINUX +#include +#endif + #include "sd-bus.h" #include "sd-daemon.h" #include "sd-id128.h" diff --git a/src/nsresourced/nsresourcework.c b/src/nsresourced/nsresourcework.c index 26ea1d066e..b1c6dd86f9 100644 --- a/src/nsresourced/nsresourcework.c +++ b/src/nsresourced/nsresourcework.c @@ -15,6 +15,7 @@ #include "sd-netlink.h" #include "sd-varlink.h" +#include "argv-util.h" #include "bus-polkit.h" #include "env-util.h" #include "fd-util.h" diff --git a/src/oom/oomctl.c b/src/oom/oomctl.c index 79adc01415..3a2ac2a6aa 100644 --- a/src/oom/oomctl.c +++ b/src/oom/oomctl.c @@ -9,6 +9,7 @@ #include "bus-message-util.h" #include "log.h" #include "main-func.h" +#include "pager.h" #include "pretty-print.h" #include "terminal-util.h" #include "verbs.h" diff --git a/src/path/path.c b/src/path/path.c index 18a88e1c69..97685d2bd8 100644 --- a/src/path/path.c +++ b/src/path/path.c @@ -9,6 +9,7 @@ #include "alloc-util.h" #include "build.h" +#include "errno-util.h" #include "log.h" #include "macro.h" #include "main-func.h" diff --git a/src/portable/portabled.c b/src/portable/portabled.c index d94feb34cf..ddc9935a7d 100644 --- a/src/portable/portabled.c +++ b/src/portable/portabled.c @@ -8,6 +8,7 @@ #include "alloc-util.h" #include "bus-log-control-api.h" #include "bus-polkit.h" +#include "bus-util.h" #include "common-signal.h" #include "constants.h" #include "daemon-util.h" diff --git a/src/ptyfwd/ptyfwd-tool.c b/src/ptyfwd/ptyfwd-tool.c index 16a49ccdc1..449c0ee497 100644 --- a/src/ptyfwd/ptyfwd-tool.c +++ b/src/ptyfwd/ptyfwd-tool.c @@ -12,6 +12,7 @@ #include "pretty-print.h" #include "process-util.h" #include "ptyfwd.h" +#include "signal-util.h" #include "strv.h" static bool arg_quiet = false; diff --git a/src/resolve/resolvectl.c b/src/resolve/resolvectl.c index f517d149dd..7d284ab28e 100644 --- a/src/resolve/resolvectl.c +++ b/src/resolve/resolvectl.c @@ -5,18 +5,21 @@ #include #include "sd-bus.h" +#include "sd-daemon.h" #include "sd-json.h" #include "sd-netlink.h" #include "sd-varlink.h" #include "af-list.h" #include "alloc-util.h" +#include "argv-util.h" #include "build.h" #include "bus-common-errors.h" #include "bus-error.h" #include "bus-locator.h" #include "bus-map-properties.h" #include "bus-message-util.h" +#include "bus-util.h" #include "dns-domain.h" #include "errno-list.h" #include "escape.h" @@ -31,6 +34,7 @@ #include "pager.h" #include "parse-argument.h" #include "parse-util.h" +#include "polkit-agent.h" #include "pretty-print.h" #include "process-util.h" #include "resolvconf-compat.h" diff --git a/src/rfkill/rfkill.c b/src/rfkill/rfkill.c index 6b216c046f..28301cd527 100644 --- a/src/rfkill/rfkill.c +++ b/src/rfkill/rfkill.c @@ -12,6 +12,7 @@ #include "alloc-util.h" #include "device-util.h" +#include "errno-util.h" #include "escape.h" #include "fd-util.h" #include "fileio.h" diff --git a/src/run/run.c b/src/run/run.c index e2b6c98e03..d03604e4ee 100644 --- a/src/run/run.c +++ b/src/run/run.c @@ -7,10 +7,12 @@ #include #include "sd-bus.h" +#include "sd-daemon.h" #include "sd-event.h" #include "sd-json.h" #include "alloc-util.h" +#include "argv-util.h" #include "ask-password-agent.h" #include "build.h" #include "bus-error.h" diff --git a/src/shared/generator.c b/src/shared/generator.c index 5e3cf2a6cb..86fcced098 100644 --- a/src/shared/generator.c +++ b/src/shared/generator.c @@ -4,6 +4,7 @@ #include #include "alloc-util.h" +#include "argv-util.h" #include "cgroup-util.h" #include "dropin.h" #include "escape.h" diff --git a/src/shared/main-func.c b/src/shared/main-func.c new file mode 100644 index 0000000000..c861218363 --- /dev/null +++ b/src/shared/main-func.c @@ -0,0 +1,50 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#if HAVE_VALGRIND_VALGRIND_H +# include +#endif + +#include "sd-daemon.h" + +#include "argv-util.h" +#include "ask-password-agent.h" +#include "hashmap.h" +#include "main-func.h" +#include "pager.h" +#include "polkit-agent.h" +#include "selinux-util.h" +#include "signal-util.h" + +void main_prepare(int argc, char *argv[]) { + assert_se(argc > 0 && !isempty(argv[0])); + save_argc_argv(argc, argv); +} + +void main_finalize(int r, int exit_status) { + if (r < 0) + (void) sd_notifyf(0, "ERRNO=%i", -r); + (void) sd_notifyf(0, "EXIT_STATUS=%i", exit_status); + ask_password_agent_close(); + polkit_agent_close(); + pager_close(); + mac_selinux_finish(); +} + +int raise_or_exit_status(int ret) { + if (ret < 0) + return EXIT_FAILURE; + if (ret == 0) + return EXIT_SUCCESS; + if (!SIGNAL_VALID(ret)) + return EXIT_FAILURE; + +#if HAVE_VALGRIND_VALGRIND_H + /* If raise() below succeeds, the destructor cleanup_pools() in hashmap.c will never called. */ + if (RUNNING_ON_VALGRIND) + hashmap_trim_pools(); +#endif + + (void) raise(ret); + /* exit with failure if raise() does not immediately abort the program. */ + return EXIT_FAILURE; +} diff --git a/src/shared/main-func.h b/src/shared/main-func.h index 67537555a1..0faaba6088 100644 --- a/src/shared/main-func.h +++ b/src/shared/main-func.h @@ -3,36 +3,19 @@ #include -#if HAVE_VALGRIND_VALGRIND_H -# include -#endif - -#include "sd-daemon.h" - -#include "argv-util.h" -#include "ask-password-agent.h" -#include "hashmap.h" -#include "pager.h" -#include "polkit-agent.h" -#include "selinux-util.h" -#include "signal-util.h" #include "static-destruct.h" +void main_prepare(int argc, char *argv[]); + +void main_finalize(int r, int exit_status); + #define _DEFINE_MAIN_FUNCTION(intro, impl, result_to_exit_status, result_to_return_value) \ int main(int argc, char *argv[]) { \ int r; \ - assert_se(argc > 0 && !isempty(argv[0])); \ - save_argc_argv(argc, argv); \ + main_prepare(argc, argv); \ intro; \ r = impl; \ - if (r < 0) \ - (void) sd_notifyf(0, "ERRNO=%i", -r); \ - (void) sd_notifyf(0, "EXIT_STATUS=%i", \ - result_to_exit_status(r)); \ - ask_password_agent_close(); \ - polkit_agent_close(); \ - pager_close(); \ - mac_selinux_finish(); \ + main_finalize(r, result_to_exit_status(r)); \ static_destruct(); \ return result_to_return_value(r); \ } @@ -56,24 +39,7 @@ static inline int exit_failure_if_nonzero(int result) { #define DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(impl) \ _DEFINE_MAIN_FUNCTION(,impl(argc, argv), exit_failure_if_nonzero, exit_failure_if_nonzero) -static inline int raise_or_exit_status(int ret) { - if (ret < 0) - return EXIT_FAILURE; - if (ret == 0) - return EXIT_SUCCESS; - if (!SIGNAL_VALID(ret)) - return EXIT_FAILURE; - -#if HAVE_VALGRIND_VALGRIND_H - /* If raise() below succeeds, the destructor cleanup_pools() in hashmap.c will never called. */ - if (RUNNING_ON_VALGRIND) - hashmap_trim_pools(); -#endif - - (void) raise(ret); - /* exit with failure if raise() does not immediately abort the program. */ - return EXIT_FAILURE; -} +int raise_or_exit_status(int ret); /* Negative return values from impl are mapped to EXIT_FAILURE, zero is mapped to EXIT_SUCCESS, * and raise if a positive signal is returned from impl. */ diff --git a/src/shared/meson.build b/src/shared/meson.build index 298dbcf18e..da1deffe4d 100644 --- a/src/shared/meson.build +++ b/src/shared/meson.build @@ -119,6 +119,7 @@ shared_sources = files( 'machine-id-setup.c', 'machine-pool.c', 'macvlan-util.c', + 'main-func.c', 'mkdir-label.c', 'mkfs-util.c', 'module-util.c', diff --git a/src/sysext/sysext.c b/src/sysext/sysext.c index 765d315b73..250aed060f 100644 --- a/src/sysext/sysext.c +++ b/src/sysext/sysext.c @@ -11,6 +11,7 @@ #include "sd-bus.h" #include "sd-varlink.h" +#include "argv-util.h" #include "blockdev-util.h" #include "build.h" #include "bus-error.h" diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index 3ed29d1026..55c597e5f9 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -6,6 +6,7 @@ #include "sd-daemon.h" +#include "argv-util.h" #include "build.h" #include "bus-util.h" #include "capsule-util.h" diff --git a/src/sysupdate/sysupdate.c b/src/sysupdate/sysupdate.c index 3bd91ac022..8393b35b73 100644 --- a/src/sysupdate/sysupdate.c +++ b/src/sysupdate/sysupdate.c @@ -3,6 +3,8 @@ #include #include +#include "sd-daemon.h" + #include "build.h" #include "chase.h" #include "conf-files.h" diff --git a/src/sysupdate/updatectl.c b/src/sysupdate/updatectl.c index 64172fe4c3..20476fae88 100644 --- a/src/sysupdate/updatectl.c +++ b/src/sysupdate/updatectl.c @@ -24,6 +24,7 @@ #include "os-util.h" #include "pager.h" #include "path-util.h" +#include "polkit-agent.h" #include "pretty-print.h" #include "strv.h" #include "sysupdate-update-set-flags.h" diff --git a/src/test/test-display-quota.c b/src/test/test-display-quota.c index 68ecfc8d01..0fe9e33956 100644 --- a/src/test/test-display-quota.c +++ b/src/test/test-display-quota.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "bitfield.h" +#include "errno-util.h" #include "fd-util.h" #include "format-util.h" #include "log.h" diff --git a/src/timedate/timedated.c b/src/timedate/timedated.c index 5e0fb165a7..8d8495a6e2 100644 --- a/src/timedate/timedated.c +++ b/src/timedate/timedated.c @@ -20,6 +20,7 @@ #include "bus-message-util.h" #include "bus-polkit.h" #include "bus-unit-util.h" +#include "bus-util.h" #include "clock-util.h" #include "conf-files.h" #include "constants.h" diff --git a/src/timesync/wait-sync.c b/src/timesync/wait-sync.c index 4104b01325..fa34138875 100644 --- a/src/timesync/wait-sync.c +++ b/src/timesync/wait-sync.c @@ -13,6 +13,7 @@ #include "sd-event.h" +#include "errno-util.h" #include "fd-util.h" #include "inotify-util.h" #include "main-func.h" diff --git a/src/udev/udevadm.c b/src/udev/udevadm.c index ebf1e5190c..c9e9a354a8 100644 --- a/src/udev/udevadm.c +++ b/src/udev/udevadm.c @@ -6,6 +6,7 @@ #include #include "alloc-util.h" +#include "argv-util.h" #include "main-func.h" #include "pretty-print.h" #include "process-util.h" diff --git a/src/userdb/userwork.c b/src/userdb/userwork.c index b030fae84c..6ce5d57ef3 100644 --- a/src/userdb/userwork.c +++ b/src/userdb/userwork.c @@ -6,7 +6,9 @@ #include "sd-daemon.h" #include "sd-varlink.h" +#include "argv-util.h" #include "env-util.h" +#include "errno-util.h" #include "fd-util.h" #include "group-record.h" #include "io-util.h" diff --git a/src/varlinkctl/varlinkctl.c b/src/varlinkctl/varlinkctl.c index 7b2ff301aa..75c656d13c 100644 --- a/src/varlinkctl/varlinkctl.c +++ b/src/varlinkctl/varlinkctl.c @@ -2,6 +2,7 @@ #include +#include "sd-daemon.h" #include "sd-varlink.h" #include "build.h" diff --git a/src/veritysetup/veritysetup.c b/src/veritysetup/veritysetup.c index 5f92cc9011..35bf5fffb7 100644 --- a/src/veritysetup/veritysetup.c +++ b/src/veritysetup/veritysetup.c @@ -7,6 +7,7 @@ #include "sd-device.h" #include "alloc-util.h" +#include "argv-util.h" #include "cryptsetup-util.h" #include "fileio.h" #include "fstab-util.h" diff --git a/src/vmspawn/vmspawn.c b/src/vmspawn/vmspawn.c index 9e062006ab..e9713dc172 100644 --- a/src/vmspawn/vmspawn.c +++ b/src/vmspawn/vmspawn.c @@ -20,6 +20,7 @@ #include "build.h" #include "bus-internal.h" #include "bus-locator.h" +#include "bus-util.h" #include "bus-wait-for-jobs.h" #include "capability-util.h" #include "chase.h" diff --git a/src/volatile-root/volatile-root.c b/src/volatile-root/volatile-root.c index 9ab9650480..f7c482de05 100644 --- a/src/volatile-root/volatile-root.c +++ b/src/volatile-root/volatile-root.c @@ -7,6 +7,7 @@ #include "chase.h" #include "devnum-util.h" #include "escape.h" +#include "log.h" #include "main-func.h" #include "mkdir.h" #include "mount-util.h" From c94f6ab1bf659963bd040301cfe24c991b8db069 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Sat, 3 May 2025 17:59:19 +0200 Subject: [PATCH 3/5] string-table: Move more implementation logic into functions Let's move some more implementation logic into functions. We keep the logic that requires the macro in the macro and move the rest into functions. While we're at it, let's also make the parameter declarations of all the string table macros less clausthrophobic. --- src/basic/confidential-virt.c | 1 + src/basic/rlimit-util.c | 1 + src/basic/string-table.c | 56 ++++++++++- src/basic/string-table.h | 123 +++++++++-------------- src/basic/time-util.c | 2 +- src/basic/virt.c | 1 + src/core/kill.c | 1 + src/core/scope.c | 1 + src/creds/creds.c | 1 + src/home/homed-home.c | 1 + src/import/import-compress.c | 1 + src/journal-remote/journal-remote-main.c | 1 + src/journal/journalctl.c | 1 + src/libsystemd/sd-journal/journal-file.c | 1 + src/libsystemd/sd-json/sd-json.c | 1 + src/network/netdev/macsec.c | 1 + src/network/networkd-link.c | 1 + src/repart/repart.c | 1 + src/shared/ask-password-api.c | 1 + src/shared/bootspec.c | 1 + src/shared/bpf-program.c | 1 + src/shared/coredump-util.c | 2 + src/shared/ethtool-util.c | 1 + src/shared/exec-util.c | 2 +- src/shared/firewall-util-nft.c | 1 + src/shared/numa-util.c | 1 + src/shared/parse-argument.c | 1 + src/shared/pe-binary.c | 2 +- src/systemctl/systemctl.c | 1 + src/sysupdate/sysupdated.c | 1 + 30 files changed, 133 insertions(+), 78 deletions(-) diff --git a/src/basic/confidential-virt.c b/src/basic/confidential-virt.c index bb8953bba4..9ca213f30d 100644 --- a/src/basic/confidential-virt.c +++ b/src/basic/confidential-virt.c @@ -16,6 +16,7 @@ #include "fileio.h" #include "log.h" #include "string-table.h" +#include "string-util.h" #include "utf8.h" #if defined(__x86_64__) diff --git a/src/basic/rlimit-util.c b/src/basic/rlimit-util.c index 0032f23c26..d5763b5f9e 100644 --- a/src/basic/rlimit-util.c +++ b/src/basic/rlimit-util.c @@ -10,6 +10,7 @@ #include "format-util.h" #include "log.h" #include "macro.h" +#include "parse-util.h" #include "process-util.h" #include "rlimit-util.h" #include "string-table.h" diff --git a/src/basic/string-table.c b/src/basic/string-table.c index 3a6376714a..8f2b4cb15b 100644 --- a/src/basic/string-table.c +++ b/src/basic/string-table.c @@ -1,9 +1,17 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include "parse-util.h" #include "string-table.h" #include "string-util.h" -ssize_t string_table_lookup(const char * const *table, size_t len, const char *key) { +const char* string_table_lookup_to_string(const char * const *table, size_t len, ssize_t i) { + if (i < 0 || i >= (ssize_t) len) + return NULL; + + return table[i]; +} + +ssize_t string_table_lookup_from_string(const char * const *table, size_t len, const char *key) { if (!key) return -EINVAL; @@ -13,3 +21,49 @@ ssize_t string_table_lookup(const char * const *table, size_t len, const char *k return -EINVAL; } + +ssize_t string_table_lookup_from_string_with_boolean(const char * const *table, size_t len, const char *key, ssize_t yes) { + if (!key) + return -EINVAL; + + int b = parse_boolean(key); + if (b == 0) + return 0; + if (b > 0) + return yes; + + return string_table_lookup_from_string(table, len, key); +} + +int string_table_lookup_to_string_fallback(const char * const *table, size_t len, ssize_t i, size_t max, char **ret) { + char *s; + + if (i < 0 || i > (ssize_t) max) + return -ERANGE; + if (i < (ssize_t) len && table[i]) { + s = strdup(table[i]); + if (!s) + return -ENOMEM; + } else if (asprintf(&s, "%zd", i) < 0) + return -ENOMEM; + + *ret = s; + return 0; +} + +ssize_t string_table_lookup_from_string_fallback(const char * const *table, size_t len, const char *s, size_t max) { + unsigned u = 0; + + if (!s) + return -EINVAL; + + ssize_t i = string_table_lookup_from_string(table, len, s); + if (i >= 0) + return i; + if (safe_atou(s, &u) < 0) + return -EINVAL; + if (u > max) + return -EINVAL; + + return u; +} diff --git a/src/basic/string-table.h b/src/basic/string-table.h index 83891eeb73..a1a966beef 100644 --- a/src/basic/string-table.h +++ b/src/basic/string-table.h @@ -2,105 +2,80 @@ #pragma once -#include #include #include #include #include "macro.h" -#include "parse-util.h" -#include "string-util.h" -ssize_t string_table_lookup(const char * const *table, size_t len, const char *key); +const char* string_table_lookup_to_string(const char * const *table, size_t len, ssize_t i); + +ssize_t string_table_lookup_from_string(const char * const *table, size_t len, const char *key); + +ssize_t string_table_lookup_from_string_with_boolean(const char * const *table, size_t len, const char *key, ssize_t yes); + +int string_table_lookup_to_string_fallback(const char * const *table, size_t len, ssize_t i, size_t max, char **ret); + +ssize_t string_table_lookup_from_string_fallback(const char * const *table, size_t len, const char *s, size_t max); /* For basic lookup tables with strictly enumerated entries */ -#define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \ - scope const char* name##_to_string(type i) { \ - if (i < 0 || i >= (type) ELEMENTSOF(name##_table)) \ - return NULL; \ - return name##_table[i]; \ +#define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name, type, scope) \ + scope const char* name##_to_string(type i) { \ + return string_table_lookup_to_string(name##_table, ELEMENTSOF(name##_table), i); \ } -#define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope) \ - scope type name##_from_string(const char *s) { \ - return (type) string_table_lookup(name##_table, ELEMENTSOF(name##_table), s); \ +#define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name, type, scope) \ + scope type name##_from_string(const char *s) { \ + return (type) string_table_lookup_from_string(name##_table, ELEMENTSOF(name##_table), s); \ } -#define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(name,type,yes,scope) \ - scope type name##_from_string(const char *s) { \ - if (!s) \ - return -EINVAL; \ - int b = parse_boolean(s); \ - if (b == 0) \ - return (type) 0; \ - if (b > 0) \ - return yes; \ - return (type) string_table_lookup(name##_table, ELEMENTSOF(name##_table), s); \ +#define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(name, type, yes, scope) \ + scope type name##_from_string(const char *s) { \ + return (type) string_table_lookup_from_string_with_boolean(name##_table, ELEMENTSOF(name##_table), s, yes); \ } -#define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max,scope) \ - scope int name##_to_string_alloc(type i, char **str) { \ - char *s; \ - if (i < 0 || i > max) \ - return -ERANGE; \ - if (i < (type) ELEMENTSOF(name##_table) && name##_table[i]) { \ - s = strdup(name##_table[i]); \ - if (!s) \ - return -ENOMEM; \ - } else if (asprintf(&s, "%i", i) < 0) \ - return -ENOMEM; \ - *str = s; \ - return 0; \ +#define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name, type, max, scope) \ + scope int name##_to_string_alloc(type i, char **ret) { \ + return string_table_lookup_to_string_fallback(name##_table, ELEMENTSOF(name##_table), i, max, ret); \ } -#define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,scope) \ - scope type name##_from_string(const char *s) { \ - unsigned u = 0; \ - type i; \ - if (!s) \ - return -EINVAL; \ - i = (type) string_table_lookup(name##_table, ELEMENTSOF(name##_table), s); \ - if (i >= 0) \ - return i; \ - if (safe_atou(s, &u) < 0) \ - return -EINVAL; \ - if (u > max) \ - return -EINVAL; \ - return (type) u; \ +#define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name, type, max, scope) \ + scope type name##_from_string(const char *s) { \ + return (type) string_table_lookup_from_string_fallback(name##_table, ELEMENTSOF(name##_table), s, max); \ } -#define _DEFINE_STRING_TABLE_LOOKUP(name,type,scope) \ - _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \ - _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope) +#define _DEFINE_STRING_TABLE_LOOKUP(name, type, scope) \ + _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name, type, scope) \ + _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name, type, scope) -#define _DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes,scope) \ - _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \ - _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(name,type,yes,scope) +#define _DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name, type, yes, scope) \ + _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name, type, scope) \ + _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(name, type, yes, scope) -#define DEFINE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,) -#define DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,) -#define DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,) -#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,static) -#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,static) -#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,static) +#define DEFINE_STRING_TABLE_LOOKUP(name, type) _DEFINE_STRING_TABLE_LOOKUP(name, type,) +#define DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name, type) _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name, type,) +#define DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name, type) _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name, type,) +#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name, type) _DEFINE_STRING_TABLE_LOOKUP(name, type, static) +#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(name, type) _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name, type, static) +#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(name, type) _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name, type, static) -#define DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes) _DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes,) -#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes) _DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes,static) -#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(name,type,yes) \ - _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(name,type,yes,static) +#define DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name, type, yes) _DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name, type, yes,) +#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name, type, yes) _DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name, type, yes, static) +#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(name, type, yes) \ + _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(name, type, yes, static) /* For string conversions where numbers are also acceptable */ -#define DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(name,type,max) \ - _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max,) \ - _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,) -#define DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_FALLBACK(name,type,max) _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,) +#define DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(name, type, max) \ + _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name, type, max,) \ + _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name, type, max,) +#define DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_FALLBACK(name, type, max) _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name, type, max,) -#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max) \ - _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max,static) -#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max) \ - _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,static) +#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name, type, max) \ + _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name, type, max, static) +#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name, type, max) \ + _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name, type, max, static) -#define DUMP_STRING_TABLE(name,type,max) \ +#define DUMP_STRING_TABLE(name, type, max) \ ({ \ flockfile(stdout); \ for (type _k = 0; _k < (max); _k++) { \ diff --git a/src/basic/time-util.c b/src/basic/time-util.c index fcbedf9e9d..17850c7e16 100644 --- a/src/basic/time-util.c +++ b/src/basic/time-util.c @@ -1785,7 +1785,7 @@ DEFINE_STRING_TABLE_LOOKUP_TO_STRING(timestamp_style, TimestampStyle); TimestampStyle timestamp_style_from_string(const char *s) { TimestampStyle t; - t = (TimestampStyle) string_table_lookup(timestamp_style_table, ELEMENTSOF(timestamp_style_table), s); + t = (TimestampStyle) string_table_lookup_from_string(timestamp_style_table, ELEMENTSOF(timestamp_style_table), s); if (t >= 0) return t; if (STRPTR_IN_SET(s, "µs", "μs")) /* accept both µ symbols in unicode, i.e. micro symbol + Greek small letter mu. */ diff --git a/src/basic/virt.c b/src/basic/virt.c index 4c5c5b3b9a..dbb035a94a 100644 --- a/src/basic/virt.c +++ b/src/basic/virt.c @@ -18,6 +18,7 @@ #include "log.h" #include "macro.h" #include "namespace-util.h" +#include "parse-util.h" #include "process-util.h" #include "stat-util.h" #include "string-table.h" diff --git a/src/core/kill.c b/src/core/kill.c index a93132ccd8..74b1b13a8d 100644 --- a/src/core/kill.c +++ b/src/core/kill.c @@ -3,6 +3,7 @@ #include "kill.h" #include "signal-util.h" #include "string-table.h" +#include "string-util.h" void kill_context_init(KillContext *c) { assert(c); diff --git a/src/core/scope.c b/src/core/scope.c index 3424d978de..174a95c649 100644 --- a/src/core/scope.c +++ b/src/core/scope.c @@ -11,6 +11,7 @@ #include "load-dropin.h" #include "log.h" #include "manager.h" +#include "parse-util.h" #include "process-util.h" #include "random-util.h" #include "scope.h" diff --git a/src/creds/creds.c b/src/creds/creds.c index 7c2e49da4f..343030b5fb 100644 --- a/src/creds/creds.c +++ b/src/creds/creds.c @@ -23,6 +23,7 @@ #include "missing_magic.h" #include "pager.h" #include "parse-argument.h" +#include "parse-util.h" #include "polkit-agent.h" #include "pretty-print.h" #include "process-util.h" diff --git a/src/home/homed-home.c b/src/home/homed-home.c index d33b4974b9..da5e304f1b 100644 --- a/src/home/homed-home.c +++ b/src/home/homed-home.c @@ -28,6 +28,7 @@ #include "missing_magic.h" #include "missing_mman.h" #include "mkdir.h" +#include "parse-util.h" #include "path-util.h" #include "process-util.h" #include "quota-util.h" diff --git a/src/import/import-compress.c b/src/import/import-compress.c index 73bfc1f0fa..f6385c6b03 100644 --- a/src/import/import-compress.c +++ b/src/import/import-compress.c @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include "assert-util.h" #include "import-compress.h" #include "log.h" #include "string-table.h" diff --git a/src/journal-remote/journal-remote-main.c b/src/journal-remote/journal-remote-main.c index e244b668ee..3dd72ddcf4 100644 --- a/src/journal-remote/journal-remote-main.c +++ b/src/journal-remote/journal-remote-main.c @@ -20,6 +20,7 @@ #include "microhttpd-util.h" #include "parse-argument.h" #include "parse-helpers.h" +#include "parse-util.h" #include "pretty-print.h" #include "process-util.h" #include "rlimit-util.h" diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index 400894e2a2..9125831a9d 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -19,6 +19,7 @@ #include "mount-util.h" #include "mountpoint-util.h" #include "parse-argument.h" +#include "parse-util.h" #include "pretty-print.h" #include "static-destruct.h" #include "string-table.h" diff --git a/src/libsystemd/sd-journal/journal-file.c b/src/libsystemd/sd-journal/journal-file.c index 720c67c7be..6945308187 100644 --- a/src/libsystemd/sd-journal/journal-file.c +++ b/src/libsystemd/sd-journal/journal-file.c @@ -30,6 +30,7 @@ #include "lookup3.h" #include "memory-util.h" #include "missing_fs.h" +#include "parse-util.h" #include "path-util.h" #include "prioq.h" #include "random-util.h" diff --git a/src/libsystemd/sd-json/sd-json.c b/src/libsystemd/sd-json/sd-json.c index 4a91bc8b81..03f94ce663 100644 --- a/src/libsystemd/sd-json/sd-json.c +++ b/src/libsystemd/sd-json/sd-json.c @@ -30,6 +30,7 @@ #include "memory-util.h" #include "memstream-util.h" #include "ordered-set.h" +#include "parse-util.h" #include "path-util.h" #include "process-util.h" #include "ratelimit.h" diff --git a/src/network/netdev/macsec.c b/src/network/netdev/macsec.c index 0df52fd360..638ad7dd50 100644 --- a/src/network/netdev/macsec.c +++ b/src/network/netdev/macsec.c @@ -15,6 +15,7 @@ #include "netlink-util.h" #include "networkd-manager.h" #include "parse-helpers.h" +#include "parse-util.h" #include "socket-util.h" #include "string-table.h" #include "string-util.h" diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c index 0bd27a45c8..91f9d1d88d 100644 --- a/src/network/networkd-link.c +++ b/src/network/networkd-link.c @@ -61,6 +61,7 @@ #include "networkd-state-file.h" #include "networkd-sysctl.h" #include "networkd-wifi.h" +#include "parse-util.h" #include "set.h" #include "socket-util.h" #include "stdio-util.h" diff --git a/src/repart/repart.c b/src/repart/repart.c index 80fbafdb4b..8eaf4308f9 100644 --- a/src/repart/repart.c +++ b/src/repart/repart.c @@ -62,6 +62,7 @@ #include "openssl-util.h" #include "parse-argument.h" #include "parse-helpers.h" +#include "parse-util.h" #include "pretty-print.h" #include "proc-cmdline.h" #include "process-util.h" diff --git a/src/shared/ask-password-api.c b/src/shared/ask-password-api.c index 62a99eb7a0..a52d874ad8 100644 --- a/src/shared/ask-password-api.c +++ b/src/shared/ask-password-api.c @@ -38,6 +38,7 @@ #include "missing_syscall.h" #include "mkdir-label.h" #include "nulstr-util.h" +#include "parse-util.h" #include "path-lookup.h" #include "plymouth-util.h" #include "process-util.h" diff --git a/src/shared/bootspec.c b/src/shared/bootspec.c index 7bdb32192a..a9cbd62fe4 100644 --- a/src/shared/bootspec.c +++ b/src/shared/bootspec.c @@ -15,6 +15,7 @@ #include "fileio.h" #include "find-esp.h" #include "log.h" +#include "parse-util.h" #include "path-util.h" #include "pe-binary.h" #include "pretty-print.h" diff --git a/src/shared/bpf-program.c b/src/shared/bpf-program.c index 05a937cc50..96ce5e9006 100644 --- a/src/shared/bpf-program.c +++ b/src/shared/bpf-program.c @@ -12,6 +12,7 @@ #include "fd-util.h" #include "memory-util.h" #include "missing_syscall.h" +#include "parse-util.h" #include "path-util.h" #include "serialize.h" #include "string-table.h" diff --git a/src/shared/coredump-util.c b/src/shared/coredump-util.c index c27678f011..77573a95c0 100644 --- a/src/shared/coredump-util.c +++ b/src/shared/coredump-util.c @@ -6,8 +6,10 @@ #include "extract-word.h" #include "fileio.h" #include "log.h" +#include "parse-util.h" #include "stdio-util.h" #include "string-table.h" +#include "string-util.h" #include "unaligned.h" #include "virt.h" diff --git a/src/shared/ethtool-util.c b/src/shared/ethtool-util.c index 0dab790305..a91cd5bbf8 100644 --- a/src/shared/ethtool-util.c +++ b/src/shared/ethtool-util.c @@ -13,6 +13,7 @@ #include "log.h" #include "macro-fundamental.h" #include "memory-util.h" +#include "parse-util.h" #include "socket-util.h" #include "string-table.h" #include "strv.h" diff --git a/src/shared/exec-util.c b/src/shared/exec-util.c index 25d919cac4..0b8b20addd 100644 --- a/src/shared/exec-util.c +++ b/src/shared/exec-util.c @@ -505,7 +505,7 @@ ExecCommandFlags exec_command_flags_from_string(const char *s) { if (streq(s, "ambient")) /* Compatibility with ambient hack, removed in v258, map to no bits set */ return 0; - idx = string_table_lookup(exec_command_strings, ELEMENTSOF(exec_command_strings), s); + idx = string_table_lookup_from_string(exec_command_strings, ELEMENTSOF(exec_command_strings), s); if (idx < 0) return _EXEC_COMMAND_FLAGS_INVALID; diff --git a/src/shared/firewall-util-nft.c b/src/shared/firewall-util-nft.c index 410a2d1390..f8273f340c 100644 --- a/src/shared/firewall-util-nft.c +++ b/src/shared/firewall-util-nft.c @@ -22,6 +22,7 @@ #include "macro.h" #include "netlink-internal.h" #include "netlink-util.h" +#include "parse-util.h" #include "socket-util.h" #include "string-table.h" #include "time-util.h" diff --git a/src/shared/numa-util.c b/src/shared/numa-util.c index 7e42f9ae5a..b479e5f776 100644 --- a/src/shared/numa-util.c +++ b/src/shared/numa-util.c @@ -12,6 +12,7 @@ #include "macro.h" #include "missing_syscall.h" #include "numa-util.h" +#include "parse-util.h" #include "stdio-util.h" #include "string-table.h" diff --git a/src/shared/parse-argument.c b/src/shared/parse-argument.c index ced3665725..288b68ceed 100644 --- a/src/shared/parse-argument.c +++ b/src/shared/parse-argument.c @@ -3,6 +3,7 @@ #include "format-table.h" #include "log.h" #include "parse-argument.h" +#include "parse-util.h" #include "path-util.h" #include "signal-util.h" #include "stdio-util.h" diff --git a/src/shared/pe-binary.c b/src/shared/pe-binary.c index 6bfc4868ae..627c59cac8 100644 --- a/src/shared/pe-binary.c +++ b/src/shared/pe-binary.c @@ -539,7 +539,7 @@ int uki_hash(int fd, if (!n) return log_oom_debug(); - i = string_table_lookup(unified_sections, _UNIFIED_SECTION_MAX, n); + i = string_table_lookup_from_string(unified_sections, _UNIFIED_SECTION_MAX, n); if (i < 0) continue; diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index 55c597e5f9..fdbde7dc5e 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -18,6 +18,7 @@ #include "output-mode.h" #include "pager.h" #include "parse-argument.h" +#include "parse-util.h" #include "path-util.h" #include "pretty-print.h" #include "process-util.h" diff --git a/src/sysupdate/sysupdated.c b/src/sysupdate/sysupdated.c index fcb949fc0f..7dd2d981e0 100644 --- a/src/sysupdate/sysupdated.c +++ b/src/sysupdate/sysupdated.c @@ -26,6 +26,7 @@ #include "mkdir-label.h" #include "notify-recv.h" #include "os-util.h" +#include "parse-util.h" #include "process-util.h" #include "service-util.h" #include "signal-util.h" From 40a6cdc29e94aa2daa606467466ca9eb92069750 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Tue, 6 May 2025 09:53:56 +0200 Subject: [PATCH 4/5] macro: Introduce ABS() macro and use it in header files abs() requires including the entirety of stdlib.h just for a single trivial function. Let's introduce the ABS() macro and use it in header files instead so we can get rid of stdlib.h transitive includes in header files in a later commit. --- src/basic/errno-list.h | 5 +++-- src/basic/errno-util.h | 10 ++++------ src/basic/log.h | 4 ++-- src/basic/macro.h | 2 +- .../cryptsetup-tokens/cryptsetup-token-util.h | 4 +++- src/fundamental/macro-fundamental.h | 7 +++++++ src/libsystemd/sd-journal/journal-internal.h | 2 +- src/shared/plymouth-util.h | 2 +- src/shared/tests.h | 2 +- 9 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/basic/errno-list.h b/src/basic/errno-list.h index 3c15afdaab..afe54d0e8c 100644 --- a/src/basic/errno-list.h +++ b/src/basic/errno-list.h @@ -2,9 +2,10 @@ #pragma once #include -#include #include +#include "macro.h" + /* * MAX_ERRNO is defined as 4095 in linux/err.h * We use the same value here. @@ -16,7 +17,7 @@ static inline const char* errno_to_name(int id) { if (id == 0) /* To stay in line with our own impl */ return NULL; - return strerrorname_np(abs(id)); + return strerrorname_np(ABS(id)); } #else const char* errno_to_name(int id); diff --git a/src/basic/errno-util.h b/src/basic/errno-util.h index 01b4d59e04..10118c6901 100644 --- a/src/basic/errno-util.h +++ b/src/basic/errno-util.h @@ -2,7 +2,6 @@ #pragma once #include -#include #include #include "assert-util.h" @@ -16,7 +15,7 @@ * https://stackoverflow.com/questions/34880638/compound-literal-lifetime-and-if-blocks * * Note that we use the GNU variant of strerror_r() here. */ -#define STRERROR(errnum) strerror_r(abs(errnum), (char[ERRNO_BUF_LEN]){}, ERRNO_BUF_LEN) +#define STRERROR(errnum) strerror_r(ABS(errnum), (char[ERRNO_BUF_LEN]){}, ERRNO_BUF_LEN) /* A helper to print an error message or message for functions that return 0 on EOF. * Note that we can't use ({ … }) to define a temporary variable, so errnum is @@ -41,7 +40,7 @@ static inline void _reset_errno_(int *saved_errno) { #define LOCAL_ERRNO(value) \ PROTECT_ERRNO; \ - errno = abs(value) + errno = ABS(value) static inline int negative_errno(void) { /* This helper should be used to shut up gcc if you know 'errno' is @@ -93,15 +92,14 @@ static inline int errno_or_else(int fallback) { if (errno > 0) return -errno; - return -abs(fallback); + return -ABS(fallback); } -/* abs(3) says: Trying to take the absolute value of the most negative integer is not defined. */ #define _DEFINE_ABS_WRAPPER(name) \ static inline bool ERRNO_IS_##name(intmax_t r) { \ if (r == INTMAX_MIN) \ return false; \ - return ERRNO_IS_NEG_##name(-imaxabs(r)); \ + return ERRNO_IS_NEG_##name(-ABS(r)); \ } assert_cc(INT_MAX <= INTMAX_MAX); diff --git a/src/basic/log.h b/src/basic/log.h index 9759b28dc0..845a458f3e 100644 --- a/src/basic/log.h +++ b/src/basic/log.h @@ -33,9 +33,9 @@ typedef enum LogTarget{ #define LOG_NULL (LOG_EMERG - 1) assert_cc(LOG_NULL == -1); -#define SYNTHETIC_ERRNO(num) (abs(num) | (1 << 30)) +#define SYNTHETIC_ERRNO(num) (ABS(num) | (1 << 30)) #define IS_SYNTHETIC_ERRNO(val) (((val) >> 30) == 1) -#define ERRNO_VALUE(val) (abs(val) & ~(1 << 30)) +#define ERRNO_VALUE(val) (ABS(val) & ~(1 << 30)) /* The callback function to be invoked when syntax warnings are seen * in the unit files. */ diff --git a/src/basic/macro.h b/src/basic/macro.h index 4ee1ad4d57..cf1c1a4617 100644 --- a/src/basic/macro.h +++ b/src/basic/macro.h @@ -105,7 +105,7 @@ static inline size_t GREEDY_ALLOC_ROUND_UP(size_t l) { #define return_with_errno(r, err) \ do { \ - errno = abs(err); \ + errno = ABS(err); \ return r; \ } while (false) diff --git a/src/cryptsetup/cryptsetup-tokens/cryptsetup-token-util.h b/src/cryptsetup/cryptsetup-tokens/cryptsetup-token-util.h index 4e9535a995..51d30c845e 100644 --- a/src/cryptsetup/cryptsetup-tokens/cryptsetup-token-util.h +++ b/src/cryptsetup/cryptsetup-tokens/cryptsetup-token-util.h @@ -6,6 +6,8 @@ #include #include +#include "macro.h" + /* crypt_dump() internal indentation magic */ #define CRYPT_DUMP_LINE_SEP "\n\t " @@ -15,7 +17,7 @@ #define crypt_log(cd, ...) crypt_logf(cd, CRYPT_LOG_NORMAL, __VA_ARGS__) #define crypt_log_full_errno(cd, e, lvl, ...) ({ \ - int _e = abs(e), _s = errno; \ + int _e = ABS(e), _s = errno; \ errno = _e; \ crypt_logf(cd, lvl, __VA_ARGS__); \ errno = _s; \ diff --git a/src/fundamental/macro-fundamental.h b/src/fundamental/macro-fundamental.h index dbf0a7d846..417a7853d4 100644 --- a/src/fundamental/macro-fundamental.h +++ b/src/fundamental/macro-fundamental.h @@ -176,6 +176,13 @@ UNIQ_T(A, aq) > UNIQ_T(B, bq) ? UNIQ_T(A, aq) : UNIQ_T(B, bq); \ }) +#define ABS(a) __ABS(UNIQ, (a)) +#define __ABS(aq, a) \ +({ \ + const typeof(a) UNIQ_T(A, aq) = (a); \ + UNIQ_T(A, aq) < 0 ? -UNIQ_T(A, aq) : UNIQ_T(A, aq); \ +}) + #define IS_UNSIGNED_INTEGER_TYPE(type) \ (__builtin_types_compatible_p(typeof(type), unsigned char) || \ __builtin_types_compatible_p(typeof(type), unsigned short) || \ diff --git a/src/libsystemd/sd-journal/journal-internal.h b/src/libsystemd/sd-journal/journal-internal.h index b13f7a45dc..badec0e943 100644 --- a/src/libsystemd/sd-journal/journal-internal.h +++ b/src/libsystemd/sd-journal/journal-internal.h @@ -149,7 +149,7 @@ int journal_add_matchf(sd_journal *j, const char *format, ...) _printf_(2, 3); /* All errors that we might encounter while extracting a field that are not real errors, * but only mean that the field is too large or we don't support the compression. */ static inline bool JOURNAL_ERRNO_IS_UNAVAILABLE_FIELD(int r) { - return IN_SET(abs(r), + return IN_SET(ABS(r), ENOBUFS, /* Field or decompressed field too large */ E2BIG, /* Field too large for pointer width */ EPROTONOSUPPORT); /* Unsupported compression */ diff --git a/src/shared/plymouth-util.h b/src/shared/plymouth-util.h index d7600767b5..9949e5dc83 100644 --- a/src/shared/plymouth-util.h +++ b/src/shared/plymouth-util.h @@ -10,5 +10,5 @@ int plymouth_send_raw(const void *raw, size_t size, int flags); int plymouth_send_msg(const char *text, bool pause_spinner); static inline bool ERRNO_IS_NO_PLYMOUTH(int r) { - return IN_SET(abs(r), EAGAIN, ENOENT) || ERRNO_IS_DISCONNECT(r); + return IN_SET(ABS(r), EAGAIN, ENOENT) || ERRNO_IS_DISCONNECT(r); } diff --git a/src/shared/tests.h b/src/shared/tests.h index 5a37c56324..d19ffcea8c 100644 --- a/src/shared/tests.h +++ b/src/shared/tests.h @@ -40,7 +40,7 @@ static inline void log_set_assert_return_is_criticalp(bool *p) { #define ASSERT_RETURN_EXPECTED_SE(expr) ASSERT_RETURN_EXPECTED(assert_se(expr)); static inline bool manager_errno_skip_test(int r) { - return IN_SET(abs(r), + return IN_SET(ABS(r), EPERM, EACCES, EADDRINUSE, From c09ce222b6379e8f73695e5ce53fce76a44d78c7 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Sun, 4 May 2025 13:31:07 +0200 Subject: [PATCH 5/5] hashmap: Drop debug params Passing in the func, file and line information complicates the interface. On top of that, it prevents forward declaring Hashmap in strv.h, as we need to pass the macros everywhere that we allocate a hashmap, which means we have to include the hashmap header everywhere we have a function that allocates a hashmap instead of just having to forward declare Hashmap. Let's drop the file, func and line information from the debug information. Instead, in the future we can add a description field to hashmaps like we already have in various other structs to describe the purpose of the hashmap which should be much more useful than having the file, line and function where the hashmap was allocated. --- src/basic/hashmap.c | 78 ++++++++++++++++------------------- src/basic/hashmap.h | 53 +++++++++--------------- src/basic/ordered-set.c | 16 +++---- src/basic/ordered-set.h | 17 +++----- src/basic/set.h | 21 ++++------ src/basic/strv.c | 8 ++-- src/basic/strv.h | 8 ++-- tools/gdb-sd_dump_hashmaps.py | 2 +- 8 files changed, 85 insertions(+), 118 deletions(-) diff --git a/src/basic/hashmap.c b/src/basic/hashmap.c index 6bbcd70c07..ccb62e7b2a 100644 --- a/src/basic/hashmap.c +++ b/src/basic/hashmap.c @@ -138,11 +138,6 @@ struct hashmap_debug_info { LIST_FIELDS(struct hashmap_debug_info, debug_list); unsigned max_entries; /* high watermark of n_entries */ - /* who allocated this hashmap */ - int line; - const char *file; - const char *func; - /* fields to detect modification while iterating */ unsigned put_count; /* counts puts into the hashmap */ unsigned rem_count; /* counts removals from hashmap */ @@ -778,7 +773,7 @@ static void shared_hash_key_initialize(void) { random_bytes(shared_hash_key, sizeof(shared_hash_key)); } -static struct HashmapBase* hashmap_base_new(const struct hash_ops *hash_ops, enum HashmapType type HASHMAP_DEBUG_PARAMS) { +static struct HashmapBase* hashmap_base_new(const struct hash_ops *hash_ops, enum HashmapType type) { HashmapBase *h; const struct hashmap_type_info *hi = &hashmap_type_info[type]; @@ -803,9 +798,6 @@ static struct HashmapBase* hashmap_base_new(const struct hash_ops *hash_ops, enu assert_se(pthread_once(&once, shared_hash_key_initialize) == 0); #if ENABLE_DEBUG_HASHMAP - h->debug.func = func; - h->debug.file = file; - h->debug.line = line; assert_se(pthread_mutex_lock(&hashmap_debug_list_mutex) == 0); LIST_PREPEND(debug_list, hashmap_debug_list, &h->debug); assert_se(pthread_mutex_unlock(&hashmap_debug_list_mutex) == 0); @@ -814,20 +806,20 @@ static struct HashmapBase* hashmap_base_new(const struct hash_ops *hash_ops, enu return h; } -Hashmap *_hashmap_new(const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS) { - return (Hashmap*) hashmap_base_new(hash_ops, HASHMAP_TYPE_PLAIN HASHMAP_DEBUG_PASS_ARGS); +Hashmap *hashmap_new(const struct hash_ops *hash_ops) { + return (Hashmap*) hashmap_base_new(hash_ops, HASHMAP_TYPE_PLAIN); } -OrderedHashmap *_ordered_hashmap_new(const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS) { - return (OrderedHashmap*) hashmap_base_new(hash_ops, HASHMAP_TYPE_ORDERED HASHMAP_DEBUG_PASS_ARGS); +OrderedHashmap *ordered_hashmap_new(const struct hash_ops *hash_ops) { + return (OrderedHashmap*) hashmap_base_new(hash_ops, HASHMAP_TYPE_ORDERED); } -Set *_set_new(const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS) { - return (Set*) hashmap_base_new(hash_ops, HASHMAP_TYPE_SET HASHMAP_DEBUG_PASS_ARGS); +Set *set_new(const struct hash_ops *hash_ops) { + return (Set*) hashmap_base_new(hash_ops, HASHMAP_TYPE_SET); } static int hashmap_base_ensure_allocated(HashmapBase **h, const struct hash_ops *hash_ops, - enum HashmapType type HASHMAP_DEBUG_PARAMS) { + enum HashmapType type) { HashmapBase *q; assert(h); @@ -835,7 +827,7 @@ static int hashmap_base_ensure_allocated(HashmapBase **h, const struct hash_ops if (*h) return 0; - q = hashmap_base_new(hash_ops, type HASHMAP_DEBUG_PASS_ARGS); + q = hashmap_base_new(hash_ops, type); if (!q) return -ENOMEM; @@ -843,52 +835,52 @@ static int hashmap_base_ensure_allocated(HashmapBase **h, const struct hash_ops return 1; } -int _hashmap_ensure_allocated(Hashmap **h, const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS) { - return hashmap_base_ensure_allocated((HashmapBase**)h, hash_ops, HASHMAP_TYPE_PLAIN HASHMAP_DEBUG_PASS_ARGS); +int hashmap_ensure_allocated(Hashmap **h, const struct hash_ops *hash_ops) { + return hashmap_base_ensure_allocated((HashmapBase**)h, hash_ops, HASHMAP_TYPE_PLAIN); } -int _ordered_hashmap_ensure_allocated(OrderedHashmap **h, const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS) { - return hashmap_base_ensure_allocated((HashmapBase**)h, hash_ops, HASHMAP_TYPE_ORDERED HASHMAP_DEBUG_PASS_ARGS); +int ordered_hashmap_ensure_allocated(OrderedHashmap **h, const struct hash_ops *hash_ops) { + return hashmap_base_ensure_allocated((HashmapBase**)h, hash_ops, HASHMAP_TYPE_ORDERED); } -int _set_ensure_allocated(Set **s, const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS) { - return hashmap_base_ensure_allocated((HashmapBase**)s, hash_ops, HASHMAP_TYPE_SET HASHMAP_DEBUG_PASS_ARGS); +int _set_ensure_allocated(Set **s, const struct hash_ops *hash_ops) { + return hashmap_base_ensure_allocated((HashmapBase**)s, hash_ops, HASHMAP_TYPE_SET); } -int _hashmap_ensure_put(Hashmap **h, const struct hash_ops *hash_ops, const void *key, void *value HASHMAP_DEBUG_PARAMS) { +int hashmap_ensure_put(Hashmap **h, const struct hash_ops *hash_ops, const void *key, void *value) { int r; - r = _hashmap_ensure_allocated(h, hash_ops HASHMAP_DEBUG_PASS_ARGS); + r = hashmap_ensure_allocated(h, hash_ops); if (r < 0) return r; return hashmap_put(*h, key, value); } -int _ordered_hashmap_ensure_put(OrderedHashmap **h, const struct hash_ops *hash_ops, const void *key, void *value HASHMAP_DEBUG_PARAMS) { +int ordered_hashmap_ensure_put(OrderedHashmap **h, const struct hash_ops *hash_ops, const void *key, void *value) { int r; - r = _ordered_hashmap_ensure_allocated(h, hash_ops HASHMAP_DEBUG_PASS_ARGS); + r = ordered_hashmap_ensure_allocated(h, hash_ops); if (r < 0) return r; return ordered_hashmap_put(*h, key, value); } -int _ordered_hashmap_ensure_replace(OrderedHashmap **h, const struct hash_ops *hash_ops, const void *key, void *value HASHMAP_DEBUG_PARAMS) { +int ordered_hashmap_ensure_replace(OrderedHashmap **h, const struct hash_ops *hash_ops, const void *key, void *value) { int r; - r = _ordered_hashmap_ensure_allocated(h, hash_ops HASHMAP_DEBUG_PASS_ARGS); + r = ordered_hashmap_ensure_allocated(h, hash_ops); if (r < 0) return r; return ordered_hashmap_replace(*h, key, value); } -int _hashmap_ensure_replace(Hashmap **h, const struct hash_ops *hash_ops, const void *key, void *value HASHMAP_DEBUG_PARAMS) { +int hashmap_ensure_replace(Hashmap **h, const struct hash_ops *hash_ops, const void *key, void *value) { int r; - r = _hashmap_ensure_allocated(h, hash_ops HASHMAP_DEBUG_PASS_ARGS); + r = hashmap_ensure_allocated(h, hash_ops); if (r < 0) return r; @@ -1290,20 +1282,20 @@ int set_put(Set *s, const void *key) { return hashmap_put_boldly(s, hash, &swap, true); } -int _set_ensure_put(Set **s, const struct hash_ops *hash_ops, const void *key HASHMAP_DEBUG_PARAMS) { +int set_ensure_put(Set **s, const struct hash_ops *hash_ops, const void *key) { int r; - r = _set_ensure_allocated(s, hash_ops HASHMAP_DEBUG_PASS_ARGS); + r = _set_ensure_allocated(s, hash_ops); if (r < 0) return r; return set_put(*s, key); } -int _set_ensure_consume(Set **s, const struct hash_ops *hash_ops, void *key HASHMAP_DEBUG_PARAMS) { +int set_ensure_consume(Set **s, const struct hash_ops *hash_ops, void *key) { int r; - r = _set_ensure_put(s, hash_ops, key HASHMAP_DEBUG_PASS_ARGS); + r = set_ensure_put(s, hash_ops, key); if (r <= 0) { if (hash_ops && hash_ops->free_key) hash_ops->free_key(key); @@ -1754,13 +1746,13 @@ int _hashmap_move_one(HashmapBase *h, HashmapBase *other, const void *key) { return 0; } -HashmapBase* _hashmap_copy(HashmapBase *h HASHMAP_DEBUG_PARAMS) { +HashmapBase* _hashmap_copy(HashmapBase *h) { HashmapBase *copy; int r; assert(h); - copy = hashmap_base_new(h->hash_ops, h->type HASHMAP_DEBUG_PASS_ARGS); + copy = hashmap_base_new(h->hash_ops, h->type); if (!copy) return NULL; @@ -1850,10 +1842,10 @@ int set_consume(Set *s, void *value) { return r; } -int _hashmap_put_strdup_full(Hashmap **h, const struct hash_ops *hash_ops, const char *k, const char *v HASHMAP_DEBUG_PARAMS) { +int hashmap_put_strdup_full(Hashmap **h, const struct hash_ops *hash_ops, const char *k, const char *v) { int r; - r = _hashmap_ensure_allocated(h, hash_ops HASHMAP_DEBUG_PASS_ARGS); + r = hashmap_ensure_allocated(h, hash_ops); if (r < 0) return r; @@ -1884,14 +1876,14 @@ int _hashmap_put_strdup_full(Hashmap **h, const struct hash_ops *hash_ops, const return r; } -int _set_put_strndup_full(Set **s, const struct hash_ops *hash_ops, const char *p, size_t n HASHMAP_DEBUG_PARAMS) { +int set_put_strndup_full(Set **s, const struct hash_ops *hash_ops, const char *p, size_t n) { char *c; int r; assert(s); assert(p); - r = _set_ensure_allocated(s, hash_ops HASHMAP_DEBUG_PASS_ARGS); + r = _set_ensure_allocated(s, hash_ops); if (r < 0) return r; @@ -1908,13 +1900,13 @@ int _set_put_strndup_full(Set **s, const struct hash_ops *hash_ops, const char * return set_consume(*s, c); } -int _set_put_strdupv_full(Set **s, const struct hash_ops *hash_ops, char **l HASHMAP_DEBUG_PARAMS) { +int set_put_strdupv_full(Set **s, const struct hash_ops *hash_ops, char **l) { int n = 0, r; assert(s); STRV_FOREACH(i, l) { - r = _set_put_strndup_full(s, hash_ops, *i, SIZE_MAX HASHMAP_DEBUG_PASS_ARGS); + r = set_put_strndup_full(s, hash_ops, *i, SIZE_MAX); if (r < 0) return r; diff --git a/src/basic/hashmap.h b/src/basic/hashmap.h index 8ced7e6a99..f7e02c814e 100644 --- a/src/basic/hashmap.h +++ b/src/basic/hashmap.h @@ -73,20 +73,8 @@ typedef struct { (Hashmap*)(h), \ (void)0) -#if ENABLE_DEBUG_HASHMAP -# define HASHMAP_DEBUG_PARAMS , const char *func, const char *file, int line -# define HASHMAP_DEBUG_SRC_ARGS , __func__, PROJECT_FILE, __LINE__ -# define HASHMAP_DEBUG_PASS_ARGS , func, file, line -#else -# define HASHMAP_DEBUG_PARAMS -# define HASHMAP_DEBUG_SRC_ARGS -# define HASHMAP_DEBUG_PASS_ARGS -#endif - -Hashmap* _hashmap_new(const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS); -OrderedHashmap* _ordered_hashmap_new(const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS); -#define hashmap_new(ops) _hashmap_new(ops HASHMAP_DEBUG_SRC_ARGS) -#define ordered_hashmap_new(ops) _ordered_hashmap_new(ops HASHMAP_DEBUG_SRC_ARGS) +Hashmap* hashmap_new(const struct hash_ops *hash_ops); +OrderedHashmap* ordered_hashmap_new(const struct hash_ops *hash_ops); #define hashmap_free_and_replace(a, b) \ free_and_replace_full(a, b, hashmap_free) @@ -104,25 +92,21 @@ static inline OrderedHashmap* ordered_hashmap_free(OrderedHashmap *h) { IteratedCache* iterated_cache_free(IteratedCache *cache); int iterated_cache_get(IteratedCache *cache, const void ***res_keys, const void ***res_values, unsigned *res_n_entries); -HashmapBase* _hashmap_copy(HashmapBase *h HASHMAP_DEBUG_PARAMS); -#define hashmap_copy(h) ((Hashmap*) _hashmap_copy(HASHMAP_BASE(h) HASHMAP_DEBUG_SRC_ARGS)) -#define ordered_hashmap_copy(h) ((OrderedHashmap*) _hashmap_copy(HASHMAP_BASE(h) HASHMAP_DEBUG_SRC_ARGS)) +HashmapBase* _hashmap_copy(HashmapBase *h); +static inline Hashmap* hashmap_copy(Hashmap *h) { + return (Hashmap*) _hashmap_copy(HASHMAP_BASE(h)); +} +static inline OrderedHashmap* ordered_hashmap_copy(OrderedHashmap *h) { + return (OrderedHashmap*) _hashmap_copy(HASHMAP_BASE(h)); +} -int _hashmap_ensure_allocated(Hashmap **h, const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS); -int _hashmap_ensure_put(Hashmap **h, const struct hash_ops *hash_ops, const void *key, void *value HASHMAP_DEBUG_PARAMS); -int _ordered_hashmap_ensure_allocated(OrderedHashmap **h, const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS); -int _hashmap_ensure_replace(Hashmap **h, const struct hash_ops *hash_ops, const void *key, void *value HASHMAP_DEBUG_PARAMS); +int hashmap_ensure_allocated(Hashmap **h, const struct hash_ops *hash_ops); +int hashmap_ensure_put(Hashmap **h, const struct hash_ops *hash_ops, const void *key, void *value); +int ordered_hashmap_ensure_allocated(OrderedHashmap **h, const struct hash_ops *hash_ops); +int hashmap_ensure_replace(Hashmap **h, const struct hash_ops *hash_ops, const void *key, void *value); -#define hashmap_ensure_allocated(h, ops) _hashmap_ensure_allocated(h, ops HASHMAP_DEBUG_SRC_ARGS) -#define hashmap_ensure_put(s, ops, key, value) _hashmap_ensure_put(s, ops, key, value HASHMAP_DEBUG_SRC_ARGS) -#define ordered_hashmap_ensure_allocated(h, ops) _ordered_hashmap_ensure_allocated(h, ops HASHMAP_DEBUG_SRC_ARGS) -#define hashmap_ensure_replace(s, ops, key, value) _hashmap_ensure_replace(s, ops, key, value HASHMAP_DEBUG_SRC_ARGS) - -int _ordered_hashmap_ensure_put(OrderedHashmap **h, const struct hash_ops *hash_ops, const void *key, void *value HASHMAP_DEBUG_PARAMS); -#define ordered_hashmap_ensure_put(s, ops, key, value) _ordered_hashmap_ensure_put(s, ops, key, value HASHMAP_DEBUG_SRC_ARGS) - -int _ordered_hashmap_ensure_replace(OrderedHashmap **h, const struct hash_ops *hash_ops, const void *key, void *value HASHMAP_DEBUG_PARAMS); -#define ordered_hashmap_ensure_replace(s, ops, key, value) _ordered_hashmap_ensure_replace(s, ops, key, value HASHMAP_DEBUG_SRC_ARGS) +int ordered_hashmap_ensure_put(OrderedHashmap **h, const struct hash_ops *hash_ops, const void *key, void *value); +int ordered_hashmap_ensure_replace(OrderedHashmap **h, const struct hash_ops *hash_ops, const void *key, void *value); IteratedCache* _hashmap_iterated_cache_new(HashmapBase *h); static inline IteratedCache* hashmap_iterated_cache_new(Hashmap *h) { @@ -137,9 +121,10 @@ static inline int ordered_hashmap_put(OrderedHashmap *h, const void *key, void * return hashmap_put(PLAIN_HASHMAP(h), key, value); } -int _hashmap_put_strdup_full(Hashmap **h, const struct hash_ops *hash_ops, const char *k, const char *v HASHMAP_DEBUG_PARAMS); -#define hashmap_put_strdup_full(h, hash_ops, k, v) _hashmap_put_strdup_full(h, hash_ops, k, v HASHMAP_DEBUG_SRC_ARGS) -#define hashmap_put_strdup(h, k, v) hashmap_put_strdup_full(h, &string_hash_ops_free_free, k, v) +int hashmap_put_strdup_full(Hashmap **h, const struct hash_ops *hash_ops, const char *k, const char *v); +static inline int hashmap_put_strdup(Hashmap **h, const char *k, const char *v) { + return hashmap_put_strdup_full(h, &string_hash_ops_free_free, k, v); +} int hashmap_update(Hashmap *h, const void *key, void *value); static inline int ordered_hashmap_update(OrderedHashmap *h, const void *key, void *value) { diff --git a/src/basic/ordered-set.c b/src/basic/ordered-set.c index 65cf3a026f..b59a9e5f1b 100644 --- a/src/basic/ordered-set.c +++ b/src/basic/ordered-set.c @@ -4,21 +4,21 @@ #include "ordered-set.h" #include "strv.h" -int _ordered_set_ensure_allocated(OrderedSet **s, const struct hash_ops *ops HASHMAP_DEBUG_PARAMS) { +int ordered_set_ensure_allocated(OrderedSet **s, const struct hash_ops *ops) { if (*s) return 0; - *s = _ordered_set_new(ops HASHMAP_DEBUG_PASS_ARGS); + *s = ordered_set_new(ops); if (!*s) return -ENOMEM; return 0; } -int _ordered_set_ensure_put(OrderedSet **s, const struct hash_ops *ops, void *p HASHMAP_DEBUG_PARAMS) { +int ordered_set_ensure_put(OrderedSet **s, const struct hash_ops *ops, void *p) { int r; - r = _ordered_set_ensure_allocated(s, ops HASHMAP_DEBUG_PASS_ARGS); + r = ordered_set_ensure_allocated(s, ops); if (r < 0) return r; @@ -35,14 +35,14 @@ int ordered_set_consume(OrderedSet *s, void *p) { return r; } -int _ordered_set_put_strdup(OrderedSet **s, const char *p HASHMAP_DEBUG_PARAMS) { +int ordered_set_put_strdup(OrderedSet **s, const char *p) { char *c; int r; assert(s); assert(p); - r = _ordered_set_ensure_allocated(s, &string_hash_ops_free HASHMAP_DEBUG_PASS_ARGS); + r = ordered_set_ensure_allocated(s, &string_hash_ops_free); if (r < 0) return r; @@ -56,11 +56,11 @@ int _ordered_set_put_strdup(OrderedSet **s, const char *p HASHMAP_DEBUG_PARAMS) return ordered_set_consume(*s, c); } -int _ordered_set_put_strdupv(OrderedSet **s, char **l HASHMAP_DEBUG_PARAMS) { +int ordered_set_put_strdupv(OrderedSet **s, char **l) { int n = 0, r; STRV_FOREACH(i, l) { - r = _ordered_set_put_strdup(s, *i HASHMAP_DEBUG_PASS_ARGS); + r = ordered_set_put_strdup(s, *i); if (r < 0) return r; diff --git a/src/basic/ordered-set.h b/src/basic/ordered-set.h index b7113ad918..77661ced66 100644 --- a/src/basic/ordered-set.h +++ b/src/basic/ordered-set.h @@ -7,16 +7,13 @@ typedef struct OrderedSet OrderedSet; -static inline OrderedSet* _ordered_set_new(const struct hash_ops *ops HASHMAP_DEBUG_PARAMS) { - return (OrderedSet*) _ordered_hashmap_new(ops HASHMAP_DEBUG_PASS_ARGS); +static inline OrderedSet* ordered_set_new(const struct hash_ops *ops) { + return (OrderedSet*) ordered_hashmap_new(ops); } -#define ordered_set_new(ops) _ordered_set_new(ops HASHMAP_DEBUG_SRC_ARGS) -int _ordered_set_ensure_allocated(OrderedSet **s, const struct hash_ops *ops HASHMAP_DEBUG_PARAMS); -#define ordered_set_ensure_allocated(s, ops) _ordered_set_ensure_allocated(s, ops HASHMAP_DEBUG_SRC_ARGS) +int ordered_set_ensure_allocated(OrderedSet **s, const struct hash_ops *ops); -int _ordered_set_ensure_put(OrderedSet **s, const struct hash_ops *ops, void *p HASHMAP_DEBUG_PARAMS); -#define ordered_set_ensure_put(s, hash_ops, key) _ordered_set_ensure_put(s, hash_ops, key HASHMAP_DEBUG_SRC_ARGS) +int ordered_set_ensure_put(OrderedSet **s, const struct hash_ops *ops, void *p); static inline void ordered_set_clear(OrderedSet *s) { return ordered_hashmap_clear((OrderedHashmap*) s); @@ -71,10 +68,8 @@ static inline int ordered_set_reserve(OrderedSet *s, unsigned entries_add) { } int ordered_set_consume(OrderedSet *s, void *p); -int _ordered_set_put_strdup(OrderedSet **s, const char *p HASHMAP_DEBUG_PARAMS); -#define ordered_set_put_strdup(s, p) _ordered_set_put_strdup(s, p HASHMAP_DEBUG_SRC_ARGS) -int _ordered_set_put_strdupv(OrderedSet **s, char **l HASHMAP_DEBUG_PARAMS); -#define ordered_set_put_strdupv(s, l) _ordered_set_put_strdupv(s, l HASHMAP_DEBUG_SRC_ARGS) +int ordered_set_put_strdup(OrderedSet **s, const char *p); +int ordered_set_put_strdupv(OrderedSet **s, char **l); int ordered_set_put_string_set(OrderedSet **s, OrderedSet *l); void ordered_set_print(FILE *f, const char *field, OrderedSet *s); diff --git a/src/basic/set.h b/src/basic/set.h index c7b84e0a54..789bf9b733 100644 --- a/src/basic/set.h +++ b/src/basic/set.h @@ -8,17 +8,16 @@ #define set_free_and_replace(a, b) \ free_and_replace_full(a, b, set_free) -Set* _set_new(const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS); -#define set_new(ops) _set_new(ops HASHMAP_DEBUG_SRC_ARGS) +Set* set_new(const struct hash_ops *hash_ops); static inline Set* set_free(Set *s) { return (Set*) _hashmap_free(HASHMAP_BASE(s)); } -#define set_copy(s) ((Set*) _hashmap_copy(HASHMAP_BASE(s) HASHMAP_DEBUG_SRC_ARGS)) +#define set_copy(s) ((Set*) _hashmap_copy(HASHMAP_BASE(s))) -int _set_ensure_allocated(Set **s, const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS); -#define set_ensure_allocated(h, ops) _set_ensure_allocated(h, ops HASHMAP_DEBUG_SRC_ARGS) +int _set_ensure_allocated(Set **s, const struct hash_ops *hash_ops); +#define set_ensure_allocated(h, ops) _set_ensure_allocated(h, ops) int set_put(Set *s, const void *key); /* no set_update */ @@ -93,22 +92,18 @@ static inline char **set_get_strv(Set *s) { char** set_to_strv(Set **s); -int _set_ensure_put(Set **s, const struct hash_ops *hash_ops, const void *key HASHMAP_DEBUG_PARAMS); -#define set_ensure_put(s, hash_ops, key) _set_ensure_put(s, hash_ops, key HASHMAP_DEBUG_SRC_ARGS) +int set_ensure_put(Set **s, const struct hash_ops *hash_ops, const void *key); -int _set_ensure_consume(Set **s, const struct hash_ops *hash_ops, void *key HASHMAP_DEBUG_PARAMS); -#define set_ensure_consume(s, hash_ops, key) _set_ensure_consume(s, hash_ops, key HASHMAP_DEBUG_SRC_ARGS) +int set_ensure_consume(Set **s, const struct hash_ops *hash_ops, void *key); int set_consume(Set *s, void *value); -int _set_put_strndup_full(Set **s, const struct hash_ops *hash_ops, const char *p, size_t n HASHMAP_DEBUG_PARAMS); -#define set_put_strndup_full(s, hash_ops, p, n) _set_put_strndup_full(s, hash_ops, p, n HASHMAP_DEBUG_SRC_ARGS) +int set_put_strndup_full(Set **s, const struct hash_ops *hash_ops, const char *p, size_t n); #define set_put_strdup_full(s, hash_ops, p) set_put_strndup_full(s, hash_ops, p, SIZE_MAX) #define set_put_strndup(s, p, n) set_put_strndup_full(s, &string_hash_ops_free, p, n) #define set_put_strdup(s, p) set_put_strndup(s, p, SIZE_MAX) -int _set_put_strdupv_full(Set **s, const struct hash_ops *hash_ops, char **l HASHMAP_DEBUG_PARAMS); -#define set_put_strdupv_full(s, hash_ops, l) _set_put_strdupv_full(s, hash_ops, l HASHMAP_DEBUG_SRC_ARGS) +int set_put_strdupv_full(Set **s, const struct hash_ops *hash_ops, char **l ); #define set_put_strdupv(s, l) set_put_strdupv_full(s, &string_hash_ops_free, l) int set_put_strsplit(Set *s, const char *v, const char *separators, ExtractFlags flags); diff --git a/src/basic/strv.c b/src/basic/strv.c index 2f30854146..76b86cbec8 100644 --- a/src/basic/strv.c +++ b/src/basic/strv.c @@ -1126,28 +1126,28 @@ static int string_strv_hashmap_put_internal(Hashmap *h, const char *key, const c return 1; } -int _string_strv_hashmap_put(Hashmap **h, const char *key, const char *value HASHMAP_DEBUG_PARAMS) { +int string_strv_hashmap_put(Hashmap **h, const char *key, const char *value) { int r; assert(h); assert(key); assert(value); - r = _hashmap_ensure_allocated(h, &string_hash_ops_free_strv_free HASHMAP_DEBUG_PASS_ARGS); + r = hashmap_ensure_allocated(h, &string_hash_ops_free_strv_free); if (r < 0) return r; return string_strv_hashmap_put_internal(*h, key, value); } -int _string_strv_ordered_hashmap_put(OrderedHashmap **h, const char *key, const char *value HASHMAP_DEBUG_PARAMS) { +int string_strv_ordered_hashmap_put(OrderedHashmap **h, const char *key, const char *value) { int r; assert(h); assert(key); assert(value); - r = _ordered_hashmap_ensure_allocated(h, &string_hash_ops_free_strv_free HASHMAP_DEBUG_PASS_ARGS); + r = ordered_hashmap_ensure_allocated(h, &string_hash_ops_free_strv_free); if (r < 0) return r; diff --git a/src/basic/strv.h b/src/basic/strv.h index 0ca90087b4..24bdd47cc5 100644 --- a/src/basic/strv.h +++ b/src/basic/strv.h @@ -262,10 +262,10 @@ void string_strv_hashmap_remove(Hashmap *h, const char *key, const char *value); static inline void string_strv_ordered_hashmap_remove(OrderedHashmap *h, const char *key, const char *value) { string_strv_hashmap_remove(PLAIN_HASHMAP(h), key, value); } -int _string_strv_hashmap_put(Hashmap **h, const char *key, const char *value HASHMAP_DEBUG_PARAMS); -int _string_strv_ordered_hashmap_put(OrderedHashmap **h, const char *key, const char *value HASHMAP_DEBUG_PARAMS); -#define string_strv_hashmap_put(h, k, v) _string_strv_hashmap_put(h, k, v HASHMAP_DEBUG_SRC_ARGS) -#define string_strv_ordered_hashmap_put(h, k, v) _string_strv_ordered_hashmap_put(h, k, v HASHMAP_DEBUG_SRC_ARGS) +int _string_strv_hashmap_put(Hashmap **h, const char *key, const char *value); +int _string_strv_ordered_hashmap_put(OrderedHashmap **h, const char *key, const char *value); +#define string_strv_hashmap_put(h, k, v) _string_strv_hashmap_put(h, k, v) +#define string_strv_ordered_hashmap_put(h, k, v) _string_strv_ordered_hashmap_put(h, k, v) int strv_rebreak_lines(char **l, size_t width, char ***ret); diff --git a/tools/gdb-sd_dump_hashmaps.py b/tools/gdb-sd_dump_hashmaps.py index 57f825a09c..596ee8d90c 100644 --- a/tools/gdb-sd_dump_hashmaps.py +++ b/tools/gdb-sd_dump_hashmaps.py @@ -32,7 +32,7 @@ class sd_dump_hashmaps(gdb.Command): t = ["plain", "ordered", "set"][int(h["type"])] - print(f'{t}, {h["hash_ops"]}, {bool(h["has_indirect"])}, {n_entries}, {d["max_entries"]}, {n_buckets}, {d["func"].string()}, {d["file"].string()}:{d["line"]}') + print(f'{t}, {h["hash_ops"]}, {bool(h["has_indirect"])}, {n_entries}, {d["max_entries"]}, {n_buckets}') if arg != "" and n_entries > 0: dib_raw_addr = storage_ptr + hashmap_type_info[h["type"]]["entry_size"] * n_buckets