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 3001–3921 of 3,921

                if (r == O_DIRECTORY)
                        return log_debug_errno(SYNTHETIC_ERRNO(EISDIR),
                                               "Unexpected path to a directory \"%s\", refusing.", *a);
 
                if (!is_path(*a) && !unit_name_is_valid(*a, UNIT_NAME_INSTANCE)) {
                        r = install_info_discover(&ctx, lp, n, SEARCH_LOAD|SEARCH_FOLLOW_CONFIG_SYMLINKS, &i, NULL, NULL);
                        if (r < 0)
                                log_debug_errno(r, "Failed to discover unit \"%s\", operating on name: %m", n);
                }
 
                r = strv_consume(&names, TAKE_PTR(n));
                if (r < 0)
                        return r;
 
                const char *p = NULL;
                if (i && i->path && i->root)
                        /* Use startswith here, because we know that paths are normalized, and
                         * path_startswith() would give us a relative path, but we need an absolute path
                         * relative to i->root.
                         *
                         * In other words: /var/tmp/instroot.1234/etc/systemd/system/frobnicator.service
                         * is replaced by /etc/systemd/system/frobnicator.service, which is "absolute"
                         * in a sense, but only makes sense "relative" to /var/tmp/instroot.1234/.
                         */
                        p = startswith(i->path, i->root);
 
                r = strv_extend(&files, p ?: *a);
                if (r < 0)
                        return r;
        }
 
        *ret_names = TAKE_PTR(names);
        *ret_files = TAKE_PTR(files);
        return 0;
}
 
int unit_file_reenable(
                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 = {};
        _cleanup_strv_free_ char **names = NULL, **files = NULL;
        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;
 
        r = normalize_linked_files(scope, &lp, names_or_paths, &names, &files);
        if (r < 0)
                return r;
 
        /* First, we invoke the disable command with only the basename... */
        r = do_unit_file_disable(&lp, scope, flags, config_path, names, changes, n_changes);
        if (r < 0)
                return r;
 
        /* But the enable command with the full name */
        return do_unit_file_enable(&lp, scope, flags, config_path, files, changes, n_changes);
}
 
int unit_file_set_default(
                RuntimeScope scope,
                UnitFileFlags flags,
                const char *root_dir,
                const char *name,
                InstallChange **changes,
                size_t *n_changes) {
 
        _cleanup_(lookup_paths_done) LookupPaths lp = {};
        _cleanup_(install_context_done) InstallContext ctx = { .scope = scope };
        InstallInfo *info;
        const char *new_path;
        int r;
 
        assert(scope >= 0);
        assert(scope < _RUNTIME_SCOPE_MAX);
        assert(name);
 
        if (unit_name_to_type(name) != UNIT_TARGET) /* this also validates the name */
                return -EINVAL;
        if (streq(name, SPECIAL_DEFAULT_TARGET))
                return -EINVAL;
 
        r = lookup_paths_init(&lp, scope, 0, root_dir);
        if (r < 0)
                return r;
 
        r = install_info_discover_and_check(&ctx, &lp, name, 0, &info, changes, n_changes);
        if (r < 0)
                return r;
 
        new_path = strjoina(lp.persistent_config, "/" SPECIAL_DEFAULT_TARGET);
        return create_symlink(&lp, info->path, new_path, flags & UNIT_FILE_FORCE, changes, n_changes);
}
 
int unit_file_get_default(
                RuntimeScope scope,
                const char *root_dir,
                char **ret) {
 
        _cleanup_(lookup_paths_done) LookupPaths lp = {};
        _cleanup_(install_context_done) InstallContext ctx = { .scope = scope };
        InstallInfo *info;
        int r;
 
        assert(scope >= 0);
        assert(scope < _RUNTIME_SCOPE_MAX);
        assert(ret);
 
        r = lookup_paths_init(&lp, scope, 0, root_dir);
        if (r < 0)
                return r;
 
        r = install_info_discover(&ctx, &lp, SPECIAL_DEFAULT_TARGET, SEARCH_FOLLOW_CONFIG_SYMLINKS,
                                  &info, NULL, NULL);
        if (r < 0)
                return r;
 
        return strdup_to(ret, info->name);
}
 
int unit_file_lookup_state(
                RuntimeScope scope,
                const LookupPaths *lp,
                const char *name,
                UnitFileState *ret) {
 
        _cleanup_(install_context_done) InstallContext ctx = { .scope = scope };
        InstallInfo *info;
        UnitFileState state;
        int r;
 
        assert(lp);
        assert(name);
 
        if (!unit_name_is_valid(name, UNIT_NAME_ANY))
                return -EINVAL;
 
        r = install_info_discover(&ctx, lp, name, SEARCH_LOAD|SEARCH_FOLLOW_CONFIG_SYMLINKS,
                                  &info, NULL, NULL);
        if (r < 0)
                return log_debug_errno(r, "Failed to discover unit %s: %m", name);
 
        assert(IN_SET(info->install_mode, INSTALL_MODE_REGULAR, INSTALL_MODE_MASKED));
        log_debug("Found unit %s at %s (%s)", name, strna(info->path),
                  info->install_mode == INSTALL_MODE_REGULAR ? "regular file" : "mask");
 
        /* Shortcut things, if the caller just wants to know if this unit exists. */
        if (!ret)
                return 0;
 
        switch (info->install_mode) {
 
        case INSTALL_MODE_MASKED:
                r = path_is_runtime(lp, info->path, true);
                if (r < 0)
                        return r;
 
                state = r > 0 ? UNIT_FILE_MASKED_RUNTIME : UNIT_FILE_MASKED;
                break;
 
        case INSTALL_MODE_REGULAR:
                /* Check if the name we were querying is actually an alias */
                if (!path_equal_filename(name, info->path) && !unit_name_is_valid(info->name, UNIT_NAME_INSTANCE)) {
                        state = UNIT_FILE_ALIAS;
                        break;
                }
 
                r = path_is_generator(lp, info->path);
                if (r < 0)
                        return r;
                if (r > 0) {
                        state = UNIT_FILE_GENERATED;
                        break;
                }
 
                r = path_is_transient(lp, info->path);
                if (r < 0)
                        return r;
                if (r > 0) {
                        state = UNIT_FILE_TRANSIENT;
                        break;
                }
 
                /* Check if any of the Alias= symlinks have been created.
                 * We ignore other aliases, and only check those that would
                 * be created by systemctl enable for this unit. */
                r = find_symlinks_in_scope(scope, lp, info, true, &state);
                if (r < 0)
                        return r;
                if (r > 0)
                        break;
 
                /* Check if the file is known under other names. If it is,
                 * it might be in use. Report that as UNIT_FILE_INDIRECT. */
                r = find_symlinks_in_scope(scope, lp, info, false, &state);
                if (r < 0)
                        return r;
                if (r > 0)
                        state = UNIT_FILE_INDIRECT;
                else {
                        if (install_info_has_rules(info))
                                state = UNIT_FILE_DISABLED;
                        else if (install_info_has_also(info))
                                state = UNIT_FILE_INDIRECT;
                        else
                                state = UNIT_FILE_STATIC;
                }
 
                break;
 
        default:
                assert_not_reached();
        }
 
        *ret = state;
        return 0;
}
 
int unit_file_get_state(
                RuntimeScope scope,
                const char *root_dir,
                const char *name,
                UnitFileState *ret) {
 
        _cleanup_(lookup_paths_done) LookupPaths lp = {};
        int r;
 
        assert(scope >= 0);
        assert(scope < _RUNTIME_SCOPE_MAX);
        assert(name);
 
        r = lookup_paths_init(&lp, scope, 0, root_dir);
        if (r < 0)
                return r;
 
        return unit_file_lookup_state(scope, &lp, name, ret);
}
 
int unit_file_exists_full(
                RuntimeScope scope,
                const LookupPaths *lp,
                SearchFlags flags,
                const char *name,
                char **ret_path) {
 
        _cleanup_(install_context_done) InstallContext c = {
                .scope = scope,
        };
        int r;
 
        assert(lp);
        assert(name);
 
        if (!unit_name_is_valid(name, UNIT_NAME_ANY))
                return -EINVAL;
 
        InstallInfo *info = NULL;
        r = install_info_discover(
                        &c,
                        lp,
                        name,
                        flags,
                        ret_path ? &info : NULL,
                        /* changes= */ NULL,
                        /* n_changes= */ NULL);
        if (r == -ENOENT) {
                if (ret_path)
                        *ret_path = NULL;
                return 0;
        }
        if (r < 0)
                return r;
 
        if (ret_path) {
                assert(info);
 
                r = strdup_to(ret_path, info->path);
                if (r < 0)
                        return r;
        }
 
        return 1;
}
 
static int split_pattern_into_name_and_instances(const char *pattern, char **out_unit_name, char ***out_instances) {
        _cleanup_strv_free_ char **instances = NULL;
        _cleanup_free_ char *unit_name = NULL;
        int r;
 
        assert(pattern);
        assert(out_instances);
        assert(out_unit_name);
 
        r = extract_first_word(&pattern, &unit_name, NULL, EXTRACT_RETAIN_ESCAPE);
        if (r < 0)
                return r;
 
        /* We handle the instances logic when unit name is extracted */
        if (pattern) {
                /* We only create instances when a rule of templated unit
                 * is seen. A rule like enable foo@.service a b c will
                 * result in an array of (a, b, c) as instance names */
                if (!unit_name_is_valid(unit_name, UNIT_NAME_TEMPLATE))
                        return -EINVAL;
 
                instances = strv_split(pattern, WHITESPACE);
                if (!instances)
                        return -ENOMEM;
 
                *out_instances = TAKE_PTR(instances);
        }
 
        *out_unit_name = TAKE_PTR(unit_name);
 
        return 0;
}
 
static int presets_find_config(RuntimeScope scope, const char *root_dir, char ***ret) {
        static const char* const initrd_dirs[] = { CONF_PATHS("systemd/initrd-preset"), NULL };
        static const char* const system_dirs[] = { CONF_PATHS("systemd/system-preset"), NULL };
        static const char* const user_dirs[] = { CONF_PATHS("systemd/user-preset"), NULL };
        const char* const* dirs;
        int r;
 
        assert(scope >= 0);
        assert(scope < _RUNTIME_SCOPE_MAX);
 
        if (scope == RUNTIME_SCOPE_SYSTEM) {
                r = chase_and_access("/etc/initrd-release", root_dir, CHASE_PREFIX_ROOT, F_OK, /* ret_path= */ NULL);
                if (r < 0 && r != -ENOENT)
                        return r;
 
                /* Make sure that we fall back to the system preset directories if we're operating on a root
                 * directory without initrd preset directories. This makes sure that we don't regress when
                 * using a newer systemctl to operate on a root directory with an older version of systemd
                 * installed that doesn't yet known about initrd preset directories. */
                if (r >= 0)
                        STRV_FOREACH(d, initrd_dirs) {
                                r = chase_and_access(*d, root_dir, CHASE_PREFIX_ROOT, F_OK, /* ret_path= */ NULL);
                                if (r >= 0)
                                        return conf_files_list_strv(ret, ".preset", root_dir, 0, initrd_dirs);
                                if (r != -ENOENT)
                                        return r;
                        }
 
                dirs = system_dirs;
        } else if (IN_SET(scope, RUNTIME_SCOPE_GLOBAL, RUNTIME_SCOPE_USER))
                dirs = user_dirs;
        else
                assert_not_reached();
 
        return conf_files_list_strv(ret, ".preset", root_dir, 0, dirs);
}
 
static int read_presets(RuntimeScope scope, const char *root_dir, UnitFilePresets *presets) {
        _cleanup_(unit_file_presets_done) UnitFilePresets ps = {};
        _cleanup_strv_free_ char **files = NULL;
        int r;
 
        assert(scope >= 0);
        assert(scope < _RUNTIME_SCOPE_MAX);
        assert(presets);
 
        r = presets_find_config(scope, root_dir, &files);
        if (r < 0)
                return r;
 
        STRV_FOREACH(p, files) {
                _cleanup_fclose_ FILE *f = NULL;
                int n = 0;
 
                f = fopen(*p, "re");
                if (!f) {
                        if (errno == ENOENT)
                                continue;
 
                        return -errno;
                }
 
                for (;;) {
                        _cleanup_free_ char *line = NULL;
                        _cleanup_(unit_file_preset_rule_done) UnitFilePresetRule rule = {};
                        const char *parameter;
 
                        r = read_stripped_line(f, LONG_LINE_MAX, &line);
                        if (r < 0)
                                return r;
                        if (r == 0)
                                break;
 
                        n++;
 
                        if (isempty(line))
                                continue;
                        if (strchr(COMMENTS, line[0]))
                                continue;
 
                        if ((parameter = first_word(line, "enable"))) {
                                char *unit_name;
                                char **instances = NULL;
 
                                /* Unit_name will remain the same as parameter when no instances are specified */
                                r = split_pattern_into_name_and_instances(parameter, &unit_name, &instances);
                                if (r < 0) {
                                        log_syntax(NULL, LOG_WARNING, *p, n, r, "Couldn't parse line '%s'. Ignoring.", line);
                                        continue;
                                }
 
                                rule = (UnitFilePresetRule) {
                                        .pattern = unit_name,
                                        .action = PRESET_ENABLE,
                                        .instances = instances,
                                };
 
                        } else if ((parameter = first_word(line, "disable"))) {
                                char *pattern;
 
                                pattern = strdup(parameter);
                                if (!pattern)
                                        return -ENOMEM;
 
                                rule = (UnitFilePresetRule) {
                                        .pattern = pattern,
                                        .action = PRESET_DISABLE,
                                };
 
                        } else if ((parameter = first_word(line, "ignore"))) {
                                char *pattern;
 
                                pattern = strdup(parameter);
                                if (!pattern)
                                        return -ENOMEM;
 
                                rule = (UnitFilePresetRule) {
                                        .pattern = pattern,
                                        .action = PRESET_IGNORE,
                                };
                        }
 
                        if (rule.action != 0) {
                                if (!GREEDY_REALLOC(ps.rules, ps.n_rules + 1))
                                        return -ENOMEM;
 
                                ps.rules[ps.n_rules++] = TAKE_STRUCT(rule);
                                continue;
                        }
 
                        log_syntax(NULL, LOG_WARNING, *p, n, 0, "Couldn't parse line '%s'. Ignoring.", line);
                }
        }
 
        ps.initialized = true;
        *presets = TAKE_STRUCT(ps);
 
        return 0;
}
 
static int pattern_match_multiple_instances(
                        const UnitFilePresetRule rule,
                        const char *unit_name,
                        char ***ret) {
 
        _cleanup_free_ char *templated_name = NULL;
        int r;
 
        assert(unit_name);
 
        /* If no ret is needed or the rule itself does not have instances
         * initialized, we return not matching */
        if (!ret || !rule.instances)
                return 0;
 
        r = unit_name_template(unit_name, &templated_name);
        if (r < 0)
                return r;
        if (!streq(rule.pattern, templated_name))
                return 0;
 
        /* Compose a list of specified instances when unit name is a template  */
        if (unit_name_is_valid(unit_name, UNIT_NAME_TEMPLATE)) {
                _cleanup_strv_free_ char **out_strv = NULL;
 
                STRV_FOREACH(iter, rule.instances) {
                        _cleanup_free_ char *name = NULL;
 
                        r = unit_name_replace_instance(unit_name, *iter, &name);
                        if (r < 0)
                                return r;
 
                        r = strv_consume(&out_strv, TAKE_PTR(name));
                        if (r < 0)
                                return r;
                }
 
                *ret = TAKE_PTR(out_strv);
                return 1;
        } else {
                /* We now know the input unit name is an instance name */
                _cleanup_free_ char *instance_name = NULL;
 
                r = unit_name_to_instance(unit_name, &instance_name);
                if (r < 0)
                        return r;
 
                if (strv_contains(rule.instances, instance_name))
                        return 1;
        }
        return 0;
}
 
static int query_presets(const char *name, const UnitFilePresets *presets, char ***instance_name_list) {
        PresetAction action = PRESET_UNKNOWN;
 
        assert(name);
        assert(presets);
        POINTER_MAY_BE_NULL(instance_name_list);
 
        if (!unit_name_is_valid(name, UNIT_NAME_ANY))
                return -EINVAL;
 
        FOREACH_ARRAY(i, presets->rules, presets->n_rules)
                if (pattern_match_multiple_instances(*i, name, instance_name_list) > 0 ||
                    fnmatch(i->pattern, name, FNM_NOESCAPE) == 0) {
                        action = i->action;
                        break;
                }
 
        switch (action) {
 
        case PRESET_UNKNOWN:
                log_debug("Preset files don't specify rule for %s. Enabling.", name);
                return PRESET_ENABLE;
 
        case PRESET_ENABLE:
                if (instance_name_list && *instance_name_list)
                        STRV_FOREACH(s, *instance_name_list)
                                log_debug("Preset files say enable %s.", *s);
                else
                        log_debug("Preset files say enable %s.", name);
                return PRESET_ENABLE;
 
        case PRESET_DISABLE:
                log_debug("Preset files say disable %s.", name);
                return PRESET_DISABLE;
 
        case PRESET_IGNORE:
                log_debug("Preset files say ignore %s.", name);
                return PRESET_IGNORE;
 
        default:
                assert_not_reached();
        }
}
 
PresetAction unit_file_query_preset(RuntimeScope scope, const char *root_dir, const char *name, UnitFilePresets *cached) {
        _cleanup_(unit_file_presets_done) UnitFilePresets tmp = {};
        int r;
 
        if (!cached)
                cached = &tmp;
        if (!cached->initialized) {
                r = read_presets(scope, root_dir, cached);
                if (r < 0)
                        return r;
        }
 
        return query_presets(name, cached, NULL);
}
 
static int execute_preset(
                UnitFileFlags file_flags,
                InstallContext *plus,
                InstallContext *minus,
                const LookupPaths *lp,
                const char *config_path,
                char * const *files,
                UnitFilePresetMode mode,
                InstallChange **changes,
                size_t *n_changes) {
 
        int r;
 
        assert(plus);
        assert(minus);
        assert(lp);
        assert(config_path);
 
        if (mode != UNIT_FILE_PRESET_ENABLE_ONLY) {
                _cleanup_set_free_ Set *remove_symlinks_to = NULL;
 
                r = install_context_mark_for_removal(minus, lp, &remove_symlinks_to, config_path, changes, n_changes);
                if (r < 0)
                        return r;
 
                r = remove_marked_symlinks(remove_symlinks_to, config_path, lp, false, changes, n_changes);
        } else
                r = 0;
 
        if (mode != UNIT_FILE_PRESET_DISABLE_ONLY) {
                int q;
 
                /* Returns number of symlinks that where supposed to be installed. */
                q = install_context_apply(plus, lp,
                                          file_flags | UNIT_FILE_IGNORE_AUXILIARY_FAILURE,
                                          config_path,
                                          SEARCH_LOAD, changes, n_changes);
                if (r >= 0) {
                        if (q < 0)
                                r = q;
                        else
                                r += q;
                }
        }
 
        return r;
}
 
static int preset_prepare_one(
                RuntimeScope scope,
                InstallContext *plus,
                InstallContext *minus,
                LookupPaths *lp,
                const char *name,
                const UnitFilePresets *presets,
                InstallChange **changes,
                size_t *n_changes) {
 
        _cleanup_(install_context_done) InstallContext tmp = { .scope = scope };
        _cleanup_strv_free_ char **instance_name_list = NULL;
        InstallInfo *info;
        int r;
 
        if (install_info_find(plus, name) || install_info_find(minus, name))
                return 0;
 
        r = install_info_discover(&tmp, lp, name, SEARCH_FOLLOW_CONFIG_SYMLINKS,
                                  &info, changes, n_changes);
        if (r < 0)
                return r;
 
        if (!streq(name, info->name)) {
                log_debug("Skipping %s because it is an alias for %s.", name, info->name);
                return 0;
        }
 
        r = query_presets(name, presets, &instance_name_list);
        if (r < 0)
                return r;
 
        if (r == PRESET_ENABLE) {
                if (instance_name_list)
                        STRV_FOREACH(s, instance_name_list) {
                                r = install_info_discover_and_check(plus, lp, *s, SEARCH_LOAD|SEARCH_FOLLOW_CONFIG_SYMLINKS,
                                                                    &info, changes, n_changes);
                                if (r < 0)
                                        return r;
                        }
                else {
                        r = install_info_discover_and_check(plus, lp, name, SEARCH_LOAD|SEARCH_FOLLOW_CONFIG_SYMLINKS,
                                                            &info, changes, n_changes);
                        if (r < 0)
                                return r;
                }
 
        } else if (r == PRESET_DISABLE)
                r = install_info_discover(minus, lp, name, SEARCH_FOLLOW_CONFIG_SYMLINKS,
                                          &info, changes, n_changes);
 
        return r;
}
 
int unit_file_preset(
                RuntimeScope scope,
                UnitFileFlags file_flags,
                const char *root_dir,
                char * const *names,
                UnitFilePresetMode mode,
                InstallChange **changes,
                size_t *n_changes) {
 
        _cleanup_(install_context_done) InstallContext plus = { .scope = scope }, minus = { .scope = scope };
        _cleanup_(lookup_paths_done) LookupPaths lp = {};
        _cleanup_(unit_file_presets_done) UnitFilePresets presets = {};
        const char *config_path;
        int r;
 
        assert(scope >= 0);
        assert(scope < _RUNTIME_SCOPE_MAX);
        assert(mode < _UNIT_FILE_PRESET_MODE_MAX);
 
        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 = read_presets(scope, root_dir, &presets);
        if (r < 0)
                return r;
 
        STRV_FOREACH(name, names) {
                r = preset_prepare_one(scope, &plus, &minus, &lp, *name, &presets, changes, n_changes);
                if (r < 0 && !ERRNO_IS_NEG_UNIT_ISSUE(r))
                        return r;
        }
 
        return execute_preset(file_flags, &plus, &minus, &lp, config_path, names, mode, changes, n_changes);
}
 
int unit_file_preset_all(
                RuntimeScope scope,
                UnitFileFlags file_flags,
                const char *root_dir,
                UnitFilePresetMode mode,
                InstallChange **changes,
                size_t *n_changes) {
 
        _cleanup_(install_context_done) InstallContext plus = { .scope = scope }, minus = { .scope = scope };
        _cleanup_(lookup_paths_done) LookupPaths lp = {};
        _cleanup_(unit_file_presets_done) UnitFilePresets presets = {};
        const char *config_path = NULL;
        int r;
 
        assert(scope >= 0);
        assert(scope < _RUNTIME_SCOPE_MAX);
        assert(mode < _UNIT_FILE_PRESET_MODE_MAX);
 
        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 = read_presets(scope, root_dir, &presets);
        if (r < 0)
                return r;
 
        STRV_FOREACH(i, lp.search_path) {
                _cleanup_closedir_ DIR *d = NULL;
 
                d = opendir(*i);
                if (!d) {
                        if (errno == ENOENT)
                                continue;
 
                        return log_debug_errno(errno, "Failed to opendir %s: %m", *i);
                }
 
                FOREACH_DIRENT(de, d,
                               return log_debug_errno(errno, "Failed to read directory %s: %m", *i)) {
 
                        if (!unit_name_is_valid(de->d_name, UNIT_NAME_ANY))
                                continue;
 
                        if (!IN_SET(de->d_type, DT_LNK, DT_REG))
                                continue;
 
                        r = preset_prepare_one(scope, &plus, &minus, &lp, de->d_name, &presets, changes, n_changes);
                        if (r < 0 && !ERRNO_IS_NEG_UNIT_ISSUE(r))
                                return r;
                }
        }
 
        return execute_preset(file_flags, &plus, &minus, &lp, config_path, NULL, mode, changes, n_changes);
}
 
static UnitFileList* unit_file_list_free(UnitFileList *f) {
        if (!f)
                return NULL;
 
        free(f->path);
        return mfree(f);
}
 
DEFINE_TRIVIAL_CLEANUP_FUNC(UnitFileList*, unit_file_list_free);
 
DEFINE_PRIVATE_HASH_OPS_FULL(unit_file_list_hash_ops_free_free,
                             char, string_hash_func, string_compare_func, free,
                             UnitFileList, unit_file_list_free);
 
int unit_file_get_list(
                RuntimeScope scope,
                const char *root_dir,
                char * const *states,
                char * const *patterns,
                Hashmap **ret) {
 
        _cleanup_(lookup_paths_done) LookupPaths lp = {};
        _cleanup_hashmap_free_ Hashmap *h = NULL;
        int r;
 
        assert(scope >= 0);
        assert(scope < _RUNTIME_SCOPE_MAX);
        assert(ret);
 
        r = lookup_paths_init(&lp, scope, 0, root_dir);
        if (r < 0)
                return r;
 
        STRV_FOREACH(dirname, lp.search_path) {
                _cleanup_closedir_ DIR *d = NULL;
 
                d = opendir(*dirname);
                if (!d) {
                        if (errno == ENOENT)
                                continue;
                        if (IN_SET(errno, ENOTDIR, EACCES)) {
                                log_debug_errno(errno, "Failed to open \"%s\": %m", *dirname);
                                continue;
                        }
 
                        return -errno;
                }
 
                FOREACH_DIRENT(de, d, return -errno) {
                        if (!IN_SET(de->d_type, DT_LNK, DT_REG))
                                continue;
 
                        if (hashmap_contains(h, de->d_name))
                                continue;
 
                        if (!unit_name_is_valid(de->d_name, UNIT_NAME_ANY))
                                continue;
 
                        if (!strv_fnmatch_or_empty(patterns, de->d_name, FNM_NOESCAPE))
                                continue;
 
                        UnitFileState state;
 
                        r = unit_file_lookup_state(scope, &lp, de->d_name, &state);
                        if (r < 0)
                                state = UNIT_FILE_BAD;
 
                        if (!strv_isempty(states) &&
                            !strv_contains(states, unit_file_state_to_string(state)))
                                continue;
 
                        _cleanup_(unit_file_list_freep) UnitFileList *f = new(UnitFileList, 1);
                        if (!f)
                                return -ENOMEM;
 
                        *f = (UnitFileList) {
                                .path = path_make_absolute(de->d_name, *dirname),
                                .state = state,
                        };
                        if (!f->path)
                                return -ENOMEM;
 
                        _cleanup_free_ char *unit_name = strdup(de->d_name);
                        if (!unit_name)
                                return -ENOMEM;
 
                        r = hashmap_ensure_put(&h, &unit_file_list_hash_ops_free_free, unit_name, f);
                        if (r < 0)
                                return r;
                        assert(r > 0);
 
                        TAKE_PTR(unit_name);
                        TAKE_PTR(f);
                }
        }
 
        *ret = TAKE_PTR(h);
        return 0;
}
 
static const char* const unit_file_state_table[_UNIT_FILE_STATE_MAX] = {
        [UNIT_FILE_ENABLED]         = "enabled",
        [UNIT_FILE_ENABLED_RUNTIME] = "enabled-runtime",
        [UNIT_FILE_LINKED]          = "linked",
        [UNIT_FILE_LINKED_RUNTIME]  = "linked-runtime",
        [UNIT_FILE_ALIAS]           = "alias",
        [UNIT_FILE_MASKED]          = "masked",
        [UNIT_FILE_MASKED_RUNTIME]  = "masked-runtime",
        [UNIT_FILE_STATIC]          = "static",
        [UNIT_FILE_DISABLED]        = "disabled",
        [UNIT_FILE_INDIRECT]        = "indirect",
        [UNIT_FILE_GENERATED]       = "generated",
        [UNIT_FILE_TRANSIENT]       = "transient",
        [UNIT_FILE_BAD]             = "bad",
};
 
DEFINE_STRING_TABLE_LOOKUP(unit_file_state, UnitFileState);
 
static const char* const install_change_type_table[_INSTALL_CHANGE_TYPE_MAX] = {
        [INSTALL_CHANGE_SYMLINK]                 = "symlink",
        [INSTALL_CHANGE_UNLINK]                  = "unlink",
        [INSTALL_CHANGE_IS_MASKED]               = "masked",
        [INSTALL_CHANGE_IS_MASKED_GENERATOR]     = "masked by generator",
        [INSTALL_CHANGE_IS_DANGLING]             = "dangling",
        [INSTALL_CHANGE_DESTINATION_NOT_PRESENT] = "destination not present",
        [INSTALL_CHANGE_AUXILIARY_FAILED]        = "auxiliary unit failed",
};
 
DEFINE_STRING_TABLE_LOOKUP(install_change_type, InstallChangeType);
 
static const char* const unit_file_preset_mode_table[_UNIT_FILE_PRESET_MODE_MAX] = {
        [UNIT_FILE_PRESET_FULL]         = "full",
        [UNIT_FILE_PRESET_ENABLE_ONLY]  = "enable-only",
        [UNIT_FILE_PRESET_DISABLE_ONLY] = "disable-only",
};
 
DEFINE_STRING_TABLE_LOOKUP(unit_file_preset_mode, UnitFilePresetMode);