mirror of
https://github.com/morgan9e/systemd
synced 2026-04-15 00:47:10 +09:00
path-lookup: unify *_generator_binary_paths()
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user