summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2013-01-11 00:21:06 +0100
committerLennart Poettering <lennart@poettering.net>2013-01-11 00:21:06 +0100
commit8afbb8e1180dce3cb33a14fc3ec5afcf501104e6 (patch)
tree9b865b95b235337ed4603c5764b9edf5ddbc24b3 /src
parent5dd9014faf58bf974352043fbddd3a8e9c3cd9d9 (diff)
unit: allow extension of unit files with .d/*.conf drop-ins
For all unit files foobar.service we will now read foobar.service.d/*.conf, too. This may be used to override certain unit settings without having to edit unit files directly. This makes it really easy to change specific settings for services without having to edit any unit file: mkdir /etc/systemd/system/avahi-daemon.service.d/ echo -e '[Service]\nNice=99' > /etc/systemd/system/avahi-daemon.service.d/nice.conf systemctl daemon-reload
Diffstat (limited to 'src')
-rw-r--r--src/core/load-dropin.c63
-rw-r--r--src/core/unit.c20
2 files changed, 54 insertions, 29 deletions
diff --git a/src/core/load-dropin.c b/src/core/load-dropin.c
index 86f81c7484..8e10d8fb4c 100644
--- a/src/core/load-dropin.c
+++ b/src/core/load-dropin.c
@@ -27,10 +27,21 @@
#include "log.h"
#include "strv.h"
#include "unit-name.h"
+#include "conf-parser.h"
+#include "load-fragment.h"
+
+static int load_dropin_config_file(Unit *u, const char *path) {
+ assert(u);
+ assert(path);
+
+ if (!endswith(path, ".conf"))
+ return 0;
+
+ return config_parse(path, NULL, UNIT_VTABLE(u)->sections, config_item_perf_lookup, (void*) load_fragment_gperf_lookup, false, u);
+}
static int iterate_dir(Unit *u, const char *path, UnitDependency dependency) {
- DIR *d;
- struct dirent *de;
+ _cleanup_closedir_ DIR *d = NULL;
int r;
assert(u);
@@ -38,37 +49,46 @@ static int iterate_dir(Unit *u, const char *path, UnitDependency dependency) {
d = opendir(path);
if (!d) {
-
if (errno == ENOENT)
return 0;
return -errno;
}
- while ((de = readdir(d))) {
- char *f;
+ for (;;) {
+ struct dirent *de;
+ union dirent_storage buf;
+ _cleanup_free_ char *f = NULL;
+ int k;
+
+ k = readdir_r(d, &buf.de, &de);
+ if (k != 0) {
+ log_error("Failed to read directory %s: %s", path, strerror(k));
+ return -k;
+ }
+
+ if (!de)
+ break;
if (ignore_file(de->d_name))
continue;
f = strjoin(path, "/", de->d_name, NULL);
- if (!f) {
- r = -ENOMEM;
- goto finish;
- }
+ if (!f)
+ return log_oom();
- r = unit_add_dependency_by_name(u, dependency, de->d_name, f, true);
- free(f);
-
- if (r < 0)
- log_error("Cannot add dependency %s to %s, ignoring: %s", de->d_name, u->id, strerror(-r));
+ if (dependency >= 0) {
+ r = unit_add_dependency_by_name(u, dependency, de->d_name, f, true);
+ if (r < 0)
+ log_error("Cannot add dependency %s to %s, ignoring: %s", de->d_name, u->id, strerror(-r));
+ } else {
+ r = load_dropin_config_file(u, f);
+ if (r < 0)
+ log_error("Cannot load drop-in configuration file %s for %s, ignoring: %s", f, u->id, strerror(-r));
+ }
}
- r = 0;
-
-finish:
- closedir(d);
- return r;
+ return 0;
}
static int process_dir(Unit *u, const char *unit_path, const char *name, const char *suffix, UnitDependency dependency) {
@@ -143,6 +163,11 @@ int unit_load_dropin(Unit *u) {
r = process_dir(u, *p, t, ".requires", UNIT_REQUIRES);
if (r < 0)
return r;
+
+ /* This loads the drop-in config snippets */
+ r = process_dir(u, *p, t, ".d", _UNIT_TYPE_INVALID);
+ if (r < 0)
+ return r;
}
}
diff --git a/src/core/unit.c b/src/core/unit.c
index 45453dce64..f00cfedb89 100644
--- a/src/core/unit.c
+++ b/src/core/unit.c
@@ -1781,6 +1781,7 @@ static const char *resolve_template(Unit *u, const char *name, const char*path,
assert(u);
assert(name || path);
+ assert(p);
if (!name)
name = path_get_file_name(path);
@@ -1795,7 +1796,8 @@ static const char *resolve_template(Unit *u, const char *name, const char*path,
else {
char *i;
- if (!(i = unit_name_to_prefix(u->id)))
+ i = unit_name_to_prefix(u->id);
+ if (!i)
return NULL;
s = unit_name_replace_instance(name, i);
@@ -1812,22 +1814,20 @@ static const char *resolve_template(Unit *u, const char *name, const char*path,
int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name, const char *path, bool add_reference) {
Unit *other;
int r;
- char *s;
+ _cleanup_free_ char *s = NULL;
assert(u);
assert(name || path);
- if (!(name = resolve_template(u, name, path, &s)))
+ name = resolve_template(u, name, path, &s);
+ if (!name)
return -ENOMEM;
- if ((r = manager_load_unit(u->manager, name, path, NULL, &other)) < 0)
- goto finish;
-
- r = unit_add_dependency(u, d, other, add_reference);
+ r = manager_load_unit(u->manager, name, path, NULL, &other);
+ if (r < 0)
+ return r;
-finish:
- free(s);
- return r;
+ return unit_add_dependency(u, d, other, add_reference);
}
int unit_add_two_dependencies_by_name(Unit *u, UnitDependency d, UnitDependency e, const char *name, const char *path, bool add_reference) {