test-cgroup-util.c revision d4fffc4b8beb86e77fd710c1f43913a490ed083a
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2013 Zbigniew Jędrzejewski-Szmek
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/>.
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmekstatic void check_p_d_u(const char *path, int code, const char *result) {
7263f72499e962b3fd54cdb7c79d49ca72121edeZbigniew Jędrzejewski-Szmek _cleanup_free_ char *unit = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering printf("%s: %s → %s %d expected %s %d\n", __func__, path, unit, r, result, code);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic void test_path_decode_unit(void) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_d_u("getty@tty2.service", 0, "getty@tty2.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_d_u("getty@tty2.service/", 0, "getty@tty2.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_d_u("getty@tty2.service/xxx", 0, "getty@tty2.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_d_u("getty@.service/", -EINVAL, NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_d_u("getty@.service", -EINVAL, NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_d_u("getty.service", 0, "getty.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_d_u("getty/waldo", -EINVAL, NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_d_u("_cpu.service", 0, "cpu.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic void check_p_g_u(const char *path, int code, const char *result) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering printf("%s: %s → %s %d expected %s %d\n", __func__, path, unit, r, result, code);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic void test_path_get_unit(void) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u("/system.slice/foobar.service/sdfdsaf", 0, "foobar.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u("/system.slice/getty@tty5.service", 0, "getty@tty5.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u("/system.slice/getty@tty5.service/aaa/bbb", 0, "getty@tty5.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u("/system.slice/getty@tty5.service/", 0, "getty@tty5.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u("/system.slice/getty@tty6.service/tty5", 0, "getty@tty6.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u("sadfdsafsda", -EINVAL, NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u("/system.slice/getty####@tty6.service/xxx", -EINVAL, NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u("/system.slice/system-waldo.slice/foobar.service/sdfdsaf", 0, "foobar.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u("/system.slice/system-waldo.slice/_cpu.service/sdfdsaf", 0, "cpu.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u("/user.slice/user-1000.slice/user@1000.service/server.service", 0, "user@1000.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u("/user.slice/user-1000.slice/user@.service/server.service", -EINVAL, NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic void check_p_g_u_u(const char *path, int code, const char *result) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering printf("%s: %s → %s %d expected %s %d\n", __func__, path, unit, r, result, code);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic void test_path_get_user_unit(void) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, "foobar.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/waldo.slice/foobar.service", 0, "foobar.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u_u("/user.slice/user-1002.slice/session-2.scope/foobar.service/waldo", 0, "foobar.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar.service/waldo/uuuux", 0, "foobar.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/waldo/waldo/uuuux", -EINVAL, NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u_u("/session-2.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u_u("/xyz.slice/xyz-waldo.slice/session-77.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u_u("/meh.service", -ENOENT, NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u_u("/session-3.scope/_cpu.service", 0, "cpu.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u_u("/user.slice/user-1000.slice/user@1000.service/server.service", 0, "server.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u_u("/user.slice/user-1000.slice/user@.service/server.service", -ENOENT, NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic void check_p_g_s(const char *path, int code, const char *result) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert_se(cg_path_get_session(path, &s) == code);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic void test_path_get_session(void) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_s("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, "2");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic void check_p_g_o_u(const char *path, int code, uid_t result) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert_se(cg_path_get_owner_uid(path, &uid) == code);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic void test_path_get_owner_uid(void) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_o_u("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, 1000);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_o_u("/user.slice/user-1006.slice", 0, 1006);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic void check_p_g_m_n(const char *path, int code, const char *result) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert_se(cg_path_get_machine_name(path, &m) == code);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic void test_path_get_machine_name(void) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_m_n("/user.slice/machine-foobar.scope", 0, "foobar");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering check_p_g_m_n("/machine-foobar.scope", 0, "foobar");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering check_p_g_m_n("/user.slice/user-kuux.slice/machine-foobar.scope", 0, "foobar");
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt check_p_g_m_n("/user.slice/user-kuux.slice/machine-foobar.scope/asjhdkj", 0, "foobar");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic void test_get_paths(void) {
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidtstatic void test_proc(void) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering _cleanup_free_ char *path = NULL, *path_shifted = NULL, *session = NULL, *unit = NULL, *user_unit = NULL, *machine = NULL, *slice = NULL;
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &path);
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt cg_pid_get_path_shifted(pid, NULL, &path_shifted);
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen printf("%lu\t%s\t%s\t%lu\t%s\t%s\t%s\t%s\t%s\n",
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering (unsigned long) pid,
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering (unsigned long) uid,
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringstatic void test_escape_one(const char *s, const char *r) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic void test_escape(void) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering test_escape_one("foobar.service", "foobar.service");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test_escape_one("cgroup.service", "_cgroup.service");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (access("/sys/fs/cgroup/cpu", F_OK) == 0)
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering test_escape_one("cpu.service", "_cpu.service");
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poetteringstatic void test_controller_is_valid(void) {
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering assert_se(cg_controller_is_valid("foobar", false));
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering assert_se(cg_controller_is_valid("foo_bar", false));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(cg_controller_is_valid("name=foo", true));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(!cg_controller_is_valid("", false));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(!cg_controller_is_valid("name=", true));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(!cg_controller_is_valid("=", false));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(!cg_controller_is_valid("cpu,cpuacct", false));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(!cg_controller_is_valid("_", false));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(!cg_controller_is_valid("_foobar", false));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(!cg_controller_is_valid("tatü", false));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic void test_slice_to_path_one(const char *unit, const char *path, int error) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(cg_slice_to_path(unit, &ret) == error);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic void test_slice_to_path(void) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test_slice_to_path_one("foobar.slice", "foobar.slice", 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test_slice_to_path_one("foobar-waldo.slice", "foobar.slice/foobar-waldo.slice", 0);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering test_slice_to_path_one("foobar-waldo.service", NULL, -EINVAL);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek test_slice_to_path_one("-.slice", NULL, -EINVAL);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering test_slice_to_path_one("-foo-.slice", NULL, -EINVAL);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering test_slice_to_path_one("-foo.slice", NULL, -EINVAL);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering test_slice_to_path_one("a-b.slice", "a.slice/a-b.slice", 0);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering 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);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering TEST_REQ_RUNNING_SYSTEMD(test_get_paths());