1 /* SPDX-License-Identifier: LGPL-2.1+ */
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"
21 #include "missing_sched.h"
22 #include "missing_syscall.h"
23 #include "parse-util.h"
24 #include "process-util.h"
25 #include "signal-util.h"
26 #include "stdio-util.h"
27 #include "string-util.h"
28 #include "terminal-util.h"
29 #include "test-helper.h"
34 static void test_get_process_comm(pid_t pid
) {
36 _cleanup_free_
char *a
= NULL
, *c
= NULL
, *d
= NULL
, *f
= NULL
, *i
= NULL
;
37 _cleanup_free_
char *env
= NULL
;
38 char path
[STRLEN("/proc//comm") + DECIMAL_STR_MAX(pid_t
)];
45 xsprintf(path
, "/proc/"PID_FMT
"/comm", pid
);
47 if (stat(path
, &st
) == 0) {
48 assert_se(get_process_comm(pid
, &a
) >= 0);
49 log_info("PID"PID_FMT
" comm: '%s'", pid
, a
);
51 log_warning("%s not exist.", path
);
53 assert_se(get_process_cmdline(pid
, 0, PROCESS_CMDLINE_COMM_FALLBACK
, &c
) >= 0);
54 log_info("PID"PID_FMT
" cmdline: '%s'", pid
, c
);
56 assert_se(get_process_cmdline(pid
, 8, 0, &d
) >= 0);
57 log_info("PID"PID_FMT
" cmdline truncated to 8: '%s'", pid
, d
);
60 assert_se(get_process_cmdline(pid
, 1, 0, &d
) >= 0);
61 log_info("PID"PID_FMT
" cmdline truncated to 1: '%s'", pid
, d
);
63 assert_se(get_process_ppid(pid
, &e
) >= 0);
64 log_info("PID"PID_FMT
" PPID: "PID_FMT
, pid
, e
);
65 assert_se(pid
== 1 ? e
== 0 : e
> 0);
67 assert_se(is_kernel_thread(pid
) == 0 || pid
!= 1);
69 r
= get_process_exe(pid
, &f
);
70 assert_se(r
>= 0 || r
== -EACCES
);
71 log_info("PID"PID_FMT
" exe: '%s'", pid
, strna(f
));
73 assert_se(get_process_uid(pid
, &u
) == 0);
74 log_info("PID"PID_FMT
" UID: "UID_FMT
, pid
, u
);
76 assert_se(get_process_gid(pid
, &g
) == 0);
77 log_info("PID"PID_FMT
" GID: "GID_FMT
, pid
, g
);
79 r
= get_process_environ(pid
, &env
);
80 assert_se(r
>= 0 || r
== -EACCES
);
81 log_info("PID"PID_FMT
" strlen(environ): %zi", pid
, env
? (ssize_t
)strlen(env
) : (ssize_t
)-errno
);
83 if (!detect_container())
84 assert_se(get_ctty_devnr(pid
, &h
) == -ENXIO
|| pid
!= 1);
86 (void) getenv_for_pid(pid
, "PATH", &i
);
87 log_info("PID"PID_FMT
" $PATH: '%s'", pid
, strna(i
));
90 static void test_get_process_comm_escape_one(const char *input
, const char *output
) {
91 _cleanup_free_
char *n
= NULL
;
93 log_info("input: <%s> — output: <%s>", input
, output
);
95 assert_se(prctl(PR_SET_NAME
, input
) >= 0);
96 assert_se(get_process_comm(0, &n
) >= 0);
98 log_info("got: <%s>", n
);
100 assert_se(streq_ptr(n
, output
));
103 static void test_get_process_comm_escape(void) {
104 _cleanup_free_
char *saved
= NULL
;
106 assert_se(get_process_comm(0, &saved
) >= 0);
108 test_get_process_comm_escape_one("", "");
109 test_get_process_comm_escape_one("foo", "foo");
110 test_get_process_comm_escape_one("012345678901234", "012345678901234");
111 test_get_process_comm_escape_one("0123456789012345", "012345678901234");
112 test_get_process_comm_escape_one("äöüß", "\\303\\244\\303\\266\\303\\274\\303\\237");
113 test_get_process_comm_escape_one("xäöüß", "x\\303\\244\\303\\266\\303\\274\\303\\237");
114 test_get_process_comm_escape_one("xxäöüß", "xx\\303\\244\\303\\266\\303\\274\\303\\237");
115 test_get_process_comm_escape_one("xxxäöüß", "xxx\\303\\244\\303\\266\\303\\274\\303\\237");
116 test_get_process_comm_escape_one("xxxxäöüß", "xxxx\\303\\244\\303\\266\\303\\274\\303\\237");
117 test_get_process_comm_escape_one("xxxxxäöüß", "xxxxx\\303\\244\\303\\266\\303\\274\\303\\237");
119 assert_se(prctl(PR_SET_NAME
, saved
) >= 0);
122 static void test_pid_is_unwaited(void) {
132 waitpid(pid
, &status
, 0);
133 assert_se(!pid_is_unwaited(pid
));
135 assert_se(pid_is_unwaited(getpid_cached()));
136 assert_se(!pid_is_unwaited(-1));
139 static void test_pid_is_alive(void) {
149 waitpid(pid
, &status
, 0);
150 assert_se(!pid_is_alive(pid
));
152 assert_se(pid_is_alive(getpid_cached()));
153 assert_se(!pid_is_alive(-1));
156 static void test_personality(void) {
158 assert_se(personality_to_string(PER_LINUX
));
159 assert_se(!personality_to_string(PERSONALITY_INVALID
));
161 assert_se(streq(personality_to_string(PER_LINUX
), architecture_to_string(native_architecture())));
163 assert_se(personality_from_string(personality_to_string(PER_LINUX
)) == PER_LINUX
);
164 assert_se(personality_from_string(architecture_to_string(native_architecture())) == PER_LINUX
);
167 assert_se(streq_ptr(personality_to_string(PER_LINUX
), "x86-64"));
168 assert_se(streq_ptr(personality_to_string(PER_LINUX32
), "x86"));
170 assert_se(personality_from_string("x86-64") == PER_LINUX
);
171 assert_se(personality_from_string("x86") == PER_LINUX32
);
172 assert_se(personality_from_string("ia64") == PERSONALITY_INVALID
);
173 assert_se(personality_from_string(NULL
) == PERSONALITY_INVALID
);
175 assert_se(personality_from_string(personality_to_string(PER_LINUX32
)) == PER_LINUX32
);
179 static void test_get_process_cmdline_harder(void) {
180 char path
[] = "/tmp/test-cmdlineXXXXXX";
181 _cleanup_close_
int fd
= -1;
182 _cleanup_free_
char *line
= NULL
;
185 if (geteuid() != 0) {
186 log_info("Skipping %s: not root", __func__
);
190 if (!have_namespaces()) {
191 log_notice("Testing without namespaces, skipping %s", __func__
);
195 #if HAVE_VALGRIND_VALGRIND_H
196 /* valgrind patches open(/proc//cmdline)
197 * so, test_get_process_cmdline_harder fails always
198 * See https://github.com/systemd/systemd/pull/3555#issuecomment-226564908 */
199 if (RUNNING_ON_VALGRIND
) {
200 log_info("Skipping %s: running on valgrind", __func__
);
209 (void) wait_for_terminate(pid
, &si
);
211 assert_se(si
.si_code
== CLD_EXITED
);
212 assert_se(si
.si_status
== 0);
218 assert_se(unshare(CLONE_NEWNS
) >= 0);
220 if (mount(NULL
, "/", NULL
, MS_SLAVE
|MS_REC
, NULL
) < 0) {
221 log_warning_errno(errno
, "mount(..., \"/\", MS_SLAVE|MS_REC, ...) failed: %m");
222 assert_se(IN_SET(errno
, EPERM
, EACCES
));
226 fd
= mkostemp(path
, O_CLOEXEC
);
229 /* Note that we don't unmount the following bind-mount at the end of the test because the kernel
230 * will clear up its /proc/PID/ hierarchy automatically as soon as the test stops. */
231 if (mount(path
, "/proc/self/cmdline", "bind", MS_BIND
, NULL
) < 0) {
232 /* This happens under selinux… Abort the test in this case. */
233 log_warning_errno(errno
, "mount(..., \"/proc/self/cmdline\", \"bind\", ...) failed: %m");
234 assert_se(IN_SET(errno
, EPERM
, EACCES
));
238 assert_se(unlink(path
) >= 0);
240 assert_se(prctl(PR_SET_NAME
, "testa") >= 0);
242 assert_se(get_process_cmdline(getpid_cached(), SIZE_MAX
, 0, &line
) == -ENOENT
);
244 assert_se(get_process_cmdline(getpid_cached(), SIZE_MAX
, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
245 assert_se(streq(line
, "[testa]"));
248 assert_se(get_process_cmdline(getpid_cached(), 0, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
249 log_info("'%s'", line
);
250 assert_se(streq(line
, ""));
253 assert_se(get_process_cmdline(getpid_cached(), 1, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
254 assert_se(streq(line
, "…"));
257 assert_se(get_process_cmdline(getpid_cached(), 2, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
258 assert_se(streq(line
, "[…"));
261 assert_se(get_process_cmdline(getpid_cached(), 3, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
262 assert_se(streq(line
, "[t…"));
265 assert_se(get_process_cmdline(getpid_cached(), 4, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
266 assert_se(streq(line
, "[te…"));
269 assert_se(get_process_cmdline(getpid_cached(), 5, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
270 assert_se(streq(line
, "[tes…"));
273 assert_se(get_process_cmdline(getpid_cached(), 6, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
274 assert_se(streq(line
, "[test…"));
277 assert_se(get_process_cmdline(getpid_cached(), 7, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
278 assert_se(streq(line
, "[testa]"));
281 assert_se(get_process_cmdline(getpid_cached(), 8, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
282 assert_se(streq(line
, "[testa]"));
285 assert_se(write(fd
, "foo\0bar", 8) == 8);
287 assert_se(get_process_cmdline(getpid_cached(), SIZE_MAX
, 0, &line
) >= 0);
288 log_info("'%s'", line
);
289 assert_se(streq(line
, "foo bar"));
292 assert_se(get_process_cmdline(getpid_cached(), SIZE_MAX
, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
293 assert_se(streq(line
, "foo bar"));
296 assert_se(write(fd
, "quux", 4) == 4);
297 assert_se(get_process_cmdline(getpid_cached(), SIZE_MAX
, 0, &line
) >= 0);
298 log_info("'%s'", line
);
299 assert_se(streq(line
, "foo bar quux"));
302 assert_se(get_process_cmdline(getpid_cached(), SIZE_MAX
, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
303 assert_se(streq(line
, "foo bar quux"));
306 assert_se(get_process_cmdline(getpid_cached(), 1, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
307 assert_se(streq(line
, "…"));
310 assert_se(get_process_cmdline(getpid_cached(), 2, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
311 assert_se(streq(line
, "f…"));
314 assert_se(get_process_cmdline(getpid_cached(), 3, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
315 assert_se(streq(line
, "fo…"));
318 assert_se(get_process_cmdline(getpid_cached(), 4, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
319 assert_se(streq(line
, "foo…"));
322 assert_se(get_process_cmdline(getpid_cached(), 5, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
323 assert_se(streq(line
, "foo …"));
326 assert_se(get_process_cmdline(getpid_cached(), 6, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
327 assert_se(streq(line
, "foo b…"));
330 assert_se(get_process_cmdline(getpid_cached(), 7, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
331 assert_se(streq(line
, "foo ba…"));
334 assert_se(get_process_cmdline(getpid_cached(), 8, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
335 assert_se(streq(line
, "foo bar…"));
338 assert_se(get_process_cmdline(getpid_cached(), 9, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
339 assert_se(streq(line
, "foo bar …"));
342 assert_se(get_process_cmdline(getpid_cached(), 10, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
343 assert_se(streq(line
, "foo bar q…"));
346 assert_se(get_process_cmdline(getpid_cached(), 11, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
347 assert_se(streq(line
, "foo bar qu…"));
350 assert_se(get_process_cmdline(getpid_cached(), 12, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
351 assert_se(streq(line
, "foo bar quux"));
354 assert_se(get_process_cmdline(getpid_cached(), 13, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
355 assert_se(streq(line
, "foo bar quux"));
358 assert_se(get_process_cmdline(getpid_cached(), 14, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
359 assert_se(streq(line
, "foo bar quux"));
362 assert_se(get_process_cmdline(getpid_cached(), 1000, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
363 assert_se(streq(line
, "foo bar quux"));
366 assert_se(ftruncate(fd
, 0) >= 0);
367 assert_se(prctl(PR_SET_NAME
, "aaaa bbbb cccc") >= 0);
369 assert_se(get_process_cmdline(getpid_cached(), SIZE_MAX
, 0, &line
) == -ENOENT
);
371 assert_se(get_process_cmdline(getpid_cached(), SIZE_MAX
, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
372 assert_se(streq(line
, "[aaaa bbbb cccc]"));
375 assert_se(get_process_cmdline(getpid_cached(), 10, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
376 assert_se(streq(line
, "[aaaa bbb…"));
379 assert_se(get_process_cmdline(getpid_cached(), 11, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
380 assert_se(streq(line
, "[aaaa bbbb…"));
383 assert_se(get_process_cmdline(getpid_cached(), 12, PROCESS_CMDLINE_COMM_FALLBACK
, &line
) >= 0);
384 assert_se(streq(line
, "[aaaa bbbb …"));
391 static void test_rename_process_now(const char *p
, int ret
) {
392 _cleanup_free_
char *comm
= NULL
, *cmdline
= NULL
;
395 r
= rename_process(p
);
396 assert_se(r
== ret
||
397 (ret
== 0 && r
>= 0) ||
403 #if HAVE_VALGRIND_VALGRIND_H
404 /* see above, valgrind is weird, we can't verify what we are doing here */
405 if (RUNNING_ON_VALGRIND
)
409 assert_se(get_process_comm(0, &comm
) >= 0);
410 log_info("comm = <%s>", comm
);
411 assert_se(strneq(comm
, p
, TASK_COMM_LEN
-1));
412 /* We expect comm to be at most 16 bytes (TASK_COMM_LEN). The kernel may raise this limit in the
413 * future. We'd only check the initial part, at least until we recompile, but this will still pass. */
415 r
= get_process_cmdline(0, SIZE_MAX
, 0, &cmdline
);
417 /* we cannot expect cmdline to be renamed properly without privileges */
418 if (geteuid() == 0) {
419 if (r
== 0 && detect_container() > 0)
420 log_info("cmdline = <%s> (not verified, Running in unprivileged container?)", cmdline
);
422 log_info("cmdline = <%s>", cmdline
);
423 assert_se(strneq(p
, cmdline
, STRLEN("test-process-util")));
424 assert_se(startswith(p
, cmdline
));
427 log_info("cmdline = <%s> (not verified)", cmdline
);
430 static void test_rename_process_one(const char *p
, int ret
) {
439 test_rename_process_now(p
, ret
);
443 assert_se(wait_for_terminate(pid
, &si
) >= 0);
444 assert_se(si
.si_code
== CLD_EXITED
);
445 assert_se(si
.si_status
== EXIT_SUCCESS
);
448 static void test_rename_process_multi(void) {
457 assert_se(wait_for_terminate(pid
, &si
) >= 0);
458 assert_se(si
.si_code
== CLD_EXITED
);
459 assert_se(si
.si_status
== EXIT_SUCCESS
);
465 test_rename_process_now("one", 1);
466 test_rename_process_now("more", 0); /* longer than "one", hence truncated */
467 (void) setresuid(99, 99, 99); /* change uid when running privileged */
468 test_rename_process_now("time!", 0);
469 test_rename_process_now("0", 1); /* shorter than "one", should fit */
470 test_rename_process_one("", -EINVAL
);
471 test_rename_process_one(NULL
, -EINVAL
);
475 static void test_rename_process(void) {
476 test_rename_process_one(NULL
, -EINVAL
);
477 test_rename_process_one("", -EINVAL
);
478 test_rename_process_one("foo", 1); /* should always fit */
479 test_rename_process_one("this is a really really long process name, followed by some more words", 0); /* unlikely to fit */
480 test_rename_process_one("1234567", 1); /* should always fit */
481 test_rename_process_multi(); /* multiple invocations and dropped privileges */
484 static void test_getpid_cached(void) {
486 pid_t a
, b
, c
, d
, e
, f
, child
;
492 assert_se(a
== b
&& a
== c
);
495 assert_se(child
>= 0);
503 assert_se(a
== b
&& a
== c
);
511 assert_se(a
== d
&& a
== e
&& a
== f
);
513 assert_se(wait_for_terminate(child
, &si
) >= 0);
514 assert_se(si
.si_status
== 0);
515 assert_se(si
.si_code
== CLD_EXITED
);
518 #define MEASURE_ITERATIONS (10000000LLU)
520 static void test_getpid_measure(void) {
521 unsigned long long i
;
524 t
= now(CLOCK_MONOTONIC
);
525 for (i
= 0; i
< MEASURE_ITERATIONS
; i
++)
527 q
= now(CLOCK_MONOTONIC
) - t
;
529 log_info(" glibc getpid(): %lf µs each\n", (double) q
/ MEASURE_ITERATIONS
);
531 t
= now(CLOCK_MONOTONIC
);
532 for (i
= 0; i
< MEASURE_ITERATIONS
; i
++)
533 (void) getpid_cached();
534 q
= now(CLOCK_MONOTONIC
) - t
;
536 log_info("getpid_cached(): %lf µs each\n", (double) q
/ MEASURE_ITERATIONS
);
539 static void test_safe_fork(void) {
544 BLOCK_SIGNALS(SIGCHLD
);
546 r
= safe_fork("(test-child)", FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_DEATHSIG
|FORK_NULL_STDIO
|FORK_REOPEN_LOG
, &pid
);
551 usleep(100 * USEC_PER_MSEC
);
556 assert_se(wait_for_terminate(pid
, &status
) >= 0);
557 assert_se(status
.si_code
== CLD_EXITED
);
558 assert_se(status
.si_status
== 88);
561 static void test_pid_to_ptr(void) {
563 assert_se(PTR_TO_PID(NULL
) == 0);
564 assert_se(PID_TO_PTR(0) == NULL
);
566 assert_se(PTR_TO_PID(PID_TO_PTR(1)) == 1);
567 assert_se(PTR_TO_PID(PID_TO_PTR(2)) == 2);
568 assert_se(PTR_TO_PID(PID_TO_PTR(-1)) == -1);
569 assert_se(PTR_TO_PID(PID_TO_PTR(-2)) == -2);
571 assert_se(PTR_TO_PID(PID_TO_PTR(INT16_MAX
)) == INT16_MAX
);
572 assert_se(PTR_TO_PID(PID_TO_PTR(INT16_MIN
)) == INT16_MIN
);
574 #if SIZEOF_PID_T >= 4
575 assert_se(PTR_TO_PID(PID_TO_PTR(INT32_MAX
)) == INT32_MAX
);
576 assert_se(PTR_TO_PID(PID_TO_PTR(INT32_MIN
)) == INT32_MIN
);
580 static void test_ioprio_class_from_to_string_one(const char *val
, int expected
) {
581 assert_se(ioprio_class_from_string(val
) == expected
);
583 _cleanup_free_
char *s
= NULL
;
586 assert_se(ioprio_class_to_string_alloc(expected
, &s
) == 0);
587 /* We sometimes get a class number and sometimes a number back */
588 assert_se(streq(s
, val
) ||
589 safe_atou(val
, &ret
) == 0);
593 static void test_ioprio_class_from_to_string(void) {
594 test_ioprio_class_from_to_string_one("none", IOPRIO_CLASS_NONE
);
595 test_ioprio_class_from_to_string_one("realtime", IOPRIO_CLASS_RT
);
596 test_ioprio_class_from_to_string_one("best-effort", IOPRIO_CLASS_BE
);
597 test_ioprio_class_from_to_string_one("idle", IOPRIO_CLASS_IDLE
);
598 test_ioprio_class_from_to_string_one("0", 0);
599 test_ioprio_class_from_to_string_one("1", 1);
600 test_ioprio_class_from_to_string_one("7", 7);
601 test_ioprio_class_from_to_string_one("8", 8);
602 test_ioprio_class_from_to_string_one("9", -1);
603 test_ioprio_class_from_to_string_one("-1", -1);
606 int main(int argc
, char *argv
[]) {
607 test_setup_logging(LOG_DEBUG
);
609 save_argc_argv(argc
, argv
);
614 (void) parse_pid(argv
[1], &pid
);
615 test_get_process_comm(pid
);
617 TEST_REQ_RUNNING_SYSTEMD(test_get_process_comm(1));
618 test_get_process_comm(getpid());
621 test_get_process_comm_escape();
622 test_pid_is_unwaited();
625 test_get_process_cmdline_harder();
626 test_rename_process();
627 test_getpid_cached();
628 test_getpid_measure();
631 test_ioprio_class_from_to_string();