test-cgroup-util.c revision a016b9228f338cb9b380ce7e00826ef462767d98
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering/***
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering This file is part of systemd.
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering Copyright 2013 Zbigniew Jędrzejewski-Szmek
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
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
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
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/>.
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering***/
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek#include <assert.h>
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek#include "util.h"
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek#include "cgroup-util.h"
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek
7027ff61a34a12487712b382a061c654acc3a679Lennart Poetteringstatic void check_p_d_u(const char *path, int code, const char *result) {
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering _cleanup_free_ char *unit = NULL;
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering assert_se(cg_path_decode_unit(path, &unit) == code);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(streq_ptr(unit, result));
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering}
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek
7027ff61a34a12487712b382a061c654acc3a679Lennart Poetteringstatic void test_path_decode_unit(void) {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering check_p_d_u("getty@.service/getty@tty2.service", 0, "getty@tty2.service");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering check_p_d_u("getty@.service/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");
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering check_p_d_u("getty", -EINVAL, NULL);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering}
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poetteringstatic void check_p_g_u(const char *path, int code, const char *result) {
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering _cleanup_free_ char *unit = NULL;
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(cg_path_get_unit(path, &unit) == code);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(streq_ptr(unit, result));
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering}
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poetteringstatic void check_p_g_u_u(const char *path, int code, const char *result) {
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering _cleanup_free_ char *unit = NULL;
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(cg_path_get_user_unit(path, &unit) == code);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(streq_ptr(unit, result));
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering}
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poetteringstatic void test_path_get_unit(void) {
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering check_p_g_u("/system/foobar.service/sdfdsaf", 0, "foobar.service");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering check_p_g_u("/system/getty@.service/getty@tty5.service", 0, "getty@tty5.service");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering check_p_g_u("/system/getty@.service/getty@tty5.service/aaa/bbb", 0, "getty@tty5.service");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering check_p_g_u("/system/getty@.service/getty@tty5.service/", 0, "getty@tty5.service");
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering check_p_g_u("/system/getty@tty6.service/tty5", 0, "getty@tty6.service");
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering check_p_g_u("sadfdsafsda", -ENOENT, NULL);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering check_p_g_u("/system/getty####@tty6.service/tty5", -EINVAL, NULL);
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering}
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
7027ff61a34a12487712b382a061c654acc3a679Lennart Poetteringstatic void test_path_get_user_unit(void) {
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering check_p_g_u_u("/user/lennart/2/systemd-21548/foobar.service", 0, "foobar.service");
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering check_p_g_u_u("/user/lennart/2/systemd-21548/foobar.service/waldo", 0, "foobar.service");
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering check_p_g_u_u("/user/lennart/2/systemd-21548/foobar.service/waldo/uuuux", 0, "foobar.service");
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering check_p_g_u_u("/user/lennart/2/systemd-21548/waldo/waldo/uuuux", -EINVAL, NULL);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering check_p_g_u_u("/user/lennart/2/foobar.service", -ENOENT, NULL);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering check_p_g_u_u("/user/lennart/2/systemd-21548/foobar@.service/foobar@pie.service/pa/po", 0, "foobar@pie.service");
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek}
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek
7027ff61a34a12487712b382a061c654acc3a679Lennart Poetteringstatic void test_get_paths(void) {
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering _cleanup_free_ char *a = NULL, *b = NULL, *c = NULL, *d = NULL;
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering assert_se(cg_get_root_path(&a) >= 0);
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering log_info("Root = %s", a);
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering assert_se(cg_get_system_path(&b) >= 0);
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering log_info("System = %s", b);
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering assert_se(cg_get_user_path(&c) >= 0);
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering log_info("User = %s", c);
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering
38158b920e772ea3a7cc9dfcf705666ce3aa5ce3Zbigniew Jędrzejewski-Szmek assert_se(cg_get_machine_path("harley", &d) >= 0);
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering log_info("Machine = %s", d);
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering}
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poetteringstatic void test_proc(void) {
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering _cleanup_closedir_ DIR *d = NULL;
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering struct dirent *de;
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering int r;
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering d = opendir("/proc");
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering assert_se(d);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering FOREACH_DIRENT(de, d, break) {
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering _cleanup_free_ char *path = NULL, *path_shifted = NULL, *session = NULL, *unit = NULL, *user_unit = NULL, *machine = NULL, *prefix = NULL;
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering pid_t pid;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering uid_t uid = (uid_t) -1;
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering if (de->d_type != DT_DIR &&
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering de->d_type != DT_UNKNOWN)
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering continue;
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering r = parse_pid(de->d_name, &pid);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering if (r < 0)
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering continue;
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering if (is_kernel_thread(pid))
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering continue;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &path);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering cg_pid_get_path_shifted(pid, &prefix, &path_shifted);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering cg_pid_get_owner_uid(pid, &uid);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering cg_pid_get_session(pid, &session);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering cg_pid_get_unit(pid, &unit);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering cg_pid_get_user_unit(pid, &user_unit);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering cg_pid_get_machine_name(pid, &machine);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering printf("%lu\t%s\t%s\t%s\t%lu\t%s\t%s\t%s\t%s\n",
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering (unsigned long) pid,
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering path,
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering prefix,
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering path_shifted,
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering (unsigned long) uid,
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering session,
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering unit,
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering user_unit,
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering machine);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering }
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering}
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poetteringstatic void test_escape_one(const char *s, const char *r) {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering _cleanup_free_ char *b;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering b = cg_escape(s);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering assert_se(b);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering assert_se(streq(b, r));
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering assert_se(streq(cg_unescape(b), s));
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering}
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poetteringstatic void test_escape(void) {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering test_escape_one("foobar", "foobar");
a0ab566574303be1ca12cdb334f284cfd407caa5Lennart Poettering test_escape_one(".foobar", "_.foobar");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering test_escape_one("foobar.service", "foobar.service");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering test_escape_one("cgroup.service", "_cgroup.service");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering test_escape_one("cpu.service", "_cpu.service");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering test_escape_one("tasks", "_tasks");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering test_escape_one("_foobar", "__foobar");
a0ab566574303be1ca12cdb334f284cfd407caa5Lennart Poettering test_escape_one("", "_");
a0ab566574303be1ca12cdb334f284cfd407caa5Lennart Poettering test_escape_one("_", "__");
a0ab566574303be1ca12cdb334f284cfd407caa5Lennart Poettering test_escape_one(".", "_.");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering}
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
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));
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poettering}
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poettering
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poetteringstatic void test_slice_to_path_one(const char *unit, const char *path, int error) {
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering _cleanup_free_ char *ret = NULL;
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering assert_se(cg_slice_to_path(unit, &ret) == error);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering assert_se(streq_ptr(ret, path));
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering}
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poetteringstatic void test_slice_to_path(void) {
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering
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);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering}
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmekint main(void) {
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering test_path_decode_unit();
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering test_path_get_unit();
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering test_path_get_user_unit();
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering test_get_paths();
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering test_proc();
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering test_escape();
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poettering test_controller_is_valid();
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering test_slice_to_path();
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek return 0;
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek}