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)];
39 #if 0 /// UNNEEDED by elogind
47 xsprintf(path, "/proc/"PID_FMT"/comm", pid);
49 if (stat(path, &st) == 0) {
50 assert_se(get_process_comm(pid, &a) >= 0);
51 log_info("PID"PID_FMT" comm: '%s'", pid, a);
53 log_warning("%s not exist.", path);
55 assert_se(get_process_cmdline(pid, 0, true, &c) >= 0);
56 log_info("PID"PID_FMT" cmdline: '%s'", pid, c);
58 assert_se(get_process_cmdline(pid, 8, false, &d) >= 0);
59 log_info("PID"PID_FMT" cmdline truncated to 8: '%s'", pid, d);
62 assert_se(get_process_cmdline(pid, 1, false, &d) >= 0);
63 log_info("PID"PID_FMT" cmdline truncated to 1: '%s'", pid, d);
65 #if 0 /// UNNEEDED by elogind
66 assert_se(get_process_ppid(pid, &e) >= 0);
67 log_info("PID"PID_FMT" PPID: "PID_FMT, pid, e);
68 assert_se(pid == 1 ? e == 0 : e > 0);
71 assert_se(is_kernel_thread(pid) == 0 || pid != 1);
73 r = get_process_exe(pid, &f);
74 assert_se(r >= 0 || r == -EACCES);
75 log_info("PID"PID_FMT" exe: '%s'", pid, strna(f));
77 #if 0 /// UNNEEDED by elogind
78 assert_se(get_process_uid(pid, &u) == 0);
79 log_info("PID"PID_FMT" UID: "UID_FMT, pid, u);
80 assert_se(u == 0 || pid != 1);
82 assert_se(get_process_gid(pid, &g) == 0);
83 log_info("PID"PID_FMT" GID: "GID_FMT, pid, g);
84 assert_se(g == 0 || pid != 1);
86 r = get_process_environ(pid, &env);
87 assert_se(r >= 0 || r == -EACCES);
88 log_info("PID"PID_FMT" strlen(environ): %zi", pid, env ? (ssize_t)strlen(env) : (ssize_t)-errno);
91 if (!detect_container())
92 assert_se(get_ctty_devnr(pid, &h) == -ENXIO || pid != 1);
94 (void) getenv_for_pid(pid, "PATH", &i);
95 log_info("PID"PID_FMT" $PATH: '%s'", pid, strna(i));
98 static void test_get_process_comm_escape_one(const char *input, const char *output) {
99 _cleanup_free_ char *n = NULL;
101 log_info("input: <%s> — output: <%s>", input, output);
103 assert_se(prctl(PR_SET_NAME, input) >= 0);
104 assert_se(get_process_comm(0, &n) >= 0);
106 log_info("got: <%s>", n);
108 assert_se(streq_ptr(n, output));
111 static void test_get_process_comm_escape(void) {
112 _cleanup_free_ char *saved = NULL;
114 assert_se(get_process_comm(0, &saved) >= 0);
116 test_get_process_comm_escape_one("", "");
117 test_get_process_comm_escape_one("foo", "foo");
118 test_get_process_comm_escape_one("012345678901234", "012345678901234");
119 test_get_process_comm_escape_one("0123456789012345", "012345678901234");
120 test_get_process_comm_escape_one("äöüß", "\\303\\244\\303…");
121 test_get_process_comm_escape_one("xäöüß", "x\\303\\244…");
122 test_get_process_comm_escape_one("xxäöüß", "xx\\303\\244…");
123 test_get_process_comm_escape_one("xxxäöüß", "xxx\\303\\244…");
124 test_get_process_comm_escape_one("xxxxäöüß", "xxxx\\303\\244…");
125 test_get_process_comm_escape_one("xxxxxäöüß", "xxxxx\\303…");
127 assert_se(prctl(PR_SET_NAME, saved) >= 0);
130 static void test_pid_is_unwaited(void) {
140 waitpid(pid, &status, 0);
141 assert_se(!pid_is_unwaited(pid));
143 assert_se(pid_is_unwaited(getpid_cached()));
144 assert_se(!pid_is_unwaited(-1));
147 static void test_pid_is_alive(void) {
157 waitpid(pid, &status, 0);
158 assert_se(!pid_is_alive(pid));
160 assert_se(pid_is_alive(getpid_cached()));
161 assert_se(!pid_is_alive(-1));
164 #if 0 /// UNNEEDED by elogind
165 static void test_personality(void) {
167 assert_se(personality_to_string(PER_LINUX));
168 assert_se(!personality_to_string(PERSONALITY_INVALID));
170 assert_se(streq(personality_to_string(PER_LINUX), architecture_to_string(native_architecture())));
172 assert_se(personality_from_string(personality_to_string(PER_LINUX)) == PER_LINUX);
173 assert_se(personality_from_string(architecture_to_string(native_architecture())) == PER_LINUX);
176 assert_se(streq_ptr(personality_to_string(PER_LINUX), "x86-64"));
177 assert_se(streq_ptr(personality_to_string(PER_LINUX32), "x86"));
179 assert_se(personality_from_string("x86-64") == PER_LINUX);
180 assert_se(personality_from_string("x86") == PER_LINUX32);
181 assert_se(personality_from_string("ia64") == PERSONALITY_INVALID);
182 assert_se(personality_from_string(NULL) == PERSONALITY_INVALID);
184 assert_se(personality_from_string(personality_to_string(PER_LINUX32)) == PER_LINUX32);
189 static void test_get_process_cmdline_harder(void) {
190 char path[] = "/tmp/test-cmdlineXXXXXX";
191 _cleanup_close_ int fd = -1;
192 _cleanup_free_ char *line = NULL;
198 #if HAVE_VALGRIND_VALGRIND_H
199 /* valgrind patches open(/proc//cmdline)
200 * so, test_get_process_cmdline_harder fails always
201 * See https://github.com/systemd/systemd/pull/3555#issuecomment-226564908 */
202 if (RUNNING_ON_VALGRIND)
210 (void) wait_for_terminate(pid, &si);
212 assert_se(si.si_code == CLD_EXITED);
213 assert_se(si.si_status == 0);
219 assert_se(unshare(CLONE_NEWNS) >= 0);
221 assert_se(mount(NULL, "/", NULL, MS_PRIVATE|MS_REC, NULL) >= 0);
223 fd = mkostemp(path, O_CLOEXEC);
226 if (mount(path, "/proc/self/cmdline", "bind", MS_BIND, NULL) < 0) {
227 /* This happens under selinux… Abort the test in this case. */
228 log_warning_errno(errno, "mount(..., \"/proc/self/cmdline\", \"bind\", ...) failed: %m");
229 assert(errno == EACCES);
233 assert_se(unlink(path) >= 0);
235 assert_se(prctl(PR_SET_NAME, "testa") >= 0);
237 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) == -ENOENT);
239 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0);
240 assert_se(streq(line, "[testa]"));
243 assert_se(get_process_cmdline(getpid_cached(), 1, true, &line) >= 0);
244 assert_se(streq(line, ""));
247 assert_se(get_process_cmdline(getpid_cached(), 2, true, &line) >= 0);
248 assert_se(streq(line, "["));
251 assert_se(get_process_cmdline(getpid_cached(), 3, true, &line) >= 0);
252 assert_se(streq(line, "[."));
255 assert_se(get_process_cmdline(getpid_cached(), 4, true, &line) >= 0);
256 assert_se(streq(line, "[.."));
259 assert_se(get_process_cmdline(getpid_cached(), 5, true, &line) >= 0);
260 assert_se(streq(line, "[..."));
263 assert_se(get_process_cmdline(getpid_cached(), 6, true, &line) >= 0);
264 assert_se(streq(line, "[...]"));
267 assert_se(get_process_cmdline(getpid_cached(), 7, true, &line) >= 0);
268 assert_se(streq(line, "[t...]"));
271 assert_se(get_process_cmdline(getpid_cached(), 8, true, &line) >= 0);
272 assert_se(streq(line, "[testa]"));
275 assert_se(write(fd, "\0\0\0\0\0\0\0\0\0", 10) == 10);
277 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) == -ENOENT);
279 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0);
280 assert_se(streq(line, "[testa]"));
283 assert_se(write(fd, "foo\0bar\0\0\0\0\0", 10) == 10);
285 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) >= 0);
286 assert_se(streq(line, "foo bar"));
289 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0);
290 assert_se(streq(line, "foo bar"));
293 assert_se(write(fd, "quux", 4) == 4);
294 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) >= 0);
295 assert_se(streq(line, "foo bar quux"));
298 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0);
299 assert_se(streq(line, "foo bar quux"));
302 assert_se(get_process_cmdline(getpid_cached(), 1, true, &line) >= 0);
303 assert_se(streq(line, ""));
306 assert_se(get_process_cmdline(getpid_cached(), 2, true, &line) >= 0);
307 assert_se(streq(line, "."));
310 assert_se(get_process_cmdline(getpid_cached(), 3, true, &line) >= 0);
311 assert_se(streq(line, ".."));
314 assert_se(get_process_cmdline(getpid_cached(), 4, true, &line) >= 0);
315 assert_se(streq(line, "..."));
318 assert_se(get_process_cmdline(getpid_cached(), 5, true, &line) >= 0);
319 assert_se(streq(line, "f..."));
322 assert_se(get_process_cmdline(getpid_cached(), 6, true, &line) >= 0);
323 assert_se(streq(line, "fo..."));
326 assert_se(get_process_cmdline(getpid_cached(), 7, true, &line) >= 0);
327 assert_se(streq(line, "foo..."));
330 assert_se(get_process_cmdline(getpid_cached(), 8, true, &line) >= 0);
331 assert_se(streq(line, "foo..."));
334 assert_se(get_process_cmdline(getpid_cached(), 9, true, &line) >= 0);
335 assert_se(streq(line, "foo b..."));
338 assert_se(get_process_cmdline(getpid_cached(), 10, true, &line) >= 0);
339 assert_se(streq(line, "foo ba..."));
342 assert_se(get_process_cmdline(getpid_cached(), 11, true, &line) >= 0);
343 assert_se(streq(line, "foo bar..."));
346 assert_se(get_process_cmdline(getpid_cached(), 12, true, &line) >= 0);
347 assert_se(streq(line, "foo bar..."));
350 assert_se(get_process_cmdline(getpid_cached(), 13, true, &line) >= 0);
351 assert_se(streq(line, "foo bar quux"));
354 assert_se(get_process_cmdline(getpid_cached(), 14, true, &line) >= 0);
355 assert_se(streq(line, "foo bar quux"));
358 assert_se(get_process_cmdline(getpid_cached(), 1000, true, &line) >= 0);
359 assert_se(streq(line, "foo bar quux"));
362 assert_se(ftruncate(fd, 0) >= 0);
363 assert_se(prctl(PR_SET_NAME, "aaaa bbbb cccc") >= 0);
365 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) == -ENOENT);
367 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0);
368 assert_se(streq(line, "[aaaa bbbb cccc]"));
371 assert_se(get_process_cmdline(getpid_cached(), 10, true, &line) >= 0);
372 assert_se(streq(line, "[aaaa...]"));
375 assert_se(get_process_cmdline(getpid_cached(), 11, true, &line) >= 0);
376 assert_se(streq(line, "[aaaa...]"));
379 assert_se(get_process_cmdline(getpid_cached(), 12, true, &line) >= 0);
380 assert_se(streq(line, "[aaaa b...]"));
387 #if 0 /// UNNEEDED by elogind
388 static void test_rename_process_now(const char *p, int ret) {
389 _cleanup_free_ char *comm = NULL, *cmdline = NULL;
392 r = rename_process(p);
393 assert_se(r == ret ||
394 (ret == 0 && r >= 0) ||
400 #if HAVE_VALGRIND_VALGRIND_H
401 /* see above, valgrind is weird, we can't verify what we are doing here */
402 if (RUNNING_ON_VALGRIND)
406 assert_se(get_process_comm(0, &comm) >= 0);
407 log_info("comm = <%s>", comm);
408 assert_se(strneq(comm, p, TASK_COMM_LEN-1));
410 assert_se(get_process_cmdline(0, 0, false, &cmdline) >= 0);
411 /* we cannot expect cmdline to be renamed properly without privileges */
412 if (geteuid() == 0) {
413 log_info("cmdline = <%s>", cmdline);
414 assert_se(strneq(p, cmdline, STRLEN("test-process-util")));
415 assert_se(startswith(p, cmdline));
417 log_info("cmdline = <%s> (not verified)", cmdline);
420 static void test_rename_process_one(const char *p, int ret) {
429 test_rename_process_now(p, ret);
433 assert_se(wait_for_terminate(pid, &si) >= 0);
434 assert_se(si.si_code == CLD_EXITED);
435 assert_se(si.si_status == EXIT_SUCCESS);
438 static void test_rename_process_multi(void) {
447 assert_se(wait_for_terminate(pid, &si) >= 0);
448 assert_se(si.si_code == CLD_EXITED);
449 assert_se(si.si_status == EXIT_SUCCESS);
455 test_rename_process_now("one", 1);
456 test_rename_process_now("more", 0); /* longer than "one", hence truncated */
457 (void) setresuid(99, 99, 99); /* change uid when running privileged */
458 test_rename_process_now("time!", 0);
459 test_rename_process_now("0", 1); /* shorter than "one", should fit */
460 test_rename_process_one("", -EINVAL);
461 test_rename_process_one(NULL, -EINVAL);
465 static void test_rename_process(void) {
466 test_rename_process_one(NULL, -EINVAL);
467 test_rename_process_one("", -EINVAL);
468 test_rename_process_one("foo", 1); /* should always fit */
469 test_rename_process_one("this is a really really long process name, followed by some more words", 0); /* unlikely to fit */
470 test_rename_process_one("1234567", 1); /* should always fit */
471 test_rename_process_multi(); /* multiple invocations and dropped privileges */
475 static void test_getpid_cached(void) {
477 pid_t a, b, c, d, e, f, child;
483 assert_se(a == b && a == c);
486 assert_se(child >= 0);
494 assert_se(a == b && a == c);
502 assert_se(a == d && a == e && a == f);
504 assert_se(wait_for_terminate(child, &si) >= 0);
505 assert_se(si.si_status == 0);
506 assert_se(si.si_code == CLD_EXITED);
509 #define MEASURE_ITERATIONS (10000000LLU)
511 static void test_getpid_measure(void) {
512 unsigned long long i;
515 t = now(CLOCK_MONOTONIC);
516 for (i = 0; i < MEASURE_ITERATIONS; i++)
518 q = now(CLOCK_MONOTONIC) - t;
520 log_info(" glibc getpid(): %llu/s\n", (unsigned long long) (MEASURE_ITERATIONS*USEC_PER_SEC/q));
522 t = now(CLOCK_MONOTONIC);
523 for (i = 0; i < MEASURE_ITERATIONS; i++)
524 (void) getpid_cached();
525 q = now(CLOCK_MONOTONIC) - t;
527 log_info("getpid_cached(): %llu/s\n", (unsigned long long) (MEASURE_ITERATIONS*USEC_PER_SEC/q));
530 static void test_safe_fork(void) {
535 BLOCK_SIGNALS(SIGCHLD);
537 r = safe_fork("(test-child)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_NULL_STDIO|FORK_REOPEN_LOG, &pid);
542 usleep(100 * USEC_PER_MSEC);
547 assert_se(wait_for_terminate(pid, &status) >= 0);
548 assert_se(status.si_code == CLD_EXITED);
549 assert_se(status.si_status == 88);
552 static void test_pid_to_ptr(void) {
554 assert_se(PTR_TO_PID(NULL) == 0);
555 assert_se(PID_TO_PTR(0) == NULL);
557 assert_se(PTR_TO_PID(PID_TO_PTR(1)) == 1);
558 assert_se(PTR_TO_PID(PID_TO_PTR(2)) == 2);
559 assert_se(PTR_TO_PID(PID_TO_PTR(-1)) == -1);
560 assert_se(PTR_TO_PID(PID_TO_PTR(-2)) == -2);
562 assert_se(PTR_TO_PID(PID_TO_PTR(INT16_MAX)) == INT16_MAX);
563 assert_se(PTR_TO_PID(PID_TO_PTR(INT16_MIN)) == INT16_MIN);
565 #if SIZEOF_PID_T >= 4
566 assert_se(PTR_TO_PID(PID_TO_PTR(INT32_MAX)) == INT32_MAX);
567 assert_se(PTR_TO_PID(PID_TO_PTR(INT32_MIN)) == INT32_MIN);
571 static void test_ioprio_class_from_to_string_one(const char *val, int expected) {
572 assert_se(ioprio_class_from_string(val) == expected);
574 _cleanup_free_ char *s = NULL;
577 assert_se(ioprio_class_to_string_alloc(expected, &s) == 0);
578 /* We sometimes get a class number and sometimes a number back */
579 assert_se(streq(s, val) ||
580 safe_atou(val, &ret) == 0);
584 static void test_ioprio_class_from_to_string(void) {
585 test_ioprio_class_from_to_string_one("none", IOPRIO_CLASS_NONE);
586 test_ioprio_class_from_to_string_one("realtime", IOPRIO_CLASS_RT);
587 test_ioprio_class_from_to_string_one("best-effort", IOPRIO_CLASS_BE);
588 test_ioprio_class_from_to_string_one("idle", IOPRIO_CLASS_IDLE);
589 test_ioprio_class_from_to_string_one("0", 0);
590 test_ioprio_class_from_to_string_one("1", 1);
591 test_ioprio_class_from_to_string_one("7", 7);
592 test_ioprio_class_from_to_string_one("8", 8);
593 test_ioprio_class_from_to_string_one("9", -1);
594 test_ioprio_class_from_to_string_one("-1", -1);
597 int main(int argc, char *argv[]) {
598 log_set_max_level(LOG_DEBUG);
599 log_parse_environment();
608 (void) parse_pid(argv[1], &pid);
609 test_get_process_comm(pid);
611 TEST_REQ_RUNNING_SYSTEMD(test_get_process_comm(1));
612 test_get_process_comm(getpid());
615 test_get_process_comm_escape();
616 test_pid_is_unwaited();
618 #if 0 /// UNNEEDED by elogind
621 test_get_process_cmdline_harder();
622 #if 0 /// UNNEEDED by elogind
623 test_rename_process();
625 test_getpid_cached();
626 test_getpid_measure();
629 test_ioprio_class_from_to_string();