1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <dbus/dbus.h>
24 #include "dbus-cgroup.h"
26 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_cgroup_append_device_policy, cgroup_device_policy, CGroupDevicePolicy);
28 static int bus_cgroup_append_device_weights(DBusMessageIter *i, const char *property, void *data) {
29 DBusMessageIter sub, sub2;
30 CGroupContext *c = data;
31 CGroupBlockIODeviceWeight *w;
37 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "(st)", &sub))
40 LIST_FOREACH(device_weights, w, c->blockio_device_weights) {
42 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2) ||
43 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &w->path) ||
44 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT64, &w->weight) ||
45 !dbus_message_iter_close_container(&sub, &sub2))
49 if (!dbus_message_iter_close_container(i, &sub))
55 static int bus_cgroup_append_device_bandwidths(DBusMessageIter *i, const char *property, void *data) {
56 DBusMessageIter sub, sub2;
57 CGroupContext *c = data;
58 CGroupBlockIODeviceBandwidth *b;
64 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "(st)", &sub))
67 LIST_FOREACH(device_bandwidths, b, c->blockio_device_bandwidths) {
69 if (streq(property, "BlockIOReadBandwidth") != b->read)
72 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2) ||
73 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &b->path) ||
74 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT64, &b->bandwidth) ||
75 !dbus_message_iter_close_container(&sub, &sub2))
79 if (!dbus_message_iter_close_container(i, &sub))
85 static int bus_cgroup_append_device_allow(DBusMessageIter *i, const char *property, void *data) {
86 DBusMessageIter sub, sub2;
87 CGroupContext *c = data;
94 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "(ss)", &sub))
97 LIST_FOREACH(device_allow, a, c->device_allow) {
112 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2) ||
113 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &a->path) ||
114 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &rwm) ||
115 !dbus_message_iter_close_container(&sub, &sub2))
119 if (!dbus_message_iter_close_container(i, &sub))
125 const BusProperty bus_cgroup_context_properties[] = {
126 { "CPUAccounting", bus_property_append_bool, "b", offsetof(CGroupContext, cpu_accounting) },
127 { "CPUShares", bus_property_append_ul, "t", offsetof(CGroupContext, cpu_shares) },
128 { "BlockIOAccounting", bus_property_append_bool, "b", offsetof(CGroupContext, blockio_accounting) },
129 { "BlockIOWeight", bus_property_append_ul, "t", offsetof(CGroupContext, blockio_weight) },
130 { "BlockIODeviceWeight", bus_cgroup_append_device_weights, "a(st)", 0 },
131 { "BlockIOReadBandwidth", bus_cgroup_append_device_bandwidths, "a(st)", 0 },
132 { "BlockIOWriteBandwidth", bus_cgroup_append_device_bandwidths, "a(st)", 0 },
133 { "MemoryAccounting", bus_property_append_bool, "b", offsetof(CGroupContext, memory_accounting) },
134 { "MemoryLimit", bus_property_append_uint64, "t", offsetof(CGroupContext, memory_limit) },
135 { "MemorySoftLimit", bus_property_append_uint64, "t", offsetof(CGroupContext, memory_soft_limit) },
136 { "DevicePolicy", bus_cgroup_append_device_policy, "s", offsetof(CGroupContext, device_policy) },
137 { "DeviceAllow", bus_cgroup_append_device_allow, "a(ss)", 0 },
141 int bus_cgroup_set_property(
146 UnitSetPropertiesMode mode,
154 if (streq(name, "CPUAccounting")) {
156 if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_BOOLEAN)
159 if (mode != UNIT_CHECK) {
161 dbus_message_iter_get_basic(i, &b);
163 c->cpu_accounting = b;
164 unit_write_drop_in_private_section(u, mode, "cpu-accounting", b ? "CPUAccounting=yes" : "CPUAccounting=no");
169 } else if (streq(name, "CPUShares")) {
173 if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_UINT64)
176 dbus_message_iter_get_basic(i, &u64);
177 ul = (unsigned long) u64;
179 if (u64 <= 0 || u64 != (uint64_t) ul)
182 if (mode != UNIT_CHECK) {
183 char buf[sizeof("CPUShares=") + DECIMAL_STR_MAX(ul)];
186 sprintf(buf, "CPUShares=%lu", ul);
187 unit_write_drop_in_private_section(u, mode, "cpu-shares", buf);
192 } else if (streq(name, "BlockIOAccounting")) {
194 if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_BOOLEAN)
197 if (mode != UNIT_CHECK) {
199 dbus_message_iter_get_basic(i, &b);
201 c->blockio_accounting = b;
202 unit_write_drop_in_private_section(u, mode, "block-io-accounting", b ? "BlockIOAccounting=yes" : "BlockIOAccounting=no");
207 } else if (streq(name, "BlockIOWeight")) {
211 if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_UINT64)
214 dbus_message_iter_get_basic(i, &u64);
215 ul = (unsigned long) u64;
217 if (u64 < 10 || u64 > 1000)
220 if (mode != UNIT_CHECK) {
221 char buf[sizeof("BlockIOWeight=") + DECIMAL_STR_MAX(ul)];
224 sprintf(buf, "BlockIOWeight=%lu", ul);
225 unit_write_drop_in_private_section(u, mode, "blockio-weight", buf);
230 } else if (streq(name, "MemoryAccounting")) {
232 if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_BOOLEAN)
235 if (mode != UNIT_CHECK) {
237 dbus_message_iter_get_basic(i, &b);
239 c->memory_accounting = b;
240 unit_write_drop_in_private_section(u, mode, "memory-accounting", b ? "MemoryAccounting=yes" : "MemoryAccounting=no");
245 } else if (streq(name, "MemoryLimit") || streq(name, "MemorySoftLimit")) {
247 if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_UINT64)
250 if (mode != UNIT_CHECK) {
252 char buf[sizeof("MemorySoftLimit=") + DECIMAL_STR_MAX(limit)];
254 dbus_message_iter_get_basic(i, &limit);
256 if (streq(name, "MemoryLimit")) {
257 c->memory_limit = limit;
258 sprintf(buf, "MemoryLimit=%" PRIu64, limit);
259 unit_write_drop_in_private_section(u, mode, "memory-limit", buf);
261 c->memory_soft_limit = limit;
262 sprintf(buf, "MemorySoftLimit=%" PRIu64, limit);
263 unit_write_drop_in_private_section(u, mode, "memory-soft-limit", buf);