]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-process-util.c
tree-wide: remove Lennart's copyright lines
[thirdparty/systemd.git] / src / test / test-process-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright 2013 Thomas H.P. Andersen
4 ***/
5
6 #include <sched.h>
7 #include <sys/mount.h>
8 #include <sys/personality.h>
9 #include <sys/prctl.h>
10 #include <sys/stat.h>
11 #include <sys/types.h>
12 #include <sys/wait.h>
13 #include <unistd.h>
14 #if HAVE_VALGRIND_VALGRIND_H
15 #include <valgrind/valgrind.h>
16 #endif
17
18 #include "alloc-util.h"
19 #include "architecture.h"
20 #include "fd-util.h"
21 #include "log.h"
22 #include "macro.h"
23 #include "parse-util.h"
24 #include "process-util.h"
25 #include "signal-util.h"
26 #include "stdio-util.h"
27 #include "string-util.h"
28 #include "terminal-util.h"
29 #include "test-helper.h"
30 #include "util.h"
31 #include "virt.h"
32
33 static void test_get_process_comm(pid_t pid) {
34 struct stat st;
35 _cleanup_free_ char *a = NULL, *c = NULL, *d = NULL, *f = NULL, *i = NULL;
36 _cleanup_free_ char *env = NULL;
37 char path[STRLEN("/proc//comm") + DECIMAL_STR_MAX(pid_t)];
38 pid_t e;
39 uid_t u;
40 gid_t g;
41 dev_t h;
42 int r;
43
44 xsprintf(path, "/proc/"PID_FMT"/comm", pid);
45
46 if (stat(path, &st) == 0) {
47 assert_se(get_process_comm(pid, &a) >= 0);
48 log_info("PID"PID_FMT" comm: '%s'", pid, a);
49 } else
50 log_warning("%s not exist.", path);
51
52 assert_se(get_process_cmdline(pid, 0, true, &c) >= 0);
53 log_info("PID"PID_FMT" cmdline: '%s'", pid, c);
54
55 assert_se(get_process_cmdline(pid, 8, false, &d) >= 0);
56 log_info("PID"PID_FMT" cmdline truncated to 8: '%s'", pid, d);
57
58 free(d);
59 assert_se(get_process_cmdline(pid, 1, false, &d) >= 0);
60 log_info("PID"PID_FMT" cmdline truncated to 1: '%s'", pid, d);
61
62 assert_se(get_process_ppid(pid, &e) >= 0);
63 log_info("PID"PID_FMT" PPID: "PID_FMT, pid, e);
64 assert_se(pid == 1 ? e == 0 : e > 0);
65
66 assert_se(is_kernel_thread(pid) == 0 || pid != 1);
67
68 r = get_process_exe(pid, &f);
69 assert_se(r >= 0 || r == -EACCES);
70 log_info("PID"PID_FMT" exe: '%s'", pid, strna(f));
71
72 assert_se(get_process_uid(pid, &u) == 0);
73 log_info("PID"PID_FMT" UID: "UID_FMT, pid, u);
74 assert_se(u == 0 || pid != 1);
75
76 assert_se(get_process_gid(pid, &g) == 0);
77 log_info("PID"PID_FMT" GID: "GID_FMT, pid, g);
78 assert_se(g == 0 || pid != 1);
79
80 r = get_process_environ(pid, &env);
81 assert_se(r >= 0 || r == -EACCES);
82 log_info("PID"PID_FMT" strlen(environ): %zi", pid, env ? (ssize_t)strlen(env) : (ssize_t)-errno);
83
84 if (!detect_container())
85 assert_se(get_ctty_devnr(pid, &h) == -ENXIO || pid != 1);
86
87 (void) getenv_for_pid(pid, "PATH", &i);
88 log_info("PID"PID_FMT" $PATH: '%s'", pid, strna(i));
89 }
90
91 static void test_get_process_comm_escape_one(const char *input, const char *output) {
92 _cleanup_free_ char *n = NULL;
93
94 log_info("input: <%s> — output: <%s>", input, output);
95
96 assert_se(prctl(PR_SET_NAME, input) >= 0);
97 assert_se(get_process_comm(0, &n) >= 0);
98
99 log_info("got: <%s>", n);
100
101 assert_se(streq_ptr(n, output));
102 }
103
104 static void test_get_process_comm_escape(void) {
105 _cleanup_free_ char *saved = NULL;
106
107 assert_se(get_process_comm(0, &saved) >= 0);
108
109 test_get_process_comm_escape_one("", "");
110 test_get_process_comm_escape_one("foo", "foo");
111 test_get_process_comm_escape_one("012345678901234", "012345678901234");
112 test_get_process_comm_escape_one("0123456789012345", "012345678901234");
113 test_get_process_comm_escape_one("äöüß", "\\303\\244\\303…");
114 test_get_process_comm_escape_one("xäöüß", "x\\303\\244…");
115 test_get_process_comm_escape_one("xxäöüß", "xx\\303\\244…");
116 test_get_process_comm_escape_one("xxxäöüß", "xxx\\303\\244…");
117 test_get_process_comm_escape_one("xxxxäöüß", "xxxx\\303\\244…");
118 test_get_process_comm_escape_one("xxxxxäöüß", "xxxxx\\303…");
119
120 assert_se(prctl(PR_SET_NAME, saved) >= 0);
121 }
122
123 static void test_pid_is_unwaited(void) {
124 pid_t pid;
125
126 pid = fork();
127 assert_se(pid >= 0);
128 if (pid == 0) {
129 _exit(EXIT_SUCCESS);
130 } else {
131 int status;
132
133 waitpid(pid, &status, 0);
134 assert_se(!pid_is_unwaited(pid));
135 }
136 assert_se(pid_is_unwaited(getpid_cached()));
137 assert_se(!pid_is_unwaited(-1));
138 }
139
140 static void test_pid_is_alive(void) {
141 pid_t pid;
142
143 pid = fork();
144 assert_se(pid >= 0);
145 if (pid == 0) {
146 _exit(EXIT_SUCCESS);
147 } else {
148 int status;
149
150 waitpid(pid, &status, 0);
151 assert_se(!pid_is_alive(pid));
152 }
153 assert_se(pid_is_alive(getpid_cached()));
154 assert_se(!pid_is_alive(-1));
155 }
156
157 static void test_personality(void) {
158
159 assert_se(personality_to_string(PER_LINUX));
160 assert_se(!personality_to_string(PERSONALITY_INVALID));
161
162 assert_se(streq(personality_to_string(PER_LINUX), architecture_to_string(native_architecture())));
163
164 assert_se(personality_from_string(personality_to_string(PER_LINUX)) == PER_LINUX);
165 assert_se(personality_from_string(architecture_to_string(native_architecture())) == PER_LINUX);
166
167 #ifdef __x86_64__
168 assert_se(streq_ptr(personality_to_string(PER_LINUX), "x86-64"));
169 assert_se(streq_ptr(personality_to_string(PER_LINUX32), "x86"));
170
171 assert_se(personality_from_string("x86-64") == PER_LINUX);
172 assert_se(personality_from_string("x86") == PER_LINUX32);
173 assert_se(personality_from_string("ia64") == PERSONALITY_INVALID);
174 assert_se(personality_from_string(NULL) == PERSONALITY_INVALID);
175
176 assert_se(personality_from_string(personality_to_string(PER_LINUX32)) == PER_LINUX32);
177 #endif
178 }
179
180 static void test_get_process_cmdline_harder(void) {
181 char path[] = "/tmp/test-cmdlineXXXXXX";
182 _cleanup_close_ int fd = -1;
183 _cleanup_free_ char *line = NULL;
184 pid_t pid;
185
186 if (geteuid() != 0)
187 return;
188
189 #if HAVE_VALGRIND_VALGRIND_H
190 /* valgrind patches open(/proc//cmdline)
191 * so, test_get_process_cmdline_harder fails always
192 * See https://github.com/systemd/systemd/pull/3555#issuecomment-226564908 */
193 if (RUNNING_ON_VALGRIND)
194 return;
195 #endif
196
197 pid = fork();
198 if (pid > 0) {
199 siginfo_t si;
200
201 (void) wait_for_terminate(pid, &si);
202
203 assert_se(si.si_code == CLD_EXITED);
204 assert_se(si.si_status == 0);
205
206 return;
207 }
208
209 assert_se(pid == 0);
210 assert_se(unshare(CLONE_NEWNS) >= 0);
211
212 assert_se(mount(NULL, "/", NULL, MS_PRIVATE|MS_REC, NULL) >= 0);
213
214 fd = mkostemp(path, O_CLOEXEC);
215 assert_se(fd >= 0);
216
217 if (mount(path, "/proc/self/cmdline", "bind", MS_BIND, NULL) < 0) {
218 /* This happens under selinux… Abort the test in this case. */
219 log_warning_errno(errno, "mount(..., \"/proc/self/cmdline\", \"bind\", ...) failed: %m");
220 assert(errno == EACCES);
221 return;
222 }
223
224 assert_se(unlink(path) >= 0);
225
226 assert_se(prctl(PR_SET_NAME, "testa") >= 0);
227
228 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) == -ENOENT);
229
230 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0);
231 assert_se(streq(line, "[testa]"));
232 line = mfree(line);
233
234 assert_se(get_process_cmdline(getpid_cached(), 1, true, &line) >= 0);
235 assert_se(streq(line, ""));
236 line = mfree(line);
237
238 assert_se(get_process_cmdline(getpid_cached(), 2, true, &line) >= 0);
239 assert_se(streq(line, "["));
240 line = mfree(line);
241
242 assert_se(get_process_cmdline(getpid_cached(), 3, true, &line) >= 0);
243 assert_se(streq(line, "[."));
244 line = mfree(line);
245
246 assert_se(get_process_cmdline(getpid_cached(), 4, true, &line) >= 0);
247 assert_se(streq(line, "[.."));
248 line = mfree(line);
249
250 assert_se(get_process_cmdline(getpid_cached(), 5, true, &line) >= 0);
251 assert_se(streq(line, "[..."));
252 line = mfree(line);
253
254 assert_se(get_process_cmdline(getpid_cached(), 6, true, &line) >= 0);
255 assert_se(streq(line, "[...]"));
256 line = mfree(line);
257
258 assert_se(get_process_cmdline(getpid_cached(), 7, true, &line) >= 0);
259 assert_se(streq(line, "[t...]"));
260 line = mfree(line);
261
262 assert_se(get_process_cmdline(getpid_cached(), 8, true, &line) >= 0);
263 assert_se(streq(line, "[testa]"));
264 line = mfree(line);
265
266 assert_se(write(fd, "\0\0\0\0\0\0\0\0\0", 10) == 10);
267
268 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) == -ENOENT);
269
270 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0);
271 assert_se(streq(line, "[testa]"));
272 line = mfree(line);
273
274 assert_se(write(fd, "foo\0bar\0\0\0\0\0", 10) == 10);
275
276 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) >= 0);
277 assert_se(streq(line, "foo bar"));
278 line = mfree(line);
279
280 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0);
281 assert_se(streq(line, "foo bar"));
282 line = mfree(line);
283
284 assert_se(write(fd, "quux", 4) == 4);
285 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) >= 0);
286 assert_se(streq(line, "foo bar quux"));
287 line = mfree(line);
288
289 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0);
290 assert_se(streq(line, "foo bar quux"));
291 line = mfree(line);
292
293 assert_se(get_process_cmdline(getpid_cached(), 1, true, &line) >= 0);
294 assert_se(streq(line, ""));
295 line = mfree(line);
296
297 assert_se(get_process_cmdline(getpid_cached(), 2, true, &line) >= 0);
298 assert_se(streq(line, "."));
299 line = mfree(line);
300
301 assert_se(get_process_cmdline(getpid_cached(), 3, true, &line) >= 0);
302 assert_se(streq(line, ".."));
303 line = mfree(line);
304
305 assert_se(get_process_cmdline(getpid_cached(), 4, true, &line) >= 0);
306 assert_se(streq(line, "..."));
307 line = mfree(line);
308
309 assert_se(get_process_cmdline(getpid_cached(), 5, true, &line) >= 0);
310 assert_se(streq(line, "f..."));
311 line = mfree(line);
312
313 assert_se(get_process_cmdline(getpid_cached(), 6, true, &line) >= 0);
314 assert_se(streq(line, "fo..."));
315 line = mfree(line);
316
317 assert_se(get_process_cmdline(getpid_cached(), 7, true, &line) >= 0);
318 assert_se(streq(line, "foo..."));
319 line = mfree(line);
320
321 assert_se(get_process_cmdline(getpid_cached(), 8, true, &line) >= 0);
322 assert_se(streq(line, "foo..."));
323 line = mfree(line);
324
325 assert_se(get_process_cmdline(getpid_cached(), 9, true, &line) >= 0);
326 assert_se(streq(line, "foo b..."));
327 line = mfree(line);
328
329 assert_se(get_process_cmdline(getpid_cached(), 10, true, &line) >= 0);
330 assert_se(streq(line, "foo ba..."));
331 line = mfree(line);
332
333 assert_se(get_process_cmdline(getpid_cached(), 11, true, &line) >= 0);
334 assert_se(streq(line, "foo bar..."));
335 line = mfree(line);
336
337 assert_se(get_process_cmdline(getpid_cached(), 12, true, &line) >= 0);
338 assert_se(streq(line, "foo bar..."));
339 line = mfree(line);
340
341 assert_se(get_process_cmdline(getpid_cached(), 13, true, &line) >= 0);
342 assert_se(streq(line, "foo bar quux"));
343 line = mfree(line);
344
345 assert_se(get_process_cmdline(getpid_cached(), 14, true, &line) >= 0);
346 assert_se(streq(line, "foo bar quux"));
347 line = mfree(line);
348
349 assert_se(get_process_cmdline(getpid_cached(), 1000, true, &line) >= 0);
350 assert_se(streq(line, "foo bar quux"));
351 line = mfree(line);
352
353 assert_se(ftruncate(fd, 0) >= 0);
354 assert_se(prctl(PR_SET_NAME, "aaaa bbbb cccc") >= 0);
355
356 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) == -ENOENT);
357
358 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0);
359 assert_se(streq(line, "[aaaa bbbb cccc]"));
360 line = mfree(line);
361
362 assert_se(get_process_cmdline(getpid_cached(), 10, true, &line) >= 0);
363 assert_se(streq(line, "[aaaa...]"));
364 line = mfree(line);
365
366 assert_se(get_process_cmdline(getpid_cached(), 11, true, &line) >= 0);
367 assert_se(streq(line, "[aaaa...]"));
368 line = mfree(line);
369
370 assert_se(get_process_cmdline(getpid_cached(), 12, true, &line) >= 0);
371 assert_se(streq(line, "[aaaa b...]"));
372 line = mfree(line);
373
374 safe_close(fd);
375 _exit(EXIT_SUCCESS);
376 }
377
378 static void test_rename_process_now(const char *p, int ret) {
379 _cleanup_free_ char *comm = NULL, *cmdline = NULL;
380 int r;
381
382 r = rename_process(p);
383 assert_se(r == ret ||
384 (ret == 0 && r >= 0) ||
385 (ret > 0 && r > 0));
386
387 if (r < 0)
388 return;
389
390 #if HAVE_VALGRIND_VALGRIND_H
391 /* see above, valgrind is weird, we can't verify what we are doing here */
392 if (RUNNING_ON_VALGRIND)
393 return;
394 #endif
395
396 assert_se(get_process_comm(0, &comm) >= 0);
397 log_info("comm = <%s>", comm);
398 assert_se(strneq(comm, p, TASK_COMM_LEN-1));
399
400 assert_se(get_process_cmdline(0, 0, false, &cmdline) >= 0);
401 /* we cannot expect cmdline to be renamed properly without privileges */
402 if (geteuid() == 0) {
403 log_info("cmdline = <%s>", cmdline);
404 assert_se(strneq(p, cmdline, STRLEN("test-process-util")));
405 assert_se(startswith(p, cmdline));
406 } else
407 log_info("cmdline = <%s> (not verified)", cmdline);
408 }
409
410 static void test_rename_process_one(const char *p, int ret) {
411 siginfo_t si;
412 pid_t pid;
413
414 pid = fork();
415 assert_se(pid >= 0);
416
417 if (pid == 0) {
418 /* child */
419 test_rename_process_now(p, ret);
420 _exit(EXIT_SUCCESS);
421 }
422
423 assert_se(wait_for_terminate(pid, &si) >= 0);
424 assert_se(si.si_code == CLD_EXITED);
425 assert_se(si.si_status == EXIT_SUCCESS);
426 }
427
428 static void test_rename_process_multi(void) {
429 pid_t pid;
430
431 pid = fork();
432 assert_se(pid >= 0);
433
434 if (pid > 0) {
435 siginfo_t si;
436
437 assert_se(wait_for_terminate(pid, &si) >= 0);
438 assert_se(si.si_code == CLD_EXITED);
439 assert_se(si.si_status == EXIT_SUCCESS);
440
441 return;
442 }
443
444 /* child */
445 test_rename_process_now("one", 1);
446 test_rename_process_now("more", 0); /* longer than "one", hence truncated */
447 (void) setresuid(99, 99, 99); /* change uid when running privileged */
448 test_rename_process_now("time!", 0);
449 test_rename_process_now("0", 1); /* shorter than "one", should fit */
450 test_rename_process_one("", -EINVAL);
451 test_rename_process_one(NULL, -EINVAL);
452 _exit(EXIT_SUCCESS);
453 }
454
455 static void test_rename_process(void) {
456 test_rename_process_one(NULL, -EINVAL);
457 test_rename_process_one("", -EINVAL);
458 test_rename_process_one("foo", 1); /* should always fit */
459 test_rename_process_one("this is a really really long process name, followed by some more words", 0); /* unlikely to fit */
460 test_rename_process_one("1234567", 1); /* should always fit */
461 test_rename_process_multi(); /* multiple invocations and dropped privileges */
462 }
463
464 static void test_getpid_cached(void) {
465 siginfo_t si;
466 pid_t a, b, c, d, e, f, child;
467
468 a = raw_getpid();
469 b = getpid_cached();
470 c = getpid();
471
472 assert_se(a == b && a == c);
473
474 child = fork();
475 assert_se(child >= 0);
476
477 if (child == 0) {
478 /* In child */
479 a = raw_getpid();
480 b = getpid_cached();
481 c = getpid();
482
483 assert_se(a == b && a == c);
484 _exit(EXIT_SUCCESS);
485 }
486
487 d = raw_getpid();
488 e = getpid_cached();
489 f = getpid();
490
491 assert_se(a == d && a == e && a == f);
492
493 assert_se(wait_for_terminate(child, &si) >= 0);
494 assert_se(si.si_status == 0);
495 assert_se(si.si_code == CLD_EXITED);
496 }
497
498 #define MEASURE_ITERATIONS (10000000LLU)
499
500 static void test_getpid_measure(void) {
501 unsigned long long i;
502 usec_t t, q;
503
504 t = now(CLOCK_MONOTONIC);
505 for (i = 0; i < MEASURE_ITERATIONS; i++)
506 (void) getpid();
507 q = now(CLOCK_MONOTONIC) - t;
508
509 log_info(" glibc getpid(): %llu/s\n", (unsigned long long) (MEASURE_ITERATIONS*USEC_PER_SEC/q));
510
511 t = now(CLOCK_MONOTONIC);
512 for (i = 0; i < MEASURE_ITERATIONS; i++)
513 (void) getpid_cached();
514 q = now(CLOCK_MONOTONIC) - t;
515
516 log_info("getpid_cached(): %llu/s\n", (unsigned long long) (MEASURE_ITERATIONS*USEC_PER_SEC/q));
517 }
518
519 static void test_safe_fork(void) {
520 siginfo_t status;
521 pid_t pid;
522 int r;
523
524 BLOCK_SIGNALS(SIGCHLD);
525
526 r = safe_fork("(test-child)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_NULL_STDIO|FORK_REOPEN_LOG, &pid);
527 assert_se(r >= 0);
528
529 if (r == 0) {
530 /* child */
531 usleep(100 * USEC_PER_MSEC);
532
533 _exit(88);
534 }
535
536 assert_se(wait_for_terminate(pid, &status) >= 0);
537 assert_se(status.si_code == CLD_EXITED);
538 assert_se(status.si_status == 88);
539 }
540
541 static void test_pid_to_ptr(void) {
542
543 assert_se(PTR_TO_PID(NULL) == 0);
544 assert_se(PID_TO_PTR(0) == NULL);
545
546 assert_se(PTR_TO_PID(PID_TO_PTR(1)) == 1);
547 assert_se(PTR_TO_PID(PID_TO_PTR(2)) == 2);
548 assert_se(PTR_TO_PID(PID_TO_PTR(-1)) == -1);
549 assert_se(PTR_TO_PID(PID_TO_PTR(-2)) == -2);
550
551 assert_se(PTR_TO_PID(PID_TO_PTR(INT16_MAX)) == INT16_MAX);
552 assert_se(PTR_TO_PID(PID_TO_PTR(INT16_MIN)) == INT16_MIN);
553
554 #if SIZEOF_PID_T >= 4
555 assert_se(PTR_TO_PID(PID_TO_PTR(INT32_MAX)) == INT32_MAX);
556 assert_se(PTR_TO_PID(PID_TO_PTR(INT32_MIN)) == INT32_MIN);
557 #endif
558 }
559
560 static void test_ioprio_class_from_to_string_one(const char *val, int expected) {
561 assert_se(ioprio_class_from_string(val) == expected);
562 if (expected >= 0) {
563 _cleanup_free_ char *s = NULL;
564 unsigned ret;
565
566 assert_se(ioprio_class_to_string_alloc(expected, &s) == 0);
567 /* We sometimes get a class number and sometimes a number back */
568 assert_se(streq(s, val) ||
569 safe_atou(val, &ret) == 0);
570 }
571 }
572
573 static void test_ioprio_class_from_to_string(void) {
574 test_ioprio_class_from_to_string_one("none", IOPRIO_CLASS_NONE);
575 test_ioprio_class_from_to_string_one("realtime", IOPRIO_CLASS_RT);
576 test_ioprio_class_from_to_string_one("best-effort", IOPRIO_CLASS_BE);
577 test_ioprio_class_from_to_string_one("idle", IOPRIO_CLASS_IDLE);
578 test_ioprio_class_from_to_string_one("0", 0);
579 test_ioprio_class_from_to_string_one("1", 1);
580 test_ioprio_class_from_to_string_one("7", 7);
581 test_ioprio_class_from_to_string_one("8", 8);
582 test_ioprio_class_from_to_string_one("9", -1);
583 test_ioprio_class_from_to_string_one("-1", -1);
584 }
585
586 int main(int argc, char *argv[]) {
587 log_set_max_level(LOG_DEBUG);
588 log_parse_environment();
589 log_open();
590
591 saved_argc = argc;
592 saved_argv = argv;
593
594 if (argc > 1) {
595 pid_t pid = 0;
596
597 (void) parse_pid(argv[1], &pid);
598 test_get_process_comm(pid);
599 } else {
600 TEST_REQ_RUNNING_SYSTEMD(test_get_process_comm(1));
601 test_get_process_comm(getpid());
602 }
603
604 test_get_process_comm_escape();
605 test_pid_is_unwaited();
606 test_pid_is_alive();
607 test_personality();
608 test_get_process_cmdline_harder();
609 test_rename_process();
610 test_getpid_cached();
611 test_getpid_measure();
612 test_safe_fork();
613 test_pid_to_ptr();
614 test_ioprio_class_from_to_string();
615
616 return 0;
617 }