chiark / gitweb /
install: introduce new DefaultInstance= field for [Install] sections
[elogind.git] / src / shared / install.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2011 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <fnmatch.h>
27
28 #include "util.h"
29 #include "mkdir.h"
30 #include "hashmap.h"
31 #include "set.h"
32 #include "path-util.h"
33 #include "path-lookup.h"
34 #include "strv.h"
35 #include "unit-name.h"
36 #include "install.h"
37 #include "conf-parser.h"
38 #include "conf-files.h"
39 #include "specifier.h"
40 #include "install-printf.h"
41 #include "special.h"
42
43 typedef struct {
44         Hashmap *will_install;
45         Hashmap *have_installed;
46 } InstallContext;
47
48 #define _cleanup_install_context_done_ _cleanup_(install_context_done)
49
50 static int lookup_paths_init_from_scope(LookupPaths *paths,
51                                         UnitFileScope scope,
52                                         const char *root_dir) {
53         assert(paths);
54         assert(scope >= 0);
55         assert(scope < _UNIT_FILE_SCOPE_MAX);
56
57         zero(*paths);
58
59         return lookup_paths_init(paths,
60                                  scope == UNIT_FILE_SYSTEM ? SYSTEMD_SYSTEM : SYSTEMD_USER,
61                                  scope == UNIT_FILE_USER,
62                                  root_dir,
63                                  NULL, NULL, NULL);
64 }
65
66 static int get_config_path(UnitFileScope scope, bool runtime, const char *root_dir, char **ret) {
67         char *p = NULL;
68         int r;
69
70         assert(scope >= 0);
71         assert(scope < _UNIT_FILE_SCOPE_MAX);
72         assert(ret);
73
74         switch (scope) {
75
76         case UNIT_FILE_SYSTEM:
77
78                 if (root_dir && runtime)
79                         asprintf(&p, "%s/run/systemd/system", root_dir);
80                 else if (runtime)
81                         p = strdup("/run/systemd/system");
82                 else if (root_dir)
83                         asprintf(&p, "%s/%s", root_dir, SYSTEM_CONFIG_UNIT_PATH);
84                 else
85                         p = strdup(SYSTEM_CONFIG_UNIT_PATH);
86
87                 break;
88
89         case UNIT_FILE_GLOBAL:
90
91                 if (root_dir)
92                         return -EINVAL;
93
94                 if (runtime)
95                         p = strdup("/run/systemd/user");
96                 else
97                         p = strdup(USER_CONFIG_UNIT_PATH);
98                 break;
99
100         case UNIT_FILE_USER:
101
102                 if (root_dir || runtime)
103                         return -EINVAL;
104
105                 r = user_config_home(&p);
106                 if (r <= 0)
107                         return r < 0 ? r : -ENOENT;
108
109                 break;
110
111         default:
112                 assert_not_reached("Bad scope");
113         }
114
115         if (!p)
116                 return -ENOMEM;
117
118         *ret = p;
119         return 0;
120 }
121
122 static int add_file_change(
123                 UnitFileChange **changes,
124                 unsigned *n_changes,
125                 UnitFileChangeType type,
126                 const char *path,
127                 const char *source) {
128
129         UnitFileChange *c;
130         unsigned i;
131
132         assert(path);
133         assert(!changes == !n_changes);
134
135         if (!changes)
136                 return 0;
137
138         c = realloc(*changes, (*n_changes + 1) * sizeof(UnitFileChange));
139         if (!c)
140                 return -ENOMEM;
141
142         *changes = c;
143         i = *n_changes;
144
145         c[i].type = type;
146         c[i].path = strdup(path);
147         if (!c[i].path)
148                 return -ENOMEM;
149
150         if (source) {
151                 c[i].source = strdup(source);
152                 if (!c[i].source) {
153                         free(c[i].path);
154                         return -ENOMEM;
155                 }
156         } else
157                 c[i].source = NULL;
158
159         *n_changes = i+1;
160         return 0;
161 }
162
163 static int mark_symlink_for_removal(
164                 Set **remove_symlinks_to,
165                 const char *p) {
166
167         char *n;
168         int r;
169
170         assert(p);
171
172         r = set_ensure_allocated(remove_symlinks_to, string_hash_func, string_compare_func);
173         if (r < 0)
174                 return r;
175
176         n = strdup(p);
177         if (!n)
178                 return -ENOMEM;
179
180         path_kill_slashes(n);
181
182         r = set_consume(*remove_symlinks_to, n);
183         if (r < 0)
184                 return r == -EEXIST ? 0 : r;
185
186         return 0;
187 }
188
189 static int remove_marked_symlinks_fd(
190                 Set *remove_symlinks_to,
191                 int fd,
192                 const char *path,
193                 const char *config_path,
194                 bool *deleted,
195                 UnitFileChange **changes,
196                 unsigned *n_changes,
197                 char** instance_whitelist) {
198
199         _cleanup_closedir_ DIR *d = NULL;
200         int r = 0;
201
202         assert(remove_symlinks_to);
203         assert(fd >= 0);
204         assert(path);
205         assert(config_path);
206         assert(deleted);
207
208         d = fdopendir(fd);
209         if (!d) {
210                 safe_close(fd);
211                 return -errno;
212         }
213
214         rewinddir(d);
215
216         for (;;) {
217                 struct dirent *de;
218
219                 errno = 0;
220                 de = readdir(d);
221                 if (!de && errno != 0) {
222                         r = -errno;
223                         break;
224                 }
225
226                 if (!de)
227                         break;
228
229                 if (ignore_file(de->d_name))
230                         continue;
231
232                 dirent_ensure_type(d, de);
233
234                 if (de->d_type == DT_DIR) {
235                         int nfd, q;
236                         _cleanup_free_ char *p = NULL;
237
238                         nfd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
239                         if (nfd < 0) {
240                                 if (errno == ENOENT)
241                                         continue;
242
243                                 if (r == 0)
244                                         r = -errno;
245                                 continue;
246                         }
247
248                         p = path_make_absolute(de->d_name, path);
249                         if (!p) {
250                                 safe_close(nfd);
251                                 return -ENOMEM;
252                         }
253
254                         /* This will close nfd, regardless whether it succeeds or not */
255                         q = remove_marked_symlinks_fd(remove_symlinks_to, nfd, p, config_path, deleted, changes, n_changes, instance_whitelist);
256                         if (q < 0 && r == 0)
257                                 r = q;
258
259                 } else if (de->d_type == DT_LNK) {
260                         _cleanup_free_ char *p = NULL, *dest = NULL;
261                         int q;
262                         bool found;
263
264                         if (!unit_name_is_valid(de->d_name, TEMPLATE_VALID))
265                                 continue;
266
267                         if (unit_name_is_instance(de->d_name) &&
268                             instance_whitelist &&
269                             !strv_contains(instance_whitelist, de->d_name))
270                                 continue;
271
272                         p = path_make_absolute(de->d_name, path);
273                         if (!p)
274                                 return -ENOMEM;
275
276                         q = readlink_and_canonicalize(p, &dest);
277                         if (q < 0) {
278                                 if (q == -ENOENT)
279                                         continue;
280
281                                 if (r == 0)
282                                         r = q;
283                                 continue;
284                         }
285
286                         found =
287                                 set_get(remove_symlinks_to, dest) ||
288                                 set_get(remove_symlinks_to, basename(dest));
289
290                         if (!found)
291                                 continue;
292
293
294                         if (unlink(p) < 0 && errno != ENOENT) {
295
296                                 if (r == 0)
297                                         r = -errno;
298                                 continue;
299                         }
300
301                         rmdir_parents(p, config_path);
302
303                         path_kill_slashes(p);
304
305                         add_file_change(changes, n_changes, UNIT_FILE_UNLINK, p, NULL);
306
307                         if (!set_get(remove_symlinks_to, p)) {
308
309                                 q = mark_symlink_for_removal(&remove_symlinks_to, p);
310                                 if (q < 0) {
311                                         if (r == 0)
312                                                 r = q;
313                                 } else
314                                         *deleted = true;
315                         }
316                 }
317         }
318
319         return r;
320 }
321
322 static int remove_marked_symlinks(
323                 Set *remove_symlinks_to,
324                 const char *config_path,
325                 UnitFileChange **changes,
326                 unsigned *n_changes,
327                 char** instance_whitelist) {
328
329         _cleanup_close_ int fd = -1;
330         int r = 0;
331         bool deleted;
332
333         assert(config_path);
334
335         if (set_size(remove_symlinks_to) <= 0)
336                 return 0;
337
338         fd = open(config_path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
339         if (fd < 0)
340                 return -errno;
341
342         do {
343                 int q, cfd;
344                 deleted = false;
345
346                 cfd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
347                 if (cfd < 0) {
348                         r = -errno;
349                         break;
350                 }
351
352                 /* This takes possession of cfd and closes it */
353                 q = remove_marked_symlinks_fd(remove_symlinks_to, cfd, config_path, config_path, &deleted, changes, n_changes, instance_whitelist);
354                 if (r == 0)
355                         r = q;
356         } while (deleted);
357
358         return r;
359 }
360
361 static int find_symlinks_fd(
362                 const char *name,
363                 int fd,
364                 const char *path,
365                 const char *config_path,
366                 bool *same_name_link) {
367
368         int r = 0;
369         _cleanup_closedir_ DIR *d = NULL;
370
371         assert(name);
372         assert(fd >= 0);
373         assert(path);
374         assert(config_path);
375         assert(same_name_link);
376
377         d = fdopendir(fd);
378         if (!d) {
379                 safe_close(fd);
380                 return -errno;
381         }
382
383         for (;;) {
384                 struct dirent *de;
385
386                 errno = 0;
387                 de = readdir(d);
388                 if (!de && errno != 0)
389                         return -errno;
390
391                 if (!de)
392                         return r;
393
394                 if (ignore_file(de->d_name))
395                         continue;
396
397                 dirent_ensure_type(d, de);
398
399                 if (de->d_type == DT_DIR) {
400                         int nfd, q;
401                         _cleanup_free_ char *p = NULL;
402
403                         nfd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
404                         if (nfd < 0) {
405                                 if (errno == ENOENT)
406                                         continue;
407
408                                 if (r == 0)
409                                         r = -errno;
410                                 continue;
411                         }
412
413                         p = path_make_absolute(de->d_name, path);
414                         if (!p) {
415                                 safe_close(nfd);
416                                 return -ENOMEM;
417                         }
418
419                         /* This will close nfd, regardless whether it succeeds or not */
420                         q = find_symlinks_fd(name, nfd, p, config_path, same_name_link);
421                         if (q > 0)
422                                 return 1;
423                         if (r == 0)
424                                 r = q;
425
426                 } else if (de->d_type == DT_LNK) {
427                         _cleanup_free_ char *p = NULL, *dest = NULL;
428                         bool found_path, found_dest, b = false;
429                         int q;
430
431                         /* Acquire symlink name */
432                         p = path_make_absolute(de->d_name, path);
433                         if (!p)
434                                 return -ENOMEM;
435
436                         /* Acquire symlink destination */
437                         q = readlink_and_canonicalize(p, &dest);
438                         if (q < 0) {
439                                 if (q == -ENOENT)
440                                         continue;
441
442                                 if (r == 0)
443                                         r = q;
444                                 continue;
445                         }
446
447                         /* Check if the symlink itself matches what we
448                          * are looking for */
449                         if (path_is_absolute(name))
450                                 found_path = path_equal(p, name);
451                         else
452                                 found_path = streq(de->d_name, name);
453
454                         /* Check if what the symlink points to
455                          * matches what we are looking for */
456                         if (path_is_absolute(name))
457                                 found_dest = path_equal(dest, name);
458                         else
459                                 found_dest = streq(basename(dest), name);
460
461                         if (found_path && found_dest) {
462                                 _cleanup_free_ char *t = NULL;
463
464                                 /* Filter out same name links in the main
465                                  * config path */
466                                 t = path_make_absolute(name, config_path);
467                                 if (!t)
468                                         return -ENOMEM;
469
470                                 b = path_equal(t, p);
471                         }
472
473                         if (b)
474                                 *same_name_link = true;
475                         else if (found_path || found_dest)
476                                 return 1;
477                 }
478         }
479 }
480
481 static int find_symlinks(
482                 const char *name,
483                 const char *config_path,
484                 bool *same_name_link) {
485
486         int fd;
487
488         assert(name);
489         assert(config_path);
490         assert(same_name_link);
491
492         fd = open(config_path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
493         if (fd < 0) {
494                 if (errno == ENOENT)
495                         return 0;
496                 return -errno;
497         }
498
499         /* This takes possession of fd and closes it */
500         return find_symlinks_fd(name, fd, config_path, config_path, same_name_link);
501 }
502
503 static int find_symlinks_in_scope(
504                 UnitFileScope scope,
505                 const char *root_dir,
506                 const char *name,
507                 UnitFileState *state) {
508
509         int r;
510         _cleanup_free_ char *path2 = NULL;
511         bool same_name_link_runtime = false, same_name_link = false;
512
513         assert(scope >= 0);
514         assert(scope < _UNIT_FILE_SCOPE_MAX);
515         assert(name);
516
517         if (scope == UNIT_FILE_SYSTEM || scope == UNIT_FILE_GLOBAL) {
518                 _cleanup_free_ char *path = NULL;
519
520                 /* First look in runtime config path */
521                 r = get_config_path(scope, true, root_dir, &path);
522                 if (r < 0)
523                         return r;
524
525                 r = find_symlinks(name, path, &same_name_link_runtime);
526                 if (r < 0)
527                         return r;
528                 else if (r > 0) {
529                         *state = UNIT_FILE_ENABLED_RUNTIME;
530                         return r;
531                 }
532         }
533
534         /* Then look in the normal config path */
535         r = get_config_path(scope, false, root_dir, &path2);
536         if (r < 0)
537                 return r;
538
539         r = find_symlinks(name, path2, &same_name_link);
540         if (r < 0)
541                 return r;
542         else if (r > 0) {
543                 *state = UNIT_FILE_ENABLED;
544                 return r;
545         }
546
547         /* Hmm, we didn't find it, but maybe we found the same name
548          * link? */
549         if (same_name_link_runtime) {
550                 *state = UNIT_FILE_LINKED_RUNTIME;
551                 return 1;
552         } else if (same_name_link) {
553                 *state = UNIT_FILE_LINKED;
554                 return 1;
555         }
556
557         return 0;
558 }
559
560 int unit_file_mask(
561                 UnitFileScope scope,
562                 bool runtime,
563                 const char *root_dir,
564                 char **files,
565                 bool force,
566                 UnitFileChange **changes,
567                 unsigned *n_changes) {
568
569         char **i;
570         _cleanup_free_ char *prefix = NULL;
571         int r;
572
573         assert(scope >= 0);
574         assert(scope < _UNIT_FILE_SCOPE_MAX);
575
576         r = get_config_path(scope, runtime, root_dir, &prefix);
577         if (r < 0)
578                 return r;
579
580         STRV_FOREACH(i, files) {
581                 _cleanup_free_ char *path = NULL;
582
583                 if (!unit_name_is_valid(*i, TEMPLATE_VALID)) {
584                         if (r == 0)
585                                 r = -EINVAL;
586                         continue;
587                 }
588
589                 path = path_make_absolute(*i, prefix);
590                 if (!path) {
591                         r = -ENOMEM;
592                         break;
593                 }
594
595                 if (symlink("/dev/null", path) >= 0) {
596                         add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, path, "/dev/null");
597                         continue;
598                 }
599
600                 if (errno == EEXIST) {
601
602                         if (null_or_empty_path(path) > 0)
603                                 continue;
604
605                         if (force) {
606                                 if (symlink_atomic("/dev/null", path) >= 0) {
607                                         add_file_change(changes, n_changes, UNIT_FILE_UNLINK, path, NULL);
608                                         add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, path, "/dev/null");
609                                         continue;
610                                 }
611                         }
612
613                         if (r == 0)
614                                 r = -EEXIST;
615                 } else {
616                         if (r == 0)
617                                 r = -errno;
618                 }
619         }
620
621         return r;
622 }
623
624 int unit_file_unmask(
625                 UnitFileScope scope,
626                 bool runtime,
627                 const char *root_dir,
628                 char **files,
629                 UnitFileChange **changes,
630                 unsigned *n_changes) {
631
632         char **i, *config_path = NULL;
633         int r, q;
634         Set *remove_symlinks_to = NULL;
635
636         assert(scope >= 0);
637         assert(scope < _UNIT_FILE_SCOPE_MAX);
638
639         r = get_config_path(scope, runtime, root_dir, &config_path);
640         if (r < 0)
641                 goto finish;
642
643         STRV_FOREACH(i, files) {
644                 char *path;
645
646                 if (!unit_name_is_valid(*i, TEMPLATE_VALID)) {
647                         if (r == 0)
648                                 r = -EINVAL;
649                         continue;
650                 }
651
652                 path = path_make_absolute(*i, config_path);
653                 if (!path) {
654                         r = -ENOMEM;
655                         break;
656                 }
657
658                 q = null_or_empty_path(path);
659                 if (q > 0) {
660                         if (unlink(path) >= 0) {
661                                 mark_symlink_for_removal(&remove_symlinks_to, path);
662                                 add_file_change(changes, n_changes, UNIT_FILE_UNLINK, path, NULL);
663
664                                 free(path);
665                                 continue;
666                         }
667
668                         q = -errno;
669                 }
670
671                 if (q != -ENOENT && r == 0)
672                         r = q;
673
674                 free(path);
675         }
676
677
678 finish:
679         q = remove_marked_symlinks(remove_symlinks_to, config_path, changes, n_changes, files);
680         if (r == 0)
681                 r = q;
682
683         set_free_free(remove_symlinks_to);
684         free(config_path);
685
686         return r;
687 }
688
689 int unit_file_link(
690                 UnitFileScope scope,
691                 bool runtime,
692                 const char *root_dir,
693                 char **files,
694                 bool force,
695                 UnitFileChange **changes,
696                 unsigned *n_changes) {
697
698         _cleanup_lookup_paths_free_ LookupPaths paths = {};
699         char **i;
700         _cleanup_free_ char *config_path = NULL;
701         int r, q;
702
703         assert(scope >= 0);
704         assert(scope < _UNIT_FILE_SCOPE_MAX);
705
706         r = lookup_paths_init_from_scope(&paths, scope, root_dir);
707         if (r < 0)
708                 return r;
709
710         r = get_config_path(scope, runtime, root_dir, &config_path);
711         if (r < 0)
712                 return r;
713
714         STRV_FOREACH(i, files) {
715                 _cleanup_free_ char *path = NULL;
716                 char *fn;
717                 struct stat st;
718
719                 fn = basename(*i);
720
721                 if (!path_is_absolute(*i) ||
722                     !unit_name_is_valid(fn, TEMPLATE_VALID)) {
723                         if (r == 0)
724                                 r = -EINVAL;
725                         continue;
726                 }
727
728                 if (lstat(*i, &st) < 0) {
729                         if (r == 0)
730                                 r = -errno;
731                         continue;
732                 }
733
734                 if (!S_ISREG(st.st_mode)) {
735                         r = -ENOENT;
736                         continue;
737                 }
738
739                 q = in_search_path(*i, paths.unit_path);
740                 if (q < 0)
741                         return q;
742
743                 if (q > 0)
744                         continue;
745
746                 path = path_make_absolute(fn, config_path);
747                 if (!path)
748                         return -ENOMEM;
749
750                 if (symlink(*i, path) >= 0) {
751                         add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, path, *i);
752                         continue;
753                 }
754
755                 if (errno == EEXIST) {
756                         _cleanup_free_ char *dest = NULL;
757
758                         q = readlink_and_make_absolute(path, &dest);
759                         if (q < 0 && errno != ENOENT) {
760                                 if (r == 0)
761                                         r = q;
762                                 continue;
763                         }
764
765                         if (q >= 0 && path_equal(dest, *i))
766                                 continue;
767
768                         if (force) {
769                                 if (symlink_atomic(*i, path) >= 0) {
770                                         add_file_change(changes, n_changes, UNIT_FILE_UNLINK, path, NULL);
771                                         add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, path, *i);
772                                         continue;
773                                 }
774                         }
775
776                         if (r == 0)
777                                 r = -EEXIST;
778                 } else {
779                         if (r == 0)
780                                 r = -errno;
781                 }
782         }
783
784         return r;
785 }
786
787 void unit_file_list_free(Hashmap *h) {
788         UnitFileList *i;
789
790         while ((i = hashmap_steal_first(h))) {
791                 free(i->path);
792                 free(i);
793         }
794
795         hashmap_free(h);
796 }
797
798 void unit_file_changes_free(UnitFileChange *changes, unsigned n_changes) {
799         unsigned i;
800
801         assert(changes || n_changes == 0);
802
803         if (!changes)
804                 return;
805
806         for (i = 0; i < n_changes; i++) {
807                 free(changes[i].path);
808                 free(changes[i].source);
809         }
810
811         free(changes);
812 }
813
814 static void install_info_free(InstallInfo *i) {
815         assert(i);
816
817         free(i->name);
818         free(i->path);
819         strv_free(i->aliases);
820         strv_free(i->wanted_by);
821         strv_free(i->required_by);
822         free(i->default_instance);
823         free(i);
824 }
825
826 static void install_info_hashmap_free(Hashmap *m) {
827         InstallInfo *i;
828
829         if (!m)
830                 return;
831
832         while ((i = hashmap_steal_first(m)))
833                 install_info_free(i);
834
835         hashmap_free(m);
836 }
837
838 static void install_context_done(InstallContext *c) {
839         assert(c);
840
841         install_info_hashmap_free(c->will_install);
842         install_info_hashmap_free(c->have_installed);
843
844         c->will_install = c->have_installed = NULL;
845 }
846
847 static int install_info_add(
848                 InstallContext *c,
849                 const char *name,
850                 const char *path) {
851         InstallInfo *i = NULL;
852         int r;
853
854         assert(c);
855         assert(name || path);
856
857         if (!name)
858                 name = basename(path);
859
860         if (!unit_name_is_valid(name, TEMPLATE_VALID))
861                 return -EINVAL;
862
863         if (hashmap_get(c->have_installed, name) ||
864             hashmap_get(c->will_install, name))
865                 return 0;
866
867         r = hashmap_ensure_allocated(&c->will_install, string_hash_func, string_compare_func);
868         if (r < 0)
869                 return r;
870
871         i = new0(InstallInfo, 1);
872         if (!i)
873                 return -ENOMEM;
874
875         i->name = strdup(name);
876         if (!i->name) {
877                 r = -ENOMEM;
878                 goto fail;
879         }
880
881         if (path) {
882                 i->path = strdup(path);
883                 if (!i->path) {
884                         r = -ENOMEM;
885                         goto fail;
886                 }
887         }
888
889         r = hashmap_put(c->will_install, i->name, i);
890         if (r < 0)
891                 goto fail;
892
893         return 0;
894
895 fail:
896         if (i)
897                 install_info_free(i);
898
899         return r;
900 }
901
902 static int install_info_add_auto(
903                 InstallContext *c,
904                 const char *name_or_path) {
905
906         assert(c);
907         assert(name_or_path);
908
909         if (path_is_absolute(name_or_path))
910                 return install_info_add(c, NULL, name_or_path);
911         else
912                 return install_info_add(c, name_or_path, NULL);
913 }
914
915 static int config_parse_also(
916                 const char *unit,
917                 const char *filename,
918                 unsigned line,
919                 const char *section,
920                 unsigned section_line,
921                 const char *lvalue,
922                 int ltype,
923                 const char *rvalue,
924                 void *data,
925                 void *userdata) {
926
927         char *w;
928         size_t l;
929         char *state;
930         InstallContext *c = data;
931
932         assert(filename);
933         assert(lvalue);
934         assert(rvalue);
935
936         FOREACH_WORD_QUOTED(w, l, rvalue, state) {
937                 _cleanup_free_ char *n;
938                 int r;
939
940                 n = strndup(w, l);
941                 if (!n)
942                         return -ENOMEM;
943
944                 r = install_info_add(c, n, NULL);
945                 if (r < 0)
946                         return r;
947         }
948
949         return 0;
950 }
951
952 static int config_parse_user(
953                 const char *unit,
954                 const char *filename,
955                 unsigned line,
956                 const char *section,
957                 unsigned section_line,
958                 const char *lvalue,
959                 int ltype,
960                 const char *rvalue,
961                 void *data,
962                 void *userdata) {
963
964         InstallInfo *i = data;
965         char *printed;
966         int r;
967
968         assert(filename);
969         assert(lvalue);
970         assert(rvalue);
971
972         r = install_full_printf(i, rvalue, &printed);
973         if (r < 0)
974                 return r;
975
976         free(i->user);
977         i->user = printed;
978
979         return 0;
980 }
981
982 static int config_parse_default_instance(
983                 const char *unit,
984                 const char *filename,
985                 unsigned line,
986                 const char *section,
987                 unsigned section_line,
988                 const char *lvalue,
989                 int ltype,
990                 const char *rvalue,
991                 void *data,
992                 void *userdata) {
993
994         InstallInfo *i = data;
995         char *printed;
996         int r;
997
998         assert(filename);
999         assert(lvalue);
1000         assert(rvalue);
1001
1002         r = install_full_printf(i, rvalue, &printed);
1003         if (r < 0)
1004                 return r;
1005
1006         if (!unit_instance_is_valid(printed))
1007                 return -EINVAL;
1008
1009         free(i->default_instance);
1010         i->default_instance = printed;
1011
1012         return 0;
1013 }
1014
1015 static int unit_file_load(
1016                 InstallContext *c,
1017                 InstallInfo *info,
1018                 const char *path,
1019                 bool allow_symlink) {
1020
1021         const ConfigTableItem items[] = {
1022                 { "Install", "Alias",           config_parse_strv,             0, &info->aliases           },
1023                 { "Install", "WantedBy",        config_parse_strv,             0, &info->wanted_by         },
1024                 { "Install", "RequiredBy",      config_parse_strv,             0, &info->required_by       },
1025                 { "Install", "DefaultInstance", config_parse_default_instance, 0, info                     },
1026                 { "Install", "Also",            config_parse_also,             0, c                        },
1027                 { "Exec",    "User",            config_parse_user,             0, info                     },
1028                 {}
1029         };
1030
1031         int fd;
1032         _cleanup_fclose_ FILE *f = NULL;
1033         int r;
1034
1035         assert(c);
1036         assert(info);
1037         assert(path);
1038
1039         fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|(allow_symlink ? 0 : O_NOFOLLOW));
1040         if (fd < 0)
1041                 return -errno;
1042
1043         f = fdopen(fd, "re");
1044         if (!f) {
1045                 safe_close(fd);
1046                 return -ENOMEM;
1047         }
1048
1049         r = config_parse(NULL, path, f, NULL, config_item_table_lookup, (void*) items, true, true, info);
1050         if (r < 0)
1051                 return r;
1052
1053         return
1054                 (int) strv_length(info->aliases) +
1055                 (int) strv_length(info->wanted_by) +
1056                 (int) strv_length(info->required_by);
1057 }
1058
1059 static int unit_file_search(
1060                 InstallContext *c,
1061                 InstallInfo *info,
1062                 LookupPaths *paths,
1063                 const char *root_dir,
1064                 bool allow_symlink) {
1065
1066         char **p;
1067         int r;
1068
1069         assert(c);
1070         assert(info);
1071         assert(paths);
1072
1073         if (info->path) {
1074                 const char *path;
1075
1076                 if (isempty(root_dir))
1077                         path = info->path;
1078                 else
1079                         path = strappenda(root_dir, info->path);
1080
1081                 return unit_file_load(c, info, path, allow_symlink);
1082         }
1083
1084         assert(info->name);
1085
1086         STRV_FOREACH(p, paths->unit_path) {
1087                 _cleanup_free_ char *path = NULL;
1088
1089                 if (isempty(root_dir))
1090                         path = strjoin(*p, "/", info->name, NULL);
1091                 else
1092                         path = strjoin(root_dir, "/", *p, "/", info->name, NULL);
1093                 if (!path)
1094                         return -ENOMEM;
1095
1096                 r = unit_file_load(c, info, path, allow_symlink);
1097                 if (r >= 0) {
1098                         info->path = path;
1099                         path = NULL;
1100                         return r;
1101                 }
1102                 if (r != -ENOENT && r != -ELOOP)
1103                         return r;
1104         }
1105
1106         if (unit_name_is_instance(info->name)) {
1107
1108                 /* Unit file doesn't exist, however instance
1109                  * enablement was requested.  We will check if it is
1110                  * possible to load template unit file. */
1111
1112                 _cleanup_free_ char *template = NULL, *template_dir = NULL;
1113
1114                 template = unit_name_template(info->name);
1115                 if (!template)
1116                         return -ENOMEM;
1117
1118                 STRV_FOREACH(p, paths->unit_path) {
1119                         _cleanup_free_ char *path = NULL;
1120
1121                         if (isempty(root_dir))
1122                                 path = strjoin(*p, "/", template, NULL);
1123                         else
1124                                 path = strjoin(root_dir, "/", *p, "/", template, NULL);
1125                         if (!path)
1126                                 return -ENOMEM;
1127
1128                         r = unit_file_load(c, info, path, allow_symlink);
1129                         if (r >= 0) {
1130                                 info->path = path;
1131                                 path = NULL;
1132                                 return r;
1133                         }
1134                         if (r != -ENOENT && r != -ELOOP)
1135                                 return r;
1136                 }
1137         }
1138
1139         return -ENOENT;
1140 }
1141
1142 static int unit_file_can_install(
1143                 LookupPaths *paths,
1144                 const char *root_dir,
1145                 const char *name,
1146                 bool allow_symlink) {
1147
1148         _cleanup_install_context_done_ InstallContext c = {};
1149         InstallInfo *i;
1150         int r;
1151
1152         assert(paths);
1153         assert(name);
1154
1155         r = install_info_add_auto(&c, name);
1156         if (r < 0)
1157                 return r;
1158
1159         assert_se(i = hashmap_first(c.will_install));
1160
1161         r = unit_file_search(&c, i, paths, root_dir, allow_symlink);
1162
1163         if (r >= 0)
1164                 r =
1165                         (int) strv_length(i->aliases) +
1166                         (int) strv_length(i->wanted_by) +
1167                         (int) strv_length(i->required_by);
1168
1169         return r;
1170 }
1171
1172 static int create_symlink(
1173                 const char *old_path,
1174                 const char *new_path,
1175                 bool force,
1176                 UnitFileChange **changes,
1177                 unsigned *n_changes) {
1178
1179         _cleanup_free_ char *dest = NULL;
1180         int r;
1181
1182         assert(old_path);
1183         assert(new_path);
1184
1185         mkdir_parents_label(new_path, 0755);
1186
1187         if (symlink(old_path, new_path) >= 0) {
1188                 add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, new_path, old_path);
1189                 return 0;
1190         }
1191
1192         if (errno != EEXIST)
1193                 return -errno;
1194
1195         r = readlink_and_make_absolute(new_path, &dest);
1196         if (r < 0)
1197                 return r;
1198
1199         if (path_equal(dest, old_path))
1200                 return 0;
1201
1202         if (!force)
1203                 return -EEXIST;
1204
1205         r = symlink_atomic(old_path, new_path);
1206         if (r < 0)
1207                 return r;
1208
1209         add_file_change(changes, n_changes, UNIT_FILE_UNLINK, new_path, NULL);
1210         add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, new_path, old_path);
1211
1212         return 0;
1213 }
1214
1215 static int install_info_symlink_alias(
1216                 InstallInfo *i,
1217                 const char *config_path,
1218                 bool force,
1219                 UnitFileChange **changes,
1220                 unsigned *n_changes) {
1221
1222         char **s;
1223         int r = 0, q;
1224
1225         assert(i);
1226         assert(config_path);
1227
1228         STRV_FOREACH(s, i->aliases) {
1229                 _cleanup_free_ char *alias_path = NULL, *dst = NULL;
1230
1231                 q = install_full_printf(i, *s, &dst);
1232                 if (q < 0)
1233                         return q;
1234
1235                 alias_path = path_make_absolute(dst, config_path);
1236                 if (!alias_path)
1237                         return -ENOMEM;
1238
1239                 q = create_symlink(i->path, alias_path, force, changes, n_changes);
1240                 if (r == 0)
1241                         r = q;
1242         }
1243
1244         return r;
1245 }
1246
1247 static int install_info_symlink_wants(
1248                 InstallInfo *i,
1249                 const char *config_path,
1250                 char **list,
1251                 const char *suffix,
1252                 bool force,
1253                 UnitFileChange **changes,
1254                 unsigned *n_changes) {
1255
1256         _cleanup_free_ char *buf = NULL;
1257         const char *n;
1258         char **s;
1259         int r = 0, q;
1260
1261         assert(i);
1262         assert(config_path);
1263
1264         if (unit_name_is_template(i->name) && i->default_instance) {
1265                 buf = unit_name_replace_instance(i->name, i->default_instance);
1266                 if (!buf)
1267                         return -ENOMEM;
1268
1269                 n = buf;
1270         } else
1271                 n = i->name;
1272
1273         STRV_FOREACH(s, list) {
1274                 _cleanup_free_ char *path = NULL, *dst = NULL;
1275
1276                 q = install_full_printf(i, *s, &dst);
1277                 if (q < 0)
1278                         return q;
1279
1280                 if (!unit_name_is_valid(dst, TEMPLATE_VALID)) {
1281                         r = -EINVAL;
1282                         continue;
1283                 }
1284
1285                 path = strjoin(config_path, "/", dst, suffix, n, NULL);
1286                 if (!path)
1287                         return -ENOMEM;
1288
1289                 q = create_symlink(i->path, path, force, changes, n_changes);
1290                 if (r == 0)
1291                         r = q;
1292         }
1293
1294         return r;
1295 }
1296
1297 static int install_info_symlink_link(
1298                 InstallInfo *i,
1299                 LookupPaths *paths,
1300                 const char *config_path,
1301                 bool force,
1302                 UnitFileChange **changes,
1303                 unsigned *n_changes) {
1304
1305         _cleanup_free_ char *path = NULL;
1306         int r;
1307
1308         assert(i);
1309         assert(paths);
1310         assert(config_path);
1311         assert(i->path);
1312
1313         r = in_search_path(i->path, paths->unit_path);
1314         if (r != 0)
1315                 return r;
1316
1317         path = strjoin(config_path, "/", i->name, NULL);
1318         if (!path)
1319                 return -ENOMEM;
1320
1321         return create_symlink(i->path, path, force, changes, n_changes);
1322 }
1323
1324 static int install_info_apply(
1325                 InstallInfo *i,
1326                 LookupPaths *paths,
1327                 const char *config_path,
1328                 bool force,
1329                 UnitFileChange **changes,
1330                 unsigned *n_changes) {
1331
1332         int r, q;
1333
1334         assert(i);
1335         assert(paths);
1336         assert(config_path);
1337
1338         r = install_info_symlink_alias(i, config_path, force, changes, n_changes);
1339
1340         q = install_info_symlink_wants(i, config_path, i->wanted_by, ".wants/", force, changes, n_changes);
1341         if (r == 0)
1342                 r = q;
1343
1344         q = install_info_symlink_wants(i, config_path, i->required_by, ".requires/", force, changes, n_changes);
1345         if (r == 0)
1346                 r = q;
1347
1348         q = install_info_symlink_link(i, paths, config_path, force, changes, n_changes);
1349         if (r == 0)
1350                 r = q;
1351
1352         return r;
1353 }
1354
1355 static int install_context_apply(
1356                 InstallContext *c,
1357                 LookupPaths *paths,
1358                 const char *config_path,
1359                 const char *root_dir,
1360                 bool force,
1361                 UnitFileChange **changes,
1362                 unsigned *n_changes) {
1363
1364         InstallInfo *i;
1365         int r = 0, q;
1366
1367         assert(c);
1368         assert(paths);
1369         assert(config_path);
1370
1371         while ((i = hashmap_first(c->will_install))) {
1372
1373                 q = hashmap_ensure_allocated(&c->have_installed, string_hash_func, string_compare_func);
1374                 if (q < 0)
1375                         return q;
1376
1377                 assert_se(hashmap_move_one(c->have_installed, c->will_install, i->name) == 0);
1378
1379                 q = unit_file_search(c, i, paths, root_dir, false);
1380                 if (q < 0) {
1381                         if (r >= 0)
1382                                 r = q;
1383
1384                         return r;
1385                 } else if (r >= 0)
1386                         r += q;
1387
1388                 q = install_info_apply(i, paths, config_path, force, changes, n_changes);
1389                 if (r >= 0 && q < 0)
1390                         r = q;
1391         }
1392
1393         return r;
1394 }
1395
1396 static int install_context_mark_for_removal(
1397                 InstallContext *c,
1398                 LookupPaths *paths,
1399                 Set **remove_symlinks_to,
1400                 const char *config_path,
1401                 const char *root_dir) {
1402
1403         InstallInfo *i;
1404         int r = 0, q;
1405
1406         assert(c);
1407         assert(paths);
1408         assert(config_path);
1409
1410         /* Marks all items for removal */
1411
1412         while ((i = hashmap_first(c->will_install))) {
1413
1414                 q = hashmap_ensure_allocated(&c->have_installed, string_hash_func, string_compare_func);
1415                 if (q < 0)
1416                         return q;
1417
1418                 assert_se(hashmap_move_one(c->have_installed, c->will_install, i->name) == 0);
1419
1420                 q = unit_file_search(c, i, paths, root_dir, false);
1421                 if (q == -ENOENT) {
1422                         /* do nothing */
1423                 } else if (q < 0) {
1424                         if (r >= 0)
1425                                 r = q;
1426
1427                         return r;
1428                 } else if (r >= 0)
1429                         r += q;
1430
1431                 if (unit_name_is_instance(i->name)) {
1432                         char *unit_file;
1433
1434                         if (i->path) {
1435                                 unit_file = basename(i->path);
1436
1437                                 if (unit_name_is_instance(unit_file))
1438                                         /* unit file named as instance exists, thus all symlinks
1439                                          * pointing to it will be removed */
1440                                         q = mark_symlink_for_removal(remove_symlinks_to, i->name);
1441                                 else
1442                                         /* does not exist, thus we will mark for removal symlinks
1443                                          * to template unit file */
1444                                         q = mark_symlink_for_removal(remove_symlinks_to, unit_file);
1445                         } else {
1446                                 /* If i->path is not set, it means that we didn't actually find
1447                                  * the unit file. But we can still remove symlinks to the
1448                                  * nonexistent template. */
1449                                 unit_file = unit_name_template(i->name);
1450                                 if (!unit_file)
1451                                         return log_oom();
1452
1453                                 q = mark_symlink_for_removal(remove_symlinks_to, unit_file);
1454                                 free(unit_file);
1455                         }
1456                 } else
1457                         q = mark_symlink_for_removal(remove_symlinks_to, i->name);
1458
1459                 if (r >= 0 && q < 0)
1460                         r = q;
1461         }
1462
1463         return r;
1464 }
1465
1466 int unit_file_enable(
1467                 UnitFileScope scope,
1468                 bool runtime,
1469                 const char *root_dir,
1470                 char **files,
1471                 bool force,
1472                 UnitFileChange **changes,
1473                 unsigned *n_changes) {
1474
1475         _cleanup_lookup_paths_free_ LookupPaths paths = {};
1476         _cleanup_install_context_done_ InstallContext c = {};
1477         char **i;
1478         _cleanup_free_ char *config_path = NULL;
1479         int r;
1480
1481         assert(scope >= 0);
1482         assert(scope < _UNIT_FILE_SCOPE_MAX);
1483
1484         r = lookup_paths_init_from_scope(&paths, scope, root_dir);
1485         if (r < 0)
1486                 return r;
1487
1488         r = get_config_path(scope, runtime, root_dir, &config_path);
1489         if (r < 0)
1490                 return r;
1491
1492         STRV_FOREACH(i, files) {
1493                 r = install_info_add_auto(&c, *i);
1494                 if (r < 0)
1495                         return r;
1496         }
1497
1498         /* This will return the number of symlink rules that were
1499         supposed to be created, not the ones actually created. This is
1500         useful to determine whether the passed files had any
1501         installation data at all. */
1502
1503         return install_context_apply(&c, &paths, config_path, root_dir, force, changes, n_changes);
1504 }
1505
1506 int unit_file_disable(
1507                 UnitFileScope scope,
1508                 bool runtime,
1509                 const char *root_dir,
1510                 char **files,
1511                 UnitFileChange **changes,
1512                 unsigned *n_changes) {
1513
1514         _cleanup_lookup_paths_free_ LookupPaths paths = {};
1515         _cleanup_install_context_done_ InstallContext c = {};
1516         char **i;
1517         _cleanup_free_ char *config_path = NULL;
1518         _cleanup_set_free_free_ Set *remove_symlinks_to = NULL;
1519         int r, q;
1520
1521         assert(scope >= 0);
1522         assert(scope < _UNIT_FILE_SCOPE_MAX);
1523
1524         r = lookup_paths_init_from_scope(&paths, scope, root_dir);
1525         if (r < 0)
1526                 return r;
1527
1528         r = get_config_path(scope, runtime, root_dir, &config_path);
1529         if (r < 0)
1530                 return r;
1531
1532         STRV_FOREACH(i, files) {
1533                 r = install_info_add_auto(&c, *i);
1534                 if (r < 0)
1535                         return r;
1536         }
1537
1538         r = install_context_mark_for_removal(&c, &paths, &remove_symlinks_to, config_path, root_dir);
1539
1540         q = remove_marked_symlinks(remove_symlinks_to, config_path, changes, n_changes, files);
1541         if (r == 0)
1542                 r = q;
1543
1544         return r;
1545 }
1546
1547 int unit_file_reenable(
1548                 UnitFileScope scope,
1549                 bool runtime,
1550                 const char *root_dir,
1551                 char **files,
1552                 bool force,
1553                 UnitFileChange **changes,
1554                 unsigned *n_changes) {
1555         int r;
1556
1557         r = unit_file_disable(scope, runtime, root_dir, files,
1558                               changes, n_changes);
1559         if (r < 0)
1560                 return r;
1561
1562         return unit_file_enable(scope, runtime, root_dir, files, force,
1563                                 changes, n_changes);
1564 }
1565
1566 int unit_file_set_default(
1567                 UnitFileScope scope,
1568                 const char *root_dir,
1569                 const char *file,
1570                 bool force,
1571                 UnitFileChange **changes,
1572                 unsigned *n_changes) {
1573
1574         _cleanup_lookup_paths_free_ LookupPaths paths = {};
1575         _cleanup_install_context_done_ InstallContext c = {};
1576         _cleanup_free_ char *config_path = NULL;
1577         char *path;
1578         int r;
1579         InstallInfo *i = NULL;
1580
1581         assert(scope >= 0);
1582         assert(scope < _UNIT_FILE_SCOPE_MAX);
1583         assert(file);
1584
1585         if (unit_name_to_type(file) != UNIT_TARGET)
1586                 return -EINVAL;
1587
1588         r = lookup_paths_init_from_scope(&paths, scope, root_dir);
1589         if (r < 0)
1590                 return r;
1591
1592         r = get_config_path(scope, false, root_dir, &config_path);
1593         if (r < 0)
1594                 return r;
1595
1596         r = install_info_add_auto(&c, file);
1597         if (r < 0)
1598                 return r;
1599
1600         assert_se(i = hashmap_first(c.will_install));
1601
1602         r = unit_file_search(&c, i, &paths, root_dir, false);
1603         if (r < 0)
1604                 return r;
1605
1606         path = strappenda(config_path, "/" SPECIAL_DEFAULT_TARGET);
1607
1608         r = create_symlink(i->path, path, force, changes, n_changes);
1609         if (r < 0)
1610                 return r;
1611
1612         return 0;
1613 }
1614
1615 int unit_file_get_default(
1616                 UnitFileScope scope,
1617                 const char *root_dir,
1618                 char **name) {
1619
1620         _cleanup_lookup_paths_free_ LookupPaths paths = {};
1621         char **p;
1622         int r;
1623
1624         assert(scope >= 0);
1625         assert(scope < _UNIT_FILE_SCOPE_MAX);
1626         assert(name);
1627
1628         r = lookup_paths_init_from_scope(&paths, scope, root_dir);
1629         if (r < 0)
1630                 return r;
1631
1632         STRV_FOREACH(p, paths.unit_path) {
1633                 _cleanup_free_ char *path = NULL, *tmp = NULL;
1634                 char *n;
1635
1636                 if (isempty(root_dir))
1637                         path = strappend(*p, "/" SPECIAL_DEFAULT_TARGET);
1638                 else
1639                         path = strjoin(root_dir, "/", *p, "/" SPECIAL_DEFAULT_TARGET, NULL);
1640
1641                 if (!path)
1642                         return -ENOMEM;
1643
1644                 r = readlink_malloc(path, &tmp);
1645                 if (r == -ENOENT)
1646                         continue;
1647                 else if (r == -EINVAL)
1648                         /* not a symlink */
1649                         n = strdup(SPECIAL_DEFAULT_TARGET);
1650                 else if (r < 0)
1651                         return r;
1652                 else
1653                         n = strdup(basename(tmp));
1654
1655                 if (!n)
1656                         return -ENOMEM;
1657
1658                 *name = n;
1659                 return 0;
1660         }
1661
1662         return -ENOENT;
1663 }
1664
1665 UnitFileState unit_file_get_state(
1666                 UnitFileScope scope,
1667                 const char *root_dir,
1668                 const char *name) {
1669
1670         _cleanup_lookup_paths_free_ LookupPaths paths = {};
1671         UnitFileState state = _UNIT_FILE_STATE_INVALID;
1672         char **i;
1673         _cleanup_free_ char *path = NULL;
1674         int r;
1675
1676         assert(scope >= 0);
1677         assert(scope < _UNIT_FILE_SCOPE_MAX);
1678         assert(name);
1679
1680         if (root_dir && scope != UNIT_FILE_SYSTEM)
1681                 return -EINVAL;
1682
1683         if (!unit_name_is_valid(name, TEMPLATE_VALID))
1684                 return -EINVAL;
1685
1686         r = lookup_paths_init_from_scope(&paths, scope, root_dir);
1687         if (r < 0)
1688                 return r;
1689
1690         STRV_FOREACH(i, paths.unit_path) {
1691                 struct stat st;
1692                 char *partial;
1693
1694                 free(path);
1695                 path = NULL;
1696
1697                 if (root_dir)
1698                         asprintf(&path, "%s/%s/%s", root_dir, *i, name);
1699                 else
1700                         asprintf(&path, "%s/%s", *i, name);
1701                 if (!path)
1702                         return -ENOMEM;
1703
1704                 if (root_dir)
1705                         partial = path + strlen(root_dir) + 1;
1706                 else
1707                         partial = path;
1708
1709                 /*
1710                  * Search for a unit file in our default paths, to
1711                  * be sure, that there are no broken symlinks.
1712                  */
1713                 if (lstat(path, &st) < 0) {
1714                         r = -errno;
1715                         if (errno != ENOENT)
1716                                 return r;
1717
1718                         if (!unit_name_is_instance(name))
1719                                 continue;
1720                 } else {
1721                         if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode))
1722                                 return -ENOENT;
1723
1724                         r = null_or_empty_path(path);
1725                         if (r < 0 && r != -ENOENT)
1726                                 return r;
1727                         else if (r > 0) {
1728                                 state = path_startswith(*i, "/run") ?
1729                                         UNIT_FILE_MASKED_RUNTIME : UNIT_FILE_MASKED;
1730                                 return state;
1731                         }
1732                 }
1733
1734                 r = find_symlinks_in_scope(scope, root_dir, name, &state);
1735                 if (r < 0)
1736                         return r;
1737                 else if (r > 0)
1738                         return state;
1739
1740                 r = unit_file_can_install(&paths, root_dir, partial, true);
1741                 if (r < 0 && errno != ENOENT)
1742                         return r;
1743                 else if (r > 0)
1744                         return UNIT_FILE_DISABLED;
1745                 else if (r == 0)
1746                         return UNIT_FILE_STATIC;
1747         }
1748
1749         return r < 0 ? r : state;
1750 }
1751
1752 int unit_file_query_preset(UnitFileScope scope, const char *root_dir, const char *name) {
1753         _cleanup_strv_free_ char **files = NULL;
1754         char **i;
1755         int r;
1756
1757         assert(scope >= 0);
1758         assert(scope < _UNIT_FILE_SCOPE_MAX);
1759         assert(name);
1760
1761         if (scope == UNIT_FILE_SYSTEM)
1762                 r = conf_files_list(&files, ".preset", root_dir,
1763                                     "/etc/systemd/system-preset",
1764                                     "/usr/local/lib/systemd/system-preset",
1765                                     "/usr/lib/systemd/system-preset",
1766 #ifdef HAVE_SPLIT_USR
1767                                     "/lib/systemd/system-preset",
1768 #endif
1769                                     NULL);
1770         else if (scope == UNIT_FILE_GLOBAL)
1771                 r = conf_files_list(&files, ".preset", root_dir,
1772                                     "/etc/systemd/user-preset",
1773                                     "/usr/local/lib/systemd/user-preset",
1774                                     "/usr/lib/systemd/user-preset",
1775                                     NULL);
1776         else
1777                 return 1;
1778
1779         if (r < 0)
1780                 return r;
1781
1782         STRV_FOREACH(i, files) {
1783                 _cleanup_free_ char *buf = NULL;
1784                 _cleanup_fclose_ FILE *f;
1785                 const char *p;
1786
1787                 if (root_dir)
1788                         p = buf = strjoin(root_dir, "/", *i, NULL);
1789                 else
1790                         p = *i;
1791
1792                 f = fopen(p, "re");
1793                 if (!f) {
1794                         if (errno == ENOENT)
1795                                 continue;
1796
1797                         return -errno;
1798                 }
1799
1800                 for (;;) {
1801                         char line[LINE_MAX], *l;
1802
1803                         if (!fgets(line, sizeof(line), f))
1804                                 break;
1805
1806                         l = strstrip(line);
1807                         if (!*l)
1808                                 continue;
1809
1810                         if (strchr(COMMENTS "\n", *l))
1811                                 continue;
1812
1813                         if (first_word(l, "enable")) {
1814                                 l += 6;
1815                                 l += strspn(l, WHITESPACE);
1816
1817                                 if (fnmatch(l, name, FNM_NOESCAPE) == 0) {
1818                                         log_debug("Preset file says enable %s.", name);
1819                                         return 1;
1820                                 }
1821
1822                         } else if (first_word(l, "disable")) {
1823                                 l += 7;
1824                                 l += strspn(l, WHITESPACE);
1825
1826                                 if (fnmatch(l, name, FNM_NOESCAPE) == 0) {
1827                                         log_debug("Preset file says disable %s.", name);
1828                                         return 0;
1829                                 }
1830
1831                         } else
1832                                 log_debug("Couldn't parse line '%s'", l);
1833                 }
1834         }
1835
1836         /* Default is "enable" */
1837         log_debug("Preset file doesn't say anything about %s, enabling.", name);
1838         return 1;
1839 }
1840
1841 int unit_file_preset(
1842                 UnitFileScope scope,
1843                 bool runtime,
1844                 const char *root_dir,
1845                 char **files,
1846                 UnitFilePresetMode mode,
1847                 bool force,
1848                 UnitFileChange **changes,
1849                 unsigned *n_changes) {
1850
1851         _cleanup_install_context_done_ InstallContext plus = {}, minus = {};
1852         _cleanup_lookup_paths_free_ LookupPaths paths = {};
1853         _cleanup_free_ char *config_path = NULL;
1854         char **i;
1855         int r, q;
1856
1857         assert(scope >= 0);
1858         assert(scope < _UNIT_FILE_SCOPE_MAX);
1859         assert(mode < _UNIT_FILE_PRESET_MODE_MAX);
1860
1861         r = lookup_paths_init_from_scope(&paths, scope, root_dir);
1862         if (r < 0)
1863                 return r;
1864
1865         r = get_config_path(scope, runtime, root_dir, &config_path);
1866         if (r < 0)
1867                 return r;
1868
1869         STRV_FOREACH(i, files) {
1870
1871                 if (!unit_name_is_valid(*i, TEMPLATE_VALID))
1872                         return -EINVAL;
1873
1874                 r = unit_file_query_preset(scope, root_dir, *i);
1875                 if (r < 0)
1876                         return r;
1877
1878                 if (r && mode != UNIT_FILE_PRESET_DISABLE_ONLY)
1879                         r = install_info_add_auto(&plus, *i);
1880                 else if (!r && mode != UNIT_FILE_PRESET_ENABLE_ONLY)
1881                         r = install_info_add_auto(&minus, *i);
1882                 else
1883                         r = 0;
1884                 if (r < 0)
1885                         return r;
1886         }
1887
1888         r = 0;
1889
1890         if (mode != UNIT_FILE_PRESET_ENABLE_ONLY) {
1891                 _cleanup_set_free_free_ Set *remove_symlinks_to = NULL;
1892
1893                 r = install_context_mark_for_removal(&minus, &paths, &remove_symlinks_to, config_path, root_dir);
1894
1895                 q = remove_marked_symlinks(remove_symlinks_to, config_path, changes, n_changes, files);
1896                 if (r == 0)
1897                         r = q;
1898         }
1899
1900         if (mode != UNIT_FILE_PRESET_DISABLE_ONLY) {
1901                 /* Returns number of symlinks that where supposed to be installed. */
1902                 q = install_context_apply(&plus, &paths, config_path, root_dir, force, changes, n_changes);
1903                 if (r == 0)
1904                         r = q;
1905         }
1906
1907         return r;
1908 }
1909
1910 int unit_file_preset_all(
1911                 UnitFileScope scope,
1912                 bool runtime,
1913                 const char *root_dir,
1914                 UnitFilePresetMode mode,
1915                 bool force,
1916                 UnitFileChange **changes,
1917                 unsigned *n_changes) {
1918
1919         _cleanup_install_context_done_ InstallContext plus = {}, minus = {};
1920         _cleanup_lookup_paths_free_ LookupPaths paths = {};
1921         _cleanup_free_ char *config_path = NULL;
1922         char **i;
1923         int r, q;
1924
1925         assert(scope >= 0);
1926         assert(scope < _UNIT_FILE_SCOPE_MAX);
1927         assert(mode < _UNIT_FILE_PRESET_MODE_MAX);
1928
1929         r = lookup_paths_init_from_scope(&paths, scope, root_dir);
1930         if (r < 0)
1931                 return r;
1932
1933         r = get_config_path(scope, runtime, root_dir, &config_path);
1934         if (r < 0)
1935                 return r;
1936
1937         STRV_FOREACH(i, paths.unit_path) {
1938                 _cleanup_closedir_ DIR *d = NULL;
1939                 _cleanup_free_ char *buf = NULL;
1940                 const char *units_dir;
1941
1942                 if (!isempty(root_dir)) {
1943                         buf = strjoin(root_dir, "/", *i, NULL);
1944                         if (!buf)
1945                                 return -ENOMEM;
1946
1947                         units_dir = buf;
1948                 } else
1949                         units_dir = *i;
1950
1951                 d = opendir(units_dir);
1952                 if (!d) {
1953                         if (errno == ENOENT)
1954                                 continue;
1955
1956                         return -errno;
1957                 }
1958
1959                 for (;;) {
1960                         struct dirent *de;
1961
1962                         errno = 0;
1963                         de = readdir(d);
1964                         if (!de && errno != 0)
1965                                 return -errno;
1966
1967                         if (!de)
1968                                 break;
1969
1970                         if (ignore_file(de->d_name))
1971                                 continue;
1972
1973                         if (!unit_name_is_valid(de->d_name, TEMPLATE_VALID))
1974                                 continue;
1975
1976                         dirent_ensure_type(d, de);
1977
1978                         if (de->d_type != DT_REG)
1979                                 continue;
1980
1981                         r = unit_file_query_preset(scope, root_dir, de->d_name);
1982                         if (r < 0)
1983                                 return r;
1984
1985                         if (r && mode != UNIT_FILE_PRESET_DISABLE_ONLY)
1986                                 r = install_info_add_auto(&plus, de->d_name);
1987                         else if (!r && mode != UNIT_FILE_PRESET_ENABLE_ONLY)
1988                                 r = install_info_add_auto(&minus, de->d_name);
1989                         else
1990                                 r = 0;
1991                         if (r < 0)
1992                                 return r;
1993                 }
1994         }
1995
1996         r = 0;
1997
1998         if (mode != UNIT_FILE_PRESET_ENABLE_ONLY) {
1999                 _cleanup_set_free_free_ Set *remove_symlinks_to = NULL;
2000
2001                 r = install_context_mark_for_removal(&minus, &paths, &remove_symlinks_to, config_path, root_dir);
2002
2003                 q = remove_marked_symlinks(remove_symlinks_to, config_path, changes, n_changes, NULL);
2004                 if (r == 0)
2005                         r = q;
2006         }
2007
2008         if (mode != UNIT_FILE_PRESET_DISABLE_ONLY) {
2009                 q = install_context_apply(&plus, &paths, config_path, root_dir, force, changes, n_changes);
2010                 if (r == 0)
2011                         r = q;
2012         }
2013
2014         return r;
2015 }
2016
2017 static void unitfilelist_free(UnitFileList **f) {
2018         if (!*f)
2019                 return;
2020
2021         free((*f)->path);
2022         free(*f);
2023 }
2024 #define _cleanup_unitfilelist_free_ _cleanup_(unitfilelist_free)
2025
2026 int unit_file_get_list(
2027                 UnitFileScope scope,
2028                 const char *root_dir,
2029                 Hashmap *h) {
2030
2031         _cleanup_lookup_paths_free_ LookupPaths paths = {};
2032         char **i;
2033         int r;
2034
2035         assert(scope >= 0);
2036         assert(scope < _UNIT_FILE_SCOPE_MAX);
2037         assert(h);
2038
2039         if (root_dir && scope != UNIT_FILE_SYSTEM)
2040                 return -EINVAL;
2041
2042         r = lookup_paths_init_from_scope(&paths, scope, root_dir);
2043         if (r < 0)
2044                 return r;
2045
2046         STRV_FOREACH(i, paths.unit_path) {
2047                 _cleanup_closedir_ DIR *d = NULL;
2048                 _cleanup_free_ char *buf = NULL;
2049                 const char *units_dir;
2050
2051                 if (!isempty(root_dir)) {
2052                         buf = strjoin(root_dir, "/", *i, NULL);
2053                         if (!buf)
2054                                 return -ENOMEM;
2055
2056                         units_dir = buf;
2057                 } else
2058                         units_dir = *i;
2059
2060                 d = opendir(units_dir);
2061                 if (!d) {
2062                         if (errno == ENOENT)
2063                                 continue;
2064
2065                         return -errno;
2066                 }
2067
2068                 for (;;) {
2069                         _cleanup_unitfilelist_free_ UnitFileList *f = NULL;
2070                         struct dirent *de;
2071
2072                         errno = 0;
2073                         de = readdir(d);
2074                         if (!de && errno != 0)
2075                                 return -errno;
2076
2077                         if (!de)
2078                                 break;
2079
2080                         if (ignore_file(de->d_name))
2081                                 continue;
2082
2083                         if (!unit_name_is_valid(de->d_name, TEMPLATE_VALID))
2084                                 continue;
2085
2086                         if (hashmap_get(h, de->d_name))
2087                                 continue;
2088
2089                         dirent_ensure_type(d, de);
2090
2091                         if (!IN_SET(de->d_type, DT_LNK, DT_REG))
2092                                 continue;
2093
2094                         f = new0(UnitFileList, 1);
2095                         if (!f)
2096                                 return -ENOMEM;
2097
2098                         f->path = path_make_absolute(de->d_name, units_dir);
2099                         if (!f->path)
2100                                 return -ENOMEM;
2101
2102                         r = null_or_empty_path(f->path);
2103                         if (r < 0 && r != -ENOENT)
2104                                 return r;
2105                         else if (r > 0) {
2106                                 f->state =
2107                                         path_startswith(*i, "/run") ?
2108                                         UNIT_FILE_MASKED_RUNTIME : UNIT_FILE_MASKED;
2109                                 goto found;
2110                         }
2111
2112                         r = find_symlinks_in_scope(scope, root_dir, de->d_name, &f->state);
2113                         if (r < 0)
2114                                 return r;
2115                         else if (r > 0) {
2116                                 f->state = UNIT_FILE_ENABLED;
2117                                 goto found;
2118                         }
2119
2120                         r = unit_file_can_install(&paths, root_dir, f->path, true);
2121                         if (r == -EINVAL ||  /* Invalid setting? */
2122                             r == -EBADMSG || /* Invalid format? */
2123                             r == -ENOENT     /* Included file not found? */)
2124                                 f->state = UNIT_FILE_INVALID;
2125                         else if (r < 0)
2126                                 return r;
2127                         else if (r > 0)
2128                                 f->state = UNIT_FILE_DISABLED;
2129                         else
2130                                 f->state = UNIT_FILE_STATIC;
2131
2132                 found:
2133                         r = hashmap_put(h, basename(f->path), f);
2134                         if (r < 0)
2135                                 return r;
2136                         f = NULL; /* prevent cleanup */
2137                 }
2138         }
2139
2140         return r;
2141 }
2142
2143 static const char* const unit_file_state_table[_UNIT_FILE_STATE_MAX] = {
2144         [UNIT_FILE_ENABLED] = "enabled",
2145         [UNIT_FILE_ENABLED_RUNTIME] = "enabled-runtime",
2146         [UNIT_FILE_LINKED] = "linked",
2147         [UNIT_FILE_LINKED_RUNTIME] = "linked-runtime",
2148         [UNIT_FILE_MASKED] = "masked",
2149         [UNIT_FILE_MASKED_RUNTIME] = "masked-runtime",
2150         [UNIT_FILE_STATIC] = "static",
2151         [UNIT_FILE_DISABLED] = "disabled",
2152         [UNIT_FILE_INVALID] = "invalid",
2153 };
2154
2155 DEFINE_STRING_TABLE_LOOKUP(unit_file_state, UnitFileState);
2156
2157 static const char* const unit_file_change_type_table[_UNIT_FILE_CHANGE_TYPE_MAX] = {
2158         [UNIT_FILE_SYMLINK] = "symlink",
2159         [UNIT_FILE_UNLINK] = "unlink",
2160 };
2161
2162 DEFINE_STRING_TABLE_LOOKUP(unit_file_change_type, UnitFileChangeType);
2163
2164 static const char* const unit_file_preset_mode_table[_UNIT_FILE_PRESET_MODE_MAX] = {
2165         [UNIT_FILE_PRESET_FULL] = "full",
2166         [UNIT_FILE_PRESET_ENABLE_ONLY] = "enable-only",
2167         [UNIT_FILE_PRESET_DISABLE_ONLY] = "disable-only",
2168 };
2169
2170 DEFINE_STRING_TABLE_LOOKUP(unit_file_preset_mode, UnitFilePresetMode);