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