]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-process-util.c
Merge pull request #15442 from poettering/fido2
[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 "errno-util.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 "rlimit-util.h"
26 #include "signal-util.h"
27 #include "stdio-util.h"
28 #include "string-util.h"
29 #include "terminal-util.h"
30 #include "tests.h"
31 #include "user-util.h"
32 #include "util.h"
33 #include "virt.h"
34
35 static void test_get_process_comm(pid_t pid) {
36 struct stat st;
37 _cleanup_free_ char *a = NULL, *c = NULL, *d = NULL, *f = NULL, *i = NULL;
38 _cleanup_free_ char *env = NULL;
39 char path[STRLEN("/proc//comm") + DECIMAL_STR_MAX(pid_t)];
40 pid_t e;
41 uid_t u;
42 gid_t g;
43 dev_t h;
44 int r;
45
46 xsprintf(path, "/proc/"PID_FMT"/comm", pid);
47
48 if (stat(path, &st) == 0) {
49 assert_se(get_process_comm(pid, &a) >= 0);
50 log_info("PID"PID_FMT" comm: '%s'", pid, a);
51 } else
52 log_warning("%s not exist.", path);
53
54 assert_se(get_process_cmdline(pid, 0, PROCESS_CMDLINE_COMM_FALLBACK, &c) >= 0);
55 log_info("PID"PID_FMT" cmdline: '%s'", pid, c);
56
57 assert_se(get_process_cmdline(pid, 8, 0, &d) >= 0);
58 log_info("PID"PID_FMT" cmdline truncated to 8: '%s'", pid, d);
59
60 free(d);
61 assert_se(get_process_cmdline(pid, 1, 0, &d) >= 0);
62 log_info("PID"PID_FMT" cmdline truncated to 1: '%s'", pid, d);
63
64 assert_se(get_process_ppid(pid, &e) >= 0);
65 log_info("PID"PID_FMT" PPID: "PID_FMT, pid, e);
66 assert_se(pid == 1 ? e == 0 : e > 0);
67
68 assert_se(is_kernel_thread(pid) == 0 || pid != 1);
69
70 r = get_process_exe(pid, &f);
71 assert_se(r >= 0 || r == -EACCES);
72 log_info("PID"PID_FMT" exe: '%s'", pid, strna(f));
73
74 assert_se(get_process_uid(pid, &u) == 0);
75 log_info("PID"PID_FMT" UID: "UID_FMT, pid, u);
76
77 assert_se(get_process_gid(pid, &g) == 0);
78 log_info("PID"PID_FMT" GID: "GID_FMT, pid, g);
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\\266\\303\\274\\303\\237");
114 test_get_process_comm_escape_one("xäöüß", "x\\303\\244\\303\\266\\303\\274\\303\\237");
115 test_get_process_comm_escape_one("xxäöüß", "xx\\303\\244\\303\\266\\303\\274\\303\\237");
116 test_get_process_comm_escape_one("xxxäöüß", "xxx\\303\\244\\303\\266\\303\\274\\303\\237");
117 test_get_process_comm_escape_one("xxxxäöüß", "xxxx\\303\\244\\303\\266\\303\\274\\303\\237");
118 test_get_process_comm_escape_one("xxxxxäöüß", "xxxxx\\303\\244\\303\\266\\303\\274\\303\\237");
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 log_info("Skipping %s: not root", __func__);
188 return;
189 }
190
191 if (!have_namespaces()) {
192 log_notice("Testing without namespaces, skipping %s", __func__);
193 return;
194 }
195
196 #if HAVE_VALGRIND_VALGRIND_H
197 /* valgrind patches open(/proc//cmdline)
198 * so, test_get_process_cmdline_harder fails always
199 * See https://github.com/systemd/systemd/pull/3555#issuecomment-226564908 */
200 if (RUNNING_ON_VALGRIND) {
201 log_info("Skipping %s: running on valgrind", __func__);
202 return;
203 }
204 #endif
205
206 pid = fork();
207 if (pid > 0) {
208 siginfo_t si;
209
210 (void) wait_for_terminate(pid, &si);
211
212 assert_se(si.si_code == CLD_EXITED);
213 assert_se(si.si_status == 0);
214
215 return;
216 }
217
218 assert_se(pid == 0);
219 assert_se(unshare(CLONE_NEWNS) >= 0);
220
221 if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL) < 0) {
222 log_warning_errno(errno, "mount(..., \"/\", MS_SLAVE|MS_REC, ...) failed: %m");
223 assert_se(IN_SET(errno, EPERM, EACCES));
224 return;
225 }
226
227 fd = mkostemp(path, O_CLOEXEC);
228 assert_se(fd >= 0);
229
230 /* Note that we don't unmount the following bind-mount at the end of the test because the kernel
231 * will clear up its /proc/PID/ hierarchy automatically as soon as the test stops. */
232 if (mount(path, "/proc/self/cmdline", "bind", MS_BIND, NULL) < 0) {
233 /* This happens under selinux… Abort the test in this case. */
234 log_warning_errno(errno, "mount(..., \"/proc/self/cmdline\", \"bind\", ...) failed: %m");
235 assert_se(IN_SET(errno, EPERM, EACCES));
236 return;
237 }
238
239 assert_se(unlink(path) >= 0);
240
241 assert_se(prctl(PR_SET_NAME, "testa") >= 0);
242
243 assert_se(get_process_cmdline(getpid_cached(), SIZE_MAX, 0, &line) == -ENOENT);
244
245 assert_se(get_process_cmdline(getpid_cached(), SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
246 assert_se(streq(line, "[testa]"));
247 line = mfree(line);
248
249 assert_se(get_process_cmdline(getpid_cached(), 0, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
250 log_info("'%s'", line);
251 assert_se(streq(line, ""));
252 line = mfree(line);
253
254 assert_se(get_process_cmdline(getpid_cached(), 1, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
255 assert_se(streq(line, "…"));
256 line = mfree(line);
257
258 assert_se(get_process_cmdline(getpid_cached(), 2, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
259 assert_se(streq(line, "[…"));
260 line = mfree(line);
261
262 assert_se(get_process_cmdline(getpid_cached(), 3, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
263 assert_se(streq(line, "[t…"));
264 line = mfree(line);
265
266 assert_se(get_process_cmdline(getpid_cached(), 4, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
267 assert_se(streq(line, "[te…"));
268 line = mfree(line);
269
270 assert_se(get_process_cmdline(getpid_cached(), 5, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
271 assert_se(streq(line, "[tes…"));
272 line = mfree(line);
273
274 assert_se(get_process_cmdline(getpid_cached(), 6, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
275 assert_se(streq(line, "[test…"));
276 line = mfree(line);
277
278 assert_se(get_process_cmdline(getpid_cached(), 7, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
279 assert_se(streq(line, "[testa]"));
280 line = mfree(line);
281
282 assert_se(get_process_cmdline(getpid_cached(), 8, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
283 assert_se(streq(line, "[testa]"));
284 line = mfree(line);
285
286 assert_se(write(fd, "foo\0bar", 8) == 8);
287
288 assert_se(get_process_cmdline(getpid_cached(), SIZE_MAX, 0, &line) >= 0);
289 log_info("'%s'", line);
290 assert_se(streq(line, "foo bar"));
291 line = mfree(line);
292
293 assert_se(get_process_cmdline(getpid_cached(), SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
294 assert_se(streq(line, "foo bar"));
295 line = mfree(line);
296
297 assert_se(write(fd, "quux", 4) == 4);
298 assert_se(get_process_cmdline(getpid_cached(), SIZE_MAX, 0, &line) >= 0);
299 log_info("'%s'", line);
300 assert_se(streq(line, "foo bar quux"));
301 line = mfree(line);
302
303 assert_se(get_process_cmdline(getpid_cached(), SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
304 assert_se(streq(line, "foo bar quux"));
305 line = mfree(line);
306
307 assert_se(get_process_cmdline(getpid_cached(), 1, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
308 assert_se(streq(line, "…"));
309 line = mfree(line);
310
311 assert_se(get_process_cmdline(getpid_cached(), 2, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
312 assert_se(streq(line, "f…"));
313 line = mfree(line);
314
315 assert_se(get_process_cmdline(getpid_cached(), 3, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
316 assert_se(streq(line, "fo…"));
317 line = mfree(line);
318
319 assert_se(get_process_cmdline(getpid_cached(), 4, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
320 assert_se(streq(line, "foo…"));
321 line = mfree(line);
322
323 assert_se(get_process_cmdline(getpid_cached(), 5, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
324 assert_se(streq(line, "foo …"));
325 line = mfree(line);
326
327 assert_se(get_process_cmdline(getpid_cached(), 6, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
328 assert_se(streq(line, "foo b…"));
329 line = mfree(line);
330
331 assert_se(get_process_cmdline(getpid_cached(), 7, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
332 assert_se(streq(line, "foo ba…"));
333 line = mfree(line);
334
335 assert_se(get_process_cmdline(getpid_cached(), 8, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
336 assert_se(streq(line, "foo bar…"));
337 line = mfree(line);
338
339 assert_se(get_process_cmdline(getpid_cached(), 9, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
340 assert_se(streq(line, "foo bar …"));
341 line = mfree(line);
342
343 assert_se(get_process_cmdline(getpid_cached(), 10, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
344 assert_se(streq(line, "foo bar q…"));
345 line = mfree(line);
346
347 assert_se(get_process_cmdline(getpid_cached(), 11, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
348 assert_se(streq(line, "foo bar qu…"));
349 line = mfree(line);
350
351 assert_se(get_process_cmdline(getpid_cached(), 12, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
352 assert_se(streq(line, "foo bar quux"));
353 line = mfree(line);
354
355 assert_se(get_process_cmdline(getpid_cached(), 13, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
356 assert_se(streq(line, "foo bar quux"));
357 line = mfree(line);
358
359 assert_se(get_process_cmdline(getpid_cached(), 14, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
360 assert_se(streq(line, "foo bar quux"));
361 line = mfree(line);
362
363 assert_se(get_process_cmdline(getpid_cached(), 1000, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
364 assert_se(streq(line, "foo bar quux"));
365 line = mfree(line);
366
367 assert_se(ftruncate(fd, 0) >= 0);
368 assert_se(prctl(PR_SET_NAME, "aaaa bbbb cccc") >= 0);
369
370 assert_se(get_process_cmdline(getpid_cached(), SIZE_MAX, 0, &line) == -ENOENT);
371
372 assert_se(get_process_cmdline(getpid_cached(), SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
373 assert_se(streq(line, "[aaaa bbbb cccc]"));
374 line = mfree(line);
375
376 assert_se(get_process_cmdline(getpid_cached(), 10, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
377 assert_se(streq(line, "[aaaa bbb…"));
378 line = mfree(line);
379
380 assert_se(get_process_cmdline(getpid_cached(), 11, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
381 assert_se(streq(line, "[aaaa bbbb…"));
382 line = mfree(line);
383
384 assert_se(get_process_cmdline(getpid_cached(), 12, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
385 assert_se(streq(line, "[aaaa bbbb …"));
386 line = mfree(line);
387
388 safe_close(fd);
389 _exit(EXIT_SUCCESS);
390 }
391
392 static void test_rename_process_now(const char *p, int ret) {
393 _cleanup_free_ char *comm = NULL, *cmdline = NULL;
394 int r;
395
396 r = rename_process(p);
397 assert_se(r == ret ||
398 (ret == 0 && r >= 0) ||
399 (ret > 0 && r > 0));
400
401 if (r < 0)
402 return;
403
404 #if HAVE_VALGRIND_VALGRIND_H
405 /* see above, valgrind is weird, we can't verify what we are doing here */
406 if (RUNNING_ON_VALGRIND)
407 return;
408 #endif
409
410 assert_se(get_process_comm(0, &comm) >= 0);
411 log_info("comm = <%s>", comm);
412 assert_se(strneq(comm, p, TASK_COMM_LEN-1));
413 /* We expect comm to be at most 16 bytes (TASK_COMM_LEN). The kernel may raise this limit in the
414 * future. We'd only check the initial part, at least until we recompile, but this will still pass. */
415
416 r = get_process_cmdline(0, SIZE_MAX, 0, &cmdline);
417 assert_se(r >= 0);
418 /* we cannot expect cmdline to be renamed properly without privileges */
419 if (geteuid() == 0) {
420 if (r == 0 && detect_container() > 0)
421 log_info("cmdline = <%s> (not verified, Running in unprivileged container?)", cmdline);
422 else {
423 log_info("cmdline = <%s>", cmdline);
424 assert_se(strneq(p, cmdline, STRLEN("test-process-util")));
425 assert_se(startswith(p, cmdline));
426 }
427 } else
428 log_info("cmdline = <%s> (not verified)", cmdline);
429 }
430
431 static void test_rename_process_one(const char *p, int ret) {
432 siginfo_t si;
433 pid_t pid;
434
435 pid = fork();
436 assert_se(pid >= 0);
437
438 if (pid == 0) {
439 /* child */
440 test_rename_process_now(p, ret);
441 _exit(EXIT_SUCCESS);
442 }
443
444 assert_se(wait_for_terminate(pid, &si) >= 0);
445 assert_se(si.si_code == CLD_EXITED);
446 assert_se(si.si_status == EXIT_SUCCESS);
447 }
448
449 static void test_rename_process_multi(void) {
450 pid_t pid;
451
452 pid = fork();
453 assert_se(pid >= 0);
454
455 if (pid > 0) {
456 siginfo_t si;
457
458 assert_se(wait_for_terminate(pid, &si) >= 0);
459 assert_se(si.si_code == CLD_EXITED);
460 assert_se(si.si_status == EXIT_SUCCESS);
461
462 return;
463 }
464
465 /* child */
466 test_rename_process_now("one", 1);
467 test_rename_process_now("more", 0); /* longer than "one", hence truncated */
468 (void) setresuid(99, 99, 99); /* change uid when running privileged */
469 test_rename_process_now("time!", 0);
470 test_rename_process_now("0", 1); /* shorter than "one", should fit */
471 test_rename_process_one("", -EINVAL);
472 test_rename_process_one(NULL, -EINVAL);
473 _exit(EXIT_SUCCESS);
474 }
475
476 static void test_rename_process(void) {
477 test_rename_process_one(NULL, -EINVAL);
478 test_rename_process_one("", -EINVAL);
479 test_rename_process_one("foo", 1); /* should always fit */
480 test_rename_process_one("this is a really really long process name, followed by some more words", 0); /* unlikely to fit */
481 test_rename_process_one("1234567", 1); /* should always fit */
482 test_rename_process_multi(); /* multiple invocations and dropped privileges */
483 }
484
485 static void test_getpid_cached(void) {
486 siginfo_t si;
487 pid_t a, b, c, d, e, f, child;
488
489 a = raw_getpid();
490 b = getpid_cached();
491 c = getpid();
492
493 assert_se(a == b && a == c);
494
495 child = fork();
496 assert_se(child >= 0);
497
498 if (child == 0) {
499 /* In child */
500 a = raw_getpid();
501 b = getpid_cached();
502 c = getpid();
503
504 assert_se(a == b && a == c);
505 _exit(EXIT_SUCCESS);
506 }
507
508 d = raw_getpid();
509 e = getpid_cached();
510 f = getpid();
511
512 assert_se(a == d && a == e && a == f);
513
514 assert_se(wait_for_terminate(child, &si) >= 0);
515 assert_se(si.si_status == 0);
516 assert_se(si.si_code == CLD_EXITED);
517 }
518
519 #define MEASURE_ITERATIONS (10000000LLU)
520
521 static void test_getpid_measure(void) {
522 unsigned long long i;
523 usec_t t, q;
524
525 t = now(CLOCK_MONOTONIC);
526 for (i = 0; i < MEASURE_ITERATIONS; i++)
527 (void) getpid();
528 q = now(CLOCK_MONOTONIC) - t;
529
530 log_info(" glibc getpid(): %lf µs each\n", (double) q / MEASURE_ITERATIONS);
531
532 t = now(CLOCK_MONOTONIC);
533 for (i = 0; i < MEASURE_ITERATIONS; i++)
534 (void) getpid_cached();
535 q = now(CLOCK_MONOTONIC) - t;
536
537 log_info("getpid_cached(): %lf µs each\n", (double) q / MEASURE_ITERATIONS);
538 }
539
540 static void test_safe_fork(void) {
541 siginfo_t status;
542 pid_t pid;
543 int r;
544
545 BLOCK_SIGNALS(SIGCHLD);
546
547 r = safe_fork("(test-child)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_NULL_STDIO|FORK_REOPEN_LOG, &pid);
548 assert_se(r >= 0);
549
550 if (r == 0) {
551 /* child */
552 usleep(100 * USEC_PER_MSEC);
553
554 _exit(88);
555 }
556
557 assert_se(wait_for_terminate(pid, &status) >= 0);
558 assert_se(status.si_code == CLD_EXITED);
559 assert_se(status.si_status == 88);
560 }
561
562 static void test_pid_to_ptr(void) {
563
564 assert_se(PTR_TO_PID(NULL) == 0);
565 assert_se(PID_TO_PTR(0) == NULL);
566
567 assert_se(PTR_TO_PID(PID_TO_PTR(1)) == 1);
568 assert_se(PTR_TO_PID(PID_TO_PTR(2)) == 2);
569 assert_se(PTR_TO_PID(PID_TO_PTR(-1)) == -1);
570 assert_se(PTR_TO_PID(PID_TO_PTR(-2)) == -2);
571
572 assert_se(PTR_TO_PID(PID_TO_PTR(INT16_MAX)) == INT16_MAX);
573 assert_se(PTR_TO_PID(PID_TO_PTR(INT16_MIN)) == INT16_MIN);
574
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 }
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 static void test_setpriority_closest(void) {
606 int r;
607
608 r = safe_fork("(test-setprio)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_WAIT|FORK_LOG, NULL);
609 assert_se(r >= 0);
610
611 if (r == 0) {
612 bool full_test;
613 int p, q;
614 /* child */
615
616 /* rlimit of 30 equals nice level of -10 */
617 if (setrlimit(RLIMIT_NICE, &RLIMIT_MAKE_CONST(30)) < 0) {
618 /* If this fails we are probably unprivileged or in a userns of some kind, let's skip
619 * the full test */
620 assert_se(ERRNO_IS_PRIVILEGE(errno));
621 full_test = false;
622 } else {
623 assert_se(setresgid(GID_NOBODY, GID_NOBODY, GID_NOBODY) >= 0);
624 assert_se(setresuid(UID_NOBODY, UID_NOBODY, UID_NOBODY) >= 0);
625 full_test = true;
626 }
627
628 errno = 0;
629 p = getpriority(PRIO_PROCESS, 0);
630 assert_se(errno == 0);
631
632 /* It should always be possible to set our nice level to the current one */
633 assert_se(setpriority_closest(p) > 0);
634
635 errno = 0;
636 q = getpriority(PRIO_PROCESS, 0);
637 assert_se(errno == 0 && p == q);
638
639 /* It should also be possible to to set the nice level to one higher */
640 if (p < PRIO_MAX-1) {
641 assert_se(setpriority_closest(++p) > 0);
642
643 errno = 0;
644 q = getpriority(PRIO_PROCESS, 0);
645 assert_se(errno == 0 && p == q);
646 }
647
648 /* It should also be possible to to set the nice level to two higher */
649 if (p < PRIO_MAX-1) {
650 assert_se(setpriority_closest(++p) > 0);
651
652 errno = 0;
653 q = getpriority(PRIO_PROCESS, 0);
654 assert_se(errno == 0 && p == q);
655 }
656
657 if (full_test) {
658 /* These two should work, given the RLIMIT_NICE we set above */
659 assert_se(setpriority_closest(-10) > 0);
660 errno = 0;
661 q = getpriority(PRIO_PROCESS, 0);
662 assert_se(errno == 0 && q == -10);
663
664 assert_se(setpriority_closest(-9) > 0);
665 errno = 0;
666 q = getpriority(PRIO_PROCESS, 0);
667 assert_se(errno == 0 && q == -9);
668
669 /* This should succeed but should be clamped to the limit */
670 assert_se(setpriority_closest(-11) == 0);
671 errno = 0;
672 q = getpriority(PRIO_PROCESS, 0);
673 assert_se(errno == 0 && q == -10);
674
675 assert_se(setpriority_closest(-8) > 0);
676 errno = 0;
677 q = getpriority(PRIO_PROCESS, 0);
678 assert_se(errno == 0 && q == -8);
679
680 /* This should succeed but should be clamped to the limit */
681 assert_se(setpriority_closest(-12) == 0);
682 errno = 0;
683 q = getpriority(PRIO_PROCESS, 0);
684 assert_se(errno == 0 && q == -10);
685 }
686
687 _exit(EXIT_SUCCESS);
688 }
689 }
690
691 int main(int argc, char *argv[]) {
692 test_setup_logging(LOG_DEBUG);
693
694 save_argc_argv(argc, argv);
695
696 if (argc > 1) {
697 pid_t pid = 0;
698
699 (void) parse_pid(argv[1], &pid);
700 test_get_process_comm(pid);
701 } else {
702 TEST_REQ_RUNNING_SYSTEMD(test_get_process_comm(1));
703 test_get_process_comm(getpid());
704 }
705
706 test_get_process_comm_escape();
707 test_pid_is_unwaited();
708 test_pid_is_alive();
709 test_personality();
710 test_get_process_cmdline_harder();
711 test_rename_process();
712 test_getpid_cached();
713 test_getpid_measure();
714 test_safe_fork();
715 test_pid_to_ptr();
716 test_ioprio_class_from_to_string();
717 test_setpriority_closest();
718
719 return 0;
720 }