test-cgroup-util.c revision 0b452006de98294d1690f045f6ea2f7f6630ec3b
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering This file is part of systemd.
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering Copyright 2013 Zbigniew Jędrzejewski-Szmek
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering systemd is free software; you can redistribute it and/or modify it
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering under the terms of the GNU Lesser General Public License as published by
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering (at your option) any later version.
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering systemd is distributed in the hope that it will be useful, but
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering Lesser General Public License for more details.
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering You should have received a copy of the GNU Lesser General Public License
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
7027ff61a34a12487712b382a061c654acc3a679Lennart Poetteringstatic void check_p_d_u(const char *path, int code, const char *result) {
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek r = cg_path_decode_unit(path, &unit);
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek printf("%s: %s → %s %d expected %s %d\n", __func__, path, unit, r, result, code);
7027ff61a34a12487712b382a061c654acc3a679Lennart Poetteringstatic void test_path_decode_unit(void) {
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering check_p_d_u("getty@tty2.service", 0, "getty@tty2.service");
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering check_p_d_u("getty@tty2.service/", 0, "getty@tty2.service");
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering check_p_d_u("getty@tty2.service/xxx", 0, "getty@tty2.service");
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering check_p_d_u("getty@.service/", -EINVAL, NULL);
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering check_p_d_u("getty@.service", -EINVAL, NULL);
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering check_p_d_u("getty.service", 0, "getty.service");
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering check_p_d_u("getty/waldo", -EINVAL, NULL);
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering check_p_d_u("_cpu.service", 0, "cpu.service");
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poetteringstatic void check_p_g_u(const char *path, int code, const char *result) {
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek r = cg_path_get_unit(path, &unit);
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek printf("%s: %s → %s %d expected %s %d\n", __func__, path, unit, r, result, code);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poetteringstatic void test_path_get_unit(void) {
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering check_p_g_u("/system.slice/foobar.service/sdfdsaf", 0, "foobar.service");
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering check_p_g_u("/system.slice/getty@tty5.service", 0, "getty@tty5.service");
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering check_p_g_u("/system.slice/getty@tty5.service/aaa/bbb", 0, "getty@tty5.service");
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering check_p_g_u("/system.slice/getty@tty5.service/", 0, "getty@tty5.service");
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering check_p_g_u("/system.slice/getty@tty6.service/tty5", 0, "getty@tty6.service");
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering check_p_g_u("sadfdsafsda", -EINVAL, NULL);
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering check_p_g_u("/system.slice/getty####@tty6.service/xxx", -EINVAL, NULL);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering check_p_g_u("/system.slice/system-waldo.slice/foobar.service/sdfdsaf", 0, "foobar.service");
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering check_p_g_u("/system.slice/system-waldo.slice/_cpu.service/sdfdsaf", 0, "cpu.service");
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek check_p_g_u("/user.slice/user-1000.slice/user@1000.service/server.service", 0, "user@1000.service");
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek check_p_g_u("/user.slice/user-1000.slice/user@.service/server.service", -EINVAL, NULL);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poetteringstatic void check_p_g_u_u(const char *path, int code, const char *result) {
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek r = cg_path_get_user_unit(path, &unit);
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek printf("%s: %s → %s %d expected %s %d\n", __func__, path, unit, r, result, code);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poetteringstatic void test_path_get_user_unit(void) {
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, "foobar.service");
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/waldo.slice/foobar.service", 0, "foobar.service");
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering check_p_g_u_u("/user.slice/user-1002.slice/session-2.scope/foobar.service/waldo", 0, "foobar.service");
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar.service/waldo/uuuux", 0, "foobar.service");
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/waldo/waldo/uuuux", -EINVAL, NULL);
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service");
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering check_p_g_u_u("/session-2.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service");
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering check_p_g_u_u("/xyz.slice/xyz-waldo.slice/session-77.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service");
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering check_p_g_u_u("/meh.service", -ENOENT, NULL);
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering check_p_g_u_u("/session-3.scope/_cpu.service", 0, "cpu.service");
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek check_p_g_u_u("/user.slice/user-1000.slice/user@1000.service/server.service", 0, "server.service");
320814811417146cfa1e416f69f1101eed630c36Luke Shumaker check_p_g_u_u("/user.slice/user-1000.slice/user@1000.service/foobar.slice/foobar@pie.service", 0, "foobar@pie.service");
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek check_p_g_u_u("/user.slice/user-1000.slice/user@.service/server.service", -ENOENT, NULL);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poetteringstatic void check_p_g_s(const char *path, int code, const char *result) {
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(cg_path_get_session(path, &s) == code);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poetteringstatic void test_path_get_session(void) {
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering check_p_g_s("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, "2");
a13ee4c792cb5738c3dd13b2a4bb1a828e7994fdZbigniew Jędrzejewski-Szmek check_p_g_s("/session-.scope", -ENOENT, NULL);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poetteringstatic void check_p_g_o_u(const char *path, int code, uid_t result) {
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(cg_path_get_owner_uid(path, &uid) == code);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poetteringstatic void test_path_get_owner_uid(void) {
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering check_p_g_o_u("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, 1000);
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering check_p_g_o_u("/user.slice/user-1006.slice", 0, 1006);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poetteringstatic void test_get_paths(void) {
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poetteringstatic void test_proc(void) {
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering _cleanup_free_ char *path = NULL, *path_shifted = NULL, *session = NULL, *unit = NULL, *user_unit = NULL, *machine = NULL, *slice = NULL;
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &path);
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering cg_pid_get_path_shifted(pid, NULL, &path_shifted);
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek printf(PID_FMT"\t%s\t%s\t"UID_FMT"\t%s\t%s\t%s\t%s\t%s\n",
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poetteringstatic void test_escape_one(const char *s, const char *r) {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poetteringstatic void test_escape(void) {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering test_escape_one("foobar.service", "foobar.service");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering test_escape_one("cgroup.service", "_cgroup.service");
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poetteringstatic void test_controller_is_valid(void) {
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poettering assert_se(cg_controller_is_valid("foobar", false));
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poettering assert_se(cg_controller_is_valid("foo_bar", false));
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poettering assert_se(cg_controller_is_valid("name=foo", true));
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poettering assert_se(!cg_controller_is_valid("", false));
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poettering assert_se(!cg_controller_is_valid("name=", true));
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poettering assert_se(!cg_controller_is_valid("=", false));
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poettering assert_se(!cg_controller_is_valid("cpu,cpuacct", false));
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poettering assert_se(!cg_controller_is_valid("_", false));
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poettering assert_se(!cg_controller_is_valid("_foobar", false));
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poettering assert_se(!cg_controller_is_valid("tatü", false));
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poetteringstatic void test_slice_to_path_one(const char *unit, const char *path, int error) {
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering assert_se(cg_slice_to_path(unit, &ret) == error);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poetteringstatic void test_slice_to_path(void) {
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering test_slice_to_path_one("foobar.slice", "foobar.slice", 0);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering test_slice_to_path_one("foobar-waldo.slice", "foobar.slice/foobar-waldo.slice", 0);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering test_slice_to_path_one("foobar-waldo.service", NULL, -EINVAL);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering test_slice_to_path_one("-.slice", NULL, -EINVAL);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering test_slice_to_path_one("-foo-.slice", NULL, -EINVAL);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering test_slice_to_path_one("-foo.slice", NULL, -EINVAL);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering test_slice_to_path_one("a-b.slice", "a.slice/a-b.slice", 0);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart 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);
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poetteringstatic void test_shift_path_one(const char *raw, const char *root, const char *shifted) {
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poettering const char *s = NULL;
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poettering assert_se(cg_shift_path(raw, root, &s) >= 0);
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poetteringstatic void test_shift_path(void) {
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poettering test_shift_path_one("/foobar/waldo", "/", "/foobar/waldo");
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poettering test_shift_path_one("/foobar/waldo", "", "/foobar/waldo");
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poettering test_shift_path_one("/foobar/waldo", "/foobar", "/waldo");
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poettering test_shift_path_one("/foobar/waldo", "/fuckfuck", "/foobar/waldo");
143bfdaf0b890fa7acadf02d1eafacaef1b696bdHolger Hans Peter Freyther TEST_REQ_RUNNING_SYSTEMD(test_get_paths());