]>
Commit | Line | Data |
---|---|---|
6c03089c LP |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2013 Zbigniew Jędrzejewski-Szmek | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU Lesser General Public License as published by | |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | Lesser General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU Lesser General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
96cde13a ZJS |
22 | #include <assert.h> |
23 | ||
24 | #include "util.h" | |
25 | #include "cgroup-util.h" | |
26 | ||
7027ff61 | 27 | static void check_p_d_u(const char *path, int code, const char *result) { |
6c03089c | 28 | _cleanup_free_ char *unit = NULL; |
96cde13a | 29 | |
7027ff61 | 30 | assert_se(cg_path_decode_unit(path, &unit) == code); |
6c03089c LP |
31 | assert_se(streq_ptr(unit, result)); |
32 | } | |
96cde13a | 33 | |
7027ff61 | 34 | static void test_path_decode_unit(void) { |
ae018d9b LP |
35 | check_p_d_u("getty@.service/getty@tty2.service", 0, "getty@tty2.service"); |
36 | check_p_d_u("getty@.service/getty@tty2.service/xxx", 0, "getty@tty2.service"); | |
7027ff61 LP |
37 | check_p_d_u("getty@.service/", -EINVAL, NULL); |
38 | check_p_d_u("getty@.service", -EINVAL, NULL); | |
39 | check_p_d_u("getty.service", 0, "getty.service"); | |
40 | check_p_d_u("getty", -EINVAL, NULL); | |
6c03089c LP |
41 | } |
42 | ||
43 | static void check_p_g_u(const char *path, int code, const char *result) { | |
44 | _cleanup_free_ char *unit = NULL; | |
45 | ||
46 | assert_se(cg_path_get_unit(path, &unit) == code); | |
47 | assert_se(streq_ptr(unit, result)); | |
48 | } | |
49 | ||
50 | static void check_p_g_u_u(const char *path, int code, const char *result) { | |
51 | _cleanup_free_ char *unit = NULL; | |
52 | ||
53 | assert_se(cg_path_get_user_unit(path, &unit) == code); | |
54 | assert_se(streq_ptr(unit, result)); | |
55 | } | |
56 | ||
57 | static void test_path_get_unit(void) { | |
58 | check_p_g_u("/system/foobar.service/sdfdsaf", 0, "foobar.service"); | |
ae018d9b LP |
59 | check_p_g_u("/system/getty@.service/getty@tty5.service", 0, "getty@tty5.service"); |
60 | check_p_g_u("/system/getty@.service/getty@tty5.service/aaa/bbb", 0, "getty@tty5.service"); | |
61 | check_p_g_u("/system/getty@.service/getty@tty5.service/", 0, "getty@tty5.service"); | |
6c03089c LP |
62 | check_p_g_u("/system/getty@tty6.service/tty5", 0, "getty@tty6.service"); |
63 | check_p_g_u("sadfdsafsda", -ENOENT, NULL); | |
64 | check_p_g_u("/system/getty####@tty6.service/tty5", -EINVAL, NULL); | |
7027ff61 | 65 | } |
6c03089c | 66 | |
7027ff61 | 67 | static void test_path_get_user_unit(void) { |
6c03089c LP |
68 | check_p_g_u_u("/user/lennart/2/systemd-21548/foobar.service", 0, "foobar.service"); |
69 | check_p_g_u_u("/user/lennart/2/systemd-21548/foobar.service/waldo", 0, "foobar.service"); | |
70 | check_p_g_u_u("/user/lennart/2/systemd-21548/foobar.service/waldo/uuuux", 0, "foobar.service"); | |
71 | check_p_g_u_u("/user/lennart/2/systemd-21548/waldo/waldo/uuuux", -EINVAL, NULL); | |
72 | check_p_g_u_u("/user/lennart/2/foobar.service", -ENOENT, NULL); | |
ae018d9b | 73 | check_p_g_u_u("/user/lennart/2/systemd-21548/foobar@.service/foobar@pie.service/pa/po", 0, "foobar@pie.service"); |
96cde13a ZJS |
74 | } |
75 | ||
7027ff61 LP |
76 | static void test_get_paths(void) { |
77 | _cleanup_free_ char *a = NULL, *b = NULL, *c = NULL, *d = NULL; | |
78 | ||
79 | assert_se(cg_get_root_path(&a) >= 0); | |
80 | log_info("Root = %s", a); | |
81 | ||
82 | assert_se(cg_get_system_path(&b) >= 0); | |
83 | log_info("System = %s", b); | |
84 | ||
85 | assert_se(cg_get_user_path(&c) >= 0); | |
86 | log_info("User = %s", c); | |
87 | ||
88 | assert_se(cg_get_machine_path(&d) >= 0); | |
89 | log_info("Machine = %s", d); | |
90 | } | |
91 | ||
aff38e74 LP |
92 | static void test_proc(void) { |
93 | _cleanup_closedir_ DIR *d = NULL; | |
94 | struct dirent *de; | |
95 | int r; | |
96 | ||
97 | d = opendir("/proc"); | |
98 | assert_se(d); | |
99 | ||
100 | FOREACH_DIRENT(de, d, break) { | |
101 | _cleanup_free_ char *path = NULL, *path_shifted = NULL, *session = NULL, *unit = NULL, *user_unit = NULL, *machine = NULL, *prefix = NULL; | |
102 | pid_t pid; | |
ae018d9b | 103 | uid_t uid = (uid_t) -1; |
aff38e74 LP |
104 | |
105 | if (de->d_type != DT_DIR && | |
106 | de->d_type != DT_UNKNOWN) | |
107 | continue; | |
108 | ||
109 | r = parse_pid(de->d_name, &pid); | |
110 | if (r < 0) | |
111 | continue; | |
112 | ||
ae018d9b LP |
113 | if (is_kernel_thread(pid)) |
114 | continue; | |
115 | ||
aff38e74 LP |
116 | cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &path); |
117 | cg_pid_get_path_shifted(pid, &prefix, &path_shifted); | |
ae018d9b | 118 | cg_pid_get_owner_uid(pid, &uid); |
aff38e74 LP |
119 | cg_pid_get_session(pid, &session); |
120 | cg_pid_get_unit(pid, &unit); | |
121 | cg_pid_get_user_unit(pid, &user_unit); | |
122 | cg_pid_get_machine_name(pid, &machine); | |
123 | ||
ae018d9b | 124 | printf("%lu\t%s\t%s\t%s\t%lu\t%s\t%s\t%s\t%s\n", |
aff38e74 LP |
125 | (unsigned long) pid, |
126 | path, | |
127 | prefix, | |
128 | path_shifted, | |
ae018d9b | 129 | (unsigned long) uid, |
aff38e74 LP |
130 | session, |
131 | unit, | |
132 | user_unit, | |
133 | machine); | |
134 | } | |
135 | } | |
136 | ||
ae018d9b LP |
137 | static void test_escape_one(const char *s, const char *r) { |
138 | _cleanup_free_ char *b; | |
139 | ||
140 | b = cg_escape(s); | |
141 | assert_se(b); | |
142 | assert_se(streq(b, r)); | |
143 | ||
144 | assert_se(streq(cg_unescape(b), s)); | |
145 | } | |
146 | ||
147 | static void test_escape(void) { | |
148 | test_escape_one("foobar", "foobar"); | |
149 | test_escape_one("foobar.service", "foobar.service"); | |
150 | test_escape_one("cgroup.service", "_cgroup.service"); | |
151 | test_escape_one("cpu.service", "_cpu.service"); | |
152 | test_escape_one("tasks", "_tasks"); | |
153 | test_escape_one("_foobar", "__foobar"); | |
154 | } | |
155 | ||
96cde13a | 156 | int main(void) { |
7027ff61 | 157 | test_path_decode_unit(); |
6c03089c | 158 | test_path_get_unit(); |
7027ff61 LP |
159 | test_path_get_user_unit(); |
160 | test_get_paths(); | |
aff38e74 | 161 | test_proc(); |
ae018d9b | 162 | test_escape(); |
96cde13a ZJS |
163 | |
164 | return 0; | |
165 | } |