X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fshared%2Finstall.c;h=a3b75243d5dce6e382b4f4edc196b64f42a80e29;hb=4a010f4e6e95964d7acaa33be768dbdb5e46d72a;hp=a77dfc77e4a096dd698c564d5d18fd63d3dbb091;hpb=2c21044f05e32ec483b6ab13e175278779e9ebe3;p=elogind.git diff --git a/src/shared/install.c b/src/shared/install.c index a77dfc77e..a3b75243d 100644 --- a/src/shared/install.c +++ b/src/shared/install.c @@ -29,6 +29,7 @@ #include "mkdir.h" #include "hashmap.h" #include "set.h" +#include "path-util.h" #include "path-lookup.h" #include "strv.h" #include "unit-name.h" @@ -42,6 +43,7 @@ typedef struct { char **aliases; char **wanted_by; + char **required_by; } InstallInfo; typedef struct { @@ -58,7 +60,8 @@ static int lookup_paths_init_from_scope(LookupPaths *paths, UnitFileScope scope) return lookup_paths_init(paths, scope == UNIT_FILE_SYSTEM ? MANAGER_SYSTEM : MANAGER_USER, - scope == UNIT_FILE_USER); + scope == UNIT_FILE_USER, + NULL, NULL, NULL); } static int get_config_path(UnitFileScope scope, bool runtime, const char *root_dir, char **ret) { @@ -283,7 +286,7 @@ static int remove_marked_symlinks_fd( found = set_get(remove_symlinks_to, dest) || - set_get(remove_symlinks_to, file_name_from_path(dest)); + set_get(remove_symlinks_to, path_get_file_name(dest)); if (found) { @@ -468,7 +471,7 @@ static int find_symlinks_fd( if (path_is_absolute(name)) found_dest = path_equal(dest, name); else - found_dest = streq(file_name_from_path(dest), name); + found_dest = streq(path_get_file_name(dest), name); free(dest); @@ -605,7 +608,7 @@ int unit_file_mask( STRV_FOREACH(i, files) { char *path; - if (!unit_name_is_valid_no_type(*i, true)) { + if (!unit_name_is_valid(*i, true)) { if (r == 0) r = -EINVAL; continue; @@ -681,7 +684,7 @@ int unit_file_unmask( STRV_FOREACH(i, files) { char *path; - if (!unit_name_is_valid_no_type(*i, true)) { + if (!unit_name_is_valid(*i, true)) { if (r == 0) r = -EINVAL; continue; @@ -754,10 +757,10 @@ int unit_file_link( char *path, *fn; struct stat st; - fn = file_name_from_path(*i); + fn = path_get_file_name(*i); if (!path_is_absolute(*i) || - !unit_name_is_valid_no_type(fn, true)) { + !unit_name_is_valid(fn, true)) { if (r == 0) r = -EINVAL; continue; @@ -882,6 +885,7 @@ static void install_info_free(InstallInfo *i) { free(i->path); strv_free(i->aliases); strv_free(i->wanted_by); + strv_free(i->required_by); free(i); } @@ -917,9 +921,9 @@ static int install_info_add( assert(name || path); if (!name) - name = file_name_from_path(path); + name = path_get_file_name(path); - if (!unit_name_is_valid_no_type(name, true)) + if (!unit_name_is_valid(name, true)) return -EINVAL; if (hashmap_get(c->have_installed, name) || @@ -1020,9 +1024,10 @@ static int unit_file_load( bool allow_symlink) { const ConfigTableItem items[] = { - { "Install", "Alias", config_parse_strv, 0, &info->aliases }, - { "Install", "WantedBy", config_parse_strv, 0, &info->wanted_by }, - { "Install", "Also", config_parse_also, 0, c }, + { "Install", "Alias", config_parse_strv, 0, &info->aliases }, + { "Install", "WantedBy", config_parse_strv, 0, &info->wanted_by }, + { "Install", "RequiredBy", config_parse_strv, 0, &info->required_by }, + { "Install", "Also", config_parse_also, 0, c }, { NULL, NULL, NULL, 0, NULL } }; @@ -1049,7 +1054,10 @@ static int unit_file_load( if (r < 0) return r; - return strv_length(info->aliases) + strv_length(info->wanted_by); + return + strv_length(info->aliases) + + strv_length(info->wanted_by) + + strv_length(info->required_by); } static int unit_file_search( @@ -1120,7 +1128,10 @@ static int unit_file_can_install( r = unit_file_search(&c, i, paths, root_dir, allow_symlink); if (r >= 0) - r = strv_length(i->aliases) + strv_length(i->wanted_by); + r = + strv_length(i->aliases) + + strv_length(i->wanted_by) + + strv_length(i->required_by); install_context_done(&c); @@ -1140,7 +1151,7 @@ static int create_symlink( assert(old_path); assert(new_path); - mkdir_parents(new_path, 0755); + mkdir_parents_label(new_path, 0755); if (symlink(old_path, new_path) >= 0) { add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, new_path, old_path); @@ -1161,7 +1172,7 @@ static int create_symlink( free(dest); - if (force) + if (!force) return -EEXIST; unlink(new_path); @@ -1222,7 +1233,7 @@ static int install_info_symlink_wants( STRV_FOREACH(s, i->wanted_by) { char *path; - if (!unit_name_is_valid_no_type(*s, true)) { + if (!unit_name_is_valid(*s, true)) { r = -EINVAL; continue; } @@ -1240,6 +1251,40 @@ static int install_info_symlink_wants( return r; } +static int install_info_symlink_requires( + InstallInfo *i, + const char *config_path, + bool force, + UnitFileChange **changes, + unsigned *n_changes) { + + char **s; + int r = 0, q; + + assert(i); + assert(config_path); + + STRV_FOREACH(s, i->required_by) { + char *path; + + if (!unit_name_is_valid(*s, true)) { + r = -EINVAL; + continue; + } + + if (asprintf(&path, "%s/%s.requires/%s", config_path, *s, i->name) < 0) + return -ENOMEM; + + q = create_symlink(i->path, path, force, changes, n_changes); + free(path); + + if (r == 0) + r = q; + } + + return r; +} + static int install_info_symlink_link( InstallInfo *i, LookupPaths *paths, @@ -1289,6 +1334,10 @@ static int install_info_apply( if (r == 0) r = q; + q = install_info_symlink_requires(i, config_path, force, changes, n_changes); + if (r == 0) + r = q; + q = install_info_symlink_link(i, paths, config_path, force, changes, n_changes); if (r == 0) r = q; @@ -1549,7 +1598,7 @@ UnitFileState unit_file_get_state( if (root_dir && scope != UNIT_FILE_SYSTEM) return -EINVAL; - if (!unit_name_is_valid_no_type(name, true)) + if (!unit_name_is_valid(name, true)) return -EINVAL; r = lookup_paths_init_from_scope(&paths, scope); @@ -1634,16 +1683,18 @@ int unit_file_query_preset(UnitFileScope scope, const char *name) { if (scope == UNIT_FILE_SYSTEM) r = conf_files_list(&files, ".preset", - "/etc/systemd/system.preset", - "/usr/local/lib/systemd/system.preset", - "/usr/lib/systemd/system.preset", - "/lib/systemd/system.preset", + "/etc/systemd/system-preset", + "/usr/local/lib/systemd/system-preset", + "/usr/lib/systemd/system-preset", +#ifdef HAVE_SPLIT_USR + "/lib/systemd/system-preset", +#endif NULL); else if (scope == UNIT_FILE_GLOBAL) r = conf_files_list(&files, ".preset", - "/etc/systemd/user.preset", - "/usr/local/lib/systemd/user.preset", - "/usr/lib/systemd/user.preset", + "/etc/systemd/user-preset", + "/usr/local/lib/systemd/user-preset", + "/usr/lib/systemd/user-preset", NULL); else return 1; @@ -1742,7 +1793,7 @@ int unit_file_preset( STRV_FOREACH(i, files) { - if (!unit_name_is_valid_no_type(*i, true)) { + if (!unit_name_is_valid(*i, true)) { r = -EINVAL; goto finish; } @@ -1847,7 +1898,7 @@ int unit_file_get_list( if (ignore_file(de->d_name)) continue; - if (!unit_name_is_valid_no_type(de->d_name, true)) + if (!unit_name_is_valid(de->d_name, true)) continue; if (hashmap_get(h, de->d_name)) @@ -1915,7 +1966,7 @@ int unit_file_get_list( continue; found: - r = hashmap_put(h, file_name_from_path(f->path), f); + r = hashmap_put(h, path_get_file_name(f->path), f); if (r < 0) { free(f->path); free(f);