]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/load-dropin.c
man: fix man page chapter in Makefile.am
[thirdparty/systemd.git] / src / core / 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
5430f7f2
LP
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
a7334b09
LP
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
5430f7f2 16 Lesser General Public License for more details.
a7334b09 17
5430f7f2 18 You should have received a copy of the GNU Lesser General Public License
a7334b09
LP
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
f917c3e5
LP
39 d = opendir(path);
40 if (!d) {
9e2f7c11
LP
41
42 if (errno == ENOENT)
43 return 0;
44
45 return -errno;
46 }
47
48 while ((de = readdir(d))) {
49 char *f;
50
51 if (ignore_file(de->d_name))
52 continue;
53
44d91056
LP
54 f = join(path, "/", de->d_name, NULL);
55 if (!f) {
9e2f7c11
LP
56 r = -ENOMEM;
57 goto finish;
58 }
59
99f08d14 60 r = unit_add_dependency_by_name(u, dependency, de->d_name, f, true);
9e2f7c11
LP
61 free(f);
62
63 if (r < 0)
ac155bb8 64 log_error("Cannot add dependency %s to %s, ignoring: %s", de->d_name, u->id, strerror(-r));
9e2f7c11
LP
65 }
66
67 r = 0;
68
69finish:
70 closedir(d);
71 return r;
72}
5cb5a6ff 73
99f08d14 74static int process_dir(Unit *u, const char *unit_path, const char *name, const char *suffix, UnitDependency dependency) {
0301abf4 75 int r;
99f08d14 76 char *path;
0301abf4 77
87f0e418 78 assert(u);
99f08d14
LP
79 assert(unit_path);
80 assert(name);
81 assert(suffix);
5cb5a6ff 82
911a4828
LP
83 path = join(unit_path, "/", name, suffix, NULL);
84 if (!path)
99f08d14 85 return -ENOMEM;
5cb5a6ff 86
ac155bb8
MS
87 if (u->manager->unit_path_cache &&
88 !set_get(u->manager->unit_path_cache, path))
99f08d14
LP
89 r = 0;
90 else
91 r = iterate_dir(u, path, dependency);
92 free(path);
0301abf4 93
99f08d14
LP
94 if (r < 0)
95 return r;
0301abf4 96
ac155bb8 97 if (u->instance) {
99f08d14
LP
98 char *template;
99 /* Also try the template dir */
0301abf4 100
f917c3e5
LP
101 template = unit_name_template(name);
102 if (!template)
99f08d14 103 return -ENOMEM;
0301abf4 104
cd0ed1db 105 path = join(unit_path, "/", template, suffix, NULL);
99f08d14 106 free(template);
0301abf4 107
cd0ed1db 108 if (!path)
99f08d14 109 return -ENOMEM;
0301abf4 110
ac155bb8
MS
111 if (u->manager->unit_path_cache &&
112 !set_get(u->manager->unit_path_cache, path))
99f08d14
LP
113 r = 0;
114 else
115 r = iterate_dir(u, path, dependency);
116 free(path);
0301abf4 117
99f08d14
LP
118 if (r < 0)
119 return r;
120 }
0301abf4 121
99f08d14
LP
122 return 0;
123}
0301abf4 124
99f08d14
LP
125int unit_load_dropin(Unit *u) {
126 Iterator i;
127 char *t;
128
129 assert(u);
130
131 /* Load dependencies from supplementary drop-in directories */
036643a2 132
ac155bb8 133 SET_FOREACH(t, u->names, i) {
99f08d14
LP
134 char **p;
135
ac155bb8 136 STRV_FOREACH(p, u->manager->lookup_paths.unit_path) {
99f08d14 137 int r;
0301abf4 138
f917c3e5
LP
139 r = process_dir(u, *p, t, ".wants", UNIT_WANTS);
140 if (r < 0)
99f08d14
LP
141 return r;
142
f917c3e5
LP
143 r = process_dir(u, *p, t, ".requires", UNIT_REQUIRES);
144 if (r < 0)
99f08d14 145 return r;
036643a2 146 }
0301abf4
LP
147 }
148
5cb5a6ff
LP
149 return 0;
150}