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
This commit is contained in:
Lennart Poettering
2013-01-11 00:21:06 +01:00
parent 5dd9014faf
commit 8afbb8e118
3 changed files with 211 additions and 164 deletions
+44 -19
View File
@@ -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();
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 = 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));
}
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;
}
}
+10 -10
View File
@@ -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 = manager_load_unit(u->manager, name, path, NULL, &other);
if (r < 0)
return r;
r = unit_add_dependency(u, d, other, add_reference);
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) {