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