]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-process-util.c
test-process-util: getpid_cached() → 0
[thirdparty/systemd.git] / src / test / test-process-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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 /* Set RLIMIT_STACK to infinity to test we don't try to allocate unncessarily large values to read
240 * the cmdline. */
241 if (setrlimit(RLIMIT_STACK, &RLIMIT_MAKE_CONST(RLIM_INFINITY)) < 0)
242 log_warning("Testing without RLIMIT_STACK=infinity");
243
244 assert_se(unlink(path) >= 0);
245
246 assert_se(prctl(PR_SET_NAME, "testa") >= 0);
247
248 assert_se(get_process_cmdline(0, SIZE_MAX, 0, &line) == -ENOENT);
249
250 assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
251 assert_se(streq(line, "[testa]"));
252 line = mfree(line);
253
254 assert_se(get_process_cmdline(0, 0, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
255 log_info("'%s'", line);
256 assert_se(streq(line, ""));
257 line = mfree(line);
258
259 assert_se(get_process_cmdline(0, 1, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
260 assert_se(streq(line, "…"));
261 line = mfree(line);
262
263 assert_se(get_process_cmdline(0, 2, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
264 assert_se(streq(line, "[…"));
265 line = mfree(line);
266
267 assert_se(get_process_cmdline(0, 3, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
268 assert_se(streq(line, "[t…"));
269 line = mfree(line);
270
271 assert_se(get_process_cmdline(0, 4, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
272 assert_se(streq(line, "[te…"));
273 line = mfree(line);
274
275 assert_se(get_process_cmdline(0, 5, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
276 assert_se(streq(line, "[tes…"));
277 line = mfree(line);
278
279 assert_se(get_process_cmdline(0, 6, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
280 assert_se(streq(line, "[test…"));
281 line = mfree(line);
282
283 assert_se(get_process_cmdline(0, 7, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
284 assert_se(streq(line, "[testa]"));
285 line = mfree(line);
286
287 assert_se(get_process_cmdline(0, 8, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
288 assert_se(streq(line, "[testa]"));
289 line = mfree(line);
290
291 assert_se(write(fd, "foo\0bar", 8) == 8);
292
293 assert_se(get_process_cmdline(0, SIZE_MAX, 0, &line) >= 0);
294 log_info("'%s'", line);
295 assert_se(streq(line, "foo bar"));
296 line = mfree(line);
297
298 assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
299 assert_se(streq(line, "foo bar"));
300 line = mfree(line);
301
302 assert_se(write(fd, "quux", 4) == 4);
303 assert_se(get_process_cmdline(0, SIZE_MAX, 0, &line) >= 0);
304 log_info("'%s'", line);
305 assert_se(streq(line, "foo bar quux"));
306 line = mfree(line);
307
308 assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
309 assert_se(streq(line, "foo bar quux"));
310 line = mfree(line);
311
312 assert_se(get_process_cmdline(0, 1, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
313 assert_se(streq(line, "…"));
314 line = mfree(line);
315
316 assert_se(get_process_cmdline(0, 2, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
317 assert_se(streq(line, "f…"));
318 line = mfree(line);
319
320 assert_se(get_process_cmdline(0, 3, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
321 assert_se(streq(line, "fo…"));
322 line = mfree(line);
323
324 assert_se(get_process_cmdline(0, 4, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
325 assert_se(streq(line, "foo…"));
326 line = mfree(line);
327
328 assert_se(get_process_cmdline(0, 5, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
329 assert_se(streq(line, "foo …"));
330 line = mfree(line);
331
332 assert_se(get_process_cmdline(0, 6, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
333 assert_se(streq(line, "foo b…"));
334 line = mfree(line);
335
336 assert_se(get_process_cmdline(0, 7, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
337 assert_se(streq(line, "foo ba…"));
338 line = mfree(line);
339
340 assert_se(get_process_cmdline(0, 8, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
341 assert_se(streq(line, "foo bar…"));
342 line = mfree(line);
343
344 assert_se(get_process_cmdline(0, 9, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
345 assert_se(streq(line, "foo bar …"));
346 line = mfree(line);
347
348 assert_se(get_process_cmdline(0, 10, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
349 assert_se(streq(line, "foo bar q…"));
350 line = mfree(line);
351
352 assert_se(get_process_cmdline(0, 11, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
353 assert_se(streq(line, "foo bar qu…"));
354 line = mfree(line);
355
356 assert_se(get_process_cmdline(0, 12, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
357 assert_se(streq(line, "foo bar quux"));
358 line = mfree(line);
359
360 assert_se(get_process_cmdline(0, 13, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
361 assert_se(streq(line, "foo bar quux"));
362 line = mfree(line);
363
364 assert_se(get_process_cmdline(0, 14, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
365 assert_se(streq(line, "foo bar quux"));
366 line = mfree(line);
367
368 assert_se(get_process_cmdline(0, 1000, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
369 assert_se(streq(line, "foo bar quux"));
370 line = mfree(line);
371
372 assert_se(ftruncate(fd, 0) >= 0);
373 assert_se(prctl(PR_SET_NAME, "aaaa bbbb cccc") >= 0);
374
375 assert_se(get_process_cmdline(0, SIZE_MAX, 0, &line) == -ENOENT);
376
377 assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
378 assert_se(streq(line, "[aaaa bbbb cccc]"));
379 line = mfree(line);
380
381 assert_se(get_process_cmdline(0, 10, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
382 assert_se(streq(line, "[aaaa bbb…"));
383 line = mfree(line);
384
385 assert_se(get_process_cmdline(0, 11, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
386 assert_se(streq(line, "[aaaa bbbb…"));
387 line = mfree(line);
388
389 assert_se(get_process_cmdline(0, 12, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
390 assert_se(streq(line, "[aaaa bbbb …"));
391 line = mfree(line);
392
393 safe_close(fd);
394 _exit(EXIT_SUCCESS);
395 }
396
397 static void test_rename_process_now(const char *p, int ret) {
398 _cleanup_free_ char *comm = NULL, *cmdline = NULL;
399 int r;
400
401 r = rename_process(p);
402 assert_se(r == ret ||
403 (ret == 0 && r >= 0) ||
404 (ret > 0 && r > 0));
405
406 if (r < 0)
407 return;
408
409 #if HAVE_VALGRIND_VALGRIND_H
410 /* see above, valgrind is weird, we can't verify what we are doing here */
411 if (RUNNING_ON_VALGRIND)
412 return;
413 #endif
414
415 assert_se(get_process_comm(0, &comm) >= 0);
416 log_info("comm = <%s>", comm);
417 assert_se(strneq(comm, p, TASK_COMM_LEN-1));
418 /* We expect comm to be at most 16 bytes (TASK_COMM_LEN). The kernel may raise this limit in the
419 * future. We'd only check the initial part, at least until we recompile, but this will still pass. */
420
421 r = get_process_cmdline(0, SIZE_MAX, 0, &cmdline);
422 assert_se(r >= 0);
423 /* we cannot expect cmdline to be renamed properly without privileges */
424 if (geteuid() == 0) {
425 if (r == 0 && detect_container() > 0)
426 log_info("cmdline = <%s> (not verified, Running in unprivileged container?)", cmdline);
427 else {
428 log_info("cmdline = <%s>", cmdline);
429 assert_se(strneq(p, cmdline, STRLEN("test-process-util")));
430 assert_se(startswith(p, cmdline));
431 }
432 } else
433 log_info("cmdline = <%s> (not verified)", cmdline);
434 }
435
436 static void test_rename_process_one(const char *p, int ret) {
437 siginfo_t si;
438 pid_t pid;
439
440 pid = fork();
441 assert_se(pid >= 0);
442
443 if (pid == 0) {
444 /* child */
445 test_rename_process_now(p, ret);
446 _exit(EXIT_SUCCESS);
447 }
448
449 assert_se(wait_for_terminate(pid, &si) >= 0);
450 assert_se(si.si_code == CLD_EXITED);
451 assert_se(si.si_status == EXIT_SUCCESS);
452 }
453
454 static void test_rename_process_multi(void) {
455 pid_t pid;
456
457 pid = fork();
458 assert_se(pid >= 0);
459
460 if (pid > 0) {
461 siginfo_t si;
462
463 assert_se(wait_for_terminate(pid, &si) >= 0);
464 assert_se(si.si_code == CLD_EXITED);
465 assert_se(si.si_status == EXIT_SUCCESS);
466
467 return;
468 }
469
470 /* child */
471 test_rename_process_now("one", 1);
472 test_rename_process_now("more", 0); /* longer than "one", hence truncated */
473 (void) setresuid(99, 99, 99); /* change uid when running privileged */
474 test_rename_process_now("time!", 0);
475 test_rename_process_now("0", 1); /* shorter than "one", should fit */
476 test_rename_process_one("", -EINVAL);
477 test_rename_process_one(NULL, -EINVAL);
478 _exit(EXIT_SUCCESS);
479 }
480
481 static void test_rename_process(void) {
482 test_rename_process_one(NULL, -EINVAL);
483 test_rename_process_one("", -EINVAL);
484 test_rename_process_one("foo", 1); /* should always fit */
485 test_rename_process_one("this is a really really long process name, followed by some more words", 0); /* unlikely to fit */
486 test_rename_process_one("1234567", 1); /* should always fit */
487 test_rename_process_multi(); /* multiple invocations and dropped privileges */
488 }
489
490 static void test_getpid_cached(void) {
491 siginfo_t si;
492 pid_t a, b, c, d, e, f, child;
493
494 a = raw_getpid();
495 b = getpid_cached();
496 c = getpid();
497
498 assert_se(a == b && a == c);
499
500 child = fork();
501 assert_se(child >= 0);
502
503 if (child == 0) {
504 /* In child */
505 a = raw_getpid();
506 b = getpid_cached();
507 c = getpid();
508
509 assert_se(a == b && a == c);
510 _exit(EXIT_SUCCESS);
511 }
512
513 d = raw_getpid();
514 e = getpid_cached();
515 f = getpid();
516
517 assert_se(a == d && a == e && a == f);
518
519 assert_se(wait_for_terminate(child, &si) >= 0);
520 assert_se(si.si_status == 0);
521 assert_se(si.si_code == CLD_EXITED);
522 }
523
524 #define MEASURE_ITERATIONS (10000000LLU)
525
526 static void test_getpid_measure(void) {
527 unsigned long long i;
528 usec_t t, q;
529
530 t = now(CLOCK_MONOTONIC);
531 for (i = 0; i < MEASURE_ITERATIONS; i++)
532 (void) getpid();
533 q = now(CLOCK_MONOTONIC) - t;
534
535 log_info(" glibc getpid(): %lf µs each\n", (double) q / MEASURE_ITERATIONS);
536
537 t = now(CLOCK_MONOTONIC);
538 for (i = 0; i < MEASURE_ITERATIONS; i++)
539 (void) getpid_cached();
540 q = now(CLOCK_MONOTONIC) - t;
541
542 log_info("getpid_cached(): %lf µs each\n", (double) q / MEASURE_ITERATIONS);
543 }
544
545 static void test_safe_fork(void) {
546 siginfo_t status;
547 pid_t pid;
548 int r;
549
550 BLOCK_SIGNALS(SIGCHLD);
551
552 r = safe_fork("(test-child)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_NULL_STDIO|FORK_REOPEN_LOG, &pid);
553 assert_se(r >= 0);
554
555 if (r == 0) {
556 /* child */
557 usleep(100 * USEC_PER_MSEC);
558
559 _exit(88);
560 }
561
562 assert_se(wait_for_terminate(pid, &status) >= 0);
563 assert_se(status.si_code == CLD_EXITED);
564 assert_se(status.si_status == 88);
565 }
566
567 static void test_pid_to_ptr(void) {
568
569 assert_se(PTR_TO_PID(NULL) == 0);
570 assert_se(PID_TO_PTR(0) == NULL);
571
572 assert_se(PTR_TO_PID(PID_TO_PTR(1)) == 1);
573 assert_se(PTR_TO_PID(PID_TO_PTR(2)) == 2);
574 assert_se(PTR_TO_PID(PID_TO_PTR(-1)) == -1);
575 assert_se(PTR_TO_PID(PID_TO_PTR(-2)) == -2);
576
577 assert_se(PTR_TO_PID(PID_TO_PTR(INT16_MAX)) == INT16_MAX);
578 assert_se(PTR_TO_PID(PID_TO_PTR(INT16_MIN)) == INT16_MIN);
579
580 assert_se(PTR_TO_PID(PID_TO_PTR(INT32_MAX)) == INT32_MAX);
581 assert_se(PTR_TO_PID(PID_TO_PTR(INT32_MIN)) == INT32_MIN);
582 }
583
584 static void test_ioprio_class_from_to_string_one(const char *val, int expected) {
585 assert_se(ioprio_class_from_string(val) == expected);
586 if (expected >= 0) {
587 _cleanup_free_ char *s = NULL;
588 unsigned ret;
589
590 assert_se(ioprio_class_to_string_alloc(expected, &s) == 0);
591 /* We sometimes get a class number and sometimes a number back */
592 assert_se(streq(s, val) ||
593 safe_atou(val, &ret) == 0);
594 }
595 }
596
597 static void test_ioprio_class_from_to_string(void) {
598 test_ioprio_class_from_to_string_one("none", IOPRIO_CLASS_NONE);
599 test_ioprio_class_from_to_string_one("realtime", IOPRIO_CLASS_RT);
600 test_ioprio_class_from_to_string_one("best-effort", IOPRIO_CLASS_BE);
601 test_ioprio_class_from_to_string_one("idle", IOPRIO_CLASS_IDLE);
602 test_ioprio_class_from_to_string_one("0", 0);
603 test_ioprio_class_from_to_string_one("1", 1);
604 test_ioprio_class_from_to_string_one("7", 7);
605 test_ioprio_class_from_to_string_one("8", 8);
606 test_ioprio_class_from_to_string_one("9", -EINVAL);
607 test_ioprio_class_from_to_string_one("-1", -EINVAL);
608 }
609
610 static void test_setpriority_closest(void) {
611 int r;
612
613 r = safe_fork("(test-setprio)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_WAIT|FORK_LOG, NULL);
614 assert_se(r >= 0);
615
616 if (r == 0) {
617 bool full_test;
618 int p, q;
619 /* child */
620
621 /* rlimit of 30 equals nice level of -10 */
622 if (setrlimit(RLIMIT_NICE, &RLIMIT_MAKE_CONST(30)) < 0) {
623 /* If this fails we are probably unprivileged or in a userns of some kind, let's skip
624 * the full test */
625 assert_se(ERRNO_IS_PRIVILEGE(errno));
626 full_test = false;
627 } else {
628 assert_se(setresgid(GID_NOBODY, GID_NOBODY, GID_NOBODY) >= 0);
629 assert_se(setresuid(UID_NOBODY, UID_NOBODY, UID_NOBODY) >= 0);
630 full_test = true;
631 }
632
633 errno = 0;
634 p = getpriority(PRIO_PROCESS, 0);
635 assert_se(errno == 0);
636
637 /* It should always be possible to set our nice level to the current one */
638 assert_se(setpriority_closest(p) > 0);
639
640 errno = 0;
641 q = getpriority(PRIO_PROCESS, 0);
642 assert_se(errno == 0 && p == q);
643
644 /* It should also be possible to set the nice level to one higher */
645 if (p < PRIO_MAX-1) {
646 assert_se(setpriority_closest(++p) > 0);
647
648 errno = 0;
649 q = getpriority(PRIO_PROCESS, 0);
650 assert_se(errno == 0 && p == q);
651 }
652
653 /* It should also be possible to set the nice level to two higher */
654 if (p < PRIO_MAX-1) {
655 assert_se(setpriority_closest(++p) > 0);
656
657 errno = 0;
658 q = getpriority(PRIO_PROCESS, 0);
659 assert_se(errno == 0 && p == q);
660 }
661
662 if (full_test) {
663 /* These two should work, given the RLIMIT_NICE we set above */
664 assert_se(setpriority_closest(-10) > 0);
665 errno = 0;
666 q = getpriority(PRIO_PROCESS, 0);
667 assert_se(errno == 0 && q == -10);
668
669 assert_se(setpriority_closest(-9) > 0);
670 errno = 0;
671 q = getpriority(PRIO_PROCESS, 0);
672 assert_se(errno == 0 && q == -9);
673
674 /* This should succeed but should be clamped to the limit */
675 assert_se(setpriority_closest(-11) == 0);
676 errno = 0;
677 q = getpriority(PRIO_PROCESS, 0);
678 assert_se(errno == 0 && q == -10);
679
680 assert_se(setpriority_closest(-8) > 0);
681 errno = 0;
682 q = getpriority(PRIO_PROCESS, 0);
683 assert_se(errno == 0 && q == -8);
684
685 /* This should succeed but should be clamped to the limit */
686 assert_se(setpriority_closest(-12) == 0);
687 errno = 0;
688 q = getpriority(PRIO_PROCESS, 0);
689 assert_se(errno == 0 && q == -10);
690 }
691
692 _exit(EXIT_SUCCESS);
693 }
694 }
695
696 int main(int argc, char *argv[]) {
697 test_setup_logging(LOG_DEBUG);
698
699 save_argc_argv(argc, argv);
700
701 if (argc > 1) {
702 pid_t pid = 0;
703
704 (void) parse_pid(argv[1], &pid);
705 test_get_process_comm(pid);
706 } else {
707 TEST_REQ_RUNNING_SYSTEMD(test_get_process_comm(1));
708 test_get_process_comm(getpid());
709 }
710
711 test_get_process_comm_escape();
712 test_pid_is_unwaited();
713 test_pid_is_alive();
714 test_personality();
715 test_get_process_cmdline_harder();
716 test_rename_process();
717 test_getpid_cached();
718 test_getpid_measure();
719 test_safe_fork();
720 test_pid_to_ptr();
721 test_ioprio_class_from_to_string();
722 test_setpriority_closest();
723
724 return 0;
725 }