From 44599b20bc3ef6cba5341a8ec62273a8464ab9d9 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Wed, 21 May 2025 13:24:22 +0200 Subject: [PATCH 1/5] stat-util: Use typeof_field() in one more place --- src/basic/stat-util.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/basic/stat-util.h b/src/basic/stat-util.h index e4fd4221390..2c5f7b9e93a 100644 --- a/src/basic/stat-util.h +++ b/src/basic/stat-util.h @@ -57,7 +57,7 @@ static inline int fd_inode_same(int fda, int fdb) { /* The .f_type field of struct statfs is really weird defined on * different archs. Let's give its type a name. */ -typedef typeof(((struct statfs*)NULL)->f_type) statfs_f_type_t; +typedef typeof_field(struct statfs, f_type) statfs_f_type_t; bool is_fs_type(const struct statfs *s, statfs_f_type_t magic_value) _pure_; int is_fs_type_at(int dir_fd, const char *path, statfs_f_type_t magic_value); From 9f74f56f3624e962fa7f51bc22df070a3678c942 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Wed, 21 May 2025 11:34:41 +0200 Subject: [PATCH 2/5] test-tables: Pass enum type to test_table() as well clang-include-cleaner has a hard time figuring out unused includes with all the macro string concatentation we're doing in test_table(). Let's help it out a little by also passing the enum type into test_table(). While technically not needed, this makes the tests a bit more self-descriptive and not really much more verbose so it feels like a good chance regardless, even if the primary purpose is to help clang-include-cleaner figure out used and unused includes. --- src/journal/test-journald-tables.c | 4 +- src/login/test-login-tables.c | 14 +-- src/machine/test-machine-tables.c | 6 +- src/network/test-network-tables.c | 50 ++++---- src/nspawn/test-nspawn-tables.c | 4 +- src/resolve/test-resolve-tables.c | 10 +- src/shared/test-tables.h | 16 +-- src/test/test-tables.c | 162 ++++++++++++------------- src/udev/net/test-link-config-tables.c | 2 +- 9 files changed, 134 insertions(+), 134 deletions(-) diff --git a/src/journal/test-journald-tables.c b/src/journal/test-journald-tables.c index 60b3ed59dd4..c066cbd5a0d 100644 --- a/src/journal/test-journald-tables.c +++ b/src/journal/test-journald-tables.c @@ -7,8 +7,8 @@ int main(int argc, char **argv) { test_setup_logging(LOG_DEBUG); - test_table(split_mode, SPLIT); - test_table(storage, STORAGE); + test_table(SplitMode, split_mode, SPLIT); + test_table(Storage, storage, STORAGE); return EXIT_SUCCESS; } diff --git a/src/login/test-login-tables.c b/src/login/test-login-tables.c index 76b62aa621e..fcba4334ea6 100644 --- a/src/login/test-login-tables.c +++ b/src/login/test-login-tables.c @@ -24,13 +24,13 @@ static void test_sleep_handle_action(void) { int main(int argc, char **argv) { test_setup_logging(LOG_DEBUG); - test_table(handle_action, HANDLE_ACTION); - test_table(inhibit_mode, INHIBIT_MODE); - test_table(kill_whom, KILL_WHOM); - test_table(session_class, SESSION_CLASS); - test_table(session_state, SESSION_STATE); - test_table(session_type, SESSION_TYPE); - test_table(user_state, USER_STATE); + test_table(HandleAction, handle_action, HANDLE_ACTION); + test_table(InhibitMode, inhibit_mode, INHIBIT_MODE); + test_table(KillWhom, kill_whom, KILL_WHOM); + test_table(SessionClass, session_class, SESSION_CLASS); + test_table(SessionState, session_state, SESSION_STATE); + test_table(SessionType, session_type, SESSION_TYPE); + test_table(UserState, user_state, USER_STATE); test_sleep_handle_action(); diff --git a/src/machine/test-machine-tables.c b/src/machine/test-machine-tables.c index d0b73358bf8..997e593977b 100644 --- a/src/machine/test-machine-tables.c +++ b/src/machine/test-machine-tables.c @@ -7,9 +7,9 @@ int main(int argc, char **argv) { test_setup_logging(LOG_DEBUG); - test_table(kill_whom, KILL_WHOM); - test_table(machine_class, MACHINE_CLASS); - test_table(machine_state, MACHINE_STATE); + test_table(KillWhom, kill_whom, KILL_WHOM); + test_table(MachineClass, machine_class, MACHINE_CLASS); + test_table(MachineState, machine_state, MACHINE_STATE); return EXIT_SUCCESS; } diff --git a/src/network/test-network-tables.c b/src/network/test-network-tables.c index 2939f344bae..5ad028bd3b5 100644 --- a/src/network/test-network-tables.c +++ b/src/network/test-network-tables.c @@ -21,33 +21,33 @@ int main(int argc, char **argv) { test_setup_logging(LOG_DEBUG); - test_table(bond_ad_select, NETDEV_BOND_AD_SELECT); - test_table(bond_arp_all_targets, NETDEV_BOND_ARP_ALL_TARGETS); - test_table(bond_arp_validate, NETDEV_BOND_ARP_VALIDATE); - test_table(bond_fail_over_mac, NETDEV_BOND_FAIL_OVER_MAC); - test_table(bond_lacp_rate, NETDEV_BOND_LACP_RATE); - test_table(bond_mode, NETDEV_BOND_MODE); - test_table(bond_primary_reselect, NETDEV_BOND_PRIMARY_RESELECT); - test_table(bond_xmit_hash_policy, NETDEV_BOND_XMIT_HASH_POLICY); - test_table(dhcp6_message_status, DHCP6_STATUS); - test_table_sparse(dhcp6_message_type, DHCP6_MESSAGE_TYPE); /* enum starts from 1 */ - test_table(use_domains, USE_DOMAINS); - test_table(duplex, DUP); - test_table(ip6tnl_mode, NETDEV_IP6_TNL_MODE); - test_table(ipv6_privacy_extensions, IPV6_PRIVACY_EXTENSIONS); - test_table(ipvlan_flags, NETDEV_IPVLAN_FLAGS); - test_table(link_operstate, LINK_OPERSTATE); + test_table(BondAdSelect, bond_ad_select, NETDEV_BOND_AD_SELECT); + test_table(BondArpAllTargets, bond_arp_all_targets, NETDEV_BOND_ARP_ALL_TARGETS); + test_table(BondArpValidate, bond_arp_validate, NETDEV_BOND_ARP_VALIDATE); + test_table(BondFailOverMac, bond_fail_over_mac, NETDEV_BOND_FAIL_OVER_MAC); + test_table(BondLacpRate, bond_lacp_rate, NETDEV_BOND_LACP_RATE); + test_table(BondMode, bond_mode, NETDEV_BOND_MODE); + test_table(BondPrimaryReselect, bond_primary_reselect, NETDEV_BOND_PRIMARY_RESELECT); + test_table(BondXmitHashPolicy, bond_xmit_hash_policy, NETDEV_BOND_XMIT_HASH_POLICY); + test_table(DHCP6Status, dhcp6_message_status, DHCP6_STATUS); + test_table_sparse(DHCP6MessageType, dhcp6_message_type, DHCP6_MESSAGE_TYPE); /* enum starts from 1 */ + test_table(UseDomains, use_domains, USE_DOMAINS); + test_table(Duplex, duplex, DUP); + test_table(Ip6TnlMode, ip6tnl_mode, NETDEV_IP6_TNL_MODE); + test_table(IPv6PrivacyExtensions, ipv6_privacy_extensions, IPV6_PRIVACY_EXTENSIONS); + test_table(IPVlanFlags, ipvlan_flags, NETDEV_IPVLAN_FLAGS); + test_table(LinkOperationalState, link_operstate, LINK_OPERSTATE); /* test_table(link_state, LINK_STATE); — not a reversible mapping */ - test_table(lldp_mode, LLDP_MODE); - test_table(netdev_kind, NETDEV_KIND); - test_table(radv_prefix_delegation, RADV_PREFIX_DELEGATION); - test_table(lldp_rx_event, SD_LLDP_RX_EVENT); - test_table(ndisc_event, SD_NDISC_EVENT); - test_table(dhcp_lease_server_type, SD_DHCP_LEASE_SERVER_TYPE); + test_table(LLDPMode, lldp_mode, LLDP_MODE); + test_table(NetDevKind, netdev_kind, NETDEV_KIND); + test_table(RADVPrefixDelegation, radv_prefix_delegation, RADV_PREFIX_DELEGATION); + test_table(sd_lldp_rx_event_t, lldp_rx_event, SD_LLDP_RX_EVENT); + test_table(sd_ndisc_event_t, ndisc_event, SD_NDISC_EVENT); + test_table(sd_dhcp_lease_server_type_t, dhcp_lease_server_type, SD_DHCP_LEASE_SERVER_TYPE); - test_table_sparse(ipvlan_mode, NETDEV_IPVLAN_MODE); - test_table_sparse(macvlan_mode, NETDEV_MACVLAN_MODE); - test_table_sparse(address_family, ADDRESS_FAMILY); + test_table_sparse(IPVlanMode, ipvlan_mode, NETDEV_IPVLAN_MODE); + test_table_sparse(MacVlanMode, macvlan_mode, NETDEV_MACVLAN_MODE); + test_table_sparse(AddressFamily, address_family, ADDRESS_FAMILY); assert_cc(sizeof(sd_lldp_rx_event_t) == sizeof(int64_t)); assert_cc(sizeof(sd_ndisc_event_t) == sizeof(int64_t)); diff --git a/src/nspawn/test-nspawn-tables.c b/src/nspawn/test-nspawn-tables.c index daea4695e3c..671429c40eb 100644 --- a/src/nspawn/test-nspawn-tables.c +++ b/src/nspawn/test-nspawn-tables.c @@ -7,8 +7,8 @@ int main(int argc, char **argv) { test_setup_logging(LOG_DEBUG); - test_table(resolv_conf_mode, RESOLV_CONF_MODE); - test_table(timezone_mode, TIMEZONE_MODE); + test_table(ResolvConfMode, resolv_conf_mode, RESOLV_CONF_MODE); + test_table(TimezoneMode, timezone_mode, TIMEZONE_MODE); return 0; } diff --git a/src/resolve/test-resolve-tables.c b/src/resolve/test-resolve-tables.c index 6b8618103f9..ec660379f44 100644 --- a/src/resolve/test-resolve-tables.c +++ b/src/resolve/test-resolve-tables.c @@ -11,12 +11,12 @@ int main(int argc, char **argv) { test_setup_logging(LOG_DEBUG); - test_table(dns_protocol, DNS_PROTOCOL); - test_table(dnssec_result, DNSSEC_RESULT); - test_table(dnssec_verdict, DNSSEC_VERDICT); + test_table(DnsProtocol, dns_protocol, DNS_PROTOCOL); + test_table(DnssecResult, dnssec_result, DNSSEC_RESULT); + test_table(DnssecVerdict, dnssec_verdict, DNSSEC_VERDICT); - test_table_sparse(dns_rcode, DNS_RCODE); - test_table_sparse(dns_type, DNS_TYPE); + test_table_sparse(int, dns_rcode, DNS_RCODE); + test_table_sparse(int, dns_type, DNS_TYPE); log_info("/* DNS_TYPE */"); for (i = 0; i < _DNS_TYPE_MAX; i++) { diff --git a/src/shared/test-tables.h b/src/shared/test-tables.h index 3f20318db16..bf8c9134378 100644 --- a/src/shared/test-tables.h +++ b/src/shared/test-tables.h @@ -9,10 +9,10 @@ #include "string-util.h" -#define _test_table(name, lookup, reverse, size, sparse) \ - for (int64_t _i = -EINVAL, _boring = 0; _i < size + 1; _i++) { \ +#define _test_table(type, name, lookup, reverse, size, sparse) \ + for (type _i = -EINVAL, _boring = 0; _i < size + 1; _i++) { \ const char* _val; \ - int64_t _rev; \ + type _rev; \ \ _val = lookup(_i); \ if (_val) { \ @@ -23,7 +23,7 @@ _boring += _i >= 0; \ } \ if (_boring == 0 || _i == size) \ - printf("%s: %" PRIi64 " → %s → %" PRIi64 "\n", name, _i, strnull(_val), _rev); \ + printf("%s: %" PRIi64 " → %s → %" PRIi64 "\n", name, (int64_t) _i, strnull(_val), (int64_t) _rev); \ else if (_boring == 1) \ printf("%*s ...\n", (int) strlen(name), ""); \ \ @@ -36,8 +36,8 @@ assert_se(!_val && _rev == -EINVAL); \ } -#define test_table(lower, upper) \ - _test_table(STRINGIFY(lower), lower##_to_string, lower##_from_string, _##upper##_MAX, false) +#define test_table(type, lower, upper) \ + _test_table(type, STRINGIFY(lower), lower##_to_string, lower##_from_string, _##upper##_MAX, false) -#define test_table_sparse(lower, upper) \ - _test_table(STRINGIFY(lower), lower##_to_string, lower##_from_string, _##upper##_MAX, true) +#define test_table_sparse(type, lower, upper) \ + _test_table(type, STRINGIFY(lower), lower##_to_string, lower##_from_string, _##upper##_MAX, true) diff --git a/src/test/test-tables.c b/src/test/test-tables.c index cd05b179890..d6e38c04d25 100644 --- a/src/test/test-tables.c +++ b/src/test/test-tables.c @@ -44,87 +44,87 @@ int main(int argc, char **argv) { test_setup_logging(LOG_DEBUG); - test_table(architecture, ARCHITECTURE); - test_table(assert_type, CONDITION_TYPE); - test_table(automount_result, AUTOMOUNT_RESULT); - test_table(automount_state, AUTOMOUNT_STATE); - test_table(cgroup_controller, CGROUP_CONTROLLER); - test_table(cgroup_device_policy, CGROUP_DEVICE_POLICY); - test_table(cgroup_io_limit_type, CGROUP_IO_LIMIT_TYPE); - test_table(collect_mode, COLLECT_MODE); - test_table(condition_result, CONDITION_RESULT); - test_table(condition_type, CONDITION_TYPE); - test_table(confidential_virtualization, CONFIDENTIAL_VIRTUALIZATION); - test_table(device_action, SD_DEVICE_ACTION); - test_table(device_state, DEVICE_STATE); - test_table(dns_over_tls_mode, DNS_OVER_TLS_MODE); - test_table(dnssec_mode, DNSSEC_MODE); - test_table(emergency_action, EMERGENCY_ACTION); - test_table(exec_directory_type, EXEC_DIRECTORY_TYPE); - test_table(exec_input, EXEC_INPUT); - test_table(exec_keyring_mode, EXEC_KEYRING_MODE); - test_table(exec_output, EXEC_OUTPUT); - test_table(exec_preserve_mode, EXEC_PRESERVE_MODE); - test_table(exec_utmp_mode, EXEC_UTMP_MODE); - test_table(image_type, IMAGE_TYPE); - test_table(import_verify, IMPORT_VERIFY); - test_table(job_mode, JOB_MODE); - test_table(job_result, JOB_RESULT); - test_table(job_state, JOB_STATE); - test_table(job_type, JOB_TYPE); - test_table(kill_mode, KILL_MODE); - test_table(kill_whom, KILL_WHOM); - test_table(locale_variable, VARIABLE_LC); - test_table(log_target, LOG_TARGET); - test_table(managed_oom_mode, MANAGED_OOM_MODE); - test_table(managed_oom_preference, MANAGED_OOM_PREFERENCE); - test_table(manager_state, MANAGER_STATE); - test_table(manager_timestamp, MANAGER_TIMESTAMP); - test_table(mount_exec_command, MOUNT_EXEC_COMMAND); - test_table(mount_result, MOUNT_RESULT); - test_table(mount_state, MOUNT_STATE); - test_table(name_policy, NAMEPOLICY); - test_table(notify_access, NOTIFY_ACCESS); - test_table(notify_state, NOTIFY_STATE); - test_table(output_mode, OUTPUT_MODE); - test_table(partition_designator, PARTITION_DESIGNATOR); - test_table(path_result, PATH_RESULT); - test_table(path_state, PATH_STATE); - test_table(path_type, PATH_TYPE); - test_table(protect_home, PROTECT_HOME); - test_table(protect_system, PROTECT_SYSTEM); - test_table(resolve_support, RESOLVE_SUPPORT); - test_table(rlimit, RLIMIT); - test_table(scope_result, SCOPE_RESULT); - test_table(scope_state, SCOPE_STATE); - test_table(service_exec_command, SERVICE_EXEC_COMMAND); - test_table(service_restart, SERVICE_RESTART); - test_table(service_restart_mode, SERVICE_RESTART_MODE); - test_table(service_result, SERVICE_RESULT); - test_table(service_state, SERVICE_STATE); - test_table(service_type, SERVICE_TYPE); - test_table(show_status, SHOW_STATUS); - test_table(slice_state, SLICE_STATE); - test_table(socket_address_bind_ipv6_only, SOCKET_ADDRESS_BIND_IPV6_ONLY); - test_table(socket_exec_command, SOCKET_EXEC_COMMAND); - test_table(socket_result, SOCKET_RESULT); - test_table(socket_state, SOCKET_STATE); - test_table(swap_exec_command, SWAP_EXEC_COMMAND); - test_table(swap_result, SWAP_RESULT); - test_table(swap_state, SWAP_STATE); - test_table(target_state, TARGET_STATE); - test_table(timer_base, TIMER_BASE); - test_table(timer_result, TIMER_RESULT); - test_table(timer_state, TIMER_STATE); - test_table(unit_active_state, UNIT_ACTIVE_STATE); - test_table(unit_dependency, UNIT_DEPENDENCY); - test_table(install_change_type, INSTALL_CHANGE_TYPE); - test_table(unit_file_preset_mode, UNIT_FILE_PRESET_MODE); - test_table(unit_file_state, UNIT_FILE_STATE); - test_table(unit_load_state, UNIT_LOAD_STATE); - test_table(unit_type, UNIT_TYPE); - test_table(virtualization, VIRTUALIZATION); - test_table(compression, COMPRESSION); + test_table(Architecture, architecture, ARCHITECTURE); + test_table(ConditionType, assert_type, CONDITION_TYPE); + test_table(AutomountResult, automount_result, AUTOMOUNT_RESULT); + test_table(AutomountState, automount_state, AUTOMOUNT_STATE); + test_table(CGroupController, cgroup_controller, CGROUP_CONTROLLER); + test_table(CGroupDevicePolicy, cgroup_device_policy, CGROUP_DEVICE_POLICY); + test_table(CGroupIOLimitType, cgroup_io_limit_type, CGROUP_IO_LIMIT_TYPE); + test_table(CollectMode, collect_mode, COLLECT_MODE); + test_table(ConditionResult, condition_result, CONDITION_RESULT); + test_table(ConditionType, condition_type, CONDITION_TYPE); + test_table(ConfidentialVirtualization, confidential_virtualization, CONFIDENTIAL_VIRTUALIZATION); + test_table(sd_device_action_t, device_action, SD_DEVICE_ACTION); + test_table(DeviceState, device_state, DEVICE_STATE); + test_table(DnsOverTlsMode, dns_over_tls_mode, DNS_OVER_TLS_MODE); + test_table(DnssecMode, dnssec_mode, DNSSEC_MODE); + test_table(EmergencyAction, emergency_action, EMERGENCY_ACTION); + test_table(ExecDirectoryType, exec_directory_type, EXEC_DIRECTORY_TYPE); + test_table(ExecInput, exec_input, EXEC_INPUT); + test_table(ExecKeyringMode, exec_keyring_mode, EXEC_KEYRING_MODE); + test_table(ExecOutput, exec_output, EXEC_OUTPUT); + test_table(ExecPreserveMode, exec_preserve_mode, EXEC_PRESERVE_MODE); + test_table(ExecUtmpMode, exec_utmp_mode, EXEC_UTMP_MODE); + test_table(ImageType, image_type, IMAGE_TYPE); + test_table(ImportVerify, import_verify, IMPORT_VERIFY); + test_table(JobMode, job_mode, JOB_MODE); + test_table(JobResult, job_result, JOB_RESULT); + test_table(JobState, job_state, JOB_STATE); + test_table(JobType, job_type, JOB_TYPE); + test_table(KillMode, kill_mode, KILL_MODE); + test_table(KillWhom, kill_whom, KILL_WHOM); + test_table(LocaleVariable, locale_variable, VARIABLE_LC); + test_table(LogTarget, log_target, LOG_TARGET); + test_table(ManagedOOMMode, managed_oom_mode, MANAGED_OOM_MODE); + test_table(ManagedOOMPreference, managed_oom_preference, MANAGED_OOM_PREFERENCE); + test_table(ManagerState, manager_state, MANAGER_STATE); + test_table(ManagerTimestamp, manager_timestamp, MANAGER_TIMESTAMP); + test_table(MountExecCommand, mount_exec_command, MOUNT_EXEC_COMMAND); + test_table(MountResult, mount_result, MOUNT_RESULT); + test_table(MountState, mount_state, MOUNT_STATE); + test_table(NamePolicy, name_policy, NAMEPOLICY); + test_table(NotifyAccess, notify_access, NOTIFY_ACCESS); + test_table(NotifyState, notify_state, NOTIFY_STATE); + test_table(OutputMode, output_mode, OUTPUT_MODE); + test_table(PartitionDesignator, partition_designator, PARTITION_DESIGNATOR); + test_table(PathResult, path_result, PATH_RESULT); + test_table(PathState, path_state, PATH_STATE); + test_table(PathType, path_type, PATH_TYPE); + test_table(ProtectHome, protect_home, PROTECT_HOME); + test_table(ProtectSystem, protect_system, PROTECT_SYSTEM); + test_table(ResolveSupport, resolve_support, RESOLVE_SUPPORT); + test_table(int, rlimit, RLIMIT); + test_table(ScopeResult, scope_result, SCOPE_RESULT); + test_table(ScopeState, scope_state, SCOPE_STATE); + test_table(ServiceExecCommand, service_exec_command, SERVICE_EXEC_COMMAND); + test_table(ServiceRestart, service_restart, SERVICE_RESTART); + test_table(ServiceRestartMode, service_restart_mode, SERVICE_RESTART_MODE); + test_table(ServiceResult, service_result, SERVICE_RESULT); + test_table(ServiceState, service_state, SERVICE_STATE); + test_table(ServiceType, service_type, SERVICE_TYPE); + test_table(ShowStatus, show_status, SHOW_STATUS); + test_table(SliceState, slice_state, SLICE_STATE); + test_table(SocketAddressBindIPv6Only, socket_address_bind_ipv6_only, SOCKET_ADDRESS_BIND_IPV6_ONLY); + test_table(SocketExecCommand, socket_exec_command, SOCKET_EXEC_COMMAND); + test_table(SocketResult, socket_result, SOCKET_RESULT); + test_table(SocketState, socket_state, SOCKET_STATE); + test_table(SwapExecCommand, swap_exec_command, SWAP_EXEC_COMMAND); + test_table(SwapResult, swap_result, SWAP_RESULT); + test_table(SwapState, swap_state, SWAP_STATE); + test_table(TargetState, target_state, TARGET_STATE); + test_table(TimerBase, timer_base, TIMER_BASE); + test_table(TimerResult, timer_result, TIMER_RESULT); + test_table(TimerState, timer_state, TIMER_STATE); + test_table(UnitActiveState, unit_active_state, UNIT_ACTIVE_STATE); + test_table(UnitDependency, unit_dependency, UNIT_DEPENDENCY); + test_table(InstallChangeType, install_change_type, INSTALL_CHANGE_TYPE); + test_table(UnitFilePresetMode, unit_file_preset_mode, UNIT_FILE_PRESET_MODE); + test_table(UnitFileState, unit_file_state, UNIT_FILE_STATE); + test_table(UnitLoadState, unit_load_state, UNIT_LOAD_STATE); + test_table(UnitType, unit_type, UNIT_TYPE); + test_table(Virtualization, virtualization, VIRTUALIZATION); + test_table(Compression, compression, COMPRESSION); assert_cc(sizeof(sd_device_action_t) == sizeof(int64_t)); diff --git a/src/udev/net/test-link-config-tables.c b/src/udev/net/test-link-config-tables.c index a4332320f68..499778505c4 100644 --- a/src/udev/net/test-link-config-tables.c +++ b/src/udev/net/test-link-config-tables.c @@ -7,7 +7,7 @@ int main(int argc, char **argv) { test_setup_logging(LOG_DEBUG); - test_table(mac_address_policy, MAC_ADDRESS_POLICY); + test_table(MACAddressPolicy, mac_address_policy, MAC_ADDRESS_POLICY); return EXIT_SUCCESS; } From 9f23910935d23254d3b6f580a5a37e0c6c2cc5a8 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Wed, 21 May 2025 11:59:16 +0200 Subject: [PATCH 3/5] tests: Move implementation of DEFINE_HEX_PTR() into source file Otherwise we have to add a hexdecoct.h include to tests.h which we want to avoid. --- src/shared/tests.c | 5 +++++ src/shared/tests.h | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/shared/tests.c b/src/shared/tests.c index c2feab93355..66e1ae88cd6 100644 --- a/src/shared/tests.c +++ b/src/shared/tests.c @@ -20,6 +20,7 @@ #include "env-util.h" #include "fd-util.h" #include "fs-util.h" +#include "hexdecoct.h" #include "log.h" #include "mountpoint-util.h" #include "namespace-util.h" @@ -327,6 +328,10 @@ int enter_cgroup_root(char **ret_cgroup) { return enter_cgroup(ret_cgroup, false); } +int define_hex_ptr_internal(const char *hex, void **name, size_t *name_len) { + return unhexmem_full(hex, strlen_ptr(hex), false, name, name_len); +} + const char* ci_environment(void) { /* We return a string because we might want to provide multiple bits of information later on: not * just the general CI environment type, but also whether we're sanitizing or not, etc. The caller is diff --git a/src/shared/tests.h b/src/shared/tests.h index c9ebafdd83b..f339e25aef0 100644 --- a/src/shared/tests.h +++ b/src/shared/tests.h @@ -86,11 +86,13 @@ bool userns_has_single_user(void); #define CAN_MEMLOCK_SIZE (512 * 1024U) bool can_memlock(void); +int define_hex_ptr_internal(const char *hex, void **name, size_t *name_len); + /* Define void* buffer and size_t length variables from a hex string. */ #define DEFINE_HEX_PTR(name, hex) \ _cleanup_free_ void *name = NULL; \ size_t name##_len = 0; \ - assert_se(unhexmem_full(hex, strlen_ptr(hex), false, &name, &name##_len) >= 0); + assert_se(define_hex_ptr_internal(hex, &name, &name##_len) >= 0) /* Provide a convenient way to check if we're running in CI. */ const char* ci_environment(void); From cdd5fac06856a70ce0afea99cd7b3a2ca50314a0 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Wed, 21 May 2025 12:22:40 +0200 Subject: [PATCH 4/5] tree-wide: Include via libaudit-util.h Let's keep the ifdeffery for the include in one place. --- src/core/audit-fd.c | 15 ++------------- src/core/manager.c | 5 +---- src/core/selinux-access.c | 4 +--- src/shared/libaudit-util.c | 13 +++++-------- src/shared/libaudit-util.h | 4 ++++ src/sysusers/sysusers.c | 4 ---- src/update-utmp/update-utmp.c | 4 ---- 7 files changed, 13 insertions(+), 36 deletions(-) diff --git a/src/core/audit-fd.c b/src/core/audit-fd.c index b09f3ac49a5..6150e7e43e5 100644 --- a/src/core/audit-fd.c +++ b/src/core/audit-fd.c @@ -3,19 +3,13 @@ #include #include "audit-fd.h" - -#if HAVE_AUDIT -# include - -# include "libaudit-util.h" -# include "capability-util.h" +#include "capability-util.h" +#include "libaudit-util.h" static bool initialized = false; static int audit_fd = -EBADF; -#endif int get_core_audit_fd(void) { -#if HAVE_AUDIT if (!initialized) { if (have_effective_cap(CAP_AUDIT_WRITE) <= 0) audit_fd = -EPERM; @@ -26,15 +20,10 @@ int get_core_audit_fd(void) { } return audit_fd; -#else - return -EAFNOSUPPORT; -#endif } void close_core_audit_fd(void) { -#if HAVE_AUDIT close_audit_fd(audit_fd); initialized = true; audit_fd = -ECONNRESET; -#endif } diff --git a/src/core/manager.c b/src/core/manager.c index 1e264a361d7..5b3d41596bb 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -12,10 +12,6 @@ #include #include -#if HAVE_AUDIT -#include -#endif - #include "sd-daemon.h" #include "sd-messages.h" #include "sd-path.h" @@ -58,6 +54,7 @@ #include "io-util.h" #include "iovec-util.h" #include "label-util.h" +#include "libaudit-util.h" #include "load-fragment.h" #include "locale-setup.h" #include "log.h" diff --git a/src/core/selinux-access.c b/src/core/selinux-access.c index 8ab488390cb..189bded2800 100644 --- a/src/core/selinux-access.c +++ b/src/core/selinux-access.c @@ -7,9 +7,6 @@ #include #include #include -#if HAVE_AUDIT -#include -#endif #include "sd-bus.h" @@ -18,6 +15,7 @@ #include "bus-util.h" #include "errno-util.h" #include "format-util.h" +#include "libaudit-util.h" #include "log.h" #include "path-util.h" #include "selinux-util.h" diff --git a/src/shared/libaudit-util.c b/src/shared/libaudit-util.c index e84684e581e..da85c84d203 100644 --- a/src/shared/libaudit-util.c +++ b/src/shared/libaudit-util.c @@ -6,10 +6,6 @@ #include #include -#if HAVE_AUDIT -# include -#endif - #include "fd-util.h" #include "iovec-util.h" #include "libaudit-util.h" @@ -100,14 +96,15 @@ int close_audit_fd(int fd) { } int open_audit_fd_or_warn(void) { - int fd = -EBADF; - #if HAVE_AUDIT /* If the kernel lacks netlink or audit support, don't worry about it. */ - fd = audit_open(); + int fd = audit_open(); if (fd < 0) return log_full_errno(ERRNO_IS_NOT_SUPPORTED(errno) ? LOG_DEBUG : LOG_WARNING, errno, "Failed to connect to audit log, ignoring: %m"); -#endif + return fd; +#else + return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "libaudit support not compiled in"); +#endif } diff --git a/src/shared/libaudit-util.h b/src/shared/libaudit-util.h index f00d816f78e..1887e180c22 100644 --- a/src/shared/libaudit-util.h +++ b/src/shared/libaudit-util.h @@ -1,6 +1,10 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once +#if HAVE_AUDIT +# include +#endif + #include bool use_audit(void); diff --git a/src/sysusers/sysusers.c b/src/sysusers/sysusers.c index c9d3870111e..658bc72d85a 100644 --- a/src/sysusers/sysusers.c +++ b/src/sysusers/sysusers.c @@ -4,10 +4,6 @@ #include #include -#if HAVE_AUDIT -# include -#endif - #include "alloc-util.h" #include "build.h" #include "chase.h" diff --git a/src/update-utmp/update-utmp.c b/src/update-utmp/update-utmp.c index 8c7a83ccb62..914c5ea3223 100644 --- a/src/update-utmp/update-utmp.c +++ b/src/update-utmp/update-utmp.c @@ -2,10 +2,6 @@ #include -#if HAVE_AUDIT -# include -#endif - #include "sd-bus.h" #include "alloc-util.h" From 19716312945cefff6196a141e468781f30d7aa6f Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Wed, 21 May 2025 13:08:50 +0200 Subject: [PATCH 5/5] meson: Don't define targets for standalone binaries if option is not enabled These are built from the same sources as the regular binaries, so we end up with the same sources multiple times in the compilation database but with different command line arguments, which trips up tooling that uses the compilation database. Let's not define the standalone targets if the option is not enabled to avoid this problem. --- meson.build | 4 ++++ src/repart/meson.build | 4 +--- src/shutdown/meson.build | 2 -- src/sysusers/meson.build | 4 +--- src/tmpfiles/meson.build | 4 +--- 5 files changed, 7 insertions(+), 11 deletions(-) diff --git a/meson.build b/meson.build index 5dd5f348aa7..733390ae8f6 100644 --- a/meson.build +++ b/meson.build @@ -2424,6 +2424,10 @@ foreach dict : executables continue endif + if name.endswith('.standalone') and not have_standalone_binaries + continue + endif + kwargs = {} foreach key, val : dict if key in ['name', 'dbus', 'public', 'conditions', diff --git a/src/repart/meson.build b/src/repart/meson.build index 9c5778dffdc..369410bab8d 100644 --- a/src/repart/meson.build +++ b/src/repart/meson.build @@ -22,7 +22,7 @@ executables += [ }, executable_template + { 'name' : 'systemd-repart.standalone', - 'public' : have_standalone_binaries, + 'public' : true, 'sources' : files('repart.c'), 'c_args' : '-DSTANDALONE', 'link_with' : [ @@ -37,8 +37,6 @@ executables += [ libopenssl, threads, ], - 'build_by_default' : have_standalone_binaries, - 'install' : have_standalone_binaries, }, ] diff --git a/src/shutdown/meson.build b/src/shutdown/meson.build index c91253ca8e0..c67d19abcc5 100644 --- a/src/shutdown/meson.build +++ b/src/shutdown/meson.build @@ -28,8 +28,6 @@ executables += [ libsystemd_static, ], 'dependencies' : libmount, - 'build_by_default' : have_standalone_binaries, - 'install' : have_standalone_binaries, }, test_template + { 'sources' : files('test-umount.c'), diff --git a/src/sysusers/meson.build b/src/sysusers/meson.build index c968f55110c..88494d8cfce 100644 --- a/src/sysusers/meson.build +++ b/src/sysusers/meson.build @@ -13,7 +13,7 @@ executables += [ }, executable_template + { 'name' : 'systemd-sysusers.standalone', - 'public' : have_standalone_binaries, + 'public' : true, 'sources' : files('sysusers.c'), 'c_args' : '-DSTANDALONE', 'link_with' : [ @@ -22,7 +22,5 @@ executables += [ libsystemd_static, ], 'dependencies' : libaudit, - 'build_by_default' : have_standalone_binaries, - 'install' : have_standalone_binaries, }, ] diff --git a/src/tmpfiles/meson.build b/src/tmpfiles/meson.build index 1016573969a..404f265f918 100644 --- a/src/tmpfiles/meson.build +++ b/src/tmpfiles/meson.build @@ -21,7 +21,7 @@ executables += [ }, executable_template + { 'name' : 'systemd-tmpfiles.standalone', - 'public' : have_standalone_binaries, + 'public' : true, 'sources' : systemd_tmpfiles_sources + systemd_tmpfiles_extract_sources, 'c_args' : '-DSTANDALONE', 'link_with' : [ @@ -30,8 +30,6 @@ executables += [ libsystemd_static, ], 'dependencies' : libacl, - 'build_by_default' : have_standalone_binaries, - 'install' : have_standalone_binaries, }, test_template + { 'sources' : files('test-offline-passwd.c'),