1 /* SPDX-License-Identifier: LGPL-2.1+ */
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"
20 #include "parse-util.h"
21 #include "process-util.h"
22 #include "signal-util.h"
23 #include "stdio-util.h"
24 #include "string-util.h"
25 #include "terminal-util.h"
26 #include "test-helper.h"
30 static void test_get_process_comm(pid_t pid) {
32 _cleanup_free_ char *a = NULL, *c = NULL, *d = NULL, *f = NULL, *i = NULL;
33 _cleanup_free_ char *env = NULL;
34 char path[STRLEN("/proc//comm") + DECIMAL_STR_MAX(pid_t)];
35 #if 0 /// UNNEEDED by elogind
43 xsprintf(path, "/proc/"PID_FMT"/comm", pid);
45 if (stat(path, &st) == 0) {
46 assert_se(get_process_comm(pid, &a) >= 0);
47 log_info("PID"PID_FMT" comm: '%s'", pid, a);
49 log_warning("%s not exist.", path);
51 assert_se(get_process_cmdline(pid, 0, true, &c) >= 0);
52 log_info("PID"PID_FMT" cmdline: '%s'", pid, c);
54 assert_se(get_process_cmdline(pid, 8, false, &d) >= 0);
55 log_info("PID"PID_FMT" cmdline truncated to 8: '%s'", pid, d);
58 assert_se(get_process_cmdline(pid, 1, false, &d) >= 0);
59 log_info("PID"PID_FMT" cmdline truncated to 1: '%s'", pid, d);
61 #if 0 /// UNNEEDED by elogind
62 assert_se(get_process_ppid(pid, &e) >= 0);
63 log_info("PID"PID_FMT" PPID: "PID_FMT, pid, e);
64 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 #if 0 /// UNNEEDED by elogind
74 assert_se(get_process_uid(pid, &u) == 0);
75 log_info("PID"PID_FMT" UID: "UID_FMT, pid, u);
76 assert_se(u == 0 || pid != 1);
78 assert_se(get_process_gid(pid, &g) == 0);
79 log_info("PID"PID_FMT" GID: "GID_FMT, pid, g);
80 assert_se(g == 0 || pid != 1);
82 r = get_process_environ(pid, &env);
83 assert_se(r >= 0 || r == -EACCES);
84 log_info("PID"PID_FMT" strlen(environ): %zi", pid, env ? (ssize_t)strlen(env) : (ssize_t)-errno);
87 if (!detect_container())
88 assert_se(get_ctty_devnr(pid, &h) == -ENXIO || pid != 1);
90 (void) getenv_for_pid(pid, "PATH", &i);
91 log_info("PID"PID_FMT" $PATH: '%s'", pid, strna(i));
94 static void test_get_process_comm_escape_one(const char *input, const char *output) {
95 _cleanup_free_ char *n = NULL;
97 log_info("input: <%s> — output: <%s>", input, output);
99 assert_se(prctl(PR_SET_NAME, input) >= 0);
100 assert_se(get_process_comm(0, &n) >= 0);
102 log_info("got: <%s>", n);
104 assert_se(streq_ptr(n, output));
107 static void test_get_process_comm_escape(void) {
108 _cleanup_free_ char *saved = NULL;
110 assert_se(get_process_comm(0, &saved) >= 0);
112 test_get_process_comm_escape_one("", "");
113 test_get_process_comm_escape_one("foo", "foo");
114 test_get_process_comm_escape_one("012345678901234", "012345678901234");
115 test_get_process_comm_escape_one("0123456789012345", "012345678901234");
116 test_get_process_comm_escape_one("äöüß", "\\303\\244\\303…");
117 test_get_process_comm_escape_one("xäöüß", "x\\303\\244…");
118 test_get_process_comm_escape_one("xxäöüß", "xx\\303\\244…");
119 test_get_process_comm_escape_one("xxxäöüß", "xxx\\303\\244…");
120 test_get_process_comm_escape_one("xxxxäöüß", "xxxx\\303\\244…");
121 test_get_process_comm_escape_one("xxxxxäöüß", "xxxxx\\303…");
123 assert_se(prctl(PR_SET_NAME, saved) >= 0);
126 static void test_pid_is_unwaited(void) {
136 waitpid(pid, &status, 0);
137 assert_se(!pid_is_unwaited(pid));
139 assert_se(pid_is_unwaited(getpid_cached()));
140 assert_se(!pid_is_unwaited(-1));
143 static void test_pid_is_alive(void) {
153 waitpid(pid, &status, 0);
154 assert_se(!pid_is_alive(pid));
156 assert_se(pid_is_alive(getpid_cached()));
157 assert_se(!pid_is_alive(-1));
160 #if 0 /// UNNEEDED by elogind
161 static void test_personality(void) {
163 assert_se(personality_to_string(PER_LINUX));
164 assert_se(!personality_to_string(PERSONALITY_INVALID));
166 assert_se(streq(personality_to_string(PER_LINUX), architecture_to_string(native_architecture())));
168 assert_se(personality_from_string(personality_to_string(PER_LINUX)) == PER_LINUX);
169 assert_se(personality_from_string(architecture_to_string(native_architecture())) == PER_LINUX);
172 assert_se(streq_ptr(personality_to_string(PER_LINUX), "x86-64"));
173 assert_se(streq_ptr(personality_to_string(PER_LINUX32), "x86"));
175 assert_se(personality_from_string("x86-64") == PER_LINUX);
176 assert_se(personality_from_string("x86") == PER_LINUX32);
177 assert_se(personality_from_string("ia64") == PERSONALITY_INVALID);
178 assert_se(personality_from_string(NULL) == PERSONALITY_INVALID);
180 assert_se(personality_from_string(personality_to_string(PER_LINUX32)) == PER_LINUX32);
185 static void test_get_process_cmdline_harder(void) {
186 char path[] = "/tmp/test-cmdlineXXXXXX";
187 _cleanup_close_ int fd = -1;
188 _cleanup_free_ char *line = NULL;
194 #if HAVE_VALGRIND_VALGRIND_H
195 /* valgrind patches open(/proc//cmdline)
196 * so, test_get_process_cmdline_harder fails always
197 * See https://github.com/systemd/systemd/pull/3555#issuecomment-226564908 */
198 if (RUNNING_ON_VALGRIND)
206 (void) wait_for_terminate(pid, &si);
208 assert_se(si.si_code == CLD_EXITED);
209 assert_se(si.si_status == 0);
215 assert_se(unshare(CLONE_NEWNS) >= 0);
217 assert_se(mount(NULL, "/", NULL, MS_PRIVATE|MS_REC, NULL) >= 0);
219 fd = mkostemp(path, O_CLOEXEC);
222 if (mount(path, "/proc/self/cmdline", "bind", MS_BIND, NULL) < 0) {
223 /* This happens under selinux… Abort the test in this case. */
224 log_warning_errno(errno, "mount(..., \"/proc/self/cmdline\", \"bind\", ...) failed: %m");
225 assert(errno == EACCES);
229 assert_se(unlink(path) >= 0);
231 assert_se(prctl(PR_SET_NAME, "testa") >= 0);
233 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) == -ENOENT);
235 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0);
236 assert_se(streq(line, "[testa]"));
239 assert_se(get_process_cmdline(getpid_cached(), 1, true, &line) >= 0);
240 assert_se(streq(line, ""));
243 assert_se(get_process_cmdline(getpid_cached(), 2, true, &line) >= 0);
244 assert_se(streq(line, "["));
247 assert_se(get_process_cmdline(getpid_cached(), 3, true, &line) >= 0);
248 assert_se(streq(line, "[."));
251 assert_se(get_process_cmdline(getpid_cached(), 4, true, &line) >= 0);
252 assert_se(streq(line, "[.."));
255 assert_se(get_process_cmdline(getpid_cached(), 5, true, &line) >= 0);
256 assert_se(streq(line, "[..."));
259 assert_se(get_process_cmdline(getpid_cached(), 6, true, &line) >= 0);
260 assert_se(streq(line, "[...]"));
263 assert_se(get_process_cmdline(getpid_cached(), 7, true, &line) >= 0);
264 assert_se(streq(line, "[t...]"));
267 assert_se(get_process_cmdline(getpid_cached(), 8, true, &line) >= 0);
268 assert_se(streq(line, "[testa]"));
271 assert_se(write(fd, "\0\0\0\0\0\0\0\0\0", 10) == 10);
273 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) == -ENOENT);
275 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0);
276 assert_se(streq(line, "[testa]"));
279 assert_se(write(fd, "foo\0bar\0\0\0\0\0", 10) == 10);
281 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) >= 0);
282 assert_se(streq(line, "foo bar"));
285 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0);
286 assert_se(streq(line, "foo bar"));
289 assert_se(write(fd, "quux", 4) == 4);
290 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) >= 0);
291 assert_se(streq(line, "foo bar quux"));
294 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0);
295 assert_se(streq(line, "foo bar quux"));
298 assert_se(get_process_cmdline(getpid_cached(), 1, true, &line) >= 0);
299 assert_se(streq(line, ""));
302 assert_se(get_process_cmdline(getpid_cached(), 2, true, &line) >= 0);
303 assert_se(streq(line, "."));
306 assert_se(get_process_cmdline(getpid_cached(), 3, true, &line) >= 0);
307 assert_se(streq(line, ".."));
310 assert_se(get_process_cmdline(getpid_cached(), 4, true, &line) >= 0);
311 assert_se(streq(line, "..."));
314 assert_se(get_process_cmdline(getpid_cached(), 5, true, &line) >= 0);
315 assert_se(streq(line, "f..."));
318 assert_se(get_process_cmdline(getpid_cached(), 6, true, &line) >= 0);
319 assert_se(streq(line, "fo..."));
322 assert_se(get_process_cmdline(getpid_cached(), 7, true, &line) >= 0);
323 assert_se(streq(line, "foo..."));
326 assert_se(get_process_cmdline(getpid_cached(), 8, true, &line) >= 0);
327 assert_se(streq(line, "foo..."));
330 assert_se(get_process_cmdline(getpid_cached(), 9, true, &line) >= 0);
331 assert_se(streq(line, "foo b..."));
334 assert_se(get_process_cmdline(getpid_cached(), 10, true, &line) >= 0);
335 assert_se(streq(line, "foo ba..."));
338 assert_se(get_process_cmdline(getpid_cached(), 11, true, &line) >= 0);
339 assert_se(streq(line, "foo bar..."));
342 assert_se(get_process_cmdline(getpid_cached(), 12, true, &line) >= 0);
343 assert_se(streq(line, "foo bar..."));
346 assert_se(get_process_cmdline(getpid_cached(), 13, true, &line) >= 0);
347 assert_se(streq(line, "foo bar quux"));
350 assert_se(get_process_cmdline(getpid_cached(), 14, true, &line) >= 0);
351 assert_se(streq(line, "foo bar quux"));
354 assert_se(get_process_cmdline(getpid_cached(), 1000, true, &line) >= 0);
355 assert_se(streq(line, "foo bar quux"));
358 assert_se(ftruncate(fd, 0) >= 0);
359 assert_se(prctl(PR_SET_NAME, "aaaa bbbb cccc") >= 0);
361 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) == -ENOENT);
363 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0);
364 assert_se(streq(line, "[aaaa bbbb cccc]"));
367 assert_se(get_process_cmdline(getpid_cached(), 10, true, &line) >= 0);
368 assert_se(streq(line, "[aaaa...]"));
371 assert_se(get_process_cmdline(getpid_cached(), 11, true, &line) >= 0);
372 assert_se(streq(line, "[aaaa...]"));
375 assert_se(get_process_cmdline(getpid_cached(), 12, true, &line) >= 0);
376 assert_se(streq(line, "[aaaa b...]"));
383 #if 0 /// UNNEEDED by elogind
384 static void test_rename_process_now(const char *p, int ret) {
385 _cleanup_free_ char *comm = NULL, *cmdline = NULL;
388 r = rename_process(p);
389 assert_se(r == ret ||
390 (ret == 0 && r >= 0) ||
396 #if HAVE_VALGRIND_VALGRIND_H
397 /* see above, valgrind is weird, we can't verify what we are doing here */
398 if (RUNNING_ON_VALGRIND)
402 assert_se(get_process_comm(0, &comm) >= 0);
403 log_info("comm = <%s>", comm);
404 assert_se(strneq(comm, p, TASK_COMM_LEN-1));
406 assert_se(get_process_cmdline(0, 0, false, &cmdline) >= 0);
407 /* we cannot expect cmdline to be renamed properly without privileges */
408 if (geteuid() == 0) {
409 log_info("cmdline = <%s>", cmdline);
410 assert_se(strneq(p, cmdline, STRLEN("test-process-util")));
411 assert_se(startswith(p, cmdline));
413 log_info("cmdline = <%s> (not verified)", cmdline);
416 static void test_rename_process_one(const char *p, int ret) {
425 test_rename_process_now(p, ret);
429 assert_se(wait_for_terminate(pid, &si) >= 0);
430 assert_se(si.si_code == CLD_EXITED);
431 assert_se(si.si_status == EXIT_SUCCESS);
434 static void test_rename_process_multi(void) {
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);
451 test_rename_process_now("one", 1);
452 test_rename_process_now("more", 0); /* longer than "one", hence truncated */
453 (void) setresuid(99, 99, 99); /* change uid when running privileged */
454 test_rename_process_now("time!", 0);
455 test_rename_process_now("0", 1); /* shorter than "one", should fit */
456 test_rename_process_one("", -EINVAL);
457 test_rename_process_one(NULL, -EINVAL);
461 static void test_rename_process(void) {
462 test_rename_process_one(NULL, -EINVAL);
463 test_rename_process_one("", -EINVAL);
464 test_rename_process_one("foo", 1); /* should always fit */
465 test_rename_process_one("this is a really really long process name, followed by some more words", 0); /* unlikely to fit */
466 test_rename_process_one("1234567", 1); /* should always fit */
467 test_rename_process_multi(); /* multiple invocations and dropped privileges */
471 static void test_getpid_cached(void) {
473 pid_t a, b, c, d, e, f, child;
479 assert_se(a == b && a == c);
482 assert_se(child >= 0);
490 assert_se(a == b && a == c);
498 assert_se(a == d && a == e && a == f);
500 assert_se(wait_for_terminate(child, &si) >= 0);
501 assert_se(si.si_status == 0);
502 assert_se(si.si_code == CLD_EXITED);
505 #define MEASURE_ITERATIONS (10000000LLU)
507 static void test_getpid_measure(void) {
508 unsigned long long i;
511 t = now(CLOCK_MONOTONIC);
512 for (i = 0; i < MEASURE_ITERATIONS; i++)
514 q = now(CLOCK_MONOTONIC) - t;
516 log_info(" glibc getpid(): %llu/s\n", (unsigned long long) (MEASURE_ITERATIONS*USEC_PER_SEC/q));
518 t = now(CLOCK_MONOTONIC);
519 for (i = 0; i < MEASURE_ITERATIONS; i++)
520 (void) getpid_cached();
521 q = now(CLOCK_MONOTONIC) - t;
523 log_info("getpid_cached(): %llu/s\n", (unsigned long long) (MEASURE_ITERATIONS*USEC_PER_SEC/q));
526 static void test_safe_fork(void) {
531 BLOCK_SIGNALS(SIGCHLD);
533 r = safe_fork("(test-child)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_NULL_STDIO|FORK_REOPEN_LOG, &pid);
538 usleep(100 * USEC_PER_MSEC);
543 assert_se(wait_for_terminate(pid, &status) >= 0);
544 assert_se(status.si_code == CLD_EXITED);
545 assert_se(status.si_status == 88);
548 static void test_pid_to_ptr(void) {
550 assert_se(PTR_TO_PID(NULL) == 0);
551 assert_se(PID_TO_PTR(0) == NULL);
553 assert_se(PTR_TO_PID(PID_TO_PTR(1)) == 1);
554 assert_se(PTR_TO_PID(PID_TO_PTR(2)) == 2);
555 assert_se(PTR_TO_PID(PID_TO_PTR(-1)) == -1);
556 assert_se(PTR_TO_PID(PID_TO_PTR(-2)) == -2);
558 assert_se(PTR_TO_PID(PID_TO_PTR(INT16_MAX)) == INT16_MAX);
559 assert_se(PTR_TO_PID(PID_TO_PTR(INT16_MIN)) == INT16_MIN);
561 #if SIZEOF_PID_T >= 4
562 assert_se(PTR_TO_PID(PID_TO_PTR(INT32_MAX)) == INT32_MAX);
563 assert_se(PTR_TO_PID(PID_TO_PTR(INT32_MIN)) == INT32_MIN);
567 #if 0 /// UNNEEDED by elogind
568 static void test_ioprio_class_from_to_string_one(const char *val, int expected) {
569 assert_se(ioprio_class_from_string(val) == expected);
571 _cleanup_free_ char *s = NULL;
574 assert_se(ioprio_class_to_string_alloc(expected, &s) == 0);
575 /* We sometimes get a class number and sometimes a number back */
576 assert_se(streq(s, val) ||
577 safe_atou(val, &ret) == 0);
581 static void test_ioprio_class_from_to_string(void) {
582 test_ioprio_class_from_to_string_one("none", IOPRIO_CLASS_NONE);
583 test_ioprio_class_from_to_string_one("realtime", IOPRIO_CLASS_RT);
584 test_ioprio_class_from_to_string_one("best-effort", IOPRIO_CLASS_BE);
585 test_ioprio_class_from_to_string_one("idle", IOPRIO_CLASS_IDLE);
586 test_ioprio_class_from_to_string_one("0", 0);
587 test_ioprio_class_from_to_string_one("1", 1);
588 test_ioprio_class_from_to_string_one("7", 7);
589 test_ioprio_class_from_to_string_one("8", 8);
590 test_ioprio_class_from_to_string_one("9", -1);
591 test_ioprio_class_from_to_string_one("-1", -1);
595 int main(int argc, char *argv[]) {
596 log_set_max_level(LOG_DEBUG);
597 log_parse_environment();
606 (void) parse_pid(argv[1], &pid);
607 test_get_process_comm(pid);
609 TEST_REQ_RUNNING_SYSTEMD(test_get_process_comm(1));
610 test_get_process_comm(getpid());
613 test_get_process_comm_escape();
614 test_pid_is_unwaited();
616 #if 0 /// UNNEEDED by elogind
619 test_get_process_cmdline_harder();
620 #if 0 /// UNNEEDED by elogind
621 test_rename_process();
623 test_getpid_cached();
624 test_getpid_measure();
627 #if 0 /// UNNEEDED by elogind
628 test_ioprio_class_from_to_string();