diff options
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/cgroup-attr.c | 58 | ||||
-rw-r--r-- | src/core/cgroup-attr.h | 7 | ||||
-rw-r--r-- | src/core/cgroup-semantics.c | 333 | ||||
-rw-r--r-- | src/core/cgroup-semantics.h | 43 | ||||
-rw-r--r-- | src/core/dbus-manager.c | 41 | ||||
-rw-r--r-- | src/core/dbus-unit.c | 329 | ||||
-rw-r--r-- | src/core/dbus-unit.h | 25 | ||||
-rw-r--r-- | src/core/execute.c | 41 | ||||
-rw-r--r-- | src/core/execute.h | 2 | ||||
-rw-r--r-- | src/core/job.c | 29 | ||||
-rw-r--r-- | src/core/load-fragment-gperf.gperf.m4 | 16 | ||||
-rw-r--r-- | src/core/load-fragment.c | 300 | ||||
-rw-r--r-- | src/core/load-fragment.h | 6 | ||||
-rw-r--r-- | src/core/manager.c | 191 | ||||
-rw-r--r-- | src/core/manager.h | 13 | ||||
-rw-r--r-- | src/core/mount-setup.c | 10 | ||||
-rw-r--r-- | src/core/transaction.c | 4 | ||||
-rw-r--r-- | src/core/unit.c | 218 | ||||
-rw-r--r-- | src/core/unit.h | 5 |
19 files changed, 1073 insertions, 598 deletions
diff --git a/src/core/cgroup-attr.c b/src/core/cgroup-attr.c index 1373684bdb..2ab4d4623e 100644 --- a/src/core/cgroup-attr.c +++ b/src/core/cgroup-attr.c @@ -25,8 +25,8 @@ #include "fileio.h" int cgroup_attribute_apply(CGroupAttribute *a, CGroupBonding *b) { - int r; _cleanup_free_ char *path = NULL, *v = NULL; + int r; assert(a); @@ -34,8 +34,8 @@ int cgroup_attribute_apply(CGroupAttribute *a, CGroupBonding *b) { if (!b) return 0; - if (a->map_callback) { - r = a->map_callback(a->controller, a->name, a->value, &v); + if (a->semantics && a->semantics->map_write) { + r = a->semantics->map_write(a->semantics, a->value, &v); if (r < 0) return r; } @@ -66,6 +66,29 @@ int cgroup_attribute_apply_list(CGroupAttribute *first, CGroupBonding *b) { return r; } +bool cgroup_attribute_matches(CGroupAttribute *a, const char *controller, const char *name) { + assert(a); + + if (controller) { + if (streq(a->controller, controller) && (!name || streq(a->name, name))) + return true; + + } else if (!name) + return true; + else if (streq(a->name, name)) { + size_t x, y; + x = strlen(a->controller); + y = strlen(name); + + if (y > x && + memcmp(a->controller, name, x) == 0 && + name[x] == '.') + return true; + } + + return false; +} + CGroupAttribute *cgroup_attribute_find_list( CGroupAttribute *first, const char *controller, @@ -74,24 +97,9 @@ CGroupAttribute *cgroup_attribute_find_list( assert(name); - LIST_FOREACH(by_unit, a, first) { - - - if (controller) { - if (streq(a->controller, controller) && streq(a->name, name)) - return a; - - } else if (streq(a->name, name)) { - size_t x, y; - x = strlen(a->controller); - y = strlen(name); - - if (y > x && - memcmp(a->controller, name, x) == 0 && - name[x] == '.') - return a; - } - } + LIST_FOREACH(by_unit, a, first) + if (cgroup_attribute_matches(a, controller, name)) + return a; return NULL; } @@ -114,3 +122,11 @@ void cgroup_attribute_free_list(CGroupAttribute *first) { LIST_FOREACH_SAFE(by_unit, a, n, first) cgroup_attribute_free(a); } + +void cgroup_attribute_free_some(CGroupAttribute *first, const char *controller, const char *name) { + CGroupAttribute *a, *n; + + LIST_FOREACH_SAFE(by_unit, a, n, first) + if (cgroup_attribute_matches(a, controller, name)) + cgroup_attribute_free(a); +} diff --git a/src/core/cgroup-attr.h b/src/core/cgroup-attr.h index 0f5b854898..0b542981e8 100644 --- a/src/core/cgroup-attr.h +++ b/src/core/cgroup-attr.h @@ -23,10 +23,9 @@ typedef struct CGroupAttribute CGroupAttribute; -typedef int (*CGroupAttributeMapCallback)(const char *controller, const char*name, const char *value, char **ret); - #include "unit.h" #include "cgroup.h" +#include "cgroup-semantics.h" struct CGroupAttribute { char *controller; @@ -35,7 +34,7 @@ struct CGroupAttribute { Unit *unit; - CGroupAttributeMapCallback map_callback; + const CGroupSemantics *semantics; LIST_FIELDS(CGroupAttribute, by_unit); }; @@ -43,7 +42,9 @@ struct CGroupAttribute { int cgroup_attribute_apply(CGroupAttribute *a, CGroupBonding *b); int cgroup_attribute_apply_list(CGroupAttribute *first, CGroupBonding *b); +bool cgroup_attribute_matches(CGroupAttribute *a, const char *controller, const char *name); CGroupAttribute *cgroup_attribute_find_list(CGroupAttribute *first, const char *controller, const char *name); void cgroup_attribute_free(CGroupAttribute *a); void cgroup_attribute_free_list(CGroupAttribute *first); +void cgroup_attribute_free_some(CGroupAttribute *first, const char *controller, const char *name); diff --git a/src/core/cgroup-semantics.c b/src/core/cgroup-semantics.c new file mode 100644 index 0000000000..82b02bbd78 --- /dev/null +++ b/src/core/cgroup-semantics.c @@ -0,0 +1,333 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +/*** + This file is part of systemd. + + Copyright 2013 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see <http://www.gnu.org/licenses/>. +***/ + +#include "util.h" +#include "strv.h" +#include "path-util.h" +#include "cgroup-util.h" + +#include "cgroup-semantics.h" + +static int parse_cpu_shares(const CGroupSemantics *s, const char *value, char **ret) { + unsigned long ul; + + assert(s); + assert(value); + assert(ret); + + if (safe_atolu(value, &ul) < 0 || ul < 1) + return -EINVAL; + + if (asprintf(ret, "%lu", ul) < 0) + return -ENOMEM; + + return 1; +} + +static int parse_memory_limit(const CGroupSemantics *s, const char *value, char **ret) { + off_t sz; + + assert(s); + assert(value); + assert(ret); + + if (parse_bytes(value, &sz) < 0 || sz <= 0) + return -EINVAL; + + if (asprintf(ret, "%llu", (unsigned long long) sz) < 0) + return -ENOMEM; + + return 1; +} + +static int parse_device(const CGroupSemantics *s, const char *value, char **ret) { + _cleanup_strv_free_ char **l = NULL; + char *x; + unsigned k; + + assert(s); + assert(value); + assert(ret); + + l = strv_split_quoted(value); + if (!l) + return -ENOMEM; + + k = strv_length(l); + if (k < 1 || k > 2) + return -EINVAL; + + if (!streq(l[0], "*") && !path_startswith(l[0], "/dev")) + return -EINVAL; + + if (!isempty(l[1]) && !in_charset(l[1], "rwm")) + return -EINVAL; + + x = strdup(value); + if (!x) + return -ENOMEM; + + *ret = x; + return 1; +} + +static int parse_blkio_weight(const CGroupSemantics *s, const char *value, char **ret) { + _cleanup_strv_free_ char **l = NULL; + unsigned long ul; + + assert(s); + assert(value); + assert(ret); + + l = strv_split_quoted(value); + if (!l) + return -ENOMEM; + + if (strv_length(l) != 1) + return 0; /* Returning 0 will cause parse_blkio_weight_device() be tried instead */ + + if (safe_atolu(l[0], &ul) < 0 || ul < 10 || ul > 1000) + return -EINVAL; + + if (asprintf(ret, "%lu", ul) < 0) + return -ENOMEM; + + return 1; +} + +static int parse_blkio_weight_device(const CGroupSemantics *s, const char *value, char **ret) { + _cleanup_strv_free_ char **l = NULL; + unsigned long ul; + + assert(s); + assert(value); + assert(ret); + + l = strv_split_quoted(value); + if (!l) + return -ENOMEM; + + if (strv_length(l) != 2) + return -EINVAL; + + if (!path_startswith(l[0], "/dev")) + return -EINVAL; + + if (safe_atolu(l[1], &ul) < 0 || ul < 10 || ul > 1000) + return -EINVAL; + + if (asprintf(ret, "%s %lu", l[0], ul) < 0) + return -ENOMEM; + + return 1; +} + +static int parse_blkio_bandwidth(const CGroupSemantics *s, const char *value, char **ret) { + _cleanup_strv_free_ char **l = NULL; + off_t bytes; + + assert(s); + assert(value); + assert(ret); + + l = strv_split_quoted(value); + if (!l) + return -ENOMEM; + + if (strv_length(l) != 2) + return -EINVAL; + + if (!path_startswith(l[0], "/dev")) { + return -EINVAL; + } + + if (parse_bytes(l[1], &bytes) < 0 || bytes <= 0) + return -EINVAL; + + if (asprintf(ret, "%s %llu", l[0], (unsigned long long) bytes) < 0) + return -ENOMEM; + + return 0; +} + +static int map_device(const CGroupSemantics *s, const char *value, char **ret) { + _cleanup_strv_free_ char **l = NULL; + unsigned k; + + assert(s); + assert(value); + assert(ret); + + l = strv_split_quoted(value); + if (!l) + return -ENOMEM; + + k = strv_length(l); + if (k < 1 || k > 2) + return -EINVAL; + + if (streq(l[0], "*")) { + + if (asprintf(ret, "a *:*%s%s", + isempty(l[1]) ? "" : " ", strempty(l[1])) < 0) + return -ENOMEM; + } else { + struct stat st; + + if (stat(l[0], &st) < 0) { + log_warning("Couldn't stat device %s", l[0]); + return -errno; + } + + if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode)) { + log_warning("%s is not a device.", l[0]); + return -ENODEV; + } + + if (asprintf(ret, "%c %u:%u%s%s", + S_ISCHR(st.st_mode) ? 'c' : 'b', + major(st.st_rdev), minor(st.st_rdev), + isempty(l[1]) ? "" : " ", strempty(l[1])) < 0) + return -ENOMEM; + } + + return 0; +} + +static int map_blkio(const CGroupSemantics *s, const char *value, char **ret) { + _cleanup_strv_free_ char **l = NULL; + struct stat st; + dev_t d; + + assert(s); + assert(value); + assert(ret); + + l = strv_split_quoted(value); + if (!l) + return log_oom(); + + if (strv_length(l) != 2) + return -EINVAL; + + if (stat(l[0], &st) < 0) { + log_warning("Couldn't stat device %s", l[0]); + return -errno; + } + + if (S_ISBLK(st.st_mode)) + d = st.st_rdev; + else if (major(st.st_dev) != 0) { + /* If this is not a device node then find the block + * device this file is stored on */ + d = st.st_dev; + + /* If this is a partition, try to get the originating + * block device */ + block_get_whole_disk(d, &d); + } else { + log_warning("%s is not a block device and file system block device cannot be determined or is not local.", l[0]); + return -ENODEV; + } + + if (asprintf(ret, "%u:%u %s", major(d), minor(d), l[1]) < 0) + return -ENOMEM; + + return 0; +} + +static const CGroupSemantics semantics[] = { + { "cpu", "cpu.shares", "CPUShare", false, parse_cpu_shares, NULL, NULL }, + { "memory", "memory.soft_limit_in_bytes", "MemorySoftLimit", false, parse_memory_limit, NULL, NULL }, + { "memory", "memory.limit_in_bytes", "MemoryLimit", false, parse_memory_limit, NULL, NULL }, + { "devices", "devices.allow", "DeviceAllow", true, parse_device, map_device, NULL }, + { "devices", "devices.deny", "DeviceDeny", true, parse_device, map_device, NULL }, + { "blkio", "blkio.weight", "BlockIOWeight", false, parse_blkio_weight, NULL, NULL }, + { "blkio", "blkio.weight_device", "BlockIOWeight", true, parse_blkio_weight_device, map_blkio, NULL }, + { "blkio", "blkio.read_bps_device", "BlockIOReadBandwidth", true, parse_blkio_bandwidth, map_blkio, NULL }, + { "blkio", "blkio.write_bps_device", "BlockIOWriteBandwidth", true, parse_blkio_bandwidth, map_blkio, NULL } +}; + +int cgroup_semantics_find( + const char *controller, + const char *name, + const char *value, + char **ret, + const CGroupSemantics **_s) { + + _cleanup_free_ char *c = NULL; + unsigned i; + int r; + + assert(name); + assert(_s); + assert(!value == !ret); + + if (!controller) { + r = cg_controller_from_attr(name, &c); + if (r < 0) + return r; + + controller = c; + } + + for (i = 0; i < ELEMENTSOF(semantics); i++) { + const CGroupSemantics *s = semantics + i; + bool matches_name, matches_pretty; + + if (controller && s->controller && !streq(s->controller, controller)) + continue; + + matches_name = s->name && streq(s->name, name); + matches_pretty = s->pretty && streq(s->pretty, name); + + if (!matches_name && !matches_pretty) + continue; + + if (value) { + if (matches_pretty && s->map_pretty) { + + r = s->map_pretty(s, value, ret); + if (r < 0) + return r; + + if (r == 0) + continue; + + } else { + char *x; + + x = strdup(value); + if (!x) + return -ENOMEM; + + *ret = x; + } + } + + *_s = s; + return 1; + } + + *ret = NULL; + *_s = NULL; + return 0; +} diff --git a/src/core/cgroup-semantics.h b/src/core/cgroup-semantics.h new file mode 100644 index 0000000000..4f848f4bb7 --- /dev/null +++ b/src/core/cgroup-semantics.h @@ -0,0 +1,43 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +#pragma once + +/*** + This file is part of systemd. + + Copyright 2011 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see <http://www.gnu.org/licenses/>. +***/ + +typedef struct CGroupSemantics CGroupSemantics; + +struct CGroupSemantics { + const char *controller; + const char *name; + const char *pretty; + + bool multiple; + + /* This call is used for parsing the pretty value to the actual attribute value */ + int (*map_pretty)(const CGroupSemantics *semantics, const char *value, char **ret); + + /* Right before writing this attribute the attribute value is converted to a low-level value */ + int (*map_write)(const CGroupSemantics *semantics, const char *value, char **ret); + + /* If this attribute takes a list, this call can be used to reset the list to empty */ + int (*reset)(const CGroupSemantics *semantics, const char *group); +}; + +int cgroup_semantics_find(const char *controller, const char *name, const char *value, char **ret, const CGroupSemantics **semantics); diff --git a/src/core/dbus-manager.c b/src/core/dbus-manager.c index de23369397..8f4bbc59b7 100644 --- a/src/core/dbus-manager.c +++ b/src/core/dbus-manager.c @@ -103,30 +103,31 @@ " <method name=\"ResetFailedUnit\">\n" \ " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \ " </method>\n" \ - " <method name=\"GetUnitControlGroupAttributes\">\n" \ + " <method name=\"SetUnitControlGroup\">\n" \ " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \ - " <arg name=\"attributes\" type=\"as\" direction=\"in\"/>\n" \ - " <arg name=\"values\" type=\"as\" direction=\"out\"/>\n" \ + " <arg name=\"group\" type=\"s\" direction=\"in\"/>\n" \ + " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \ " </method>\n" \ - " <method name=\"SetUnitControlGroupAttributes\">\n" \ + " <method name=\"UnsetUnitControlGroup\">\n" \ " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \ - " <arg name=\"attributes\" type=\"a(sss)\" direction=\"in\"/>\n" \ + " <arg name=\"group\" type=\"s\" direction=\"in\"/>\n" \ " <arg name=\"mode\" type=\"s\" direction=\"in\"\n/>" \ " </method>\n" \ - " <method name=\"UnsetUnitControlGroupAttributes\">\n" \ + " <method name=\"GetUnitControlGroupAttribute\">\n" \ " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \ - " <arg name=\"attributes\" type=\"a(ss)\" direction=\"in\"/>\n" \ - " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \ + " <arg name=\"attribute\" type=\"s\" direction=\"in\"/>\n" \ + " <arg name=\"values\" type=\"as\" direction=\"out\"/>\n" \ " </method>\n" \ - " <method name=\"SetUnitControlGroups\">\n" \ + " <method name=\"SetUnitControlGroupAttribute\">\n" \ " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \ - " <arg name=\"groups\" type=\"as\" direction=\"in\"/>\n" \ - " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \ + " <arg name=\"attribute\" type=\"s\" direction=\"in\"/>\n" \ + " <arg name=\"values\" type=\"as\" direction=\"in\"/>\n" \ + " <arg name=\"mode\" type=\"s\" direction=\"in\"\n/>" \ " </method>\n" \ - " <method name=\"UnsetUnitControlGroups\">\n" \ + " <method name=\"UnsetUnitControlGroupAttributes\">\n" \ " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \ - " <arg name=\"groups\" type=\"as\" direction=\"in\"/>\n" \ - " <arg name=\"mode\" type=\"s\" direction=\"in\"\n/>" \ + " <arg name=\"attribute\" type=\"s\" direction=\"in\"/>\n" \ + " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \ " </method>\n" \ " <method name=\"GetJob\">\n" \ " <arg name=\"id\" type=\"u\" direction=\"in\"/>\n" \ @@ -874,7 +875,7 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, if (!reply) goto oom; - } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetUnitControlGroups")) { + } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetUnitControlGroup")) { const char *name; Unit *u; DBusMessageIter iter; @@ -902,7 +903,7 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, if (!reply) goto oom; - } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetUnitControlGroups")) { + } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetUnitControlGroup")) { const char *name; Unit *u; DBusMessageIter iter; @@ -930,7 +931,7 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, if (!reply) goto oom; - } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetUnitControlGroupAttributes")) { + } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetUnitControlGroupAttribute")) { const char *name; Unit *u; DBusMessageIter iter; @@ -949,6 +950,7 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, } SELINUX_UNIT_ACCESS_CHECK(u, connection, message, "start"); + r = bus_unit_cgroup_attribute_set(u, &iter); if (r < 0) return bus_send_error_reply(connection, message, NULL, r); @@ -957,7 +959,7 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, if (!reply) goto oom; - } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetUnitControlGroupAttributes")) { + } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetUnitControlGroupAttribute")) { const char *name; Unit *u; DBusMessageIter iter; @@ -985,7 +987,7 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, if (!reply) goto oom; - } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnitControlGroupAttributes")) { + } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnitControlGroupAttribute")) { const char *name; Unit *u; DBusMessageIter iter; @@ -1005,6 +1007,7 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, } SELINUX_UNIT_ACCESS_CHECK(u, connection, message, "status"); + r = bus_unit_cgroup_attribute_get(u, &iter, &list); if (r < 0) return bus_send_error_reply(connection, message, NULL, r); diff --git a/src/core/dbus-unit.c b/src/core/dbus-unit.c index 4f968c20f2..7c23e1e616 100644 --- a/src/core/dbus-unit.c +++ b/src/core/dbus-unit.c @@ -344,8 +344,8 @@ static int bus_unit_append_cgroup_attrs(DBusMessageIter *i, const char *property char _cleanup_free_ *v = NULL; bool success; - if (a->map_callback) - a->map_callback(a->controller, a->name, a->value, &v); + if (a->semantics && a->semantics->map_write) + a->semantics->map_write(a->semantics, a->value, &v); success = dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2) && @@ -472,7 +472,7 @@ static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusConnection *conn if (!reply) goto oom; - } else if (streq_ptr(dbus_message_get_member(message), "SetControlGroups")) { + } else if (streq_ptr(dbus_message_get_member(message), "SetControlGroup")) { DBusMessageIter iter; SELINUX_UNIT_ACCESS_CHECK(u, connection, message, "start"); @@ -488,7 +488,7 @@ static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusConnection *conn if (!reply) goto oom; - } else if (streq_ptr(dbus_message_get_member(message), "UnsetControlGroups")) { + } else if (streq_ptr(dbus_message_get_member(message), "UnsetControlGroup")) { DBusMessageIter iter; SELINUX_UNIT_ACCESS_CHECK(u, connection, message, "stop"); @@ -496,14 +496,14 @@ static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusConnection *conn if (!dbus_message_iter_init(message, &iter)) goto oom; - r = bus_unit_cgroup_set(u, &iter); + r = bus_unit_cgroup_unset(u, &iter); if (r < 0) return bus_send_error_reply(connection, message, NULL, r); reply = dbus_message_new_method_return(message); if (!reply) goto oom; - } else if (streq_ptr(dbus_message_get_member(message), "GetControlGroupAttributes")) { + } else if (streq_ptr(dbus_message_get_member(message), "GetControlGroupAttribute")) { DBusMessageIter iter; _cleanup_strv_free_ char **list = NULL; @@ -524,7 +524,7 @@ static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusConnection *conn if (bus_append_strv_iter(&iter, list) < 0) goto oom; - } else if (streq_ptr(dbus_message_get_member(message), "SetControlGroupAttributes")) { + } else if (streq_ptr(dbus_message_get_member(message), "SetControlGroupAttribute")) { DBusMessageIter iter; SELINUX_UNIT_ACCESS_CHECK(u, connection, message, "start"); @@ -540,7 +540,7 @@ static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusConnection *conn if (!reply) goto oom; - } else if (streq_ptr(dbus_message_get_member(message), "UnsetControlGroupAttributes")) { + } else if (streq_ptr(dbus_message_get_member(message), "UnsetControlGroupAttribute")) { DBusMessageIter iter; SELINUX_UNIT_ACCESS_CHECK(u, connection, message, "stop"); @@ -897,17 +897,17 @@ oom: return DBUS_HANDLER_RESULT_NEED_MEMORY; } -static int next_and_parse_mode(DBusMessageIter *iter, bool *runtime) { +static int parse_mode(DBusMessageIter *iter, bool *runtime, bool next) { const char *mode; + int r; assert(iter); assert(runtime); - dbus_message_iter_next(iter); - if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_STRING) - return -EINVAL; + r = bus_iter_get_basic_and_next(iter, DBUS_TYPE_STRING, &mode, next); + if (r < 0) + return r; - dbus_message_iter_get_basic(iter, &mode); if (streq(mode, "runtime")) *runtime = true; else if (streq(mode, "persistent")) @@ -919,10 +919,11 @@ static int next_and_parse_mode(DBusMessageIter *iter, bool *runtime) { } int bus_unit_cgroup_set(Unit *u, DBusMessageIter *iter) { - int r; - _cleanup_strv_free_ char **a = NULL; - char **name; + _cleanup_free_ char *controller = NULL, *old_path = NULL, *new_path = NULL, *contents = NULL; + const char *name; + CGroupBonding *b; bool runtime; + int r; assert(u); assert(iter); @@ -930,60 +931,74 @@ int bus_unit_cgroup_set(Unit *u, DBusMessageIter *iter) { if (!unit_get_exec_context(u)) return -EINVAL; - r = bus_parse_strv_iter(iter, &a); + r = bus_iter_get_basic_and_next(iter, DBUS_TYPE_STRING, &name, true); if (r < 0) return r; - r = next_and_parse_mode(iter, &runtime); + r = parse_mode(iter, &runtime, false); if (r < 0) return r; - STRV_FOREACH(name, a) { - _cleanup_free_ char *controller = NULL, *old_path = NULL, *new_path = NULL, *contents = NULL; - CGroupBonding *b; - - r = cg_split_spec(*name, &controller, &new_path); - if (r < 0) - return r; + r = cg_split_spec(name, &controller, &new_path); + if (r < 0) + return r; - b = cgroup_bonding_find_list(u->cgroup_bondings, controller); - if (b) { - old_path = strdup(b->path); - if (!old_path) - return -ENOMEM; - } + if (!new_path) { + new_path = unit_default_cgroup_path(u); + if (!new_path) + return -ENOMEM; + } - r = unit_add_cgroup_from_text(u, *name, true, &b); - if (r < 0) - return r; + if (!controller || streq(controller, SYSTEMD_CGROUP_CONTROLLER)) + return -EINVAL; - if (r > 0) { - /* Try to move things to the new place, and clean up the old place */ - cgroup_bonding_realize(b); - cgroup_bonding_migrate(b, u->cgroup_bondings); + b = cgroup_bonding_find_list(u->cgroup_bondings, controller); + if (b) { + if (streq(b->path, new_path)) + return 0; - if (old_path) - cg_trim(controller, old_path, true); - } + if (b->essential) + return -EINVAL; - contents = strjoin("[", UNIT_VTABLE(u)->exec_section, "]\n" - "ControlGroup=", *name, "\n", NULL); - if (!contents) + old_path = strdup(b->path); + if (!old_path) return -ENOMEM; + } - r = unit_write_drop_in(u, runtime, *name, contents); - if (r < 0) - return r; + r = unit_add_cgroup_from_text(u, name, true, &b); + if (r < 0) + return r; + if (r > 0) { + CGroupAttribute *a; + + /* Try to move things to the new place, and clean up the old place */ + cgroup_bonding_realize(b); + cgroup_bonding_migrate(b, u->cgroup_bondings); + + if (old_path) + cg_trim(controller, old_path, true); + + /* Apply the attributes to the new group */ + LIST_FOREACH(by_unit, a, u->cgroup_attributes) + if (streq(a->controller, controller)) + cgroup_attribute_apply(a, b); } - return 0; + contents = strjoin("[", UNIT_VTABLE(u)->exec_section, "]\n" + "ControlGroup=", name, "\n", NULL); + if (!contents) + return -ENOMEM; + + return unit_write_drop_in(u, runtime, controller, contents); } int bus_unit_cgroup_unset(Unit *u, DBusMessageIter *iter) { - _cleanup_strv_free_ char **a = NULL; - char **name; - int r; + _cleanup_free_ char *controller = NULL, *path = NULL, *target = NULL; + const char *name; + CGroupAttribute *a, *n; + CGroupBonding *b; bool runtime; + int r; assert(u); assert(iter); @@ -991,50 +1006,57 @@ int bus_unit_cgroup_unset(Unit *u, DBusMessageIter *iter) { if (!unit_get_exec_context(u)) return -EINVAL; - r = bus_parse_strv_iter(iter, &a); + r = bus_iter_get_basic_and_next(iter, DBUS_TYPE_STRING, &name, true); if (r < 0) return r; - r = next_and_parse_mode(iter, &runtime); + r = parse_mode(iter, &runtime, false); if (r < 0) return r; - STRV_FOREACH(name, a) { - _cleanup_free_ char *controller = NULL, *path = NULL, *target = NULL; - CGroupBonding *b; + r = cg_split_spec(name, &controller, &path); + if (r < 0) + return r; - r = cg_split_spec(*name, &controller, &path); - if (r < 0) - return r; + if (!controller || streq(controller, SYSTEMD_CGROUP_CONTROLLER)) + return -EINVAL; - if (!controller || streq(controller, SYSTEMD_CGROUP_CONTROLLER)) - return -EINVAL; + b = cgroup_bonding_find_list(u->cgroup_bondings, controller); + if (!b) + return -ENOENT; - unit_remove_drop_in(u, runtime, *name); + if (path && !path_equal(path, b->path)) + return -ENOENT; - b = cgroup_bonding_find_list(u->cgroup_bondings, controller); - if (!b) - continue; + if (b->essential) + return -EINVAL; - if (path && !path_equal(path, b->path)) - continue; + unit_remove_drop_in(u, runtime, controller); - if (b->essential) - return -EINVAL; + /* Try to migrate the old group away */ + if (cg_get_by_pid(controller, 0, &target) >= 0) + cgroup_bonding_migrate_to(u->cgroup_bondings, target, false); + + cgroup_bonding_free(b, true); - /* Try to migrate the old group away */ - if (cg_get_by_pid(controller, 0, &target) >= 0) - cgroup_bonding_migrate_to(u->cgroup_bondings, target, false); + /* Drop all attributes of this controller */ + LIST_FOREACH_SAFE(by_unit, a, n, u->cgroup_attributes) { + if (!streq(a->controller, controller)) + continue; - cgroup_bonding_free(b, true); + unit_remove_drop_in(u, runtime, a->name); + cgroup_attribute_free(a); } return 0; } int bus_unit_cgroup_attribute_get(Unit *u, DBusMessageIter *iter, char ***_result) { - _cleanup_strv_free_ char **l = NULL, **result = NULL; - char **name; + _cleanup_free_ char *controller = NULL; + CGroupAttribute *a; + CGroupBonding *b; + const char *name; + char **l = NULL; int r; assert(u); @@ -1044,63 +1066,99 @@ int bus_unit_cgroup_attribute_get(Unit *u, DBusMessageIter *iter, char ***_resul if (!unit_get_exec_context(u)) return -EINVAL; - r = bus_parse_strv_iter(iter, &l); + r = bus_iter_get_basic_and_next(iter, DBUS_TYPE_STRING, &name, false); if (r < 0) return r; - STRV_FOREACH(name, l) { - _cleanup_free_ char *controller = NULL; - CGroupAttribute *a; - CGroupBonding *b; + r = cg_controller_from_attr(name, &controller); + if (r < 0) + return r; + + /* First attempt, read the value from the kernel */ + b = cgroup_bonding_find_list(u->cgroup_bondings, controller); + if (b) { + _cleanup_free_ char *p = NULL, *v = NULL; - r = cg_controller_from_attr(*name, &controller); + r = cg_get_path(b->controller, b->path, name, &p); if (r < 0) return r; - /* First attempt, read the value from the kernel */ - b = cgroup_bonding_find_list(u->cgroup_bondings, controller); - if (b) { - _cleanup_free_ char *p = NULL, *v = NULL; + r = read_full_file(p, &v, NULL); + if (r >= 0) { + /* Split on new lines */ + l = strv_split_newlines(v); + if (!l) + return -ENOMEM; - r = cg_get_path(b->controller, b->path, *name, &p); - if (r < 0) - return r; + *_result = l; + return 0; - r = read_full_file(p, &v, NULL); - if (r >= 0) { - r = strv_extend(&result, v); - if (r < 0) - return r; - - continue; - } else if (r != -ENOENT) - return r; } + } - /* If that didn't work, read our cached value */ - a = cgroup_attribute_find_list(u->cgroup_attributes, NULL, *name); - if (a) { - r = strv_extend(&result, a->value); - if (r < 0) - return r; + /* If that didn't work, read our cached value */ + LIST_FOREACH(by_unit, a, u->cgroup_attributes) { + if (!cgroup_attribute_matches(a, controller, name)) continue; - } - return -ENOENT; + r = strv_extend(&l, a->value); + if (r < 0) { + strv_free(l); + return r; + } } - *_result = result; - result = NULL; + if (!l) + return -ENOENT; + *_result = l; return 0; } +static int update_attribute_drop_in(Unit *u, bool runtime, const char *name) { + _cleanup_free_ char *buf = NULL; + CGroupAttribute *a; + + assert(u); + assert(name); + + LIST_FOREACH(by_unit, a, u->cgroup_attributes) { + if (!cgroup_attribute_matches(a, NULL, name)) + continue; + + if (!buf) { + buf = strjoin("[", UNIT_VTABLE(u)->exec_section, "]\n" + "ControlGroupAttribute=", a->name, " ", a->value, "\n", NULL); + + if (!buf) + return -ENOMEM; + } else { + char *b; + + b = strjoin(buf, + "ControlGroupAttribute=", a->name, " ", a->value, "\n", NULL); + + if (!b) + return -ENOMEM; + + free(buf); + buf = b; + } + } + + if (buf) + return unit_write_drop_in(u, runtime, name, buf); + else + return unit_remove_drop_in(u, runtime, name); +} + int bus_unit_cgroup_attribute_set(Unit *u, DBusMessageIter *iter) { _cleanup_strv_free_ char **l = NULL; int r; bool runtime = false; - char **name, **value; + char **value; + const char *name; assert(u); assert(iter); @@ -1108,19 +1166,34 @@ int bus_unit_cgroup_attribute_set(Unit *u, DBusMessageIter *iter) { if (!unit_get_exec_context(u)) return -EINVAL; - r = bus_parse_strv_pairs_iter(iter, &l); + r = bus_iter_get_basic_and_next(iter, DBUS_TYPE_STRING, &name, true); + if (r < 0) + return r; + + r = bus_parse_strv_iter(iter, &l); if (r < 0) return r; - r = next_and_parse_mode(iter, &runtime); + if (!dbus_message_iter_next(iter)) + return -EINVAL; + + r = parse_mode(iter, &runtime, false); if (r < 0) return r; - STRV_FOREACH_PAIR(name, value, l) { - _cleanup_free_ char *contents = NULL; + STRV_FOREACH(value, l) { + _cleanup_free_ char *v = NULL; CGroupAttribute *a; + const CGroupSemantics *s; + + r = cgroup_semantics_find(NULL, name, *value, &v, &s); + if (r < 0) + return r; + + if (s && !s->multiple && l[1]) + return -EINVAL; - r = unit_add_cgroup_attribute(u, NULL, *name, *value, NULL, &a); + r = unit_add_cgroup_attribute(u, s, NULL, name, v ? v : *value, &a); if (r < 0) return r; @@ -1144,22 +1217,17 @@ int bus_unit_cgroup_attribute_set(Unit *u, DBusMessageIter *iter) { cgroup_attribute_apply(a, u->cgroup_bondings); } - contents = strjoin("[", UNIT_VTABLE(u)->exec_section, "]\n" - "ControlGroupAttribute=", *name, " ", *value, "\n", NULL); - if (!contents) - return -ENOMEM; - - r = unit_write_drop_in(u, runtime, *name, contents); - if (r < 0) - return r; } + r = update_attribute_drop_in(u, runtime, name); + if (r < 0) + return r; + return 0; } int bus_unit_cgroup_attribute_unset(Unit *u, DBusMessageIter *iter) { - _cleanup_strv_free_ char **l = NULL; - char **name; + const char *name; bool runtime; int r; @@ -1169,23 +1237,16 @@ int bus_unit_cgroup_attribute_unset(Unit *u, DBusMessageIter *iter) { if (!unit_get_exec_context(u)) return -EINVAL; - r = bus_parse_strv_iter(iter, &l); + r = bus_iter_get_basic_and_next(iter, DBUS_TYPE_STRING, &name, true); if (r < 0) return r; - r = next_and_parse_mode(iter, &runtime); + r = parse_mode(iter, &runtime, false); if (r < 0) return r; - STRV_FOREACH(name, l) { - CGroupAttribute *a; - - a = cgroup_attribute_find_list(u->cgroup_attributes, NULL, *name); - if (a) - cgroup_attribute_free(a); - - unit_remove_drop_in(u, runtime, *name); - } + cgroup_attribute_free_some(u->cgroup_attributes, NULL, name); + update_attribute_drop_in(u, runtime, name); return 0; } diff --git a/src/core/dbus-unit.h b/src/core/dbus-unit.h index c8903f8e5e..34980b046e 100644 --- a/src/core/dbus-unit.h +++ b/src/core/dbus-unit.h @@ -127,24 +127,25 @@ " <property name=\"DefaultControlGroup\" type=\"s\" access=\"read\"/>\n" \ " <property name=\"ControlGroups\" type=\"as\" access=\"read\"/>\n" \ " <property name=\"ControlGroupAttributes\" type=\"a(sss)\" access=\"read\"/>\n" \ - " <method name=\"GetControlGroupAttributes\">\n" \ - " <arg name=\"attributes\" type=\"as\" direction=\"in\"/>\n" \ - " <arg name=\"values\" type=\"as\" direction=\"out\"/>\n" \ - " </method>\n" \ - " <method name=\"SetControlGroupAttributes\">\n" \ - " <arg name=\"attributes\" type=\"a(ss)\" direction=\"in\"/>\n" \ + " <method name=\"SetControlGroup\">\n" \ + " <arg name=\"group\" type=\"s\" direction=\"in\"/>\n" \ " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \ " </method>\n" \ - " <method name=\"UnsetControlGroupAttributes\">\n" \ - " <arg name=\"attributes\" type=\"as\" direction=\"in\"/>\n" \ + " <method name=\"UnsetControlGroup\">\n" \ + " <arg name=\"group\" type=\"s\" direction=\"in\"/>\n" \ " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \ " </method>\n" \ - " <method name=\"SetControlGroups\">\n" \ - " <arg name=\"groups\" type=\"as\" direction=\"in\"/>\n" \ + " <method name=\"GetControlGroupAttribute\">\n" \ + " <arg name=\"attribute\" type=\"s\" direction=\"in\"/>\n" \ + " <arg name=\"values\" type=\"as\" direction=\"out\"/>\n" \ + " </method>\n" \ + " <method name=\"SetControlGroupAttribute\">\n" \ + " <arg name=\"attribute\" type=\"s\" direction=\"in\"/>\n" \ + " <arg name=\"values\" type=\"as\" direction=\"in\"/>\n" \ " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \ " </method>\n" \ - " <method name=\"UnsetControlGroups\">\n" \ - " <arg name=\"groups\" type=\"as\" direction=\"in\"/>\n" \ + " <method name=\"UnsetControlGroupAttribute\">\n" \ + " <arg name=\"attribute\" type=\"s\" direction=\"in\"/>\n" \ " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \ " </method>\n" diff --git a/src/core/execute.c b/src/core/execute.c index b28962a3c3..92cf174641 100644 --- a/src/core/execute.c +++ b/src/core/execute.c @@ -165,6 +165,14 @@ void exec_context_tty_reset(const ExecContext *context) { vt_disallocate(context->tty_path); } +static bool is_terminal_output(ExecOutput o) { + return + o == EXEC_OUTPUT_TTY || + o == EXEC_OUTPUT_SYSLOG_AND_CONSOLE || + o == EXEC_OUTPUT_KMSG_AND_CONSOLE || + o == EXEC_OUTPUT_JOURNAL_AND_CONSOLE; +} + static int open_null_as(int flags, int nfd) { int fd, r; @@ -224,7 +232,7 @@ static int connect_logger_as(const ExecContext *context, ExecOutput output, cons !!context->syslog_level_prefix, output == EXEC_OUTPUT_SYSLOG || output == EXEC_OUTPUT_SYSLOG_AND_CONSOLE, output == EXEC_OUTPUT_KMSG || output == EXEC_OUTPUT_KMSG_AND_CONSOLE, - output == EXEC_OUTPUT_SYSLOG_AND_CONSOLE || output == EXEC_OUTPUT_KMSG_AND_CONSOLE || output == EXEC_OUTPUT_JOURNAL_AND_CONSOLE); + is_terminal_output(output)); if (fd != nfd) { r = dup2(fd, nfd) < 0 ? -errno : nfd; @@ -1710,6 +1718,37 @@ int exec_context_load_environment(const ExecContext *c, char ***l) { return 0; } +static bool tty_may_match_dev_console(const char *tty) { + char *active = NULL, *console; + bool b; + + if (startswith(tty, "/dev/")) + tty += 5; + + /* trivial identity? */ + if (streq(tty, "console")) + return true; + + console = resolve_dev_console(&active); + /* if we could not resolve, assume it may */ + if (!console) + return true; + + /* "tty0" means the active VC, so it may be the same sometimes */ + b = streq(console, tty) || (streq(console, "tty0") && tty_is_vc(tty)); + free(active); + + return b; +} + +bool exec_context_may_touch_console(ExecContext *ec) { + return (ec->tty_reset || ec->tty_vhangup || ec->tty_vt_disallocate || + is_terminal_input(ec->std_input) || + is_terminal_output(ec->std_output) || + is_terminal_output(ec->std_error)) && + tty_may_match_dev_console(tty_path(ec)); +} + static void strv_fprintf(FILE *f, char **l) { char **g; diff --git a/src/core/execute.h b/src/core/execute.h index 2bcd2e1e6c..001eb0e7cc 100644 --- a/src/core/execute.h +++ b/src/core/execute.h @@ -198,6 +198,8 @@ void exec_context_tty_reset(const ExecContext *context); int exec_context_load_environment(const ExecContext *c, char ***l); +bool exec_context_may_touch_console(ExecContext *c); + void exec_status_start(ExecStatus *s, pid_t pid); void exec_status_exit(ExecStatus *s, ExecContext *context, pid_t pid, int code, int status); void exec_status_dump(ExecStatus *s, FILE *f, const char *prefix); diff --git a/src/core/job.c b/src/core/job.c index 990607f990..90de550016 100644 --- a/src/core/job.c +++ b/src/core/job.c @@ -206,6 +206,7 @@ Job* job_install(Job *j) { "Merged into running job, re-running: %s/%s as %u", uj->unit->id, job_type_to_string(uj->type), (unsigned) uj->id); uj->state = JOB_WAITING; + uj->manager->n_running_jobs--; return uj; } } @@ -483,7 +484,7 @@ static void job_change_type(Job *j, JobType newtype) { int job_run_and_invalidate(Job *j) { int r; uint32_t id; - Manager *m; + Manager *m = j->manager; assert(j); assert(j->installed); @@ -500,6 +501,7 @@ int job_run_and_invalidate(Job *j) { return -EAGAIN; j->state = JOB_RUNNING; + m->n_running_jobs++; job_add_to_dbus_queue(j); /* While we execute this operation the job might go away (for @@ -508,7 +510,6 @@ int job_run_and_invalidate(Job *j) { * store the id here, so that we can verify the job is still * valid. */ id = j->id; - m = j->manager; switch (j->type) { @@ -558,9 +559,10 @@ int job_run_and_invalidate(Job *j) { r = job_finish_and_invalidate(j, JOB_DONE, true); else if (r == -ENOEXEC) r = job_finish_and_invalidate(j, JOB_SKIPPED, true); - else if (r == -EAGAIN) + else if (r == -EAGAIN) { j->state = JOB_WAITING; - else if (r < 0) + m->n_running_jobs--; + } else if (r < 0) r = job_finish_and_invalidate(j, JOB_FAILED, true); } @@ -642,20 +644,20 @@ static void job_print_status_message(Unit *u, JobType t, JobResult result) { case JOB_DONE: if (u->condition_result) - unit_status_printf(u, ANSI_HIGHLIGHT_GREEN_ON " OK " ANSI_HIGHLIGHT_OFF, format, unit_description(u)); + unit_status_printf(u, ANSI_HIGHLIGHT_GREEN_ON " OK " ANSI_HIGHLIGHT_OFF, format); break; case JOB_FAILED: - unit_status_printf(u, ANSI_HIGHLIGHT_RED_ON "FAILED" ANSI_HIGHLIGHT_OFF, format, unit_description(u)); - unit_status_printf(u, NULL, "See 'systemctl status %s' for details.", u->id); + unit_status_printf(u, ANSI_HIGHLIGHT_RED_ON "FAILED" ANSI_HIGHLIGHT_OFF, format); + manager_status_printf(u->manager, false, NULL, "See 'systemctl status %s' for details.", u->id); break; case JOB_DEPENDENCY: - unit_status_printf(u, ANSI_HIGHLIGHT_YELLOW_ON "DEPEND" ANSI_HIGHLIGHT_OFF, format, unit_description(u)); + unit_status_printf(u, ANSI_HIGHLIGHT_YELLOW_ON "DEPEND" ANSI_HIGHLIGHT_OFF, format); break; case JOB_TIMEOUT: - unit_status_printf(u, ANSI_HIGHLIGHT_RED_ON " TIME " ANSI_HIGHLIGHT_OFF, format, unit_description(u)); + unit_status_printf(u, ANSI_HIGHLIGHT_RED_ON " TIME " ANSI_HIGHLIGHT_OFF, format); break; default: @@ -671,12 +673,12 @@ static void job_print_status_message(Unit *u, JobType t, JobResult result) { switch (result) { case JOB_TIMEOUT: - unit_status_printf(u, ANSI_HIGHLIGHT_RED_ON " TIME " ANSI_HIGHLIGHT_OFF, format, unit_description(u)); + unit_status_printf(u, ANSI_HIGHLIGHT_RED_ON " TIME " ANSI_HIGHLIGHT_OFF, format); break; case JOB_DONE: case JOB_FAILED: - unit_status_printf(u, ANSI_HIGHLIGHT_GREEN_ON " OK " ANSI_HIGHLIGHT_OFF, format, unit_description(u)); + unit_status_printf(u, ANSI_HIGHLIGHT_GREEN_ON " OK " ANSI_HIGHLIGHT_OFF, format); break; default: @@ -689,7 +691,7 @@ static void job_print_status_message(Unit *u, JobType t, JobResult result) { * Most likely a DEPEND warning from a requisiting unit will * occur next and it's nice to see what was requisited. */ if (result == JOB_SKIPPED) - unit_status_printf(u, ANSI_HIGHLIGHT_ON " INFO " ANSI_HIGHLIGHT_OFF, "%s is not active.", unit_description(u)); + unit_status_printf(u, ANSI_HIGHLIGHT_ON " INFO " ANSI_HIGHLIGHT_OFF, "%s is not active."); } } @@ -760,6 +762,9 @@ int job_finish_and_invalidate(Job *j, JobResult result, bool recursive) { j->result = result; + if (j->state == JOB_RUNNING) + j->manager->n_running_jobs--; + log_debug_unit(u->id, "Job %s/%s finished, result=%s", u->id, job_type_to_string(t), job_result_to_string(result)); diff --git a/src/core/load-fragment-gperf.gperf.m4 b/src/core/load-fragment-gperf.gperf.m4 index 0b6a5cc659..22c1761dc2 100644 --- a/src/core/load-fragment-gperf.gperf.m4 +++ b/src/core/load-fragment-gperf.gperf.m4 @@ -68,14 +68,14 @@ $1.LimitRTPRIO, config_parse_limit, RLIMIT_RTPR $1.LimitRTTIME, config_parse_limit, RLIMIT_RTTIME, offsetof($1, exec_context.rlimit) $1.ControlGroup, config_parse_unit_cgroup, 0, 0 $1.ControlGroupAttribute, config_parse_unit_cgroup_attr, 0, 0 -$1.CPUShares, config_parse_unit_cpu_shares, 0, 0 -$1.MemoryLimit, config_parse_unit_memory_limit, 0, 0 -$1.MemorySoftLimit, config_parse_unit_memory_limit, 0, 0 -$1.DeviceAllow, config_parse_unit_device_allow, 0, 0 -$1.DeviceDeny, config_parse_unit_device_allow, 0, 0 -$1.BlockIOWeight, config_parse_unit_blkio_weight, 0, 0 -$1.BlockIOReadBandwidth, config_parse_unit_blkio_bandwidth, 0, 0 -$1.BlockIOWriteBandwidth, config_parse_unit_blkio_bandwidth, 0, 0 +$1.CPUShares, config_parse_unit_cgroup_attr_pretty, 0, 0 +$1.MemoryLimit, config_parse_unit_cgroup_attr_pretty, 0, 0 +$1.MemorySoftLimit, config_parse_unit_cgroup_attr_pretty, 0, 0 +$1.DeviceAllow, config_parse_unit_cgroup_attr_pretty, 0, 0 +$1.DeviceDeny, config_parse_unit_cgroup_attr_pretty, 0, 0 +$1.BlockIOWeight, config_parse_unit_cgroup_attr_pretty, 0, 0 +$1.BlockIOReadBandwidth, config_parse_unit_cgroup_attr_pretty, 0, 0 +$1.BlockIOWriteBandwidth, config_parse_unit_cgroup_attr_pretty, 0, 0 $1.ReadWriteDirectories, config_parse_path_strv, 0, offsetof($1, exec_context.read_write_dirs) $1.ReadOnlyDirectories, config_parse_path_strv, 0, offsetof($1, exec_context.read_only_dirs) $1.InaccessibleDirectories, config_parse_path_strv, 0, offsetof($1, exec_context.inaccessible_dirs) diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c index 6e333aaf15..d79e1d936c 100644 --- a/src/core/load-fragment.c +++ b/src/core/load-fragment.c @@ -1769,7 +1769,9 @@ int config_parse_unit_cgroup_attr( void *userdata) { Unit *u = data; - _cleanup_strv_free_ char **l = NULL; + size_t a, b; + _cleanup_free_ char *n = NULL, *v = NULL; + const CGroupSemantics *s; int r; assert(filename); @@ -1784,160 +1786,24 @@ int config_parse_unit_cgroup_attr( return 0; } - l = strv_split_quoted(rvalue); - if (!l) - return log_oom(); - - if (strv_length(l) != 2) { + a = strcspn(rvalue, WHITESPACE); + b = strspn(rvalue + a, WHITESPACE); + if (a <= 0 || b <= 0) { log_error("[%s:%u] Failed to parse cgroup attribute value, ignoring: %s", filename, line, rvalue); return 0; } - r = unit_add_cgroup_attribute(u, NULL, l[0], l[1], NULL, NULL); - if (r < 0) { - log_error("[%s:%u] Failed to add cgroup attribute value, ignoring: %s", filename, line, rvalue); - return 0; - } - - return 0; -} - -int config_parse_unit_cpu_shares(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata) { - Unit *u = data; - int r; - unsigned long ul; - _cleanup_free_ char *t = NULL; - - assert(filename); - assert(lvalue); - assert(rvalue); - assert(data); - - if (safe_atolu(rvalue, &ul) < 0 || ul < 1) { - log_error("[%s:%u] Failed to parse CPU shares value, ignoring: %s", filename, line, rvalue); - return 0; - } - - if (asprintf(&t, "%lu", ul) < 0) - return log_oom(); - - r = unit_add_cgroup_attribute(u, "cpu", "cpu.shares", t, NULL, NULL); - if (r < 0) { - log_error("[%s:%u] Failed to add cgroup attribute value, ignoring: %s", filename, line, rvalue); - return 0; - } - - return 0; -} - -int config_parse_unit_memory_limit(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata) { - Unit *u = data; - int r; - off_t sz; - _cleanup_free_ char *t = NULL; - - assert(filename); - assert(lvalue); - assert(rvalue); - assert(data); - - if (parse_bytes(rvalue, &sz) < 0 || sz <= 0) { - log_error("[%s:%u] Failed to parse memory limit value, ignoring: %s", filename, line, rvalue); - return 0; - } - - if (asprintf(&t, "%llu", (unsigned long long) sz) < 0) + n = strndup(rvalue, a); + if (!n) return log_oom(); - r = unit_add_cgroup_attribute(u, - "memory", - streq(lvalue, "MemorySoftLimit") ? "memory.soft_limit_in_bytes" : "memory.limit_in_bytes", - t, NULL, NULL); + r = cgroup_semantics_find(NULL, n, rvalue + a + b, &v, &s); if (r < 0) { - log_error("[%s:%u] Failed to add cgroup attribute value, ignoring: %s", filename, line, rvalue); - return 0; - } - - return 0; -} - -static int device_map(const char *controller, const char *name, const char *value, char **ret) { - _cleanup_strv_free_ char **l = NULL; - - assert(controller); - assert(name); - assert(value); - assert(ret); - - l = strv_split_quoted(value); - if (!l) - return -ENOMEM; - - assert(strv_length(l) >= 1); - - if (streq(l[0], "*")) { - - if (asprintf(ret, "a *:*%s%s", - isempty(l[1]) ? "" : " ", strempty(l[1])) < 0) - return -ENOMEM; - } else { - struct stat st; - - if (stat(l[0], &st) < 0) { - log_warning("Couldn't stat device %s", l[0]); - return -errno; - } - - if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode)) { - log_warning("%s is not a device.", l[0]); - return -ENODEV; - } - - if (asprintf(ret, "%c %u:%u%s%s", - S_ISCHR(st.st_mode) ? 'c' : 'b', - major(st.st_rdev), minor(st.st_rdev), - isempty(l[1]) ? "" : " ", strempty(l[1])) < 0) - return -ENOMEM; - } - - return 0; -} - -int config_parse_unit_device_allow(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata) { - Unit *u = data; - _cleanup_strv_free_ char **l = NULL; - int r; - unsigned k; - - assert(filename); - assert(lvalue); - assert(rvalue); - assert(data); - - l = strv_split_quoted(rvalue); - if (!l) - return log_oom(); - - k = strv_length(l); - if (k < 1 || k > 2) { - log_error("[%s:%u] Failed to parse device value, ignoring: %s", filename, line, rvalue); - return 0; - } - - if (!streq(l[0], "*") && !path_startswith(l[0], "/dev")) { - log_error("[%s:%u] Device node path not absolute, ignoring: %s", filename, line, rvalue); - return 0; - } - - if (!isempty(l[1]) && !in_charset(l[1], "rwm")) { - log_error("[%s:%u] Device access string invalid, ignoring: %s", filename, line, rvalue); + log_error("[%s:%u] Failed to parse cgroup attribute value, ignoring: %s", filename, line, rvalue); return 0; } - r = unit_add_cgroup_attribute(u, "devices", - streq(lvalue, "DeviceAllow") ? "devices.allow" : "devices.deny", - rvalue, device_map, NULL); - + r = unit_add_cgroup_attribute(u, s, NULL, n, v ? v : rvalue + a + b, NULL); if (r < 0) { log_error("[%s:%u] Failed to add cgroup attribute value, ignoring: %s", filename, line, rvalue); return 0; @@ -1946,148 +1812,36 @@ int config_parse_unit_device_allow(const char *filename, unsigned line, const ch return 0; } -static int blkio_map(const char *controller, const char *name, const char *value, char **ret) { - struct stat st; - _cleanup_strv_free_ char **l = NULL; - dev_t d; - - assert(controller); - assert(name); - assert(value); - assert(ret); - - l = strv_split_quoted(value); - if (!l) - return log_oom(); - - assert(strv_length(l) == 2); - - if (stat(l[0], &st) < 0) { - log_warning("Couldn't stat device %s", l[0]); - return -errno; - } - - if (S_ISBLK(st.st_mode)) - d = st.st_rdev; - else if (major(st.st_dev) != 0) { - /* If this is not a device node then find the block - * device this file is stored on */ - d = st.st_dev; - - /* If this is a partition, try to get the originating - * block device */ - block_get_whole_disk(d, &d); - } else { - log_warning("%s is not a block device and file system block device cannot be determined or is not local.", l[0]); - return -ENODEV; - } - - if (asprintf(ret, "%u:%u %s", major(d), minor(d), l[1]) < 0) - return -ENOMEM; - - return 0; -} +int config_parse_unit_cgroup_attr_pretty( + const char *filename, + unsigned line, + const char *section, + const char *lvalue, + int ltype, + const char *rvalue, + void *data, + void *userdata) { -int config_parse_unit_blkio_weight(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata) { Unit *u = data; + _cleanup_free_ char *v = NULL; + const CGroupSemantics *s; int r; - unsigned long ul; - const char *device = NULL, *weight; - unsigned k; - _cleanup_free_ char *t = NULL; - _cleanup_strv_free_ char **l = NULL; assert(filename); assert(lvalue); assert(rvalue); assert(data); - l = strv_split_quoted(rvalue); - if (!l) - return log_oom(); - - k = strv_length(l); - if (k < 1 || k > 2) { - log_error("[%s:%u] Failed to parse weight value, ignoring: %s", filename, line, rvalue); - return 0; - } - - if (k == 1) - weight = l[0]; - else { - device = l[0]; - weight = l[1]; - } - - if (device && !path_is_absolute(device)) { - log_error("[%s:%u] Failed to parse block device node value, ignoring: %s", filename, line, rvalue); - return 0; - } - - if (safe_atolu(weight, &ul) < 0 || ul < 10 || ul > 1000) { - log_error("[%s:%u] Failed to parse block IO weight value, ignoring: %s", filename, line, rvalue); - return 0; - } - - if (device) - r = asprintf(&t, "%s %lu", device, ul); - else - r = asprintf(&t, "%lu", ul); - if (r < 0) - return log_oom(); - - if (device) - r = unit_add_cgroup_attribute(u, "blkio", "blkio.weight_device", t, blkio_map, NULL); - else - r = unit_add_cgroup_attribute(u, "blkio", "blkio.weight", t, NULL, NULL); + r = cgroup_semantics_find(NULL, lvalue, rvalue, &v, &s); if (r < 0) { - log_error("[%s:%u] Failed to add cgroup attribute value, ignoring: %s", filename, line, rvalue); - return 0; - } - - return 0; -} - -int config_parse_unit_blkio_bandwidth(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata) { - Unit *u = data; - int r; - off_t bytes; - unsigned k; - _cleanup_free_ char *t = NULL; - _cleanup_strv_free_ char **l = NULL; - - assert(filename); - assert(lvalue); - assert(rvalue); - assert(data); - - l = strv_split_quoted(rvalue); - if (!l) - return log_oom(); - - k = strv_length(l); - if (k != 2) { - log_error("[%s:%u] Failed to parse bandwidth value, ignoring: %s", filename, line, rvalue); + log_error("[%s:%u] Failed to parse cgroup attribute value, ignoring: %s", filename, line, rvalue); return 0; - } - - if (!path_is_absolute(l[0])) { - log_error("[%s:%u] Failed to parse block device node value, ignoring: %s", filename, line, rvalue); + } else if (r == 0) { + log_error("[%s:%u] Unknown or unsupported cgroup attribute %s, ignoring: %s", filename, line, lvalue, rvalue); return 0; } - if (parse_bytes(l[1], &bytes) < 0 || bytes <= 0) { - log_error("[%s:%u] Failed to parse block IO bandwidth value, ignoring: %s", filename, line, rvalue); - return 0; - } - - r = asprintf(&t, "%s %llu", l[0], (unsigned long long) bytes); - if (r < 0) - return log_oom(); - - r = unit_add_cgroup_attribute(u, "blkio", - streq(lvalue, "BlockIOReadBandwidth") ? "blkio.read_bps_device" : "blkio.write_bps_device", - t, blkio_map, NULL); + r = unit_add_cgroup_attribute(u, s, NULL, NULL, v, NULL); if (r < 0) { log_error("[%s:%u] Failed to add cgroup attribute value, ignoring: %s", filename, line, rvalue); return 0; diff --git a/src/core/load-fragment.h b/src/core/load-fragment.h index 421b4c33ec..dfb2ef0c3b 100644 --- a/src/core/load-fragment.h +++ b/src/core/load-fragment.h @@ -75,11 +75,7 @@ int config_parse_kill_mode(const char *filename, unsigned line, const char *sect int config_parse_notify_access(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata); int config_parse_start_limit_action(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata); int config_parse_unit_cgroup_attr(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata); -int config_parse_unit_cpu_shares(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata); -int config_parse_unit_memory_limit(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata); -int config_parse_unit_device_allow(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata); -int config_parse_unit_blkio_weight(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata); -int config_parse_unit_blkio_bandwidth(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata); +int config_parse_unit_cgroup_attr_pretty(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata); int config_parse_unit_requires_mounts_for(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata); int config_parse_syscall_filter(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata); int config_parse_environ(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata); diff --git a/src/core/manager.c b/src/core/manager.c index a578813617..ec12a75371 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -79,6 +79,11 @@ /* As soon as 5s passed since a unit was added to our GC queue, make sure to run a gc sweep */ #define GC_QUEUE_USEC_MAX (10*USEC_PER_SEC) +/* Initial delay and the interval for printing status messages about running jobs */ +#define JOBS_IN_PROGRESS_WAIT_SEC 5 +#define JOBS_IN_PROGRESS_PERIOD_SEC 1 +#define JOBS_IN_PROGRESS_PERIOD_DIVISOR 3 + /* Where clients shall send notification messages to */ #define NOTIFY_SOCKET "@/org/freedesktop/systemd1/notify" @@ -140,6 +145,146 @@ static int manager_setup_notify(Manager *m) { return 0; } +static int manager_jobs_in_progress_mod_timer(Manager *m) { + struct itimerspec its; + + zero(its); + + its.it_value.tv_sec = JOBS_IN_PROGRESS_WAIT_SEC; + its.it_interval.tv_sec = JOBS_IN_PROGRESS_PERIOD_SEC; + + if (timerfd_settime(m->jobs_in_progress_watch.fd, 0, &its, NULL) < 0) + return -errno; + + return 0; +} + +static int manager_watch_jobs_in_progress(Manager *m) { + struct epoll_event ev; + int r; + + assert(m); + + if (m->jobs_in_progress_watch.type != WATCH_INVALID) + return 0; + + m->jobs_in_progress_watch.type = WATCH_JOBS_IN_PROGRESS; + m->jobs_in_progress_watch.fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC); + if (m->jobs_in_progress_watch.fd < 0) { + log_error("Failed to create timerfd: %m"); + r = -errno; + goto err; + } + + r = manager_jobs_in_progress_mod_timer(m); + if (r < 0) { + log_error("Failed to set up timer for jobs progress watch: %s", strerror(-r)); + goto err; + } + + zero(ev); + ev.events = EPOLLIN; + ev.data.ptr = &m->jobs_in_progress_watch; + + if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->jobs_in_progress_watch.fd, &ev) < 0) { + log_error("Failed to add jobs progress timer fd to epoll: %m"); + r = -errno; + goto err; + } + + log_debug("Set up jobs progress timerfd."); + + return 0; + +err: + if (m->jobs_in_progress_watch.fd >= 0) + close_nointr_nofail(m->jobs_in_progress_watch.fd); + watch_init(&m->jobs_in_progress_watch); + return r; +} + +static void manager_unwatch_jobs_in_progress(Manager *m) { + if (m->jobs_in_progress_watch.type != WATCH_JOBS_IN_PROGRESS) + return; + + assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_DEL, m->jobs_in_progress_watch.fd, NULL) >= 0); + close_nointr_nofail(m->jobs_in_progress_watch.fd); + watch_init(&m->jobs_in_progress_watch); + m->jobs_in_progress_iteration = 0; + + log_debug("Closed jobs progress timerfd."); +} + +#define CYLON_BUFFER_EXTRA (2*strlen(ANSI_RED_ON) + strlen(ANSI_HIGHLIGHT_RED_ON) + 2*strlen(ANSI_HIGHLIGHT_OFF)) +static void draw_cylon(char buffer[], size_t buflen, unsigned width, unsigned pos) { + char *p = buffer; + + assert(buflen >= CYLON_BUFFER_EXTRA + width + 1); + assert(pos <= width+1); /* 0 or width+1 mean that the center light is behind the corner */ + + if (pos > 1) { + if (pos > 2) { + memset(p, ' ', pos-2); + p += pos-2; + } + memcpy(p, ANSI_RED_ON, strlen(ANSI_RED_ON)); + p += strlen(ANSI_RED_ON); + *p++ = '*'; + } + + if (pos > 0 && pos <= width) { + memcpy(p, ANSI_HIGHLIGHT_RED_ON, strlen(ANSI_HIGHLIGHT_RED_ON)); + p += strlen(ANSI_HIGHLIGHT_RED_ON); + *p++ = '*'; + } + + memcpy(p, ANSI_HIGHLIGHT_OFF, strlen(ANSI_HIGHLIGHT_OFF)); + p += strlen(ANSI_HIGHLIGHT_OFF); + + if (pos < width) { + memcpy(p, ANSI_RED_ON, strlen(ANSI_RED_ON)); + p += strlen(ANSI_RED_ON); + *p++ = '*'; + if (pos < width-1) { + memset(p, ' ', width-1-pos); + p += width-1-pos; + } + memcpy(p, ANSI_HIGHLIGHT_OFF, strlen(ANSI_HIGHLIGHT_OFF)); + p += strlen(ANSI_HIGHLIGHT_OFF); + } + *p = 0; +} + +static void manager_print_jobs_in_progress(Manager *m) { + Iterator i; + Job *j; + char *job_of_n = NULL; + unsigned counter = 0, print_nr; + char cylon[6 + CYLON_BUFFER_EXTRA + 1]; + unsigned cylon_pos; + + print_nr = (m->jobs_in_progress_iteration / JOBS_IN_PROGRESS_PERIOD_DIVISOR) % m->n_running_jobs; + + HASHMAP_FOREACH(j, m->jobs, i) + if (j->state == JOB_RUNNING && counter++ == print_nr) + break; + + cylon_pos = m->jobs_in_progress_iteration % 14; + if (cylon_pos >= 8) + cylon_pos = 14 - cylon_pos; + draw_cylon(cylon, sizeof(cylon), 6, cylon_pos); + + if (m->n_running_jobs > 1) + if (asprintf(&job_of_n, "(%u of %u) ", counter, m->n_running_jobs) < 0) + job_of_n = NULL; + + manager_status_printf(m, true, cylon, "%sA %s job is running for %s", + strempty(job_of_n), job_type_to_string(j->type), unit_description(j->unit)); + free(job_of_n); + + m->jobs_in_progress_iteration++; +} + static int manager_setup_time_change(Manager *m) { struct epoll_event ev; struct itimerspec its; @@ -324,6 +469,7 @@ int manager_new(SystemdRunningAs running_as, Manager **_m) { watch_init(&m->swap_watch); watch_init(&m->udev_watch); watch_init(&m->time_change_watch); + watch_init(&m->jobs_in_progress_watch); m->epoll_fd = m->dev_autofs_fd = -1; m->current_job_id = 1; /* start as id #1, so that we can leave #0 around as "null-like" value */ @@ -564,6 +710,8 @@ void manager_free(Manager *m) { close_nointr_nofail(m->notify_watch.fd); if (m->time_change_watch.fd >= 0) close_nointr_nofail(m->time_change_watch.fd); + if (m->jobs_in_progress_watch.fd >= 0) + close_nointr_nofail(m->jobs_in_progress_watch.fd); free(m->notify_socket); @@ -988,6 +1136,10 @@ unsigned manager_dispatch_run_queue(Manager *m) { } m->dispatching_run_queue = false; + + if (hashmap_size(m->jobs) > 0) + manager_watch_jobs_in_progress(m); + return n; } @@ -1527,6 +1679,16 @@ static int process_event(Manager *m, struct epoll_event *ev) { break; } + case WATCH_JOBS_IN_PROGRESS: { + uint64_t v; + + /* not interested in the data */ + read(w->fd, &v, sizeof(v)); + + manager_print_jobs_in_progress(m); + break; + } + default: log_error("event type=%i", w->type); assert_not_reached("Unknown epoll event type."); @@ -2152,7 +2314,7 @@ finish: return r; } -bool manager_is_booting_or_shutting_down(Manager *m) { +static bool manager_is_booting_or_shutting_down(Manager *m) { Unit *u; assert(m); @@ -2199,8 +2361,10 @@ void manager_check_finished(Manager *m) { assert(m); - if (hashmap_size(m->jobs) > 0) + if (hashmap_size(m->jobs) > 0) { + manager_jobs_in_progress_mod_timer(m); return; + } /* Notify Type=idle units that we are done now */ close_pipe(m->idle_pipe); @@ -2208,6 +2372,8 @@ void manager_check_finished(Manager *m) { /* Turn off confirm spawn now */ m->confirm_spawn = false; + manager_unwatch_jobs_in_progress(m); + if (dual_timestamp_is_set(&m->finish_timestamp)) return; @@ -2479,7 +2645,7 @@ void manager_set_show_status(Manager *m, bool b) { unlink("/run/systemd/show-status"); } -bool manager_get_show_status(Manager *m) { +static bool manager_get_show_status(Manager *m) { assert(m); if (m->running_as != SYSTEMD_SYSTEM) @@ -2494,6 +2660,25 @@ bool manager_get_show_status(Manager *m) { return plymouth_running(); } +void manager_status_printf(Manager *m, bool ephemeral, const char *status, const char *format, ...) { + va_list ap; + + if (!manager_get_show_status(m)) + return; + + /* XXX We should totally drop the check for ephemeral here + * and thus effectively make 'Type=idle' pointless. */ + if (ephemeral && m->n_on_console > 0) + return; + + if (!manager_is_booting_or_shutting_down(m)) + return; + + va_start(ap, format); + status_vprintf(status, true, ephemeral, format, ap); + va_end(ap); +} + void watch_init(Watch *w) { assert(w); diff --git a/src/core/manager.h b/src/core/manager.h index cc4edf8f1e..c486a16887 100644 --- a/src/core/manager.h +++ b/src/core/manager.h @@ -61,7 +61,8 @@ enum WatchType { WATCH_UDEV, WATCH_DBUS_WATCH, WATCH_DBUS_TIMEOUT, - WATCH_TIME_CHANGE + WATCH_TIME_CHANGE, + WATCH_JOBS_IN_PROGRESS }; struct Watch { @@ -127,6 +128,7 @@ struct Manager { Watch notify_watch; Watch signal_watch; Watch time_change_watch; + Watch jobs_in_progress_watch; int epoll_fd; @@ -225,6 +227,11 @@ struct Manager { unsigned n_installed_jobs; unsigned n_failed_jobs; + /* Jobs in progress watching */ + unsigned n_running_jobs; + unsigned n_on_console; + unsigned jobs_in_progress_iteration; + /* Type=idle pipes */ int idle_pipe[2]; @@ -276,8 +283,6 @@ int manager_distribute_fds(Manager *m, FDSet *fds); int manager_reload(Manager *m); -bool manager_is_booting_or_shutting_down(Manager *m); - void manager_reset_failed(Manager *m); void manager_send_unit_audit(Manager *m, Unit *u, int type, bool success); @@ -293,6 +298,6 @@ void manager_undo_generators(Manager *m); void manager_recheck_journal(Manager *m); void manager_set_show_status(Manager *m, bool b); -bool manager_get_show_status(Manager *m); +void manager_status_printf(Manager *m, bool ephemeral, const char *status, const char *format, ...); void watch_init(Watch *w); diff --git a/src/core/mount-setup.c b/src/core/mount-setup.c index e7e2736615..dab3601467 100644 --- a/src/core/mount-setup.c +++ b/src/core/mount-setup.c @@ -77,10 +77,6 @@ static const MountPoint mount_table[] = { NULL, MNT_FATAL|MNT_IN_CONTAINER }, { "securityfs", "/sys/kernel/security", "securityfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, NULL, MNT_NONE }, -#ifdef ENABLE_EFI - { "efivarfs", "/sys/firmware/efi/efivars", "efivarfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, - is_efi_boot, MNT_NONE }, -#endif { "tmpfs", "/dev/shm", "tmpfs", "mode=1777", MS_NOSUID|MS_NODEV|MS_STRICTATIME, NULL, MNT_FATAL|MNT_IN_CONTAINER }, { "devpts", "/dev/pts", "devpts", "mode=620,gid=" STRINGIFY(TTY_GID), MS_NOSUID|MS_NOEXEC, @@ -91,6 +87,12 @@ static const MountPoint mount_table[] = { NULL, MNT_IN_CONTAINER }, { "cgroup", "/sys/fs/cgroup/systemd", "cgroup", "none,name=systemd", MS_NOSUID|MS_NOEXEC|MS_NODEV, NULL, MNT_IN_CONTAINER }, + { "pstore", "/sys/fs/pstore", "pstore", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, + NULL, MNT_NONE }, +#ifdef ENABLE_EFI + { "efivarfs", "/sys/firmware/efi/efivars", "efivarfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, + is_efi_boot, MNT_NONE }, +#endif }; /* These are API file systems that might be mounted by other software, diff --git a/src/core/transaction.c b/src/core/transaction.c index 0329366350..4a8d90e6e5 100644 --- a/src/core/transaction.c +++ b/src/core/transaction.c @@ -396,8 +396,8 @@ static int transaction_verify_order_one(Transaction *tr, Job *j, Job *from, unsi "Job %s/%s deleted to break ordering cycle starting with %s/%s", delete->unit->id, job_type_to_string(delete->type), j->unit->id, job_type_to_string(j->type)); - status_printf(ANSI_HIGHLIGHT_RED_ON " SKIP " ANSI_HIGHLIGHT_OFF, true, - "Ordering cycle found, skipping %s", unit_description(delete->unit)); + unit_status_printf(delete->unit, ANSI_HIGHLIGHT_RED_ON " SKIP " ANSI_HIGHLIGHT_OFF, + "Ordering cycle found, skipping %s"); transaction_delete_unit(tr, delete->unit); return -EAGAIN; } diff --git a/src/core/unit.c b/src/core/unit.c index 3a88996eb7..e2c06ae8b6 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -749,15 +749,13 @@ void unit_dump(Unit *u, FILE *f, const char *prefix) { prefix, b->controller, b->path); LIST_FOREACH(by_unit, a, u->cgroup_attributes) { - char *v = NULL; + _cleanup_free_ char *v = NULL; - if (a->map_callback) - a->map_callback(a->controller, a->name, a->value, &v); + if (a->semantics && a->semantics->map_write) + a->semantics->map_write(a->semantics, a->value, &v); fprintf(f, "%s\tControlGroupAttribute: %s %s \"%s\"\n", prefix, a->controller, a->name, v ? v : a->value); - - free(v); } if (UNIT_VTABLE(u)->dump) @@ -996,7 +994,7 @@ static void unit_status_print_starting_stopping(Unit *u, JobType t) { if (!format) return; - unit_status_printf(u, "", format, unit_description(u)); + unit_status_printf(u, "", format); } #pragma GCC diagnostic push @@ -1322,6 +1320,7 @@ void unit_trigger_on_failure(Unit *u) { } void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns, bool reload_success) { + Manager *m; bool unexpected; assert(u); @@ -1334,7 +1333,9 @@ void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns, bool reload_su * behavior here. For example: if a mount point is remounted * this function will be called too! */ - if (u->manager->n_reloading <= 0) { + m = u->manager; + + if (m->n_reloading <= 0) { dual_timestamp ts; dual_timestamp_get(&ts); @@ -1356,6 +1357,21 @@ void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns, bool reload_su if (UNIT_IS_INACTIVE_OR_FAILED(ns)) cgroup_bonding_trim_list(u->cgroup_bondings, true); + if (UNIT_IS_INACTIVE_OR_FAILED(os) != UNIT_IS_INACTIVE_OR_FAILED(ns)) { + ExecContext *ec = unit_get_exec_context(u); + if (ec && exec_context_may_touch_console(ec)) { + /* XXX The counter may get out of sync if the admin edits + * TTY-related unit file properties and issues a daemon-reload + * while the unit is active. No big deal though, because + * it influences only the printing of boot/shutdown + * status messages. */ + if (UNIT_IS_INACTIVE_OR_FAILED(ns)) + m->n_on_console--; + else + m->n_on_console++; + } + } + if (u->job) { unexpected = false; @@ -1422,7 +1438,7 @@ void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns, bool reload_su } else unexpected = true; - if (u->manager->n_reloading <= 0) { + if (m->n_reloading <= 0) { /* If this state change happened without being * requested by a job, then let's retroactively start @@ -1458,18 +1474,18 @@ void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns, bool reload_su /* The bus just might have become available, * hence try to connect to it, if we aren't * yet connected. */ - bus_init(u->manager, true); + bus_init(m, true); if (u->type == UNIT_SERVICE && !UNIT_IS_ACTIVE_OR_RELOADING(os) && - u->manager->n_reloading <= 0) { + m->n_reloading <= 0) { /* Write audit record if we have just finished starting up */ - manager_send_unit_audit(u->manager, u, AUDIT_SERVICE_START, true); + manager_send_unit_audit(m, u, AUDIT_SERVICE_START, true); u->in_audit = true; } if (!UNIT_IS_ACTIVE_OR_RELOADING(os)) - manager_send_unit_plymouth(u->manager, u); + manager_send_unit_plymouth(m, u); } else { @@ -1479,25 +1495,25 @@ void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns, bool reload_su if (u->type == UNIT_SERVICE && UNIT_IS_INACTIVE_OR_FAILED(ns) && !UNIT_IS_INACTIVE_OR_FAILED(os) && - u->manager->n_reloading <= 0) { + m->n_reloading <= 0) { /* Hmm, if there was no start record written * write it now, so that we always have a nice * pair */ if (!u->in_audit) { - manager_send_unit_audit(u->manager, u, AUDIT_SERVICE_START, ns == UNIT_INACTIVE); + manager_send_unit_audit(m, u, AUDIT_SERVICE_START, ns == UNIT_INACTIVE); if (ns == UNIT_INACTIVE) - manager_send_unit_audit(u->manager, u, AUDIT_SERVICE_STOP, true); + manager_send_unit_audit(m, u, AUDIT_SERVICE_STOP, true); } else /* Write audit record if we have just finished shutting down */ - manager_send_unit_audit(u->manager, u, AUDIT_SERVICE_STOP, ns == UNIT_INACTIVE); + manager_send_unit_audit(m, u, AUDIT_SERVICE_STOP, ns == UNIT_INACTIVE); u->in_audit = false; } } - manager_recheck_journal(u->manager); + manager_recheck_journal(m); /* Maybe we finished startup and are now ready for being * stopped because unneeded? */ @@ -1900,30 +1916,12 @@ finish: } int set_unit_path(const char *p) { - char *cwd, *c; - int r; + _cleanup_free_ char *c = NULL; /* This is mostly for debug purposes */ - - if (path_is_absolute(p)) { - if (!(c = strdup(p))) - return -ENOMEM; - } else { - if (!(cwd = get_current_dir_name())) - return -errno; - - r = asprintf(&c, "%s/%s", cwd, p); - free(cwd); - - if (r < 0) - return -ENOMEM; - } - - if (setenv("SYSTEMD_UNIT_PATH", c, 0) < 0) { - r = -errno; - free(c); - return r; - } + c = path_make_absolute_cwd(p); + if (setenv("SYSTEMD_UNIT_PATH", c, 0) < 0) + return -errno; return 0; } @@ -2109,7 +2107,7 @@ static int unit_add_one_default_cgroup(Unit *u, const char *controller) { if (r < 0) goto fail; - return 0; + return 1; fail: free(b->path); @@ -2153,10 +2151,10 @@ CGroupBonding* unit_get_default_cgroup(Unit *u) { int unit_add_cgroup_attribute( Unit *u, + const CGroupSemantics *semantics, const char *controller, const char *name, const char *value, - CGroupAttributeMapCallback map_callback, CGroupAttribute **ret) { _cleanup_free_ char *c = NULL; @@ -2164,48 +2162,67 @@ int unit_add_cgroup_attribute( int r; assert(u); - assert(name); assert(value); + if (semantics) { + /* Semantics always take precedence */ + if (semantics->name) + name = semantics->name; + + if (semantics->controller) + controller = semantics->controller; + } + + if (!name) + return -EINVAL; + if (!controller) { r = cg_controller_from_attr(name, &c); if (r < 0) return -EINVAL; controller = c; - } else { - if (!filename_is_safe(name)) - return -EINVAL; - - if (!filename_is_safe(controller)) - return -EINVAL; } if (!controller || streq(controller, SYSTEMD_CGROUP_CONTROLLER)) return -EINVAL; + if (!filename_is_safe(name)) + return -EINVAL; + + if (!filename_is_safe(controller)) + return -EINVAL; + + /* Check if this attribute already exists. Note that we will + * explicitly check for the value here too, as there are + * attributes which accept multiple values. */ a = cgroup_attribute_find_list(u->cgroup_attributes, controller, name); if (a) { - char *v; - if (streq(value, a->value)) { + /* Exactly the same value is always OK, let's ignore this */ if (ret) *ret = a; return 0; } - v = strdup(value); - if (!v) - return -ENOMEM; + if (semantics && !semantics->multiple) { + char *v; - free(a->value); - a->value = v; + /* If this is a single-item entry, we can + * simply patch the existing attribute */ - if (ret) - *ret = a; + v = strdup(value); + if (!v) + return -ENOMEM; + + free(a->value); + a->value = v; - return 1; + if (ret) + *ret = a; + return 1; + } } a = new0(CGroupAttribute, 1); @@ -2226,11 +2243,10 @@ int unit_add_cgroup_attribute( free(a->name); free(a->value); free(a); - return -ENOMEM; } - a->map_callback = map_callback; + a->semantics = semantics; a->unit = u; LIST_PREPEND(CGroupAttribute, by_unit, u->cgroup_attributes, a); @@ -2537,21 +2553,8 @@ int unit_coldplug(Unit *u) { return 0; } -void unit_status_printf(Unit *u, const char *status, const char *format, ...) { - va_list ap; - - assert(u); - assert(format); - - if (!manager_get_show_status(u->manager)) - return; - - if (!manager_is_booting_or_shutting_down(u->manager)) - return; - - va_start(ap, format); - status_vprintf(status, true, format, ap); - va_end(ap); +void unit_status_printf(Unit *u, const char *status, const char *unit_status_msg_format) { + manager_status_printf(u->manager, false, status, unit_status_msg_format, unit_description(u)); } bool unit_need_daemon_reload(Unit *u) { @@ -2763,52 +2766,77 @@ 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; +static int drop_in_file(Unit *u, bool runtime, const char *name, char **_p, char **_q) { + char *p, *q; + int r; + assert(u); + assert(name); + assert(_p); + assert(_q); - if (u->manager->running_as != SYSTEMD_SYSTEM) + if (u->manager->running_as == SYSTEMD_USER && runtime) return -ENOTSUP; if (!filename_is_safe(name)) return -EINVAL; - p = strjoin(runtime ? "/run/systemd/system/" : "/etc/systemd/system/", u->id, ".d", NULL); + if (u->manager->running_as == SYSTEMD_USER) { + _cleanup_free_ char *c = NULL; + + r = user_config_home(&c); + if (r < 0) + return r; + if (r == 0) + return -ENOENT; + + p = strjoin(c, "/", u->id, ".d", NULL); + } else if (runtime) + p = strjoin("/run/systemd/system/", u->id, ".d", NULL); + else + p = strjoin("/etc/systemd/system/", u->id, ".d", NULL); if (!p) return -ENOMEM; q = strjoin(p, "/50-", name, ".conf", NULL); - if (!q) + if (!q) { + free(p); return -ENOMEM; + } - mkdir_p(p, 0755); - return write_one_line_file_atomic_label(q, data); + *_p = p; + *_q = q; + return 0; } -int unit_remove_drop_in(Unit *u, bool runtime, const char *name) { +int unit_write_drop_in(Unit *u, bool runtime, const char *name, const char *data) { _cleanup_free_ char *p = NULL, *q = NULL; + int r; assert(u); - if (u->manager->running_as != SYSTEMD_SYSTEM) - return -ENOTSUP; + r = drop_in_file(u, runtime, name, &p, &q); + if (r < 0) + return r; - if (!filename_is_safe(name)) - return -EINVAL; + mkdir_p(p, 0755); + return write_one_line_file_atomic_label(q, data); +} - p = strjoin(runtime ? "/run/systemd/system/" : "/etc/systemd/system/", u->id, ".d", NULL); - if (!p) - return -ENOMEM; +int unit_remove_drop_in(Unit *u, bool runtime, const char *name) { + _cleanup_free_ char *p = NULL, *q = NULL; + int r; - q = strjoin(p, "/50-", name, ".conf", NULL); - if (!q) - return -ENOMEM; + assert(u); + r = drop_in_file(u, runtime, name, &p, &q); if (unlink(q) < 0) - return -errno; + r = -errno; + else + r = 0; rmdir(p); - return 0; + return r; } int unit_kill_context( diff --git a/src/core/unit.h b/src/core/unit.h index c90210302d..9029d6225b 100644 --- a/src/core/unit.h +++ b/src/core/unit.h @@ -40,6 +40,7 @@ typedef struct UnitStatusMessageFormats UnitStatusMessageFormats; #include "condition.h" #include "install.h" #include "unit-name.h" +#include "cgroup-semantics.h" enum UnitActiveState { UNIT_ACTIVE, @@ -445,7 +446,7 @@ int unit_add_cgroup(Unit *u, CGroupBonding *b); int unit_add_cgroup_from_text(Unit *u, const char *name, bool overwrite, CGroupBonding **ret); int unit_add_default_cgroups(Unit *u); CGroupBonding* unit_get_default_cgroup(Unit *u); -int unit_add_cgroup_attribute(Unit *u, const char *controller, const char *name, const char *value, CGroupAttributeMapCallback map_callback, CGroupAttribute **ret); +int unit_add_cgroup_attribute(Unit *u, const CGroupSemantics *semantics, const char *controller, const char *name, const char *value, CGroupAttribute **ret); int unit_choose_id(Unit *u, const char *name); int unit_set_description(Unit *u, const char *description); @@ -519,7 +520,7 @@ int unit_add_node_link(Unit *u, const char *what, bool wants); int unit_coldplug(Unit *u); -void unit_status_printf(Unit *u, const char *status, const char *format, ...); +void unit_status_printf(Unit *u, const char *status, const char *unit_status_msg_format); bool unit_need_daemon_reload(Unit *u); |