HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/shared/install.c

140,485 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 58fd75b5f81c1d9d848889b7425f73d76196fe7b

Lines 2001–3000 of 3,921

                if (!alias_path)
                        return -ENOMEM;
 
                r = in_search_path(lp, info->path);
                if (r < 0)
                        return r;
                if (r == 0) {
                        /* The unit path itself is outside of the search path. To
                         * correctly apply the alias, we need the alias symlink to
                         * point to the symlink that was created in the search path. */
                        alias_target = path_join(config_path, info->name);
                        if (!alias_target)
                                return -ENOMEM;
                }
 
                bool broken;
                r = chase(alias_path, lp->root_dir, CHASE_NONEXISTENT, /* ret_path= */ NULL, /* ret_fd= */ NULL);
                if (r < 0 && r != -ENOENT) {
                        RET_GATHER(ret, r);
                        continue;
                }
                broken = r == 0; /* symlink target does not exist? */
 
                r = create_symlink(lp, alias_target ?: info->path, alias_path, force || broken, changes, n_changes);
                if (r == -EEXIST && FLAGS_SET(file_flags, UNIT_FILE_IGNORE_AUXILIARY_FAILURE))
                        /* We cannot realize the alias because a conflicting alias exists.
                         * Do not propagate this as error. */
                        continue;
                if (r != 0 && ret >= 0)
                        ret = r;
        }
 
        return ret;
}
 
static int install_info_symlink_wants(
                RuntimeScope scope,
                UnitFileFlags file_flags,
                InstallInfo *info,
                const LookupPaths *lp,
                const char *config_path,
                char **list,
                const char *suffix,
                InstallChange **changes,
                size_t *n_changes) {
 
        _cleanup_(install_info_clear) InstallInfo instance = {
                .install_mode = _INSTALL_MODE_INVALID,
        };
 
        UnitNameFlags valid_dst_type = UNIT_NAME_ANY;
        const char *n;
        int r, q;
 
        assert(info);
        assert(lp);
        assert(config_path);
 
        if (strv_isempty(list))
                return 0;
 
        if (unit_name_is_valid(info->name, UNIT_NAME_PLAIN | UNIT_NAME_INSTANCE))
                /* Not a template unit. Use the name directly. */
                n = info->name;
 
        else if (info->default_instance) {
                /* If this is a template, and we have a default instance, use it. */
 
                r = unit_name_replace_instance(info->name, info->default_instance, &instance.name);
                if (r < 0)
                        return r;
 
                r = unit_file_search(NULL, &instance, lp, SEARCH_FOLLOW_CONFIG_SYMLINKS);
                if (r < 0)
                        return r;
 
                if (instance.install_mode == INSTALL_MODE_MASKED)
                        return install_changes_add(changes, n_changes, -ERFKILL, instance.path, NULL);
 
                n = instance.name;
 
        } else {
                /* We have a template, but no instance yet. When used with an instantiated unit, we will get
                 * the instance from that unit. Cannot be used with non-instance units. */
 
                valid_dst_type = UNIT_NAME_INSTANCE | UNIT_NAME_TEMPLATE;
                n = info->name;
        }
 
        r = 0;
        STRV_FOREACH(s, list) {
                _cleanup_free_ char *path = NULL, *dst = NULL;
 
                q = install_name_printf(scope, info, *s, &dst);
                if (q < 0) {
                        RET_GATHER(r, install_changes_add(changes, n_changes, q, *s, NULL));
                        continue;
                }
 
                if (!unit_name_is_valid(dst, valid_dst_type)) {
                        /* Generate a proper error here: EUCLEAN if the name is generally bad, EIDRM if the
                         * template status doesn't match. If we are doing presets don't bother reporting the
                         * error. This also covers cases like 'systemctl preset serial-getty@.service', which
                         * has no DefaultInstance, so there is nothing we can do. At the same time,
                         * 'systemctl enable serial-getty@.service' should fail, the user should specify an
                         * instance like in 'systemctl enable serial-getty@ttyS0.service'.
                         */
                        if (FLAGS_SET(file_flags, UNIT_FILE_IGNORE_AUXILIARY_FAILURE))
                                continue;
 
                        if (unit_name_is_valid(dst, UNIT_NAME_ANY))
                                RET_GATHER(r, install_changes_add(changes, n_changes, -EIDRM, dst, n));
                        else
                                RET_GATHER(r, install_changes_add(changes, n_changes, -EUCLEAN, dst, NULL));
 
                        continue;
                }
 
                path = strjoin(config_path, "/", dst, suffix, n);
                if (!path)
                        return -ENOMEM;
 
                q = create_symlink(lp, info->path, path, /* force= */ true, changes, n_changes);
                if (q != 0 && r >= 0)
                        r = q;
 
                if (unit_file_exists(scope, lp, dst) == 0) {
                        q = install_changes_add(changes, n_changes, INSTALL_CHANGE_DESTINATION_NOT_PRESENT, dst, info->path);
                        if (q < 0)
                                return q;
                }
        }
 
        return r;
}
 
static int install_info_symlink_link(
                InstallInfo *info,
                const LookupPaths *lp,
                const char *config_path,
                bool force,
                InstallChange **changes,
                size_t *n_changes) {
 
        _cleanup_free_ char *path = NULL;
        int r;
 
        assert(info);
        assert(lp);
        assert(config_path);
        assert(info->path);
 
        r = in_search_path(lp, info->path);
        if (r < 0)
                return r;
        if (r > 0)
                return 0;
 
        path = path_join(config_path, info->name);
        if (!path)
                return -ENOMEM;
 
        return create_symlink(lp, info->path, path, force, changes, n_changes);
}
 
static int install_info_apply(
                RuntimeScope scope,
                UnitFileFlags file_flags,
                InstallInfo *info,
                const LookupPaths *lp,
                const char *config_path,
                InstallChange **changes,
                size_t *n_changes) {
 
        int r, q;
 
        assert(info);
        assert(lp);
        assert(config_path);
 
        if (info->install_mode != INSTALL_MODE_REGULAR)
                return 0;
 
        bool force = file_flags & UNIT_FILE_FORCE;
 
        r = install_info_symlink_link(info, lp, config_path, force, changes, n_changes);
        /* Do not count links to the unit file towards the "carries_install_info" count */
        if (r < 0)
                /* If linking of the file failed, do not try to create other symlinks,
                 * because they might would pointing to a non-existent or wrong unit. */
                return r;
 
        r = install_info_symlink_alias(scope, file_flags, info, lp, config_path, force, changes, n_changes);
 
        q = install_info_symlink_wants(scope, file_flags, info, lp, config_path, info->wanted_by, ".wants/", changes, n_changes);
        if (q != 0 && r >= 0)
                r = q;
 
        q = install_info_symlink_wants(scope, file_flags, info, lp, config_path, info->required_by, ".requires/", changes, n_changes);
        if (q != 0 && r >= 0)
                r = q;
 
        q = install_info_symlink_wants(scope, file_flags, info, lp, config_path, info->upheld_by, ".upholds/", changes, n_changes);
        if (q != 0 && r >= 0)
                r = q;
 
        return r;
}
 
static int install_context_apply(
                InstallContext *ctx,
                const LookupPaths *lp,
                UnitFileFlags file_flags,
                const char *config_path,
                SearchFlags flags,
                InstallChange **changes,
                size_t *n_changes) {
 
        InstallInfo *i;
        int r;
 
        assert(ctx);
        assert(lp);
        assert(config_path);
 
        if (ordered_hashmap_isempty(ctx->will_process))
                return 0;
 
        r = ordered_hashmap_ensure_allocated(&ctx->have_processed, &install_info_hash_ops);
        if (r < 0)
                return r;
 
        r = 0;
        while ((i = ordered_hashmap_first(ctx->will_process))) {
                int q;
 
                q = ordered_hashmap_move_one(ctx->have_processed, ctx->will_process, i->name);
                if (q < 0)
                        return q;
 
                q = install_info_traverse(ctx, lp, i, flags, NULL);
                if (q < 0) {
                        if (i->auxiliary) {
                                q = install_changes_add(changes, n_changes, INSTALL_CHANGE_AUXILIARY_FAILED, i->name, NULL);
                                if (q < 0)
                                        return q;
                                continue;
                        }
 
                        return install_changes_add(changes, n_changes, q, i->name, NULL);
                }
 
                /* We can attempt to process a masked unit when a different unit
                 * that we were processing specifies it in Also=. */
                if (i->install_mode == INSTALL_MODE_MASKED) {
                        q = install_changes_add(changes, n_changes, INSTALL_CHANGE_IS_MASKED, i->path, NULL);
                        if (q < 0)
                                return q;
                        if (r >= 0)
                                /* Assume that something *could* have been enabled here,
                                 * avoid "empty [Install] section" warning. */
                                r += 1;
                        continue;
                }
 
                if (i->install_mode != INSTALL_MODE_REGULAR)
                        continue;
 
                q = install_info_apply(ctx->scope, file_flags, i, lp, config_path, changes, n_changes);
                if (r >= 0) {
                        if (q < 0)
                                r = q;
                        else
                                r += q;
                }
        }
 
        return r;
}
 
static int install_context_mark_for_removal(
                InstallContext *ctx,
                const LookupPaths *lp,
                Set **remove_symlinks_to,
                const char *config_path,
                InstallChange **changes,
                size_t *n_changes) {
 
        InstallInfo *i;
        int r;
 
        assert(ctx);
        assert(lp);
        assert(config_path);
 
        /* Marks all items for removal */
 
        if (ordered_hashmap_isempty(ctx->will_process))
                return 0;
 
        r = ordered_hashmap_ensure_allocated(&ctx->have_processed, &install_info_hash_ops);
        if (r < 0)
                return r;
 
        while ((i = ordered_hashmap_first(ctx->will_process))) {
 
                r = ordered_hashmap_move_one(ctx->have_processed, ctx->will_process, i->name);
                if (r < 0)
                        return r;
 
                r = install_info_traverse(ctx, lp, i, SEARCH_LOAD|SEARCH_FOLLOW_CONFIG_SYMLINKS, NULL);
                if (r == -ENOLINK) {
                        log_debug_errno(r, "Name %s leads to a dangling symlink, removing name.", i->name);
                        r = install_changes_add(changes, n_changes, INSTALL_CHANGE_IS_DANGLING, i->path ?: i->name, NULL);
                        if (r < 0)
                                return r;
                } else if (r == -ENOENT) {
                        if (i->auxiliary)  /* some unit specified in Also= or similar is missing */
                                log_debug_errno(r, "Auxiliary unit of %s not found, removing name.", i->name);
                        else {
                                log_debug_errno(r, "Unit %s not found, removing name.", i->name);
                                r = install_changes_add(changes, n_changes, r, i->path ?: i->name, NULL);
                                /* In case there's no unit, we still want to remove any leftover symlink, even if
                                 * the unit might have been removed already, hence treating ENOENT as non-fatal. */
                                if (r != -ENOENT)
                                        return r;
                        }
                } else if (r < 0) {
                        log_debug_errno(r, "Failed to find unit %s, removing name: %m", i->name);
                        int k = install_changes_add(changes, n_changes, r, i->path ?: i->name, NULL);
                        if (k != r)
                                return k;
                } else if (i->install_mode == INSTALL_MODE_MASKED) {
                        log_debug("Unit file %s is masked, ignoring.", i->name);
                        r = install_changes_add(changes, n_changes, INSTALL_CHANGE_IS_MASKED, i->path ?: i->name, NULL);
                        if (r < 0)
                                return r;
                        continue;
                } else if (i->install_mode != INSTALL_MODE_REGULAR) {
                        log_debug("Unit %s has install mode %s, ignoring.",
                                  i->name, install_mode_to_string(i->install_mode) ?: "invalid");
                        continue;
                }
 
                r = mark_symlink_for_removal(remove_symlinks_to, i->name);
                if (r < 0)
                        return r;
        }
 
        return 0;
}
 
int unit_file_mask(
                RuntimeScope scope,
                UnitFileFlags flags,
                const char *root_dir,
                char * const *names,
                InstallChange **changes,
                size_t *n_changes) {
 
        _cleanup_(lookup_paths_done) LookupPaths lp = {};
        const char *config_path;
        int r;
 
        assert(scope >= 0);
        assert(scope < _RUNTIME_SCOPE_MAX);
 
        r = lookup_paths_init(&lp, scope, 0, root_dir);
        if (r < 0)
                return r;
 
        config_path = (flags & UNIT_FILE_RUNTIME) ? lp.runtime_config : lp.persistent_config;
        if (!config_path)
                return -ENXIO;
 
        r = 0;
 
        STRV_FOREACH(name, names) {
                _cleanup_free_ char *path = NULL;
 
                if (!unit_name_is_valid(*name, UNIT_NAME_ANY)) {
                        RET_GATHER(r, -EINVAL);
                        continue;
                }
 
                path = path_make_absolute(*name, config_path);
                if (!path)
                        return -ENOMEM;
 
                RET_GATHER(r, create_symlink(&lp, "/dev/null", path, flags & UNIT_FILE_FORCE, changes, n_changes));
        }
 
        return r;
}
 
int unit_file_unmask(
                RuntimeScope scope,
                UnitFileFlags flags,
                const char *root_dir,
                char * const *names,
                InstallChange **changes,
                size_t *n_changes) {
 
        _cleanup_(lookup_paths_done) LookupPaths lp = {};
        _cleanup_set_free_ Set *remove_symlinks_to = NULL;
        _cleanup_strv_free_ char **todo = NULL;
        const char *config_path;
        size_t n_todo = 0;
        int r, q;
 
        assert(scope >= 0);
        assert(scope < _RUNTIME_SCOPE_MAX);
 
        r = lookup_paths_init(&lp, scope, 0, root_dir);
        if (r < 0)
                return r;
 
        config_path = (flags & UNIT_FILE_RUNTIME) ? lp.runtime_config : lp.persistent_config;
        if (!config_path)
                return -ENXIO;
 
        bool dry_run = flags & UNIT_FILE_DRY_RUN;
 
        STRV_FOREACH(name, names) {
                if (!unit_name_is_valid(*name, UNIT_NAME_ANY))
                        return -EINVAL;
 
                /* If root_dir is set, we don't care about kernel command line or generators.
                 * But if it is not set, we need to check for interference. */
                if (!root_dir) {
                        _cleanup_(install_info_clear) InstallInfo info = {
                                .name = *name,  /* We borrow *name temporarily… */
                                .install_mode = _INSTALL_MODE_INVALID,
                        };
 
                        r = unit_file_search(NULL, &info, &lp, 0);
                        if (r < 0) {
                                if (r != -ENOENT)
                                        log_debug_errno(r, "Failed to look up unit %s, ignoring: %m", info.name);
                        } else if (info.install_mode == INSTALL_MODE_MASKED &&
                                   path_is_generator(&lp, info.path)) {
                                r = install_changes_add(changes, n_changes,
                                                        INSTALL_CHANGE_IS_MASKED_GENERATOR, info.name, info.path);
                                if (r < 0) {
                                        TAKE_PTR(info.name); /* Return the borrowed name before bailing */
                                        return r;
                                }
                        }
 
                        TAKE_PTR(info.name);  /* … and give it back here */
                }
 
                _cleanup_free_ char *path = path_make_absolute(*name, config_path);
                if (!path)
                        return -ENOMEM;
 
                r = null_or_empty_path(path);
                if (r == -ENOENT)
                        continue;
                if (r < 0)
                        return r;
                if (r == 0)
                        continue;
 
                if (!GREEDY_REALLOC0(todo, n_todo + 2))
                        return -ENOMEM;
 
                todo[n_todo] = strdup(*name);
                if (!todo[n_todo])
                        return -ENOMEM;
 
                n_todo++;
        }
 
        strv_uniq(todo);
 
        r = 0;
        STRV_FOREACH(i, todo) {
                _cleanup_free_ char *path = NULL;
                const char *rp;
 
                path = path_make_absolute(*i, config_path);
                if (!path)
                        return -ENOMEM;
 
                if (!dry_run && unlink(path) < 0) {
                        if (errno != ENOENT)
                                RET_GATHER(r, install_changes_add(changes, n_changes, -errno, path, NULL));
 
                        continue;
                }
 
                q = install_changes_add(changes, n_changes, INSTALL_CHANGE_UNLINK, path, NULL);
                if (q < 0)
                        return q;
 
                rp = skip_root(lp.root_dir, path);
                q = mark_symlink_for_removal(&remove_symlinks_to, rp ?: path);
                if (q < 0)
                        return q;
        }
 
        RET_GATHER(r, remove_marked_symlinks(remove_symlinks_to, config_path, &lp, dry_run, changes, n_changes));
 
        return r;
}
 
int unit_file_link(
                RuntimeScope scope,
                UnitFileFlags flags,
                const char *root_dir,
                char * const *files,
                InstallChange **changes,
                size_t *n_changes) {
 
        _cleanup_(lookup_paths_done) LookupPaths lp = {};
        _cleanup_ordered_hashmap_free_ OrderedHashmap *todo = NULL;
        const char *config_path;
        int r;
 
        assert(scope >= 0);
        assert(scope < _RUNTIME_SCOPE_MAX);
        assert(changes);
        assert(n_changes);
 
        r = lookup_paths_init(&lp, scope, 0, root_dir);
        if (r < 0)
                return r;
 
        config_path = FLAGS_SET(flags, UNIT_FILE_RUNTIME) ? lp.runtime_config : lp.persistent_config;
        if (!config_path)
                return -ENXIO;
 
        STRV_FOREACH(file, files) {
                _cleanup_free_ char *fn = NULL, *path = NULL, *full = NULL;
 
                if (ordered_hashmap_contains(todo, *file))
                        continue;
 
                if (!path_is_absolute(*file))
                        return install_changes_add(changes, n_changes, -EINVAL, *file, NULL);
 
                r = path_extract_filename(*file, &fn);
                if (r < 0)
                        return install_changes_add(changes, n_changes, r, *file, NULL);
 
                if (!unit_name_is_valid(fn, UNIT_NAME_ANY))
                        return install_changes_add(changes, n_changes, -EUCLEAN, *file, NULL);
 
                full = path_join(lp.root_dir, *file);
                if (!full)
                        return -ENOMEM;
 
                r = verify_regular_at(AT_FDCWD, full, /* follow= */ false);
                if (r < 0)
                        return install_changes_add(changes, n_changes, r, *file, NULL);
 
                r = in_search_path(&lp, *file);
                if (r < 0)
                        return install_changes_add(changes, n_changes, r, *file, NULL);
                if (r > 0)
                        /* A silent noop if the file is already in the search path. */
                        continue;
 
                if (underneath_search_path(&lp, *file))
                        return install_changes_add(changes, n_changes, -ETXTBSY, *file, NULL);
 
                path = strdup(*file);
                if (!path)
                        return -ENOMEM;
 
                r = ordered_hashmap_ensure_put(&todo, &path_hash_ops_free_free, path, fn);
                if (r < 0)
                        return r;
                assert(r > 0);
 
                TAKE_PTR(path);
                TAKE_PTR(fn);
        }
 
        r = 0;
 
        const char *fn, *path;
        ORDERED_HASHMAP_FOREACH_KEY(fn, path, todo) {
                _cleanup_free_ char *new_path = NULL;
 
                new_path = path_make_absolute(fn, config_path);
                if (!new_path)
                        return -ENOMEM;
 
                RET_GATHER(r, create_symlink(&lp, path, new_path, FLAGS_SET(flags, UNIT_FILE_FORCE), changes, n_changes));
        }
 
        return r;
}
 
static int path_shall_revert(const LookupPaths *lp, const char *path) {
        int r;
 
        assert(lp);
        assert(path);
 
        /* Checks whether the path is one where the drop-in directories shall be removed. */
 
        r = path_is_config(lp, path, true);
        if (r != 0)
                return r;
 
        r = path_is_control(lp, path);
        if (r != 0)
                return r;
 
        return path_is_transient(lp, path);
}
 
int unit_file_revert(
                RuntimeScope scope,
                const char *root_dir,
                char * const *names,
                InstallChange **changes,
                size_t *n_changes) {
 
        _cleanup_set_free_ Set *remove_symlinks_to = NULL;
        _cleanup_(lookup_paths_done) LookupPaths lp = {};
        _cleanup_strv_free_ char **todo = NULL;
        size_t n_todo = 0;
        int r, q;
 
        /* Puts a unit file back into vendor state. This means:
         *
         * a) we remove all drop-in snippets added by the user ("config"), add to transient units
         *    ("transient"), and added via "systemctl set-property" ("control"), but not if the drop-in is
         *    generated ("generated").
         *
         * c) if there's a vendor unit file (i.e. one in /usr) we remove any configured overriding unit files
         *    (i.e. in "config", but not in "transient" or "control" or even "generated").
         *
         * We remove all that in both the runtime and the persistent directories, if that applies.
         */
 
        r = lookup_paths_init(&lp, scope, 0, root_dir);
        if (r < 0)
                return r;
 
        STRV_FOREACH(name, names) {
                bool has_vendor = false;
 
                if (!unit_name_is_valid(*name, UNIT_NAME_ANY))
                        return -EINVAL;
 
                STRV_FOREACH(p, lp.search_path) {
                        _cleanup_free_ char *path = NULL, *dropin = NULL;
                        struct stat st;
 
                        path = path_make_absolute(*name, *p);
                        if (!path)
                                return -ENOMEM;
 
                        r = RET_NERRNO(lstat(path, &st));
                        if (r < 0) {
                                if (r != -ENOENT)
                                        return install_changes_add(changes, n_changes, r, path, NULL);
                        } else if (S_ISREG(st.st_mode)) {
                                /* Check if there's a vendor version */
                                r = path_is_vendor_or_generator(&lp, path);
                                if (r < 0)
                                        return install_changes_add(changes, n_changes, r, path, NULL);
                                if (r > 0)
                                        has_vendor = true;
                        }
 
                        dropin = strjoin(path, ".d");
                        if (!dropin)
                                return -ENOMEM;
 
                        r = RET_NERRNO(lstat(dropin, &st));
                        if (r < 0) {
                                if (r != -ENOENT)
                                        return install_changes_add(changes, n_changes, r, dropin, NULL);
                        } else if (S_ISDIR(st.st_mode)) {
                                /* Remove the drop-ins */
                                r = path_shall_revert(&lp, dropin);
                                if (r < 0)
                                        return install_changes_add(changes, n_changes, r, dropin, NULL);
                                if (r > 0) {
                                        if (!GREEDY_REALLOC0(todo, n_todo + 2))
                                                return -ENOMEM;
 
                                        todo[n_todo++] = TAKE_PTR(dropin);
                                }
                        }
                }
 
                if (!has_vendor)
                        continue;
 
                /* OK, there's a vendor version, hence drop all configuration versions */
                STRV_FOREACH(p, lp.search_path) {
                        _cleanup_free_ char *path = NULL;
                        struct stat st;
 
                        path = path_make_absolute(*name, *p);
                        if (!path)
                                return -ENOMEM;
 
                        r = RET_NERRNO(lstat(path, &st));
                        if (r < 0) {
                                if (r != -ENOENT)
                                        return install_changes_add(changes, n_changes, r, path, NULL);
                        } else if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
                                r = path_is_config(&lp, path, true);
                                if (r < 0)
                                        return install_changes_add(changes, n_changes, r, path, NULL);
                                if (r > 0) {
                                        if (!GREEDY_REALLOC0(todo, n_todo + 2))
                                                return -ENOMEM;
 
                                        todo[n_todo++] = TAKE_PTR(path);
                                }
                        }
                }
        }
 
        strv_uniq(todo);
 
        r = 0;
        STRV_FOREACH(i, todo) {
                _cleanup_strv_free_ char **fs = NULL;
                const char *rp;
 
                (void) get_files_in_directory(*i, &fs);
 
                q = rm_rf(*i, REMOVE_ROOT|REMOVE_PHYSICAL);
                if (q < 0 && q != -ENOENT && r >= 0) {
                        r = q;
                        continue;
                }
 
                STRV_FOREACH(j, fs) {
                        _cleanup_free_ char *t = NULL;
 
                        t = path_join(*i, *j);
                        if (!t)
                                return -ENOMEM;
 
                        q = install_changes_add(changes, n_changes, INSTALL_CHANGE_UNLINK, t, NULL);
                        if (q < 0)
                                return q;
                }
 
                q = install_changes_add(changes, n_changes, INSTALL_CHANGE_UNLINK, *i, NULL);
                if (q < 0)
                        return q;
 
                rp = skip_root(lp.root_dir, *i);
                q = mark_symlink_for_removal(&remove_symlinks_to, rp ?: *i);
                if (q < 0)
                        return q;
        }
 
        q = remove_marked_symlinks(remove_symlinks_to, lp.runtime_config, &lp, false, changes, n_changes);
        if (r >= 0)
                r = q;
 
        q = remove_marked_symlinks(remove_symlinks_to, lp.persistent_config, &lp, false, changes, n_changes);
        if (r >= 0)
                r = q;
 
        return r;
}
 
int unit_file_add_dependency(
                RuntimeScope scope,
                UnitFileFlags file_flags,
                const char *root_dir,
                char * const *names,
                const char *target,
                UnitDependency dep,
                InstallChange **changes,
                size_t *n_changes) {
 
        _cleanup_(lookup_paths_done) LookupPaths lp = {};
        _cleanup_(install_context_done) InstallContext ctx = { .scope = scope };
        InstallInfo *info, *target_info;
        const char *config_path;
        int r;
 
        assert(scope >= 0);
        assert(scope < _RUNTIME_SCOPE_MAX);
        assert(target);
        assert(IN_SET(dep, UNIT_WANTS, UNIT_REQUIRES));
 
        if (!unit_name_is_valid(target, UNIT_NAME_ANY))
                return install_changes_add(changes, n_changes, -EUCLEAN, target, NULL);
 
        r = lookup_paths_init(&lp, scope, 0, root_dir);
        if (r < 0)
                return r;
 
        config_path = (file_flags & UNIT_FILE_RUNTIME) ? lp.runtime_config : lp.persistent_config;
        if (!config_path)
                return -ENXIO;
 
        r = install_info_discover_and_check(&ctx, &lp, target, SEARCH_FOLLOW_CONFIG_SYMLINKS,
                                            &target_info, changes, n_changes);
        if (r < 0)
                return r;
 
        assert(target_info->install_mode == INSTALL_MODE_REGULAR);
 
        STRV_FOREACH(name, names) {
                char ***l;
 
                r = install_info_discover_and_check(&ctx, &lp, *name,
                                                    SEARCH_FOLLOW_CONFIG_SYMLINKS,
                                                    &info, changes, n_changes);
                if (r < 0)
                        return r;
 
                assert(info->install_mode == INSTALL_MODE_REGULAR);
 
                /* We didn't actually load anything from the unit
                 * file, but instead just add in our new symlink to
                 * create. */
 
                if (dep == UNIT_WANTS)
                        l = &info->wanted_by;
                else if (dep == UNIT_REQUIRES)
                        l = &info->required_by;
                else
                        l = &info->upheld_by;
 
                strv_free(*l);
                *l = strv_new(target_info->name);
                if (!*l)
                        return -ENOMEM;
        }
 
        return install_context_apply(&ctx, &lp, file_flags, config_path,
                                     SEARCH_FOLLOW_CONFIG_SYMLINKS, changes, n_changes);
}
 
static int do_unit_file_enable(
                const LookupPaths *lp,
                RuntimeScope scope,
                UnitFileFlags flags,
                const char *config_path,
                char * const *names_or_paths,
                InstallChange **changes,
                size_t *n_changes) {
 
        _cleanup_(install_context_done) InstallContext ctx = { .scope = scope };
        InstallInfo *info;
        int r;
 
        STRV_FOREACH(name, names_or_paths) {
                r = install_info_discover_and_check(&ctx, lp, *name,
                                                    SEARCH_LOAD | SEARCH_FOLLOW_CONFIG_SYMLINKS,
                                                    &info, changes, n_changes);
                if (r < 0)
                        return r;
 
                assert(info->install_mode == INSTALL_MODE_REGULAR);
        }
 
        /* This will return the number of symlink rules that were
           supposed to be created, not the ones actually created. This
           is useful to determine whether the passed units had any
           installation data at all. */
 
        return install_context_apply(&ctx, lp, flags, config_path,
                                     SEARCH_LOAD, changes, n_changes);
}
 
int unit_file_enable(
                RuntimeScope scope,
                UnitFileFlags flags,
                const char *root_dir,
                char * const *names_or_paths,
                InstallChange **changes,
                size_t *n_changes) {
 
        _cleanup_(lookup_paths_done) LookupPaths lp = {};
        int r;
 
        assert(scope >= 0);
        assert(scope < _RUNTIME_SCOPE_MAX);
 
        r = lookup_paths_init(&lp, scope, 0, root_dir);
        if (r < 0)
                return r;
 
        const char *config_path = config_path_from_flags(&lp, flags);
        if (!config_path)
                return -ENXIO;
 
        return do_unit_file_enable(&lp, scope, flags, config_path, names_or_paths, changes, n_changes);
}
 
static int do_unit_file_disable(
                const LookupPaths *lp,
                RuntimeScope scope,
                UnitFileFlags flags,
                const char *config_path,
                char * const *names,
                InstallChange **changes,
                size_t *n_changes) {
 
        _cleanup_(install_context_done) InstallContext ctx = { .scope = scope };
        bool has_install_info = false;
        int r;
 
        assert(changes);
        assert(n_changes);
 
        STRV_FOREACH(name, names) {
                InstallInfo *info;
 
                if (!unit_name_is_valid(*name, UNIT_NAME_ANY))
                        return install_changes_add(changes, n_changes, -EUCLEAN, *name, NULL);
 
                r = install_info_add(&ctx, *name, NULL, lp->root_dir, /* auxiliary= */ false, &info);
                if (r >= 0)
                        r = install_info_traverse(&ctx, lp, info, SEARCH_LOAD|SEARCH_FOLLOW_CONFIG_SYMLINKS, NULL);
                if (r < 0) {
                        r = install_changes_add(changes, n_changes, r, *name, NULL);
                        /* In case there's no unit, we still want to remove any leftover symlink, even if
                         * the unit might have been removed already, hence treating ENOENT as non-fatal. */
                        if (r != -ENOENT)
                                return r;
                }
 
                /* If we enable multiple units, some with install info and others without,
                 * the "empty [Install] section" warning is not shown. Let's make the behavior
                 * of disable align with that. */
                has_install_info = has_install_info || install_info_has_rules(info) || install_info_has_also(info);
        }
 
        _cleanup_set_free_ Set *remove_symlinks_to = NULL;
        r = install_context_mark_for_removal(&ctx, lp, &remove_symlinks_to, config_path, changes, n_changes);
        if (r >= 0)
                r = remove_marked_symlinks(remove_symlinks_to, config_path, lp, flags & UNIT_FILE_DRY_RUN, changes, n_changes);
        if (r < 0)
                return r;
 
        /* The warning is shown only if it's a no-op */
        return install_changes_have_modification(*changes, *n_changes) || has_install_info;
}
 
int unit_file_disable(
                RuntimeScope scope,
                UnitFileFlags flags,
                const char *root_dir,
                char * const *files,
                InstallChange **changes,
                size_t *n_changes) {
 
        _cleanup_(lookup_paths_done) LookupPaths lp = {};
        int r;
 
        assert(scope >= 0);
        assert(scope < _RUNTIME_SCOPE_MAX);
 
        r = lookup_paths_init(&lp, scope, 0, root_dir);
        if (r < 0)
                return r;
 
        const char *config_path = config_path_from_flags(&lp, flags);
        if (!config_path)
                return -ENXIO;
 
        return do_unit_file_disable(&lp, scope, flags, config_path, files, changes, n_changes);
}
 
static int normalize_linked_files(
                RuntimeScope scope,
                const LookupPaths *lp,
                char * const *names_or_paths,
                char ***ret_names,
                char ***ret_files) {
 
        /* This is similar to normalize_filenames()/normalize_names() in src/systemctl/,
         * but operates on real unit names. For each argument we look up the actual path
         * where the unit is found. This way linked units can be re-enabled successfully. */
 
        _cleanup_strv_free_ char **files = NULL, **names = NULL;
        int r;
 
        assert(lp);
        assert(ret_names);
        assert(ret_files);
 
        STRV_FOREACH(a, names_or_paths) {
                _cleanup_(install_context_done) InstallContext ctx = { .scope = scope };
                InstallInfo *i = NULL;
                _cleanup_free_ char *n = NULL;
 
                r = path_extract_filename(*a, &n);
                if (r < 0)
                        return r;