From 457a39a866a765c9c0b1aef3279c1dfa5266bdcc Mon Sep 17 00:00:00 2001 From: Goffredo Baroncelli Date: Mon, 10 Nov 2025 22:58:25 +0100 Subject: [PATCH 1/3] libsystemd: add new type SD_PATH_SEARCH_SYSCTL for sd_path_lookup* Add the new type SD_PATH_SEARCH_SYSCTL to libsystemd. With this new type sd_path_lookup() and sd_path_lookup_strv() will return the paths used by systemd-sysctl(1) to search the .conf files: /etc/sysctl.d/ /run/sysctl.d/ /usr/local/lib/sysctl.d/ /usr/lib/sysctl.d/ Refer to sysctl.d(5) man page. Note: the old type SD_PATH_SYSCTL is still available, and returns the last path (/usr/lib/sysctl.d/). --- man/sd_path_lookup.xml | 1 + src/libsystemd/sd-path/sd-path.c | 6 +++++- src/systemd/sd-path.h | 2 ++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/man/sd_path_lookup.xml b/man/sd_path_lookup.xml index fc6bac640c..8fe04ae8af 100644 --- a/man/sd_path_lookup.xml +++ b/man/sd_path_lookup.xml @@ -76,6 +76,7 @@ SD_PATH_SEARCH_CONFIGURATION_FACTORY, SD_PATH_SEARCH_STATE_FACTORY, SD_PATH_SEARCH_CONFIGURATION, + SD_PATH_SEARCH_SYSCTL, SD_PATH_SYSTEMD_UTIL, SD_PATH_SYSTEMD_SYSTEM_UNIT, diff --git a/src/libsystemd/sd-path/sd-path.c b/src/libsystemd/sd-path/sd-path.c index 0096997135..e3877fff4a 100644 --- a/src/libsystemd/sd-path/sd-path.c +++ b/src/libsystemd/sd-path/sd-path.c @@ -683,7 +683,11 @@ static int get_search(uint64_t type, char ***ret) { *ret = TAKE_PTR(l); return 0; - }} + } + + case SD_PATH_SEARCH_SYSCTL: + return strv_from_nulstr(ret, CONF_PATHS_NULSTR("sysctl.d")); + } return -EOPNOTSUPP; } diff --git a/src/systemd/sd-path.h b/src/systemd/sd-path.h index eb42e31a68..3199996011 100644 --- a/src/systemd/sd-path.h +++ b/src/systemd/sd-path.h @@ -129,6 +129,8 @@ __extension__ enum { SD_PATH_USER_CREDENTIAL_STORE_ENCRYPTED, SD_PATH_USER_SEARCH_CREDENTIAL_STORE_ENCRYPTED, + SD_PATH_SEARCH_SYSCTL, + _SD_PATH_MAX, _SD_PATH_INVALID = UINT64_MAX }; From 52efca47237ca163150402c8d61f24dfabc9e27e Mon Sep 17 00:00:00 2001 From: Goffredo Baroncelli Date: Mon, 10 Nov 2025 22:56:04 +0100 Subject: [PATCH 2/3] systemd-path: add new type SD_PATH_SEARCH_SYSCTL Add new type SD_PATH_SEARCH_SYSCTL to the ones that systemd-path already know. Before the change: $ systemd-path | egrep sysctl sysctl: /usr/lib/sysctl.d After the change: $ ./systemd-path | egrep sysctl search-sysctl: /etc/sysctl.d:/run/sysctl.d:/usr/local/lib/sysctl.d:/usr/lib/sysctl.d sysctl: /usr/lib/sysctl.d --- src/path/path-tool.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/path/path-tool.c b/src/path/path-tool.c index 3dc31832b3..92dbd3496b 100644 --- a/src/path/path-tool.c +++ b/src/path/path-tool.c @@ -66,6 +66,7 @@ static const char* const path_table[_SD_PATH_MAX] = { [SD_PATH_SEARCH_CONFIGURATION_FACTORY] = "search-configuration-factory", [SD_PATH_SEARCH_STATE_FACTORY] = "search-state-factory", [SD_PATH_SEARCH_CONFIGURATION] = "search-configuration", + [SD_PATH_SEARCH_SYSCTL] = "search-sysctl", [SD_PATH_SYSTEMD_UTIL] = "systemd-util", From f3797845501cfc744abcab6a00d21daab0c2d2a5 Mon Sep 17 00:00:00 2001 From: Goffredo Baroncelli Date: Mon, 10 Nov 2025 22:56:04 +0100 Subject: [PATCH 3/3] systemd-sysctl: add SD_PATH_SEARCH_SYSCTL Update systemd-sysctl to use libsystemd with the new type SD_PATH_SEARCH_SYSCTL to get the directories list from which load the .conf files. --- src/sysctl/sysctl.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/sysctl/sysctl.c b/src/sysctl/sysctl.c index d761c7375a..e90d6c3799 100644 --- a/src/sysctl/sysctl.c +++ b/src/sysctl/sysctl.c @@ -4,6 +4,8 @@ #include #include +#include "sd-path.h" + #include "alloc-util.h" #include "build.h" #include "conf-files.h" @@ -279,10 +281,10 @@ static int parse_line(const char *fname, unsigned line, const char *buffer, bool return 0; } -static int parse_file(OrderedHashmap **sysctl_options, const char *path, bool ignore_enoent) { +static int parse_file(OrderedHashmap **sysctl_options, const char *path, bool ignore_enoent, const char **search_paths) { return conf_file_read( /* root = */ NULL, - (const char**) CONF_PATHS_STRV("sysctl.d"), + search_paths, path, parse_line, sysctl_options, @@ -305,7 +307,7 @@ static int read_credential_lines(OrderedHashmap **sysctl_options) { if (!j) return log_oom(); - return parse_file(sysctl_options, j, /* ignore_enoent= */ true); + return parse_file(sysctl_options, j, /* ignore_enoent= */ true, /* search_paths= */ NULL); } static int cat_config(char **files) { @@ -440,8 +442,13 @@ static int parse_argv(int argc, char *argv[]) { static int run(int argc, char *argv[]) { _cleanup_ordered_hashmap_free_ OrderedHashmap *sysctl_options = NULL; + _cleanup_strv_free_ char **search_paths = NULL; int r; + r = sd_path_lookup_strv(SD_PATH_SEARCH_SYSCTL, /* suffix= */ NULL, &search_paths); + if (r < 0) + return log_error_errno(r, "Failed to get sysctl.d/ search paths: %m"); + r = parse_argv(argc, argv); if (r <= 0) return r; @@ -458,12 +465,12 @@ static int run(int argc, char *argv[]) { /* Use (argument):n, where n==1 for the first positional arg */ RET_GATHER(r, parse_line("(argument)", ++pos, *arg, /* invalid_config = */ NULL, &sysctl_options)); else - RET_GATHER(r, parse_file(&sysctl_options, *arg, false)); + RET_GATHER(r, parse_file(&sysctl_options, *arg, /* ignore_enoent= */ false, (const char**) search_paths)); } } else { _cleanup_strv_free_ char **files = NULL; - r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char**) CONF_PATHS_STRV("sysctl.d")); + r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char**) search_paths); if (r < 0) return log_error_errno(r, "Failed to enumerate sysctl.d files: %m"); @@ -471,7 +478,7 @@ static int run(int argc, char *argv[]) { return cat_config(files); STRV_FOREACH(f, files) - RET_GATHER(r, parse_file(&sysctl_options, *f, true)); + RET_GATHER(r, parse_file(&sysctl_options, *f, /* ignore_enoent= */ true, /* search_paths= */ NULL)); RET_GATHER(r, read_credential_lines(&sysctl_options)); }