test-cgroup.c revision 35d2e7ec19f8d3960a14dc04642060ccee3faa43
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering/*-*- Mode: C; c-basic-offset: 8 -*-*/
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering/***
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering This file is part of systemd.
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering Copyright 2010 Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering systemd is free software; you can redistribute it and/or modify it
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering under the terms of the GNU General Public License as published by
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering the Free Software Foundation; either version 2 of the License, or
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering (at your option) any later version.
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering systemd is distributed in the hope that it will be useful, but
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering General Public License for more details.
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering You should have received a copy of the GNU General Public License
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
1b00a255225bd65bcc3f6257bd4b1e2089f231b4Tollef Fog Heen***/
e99e38bbdcca3fe5956823bdb3d38544ccf93221Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering#include <unistd.h>
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering#include <string.h>
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
e99e38bbdcca3fe5956823bdb3d38544ccf93221Lennart Poettering#include "cgroup-util.h"
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering#include "util.h"
1b322bdb2a147e999c3861cba8a6b3ac0f3b712aTollef Fog Heen#include "log.h"
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poetteringint main(int argc, char*argv[]) {
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering char *path;
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering char *c, *p;
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering assert_se(cg_create(SYSTEMD_CGROUP_CONTROLLER, "/test-a") == 0);
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering assert_se(cg_create(SYSTEMD_CGROUP_CONTROLLER, "/test-a") == 0);
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering assert_se(cg_create(SYSTEMD_CGROUP_CONTROLLER, "/test-b") == 0);
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering assert_se(cg_create(SYSTEMD_CGROUP_CONTROLLER, "/test-b/test-c") == 0);
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering assert_se(cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, "/test-b", 0) == 0);
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering assert_se(cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, getpid(), &path) == 0);
e9da3678fcfc774b325dc1eaa054d0e00028a1fcLennart Poettering assert_se(streq(path, "/test-b"));
e9da3678fcfc774b325dc1eaa054d0e00028a1fcLennart Poettering free(path);
e9da3678fcfc774b325dc1eaa054d0e00028a1fcLennart Poettering
e9da3678fcfc774b325dc1eaa054d0e00028a1fcLennart Poettering assert_se(cg_attach(SYSTEMD_CGROUP_CONTROLLER, "/test-a", 0) == 0);
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering assert_se(cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, getpid(), &path) == 0);
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering assert_se(path_equal(path, "/test-a"));
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering free(path);
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering assert_se(cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, "/test-b/test-d", 0) == 0);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering assert_se(cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, getpid(), &path) == 0);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering assert_se(path_equal(path, "/test-b/test-d"));
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering free(path);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering assert_se(cg_get_path(SYSTEMD_CGROUP_CONTROLLER, "/test-b/test-d", NULL, &path) == 0);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering assert_se(path_equal(path, "/cgroup/systemd/test-b/test-d"));
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering free(path);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering assert_se(cg_is_empty(SYSTEMD_CGROUP_CONTROLLER, "/test-a", false) > 0);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering assert_se(cg_is_empty(SYSTEMD_CGROUP_CONTROLLER, "/test-b", false) > 0);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering assert_se(cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, "/test-a", false) > 0);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering assert_se(cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, "/test-b", false) == 0);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering assert_se(cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, "/test-a", 0, false, false) == 0);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering assert_se(cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, "/test-b", 0, false, false) > 0);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering assert_se(cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, "/test-b", "/test-a", false, false) > 0);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering assert_se(cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, "/test-a", false) == 0);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering assert_se(cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, "/test-b", false) > 0);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering assert_se(cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, "/test-a", 0, false, false) > 0);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering assert_se(cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, "/test-b", 0, false, false) == 0);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering cg_trim(SYSTEMD_CGROUP_CONTROLLER, "/", false);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering assert_se(cg_delete(SYSTEMD_CGROUP_CONTROLLER, "/test-b") < 0);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering assert_se(cg_delete(SYSTEMD_CGROUP_CONTROLLER, "/test-a") >= 0);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering assert_se(cg_split_spec("foobar:/", &c, &p) == 0);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering assert(streq(c, "foobar"));
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering assert(streq(p, "/"));
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering free(c);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering free(p);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering assert_se(cg_split_spec("foobar:", &c, &p) < 0);
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering assert_se(cg_split_spec("foobar:asdfd", &c, &p) < 0);
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering assert_se(cg_split_spec(":///", &c, &p) < 0);
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering assert_se(cg_split_spec(":", &c, &p) < 0);
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering assert_se(cg_split_spec("", &c, &p) < 0);
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering assert_se(cg_split_spec("fo/obar:/", &c, &p) < 0);
b237ef2cfac7ab0b33170809e8cb64628606207dTollef Fog Heen
a9b5b03212f9c854938483b8901e433c2ba6619bMichael Tremer assert_se(cg_split_spec("/", &c, &p) >= 0);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering assert(c == NULL);
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering assert(streq(p, "/"));
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering free(p);
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering assert_se(cg_split_spec("foo", &c, &p) >= 0);
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering assert(streq(c, "foo"));
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering assert(p == NULL);
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering free(c);
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering return 0;
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering}
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering