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 "dirent-util.h"
18 #include "errno-list.h"
19 #include "errno-util.h"
23 #include "missing_sched.h"
24 #include "missing_syscall.h"
25 #include "parse-util.h"
26 #include "process-util.h"
27 #include "procfs-util.h"
28 #include "rlimit-util.h"
29 #include "signal-util.h"
30 #include "stdio-util.h"
31 #include "string-util.h"
32 #include "terminal-util.h"
34 #include "user-util.h"
38 static void test_get_process_comm(pid_t pid
) {
40 _cleanup_free_
char *a
= NULL
, *c
= NULL
, *d
= NULL
, *f
= NULL
, *i
= NULL
;
41 _cleanup_free_
char *env
= NULL
;
42 char path
[STRLEN("/proc//comm") + DECIMAL_STR_MAX(pid_t
)];
49 log_info("/* %s */", __func__
);
51 xsprintf(path
, "/proc/"PID_FMT
"/comm", pid
);
53 if (stat(path
, &st
) == 0) {
54 assert_se(get_process_comm(pid
, &a
) >= 0);
55 log_info("PID"PID_FMT
" comm: '%s'", pid
, a
);
57 log_warning("%s not exist.", path
);
59 assert_se(get_process_cmdline(pid
, 0, PROCESS_CMDLINE_COMM_FALLBACK
, &c
) >= 0);
60 log_info("PID"PID_FMT
" cmdline: '%s'", pid
, c
);
62 assert_se(get_process_cmdline(pid
, 8, 0, &d
) >= 0);
63 log_info("PID"PID_FMT
" cmdline truncated to 8: '%s'", pid
, d
);
66 assert_se(get_process_cmdline(pid
, 1, 0, &d
) >= 0);
67 log_info("PID"PID_FMT
" cmdline truncated to 1: '%s'", pid
, d
);
69 r
= get_process_ppid(pid
, &e
);
70 assert_se(pid
== 1 ? r
== -EADDRNOTAVAIL
: r
>= 0);
72 log_info("PID"PID_FMT
" PPID: "PID_FMT
, pid
, e
);
76 assert_se(is_kernel_thread(pid
) == 0 || pid
!= 1);
78 r
= get_process_exe(pid
, &f
);
79 assert_se(r
>= 0 || r
== -EACCES
);
80 log_info("PID"PID_FMT
" exe: '%s'", pid
, strna(f
));
82 assert_se(get_process_uid(pid
, &u
) == 0);
83 log_info("PID"PID_FMT
" UID: "UID_FMT
, pid
, u
);
85 assert_se(get_process_gid(pid
, &g
) == 0);
86 log_info("PID"PID_FMT
" GID: "GID_FMT
, pid
, g
);
88 r
= get_process_environ(pid
, &env
);
89 assert_se(r
>= 0 || r
== -EACCES
);
90 log_info("PID"PID_FMT
" strlen(environ): %zi", pid
, env
? (ssize_t
)strlen(env
) : (ssize_t
)-errno
);
92 if (!detect_container())
93 assert_se(get_ctty_devnr(pid
, &h
) == -ENXIO
|| pid
!= 1);
95 (void) getenv_for_pid(pid
, "PATH", &i
);
96 log_info("PID"PID_FMT
" $PATH: '%s'", pid
, strna(i
));
99 static void test_get_process_cmdline_one(pid_t pid
) {
100 _cleanup_free_
char *c
= NULL
, *d
= NULL
, *e
= NULL
, *f
= NULL
, *g
= NULL
, *h
= NULL
;
103 r
= get_process_cmdline(pid
, SIZE_MAX
, 0, &c
);
104 log_info("PID "PID_FMT
": %s", pid
, r
>= 0 ? c
: errno_to_name(r
));
106 r
= get_process_cmdline(pid
, SIZE_MAX
, PROCESS_CMDLINE_COMM_FALLBACK
, &d
);
107 log_info(" %s", r
>= 0 ? d
: errno_to_name(r
));
109 r
= get_process_cmdline(pid
, SIZE_MAX
, PROCESS_CMDLINE_QUOTE
, &e
);
110 log_info(" %s", r
>= 0 ? e
: errno_to_name(r
));
112 r
= get_process_cmdline(pid
, SIZE_MAX
, PROCESS_CMDLINE_QUOTE
| PROCESS_CMDLINE_COMM_FALLBACK
, &f
);
113 log_info(" %s", r
>= 0 ? f
: errno_to_name(r
));
115 r
= get_process_cmdline(pid
, SIZE_MAX
, PROCESS_CMDLINE_QUOTE_POSIX
, &g
);
116 log_info(" %s", r
>= 0 ? g
: errno_to_name(r
));
118 r
= get_process_cmdline(pid
, SIZE_MAX
, PROCESS_CMDLINE_QUOTE_POSIX
| PROCESS_CMDLINE_COMM_FALLBACK
, &h
);
119 log_info(" %s", r
>= 0 ? h
: errno_to_name(r
));
122 static void test_get_process_cmdline(void) {
123 _cleanup_closedir_
DIR *d
= NULL
;
126 log_info("/* %s */", __func__
);
128 assert_se(d
= opendir("/proc"));
130 FOREACH_DIRENT(de
, d
, return) {
133 if (de
->d_type
!= DT_DIR
)
136 if (parse_pid(de
->d_name
, &pid
) < 0)
139 test_get_process_cmdline_one(pid
);
143 static void test_get_process_comm_escape_one(const char *input
, const char *output
) {
144 _cleanup_free_
char *n
= NULL
;
146 log_debug("input: <%s> — output: <%s>", input
, output
);
148 assert_se(prctl(PR_SET_NAME
, input
) >= 0);
149 assert_se(get_process_comm(0, &n
) >= 0);
151 log_debug("got: <%s>", n
);
153 assert_se(streq_ptr(n
, output
));
156 static void test_get_process_comm_escape(void) {
157 _cleanup_free_
char *saved
= NULL
;
159 log_info("/* %s */", __func__
);
161 assert_se(get_process_comm(0, &saved
) >= 0);
163 test_get_process_comm_escape_one("", "");
164 test_get_process_comm_escape_one("foo", "foo");
165 test_get_process_comm_escape_one("012345678901234", "012345678901234");
166 test_get_process_comm_escape_one("0123456789012345", "012345678901234");
167 test_get_process_comm_escape_one("äöüß", "\\303\\244\\303\\266\\303\\274\\303\\237");
168 test_get_process_comm_escape_one("xäöüß", "x\\303\\244\\303\\266\\303\\274\\303\\237");
169 test_get_process_comm_escape_one("xxäöüß", "xx\\303\\244\\303\\266\\303\\274\\303\\237");
170 test_get_process_comm_escape_one("xxxäöüß", "xxx\\303\\244\\303\\266\\303\\274\\303\\237");
171 test_get_process_comm_escape_one("xxxxäöüß", "xxxx\\303\\244\\303\\266\\303\\274\\303\\237");
172 test_get_process_comm_escape_one("xxxxxäöüß", "xxxxx\\303\\244\\303\\266\\303\\274\\303\\237");
174 assert_se(prctl(PR_SET_NAME
, saved
) >= 0);
177 static void test_pid_is_unwaited(void) {
187 waitpid(pid
, &status
, 0);
188 assert_se(!pid_is_unwaited(pid
));
190 assert_se(pid_is_unwaited(getpid_cached()));
191 assert_se(!pid_is_unwaited(-1));
194 static void test_pid_is_alive(void) {
197 log_info("/* %s */", __func__
);
206 waitpid(pid
, &status
, 0);
207 assert_se(!pid_is_alive(pid
));
209 assert_se(pid_is_alive(getpid_cached()));
210 assert_se(!pid_is_alive(-1));
213 static void test_personality(void) {
214 log_info("/* %s */", __func__
);
216 assert_se(personality_to_string(PER_LINUX
));
217 assert_se(!personality_to_string(PERSONALITY_INVALID
));
219 assert_se(streq(personality_to_string(PER_LINUX
), architecture_to_string(native_architecture())));
221 assert_se(personality_from_string(personality_to_string(PER_LINUX
)) == PER_LINUX
);
222 assert_se(personality_from_string(architecture_to_string(native_architecture())) == PER_LINUX
);
225 assert_se(streq_ptr(personality_to_string(PER_LINUX
), "x86-64"));
226 assert_se(streq_ptr(personality_to_string(PER_LINUX32
), "x86"));
228 assert_se(personality_from_string("x86-64") == PER_LINUX
);
229 assert_se(personality_from_string("x86") == PER_LINUX32
);
230 assert_se(personality_from_string("ia64") == PERSONALITY_INVALID
);
231 assert_se(personality_from_string(NULL
) == PERSONALITY_INVALID
);
233 assert_se(personality_from_string(personality_to_string(PER_LINUX32
)) == PER_LINUX32
);
237 static void test_get_process_cmdline_harder(void) {
238 char path
[] = "/tmp/test-cmdlineXXXXXX";
239 _cleanup_close_
int fd
= -1;
240 _cleanup_free_
char *line
= NULL
;
243 log_info("/* %s */", __func__
);
245 if (geteuid() != 0) {
246 log_info("Skipping %s: not root", __func__
);
250 if (!have_namespaces()) {
251 log_notice("Testing without namespaces, skipping %s", __func__
);
255 #if HAVE_VALGRIND_VALGRIND_H
256 /* valgrind patches open(/proc//cmdline)
257 * so, test_get_process_cmdline_harder fails always
258 * See https://github.com/systemd/systemd/pull/3555#issuecomment-226564908 */
259 if (RUNNING_ON_VALGRIND
) {
260 log_info("Skipping %s: running on valgrind", __func__
);
269 (void) wait_for_terminate(pid
, &si
);
271 assert_se(si
.si_code
== CLD_EXITED
);
272 assert_se(si
.si_status
== 0);
278 assert_se(unshare(CLONE_NEWNS
) >= 0);
280 if (mount(NULL
, "/", NULL
, MS_SLAVE
|MS_REC
, NULL
) < 0) {
281 log_warning_errno(errno
, "mount(..., \"/\", MS_SLAVE|MS_REC, ...) failed: %m");
282 assert_se(IN_SET(errno
, EPERM
, EACCES
));
286 fd
= mkostemp(path
, O_CLOEXEC
);
289 /* Note that we don't unmount the following bind-mount at the end of the test because the kernel
290 * will clear up its /proc/PID/ hierarchy automatically as soon as the test stops. */
291 if (mount(path
, "/proc/self/cmdline", "bind", MS_BIND
, NULL
) < 0) {
292 /* This happens under selinux… Abort the test in this case. */
293 log_warning_errno(errno
, "mount(..., \"/proc/self/cmdline\", \"bind\", ...) failed: %m");
294 assert_se(IN_SET(errno
, EPERM
, EACCES
));
298 /* Set RLIMIT_STACK to infinity to test we don't try to allocate unncessarily large values to read
300 if (setrlimit(RLIMIT_STACK
, &RLIMIT_MAKE_CONST(RLIM_INFINITY
)) < 0)
301 log_warning("Testing without RLIMIT_STACK=infinity");
303 assert_se(unlink(path
) >= 0);
305 assert_se(prctl(PR_SET_NAME
, "testa") >= 0);
307 assert_se(get_process_cmdline(0, SIZE_MAX
, 0, &line
) == -ENOENT
);
309 assert_se(get_process_cmdline(0, SIZE_MAX
, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
310 log_debug("'%s'", line
);
311 assert_se(streq(line
, "[testa]"));
314 assert_se(get_process_cmdline(0, SIZE_MAX
, PROCESS_CMDLINE_COMM_FALLBACK
| PROCESS_CMDLINE_QUOTE
, &line
) >= 0);
315 log_debug("'%s'", line
);
316 assert_se(streq(line
, "\"[testa]\"")); /* quoting is enabled here */
319 assert_se(get_process_cmdline(0, 0, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
320 log_debug("'%s'", line
);
321 assert_se(streq(line
, ""));
324 assert_se(get_process_cmdline(0, 1, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
325 assert_se(streq(line
, "…"));
328 assert_se(get_process_cmdline(0, 2, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
329 assert_se(streq(line
, "[…"));
332 assert_se(get_process_cmdline(0, 3, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
333 assert_se(streq(line
, "[t…"));
336 assert_se(get_process_cmdline(0, 4, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
337 assert_se(streq(line
, "[te…"));
340 assert_se(get_process_cmdline(0, 5, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
341 assert_se(streq(line
, "[tes…"));
344 assert_se(get_process_cmdline(0, 6, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
345 assert_se(streq(line
, "[test…"));
348 assert_se(get_process_cmdline(0, 7, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
349 assert_se(streq(line
, "[testa]"));
352 assert_se(get_process_cmdline(0, 8, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
353 assert_se(streq(line
, "[testa]"));
356 /* Test with multiple arguments that don't require quoting */
358 assert_se(write(fd
, "foo\0bar", 8) == 8);
360 assert_se(get_process_cmdline(0, SIZE_MAX
, 0, &line
) >= 0);
361 log_debug("'%s'", line
);
362 assert_se(streq(line
, "foo bar"));
365 assert_se(get_process_cmdline(0, SIZE_MAX
, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
366 assert_se(streq(line
, "foo bar"));
369 assert_se(write(fd
, "quux", 4) == 4);
370 assert_se(get_process_cmdline(0, SIZE_MAX
, 0, &line
) >= 0);
371 log_debug("'%s'", line
);
372 assert_se(streq(line
, "foo bar quux"));
375 assert_se(get_process_cmdline(0, SIZE_MAX
, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
376 log_debug("'%s'", line
);
377 assert_se(streq(line
, "foo bar quux"));
380 assert_se(get_process_cmdline(0, 1, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
381 log_debug("'%s'", line
);
382 assert_se(streq(line
, "…"));
385 assert_se(get_process_cmdline(0, 2, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
386 log_debug("'%s'", line
);
387 assert_se(streq(line
, "f…"));
390 assert_se(get_process_cmdline(0, 3, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
391 log_debug("'%s'", line
);
392 assert_se(streq(line
, "fo…"));
395 assert_se(get_process_cmdline(0, 4, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
396 log_debug("'%s'", line
);
397 assert_se(streq(line
, "foo…"));
400 assert_se(get_process_cmdline(0, 5, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
401 log_debug("'%s'", line
);
402 assert_se(streq(line
, "foo …"));
405 assert_se(get_process_cmdline(0, 6, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
406 log_debug("'%s'", line
);
407 assert_se(streq(line
, "foo b…"));
410 assert_se(get_process_cmdline(0, 7, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
411 log_debug("'%s'", line
);
412 assert_se(streq(line
, "foo ba…"));
415 assert_se(get_process_cmdline(0, 8, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
416 log_debug("'%s'", line
);
417 assert_se(streq(line
, "foo bar…"));
420 assert_se(get_process_cmdline(0, 9, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
421 log_debug("'%s'", line
);
422 assert_se(streq(line
, "foo bar …"));
425 assert_se(get_process_cmdline(0, 10, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
426 log_debug("'%s'", line
);
427 assert_se(streq(line
, "foo bar q…"));
430 assert_se(get_process_cmdline(0, 11, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
431 log_debug("'%s'", line
);
432 assert_se(streq(line
, "foo bar qu…"));
435 assert_se(get_process_cmdline(0, 12, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
436 log_debug("'%s'", line
);
437 assert_se(streq(line
, "foo bar quux"));
440 assert_se(get_process_cmdline(0, 13, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
441 log_debug("'%s'", line
);
442 assert_se(streq(line
, "foo bar quux"));
445 assert_se(get_process_cmdline(0, 14, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
446 log_debug("'%s'", line
);
447 assert_se(streq(line
, "foo bar quux"));
450 assert_se(get_process_cmdline(0, 1000, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
451 log_debug("'%s'", line
);
452 assert_se(streq(line
, "foo bar quux"));
455 assert_se(ftruncate(fd
, 0) >= 0);
456 assert_se(prctl(PR_SET_NAME
, "aaaa bbbb cccc") >= 0);
458 assert_se(get_process_cmdline(0, SIZE_MAX
, 0, &line
) == -ENOENT
);
460 assert_se(get_process_cmdline(0, SIZE_MAX
, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
461 log_debug("'%s'", line
);
462 assert_se(streq(line
, "[aaaa bbbb cccc]"));
465 assert_se(get_process_cmdline(0, 10, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
466 log_debug("'%s'", line
);
467 assert_se(streq(line
, "[aaaa bbb…"));
470 assert_se(get_process_cmdline(0, 11, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
471 log_debug("'%s'", line
);
472 assert_se(streq(line
, "[aaaa bbbb…"));
475 assert_se(get_process_cmdline(0, 12, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
476 log_debug("'%s'", line
);
477 assert_se(streq(line
, "[aaaa bbbb …"));
480 /* Test with multiple arguments that do require quoting */
482 #define CMDLINE1 "foo\0'bar'\0\"bar$\"\0x y z\0!``\0"
483 #define EXPECT1 "foo \"'bar'\" \"\\\"bar\\$\\\"\" \"x y z\" \"!\\`\\`\" \"\""
484 #define EXPECT1p "foo $'\\'bar\\'' $'\"bar$\"' $'x y z' $'!``' \"\""
485 assert_se(lseek(fd
, SEEK_SET
, 0) == 0);
486 assert_se(write(fd
, CMDLINE1
, sizeof CMDLINE1
) == sizeof CMDLINE1
);
487 assert_se(ftruncate(fd
, sizeof CMDLINE1
) == 0);
489 assert_se(get_process_cmdline(0, SIZE_MAX
, PROCESS_CMDLINE_QUOTE
, &line
) >= 0);
490 log_debug("got: ==%s==", line
);
491 log_debug("exp: ==%s==", EXPECT1
);
492 assert_se(streq(line
, EXPECT1
));
495 assert_se(get_process_cmdline(0, SIZE_MAX
, PROCESS_CMDLINE_QUOTE_POSIX
, &line
) >= 0);
496 log_debug("got: ==%s==", line
);
497 log_debug("exp: ==%s==", EXPECT1p
);
498 assert_se(streq(line
, EXPECT1p
));
501 #define CMDLINE2 "foo\0\1\2\3\0\0"
502 #define EXPECT2 "foo \"\\001\\002\\003\" \"\" \"\""
503 #define EXPECT2p "foo $'\\001\\002\\003' \"\" \"\""
504 assert_se(lseek(fd
, SEEK_SET
, 0) == 0);
505 assert_se(write(fd
, CMDLINE2
, sizeof CMDLINE2
) == sizeof CMDLINE2
);
506 assert_se(ftruncate(fd
, sizeof CMDLINE2
) == 0);
508 assert_se(get_process_cmdline(0, SIZE_MAX
, PROCESS_CMDLINE_QUOTE
, &line
) >= 0);
509 log_debug("got: ==%s==", line
);
510 log_debug("exp: ==%s==", EXPECT2
);
511 assert_se(streq(line
, EXPECT2
));
514 assert_se(get_process_cmdline(0, SIZE_MAX
, PROCESS_CMDLINE_QUOTE_POSIX
, &line
) >= 0);
515 log_debug("got: ==%s==", line
);
516 log_debug("exp: ==%s==", EXPECT2p
);
517 assert_se(streq(line
, EXPECT2p
));
524 static void test_rename_process_now(const char *p
, int ret
) {
525 _cleanup_free_
char *comm
= NULL
, *cmdline
= NULL
;
528 log_info("/* %s */", __func__
);
530 r
= rename_process(p
);
531 assert_se(r
== ret
||
532 (ret
== 0 && r
>= 0) ||
535 log_debug_errno(r
, "rename_process(%s): %m", p
);
540 #if HAVE_VALGRIND_VALGRIND_H
541 /* see above, valgrind is weird, we can't verify what we are doing here */
542 if (RUNNING_ON_VALGRIND
)
546 assert_se(get_process_comm(0, &comm
) >= 0);
547 log_debug("comm = <%s>", comm
);
548 assert_se(strneq(comm
, p
, TASK_COMM_LEN
-1));
549 /* We expect comm to be at most 16 bytes (TASK_COMM_LEN). The kernel may raise this limit in the
550 * future. We'd only check the initial part, at least until we recompile, but this will still pass. */
552 r
= get_process_cmdline(0, SIZE_MAX
, 0, &cmdline
);
554 /* we cannot expect cmdline to be renamed properly without privileges */
555 if (geteuid() == 0) {
556 if (r
== 0 && detect_container() > 0)
557 log_info("cmdline = <%s> (not verified, Running in unprivileged container?)", cmdline
);
559 log_info("cmdline = <%s> (expected <%.*s>)", cmdline
, (int) strlen("test-process-util"), p
);
561 bool skip
= cmdline
[0] == '"'; /* A shortcut to check if the string is quoted */
563 assert_se(strneq(cmdline
+ skip
, p
, strlen("test-process-util")));
564 assert_se(startswith(cmdline
+ skip
, p
));
567 log_info("cmdline = <%s> (not verified)", cmdline
);
570 static void test_rename_process_one(const char *p
, int ret
) {
574 log_info("/* %s */", __func__
);
581 test_rename_process_now(p
, ret
);
585 assert_se(wait_for_terminate(pid
, &si
) >= 0);
586 assert_se(si
.si_code
== CLD_EXITED
);
587 assert_se(si
.si_status
== EXIT_SUCCESS
);
590 static void test_rename_process_multi(void) {
599 assert_se(wait_for_terminate(pid
, &si
) >= 0);
600 assert_se(si
.si_code
== CLD_EXITED
);
601 assert_se(si
.si_status
== EXIT_SUCCESS
);
607 test_rename_process_now("one", 1);
608 test_rename_process_now("more", 0); /* longer than "one", hence truncated */
609 (void) setresuid(99, 99, 99); /* change uid when running privileged */
610 test_rename_process_now("time!", 0);
611 test_rename_process_now("0", 1); /* shorter than "one", should fit */
612 test_rename_process_one("", -EINVAL
);
613 test_rename_process_one(NULL
, -EINVAL
);
617 static void test_rename_process(void) {
618 test_rename_process_one(NULL
, -EINVAL
);
619 test_rename_process_one("", -EINVAL
);
620 test_rename_process_one("foo", 1); /* should always fit */
621 test_rename_process_one("this is a really really long process name, followed by some more words", 0); /* unlikely to fit */
622 test_rename_process_one("1234567", 1); /* should always fit */
623 test_rename_process_multi(); /* multiple invocations and dropped privileges */
626 static void test_getpid_cached(void) {
628 pid_t a
, b
, c
, d
, e
, f
, child
;
630 log_info("/* %s */", __func__
);
636 assert_se(a
== b
&& a
== c
);
639 assert_se(child
>= 0);
647 assert_se(a
== b
&& a
== c
);
655 assert_se(a
== d
&& a
== e
&& a
== f
);
657 assert_se(wait_for_terminate(child
, &si
) >= 0);
658 assert_se(si
.si_status
== 0);
659 assert_se(si
.si_code
== CLD_EXITED
);
662 static void test_getpid_measure(void) {
665 unsigned long long iterations
= slow_tests_enabled() ? 1000000 : 1000;
667 log_info("/* %s (%llu iterations) */", __func__
, iterations
);
669 t
= now(CLOCK_MONOTONIC
);
670 for (unsigned long long i
= 0; i
< iterations
; i
++)
672 q
= now(CLOCK_MONOTONIC
) - t
;
674 log_info(" glibc getpid(): %lf µs each\n", (double) q
/ iterations
);
676 iterations
*= 50; /* _cached() is about 50 times faster, so we need more iterations */
678 t
= now(CLOCK_MONOTONIC
);
679 for (unsigned long long i
= 0; i
< iterations
; i
++)
680 (void) getpid_cached();
681 q
= now(CLOCK_MONOTONIC
) - t
;
683 log_info("getpid_cached(): %lf µs each\n", (double) q
/ iterations
);
686 static void test_safe_fork(void) {
691 log_info("/* %s */", __func__
);
693 BLOCK_SIGNALS(SIGCHLD
);
695 r
= safe_fork("(test-child)", FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_DEATHSIG
|FORK_NULL_STDIO
|FORK_REOPEN_LOG
, &pid
);
700 usleep(100 * USEC_PER_MSEC
);
705 assert_se(wait_for_terminate(pid
, &status
) >= 0);
706 assert_se(status
.si_code
== CLD_EXITED
);
707 assert_se(status
.si_status
== 88);
710 static void test_pid_to_ptr(void) {
712 assert_se(PTR_TO_PID(NULL
) == 0);
713 assert_se(PID_TO_PTR(0) == NULL
);
715 assert_se(PTR_TO_PID(PID_TO_PTR(1)) == 1);
716 assert_se(PTR_TO_PID(PID_TO_PTR(2)) == 2);
717 assert_se(PTR_TO_PID(PID_TO_PTR(-1)) == -1);
718 assert_se(PTR_TO_PID(PID_TO_PTR(-2)) == -2);
720 assert_se(PTR_TO_PID(PID_TO_PTR(INT16_MAX
)) == INT16_MAX
);
721 assert_se(PTR_TO_PID(PID_TO_PTR(INT16_MIN
)) == INT16_MIN
);
723 assert_se(PTR_TO_PID(PID_TO_PTR(INT32_MAX
)) == INT32_MAX
);
724 assert_se(PTR_TO_PID(PID_TO_PTR(INT32_MIN
)) == INT32_MIN
);
727 static void test_ioprio_class_from_to_string_one(const char *val
, int expected
) {
728 assert_se(ioprio_class_from_string(val
) == expected
);
730 _cleanup_free_
char *s
= NULL
;
733 assert_se(ioprio_class_to_string_alloc(expected
, &s
) == 0);
734 /* We sometimes get a class number and sometimes a number back */
735 assert_se(streq(s
, val
) ||
736 safe_atou(val
, &ret
) == 0);
740 static void test_ioprio_class_from_to_string(void) {
741 log_info("/* %s */", __func__
);
743 test_ioprio_class_from_to_string_one("none", IOPRIO_CLASS_NONE
);
744 test_ioprio_class_from_to_string_one("realtime", IOPRIO_CLASS_RT
);
745 test_ioprio_class_from_to_string_one("best-effort", IOPRIO_CLASS_BE
);
746 test_ioprio_class_from_to_string_one("idle", IOPRIO_CLASS_IDLE
);
747 test_ioprio_class_from_to_string_one("0", 0);
748 test_ioprio_class_from_to_string_one("1", 1);
749 test_ioprio_class_from_to_string_one("7", 7);
750 test_ioprio_class_from_to_string_one("8", 8);
751 test_ioprio_class_from_to_string_one("9", -EINVAL
);
752 test_ioprio_class_from_to_string_one("-1", -EINVAL
);
755 static void test_setpriority_closest(void) {
758 log_info("/* %s */", __func__
);
760 r
= safe_fork("(test-setprio)",
761 FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_DEATHSIG
|FORK_WAIT
|FORK_LOG
, NULL
);
769 /* rlimit of 30 equals nice level of -10 */
770 if (setrlimit(RLIMIT_NICE
, &RLIMIT_MAKE_CONST(30)) < 0) {
771 /* If this fails we are probably unprivileged or in a userns of some kind, let's skip
773 assert_se(ERRNO_IS_PRIVILEGE(errno
));
776 assert_se(setresgid(GID_NOBODY
, GID_NOBODY
, GID_NOBODY
) >= 0);
777 assert_se(setresuid(UID_NOBODY
, UID_NOBODY
, UID_NOBODY
) >= 0);
782 p
= getpriority(PRIO_PROCESS
, 0);
783 assert_se(errno
== 0);
785 /* It should always be possible to set our nice level to the current one */
786 assert_se(setpriority_closest(p
) > 0);
789 q
= getpriority(PRIO_PROCESS
, 0);
790 assert_se(errno
== 0 && p
== q
);
792 /* It should also be possible to set the nice level to one higher */
793 if (p
< PRIO_MAX
-1) {
794 assert_se(setpriority_closest(++p
) > 0);
797 q
= getpriority(PRIO_PROCESS
, 0);
798 assert_se(errno
== 0 && p
== q
);
801 /* It should also be possible to set the nice level to two higher */
802 if (p
< PRIO_MAX
-1) {
803 assert_se(setpriority_closest(++p
) > 0);
806 q
= getpriority(PRIO_PROCESS
, 0);
807 assert_se(errno
== 0 && p
== q
);
811 /* These two should work, given the RLIMIT_NICE we set above */
812 assert_se(setpriority_closest(-10) > 0);
814 q
= getpriority(PRIO_PROCESS
, 0);
815 assert_se(errno
== 0 && q
== -10);
817 assert_se(setpriority_closest(-9) > 0);
819 q
= getpriority(PRIO_PROCESS
, 0);
820 assert_se(errno
== 0 && q
== -9);
822 /* This should succeed but should be clamped to the limit */
823 assert_se(setpriority_closest(-11) == 0);
825 q
= getpriority(PRIO_PROCESS
, 0);
826 assert_se(errno
== 0 && q
== -10);
828 assert_se(setpriority_closest(-8) > 0);
830 q
= getpriority(PRIO_PROCESS
, 0);
831 assert_se(errno
== 0 && q
== -8);
833 /* This should succeed but should be clamped to the limit */
834 assert_se(setpriority_closest(-12) == 0);
836 q
= getpriority(PRIO_PROCESS
, 0);
837 assert_se(errno
== 0 && q
== -10);
844 static void test_get_process_ppid(void) {
848 log_info("/* %s */", __func__
);
850 assert_se(get_process_ppid(1, NULL
) == -EADDRNOTAVAIL
);
852 /* the process with the PID above the global limit definitely doesn't exist. Verify that */
853 assert_se(procfs_tasks_get_limit(&limit
) >= 0);
854 assert_se(limit
>= INT_MAX
|| get_process_ppid(limit
+1, NULL
) == -ESRCH
);
856 for (pid_t pid
= 0;;) {
857 _cleanup_free_
char *c1
= NULL
, *c2
= NULL
;
860 r
= get_process_ppid(pid
, &ppid
);
861 if (r
== -EADDRNOTAVAIL
) {
862 log_info("No further parent PID");
868 assert_se(get_process_cmdline(pid
, SIZE_MAX
, PROCESS_CMDLINE_COMM_FALLBACK
, &c1
) >= 0);
869 assert_se(get_process_cmdline(ppid
, SIZE_MAX
, PROCESS_CMDLINE_COMM_FALLBACK
, &c2
) >= 0);
871 log_info("Parent of " PID_FMT
" (%s) is " PID_FMT
" (%s).", pid
, c1
, ppid
, c2
);
877 int main(int argc
, char *argv
[]) {
878 log_show_color(true);
879 test_setup_logging(LOG_INFO
);
881 save_argc_argv(argc
, argv
);
886 (void) parse_pid(argv
[1], &pid
);
887 test_get_process_comm(pid
);
889 TEST_REQ_RUNNING_SYSTEMD(test_get_process_comm(1));
890 test_get_process_comm(getpid());
893 test_get_process_comm_escape();
894 test_get_process_cmdline();
895 test_pid_is_unwaited();
898 test_get_process_cmdline_harder();
899 test_rename_process();
900 test_getpid_cached();
901 test_getpid_measure();
904 test_ioprio_class_from_to_string();
905 test_setpriority_closest();
906 test_get_process_ppid();