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 (hidden_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 (hidden_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);
541 /* First look in runtime config path */
542 r = get_config_path(scope, true, root_dir, &path);
546 r = find_symlinks(name, path, &same_name_link_runtime);
550 *state = UNIT_FILE_ENABLED_RUNTIME;
554 /* Then look in the normal config path */
555 r = get_config_path(scope, false, root_dir, &path);
559 r = find_symlinks(name, path, &same_name_link);
563 *state = UNIT_FILE_ENABLED;
567 /* Hmm, we didn't find it, but maybe we found the same name
569 if (same_name_link_runtime) {
570 *state = UNIT_FILE_LINKED_RUNTIME;
572 } else if (same_name_link) {
573 *state = UNIT_FILE_LINKED;
583 const char *root_dir,
586 UnitFileChange **changes,
587 unsigned *n_changes) {
590 _cleanup_free_ char *prefix = NULL;
594 assert(scope < _UNIT_FILE_SCOPE_MAX);
596 r = get_config_path(scope, runtime, root_dir, &prefix);
600 STRV_FOREACH(i, files) {
601 _cleanup_free_ char *path = NULL;
603 if (!unit_name_is_valid(*i, TEMPLATE_VALID)) {
609 path = path_make_absolute(*i, prefix);
615 if (symlink("/dev/null", path) >= 0) {
616 add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, path, "/dev/null");
620 if (errno == EEXIST) {
622 if (null_or_empty_path(path) > 0)
626 if (symlink_atomic("/dev/null", path) >= 0) {
627 add_file_change(changes, n_changes, UNIT_FILE_UNLINK, path, NULL);
628 add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, path, "/dev/null");
644 int unit_file_unmask(
647 const char *root_dir,
649 UnitFileChange **changes,
650 unsigned *n_changes) {
652 char **i, *config_path = NULL;
654 Set *remove_symlinks_to = NULL;
657 assert(scope < _UNIT_FILE_SCOPE_MAX);
659 r = get_config_path(scope, runtime, root_dir, &config_path);
663 STRV_FOREACH(i, files) {
664 _cleanup_free_ char *path = NULL;
666 if (!unit_name_is_valid(*i, TEMPLATE_VALID)) {
672 path = path_make_absolute(*i, config_path);
678 q = null_or_empty_path(path);
680 if (unlink(path) < 0)
683 q = mark_symlink_for_removal(&remove_symlinks_to, path);
684 add_file_change(changes, n_changes, UNIT_FILE_UNLINK, path, NULL);
688 if (q != -ENOENT && r == 0)
694 q = remove_marked_symlinks(remove_symlinks_to, config_path, changes, n_changes, files);
698 set_free_free(remove_symlinks_to);
707 const char *root_dir,
710 UnitFileChange **changes,
711 unsigned *n_changes) {
713 _cleanup_lookup_paths_free_ LookupPaths paths = {};
715 _cleanup_free_ char *config_path = NULL;
719 assert(scope < _UNIT_FILE_SCOPE_MAX);
721 r = lookup_paths_init_from_scope(&paths, scope, root_dir);
725 r = get_config_path(scope, runtime, root_dir, &config_path);
729 STRV_FOREACH(i, files) {
730 _cleanup_free_ char *path = NULL;
736 if (!path_is_absolute(*i) ||
737 !unit_name_is_valid(fn, TEMPLATE_VALID)) {
743 if (lstat(*i, &st) < 0) {
749 if (!S_ISREG(st.st_mode)) {
754 q = in_search_path(*i, paths.unit_path);
761 path = path_make_absolute(fn, config_path);
765 if (symlink(*i, path) >= 0) {
766 add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, path, *i);
770 if (errno == EEXIST) {
771 _cleanup_free_ char *dest = NULL;
773 q = readlink_and_make_absolute(path, &dest);
774 if (q < 0 && errno != ENOENT) {
780 if (q >= 0 && path_equal(dest, *i))
784 if (symlink_atomic(*i, path) >= 0) {
785 add_file_change(changes, n_changes, UNIT_FILE_UNLINK, path, NULL);
786 add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, path, *i);
802 void unit_file_list_free(Hashmap *h) {
805 while ((i = hashmap_steal_first(h))) {
813 void unit_file_changes_free(UnitFileChange *changes, unsigned n_changes) {
816 assert(changes || n_changes == 0);
821 for (i = 0; i < n_changes; i++) {
822 free(changes[i].path);
823 free(changes[i].source);
829 static void install_info_free(InstallInfo *i) {
834 strv_free(i->aliases);
835 strv_free(i->wanted_by);
836 strv_free(i->required_by);
838 free(i->default_instance);
842 static void install_info_hashmap_free(OrderedHashmap *m) {
848 while ((i = ordered_hashmap_steal_first(m)))
849 install_info_free(i);
851 ordered_hashmap_free(m);
854 static void install_context_done(InstallContext *c) {
857 install_info_hashmap_free(c->will_install);
858 install_info_hashmap_free(c->have_installed);
860 c->will_install = c->have_installed = NULL;
863 static int install_info_add(
867 InstallInfo *i = NULL;
871 assert(name || path);
874 name = basename(path);
876 if (!unit_name_is_valid(name, TEMPLATE_VALID))
879 if (ordered_hashmap_get(c->have_installed, name) ||
880 ordered_hashmap_get(c->will_install, name))
883 r = ordered_hashmap_ensure_allocated(&c->will_install, &string_hash_ops);
887 i = new0(InstallInfo, 1);
891 i->name = strdup(name);
898 i->path = strdup(path);
905 r = ordered_hashmap_put(c->will_install, i->name, i);
913 install_info_free(i);
918 static int install_info_add_auto(
920 const char *name_or_path) {
923 assert(name_or_path);
925 if (path_is_absolute(name_or_path))
926 return install_info_add(c, NULL, name_or_path);
928 return install_info_add(c, name_or_path, NULL);
931 static int config_parse_also(
933 const char *filename,
936 unsigned section_line,
944 const char *word, *state;
945 InstallContext *c = data;
946 InstallInfo *i = userdata;
952 FOREACH_WORD_QUOTED(word, l, rvalue, state) {
953 _cleanup_free_ char *n;
956 n = strndup(word, l);
960 r = install_info_add(c, n, NULL);
964 r = strv_extend(&i->also, n);
969 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
970 "Trailing garbage, ignoring.");
975 static int config_parse_user(
977 const char *filename,
980 unsigned section_line,
987 InstallInfo *i = data;
995 r = install_full_printf(i, rvalue, &printed);
1005 static int config_parse_default_instance(
1007 const char *filename,
1009 const char *section,
1010 unsigned section_line,
1017 InstallInfo *i = data;
1025 r = install_full_printf(i, rvalue, &printed);
1029 if (!unit_instance_is_valid(printed)) {
1034 free(i->default_instance);
1035 i->default_instance = printed;
1040 static int unit_file_load(
1044 const char *root_dir,
1049 const ConfigTableItem items[] = {
1050 { "Install", "Alias", config_parse_strv, 0, &info->aliases },
1051 { "Install", "WantedBy", config_parse_strv, 0, &info->wanted_by },
1052 { "Install", "RequiredBy", config_parse_strv, 0, &info->required_by },
1053 { "Install", "DefaultInstance", config_parse_default_instance, 0, info },
1054 { "Install", "Also", config_parse_also, 0, c },
1055 { "Exec", "User", config_parse_user, 0, info },
1059 _cleanup_fclose_ FILE *f = NULL;
1066 if (!isempty(root_dir))
1067 path = strappenda(root_dir, "/", path);
1070 r = access(path, F_OK) ? -errno : 0;
1074 fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|(allow_symlink ? 0 : O_NOFOLLOW));
1078 f = fdopen(fd, "re");
1084 r = config_parse(NULL, path, f,
1086 config_item_table_lookup, items,
1087 true, true, false, info);
1092 *also = !strv_isempty(info->also);
1095 (int) strv_length(info->aliases) +
1096 (int) strv_length(info->wanted_by) +
1097 (int) strv_length(info->required_by);
1100 static int unit_file_search(
1104 const char *root_dir,
1117 return unit_file_load(c, info, info->path, root_dir, allow_symlink, load, also);
1121 STRV_FOREACH(p, paths->unit_path) {
1122 _cleanup_free_ char *path = NULL;
1124 path = strjoin(*p, "/", info->name, NULL);
1128 r = unit_file_load(c, info, path, root_dir, allow_symlink, load, also);
1134 if (r != -ENOENT && r != -ELOOP)
1138 if (unit_name_is_instance(info->name)) {
1140 /* Unit file doesn't exist, however instance
1141 * enablement was requested. We will check if it is
1142 * possible to load template unit file. */
1144 _cleanup_free_ char *template = NULL;
1146 template = unit_name_template(info->name);
1150 STRV_FOREACH(p, paths->unit_path) {
1151 _cleanup_free_ char *path = NULL;
1153 path = strjoin(*p, "/", template, NULL);
1157 r = unit_file_load(c, info, path, root_dir, allow_symlink, load, also);
1163 if (r != -ENOENT && r != -ELOOP)
1171 static int unit_file_can_install(
1173 const char *root_dir,
1178 _cleanup_(install_context_done) InstallContext c = {};
1185 r = install_info_add_auto(&c, name);
1189 assert_se(i = ordered_hashmap_first(c.will_install));
1191 r = unit_file_search(&c, i, paths, root_dir, allow_symlink, true, also);
1195 (int) strv_length(i->aliases) +
1196 (int) strv_length(i->wanted_by) +
1197 (int) strv_length(i->required_by);
1202 static int create_symlink(
1203 const char *old_path,
1204 const char *new_path,
1206 UnitFileChange **changes,
1207 unsigned *n_changes) {
1209 _cleanup_free_ char *dest = NULL;
1215 mkdir_parents_label(new_path, 0755);
1217 if (symlink(old_path, new_path) >= 0) {
1218 add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, new_path, old_path);
1222 if (errno != EEXIST)
1225 r = readlink_and_make_absolute(new_path, &dest);
1229 if (path_equal(dest, old_path))
1235 r = symlink_atomic(old_path, new_path);
1239 add_file_change(changes, n_changes, UNIT_FILE_UNLINK, new_path, NULL);
1240 add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, new_path, old_path);
1245 static int install_info_symlink_alias(
1247 const char *config_path,
1249 UnitFileChange **changes,
1250 unsigned *n_changes) {
1256 assert(config_path);
1258 STRV_FOREACH(s, i->aliases) {
1259 _cleanup_free_ char *alias_path = NULL, *dst = NULL;
1261 q = install_full_printf(i, *s, &dst);
1265 alias_path = path_make_absolute(dst, config_path);
1269 q = create_symlink(i->path, alias_path, force, changes, n_changes);
1277 static int install_info_symlink_wants(
1279 const char *config_path,
1283 UnitFileChange **changes,
1284 unsigned *n_changes) {
1286 _cleanup_free_ char *buf = NULL;
1292 assert(config_path);
1294 if (unit_name_is_template(i->name)) {
1296 /* Don't install any symlink if there's no default
1297 * instance configured */
1299 if (!i->default_instance)
1302 buf = unit_name_replace_instance(i->name, i->default_instance);
1310 STRV_FOREACH(s, list) {
1311 _cleanup_free_ char *path = NULL, *dst = NULL;
1313 q = install_full_printf(i, *s, &dst);
1317 if (!unit_name_is_valid(dst, TEMPLATE_VALID)) {
1322 path = strjoin(config_path, "/", dst, suffix, n, NULL);
1326 q = create_symlink(i->path, path, force, changes, n_changes);
1334 static int install_info_symlink_link(
1337 const char *config_path,
1338 const char *root_dir,
1340 UnitFileChange **changes,
1341 unsigned *n_changes) {
1343 _cleanup_free_ char *path = NULL;
1348 assert(config_path);
1351 r = in_search_path(i->path, paths->unit_path);
1355 path = strjoin(config_path, "/", i->name, NULL);
1359 return create_symlink(i->path, path, force, changes, n_changes);
1362 static int install_info_apply(
1365 const char *config_path,
1366 const char *root_dir,
1368 UnitFileChange **changes,
1369 unsigned *n_changes) {
1375 assert(config_path);
1377 r = install_info_symlink_alias(i, config_path, force, changes, n_changes);
1379 q = install_info_symlink_wants(i, config_path, i->wanted_by, ".wants/", force, changes, n_changes);
1383 q = install_info_symlink_wants(i, config_path, i->required_by, ".requires/", force, changes, n_changes);
1387 q = install_info_symlink_link(i, paths, config_path, root_dir, force, changes, n_changes);
1394 static int install_context_apply(
1397 const char *config_path,
1398 const char *root_dir,
1400 UnitFileChange **changes,
1401 unsigned *n_changes) {
1408 assert(config_path);
1410 if (!ordered_hashmap_isempty(c->will_install)) {
1411 r = ordered_hashmap_ensure_allocated(&c->have_installed, &string_hash_ops);
1415 r = ordered_hashmap_reserve(c->have_installed, ordered_hashmap_size(c->will_install));
1421 while ((i = ordered_hashmap_first(c->will_install))) {
1422 assert_se(ordered_hashmap_move_one(c->have_installed, c->will_install, i->name) == 0);
1424 q = unit_file_search(c, i, paths, root_dir, false, true, NULL);
1433 q = install_info_apply(i, paths, config_path, root_dir, force, changes, n_changes);
1434 if (r >= 0 && q < 0)
1441 static int install_context_mark_for_removal(
1444 Set **remove_symlinks_to,
1445 const char *config_path,
1446 const char *root_dir) {
1453 assert(config_path);
1455 /* Marks all items for removal */
1457 if (!ordered_hashmap_isempty(c->will_install)) {
1458 r = ordered_hashmap_ensure_allocated(&c->have_installed, &string_hash_ops);
1462 r = ordered_hashmap_reserve(c->have_installed, ordered_hashmap_size(c->will_install));
1468 while ((i = ordered_hashmap_first(c->will_install))) {
1469 assert_se(ordered_hashmap_move_one(c->have_installed, c->will_install, i->name) == 0);
1471 q = unit_file_search(c, i, paths, root_dir, false, true, NULL);
1482 if (unit_name_is_instance(i->name)) {
1486 unit_file = basename(i->path);
1488 if (unit_name_is_instance(unit_file))
1489 /* unit file named as instance exists, thus all symlinks
1490 * pointing to it will be removed */
1491 q = mark_symlink_for_removal(remove_symlinks_to, i->name);
1493 /* does not exist, thus we will mark for removal symlinks
1494 * to template unit file */
1495 q = mark_symlink_for_removal(remove_symlinks_to, unit_file);
1497 /* If i->path is not set, it means that we didn't actually find
1498 * the unit file. But we can still remove symlinks to the
1499 * nonexistent template. */
1500 unit_file = unit_name_template(i->name);
1504 q = mark_symlink_for_removal(remove_symlinks_to, unit_file);
1508 q = mark_symlink_for_removal(remove_symlinks_to, i->name);
1510 if (r >= 0 && q < 0)
1517 int unit_file_add_dependency(
1518 UnitFileScope scope,
1520 const char *root_dir,
1525 UnitFileChange **changes,
1526 unsigned *n_changes) {
1528 _cleanup_lookup_paths_free_ LookupPaths paths = {};
1529 _cleanup_(install_context_done) InstallContext c = {};
1530 _cleanup_free_ char *config_path = NULL;
1536 assert(scope < _UNIT_FILE_SCOPE_MAX);
1538 r = lookup_paths_init_from_scope(&paths, scope, root_dir);
1542 r = get_config_path(scope, runtime, root_dir, &config_path);
1546 STRV_FOREACH(i, files) {
1547 UnitFileState state;
1549 state = unit_file_get_state(scope, root_dir, *i);
1551 return log_error_errno(state, "Failed to get unit file state for %s: %m", *i);
1553 if (state == UNIT_FILE_MASKED || state == UNIT_FILE_MASKED_RUNTIME) {
1554 log_error("Failed to enable unit: Unit %s is masked", *i);
1558 r = install_info_add_auto(&c, *i);
1563 if (!ordered_hashmap_isempty(c.will_install)) {
1564 r = ordered_hashmap_ensure_allocated(&c.have_installed, &string_hash_ops);
1568 r = ordered_hashmap_reserve(c.have_installed, ordered_hashmap_size(c.will_install));
1573 while ((info = ordered_hashmap_first(c.will_install))) {
1574 assert_se(ordered_hashmap_move_one(c.have_installed, c.will_install, info->name) == 0);
1576 r = unit_file_search(&c, info, &paths, root_dir, false, false, NULL);
1580 if (dep == UNIT_WANTS)
1581 r = strv_extend(&info->wanted_by, target);
1582 else if (dep == UNIT_REQUIRES)
1583 r = strv_extend(&info->required_by, target);
1590 r = install_info_apply(info, &paths, config_path, root_dir, force, changes, n_changes);
1598 int unit_file_enable(
1599 UnitFileScope scope,
1601 const char *root_dir,
1604 UnitFileChange **changes,
1605 unsigned *n_changes) {
1607 _cleanup_lookup_paths_free_ LookupPaths paths = {};
1608 _cleanup_(install_context_done) InstallContext c = {};
1610 _cleanup_free_ char *config_path = NULL;
1614 assert(scope < _UNIT_FILE_SCOPE_MAX);
1616 r = lookup_paths_init_from_scope(&paths, scope, root_dir);
1620 r = get_config_path(scope, runtime, root_dir, &config_path);
1624 STRV_FOREACH(i, files) {
1625 UnitFileState state;
1627 /* We only want to know if this unit is masked, so we ignore
1628 * errors from unit_file_get_state, deferring other checks.
1629 * This allows templated units to be enabled on the fly. */
1630 state = unit_file_get_state(scope, root_dir, *i);
1631 if (state == UNIT_FILE_MASKED || state == UNIT_FILE_MASKED_RUNTIME) {
1632 log_error("Failed to enable unit: Unit %s is masked", *i);
1636 r = install_info_add_auto(&c, *i);
1641 /* This will return the number of symlink rules that were
1642 supposed to be created, not the ones actually created. This is
1643 useful to determine whether the passed files had any
1644 installation data at all. */
1646 return install_context_apply(&c, &paths, config_path, root_dir, force, changes, n_changes);
1649 int unit_file_disable(
1650 UnitFileScope scope,
1652 const char *root_dir,
1654 UnitFileChange **changes,
1655 unsigned *n_changes) {
1657 _cleanup_lookup_paths_free_ LookupPaths paths = {};
1658 _cleanup_(install_context_done) InstallContext c = {};
1660 _cleanup_free_ char *config_path = NULL;
1661 _cleanup_set_free_free_ Set *remove_symlinks_to = NULL;
1665 assert(scope < _UNIT_FILE_SCOPE_MAX);
1667 r = lookup_paths_init_from_scope(&paths, scope, root_dir);
1671 r = get_config_path(scope, runtime, root_dir, &config_path);
1675 STRV_FOREACH(i, files) {
1676 r = install_info_add_auto(&c, *i);
1681 r = install_context_mark_for_removal(&c, &paths, &remove_symlinks_to, config_path, root_dir);
1683 q = remove_marked_symlinks(remove_symlinks_to, config_path, changes, n_changes, files);
1690 int unit_file_reenable(
1691 UnitFileScope scope,
1693 const char *root_dir,
1696 UnitFileChange **changes,
1697 unsigned *n_changes) {
1700 r = unit_file_disable(scope, runtime, root_dir, files,
1701 changes, n_changes);
1705 return unit_file_enable(scope, runtime, root_dir, files, force,
1706 changes, n_changes);
1709 int unit_file_set_default(
1710 UnitFileScope scope,
1711 const char *root_dir,
1714 UnitFileChange **changes,
1715 unsigned *n_changes) {
1717 _cleanup_lookup_paths_free_ LookupPaths paths = {};
1718 _cleanup_(install_context_done) InstallContext c = {};
1719 _cleanup_free_ char *config_path = NULL;
1722 InstallInfo *i = NULL;
1725 assert(scope < _UNIT_FILE_SCOPE_MAX);
1728 if (unit_name_to_type(file) != UNIT_TARGET)
1731 r = lookup_paths_init_from_scope(&paths, scope, root_dir);
1735 r = get_config_path(scope, false, root_dir, &config_path);
1739 r = install_info_add_auto(&c, file);
1743 assert_se(i = ordered_hashmap_first(c.will_install));
1745 r = unit_file_search(&c, i, &paths, root_dir, false, true, NULL);
1749 path = strappenda(config_path, "/" SPECIAL_DEFAULT_TARGET);
1751 r = create_symlink(i->path, path, force, changes, n_changes);
1758 int unit_file_get_default(
1759 UnitFileScope scope,
1760 const char *root_dir,
1763 _cleanup_lookup_paths_free_ LookupPaths paths = {};
1768 assert(scope < _UNIT_FILE_SCOPE_MAX);
1771 r = lookup_paths_init_from_scope(&paths, scope, root_dir);
1775 STRV_FOREACH(p, paths.unit_path) {
1776 _cleanup_free_ char *path = NULL, *tmp = NULL;
1779 path = path_join(root_dir, *p, SPECIAL_DEFAULT_TARGET);
1783 r = readlink_malloc(path, &tmp);
1786 else if (r == -EINVAL)
1788 n = strdup(SPECIAL_DEFAULT_TARGET);
1792 n = strdup(basename(tmp));
1804 UnitFileState unit_file_get_state(
1805 UnitFileScope scope,
1806 const char *root_dir,
1809 _cleanup_lookup_paths_free_ LookupPaths paths = {};
1810 UnitFileState state = _UNIT_FILE_STATE_INVALID;
1812 _cleanup_free_ char *path = NULL;
1816 assert(scope < _UNIT_FILE_SCOPE_MAX);
1819 if (root_dir && scope != UNIT_FILE_SYSTEM)
1822 if (!unit_name_is_valid(name, TEMPLATE_VALID))
1825 r = lookup_paths_init_from_scope(&paths, scope, root_dir);
1829 STRV_FOREACH(i, paths.unit_path) {
1837 path = path_join(root_dir, *i, name);
1842 partial = path + strlen(root_dir);
1847 * Search for a unit file in our default paths, to
1848 * be sure, that there are no broken symlinks.
1850 if (lstat(path, &st) < 0) {
1852 if (errno != ENOENT)
1855 if (!unit_name_is_instance(name))
1858 if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode))
1861 r = null_or_empty_path(path);
1862 if (r < 0 && r != -ENOENT)
1865 state = path_startswith(*i, "/run") ?
1866 UNIT_FILE_MASKED_RUNTIME : UNIT_FILE_MASKED;
1871 r = find_symlinks_in_scope(scope, root_dir, name, &state);
1877 r = unit_file_can_install(&paths, root_dir, partial, true, &also);
1878 if (r < 0 && errno != ENOENT)
1881 return UNIT_FILE_DISABLED;
1884 return UNIT_FILE_INDIRECT;
1885 return UNIT_FILE_STATIC;
1889 return r < 0 ? r : state;
1892 int unit_file_query_preset(UnitFileScope scope, const char *root_dir, const char *name) {
1893 _cleanup_strv_free_ char **files = NULL;
1898 assert(scope < _UNIT_FILE_SCOPE_MAX);
1901 if (scope == UNIT_FILE_SYSTEM)
1902 r = conf_files_list(&files, ".preset", root_dir,
1903 "/etc/systemd/system-preset",
1904 "/usr/local/lib/systemd/system-preset",
1905 "/usr/lib/systemd/system-preset",
1906 #ifdef HAVE_SPLIT_USR
1907 "/lib/systemd/system-preset",
1910 else if (scope == UNIT_FILE_GLOBAL)
1911 r = conf_files_list(&files, ".preset", root_dir,
1912 "/etc/systemd/user-preset",
1913 "/usr/local/lib/systemd/user-preset",
1914 "/usr/lib/systemd/user-preset",
1922 STRV_FOREACH(p, files) {
1923 _cleanup_fclose_ FILE *f;
1925 f = fopen(*p, "re");
1927 if (errno == ENOENT)
1934 char line[LINE_MAX], *l;
1936 if (!fgets(line, sizeof(line), f))
1943 if (strchr(COMMENTS "\n", *l))
1946 if (first_word(l, "enable")) {
1948 l += strspn(l, WHITESPACE);
1950 if (fnmatch(l, name, FNM_NOESCAPE) == 0) {
1951 log_debug("Preset file says enable %s.", name);
1955 } else if (first_word(l, "disable")) {
1957 l += strspn(l, WHITESPACE);
1959 if (fnmatch(l, name, FNM_NOESCAPE) == 0) {
1960 log_debug("Preset file says disable %s.", name);
1965 log_debug("Couldn't parse line '%s'", l);
1969 /* Default is "enable" */
1970 log_debug("Preset file doesn't say anything about %s, enabling.", name);
1974 int unit_file_preset(
1975 UnitFileScope scope,
1977 const char *root_dir,
1979 UnitFilePresetMode mode,
1981 UnitFileChange **changes,
1982 unsigned *n_changes) {
1984 _cleanup_(install_context_done) InstallContext plus = {}, minus = {};
1985 _cleanup_lookup_paths_free_ LookupPaths paths = {};
1986 _cleanup_free_ char *config_path = NULL;
1991 assert(scope < _UNIT_FILE_SCOPE_MAX);
1992 assert(mode < _UNIT_FILE_PRESET_MAX);
1994 r = lookup_paths_init_from_scope(&paths, scope, root_dir);
1998 r = get_config_path(scope, runtime, root_dir, &config_path);
2002 STRV_FOREACH(i, files) {
2004 if (!unit_name_is_valid(*i, TEMPLATE_VALID))
2007 r = unit_file_query_preset(scope, root_dir, *i);
2011 if (r && mode != UNIT_FILE_PRESET_DISABLE_ONLY)
2012 r = install_info_add_auto(&plus, *i);
2013 else if (!r && mode != UNIT_FILE_PRESET_ENABLE_ONLY)
2014 r = install_info_add_auto(&minus, *i);
2023 if (mode != UNIT_FILE_PRESET_ENABLE_ONLY) {
2024 _cleanup_set_free_free_ Set *remove_symlinks_to = NULL;
2026 r = install_context_mark_for_removal(&minus, &paths, &remove_symlinks_to, config_path, root_dir);
2028 q = remove_marked_symlinks(remove_symlinks_to, config_path, changes, n_changes, files);
2033 if (mode != UNIT_FILE_PRESET_DISABLE_ONLY) {
2034 /* Returns number of symlinks that where supposed to be installed. */
2035 q = install_context_apply(&plus, &paths, config_path, root_dir, force, changes, n_changes);
2043 int unit_file_preset_all(
2044 UnitFileScope scope,
2046 const char *root_dir,
2047 UnitFilePresetMode mode,
2049 UnitFileChange **changes,
2050 unsigned *n_changes) {
2052 _cleanup_(install_context_done) InstallContext plus = {}, minus = {};
2053 _cleanup_lookup_paths_free_ LookupPaths paths = {};
2054 _cleanup_free_ char *config_path = NULL;
2059 assert(scope < _UNIT_FILE_SCOPE_MAX);
2060 assert(mode < _UNIT_FILE_PRESET_MAX);
2062 r = lookup_paths_init_from_scope(&paths, scope, root_dir);
2066 r = get_config_path(scope, runtime, root_dir, &config_path);
2070 STRV_FOREACH(i, paths.unit_path) {
2071 _cleanup_closedir_ DIR *d = NULL;
2072 _cleanup_free_ char *units_dir;
2074 units_dir = path_join(root_dir, *i, NULL);
2078 d = opendir(units_dir);
2080 if (errno == ENOENT)
2091 if (!de && errno != 0)
2097 if (hidden_file(de->d_name))
2100 if (!unit_name_is_valid(de->d_name, TEMPLATE_VALID))
2103 dirent_ensure_type(d, de);
2105 if (de->d_type != DT_REG)
2108 r = unit_file_query_preset(scope, root_dir, de->d_name);
2112 if (r && mode != UNIT_FILE_PRESET_DISABLE_ONLY)
2113 r = install_info_add_auto(&plus, de->d_name);
2114 else if (!r && mode != UNIT_FILE_PRESET_ENABLE_ONLY)
2115 r = install_info_add_auto(&minus, de->d_name);
2125 if (mode != UNIT_FILE_PRESET_ENABLE_ONLY) {
2126 _cleanup_set_free_free_ Set *remove_symlinks_to = NULL;
2128 r = install_context_mark_for_removal(&minus, &paths, &remove_symlinks_to, config_path, root_dir);
2130 q = remove_marked_symlinks(remove_symlinks_to, config_path, changes, n_changes, NULL);
2135 if (mode != UNIT_FILE_PRESET_DISABLE_ONLY) {
2136 q = install_context_apply(&plus, &paths, config_path, root_dir, force, changes, n_changes);
2144 static void unit_file_list_free_one(UnitFileList *f) {
2152 DEFINE_TRIVIAL_CLEANUP_FUNC(UnitFileList*, unit_file_list_free_one);
2154 int unit_file_get_list(
2155 UnitFileScope scope,
2156 const char *root_dir,
2159 _cleanup_lookup_paths_free_ LookupPaths paths = {};
2164 assert(scope < _UNIT_FILE_SCOPE_MAX);
2167 if (root_dir && scope != UNIT_FILE_SYSTEM)
2171 r = access(root_dir, F_OK);
2176 r = lookup_paths_init_from_scope(&paths, scope, root_dir);
2180 STRV_FOREACH(i, paths.unit_path) {
2181 _cleanup_closedir_ DIR *d = NULL;
2182 _cleanup_free_ char *units_dir;
2184 units_dir = path_join(root_dir, *i, NULL);
2188 d = opendir(units_dir);
2190 if (errno == ENOENT)
2197 _cleanup_(unit_file_list_free_onep) UnitFileList *f = NULL;
2199 _cleanup_free_ char *path = NULL;
2203 if (!de && errno != 0)
2209 if (hidden_file(de->d_name))
2212 if (!unit_name_is_valid(de->d_name, TEMPLATE_VALID))
2215 if (hashmap_get(h, de->d_name))
2218 dirent_ensure_type(d, de);
2220 if (!IN_SET(de->d_type, DT_LNK, DT_REG))
2223 f = new0(UnitFileList, 1);
2227 f->path = path_make_absolute(de->d_name, units_dir);
2231 r = null_or_empty_path(f->path);
2232 if (r < 0 && r != -ENOENT)
2236 path_startswith(*i, "/run") ?
2237 UNIT_FILE_MASKED_RUNTIME : UNIT_FILE_MASKED;
2241 r = find_symlinks_in_scope(scope, root_dir, de->d_name, &f->state);
2245 f->state = UNIT_FILE_ENABLED;
2249 path = path_make_absolute(de->d_name, *i);
2253 r = unit_file_can_install(&paths, root_dir, path, true, NULL);
2254 if (r == -EINVAL || /* Invalid setting? */
2255 r == -EBADMSG || /* Invalid format? */
2256 r == -ENOENT /* Included file not found? */)
2257 f->state = UNIT_FILE_INVALID;
2261 f->state = UNIT_FILE_DISABLED;
2263 f->state = UNIT_FILE_STATIC;
2266 r = hashmap_put(h, basename(f->path), f);
2269 f = NULL; /* prevent cleanup */
2276 static const char* const unit_file_state_table[_UNIT_FILE_STATE_MAX] = {
2277 [UNIT_FILE_ENABLED] = "enabled",
2278 [UNIT_FILE_ENABLED_RUNTIME] = "enabled-runtime",
2279 [UNIT_FILE_LINKED] = "linked",
2280 [UNIT_FILE_LINKED_RUNTIME] = "linked-runtime",
2281 [UNIT_FILE_MASKED] = "masked",
2282 [UNIT_FILE_MASKED_RUNTIME] = "masked-runtime",
2283 [UNIT_FILE_STATIC] = "static",
2284 [UNIT_FILE_DISABLED] = "disabled",
2285 [UNIT_FILE_INDIRECT] = "indirect",
2286 [UNIT_FILE_INVALID] = "invalid",
2289 DEFINE_STRING_TABLE_LOOKUP(unit_file_state, UnitFileState);
2291 static const char* const unit_file_change_type_table[_UNIT_FILE_CHANGE_TYPE_MAX] = {
2292 [UNIT_FILE_SYMLINK] = "symlink",
2293 [UNIT_FILE_UNLINK] = "unlink",
2296 DEFINE_STRING_TABLE_LOOKUP(unit_file_change_type, UnitFileChangeType);
2298 static const char* const unit_file_preset_mode_table[_UNIT_FILE_PRESET_MAX] = {
2299 [UNIT_FILE_PRESET_FULL] = "full",
2300 [UNIT_FILE_PRESET_ENABLE_ONLY] = "enable-only",
2301 [UNIT_FILE_PRESET_DISABLE_ONLY] = "disable-only",
2304 DEFINE_STRING_TABLE_LOOKUP(unit_file_preset_mode, UnitFilePresetMode);