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