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