chiark / gitweb /
logind: implement idle hint logic
[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                 if (asprintf(&f, "%s/%s", path, de->d_name) < 0) {
54                         r = -ENOMEM;
55                         goto finish;
56                 }
57
58                 r = unit_add_dependency_by_name(u, dependency, de->d_name, f, true);
59                 free(f);
60
61                 if (r < 0)
62                         goto finish;
63         }
64
65         r = 0;
66
67 finish:
68         closedir(d);
69         return r;
70 }
71
72 static int process_dir(Unit *u, const char *unit_path, const char *name, const char *suffix, UnitDependency dependency) {
73         int r;
74         char *path;
75
76         assert(u);
77         assert(unit_path);
78         assert(name);
79         assert(suffix);
80
81         if (asprintf(&path, "%s/%s%s", unit_path, name, suffix) < 0)
82                 return -ENOMEM;
83
84         if (u->meta.manager->unit_path_cache &&
85             !set_get(u->meta.manager->unit_path_cache, path))
86                 r = 0;
87         else
88                 r = iterate_dir(u, path, dependency);
89         free(path);
90
91         if (r < 0)
92                 return r;
93
94         if (u->meta.instance) {
95                 char *template;
96                 /* Also try the template dir */
97
98                 if (!(template = unit_name_template(name)))
99                         return -ENOMEM;
100
101                 r = asprintf(&path, "%s/%s%s", unit_path, template, suffix);
102                 free(template);
103
104                 if (r < 0)
105                         return -ENOMEM;
106
107                 if (u->meta.manager->unit_path_cache &&
108                     !set_get(u->meta.manager->unit_path_cache, path))
109                         r = 0;
110                 else
111                         r = iterate_dir(u, path, dependency);
112                 free(path);
113
114                 if (r < 0)
115                         return r;
116         }
117
118         return 0;
119 }
120
121 int unit_load_dropin(Unit *u) {
122         Iterator i;
123         char *t;
124
125         assert(u);
126
127         /* Load dependencies from supplementary drop-in directories */
128
129         SET_FOREACH(t, u->meta.names, i) {
130                 char **p;
131
132                 STRV_FOREACH(p, u->meta.manager->lookup_paths.unit_path) {
133                         int r;
134
135                         if ((r = process_dir(u, *p, t, ".wants", UNIT_WANTS)) < 0)
136                                 return r;
137
138                         if ((r = process_dir(u, *p, t, ".requires", UNIT_REQUIRES)) < 0)
139                                 return r;
140                 }
141         }
142
143         return 0;
144 }