1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include <sys/personality.h>
11 #if HAVE_VALGRIND_VALGRIND_H
12 #include <valgrind/valgrind.h>
15 #include "alloc-util.h"
16 #include "architecture.h"
17 #include "errno-util.h"
18 #include "errno-list.h"
19 #include "dirent-util.h"
23 #include "missing_sched.h"
24 #include "missing_syscall.h"
25 #include "parse-util.h"
26 #include "process-util.h"
27 #include "rlimit-util.h"
28 #include "signal-util.h"
29 #include "stdio-util.h"
30 #include "string-util.h"
31 #include "terminal-util.h"
33 #include "user-util.h"
37 static void test_get_process_comm(pid_t pid
) {
39 _cleanup_free_
char *a
= NULL
, *c
= NULL
, *d
= NULL
, *f
= NULL
, *i
= NULL
;
40 _cleanup_free_
char *env
= NULL
;
41 char path
[STRLEN("/proc//comm") + DECIMAL_STR_MAX(pid_t
)];
48 log_info("/* %s */", __func__
);
50 xsprintf(path
, "/proc/"PID_FMT
"/comm", pid
);
52 if (stat(path
, &st
) == 0) {
53 assert_se(get_process_comm(pid
, &a
) >= 0);
54 log_info("PID"PID_FMT
" comm: '%s'", pid
, a
);
56 log_warning("%s not exist.", path
);
58 assert_se(get_process_cmdline(pid
, 0, PROCESS_CMDLINE_COMM_FALLBACK
, &c
) >= 0);
59 log_info("PID"PID_FMT
" cmdline: '%s'", pid
, c
);
61 assert_se(get_process_cmdline(pid
, 8, 0, &d
) >= 0);
62 log_info("PID"PID_FMT
" cmdline truncated to 8: '%s'", pid
, d
);
65 assert_se(get_process_cmdline(pid
, 1, 0, &d
) >= 0);
66 log_info("PID"PID_FMT
" cmdline truncated to 1: '%s'", pid
, d
);
68 assert_se(get_process_ppid(pid
, &e
) >= 0);
69 log_info("PID"PID_FMT
" PPID: "PID_FMT
, pid
, e
);
70 assert_se(pid
== 1 ? e
== 0 : e
> 0);
72 assert_se(is_kernel_thread(pid
) == 0 || pid
!= 1);
74 r
= get_process_exe(pid
, &f
);
75 assert_se(r
>= 0 || r
== -EACCES
);
76 log_info("PID"PID_FMT
" exe: '%s'", pid
, strna(f
));
78 assert_se(get_process_uid(pid
, &u
) == 0);
79 log_info("PID"PID_FMT
" UID: "UID_FMT
, pid
, u
);
81 assert_se(get_process_gid(pid
, &g
) == 0);
82 log_info("PID"PID_FMT
" GID: "GID_FMT
, pid
, g
);
84 r
= get_process_environ(pid
, &env
);
85 assert_se(r
>= 0 || r
== -EACCES
);
86 log_info("PID"PID_FMT
" strlen(environ): %zi", pid
, env
? (ssize_t
)strlen(env
) : (ssize_t
)-errno
);
88 if (!detect_container())
89 assert_se(get_ctty_devnr(pid
, &h
) == -ENXIO
|| pid
!= 1);
91 (void) getenv_for_pid(pid
, "PATH", &i
);
92 log_info("PID"PID_FMT
" $PATH: '%s'", pid
, strna(i
));
95 static void test_get_process_cmdline_one(pid_t pid
) {
96 _cleanup_free_
char *c
= NULL
, *d
= NULL
, *e
= NULL
, *f
= NULL
, *g
= NULL
, *h
= NULL
;
99 r
= get_process_cmdline(pid
, SIZE_MAX
, 0, &c
);
100 log_info("PID "PID_FMT
": %s", pid
, r
>= 0 ? c
: errno_to_name(r
));
102 r
= get_process_cmdline(pid
, SIZE_MAX
, PROCESS_CMDLINE_COMM_FALLBACK
, &d
);
103 log_info(" %s", r
>= 0 ? d
: errno_to_name(r
));
105 r
= get_process_cmdline(pid
, SIZE_MAX
, PROCESS_CMDLINE_QUOTE
, &e
);
106 log_info(" %s", r
>= 0 ? e
: errno_to_name(r
));
108 r
= get_process_cmdline(pid
, SIZE_MAX
, PROCESS_CMDLINE_QUOTE
| PROCESS_CMDLINE_COMM_FALLBACK
, &f
);
109 log_info(" %s", r
>= 0 ? f
: errno_to_name(r
));
111 r
= get_process_cmdline(pid
, SIZE_MAX
, PROCESS_CMDLINE_QUOTE_POSIX
, &g
);
112 log_info(" %s", r
>= 0 ? g
: errno_to_name(r
));
114 r
= get_process_cmdline(pid
, SIZE_MAX
, PROCESS_CMDLINE_QUOTE_POSIX
| PROCESS_CMDLINE_COMM_FALLBACK
, &h
);
115 log_info(" %s", r
>= 0 ? h
: errno_to_name(r
));
118 static void test_get_process_cmdline(void) {
119 _cleanup_closedir_
DIR *d
= NULL
;
122 log_info("/* %s */", __func__
);
124 assert_se(d
= opendir("/proc"));
126 FOREACH_DIRENT(de
, d
, return) {
129 if (de
->d_type
!= DT_DIR
)
132 if (parse_pid(de
->d_name
, &pid
) < 0)
135 test_get_process_cmdline_one(pid
);
139 static void test_get_process_comm_escape_one(const char *input
, const char *output
) {
140 _cleanup_free_
char *n
= NULL
;
142 log_debug("input: <%s> — output: <%s>", input
, output
);
144 assert_se(prctl(PR_SET_NAME
, input
) >= 0);
145 assert_se(get_process_comm(0, &n
) >= 0);
147 log_debug("got: <%s>", n
);
149 assert_se(streq_ptr(n
, output
));
152 static void test_get_process_comm_escape(void) {
153 _cleanup_free_
char *saved
= NULL
;
155 log_info("/* %s */", __func__
);
157 assert_se(get_process_comm(0, &saved
) >= 0);
159 test_get_process_comm_escape_one("", "");
160 test_get_process_comm_escape_one("foo", "foo");
161 test_get_process_comm_escape_one("012345678901234", "012345678901234");
162 test_get_process_comm_escape_one("0123456789012345", "012345678901234");
163 test_get_process_comm_escape_one("äöüß", "\\303\\244\\303\\266\\303\\274\\303\\237");
164 test_get_process_comm_escape_one("xäöüß", "x\\303\\244\\303\\266\\303\\274\\303\\237");
165 test_get_process_comm_escape_one("xxäöüß", "xx\\303\\244\\303\\266\\303\\274\\303\\237");
166 test_get_process_comm_escape_one("xxxäöüß", "xxx\\303\\244\\303\\266\\303\\274\\303\\237");
167 test_get_process_comm_escape_one("xxxxäöüß", "xxxx\\303\\244\\303\\266\\303\\274\\303\\237");
168 test_get_process_comm_escape_one("xxxxxäöüß", "xxxxx\\303\\244\\303\\266\\303\\274\\303\\237");
170 assert_se(prctl(PR_SET_NAME
, saved
) >= 0);
173 static void test_pid_is_unwaited(void) {
183 waitpid(pid
, &status
, 0);
184 assert_se(!pid_is_unwaited(pid
));
186 assert_se(pid_is_unwaited(getpid_cached()));
187 assert_se(!pid_is_unwaited(-1));
190 static void test_pid_is_alive(void) {
193 log_info("/* %s */", __func__
);
202 waitpid(pid
, &status
, 0);
203 assert_se(!pid_is_alive(pid
));
205 assert_se(pid_is_alive(getpid_cached()));
206 assert_se(!pid_is_alive(-1));
209 static void test_personality(void) {
210 log_info("/* %s */", __func__
);
212 assert_se(personality_to_string(PER_LINUX
));
213 assert_se(!personality_to_string(PERSONALITY_INVALID
));
215 assert_se(streq(personality_to_string(PER_LINUX
), architecture_to_string(native_architecture())));
217 assert_se(personality_from_string(personality_to_string(PER_LINUX
)) == PER_LINUX
);
218 assert_se(personality_from_string(architecture_to_string(native_architecture())) == PER_LINUX
);
221 assert_se(streq_ptr(personality_to_string(PER_LINUX
), "x86-64"));
222 assert_se(streq_ptr(personality_to_string(PER_LINUX32
), "x86"));
224 assert_se(personality_from_string("x86-64") == PER_LINUX
);
225 assert_se(personality_from_string("x86") == PER_LINUX32
);
226 assert_se(personality_from_string("ia64") == PERSONALITY_INVALID
);
227 assert_se(personality_from_string(NULL
) == PERSONALITY_INVALID
);
229 assert_se(personality_from_string(personality_to_string(PER_LINUX32
)) == PER_LINUX32
);
233 static void test_get_process_cmdline_harder(void) {
234 char path
[] = "/tmp/test-cmdlineXXXXXX";
235 _cleanup_close_
int fd
= -1;
236 _cleanup_free_
char *line
= NULL
;
239 log_info("/* %s */", __func__
);
241 if (geteuid() != 0) {
242 log_info("Skipping %s: not root", __func__
);
246 if (!have_namespaces()) {
247 log_notice("Testing without namespaces, skipping %s", __func__
);
251 #if HAVE_VALGRIND_VALGRIND_H
252 /* valgrind patches open(/proc//cmdline)
253 * so, test_get_process_cmdline_harder fails always
254 * See https://github.com/systemd/systemd/pull/3555#issuecomment-226564908 */
255 if (RUNNING_ON_VALGRIND
) {
256 log_info("Skipping %s: running on valgrind", __func__
);
265 (void) wait_for_terminate(pid
, &si
);
267 assert_se(si
.si_code
== CLD_EXITED
);
268 assert_se(si
.si_status
== 0);
274 assert_se(unshare(CLONE_NEWNS
) >= 0);
276 if (mount(NULL
, "/", NULL
, MS_SLAVE
|MS_REC
, NULL
) < 0) {
277 log_warning_errno(errno
, "mount(..., \"/\", MS_SLAVE|MS_REC, ...) failed: %m");
278 assert_se(IN_SET(errno
, EPERM
, EACCES
));
282 fd
= mkostemp(path
, O_CLOEXEC
);
285 /* Note that we don't unmount the following bind-mount at the end of the test because the kernel
286 * will clear up its /proc/PID/ hierarchy automatically as soon as the test stops. */
287 if (mount(path
, "/proc/self/cmdline", "bind", MS_BIND
, NULL
) < 0) {
288 /* This happens under selinux… Abort the test in this case. */
289 log_warning_errno(errno
, "mount(..., \"/proc/self/cmdline\", \"bind\", ...) failed: %m");
290 assert_se(IN_SET(errno
, EPERM
, EACCES
));
294 /* Set RLIMIT_STACK to infinity to test we don't try to allocate unncessarily large values to read
296 if (setrlimit(RLIMIT_STACK
, &RLIMIT_MAKE_CONST(RLIM_INFINITY
)) < 0)
297 log_warning("Testing without RLIMIT_STACK=infinity");
299 assert_se(unlink(path
) >= 0);
301 assert_se(prctl(PR_SET_NAME
, "testa") >= 0);
303 assert_se(get_process_cmdline(0, SIZE_MAX
, 0, &line
) == -ENOENT
);
305 assert_se(get_process_cmdline(0, SIZE_MAX
, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
306 log_debug("'%s'", line
);
307 assert_se(streq(line
, "[testa]"));
310 assert_se(get_process_cmdline(0, SIZE_MAX
, PROCESS_CMDLINE_COMM_FALLBACK
| PROCESS_CMDLINE_QUOTE
, &line
) >= 0);
311 log_debug("'%s'", line
);
312 assert_se(streq(line
, "\"[testa]\"")); /* quoting is enabled here */
315 assert_se(get_process_cmdline(0, 0, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
316 log_debug("'%s'", line
);
317 assert_se(streq(line
, ""));
320 assert_se(get_process_cmdline(0, 1, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
321 assert_se(streq(line
, "…"));
324 assert_se(get_process_cmdline(0, 2, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
325 assert_se(streq(line
, "[…"));
328 assert_se(get_process_cmdline(0, 3, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
329 assert_se(streq(line
, "[t…"));
332 assert_se(get_process_cmdline(0, 4, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
333 assert_se(streq(line
, "[te…"));
336 assert_se(get_process_cmdline(0, 5, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
337 assert_se(streq(line
, "[tes…"));
340 assert_se(get_process_cmdline(0, 6, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
341 assert_se(streq(line
, "[test…"));
344 assert_se(get_process_cmdline(0, 7, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
345 assert_se(streq(line
, "[testa]"));
348 assert_se(get_process_cmdline(0, 8, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
349 assert_se(streq(line
, "[testa]"));
352 /* Test with multiple arguments that don't require quoting */
354 assert_se(write(fd
, "foo\0bar", 8) == 8);
356 assert_se(get_process_cmdline(0, SIZE_MAX
, 0, &line
) >= 0);
357 log_debug("'%s'", line
);
358 assert_se(streq(line
, "foo bar"));
361 assert_se(get_process_cmdline(0, SIZE_MAX
, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
362 assert_se(streq(line
, "foo bar"));
365 assert_se(write(fd
, "quux", 4) == 4);
366 assert_se(get_process_cmdline(0, SIZE_MAX
, 0, &line
) >= 0);
367 log_debug("'%s'", line
);
368 assert_se(streq(line
, "foo bar quux"));
371 assert_se(get_process_cmdline(0, SIZE_MAX
, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
372 log_debug("'%s'", line
);
373 assert_se(streq(line
, "foo bar quux"));
376 assert_se(get_process_cmdline(0, 1, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
377 log_debug("'%s'", line
);
378 assert_se(streq(line
, "…"));
381 assert_se(get_process_cmdline(0, 2, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
382 log_debug("'%s'", line
);
383 assert_se(streq(line
, "f…"));
386 assert_se(get_process_cmdline(0, 3, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
387 log_debug("'%s'", line
);
388 assert_se(streq(line
, "fo…"));
391 assert_se(get_process_cmdline(0, 4, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
392 log_debug("'%s'", line
);
393 assert_se(streq(line
, "foo…"));
396 assert_se(get_process_cmdline(0, 5, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
397 log_debug("'%s'", line
);
398 assert_se(streq(line
, "foo …"));
401 assert_se(get_process_cmdline(0, 6, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
402 log_debug("'%s'", line
);
403 assert_se(streq(line
, "foo b…"));
406 assert_se(get_process_cmdline(0, 7, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
407 log_debug("'%s'", line
);
408 assert_se(streq(line
, "foo ba…"));
411 assert_se(get_process_cmdline(0, 8, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
412 log_debug("'%s'", line
);
413 assert_se(streq(line
, "foo bar…"));
416 assert_se(get_process_cmdline(0, 9, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
417 log_debug("'%s'", line
);
418 assert_se(streq(line
, "foo bar …"));
421 assert_se(get_process_cmdline(0, 10, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
422 log_debug("'%s'", line
);
423 assert_se(streq(line
, "foo bar q…"));
426 assert_se(get_process_cmdline(0, 11, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
427 log_debug("'%s'", line
);
428 assert_se(streq(line
, "foo bar qu…"));
431 assert_se(get_process_cmdline(0, 12, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
432 log_debug("'%s'", line
);
433 assert_se(streq(line
, "foo bar quux"));
436 assert_se(get_process_cmdline(0, 13, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
437 log_debug("'%s'", line
);
438 assert_se(streq(line
, "foo bar quux"));
441 assert_se(get_process_cmdline(0, 14, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
442 log_debug("'%s'", line
);
443 assert_se(streq(line
, "foo bar quux"));
446 assert_se(get_process_cmdline(0, 1000, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
447 log_debug("'%s'", line
);
448 assert_se(streq(line
, "foo bar quux"));
451 assert_se(ftruncate(fd
, 0) >= 0);
452 assert_se(prctl(PR_SET_NAME
, "aaaa bbbb cccc") >= 0);
454 assert_se(get_process_cmdline(0, SIZE_MAX
, 0, &line
) == -ENOENT
);
456 assert_se(get_process_cmdline(0, SIZE_MAX
, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
457 log_debug("'%s'", line
);
458 assert_se(streq(line
, "[aaaa bbbb cccc]"));
461 assert_se(get_process_cmdline(0, 10, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
462 log_debug("'%s'", line
);
463 assert_se(streq(line
, "[aaaa bbb…"));
466 assert_se(get_process_cmdline(0, 11, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
467 log_debug("'%s'", line
);
468 assert_se(streq(line
, "[aaaa bbbb…"));
471 assert_se(get_process_cmdline(0, 12, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
472 log_debug("'%s'", line
);
473 assert_se(streq(line
, "[aaaa bbbb …"));
476 /* Test with multiple arguments that do require quoting */
478 #define CMDLINE1 "foo\0'bar'\0\"bar$\"\0x y z\0!``\0"
479 #define EXPECT1 "foo \"'bar'\" \"\\\"bar\\$\\\"\" \"x y z\" \"!\\`\\`\" \"\""
480 #define EXPECT1p "foo $'\\'bar\\'' $'\"bar$\"' $'x y z' $'!``' \"\""
481 assert_se(lseek(fd
, SEEK_SET
, 0) == 0);
482 assert_se(write(fd
, CMDLINE1
, sizeof CMDLINE1
) == sizeof CMDLINE1
);
483 assert_se(ftruncate(fd
, sizeof CMDLINE1
) == 0);
485 assert_se(get_process_cmdline(0, SIZE_MAX
, PROCESS_CMDLINE_QUOTE
, &line
) >= 0);
486 log_debug("got: ==%s==", line
);
487 log_debug("exp: ==%s==", EXPECT1
);
488 assert_se(streq(line
, EXPECT1
));
491 assert_se(get_process_cmdline(0, SIZE_MAX
, PROCESS_CMDLINE_QUOTE_POSIX
, &line
) >= 0);
492 log_debug("got: ==%s==", line
);
493 log_debug("exp: ==%s==", EXPECT1p
);
494 assert_se(streq(line
, EXPECT1p
));
497 #define CMDLINE2 "foo\0\1\2\3\0\0"
498 #define EXPECT2 "foo \"\\001\\002\\003\" \"\" \"\""
499 #define EXPECT2p "foo $'\\001\\002\\003' \"\" \"\""
500 assert_se(lseek(fd
, SEEK_SET
, 0) == 0);
501 assert_se(write(fd
, CMDLINE2
, sizeof CMDLINE2
) == sizeof CMDLINE2
);
502 assert_se(ftruncate(fd
, sizeof CMDLINE2
) == 0);
504 assert_se(get_process_cmdline(0, SIZE_MAX
, PROCESS_CMDLINE_QUOTE
, &line
) >= 0);
505 log_debug("got: ==%s==", line
);
506 log_debug("exp: ==%s==", EXPECT2
);
507 assert_se(streq(line
, EXPECT2
));
510 assert_se(get_process_cmdline(0, SIZE_MAX
, PROCESS_CMDLINE_QUOTE_POSIX
, &line
) >= 0);
511 log_debug("got: ==%s==", line
);
512 log_debug("exp: ==%s==", EXPECT2p
);
513 assert_se(streq(line
, EXPECT2p
));
520 static void test_rename_process_now(const char *p
, int ret
) {
521 _cleanup_free_
char *comm
= NULL
, *cmdline
= NULL
;
524 log_info("/* %s */", __func__
);
526 r
= rename_process(p
);
527 assert_se(r
== ret
||
528 (ret
== 0 && r
>= 0) ||
531 log_debug_errno(r
, "rename_process(%s): %m", p
);
536 #if HAVE_VALGRIND_VALGRIND_H
537 /* see above, valgrind is weird, we can't verify what we are doing here */
538 if (RUNNING_ON_VALGRIND
)
542 assert_se(get_process_comm(0, &comm
) >= 0);
543 log_debug("comm = <%s>", comm
);
544 assert_se(strneq(comm
, p
, TASK_COMM_LEN
-1));
545 /* We expect comm to be at most 16 bytes (TASK_COMM_LEN). The kernel may raise this limit in the
546 * future. We'd only check the initial part, at least until we recompile, but this will still pass. */
548 r
= get_process_cmdline(0, SIZE_MAX
, 0, &cmdline
);
550 /* we cannot expect cmdline to be renamed properly without privileges */
551 if (geteuid() == 0) {
552 if (r
== 0 && detect_container() > 0)
553 log_info("cmdline = <%s> (not verified, Running in unprivileged container?)", cmdline
);
555 log_info("cmdline = <%s> (expected <%.*s>)", cmdline
, (int) strlen("test-process-util"), p
);
557 bool skip
= cmdline
[0] == '"'; /* A shortcut to check if the string is quoted */
559 assert_se(strneq(cmdline
+ skip
, p
, strlen("test-process-util")));
560 assert_se(startswith(cmdline
+ skip
, p
));
563 log_info("cmdline = <%s> (not verified)", cmdline
);
566 static void test_rename_process_one(const char *p
, int ret
) {
570 log_info("/* %s */", __func__
);
577 test_rename_process_now(p
, ret
);
581 assert_se(wait_for_terminate(pid
, &si
) >= 0);
582 assert_se(si
.si_code
== CLD_EXITED
);
583 assert_se(si
.si_status
== EXIT_SUCCESS
);
586 static void test_rename_process_multi(void) {
595 assert_se(wait_for_terminate(pid
, &si
) >= 0);
596 assert_se(si
.si_code
== CLD_EXITED
);
597 assert_se(si
.si_status
== EXIT_SUCCESS
);
603 test_rename_process_now("one", 1);
604 test_rename_process_now("more", 0); /* longer than "one", hence truncated */
605 (void) setresuid(99, 99, 99); /* change uid when running privileged */
606 test_rename_process_now("time!", 0);
607 test_rename_process_now("0", 1); /* shorter than "one", should fit */
608 test_rename_process_one("", -EINVAL
);
609 test_rename_process_one(NULL
, -EINVAL
);
613 static void test_rename_process(void) {
614 test_rename_process_one(NULL
, -EINVAL
);
615 test_rename_process_one("", -EINVAL
);
616 test_rename_process_one("foo", 1); /* should always fit */
617 test_rename_process_one("this is a really really long process name, followed by some more words", 0); /* unlikely to fit */
618 test_rename_process_one("1234567", 1); /* should always fit */
619 test_rename_process_multi(); /* multiple invocations and dropped privileges */
622 static void test_getpid_cached(void) {
624 pid_t a
, b
, c
, d
, e
, f
, child
;
626 log_info("/* %s */", __func__
);
632 assert_se(a
== b
&& a
== c
);
635 assert_se(child
>= 0);
643 assert_se(a
== b
&& a
== c
);
651 assert_se(a
== d
&& a
== e
&& a
== f
);
653 assert_se(wait_for_terminate(child
, &si
) >= 0);
654 assert_se(si
.si_status
== 0);
655 assert_se(si
.si_code
== CLD_EXITED
);
658 static void test_getpid_measure(void) {
661 unsigned long long iterations
= slow_tests_enabled() ? 1000000 : 1000;
663 log_info("/* %s (%llu iterations) */", __func__
, iterations
);
665 t
= now(CLOCK_MONOTONIC
);
666 for (unsigned long long i
= 0; i
< iterations
; i
++)
668 q
= now(CLOCK_MONOTONIC
) - t
;
670 log_info(" glibc getpid(): %lf µs each\n", (double) q
/ iterations
);
672 iterations
*= 50; /* _cached() is about 50 times faster, so we need more iterations */
674 t
= now(CLOCK_MONOTONIC
);
675 for (unsigned long long i
= 0; i
< iterations
; i
++)
676 (void) getpid_cached();
677 q
= now(CLOCK_MONOTONIC
) - t
;
679 log_info("getpid_cached(): %lf µs each\n", (double) q
/ iterations
);
682 static void test_safe_fork(void) {
687 log_info("/* %s */", __func__
);
689 BLOCK_SIGNALS(SIGCHLD
);
691 r
= safe_fork("(test-child)", FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_DEATHSIG
|FORK_NULL_STDIO
|FORK_REOPEN_LOG
, &pid
);
696 usleep(100 * USEC_PER_MSEC
);
701 assert_se(wait_for_terminate(pid
, &status
) >= 0);
702 assert_se(status
.si_code
== CLD_EXITED
);
703 assert_se(status
.si_status
== 88);
706 static void test_pid_to_ptr(void) {
708 assert_se(PTR_TO_PID(NULL
) == 0);
709 assert_se(PID_TO_PTR(0) == NULL
);
711 assert_se(PTR_TO_PID(PID_TO_PTR(1)) == 1);
712 assert_se(PTR_TO_PID(PID_TO_PTR(2)) == 2);
713 assert_se(PTR_TO_PID(PID_TO_PTR(-1)) == -1);
714 assert_se(PTR_TO_PID(PID_TO_PTR(-2)) == -2);
716 assert_se(PTR_TO_PID(PID_TO_PTR(INT16_MAX
)) == INT16_MAX
);
717 assert_se(PTR_TO_PID(PID_TO_PTR(INT16_MIN
)) == INT16_MIN
);
719 assert_se(PTR_TO_PID(PID_TO_PTR(INT32_MAX
)) == INT32_MAX
);
720 assert_se(PTR_TO_PID(PID_TO_PTR(INT32_MIN
)) == INT32_MIN
);
723 static void test_ioprio_class_from_to_string_one(const char *val
, int expected
) {
724 assert_se(ioprio_class_from_string(val
) == expected
);
726 _cleanup_free_
char *s
= NULL
;
729 assert_se(ioprio_class_to_string_alloc(expected
, &s
) == 0);
730 /* We sometimes get a class number and sometimes a number back */
731 assert_se(streq(s
, val
) ||
732 safe_atou(val
, &ret
) == 0);
736 static void test_ioprio_class_from_to_string(void) {
737 log_info("/* %s */", __func__
);
739 test_ioprio_class_from_to_string_one("none", IOPRIO_CLASS_NONE
);
740 test_ioprio_class_from_to_string_one("realtime", IOPRIO_CLASS_RT
);
741 test_ioprio_class_from_to_string_one("best-effort", IOPRIO_CLASS_BE
);
742 test_ioprio_class_from_to_string_one("idle", IOPRIO_CLASS_IDLE
);
743 test_ioprio_class_from_to_string_one("0", 0);
744 test_ioprio_class_from_to_string_one("1", 1);
745 test_ioprio_class_from_to_string_one("7", 7);
746 test_ioprio_class_from_to_string_one("8", 8);
747 test_ioprio_class_from_to_string_one("9", -EINVAL
);
748 test_ioprio_class_from_to_string_one("-1", -EINVAL
);
751 static void test_setpriority_closest(void) {
754 log_info("/* %s */", __func__
);
756 r
= safe_fork("(test-setprio)",
757 FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_DEATHSIG
|FORK_WAIT
|FORK_LOG
, NULL
);
765 /* rlimit of 30 equals nice level of -10 */
766 if (setrlimit(RLIMIT_NICE
, &RLIMIT_MAKE_CONST(30)) < 0) {
767 /* If this fails we are probably unprivileged or in a userns of some kind, let's skip
769 assert_se(ERRNO_IS_PRIVILEGE(errno
));
772 assert_se(setresgid(GID_NOBODY
, GID_NOBODY
, GID_NOBODY
) >= 0);
773 assert_se(setresuid(UID_NOBODY
, UID_NOBODY
, UID_NOBODY
) >= 0);
778 p
= getpriority(PRIO_PROCESS
, 0);
779 assert_se(errno
== 0);
781 /* It should always be possible to set our nice level to the current one */
782 assert_se(setpriority_closest(p
) > 0);
785 q
= getpriority(PRIO_PROCESS
, 0);
786 assert_se(errno
== 0 && p
== q
);
788 /* It should also be possible to set the nice level to one higher */
789 if (p
< PRIO_MAX
-1) {
790 assert_se(setpriority_closest(++p
) > 0);
793 q
= getpriority(PRIO_PROCESS
, 0);
794 assert_se(errno
== 0 && p
== q
);
797 /* It should also be possible to set the nice level to two higher */
798 if (p
< PRIO_MAX
-1) {
799 assert_se(setpriority_closest(++p
) > 0);
802 q
= getpriority(PRIO_PROCESS
, 0);
803 assert_se(errno
== 0 && p
== q
);
807 /* These two should work, given the RLIMIT_NICE we set above */
808 assert_se(setpriority_closest(-10) > 0);
810 q
= getpriority(PRIO_PROCESS
, 0);
811 assert_se(errno
== 0 && q
== -10);
813 assert_se(setpriority_closest(-9) > 0);
815 q
= getpriority(PRIO_PROCESS
, 0);
816 assert_se(errno
== 0 && q
== -9);
818 /* This should succeed but should be clamped to the limit */
819 assert_se(setpriority_closest(-11) == 0);
821 q
= getpriority(PRIO_PROCESS
, 0);
822 assert_se(errno
== 0 && q
== -10);
824 assert_se(setpriority_closest(-8) > 0);
826 q
= getpriority(PRIO_PROCESS
, 0);
827 assert_se(errno
== 0 && q
== -8);
829 /* This should succeed but should be clamped to the limit */
830 assert_se(setpriority_closest(-12) == 0);
832 q
= getpriority(PRIO_PROCESS
, 0);
833 assert_se(errno
== 0 && q
== -10);
840 int main(int argc
, char *argv
[]) {
841 log_show_color(true);
842 test_setup_logging(LOG_INFO
);
844 save_argc_argv(argc
, argv
);
849 (void) parse_pid(argv
[1], &pid
);
850 test_get_process_comm(pid
);
852 TEST_REQ_RUNNING_SYSTEMD(test_get_process_comm(1));
853 test_get_process_comm(getpid());
856 test_get_process_comm_escape();
857 test_get_process_cmdline();
858 test_pid_is_unwaited();
861 test_get_process_cmdline_harder();
862 test_rename_process();
863 test_getpid_cached();
864 test_getpid_measure();
867 test_ioprio_class_from_to_string();
868 test_setpriority_closest();