From 2bf5b262b581187b28dad4d015955888290b9e3d Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Thu, 19 Sep 2024 13:48:05 +0200 Subject: [PATCH 01/17] basic/unit-file: move to shared/ Preparation for later commits, where path-lookup would be moved into libsystemd. Note that it currently includes sd-id128.h, hence shared/ seems more appropriate anyway. --- src/basic/meson.build | 1 - src/shared/meson.build | 1 + src/{basic => shared}/unit-file.c | 0 src/{basic => shared}/unit-file.h | 0 4 files changed, 1 insertion(+), 1 deletion(-) rename src/{basic => shared}/unit-file.c (100%) rename src/{basic => shared}/unit-file.h (100%) diff --git a/src/basic/meson.build b/src/basic/meson.build index 4f83eb67a0..9a3361bb07 100644 --- a/src/basic/meson.build +++ b/src/basic/meson.build @@ -106,7 +106,6 @@ basic_sources = files( 'uid-classification.c', 'uid-range.c', 'unit-def.c', - 'unit-file.c', 'unit-name.c', 'user-util.c', 'utf8.c', diff --git a/src/shared/meson.build b/src/shared/meson.build index 8fc39429e5..7b519e9ee8 100644 --- a/src/shared/meson.build +++ b/src/shared/meson.build @@ -170,6 +170,7 @@ shared_sources = files( 'tpm2-util.c', 'tpm2-event-log.c', 'udev-util.c', + 'unit-file.c', 'user-record-nss.c', 'user-record-show.c', 'user-record.c', diff --git a/src/basic/unit-file.c b/src/shared/unit-file.c similarity index 100% rename from src/basic/unit-file.c rename to src/shared/unit-file.c diff --git a/src/basic/unit-file.h b/src/shared/unit-file.h similarity index 100% rename from src/basic/unit-file.h rename to src/shared/unit-file.h From 1a7627f4e1058f7518e970e289ed020f3d329b22 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Thu, 19 Sep 2024 13:53:25 +0200 Subject: [PATCH 02/17] unit-file: make unit_type_may_{alias_template} static inline --- src/shared/unit-file.c | 19 ------------------- src/shared/unit-file.h | 20 ++++++++++++++++++-- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/shared/unit-file.c b/src/shared/unit-file.c index ca2686b209..5bb580285c 100644 --- a/src/shared/unit-file.c +++ b/src/shared/unit-file.c @@ -16,25 +16,6 @@ #include "strv.h" #include "unit-file.h" -bool unit_type_may_alias(UnitType type) { - return IN_SET(type, - UNIT_SERVICE, - UNIT_SOCKET, - UNIT_TARGET, - UNIT_DEVICE, - UNIT_TIMER, - UNIT_PATH); -} - -bool unit_type_may_template(UnitType type) { - return IN_SET(type, - UNIT_SERVICE, - UNIT_SOCKET, - UNIT_TARGET, - UNIT_TIMER, - UNIT_PATH); -} - int unit_symlink_name_compatible(const char *symlink, const char *target, bool instance_propagation) { _cleanup_free_ char *template = NULL; int r, un_type1, un_type2; diff --git a/src/shared/unit-file.h b/src/shared/unit-file.h index 1c43861f00..1c89a92c7d 100644 --- a/src/shared/unit-file.h +++ b/src/shared/unit-file.h @@ -28,8 +28,24 @@ enum UnitFileState { _UNIT_FILE_STATE_INVALID = -EINVAL, }; -bool unit_type_may_alias(UnitType type) _const_; -bool unit_type_may_template(UnitType type) _const_; +static inline bool unit_type_may_alias(UnitType type) { + return IN_SET(type, + UNIT_SERVICE, + UNIT_SOCKET, + UNIT_TARGET, + UNIT_DEVICE, + UNIT_TIMER, + UNIT_PATH); +} + +static inline bool unit_type_may_template(UnitType type) { + return IN_SET(type, + UNIT_SERVICE, + UNIT_SOCKET, + UNIT_TARGET, + UNIT_TIMER, + UNIT_PATH); +} int unit_symlink_name_compatible(const char *symlink, const char *target, bool instance_propagation); int unit_validate_alias_symlink_or_warn(int log_level, const char *filename, const char *target); From cd7d732dc54ed3bcd2dd22273473bc410f501226 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Fri, 4 Oct 2024 12:56:34 +0200 Subject: [PATCH 03/17] path-lookup: move NETWORK_DIRS to network-util.h --- src/basic/path-lookup.h | 3 --- src/libsystemd/sd-network/network-util.h | 4 ++++ src/libsystemd/sd-path/sd-path.c | 1 + src/network/netdev/netdev.c | 1 + src/network/networkctl-config-file.c | 1 + src/network/networkd-network.c | 1 + src/udev/net/link-config.c | 1 + 7 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/basic/path-lookup.h b/src/basic/path-lookup.h index 0db2c5a98c..fab6edd6ca 100644 --- a/src/basic/path-lookup.h +++ b/src/basic/path-lookup.h @@ -70,8 +70,5 @@ void lookup_paths_done(LookupPaths *p); char **generator_binary_paths(RuntimeScope scope); char **env_generator_binary_paths(RuntimeScope scope); -#define NETWORK_DIRS ((const char* const*) CONF_PATHS_STRV("systemd/network")) -#define NETWORK_DIRS_NULSTR CONF_PATHS_NULSTR("systemd/network") - #define PORTABLE_PROFILE_DIRS CONF_PATHS_NULSTR("systemd/portable/profile") int find_portable_profile(const char *name, const char *unit, char **ret_path); diff --git a/src/libsystemd/sd-network/network-util.h b/src/libsystemd/sd-network/network-util.h index 6fc6015902..14c6f6819d 100644 --- a/src/libsystemd/sd-network/network-util.h +++ b/src/libsystemd/sd-network/network-util.h @@ -4,8 +4,12 @@ #include #include +#include "constants.h" #include "macro.h" +#define NETWORK_DIRS ((const char* const*) CONF_PATHS_STRV("systemd/network")) +#define NETWORK_DIRS_NULSTR CONF_PATHS_NULSTR("systemd/network") + bool network_is_online(void); typedef enum AddressFamily { diff --git a/src/libsystemd/sd-path/sd-path.c b/src/libsystemd/sd-path/sd-path.c index ebeeec1194..096f3a1156 100644 --- a/src/libsystemd/sd-path/sd-path.c +++ b/src/libsystemd/sd-path/sd-path.c @@ -7,6 +7,7 @@ #include "fd-util.h" #include "fileio.h" #include "fs-util.h" +#include "network-util.h" #include "nulstr-util.h" #include "path-lookup.h" #include "path-util.h" diff --git a/src/network/netdev/netdev.c b/src/network/netdev/netdev.c index 53f74f6e8b..8e58a1ae12 100644 --- a/src/network/netdev/netdev.c +++ b/src/network/netdev/netdev.c @@ -29,6 +29,7 @@ #include "netdevsim.h" #include "netif-util.h" #include "netlink-util.h" +#include "network-util.h" #include "networkd-manager.h" #include "networkd-queue.h" #include "networkd-setlink.h" diff --git a/src/network/networkctl-config-file.c b/src/network/networkctl-config-file.c index eeddfb2281..7ef7668f30 100644 --- a/src/network/networkctl-config-file.c +++ b/src/network/networkctl-config-file.c @@ -15,6 +15,7 @@ #include "edit-util.h" #include "mkdir-label.h" #include "netlink-util.h" +#include "network-util.h" #include "networkctl.h" #include "networkctl-config-file.h" #include "networkctl-util.h" diff --git a/src/network/networkd-network.c b/src/network/networkd-network.c index b6d34c4a9c..1fff264036 100644 --- a/src/network/networkd-network.c +++ b/src/network/networkd-network.c @@ -15,6 +15,7 @@ #include "in-addr-util.h" #include "net-condition.h" #include "netdev/macvlan.h" +#include "network-util.h" #include "networkd-address-label.h" #include "networkd-address.h" #include "networkd-bridge-fdb.h" diff --git a/src/udev/net/link-config.c b/src/udev/net/link-config.c index 645c7f840a..edfbb38ad8 100644 --- a/src/udev/net/link-config.c +++ b/src/udev/net/link-config.c @@ -27,6 +27,7 @@ #include "netif-sriov.h" #include "netif-util.h" #include "netlink-util.h" +#include "network-util.h" #include "parse-util.h" #include "path-lookup.h" #include "path-util.h" From feae34d64ea3e037a2a6b631b768ea36c90d30bb Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Fri, 4 Oct 2024 12:57:23 +0200 Subject: [PATCH 04/17] path-lookup: move find_portable_profile() to portable-util --- src/analyze/analyze-security.c | 1 + src/basic/path-lookup.c | 28 ---------------------------- src/basic/path-lookup.h | 5 ----- src/portable/portable.c | 1 + src/shared/meson.build | 1 + src/shared/portable-util.c | 34 ++++++++++++++++++++++++++++++++++ src/shared/portable-util.h | 9 +++++++++ 7 files changed, 46 insertions(+), 33 deletions(-) create mode 100644 src/shared/portable-util.c create mode 100644 src/shared/portable-util.h diff --git a/src/analyze/analyze-security.c b/src/analyze/analyze-security.c index f7e9b94213..fb4fd654f5 100644 --- a/src/analyze/analyze-security.c +++ b/src/analyze/analyze-security.c @@ -26,6 +26,7 @@ #include "nulstr-util.h" #include "parse-util.h" #include "path-util.h" +#include "portable-util.h" #include "pretty-print.h" #include "seccomp-util.h" #include "service.h" diff --git a/src/basic/path-lookup.c b/src/basic/path-lookup.c index 234b197394..d4d888c362 100644 --- a/src/basic/path-lookup.c +++ b/src/basic/path-lookup.c @@ -901,31 +901,3 @@ char **env_generator_binary_paths(RuntimeScope runtime_scope) { return TAKE_PTR(paths); } - -int find_portable_profile(const char *name, const char *unit, char **ret_path) { - const char *dot; - int r; - - assert(name); - assert(ret_path); - - assert_se(dot = strrchr(unit, '.')); - - NULSTR_FOREACH(p, PORTABLE_PROFILE_DIRS) { - _cleanup_free_ char *joined = NULL; - - joined = strjoin(p, "/", name, "/", dot + 1, ".conf"); - if (!joined) - return -ENOMEM; - - r = access_nofollow(joined, F_OK); - if (r >= 0) { - *ret_path = TAKE_PTR(joined); - return 0; - } - if (r != -ENOENT) - return r; - } - - return -ENOENT; -} diff --git a/src/basic/path-lookup.h b/src/basic/path-lookup.h index fab6edd6ca..1176ad8871 100644 --- a/src/basic/path-lookup.h +++ b/src/basic/path-lookup.h @@ -3,8 +3,6 @@ #include -#include "constants.h" -#include "macro.h" #include "runtime-scope.h" typedef enum LookupPathsFlags { @@ -69,6 +67,3 @@ void lookup_paths_done(LookupPaths *p); char **generator_binary_paths(RuntimeScope scope); char **env_generator_binary_paths(RuntimeScope scope); - -#define PORTABLE_PROFILE_DIRS CONF_PATHS_NULSTR("systemd/portable/profile") -int find_portable_profile(const char *name, const char *unit, char **ret_path); diff --git a/src/portable/portable.c b/src/portable/portable.c index f0c508c306..b1055aa8c3 100644 --- a/src/portable/portable.c +++ b/src/portable/portable.c @@ -32,6 +32,7 @@ #include "os-util.h" #include "path-lookup.h" #include "portable.h" +#include "portable-util.h" #include "process-util.h" #include "rm-rf.h" #include "selinux-util.h" diff --git a/src/shared/meson.build b/src/shared/meson.build index 7b519e9ee8..e759293364 100644 --- a/src/shared/meson.build +++ b/src/shared/meson.build @@ -144,6 +144,7 @@ shared_sources = files( 'pkcs11-util.c', 'plymouth-util.c', 'polkit-agent.c', + 'portable-util.c', 'pretty-print.c', 'capsule-util.c', 'ptyfwd.c', diff --git a/src/shared/portable-util.c b/src/shared/portable-util.c new file mode 100644 index 0000000000..85c128a1b4 --- /dev/null +++ b/src/shared/portable-util.c @@ -0,0 +1,34 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include "fs-util.h" +#include "nulstr-util.h" +#include "portable-util.h" +#include "string-util.h" + +int find_portable_profile(const char *name, const char *unit, char **ret_path) { + const char *dot; + int r; + + assert(name); + assert(ret_path); + + assert_se(dot = strrchr(unit, '.')); + + NULSTR_FOREACH(p, PORTABLE_PROFILE_DIRS) { + _cleanup_free_ char *joined = NULL; + + joined = strjoin(p, "/", name, "/", dot + 1, ".conf"); + if (!joined) + return -ENOMEM; + + r = access_nofollow(joined, F_OK); + if (r >= 0) { + *ret_path = TAKE_PTR(joined); + return 0; + } + if (r != -ENOENT) + return r; + } + + return -ENOENT; +} diff --git a/src/shared/portable-util.h b/src/shared/portable-util.h new file mode 100644 index 0000000000..2c89fe3eb8 --- /dev/null +++ b/src/shared/portable-util.h @@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +#include "constants.h" +#include "macro.h" + +#define PORTABLE_PROFILE_DIRS CONF_PATHS_NULSTR("systemd/portable/profile") + +int find_portable_profile(const char *name, const char *unit, char **ret_path); From f005e267bc09e3cc78576a960434890892c2b517 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Sun, 15 Sep 2024 20:57:03 +0200 Subject: [PATCH 05/17] path-lookup: move from basic/ to libsystemd/ So that sd_path_lookup() can be utilized to replace duplicate functions. --- meson.build | 1 + src/basic/meson.build | 1 - src/libsystemd/meson.build | 89 ++++++++++++------- .../sd-path}/path-lookup.c | 0 .../sd-path}/path-lookup.h | 0 5 files changed, 60 insertions(+), 31 deletions(-) rename src/{basic => libsystemd/sd-path}/path-lookup.c (100%) rename src/{basic => libsystemd/sd-path}/path-lookup.h (100%) diff --git a/meson.build b/meson.build index e258a65a1a..651fb30584 100644 --- a/meson.build +++ b/meson.build @@ -2092,6 +2092,7 @@ libsystemd_includes = [basic_includes, include_directories( 'src/libsystemd/sd-json', 'src/libsystemd/sd-netlink', 'src/libsystemd/sd-network', + 'src/libsystemd/sd-path', 'src/libsystemd/sd-resolve', 'src/libsystemd/sd-varlink')] diff --git a/src/basic/meson.build b/src/basic/meson.build index 9a3361bb07..e02f787c75 100644 --- a/src/basic/meson.build +++ b/src/basic/meson.build @@ -70,7 +70,6 @@ basic_sources = files( 'ordered-set.c', 'os-util.c', 'parse-util.c', - 'path-lookup.c', 'path-util.c', 'percent-util.c', 'pidref.c', diff --git a/src/libsystemd/meson.build b/src/libsystemd/meson.build index d73c3a8974..fa28aa09fb 100644 --- a/src/libsystemd/meson.build +++ b/src/libsystemd/meson.build @@ -44,7 +44,7 @@ sd_journal_sources += [audit_type_to_name] ############################################################ -id128_sources = files( +sd_id128_sources = files( 'sd-id128/id128-util.c', 'sd-id128/sd-id128.c', ) @@ -62,6 +62,41 @@ sd_event_sources = files( ############################################################ +sd_bus_sources = files( + 'sd-bus/bus-common-errors.c', + 'sd-bus/bus-container.c', + 'sd-bus/bus-control.c', + 'sd-bus/bus-convenience.c', + 'sd-bus/bus-creds.c', + 'sd-bus/bus-dump.c', + 'sd-bus/bus-error.c', + 'sd-bus/bus-internal.c', + 'sd-bus/bus-introspect.c', + 'sd-bus/bus-kernel.c', + 'sd-bus/bus-match.c', + 'sd-bus/bus-message.c', + 'sd-bus/bus-objects.c', + 'sd-bus/bus-signature.c', + 'sd-bus/bus-slot.c', + 'sd-bus/bus-socket.c', + 'sd-bus/bus-track.c', + 'sd-bus/bus-type.c', + 'sd-bus/sd-bus.c', +) + +############################################################ + +sd_device_sources = files( + 'sd-device/device-enumerator.c', + 'sd-device/device-filter.c', + 'sd-device/device-monitor.c', + 'sd-device/device-private.c', + 'sd-device/device-util.c', + 'sd-device/sd-device.c', +) + +############################################################ + sd_login_sources = files('sd-login/sd-login.c') ############################################################ @@ -83,33 +118,14 @@ sd_varlink_sources = files( ############################################################ -libsystemd_sources = files( - 'sd-bus/bus-common-errors.c', - 'sd-bus/bus-container.c', - 'sd-bus/bus-control.c', - 'sd-bus/bus-convenience.c', - 'sd-bus/bus-creds.c', - 'sd-bus/bus-dump.c', - 'sd-bus/bus-error.c', - 'sd-bus/bus-internal.c', - 'sd-bus/bus-introspect.c', - 'sd-bus/bus-kernel.c', - 'sd-bus/bus-match.c', - 'sd-bus/bus-message.c', - 'sd-bus/bus-objects.c', - 'sd-bus/bus-signature.c', - 'sd-bus/bus-slot.c', - 'sd-bus/bus-socket.c', - 'sd-bus/bus-track.c', - 'sd-bus/bus-type.c', - 'sd-bus/sd-bus.c', - 'sd-device/device-enumerator.c', - 'sd-device/device-filter.c', - 'sd-device/device-monitor.c', - 'sd-device/device-private.c', - 'sd-device/device-util.c', - 'sd-device/sd-device.c', - 'sd-hwdb/sd-hwdb.c', +sd_path_sources = files( + 'sd-path/path-lookup.c', + 'sd-path/sd-path.c', +) + +############################################################ + +sd_netlink_sources = files( 'sd-netlink/netlink-genl.c', 'sd-netlink/netlink-message-nfnl.c', 'sd-netlink/netlink-message-rtnl.c', @@ -122,11 +138,24 @@ libsystemd_sources = files( 'sd-netlink/netlink-types.c', 'sd-netlink/netlink-util.c', 'sd-netlink/sd-netlink.c', +) + +############################################################ + +sd_network_sources = files( 'sd-network/network-util.c', 'sd-network/sd-network.c', - 'sd-path/sd-path.c', +) + +############################################################ + +libsystemd_sources = files( + 'sd-hwdb/sd-hwdb.c', 'sd-resolve/sd-resolve.c', -) + sd_journal_sources + id128_sources + sd_daemon_sources + sd_event_sources + sd_login_sources + sd_json_sources + sd_varlink_sources +) + sd_journal_sources + sd_id128_sources + sd_daemon_sources \ + + sd_event_sources + sd_bus_sources + sd_device_sources \ + + sd_login_sources + sd_json_sources + sd_varlink_sources \ + + sd_path_sources + sd_netlink_sources + sd_network_sources libsystemd_c_args = ['-fvisibility=default'] diff --git a/src/basic/path-lookup.c b/src/libsystemd/sd-path/path-lookup.c similarity index 100% rename from src/basic/path-lookup.c rename to src/libsystemd/sd-path/path-lookup.c diff --git a/src/basic/path-lookup.h b/src/libsystemd/sd-path/path-lookup.h similarity index 100% rename from src/basic/path-lookup.h rename to src/libsystemd/sd-path/path-lookup.h From 60cd6deb06d5c70beed1c91fd6c79e2f9e1d13cd Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Fri, 23 Aug 2024 18:55:24 +0200 Subject: [PATCH 06/17] path-lookup: deduplicate xdg_user_*() with sd_path_lookup() While at it, place ret param at last. --- src/core/main.c | 4 +- src/core/manager.c | 2 +- src/core/unit.c | 5 +- src/libsystemd/sd-path/path-lookup.c | 88 ++----------------- src/libsystemd/sd-path/path-lookup.h | 24 +++-- src/tmpfiles/tmpfiles.c | 6 +- src/vmspawn/vmspawn-util.c | 2 +- .../xdg-autostart-generator.c | 2 +- 8 files changed, 36 insertions(+), 97 deletions(-) diff --git a/src/core/main.c b/src/core/main.c index e526c37e6a..0c171a788d 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -182,7 +182,7 @@ static int manager_find_user_config_paths(char ***ret_files, char ***ret_dirs) { _cleanup_strv_free_ char **files = NULL, **dirs = NULL; int r; - r = xdg_user_config_dir(&base, "/systemd"); + r = xdg_user_config_dir("/systemd", &base); if (r < 0) return r; @@ -2479,7 +2479,7 @@ static int initialize_runtime( /* Create the runtime directory and place the inaccessible device nodes there, if we run in * user mode. In system mode mount_setup() already did that. */ - r = xdg_user_runtime_dir(&p, "/systemd"); + r = xdg_user_runtime_dir("/systemd", &p); if (r < 0) { *ret_error_message = "$XDG_RUNTIME_DIR is not set"; return log_struct_errno(LOG_EMERG, r, diff --git a/src/core/manager.c b/src/core/manager.c index 2dddc79722..18fb8fdaf8 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -1031,7 +1031,7 @@ int manager_new(RuntimeScope runtime_scope, ManagerTestRunFlags test_run_flags, r = mkdir_label("/run/systemd/units", 0755); else { _cleanup_free_ char *units_path = NULL; - r = xdg_user_runtime_dir(&units_path, "/systemd/units"); + r = xdg_user_runtime_dir("/systemd/units", &units_path); if (r < 0) return r; diff --git a/src/core/unit.c b/src/core/unit.c index ae3cefcdde..c303ce9282 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -5576,12 +5576,13 @@ static int unit_get_invocation_path(Unit *u, char **ret) { p = strjoin("/run/systemd/units/invocation:", u->id); else { _cleanup_free_ char *user_path = NULL; - r = xdg_user_runtime_dir(&user_path, "/systemd/units/invocation:"); + + r = xdg_user_runtime_dir("/systemd/units/invocation:", &user_path); if (r < 0) return r; + p = strjoin(user_path, u->id); } - if (!p) return -ENOMEM; diff --git a/src/libsystemd/sd-path/path-lookup.c b/src/libsystemd/sd-path/path-lookup.c index d4d888c362..5d0d66914c 100644 --- a/src/libsystemd/sd-path/path-lookup.c +++ b/src/libsystemd/sd-path/path-lookup.c @@ -17,80 +17,6 @@ #include "tmpfile-util.h" #include "user-util.h" -int xdg_user_runtime_dir(char **ret, const char *suffix) { - const char *e; - char *j; - - assert(ret); - assert(suffix); - - e = getenv("XDG_RUNTIME_DIR"); - if (!e) - return -ENXIO; - - j = path_join(e, suffix); - if (!j) - return -ENOMEM; - - *ret = j; - return 0; -} - -int xdg_user_config_dir(char **ret, const char *suffix) { - _cleanup_free_ char *j = NULL; - const char *e; - int r; - - assert(ret); - - e = getenv("XDG_CONFIG_HOME"); - if (e) { - j = path_join(e, suffix); - if (!j) - return -ENOMEM; - } else { - r = get_home_dir(&j); - if (r < 0) - return r; - - if (!path_extend(&j, "/.config", suffix)) - return -ENOMEM; - } - - *ret = TAKE_PTR(j); - return 0; -} - -int xdg_user_data_dir(char **ret, const char *suffix) { - _cleanup_free_ char *j = NULL; - const char *e; - int r; - - assert(ret); - assert(suffix); - - /* We don't treat /etc/xdg/systemd here as the spec - * suggests because we assume that is a link to - * /etc/systemd/ anyway. */ - - e = getenv("XDG_DATA_HOME"); - if (e) { - j = path_join(e, suffix); - if (!j) - return -ENOMEM; - } else { - r = get_home_dir(&j); - if (r < 0) - return r; - - if (!path_extend(&j, "/.local/share", suffix)) - return -ENOMEM; - } - - *ret = TAKE_PTR(j); - return 1; -} - int runtime_directory(char **ret, RuntimeScope scope, const char *suffix) { int r; @@ -109,7 +35,7 @@ int runtime_directory(char **ret, RuntimeScope scope, const char *suffix) { return strdup_to(ret, e); if (scope == RUNTIME_SCOPE_USER) { - r = xdg_user_runtime_dir(ret, suffix); + r = xdg_user_runtime_dir(suffix, ret); if (r < 0) return r; } else { @@ -193,7 +119,7 @@ static char** user_dirs( if (r < 0) return NULL; - r = xdg_user_data_dir(&data_home, "/systemd/user"); + r = xdg_user_data_dir("/systemd/user", &data_home); if (r < 0 && r != -ENXIO) return NULL; @@ -326,7 +252,7 @@ static int acquire_transient_dir( else if (scope == RUNTIME_SCOPE_SYSTEM) transient = strdup("/run/systemd/transient"); else - return xdg_user_runtime_dir(ret, "/systemd/transient"); + return xdg_user_runtime_dir("/systemd/transient", ret); if (!transient) return -ENOMEM; @@ -354,11 +280,11 @@ static int acquire_config_dirs(RuntimeScope scope, char **persistent, char **run break; case RUNTIME_SCOPE_USER: - r = xdg_user_config_dir(&a, "/systemd/user"); + r = xdg_user_config_dir("/systemd/user", &a); if (r < 0 && r != -ENXIO) return r; - r = xdg_user_runtime_dir(runtime, "/systemd/user"); + r = xdg_user_runtime_dir("/systemd/user", runtime); if (r < 0) { if (r != -ENXIO) return r; @@ -411,11 +337,11 @@ static int acquire_control_dirs(RuntimeScope scope, char **persistent, char **ru } case RUNTIME_SCOPE_USER: - r = xdg_user_config_dir(&a, "/systemd/user.control"); + r = xdg_user_config_dir("/systemd/user.control", &a); if (r < 0 && r != -ENXIO) return r; - r = xdg_user_runtime_dir(runtime, "/systemd/user.control"); + r = xdg_user_runtime_dir("/systemd/user.control", runtime); if (r < 0) { if (r != -ENXIO) return r; diff --git a/src/libsystemd/sd-path/path-lookup.h b/src/libsystemd/sd-path/path-lookup.h index 1176ad8871..c325fa6041 100644 --- a/src/libsystemd/sd-path/path-lookup.h +++ b/src/libsystemd/sd-path/path-lookup.h @@ -3,6 +3,8 @@ #include +#include "sd-path.h" + #include "runtime-scope.h" typedef enum LookupPathsFlags { @@ -53,17 +55,27 @@ typedef struct LookupPaths { int lookup_paths_init(LookupPaths *lp, RuntimeScope scope, LookupPathsFlags flags, const char *root_dir); int lookup_paths_init_or_warn(LookupPaths *lp, RuntimeScope scope, LookupPathsFlags flags, const char *root_dir); +void lookup_paths_log(LookupPaths *p); +void lookup_paths_done(LookupPaths *p); + int xdg_user_dirs(char ***ret_config_dirs, char ***ret_data_dirs); -int xdg_user_runtime_dir(char **ret, const char *suffix); -int xdg_user_config_dir(char **ret, const char *suffix); -int xdg_user_data_dir(char **ret, const char *suffix); + +/* We don't treat /etc/xdg/systemd/ in these functions as the xdg base dir spec suggests because we assume + * that is a link to /etc/systemd/ anyway. */ + +static inline int xdg_user_runtime_dir(const char *suffix, char **ret) { + return sd_path_lookup(SD_PATH_USER_RUNTIME, suffix, ret); +} +static inline int xdg_user_config_dir(const char *suffix, char **ret) { + return sd_path_lookup(SD_PATH_USER_CONFIGURATION, suffix, ret); +} +static inline int xdg_user_data_dir(const char *suffix, char **ret) { + return sd_path_lookup(SD_PATH_USER_SHARED, suffix, ret); +} int runtime_directory(char **ret, RuntimeScope scope, const char *suffix); bool path_is_user_data_dir(const char *path); bool path_is_user_config_dir(const char *path); -void lookup_paths_log(LookupPaths *p); -void lookup_paths_done(LookupPaths *p); - char **generator_binary_paths(RuntimeScope scope); char **env_generator_binary_paths(RuntimeScope scope); diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c index a7655aad1c..25403325e7 100644 --- a/src/tmpfiles/tmpfiles.c +++ b/src/tmpfiles/tmpfiles.c @@ -359,15 +359,15 @@ static int user_config_paths(char*** ret) { if (r < 0) return r; - r = xdg_user_config_dir(&persistent_config, "/user-tmpfiles.d"); + r = xdg_user_config_dir("/user-tmpfiles.d", &persistent_config); if (r < 0 && !ERRNO_IS_NEG_NOINFO(r)) return r; - r = xdg_user_runtime_dir(&runtime_config, "/user-tmpfiles.d"); + r = xdg_user_runtime_dir("/user-tmpfiles.d", &runtime_config); if (r < 0 && !ERRNO_IS_NEG_NOINFO(r)) return r; - r = xdg_user_data_dir(&data_home, "/user-tmpfiles.d"); + r = xdg_user_data_dir("/user-tmpfiles.d", &data_home); if (r < 0 && !ERRNO_IS_NEG_NOINFO(r)) return r; diff --git a/src/vmspawn/vmspawn-util.c b/src/vmspawn/vmspawn-util.c index 8a6122dd9d..22cfa289fa 100644 --- a/src/vmspawn/vmspawn-util.c +++ b/src/vmspawn/vmspawn-util.c @@ -212,7 +212,7 @@ static int get_firmware_search_dirs(char ***ret) { * Prioritising entries in "more specific" directories */ _cleanup_free_ char *user_firmware_dir = NULL; - r = xdg_user_config_dir(&user_firmware_dir, "/qemu/firmware"); + r = xdg_user_config_dir("/qemu/firmware", &user_firmware_dir); if (r < 0) return r; diff --git a/src/xdg-autostart-generator/xdg-autostart-generator.c b/src/xdg-autostart-generator/xdg-autostart-generator.c index 71e1a66435..455f371fa8 100644 --- a/src/xdg-autostart-generator/xdg-autostart-generator.c +++ b/src/xdg-autostart-generator/xdg-autostart-generator.c @@ -27,7 +27,7 @@ static int enumerate_xdg_autostart(Hashmap *all_services) { _cleanup_free_ char *user_config_autostart_dir = NULL; int r; - r = xdg_user_config_dir(&user_config_autostart_dir, "/autostart"); + r = xdg_user_config_dir("/autostart", &user_config_autostart_dir); if (r < 0) return r; r = strv_extend(&autostart_dirs, user_config_autostart_dir); From 8cff087d27f8b0a1eca5c63261f5918e10b0b70f Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Sat, 24 Aug 2024 15:33:53 +0200 Subject: [PATCH 07/17] path-lookup: modernize runtime_directory() too --- src/libsystemd/sd-path/path-lookup.c | 11 +++++------ src/libsystemd/sd-path/path-lookup.h | 3 ++- src/vmspawn/vmspawn.c | 6 +++--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/libsystemd/sd-path/path-lookup.c b/src/libsystemd/sd-path/path-lookup.c index 5d0d66914c..2b0607df07 100644 --- a/src/libsystemd/sd-path/path-lookup.c +++ b/src/libsystemd/sd-path/path-lookup.c @@ -17,16 +17,15 @@ #include "tmpfile-util.h" #include "user-util.h" -int runtime_directory(char **ret, RuntimeScope scope, const char *suffix) { +int runtime_directory(RuntimeScope scope, const char *suffix, char **ret) { int r; - assert(ret); + assert(IN_SET(scope, RUNTIME_SCOPE_SYSTEM, RUNTIME_SCOPE_USER)); assert(suffix); - assert(IN_SET(scope, RUNTIME_SCOPE_SYSTEM, RUNTIME_SCOPE_USER, RUNTIME_SCOPE_GLOBAL)); + assert(ret); /* Accept $RUNTIME_DIRECTORY as authoritative - * If its missing apply the suffix to /run or $XDG_RUNTIME_DIR - * if we are in a user runtime scope. + * If it's missing, apply the suffix to /run/, or $XDG_RUNTIME_DIR if we are in a user runtime scope. * * Return value indicates whether the suffix was applied or not */ @@ -45,7 +44,7 @@ int runtime_directory(char **ret, RuntimeScope scope, const char *suffix) { *ret = d; } - return true; + return 1; } static const char* const user_data_unit_paths[] = { diff --git a/src/libsystemd/sd-path/path-lookup.h b/src/libsystemd/sd-path/path-lookup.h index c325fa6041..6c7c9f42da 100644 --- a/src/libsystemd/sd-path/path-lookup.h +++ b/src/libsystemd/sd-path/path-lookup.h @@ -58,6 +58,8 @@ int lookup_paths_init_or_warn(LookupPaths *lp, RuntimeScope scope, LookupPathsFl void lookup_paths_log(LookupPaths *p); void lookup_paths_done(LookupPaths *p); +int runtime_directory(RuntimeScope scope, const char *suffix, char **ret); + int xdg_user_dirs(char ***ret_config_dirs, char ***ret_data_dirs); /* We don't treat /etc/xdg/systemd/ in these functions as the xdg base dir spec suggests because we assume @@ -72,7 +74,6 @@ static inline int xdg_user_config_dir(const char *suffix, char **ret) { static inline int xdg_user_data_dir(const char *suffix, char **ret) { return sd_path_lookup(SD_PATH_USER_SHARED, suffix, ret); } -int runtime_directory(char **ret, RuntimeScope scope, const char *suffix); bool path_is_user_data_dir(const char *path); bool path_is_user_config_dir(const char *path); diff --git a/src/vmspawn/vmspawn.c b/src/vmspawn/vmspawn.c index 7474b301f3..9d7f943200 100644 --- a/src/vmspawn/vmspawn.c +++ b/src/vmspawn/vmspawn.c @@ -1488,11 +1488,11 @@ static int run_virtual_machine(int kvm_device_fd, int vhost_device_fd) { /* if we are going to be starting any units with state then create our runtime dir */ if (arg_tpm != 0 || arg_directory || arg_runtime_mounts.n_mounts != 0) { - r = runtime_directory(&arg_runtime_directory, arg_privileged ? RUNTIME_SCOPE_SYSTEM : RUNTIME_SCOPE_USER, "systemd/vmspawn"); + r = runtime_directory(arg_privileged ? RUNTIME_SCOPE_SYSTEM : RUNTIME_SCOPE_USER, "systemd/vmspawn", + &arg_runtime_directory); if (r < 0) return log_error_errno(r, "Failed to lookup runtime directory: %m"); - if (r) { - /* r > 0 means we need to create our own runtime dir */ + if (r > 0) { /* We need to create our own runtime dir */ r = mkdir_p(arg_runtime_directory, 0755); if (r < 0) return log_error_errno(r, "Failed to create runtime directory: %m"); From 0e0e20bfb1819d5292982dea0f750c4d917d59c5 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Sun, 25 Aug 2024 19:42:59 +0200 Subject: [PATCH 08/17] path-lookup: clean up acquire_{generator,transient}_dirs() a bit --- src/libsystemd/sd-path/path-lookup.c | 61 ++++++++++++---------------- 1 file changed, 26 insertions(+), 35 deletions(-) diff --git a/src/libsystemd/sd-path/path-lookup.c b/src/libsystemd/sd-path/path-lookup.c index 2b0607df07..4a4e96ea64 100644 --- a/src/libsystemd/sd-path/path-lookup.c +++ b/src/libsystemd/sd-path/path-lookup.c @@ -180,17 +180,18 @@ bool path_is_user_config_dir(const char *path) { static int acquire_generator_dirs( RuntimeScope scope, const char *tempdir, - char **generator, - char **generator_early, - char **generator_late) { + char **ret, + char **ret_early, + char **ret_late) { - _cleanup_free_ char *x = NULL, *y = NULL, *z = NULL, *p = NULL; + _cleanup_free_ char *prefix_alloc = NULL, *g = NULL, *early = NULL, *late = NULL; const char *prefix; + int r; - assert(generator); - assert(generator_early); - assert(generator_late); assert(IN_SET(scope, RUNTIME_SCOPE_SYSTEM, RUNTIME_SCOPE_USER, RUNTIME_SCOPE_GLOBAL)); + assert(ret); + assert(ret_early); + assert(ret_late); if (scope == RUNTIME_SCOPE_GLOBAL) return -EOPNOTSUPP; @@ -199,49 +200,38 @@ static int acquire_generator_dirs( prefix = tempdir; else if (scope == RUNTIME_SCOPE_SYSTEM) prefix = "/run/systemd"; - else { - /* RUNTIME_SCOPE_USER */ - const char *e; + else { /* RUNTIME_SCOPE_USER */ + r = xdg_user_runtime_dir("/systemd", &prefix_alloc); + if (r < 0) + return r; - e = getenv("XDG_RUNTIME_DIR"); - if (!e) - return -ENXIO; - - p = path_join(e, "/systemd"); - if (!p) - return -ENOMEM; - - prefix = p; + prefix = prefix_alloc; } - x = path_join(prefix, "generator"); - if (!x) + g = path_join(prefix, "generator"); + if (!g) return -ENOMEM; - y = path_join(prefix, "generator.early"); - if (!y) + early = path_join(prefix, "generator.early"); + if (!early) return -ENOMEM; - z = path_join(prefix, "generator.late"); - if (!z) + late = path_join(prefix, "generator.late"); + if (!late) return -ENOMEM; - *generator = TAKE_PTR(x); - *generator_early = TAKE_PTR(y); - *generator_late = TAKE_PTR(z); + *ret = TAKE_PTR(g); + *ret_early = TAKE_PTR(early); + *ret_late = TAKE_PTR(late); return 0; } -static int acquire_transient_dir( - RuntimeScope scope, - const char *tempdir, - char **ret) { - +static int acquire_transient_dir(RuntimeScope scope, const char *tempdir, char **ret) { char *transient; - assert(ret); assert(IN_SET(scope, RUNTIME_SCOPE_SYSTEM, RUNTIME_SCOPE_USER, RUNTIME_SCOPE_GLOBAL)); + assert(ret); if (scope == RUNTIME_SCOPE_GLOBAL) return -EOPNOTSUPP; @@ -250,11 +240,12 @@ static int acquire_transient_dir( transient = path_join(tempdir, "transient"); else if (scope == RUNTIME_SCOPE_SYSTEM) transient = strdup("/run/systemd/transient"); - else + else /* RUNTIME_SCOPE_USER */ return xdg_user_runtime_dir("/systemd/transient", ret); if (!transient) return -ENOMEM; + *ret = transient; return 0; } From bd79a88f3413229558c46e7939fa78de4d6538cf Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Sun, 15 Sep 2024 18:53:22 +0200 Subject: [PATCH 09/17] path-lookup: use path_strv_contains() rather than strv_contains() --- src/libsystemd/sd-path/path-lookup.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libsystemd/sd-path/path-lookup.c b/src/libsystemd/sd-path/path-lookup.c index 4a4e96ea64..6f6a7cb4b0 100644 --- a/src/libsystemd/sd-path/path-lookup.c +++ b/src/libsystemd/sd-path/path-lookup.c @@ -168,13 +168,13 @@ static char** user_dirs( bool path_is_user_data_dir(const char *path) { assert(path); - return strv_contains((char**) user_data_unit_paths, path); + return path_strv_contains((char* const*) user_data_unit_paths, path); } bool path_is_user_config_dir(const char *path) { assert(path); - return strv_contains((char**) user_config_unit_paths, path); + return path_strv_contains((char* const*) user_config_unit_paths, path); } static int acquire_generator_dirs( From 716fab66ab07d8a594544b52d16f87d11e7c6013 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Sun, 25 Aug 2024 20:07:12 +0200 Subject: [PATCH 10/17] path-lookup: unify acquire_{config,control,attached}_dir() Note that -ENXIO reported by xdg_user_config_dir() is now properly propagated rather than ignored, as unlike XDG_RUNTIME_DIR, XDG_CONFIG_HOME has a default value hence ENXIO is not really expected. --- src/libsystemd/sd-path/path-lookup.c | 189 ++++++++++----------------- 1 file changed, 71 insertions(+), 118 deletions(-) diff --git a/src/libsystemd/sd-path/path-lookup.c b/src/libsystemd/sd-path/path-lookup.c index 6f6a7cb4b0..d37da14ec6 100644 --- a/src/libsystemd/sd-path/path-lookup.c +++ b/src/libsystemd/sd-path/path-lookup.c @@ -250,137 +250,91 @@ static int acquire_transient_dir(RuntimeScope scope, const char *tempdir, char * return 0; } -static int acquire_config_dirs(RuntimeScope scope, char **persistent, char **runtime) { +typedef enum LookupDirType { + LOOKUP_DIR_CONFIG, + LOOKUP_DIR_CONTROL, + LOOKUP_DIR_ATTACHED, + _LOOKUP_DIR_MAX, + _LOOKUP_DIR_INVALID = -EINVAL, +} LookupDirType; + +static int acquire_lookup_dirs( + LookupDirType type, + RuntimeScope scope, + char **ret_persistent, + char **ret_runtime) { + + /* RUNTIME_SCOPE_USER dirs are relative to XDG_CONFIG_DIR and XDG_RUNTIME_DIR, respectively */ + static const struct { + const char *persistent; + const char *runtime; + } dirs[_LOOKUP_DIR_MAX][_RUNTIME_SCOPE_MAX] = { + [LOOKUP_DIR_CONFIG] = { + [RUNTIME_SCOPE_SYSTEM] = { SYSTEM_CONFIG_UNIT_DIR, "/run/systemd/system" }, + [RUNTIME_SCOPE_GLOBAL] = { USER_CONFIG_UNIT_DIR, "/run/systemd/user" }, + [RUNTIME_SCOPE_USER] = { "systemd/user", "systemd/user" }, + }, + [LOOKUP_DIR_CONTROL] = { + [RUNTIME_SCOPE_SYSTEM] = { "/etc/systemd/system.control", "/run/systemd/system.control" }, + [RUNTIME_SCOPE_USER] = { "systemd/user.control", "systemd/user.control" }, + }, + [LOOKUP_DIR_ATTACHED] = { + [RUNTIME_SCOPE_SYSTEM] = { "/etc/systemd/system.attached", "/run/systemd/system.attached" }, + /* Portable services are not available to regular users for now. */ + }, + }; + _cleanup_free_ char *a = NULL, *b = NULL; int r; - assert(persistent); - assert(runtime); + assert(type >= 0 && type < _LOOKUP_DIR_MAX); + assert(IN_SET(scope, RUNTIME_SCOPE_SYSTEM, RUNTIME_SCOPE_USER, RUNTIME_SCOPE_GLOBAL)); + assert(ret_persistent); + assert(ret_runtime); + + const char *persistent = dirs[type][scope].persistent; + const char *runtime = dirs[type][scope].runtime; + assert(!persistent == !runtime); + + if (!persistent) + return -EOPNOTSUPP; switch (scope) { case RUNTIME_SCOPE_SYSTEM: - a = strdup(SYSTEM_CONFIG_UNIT_DIR); - b = strdup("/run/systemd/system"); - break; - case RUNTIME_SCOPE_GLOBAL: - a = strdup(USER_CONFIG_UNIT_DIR); - b = strdup("/run/systemd/user"); - break; + a = strdup(persistent); + b = strdup(runtime); + if (!a || !b) + return -ENOMEM; + + *ret_persistent = TAKE_PTR(a); + *ret_runtime = TAKE_PTR(b); + + return 0; case RUNTIME_SCOPE_USER: - r = xdg_user_config_dir("/systemd/user", &a); - if (r < 0 && r != -ENXIO) + r = xdg_user_config_dir(persistent, &a); + if (r < 0) return r; - r = xdg_user_runtime_dir("/systemd/user", runtime); + r = xdg_user_runtime_dir(runtime, ret_runtime); if (r < 0) { if (r != -ENXIO) return r; - /* If XDG_RUNTIME_DIR is not set, don't consider that fatal, simply initialize the runtime - * directory to NULL */ - *runtime = NULL; + /* If XDG_RUNTIME_DIR is not set, don't consider that fatal, simply initialize + * the runtime directory to NULL. */ + *ret_runtime = NULL; } - *persistent = TAKE_PTR(a); + *ret_persistent = TAKE_PTR(a); return 0; default: assert_not_reached(); } - - if (!a || !b) - return -ENOMEM; - - *persistent = TAKE_PTR(a); - *runtime = TAKE_PTR(b); - - return 0; -} - -static int acquire_control_dirs(RuntimeScope scope, char **persistent, char **runtime) { - _cleanup_free_ char *a = NULL; - int r; - - assert(persistent); - assert(runtime); - - switch (scope) { - - case RUNTIME_SCOPE_SYSTEM: { - _cleanup_free_ char *b = NULL; - - a = strdup("/etc/systemd/system.control"); - if (!a) - return -ENOMEM; - - b = strdup("/run/systemd/system.control"); - if (!b) - return -ENOMEM; - - *runtime = TAKE_PTR(b); - - break; - } - - case RUNTIME_SCOPE_USER: - r = xdg_user_config_dir("/systemd/user.control", &a); - if (r < 0 && r != -ENXIO) - return r; - - r = xdg_user_runtime_dir("/systemd/user.control", runtime); - if (r < 0) { - if (r != -ENXIO) - return r; - - /* If XDG_RUNTIME_DIR is not set, don't consider this fatal, simply initialize the directory to - * NULL */ - *runtime = NULL; - } - - break; - - case RUNTIME_SCOPE_GLOBAL: - return -EOPNOTSUPP; - - default: - assert_not_reached(); - } - - *persistent = TAKE_PTR(a); - - return 0; -} - -static int acquire_attached_dirs( - RuntimeScope scope, - char **ret_persistent, - char **ret_runtime) { - - _cleanup_free_ char *a = NULL, *b = NULL; - - assert(ret_persistent); - assert(ret_runtime); - - /* Portable services are not available to regular users for now. */ - if (scope != RUNTIME_SCOPE_SYSTEM) - return -EOPNOTSUPP; - - a = strdup("/etc/systemd/system.attached"); - if (!a) - return -ENOMEM; - - b = strdup("/run/systemd/system.attached"); - if (!b) - return -ENOMEM; - - *ret_persistent = TAKE_PTR(a); - *ret_runtime = TAKE_PTR(b); - - return 0; } static int patch_root_prefix(char **p, const char *root_dir) { @@ -489,16 +443,24 @@ int lookup_paths_init( } /* Note: when XDG_RUNTIME_DIR is not set this will not return -ENXIO, but simply set runtime_config to NULL */ - r = acquire_config_dirs(scope, &persistent_config, &runtime_config); + r = acquire_lookup_dirs(LOOKUP_DIR_CONFIG, scope, &persistent_config, &runtime_config); if (r < 0) return r; if (scope == RUNTIME_SCOPE_USER) { - r = acquire_config_dirs(RUNTIME_SCOPE_GLOBAL, &global_persistent_config, &global_runtime_config); + r = acquire_lookup_dirs(LOOKUP_DIR_CONFIG, RUNTIME_SCOPE_GLOBAL, &global_persistent_config, &global_runtime_config); if (r < 0) return r; } + r = acquire_lookup_dirs(LOOKUP_DIR_CONTROL, scope, &persistent_control, &runtime_control); + if (r < 0 && r != -EOPNOTSUPP) + return r; + + r = acquire_lookup_dirs(LOOKUP_DIR_ATTACHED, scope, &persistent_attached, &runtime_attached); + if (r < 0 && r != -EOPNOTSUPP) + return r; + if ((flags & LOOKUP_PATHS_EXCLUDE_GENERATED) == 0) { /* Note: if XDG_RUNTIME_DIR is not set, this will fail completely with ENXIO */ r = acquire_generator_dirs(scope, tempdir, @@ -512,15 +474,6 @@ int lookup_paths_init( if (r < 0 && !IN_SET(r, -EOPNOTSUPP, -ENXIO)) return r; - /* Note: when XDG_RUNTIME_DIR is not set this will not return -ENXIO, but simply set runtime_control to NULL */ - r = acquire_control_dirs(scope, &persistent_control, &runtime_control); - if (r < 0 && r != -EOPNOTSUPP) - return r; - - r = acquire_attached_dirs(scope, &persistent_attached, &runtime_attached); - if (r < 0 && r != -EOPNOTSUPP) - return r; - /* First priority is whatever has been passed to us via env vars */ r = get_paths_from_environ("SYSTEMD_UNIT_PATH", &paths, &append); if (r < 0) From b8018297a826c2fc3aecec3ab55186d606d5a0d6 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Sun, 25 Aug 2024 20:18:58 +0200 Subject: [PATCH 11/17] path-lookup: shortcut patch_root_prefix() if no root_dir --- src/libsystemd/sd-path/path-lookup.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/libsystemd/sd-path/path-lookup.c b/src/libsystemd/sd-path/path-lookup.c index d37da14ec6..673a255685 100644 --- a/src/libsystemd/sd-path/path-lookup.c +++ b/src/libsystemd/sd-path/path-lookup.c @@ -342,6 +342,9 @@ static int patch_root_prefix(char **p, const char *root_dir) { assert(p); + if (!root_dir) + return 0; + if (!*p) return 0; From 536f6bd4673f3d708c6d5581dc74e4b816add67c Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Sun, 25 Aug 2024 20:32:18 +0200 Subject: [PATCH 12/17] path-lookup: modernize get_paths_from_environ() Use retval rather than additional param to indicate whether the normal paths shall be appended. --- src/libsystemd/sd-path/path-lookup.c | 50 ++++++++++++---------------- 1 file changed, 21 insertions(+), 29 deletions(-) diff --git a/src/libsystemd/sd-path/path-lookup.c b/src/libsystemd/sd-path/path-lookup.c index 673a255685..64bb46096a 100644 --- a/src/libsystemd/sd-path/path-lookup.c +++ b/src/libsystemd/sd-path/path-lookup.c @@ -371,34 +371,29 @@ static int patch_root_prefix_strv(char **l, const char *root_dir) { return 0; } -static int get_paths_from_environ(const char *var, char ***paths, bool *append) { +static int get_paths_from_environ(const char *var, char ***ret) { const char *e; int r; assert(var); - assert(paths); - assert(append); - - *append = false; + assert(ret); e = getenv(var); - if (e) { - const char *k; - - k = endswith(e, ":"); - if (k) { - e = strndupa_safe(e, k - e); - *append = true; - } - - /* FIXME: empty components in other places should be rejected. */ - - r = path_split_and_make_absolute(e, paths); - if (r < 0) - return r; + if (!e) { + *ret = NULL; + return 0; } - return 0; + bool append = endswith(e, ":"); /* Whether to append the normal search paths after what's obtained + from envvar */ + + /* FIXME: empty components in other places should be rejected. */ + + r = path_split_and_make_absolute(e, ret); + if (r < 0) + return r; + + return append; } int lookup_paths_init( @@ -416,7 +411,6 @@ int lookup_paths_init( *transient = NULL, *persistent_control = NULL, *runtime_control = NULL, *persistent_attached = NULL, *runtime_attached = NULL; - bool append = false; /* Add items from SYSTEMD_UNIT_PATH before normal directories */ _cleanup_strv_free_ char **paths = NULL; int r; @@ -478,11 +472,11 @@ int lookup_paths_init( return r; /* First priority is whatever has been passed to us via env vars */ - r = get_paths_from_environ("SYSTEMD_UNIT_PATH", &paths, &append); + r = get_paths_from_environ("SYSTEMD_UNIT_PATH", &paths); if (r < 0) return r; - if (!paths || append) { + if (!paths || r > 0) { /* Let's figure something out. */ _cleanup_strv_free_ char **add = NULL; @@ -680,16 +674,15 @@ void lookup_paths_log(LookupPaths *lp) { } char **generator_binary_paths(RuntimeScope scope) { - bool append = false; /* Add items from SYSTEMD_GENERATOR_PATH before normal directories */ _cleanup_strv_free_ char **paths = NULL; int r; /* First priority is whatever has been passed to us via env vars */ - r = get_paths_from_environ("SYSTEMD_GENERATOR_PATH", &paths, &append); + r = get_paths_from_environ("SYSTEMD_GENERATOR_PATH", &paths); if (r < 0) return NULL; - if (!paths || append) { + if (!paths || r > 0) { _cleanup_strv_free_ char **add = NULL; switch (scope) { @@ -730,15 +723,14 @@ char **generator_binary_paths(RuntimeScope scope) { char **env_generator_binary_paths(RuntimeScope runtime_scope) { _cleanup_strv_free_ char **paths = NULL, **add = NULL; - bool append = false; /* Add items from SYSTEMD_ENVIRONMENT_GENERATOR_PATH before normal directories */ int r; /* First priority is whatever has been passed to us via env vars */ - r = get_paths_from_environ("SYSTEMD_ENVIRONMENT_GENERATOR_PATH", &paths, &append); + r = get_paths_from_environ("SYSTEMD_ENVIRONMENT_GENERATOR_PATH", &paths); if (r < 0) return NULL; - if (!paths || append) { + if (!paths || r > 0) { switch (runtime_scope) { case RUNTIME_SCOPE_SYSTEM: From cfda202fa9fd2a3ba63db5e7765cf0268600db71 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Sat, 7 Sep 2024 15:27:04 +0200 Subject: [PATCH 13/17] path-lookup: unify *_generator_binary_paths() --- src/libsystemd/sd-path/path-lookup.c | 138 ++++++++++++--------------- src/libsystemd/sd-path/path-lookup.h | 9 +- src/libsystemd/sd-path/sd-path.c | 23 ++--- 3 files changed, 73 insertions(+), 97 deletions(-) diff --git a/src/libsystemd/sd-path/path-lookup.c b/src/libsystemd/sd-path/path-lookup.c index 64bb46096a..2e59dca235 100644 --- a/src/libsystemd/sd-path/path-lookup.c +++ b/src/libsystemd/sd-path/path-lookup.c @@ -673,95 +673,75 @@ void lookup_paths_log(LookupPaths *lp) { } } -char **generator_binary_paths(RuntimeScope scope) { +static const char* const system_generator_paths[] = { + "/run/systemd/system-generators", + "/etc/systemd/system-generators", + "/usr/local/lib/systemd/system-generators", + SYSTEM_GENERATOR_DIR, + NULL, +}; + +static const char* const user_generator_paths[] = { + "/run/systemd/user-generators", + "/etc/systemd/user-generators", + "/usr/local/lib/systemd/user-generators", + USER_GENERATOR_DIR, + NULL, +}; + +static const char* const system_env_generator_paths[] = { + "/run/systemd/system-environment-generators", + "/etc/systemd/system-environment-generators", + "/usr/local/lib/systemd/system-environment-generators", + SYSTEM_ENV_GENERATOR_DIR, + NULL, +}; + +static const char* const user_env_generator_paths[] = { + "/run/systemd/user-environment-generators", + "/etc/systemd/user-environment-generators", + "/usr/local/lib/systemd/user-environment-generators", + USER_ENV_GENERATOR_DIR, + NULL, +}; + +char** generator_binary_paths_internal(RuntimeScope scope, bool env_generator) { + + static const struct { + const char *env_name; + const char * const *paths[_RUNTIME_SCOPE_MAX]; + } unit_generator = { + "SYSTEMD_GENERATOR_PATH", + { + [RUNTIME_SCOPE_SYSTEM] = system_generator_paths, + [RUNTIME_SCOPE_USER] = user_generator_paths, + } + }, environment_generator = { + "SYSTEMD_ENVIRONMENT_GENERATOR_PATH", + { + [RUNTIME_SCOPE_SYSTEM] = system_env_generator_paths, + [RUNTIME_SCOPE_USER] = user_env_generator_paths, + } + }; + _cleanup_strv_free_ char **paths = NULL; int r; + assert(IN_SET(scope, RUNTIME_SCOPE_SYSTEM, RUNTIME_SCOPE_USER)); + + const char *env_name = ASSERT_PTR((env_generator ? environment_generator : unit_generator).env_name); + const char * const *generator_paths = ASSERT_PTR((env_generator ? environment_generator : unit_generator).paths[scope]); + /* First priority is whatever has been passed to us via env vars */ - r = get_paths_from_environ("SYSTEMD_GENERATOR_PATH", &paths); + r = get_paths_from_environ(env_name, &paths); if (r < 0) return NULL; if (!paths || r > 0) { - _cleanup_strv_free_ char **add = NULL; - - switch (scope) { - - case RUNTIME_SCOPE_SYSTEM: - add = strv_new("/run/systemd/system-generators", - "/etc/systemd/system-generators", - "/usr/local/lib/systemd/system-generators", - SYSTEM_GENERATOR_DIR); - break; - - case RUNTIME_SCOPE_GLOBAL: - case RUNTIME_SCOPE_USER: - add = strv_new("/run/systemd/user-generators", - "/etc/systemd/user-generators", - "/usr/local/lib/systemd/user-generators", - USER_GENERATOR_DIR); - break; - - default: - assert_not_reached(); - } - if (!add) - return NULL; - - if (paths) { - r = strv_extend_strv(&paths, add, true); - if (r < 0) - return NULL; - } else - /* Small optimization: if paths is NULL (and it usually is), we can simply assign 'add' to it, - * and don't have to copy anything */ - paths = TAKE_PTR(add); - } - - return TAKE_PTR(paths); -} - -char **env_generator_binary_paths(RuntimeScope runtime_scope) { - _cleanup_strv_free_ char **paths = NULL, **add = NULL; - int r; - - /* First priority is whatever has been passed to us via env vars */ - r = get_paths_from_environ("SYSTEMD_ENVIRONMENT_GENERATOR_PATH", &paths); - if (r < 0) - return NULL; - - if (!paths || r > 0) { - switch (runtime_scope) { - - case RUNTIME_SCOPE_SYSTEM: - add = strv_new("/run/systemd/system-environment-generators", - "/etc/systemd/system-environment-generators", - "/usr/local/lib/systemd/system-environment-generators", - SYSTEM_ENV_GENERATOR_DIR); - break; - - case RUNTIME_SCOPE_USER: - add = strv_new("/run/systemd/user-environment-generators", - "/etc/systemd/user-environment-generators", - "/usr/local/lib/systemd/user-environment-generators", - USER_ENV_GENERATOR_DIR); - break; - - default: - assert_not_reached(); - } - if (!add) - return NULL; - } - - if (paths) { - r = strv_extend_strv(&paths, add, true); + r = strv_extend_strv(&paths, (char* const*) generator_paths, /* filter_duplicates = */ true); if (r < 0) return NULL; - } else - /* Small optimization: if paths is NULL (and it usually is), we can simply assign 'add' to it, - * and don't have to copy anything */ - paths = TAKE_PTR(add); + } return TAKE_PTR(paths); } diff --git a/src/libsystemd/sd-path/path-lookup.h b/src/libsystemd/sd-path/path-lookup.h index 6c7c9f42da..ca90184125 100644 --- a/src/libsystemd/sd-path/path-lookup.h +++ b/src/libsystemd/sd-path/path-lookup.h @@ -78,5 +78,10 @@ static inline int xdg_user_data_dir(const char *suffix, char **ret) { bool path_is_user_data_dir(const char *path); bool path_is_user_config_dir(const char *path); -char **generator_binary_paths(RuntimeScope scope); -char **env_generator_binary_paths(RuntimeScope scope); +char** generator_binary_paths_internal(RuntimeScope scope, bool env_generator); +static inline char** generator_binary_paths(RuntimeScope runtime_scope) { + return generator_binary_paths_internal(runtime_scope, false); +} +static inline char** env_generator_binary_paths(RuntimeScope runtime_scope) { + return generator_binary_paths_internal(runtime_scope, true); +} diff --git a/src/libsystemd/sd-path/sd-path.c b/src/libsystemd/sd-path/sd-path.c index 096f3a1156..9f495f3051 100644 --- a/src/libsystemd/sd-path/sd-path.c +++ b/src/libsystemd/sd-path/sd-path.c @@ -581,25 +581,16 @@ static int get_search(uint64_t type, char ***ret) { } case SD_PATH_SYSTEMD_SEARCH_SYSTEM_GENERATOR: - case SD_PATH_SYSTEMD_SEARCH_USER_GENERATOR: { - RuntimeScope scope = type == SD_PATH_SYSTEMD_SEARCH_SYSTEM_GENERATOR ? - RUNTIME_SCOPE_SYSTEM : RUNTIME_SCOPE_USER; - char **t; - - t = generator_binary_paths(scope); - if (!t) - return -ENOMEM; - - *ret = t; - return 0; - } - + case SD_PATH_SYSTEMD_SEARCH_USER_GENERATOR: case SD_PATH_SYSTEMD_SEARCH_SYSTEM_ENVIRONMENT_GENERATOR: case SD_PATH_SYSTEMD_SEARCH_USER_ENVIRONMENT_GENERATOR: { - char **t; + RuntimeScope scope = IN_SET(type, SD_PATH_SYSTEMD_SEARCH_SYSTEM_GENERATOR, + SD_PATH_SYSTEMD_SEARCH_SYSTEM_ENVIRONMENT_GENERATOR) ? + RUNTIME_SCOPE_SYSTEM : RUNTIME_SCOPE_USER; + bool env_generator = IN_SET(type, SD_PATH_SYSTEMD_SEARCH_SYSTEM_ENVIRONMENT_GENERATOR, + SD_PATH_SYSTEMD_SEARCH_USER_ENVIRONMENT_GENERATOR); - t = env_generator_binary_paths(type == SD_PATH_SYSTEMD_SEARCH_SYSTEM_ENVIRONMENT_GENERATOR ? - RUNTIME_SCOPE_SYSTEM : RUNTIME_SCOPE_USER); + char **t = generator_binary_paths_internal(scope, env_generator); if (!t) return -ENOMEM; From 546110ded9c1cc0124e604f209c3f0ef442daaf0 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Sun, 6 Oct 2024 19:41:33 +0200 Subject: [PATCH 14/17] path-lookup: assert that LOOKUP_PATHS_{EXCLUDE,TEMPORARY}_GENERATED are not used in conjunction --- src/libsystemd/sd-path/path-lookup.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libsystemd/sd-path/path-lookup.c b/src/libsystemd/sd-path/path-lookup.c index 2e59dca235..6325144407 100644 --- a/src/libsystemd/sd-path/path-lookup.c +++ b/src/libsystemd/sd-path/path-lookup.c @@ -417,6 +417,7 @@ int lookup_paths_init( assert(lp); assert(scope >= 0); assert(scope < _RUNTIME_SCOPE_MAX); + assert(!FLAGS_SET(flags, LOOKUP_PATHS_EXCLUDE_GENERATED|LOOKUP_PATHS_TEMPORARY_GENERATED)); if (!empty_or_root(root_dir)) { if (scope == RUNTIME_SCOPE_USER) From 1f8eedba9d9b2c81455c788d0edca2146d5f30a4 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Sat, 14 Sep 2024 19:02:32 +0200 Subject: [PATCH 15/17] path-lookup: introduce user_search_dirs() (shall replace xdg_user_dirs()) xdg_user_dirs() doesn't seem well-organized currently. In all other xdg_user_*() funcs we assume /etc/xdg/systemd to be a symlink to /etc/systemd/, hence it is the odd one out. Also, when the relevant envvar is unset, it only returns the global search dirs. sd_path_lookup() actually covers this nicely with SD_PATH_SEARCH_*, where the combined search paths (from user home and system) are used. Therefore, let's introduce a wrapper for that, and deprecate xdg_user_dirs() (would be removed in later commits). --- src/libsystemd/sd-path/path-lookup.c | 21 +++++++++++++++++ src/libsystemd/sd-path/path-lookup.h | 1 + src/tmpfiles/tmpfiles.c | 34 ++++++++-------------------- 3 files changed, 32 insertions(+), 24 deletions(-) diff --git a/src/libsystemd/sd-path/path-lookup.c b/src/libsystemd/sd-path/path-lookup.c index 6325144407..6200acd8f5 100644 --- a/src/libsystemd/sd-path/path-lookup.c +++ b/src/libsystemd/sd-path/path-lookup.c @@ -17,6 +17,27 @@ #include "tmpfile-util.h" #include "user-util.h" +int user_search_dirs(const char *suffix, char ***ret_config_dirs, char ***ret_data_dirs) { + _cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL; + int r; + + assert(ret_config_dirs); + assert(ret_data_dirs); + + r = sd_path_lookup_strv(SD_PATH_SEARCH_CONFIGURATION, suffix, &config_dirs); + if (r < 0) + return r; + + r = sd_path_lookup_strv(SD_PATH_SEARCH_SHARED, suffix, &data_dirs); + if (r < 0) + return r; + + *ret_config_dirs = TAKE_PTR(config_dirs); + *ret_data_dirs = TAKE_PTR(data_dirs); + + return 0; +} + int runtime_directory(RuntimeScope scope, const char *suffix, char **ret) { int r; diff --git a/src/libsystemd/sd-path/path-lookup.h b/src/libsystemd/sd-path/path-lookup.h index ca90184125..3ef831323a 100644 --- a/src/libsystemd/sd-path/path-lookup.h +++ b/src/libsystemd/sd-path/path-lookup.h @@ -65,6 +65,7 @@ int xdg_user_dirs(char ***ret_config_dirs, char ***ret_data_dirs); /* We don't treat /etc/xdg/systemd/ in these functions as the xdg base dir spec suggests because we assume * that is a link to /etc/systemd/ anyway. */ +int user_search_dirs(const char *suffix, char ***ret_config_dirs, char ***ret_data_dirs); static inline int xdg_user_runtime_dir(const char *suffix, char **ret) { return sd_path_lookup(SD_PATH_USER_RUNTIME, suffix, ret); } diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c index 25403325e7..421e465d71 100644 --- a/src/tmpfiles/tmpfiles.c +++ b/src/tmpfiles/tmpfiles.c @@ -349,49 +349,35 @@ static int log_unresolvable_specifier(const char *filename, unsigned line) { arg_dry_run ? (would) : (doing), \ __VA_ARGS__) -static int user_config_paths(char*** ret) { +static int user_config_paths(char ***ret) { _cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL; - _cleanup_free_ char *persistent_config = NULL, *runtime_config = NULL, *data_home = NULL; - _cleanup_strv_free_ char **res = NULL; + _cleanup_free_ char *runtime_config = NULL; int r; - r = xdg_user_dirs(&config_dirs, &data_dirs); - if (r < 0) - return r; + assert(ret); - r = xdg_user_config_dir("/user-tmpfiles.d", &persistent_config); - if (r < 0 && !ERRNO_IS_NEG_NOINFO(r)) + /* Combined user-specific and global dirs */ + r = user_search_dirs("/user-tmpfiles.d", &config_dirs, &data_dirs); + if (r < 0) return r; r = xdg_user_runtime_dir("/user-tmpfiles.d", &runtime_config); if (r < 0 && !ERRNO_IS_NEG_NOINFO(r)) return r; - r = xdg_user_data_dir("/user-tmpfiles.d", &data_home); - if (r < 0 && !ERRNO_IS_NEG_NOINFO(r)) - return r; - - r = strv_extend_strv_concat(&res, (const char* const*) config_dirs, "/user-tmpfiles.d"); + r = strv_consume(&config_dirs, TAKE_PTR(runtime_config)); if (r < 0) return r; - r = strv_extend_many( - &res, - persistent_config, - runtime_config, - data_home); + r = strv_extend_strv_consume(&config_dirs, TAKE_PTR(data_dirs), /* filter_duplicates = */ true); if (r < 0) return r; - r = strv_extend_strv_concat(&res, (const char* const*) data_dirs, "/user-tmpfiles.d"); + r = path_strv_make_absolute_cwd(config_dirs); if (r < 0) return r; - r = path_strv_make_absolute_cwd(res); - if (r < 0) - return r; - - *ret = TAKE_PTR(res); + *ret = TAKE_PTR(config_dirs); return 0; } From 5ce4902249e4f10d65022f097d8b7ac0348f3f4f Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Sat, 7 Sep 2024 19:12:58 +0200 Subject: [PATCH 16/17] path-lookup: refactor lookup_paths_init() search paths handling * Rename user_dirs() -> user_unit_search_dirs() and port to user_search_dirs() * Use STRV_IFNOTNULL to guard paths that could be NULL, assert otherwise --- src/libsystemd/sd-path/path-lookup.c | 248 +++++++++++++-------------- 1 file changed, 119 insertions(+), 129 deletions(-) diff --git a/src/libsystemd/sd-path/path-lookup.c b/src/libsystemd/sd-path/path-lookup.c index 6200acd8f5..d56144f3cc 100644 --- a/src/libsystemd/sd-path/path-lookup.c +++ b/src/libsystemd/sd-path/path-lookup.c @@ -118,74 +118,6 @@ int xdg_user_dirs(char ***ret_config_dirs, char ***ret_data_dirs) { return 0; } -static char** user_dirs( - const char *persistent_config, - const char *runtime_config, - const char *global_persistent_config, - const char *global_runtime_config, - const char *generator, - const char *generator_early, - const char *generator_late, - const char *transient, - const char *persistent_control, - const char *runtime_control) { - - _cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL; - _cleanup_free_ char *data_home = NULL; - _cleanup_strv_free_ char **res = NULL; - int r; - - r = xdg_user_dirs(&config_dirs, &data_dirs); - if (r < 0) - return NULL; - - r = xdg_user_data_dir("/systemd/user", &data_home); - if (r < 0 && r != -ENXIO) - return NULL; - - /* Now merge everything we found. */ - if (strv_extend_many( - &res, - persistent_control, - runtime_control, - transient, - generator_early, - persistent_config) < 0) - return NULL; - - if (strv_extend_strv_concat(&res, (const char* const*) config_dirs, "/systemd/user") < 0) - return NULL; - - /* global config has lower priority than the user config of the same type */ - if (strv_extend(&res, global_persistent_config) < 0) - return NULL; - - if (strv_extend_strv(&res, (char**) user_config_unit_paths, false) < 0) - return NULL; - - if (strv_extend_many( - &res, - runtime_config, - global_runtime_config, - generator, - data_home) < 0) - return NULL; - - if (strv_extend_strv_concat(&res, (const char* const*) data_dirs, "/systemd/user") < 0) - return NULL; - - if (strv_extend_strv(&res, (char**) user_data_unit_paths, false) < 0) - return NULL; - - if (strv_extend(&res, generator_late) < 0) - return NULL; - - if (path_strv_make_absolute_cwd(res) < 0) - return NULL; - - return TAKE_PTR(res); -} - bool path_is_user_data_dir(const char *path) { assert(path); @@ -417,6 +349,70 @@ static int get_paths_from_environ(const char *var, char ***ret) { return append; } +static char** user_unit_search_dirs( + const char *persistent_config, + const char *runtime_config, + const char *global_persistent_config, + const char *global_runtime_config, + const char *generator, + const char *generator_early, + const char *generator_late, + const char *transient, + const char *persistent_control, + const char *runtime_control) { + + _cleanup_strv_free_ char **paths = NULL, **config_dirs = NULL, **data_dirs = NULL; + + /* The returned strv might contain duplicates, and we expect caller to filter them. */ + + assert(persistent_config); + assert(global_persistent_config); + assert(global_runtime_config); + assert(persistent_control); + + if (user_search_dirs("/systemd/user", &config_dirs, &data_dirs) < 0) + return NULL; + + paths = strv_new(persistent_control, + STRV_IFNOTNULL(runtime_control), + STRV_IFNOTNULL(transient), + STRV_IFNOTNULL(generator_early), + persistent_config); + if (!paths) + return NULL; + + if (strv_extend_strv_consume(&paths, TAKE_PTR(config_dirs), /* filter_duplicates = */ false) < 0) + return NULL; + + /* global config has lower priority than the user config of the same type */ + if (strv_extend(&paths, global_persistent_config) < 0) + return NULL; + + if (strv_extend_strv(&paths, (char* const*) user_config_unit_paths, /* filter_duplicates = */ false) < 0) + return NULL; + + /* strv_extend_many() can deal with NULL-s in arguments */ + if (strv_extend_many(&paths, + runtime_config, + global_runtime_config, + generator) < 0) + return NULL; + + if (strv_extend_strv_consume(&paths, TAKE_PTR(data_dirs), /* filter_duplicates = */ false) < 0) + return NULL; + + if (strv_extend_strv(&paths, (char* const*) user_data_unit_paths, false) < 0) + return NULL; + + if (strv_extend(&paths, generator_late) < 0) + return NULL; + + if (path_strv_make_absolute_cwd(paths) < 0) + return NULL; + + return TAKE_PTR(paths); +} + int lookup_paths_init( LookupPaths *lp, RuntimeScope scope, @@ -455,7 +451,7 @@ int lookup_paths_init( return -ENOMEM; } - if (flags & LOOKUP_PATHS_TEMPORARY_GENERATED) { + if (FLAGS_SET(flags, LOOKUP_PATHS_TEMPORARY_GENERATED)) { r = mkdtemp_malloc("/tmp/systemd-temporary-XXXXXX", &tempdir); if (r < 0) return log_debug_errno(r, "Failed to create temporary directory: %m"); @@ -480,7 +476,7 @@ int lookup_paths_init( if (r < 0 && r != -EOPNOTSUPP) return r; - if ((flags & LOOKUP_PATHS_EXCLUDE_GENERATED) == 0) { + if (!FLAGS_SET(flags, LOOKUP_PATHS_EXCLUDE_GENERATED)) { /* Note: if XDG_RUNTIME_DIR is not set, this will fail completely with ENXIO */ r = acquire_generator_dirs(scope, tempdir, &generator, &generator_early, &generator_late); @@ -503,67 +499,65 @@ int lookup_paths_init( _cleanup_strv_free_ char **add = NULL; - /* For the user units we include share/ in the search - * path in order to comply with the XDG basedir spec. - * For the system stuff we avoid such nonsense. OTOH - * we include /lib in the search path for the system - * stuff but avoid it for user stuff. */ + /* For the user units we include share/ in the search paths in order to comply with + * the XDG basedir spec. For the system stuff we avoid such nonsense. OTOH we include (/usr/)lib/ + * in the search paths for the system stuff but avoid it for user stuff. */ + + assert(persistent_config); switch (scope) { case RUNTIME_SCOPE_SYSTEM: - add = strv_new( - /* If you modify this you also want to modify - * systemdsystemunitpath= in systemd.pc.in! */ - STRV_IFNOTNULL(persistent_control), - STRV_IFNOTNULL(runtime_control), - STRV_IFNOTNULL(transient), - STRV_IFNOTNULL(generator_early), - persistent_config, - SYSTEM_CONFIG_UNIT_DIR, - "/etc/systemd/system", - STRV_IFNOTNULL(persistent_attached), - runtime_config, - "/run/systemd/system", - STRV_IFNOTNULL(runtime_attached), - STRV_IFNOTNULL(generator), - "/usr/local/lib/systemd/system", - SYSTEM_DATA_UNIT_DIR, - "/usr/lib/systemd/system", - /* To be used ONLY for images which might be legacy split-usr */ - STRV_IFNOTNULL(flags & LOOKUP_PATHS_SPLIT_USR ? "/lib/systemd/system" : NULL), - STRV_IFNOTNULL(generator_late)); + /* If you modify this you also want to modify systemdsystemunitpath= in systemd.pc.in! */ + add = strv_new(ASSERT_PTR(persistent_control), + ASSERT_PTR(runtime_control), + ASSERT_PTR(transient), + STRV_IFNOTNULL(generator_early), + persistent_config, + SYSTEM_CONFIG_UNIT_DIR, + "/etc/systemd/system", + ASSERT_PTR(persistent_attached), + ASSERT_PTR(runtime_config), + "/run/systemd/system", + ASSERT_PTR(runtime_attached), + STRV_IFNOTNULL(generator), + "/usr/local/lib/systemd/system", + SYSTEM_DATA_UNIT_DIR, + "/usr/lib/systemd/system", + /* To be used ONLY for images which might be legacy split-usr */ + FLAGS_SET(flags, LOOKUP_PATHS_SPLIT_USR) ? "/lib/systemd/system" : STRV_IGNORE, + STRV_IFNOTNULL(generator_late)); break; case RUNTIME_SCOPE_GLOBAL: - add = strv_new( - /* If you modify this you also want to modify - * systemduserunitpath= in systemd.pc.in, and - * the arrays in user_dirs() above! */ - STRV_IFNOTNULL(persistent_control), - STRV_IFNOTNULL(runtime_control), - STRV_IFNOTNULL(transient), - STRV_IFNOTNULL(generator_early), - persistent_config, - USER_CONFIG_UNIT_DIR, - "/etc/systemd/user", - runtime_config, - "/run/systemd/user", - STRV_IFNOTNULL(generator), - "/usr/local/share/systemd/user", - "/usr/share/systemd/user", - "/usr/local/lib/systemd/user", - USER_DATA_UNIT_DIR, - "/usr/lib/systemd/user", - STRV_IFNOTNULL(generator_late)); + /* If you modify this you also want to modify systemduserunitpath= in systemd.pc.in, + * and RUNTIME_SCOPE_USER search paths below! */ + + assert(!persistent_control); + assert(!runtime_control); + assert(!transient); + assert(!generator_early); + assert(!generator); + assert(!generator_late); + + add = strv_new(persistent_config, + USER_CONFIG_UNIT_DIR, + "/etc/systemd/user", + ASSERT_PTR(runtime_config), + "/run/systemd/user", + "/usr/local/share/systemd/user", + "/usr/share/systemd/user", + "/usr/local/lib/systemd/user", + USER_DATA_UNIT_DIR, + "/usr/lib/systemd/user"); break; case RUNTIME_SCOPE_USER: - add = user_dirs(persistent_config, runtime_config, - global_persistent_config, global_runtime_config, - generator, generator_early, generator_late, - transient, - persistent_control, runtime_control); + add = user_unit_search_dirs(persistent_config, runtime_config, + global_persistent_config, global_runtime_config, + generator, generator_early, generator_late, + transient, + persistent_control, runtime_control); break; default: @@ -573,14 +567,10 @@ int lookup_paths_init( if (!add) return -ENOMEM; - if (paths) { - r = strv_extend_strv(&paths, add, true); - if (r < 0) - return r; - } else - /* Small optimization: if paths is NULL (and it usually is), we can simply assign 'add' to it, - * and don't have to copy anything */ - paths = TAKE_PTR(add); + /* strv_uniq() below would filter all duplicates against the final strv */ + r = strv_extend_strv_consume(&paths, TAKE_PTR(add), /* filter_duplicates = */ false); + if (r < 0) + return r; } r = patch_root_prefix(&persistent_config, root); From 0aae1a09a3868420c654c0f1e51927af81a7e3e5 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Sat, 14 Sep 2024 19:14:33 +0200 Subject: [PATCH 17/17] path-lookup: move xdg_user_dirs() to xdg-autostart-generator This is the only place where xdg_user_dir() is needed and makes sense. All other invocations have been replaced with user_search_dirs() - see previous commits for details. --- src/libsystemd/sd-path/path-lookup.c | 35 ------------------- src/libsystemd/sd-path/path-lookup.h | 2 -- .../xdg-autostart-generator.c | 35 ++++++++++++++++++- 3 files changed, 34 insertions(+), 38 deletions(-) diff --git a/src/libsystemd/sd-path/path-lookup.c b/src/libsystemd/sd-path/path-lookup.c index d56144f3cc..a3b09208cb 100644 --- a/src/libsystemd/sd-path/path-lookup.c +++ b/src/libsystemd/sd-path/path-lookup.c @@ -83,41 +83,6 @@ static const char* const user_config_unit_paths[] = { NULL }; -int xdg_user_dirs(char ***ret_config_dirs, char ***ret_data_dirs) { - /* Implement the mechanisms defined in - * - * https://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html - * - * We look in both the config and the data dirs because we - * want to encourage that distributors ship their unit files - * as data, and allow overriding as configuration. - */ - const char *e; - _cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL; - - e = getenv("XDG_CONFIG_DIRS"); - if (e) - config_dirs = strv_split(e, ":"); - else - config_dirs = strv_new("/etc/xdg"); - if (!config_dirs) - return -ENOMEM; - - e = getenv("XDG_DATA_DIRS"); - if (e) - data_dirs = strv_split(e, ":"); - else - data_dirs = strv_new("/usr/local/share", - "/usr/share"); - if (!data_dirs) - return -ENOMEM; - - *ret_config_dirs = TAKE_PTR(config_dirs); - *ret_data_dirs = TAKE_PTR(data_dirs); - - return 0; -} - bool path_is_user_data_dir(const char *path) { assert(path); diff --git a/src/libsystemd/sd-path/path-lookup.h b/src/libsystemd/sd-path/path-lookup.h index 3ef831323a..819c4cdb15 100644 --- a/src/libsystemd/sd-path/path-lookup.h +++ b/src/libsystemd/sd-path/path-lookup.h @@ -60,8 +60,6 @@ void lookup_paths_done(LookupPaths *p); int runtime_directory(RuntimeScope scope, const char *suffix, char **ret); -int xdg_user_dirs(char ***ret_config_dirs, char ***ret_data_dirs); - /* We don't treat /etc/xdg/systemd/ in these functions as the xdg base dir spec suggests because we assume * that is a link to /etc/systemd/ anyway. */ diff --git a/src/xdg-autostart-generator/xdg-autostart-generator.c b/src/xdg-autostart-generator/xdg-autostart-generator.c index 455f371fa8..47252c2e36 100644 --- a/src/xdg-autostart-generator/xdg-autostart-generator.c +++ b/src/xdg-autostart-generator/xdg-autostart-generator.c @@ -20,6 +20,39 @@ DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(xdgautostartservice_hash_ops, char, string_hash_func, string_compare_func, XdgAutostartService, xdg_autostart_service_free); +static int xdg_base_dirs(char ***ret_config_dirs, char ***ret_data_dirs) { + _cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL; + const char *e; + + /* Implement the mechanisms defined in + * https://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html */ + + assert(ret_config_dirs); + assert(ret_data_dirs); + + e = getenv("XDG_CONFIG_DIRS"); + if (e) + config_dirs = strv_split(e, ":"); + else + config_dirs = strv_new("/etc/xdg"); + if (!config_dirs) + return -ENOMEM; + + e = getenv("XDG_DATA_DIRS"); + if (e) + data_dirs = strv_split(e, ":"); + else + data_dirs = strv_new("/usr/local/share", + "/usr/share"); + if (!data_dirs) + return -ENOMEM; + + *ret_config_dirs = TAKE_PTR(config_dirs); + *ret_data_dirs = TAKE_PTR(data_dirs); + + return 0; +} + static int enumerate_xdg_autostart(Hashmap *all_services) { _cleanup_strv_free_ char **autostart_dirs = NULL; _cleanup_strv_free_ char **config_dirs = NULL; @@ -34,7 +67,7 @@ static int enumerate_xdg_autostart(Hashmap *all_services) { if (r < 0) return r; - r = xdg_user_dirs(&config_dirs, &data_dirs); + r = xdg_base_dirs(&config_dirs, &data_dirs); if (r < 0) return r; r = strv_extend_strv_concat(&autostart_dirs, (const char* const*) config_dirs, "/autostart");