]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-process-util.c
Merge pull request #3481 from poettering/relative-memcg
[thirdparty/systemd.git] / src / test / test-process-util.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5 Copyright 2013 Thomas H.P. Andersen
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <sched.h>
22 #include <sys/mount.h>
23 #include <sys/personality.h>
24 #include <sys/prctl.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <sys/wait.h>
28 #include <unistd.h>
29
30 #include "alloc-util.h"
31 #include "architecture.h"
32 #include "fd-util.h"
33 #include "log.h"
34 #include "macro.h"
35 #include "parse-util.h"
36 #include "process-util.h"
37 #include "stdio-util.h"
38 #include "string-util.h"
39 #include "terminal-util.h"
40 #include "util.h"
41 #include "virt.h"
42
43 static void test_get_process_comm(pid_t pid) {
44 struct stat st;
45 _cleanup_free_ char *a = NULL, *c = NULL, *d = NULL, *f = NULL, *i = NULL, *cwd = NULL, *root = NULL;
46 _cleanup_free_ char *env = NULL;
47 char path[strlen("/proc//comm") + DECIMAL_STR_MAX(pid_t)];
48 pid_t e;
49 uid_t u;
50 gid_t g;
51 dev_t h;
52 int r;
53
54 xsprintf(path, "/proc/"PID_FMT"/comm", pid);
55
56 if (stat(path, &st) == 0) {
57 assert_se(get_process_comm(pid, &a) >= 0);
58 log_info("PID"PID_FMT" comm: '%s'", pid, a);
59 } else
60 log_warning("%s not exist.", path);
61
62 assert_se(get_process_cmdline(pid, 0, true, &c) >= 0);
63 log_info("PID"PID_FMT" cmdline: '%s'", pid, c);
64
65 assert_se(get_process_cmdline(pid, 8, false, &d) >= 0);
66 log_info("PID"PID_FMT" cmdline truncated to 8: '%s'", pid, d);
67
68 free(d);
69 assert_se(get_process_cmdline(pid, 1, false, &d) >= 0);
70 log_info("PID"PID_FMT" cmdline truncated to 1: '%s'", pid, d);
71
72 assert_se(get_process_ppid(pid, &e) >= 0);
73 log_info("PID"PID_FMT" PPID: "PID_FMT, pid, e);
74 assert_se(pid == 1 ? e == 0 : e > 0);
75
76 assert_se(is_kernel_thread(pid) == 0 || pid != 1);
77
78 r = get_process_exe(pid, &f);
79 assert_se(r >= 0 || r == -EACCES);
80 log_info("PID"PID_FMT" exe: '%s'", pid, strna(f));
81
82 assert_se(get_process_uid(pid, &u) == 0);
83 log_info("PID"PID_FMT" UID: "UID_FMT, pid, u);
84 assert_se(u == 0 || pid != 1);
85
86 assert_se(get_process_gid(pid, &g) == 0);
87 log_info("PID"PID_FMT" GID: "GID_FMT, pid, g);
88 assert_se(g == 0 || pid != 1);
89
90 r = get_process_environ(pid, &env);
91 assert_se(r >= 0 || r == -EACCES);
92 log_info("PID"PID_FMT" strlen(environ): %zi", pid, env ? (ssize_t)strlen(env) : (ssize_t)-errno);
93
94 if (!detect_container())
95 assert_se(get_ctty_devnr(pid, &h) == -ENXIO || pid != 1);
96
97 getenv_for_pid(pid, "PATH", &i);
98 log_info("PID"PID_FMT" $PATH: '%s'", pid, strna(i));
99 }
100
101 static void test_pid_is_unwaited(void) {
102 pid_t pid;
103
104 pid = fork();
105 assert_se(pid >= 0);
106 if (pid == 0) {
107 _exit(EXIT_SUCCESS);
108 } else {
109 int status;
110
111 waitpid(pid, &status, 0);
112 assert_se(!pid_is_unwaited(pid));
113 }
114 assert_se(pid_is_unwaited(getpid()));
115 assert_se(!pid_is_unwaited(-1));
116 }
117
118 static void test_pid_is_alive(void) {
119 pid_t pid;
120
121 pid = fork();
122 assert_se(pid >= 0);
123 if (pid == 0) {
124 _exit(EXIT_SUCCESS);
125 } else {
126 int status;
127
128 waitpid(pid, &status, 0);
129 assert_se(!pid_is_alive(pid));
130 }
131 assert_se(pid_is_alive(getpid()));
132 assert_se(!pid_is_alive(-1));
133 }
134
135 static void test_personality(void) {
136
137 assert_se(personality_to_string(PER_LINUX));
138 assert_se(!personality_to_string(PERSONALITY_INVALID));
139
140 assert_se(streq(personality_to_string(PER_LINUX), architecture_to_string(native_architecture())));
141
142 assert_se(personality_from_string(personality_to_string(PER_LINUX)) == PER_LINUX);
143 assert_se(personality_from_string(architecture_to_string(native_architecture())) == PER_LINUX);
144
145 #ifdef __x86_64__
146 assert_se(streq_ptr(personality_to_string(PER_LINUX), "x86-64"));
147 assert_se(streq_ptr(personality_to_string(PER_LINUX32), "x86"));
148
149 assert_se(personality_from_string("x86-64") == PER_LINUX);
150 assert_se(personality_from_string("x86") == PER_LINUX32);
151 assert_se(personality_from_string("ia64") == PERSONALITY_INVALID);
152 assert_se(personality_from_string(NULL) == PERSONALITY_INVALID);
153
154 assert_se(personality_from_string(personality_to_string(PER_LINUX32)) == PER_LINUX32);
155 #endif
156 }
157
158 static void test_get_process_cmdline_harder(void) {
159 char path[] = "/tmp/test-cmdlineXXXXXX";
160 _cleanup_close_ int fd = -1;
161 _cleanup_free_ char *line = NULL;
162 pid_t pid;
163
164 if (geteuid() != 0)
165 return;
166
167 pid = fork();
168 if (pid > 0) {
169 siginfo_t si;
170
171 (void) wait_for_terminate(pid, &si);
172
173 assert_se(si.si_code == CLD_EXITED);
174 assert_se(si.si_status == 0);
175
176 return;
177 }
178
179 assert_se(pid == 0);
180 assert_se(unshare(CLONE_NEWNS) >= 0);
181
182 fd = mkostemp(path, O_CLOEXEC);
183 assert_se(fd >= 0);
184 assert_se(mount(path, "/proc/self/cmdline", "bind", MS_BIND, NULL) >= 0);
185 assert_se(unlink(path) >= 0);
186
187 assert_se(prctl(PR_SET_NAME, "testa") >= 0);
188
189 assert_se(get_process_cmdline(getpid(), 0, false, &line) == -ENOENT);
190
191 assert_se(get_process_cmdline(getpid(), 0, true, &line) >= 0);
192 assert_se(streq(line, "[testa]"));
193 line = mfree(line);
194
195 assert_se(get_process_cmdline(getpid(), 1, true, &line) >= 0);
196 assert_se(streq(line, ""));
197 line = mfree(line);
198
199 assert_se(get_process_cmdline(getpid(), 2, true, &line) >= 0);
200 assert_se(streq(line, "["));
201 line = mfree(line);
202
203 assert_se(get_process_cmdline(getpid(), 3, true, &line) >= 0);
204 assert_se(streq(line, "[."));
205 line = mfree(line);
206
207 assert_se(get_process_cmdline(getpid(), 4, true, &line) >= 0);
208 assert_se(streq(line, "[.."));
209 line = mfree(line);
210
211 assert_se(get_process_cmdline(getpid(), 5, true, &line) >= 0);
212 assert_se(streq(line, "[..."));
213 line = mfree(line);
214
215 assert_se(get_process_cmdline(getpid(), 6, true, &line) >= 0);
216 assert_se(streq(line, "[...]"));
217 line = mfree(line);
218
219 assert_se(get_process_cmdline(getpid(), 7, true, &line) >= 0);
220 assert_se(streq(line, "[t...]"));
221 line = mfree(line);
222
223 assert_se(get_process_cmdline(getpid(), 8, true, &line) >= 0);
224 assert_se(streq(line, "[testa]"));
225 line = mfree(line);
226
227 assert_se(write(fd, "\0\0\0\0\0\0\0\0\0", 10) == 10);
228
229 assert_se(get_process_cmdline(getpid(), 0, false, &line) == -ENOENT);
230
231 assert_se(get_process_cmdline(getpid(), 0, true, &line) >= 0);
232 assert_se(streq(line, "[testa]"));
233 line = mfree(line);
234
235 assert_se(write(fd, "foo\0bar\0\0\0\0\0", 10) == 10);
236
237 assert_se(get_process_cmdline(getpid(), 0, false, &line) >= 0);
238 assert_se(streq(line, "foo bar"));
239 line = mfree(line);
240
241 assert_se(get_process_cmdline(getpid(), 0, true, &line) >= 0);
242 assert_se(streq(line, "foo bar"));
243 line = mfree(line);
244
245 assert_se(write(fd, "quux", 4) == 4);
246 assert_se(get_process_cmdline(getpid(), 0, false, &line) >= 0);
247 assert_se(streq(line, "foo bar quux"));
248 line = mfree(line);
249
250 assert_se(get_process_cmdline(getpid(), 0, true, &line) >= 0);
251 assert_se(streq(line, "foo bar quux"));
252 line = mfree(line);
253
254 assert_se(get_process_cmdline(getpid(), 1, true, &line) >= 0);
255 assert_se(streq(line, ""));
256 line = mfree(line);
257
258 assert_se(get_process_cmdline(getpid(), 2, true, &line) >= 0);
259 assert_se(streq(line, "."));
260 line = mfree(line);
261
262 assert_se(get_process_cmdline(getpid(), 3, true, &line) >= 0);
263 assert_se(streq(line, ".."));
264 line = mfree(line);
265
266 assert_se(get_process_cmdline(getpid(), 4, true, &line) >= 0);
267 assert_se(streq(line, "..."));
268 line = mfree(line);
269
270 assert_se(get_process_cmdline(getpid(), 5, true, &line) >= 0);
271 assert_se(streq(line, "f..."));
272 line = mfree(line);
273
274 assert_se(get_process_cmdline(getpid(), 6, true, &line) >= 0);
275 assert_se(streq(line, "fo..."));
276 line = mfree(line);
277
278 assert_se(get_process_cmdline(getpid(), 7, true, &line) >= 0);
279 assert_se(streq(line, "foo..."));
280 line = mfree(line);
281
282 assert_se(get_process_cmdline(getpid(), 8, true, &line) >= 0);
283 assert_se(streq(line, "foo..."));
284 line = mfree(line);
285
286 assert_se(get_process_cmdline(getpid(), 9, true, &line) >= 0);
287 assert_se(streq(line, "foo b..."));
288 line = mfree(line);
289
290 assert_se(get_process_cmdline(getpid(), 10, true, &line) >= 0);
291 assert_se(streq(line, "foo ba..."));
292 line = mfree(line);
293
294 assert_se(get_process_cmdline(getpid(), 11, true, &line) >= 0);
295 assert_se(streq(line, "foo bar..."));
296 line = mfree(line);
297
298 assert_se(get_process_cmdline(getpid(), 12, true, &line) >= 0);
299 assert_se(streq(line, "foo bar..."));
300 line = mfree(line);
301
302 assert_se(get_process_cmdline(getpid(), 13, true, &line) >= 0);
303 assert_se(streq(line, "foo bar quux"));
304 line = mfree(line);
305
306 assert_se(get_process_cmdline(getpid(), 14, true, &line) >= 0);
307 assert_se(streq(line, "foo bar quux"));
308 line = mfree(line);
309
310 assert_se(get_process_cmdline(getpid(), 1000, true, &line) >= 0);
311 assert_se(streq(line, "foo bar quux"));
312 line = mfree(line);
313
314 assert_se(ftruncate(fd, 0) >= 0);
315 assert_se(prctl(PR_SET_NAME, "aaaa bbbb cccc") >= 0);
316
317 assert_se(get_process_cmdline(getpid(), 0, false, &line) == -ENOENT);
318
319 assert_se(get_process_cmdline(getpid(), 0, true, &line) >= 0);
320 assert_se(streq(line, "[aaaa bbbb cccc]"));
321 line = mfree(line);
322
323 assert_se(get_process_cmdline(getpid(), 10, true, &line) >= 0);
324 assert_se(streq(line, "[aaaa...]"));
325 line = mfree(line);
326
327 assert_se(get_process_cmdline(getpid(), 11, true, &line) >= 0);
328 assert_se(streq(line, "[aaaa...]"));
329 line = mfree(line);
330
331 assert_se(get_process_cmdline(getpid(), 12, true, &line) >= 0);
332 assert_se(streq(line, "[aaaa b...]"));
333 line = mfree(line);
334
335 safe_close(fd);
336 _exit(0);
337 }
338
339 int main(int argc, char *argv[]) {
340 log_parse_environment();
341 log_open();
342
343 if (argc > 1) {
344 pid_t pid = 0;
345
346 (void) parse_pid(argv[1], &pid);
347 test_get_process_comm(pid);
348 } else {
349 test_get_process_comm(1);
350 test_get_process_comm(getpid());
351 }
352
353 test_pid_is_unwaited();
354 test_pid_is_alive();
355 test_personality();
356 test_get_process_cmdline_harder();
357
358 return 0;
359 }