test-cgroup-util.c revision aff38e74bd776471f15ba54b305a24b0251eb865
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen This file is part of systemd.
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen Copyright 2013 Zbigniew Jędrzejewski-Szmek
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen systemd is free software; you can redistribute it and/or modify it
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen under the terms of the GNU Lesser General Public License as published by
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen the Free Software Foundation; either version 2.1 of the License, or
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen (at your option) any later version.
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen systemd is distributed in the hope that it will be useful, but
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen Lesser General Public License for more details.
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen You should have received a copy of the GNU Lesser General Public License
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersenstatic void check_p_d_u(const char *path, int code, const char *result) {
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen assert_se(cg_path_decode_unit(path, &unit) == code);
99634696183dfabae20104e58157c69029a11594Tom Gundersenstatic void test_path_decode_unit(void) {
99634696183dfabae20104e58157c69029a11594Tom Gundersen check_p_d_u("getty@.service/tty2", 0, "getty@tty2.service");
99634696183dfabae20104e58157c69029a11594Tom Gundersen check_p_d_u("getty@.service/tty2/xxx", 0, "getty@tty2.service");
99634696183dfabae20104e58157c69029a11594Tom Gundersen check_p_d_u("getty@.service/", -EINVAL, NULL);
6f08fb7b34b4eebf664de83c8bc7b9941658aeb4Tom Gundersen check_p_d_u("getty.service", 0, "getty.service");
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersenstatic void check_p_g_u(const char *path, int code, const char *result) {
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen assert_se(cg_path_get_unit(path, &unit) == code);
3a864fe4a894745ac61f1ecabd7cadf04139a284Tom Gundersenstatic void check_p_g_u_u(const char *path, int code, const char *result) {
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen assert_se(cg_path_get_user_unit(path, &unit) == code);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersenstatic void test_path_get_unit(void) {
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen check_p_g_u("/system/foobar.service/sdfdsaf", 0, "foobar.service");
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen check_p_g_u("/system/getty@.service/tty5", 0, "getty@tty5.service");
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen check_p_g_u("/system/getty@.service/tty5/aaa/bbb", 0, "getty@tty5.service");
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen check_p_g_u("/system/getty@.service/tty5/", 0, "getty@tty5.service");
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen check_p_g_u("/system/getty@tty6.service/tty5", 0, "getty@tty6.service");
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen check_p_g_u("/system/getty####@tty6.service/tty5", -EINVAL, NULL);
99634696183dfabae20104e58157c69029a11594Tom Gundersenstatic void test_path_get_user_unit(void) {
99634696183dfabae20104e58157c69029a11594Tom Gundersen check_p_g_u_u("/user/lennart/2/systemd-21548/foobar.service", 0, "foobar.service");
99634696183dfabae20104e58157c69029a11594Tom Gundersen check_p_g_u_u("/user/lennart/2/systemd-21548/foobar.service/waldo", 0, "foobar.service");
99634696183dfabae20104e58157c69029a11594Tom Gundersen check_p_g_u_u("/user/lennart/2/systemd-21548/foobar.service/waldo/uuuux", 0, "foobar.service");
99634696183dfabae20104e58157c69029a11594Tom Gundersen check_p_g_u_u("/user/lennart/2/systemd-21548/waldo/waldo/uuuux", -EINVAL, NULL);
99634696183dfabae20104e58157c69029a11594Tom Gundersen check_p_g_u_u("/user/lennart/2/foobar.service", -ENOENT, NULL);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen check_p_g_u_u("/user/lennart/2/systemd-21548/foobar@.service/pie/pa/po", 0, "foobar@pie.service");
6f08fb7b34b4eebf664de83c8bc7b9941658aeb4Tom Gundersenstatic void test_get_paths(void) {
6f08fb7b34b4eebf664de83c8bc7b9941658aeb4Tom Gundersen _cleanup_free_ char *a = NULL, *b = NULL, *c = NULL, *d = NULL;
be077570f779664ed87b50f60608df9fbe258821Tom Gundersenstatic void test_proc(void) {
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen _cleanup_free_ char *path = NULL, *path_shifted = NULL, *session = NULL, *unit = NULL, *user_unit = NULL, *machine = NULL, *prefix = NULL;
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &path);
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen cg_pid_get_path_shifted(pid, &prefix, &path_shifted);
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen (unsigned long) pid,