1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include <sys/personality.h>
12 #if HAVE_VALGRIND_VALGRIND_H
13 #include <valgrind/valgrind.h>
16 #include "alloc-util.h"
17 #include "architecture.h"
18 #include "dirent-util.h"
19 #include "errno-list.h"
20 #include "errno-util.h"
22 #include "ioprio-util.h"
25 #include "missing_sched.h"
26 #include "missing_syscall.h"
27 #include "parse-util.h"
28 #include "process-util.h"
29 #include "procfs-util.h"
30 #include "rlimit-util.h"
31 #include "signal-util.h"
32 #include "stdio-util.h"
33 #include "string-util.h"
34 #include "terminal-util.h"
36 #include "user-util.h"
40 static void test_get_process_comm_one(pid_t pid
) {
42 _cleanup_free_
char *a
= NULL
, *c
= NULL
, *d
= NULL
, *f
= NULL
, *i
= NULL
;
43 _cleanup_free_
char *env
= NULL
;
44 char path
[STRLEN("/proc//comm") + DECIMAL_STR_MAX(pid_t
)];
51 log_info("/* %s */", __func__
);
53 xsprintf(path
, "/proc/"PID_FMT
"/comm", pid
);
55 if (stat(path
, &st
) == 0) {
56 assert_se(get_process_comm(pid
, &a
) >= 0);
57 log_info("PID"PID_FMT
" comm: '%s'", pid
, a
);
59 log_warning("%s not exist.", path
);
61 assert_se(get_process_cmdline(pid
, 0, PROCESS_CMDLINE_COMM_FALLBACK
, &c
) >= 0);
62 log_info("PID"PID_FMT
" cmdline: '%s'", pid
, c
);
64 assert_se(get_process_cmdline(pid
, 8, 0, &d
) >= 0);
65 log_info("PID"PID_FMT
" cmdline truncated to 8: '%s'", pid
, d
);
68 assert_se(get_process_cmdline(pid
, 1, 0, &d
) >= 0);
69 log_info("PID"PID_FMT
" cmdline truncated to 1: '%s'", pid
, d
);
71 r
= get_process_ppid(pid
, &e
);
72 assert_se(pid
== 1 ? r
== -EADDRNOTAVAIL
: r
>= 0);
74 log_info("PID"PID_FMT
" PPID: "PID_FMT
, pid
, e
);
78 assert_se(is_kernel_thread(pid
) == 0 || pid
!= 1);
80 r
= get_process_exe(pid
, &f
);
81 assert_se(r
>= 0 || r
== -EACCES
);
82 log_info("PID"PID_FMT
" exe: '%s'", pid
, strna(f
));
84 assert_se(get_process_uid(pid
, &u
) == 0);
85 log_info("PID"PID_FMT
" UID: "UID_FMT
, pid
, u
);
87 assert_se(get_process_gid(pid
, &g
) == 0);
88 log_info("PID"PID_FMT
" GID: "GID_FMT
, pid
, g
);
90 r
= get_process_environ(pid
, &env
);
91 assert_se(r
>= 0 || r
== -EACCES
);
92 log_info("PID"PID_FMT
" strlen(environ): %zi", pid
, env
? (ssize_t
)strlen(env
) : (ssize_t
)-errno
);
94 if (!detect_container())
95 assert_se(get_ctty_devnr(pid
, &h
) == -ENXIO
|| pid
!= 1);
97 (void) getenv_for_pid(pid
, "PATH", &i
);
98 log_info("PID"PID_FMT
" $PATH: '%s'", pid
, strna(i
));
101 TEST(get_process_comm
) {
102 if (saved_argc
> 1) {
105 (void) parse_pid(saved_argv
[1], &pid
);
106 test_get_process_comm_one(pid
);
108 TEST_REQ_RUNNING_SYSTEMD(test_get_process_comm_one(1));
109 test_get_process_comm_one(getpid());
113 static void test_get_process_cmdline_one(pid_t pid
) {
114 _cleanup_free_
char *c
= NULL
, *d
= NULL
, *e
= NULL
, *f
= NULL
, *g
= NULL
, *h
= NULL
;
117 r
= get_process_cmdline(pid
, SIZE_MAX
, 0, &c
);
118 log_info("PID "PID_FMT
": %s", pid
, r
>= 0 ? c
: errno_to_name(r
));
120 r
= get_process_cmdline(pid
, SIZE_MAX
, PROCESS_CMDLINE_COMM_FALLBACK
, &d
);
121 log_info(" %s", r
>= 0 ? d
: errno_to_name(r
));
123 r
= get_process_cmdline(pid
, SIZE_MAX
, PROCESS_CMDLINE_QUOTE
, &e
);
124 log_info(" %s", r
>= 0 ? e
: errno_to_name(r
));
126 r
= get_process_cmdline(pid
, SIZE_MAX
, PROCESS_CMDLINE_QUOTE
| PROCESS_CMDLINE_COMM_FALLBACK
, &f
);
127 log_info(" %s", r
>= 0 ? f
: errno_to_name(r
));
129 r
= get_process_cmdline(pid
, SIZE_MAX
, PROCESS_CMDLINE_QUOTE_POSIX
, &g
);
130 log_info(" %s", r
>= 0 ? g
: errno_to_name(r
));
132 r
= get_process_cmdline(pid
, SIZE_MAX
, PROCESS_CMDLINE_QUOTE_POSIX
| PROCESS_CMDLINE_COMM_FALLBACK
, &h
);
133 log_info(" %s", r
>= 0 ? h
: errno_to_name(r
));
136 TEST(get_process_cmdline
) {
137 _cleanup_closedir_
DIR *d
= NULL
;
140 assert_se(d
= opendir("/proc"));
142 FOREACH_DIRENT(de
, d
, return) {
145 if (de
->d_type
!= DT_DIR
)
148 if (parse_pid(de
->d_name
, &pid
) < 0)
151 test_get_process_cmdline_one(pid
);
155 static void test_get_process_comm_escape_one(const char *input
, const char *output
) {
156 _cleanup_free_
char *n
= NULL
;
158 log_debug("input: <%s> — output: <%s>", input
, output
);
160 assert_se(prctl(PR_SET_NAME
, input
) >= 0);
161 assert_se(get_process_comm(0, &n
) >= 0);
163 log_debug("got: <%s>", n
);
165 assert_se(streq_ptr(n
, output
));
168 TEST(get_process_comm_escape
) {
169 _cleanup_free_
char *saved
= NULL
;
171 assert_se(get_process_comm(0, &saved
) >= 0);
173 test_get_process_comm_escape_one("", "");
174 test_get_process_comm_escape_one("foo", "foo");
175 test_get_process_comm_escape_one("012345678901234", "012345678901234");
176 test_get_process_comm_escape_one("0123456789012345", "012345678901234");
177 test_get_process_comm_escape_one("äöüß", "\\303\\244\\303\\266\\303\\274\\303\\237");
178 test_get_process_comm_escape_one("xäöüß", "x\\303\\244\\303\\266\\303\\274\\303\\237");
179 test_get_process_comm_escape_one("xxäöüß", "xx\\303\\244\\303\\266\\303\\274\\303\\237");
180 test_get_process_comm_escape_one("xxxäöüß", "xxx\\303\\244\\303\\266\\303\\274\\303\\237");
181 test_get_process_comm_escape_one("xxxxäöüß", "xxxx\\303\\244\\303\\266\\303\\274\\303\\237");
182 test_get_process_comm_escape_one("xxxxxäöüß", "xxxxx\\303\\244\\303\\266\\303\\274\\303\\237");
184 assert_se(prctl(PR_SET_NAME
, saved
) >= 0);
187 TEST(pid_is_unwaited
) {
197 waitpid(pid
, &status
, 0);
198 assert_se(!pid_is_unwaited(pid
));
200 assert_se(pid_is_unwaited(getpid_cached()));
201 assert_se(!pid_is_unwaited(-1));
214 waitpid(pid
, &status
, 0);
215 assert_se(!pid_is_alive(pid
));
217 assert_se(pid_is_alive(getpid_cached()));
218 assert_se(!pid_is_alive(-1));
222 assert_se(personality_to_string(PER_LINUX
));
223 assert_se(!personality_to_string(PERSONALITY_INVALID
));
225 assert_se(streq(personality_to_string(PER_LINUX
), architecture_to_string(native_architecture())));
227 assert_se(personality_from_string(personality_to_string(PER_LINUX
)) == PER_LINUX
);
228 assert_se(personality_from_string(architecture_to_string(native_architecture())) == PER_LINUX
);
231 assert_se(streq_ptr(personality_to_string(PER_LINUX
), "x86-64"));
232 assert_se(streq_ptr(personality_to_string(PER_LINUX32
), "x86"));
234 assert_se(personality_from_string("x86-64") == PER_LINUX
);
235 assert_se(personality_from_string("x86") == PER_LINUX32
);
236 assert_se(personality_from_string("ia64") == PERSONALITY_INVALID
);
237 assert_se(personality_from_string(NULL
) == PERSONALITY_INVALID
);
239 assert_se(personality_from_string(personality_to_string(PER_LINUX32
)) == PER_LINUX32
);
243 TEST(get_process_cmdline_harder
) {
244 char path
[] = "/tmp/test-cmdlineXXXXXX";
245 _cleanup_close_
int fd
= -1;
246 _cleanup_free_
char *line
= NULL
;
249 if (geteuid() != 0) {
250 log_info("Skipping %s: not root", __func__
);
254 if (!have_namespaces()) {
255 log_notice("Testing without namespaces, skipping %s", __func__
);
259 #if HAVE_VALGRIND_VALGRIND_H
260 /* valgrind patches open(/proc//cmdline)
261 * so, test_get_process_cmdline_harder fails always
262 * See https://github.com/systemd/systemd/pull/3555#issuecomment-226564908 */
263 if (RUNNING_ON_VALGRIND
) {
264 log_info("Skipping %s: running on valgrind", __func__
);
273 (void) wait_for_terminate(pid
, &si
);
275 assert_se(si
.si_code
== CLD_EXITED
);
276 assert_se(si
.si_status
== 0);
282 assert_se(unshare(CLONE_NEWNS
) >= 0);
284 if (mount(NULL
, "/", NULL
, MS_SLAVE
|MS_REC
, NULL
) < 0) {
285 log_warning_errno(errno
, "mount(..., \"/\", MS_SLAVE|MS_REC, ...) failed: %m");
286 assert_se(IN_SET(errno
, EPERM
, EACCES
));
290 fd
= mkostemp(path
, O_CLOEXEC
);
293 /* Note that we don't unmount the following bind-mount at the end of the test because the kernel
294 * will clear up its /proc/PID/ hierarchy automatically as soon as the test stops. */
295 if (mount(path
, "/proc/self/cmdline", "bind", MS_BIND
, NULL
) < 0) {
296 /* This happens under selinux… Abort the test in this case. */
297 log_warning_errno(errno
, "mount(..., \"/proc/self/cmdline\", \"bind\", ...) failed: %m");
298 assert_se(IN_SET(errno
, EPERM
, EACCES
));
302 /* Set RLIMIT_STACK to infinity to test we don't try to allocate unncessarily large values to read
304 if (setrlimit(RLIMIT_STACK
, &RLIMIT_MAKE_CONST(RLIM_INFINITY
)) < 0)
305 log_warning("Testing without RLIMIT_STACK=infinity");
307 assert_se(unlink(path
) >= 0);
309 assert_se(prctl(PR_SET_NAME
, "testa") >= 0);
311 assert_se(get_process_cmdline(0, SIZE_MAX
, 0, &line
) == -ENOENT
);
313 assert_se(get_process_cmdline(0, SIZE_MAX
, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
314 log_debug("'%s'", line
);
315 assert_se(streq(line
, "[testa]"));
318 assert_se(get_process_cmdline(0, SIZE_MAX
, PROCESS_CMDLINE_COMM_FALLBACK
| PROCESS_CMDLINE_QUOTE
, &line
) >= 0);
319 log_debug("'%s'", line
);
320 assert_se(streq(line
, "\"[testa]\"")); /* quoting is enabled here */
323 assert_se(get_process_cmdline(0, 0, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
324 log_debug("'%s'", line
);
325 assert_se(streq(line
, ""));
328 assert_se(get_process_cmdline(0, 1, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
329 assert_se(streq(line
, "…"));
332 assert_se(get_process_cmdline(0, 2, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
333 assert_se(streq(line
, "[…"));
336 assert_se(get_process_cmdline(0, 3, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
337 assert_se(streq(line
, "[t…"));
340 assert_se(get_process_cmdline(0, 4, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
341 assert_se(streq(line
, "[te…"));
344 assert_se(get_process_cmdline(0, 5, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
345 assert_se(streq(line
, "[tes…"));
348 assert_se(get_process_cmdline(0, 6, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
349 assert_se(streq(line
, "[test…"));
352 assert_se(get_process_cmdline(0, 7, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
353 assert_se(streq(line
, "[testa]"));
356 assert_se(get_process_cmdline(0, 8, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
357 assert_se(streq(line
, "[testa]"));
360 /* Test with multiple arguments that don't require quoting */
362 assert_se(write(fd
, "foo\0bar", 8) == 8);
364 assert_se(get_process_cmdline(0, SIZE_MAX
, 0, &line
) >= 0);
365 log_debug("'%s'", line
);
366 assert_se(streq(line
, "foo bar"));
369 assert_se(get_process_cmdline(0, SIZE_MAX
, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
370 assert_se(streq(line
, "foo bar"));
373 assert_se(write(fd
, "quux", 4) == 4);
374 assert_se(get_process_cmdline(0, SIZE_MAX
, 0, &line
) >= 0);
375 log_debug("'%s'", line
);
376 assert_se(streq(line
, "foo bar quux"));
379 assert_se(get_process_cmdline(0, SIZE_MAX
, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
380 log_debug("'%s'", line
);
381 assert_se(streq(line
, "foo bar quux"));
384 assert_se(get_process_cmdline(0, 1, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
385 log_debug("'%s'", line
);
386 assert_se(streq(line
, "…"));
389 assert_se(get_process_cmdline(0, 2, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
390 log_debug("'%s'", line
);
391 assert_se(streq(line
, "f…"));
394 assert_se(get_process_cmdline(0, 3, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
395 log_debug("'%s'", line
);
396 assert_se(streq(line
, "fo…"));
399 assert_se(get_process_cmdline(0, 4, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
400 log_debug("'%s'", line
);
401 assert_se(streq(line
, "foo…"));
404 assert_se(get_process_cmdline(0, 5, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
405 log_debug("'%s'", line
);
406 assert_se(streq(line
, "foo …"));
409 assert_se(get_process_cmdline(0, 6, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
410 log_debug("'%s'", line
);
411 assert_se(streq(line
, "foo b…"));
414 assert_se(get_process_cmdline(0, 7, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
415 log_debug("'%s'", line
);
416 assert_se(streq(line
, "foo ba…"));
419 assert_se(get_process_cmdline(0, 8, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
420 log_debug("'%s'", line
);
421 assert_se(streq(line
, "foo bar…"));
424 assert_se(get_process_cmdline(0, 9, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
425 log_debug("'%s'", line
);
426 assert_se(streq(line
, "foo bar …"));
429 assert_se(get_process_cmdline(0, 10, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
430 log_debug("'%s'", line
);
431 assert_se(streq(line
, "foo bar q…"));
434 assert_se(get_process_cmdline(0, 11, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
435 log_debug("'%s'", line
);
436 assert_se(streq(line
, "foo bar qu…"));
439 assert_se(get_process_cmdline(0, 12, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
440 log_debug("'%s'", line
);
441 assert_se(streq(line
, "foo bar quux"));
444 assert_se(get_process_cmdline(0, 13, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
445 log_debug("'%s'", line
);
446 assert_se(streq(line
, "foo bar quux"));
449 assert_se(get_process_cmdline(0, 14, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
450 log_debug("'%s'", line
);
451 assert_se(streq(line
, "foo bar quux"));
454 assert_se(get_process_cmdline(0, 1000, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
455 log_debug("'%s'", line
);
456 assert_se(streq(line
, "foo bar quux"));
459 assert_se(ftruncate(fd
, 0) >= 0);
460 assert_se(prctl(PR_SET_NAME
, "aaaa bbbb cccc") >= 0);
462 assert_se(get_process_cmdline(0, SIZE_MAX
, 0, &line
) == -ENOENT
);
464 assert_se(get_process_cmdline(0, SIZE_MAX
, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
465 log_debug("'%s'", line
);
466 assert_se(streq(line
, "[aaaa bbbb cccc]"));
469 assert_se(get_process_cmdline(0, 10, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
470 log_debug("'%s'", line
);
471 assert_se(streq(line
, "[aaaa bbb…"));
474 assert_se(get_process_cmdline(0, 11, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
475 log_debug("'%s'", line
);
476 assert_se(streq(line
, "[aaaa bbbb…"));
479 assert_se(get_process_cmdline(0, 12, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
480 log_debug("'%s'", line
);
481 assert_se(streq(line
, "[aaaa bbbb …"));
484 /* Test with multiple arguments that do require quoting */
486 #define CMDLINE1 "foo\0'bar'\0\"bar$\"\0x y z\0!``\0"
487 #define EXPECT1 "foo \"'bar'\" \"\\\"bar\\$\\\"\" \"x y z\" \"!\\`\\`\""
488 #define EXPECT1p "foo $'\\'bar\\'' $'\"bar$\"' $'x y z' $'!``'"
489 assert_se(lseek(fd
, SEEK_SET
, 0) == 0);
490 assert_se(write(fd
, CMDLINE1
, sizeof CMDLINE1
) == sizeof CMDLINE1
);
491 assert_se(ftruncate(fd
, sizeof CMDLINE1
) == 0);
493 assert_se(get_process_cmdline(0, SIZE_MAX
, PROCESS_CMDLINE_QUOTE
, &line
) >= 0);
494 log_debug("got: ==%s==", line
);
495 log_debug("exp: ==%s==", EXPECT1
);
496 assert_se(streq(line
, EXPECT1
));
499 assert_se(get_process_cmdline(0, SIZE_MAX
, PROCESS_CMDLINE_QUOTE_POSIX
, &line
) >= 0);
500 log_debug("got: ==%s==", line
);
501 log_debug("exp: ==%s==", EXPECT1p
);
502 assert_se(streq(line
, EXPECT1p
));
505 #define CMDLINE2 "foo\0\1\2\3\0\0"
506 #define EXPECT2 "foo \"\\001\\002\\003\""
507 #define EXPECT2p "foo $'\\001\\002\\003'"
508 assert_se(lseek(fd
, SEEK_SET
, 0) == 0);
509 assert_se(write(fd
, CMDLINE2
, sizeof CMDLINE2
) == sizeof CMDLINE2
);
510 assert_se(ftruncate(fd
, sizeof CMDLINE2
) == 0);
512 assert_se(get_process_cmdline(0, SIZE_MAX
, PROCESS_CMDLINE_QUOTE
, &line
) >= 0);
513 log_debug("got: ==%s==", line
);
514 log_debug("exp: ==%s==", EXPECT2
);
515 assert_se(streq(line
, EXPECT2
));
518 assert_se(get_process_cmdline(0, SIZE_MAX
, PROCESS_CMDLINE_QUOTE_POSIX
, &line
) >= 0);
519 log_debug("got: ==%s==", line
);
520 log_debug("exp: ==%s==", EXPECT2p
);
521 assert_se(streq(line
, EXPECT2p
));
528 static void test_rename_process_now(const char *p
, int ret
) {
529 _cleanup_free_
char *comm
= NULL
, *cmdline
= NULL
;
532 log_info("/* %s */", __func__
);
534 r
= rename_process(p
);
535 assert_se(r
== ret
||
536 (ret
== 0 && r
>= 0) ||
539 log_debug_errno(r
, "rename_process(%s): %m", p
);
544 #if HAVE_VALGRIND_VALGRIND_H
545 /* see above, valgrind is weird, we can't verify what we are doing here */
546 if (RUNNING_ON_VALGRIND
)
550 assert_se(get_process_comm(0, &comm
) >= 0);
551 log_debug("comm = <%s>", comm
);
552 assert_se(strneq(comm
, p
, TASK_COMM_LEN
-1));
553 /* We expect comm to be at most 16 bytes (TASK_COMM_LEN). The kernel may raise this limit in the
554 * future. We'd only check the initial part, at least until we recompile, but this will still pass. */
556 r
= get_process_cmdline(0, SIZE_MAX
, 0, &cmdline
);
558 /* we cannot expect cmdline to be renamed properly without privileges */
559 if (geteuid() == 0) {
560 if (r
== 0 && detect_container() > 0)
561 log_info("cmdline = <%s> (not verified, Running in unprivileged container?)", cmdline
);
563 log_info("cmdline = <%s> (expected <%.*s>)", cmdline
, (int) strlen("test-process-util"), p
);
565 bool skip
= cmdline
[0] == '"'; /* A shortcut to check if the string is quoted */
567 assert_se(strneq(cmdline
+ skip
, p
, strlen("test-process-util")));
568 assert_se(startswith(cmdline
+ skip
, p
));
571 log_info("cmdline = <%s> (not verified)", cmdline
);
574 static void test_rename_process_one(const char *p
, int ret
) {
578 log_info("/* %s */", __func__
);
585 test_rename_process_now(p
, ret
);
589 assert_se(wait_for_terminate(pid
, &si
) >= 0);
590 assert_se(si
.si_code
== CLD_EXITED
);
591 assert_se(si
.si_status
== EXIT_SUCCESS
);
594 TEST(rename_process_multi
) {
603 assert_se(wait_for_terminate(pid
, &si
) >= 0);
604 assert_se(si
.si_code
== CLD_EXITED
);
605 assert_se(si
.si_status
== EXIT_SUCCESS
);
611 test_rename_process_now("one", 1);
612 test_rename_process_now("more", 0); /* longer than "one", hence truncated */
613 (void) setresuid(99, 99, 99); /* change uid when running privileged */
614 test_rename_process_now("time!", 0);
615 test_rename_process_now("0", 1); /* shorter than "one", should fit */
616 test_rename_process_one("", -EINVAL
);
617 test_rename_process_one(NULL
, -EINVAL
);
621 TEST(rename_process
) {
622 test_rename_process_one(NULL
, -EINVAL
);
623 test_rename_process_one("", -EINVAL
);
624 test_rename_process_one("foo", 1); /* should always fit */
625 test_rename_process_one("this is a really really long process name, followed by some more words", 0); /* unlikely to fit */
626 test_rename_process_one("1234567", 1); /* should always fit */
629 TEST(getpid_cached
) {
631 pid_t a
, b
, c
, d
, e
, f
, child
;
637 assert_se(a
== b
&& a
== c
);
640 assert_se(child
>= 0);
648 assert_se(a
== b
&& a
== c
);
656 assert_se(a
== d
&& a
== e
&& a
== f
);
658 assert_se(wait_for_terminate(child
, &si
) >= 0);
659 assert_se(si
.si_status
== 0);
660 assert_se(si
.si_code
== CLD_EXITED
);
663 TEST(getpid_measure
) {
666 unsigned long long iterations
= slow_tests_enabled() ? 1000000 : 1000;
668 log_info("/* %s (%llu iterations) */", __func__
, iterations
);
670 t
= now(CLOCK_MONOTONIC
);
671 for (unsigned long long i
= 0; i
< iterations
; i
++)
673 q
= now(CLOCK_MONOTONIC
) - t
;
675 log_info(" glibc getpid(): %lf µs each\n", (double) q
/ iterations
);
677 iterations
*= 50; /* _cached() is about 50 times faster, so we need more iterations */
679 t
= now(CLOCK_MONOTONIC
);
680 for (unsigned long long i
= 0; i
< iterations
; i
++)
681 (void) getpid_cached();
682 q
= now(CLOCK_MONOTONIC
) - t
;
684 log_info("getpid_cached(): %lf µs each\n", (double) q
/ iterations
);
692 BLOCK_SIGNALS(SIGCHLD
);
694 r
= safe_fork("(test-child)", FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_DEATHSIG
|FORK_NULL_STDIO
|FORK_REOPEN_LOG
, &pid
);
699 usleep(100 * USEC_PER_MSEC
);
704 assert_se(wait_for_terminate(pid
, &status
) >= 0);
705 assert_se(status
.si_code
== CLD_EXITED
);
706 assert_se(status
.si_status
== 88);
710 assert_se(PTR_TO_PID(NULL
) == 0);
711 assert_se(PID_TO_PTR(0) == NULL
);
713 assert_se(PTR_TO_PID(PID_TO_PTR(1)) == 1);
714 assert_se(PTR_TO_PID(PID_TO_PTR(2)) == 2);
715 assert_se(PTR_TO_PID(PID_TO_PTR(-1)) == -1);
716 assert_se(PTR_TO_PID(PID_TO_PTR(-2)) == -2);
718 assert_se(PTR_TO_PID(PID_TO_PTR(INT16_MAX
)) == INT16_MAX
);
719 assert_se(PTR_TO_PID(PID_TO_PTR(INT16_MIN
)) == INT16_MIN
);
721 assert_se(PTR_TO_PID(PID_TO_PTR(INT32_MAX
)) == INT32_MAX
);
722 assert_se(PTR_TO_PID(PID_TO_PTR(INT32_MIN
)) == INT32_MIN
);
725 static void test_ioprio_class_from_to_string_one(const char *val
, int expected
, int normalized
) {
726 assert_se(ioprio_class_from_string(val
) == expected
);
728 _cleanup_free_
char *s
= NULL
;
732 assert_se(ioprio_class_to_string_alloc(expected
, &s
) == 0);
733 /* We sometimes get a class number and sometimes a name back */
734 assert_se(streq(s
, val
) ||
735 safe_atou(val
, &ret
) == 0);
737 /* Make sure normalization works, i.e. NONE → BE gets normalized */
738 combined
= ioprio_normalize(ioprio_prio_value(expected
, 0));
739 assert_se(ioprio_prio_class(combined
) == normalized
);
740 assert_se(expected
!= IOPRIO_CLASS_NONE
|| ioprio_prio_data(combined
) == 4);
744 TEST(ioprio_class_from_to_string
) {
745 test_ioprio_class_from_to_string_one("none", IOPRIO_CLASS_NONE
, IOPRIO_CLASS_BE
);
746 test_ioprio_class_from_to_string_one("realtime", IOPRIO_CLASS_RT
, IOPRIO_CLASS_RT
);
747 test_ioprio_class_from_to_string_one("best-effort", IOPRIO_CLASS_BE
, IOPRIO_CLASS_BE
);
748 test_ioprio_class_from_to_string_one("idle", IOPRIO_CLASS_IDLE
, IOPRIO_CLASS_IDLE
);
749 test_ioprio_class_from_to_string_one("0", IOPRIO_CLASS_NONE
, IOPRIO_CLASS_BE
);
750 test_ioprio_class_from_to_string_one("1", 1, 1);
751 test_ioprio_class_from_to_string_one("7", 7, 7);
752 test_ioprio_class_from_to_string_one("8", 8, 8);
753 test_ioprio_class_from_to_string_one("9", -EINVAL
, -EINVAL
);
754 test_ioprio_class_from_to_string_one("-1", -EINVAL
, -EINVAL
);
757 TEST(setpriority_closest
) {
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 TEST(get_process_ppid
) {
848 assert_se(get_process_ppid(1, NULL
) == -EADDRNOTAVAIL
);
850 /* the process with the PID above the global limit definitely doesn't exist. Verify that */
851 assert_se(procfs_get_pid_max(&limit
) >= 0);
852 log_debug("kernel.pid_max = %"PRIu64
, limit
);
854 if (limit
< INT_MAX
) {
855 r
= get_process_ppid(limit
+ 1, NULL
);
856 log_debug_errno(r
, "get_process_limit(%"PRIu64
") → %d/%m", limit
+ 1, r
);
860 for (pid_t pid
= 0;;) {
861 _cleanup_free_
char *c1
= NULL
, *c2
= NULL
;
864 r
= get_process_ppid(pid
, &ppid
);
865 if (r
== -EADDRNOTAVAIL
) {
866 log_info("No further parent PID");
872 assert_se(get_process_cmdline(pid
, SIZE_MAX
, PROCESS_CMDLINE_COMM_FALLBACK
, &c1
) >= 0);
873 assert_se(get_process_cmdline(ppid
, SIZE_MAX
, PROCESS_CMDLINE_COMM_FALLBACK
, &c2
) >= 0);
875 log_info("Parent of " PID_FMT
" (%s) is " PID_FMT
" (%s).", pid
, c1
, ppid
, c2
);
881 TEST(set_oom_score_adjust
) {
884 assert_se(get_oom_score_adjust(&a
) >= 0);
886 r
= set_oom_score_adjust(OOM_SCORE_ADJ_MIN
);
887 assert_se(r
>= 0 || ERRNO_IS_PRIVILEGE(r
));
890 assert_se(get_oom_score_adjust(&b
) >= 0);
891 assert_se(b
== OOM_SCORE_ADJ_MIN
);
894 assert_se(set_oom_score_adjust(a
) >= 0);
895 assert_se(get_oom_score_adjust(&b
) >= 0);
899 DEFINE_CUSTOM_TEST_MAIN(LOG_INFO
, log_show_color(true), /* no outro */);