X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fcore%2Funit.c;h=b2a47653b66061fe8c4e60edafb8159010cdc3fb;hb=e45460d666512db4f908f86e8722d7932dcf0f82;hp=d26f6e456c1dcfb5a451b2a495eee0357e561059;hpb=8755586eba8b824851f11aa6c3f8f184ed69a30a;p=elogind.git diff --git a/src/core/unit.c b/src/core/unit.c index d26f6e456..b2a47653b 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -45,6 +45,7 @@ #include "cgroup-util.h" #include "missing.h" #include "cgroup-attr.h" +#include "mkdir.h" const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX] = { [UNIT_SERVICE] = &service_vtable, @@ -1022,9 +1023,9 @@ static void unit_status_log_starting_stopping_reloading(Unit *u, JobType t) { t == JOB_STOP ? SD_MESSAGE_UNIT_STOPPING : SD_MESSAGE_UNIT_RELOADING; - log_struct(LOG_INFO, + log_struct_unit(LOG_INFO, + u->id, MESSAGE_ID(mid), - "UNIT=%s", u->id, "MESSAGE=%s", buf, NULL); } @@ -1438,9 +1439,9 @@ void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns, bool reload_su check_unneeded_dependencies(u); if (ns != os && ns == UNIT_FAILED) { - log_struct(LOG_NOTICE, + log_struct_unit(LOG_NOTICE, + u->id, "MESSAGE=Unit %s entered failed state", u->id, - "UNIT=%s", u->id, NULL); unit_trigger_on_failure(u); } @@ -2156,26 +2157,27 @@ int unit_add_cgroup_attribute( _cleanup_free_ char *c = NULL; CGroupAttribute *a; + int r; assert(u); assert(name); assert(value); if (!controller) { - const char *dot; - - dot = strchr(name, '.'); - if (!dot) + r = cg_controller_from_attr(name, &c); + if (r < 0) return -EINVAL; - c = strndup(name, dot - name); - if (!c) - return -ENOMEM; - controller = c; + } else { + if (!filename_is_safe(name)) + return -EINVAL; + + if (!filename_is_safe(controller)) + return -EINVAL; } - if (streq(controller, SYSTEMD_CGROUP_CONTROLLER)) + if (!controller || streq(controller, SYSTEMD_CGROUP_CONTROLLER)) return -EINVAL; a = cgroup_attribute_find_list(u->cgroup_attributes, controller, name); @@ -2757,6 +2759,54 @@ ExecContext *unit_get_exec_context(Unit *u) { return (ExecContext*) ((uint8_t*) u + offset); } +int unit_write_drop_in(Unit *u, bool runtime, const char *name, const char *data) { + _cleanup_free_ char *p = NULL, *q = NULL; + assert(u); + + if (u->manager->running_as != SYSTEMD_SYSTEM) + return -ENOTSUP; + + if (!filename_is_safe(name)) + return -EINVAL; + + p = strjoin(runtime ? "/run/systemd/system/" : "/etc/systemd/system/", u->id, ".d", NULL); + if (!p) + return -ENOMEM; + + q = strjoin(p, "/50-", name, ".conf", NULL); + if (!q) + return -ENOMEM; + + mkdir_p(p, 0755); + return write_one_line_file_atomic(q, data); +} + +int unit_remove_drop_in(Unit *u, bool runtime, const char *name) { + _cleanup_free_ char *p = NULL, *q = NULL; + + assert(u); + + if (u->manager->running_as != SYSTEMD_SYSTEM) + return -ENOTSUP; + + if (!filename_is_safe(name)) + return -EINVAL; + + p = strjoin(runtime ? "/run/systemd/system/" : "/etc/systemd/system/", u->id, ".d", NULL); + if (!p) + return -ENOMEM; + + q = strjoin(p, "/50-", name, ".conf", NULL); + if (!q) + return -ENOMEM; + + if (unlink(q) < 0) + return -errno; + + rmdir(p); + return 0; +} + static const char* const unit_active_state_table[_UNIT_ACTIVE_STATE_MAX] = { [UNIT_ACTIVE] = "active", [UNIT_RELOADING] = "reloading",