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
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include "alloc-util.h"
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek#include "cgroup-util.h"
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering#include "dirent-util.h"
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering#include "fd-util.h"
6482f6269c87d2249e52e889a63adbdd50f2d691Ronny Chevalier#include "formats-util.h"
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering#include "parse-util.h"
0b452006de98294d1690f045f6ea2f7f6630ec3bRonny Chevalier#include "process-util.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "string-util.h"
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering#include "test-helper.h"
ee104e11e303499a637c5cd8157bd12ad5cc116dLennart Poettering#include "user-util.h"
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering#include "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;
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek int r;
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek
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);
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek assert_se(r == code);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(streq_ptr(unit, result));
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering}
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek
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", -ENXIO, NULL);
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering check_p_d_u("getty/waldo", -ENXIO, NULL);
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering check_p_d_u("_cpu.service", 0, "cpu.service");
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;
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek int r;
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
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);
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek assert_se(r == code);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(streq_ptr(unit, result));
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering}
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
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);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering}
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poetteringstatic void check_p_g_u_u(const char *path, int code, const char *result) {
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering _cleanup_free_ char *unit = NULL;
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek int r;
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
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);
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek assert_se(r == code);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(streq_ptr(unit, result));
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering}
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
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);
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering}
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poetteringstatic void check_p_g_s(const char *path, int code, const char *result) {
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering _cleanup_free_ char *s = NULL;
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(cg_path_get_session(path, &s) == code);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(streq_ptr(s, result));
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek}
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek
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");
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering check_p_g_s("/session-3.scope", 0, "3");
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering check_p_g_s("/session-.scope", -ENXIO, NULL);
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering check_p_g_s("", -ENXIO, NULL);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering}
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poetteringstatic void check_p_g_o_u(const char *path, int code, uid_t result) {
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering uid_t uid = 0;
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(cg_path_get_owner_uid(path, &uid) == code);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(uid == result);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering}
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering
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);
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering check_p_g_o_u("", -ENXIO, 0);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering}
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poetteringstatic void check_p_g_slice(const char *path, int code, const char *result) {
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering _cleanup_free_ char *s = NULL;
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering assert_se(cg_path_get_slice(path, &s) == code);
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering assert_se(streq_ptr(s, result));
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering}
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering
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("", 0, "-.slice");
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering check_p_g_slice("foobar", 0, "-.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");
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering}
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poetteringstatic void check_p_g_u_slice(const char *path, int code, const char *result) {
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering _cleanup_free_ char *s = NULL;
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering assert_se(cg_path_get_user_slice(path, &s) == code);
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering assert_se(streq_ptr(s, result));
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering}
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering
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("", -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
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");
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering}
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poetteringstatic void test_get_paths(void) {
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering _cleanup_free_ char *a = NULL;
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(cg_get_root_path(&a) >= 0);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering log_info("Root = %s", a);
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) {
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering _cleanup_free_ char *path = NULL, *path_shifted = NULL, *session = NULL, *unit = NULL, *user_unit = NULL, *machine = NULL, *slice = NULL;
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering pid_t pid;
fed1e721fd0c81e60c77120539f34e16c2585634Lennart Poettering uid_t uid = UID_INVALID;
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);
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering cg_pid_get_path_shifted(pid, NULL, &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);
1021b21bc6f8dd522b46116e8598b17f9f93f1b7Lennart Poettering cg_pid_get_slice(pid, &slice);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering
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",
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek pid,
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering path,
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering path_shifted,
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek uid,
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering session,
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering unit,
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering user_unit,
1021b21bc6f8dd522b46116e8598b17f9f93f1b7Lennart Poettering machine,
1021b21bc6f8dd522b46116e8598b17f9f93f1b7Lennart Poettering slice);
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("tasks", "_tasks");
e13bb5d2b133f9ae51c0a2d20aa51071c780e9aeKay Sievers if (access("/sys/fs/cgroup/cpu", F_OK) == 0)
e13bb5d2b133f9ae51c0a2d20aa51071c780e9aeKay Sievers test_escape_one("cpu.service", "_cpu.service");
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) {
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(""));
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ü"));
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);
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);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering}
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poetteringstatic void test_shift_path_one(const char *raw, const char *root, const char *shifted) {
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poettering const char *s = NULL;
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poettering
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poettering assert_se(cg_shift_path(raw, root, &s) >= 0);
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poettering assert_se(streq(s, shifted));
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poettering}
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poettering
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poetteringstatic void test_shift_path(void) {
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poettering
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");
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poettering}
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poettering
5f4c5fef66581383ee852b301db67f687663004cLennart Poetteringstatic void test_mask_supported(void) {
5f4c5fef66581383ee852b301db67f687663004cLennart Poettering
5f4c5fef66581383ee852b301db67f687663004cLennart Poettering CGroupMask m;
5f4c5fef66581383ee852b301db67f687663004cLennart Poettering CGroupController c;
5f4c5fef66581383ee852b301db67f687663004cLennart Poettering
5f4c5fef66581383ee852b301db67f687663004cLennart Poettering assert_se(cg_mask_supported(&m) >= 0);
5f4c5fef66581383ee852b301db67f687663004cLennart Poettering
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)));
5f4c5fef66581383ee852b301db67f687663004cLennart Poettering}
5f4c5fef66581383ee852b301db67f687663004cLennart 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();
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering test_path_get_session();
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering test_path_get_owner_uid();
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering test_path_get_slice();
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering test_path_get_user_slice();
143bfdaf0b890fa7acadf02d1eafacaef1b696bdHolger Hans Peter Freyther TEST_REQ_RUNNING_SYSTEMD(test_get_paths());
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering test_proc();
143bfdaf0b890fa7acadf02d1eafacaef1b696bdHolger Hans Peter Freyther TEST_REQ_RUNNING_SYSTEMD(test_escape());
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poettering test_controller_is_valid();
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering test_slice_to_path();
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poettering test_shift_path();
b3a7ba896851708cce0e5026a814007fbb11b4cdMartin Pitt TEST_REQ_RUNNING_SYSTEMD(test_mask_supported());
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek return 0;
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek}