core: rework generator dir logic, move the dirs into LookupPaths structure

A long time ago – when generators where first introduced – the directories for
them were randomly created via mkdtemp(). This was changed later so that they
use fixed name directories now. Let's make use of this, and add the genrator
dirs to the LookupPaths structure and into the unit file search path maintained
in it. This has the benefit that the generator dirs are now normal part of the
search path for all tools, and thus are shown in "systemctl list-unit-files"
too.
This commit is contained in:
Lennart Poettering 2016-02-24 15:31:33 +01:00
parent 4447e799be
commit a3c4eb0710
13 changed files with 211 additions and 208 deletions

View File

@ -1924,7 +1924,7 @@ const sd_bus_vtable bus_manager_vtable[] = {
SD_BUS_PROPERTY("Environment", "as", NULL, offsetof(Manager, environment), 0),
SD_BUS_PROPERTY("ConfirmSpawn", "b", bus_property_get_bool, offsetof(Manager, confirm_spawn), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("ShowStatus", "b", bus_property_get_bool, offsetof(Manager, show_status), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("UnitPath", "as", NULL, offsetof(Manager, lookup_paths.unit_path), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("UnitPath", "as", NULL, offsetof(Manager, lookup_paths.search_path), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("DefaultStandardOutput", "s", bus_property_get_exec_output, offsetof(Manager, default_std_output), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("DefaultStandardError", "s", bus_property_get_exec_output, offsetof(Manager, default_std_output), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_WRITABLE_PROPERTY("RuntimeWatchdogUSec", "t", bus_property_get_usec, property_set_runtime_watchdog, offsetof(Manager, runtime_watchdog), 0),

View File

@ -55,7 +55,7 @@ int unit_load_dropin(Unit *u) {
SET_FOREACH(t, u->names, i) {
char **p;
STRV_FOREACH(p, u->manager->lookup_paths.unit_path) {
STRV_FOREACH(p, u->manager->lookup_paths.search_path) {
unit_file_process_dir(u->manager->unit_path_cache, *p, t, ".wants", UNIT_WANTS,
add_dependency_consumer, u, NULL);
unit_file_process_dir(u->manager->unit_path_cache, *p, t, ".requires", UNIT_REQUIRES,

View File

@ -25,7 +25,7 @@
/* Read service data supplementary drop-in directories */
static inline int unit_find_dropin_paths(Unit *u, char ***paths) {
return unit_file_find_dropin_paths(u->manager->lookup_paths.unit_path,
return unit_file_find_dropin_paths(u->manager->lookup_paths.search_path,
u->manager->unit_path_cache,
u->names,
paths);

View File

@ -3574,7 +3574,7 @@ static int load_from_path(Unit *u, const char *path) {
} else {
char **p;
STRV_FOREACH(p, u->manager->lookup_paths.unit_path) {
STRV_FOREACH(p, u->manager->lookup_paths.search_path) {
/* Instead of opening the path right away, we manually
* follow all symlinks and add their name to our unit

View File

@ -1053,7 +1053,7 @@ static void manager_build_unit_path_cache(Manager *m) {
/* This simply builds a list of files we know exist, so that
* we don't always have to go to disk */
STRV_FOREACH(i, m->lookup_paths.unit_path) {
STRV_FOREACH(i, m->lookup_paths.search_path) {
struct dirent *de;
d = opendir(*i);
@ -1116,18 +1116,13 @@ int manager_startup(Manager *m, FILE *serialization, FDSet *fds) {
assert(m);
dual_timestamp_get(&m->generators_start_timestamp);
r = manager_run_generators(m);
dual_timestamp_get(&m->generators_finish_timestamp);
r = lookup_paths_init(&m->lookup_paths, m->running_as, true, NULL);
if (r < 0)
return r;
r = lookup_paths_init(
&m->lookup_paths, m->running_as, true,
NULL,
m->generator_unit_path,
m->generator_unit_path_early,
m->generator_unit_path_late);
dual_timestamp_get(&m->generators_start_timestamp);
r = manager_run_generators(m);
dual_timestamp_get(&m->generators_finish_timestamp);
if (r < 0)
return r;
@ -2542,17 +2537,12 @@ int manager_reload(Manager *m) {
manager_undo_generators(m);
lookup_paths_free(&m->lookup_paths);
/* Find new unit paths */
q = manager_run_generators(m);
q = lookup_paths_init(&m->lookup_paths, m->running_as, true, NULL);
if (q < 0 && r >= 0)
r = q;
q = lookup_paths_init(
&m->lookup_paths, m->running_as, true,
NULL,
m->generator_unit_path,
m->generator_unit_path_early,
m->generator_unit_path_late);
/* Find new unit paths */
q = manager_run_generators(m);
if (q < 0 && r >= 0)
r = q;
@ -2732,77 +2722,6 @@ void manager_check_finished(Manager *m) {
manager_invalidate_startup_units(m);
}
static int create_generator_dir(Manager *m, char **generator, const char *name) {
char *p;
int r;
assert(m);
assert(generator);
assert(name);
if (*generator)
return 0;
if (m->running_as == MANAGER_SYSTEM && getpid() == 1) {
/* systemd --system, not running --test */
p = strappend("/run/systemd/", name);
if (!p)
return log_oom();
r = mkdir_p_label(p, 0755);
if (r < 0) {
log_error_errno(r, "Failed to create generator directory %s: %m", p);
free(p);
return r;
}
} else if (m->running_as == MANAGER_USER) {
const char *s = NULL;
s = getenv("XDG_RUNTIME_DIR");
if (!s)
return -EINVAL;
p = strjoin(s, "/systemd/", name, NULL);
if (!p)
return log_oom();
r = mkdir_p_label(p, 0755);
if (r < 0) {
log_error_errno(r, "Failed to create generator directory %s: %m", p);
free(p);
return r;
}
} else {
/* systemd --system --test */
p = strjoin("/tmp/systemd-", name, ".XXXXXX", NULL);
if (!p)
return log_oom();
if (!mkdtemp(p)) {
log_error_errno(errno, "Failed to create generator directory %s: %m", p);
free(p);
return -errno;
}
}
*generator = p;
return 0;
}
static void trim_generator_dir(Manager *m, char **generator) {
assert(m);
assert(generator);
if (!*generator)
return;
if (rmdir(*generator) >= 0)
*generator = mfree(*generator);
return;
}
static int manager_run_generators(Manager *m) {
_cleanup_strv_free_ char **paths = NULL;
const char *argv[5];
@ -2821,62 +2740,53 @@ static int manager_run_generators(Manager *m) {
/* Optimize by skipping the whole process by not creating output directories
* if no generators are found. */
STRV_FOREACH(path, paths) {
r = access(*path, F_OK);
if (r == 0)
if (access(*path, F_OK) >= 0)
goto found;
if (errno != ENOENT)
log_warning_errno(errno, "Failed to open generator directory %s: %m", *path);
}
return 0;
found:
r = create_generator_dir(m, &m->generator_unit_path, "generator");
r = mkdir_p_label(m->lookup_paths.generator, 0755);
if (r < 0)
goto finish;
r = create_generator_dir(m, &m->generator_unit_path_early, "generator.early");
r = mkdir_p_label(m->lookup_paths.generator_early, 0755);
if (r < 0)
goto finish;
r = create_generator_dir(m, &m->generator_unit_path_late, "generator.late");
r = mkdir_p_label(m->lookup_paths.generator_late, 0755);
if (r < 0)
goto finish;
argv[0] = NULL; /* Leave this empty, execute_directory() will fill something in */
argv[1] = m->generator_unit_path;
argv[2] = m->generator_unit_path_early;
argv[3] = m->generator_unit_path_late;
argv[1] = m->lookup_paths.generator;
argv[2] = m->lookup_paths.generator_early;
argv[3] = m->lookup_paths.generator_late;
argv[4] = NULL;
RUN_WITH_UMASK(0022)
execute_directories((const char* const*) paths, DEFAULT_TIMEOUT_USEC, (char**) argv);
finish:
trim_generator_dir(m, &m->generator_unit_path);
trim_generator_dir(m, &m->generator_unit_path_early);
trim_generator_dir(m, &m->generator_unit_path_late);
/* Trim empty dirs */
(void) rmdir(m->lookup_paths.generator);
(void) rmdir(m->lookup_paths.generator_early);
(void) rmdir(m->lookup_paths.generator_late);
return r;
}
static void remove_generator_dir(Manager *m, char **generator) {
assert(m);
assert(generator);
if (!*generator)
return;
strv_remove(m->lookup_paths.unit_path, *generator);
(void) rm_rf(*generator, REMOVE_ROOT);
*generator = mfree(*generator);
}
static void manager_undo_generators(Manager *m) {
assert(m);
remove_generator_dir(m, &m->generator_unit_path);
remove_generator_dir(m, &m->generator_unit_path_early);
remove_generator_dir(m, &m->generator_unit_path_late);
if (m->lookup_paths.generator)
(void) rm_rf(m->lookup_paths.generator, REMOVE_ROOT);
if (m->lookup_paths.generator_early)
(void) rm_rf(m->lookup_paths.generator_early, REMOVE_ROOT);
if (m->lookup_paths.generator_late)
(void) rm_rf(m->lookup_paths.generator_late, REMOVE_ROOT);
}
int manager_environment_add(Manager *m, char **minus, char **plus) {

View File

@ -162,10 +162,6 @@ struct Manager {
dual_timestamp units_load_start_timestamp;
dual_timestamp units_load_finish_timestamp;
char *generator_unit_path;
char *generator_unit_path_early;
char *generator_unit_path_late;
struct udev* udev;
/* Data specific to the device subsystem */

View File

@ -1060,7 +1060,7 @@ static int unit_file_search(
assert(info->name);
STRV_FOREACH(p, paths->unit_path) {
STRV_FOREACH(p, paths->search_path) {
_cleanup_free_ char *path = NULL;
path = strjoin(*p, "/", info->name, NULL);
@ -1090,7 +1090,7 @@ static int unit_file_search(
if (r < 0)
return r;
STRV_FOREACH(p, paths->unit_path) {
STRV_FOREACH(p, paths->search_path) {
_cleanup_free_ char *path = NULL;
path = strjoin(*p, "/", template, NULL);
@ -1348,7 +1348,7 @@ static int install_info_symlink_link(
assert(config_path);
assert(i->path);
r = in_search_path(i->path, paths->unit_path);
r = in_search_path(i->path, paths->search_path);
if (r != 0)
return r;
@ -1672,7 +1672,7 @@ int unit_file_link(
if (!S_ISREG(st.st_mode))
return -ENOTTY;
q = in_search_path(*i, paths.unit_path);
q = in_search_path(*i, paths.search_path);
if (q < 0)
return q;
if (q > 0)
@ -2313,7 +2313,7 @@ int unit_file_preset_all(
if (r < 0)
return r;
STRV_FOREACH(i, paths.unit_path) {
STRV_FOREACH(i, paths.search_path) {
_cleanup_closedir_ DIR *d = NULL;
_cleanup_free_ char *units_dir;
struct dirent *de;
@ -2389,7 +2389,7 @@ int unit_file_get_list(
if (r < 0)
return r;
STRV_FOREACH(i, paths.unit_path) {
STRV_FOREACH(i, paths.search_path) {
_cleanup_closedir_ DIR *d = NULL;
_cleanup_free_ char *units_dir;
struct dirent *de;

View File

@ -135,7 +135,7 @@ int unit_file_set_default(UnitFileScope scope, const char *root_dir, const char
int unit_file_get_default(UnitFileScope scope, const char *root_dir, char **name);
int unit_file_add_dependency(UnitFileScope scope, bool runtime, const char *root_dir, char **files, const char *target, UnitDependency dep, bool force, UnitFileChange **changes, unsigned *n_changes);
int unit_file_lookup_state(UnitFileScope scope, const char *root_dir,const LookupPaths *paths, const char *name, UnitFileState *ret);
int unit_file_lookup_state(UnitFileScope scope, const char *root_dir, const LookupPaths *paths, const char *name, UnitFileState *ret);
int unit_file_get_state(UnitFileScope scope, const char *root_dir, const char *filename, UnitFileState *ret);
int unit_file_get_list(UnitFileScope scope, const char *root_dir, Hashmap *h);

View File

@ -235,43 +235,119 @@ char **generator_paths(ManagerRunningAs running_as) {
NULL);
}
static int acquire_generator_dirs(
ManagerRunningAs running_as,
char **generator,
char **generator_early,
char **generator_late) {
_cleanup_free_ char *x = NULL, *y = NULL, *z = NULL;
const char *prefix;
assert(generator);
assert(generator_early);
assert(generator_late);
if (running_as == MANAGER_SYSTEM)
prefix = "/run/systemd/";
else {
const char *e;
assert(running_as == MANAGER_USER);
e = getenv("XDG_RUNTIME_DIR");
if (!e)
return -EINVAL;
prefix = strjoina(e, "/systemd/", NULL);
}
x = strappend(prefix, "generator");
if (!x)
return -ENOMEM;
y = strappend(prefix, "generator.early");
if (!y)
return -ENOMEM;
z = strappend(prefix, "generator.late");
if (!z)
return -ENOMEM;
*generator = x;
*generator_early = y;
*generator_late = z;
x = y = z = NULL;
return 0;
}
static int patch_root_prefix(char **p, const char *root_dir) {
char *c;
assert(p);
if (!*p)
return 0;
if (isempty(root_dir) || path_equal(root_dir, "/"))
return 0;
c = prefix_root(root_dir, *p);
if (!c)
return -ENOMEM;
free(*p);
*p = c;
return 0;
}
int lookup_paths_init(
LookupPaths *p,
ManagerRunningAs running_as,
bool personal,
const char *root_dir,
const char *generator,
const char *generator_early,
const char *generator_late) {
const char *root_dir) {
const char *e;
_cleanup_free_ char *generator = NULL, *generator_early = NULL, *generator_late = NULL;
bool append = false; /* Add items from SYSTEMD_UNIT_PATH before normal directories */
char **l = NULL;
const char *e;
int r;
assert(p);
assert(running_as >= 0);
assert(running_as < _MANAGER_RUNNING_AS_MAX);
r = acquire_generator_dirs(running_as, &generator, &generator_early, &generator_late);
if (r < 0)
return r;
/* First priority is whatever has been passed to us via env
* vars */
e = getenv("SYSTEMD_UNIT_PATH");
if (e) {
if (endswith(e, ":")) {
e = strndupa(e, strlen(e) - 1);
const char *k;
k = endswith(e, ":");
if (k) {
e = strndupa(e, k - e);
append = true;
}
/* FIXME: empty components in other places should be
* rejected. */
r = path_split_and_make_absolute(e, &p->unit_path);
r = path_split_and_make_absolute(e, &l);
if (r < 0)
return r;
} else
p->unit_path = NULL;
l = NULL;
if (!p->unit_path || append) {
if (!l || append) {
/* Let's figure something out. */
_cleanup_strv_free_ char **unit_path;
_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.
@ -281,85 +357,114 @@ int lookup_paths_init(
if (running_as == MANAGER_USER) {
if (personal)
unit_path = user_dirs(generator, generator_early, generator_late);
add = user_dirs(generator, generator_early, generator_late);
else
unit_path = strv_new(
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(generator_early),
generator_early,
USER_CONFIG_UNIT_PATH,
"/etc/systemd/user",
"/run/systemd/user",
STRV_IFNOTNULL(generator),
generator,
"/usr/local/lib/systemd/user",
"/usr/local/share/systemd/user",
USER_DATA_UNIT_PATH,
"/usr/lib/systemd/user",
"/usr/share/systemd/user",
STRV_IFNOTNULL(generator_late),
generator_late,
NULL);
} else
unit_path = strv_new(
add = strv_new(
/* If you modify this you also want to modify
* systemdsystemunitpath= in systemd.pc.in! */
STRV_IFNOTNULL(generator_early),
generator_early,
SYSTEM_CONFIG_UNIT_PATH,
"/etc/systemd/system",
"/run/systemd/system",
STRV_IFNOTNULL(generator),
generator,
"/usr/local/lib/systemd/system",
SYSTEM_DATA_UNIT_PATH,
"/usr/lib/systemd/system",
#ifdef HAVE_SPLIT_USR
"/lib/systemd/system",
#endif
STRV_IFNOTNULL(generator_late),
generator_late,
NULL);
if (!unit_path)
if (!add)
return -ENOMEM;
r = strv_extend_strv(&p->unit_path, unit_path, false);
if (r < 0)
return r;
if (l) {
r = strv_extend_strv(&l, add, false);
if (r < 0)
return r;
} else {
l = add;
add = NULL;
}
}
if (!path_strv_resolve_uniq(p->unit_path, root_dir))
r = patch_root_prefix(&generator, root_dir);
if (r < 0)
return r;
r = patch_root_prefix(&generator_early, root_dir);
if (r < 0)
return r;
r = patch_root_prefix(&generator_late, root_dir);
if (r < 0)
return r;
if (!path_strv_resolve_uniq(l, root_dir))
return -ENOMEM;
if (!strv_isempty(p->unit_path)) {
_cleanup_free_ char *t = strv_join(p->unit_path, "\n\t");
if (strv_isempty(l)) {
log_debug("Ignoring unit files.");
l = strv_free(l);
} else {
_cleanup_free_ char *t;
t = strv_join(l, "\n\t");
if (!t)
return -ENOMEM;
log_debug("Looking for unit files in (higher priority first):\n\t%s", t);
} else {
log_debug("Ignoring unit files.");
p->unit_path = strv_free(p->unit_path);
}
p->search_path = l;
l = NULL;
p->generator = generator;
p->generator_early = generator_early;
p->generator_late = generator_late;
generator = generator_early = generator_late = NULL;
return 0;
}
void lookup_paths_free(LookupPaths *p) {
assert(p);
if (!p)
return;
p->unit_path = strv_free(p->unit_path);
p->search_path = strv_free(p->search_path);
p->generator = mfree(p->generator);
p->generator_early = mfree(p->generator_early);
p->generator_late = mfree(p->generator_late);
}
int lookup_paths_init_from_scope(LookupPaths *paths,
UnitFileScope scope,
const char *root_dir) {
assert(paths);
int lookup_paths_init_from_scope(
LookupPaths *p,
UnitFileScope scope,
const char *root_dir) {
assert(p);
assert(scope >= 0);
assert(scope < _UNIT_FILE_SCOPE_MAX);
zero(*paths);
return lookup_paths_init(paths,
scope == UNIT_FILE_SYSTEM ? MANAGER_SYSTEM : MANAGER_USER,
scope == UNIT_FILE_USER,
root_dir,
NULL, NULL, NULL);
return lookup_paths_init(
p,
scope == UNIT_FILE_SYSTEM ? MANAGER_SYSTEM : MANAGER_USER,
scope == UNIT_FILE_USER,
root_dir);
}

View File

@ -20,37 +20,34 @@
***/
#include <stdbool.h>
typedef struct LookupPaths LookupPaths;
typedef enum ManagerRunningAs ManagerRunningAs;
#include "install.h"
#include "macro.h"
typedef struct LookupPaths {
char **unit_path;
} LookupPaths;
struct LookupPaths {
char **search_path;
char *generator;
char *generator_early;
char *generator_late;
};
typedef enum ManagerRunningAs {
enum ManagerRunningAs {
MANAGER_SYSTEM,
MANAGER_USER,
_MANAGER_RUNNING_AS_MAX,
_MANAGER_RUNNING_AS_INVALID = -1
} ManagerRunningAs;
};
int user_config_home(char **config_home);
int user_runtime_dir(char **runtime_dir);
char **generator_paths(ManagerRunningAs running_as);
int lookup_paths_init(LookupPaths *p,
ManagerRunningAs running_as,
bool personal,
const char *root_dir,
const char *generator,
const char *generator_early,
const char *generator_late);
#include "install.h"
int lookup_paths_init_from_scope(LookupPaths *paths,
UnitFileScope scope,
const char *root_dir);
int lookup_paths_init(LookupPaths *p, ManagerRunningAs running_as, bool personal, const char *root_dir);
int lookup_paths_init_from_scope(LookupPaths *p, UnitFileScope scope, const char *root_dir);
void lookup_paths_free(LookupPaths *p);
#define _cleanup_lookup_paths_free_ _cleanup_(lookup_paths_free)

View File

@ -2295,7 +2295,7 @@ static int unit_file_find_path(LookupPaths *lp, const char *unit_name, char **un
assert(unit_name);
assert(unit_path);
STRV_FOREACH(p, lp->unit_path) {
STRV_FOREACH(p, lp->search_path) {
_cleanup_free_ char *path;
path = path_join(arg_root, *p, unit_name);
@ -2395,7 +2395,7 @@ static int unit_find_paths(
}
if (dropin_paths) {
r = unit_file_find_dropin_paths(lp->unit_path, NULL, names, &dropins);
r = unit_file_find_dropin_paths(lp->search_path, NULL, names, &dropins);
if (r < 0)
return r;
}
@ -5271,7 +5271,7 @@ static int enable_sysv_units(const char *verb, char **args) {
/* Processes all SysV units, and reshuffles the array so that
* afterwards only the native units remain */
r = lookup_paths_init(&paths, MANAGER_SYSTEM, false, arg_root, NULL, NULL, NULL);
r = lookup_paths_init(&paths, MANAGER_SYSTEM, false, arg_root);
if (r < 0)
return r;
@ -5295,7 +5295,7 @@ static int enable_sysv_units(const char *verb, char **args) {
if (path_is_absolute(name))
continue;
STRV_FOREACH(k, paths.unit_path) {
STRV_FOREACH(k, paths.search_path) {
_cleanup_free_ char *path = NULL;
path = path_join(arg_root, *k, name);

View File

@ -1004,7 +1004,7 @@ int main(int argc, char *argv[]) {
umask(0022);
r = lookup_paths_init(&lp, MANAGER_SYSTEM, true, NULL, NULL, NULL, NULL);
r = lookup_paths_init(&lp, MANAGER_SYSTEM, true, NULL);
if (r < 0) {
log_error_errno(r, "Failed to find lookup paths: %m");
goto finish;

View File

@ -31,25 +31,20 @@ static void test_paths(ManagerRunningAs running_as, bool personal) {
_cleanup_lookup_paths_free_ LookupPaths lp_without_env = {};
_cleanup_lookup_paths_free_ LookupPaths lp_with_env = {};
char *exists, *not, *systemd_unit_path;
char *systemd_unit_path;
assert_se(mkdtemp(template));
exists = strjoina(template, "/exists");
assert_se(mkdir(exists, 0755) == 0);
not = strjoina(template, "/not");
assert_se(unsetenv("SYSTEMD_UNIT_PATH") == 0);
assert_se(lookup_paths_init(&lp_without_env, running_as, personal, NULL, exists, not, not) == 0);
assert_se(lookup_paths_init(&lp_without_env, running_as, personal, NULL) == 0);
assert_se(!strv_isempty(lp_without_env.unit_path));
assert_se(strv_contains(lp_without_env.unit_path, exists));
assert_se(strv_contains(lp_without_env.unit_path, not));
assert_se(!strv_isempty(lp_without_env.search_path));
systemd_unit_path = strjoina(template, "/systemd-unit-path");
assert_se(setenv("SYSTEMD_UNIT_PATH", systemd_unit_path, 1) == 0);
assert_se(lookup_paths_init(&lp_with_env, running_as, personal, NULL, exists, not, not) == 0);
assert_se(strv_length(lp_with_env.unit_path) == 1);
assert_se(streq(lp_with_env.unit_path[0], systemd_unit_path));
assert_se(lookup_paths_init(&lp_with_env, running_as, personal, NULL) == 0);
assert_se(strv_length(lp_with_env.search_path) == 1);
assert_se(streq(lp_with_env.search_path[0], systemd_unit_path));
assert_se(rm_rf(template, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
}