Hashmap *have_installed;
} InstallContext;
-#define _cleanup_install_context_done_ _cleanup_(install_context_done)
+static int in_search_path(const char *path, char **search) {
+ _cleanup_free_ char *parent = NULL;
+ int r;
+
+ assert(path);
+
+ r = path_get_parent(path, &parent);
+ if (r < 0)
+ return r;
+
+ return strv_contains(search, parent);
+}
static int lookup_paths_init_from_scope(LookupPaths *paths,
UnitFileScope scope,
case UNIT_FILE_SYSTEM:
- if (root_dir && runtime)
- asprintf(&p, "%s/run/systemd/system", root_dir);
- else if (runtime)
- p = strdup("/run/systemd/system");
- else if (root_dir)
- asprintf(&p, "%s/%s", root_dir, SYSTEM_CONFIG_UNIT_PATH);
+ if (runtime)
+ p = path_join(root_dir, "/run/systemd/system", NULL);
else
- p = strdup(SYSTEM_CONFIG_UNIT_PATH);
-
+ p = path_join(root_dir, SYSTEM_CONFIG_UNIT_PATH, NULL);
break;
case UNIT_FILE_GLOBAL:
if (!c[i].path)
return -ENOMEM;
+ path_kill_slashes(c[i].path);
+
if (source) {
c[i].source = strdup(source);
if (!c[i].source) {
free(c[i].path);
return -ENOMEM;
}
+
+ path_kill_slashes(c[i].path);
} else
c[i].source = NULL;
assert(p);
- r = set_ensure_allocated(remove_symlinks_to, string_hash_func, string_compare_func);
+ r = set_ensure_allocated(remove_symlinks_to, &string_hash_ops);
if (r < 0)
return r;
if (unit_name_is_instance(de->d_name) &&
instance_whitelist &&
- !strv_contains(instance_whitelist, de->d_name))
- continue;
+ !strv_contains(instance_whitelist, de->d_name)) {
+
+ _cleanup_free_ char *w;
+
+ /* OK, the file is not listed directly
+ * in the whitelist, so let's check if
+ * the template of it might be
+ * listed. */
+
+ w = unit_name_template(de->d_name);
+ if (!w)
+ return -ENOMEM;
+
+ if (!strv_contains(instance_whitelist, w))
+ continue;
+ }
p = path_make_absolute(de->d_name, path);
if (!p)
if (!found)
continue;
-
if (unlink(p) < 0 && errno != ENOENT) {
-
if (r == 0)
r = -errno;
continue;
}
- rmdir_parents(p, config_path);
-
path_kill_slashes(p);
-
+ rmdir_parents(p, config_path);
add_file_change(changes, n_changes, UNIT_FILE_UNLINK, p, NULL);
if (!set_get(remove_symlinks_to, p)) {
hashmap_get(c->will_install, name))
return 0;
- r = hashmap_ensure_allocated(&c->will_install, string_hash_func, string_compare_func);
+ r = hashmap_ensure_allocated(&c->will_install, &string_hash_ops);
if (r < 0)
return r;
void *data,
void *userdata) {
- char *w;
size_t l;
- char *state;
+ const char *word, *state;
InstallContext *c = data;
assert(filename);
assert(lvalue);
assert(rvalue);
- FOREACH_WORD_QUOTED(w, l, rvalue, state) {
+ FOREACH_WORD_QUOTED(word, l, rvalue, state) {
_cleanup_free_ char *n;
int r;
- n = strndup(w, l);
+ n = strndup(word, l);
if (!n)
return -ENOMEM;
if (r < 0)
return r;
}
+ if (!isempty(state))
+ log_syntax(unit, LOG_ERR, filename, line, EINVAL,
+ "Trailing garbage, ignoring.");
return 0;
}
if (r < 0)
return r;
- if (!unit_instance_is_valid(printed))
+ if (!unit_instance_is_valid(printed)) {
+ free(printed);
return -EINVAL;
+ }
free(i->default_instance);
i->default_instance = printed;
InstallContext *c,
InstallInfo *info,
const char *path,
+ const char *root_dir,
bool allow_symlink) {
const ConfigTableItem items[] = {
{}
};
- int fd;
_cleanup_fclose_ FILE *f = NULL;
- int r;
+ int fd, r;
assert(c);
assert(info);
assert(path);
+ if (!isempty(root_dir))
+ path = strappenda(root_dir, "/", path);
+
fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|(allow_symlink ? 0 : O_NOFOLLOW));
if (fd < 0)
return -errno;
return -ENOMEM;
}
- r = config_parse(NULL, path, f, NULL, config_item_table_lookup, (void*) items, true, true, info);
+ r = config_parse(NULL, path, f,
+ NULL,
+ config_item_table_lookup, items,
+ true, true, false, info);
if (r < 0)
return r;
assert(info);
assert(paths);
- if (info->path) {
- const char *path;
-
- if (isempty(root_dir))
- path = info->path;
- else
- path = strappenda(root_dir, info->path);
-
- return unit_file_load(c, info, path, allow_symlink);
- }
+ if (info->path)
+ return unit_file_load(c, info, info->path, root_dir, allow_symlink);
assert(info->name);
STRV_FOREACH(p, paths->unit_path) {
_cleanup_free_ char *path = NULL;
- if (isempty(root_dir))
- path = strjoin(*p, "/", info->name, NULL);
- else
- path = strjoin(root_dir, "/", *p, "/", info->name, NULL);
+ path = strjoin(*p, "/", info->name, NULL);
if (!path)
return -ENOMEM;
- r = unit_file_load(c, info, path, allow_symlink);
+ r = unit_file_load(c, info, path, root_dir, allow_symlink);
if (r >= 0) {
info->path = path;
path = NULL;
* enablement was requested. We will check if it is
* possible to load template unit file. */
- _cleanup_free_ char *template = NULL, *template_dir = NULL;
+ _cleanup_free_ char *template = NULL;
template = unit_name_template(info->name);
if (!template)
STRV_FOREACH(p, paths->unit_path) {
_cleanup_free_ char *path = NULL;
- if (isempty(root_dir))
- path = strjoin(*p, "/", template, NULL);
- else
- path = strjoin(root_dir, "/", *p, "/", template, NULL);
+ path = strjoin(*p, "/", template, NULL);
if (!path)
return -ENOMEM;
- r = unit_file_load(c, info, path, allow_symlink);
+ r = unit_file_load(c, info, path, root_dir, allow_symlink);
if (r >= 0) {
info->path = path;
path = NULL;
const char *name,
bool allow_symlink) {
- _cleanup_install_context_done_ InstallContext c = {};
+ _cleanup_(install_context_done) InstallContext c = {};
InstallInfo *i;
int r;
assert(i);
assert(config_path);
- if (unit_name_is_template(i->name) && i->default_instance) {
+ if (unit_name_is_template(i->name)) {
+
+ /* Don't install any symlink if there's no default
+ * instance configured */
+
+ if (!i->default_instance)
+ return 0;
+
buf = unit_name_replace_instance(i->name, i->default_instance);
if (!buf)
return -ENOMEM;
InstallInfo *i,
LookupPaths *paths,
const char *config_path,
+ const char *root_dir,
bool force,
UnitFileChange **changes,
unsigned *n_changes) {
InstallInfo *i,
LookupPaths *paths,
const char *config_path,
+ const char *root_dir,
bool force,
UnitFileChange **changes,
unsigned *n_changes) {
if (r == 0)
r = q;
- q = install_info_symlink_link(i, paths, config_path, force, changes, n_changes);
+ q = install_info_symlink_link(i, paths, config_path, root_dir, force, changes, n_changes);
if (r == 0)
r = q;
while ((i = hashmap_first(c->will_install))) {
- q = hashmap_ensure_allocated(&c->have_installed, string_hash_func, string_compare_func);
+ q = hashmap_ensure_allocated(&c->have_installed, &string_hash_ops);
if (q < 0)
return q;
} else if (r >= 0)
r += q;
- q = install_info_apply(i, paths, config_path, force, changes, n_changes);
+ q = install_info_apply(i, paths, config_path, root_dir, force, changes, n_changes);
if (r >= 0 && q < 0)
r = q;
}
while ((i = hashmap_first(c->will_install))) {
- q = hashmap_ensure_allocated(&c->have_installed, string_hash_func, string_compare_func);
+ q = hashmap_ensure_allocated(&c->have_installed, &string_hash_ops);
if (q < 0)
return q;
unsigned *n_changes) {
_cleanup_lookup_paths_free_ LookupPaths paths = {};
- _cleanup_install_context_done_ InstallContext c = {};
+ _cleanup_(install_context_done) InstallContext c = {};
char **i;
_cleanup_free_ char *config_path = NULL;
int r;
unsigned *n_changes) {
_cleanup_lookup_paths_free_ LookupPaths paths = {};
- _cleanup_install_context_done_ InstallContext c = {};
+ _cleanup_(install_context_done) InstallContext c = {};
char **i;
_cleanup_free_ char *config_path = NULL;
_cleanup_set_free_free_ Set *remove_symlinks_to = NULL;
unsigned *n_changes) {
_cleanup_lookup_paths_free_ LookupPaths paths = {};
- _cleanup_install_context_done_ InstallContext c = {};
+ _cleanup_(install_context_done) InstallContext c = {};
_cleanup_free_ char *config_path = NULL;
char *path;
int r;
_cleanup_free_ char *path = NULL, *tmp = NULL;
char *n;
- if (isempty(root_dir))
- path = strappend(*p, "/" SPECIAL_DEFAULT_TARGET);
- else
- path = strjoin(root_dir, "/", *p, "/" SPECIAL_DEFAULT_TARGET, NULL);
-
+ path = path_join(root_dir, *p, SPECIAL_DEFAULT_TARGET);
if (!path)
return -ENOMEM;
free(path);
path = NULL;
- if (root_dir)
- asprintf(&path, "%s/%s/%s", root_dir, *i, name);
- else
- asprintf(&path, "%s/%s", *i, name);
+ path = path_join(root_dir, *i, name);
if (!path)
return -ENOMEM;
if (root_dir)
- partial = path + strlen(root_dir) + 1;
+ partial = path + strlen(root_dir);
else
partial = path;
int unit_file_query_preset(UnitFileScope scope, const char *root_dir, const char *name) {
_cleanup_strv_free_ char **files = NULL;
- char **i;
+ char **p;
int r;
assert(scope >= 0);
if (r < 0)
return r;
- STRV_FOREACH(i, files) {
- _cleanup_free_ char *buf = NULL;
+ STRV_FOREACH(p, files) {
_cleanup_fclose_ FILE *f;
- const char *p;
-
- if (root_dir)
- p = buf = strjoin(root_dir, "/", *i, NULL);
- else
- p = *i;
- f = fopen(p, "re");
+ f = fopen(*p, "re");
if (!f) {
if (errno == ENOENT)
continue;
UnitFileChange **changes,
unsigned *n_changes) {
- _cleanup_install_context_done_ InstallContext plus = {}, minus = {};
+ _cleanup_(install_context_done) InstallContext plus = {}, minus = {};
_cleanup_lookup_paths_free_ LookupPaths paths = {};
_cleanup_free_ char *config_path = NULL;
char **i;
assert(scope >= 0);
assert(scope < _UNIT_FILE_SCOPE_MAX);
- assert(mode < _UNIT_FILE_PRESET_MODE_MAX);
+ assert(mode < _UNIT_FILE_PRESET_MAX);
r = lookup_paths_init_from_scope(&paths, scope, root_dir);
if (r < 0)
UnitFileChange **changes,
unsigned *n_changes) {
- _cleanup_install_context_done_ InstallContext plus = {}, minus = {};
+ _cleanup_(install_context_done) InstallContext plus = {}, minus = {};
_cleanup_lookup_paths_free_ LookupPaths paths = {};
_cleanup_free_ char *config_path = NULL;
char **i;
assert(scope >= 0);
assert(scope < _UNIT_FILE_SCOPE_MAX);
- assert(mode < _UNIT_FILE_PRESET_MODE_MAX);
+ assert(mode < _UNIT_FILE_PRESET_MAX);
r = lookup_paths_init_from_scope(&paths, scope, root_dir);
if (r < 0)
STRV_FOREACH(i, paths.unit_path) {
_cleanup_closedir_ DIR *d = NULL;
- _cleanup_free_ char *buf = NULL;
- const char *units_dir;
+ _cleanup_free_ char *units_dir;
- if (!isempty(root_dir)) {
- buf = strjoin(root_dir, "/", *i, NULL);
- if (!buf)
- return -ENOMEM;
-
- units_dir = buf;
- } else
- units_dir = *i;
+ units_dir = path_join(root_dir, *i, NULL);
+ if (!units_dir)
+ return -ENOMEM;
d = opendir(units_dir);
if (!d) {
return r;
}
-static void unitfilelist_free(UnitFileList **f) {
- if (!*f)
+static void unit_file_list_free_one(UnitFileList *f) {
+ if (!f)
return;
- free((*f)->path);
- free(*f);
+ free(f->path);
+ free(f);
}
-#define _cleanup_unitfilelist_free_ _cleanup_(unitfilelist_free)
+
+DEFINE_TRIVIAL_CLEANUP_FUNC(UnitFileList*, unit_file_list_free_one);
int unit_file_get_list(
UnitFileScope scope,
if (root_dir && scope != UNIT_FILE_SYSTEM)
return -EINVAL;
+ if (root_dir) {
+ r = access(root_dir, F_OK);
+ if (r < 0)
+ return -errno;
+ }
+
r = lookup_paths_init_from_scope(&paths, scope, root_dir);
if (r < 0)
return r;
STRV_FOREACH(i, paths.unit_path) {
_cleanup_closedir_ DIR *d = NULL;
- _cleanup_free_ char *buf = NULL;
- const char *units_dir;
+ _cleanup_free_ char *units_dir;
- if (!isempty(root_dir)) {
- buf = strjoin(root_dir, "/", *i, NULL);
- if (!buf)
- return -ENOMEM;
-
- units_dir = buf;
- } else
- units_dir = *i;
+ units_dir = path_join(root_dir, *i, NULL);
+ if (!units_dir)
+ return -ENOMEM;
d = opendir(units_dir);
if (!d) {
}
for (;;) {
- _cleanup_unitfilelist_free_ UnitFileList *f = NULL;
+ _cleanup_(unit_file_list_free_onep) UnitFileList *f = NULL;
struct dirent *de;
+ _cleanup_free_ char *path = NULL;
errno = 0;
de = readdir(d);
goto found;
}
- r = unit_file_can_install(&paths, root_dir, f->path, true);
+ path = path_make_absolute(de->d_name, *i);
+ if (!path)
+ return -ENOMEM;
+
+ r = unit_file_can_install(&paths, root_dir, path, true);
if (r == -EINVAL || /* Invalid setting? */
r == -EBADMSG || /* Invalid format? */
r == -ENOENT /* Included file not found? */)
DEFINE_STRING_TABLE_LOOKUP(unit_file_change_type, UnitFileChangeType);
-static const char* const unit_file_preset_mode_table[_UNIT_FILE_PRESET_MODE_MAX] = {
+static const char* const unit_file_preset_mode_table[_UNIT_FILE_PRESET_MAX] = {
[UNIT_FILE_PRESET_FULL] = "full",
[UNIT_FILE_PRESET_ENABLE_ONLY] = "enable-only",
[UNIT_FILE_PRESET_DISABLE_ONLY] = "disable-only",