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