]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-process-util.c
44df43024647208094ef3276877b7f3aec774da2
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2010 Lennart Poettering
4 Copyright 2013 Thomas H.P. Andersen
9 #include <sys/personality.h>
10 #include <sys/prctl.h>
12 #include <sys/types.h>
15 #if HAVE_VALGRIND_VALGRIND_H
16 #include <valgrind/valgrind.h>
19 #include "alloc-util.h"
20 #include "architecture.h"
24 #include "parse-util.h"
25 #include "process-util.h"
26 #include "signal-util.h"
27 #include "stdio-util.h"
28 #include "string-util.h"
29 #include "terminal-util.h"
30 #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, true, &c
) >= 0);
54 log_info("PID"PID_FMT
" cmdline: '%s'", pid
, c
);
56 assert_se(get_process_cmdline(pid
, 8, false, &d
) >= 0);
57 log_info("PID"PID_FMT
" cmdline truncated to 8: '%s'", pid
, d
);
60 assert_se(get_process_cmdline(pid
, 1, false, &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
);
75 assert_se(u
== 0 || pid
!= 1);
77 assert_se(get_process_gid(pid
, &g
) == 0);
78 log_info("PID"PID_FMT
" GID: "GID_FMT
, pid
, g
);
79 assert_se(g
== 0 || pid
!= 1);
81 r
= get_process_environ(pid
, &env
);
82 assert_se(r
>= 0 || r
== -EACCES
);
83 log_info("PID"PID_FMT
" strlen(environ): %zi", pid
, env
? (ssize_t
)strlen(env
) : (ssize_t
)-errno
);
85 if (!detect_container())
86 assert_se(get_ctty_devnr(pid
, &h
) == -ENXIO
|| pid
!= 1);
88 (void) getenv_for_pid(pid
, "PATH", &i
);
89 log_info("PID"PID_FMT
" $PATH: '%s'", pid
, strna(i
));
92 static void test_get_process_comm_escape_one(const char *input
, const char *output
) {
93 _cleanup_free_
char *n
= NULL
;
95 log_info("input: <%s> — output: <%s>", input
, output
);
97 assert_se(prctl(PR_SET_NAME
, input
) >= 0);
98 assert_se(get_process_comm(0, &n
) >= 0);
100 log_info("got: <%s>", n
);
102 assert_se(streq_ptr(n
, output
));
105 static void test_get_process_comm_escape(void) {
106 _cleanup_free_
char *saved
= NULL
;
108 assert_se(get_process_comm(0, &saved
) >= 0);
110 test_get_process_comm_escape_one("", "");
111 test_get_process_comm_escape_one("foo", "foo");
112 test_get_process_comm_escape_one("012345678901234", "012345678901234");
113 test_get_process_comm_escape_one("0123456789012345", "012345678901234");
114 test_get_process_comm_escape_one("äöüß", "\\303\\244\\303…");
115 test_get_process_comm_escape_one("xäöüß", "x\\303\\244…");
116 test_get_process_comm_escape_one("xxäöüß", "xx\\303\\244…");
117 test_get_process_comm_escape_one("xxxäöüß", "xxx\\303\\244…");
118 test_get_process_comm_escape_one("xxxxäöüß", "xxxx\\303\\244…");
119 test_get_process_comm_escape_one("xxxxxäöüß", "xxxxx\\303…");
121 assert_se(prctl(PR_SET_NAME
, saved
) >= 0);
124 static void test_pid_is_unwaited(void) {
134 waitpid(pid
, &status
, 0);
135 assert_se(!pid_is_unwaited(pid
));
137 assert_se(pid_is_unwaited(getpid_cached()));
138 assert_se(!pid_is_unwaited(-1));
141 static void test_pid_is_alive(void) {
151 waitpid(pid
, &status
, 0);
152 assert_se(!pid_is_alive(pid
));
154 assert_se(pid_is_alive(getpid_cached()));
155 assert_se(!pid_is_alive(-1));
158 static void test_personality(void) {
160 assert_se(personality_to_string(PER_LINUX
));
161 assert_se(!personality_to_string(PERSONALITY_INVALID
));
163 assert_se(streq(personality_to_string(PER_LINUX
), architecture_to_string(native_architecture())));
165 assert_se(personality_from_string(personality_to_string(PER_LINUX
)) == PER_LINUX
);
166 assert_se(personality_from_string(architecture_to_string(native_architecture())) == PER_LINUX
);
169 assert_se(streq_ptr(personality_to_string(PER_LINUX
), "x86-64"));
170 assert_se(streq_ptr(personality_to_string(PER_LINUX32
), "x86"));
172 assert_se(personality_from_string("x86-64") == PER_LINUX
);
173 assert_se(personality_from_string("x86") == PER_LINUX32
);
174 assert_se(personality_from_string("ia64") == PERSONALITY_INVALID
);
175 assert_se(personality_from_string(NULL
) == PERSONALITY_INVALID
);
177 assert_se(personality_from_string(personality_to_string(PER_LINUX32
)) == PER_LINUX32
);
181 static void test_get_process_cmdline_harder(void) {
182 char path
[] = "/tmp/test-cmdlineXXXXXX";
183 _cleanup_close_
int fd
= -1;
184 _cleanup_free_
char *line
= NULL
;
190 #if HAVE_VALGRIND_VALGRIND_H
191 /* valgrind patches open(/proc//cmdline)
192 * so, test_get_process_cmdline_harder fails always
193 * See https://github.com/systemd/systemd/pull/3555#issuecomment-226564908 */
194 if (RUNNING_ON_VALGRIND
)
202 (void) wait_for_terminate(pid
, &si
);
204 assert_se(si
.si_code
== CLD_EXITED
);
205 assert_se(si
.si_status
== 0);
211 assert_se(unshare(CLONE_NEWNS
) >= 0);
213 assert_se(mount(NULL
, "/", NULL
, MS_PRIVATE
|MS_REC
, NULL
) >= 0);
215 fd
= mkostemp(path
, O_CLOEXEC
);
218 if (mount(path
, "/proc/self/cmdline", "bind", MS_BIND
, NULL
) < 0) {
219 /* This happens under selinux… Abort the test in this case. */
220 log_warning_errno(errno
, "mount(..., \"/proc/self/cmdline\", \"bind\", ...) failed: %m");
221 assert(errno
== EACCES
);
225 assert_se(unlink(path
) >= 0);
227 assert_se(prctl(PR_SET_NAME
, "testa") >= 0);
229 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line
) == -ENOENT
);
231 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line
) >= 0);
232 assert_se(streq(line
, "[testa]"));
235 assert_se(get_process_cmdline(getpid_cached(), 1, true, &line
) >= 0);
236 assert_se(streq(line
, ""));
239 assert_se(get_process_cmdline(getpid_cached(), 2, true, &line
) >= 0);
240 assert_se(streq(line
, "["));
243 assert_se(get_process_cmdline(getpid_cached(), 3, true, &line
) >= 0);
244 assert_se(streq(line
, "[."));
247 assert_se(get_process_cmdline(getpid_cached(), 4, true, &line
) >= 0);
248 assert_se(streq(line
, "[.."));
251 assert_se(get_process_cmdline(getpid_cached(), 5, true, &line
) >= 0);
252 assert_se(streq(line
, "[..."));
255 assert_se(get_process_cmdline(getpid_cached(), 6, true, &line
) >= 0);
256 assert_se(streq(line
, "[...]"));
259 assert_se(get_process_cmdline(getpid_cached(), 7, true, &line
) >= 0);
260 assert_se(streq(line
, "[t...]"));
263 assert_se(get_process_cmdline(getpid_cached(), 8, true, &line
) >= 0);
264 assert_se(streq(line
, "[testa]"));
267 assert_se(write(fd
, "\0\0\0\0\0\0\0\0\0", 10) == 10);
269 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line
) == -ENOENT
);
271 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line
) >= 0);
272 assert_se(streq(line
, "[testa]"));
275 assert_se(write(fd
, "foo\0bar\0\0\0\0\0", 10) == 10);
277 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line
) >= 0);
278 assert_se(streq(line
, "foo bar"));
281 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line
) >= 0);
282 assert_se(streq(line
, "foo bar"));
285 assert_se(write(fd
, "quux", 4) == 4);
286 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line
) >= 0);
287 assert_se(streq(line
, "foo bar quux"));
290 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line
) >= 0);
291 assert_se(streq(line
, "foo bar quux"));
294 assert_se(get_process_cmdline(getpid_cached(), 1, true, &line
) >= 0);
295 assert_se(streq(line
, ""));
298 assert_se(get_process_cmdline(getpid_cached(), 2, true, &line
) >= 0);
299 assert_se(streq(line
, "."));
302 assert_se(get_process_cmdline(getpid_cached(), 3, true, &line
) >= 0);
303 assert_se(streq(line
, ".."));
306 assert_se(get_process_cmdline(getpid_cached(), 4, true, &line
) >= 0);
307 assert_se(streq(line
, "..."));
310 assert_se(get_process_cmdline(getpid_cached(), 5, true, &line
) >= 0);
311 assert_se(streq(line
, "f..."));
314 assert_se(get_process_cmdline(getpid_cached(), 6, true, &line
) >= 0);
315 assert_se(streq(line
, "fo..."));
318 assert_se(get_process_cmdline(getpid_cached(), 7, true, &line
) >= 0);
319 assert_se(streq(line
, "foo..."));
322 assert_se(get_process_cmdline(getpid_cached(), 8, true, &line
) >= 0);
323 assert_se(streq(line
, "foo..."));
326 assert_se(get_process_cmdline(getpid_cached(), 9, true, &line
) >= 0);
327 assert_se(streq(line
, "foo b..."));
330 assert_se(get_process_cmdline(getpid_cached(), 10, true, &line
) >= 0);
331 assert_se(streq(line
, "foo ba..."));
334 assert_se(get_process_cmdline(getpid_cached(), 11, true, &line
) >= 0);
335 assert_se(streq(line
, "foo bar..."));
338 assert_se(get_process_cmdline(getpid_cached(), 12, true, &line
) >= 0);
339 assert_se(streq(line
, "foo bar..."));
342 assert_se(get_process_cmdline(getpid_cached(), 13, true, &line
) >= 0);
343 assert_se(streq(line
, "foo bar quux"));
346 assert_se(get_process_cmdline(getpid_cached(), 14, true, &line
) >= 0);
347 assert_se(streq(line
, "foo bar quux"));
350 assert_se(get_process_cmdline(getpid_cached(), 1000, true, &line
) >= 0);
351 assert_se(streq(line
, "foo bar quux"));
354 assert_se(ftruncate(fd
, 0) >= 0);
355 assert_se(prctl(PR_SET_NAME
, "aaaa bbbb cccc") >= 0);
357 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line
) == -ENOENT
);
359 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line
) >= 0);
360 assert_se(streq(line
, "[aaaa bbbb cccc]"));
363 assert_se(get_process_cmdline(getpid_cached(), 10, true, &line
) >= 0);
364 assert_se(streq(line
, "[aaaa...]"));
367 assert_se(get_process_cmdline(getpid_cached(), 11, true, &line
) >= 0);
368 assert_se(streq(line
, "[aaaa...]"));
371 assert_se(get_process_cmdline(getpid_cached(), 12, true, &line
) >= 0);
372 assert_se(streq(line
, "[aaaa b...]"));
379 static void test_rename_process_now(const char *p
, int ret
) {
380 _cleanup_free_
char *comm
= NULL
, *cmdline
= NULL
;
383 r
= rename_process(p
);
384 assert_se(r
== ret
||
385 (ret
== 0 && r
>= 0) ||
391 #if HAVE_VALGRIND_VALGRIND_H
392 /* see above, valgrind is weird, we can't verify what we are doing here */
393 if (RUNNING_ON_VALGRIND
)
397 assert_se(get_process_comm(0, &comm
) >= 0);
398 log_info("comm = <%s>", comm
);
399 assert_se(strneq(comm
, p
, TASK_COMM_LEN
-1));
401 assert_se(get_process_cmdline(0, 0, false, &cmdline
) >= 0);
402 /* we cannot expect cmdline to be renamed properly without privileges */
403 if (geteuid() == 0) {
404 log_info("cmdline = <%s>", cmdline
);
405 assert_se(strneq(p
, cmdline
, STRLEN("test-process-util")));
406 assert_se(startswith(p
, cmdline
));
408 log_info("cmdline = <%s> (not verified)", cmdline
);
411 static void test_rename_process_one(const char *p
, int ret
) {
420 test_rename_process_now(p
, ret
);
424 assert_se(wait_for_terminate(pid
, &si
) >= 0);
425 assert_se(si
.si_code
== CLD_EXITED
);
426 assert_se(si
.si_status
== EXIT_SUCCESS
);
429 static void test_rename_process_multi(void) {
438 assert_se(wait_for_terminate(pid
, &si
) >= 0);
439 assert_se(si
.si_code
== CLD_EXITED
);
440 assert_se(si
.si_status
== EXIT_SUCCESS
);
446 test_rename_process_now("one", 1);
447 test_rename_process_now("more", 0); /* longer than "one", hence truncated */
448 (void) setresuid(99, 99, 99); /* change uid when running privileged */
449 test_rename_process_now("time!", 0);
450 test_rename_process_now("0", 1); /* shorter than "one", should fit */
451 test_rename_process_one("", -EINVAL
);
452 test_rename_process_one(NULL
, -EINVAL
);
456 static void test_rename_process(void) {
457 test_rename_process_one(NULL
, -EINVAL
);
458 test_rename_process_one("", -EINVAL
);
459 test_rename_process_one("foo", 1); /* should always fit */
460 test_rename_process_one("this is a really really long process name, followed by some more words", 0); /* unlikely to fit */
461 test_rename_process_one("1234567", 1); /* should always fit */
462 test_rename_process_multi(); /* multiple invocations and dropped privileges */
465 static void test_getpid_cached(void) {
467 pid_t a
, b
, c
, d
, e
, f
, child
;
473 assert_se(a
== b
&& a
== c
);
476 assert_se(child
>= 0);
484 assert_se(a
== b
&& a
== c
);
492 assert_se(a
== d
&& a
== e
&& a
== f
);
494 assert_se(wait_for_terminate(child
, &si
) >= 0);
495 assert_se(si
.si_status
== 0);
496 assert_se(si
.si_code
== CLD_EXITED
);
499 #define MEASURE_ITERATIONS (10000000LLU)
501 static void test_getpid_measure(void) {
502 unsigned long long i
;
505 t
= now(CLOCK_MONOTONIC
);
506 for (i
= 0; i
< MEASURE_ITERATIONS
; i
++)
508 q
= now(CLOCK_MONOTONIC
) - t
;
510 log_info(" glibc getpid(): %llu/s\n", (unsigned long long) (MEASURE_ITERATIONS
*USEC_PER_SEC
/q
));
512 t
= now(CLOCK_MONOTONIC
);
513 for (i
= 0; i
< MEASURE_ITERATIONS
; i
++)
514 (void) getpid_cached();
515 q
= now(CLOCK_MONOTONIC
) - t
;
517 log_info("getpid_cached(): %llu/s\n", (unsigned long long) (MEASURE_ITERATIONS
*USEC_PER_SEC
/q
));
520 static void test_safe_fork(void) {
525 BLOCK_SIGNALS(SIGCHLD
);
527 r
= safe_fork("(test-child)", FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_DEATHSIG
|FORK_NULL_STDIO
|FORK_REOPEN_LOG
, &pid
);
532 usleep(100 * USEC_PER_MSEC
);
537 assert_se(wait_for_terminate(pid
, &status
) >= 0);
538 assert_se(status
.si_code
== CLD_EXITED
);
539 assert_se(status
.si_status
== 88);
542 static void test_pid_to_ptr(void) {
544 assert_se(PTR_TO_PID(NULL
) == 0);
545 assert_se(PID_TO_PTR(0) == NULL
);
547 assert_se(PTR_TO_PID(PID_TO_PTR(1)) == 1);
548 assert_se(PTR_TO_PID(PID_TO_PTR(2)) == 2);
549 assert_se(PTR_TO_PID(PID_TO_PTR(-1)) == -1);
550 assert_se(PTR_TO_PID(PID_TO_PTR(-2)) == -2);
552 assert_se(PTR_TO_PID(PID_TO_PTR(INT16_MAX
)) == INT16_MAX
);
553 assert_se(PTR_TO_PID(PID_TO_PTR(INT16_MIN
)) == INT16_MIN
);
555 #if SIZEOF_PID_T >= 4
556 assert_se(PTR_TO_PID(PID_TO_PTR(INT32_MAX
)) == INT32_MAX
);
557 assert_se(PTR_TO_PID(PID_TO_PTR(INT32_MIN
)) == INT32_MIN
);
561 static void test_ioprio_class_from_to_string_one(const char *val
, int expected
) {
562 assert_se(ioprio_class_from_string(val
) == expected
);
564 _cleanup_free_
char *s
= NULL
;
567 assert_se(ioprio_class_to_string_alloc(expected
, &s
) == 0);
568 /* We sometimes get a class number and sometimes a number back */
569 assert_se(streq(s
, val
) ||
570 safe_atou(val
, &ret
) == 0);
574 static void test_ioprio_class_from_to_string(void) {
575 test_ioprio_class_from_to_string_one("none", IOPRIO_CLASS_NONE
);
576 test_ioprio_class_from_to_string_one("realtime", IOPRIO_CLASS_RT
);
577 test_ioprio_class_from_to_string_one("best-effort", IOPRIO_CLASS_BE
);
578 test_ioprio_class_from_to_string_one("idle", IOPRIO_CLASS_IDLE
);
579 test_ioprio_class_from_to_string_one("0", 0);
580 test_ioprio_class_from_to_string_one("1", 1);
581 test_ioprio_class_from_to_string_one("7", 7);
582 test_ioprio_class_from_to_string_one("8", 8);
583 test_ioprio_class_from_to_string_one("9", -1);
584 test_ioprio_class_from_to_string_one("-1", -1);
587 int main(int argc
, char *argv
[]) {
588 log_set_max_level(LOG_DEBUG
);
589 log_parse_environment();
598 (void) parse_pid(argv
[1], &pid
);
599 test_get_process_comm(pid
);
601 TEST_REQ_RUNNING_SYSTEMD(test_get_process_comm(1));
602 test_get_process_comm(getpid());
605 test_get_process_comm_escape();
606 test_pid_is_unwaited();
609 test_get_process_cmdline_harder();
610 test_rename_process();
611 test_getpid_cached();
612 test_getpid_measure();
615 test_ioprio_class_from_to_string();