1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty <of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
32 #include "path-util.h"
33 #include "path-lookup.h"
35 #include "unit-name.h"
37 #include "conf-parser.h"
38 #include "conf-files.h"
39 #include "specifier.h"
40 #include "install-printf.h"
44 OrderedHashmap *will_install;
45 OrderedHashmap *have_installed;
48 static int in_search_path(const char *path, char **search) {
49 _cleanup_free_ char *parent = NULL;
54 r = path_get_parent(path, &parent);
58 return strv_contains(search, parent);
61 static int lookup_paths_init_from_scope(LookupPaths *paths,
63 const char *root_dir) {
66 assert(scope < _UNIT_FILE_SCOPE_MAX);
70 return lookup_paths_init(paths,
71 scope == UNIT_FILE_SYSTEM ? SYSTEMD_SYSTEM : SYSTEMD_USER,
72 scope == UNIT_FILE_USER,
77 static int get_config_path(UnitFileScope scope, bool runtime, const char *root_dir, char **ret) {
82 assert(scope < _UNIT_FILE_SCOPE_MAX);
87 case UNIT_FILE_SYSTEM:
90 p = path_join(root_dir, "/run/systemd/system", NULL);
92 p = path_join(root_dir, SYSTEM_CONFIG_UNIT_PATH, NULL);
95 case UNIT_FILE_GLOBAL:
101 p = strdup("/run/systemd/user");
103 p = strdup(USER_CONFIG_UNIT_PATH);
112 r = user_runtime_dir(&p);
114 r = user_config_home(&p);
117 return r < 0 ? r : -ENOENT;
122 assert_not_reached("Bad scope");
132 static int add_file_change(
133 UnitFileChange **changes,
135 UnitFileChangeType type,
137 const char *source) {
143 assert(!changes == !n_changes);
148 c = realloc(*changes, (*n_changes + 1) * sizeof(UnitFileChange));
156 c[i].path = strdup(path);
160 path_kill_slashes(c[i].path);
163 c[i].source = strdup(source);
169 path_kill_slashes(c[i].path);
177 static int mark_symlink_for_removal(
178 Set **remove_symlinks_to,
186 r = set_ensure_allocated(remove_symlinks_to, &string_hash_ops);
194 path_kill_slashes(n);
196 r = set_consume(*remove_symlinks_to, n);
198 return r == -EEXIST ? 0 : r;
203 static int remove_marked_symlinks_fd(
204 Set *remove_symlinks_to,
207 const char *config_path,
209 UnitFileChange **changes,
211 char** instance_whitelist) {
213 _cleanup_closedir_ DIR *d = NULL;
216 assert(remove_symlinks_to);
235 if (!de && errno != 0) {
243 if (ignore_file(de->d_name))
246 dirent_ensure_type(d, de);
248 if (de->d_type == DT_DIR) {
250 _cleanup_free_ char *p = NULL;
252 nfd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
262 p = path_make_absolute(de->d_name, path);
268 /* This will close nfd, regardless whether it succeeds or not */
269 q = remove_marked_symlinks_fd(remove_symlinks_to, nfd, p, config_path, deleted, changes, n_changes, instance_whitelist);
273 } else if (de->d_type == DT_LNK) {
274 _cleanup_free_ char *p = NULL, *dest = NULL;
278 if (!unit_name_is_valid(de->d_name, TEMPLATE_VALID))
281 if (unit_name_is_instance(de->d_name) &&
282 instance_whitelist &&
283 !strv_contains(instance_whitelist, de->d_name)) {
285 _cleanup_free_ char *w;
287 /* OK, the file is not listed directly
288 * in the whitelist, so let's check if
289 * the template of it might be
292 w = unit_name_template(de->d_name);
296 if (!strv_contains(instance_whitelist, w))
300 p = path_make_absolute(de->d_name, path);
304 q = readlink_and_canonicalize(p, &dest);
315 set_get(remove_symlinks_to, dest) ||
316 set_get(remove_symlinks_to, basename(dest));
321 if (unlink(p) < 0 && errno != ENOENT) {
327 path_kill_slashes(p);
328 rmdir_parents(p, config_path);
329 add_file_change(changes, n_changes, UNIT_FILE_UNLINK, p, NULL);
331 if (!set_get(remove_symlinks_to, p)) {
333 q = mark_symlink_for_removal(&remove_symlinks_to, p);
346 static int remove_marked_symlinks(
347 Set *remove_symlinks_to,
348 const char *config_path,
349 UnitFileChange **changes,
351 char** instance_whitelist) {
353 _cleanup_close_ int fd = -1;
359 if (set_size(remove_symlinks_to) <= 0)
362 fd = open(config_path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
370 cfd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
376 /* This takes possession of cfd and closes it */
377 q = remove_marked_symlinks_fd(remove_symlinks_to, cfd, config_path, config_path, &deleted, changes, n_changes, instance_whitelist);
385 static int find_symlinks_fd(
389 const char *config_path,
390 bool *same_name_link) {
393 _cleanup_closedir_ DIR *d = NULL;
399 assert(same_name_link);
412 if (!de && errno != 0)
418 if (ignore_file(de->d_name))
421 dirent_ensure_type(d, de);
423 if (de->d_type == DT_DIR) {
425 _cleanup_free_ char *p = NULL;
427 nfd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
437 p = path_make_absolute(de->d_name, path);
443 /* This will close nfd, regardless whether it succeeds or not */
444 q = find_symlinks_fd(name, nfd, p, config_path, same_name_link);
450 } else if (de->d_type == DT_LNK) {
451 _cleanup_free_ char *p = NULL, *dest = NULL;
452 bool found_path, found_dest, b = false;
455 /* Acquire symlink name */
456 p = path_make_absolute(de->d_name, path);
460 /* Acquire symlink destination */
461 q = readlink_and_canonicalize(p, &dest);
471 /* Check if the symlink itself matches what we
473 if (path_is_absolute(name))
474 found_path = path_equal(p, name);
476 found_path = streq(de->d_name, name);
478 /* Check if what the symlink points to
479 * matches what we are looking for */
480 if (path_is_absolute(name))
481 found_dest = path_equal(dest, name);
483 found_dest = streq(basename(dest), name);
485 if (found_path && found_dest) {
486 _cleanup_free_ char *t = NULL;
488 /* Filter out same name links in the main
490 t = path_make_absolute(name, config_path);
494 b = path_equal(t, p);
498 *same_name_link = true;
499 else if (found_path || found_dest)
505 static int find_symlinks(
507 const char *config_path,
508 bool *same_name_link) {
514 assert(same_name_link);
516 fd = open(config_path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
523 /* This takes possession of fd and closes it */
524 return find_symlinks_fd(name, fd, config_path, config_path, same_name_link);
527 static int find_symlinks_in_scope(
529 const char *root_dir,
531 UnitFileState *state) {
534 _cleanup_free_ char *path = NULL;
535 bool same_name_link_runtime = false, same_name_link = false;
538 assert(scope < _UNIT_FILE_SCOPE_MAX);
542 /* First look in runtime config path */
543 r = get_config_path(scope, true, root_dir, &path);
547 r = find_symlinks(name, path, &same_name_link_runtime);
551 *state = UNIT_FILE_ENABLED_RUNTIME;
555 /* Then look in the normal config path */
556 r = get_config_path(scope, false, root_dir, &path);
560 r = find_symlinks(name, path, &same_name_link);
564 *state = UNIT_FILE_ENABLED;
568 /* Hmm, we didn't find it, but maybe we found the same name
570 if (same_name_link_runtime) {
571 *state = UNIT_FILE_LINKED_RUNTIME;
573 } else if (same_name_link) {
574 *state = UNIT_FILE_LINKED;
584 const char *root_dir,
587 UnitFileChange **changes,
588 unsigned *n_changes) {
591 _cleanup_free_ char *prefix = NULL;
595 assert(scope < _UNIT_FILE_SCOPE_MAX);
597 r = get_config_path(scope, runtime, root_dir, &prefix);
601 STRV_FOREACH(i, files) {
602 _cleanup_free_ char *path = NULL;
604 if (!unit_name_is_valid(*i, TEMPLATE_VALID)) {
610 path = path_make_absolute(*i, prefix);
616 if (symlink("/dev/null", path) >= 0) {
617 add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, path, "/dev/null");
621 if (errno == EEXIST) {
623 if (null_or_empty_path(path) > 0)
627 if (symlink_atomic("/dev/null", path) >= 0) {
628 add_file_change(changes, n_changes, UNIT_FILE_UNLINK, path, NULL);
629 add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, path, "/dev/null");
645 int unit_file_unmask(
648 const char *root_dir,
650 UnitFileChange **changes,
651 unsigned *n_changes) {
653 char **i, *config_path = NULL;
655 Set *remove_symlinks_to = NULL;
658 assert(scope < _UNIT_FILE_SCOPE_MAX);
660 r = get_config_path(scope, runtime, root_dir, &config_path);
664 STRV_FOREACH(i, files) {
665 _cleanup_free_ char *path = NULL;
667 if (!unit_name_is_valid(*i, TEMPLATE_VALID)) {
673 path = path_make_absolute(*i, config_path);
679 q = null_or_empty_path(path);
681 if (unlink(path) < 0)
684 q = mark_symlink_for_removal(&remove_symlinks_to, path);
685 add_file_change(changes, n_changes, UNIT_FILE_UNLINK, path, NULL);
689 if (q != -ENOENT && r == 0)
695 q = remove_marked_symlinks(remove_symlinks_to, config_path, changes, n_changes, files);
699 set_free_free(remove_symlinks_to);
708 const char *root_dir,
711 UnitFileChange **changes,
712 unsigned *n_changes) {
714 _cleanup_lookup_paths_free_ LookupPaths paths = {};
716 _cleanup_free_ char *config_path = NULL;
720 assert(scope < _UNIT_FILE_SCOPE_MAX);
722 r = lookup_paths_init_from_scope(&paths, scope, root_dir);
726 r = get_config_path(scope, runtime, root_dir, &config_path);
730 STRV_FOREACH(i, files) {
731 _cleanup_free_ char *path = NULL;
737 if (!path_is_absolute(*i) ||
738 !unit_name_is_valid(fn, TEMPLATE_VALID)) {
744 if (lstat(*i, &st) < 0) {
750 if (!S_ISREG(st.st_mode)) {
755 q = in_search_path(*i, paths.unit_path);
762 path = path_make_absolute(fn, config_path);
766 if (symlink(*i, path) >= 0) {
767 add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, path, *i);
771 if (errno == EEXIST) {
772 _cleanup_free_ char *dest = NULL;
774 q = readlink_and_make_absolute(path, &dest);
775 if (q < 0 && errno != ENOENT) {
781 if (q >= 0 && path_equal(dest, *i))
785 if (symlink_atomic(*i, path) >= 0) {
786 add_file_change(changes, n_changes, UNIT_FILE_UNLINK, path, NULL);
787 add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, path, *i);
803 void unit_file_list_free(Hashmap *h) {
806 while ((i = hashmap_steal_first(h))) {
814 void unit_file_changes_free(UnitFileChange *changes, unsigned n_changes) {
817 assert(changes || n_changes == 0);
822 for (i = 0; i < n_changes; i++) {
823 free(changes[i].path);
824 free(changes[i].source);
830 static void install_info_free(InstallInfo *i) {
835 strv_free(i->aliases);
836 strv_free(i->wanted_by);
837 strv_free(i->required_by);
839 free(i->default_instance);
843 static void install_info_hashmap_free(OrderedHashmap *m) {
849 while ((i = ordered_hashmap_steal_first(m)))
850 install_info_free(i);
852 ordered_hashmap_free(m);
855 static void install_context_done(InstallContext *c) {
858 install_info_hashmap_free(c->will_install);
859 install_info_hashmap_free(c->have_installed);
861 c->will_install = c->have_installed = NULL;
864 static int install_info_add(
868 InstallInfo *i = NULL;
872 assert(name || path);
875 name = basename(path);
877 if (!unit_name_is_valid(name, TEMPLATE_VALID))
880 if (ordered_hashmap_get(c->have_installed, name) ||
881 ordered_hashmap_get(c->will_install, name))
884 r = ordered_hashmap_ensure_allocated(&c->will_install, &string_hash_ops);
888 i = new0(InstallInfo, 1);
892 i->name = strdup(name);
899 i->path = strdup(path);
906 r = ordered_hashmap_put(c->will_install, i->name, i);
914 install_info_free(i);
919 static int install_info_add_auto(
921 const char *name_or_path) {
924 assert(name_or_path);
926 if (path_is_absolute(name_or_path))
927 return install_info_add(c, NULL, name_or_path);
929 return install_info_add(c, name_or_path, NULL);
932 static int config_parse_also(
934 const char *filename,
937 unsigned section_line,
945 const char *word, *state;
946 InstallContext *c = data;
947 InstallInfo *i = userdata;
953 FOREACH_WORD_QUOTED(word, l, rvalue, state) {
954 _cleanup_free_ char *n;
957 n = strndup(word, l);
961 r = install_info_add(c, n, NULL);
965 r = strv_extend(&i->also, n);
970 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
971 "Trailing garbage, ignoring.");
976 static int config_parse_user(
978 const char *filename,
981 unsigned section_line,
988 InstallInfo *i = data;
996 r = install_full_printf(i, rvalue, &printed);
1006 static int config_parse_default_instance(
1008 const char *filename,
1010 const char *section,
1011 unsigned section_line,
1018 InstallInfo *i = data;
1026 r = install_full_printf(i, rvalue, &printed);
1030 if (!unit_instance_is_valid(printed)) {
1035 free(i->default_instance);
1036 i->default_instance = printed;
1041 static int unit_file_load(
1045 const char *root_dir,
1050 const ConfigTableItem items[] = {
1051 { "Install", "Alias", config_parse_strv, 0, &info->aliases },
1052 { "Install", "WantedBy", config_parse_strv, 0, &info->wanted_by },
1053 { "Install", "RequiredBy", config_parse_strv, 0, &info->required_by },
1054 { "Install", "DefaultInstance", config_parse_default_instance, 0, info },
1055 { "Install", "Also", config_parse_also, 0, c },
1056 { "Exec", "User", config_parse_user, 0, info },
1060 _cleanup_fclose_ FILE *f = NULL;
1067 if (!isempty(root_dir))
1068 path = strappenda(root_dir, "/", path);
1071 r = access(path, F_OK) ? -errno : 0;
1075 fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|(allow_symlink ? 0 : O_NOFOLLOW));
1079 f = fdopen(fd, "re");
1085 r = config_parse(NULL, path, f,
1087 config_item_table_lookup, items,
1088 true, true, false, info);
1093 *also = !strv_isempty(info->also);
1096 (int) strv_length(info->aliases) +
1097 (int) strv_length(info->wanted_by) +
1098 (int) strv_length(info->required_by);
1101 static int unit_file_search(
1105 const char *root_dir,
1118 return unit_file_load(c, info, info->path, root_dir, allow_symlink, load, also);
1122 STRV_FOREACH(p, paths->unit_path) {
1123 _cleanup_free_ char *path = NULL;
1125 path = strjoin(*p, "/", info->name, NULL);
1129 r = unit_file_load(c, info, path, root_dir, allow_symlink, load, also);
1135 if (r != -ENOENT && r != -ELOOP)
1139 if (unit_name_is_instance(info->name)) {
1141 /* Unit file doesn't exist, however instance
1142 * enablement was requested. We will check if it is
1143 * possible to load template unit file. */
1145 _cleanup_free_ char *template = NULL;
1147 template = unit_name_template(info->name);
1151 STRV_FOREACH(p, paths->unit_path) {
1152 _cleanup_free_ char *path = NULL;
1154 path = strjoin(*p, "/", template, NULL);
1158 r = unit_file_load(c, info, path, root_dir, allow_symlink, load, also);
1164 if (r != -ENOENT && r != -ELOOP)
1172 static int unit_file_can_install(
1174 const char *root_dir,
1179 _cleanup_(install_context_done) InstallContext c = {};
1186 r = install_info_add_auto(&c, name);
1190 assert_se(i = ordered_hashmap_first(c.will_install));
1192 r = unit_file_search(&c, i, paths, root_dir, allow_symlink, true, also);
1196 (int) strv_length(i->aliases) +
1197 (int) strv_length(i->wanted_by) +
1198 (int) strv_length(i->required_by);
1203 static int create_symlink(
1204 const char *old_path,
1205 const char *new_path,
1207 UnitFileChange **changes,
1208 unsigned *n_changes) {
1210 _cleanup_free_ char *dest = NULL;
1216 mkdir_parents_label(new_path, 0755);
1218 if (symlink(old_path, new_path) >= 0) {
1219 add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, new_path, old_path);
1223 if (errno != EEXIST)
1226 r = readlink_and_make_absolute(new_path, &dest);
1230 if (path_equal(dest, old_path))
1236 r = symlink_atomic(old_path, new_path);
1240 add_file_change(changes, n_changes, UNIT_FILE_UNLINK, new_path, NULL);
1241 add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, new_path, old_path);
1246 static int install_info_symlink_alias(
1248 const char *config_path,
1250 UnitFileChange **changes,
1251 unsigned *n_changes) {
1257 assert(config_path);
1259 STRV_FOREACH(s, i->aliases) {
1260 _cleanup_free_ char *alias_path = NULL, *dst = NULL;
1262 q = install_full_printf(i, *s, &dst);
1266 alias_path = path_make_absolute(dst, config_path);
1270 q = create_symlink(i->path, alias_path, force, changes, n_changes);
1278 static int install_info_symlink_wants(
1280 const char *config_path,
1284 UnitFileChange **changes,
1285 unsigned *n_changes) {
1287 _cleanup_free_ char *buf = NULL;
1293 assert(config_path);
1295 if (unit_name_is_template(i->name)) {
1297 /* Don't install any symlink if there's no default
1298 * instance configured */
1300 if (!i->default_instance)
1303 buf = unit_name_replace_instance(i->name, i->default_instance);
1311 STRV_FOREACH(s, list) {
1312 _cleanup_free_ char *path = NULL, *dst = NULL;
1314 q = install_full_printf(i, *s, &dst);
1318 if (!unit_name_is_valid(dst, TEMPLATE_VALID)) {
1323 path = strjoin(config_path, "/", dst, suffix, n, NULL);
1327 q = create_symlink(i->path, path, force, changes, n_changes);
1335 static int install_info_symlink_link(
1338 const char *config_path,
1339 const char *root_dir,
1341 UnitFileChange **changes,
1342 unsigned *n_changes) {
1344 _cleanup_free_ char *path = NULL;
1349 assert(config_path);
1352 r = in_search_path(i->path, paths->unit_path);
1356 path = strjoin(config_path, "/", i->name, NULL);
1360 return create_symlink(i->path, path, force, changes, n_changes);
1363 static int install_info_apply(
1366 const char *config_path,
1367 const char *root_dir,
1369 UnitFileChange **changes,
1370 unsigned *n_changes) {
1376 assert(config_path);
1378 r = install_info_symlink_alias(i, config_path, force, changes, n_changes);
1380 q = install_info_symlink_wants(i, config_path, i->wanted_by, ".wants/", force, changes, n_changes);
1384 q = install_info_symlink_wants(i, config_path, i->required_by, ".requires/", force, changes, n_changes);
1388 q = install_info_symlink_link(i, paths, config_path, root_dir, force, changes, n_changes);
1395 static int install_context_apply(
1398 const char *config_path,
1399 const char *root_dir,
1401 UnitFileChange **changes,
1402 unsigned *n_changes) {
1409 assert(config_path);
1411 if (!ordered_hashmap_isempty(c->will_install)) {
1412 r = ordered_hashmap_ensure_allocated(&c->have_installed, &string_hash_ops);
1416 r = ordered_hashmap_reserve(c->have_installed, ordered_hashmap_size(c->will_install));
1422 while ((i = ordered_hashmap_first(c->will_install))) {
1423 assert_se(ordered_hashmap_move_one(c->have_installed, c->will_install, i->name) == 0);
1425 q = unit_file_search(c, i, paths, root_dir, false, true, NULL);
1434 q = install_info_apply(i, paths, config_path, root_dir, force, changes, n_changes);
1435 if (r >= 0 && q < 0)
1442 static int install_context_mark_for_removal(
1445 Set **remove_symlinks_to,
1446 const char *config_path,
1447 const char *root_dir) {
1454 assert(config_path);
1456 /* Marks all items for removal */
1458 if (!ordered_hashmap_isempty(c->will_install)) {
1459 r = ordered_hashmap_ensure_allocated(&c->have_installed, &string_hash_ops);
1463 r = ordered_hashmap_reserve(c->have_installed, ordered_hashmap_size(c->will_install));
1469 while ((i = ordered_hashmap_first(c->will_install))) {
1470 assert_se(ordered_hashmap_move_one(c->have_installed, c->will_install, i->name) == 0);
1472 q = unit_file_search(c, i, paths, root_dir, false, true, NULL);
1483 if (unit_name_is_instance(i->name)) {
1487 unit_file = basename(i->path);
1489 if (unit_name_is_instance(unit_file))
1490 /* unit file named as instance exists, thus all symlinks
1491 * pointing to it will be removed */
1492 q = mark_symlink_for_removal(remove_symlinks_to, i->name);
1494 /* does not exist, thus we will mark for removal symlinks
1495 * to template unit file */
1496 q = mark_symlink_for_removal(remove_symlinks_to, unit_file);
1498 /* If i->path is not set, it means that we didn't actually find
1499 * the unit file. But we can still remove symlinks to the
1500 * nonexistent template. */
1501 unit_file = unit_name_template(i->name);
1505 q = mark_symlink_for_removal(remove_symlinks_to, unit_file);
1509 q = mark_symlink_for_removal(remove_symlinks_to, i->name);
1511 if (r >= 0 && q < 0)
1518 int unit_file_add_dependency(
1519 UnitFileScope scope,
1521 const char *root_dir,
1526 UnitFileChange **changes,
1527 unsigned *n_changes) {
1529 _cleanup_lookup_paths_free_ LookupPaths paths = {};
1530 _cleanup_(install_context_done) InstallContext c = {};
1531 _cleanup_free_ char *config_path = NULL;
1537 assert(scope < _UNIT_FILE_SCOPE_MAX);
1539 r = lookup_paths_init_from_scope(&paths, scope, root_dir);
1543 r = get_config_path(scope, runtime, root_dir, &config_path);
1547 STRV_FOREACH(i, files) {
1548 UnitFileState state;
1550 state = unit_file_get_state(scope, root_dir, *i);
1552 return log_error_errno(state, "Failed to get unit file state for %s: %m", *i);
1554 if (state == UNIT_FILE_MASKED || state == UNIT_FILE_MASKED_RUNTIME) {
1555 log_error("Failed to enable unit: Unit %s is masked", *i);
1559 r = install_info_add_auto(&c, *i);
1564 if (!ordered_hashmap_isempty(c.will_install)) {
1565 r = ordered_hashmap_ensure_allocated(&c.have_installed, &string_hash_ops);
1569 r = ordered_hashmap_reserve(c.have_installed, ordered_hashmap_size(c.will_install));
1574 while ((info = ordered_hashmap_first(c.will_install))) {
1575 assert_se(ordered_hashmap_move_one(c.have_installed, c.will_install, info->name) == 0);
1577 r = unit_file_search(&c, info, &paths, root_dir, false, false, NULL);
1581 if (dep == UNIT_WANTS)
1582 r = strv_extend(&info->wanted_by, target);
1583 else if (dep == UNIT_REQUIRES)
1584 r = strv_extend(&info->required_by, target);
1591 r = install_info_apply(info, &paths, config_path, root_dir, force, changes, n_changes);
1599 int unit_file_enable(
1600 UnitFileScope scope,
1602 const char *root_dir,
1605 UnitFileChange **changes,
1606 unsigned *n_changes) {
1608 _cleanup_lookup_paths_free_ LookupPaths paths = {};
1609 _cleanup_(install_context_done) InstallContext c = {};
1611 _cleanup_free_ char *config_path = NULL;
1615 assert(scope < _UNIT_FILE_SCOPE_MAX);
1617 r = lookup_paths_init_from_scope(&paths, scope, root_dir);
1621 r = get_config_path(scope, runtime, root_dir, &config_path);
1625 STRV_FOREACH(i, files) {
1626 UnitFileState state;
1628 /* We only want to know if this unit is masked, so we ignore
1629 * errors from unit_file_get_state, deferring other checks.
1630 * This allows templated units to be enabled on the fly. */
1631 state = unit_file_get_state(scope, root_dir, *i);
1632 if (state == UNIT_FILE_MASKED || state == UNIT_FILE_MASKED_RUNTIME) {
1633 log_error("Failed to enable unit: Unit %s is masked", *i);
1637 r = install_info_add_auto(&c, *i);
1642 /* This will return the number of symlink rules that were
1643 supposed to be created, not the ones actually created. This is
1644 useful to determine whether the passed files had any
1645 installation data at all. */
1647 return install_context_apply(&c, &paths, config_path, root_dir, force, changes, n_changes);
1650 int unit_file_disable(
1651 UnitFileScope scope,
1653 const char *root_dir,
1655 UnitFileChange **changes,
1656 unsigned *n_changes) {
1658 _cleanup_lookup_paths_free_ LookupPaths paths = {};
1659 _cleanup_(install_context_done) InstallContext c = {};
1661 _cleanup_free_ char *config_path = NULL;
1662 _cleanup_set_free_free_ Set *remove_symlinks_to = NULL;
1666 assert(scope < _UNIT_FILE_SCOPE_MAX);
1668 r = lookup_paths_init_from_scope(&paths, scope, root_dir);
1672 r = get_config_path(scope, runtime, root_dir, &config_path);
1676 STRV_FOREACH(i, files) {
1677 r = install_info_add_auto(&c, *i);
1682 r = install_context_mark_for_removal(&c, &paths, &remove_symlinks_to, config_path, root_dir);
1684 q = remove_marked_symlinks(remove_symlinks_to, config_path, changes, n_changes, files);
1691 int unit_file_reenable(
1692 UnitFileScope scope,
1694 const char *root_dir,
1697 UnitFileChange **changes,
1698 unsigned *n_changes) {
1701 r = unit_file_disable(scope, runtime, root_dir, files,
1702 changes, n_changes);
1706 return unit_file_enable(scope, runtime, root_dir, files, force,
1707 changes, n_changes);
1710 int unit_file_set_default(
1711 UnitFileScope scope,
1712 const char *root_dir,
1715 UnitFileChange **changes,
1716 unsigned *n_changes) {
1718 _cleanup_lookup_paths_free_ LookupPaths paths = {};
1719 _cleanup_(install_context_done) InstallContext c = {};
1720 _cleanup_free_ char *config_path = NULL;
1723 InstallInfo *i = NULL;
1726 assert(scope < _UNIT_FILE_SCOPE_MAX);
1729 if (unit_name_to_type(file) != UNIT_TARGET)
1732 r = lookup_paths_init_from_scope(&paths, scope, root_dir);
1736 r = get_config_path(scope, false, root_dir, &config_path);
1740 r = install_info_add_auto(&c, file);
1744 assert_se(i = ordered_hashmap_first(c.will_install));
1746 r = unit_file_search(&c, i, &paths, root_dir, false, true, NULL);
1750 path = strappenda(config_path, "/" SPECIAL_DEFAULT_TARGET);
1752 r = create_symlink(i->path, path, force, changes, n_changes);
1759 int unit_file_get_default(
1760 UnitFileScope scope,
1761 const char *root_dir,
1764 _cleanup_lookup_paths_free_ LookupPaths paths = {};
1769 assert(scope < _UNIT_FILE_SCOPE_MAX);
1772 r = lookup_paths_init_from_scope(&paths, scope, root_dir);
1776 STRV_FOREACH(p, paths.unit_path) {
1777 _cleanup_free_ char *path = NULL, *tmp = NULL;
1780 path = path_join(root_dir, *p, SPECIAL_DEFAULT_TARGET);
1784 r = readlink_malloc(path, &tmp);
1787 else if (r == -EINVAL)
1789 n = strdup(SPECIAL_DEFAULT_TARGET);
1793 n = strdup(basename(tmp));
1805 UnitFileState unit_file_get_state(
1806 UnitFileScope scope,
1807 const char *root_dir,
1810 _cleanup_lookup_paths_free_ LookupPaths paths = {};
1811 UnitFileState state = _UNIT_FILE_STATE_INVALID;
1813 _cleanup_free_ char *path = NULL;
1817 assert(scope < _UNIT_FILE_SCOPE_MAX);
1820 if (root_dir && scope != UNIT_FILE_SYSTEM)
1823 if (!unit_name_is_valid(name, TEMPLATE_VALID))
1826 r = lookup_paths_init_from_scope(&paths, scope, root_dir);
1830 STRV_FOREACH(i, paths.unit_path) {
1838 path = path_join(root_dir, *i, name);
1843 partial = path + strlen(root_dir);
1848 * Search for a unit file in our default paths, to
1849 * be sure, that there are no broken symlinks.
1851 if (lstat(path, &st) < 0) {
1853 if (errno != ENOENT)
1856 if (!unit_name_is_instance(name))
1859 if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode))
1862 r = null_or_empty_path(path);
1863 if (r < 0 && r != -ENOENT)
1866 state = path_startswith(*i, "/run") ?
1867 UNIT_FILE_MASKED_RUNTIME : UNIT_FILE_MASKED;
1872 r = find_symlinks_in_scope(scope, root_dir, name, &state);
1878 r = unit_file_can_install(&paths, root_dir, partial, true, &also);
1879 if (r < 0 && errno != ENOENT)
1882 return UNIT_FILE_DISABLED;
1885 return UNIT_FILE_INDIRECT;
1886 return UNIT_FILE_STATIC;
1890 return r < 0 ? r : state;
1893 int unit_file_query_preset(UnitFileScope scope, const char *root_dir, const char *name) {
1894 _cleanup_strv_free_ char **files = NULL;
1899 assert(scope < _UNIT_FILE_SCOPE_MAX);
1902 if (scope == UNIT_FILE_SYSTEM)
1903 r = conf_files_list(&files, ".preset", root_dir,
1904 "/etc/systemd/system-preset",
1905 "/usr/local/lib/systemd/system-preset",
1906 "/usr/lib/systemd/system-preset",
1907 #ifdef HAVE_SPLIT_USR
1908 "/lib/systemd/system-preset",
1911 else if (scope == UNIT_FILE_GLOBAL)
1912 r = conf_files_list(&files, ".preset", root_dir,
1913 "/etc/systemd/user-preset",
1914 "/usr/local/lib/systemd/user-preset",
1915 "/usr/lib/systemd/user-preset",
1923 STRV_FOREACH(p, files) {
1924 _cleanup_fclose_ FILE *f;
1926 f = fopen(*p, "re");
1928 if (errno == ENOENT)
1935 char line[LINE_MAX], *l;
1937 if (!fgets(line, sizeof(line), f))
1944 if (strchr(COMMENTS "\n", *l))
1947 if (first_word(l, "enable")) {
1949 l += strspn(l, WHITESPACE);
1951 if (fnmatch(l, name, FNM_NOESCAPE) == 0) {
1952 log_debug("Preset file says enable %s.", name);
1956 } else if (first_word(l, "disable")) {
1958 l += strspn(l, WHITESPACE);
1960 if (fnmatch(l, name, FNM_NOESCAPE) == 0) {
1961 log_debug("Preset file says disable %s.", name);
1966 log_debug("Couldn't parse line '%s'", l);
1970 /* Default is "enable" */
1971 log_debug("Preset file doesn't say anything about %s, enabling.", name);
1975 int unit_file_preset(
1976 UnitFileScope scope,
1978 const char *root_dir,
1980 UnitFilePresetMode mode,
1982 UnitFileChange **changes,
1983 unsigned *n_changes) {
1985 _cleanup_(install_context_done) InstallContext plus = {}, minus = {};
1986 _cleanup_lookup_paths_free_ LookupPaths paths = {};
1987 _cleanup_free_ char *config_path = NULL;
1992 assert(scope < _UNIT_FILE_SCOPE_MAX);
1993 assert(mode < _UNIT_FILE_PRESET_MAX);
1995 r = lookup_paths_init_from_scope(&paths, scope, root_dir);
1999 r = get_config_path(scope, runtime, root_dir, &config_path);
2003 STRV_FOREACH(i, files) {
2005 if (!unit_name_is_valid(*i, TEMPLATE_VALID))
2008 r = unit_file_query_preset(scope, root_dir, *i);
2012 if (r && mode != UNIT_FILE_PRESET_DISABLE_ONLY)
2013 r = install_info_add_auto(&plus, *i);
2014 else if (!r && mode != UNIT_FILE_PRESET_ENABLE_ONLY)
2015 r = install_info_add_auto(&minus, *i);
2024 if (mode != UNIT_FILE_PRESET_ENABLE_ONLY) {
2025 _cleanup_set_free_free_ Set *remove_symlinks_to = NULL;
2027 r = install_context_mark_for_removal(&minus, &paths, &remove_symlinks_to, config_path, root_dir);
2029 q = remove_marked_symlinks(remove_symlinks_to, config_path, changes, n_changes, files);
2034 if (mode != UNIT_FILE_PRESET_DISABLE_ONLY) {
2035 /* Returns number of symlinks that where supposed to be installed. */
2036 q = install_context_apply(&plus, &paths, config_path, root_dir, force, changes, n_changes);
2044 int unit_file_preset_all(
2045 UnitFileScope scope,
2047 const char *root_dir,
2048 UnitFilePresetMode mode,
2050 UnitFileChange **changes,
2051 unsigned *n_changes) {
2053 _cleanup_(install_context_done) InstallContext plus = {}, minus = {};
2054 _cleanup_lookup_paths_free_ LookupPaths paths = {};
2055 _cleanup_free_ char *config_path = NULL;
2060 assert(scope < _UNIT_FILE_SCOPE_MAX);
2061 assert(mode < _UNIT_FILE_PRESET_MAX);
2063 r = lookup_paths_init_from_scope(&paths, scope, root_dir);
2067 r = get_config_path(scope, runtime, root_dir, &config_path);
2071 STRV_FOREACH(i, paths.unit_path) {
2072 _cleanup_closedir_ DIR *d = NULL;
2073 _cleanup_free_ char *units_dir;
2075 units_dir = path_join(root_dir, *i, NULL);
2079 d = opendir(units_dir);
2081 if (errno == ENOENT)
2092 if (!de && errno != 0)
2098 if (ignore_file(de->d_name))
2101 if (!unit_name_is_valid(de->d_name, TEMPLATE_VALID))
2104 dirent_ensure_type(d, de);
2106 if (de->d_type != DT_REG)
2109 r = unit_file_query_preset(scope, root_dir, de->d_name);
2113 if (r && mode != UNIT_FILE_PRESET_DISABLE_ONLY)
2114 r = install_info_add_auto(&plus, de->d_name);
2115 else if (!r && mode != UNIT_FILE_PRESET_ENABLE_ONLY)
2116 r = install_info_add_auto(&minus, de->d_name);
2126 if (mode != UNIT_FILE_PRESET_ENABLE_ONLY) {
2127 _cleanup_set_free_free_ Set *remove_symlinks_to = NULL;
2129 r = install_context_mark_for_removal(&minus, &paths, &remove_symlinks_to, config_path, root_dir);
2131 q = remove_marked_symlinks(remove_symlinks_to, config_path, changes, n_changes, NULL);
2136 if (mode != UNIT_FILE_PRESET_DISABLE_ONLY) {
2137 q = install_context_apply(&plus, &paths, config_path, root_dir, force, changes, n_changes);
2145 static void unit_file_list_free_one(UnitFileList *f) {
2153 DEFINE_TRIVIAL_CLEANUP_FUNC(UnitFileList*, unit_file_list_free_one);
2155 int unit_file_get_list(
2156 UnitFileScope scope,
2157 const char *root_dir,
2160 _cleanup_lookup_paths_free_ LookupPaths paths = {};
2165 assert(scope < _UNIT_FILE_SCOPE_MAX);
2168 if (root_dir && scope != UNIT_FILE_SYSTEM)
2172 r = access(root_dir, F_OK);
2177 r = lookup_paths_init_from_scope(&paths, scope, root_dir);
2181 STRV_FOREACH(i, paths.unit_path) {
2182 _cleanup_closedir_ DIR *d = NULL;
2183 _cleanup_free_ char *units_dir;
2185 units_dir = path_join(root_dir, *i, NULL);
2189 d = opendir(units_dir);
2191 if (errno == ENOENT)
2198 _cleanup_(unit_file_list_free_onep) UnitFileList *f = NULL;
2200 _cleanup_free_ char *path = NULL;
2204 if (!de && errno != 0)
2210 if (ignore_file(de->d_name))
2213 if (!unit_name_is_valid(de->d_name, TEMPLATE_VALID))
2216 if (hashmap_get(h, de->d_name))
2219 dirent_ensure_type(d, de);
2221 if (!IN_SET(de->d_type, DT_LNK, DT_REG))
2224 f = new0(UnitFileList, 1);
2228 f->path = path_make_absolute(de->d_name, units_dir);
2232 r = null_or_empty_path(f->path);
2233 if (r < 0 && r != -ENOENT)
2237 path_startswith(*i, "/run") ?
2238 UNIT_FILE_MASKED_RUNTIME : UNIT_FILE_MASKED;
2242 r = find_symlinks_in_scope(scope, root_dir, de->d_name, &f->state);
2246 f->state = UNIT_FILE_ENABLED;
2250 path = path_make_absolute(de->d_name, *i);
2254 r = unit_file_can_install(&paths, root_dir, path, true, NULL);
2255 if (r == -EINVAL || /* Invalid setting? */
2256 r == -EBADMSG || /* Invalid format? */
2257 r == -ENOENT /* Included file not found? */)
2258 f->state = UNIT_FILE_INVALID;
2262 f->state = UNIT_FILE_DISABLED;
2264 f->state = UNIT_FILE_STATIC;
2267 r = hashmap_put(h, basename(f->path), f);
2270 f = NULL; /* prevent cleanup */
2277 static const char* const unit_file_state_table[_UNIT_FILE_STATE_MAX] = {
2278 [UNIT_FILE_ENABLED] = "enabled",
2279 [UNIT_FILE_ENABLED_RUNTIME] = "enabled-runtime",
2280 [UNIT_FILE_LINKED] = "linked",
2281 [UNIT_FILE_LINKED_RUNTIME] = "linked-runtime",
2282 [UNIT_FILE_MASKED] = "masked",
2283 [UNIT_FILE_MASKED_RUNTIME] = "masked-runtime",
2284 [UNIT_FILE_STATIC] = "static",
2285 [UNIT_FILE_DISABLED] = "disabled",
2286 [UNIT_FILE_INDIRECT] = "indirect",
2287 [UNIT_FILE_INVALID] = "invalid",
2290 DEFINE_STRING_TABLE_LOOKUP(unit_file_state, UnitFileState);
2292 static const char* const unit_file_change_type_table[_UNIT_FILE_CHANGE_TYPE_MAX] = {
2293 [UNIT_FILE_SYMLINK] = "symlink",
2294 [UNIT_FILE_UNLINK] = "unlink",
2297 DEFINE_STRING_TABLE_LOOKUP(unit_file_change_type, UnitFileChangeType);
2299 static const char* const unit_file_preset_mode_table[_UNIT_FILE_PRESET_MAX] = {
2300 [UNIT_FILE_PRESET_FULL] = "full",
2301 [UNIT_FILE_PRESET_ENABLE_ONLY] = "enable-only",
2302 [UNIT_FILE_PRESET_DISABLE_ONLY] = "disable-only",
2305 DEFINE_STRING_TABLE_LOOKUP(unit_file_preset_mode, UnitFilePresetMode);