test-cgroup-util.c revision e13bb5d2b133f9ae51c0a2d20aa51071c780e9ae
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering This file is part of systemd.
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering Copyright 2013 Zbigniew Jędrzejewski-Szmek
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering systemd is free software; you can redistribute it and/or modify it
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering under the terms of the GNU Lesser General Public License as published by
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering (at your option) any later version.
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering systemd is distributed in the hope that it will be useful, but
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering Lesser General Public License for more details.
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering You should have received a copy of the GNU Lesser General Public License
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poetteringstatic void check_p_d_u(const char *path, int code, const char *result) {
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering assert_se(cg_path_decode_unit(path, &unit) == code);
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poetteringstatic void test_path_decode_unit(void) {
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering check_p_d_u("getty@tty2.service", 0, "getty@tty2.service");
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering check_p_d_u("getty@tty2.service/", 0, "getty@tty2.service");
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering check_p_d_u("getty@tty2.service/xxx", 0, "getty@tty2.service");
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering check_p_d_u("getty@.service/", -EINVAL, NULL);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering check_p_d_u("getty@.service", -EINVAL, NULL);
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering check_p_d_u("getty.service", 0, "getty.service");
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering check_p_d_u("getty/waldo", -EINVAL, NULL);
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering check_p_d_u("_cpu.service", 0, "cpu.service");
8fcde01280adcbd07e8205b91ac52b06305b6208Lennart Poetteringstatic void check_p_g_u(const char *path, int code, const char *result) {
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering assert_se(cg_path_get_unit(path, &unit) == code);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poetteringstatic void test_path_get_unit(void) {
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering check_p_g_u("/system.slice/foobar.service/sdfdsaf", 0, "foobar.service");
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering check_p_g_u("/system.slice/getty@tty5.service", 0, "getty@tty5.service");
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering check_p_g_u("/system.slice/getty@tty5.service/aaa/bbb", 0, "getty@tty5.service");
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering check_p_g_u("/system.slice/getty@tty5.service/", 0, "getty@tty5.service");
cca1dfddd4ce4357113663532696488427cc54e4Lennart Poettering check_p_g_u("/system.slice/getty@tty6.service/tty5", 0, "getty@tty6.service");
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering check_p_g_u("sadfdsafsda", -EINVAL, NULL);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering check_p_g_u("/system.slice/getty####@tty6.service/xxx", -EINVAL, NULL);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering check_p_g_u("/system.slice/system-waldo.slice/foobar.service/sdfdsaf", 0, "foobar.service");
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering check_p_g_u("/system.slice/system-waldo.slice/_cpu.service/sdfdsaf", 0, "cpu.service");
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poetteringstatic void check_p_g_u_u(const char *path, int code, const char *result) {
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering assert_se(cg_path_get_user_unit(path, &unit) == code);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poetteringstatic void test_path_get_user_unit(void) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, "foobar.service");
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/waldo.slice/foobar.service", 0, "foobar.service");
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering check_p_g_u_u("/user.slice/user-1002.slice/session-2.scope/foobar.service/waldo", 0, "foobar.service");
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar.service/waldo/uuuux", 0, "foobar.service");
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/waldo/waldo/uuuux", -EINVAL, NULL);
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service");
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering check_p_g_u_u("/session-2.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service");
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering check_p_g_u_u("/xyz.slice/xyz-waldo.slice/session-77.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service");
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering check_p_g_u_u("/session-3.scope/_cpu.service", 0, "cpu.service");
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidtstatic void check_p_g_s(const char *path, int code, const char *result) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering assert_se(cg_path_get_session(path, &s) == code);
c3834f9b881f2b1a68dc7d797c134f0b66b47b57Lennart Poetteringstatic void test_path_get_session(void) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering check_p_g_s("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, "2");
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poetteringstatic void check_p_g_o_u(const char *path, int code, uid_t result) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering assert_se(cg_path_get_owner_uid(path, &uid) == code);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poetteringstatic void test_path_get_owner_uid(void) {
cca1dfddd4ce4357113663532696488427cc54e4Lennart Poettering check_p_g_o_u("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, 1000);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering check_p_g_o_u("/user.slice/user-1006.slice", 0, 1006);
dacd6cee76a08331b8c8616c5f30f70ee49aa2f9Lennart Poetteringstatic void check_p_g_m_n(const char *path, int code, const char *result) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering assert_se(cg_path_get_machine_name(path, &m) == code);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poetteringstatic void test_path_get_machine_name(void) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering check_p_g_m_n("/user.slice/machine-foobar.scope", 0, "foobar");
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt check_p_g_m_n("/machine-foobar.scope", 0, "foobar");
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt check_p_g_m_n("/user.slice/user-kuux.slice/machine-foobar.scope", 0, "foobar");
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering check_p_g_m_n("/user.slice/user-kuux.slice/machine-foobar.scope/asjhdkj", 0, "foobar");
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poetteringstatic void test_get_paths(void) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poetteringstatic void test_proc(void) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering _cleanup_free_ char *path = NULL, *path_shifted = NULL, *session = NULL, *unit = NULL, *user_unit = NULL, *machine = NULL, *prefix = NULL, *slice = NULL;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &path);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering cg_pid_get_path_shifted(pid, &prefix, &path_shifted);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering printf("%lu\t%s\t%s\t%s\t%lu\t%s\t%s\t%s\t%s\t%s\n",
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering (unsigned long) pid,
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering (unsigned long) uid,
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poetteringstatic void test_escape_one(const char *s, const char *r) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poetteringstatic void test_escape(void) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering test_escape_one("foobar.service", "foobar.service");
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering test_escape_one("cgroup.service", "_cgroup.service");
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering if (access("/sys/fs/cgroup/cpu", F_OK) == 0)
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering test_escape_one("cpu.service", "_cpu.service");
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidtstatic void test_controller_is_valid(void) {
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering assert_se(cg_controller_is_valid("foobar", false));
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering assert_se(cg_controller_is_valid("foo_bar", false));
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering assert_se(cg_controller_is_valid("name=foo", true));
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering assert_se(!cg_controller_is_valid("", false));
c3834f9b881f2b1a68dc7d797c134f0b66b47b57Lennart Poettering assert_se(!cg_controller_is_valid("name=", true));
c3834f9b881f2b1a68dc7d797c134f0b66b47b57Lennart Poettering assert_se(!cg_controller_is_valid("=", false));
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering assert_se(!cg_controller_is_valid("cpu,cpuacct", false));
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering assert_se(!cg_controller_is_valid("_", false));
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering assert_se(!cg_controller_is_valid("_foobar", false));
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering assert_se(!cg_controller_is_valid("tatü", false));
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poetteringstatic void test_slice_to_path_one(const char *unit, const char *path, int error) {
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering assert_se(cg_slice_to_path(unit, &ret) == error);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poetteringstatic void test_slice_to_path(void) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering test_slice_to_path_one("foobar.slice", "foobar.slice", 0);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering test_slice_to_path_one("foobar-waldo.slice", "foobar.slice/foobar-waldo.slice", 0);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering test_slice_to_path_one("foobar-waldo.service", NULL, -EINVAL);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering test_slice_to_path_one("-.slice", NULL, -EINVAL);
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers test_slice_to_path_one("-foo-.slice", NULL, -EINVAL);
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers test_slice_to_path_one("-foo.slice", NULL, -EINVAL);
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers test_slice_to_path_one("a-b.slice", "a.slice/a-b.slice", 0);
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers test_slice_to_path_one("a-b-c-d-e.slice", "a.slice/a-b.slice/a-b-c.slice/a-b-c-d.slice/a-b-c-d-e.slice", 0);