cgroup.c revision be2c1bd2a843aa61901086fccbae15b3aa085fb1
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2013 Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is free software; you can redistribute it and/or modify it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering under the terms of the GNU Lesser General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poetteringvoid cgroup_context_init(CGroupContext *c) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* Initialize everything to the kernel defaults, assuming the
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * structure is preinitialized to 0 */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering c->memory_limit = c->memory_soft_limit = (uint64_t) -1;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringvoid cgroup_context_free_device_allow(CGroupContext *c, CGroupDeviceAllow *a) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering LIST_REMOVE(CGroupDeviceAllow, device_allow, c->device_allow, a);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringvoid cgroup_context_free_blockio_device_weight(CGroupContext *c, CGroupBlockIODeviceWeight *w) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering LIST_REMOVE(CGroupBlockIODeviceWeight, device_weights, c->blockio_device_weights, w);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringvoid cgroup_context_free_blockio_device_bandwidth(CGroupContext *c, CGroupBlockIODeviceBandwidth *b) {
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering LIST_REMOVE(CGroupBlockIODeviceBandwidth, device_bandwidths, c->blockio_device_bandwidths, b);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringvoid cgroup_context_done(CGroupContext *c) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering cgroup_context_free_blockio_device_weight(c, c->blockio_device_weights);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering cgroup_context_free_blockio_device_bandwidth(c, c->blockio_device_bandwidths);
c0eb11cfd016381fe02875a4ef29c1ade00c94e7Lennart Poettering cgroup_context_free_device_allow(c, c->device_allow);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringvoid cgroup_context_dump(CGroupContext *c, FILE* f, const char *prefix) {
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering "%sCPUAccounting=%s\n"
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering "%sBlockIOAccounting=%s\n"
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering "%sMemoryAccounting=%s\n"
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering "%sCPUShares=%lu\n"
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering "%sBlockIOWeight%lu\n"
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek "%sMemorySoftLimit=%" PRIu64 "\n"
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek "%sDevicePolicy=%s\n",
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek prefix, yes_no(c->cpu_accounting),
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek prefix, yes_no(c->blockio_accounting),
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek prefix, yes_no(c->memory_accounting),
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek prefix, cgroup_device_policy_to_string(c->device_policy));
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek LIST_FOREACH(device_allow, a, c->device_allow)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek "%sDeviceAllow=%s %s%s%s\n",
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen a->r ? "r" : "", a->w ? "w" : "", a->m ? "m" : "");
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen LIST_FOREACH(device_weights, w, c->blockio_device_weights)
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen "%sBlockIOWeight=%s %lu",
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering LIST_FOREACH(device_bandwidths, b, c->blockio_device_bandwidths) {
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering "%s%s=%s %s\n",
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek b->read ? "BlockIOReadBandwidth" : "BlockIOWriteBandwidth",
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek format_bytes(buf, sizeof(buf), b->bandwidth));
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmekstatic int lookup_blkio_device(const char *p, dev_t *dev) {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek log_warning("Couldn't stat device %s: %m", p);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek else if (major(st.st_dev) != 0) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen /* If this is not a device node then find the block
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen * device this file is stored on */
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen /* If this is a partition, try to get the originating
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen * block device */
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen log_warning("%s is not a block device and file system block device cannot be determined or is not local.", p);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersenstatic int whitelist_device(const char *path, const char *node, const char *acc) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering char buf[2+DECIMAL_STR_MAX(dev_t)*2+2+4];
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering log_warning("Couldn't stat device %s", node);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode)) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering log_warning("%s is not a device.", node);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering "%c %u:%u %s",
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = cg_set_attribute("devices", path, "devices.allow", buf);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering log_warning("Failed to set devices.allow on %s: %s", path, strerror(-r));
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poetteringvoid cgroup_context_apply(CGroupContext *c, CGroupControllerMask mask, const char *path) {
static const char auto_devices[] =
acc[k++] = 0;
if (c->blockio_accounting ||
c->blockio_device_weights ||
if (c->memory_accounting ||
return mask;
CGroupContext *c;
c = unit_get_cgroup_context(u);
return cgroup_context_get_mask(c);
Unit *m;
Iterator i;
assert(u);
return mask;
assert(u);
assert(u);
if (!path)
return -ENOMEM;
u->cgroup_realized = true;
assert(u);
if (u->in_cgroup_queue) {
u->in_cgroup_queue = false;
if (u->cgroup_realized &&
if (u->in_cgroup_queue)
u->in_cgroup_queue = true;
Unit *i;
while ((i = m->cgroup_queue)) {
if (unit_realize_cgroup_now(i) >= 0)
Iterator i;
Unit *m;
u = slice;
CGroupContext *c;
assert(u);
c = unit_get_cgroup_context(u);
r = unit_realize_cgroup_now(u);
assert(u);
if (!u->cgroup_path)
u->cgroup_realized = false;
u->cgroup_mask = 0;
assert(u);
if (!u->cgroup_path)
if (pid != 0) {
pid = 0;
return pid;
assert(m);
/* Already in /system.slice? If so, let's cut this off again */
m->cgroup_root[0] = 0;
log_debug("Using cgroup controller " SYSTEMD_CGROUP_CONTROLLER ". File system hierarchy is at %s.", path);
if (m->pin_cgroupfs_fd >= 0)
return -errno;
assert(m);
if (m->pin_cgroupfs_fd >= 0) {
Unit *u;
assert(m);
return NULL;
assert(m);
return NULL;
return NULL;
Unit *u;
assert(m);