]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/load-dropin.c
dbus: don't hit assert when dumping properties
[thirdparty/systemd.git] / src / load-dropin.c
CommitLineData
d6c9574f 1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
5cb5a6ff 2
a7334b09
LP
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
0301abf4
LP
22#include <dirent.h>
23#include <errno.h>
24
25#include "unit.h"
5cb5a6ff 26#include "load-dropin.h"
b952f2e1 27#include "log.h"
036643a2 28#include "strv.h"
9e2f7c11
LP
29#include "unit-name.h"
30
99f08d14 31static int iterate_dir(Unit *u, const char *path, UnitDependency dependency) {
9e2f7c11
LP
32 DIR *d;
33 struct dirent *de;
34 int r;
35
99f08d14
LP
36 assert(u);
37 assert(path);
38
9e2f7c11
LP
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
99f08d14 58 r = unit_add_dependency_by_name(u, dependency, de->d_name, f, true);
9e2f7c11
LP
59 free(f);
60
61 if (r < 0)
62 goto finish;
63 }
64
65 r = 0;
66
67finish:
68 closedir(d);
69 return r;
70}
5cb5a6ff 71
99f08d14 72static int process_dir(Unit *u, const char *unit_path, const char *name, const char *suffix, UnitDependency dependency) {
0301abf4 73 int r;
99f08d14 74 char *path;
0301abf4 75
87f0e418 76 assert(u);
99f08d14
LP
77 assert(unit_path);
78 assert(name);
79 assert(suffix);
5cb5a6ff 80
99f08d14
LP
81 if (asprintf(&path, "%s/%s%s", unit_path, name, suffix) < 0)
82 return -ENOMEM;
5cb5a6ff 83
99f08d14
LP
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);
0301abf4 90
99f08d14
LP
91 if (r < 0)
92 return r;
0301abf4 93
99f08d14
LP
94 if (u->meta.instance) {
95 char *template;
96 /* Also try the template dir */
0301abf4 97
99f08d14
LP
98 if (!(template = unit_name_template(name)))
99 return -ENOMEM;
0301abf4 100
99f08d14
LP
101 r = asprintf(&path, "%s/%s%s", unit_path, template, suffix);
102 free(template);
0301abf4 103
99f08d14
LP
104 if (r < 0)
105 return -ENOMEM;
0301abf4 106
99f08d14
LP
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);
0301abf4 113
99f08d14
LP
114 if (r < 0)
115 return r;
116 }
0301abf4 117
99f08d14
LP
118 return 0;
119}
0301abf4 120
99f08d14
LP
121int unit_load_dropin(Unit *u) {
122 Iterator i;
123 char *t;
124
125 assert(u);
126
127 /* Load dependencies from supplementary drop-in directories */
036643a2 128
99f08d14
LP
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;
0301abf4 134
99f08d14
LP
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;
036643a2 140 }
0301abf4
LP
141 }
142
5cb5a6ff
LP
143 return 0;
144}