chiark / gitweb /
util: introduce join() to speed up simple string concatenations
[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         path = join(unit_path, "/", name, suffix, NULL);
82         if (!path)
83                 return -ENOMEM;
84
85         if (u->meta.manager->unit_path_cache &&
86             !set_get(u->meta.manager->unit_path_cache, path))
87                 r = 0;
88         else
89                 r = iterate_dir(u, path, dependency);
90         free(path);
91
92         if (r < 0)
93                 return r;
94
95         if (u->meta.instance) {
96                 char *template;
97                 /* Also try the template dir */
98
99                 if (!(template = unit_name_template(name)))
100                         return -ENOMEM;
101
102                 r = asprintf(&path, "%s/%s%s", unit_path, template, suffix);
103                 free(template);
104
105                 if (r < 0)
106                         return -ENOMEM;
107
108                 if (u->meta.manager->unit_path_cache &&
109                     !set_get(u->meta.manager->unit_path_cache, path))
110                         r = 0;
111                 else
112                         r = iterate_dir(u, path, dependency);
113                 free(path);
114
115                 if (r < 0)
116                         return r;
117         }
118
119         return 0;
120 }
121
122 int unit_load_dropin(Unit *u) {
123         Iterator i;
124         char *t;
125
126         assert(u);
127
128         /* Load dependencies from supplementary drop-in directories */
129
130         SET_FOREACH(t, u->meta.names, i) {
131                 char **p;
132
133                 STRV_FOREACH(p, u->meta.manager->lookup_paths.unit_path) {
134                         int r;
135
136                         if ((r = process_dir(u, *p, t, ".wants", UNIT_WANTS)) < 0)
137                                 return r;
138
139                         if ((r = process_dir(u, *p, t, ".requires", UNIT_REQUIRES)) < 0)
140                                 return r;
141                 }
142         }
143
144         return 0;
145 }