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