test-cgroup-util.c revision b5efdb8af40ea759a1ea584c1bc44ecc81dd00ce
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");
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering check_p_d_u("getty@.service/", -ENXIO, NULL);
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering check_p_d_u("getty@.service", -ENXIO, NULL);
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering check_p_d_u("getty.service", 0, "getty.service");
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering check_p_d_u("getty/waldo", -ENXIO, 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");
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering check_p_g_u("sadfdsafsda", -ENXIO, NULL);
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering check_p_g_u("/system.slice/getty####@tty6.service/xxx", -ENXIO, 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");
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering check_p_g_u("/user.slice/user-1000.slice/user@.service/server.service", -ENXIO, 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");
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/waldo/waldo/uuuux", -ENXIO, 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");
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering check_p_g_u_u("/meh.service", -ENXIO, 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");
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering check_p_g_u_u("/user.slice/user-1000.slice/user@.service/server.service", -ENXIO, 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");
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering check_p_g_s("/session-.scope", -ENXIO, 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);
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poetteringstatic void check_p_g_slice(const char *path, int code, const char *result) {
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering assert_se(cg_path_get_slice(path, &s) == code);
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poetteringstatic void test_path_get_slice(void) {
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering check_p_g_slice("/user.slice", 0, "user.slice");
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering check_p_g_slice("/foobar", 0, "-.slice");
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering check_p_g_slice("/user.slice/user-waldo.slice", 0, "user-waldo.slice");
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering check_p_g_slice("foobar.slice", 0, "foobar.slice");
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering check_p_g_slice("foo.slice/foo-bar.slice/waldo.service", 0, "foo-bar.slice");
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poetteringstatic void check_p_g_u_slice(const char *path, int code, const char *result) {
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering assert_se(cg_path_get_user_slice(path, &s) == code);
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poetteringstatic void test_path_get_user_slice(void) {
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering check_p_g_u_slice("/user.slice", -ENXIO, NULL);
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering check_p_g_u_slice("/foobar", -ENXIO, NULL);
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering check_p_g_u_slice("/user.slice/user-waldo.slice", -ENXIO, NULL);
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering check_p_g_u_slice("foobar", -ENXIO, NULL);
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering check_p_g_u_slice("foobar.slice", -ENXIO, NULL);
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering check_p_g_u_slice("foo.slice/foo-bar.slice/waldo.service", -ENXIO, NULL);
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service", 0, "-.slice");
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service/", 0, "-.slice");
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service///", 0, "-.slice");
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service/waldo.service", 0, "-.slice");
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service/piep.slice/foo.service", 0, "piep.slice");
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering check_p_g_u_slice("/foo.slice//foo-bar.slice/user@1000.service/piep.slice//piep-pap.slice//foo.service", 0, "piep-pap.slice");
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) {
185a08745957cbd32e8293daf8c51ab9c995a71eDimitri John Ledkov assert_se(cg_controller_is_valid("foobar"));
185a08745957cbd32e8293daf8c51ab9c995a71eDimitri John Ledkov assert_se(cg_controller_is_valid("foo_bar"));
185a08745957cbd32e8293daf8c51ab9c995a71eDimitri John Ledkov assert_se(cg_controller_is_valid("name=foo"));
185a08745957cbd32e8293daf8c51ab9c995a71eDimitri John Ledkov assert_se(!cg_controller_is_valid("name="));
185a08745957cbd32e8293daf8c51ab9c995a71eDimitri John Ledkov assert_se(!cg_controller_is_valid("="));
185a08745957cbd32e8293daf8c51ab9c995a71eDimitri John Ledkov assert_se(!cg_controller_is_valid("cpu,cpuacct"));
185a08745957cbd32e8293daf8c51ab9c995a71eDimitri John Ledkov assert_se(!cg_controller_is_valid("_"));
185a08745957cbd32e8293daf8c51ab9c995a71eDimitri John Ledkov assert_se(!cg_controller_is_valid("_foobar"));
185a08745957cbd32e8293daf8c51ab9c995a71eDimitri John Ledkov assert_se(!cg_controller_is_valid("tatü"));
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);
c96cc5822c165e86be78ed96dac6573986032fabLennart Poettering test_slice_to_path_one("-.slice", "", 0);
c96cc5822c165e86be78ed96dac6573986032fabLennart Poettering test_slice_to_path_one("--.slice", NULL, -EINVAL);
c96cc5822c165e86be78ed96dac6573986032fabLennart Poettering test_slice_to_path_one("-", NULL, -EINVAL);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering test_slice_to_path_one("-foo-.slice", NULL, -EINVAL);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering test_slice_to_path_one("-foo.slice", NULL, -EINVAL);
c96cc5822c165e86be78ed96dac6573986032fabLennart Poettering test_slice_to_path_one("foo-.slice", NULL, -EINVAL);
e66e5b612a9e5921d79a6aedab4983e33dff8cb1Lennart Poettering test_slice_to_path_one("foo--bar.slice", NULL, -EINVAL);
c96cc5822c165e86be78ed96dac6573986032fabLennart Poettering test_slice_to_path_one("foo.slice/foo--bar.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");
5f4c5fef66581383ee852b301db67f687663004cLennart Poetteringstatic void test_mask_supported(void) {
5f4c5fef66581383ee852b301db67f687663004cLennart Poettering for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++)
5f4c5fef66581383ee852b301db67f687663004cLennart Poettering printf("'%s' is supported: %s\n", cgroup_controller_to_string(c), yes_no(m & CGROUP_CONTROLLER_TO_MASK(c)));
143bfdaf0b890fa7acadf02d1eafacaef1b696bdHolger Hans Peter Freyther TEST_REQ_RUNNING_SYSTEMD(test_get_paths());
143bfdaf0b890fa7acadf02d1eafacaef1b696bdHolger Hans Peter Freyther TEST_REQ_RUNNING_SYSTEMD(test_escape());