diff options
author | Lennart Poettering <lennart@poettering.net> | 2013-01-18 01:44:41 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2013-01-18 01:44:41 +0100 |
commit | d2a30975827b3447ca0fd5a2c06ec1ff15ce7f0f (patch) | |
tree | 65d593f4f9d129354ab081b6476ce46a596dd7a6 /src/core | |
parent | d54110d11d5ea3381cfdd129356b91669b547216 (diff) |
systemctl: add new "get-cgroup-attr" to query current cgroup attribute value
Also adds a pair of bus calls for this to the daemon.
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/cgroup-attr.c | 10 | ||||
-rw-r--r-- | src/core/dbus-manager.c | 53 | ||||
-rw-r--r-- | src/core/dbus-unit.c | 89 | ||||
-rw-r--r-- | src/core/dbus-unit.h | 19 |
4 files changed, 148 insertions, 23 deletions
diff --git a/src/core/cgroup-attr.c b/src/core/cgroup-attr.c index cedf37de50..aed4e99d86 100644 --- a/src/core/cgroup-attr.c +++ b/src/core/cgroup-attr.c @@ -25,8 +25,7 @@ int cgroup_attribute_apply(CGroupAttribute *a, CGroupBonding *b) { int r; - char *path = NULL; - char *v = NULL; + _cleanup_free_ char *path = NULL, *v = NULL; assert(a); @@ -41,18 +40,13 @@ int cgroup_attribute_apply(CGroupAttribute *a, CGroupBonding *b) { } r = cg_get_path(a->controller, b->path, a->name, &path); - if (r < 0) { - free(v); + if (r < 0) return r; - } r = write_one_line_file(path, v ? v : a->value); if (r < 0) log_warning("Failed to write '%s' to %s: %s", v ? v : a->value, path, strerror(-r)); - free(path); - free(v); - return r; } diff --git a/src/core/dbus-manager.c b/src/core/dbus-manager.c index 1d785a2347..b7829572f5 100644 --- a/src/core/dbus-manager.c +++ b/src/core/dbus-manager.c @@ -102,15 +102,10 @@ " <method name=\"ResetFailedUnit\">\n" \ " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \ " </method>\n" \ - " <method name=\"SetUnitControlGroups\">\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" \ - " </method>\n" \ - " <method name=\"UnsetUnitControlGroups\">\n" \ + " <method name=\"GetUnitControlGroupAttributes\">\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=\"attributes\" type=\"as\" direction=\"in\"/>\n" \ + " <arg name=\"values\" type=\"as\" direction=\"out\"/>\n" \ " </method>\n" \ " <method name=\"SetUnitControlGroupAttributes\">\n" \ " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \ @@ -122,6 +117,16 @@ " <arg name=\"attributes\" type=\"a(ss)\" direction=\"in\"/>\n" \ " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \ " </method>\n" \ + " <method name=\"SetUnitControlGroups\">\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" \ + " </method>\n" \ + " <method name=\"UnsetUnitControlGroups\">\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/>" \ + " </method>\n" \ " <method name=\"GetJob\">\n" \ " <arg name=\"id\" type=\"u\" direction=\"in\"/>\n" \ " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \ @@ -979,6 +984,38 @@ 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")) { + const char *name; + Unit *u; + DBusMessageIter iter; + _cleanup_strv_free_ char **list = NULL; + + if (!dbus_message_iter_init(message, &iter)) + goto oom; + + r = bus_iter_get_basic_and_next(&iter, DBUS_TYPE_STRING, &name, true); + if (r < 0) + return bus_send_error_reply(connection, message, NULL, r); + + u = manager_get_unit(m, name); + if (!u) { + dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name); + return bus_send_error_reply(connection, message, &error, -ENOENT); + } + + 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); + + reply = dbus_message_new_method_return(message); + if (!reply) + goto oom; + + dbus_message_iter_init_append(reply, &iter); + if (bus_append_strv_iter(&iter, list) < 0) + goto oom; + } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) { DBusMessageIter iter, sub; Iterator i; diff --git a/src/core/dbus-unit.c b/src/core/dbus-unit.c index c7bf043764..d2f8ebf760 100644 --- a/src/core/dbus-unit.c +++ b/src/core/dbus-unit.c @@ -502,6 +502,27 @@ static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusConnection *conn reply = dbus_message_new_method_return(message); if (!reply) goto oom; + } else if (streq_ptr(dbus_message_get_member(message), "GetControlGroupAttributes")) { + DBusMessageIter iter; + _cleanup_strv_free_ char **list = NULL; + + SELINUX_UNIT_ACCESS_CHECK(u, connection, message, "status"); + + if (!dbus_message_iter_init(message, &iter)) + goto oom; + + r = bus_unit_cgroup_attribute_get(u, &iter, &list); + if (r < 0) + return bus_send_error_reply(connection, message, NULL, r); + + reply = dbus_message_new_method_return(message); + if (!reply) + goto oom; + + dbus_message_iter_init_append(reply, &iter); + if (bus_append_strv_iter(&iter, list) < 0) + goto oom; + } else if (streq_ptr(dbus_message_get_member(message), "SetControlGroupAttributes")) { DBusMessageIter iter; @@ -965,6 +986,74 @@ int bus_unit_cgroup_unset(Unit *u, DBusMessageIter *iter) { return 0; } +int bus_unit_cgroup_attribute_get(Unit *u, DBusMessageIter *iter, char ***_result) { + _cleanup_strv_free_ char **l = NULL, **result = NULL; + char **name; + int r; + + assert(u); + assert(iter); + assert(_result); + + if (!unit_get_exec_context(u)) + return -EINVAL; + + r = bus_parse_strv_iter(iter, &l); + if (r < 0) + return r; + + STRV_FOREACH(name, l) { + _cleanup_free_ char *controller = NULL; + const char *dot; + CGroupAttribute *a; + CGroupBonding *b; + + dot = strchr(*name, '.'); + if (dot) { + controller = strndup(*name, dot - *name); + if (!controller) + return -ENOMEM; + } + + /* 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_get_path(b->controller, b->path, *name, &p); + if (r < 0) + return r; + + 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; + + continue; + } + + return -ENOENT; + } + + *_result = result; + result = NULL; + + return 0; +} + int bus_unit_cgroup_attribute_set(Unit *u, DBusMessageIter *iter) { DBusMessageIter sub, sub2; int r; diff --git a/src/core/dbus-unit.h b/src/core/dbus-unit.h index c7d5863a97..c8903f8e5e 100644 --- a/src/core/dbus-unit.h +++ b/src/core/dbus-unit.h @@ -127,13 +127,9 @@ " <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=\"SetControlGroups\">\n" \ - " <arg name=\"groups\" 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" \ - " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\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" \ @@ -142,6 +138,14 @@ " <method name=\"UnsetControlGroupAttributes\">\n" \ " <arg name=\"attributes\" type=\"as\" 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" \ + " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \ + " </method>\n" \ + " <method name=\"UnsetControlGroups\">\n" \ + " <arg name=\"groups\" type=\"as\" direction=\"in\"/>\n" \ + " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \ " </method>\n" #define BUS_UNIT_INTERFACES_LIST \ @@ -164,6 +168,7 @@ DBusHandlerResult bus_unit_queue_job( int bus_unit_cgroup_set(Unit *u, DBusMessageIter *iter); int bus_unit_cgroup_unset(Unit *u, DBusMessageIter *iter); +int bus_unit_cgroup_attribute_get(Unit *u, DBusMessageIter *iter, char ***_result); int bus_unit_cgroup_attribute_set(Unit *u, DBusMessageIter *iter); int bus_unit_cgroup_attribute_unset(Unit *u, DBusMessageIter *iter); |