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