6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss/***
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss This file is part of systemd.
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss Copyright 2013 David Strauss
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss systemd is free software; you can redistribute it and/or modify it
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss under the terms of the GNU Lesser General Public License as published by
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss the Free Software Foundation; either version 2.1 of the License, or
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss (at your option) any later version.
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss systemd is distributed in the hope that it will be useful, but
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss WITHOUT ANY WARRANTY; without even the implied warranty of
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss Lesser General Public License for more details.
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss You should have received a copy of the GNU Lesser General Public License
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss along with systemd; If not, see <http://www.gnu.org/licenses/>.
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss***/
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss#include <stdio.h>
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss#include "macro.h"
cf0fbc49e67b55f8d346fc94de28c90113505297Thomas Hindoe Paaboel Andersen#include "manager.h"
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss#include "test-helper.h"
cf0fbc49e67b55f8d346fc94de28c90113505297Thomas Hindoe Paaboel Andersen#include "unit.h"
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Straussstatic int test_cgroup_mask(void) {
39883f622f392d8579f4428fc5a789a102efbb10Lennart Poettering Manager *m = NULL;
bc432dc7eb62c5671f2b741a86a66393adb350dcLennart Poettering Unit *son, *daughter, *parent, *root, *grandchild, *parent_deep;
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss FILE *serial = NULL;
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss FDSet *fdset = NULL;
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss int r;
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss /* Prepare the manager. */
0d8c31ff7237149b505290652864b4e7e866b2a7Zbigniew Jędrzejewski-Szmek assert_se(set_unit_path(TEST_DIR) >= 0);
b2c23da8cea1987a1a329f5a964d3299b7ca7890Lennart Poettering r = manager_new(MANAGER_USER, true, &m);
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss if (r == -EPERM || r == -EACCES) {
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss puts("manager_new: Permission denied. Skipping test.");
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss return EXIT_TEST_SKIP;
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss }
def8b4c5d6c3dd6944e481dd5ecc24cac3c2ab58Zbigniew Jędrzejewski-Szmek assert_se(r >= 0);
9ded9cd14cc03c67291b10a5c42ce5094ba0912fLennart Poettering
9ded9cd14cc03c67291b10a5c42ce5094ba0912fLennart Poettering /* Turn off all kinds of default accouning, so that we can
9ded9cd14cc03c67291b10a5c42ce5094ba0912fLennart Poettering * verify the masks resulting of our configuration and nothing
9ded9cd14cc03c67291b10a5c42ce5094ba0912fLennart Poettering * else. */
9ded9cd14cc03c67291b10a5c42ce5094ba0912fLennart Poettering m->default_cpu_accounting =
9ded9cd14cc03c67291b10a5c42ce5094ba0912fLennart Poettering m->default_memory_accounting =
9ded9cd14cc03c67291b10a5c42ce5094ba0912fLennart Poettering m->default_blockio_accounting =
9ded9cd14cc03c67291b10a5c42ce5094ba0912fLennart Poettering m->default_tasks_accounting = false;
9ded9cd14cc03c67291b10a5c42ce5094ba0912fLennart Poettering m->default_tasks_max = (uint64_t) -1;
9ded9cd14cc03c67291b10a5c42ce5094ba0912fLennart Poettering
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(r >= 0);
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss assert_se(manager_startup(m, serial, fdset) >= 0);
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss /* Load units and verify hierarchy. */
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss assert_se(manager_load_unit(m, "parent.slice", NULL, NULL, &parent) >= 0);
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss assert_se(manager_load_unit(m, "son.service", NULL, NULL, &son) >= 0);
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss assert_se(manager_load_unit(m, "daughter.service", NULL, NULL, &daughter) >= 0);
bc432dc7eb62c5671f2b741a86a66393adb350dcLennart Poettering assert_se(manager_load_unit(m, "grandchild.service", NULL, NULL, &grandchild) >= 0);
bc432dc7eb62c5671f2b741a86a66393adb350dcLennart Poettering assert_se(manager_load_unit(m, "parent-deep.slice", NULL, NULL, &parent_deep) >= 0);
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(parent->load_state == UNIT_LOADED);
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(son->load_state == UNIT_LOADED);
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(daughter->load_state == UNIT_LOADED);
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(grandchild->load_state == UNIT_LOADED);
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(parent_deep->load_state == UNIT_LOADED);
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(UNIT_DEREF(son->slice) == parent);
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(UNIT_DEREF(daughter->slice) == parent);
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(UNIT_DEREF(parent_deep->slice) == parent);
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(UNIT_DEREF(grandchild->slice) == parent_deep);
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss root = UNIT_DEREF(parent->slice);
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss /* Verify per-unit cgroups settings. */
efdb02375beb0a940c3320865572913780b4d7deLennart Poettering assert_se(unit_get_own_mask(son) == (CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT));
efdb02375beb0a940c3320865572913780b4d7deLennart Poettering assert_se(unit_get_own_mask(daughter) == 0);
efdb02375beb0a940c3320865572913780b4d7deLennart Poettering assert_se(unit_get_own_mask(grandchild) == 0);
efdb02375beb0a940c3320865572913780b4d7deLennart Poettering assert_se(unit_get_own_mask(parent_deep) == CGROUP_MASK_MEMORY);
efdb02375beb0a940c3320865572913780b4d7deLennart Poettering assert_se(unit_get_own_mask(parent) == CGROUP_MASK_BLKIO);
efdb02375beb0a940c3320865572913780b4d7deLennart Poettering assert_se(unit_get_own_mask(root) == 0);
bc432dc7eb62c5671f2b741a86a66393adb350dcLennart Poettering
bc432dc7eb62c5671f2b741a86a66393adb350dcLennart Poettering /* Verify aggregation of member masks */
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(unit_get_members_mask(son) == 0);
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(unit_get_members_mask(daughter) == 0);
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(unit_get_members_mask(grandchild) == 0);
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(unit_get_members_mask(parent_deep) == 0);
efdb02375beb0a940c3320865572913780b4d7deLennart Poettering assert_se(unit_get_members_mask(parent) == (CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_MEMORY));
efdb02375beb0a940c3320865572913780b4d7deLennart Poettering assert_se(unit_get_members_mask(root) == (CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_BLKIO | CGROUP_MASK_MEMORY));
bc432dc7eb62c5671f2b741a86a66393adb350dcLennart Poettering
bc432dc7eb62c5671f2b741a86a66393adb350dcLennart Poettering /* Verify aggregation of sibling masks. */
efdb02375beb0a940c3320865572913780b4d7deLennart Poettering assert_se(unit_get_siblings_mask(son) == (CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_MEMORY));
efdb02375beb0a940c3320865572913780b4d7deLennart Poettering assert_se(unit_get_siblings_mask(daughter) == (CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_MEMORY));
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(unit_get_siblings_mask(grandchild) == 0);
efdb02375beb0a940c3320865572913780b4d7deLennart Poettering assert_se(unit_get_siblings_mask(parent_deep) == (CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_MEMORY));
efdb02375beb0a940c3320865572913780b4d7deLennart Poettering assert_se(unit_get_siblings_mask(parent) == (CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_BLKIO | CGROUP_MASK_MEMORY));
efdb02375beb0a940c3320865572913780b4d7deLennart Poettering assert_se(unit_get_siblings_mask(root) == (CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_BLKIO | CGROUP_MASK_MEMORY));
bc432dc7eb62c5671f2b741a86a66393adb350dcLennart Poettering
bc432dc7eb62c5671f2b741a86a66393adb350dcLennart Poettering /* Verify aggregation of target masks. */
efdb02375beb0a940c3320865572913780b4d7deLennart Poettering assert_se(unit_get_target_mask(son) == ((CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_MEMORY) & m->cgroup_supported));
efdb02375beb0a940c3320865572913780b4d7deLennart Poettering assert_se(unit_get_target_mask(daughter) == ((CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_MEMORY) & m->cgroup_supported));
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(unit_get_target_mask(grandchild) == 0);
efdb02375beb0a940c3320865572913780b4d7deLennart Poettering assert_se(unit_get_target_mask(parent_deep) == ((CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_MEMORY) & m->cgroup_supported));
efdb02375beb0a940c3320865572913780b4d7deLennart Poettering assert_se(unit_get_target_mask(parent) == ((CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_BLKIO | CGROUP_MASK_MEMORY) & m->cgroup_supported));
efdb02375beb0a940c3320865572913780b4d7deLennart Poettering assert_se(unit_get_target_mask(root) == ((CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_BLKIO | CGROUP_MASK_MEMORY) & m->cgroup_supported));
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss manager_free(m);
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss return 0;
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss}
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Straussint main(int argc, char* argv[]) {
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss int rc = 0;
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss TEST_REQ_RUNNING_SYSTEMD(rc = test_cgroup_mask());
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss return rc;
6414b7c981378a6eef480f6806d7cbfc98ca22a1David Strauss}