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 1–1000 of 3,921

/* SPDX-License-Identifier: LGPL-2.1-or-later */
 
#include <fcntl.h>
#include <fnmatch.h>
#include <stdio.h>
#include <unistd.h>
 
#include "alloc-util.h"
#include "bus-common-errors.h"
#include "chase.h"
#include "conf-files.h"
#include "conf-parser.h"
#include "constants.h"
#include "dirent-util.h"
#include "errno-util.h"
#include "extract-word.h"
#include "fd-util.h"
#include "fileio.h"
#include "fs-util.h"
#include "glyph-util.h"
#include "hashmap.h"
#include "install.h"
#include "install-printf.h"
#include "log.h"
#include "mkdir.h"
#include "path-lookup.h"
#include "path-util.h"
#include "rm-rf.h"
#include "set.h"
#include "special.h"
#include "stat-util.h"
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
#include "unit-file.h"
#include "unit-name.h"
 
#define UNIT_FILE_FOLLOW_SYMLINK_MAX 64
 
typedef struct {
        RuntimeScope scope;
        OrderedHashmap *will_process;
        OrderedHashmap *have_processed;
} InstallContext;
 
struct UnitFilePresetRule {
        char *pattern;
        PresetAction action;
        char **instances;
};
 
/* NB! strings use past tense. */
static const char *const preset_action_past_tense_table[_PRESET_ACTION_MAX] = {
        [PRESET_UNKNOWN] = "unknown",
        [PRESET_ENABLE]  = "enabled",
        [PRESET_DISABLE] = "disabled",
        [PRESET_IGNORE]  = "ignored",
};
 
DEFINE_STRING_TABLE_LOOKUP_TO_STRING(preset_action_past_tense, PresetAction);
 
static bool install_info_has_rules(const InstallInfo *i) {
        assert(i);
 
        return !strv_isempty(i->aliases) ||
               !strv_isempty(i->wanted_by) ||
               !strv_isempty(i->required_by) ||
               !strv_isempty(i->upheld_by);
}
 
static bool install_info_has_also(const InstallInfo *i) {
        assert(i);
 
        return !strv_isempty(i->also);
}
 
static void unit_file_preset_rule_done(UnitFilePresetRule *rule) {
        assert(rule);
 
        free(rule->pattern);
        strv_free(rule->instances);
}
 
void unit_file_presets_done(UnitFilePresets *p) {
        if (!p)
                return;
 
        FOREACH_ARRAY(rule, p->rules, p->n_rules)
                unit_file_preset_rule_done(rule);
 
        free(p->rules);
        p->n_rules = 0;
}
 
static const char *const install_mode_table[_INSTALL_MODE_MAX] = {
        [INSTALL_MODE_REGULAR] = "regular",
        [INSTALL_MODE_LINKED]  = "linked",
        [INSTALL_MODE_ALIAS]   = "alias",
        [INSTALL_MODE_MASKED]  = "masked",
};
 
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(install_mode, InstallMode);
 
static int in_search_path(const LookupPaths *lp, const char *path) {
        _cleanup_free_ char *parent = NULL;
        int r;
 
        /* Check if 'path' is in lp->search_path. */
 
        assert(lp);
        assert(path);
 
        r = path_extract_directory(path, &parent);
        if (r < 0)
                return r;
 
        return path_strv_contains(lp->search_path, parent);
}
 
static bool underneath_search_path(const LookupPaths *lp, const char *path) {
        /* Check if 'path' is underneath lp->search_path. */
 
        assert(lp);
        assert(path);
 
        return path_startswith_strv(path, lp->search_path);
}
 
static const char* skip_root(const char *root_dir, const char *path) {
        assert(path);
 
        if (!root_dir)
                return path;
 
        const char *e = path_startswith(path, root_dir);
        if (!e)
                return NULL;
 
        /* Make sure the returned path starts with a slash */
        if (e[0] != '/') {
                if (e == path || e[-1] != '/')
                        return NULL;
 
                e--;
        }
 
        return e;
}
 
static int path_is_generator(const LookupPaths *lp, const char *path) {
        _cleanup_free_ char *parent = NULL;
        int r;
 
        assert(lp);
        assert(path);
 
        r = path_extract_directory(path, &parent);
        if (r < 0)
                return r;
 
        return PATH_IN_SET(parent,
                           lp->generator,
                           lp->generator_early,
                           lp->generator_late);
}
 
static int path_is_transient(const LookupPaths *lp, const char *path) {
        _cleanup_free_ char *parent = NULL;
        int r;
 
        assert(lp);
        assert(path);
 
        r = path_extract_directory(path, &parent);
        if (r < 0)
                return r;
 
        return path_equal(parent, lp->transient);
}
 
static int path_is_control(const LookupPaths *lp, const char *path) {
        _cleanup_free_ char *parent = NULL;
        int r;
 
        assert(lp);
        assert(path);
 
        r = path_extract_directory(path, &parent);
        if (r < 0)
                return r;
 
        return PATH_IN_SET(parent,
                           lp->persistent_control,
                           lp->runtime_control);
}
 
static int path_is_config(const LookupPaths *lp, const char *path, bool check_parent) {
        _cleanup_free_ char *parent = NULL;
        int r;
 
        assert(lp);
        assert(path);
 
        /* Note that we do *not* have generic checks for /etc or /run in place, since with
         * them we couldn't discern configuration from transient or generated units */
 
        if (check_parent) {
                r = path_extract_directory(path, &parent);
                if (r < 0)
                        return r;
 
                path = parent;
        }
 
        return PATH_IN_SET(path,
                           lp->persistent_config,
                           lp->runtime_config);
}
 
static int path_is_runtime(const LookupPaths *lp, const char *path, bool check_parent) {
        _cleanup_free_ char *parent = NULL;
        const char *rpath;
        int r;
 
        assert(lp);
        assert(path);
 
        /* Everything in /run is considered runtime. On top of that we also add
         * explicit checks for the various runtime directories, as safety net. */
 
        rpath = skip_root(lp->root_dir, path);
        if (rpath && path_startswith(rpath, "/run"))
                return true;
 
        if (check_parent) {
                r = path_extract_directory(path, &parent);
                if (r < 0)
                        return r;
 
                path = parent;
        }
 
        return PATH_IN_SET(path,
                           lp->runtime_config,
                           lp->generator,
                           lp->generator_early,
                           lp->generator_late,
                           lp->transient,
                           lp->runtime_control);
}
 
static int path_is_vendor_or_generator(const LookupPaths *lp, const char *path) {
        const char *rpath;
        int r;
 
        assert(lp);
        assert(path);
 
        rpath = skip_root(lp->root_dir, path);
        if (!rpath)
                return 0;
 
        if (path_startswith(rpath, "/usr"))
                return true;
 
        /* The lookup paths are root prefixed, so we have to pass in a root prefixed path. */
        r = path_is_generator(lp, path);
        if (r != 0)
                return r;
 
        return path_equal(rpath, SYSTEM_DATA_UNIT_DIR);
}
 
static const char* config_path_from_flags(const LookupPaths *lp, UnitFileFlags flags) {
        assert(lp);
 
        if (FLAGS_SET(flags, UNIT_FILE_PORTABLE))
                return FLAGS_SET(flags, UNIT_FILE_RUNTIME) ? lp->runtime_attached : lp->persistent_attached;
        else
                return FLAGS_SET(flags, UNIT_FILE_RUNTIME) ? lp->runtime_config : lp->persistent_config;
}
 
InstallChangeType install_changes_add(
                InstallChange **changes,
                size_t *n_changes,
                InstallChangeType type, /* INSTALL_CHANGE_SYMLINK, _UNLINK, _IS_MASKED, _IS_DANGLING, … if positive or errno if negative */
                const char *path,
                const char *source) {
 
        _cleanup_free_ char *p = NULL, *s = NULL;
        int r;
 
        assert(!changes == !n_changes);
        assert(INSTALL_CHANGE_TYPE_VALID(type));
 
        /* Message formatting requires <path> to be set. */
        assert(path);
 
        /* Register a change or error. Note that the return value may be the error
         * that was passed in, or -ENOMEM generated internally. */
 
        if (!changes)
                return type;
 
        if (!GREEDY_REALLOC(*changes, *n_changes + 1))
                return -ENOMEM;
 
        r = path_simplify_alloc(path, &p);
        if (r < 0)
                return r;
 
        r = path_simplify_alloc(source, &s);
        if (r < 0)
                return r;
 
        (*changes)[(*n_changes)++] = (InstallChange) {
                .type = type,
                .path = TAKE_PTR(p),
                .source = TAKE_PTR(s),
        };
 
        return type;
}
 
static void install_change_done(InstallChange *change) {
        assert(change);
 
        change->path = mfree(change->path);
        change->source = mfree(change->source);
}
 
DEFINE_ARRAY_FREE_FUNC(install_changes_free, InstallChange, install_change_done);
 
static void install_change_dump_success(const InstallChange *change) {
        assert(change);
        assert(change->path);
 
        switch (change->type) {
 
        case INSTALL_CHANGE_SYMLINK:
                return log_info("Created symlink '%s' %s '%s'.",
                                change->path, glyph(GLYPH_ARROW_RIGHT), change->source);
 
        case INSTALL_CHANGE_UNLINK:
                return log_info("Removed '%s'.", change->path);
 
        case INSTALL_CHANGE_IS_MASKED:
                return log_info("Unit %s is masked, ignoring.", change->path);
 
        case INSTALL_CHANGE_IS_MASKED_GENERATOR:
                return log_info("Unit %s is masked via a generator and cannot be unmasked, skipping.", change->path);
 
        case INSTALL_CHANGE_IS_DANGLING:
                return log_info("Unit %s is an alias to a non-existent unit, ignoring.", change->path);
 
        case INSTALL_CHANGE_DESTINATION_NOT_PRESENT:
                return log_warning("Unit %s is added as a dependency to a non-existent unit %s.",
                                   change->source, change->path);
 
        case INSTALL_CHANGE_AUXILIARY_FAILED:
                return log_warning("Failed to enable auxiliary unit %s, ignoring.", change->path);
 
        default:
                assert_not_reached();
        }
}
 
/* Generated/transient/missing/invalid units when applying presets.
 * Coordinate with install_change_dump_error() below. */
static bool ERRNO_IS_NEG_UNIT_ISSUE(intmax_t r) {
        return IN_SET(r,
                      -EEXIST,
                      -ERFKILL,
                      -EADDRNOTAVAIL,
                      -ETXTBSY,
                      -EBADSLT,
                      -EIDRM,
                      -EUCLEAN,
                      -ELOOP,
                      -EXDEV,
                      -ENOENT,
                      -ENOLINK,
                      -EUNATCH);
}
 
int install_change_dump_error(const InstallChange *change, char **ret_errmsg, const char **ret_bus_error) {
        char *m;
        const char *bus_error;
 
        /* Returns 0:   known error and ret_errmsg formatted
         *         < 0: non-recognizable error */
 
        assert(change);
        assert(change->path);
        assert(change->type < 0);
        assert(ret_errmsg);
 
        switch (change->type) {
 
        case -EEXIST:
                m = strjoin("File '", change->path, "' already exists",
                            change->source ? " and is a symlink to " : NULL, change->source);
                bus_error = BUS_ERROR_UNIT_EXISTS;
                break;
 
        case -ERFKILL:
                m = strjoin("Unit ", change->path, " is masked");
                bus_error = BUS_ERROR_UNIT_MASKED;
                break;
 
        case -EADDRNOTAVAIL:
                m = strjoin("Unit ", change->path, " is transient or generated");
                bus_error = BUS_ERROR_UNIT_GENERATED;
                break;
 
        case -ETXTBSY:
                m = strjoin("File '", change->path, "' is under the systemd unit hierarchy already");
                bus_error = BUS_ERROR_UNIT_BAD_PATH;
                break;
 
        case -EBADSLT:
                m = strjoin("Invalid specifier in unit ", change->path);
                bus_error = BUS_ERROR_BAD_UNIT_SETTING;
                break;
 
        case -EIDRM:
                m = strjoin("Refusing to operate on template unit ", change->source,
                            " when destination unit ", change->path, " is a non-template unit");
                bus_error = BUS_ERROR_BAD_UNIT_SETTING;
                break;
 
        case -EUCLEAN:
                m = strjoin("Invalid unit name ", change->path);
                bus_error = BUS_ERROR_BAD_UNIT_SETTING;
                break;
 
        case -ELOOP:
                m = strjoin("Refusing to operate on linked unit file ", change->path);
                bus_error = BUS_ERROR_UNIT_LINKED;
                break;
 
        case -EXDEV:
                if (change->source)
                        m = strjoin("Cannot alias ", change->source, " as ", change->path);
                else
                        m = strjoin("Invalid unit reference ", change->path);
                bus_error = BUS_ERROR_BAD_UNIT_SETTING;
                break;
 
        case -ENOENT:
                m = strjoin("Unit ", change->path, " does not exist");
                bus_error = BUS_ERROR_NO_SUCH_UNIT;
                break;
 
        case -ENOLINK:
                m = strjoin("Unit ", change->path, " is an unresolvable alias");
                bus_error = BUS_ERROR_NO_SUCH_UNIT;
                break;
 
        case -EUNATCH:
                m = strjoin("Cannot resolve specifiers in unit ", change->path);
                bus_error = BUS_ERROR_BAD_UNIT_SETTING;
                break;
 
        default:
                return change->type;
        }
        if (!m)
                return -ENOMEM;
 
        *ret_errmsg = m;
        if (ret_bus_error)
                *ret_bus_error = bus_error;
 
        return 0;
}
 
int install_changes_dump(
                int error,
                const char *verb,
                const InstallChange *changes,
                size_t n_changes,
                bool quiet) {
 
        bool err_logged = false;
        int r;
 
        /* If verb is not specified, errors are not allowed! */
        assert(verb || error >= 0);
        assert(changes || n_changes == 0);
 
        /* An error is returned if 'error' contains an error or if any of the changes failed. */
 
        FOREACH_ARRAY(i, changes, n_changes)
                if (i->type >= 0) {
                        if (!quiet)
                                install_change_dump_success(i);
                } else {
                        _cleanup_free_ char *err_message = NULL;
 
                        assert(verb);
 
                        r = install_change_dump_error(i, &err_message, /* ret_bus_error= */ NULL);
                        if (r == -ENOMEM)
                                return log_oom();
                        if (r < 0)
                                RET_GATHER(error,
                                           log_error_errno(r, "Failed to %s unit %s: %m", verb, i->path));
                        else
                                RET_GATHER(error,
                                           log_error_errno(i->type, "Failed to %s unit: %s", verb, err_message));
 
                        err_logged = true;
                }
 
        if (error < 0 && !err_logged)
                log_error_errno(error, "Failed to %s units: %m.", verb);
 
        return error;
}
 
/**
 * Checks if two symlink targets (starting from src) are equivalent as far as the unit enablement logic is
 * concerned. If the target is in the unit search path, then anything with the same name is equivalent.
 * If outside the unit search path, paths must be identical.
 */
static int chroot_unit_symlinks_equivalent(
                const LookupPaths *lp,
                const char *src,
                const char *target_a,
                const char *target_b) {
 
        assert(lp);
        assert(src);
        assert(target_a);
        assert(target_b);
 
        /* This will give incorrect results if the paths are relative and go outside
         * of the chroot. False negatives are possible. */
 
        const char *root = lp->root_dir ?: "/";
        _cleanup_free_ char *dirname = NULL;
        int r;
 
        if (!path_is_absolute(target_a) || !path_is_absolute(target_b)) {
                r = path_extract_directory(src, &dirname);
                if (r < 0)
                        return r;
        }
 
        _cleanup_free_ char *a = path_join(path_is_absolute(target_a) ? root : dirname, target_a);
        _cleanup_free_ char *b = path_join(path_is_absolute(target_b) ? root : dirname, target_b);
        if (!a || !b)
                return log_oom();
 
        r = path_equal_or_inode_same(a, b, 0);
        if (r != 0)
                return r;
 
        _cleanup_free_ char *a_name = NULL, *b_name = NULL;
        r = path_extract_filename(a, &a_name);
        if (r < 0)
                return r;
        r = path_extract_filename(b, &b_name);
        if (r < 0)
                return r;
 
        return streq(a_name, b_name) &&
               path_startswith_strv(a, lp->search_path) &&
               path_startswith_strv(b, lp->search_path);
}
 
static int create_symlink(
                const LookupPaths *lp,
                const char *old_path,
                const char *new_path,
                bool force,
                InstallChange **changes,
                size_t *n_changes) {
 
        _cleanup_free_ char *dest = NULL;
        const char *rp;
        int r;
 
        assert(old_path);
        assert(new_path);
 
        rp = skip_root(lp->root_dir, old_path);
        if (rp)
                old_path = rp;
 
        /* Actually create a symlink, and remember that we did. This function is
         * smart enough to check if there's already a valid symlink in place.
         *
         * Returns 1 if a symlink was created or already exists and points to the
         * right place, or negative on error.
         */
 
        (void) mkdir_parents_label(new_path, 0755);
 
        if (symlink(old_path, new_path) >= 0) {
                r = install_changes_add(changes, n_changes, INSTALL_CHANGE_SYMLINK, new_path, old_path);
                if (r < 0)
                        return r;
                return 1;
        }
 
        if (errno != EEXIST)
                return install_changes_add(changes, n_changes, -errno, new_path, NULL);
 
        r = readlink_malloc(new_path, &dest);
        if (r < 0) {
                /* translate EINVAL (non-symlink exists) to EEXIST */
                if (r == -EINVAL)
                        r = -EEXIST;
 
                return install_changes_add(changes, n_changes, r, new_path, NULL);
        }
 
        if (chroot_unit_symlinks_equivalent(lp, new_path, dest, old_path)) {
                log_debug("Symlink %s %s %s already exists",
                          new_path, glyph(GLYPH_ARROW_RIGHT), dest);
                return 1;
        }
 
        if (!force)
                return install_changes_add(changes, n_changes, -EEXIST, new_path, dest);
 
        r = symlink_atomic(old_path, new_path);
        if (r < 0)
                return install_changes_add(changes, n_changes, r, new_path, NULL);
 
        r = install_changes_add(changes, n_changes, INSTALL_CHANGE_UNLINK, new_path, NULL);
        if (r < 0)
                return r;
        r = install_changes_add(changes, n_changes, INSTALL_CHANGE_SYMLINK, new_path, old_path);
        if (r < 0)
                return r;
 
        return 1;
}
 
static int mark_symlink_for_removal(
                Set **remove_symlinks_to,
                const char *p) {
 
        char *n;
        int r;
 
        assert(remove_symlinks_to);
        assert(p);
 
        r = set_ensure_allocated(remove_symlinks_to, &path_hash_ops_free);
        if (r < 0)
                return r;
 
        r = path_simplify_alloc(p, &n);
        if (r < 0)
                return r;
 
        r = set_consume(*remove_symlinks_to, n);
        if (r == -EEXIST)
                return 0;
        if (r < 0)
                return r;
 
        return 1;
}
 
static int remove_marked_symlinks_fd(
                Set *remove_symlinks_to,
                int fd,
                const char *path,
                const char *config_path,
                const LookupPaths *lp,
                bool dry_run,
                bool *restart,
                InstallChange **changes,
                size_t *n_changes) {
 
        _cleanup_closedir_ DIR *d = NULL;
        int r, ret = 0;
 
        assert(remove_symlinks_to);
        assert(fd >= 0);
        assert(path);
        assert(config_path);
        assert(lp);
        assert(restart);
 
        d = fdopendir(fd);
        if (!d) {
                safe_close(fd);
                return -errno;
        }
 
        rewinddir(d);
 
        FOREACH_DIRENT(de, d, return -errno)
                if (de->d_type == DT_DIR) {
                        _cleanup_close_ int nfd = -EBADF;
                        _cleanup_free_ char *p = NULL;
 
                        nfd = RET_NERRNO(openat(fd, de->d_name, O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW));
                        if (nfd < 0) {
                                if (nfd != -ENOENT)
                                        RET_GATHER(ret, nfd);
                                continue;
                        }
 
                        p = path_make_absolute(de->d_name, path);
                        if (!p)
                                return -ENOMEM;
 
                        /* This will close nfd, regardless whether it succeeds or not */
                        RET_GATHER(ret, remove_marked_symlinks_fd(remove_symlinks_to,
                                                                  TAKE_FD(nfd), p,
                                                                  config_path, lp,
                                                                  dry_run,
                                                                  restart,
                                                                  changes, n_changes));
 
                } else if (de->d_type == DT_LNK) {
                        _cleanup_free_ char *p = NULL;
                        bool found;
 
                        if (!unit_name_is_valid(de->d_name, UNIT_NAME_ANY))
                                continue;
 
                        p = path_make_absolute(de->d_name, path);
                        if (!p)
                                return -ENOMEM;
                        path_simplify(p);
 
                        /* We remove all links pointing to a file or path that is marked, as well as all
                         * files sharing the same name as a file that is marked, and files sharing the same
                         * name after the instance has been removed. Do path chasing only if we don't already
                         * know that we want to remove the symlink. */
                        found = set_contains(remove_symlinks_to, de->d_name);
 
                        if (!found) {
                                _cleanup_free_ char *template = NULL;
 
                                r = unit_name_template(de->d_name, &template);
                                if (r < 0 && r != -EINVAL)
                                        return r;
                                if (r >= 0)
                                        found = set_contains(remove_symlinks_to, template);
                        }
 
                        if (!found) {
                                _cleanup_free_ char *dest = NULL, *dest_name = NULL;
 
                                r = chase(p, lp->root_dir, CHASE_NONEXISTENT, &dest, NULL);
                                if (r == -ENOENT)
                                        continue;
                                if (r < 0) {
                                        log_debug_errno(r, "Failed to resolve symlink \"%s\": %m", p);
                                        RET_GATHER(ret, install_changes_add(changes, n_changes, r, p, NULL));
                                        continue;
                                }
 
                                r = path_extract_filename(dest, &dest_name);
                                if (r < 0)
                                        return r;
 
                                found = set_contains(remove_symlinks_to, dest) ||
                                        set_contains(remove_symlinks_to, dest_name);
                        }
 
                        if (!found)
                                continue;
 
                        if (!dry_run) {
                                if (unlinkat(fd, de->d_name, 0) < 0 && errno != ENOENT) {
                                        RET_GATHER(ret, install_changes_add(changes, n_changes, -errno, p, NULL));
                                        continue;
                                }
 
                                (void) rmdir_parents(p, config_path);
                        }
 
                        r = install_changes_add(changes, n_changes, INSTALL_CHANGE_UNLINK, p, NULL);
                        if (r < 0)
                                return r;
 
                        /* Now, remember the full path (but with the root prefix removed) of
                         * the symlink we just removed, and remove any symlinks to it, too. */
 
                        const char *rp = skip_root(lp->root_dir, p);
                        r = mark_symlink_for_removal(&remove_symlinks_to, rp ?: p);
                        if (r < 0)
                                return r;
                        if (r > 0 && !dry_run)
                                *restart = true;
                }
 
        return ret;
}
 
static int remove_marked_symlinks(
                Set *remove_symlinks_to,
                const char *config_path,
                const LookupPaths *lp,
                bool dry_run,
                InstallChange **changes,
                size_t *n_changes) {
 
        _cleanup_close_ int fd = -EBADF;
        bool restart;
        int r = 0;
 
        assert(config_path);
        assert(lp);
 
        if (set_isempty(remove_symlinks_to))
                return 0;
 
        fd = open(config_path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC);
        if (fd < 0)
                return errno == ENOENT ? 0 : -errno;
 
        do {
                int cfd;
                restart = false;
 
                cfd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
                if (cfd < 0)
                        return -errno;
 
                /* This takes possession of cfd and closes it */
                RET_GATHER(r, remove_marked_symlinks_fd(remove_symlinks_to,
                                                        cfd, config_path,
                                                        config_path, lp,
                                                        dry_run,
                                                        &restart,
                                                        changes, n_changes));
        } while (restart);
 
        return r;
}
 
static int is_symlink_with_known_name(const InstallInfo *i, const char *name) {
        int r;
 
        assert(i);
        assert(name);
 
        if (streq(name, i->name))
                return true;
 
        if (strv_contains(i->aliases, name))
                return true;
 
        /* Look for template symlink matching DefaultInstance */
        if (i->default_instance && unit_name_is_valid(i->name, UNIT_NAME_TEMPLATE)) {
                _cleanup_free_ char *s = NULL;
 
                r = unit_name_replace_instance(i->name, i->default_instance, &s);
                if (r < 0) {
                        if (r != -EINVAL)
                                return r;
 
                } else if (streq(name, s))
                        return true;
        }
 
        return false;
}
 
static int find_symlinks_in_directory(
                DIR *dir,
                const char *dir_path,
                const char *root_dir,
                const InstallInfo *info,
                bool ignore_destination,
                bool match_name,
                bool ignore_same_name,
                const char *config_path,
                bool *same_name_link) {
 
        int r, ret = 0;
 
        assert(dir);
        assert(dir_path);
        assert(info);
        assert(unit_name_is_valid(info->name, UNIT_NAME_ANY));
        assert(config_path);
        assert(same_name_link);
 
        FOREACH_DIRENT(de, dir, return -errno) {
                bool found_path = false, found_dest = false, b = false;
 
                if (de->d_type != DT_LNK)
                        continue;
 
                if (!ignore_destination) {
                        _cleanup_free_ char *dest = NULL;
 
                        /* Acquire symlink destination */
                        r = readlinkat_malloc(dirfd(dir), de->d_name, &dest);
                        if (r < 0) {
                                if (r != -ENOENT)
                                        RET_GATHER(ret, r);
                                continue;
                        }
 
                        /* Check if what the symlink points to matches what we are looking for */
                        found_dest = path_equal_filename(dest, info->name);
                }
 
                /* Check if the symlink itself matches what we are looking for.
                 *
                 * If ignore_destination is specified, we only look at the source name.
                 *
                 * If ignore_same_name is specified, we are in one of the directories which
                 * have lower priority than the unit file, and even if a file or symlink with
                 * this name was found, we should ignore it. */
 
                if (ignore_destination || !ignore_same_name)
                        found_path = streq(de->d_name, info->name);
 
                if (!found_path && ignore_destination) {
                        _cleanup_free_ char *template = NULL;
 
                        r = unit_name_template(de->d_name, &template);
                        if (r < 0 && r != -EINVAL)
                                return r;
                        if (r >= 0)
                                found_dest = streq(template, info->name);
                }
 
                if (found_path && found_dest) {
                        _cleanup_free_ char *p = NULL, *t = NULL;
 
                        /* Filter out same name links in the main config path */
                        p = path_make_absolute(de->d_name, dir_path);
                        t = path_make_absolute(info->name, config_path);
                        if (!p || !t)
                                return -ENOMEM;
 
                        b = path_equal(p, t);
                }
 
                if (b)
                        *same_name_link = true;
                else if (found_path || found_dest) {
                        if (!match_name)
                                return 1;
 
                        /* Check if symlink name is in the set of names used by [Install] */
                        r = is_symlink_with_known_name(info, de->d_name);
                        if (r != 0)
                                return r;
                }
        }
 
        return ret;
}
 
static int find_symlinks(
                const char *root_dir,
                const InstallInfo *i,
                bool match_name,
                bool ignore_same_name,
                const char *config_path,
                bool *same_name_link) {
 
        _cleanup_closedir_ DIR *config_dir = NULL;
        int r;
 
        assert(i);
        assert(config_path);
        assert(same_name_link);
 
        config_dir = opendir(config_path);
        if (!config_dir) {
                if (IN_SET(errno, ENOENT, ENOTDIR, EACCES))
                        return 0;
                return -errno;
        }
 
        FOREACH_DIRENT(de, config_dir, return -errno) {
                const char *suffix;
                _cleanup_free_ const char *path = NULL;
                _cleanup_closedir_ DIR *d = NULL;
 
                if (de->d_type != DT_DIR)
                        continue;
 
                suffix = strrchr(de->d_name, '.');
                if (!STRPTR_IN_SET(suffix, ".wants", ".requires", ".upholds"))
                        continue;
 
                path = path_join(config_path, de->d_name);
                if (!path)
                        return -ENOMEM;
 
                d = opendir(path);
                if (!d) {