diff options
author | Lennart Poettering <lennart@poettering.net> | 2013-07-11 21:29:33 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2013-07-11 21:29:33 +0200 |
commit | b9ec9359369f224bfb13db616f97401a6a177bd8 (patch) | |
tree | fffa85a989179c0d0fc76a28e15eaf2869bf0562 | |
parent | 665f8316f435772ed539be5e164a85cd188f84b4 (diff) |
core: simplify drop-in writing logic a bit
let's make use of some format string magic!
-rw-r--r-- | src/core/dbus-cgroup.c | 32 | ||||
-rw-r--r-- | src/core/dbus-service.c | 2 | ||||
-rw-r--r-- | src/core/dbus-unit.c | 36 | ||||
-rw-r--r-- | src/core/unit.c | 58 | ||||
-rw-r--r-- | src/core/unit.h | 6 |
5 files changed, 85 insertions, 49 deletions
diff --git a/src/core/dbus-cgroup.c b/src/core/dbus-cgroup.c index 27b54f95c6..8ad3d118c5 100644 --- a/src/core/dbus-cgroup.c +++ b/src/core/dbus-cgroup.c @@ -162,7 +162,7 @@ int bus_cgroup_set_property( dbus_message_iter_get_basic(i, &b); c->cpu_accounting = b; - unit_write_drop_in_private_section(u, mode, name, b ? "CPUAccounting=yes" : "CPUAccounting=no"); + unit_write_drop_in_private(u, mode, name, b ? "CPUAccounting=yes" : "CPUAccounting=no"); } return 1; @@ -181,11 +181,8 @@ int bus_cgroup_set_property( return -EINVAL; if (mode != UNIT_CHECK) { - char buf[sizeof("CPUShares=") + DECIMAL_STR_MAX(ul)]; c->cpu_shares = ul; - - sprintf(buf, "CPUShares=%lu", ul); - unit_write_drop_in_private_section(u, mode, name, buf); + unit_write_drop_in_private_format(u, mode, name, "CPUShares=%lu", ul); } return 1; @@ -200,7 +197,7 @@ int bus_cgroup_set_property( dbus_message_iter_get_basic(i, &b); c->blockio_accounting = b; - unit_write_drop_in_private_section(u, mode, name, b ? "BlockIOAccounting=yes" : "BlockIOAccounting=no"); + unit_write_drop_in_private(u, mode, name, b ? "BlockIOAccounting=yes" : "BlockIOAccounting=no"); } return 1; @@ -219,11 +216,8 @@ int bus_cgroup_set_property( return -EINVAL; if (mode != UNIT_CHECK) { - char buf[sizeof("BlockIOWeight=") + DECIMAL_STR_MAX(ul)]; c->cpu_shares = ul; - - sprintf(buf, "BlockIOWeight=%lu", ul); - unit_write_drop_in_private_section(u, mode, name, buf); + unit_write_drop_in_private_format(u, mode, name, "BlockIOWeight=%lu", ul); } return 1; @@ -238,7 +232,7 @@ int bus_cgroup_set_property( dbus_message_iter_get_basic(i, &b); c->memory_accounting = b; - unit_write_drop_in_private_section(u, mode, name, b ? "MemoryAccounting=yes" : "MemoryAccounting=no"); + unit_write_drop_in_private(u, mode, name, b ? "MemoryAccounting=yes" : "MemoryAccounting=no"); } return 1; @@ -250,19 +244,15 @@ int bus_cgroup_set_property( if (mode != UNIT_CHECK) { uint64_t limit; - char buf[sizeof("MemorySoftLimit=") + DECIMAL_STR_MAX(limit)]; dbus_message_iter_get_basic(i, &limit); - if (streq(name, "MemoryLimit")) { + if (streq(name, "MemoryLimit")) c->memory_limit = limit; - sprintf(buf, "MemoryLimit=%" PRIu64, limit); - unit_write_drop_in_private_section(u, mode, name, buf); - } else { + else c->memory_soft_limit = limit; - sprintf(buf, "MemorySoftLimit=%" PRIu64, limit); - unit_write_drop_in_private_section(u, mode, name, buf); - } + + unit_write_drop_in_private_format(u, mode, name, "%s=%" PRIu64, name, limit); } return 1; @@ -285,7 +275,7 @@ int bus_cgroup_set_property( c->device_policy = p; buf = strappenda("DevicePolicy=", policy); - unit_write_drop_in_private_section(u, mode, name, buf); + unit_write_drop_in_private(u, mode, name, buf); } return 1; @@ -365,7 +355,7 @@ int bus_cgroup_set_property( fprintf(f, "DeviceAllow=%s %s%s%s\n", a->path, a->r ? "r" : "", a->w ? "w" : "", a->m ? "m" : ""); fflush(f); - unit_write_drop_in_private_section(u, mode, name, buf); + unit_write_drop_in_private(u, mode, name, buf); } return 1; diff --git a/src/core/dbus-service.c b/src/core/dbus-service.c index d63ca01c4f..8b157b5f3c 100644 --- a/src/core/dbus-service.c +++ b/src/core/dbus-service.c @@ -276,7 +276,7 @@ static int bus_service_set_transient_property( } fflush(f); - unit_write_drop_in_private_section(UNIT(s), mode, name, buf); + unit_write_drop_in_private(UNIT(s), mode, name, buf); } return 1; diff --git a/src/core/dbus-unit.c b/src/core/dbus-unit.c index 5814971b8c..ba4d42652e 100644 --- a/src/core/dbus-unit.c +++ b/src/core/dbus-unit.c @@ -794,7 +794,6 @@ static int bus_unit_set_transient_property( return -EINVAL; if (mode != UNIT_CHECK) { - _cleanup_free_ char *contents = NULL; const char *description; dbus_message_iter_get_basic(i, &description); @@ -803,18 +802,13 @@ static int bus_unit_set_transient_property( if (r < 0) return r; - contents = strjoin("[Unit]\nDescription=", description, "\n", NULL); - if (!contents) - return -ENOMEM; - - unit_write_drop_in(u, mode, name, contents); + unit_write_drop_in_format(u, mode, name, "[Unit]\nDescription=%s\n", description); } return 1; } else if (streq(name, "Slice") && unit_get_cgroup_context(u)) { const char *s; - Unit *slice; if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_STRING) return -EINVAL; @@ -822,10 +816,12 @@ static int bus_unit_set_transient_property( dbus_message_iter_get_basic(i, &s); if (isempty(s)) { - if (mode != UNIT_CHECK) + if (mode != UNIT_CHECK) { unit_ref_unset(&u->slice); + unit_remove_drop_in(u, mode, name); + } } else { - _cleanup_free_ char *contents = NULL; + Unit *slice; r = manager_load_unit(u->manager, s, NULL, error, &slice); if (r < 0) @@ -834,15 +830,12 @@ static int bus_unit_set_transient_property( if (slice->type != UNIT_SLICE) return -EINVAL; - if (mode != UNIT_CHECK) + if (mode != UNIT_CHECK) { unit_ref_set(&u->slice, slice); - - contents = strjoin("[", UNIT_VTABLE(u)->private_section, "]\nSlice=", s, NULL); - if (!contents) - return -ENOMEM; - - unit_write_drop_in(u, mode, name, contents); + unit_write_drop_in_private_format(u, mode, name, "Slice=%s\n", s); + } } + return 1; } else if (streq(name, "Requires") || @@ -880,7 +873,7 @@ static int bus_unit_set_transient_property( return -EINVAL; if (mode != UNIT_CHECK) { - _cleanup_free_ char *label = NULL, *contents = NULL; + _cleanup_free_ char *label = NULL; r = unit_add_dependency_by_name(u, d, other, NULL, true); if (r < 0) @@ -890,11 +883,7 @@ static int bus_unit_set_transient_property( if (!label) return -ENOMEM; - contents = strjoin("[Unit]\n", name, "=", other, "\n", NULL); - if (!contents) - return -ENOMEM; - - unit_write_drop_in(u, mode, label, contents); + unit_write_drop_in_format(u, mode, label, "[Unit]\n%s=%s\n", name, other); } dbus_message_iter_next(&sub); @@ -1048,10 +1037,11 @@ const BusProperty bus_unit_properties[] = { { "ConditionResult", bus_property_append_bool, "b", offsetof(Unit, condition_result) }, { "LoadError", bus_unit_append_load_error, "(ss)", 0 }, { "Transient", bus_property_append_bool, "b", offsetof(Unit, transient) }, - { NULL, } + {} }; const BusProperty bus_unit_cgroup_properties[] = { { "Slice", bus_unit_append_slice, "s", 0 }, { "ControlGroup", bus_property_append_string, "s", offsetof(Unit, cgroup_path), true }, + {} }; diff --git a/src/core/unit.c b/src/core/unit.c index 86452859bc..dd08011ff4 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -2733,6 +2733,7 @@ CGroupContext *unit_get_cgroup_context(Unit *u) { } static int drop_in_file(Unit *u, UnitSetPropertiesMode mode, const char *name, char **_p, char **_q) { + _cleanup_free_ char *b = NULL; char *p, *q; int r; @@ -2742,7 +2743,11 @@ static int drop_in_file(Unit *u, UnitSetPropertiesMode mode, const char *name, c assert(_q); assert(mode & (UNIT_PERSISTENT|UNIT_RUNTIME)); - if (!filename_is_safe(name)) + b = xescape(name, "/."); + if (!b) + return -ENOMEM; + + if (!filename_is_safe(b)) return -EINVAL; if (u->manager->running_as == SYSTEMD_USER) { @@ -2762,7 +2767,7 @@ static int drop_in_file(Unit *u, UnitSetPropertiesMode mode, const char *name, c if (!p) return -ENOMEM; - q = strjoin(p, "/90-", name, ".conf", NULL); + q = strjoin(p, "/90-", b, ".conf", NULL); if (!q) { free(p); return -ENOMEM; @@ -2792,7 +2797,29 @@ int unit_write_drop_in(Unit *u, UnitSetPropertiesMode mode, const char *name, co return write_string_file_atomic_label(q, data); } -int unit_write_drop_in_private_section(Unit *u, UnitSetPropertiesMode mode, const char *name, const char *data) { +int unit_write_drop_in_format(Unit *u, UnitSetPropertiesMode mode, const char *name, const char *format, ...) { + _cleanup_free_ char *p = NULL; + va_list ap; + int r; + + assert(u); + assert(name); + assert(format); + + if (!(mode & (UNIT_PERSISTENT|UNIT_RUNTIME))) + return 0; + + va_start(ap, format); + r = vasprintf(&p, format, ap); + va_end(ap); + + if (r < 0) + return -ENOMEM; + + return unit_write_drop_in(u, mode, name, p); +} + +int unit_write_drop_in_private(Unit *u, UnitSetPropertiesMode mode, const char *name, const char *data) { _cleanup_free_ char *ndata = NULL; assert(u); @@ -2802,6 +2829,9 @@ int unit_write_drop_in_private_section(Unit *u, UnitSetPropertiesMode mode, cons if (!UNIT_VTABLE(u)->private_section) return -EINVAL; + if (!(mode & (UNIT_PERSISTENT|UNIT_RUNTIME))) + return 0; + ndata = strjoin("[", UNIT_VTABLE(u)->private_section, "]\n", data, NULL); if (!ndata) return -ENOMEM; @@ -2809,6 +2839,28 @@ int unit_write_drop_in_private_section(Unit *u, UnitSetPropertiesMode mode, cons return unit_write_drop_in(u, mode, name, ndata); } +int unit_write_drop_in_private_format(Unit *u, UnitSetPropertiesMode mode, const char *name, const char *format, ...) { + _cleanup_free_ char *p = NULL; + va_list ap; + int r; + + assert(u); + assert(name); + assert(format); + + if (!(mode & (UNIT_PERSISTENT|UNIT_RUNTIME))) + return 0; + + va_start(ap, format); + r = vasprintf(&p, format, ap); + va_end(ap); + + if (r < 0) + return -ENOMEM; + + return unit_write_drop_in_private(u, mode, name, p); +} + int unit_remove_drop_in(Unit *u, UnitSetPropertiesMode mode, const char *name) { _cleanup_free_ char *p = NULL, *q = NULL; int r; diff --git a/src/core/unit.h b/src/core/unit.h index ed4df18246..0caea183c4 100644 --- a/src/core/unit.h +++ b/src/core/unit.h @@ -598,7 +598,11 @@ ExecContext *unit_get_exec_context(Unit *u) _pure_; CGroupContext *unit_get_cgroup_context(Unit *u) _pure_; int unit_write_drop_in(Unit *u, UnitSetPropertiesMode mode, const char *name, const char *data); -int unit_write_drop_in_private_section(Unit *u, UnitSetPropertiesMode mode, const char *name, const char *data); +int unit_write_drop_in_format(Unit *u, UnitSetPropertiesMode mode, const char *name, const char *format, ...) _printf_attr_(4,5); + +int unit_write_drop_in_private(Unit *u, UnitSetPropertiesMode mode, const char *name, const char *data); +int unit_write_drop_in_private_format(Unit *u, UnitSetPropertiesMode mode, const char *name, const char *format, ...) _printf_attr_(4,5); + int unit_remove_drop_in(Unit *u, UnitSetPropertiesMode mode, const char *name); int unit_kill_context(Unit *u, KillContext *c, bool sigkill, pid_t main_pid, pid_t control_pid, bool main_pid_alien); |