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