chiark / gitweb /
getty: fix message
[elogind.git] / src / load-dropin.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 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   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <dirent.h>
23 #include <errno.h>
24
25 #include "unit.h"
26 #include "load-dropin.h"
27 #include "log.h"
28 #include "strv.h"
29 #include "unit-name.h"
30
31 static int iterate_dir(Unit *u, const char *path, UnitDependency dependency) {
32         DIR *d;
33         struct dirent *de;
34         int r;
35
36         assert(u);
37         assert(path);
38
39         if (!(d = opendir(path))) {
40
41                 if (errno == ENOENT)
42                         return 0;
43
44                 return -errno;
45         }
46
47         while ((de = readdir(d))) {
48                 char *f;
49
50                 if (ignore_file(de->d_name))
51                         continue;
52
53                 f = join(path, "/", de->d_name, NULL);
54                 if (!f) {
55                         r = -ENOMEM;
56                         goto finish;
57                 }
58
59                 r = unit_add_dependency_by_name(u, dependency, de->d_name, f, true);
60                 free(f);
61
62                 if (r < 0)
63                         goto finish;
64         }
65
66         r = 0;
67
68 finish:
69         closedir(d);
70         return r;
71 }
72
73 static int process_dir(Unit *u, const char *unit_path, const char *name, const char *suffix, UnitDependency dependency) {
74         int r;
75         char *path;
76
77         assert(u);
78         assert(unit_path);
79         assert(name);
80         assert(suffix);
81
82         path = join(unit_path, "/", name, suffix, NULL);
83         if (!path)
84                 return -ENOMEM;
85
86         if (u->meta.manager->unit_path_cache &&
87             !set_get(u->meta.manager->unit_path_cache, path))
88                 r = 0;
89         else
90                 r = iterate_dir(u, path, dependency);
91         free(path);
92
93         if (r < 0)
94                 return r;
95
96         if (u->meta.instance) {
97                 char *template;
98                 /* Also try the template dir */
99
100                 if (!(template = unit_name_template(name)))
101                         return -ENOMEM;
102
103                 path = join(unit_path, "/", template, suffix, NULL);
104                 free(template);
105
106                 if (!path)
107                         return -ENOMEM;
108
109                 if (u->meta.manager->unit_path_cache &&
110                     !set_get(u->meta.manager->unit_path_cache, path))
111                         r = 0;
112                 else
113                         r = iterate_dir(u, path, dependency);
114                 free(path);
115
116                 if (r < 0)
117                         return r;
118         }
119
120         return 0;
121 }
122
123 int unit_load_dropin(Unit *u) {
124         Iterator i;
125         char *t;
126
127         assert(u);
128
129         /* Load dependencies from supplementary drop-in directories */
130
131         SET_FOREACH(t, u->meta.names, i) {
132                 char **p;
133
134                 STRV_FOREACH(p, u->meta.manager->lookup_paths.unit_path) {
135                         int r;
136
137                         if ((r = process_dir(u, *p, t, ".wants", UNIT_WANTS)) < 0)
138                                 return r;
139
140                         if ((r = process_dir(u, *p, t, ".requires", UNIT_REQUIRES)) < 0)
141                                 return r;
142                 }
143         }
144
145         return 0;
146 }