test-cgroup-mask.c revision bdf7026e9557349cd3eeb291c01655d5f2a55db8
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/***
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen This file is part of systemd.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen Copyright 2013 David Strauss
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen systemd is free software; you can redistribute it and/or modify it
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen under the terms of the GNU Lesser General Public License as published by
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen the Free Software Foundation; either version 2.1 of the License, or
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen (at your option) any later version.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen systemd is distributed in the hope that it will be useful, but
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen Lesser General Public License for more details.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen You should have received a copy of the GNU Lesser General Public License
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen***/
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <stdio.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <stdlib.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <string.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <sys/types.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <pwd.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include "manager.h"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include "unit.h"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include "util.h"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include "macro.h"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include "test-helper.h"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic int test_cgroup_mask(void) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen Manager *m = NULL;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen Unit *son, *daughter, *parent, *root, *grandchild, *parent_deep;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen FILE *serial = NULL;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen FDSet *fdset = NULL;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int r;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* Prepare the manager. */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(set_unit_path(TEST_DIR) >= 0);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen r = manager_new(SYSTEMD_USER, true, &m);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (r == -EPERM || r == -EACCES) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen puts("manager_new: Permission denied. Skipping test.");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return EXIT_TEST_SKIP;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(r >= 0);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(manager_startup(m, serial, fdset) >= 0);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* Load units and verify hierarchy. */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(manager_load_unit(m, "parent.slice", NULL, NULL, &parent) >= 0);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(manager_load_unit(m, "son.service", NULL, NULL, &son) >= 0);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(manager_load_unit(m, "daughter.service", NULL, NULL, &daughter) >= 0);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(manager_load_unit(m, "grandchild.service", NULL, NULL, &grandchild) >= 0);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(manager_load_unit(m, "parent-deep.slice", NULL, NULL, &parent_deep) >= 0);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(parent->load_state == UNIT_LOADED);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(son->load_state == UNIT_LOADED);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(daughter->load_state == UNIT_LOADED);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(grandchild->load_state == UNIT_LOADED);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(parent_deep->load_state == UNIT_LOADED);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(UNIT_DEREF(son->slice) == parent);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(UNIT_DEREF(daughter->slice) == parent);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(UNIT_DEREF(parent_deep->slice) == parent);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(UNIT_DEREF(grandchild->slice) == parent_deep);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen root = UNIT_DEREF(parent->slice);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* Verify per-unit cgroups settings. */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(unit_get_cgroup_mask(son) == (CGROUP_CPU | CGROUP_CPUACCT));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(unit_get_cgroup_mask(daughter) == 0);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(unit_get_cgroup_mask(grandchild) == 0);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(unit_get_cgroup_mask(parent_deep) == CGROUP_MEMORY);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(unit_get_cgroup_mask(parent) == CGROUP_BLKIO);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(unit_get_cgroup_mask(root) == 0);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* Verify aggregation of member masks */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(unit_get_members_mask(son) == 0);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(unit_get_members_mask(daughter) == 0);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(unit_get_members_mask(grandchild) == 0);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(unit_get_members_mask(parent_deep) == 0);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(unit_get_members_mask(parent) == (CGROUP_CPU | CGROUP_CPUACCT | CGROUP_MEMORY));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(unit_get_members_mask(root) == (CGROUP_CPU | CGROUP_CPUACCT | CGROUP_BLKIO | CGROUP_MEMORY));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* Verify aggregation of sibling masks. */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(unit_get_siblings_mask(son) == ((CGROUP_CPU | CGROUP_CPUACCT | CGROUP_MEMORY) & m->cgroup_supported));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(unit_get_siblings_mask(daughter) == ((CGROUP_CPU | CGROUP_CPUACCT | CGROUP_MEMORY) & m->cgroup_supported));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(unit_get_siblings_mask(grandchild) == 0);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(unit_get_siblings_mask(parent_deep) == ((CGROUP_CPU | CGROUP_CPUACCT | CGROUP_MEMORY) & m->cgroup_supported));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(unit_get_siblings_mask(parent) == ((CGROUP_CPU | CGROUP_CPUACCT | CGROUP_BLKIO | CGROUP_MEMORY) & m->cgroup_supported));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(unit_get_siblings_mask(root) == ((CGROUP_CPU | CGROUP_CPUACCT | CGROUP_BLKIO | CGROUP_MEMORY) & m->cgroup_supported));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* Verify aggregation of target masks. */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(unit_get_target_mask(son) == ((CGROUP_CPU | CGROUP_CPUACCT | CGROUP_MEMORY) & m->cgroup_supported));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(unit_get_target_mask(daughter) == ((CGROUP_CPU | CGROUP_CPUACCT | CGROUP_MEMORY) & m->cgroup_supported));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(unit_get_target_mask(grandchild) == 0);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(unit_get_target_mask(parent_deep) == ((CGROUP_CPU | CGROUP_CPUACCT | CGROUP_MEMORY) & m->cgroup_supported));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(unit_get_target_mask(parent) == ((CGROUP_CPU | CGROUP_CPUACCT | CGROUP_BLKIO | CGROUP_MEMORY) & m->cgroup_supported));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_se(unit_get_target_mask(root) == ((CGROUP_CPU | CGROUP_CPUACCT | CGROUP_BLKIO | CGROUP_MEMORY) & m->cgroup_supported));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen manager_free(m);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 0;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint main(int argc, char* argv[]) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int rc = 0;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen TEST_REQ_RUNNING_SYSTEMD(rc = test_cgroup_mask());
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return rc;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen