test-cgroup-util.c revision aff38e74bd776471f15ba54b305a24b0251eb865
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen/***
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen This file is part of systemd.
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen Copyright 2013 Zbigniew Jędrzejewski-Szmek
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
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
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
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 Gundersen***/
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen#include <assert.h>
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen#include "util.h"
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen#include "cgroup-util.h"
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersenstatic void check_p_d_u(const char *path, int code, const char *result) {
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen _cleanup_free_ char *unit = NULL;
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen assert_se(cg_path_decode_unit(path, &unit) == code);
99634696183dfabae20104e58157c69029a11594Tom Gundersen assert_se(streq_ptr(unit, result));
99634696183dfabae20104e58157c69029a11594Tom Gundersen}
99634696183dfabae20104e58157c69029a11594Tom Gundersen
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);
99634696183dfabae20104e58157c69029a11594Tom Gundersen check_p_d_u("getty@.service", -EINVAL, NULL);
6f08fb7b34b4eebf664de83c8bc7b9941658aeb4Tom Gundersen check_p_d_u("getty.service", 0, "getty.service");
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen check_p_d_u("getty", -EINVAL, NULL);
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen}
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersenstatic void check_p_g_u(const char *path, int code, const char *result) {
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen _cleanup_free_ char *unit = NULL;
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen assert_se(cg_path_get_unit(path, &unit) == code);
6f08fb7b34b4eebf664de83c8bc7b9941658aeb4Tom Gundersen assert_se(streq_ptr(unit, result));
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen}
3a864fe4a894745ac61f1ecabd7cadf04139a284Tom Gundersen
3a864fe4a894745ac61f1ecabd7cadf04139a284Tom Gundersenstatic void check_p_g_u_u(const char *path, int code, const char *result) {
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen _cleanup_free_ char *unit = NULL;
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen assert_se(cg_path_get_user_unit(path, &unit) == code);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen assert_se(streq_ptr(unit, result));
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen}
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
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");
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen check_p_g_u("sadfdsafsda", -ENOENT, NULL);
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen check_p_g_u("/system/getty####@tty6.service/tty5", -EINVAL, NULL);
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen}
99634696183dfabae20104e58157c69029a11594Tom Gundersen
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 Gundersen}
6f08fb7b34b4eebf664de83c8bc7b9941658aeb4Tom Gundersen
6f08fb7b34b4eebf664de83c8bc7b9941658aeb4Tom Gundersenstatic void test_get_paths(void) {
6f08fb7b34b4eebf664de83c8bc7b9941658aeb4Tom Gundersen _cleanup_free_ char *a = NULL, *b = NULL, *c = NULL, *d = NULL;
6f08fb7b34b4eebf664de83c8bc7b9941658aeb4Tom Gundersen
6f08fb7b34b4eebf664de83c8bc7b9941658aeb4Tom Gundersen assert_se(cg_get_root_path(&a) >= 0);
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen log_info("Root = %s", a);
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen assert_se(cg_get_system_path(&b) >= 0);
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen log_info("System = %s", b);
6f08fb7b34b4eebf664de83c8bc7b9941658aeb4Tom Gundersen
6f08fb7b34b4eebf664de83c8bc7b9941658aeb4Tom Gundersen assert_se(cg_get_user_path(&c) >= 0);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen log_info("User = %s", c);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen assert_se(cg_get_machine_path(&d) >= 0);
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen log_info("Machine = %s", d);
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen}
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen
be077570f779664ed87b50f60608df9fbe258821Tom Gundersenstatic void test_proc(void) {
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen _cleanup_closedir_ DIR *d = NULL;
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen struct dirent *de;
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen int r;
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen d = opendir("/proc");
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen assert_se(d);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen FOREACH_DIRENT(de, d, break) {
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen _cleanup_free_ char *path = NULL, *path_shifted = NULL, *session = NULL, *unit = NULL, *user_unit = NULL, *machine = NULL, *prefix = NULL;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen pid_t pid;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen if (de->d_type != DT_DIR &&
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen de->d_type != DT_UNKNOWN)
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen continue;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen r = parse_pid(de->d_name, &pid);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen if (r < 0)
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen continue;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &path);
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen cg_pid_get_path_shifted(pid, &prefix, &path_shifted);
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen cg_pid_get_session(pid, &session);
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen cg_pid_get_unit(pid, &unit);
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen cg_pid_get_user_unit(pid, &user_unit);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen cg_pid_get_machine_name(pid, &machine);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen printf("%lu\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen (unsigned long) pid,
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen path,
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen prefix,
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen path_shifted,
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen session,
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen unit,
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen user_unit,
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen machine);
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen }
99634696183dfabae20104e58157c69029a11594Tom Gundersen}
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersenint main(void) {
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen test_path_decode_unit();
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen test_path_get_unit();
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen test_path_get_user_unit();
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen test_get_paths();
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen test_proc();
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen return 0;
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen}
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen