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 1001–2000 of 3,921

                        log_error_errno(errno, "Failed to open directory \"%s\" while scanning for symlinks, ignoring: %m", path);
                        continue;
                }
 
                r = find_symlinks_in_directory(d, path, root_dir, i,
                                               /* ignore_destination= */ true,
                                               /* match_name= */ match_name,
                                               /* ignore_same_name= */ ignore_same_name,
                                               config_path,
                                               same_name_link);
                if (r > 0)
                        return 1;
                if (r < 0)
                        log_debug_errno(r, "Failed to look up symlinks in \"%s\": %m", path);
        }
 
        /* We didn't find any suitable symlinks in .wants, .requires or .upholds directories,
         * let's look for linked unit files in this directory. */
        rewinddir(config_dir);
        return find_symlinks_in_directory(config_dir, config_path, root_dir, i,
                                          /* ignore_destination= */ false,
                                          /* match_name= */ match_name,
                                          /* ignore_same_name= */ ignore_same_name,
                                          config_path,
                                          same_name_link);
}
 
static int find_symlinks_in_scope(
                RuntimeScope scope,
                const LookupPaths *lp,
                const InstallInfo *info,
                bool match_name,
                UnitFileState *state) {
 
        bool same_name_link_runtime = false, same_name_link_config = false;
        bool enabled_in_runtime = false, enabled_at_all = false;
        bool ignore_same_name = false;
        int r;
 
        assert(lp);
        assert(info);
        assert(state);
 
        /* As we iterate over the list of search paths in lp->search_path, we may encounter "same name"
         * symlinks. The ones which are "below" (i.e. have lower priority) than the unit file itself are
         * effectively masked, so we should ignore them. */
 
        STRV_FOREACH(p, lp->search_path)  {
                bool same_name_link = false;
 
                r = find_symlinks(lp->root_dir, info, match_name, ignore_same_name, *p, &same_name_link);
                if (r < 0)
                        return r;
                if (r > 0) {
                        /* We found symlinks in this dir? Yay! Let's see where precisely it is enabled. */
 
                        if (path_equal(*p, lp->persistent_config)) {
                                /* This is the best outcome, let's return it immediately. */
                                *state = UNIT_FILE_ENABLED;
                                return 1;
                        }
 
                        /* look for global enablement of user units */
                        if (scope == RUNTIME_SCOPE_USER && path_is_user_config_dir(*p)) {
                                *state = UNIT_FILE_ENABLED;
                                return 1;
                        }
 
                        r = path_is_runtime(lp, *p, false);
                        if (r < 0)
                                return r;
                        if (r > 0)
                                enabled_in_runtime = true;
                        else
                                enabled_at_all = true;
 
                } else if (same_name_link) {
                        if (path_equal(*p, lp->persistent_config))
                                same_name_link_config = true;
                        else {
                                r = path_is_runtime(lp, *p, false);
                                if (r < 0)
                                        return r;
                                if (r > 0)
                                        same_name_link_runtime = true;
                        }
                }
 
                /* Check if next iteration will be "below" the unit file (either a regular file
                 * or a symlink), and hence should be ignored */
                if (!ignore_same_name && path_startswith(info->path, *p))
                        ignore_same_name = true;
        }
 
        if (enabled_in_runtime) {
                *state = UNIT_FILE_ENABLED_RUNTIME;
                return 1;
        }
 
        /* Here's a special rule: if the unit we are looking for is an instance, and it symlinked in the search path
         * outside of runtime and configuration directory, then we consider it statically enabled. Note we do that only
         * for instance, not for regular names, as those are merely aliases, while instances explicitly instantiate
         * something, and hence are a much stronger concept. */
        if (enabled_at_all && unit_name_is_valid(info->name, UNIT_NAME_INSTANCE)) {
                *state = UNIT_FILE_STATIC;
                return 1;
        }
 
        /* Hmm, we didn't find it, but maybe we found the same name
         * link? */
        if (same_name_link_config) {
                *state = UNIT_FILE_LINKED;
                return 1;
        }
        if (same_name_link_runtime) {
                *state = UNIT_FILE_LINKED_RUNTIME;
                return 1;
        }
 
        return 0;
}
 
static void install_info_clear(InstallInfo *i) {
        if (!i)
                return;
 
        i->name = mfree(i->name);
        i->path = mfree(i->path);
        i->root = mfree(i->root);
        i->aliases = strv_free(i->aliases);
        i->wanted_by = strv_free(i->wanted_by);
        i->required_by = strv_free(i->required_by);
        i->upheld_by = strv_free(i->upheld_by);
        i->also = strv_free(i->also);
        i->default_instance = mfree(i->default_instance);
        i->symlink_target = mfree(i->symlink_target);
}
 
static InstallInfo* install_info_free(InstallInfo *i) {
        install_info_clear(i);
        return mfree(i);
}
 
DEFINE_TRIVIAL_CLEANUP_FUNC(InstallInfo*, install_info_free);
 
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
                install_info_hash_ops,
                char, string_hash_func, string_compare_func,
                InstallInfo, install_info_free);
 
static void install_context_done(InstallContext *ctx) {
        assert(ctx);
 
        ctx->will_process = ordered_hashmap_free(ctx->will_process);
        ctx->have_processed = ordered_hashmap_free(ctx->have_processed);
}
 
static InstallInfo *install_info_find(InstallContext *ctx, const char *name) {
        InstallInfo *i;
 
        i = ordered_hashmap_get(ctx->have_processed, name);
        if (i)
                return i;
 
        return ordered_hashmap_get(ctx->will_process, name);
}
 
static int install_info_may_process(
                const InstallInfo *i,
                const LookupPaths *lp,
                InstallChange **changes,
                size_t *n_changes) {
        assert(i);
        assert(lp);
 
        /* Checks whether the loaded unit file is one we should process, or is masked,
         * transient or generated and thus not subject to enable/disable operations. */
 
        if (i->install_mode == INSTALL_MODE_MASKED)
                return install_changes_add(changes, n_changes, -ERFKILL, i->path, NULL);
        if (path_is_generator(lp, i->path) ||
            path_is_transient(lp, i->path))
                return install_changes_add(changes, n_changes, -EADDRNOTAVAIL, i->path, NULL);
 
        return 0;
}
 
/**
 * Adds a new InstallInfo entry under name in the InstallContext.will_process
 * hashmap, or retrieves the existing one if already present.
 *
 * Returns negative on error, 0 if the unit was already known, 1 otherwise.
 */
static int install_info_add(
                InstallContext *ctx,
                const char *name,
                const char *path,
                const char *root,
                bool auxiliary,
                InstallInfo **ret) {
 
        _cleanup_free_ char *name_alloc = NULL;
        int r;
 
        assert(ctx);
 
        if (!name) {
                assert(path);
 
                r = path_extract_filename(path, &name_alloc);
                if (r < 0)
                        return r;
 
                name = name_alloc;
        }
 
        if (!unit_name_is_valid(name, UNIT_NAME_ANY))
                return -EINVAL;
 
        InstallInfo *i = install_info_find(ctx, name);
        if (i) {
                i->auxiliary = i->auxiliary && auxiliary;
 
                if (ret)
                        *ret = i;
                return 0;
        }
 
        _cleanup_(install_info_freep) InstallInfo *new_info = new(InstallInfo, 1);
        if (!new_info)
                return -ENOMEM;
 
        *new_info = (InstallInfo) {
                .install_mode = _INSTALL_MODE_INVALID,
                .auxiliary = auxiliary,
        };
 
        if (name_alloc)
                new_info->name = TAKE_PTR(name_alloc);
        else {
                new_info->name = strdup(name);
                if (!new_info->name)
                        return -ENOMEM;
        }
 
        r = strdup_to(&new_info->root, root);
        if (r < 0)
                return r;
 
        r = strdup_to(&new_info->path, path);
        if (r < 0)
                return r;
 
        r = ordered_hashmap_ensure_put(&ctx->will_process, &install_info_hash_ops, new_info->name, new_info);
        if (r < 0)
                return r;
        i = TAKE_PTR(new_info);
 
        if (ret)
                *ret = i;
        return 1;
}
 
static int config_parse_alias(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {
 
        UnitType type;
 
        assert(unit);
        assert(filename);
        assert(lvalue);
        assert(rvalue);
 
        type = unit_name_to_type(unit);
        if (!unit_type_may_alias(type))
                return log_syntax(unit, LOG_WARNING, filename, line, 0,
                                  "Alias= is not allowed for %s units, ignoring.",
                                  unit_type_to_string(type));
 
        return config_parse_strv(unit, filename, line, section, section_line,
                                 lvalue, ltype, rvalue, data, userdata);
}
 
static int config_parse_also(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {
 
        InstallInfo *info = ASSERT_PTR(userdata);
        InstallContext *ctx = ASSERT_PTR(data);
        int r;
 
        assert(unit);
        assert(filename);
        assert(lvalue);
        assert(rvalue);
 
        for (;;) {
                _cleanup_free_ char *word = NULL, *printed = NULL;
 
                r = extract_first_word(&rvalue, &word, NULL, 0);
                if (r < 0)
                        return r;
                if (r == 0)
                        break;
 
                r = install_name_printf(ctx->scope, info, word, &printed);
                if (r < 0)
                        return log_syntax(unit, LOG_WARNING, filename, line, r,
                                          "Failed to resolve unit name in Also=\"%s\": %m", word);
 
                r = install_info_add(ctx, printed, NULL, info->root, /* auxiliary= */ true, NULL);
                if (r < 0)
                        return r;
 
                r = strv_push(&info->also, printed);
                if (r < 0)
                        return r;
 
                printed = NULL;
        }
 
        return 0;
}
 
static int config_parse_default_instance(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {
 
        InstallContext *ctx = ASSERT_PTR(data);
        InstallInfo *info = ASSERT_PTR(userdata);
        _cleanup_free_ char *printed = NULL;
        int r;
 
        assert(unit);
        assert(filename);
        assert(lvalue);
        assert(rvalue);
 
        if (unit_name_is_valid(unit, UNIT_NAME_INSTANCE))
                /* When enabling an instance, we might be using a template unit file,
                 * but we should ignore DefaultInstance silently. */
                return 0;
        if (!unit_name_is_valid(unit, UNIT_NAME_TEMPLATE))
                return log_syntax(unit, LOG_WARNING, filename, line, 0,
                                  "DefaultInstance= only makes sense for template units, ignoring.");
 
        r = install_name_printf(ctx->scope, info, rvalue, &printed);
        if (r < 0)
                return log_syntax(unit, LOG_WARNING, filename, line, r,
                                  "Failed to resolve instance name in DefaultInstance=\"%s\": %m", rvalue);
 
        if (isempty(printed))
                printed = mfree(printed);
 
        if (printed && !unit_instance_is_valid(printed))
                return log_syntax(unit, LOG_WARNING, filename, line, SYNTHETIC_ERRNO(EINVAL),
                                  "Invalid DefaultInstance= value \"%s\".", printed);
 
        return free_and_replace(info->default_instance, printed);
}
 
static int unit_file_load(
                InstallContext *ctx,
                InstallInfo *info,
                const char *path,
                const char *root_dir,
                SearchFlags flags) {
 
        const ConfigTableItem items[] = {
                { "Install", "Alias",           config_parse_alias,            0, &info->aliases           },
                { "Install", "WantedBy",        config_parse_strv,             0, &info->wanted_by         },
                { "Install", "RequiredBy",      config_parse_strv,             0, &info->required_by       },
                { "Install", "UpheldBy",        config_parse_strv,             0, &info->upheld_by         },
                { "Install", "DefaultInstance", config_parse_default_instance, 0, info                     },
                { "Install", "Also",            config_parse_also,             0, ctx                      },
                {}
        };
 
        UnitType type;
        _cleanup_fclose_ FILE *f = NULL;
        _cleanup_close_ int fd = -EBADF;
        struct stat st;
        int r;
 
        assert(info);
        assert(path);
 
        if (!(flags & SEARCH_DROPIN)) {
                /* Loading or checking for the main unit file… */
 
                type = unit_name_to_type(info->name);
                if (type < 0)
                        return -EINVAL;
                if (unit_name_is_valid(info->name, UNIT_NAME_TEMPLATE|UNIT_NAME_INSTANCE) && !unit_type_may_template(type))
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                               "%s: unit type %s cannot be templated, ignoring.", path, unit_type_to_string(type));
 
                if (!(flags & SEARCH_LOAD)) {
                        if (lstat(path, &st) < 0)
                                return -errno;
 
                        if (null_or_empty(&st))
                                info->install_mode = INSTALL_MODE_MASKED;
                        else if (S_ISREG(st.st_mode))
                                info->install_mode = INSTALL_MODE_REGULAR;
                        else if (S_ISLNK(st.st_mode))
                                return -ELOOP;
                        else if (S_ISDIR(st.st_mode))
                                return -EISDIR;
                        else
                                return -ENOTTY;
 
                        return 0;
                }
 
                fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
                if (fd < 0)
                        return -errno;
        } else {
                /* Operating on a drop-in file. If we aren't supposed to load the unit file drop-ins don't matter, let's hence shortcut this. */
 
                if (!(flags & SEARCH_LOAD))
                        return 0;
 
                fd = chase_and_open(path, root_dir, 0, O_RDONLY|O_CLOEXEC|O_NOCTTY, NULL);
                if (fd < 0)
                        return fd;
        }
 
        if (fstat(fd, &st) < 0)
                return -errno;
 
        if (null_or_empty(&st)) {
                if ((flags & SEARCH_DROPIN) == 0)
                        info->install_mode = INSTALL_MODE_MASKED;
 
                return 0;
        }
 
        r = stat_verify_regular(&st);
        if (r < 0)
                return r;
 
        f = take_fdopen(&fd, "r");
        if (!f)
                return -errno;
 
        /* ctx is only needed if we actually load the file (it's referenced from items[] btw, in case you wonder.) */
        assert(ctx);
 
        r = config_parse(info->name, path, f,
                         "Install\0"
                         "-Unit\0"
                         "-Automount\0"
                         "-Device\0"
                         "-Mount\0"
                         "-Path\0"
                         "-Scope\0"
                         "-Service\0"
                         "-Slice\0"
                         "-Socket\0"
                         "-Swap\0"
                         "-Target\0"
                         "-Timer\0",
                         config_item_table_lookup, items,
                         0, info,
                         NULL);
        if (r < 0)
                return log_debug_errno(r, "Failed to parse \"%s\": %m", info->name);
 
        if ((flags & SEARCH_DROPIN) == 0)
                info->install_mode = INSTALL_MODE_REGULAR;
 
        return
                (int) strv_length(info->aliases) +
                (int) strv_length(info->wanted_by) +
                (int) strv_length(info->required_by) +
                (int) strv_length(info->upheld_by);
}
 
static int unit_file_load_or_readlink(
                InstallContext *ctx,
                InstallInfo *info,
                const char *path,
                const LookupPaths *lp,
                SearchFlags flags) {
        int r;
 
        r = unit_file_load(ctx, info, path, lp->root_dir, flags);
        if (r != -ELOOP || (flags & SEARCH_DROPIN))
                return r;
 
        /* This is a symlink, let's read and verify it. */
        r = unit_file_resolve_symlink(lp->root_dir, lp->search_path,
                                      NULL, AT_FDCWD, path,
                                      true, &info->symlink_target);
        if (r < 0)
                return r;
        bool outside_search_path = r > 0;
 
        r = null_or_empty_path_with_root(info->symlink_target, lp->root_dir);
        if (r < 0 && r != -ENOENT)
                return log_debug_errno(r, "Failed to stat %s: %m", info->symlink_target);
        if (r > 0)
                info->install_mode = INSTALL_MODE_MASKED;
        else if (outside_search_path)
                info->install_mode = INSTALL_MODE_LINKED;
        else
                info->install_mode = INSTALL_MODE_ALIAS;
 
        return 0;
}
 
static int unit_file_search(
                InstallContext *ctx,
                InstallInfo *info,
                const LookupPaths *lp,
                SearchFlags flags) {
 
        const char *dropin_dir_name = NULL, *dropin_template_dir_name = NULL;
        _cleanup_strv_free_ char **dirs = NULL, **files = NULL;
        _cleanup_free_ char *template = NULL;
        bool found_unit = false;
        int r, result;
 
        assert(info);
        assert(lp);
 
        /* Was this unit already loaded? */
        if (info->install_mode != _INSTALL_MODE_INVALID)
                return 0;
 
        if (info->path)
                return unit_file_load_or_readlink(ctx, info, info->path, lp, flags);
 
        assert(info->name);
 
        if (!FLAGS_SET(flags, SEARCH_IGNORE_TEMPLATE) && unit_name_is_valid(info->name, UNIT_NAME_INSTANCE)) {
                r = unit_name_template(info->name, &template);
                if (r < 0)
                        return r;
        }
 
        STRV_FOREACH(p, lp->search_path) {
                _cleanup_free_ char *path = NULL;
 
                path = path_join(*p, info->name);
                if (!path)
                        return -ENOMEM;
 
                r = unit_file_load_or_readlink(ctx, info, path, lp, flags);
                if (r >= 0) {
                        info->path = TAKE_PTR(path);
                        result = r;
                        found_unit = true;
                        break;
                } else if (!IN_SET(r, -ENOENT, -ENOTDIR, -EACCES))
                        return r;
        }
 
        if (!found_unit && template) {
 
                /* Unit file doesn't exist, however instance
                 * enablement was requested.  We will check if it is
                 * possible to load template unit file. */
 
                STRV_FOREACH(p, lp->search_path) {
                        _cleanup_free_ char *path = NULL;
 
                        path = path_join(*p, template);
                        if (!path)
                                return -ENOMEM;
 
                        r = unit_file_load_or_readlink(ctx, info, path, lp, flags);
                        if (r >= 0) {
                                info->path = TAKE_PTR(path);
                                result = r;
                                found_unit = true;
                                break;
                        } else if (!IN_SET(r, -ENOENT, -ENOTDIR, -EACCES))
                                return r;
                }
        }
 
        if (!found_unit)
                return log_debug_errno(SYNTHETIC_ERRNO(ENOENT),
                                       "Cannot find unit %s%s%s.",
                                       info->name, template ? " or " : "", strempty(template));
 
        if (info->install_mode == INSTALL_MODE_MASKED)
                return result;
 
        /* Search for drop-in directories */
 
        dropin_dir_name = strjoina(info->name, ".d");
        STRV_FOREACH(p, lp->search_path) {
                char *path;
 
                path = path_join(*p, dropin_dir_name);
                if (!path)
                        return -ENOMEM;
 
                r = strv_consume(&dirs, path);
                if (r < 0)
                        return r;
        }
 
        if (template) {
                dropin_template_dir_name = strjoina(template, ".d");
                STRV_FOREACH(p, lp->search_path) {
                        char *path;
 
                        path = path_join(*p, dropin_template_dir_name);
                        if (!path)
                                return -ENOMEM;
 
                        r = strv_consume(&dirs, path);
                        if (r < 0)
                                return r;
                }
        }
 
        /* Load drop-in conf files */
 
        r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char**) dirs);
        if (r < 0)
                return log_debug_errno(r, "Failed to get list of conf files: %m");
 
        STRV_FOREACH(p, files) {
                r = unit_file_load_or_readlink(ctx, info, *p, lp, flags | SEARCH_DROPIN);
                if (r < 0)
                        return log_debug_errno(r, "Failed to load conf file \"%s\": %m", *p);
        }
 
        return result;
}
 
static int install_info_follow(
                InstallContext *ctx,
                InstallInfo *info,
                const LookupPaths *lp,
                SearchFlags flags,
                bool ignore_different_name) {
 
        assert(ctx);
        assert(info);
 
        if (!IN_SET(info->install_mode, INSTALL_MODE_ALIAS, INSTALL_MODE_LINKED))
                return -EINVAL;
        if (!info->symlink_target)
                return -EINVAL;
 
        /* If the basename doesn't match, the caller should add a complete new entry for this. */
 
        if (!ignore_different_name && !path_equal_filename(info->symlink_target, info->name))
                return -EXDEV;
 
        free_and_replace(info->path, info->symlink_target);
        info->install_mode = _INSTALL_MODE_INVALID;
 
        return unit_file_load_or_readlink(ctx, info, info->path, lp, flags);
}
 
/**
 * Search for the unit file. If the unit name is a symlink, follow the symlink to the
 * target, maybe more than once. Propagate the instance name if present.
 */
static int install_info_traverse(
                InstallContext *ctx,
                const LookupPaths *lp,
                InstallInfo *start,
                SearchFlags flags,
                InstallInfo **ret) {
 
        InstallInfo *i;
        unsigned k = 0;
        int r;
 
        assert(ctx);
        assert(lp);
        assert(start);
 
        r = unit_file_search(ctx, start, lp, flags);
        if (r < 0)
                return r;
 
        i = start;
        while (IN_SET(i->install_mode, INSTALL_MODE_ALIAS, INSTALL_MODE_LINKED)) {
                /* Follow the symlink */
 
                if (++k > UNIT_FILE_FOLLOW_SYMLINK_MAX)
                        return -ELOOP;
 
                if (!FLAGS_SET(flags, SEARCH_FOLLOW_CONFIG_SYMLINKS)) {
                        r = path_is_config(lp, i->path, true);
                        if (r < 0)
                                return r;
                        if (r > 0)
                                return -ELOOP;
                }
 
                r = install_info_follow(ctx, i, lp, flags,
                                        /* If linked, don't look at the target name */
                                        /* ignore_different_name= */ i->install_mode == INSTALL_MODE_LINKED);
                if (r == -EXDEV && i->symlink_target) {
                        _cleanup_free_ char *target_name = NULL, *unit_instance = NULL;
                        const char *bn;
 
                        /* Target is an alias, create a new install info object and continue with that. */
 
                        r = path_extract_filename(i->symlink_target, &target_name);
                        if (r < 0)
                                return r;
 
                        if (unit_name_is_valid(i->name, UNIT_NAME_INSTANCE) &&
                            unit_name_is_valid(target_name, UNIT_NAME_TEMPLATE)) {
 
                                _cleanup_free_ char *instance = NULL;
 
                                r = unit_name_to_instance(i->name, &instance);
                                if (r < 0)
                                        return r;
 
                                r = unit_name_replace_instance(target_name, instance, &unit_instance);
                                if (r < 0)
                                        return r;
 
                                if (streq(unit_instance, i->name)) {
                                        /* We filled in the instance, and the target stayed the same? If so,
                                         * then let's honour the link as it is. */
 
                                        r = install_info_follow(ctx, i, lp, flags, true);
                                        if (r < 0)
                                                return r;
 
                                        continue;
                                }
 
                                bn = unit_instance;
                        } else
                                bn = target_name;
 
                        r = install_info_add(ctx, bn, NULL, lp->root_dir, /* auxiliary= */ false, &i);
                        if (r < 0)
                                return r;
 
                        /* Try again, with the new target we found. */
                        r = unit_file_search(ctx, i, lp, flags);
                        if (r == -ENOENT)
                                /* Translate error code to highlight this specific case */
                                return -ENOLINK;
                }
                if (r < 0)
                        return r;
        }
 
        if (ret)
                *ret = i;
 
        return 0;
}
 
/**
 * Call install_info_add() with name_or_path as the path (if name_or_path starts with "/")
 * or the name (otherwise). root_dir is prepended to the path.
 */
static int install_info_add_auto(
                InstallContext *ctx,
                const LookupPaths *lp,
                const char *name_or_path,
                InstallInfo **ret) {
 
        assert(ctx);
        assert(name_or_path);
 
        if (path_is_absolute(name_or_path)) {
                _cleanup_free_ char *pp = path_join(lp->root_dir, name_or_path);
                if (!pp)
                        return -ENOMEM;
 
                return install_info_add(ctx, NULL, pp, lp->root_dir, /* auxiliary= */ false, ret);
        } else
                return install_info_add(ctx, name_or_path, NULL, lp->root_dir, /* auxiliary= */ false, ret);
}
 
static int install_info_discover(
                InstallContext *ctx,
                const LookupPaths *lp,
                const char *name_or_path,
                SearchFlags flags,
                InstallInfo **ret,
                InstallChange **changes,
                size_t *n_changes) {
 
        InstallInfo *info;
        int r;
 
        assert(ctx);
        assert(lp);
        assert(name_or_path);
 
        r = install_info_add_auto(ctx, lp, name_or_path, &info);
        if (r >= 0)
                r = install_info_traverse(ctx, lp, info, flags, ret);
 
        if (r < 0)
                return install_changes_add(changes, n_changes, r, name_or_path, NULL);
 
        return r;
}
 
static int install_info_discover_and_check(
                InstallContext *ctx,
                const LookupPaths *lp,
                const char *name_or_path,
                SearchFlags flags,
                InstallInfo **ret,
                InstallChange **changes,
                size_t *n_changes) {
 
        int r;
 
        POINTER_MAY_BE_NULL(ret);
 
        r = install_info_discover(ctx, lp, name_or_path, flags, ret, changes, n_changes);
        if (r < 0)
                return r;
 
        return install_info_may_process(ret ? *ret : NULL, lp, changes, n_changes);
}
 
int unit_file_verify_alias(
                const InstallInfo *info,
                const char *dst,
                char **ret_dst,
                InstallChange **changes,
                size_t *n_changes) {
 
        _cleanup_free_ char *dst_updated = NULL;
        int r;
 
        assert(ret_dst);
 
        /* Verify that dst is a valid either a valid alias or a valid .wants/.requires symlink for the target
         * unit *i. Return negative on error or if not compatible, zero on success.
         *
         * ret_dst is set in cases where "instance propagation" happens, i.e. when the instance part is
         * inserted into dst. It is not normally set, even on success, so that the caller can easily
         * distinguish the case where instance propagation occurred.
         *
         * Returns:
         * -EXDEV when the alias doesn't match the unit,
         * -EUCLEAN when the name is invalid,
         * -ELOOP when the alias it to the unit itself.
         */
 
        const char *path_alias = strrchr(dst, '/');
        if (path_alias) {
                /* This branch covers legacy Alias= function of creating .wants and .requires symlinks. */
                _cleanup_free_ char *dir = NULL;
                char *p;
 
                path_alias++; /* skip over slash */
 
                r = path_extract_directory(dst, &dir);
                if (r < 0)
                        return log_error_errno(r, "Failed to extract parent directory from '%s': %m", dst);
 
                p = endswith(dir, ".wants");
                if (!p)
                        p = endswith(dir, ".requires");
                if (!p) {
                        r = install_changes_add(changes, n_changes, -EXDEV, dst, NULL);
                        if (r != -EXDEV)
                                return r;
 
                        return log_debug_errno(SYNTHETIC_ERRNO(EXDEV), "Invalid path \"%s\" in alias.", dir);
                }
 
                *p = '\0'; /* dir should now be a unit name */
 
                UnitNameFlags type = unit_name_classify(dir);
                if (type < 0) {
                        r = install_changes_add(changes, n_changes, -EXDEV, dst, NULL);
                        if (r != -EXDEV)
                                return r;
                        return log_debug_errno(SYNTHETIC_ERRNO(EXDEV),
                                               "Invalid unit name component \"%s\" in alias.", dir);
                }
 
                const bool instance_propagation = type == UNIT_NAME_TEMPLATE;
 
                /* That's the name we want to use for verification. */
                r = unit_symlink_name_compatible(path_alias, info->name, instance_propagation);
                if (r < 0)
                        return log_error_errno(r, "Failed to verify alias validity: %m");
                if (r == 0) {
                        r = install_changes_add(changes, n_changes, -EXDEV, dst, info->name);
                        if (r != -EXDEV)
                                return r;
 
                        return log_debug_errno(SYNTHETIC_ERRNO(EXDEV),
                                               "Invalid unit \"%s\" symlink \"%s\".",
                                               info->name, dst);
                }
 
        } else {
                /* If the symlink target has an instance set and the symlink source doesn't, we "propagate
                 * the instance", i.e. instantiate the symlink source with the target instance. */
                if (unit_name_is_valid(dst, UNIT_NAME_TEMPLATE)) {
                        _cleanup_free_ char *inst = NULL;
 
                        UnitNameFlags type = unit_name_to_instance(info->name, &inst);
                        if (type < 0) {
                                r = install_changes_add(changes, n_changes, -EUCLEAN, info->name, NULL);
                                if (r != -EUCLEAN)
                                        return r;
                                return log_debug_errno(type, "Failed to extract instance name from \"%s\": %m", info->name);
                        }
 
                        if (type == UNIT_NAME_INSTANCE) {
                                r = unit_name_replace_instance(dst, inst, &dst_updated);
                                if (r < 0)
                                        return log_error_errno(r, "Failed to build unit name from %s+%s: %m",
                                                               dst, inst);
                        }
                }
 
                r = unit_validate_alias_symlink_or_warn(LOG_DEBUG, dst_updated ?: dst, info->name);
                if (r == -ELOOP)  /* -ELOOP means self-alias, which we (quietly) ignore */
                        return r;
                if (r < 0)
                        return install_changes_add(changes, n_changes,
                                                   r == -EINVAL ? -EXDEV : r,
                                                   dst_updated ?: dst,
                                                   info->name);
        }
 
        *ret_dst = TAKE_PTR(dst_updated);
        return 0;
}
 
static int install_info_symlink_alias(
                RuntimeScope scope,
                UnitFileFlags file_flags,
                InstallInfo *info,
                const LookupPaths *lp,
                const char *config_path,
                bool force,
                InstallChange **changes,
                size_t *n_changes) {
 
        int r, ret = 0;
 
        assert(info);
        assert(lp);
        assert(config_path);
 
        STRV_FOREACH(s, info->aliases) {
                _cleanup_free_ char *alias_path = NULL, *alias_target = NULL, *dst = NULL, *dst_updated = NULL;
 
                r = install_name_printf(scope, info, *s, &dst);
                if (r < 0) {
                        RET_GATHER(ret, install_changes_add(changes, n_changes, r, *s, NULL));
                        continue;
                }
 
                r = unit_file_verify_alias(info, dst, &dst_updated, changes, n_changes);
                if (r < 0) {
                        if (r != -ELOOP)
                                RET_GATHER(ret, r);
                        continue;
                }
 
                alias_path = path_make_absolute(dst_updated ?: dst, config_path);