]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-process-util.c
process-util: explicitly handle processes lacking parents in get_process_ppid()
[thirdparty/systemd.git] / src / test / test-process-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <sys/mount.h>
5 #include <sys/personality.h>
6 #include <sys/prctl.h>
7 #include <sys/stat.h>
8 #include <sys/types.h>
9 #include <sys/wait.h>
10 #include <unistd.h>
11 #if HAVE_VALGRIND_VALGRIND_H
12 #include <valgrind/valgrind.h>
13 #endif
14
15 #include "alloc-util.h"
16 #include "architecture.h"
17 #include "dirent-util.h"
18 #include "errno-list.h"
19 #include "errno-util.h"
20 #include "fd-util.h"
21 #include "log.h"
22 #include "macro.h"
23 #include "missing_sched.h"
24 #include "missing_syscall.h"
25 #include "parse-util.h"
26 #include "process-util.h"
27 #include "procfs-util.h"
28 #include "rlimit-util.h"
29 #include "signal-util.h"
30 #include "stdio-util.h"
31 #include "string-util.h"
32 #include "terminal-util.h"
33 #include "tests.h"
34 #include "user-util.h"
35 #include "util.h"
36 #include "virt.h"
37
38 static void test_get_process_comm(pid_t pid) {
39 struct stat st;
40 _cleanup_free_ char *a = NULL, *c = NULL, *d = NULL, *f = NULL, *i = NULL;
41 _cleanup_free_ char *env = NULL;
42 char path[STRLEN("/proc//comm") + DECIMAL_STR_MAX(pid_t)];
43 pid_t e;
44 uid_t u;
45 gid_t g;
46 dev_t h;
47 int r;
48
49 log_info("/* %s */", __func__);
50
51 xsprintf(path, "/proc/"PID_FMT"/comm", pid);
52
53 if (stat(path, &st) == 0) {
54 assert_se(get_process_comm(pid, &a) >= 0);
55 log_info("PID"PID_FMT" comm: '%s'", pid, a);
56 } else
57 log_warning("%s not exist.", path);
58
59 assert_se(get_process_cmdline(pid, 0, PROCESS_CMDLINE_COMM_FALLBACK, &c) >= 0);
60 log_info("PID"PID_FMT" cmdline: '%s'", pid, c);
61
62 assert_se(get_process_cmdline(pid, 8, 0, &d) >= 0);
63 log_info("PID"PID_FMT" cmdline truncated to 8: '%s'", pid, d);
64
65 free(d);
66 assert_se(get_process_cmdline(pid, 1, 0, &d) >= 0);
67 log_info("PID"PID_FMT" cmdline truncated to 1: '%s'", pid, d);
68
69 r = get_process_ppid(pid, &e);
70 assert_se(pid == 1 ? r == -EADDRNOTAVAIL : r >= 0);
71 if (r >= 0) {
72 log_info("PID"PID_FMT" PPID: "PID_FMT, pid, e);
73 assert_se(e > 0);
74 }
75
76 assert_se(is_kernel_thread(pid) == 0 || pid != 1);
77
78 r = get_process_exe(pid, &f);
79 assert_se(r >= 0 || r == -EACCES);
80 log_info("PID"PID_FMT" exe: '%s'", pid, strna(f));
81
82 assert_se(get_process_uid(pid, &u) == 0);
83 log_info("PID"PID_FMT" UID: "UID_FMT, pid, u);
84
85 assert_se(get_process_gid(pid, &g) == 0);
86 log_info("PID"PID_FMT" GID: "GID_FMT, pid, g);
87
88 r = get_process_environ(pid, &env);
89 assert_se(r >= 0 || r == -EACCES);
90 log_info("PID"PID_FMT" strlen(environ): %zi", pid, env ? (ssize_t)strlen(env) : (ssize_t)-errno);
91
92 if (!detect_container())
93 assert_se(get_ctty_devnr(pid, &h) == -ENXIO || pid != 1);
94
95 (void) getenv_for_pid(pid, "PATH", &i);
96 log_info("PID"PID_FMT" $PATH: '%s'", pid, strna(i));
97 }
98
99 static void test_get_process_cmdline_one(pid_t pid) {
100 _cleanup_free_ char *c = NULL, *d = NULL, *e = NULL, *f = NULL, *g = NULL, *h = NULL;
101 int r;
102
103 r = get_process_cmdline(pid, SIZE_MAX, 0, &c);
104 log_info("PID "PID_FMT": %s", pid, r >= 0 ? c : errno_to_name(r));
105
106 r = get_process_cmdline(pid, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &d);
107 log_info(" %s", r >= 0 ? d : errno_to_name(r));
108
109 r = get_process_cmdline(pid, SIZE_MAX, PROCESS_CMDLINE_QUOTE, &e);
110 log_info(" %s", r >= 0 ? e : errno_to_name(r));
111
112 r = get_process_cmdline(pid, SIZE_MAX, PROCESS_CMDLINE_QUOTE | PROCESS_CMDLINE_COMM_FALLBACK, &f);
113 log_info(" %s", r >= 0 ? f : errno_to_name(r));
114
115 r = get_process_cmdline(pid, SIZE_MAX, PROCESS_CMDLINE_QUOTE_POSIX, &g);
116 log_info(" %s", r >= 0 ? g : errno_to_name(r));
117
118 r = get_process_cmdline(pid, SIZE_MAX, PROCESS_CMDLINE_QUOTE_POSIX | PROCESS_CMDLINE_COMM_FALLBACK, &h);
119 log_info(" %s", r >= 0 ? h : errno_to_name(r));
120 }
121
122 static void test_get_process_cmdline(void) {
123 _cleanup_closedir_ DIR *d = NULL;
124 struct dirent *de;
125
126 log_info("/* %s */", __func__);
127
128 assert_se(d = opendir("/proc"));
129
130 FOREACH_DIRENT(de, d, return) {
131 pid_t pid;
132
133 if (de->d_type != DT_DIR)
134 continue;
135
136 if (parse_pid(de->d_name, &pid) < 0)
137 continue;
138
139 test_get_process_cmdline_one(pid);
140 }
141 }
142
143 static void test_get_process_comm_escape_one(const char *input, const char *output) {
144 _cleanup_free_ char *n = NULL;
145
146 log_debug("input: <%s> — output: <%s>", input, output);
147
148 assert_se(prctl(PR_SET_NAME, input) >= 0);
149 assert_se(get_process_comm(0, &n) >= 0);
150
151 log_debug("got: <%s>", n);
152
153 assert_se(streq_ptr(n, output));
154 }
155
156 static void test_get_process_comm_escape(void) {
157 _cleanup_free_ char *saved = NULL;
158
159 log_info("/* %s */", __func__);
160
161 assert_se(get_process_comm(0, &saved) >= 0);
162
163 test_get_process_comm_escape_one("", "");
164 test_get_process_comm_escape_one("foo", "foo");
165 test_get_process_comm_escape_one("012345678901234", "012345678901234");
166 test_get_process_comm_escape_one("0123456789012345", "012345678901234");
167 test_get_process_comm_escape_one("äöüß", "\\303\\244\\303\\266\\303\\274\\303\\237");
168 test_get_process_comm_escape_one("xäöüß", "x\\303\\244\\303\\266\\303\\274\\303\\237");
169 test_get_process_comm_escape_one("xxäöüß", "xx\\303\\244\\303\\266\\303\\274\\303\\237");
170 test_get_process_comm_escape_one("xxxäöüß", "xxx\\303\\244\\303\\266\\303\\274\\303\\237");
171 test_get_process_comm_escape_one("xxxxäöüß", "xxxx\\303\\244\\303\\266\\303\\274\\303\\237");
172 test_get_process_comm_escape_one("xxxxxäöüß", "xxxxx\\303\\244\\303\\266\\303\\274\\303\\237");
173
174 assert_se(prctl(PR_SET_NAME, saved) >= 0);
175 }
176
177 static void test_pid_is_unwaited(void) {
178 pid_t pid;
179
180 pid = fork();
181 assert_se(pid >= 0);
182 if (pid == 0) {
183 _exit(EXIT_SUCCESS);
184 } else {
185 int status;
186
187 waitpid(pid, &status, 0);
188 assert_se(!pid_is_unwaited(pid));
189 }
190 assert_se(pid_is_unwaited(getpid_cached()));
191 assert_se(!pid_is_unwaited(-1));
192 }
193
194 static void test_pid_is_alive(void) {
195 pid_t pid;
196
197 log_info("/* %s */", __func__);
198
199 pid = fork();
200 assert_se(pid >= 0);
201 if (pid == 0) {
202 _exit(EXIT_SUCCESS);
203 } else {
204 int status;
205
206 waitpid(pid, &status, 0);
207 assert_se(!pid_is_alive(pid));
208 }
209 assert_se(pid_is_alive(getpid_cached()));
210 assert_se(!pid_is_alive(-1));
211 }
212
213 static void test_personality(void) {
214 log_info("/* %s */", __func__);
215
216 assert_se(personality_to_string(PER_LINUX));
217 assert_se(!personality_to_string(PERSONALITY_INVALID));
218
219 assert_se(streq(personality_to_string(PER_LINUX), architecture_to_string(native_architecture())));
220
221 assert_se(personality_from_string(personality_to_string(PER_LINUX)) == PER_LINUX);
222 assert_se(personality_from_string(architecture_to_string(native_architecture())) == PER_LINUX);
223
224 #ifdef __x86_64__
225 assert_se(streq_ptr(personality_to_string(PER_LINUX), "x86-64"));
226 assert_se(streq_ptr(personality_to_string(PER_LINUX32), "x86"));
227
228 assert_se(personality_from_string("x86-64") == PER_LINUX);
229 assert_se(personality_from_string("x86") == PER_LINUX32);
230 assert_se(personality_from_string("ia64") == PERSONALITY_INVALID);
231 assert_se(personality_from_string(NULL) == PERSONALITY_INVALID);
232
233 assert_se(personality_from_string(personality_to_string(PER_LINUX32)) == PER_LINUX32);
234 #endif
235 }
236
237 static void test_get_process_cmdline_harder(void) {
238 char path[] = "/tmp/test-cmdlineXXXXXX";
239 _cleanup_close_ int fd = -1;
240 _cleanup_free_ char *line = NULL;
241 pid_t pid;
242
243 log_info("/* %s */", __func__);
244
245 if (geteuid() != 0) {
246 log_info("Skipping %s: not root", __func__);
247 return;
248 }
249
250 if (!have_namespaces()) {
251 log_notice("Testing without namespaces, skipping %s", __func__);
252 return;
253 }
254
255 #if HAVE_VALGRIND_VALGRIND_H
256 /* valgrind patches open(/proc//cmdline)
257 * so, test_get_process_cmdline_harder fails always
258 * See https://github.com/systemd/systemd/pull/3555#issuecomment-226564908 */
259 if (RUNNING_ON_VALGRIND) {
260 log_info("Skipping %s: running on valgrind", __func__);
261 return;
262 }
263 #endif
264
265 pid = fork();
266 if (pid > 0) {
267 siginfo_t si;
268
269 (void) wait_for_terminate(pid, &si);
270
271 assert_se(si.si_code == CLD_EXITED);
272 assert_se(si.si_status == 0);
273
274 return;
275 }
276
277 assert_se(pid == 0);
278 assert_se(unshare(CLONE_NEWNS) >= 0);
279
280 if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL) < 0) {
281 log_warning_errno(errno, "mount(..., \"/\", MS_SLAVE|MS_REC, ...) failed: %m");
282 assert_se(IN_SET(errno, EPERM, EACCES));
283 return;
284 }
285
286 fd = mkostemp(path, O_CLOEXEC);
287 assert_se(fd >= 0);
288
289 /* Note that we don't unmount the following bind-mount at the end of the test because the kernel
290 * will clear up its /proc/PID/ hierarchy automatically as soon as the test stops. */
291 if (mount(path, "/proc/self/cmdline", "bind", MS_BIND, NULL) < 0) {
292 /* This happens under selinux… Abort the test in this case. */
293 log_warning_errno(errno, "mount(..., \"/proc/self/cmdline\", \"bind\", ...) failed: %m");
294 assert_se(IN_SET(errno, EPERM, EACCES));
295 return;
296 }
297
298 /* Set RLIMIT_STACK to infinity to test we don't try to allocate unncessarily large values to read
299 * the cmdline. */
300 if (setrlimit(RLIMIT_STACK, &RLIMIT_MAKE_CONST(RLIM_INFINITY)) < 0)
301 log_warning("Testing without RLIMIT_STACK=infinity");
302
303 assert_se(unlink(path) >= 0);
304
305 assert_se(prctl(PR_SET_NAME, "testa") >= 0);
306
307 assert_se(get_process_cmdline(0, SIZE_MAX, 0, &line) == -ENOENT);
308
309 assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
310 log_debug("'%s'", line);
311 assert_se(streq(line, "[testa]"));
312 line = mfree(line);
313
314 assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK | PROCESS_CMDLINE_QUOTE, &line) >= 0);
315 log_debug("'%s'", line);
316 assert_se(streq(line, "\"[testa]\"")); /* quoting is enabled here */
317 line = mfree(line);
318
319 assert_se(get_process_cmdline(0, 0, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
320 log_debug("'%s'", line);
321 assert_se(streq(line, ""));
322 line = mfree(line);
323
324 assert_se(get_process_cmdline(0, 1, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
325 assert_se(streq(line, "…"));
326 line = mfree(line);
327
328 assert_se(get_process_cmdline(0, 2, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
329 assert_se(streq(line, "[…"));
330 line = mfree(line);
331
332 assert_se(get_process_cmdline(0, 3, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
333 assert_se(streq(line, "[t…"));
334 line = mfree(line);
335
336 assert_se(get_process_cmdline(0, 4, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
337 assert_se(streq(line, "[te…"));
338 line = mfree(line);
339
340 assert_se(get_process_cmdline(0, 5, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
341 assert_se(streq(line, "[tes…"));
342 line = mfree(line);
343
344 assert_se(get_process_cmdline(0, 6, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
345 assert_se(streq(line, "[test…"));
346 line = mfree(line);
347
348 assert_se(get_process_cmdline(0, 7, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
349 assert_se(streq(line, "[testa]"));
350 line = mfree(line);
351
352 assert_se(get_process_cmdline(0, 8, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
353 assert_se(streq(line, "[testa]"));
354 line = mfree(line);
355
356 /* Test with multiple arguments that don't require quoting */
357
358 assert_se(write(fd, "foo\0bar", 8) == 8);
359
360 assert_se(get_process_cmdline(0, SIZE_MAX, 0, &line) >= 0);
361 log_debug("'%s'", line);
362 assert_se(streq(line, "foo bar"));
363 line = mfree(line);
364
365 assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
366 assert_se(streq(line, "foo bar"));
367 line = mfree(line);
368
369 assert_se(write(fd, "quux", 4) == 4);
370 assert_se(get_process_cmdline(0, SIZE_MAX, 0, &line) >= 0);
371 log_debug("'%s'", line);
372 assert_se(streq(line, "foo bar quux"));
373 line = mfree(line);
374
375 assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
376 log_debug("'%s'", line);
377 assert_se(streq(line, "foo bar quux"));
378 line = mfree(line);
379
380 assert_se(get_process_cmdline(0, 1, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
381 log_debug("'%s'", line);
382 assert_se(streq(line, "…"));
383 line = mfree(line);
384
385 assert_se(get_process_cmdline(0, 2, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
386 log_debug("'%s'", line);
387 assert_se(streq(line, "f…"));
388 line = mfree(line);
389
390 assert_se(get_process_cmdline(0, 3, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
391 log_debug("'%s'", line);
392 assert_se(streq(line, "fo…"));
393 line = mfree(line);
394
395 assert_se(get_process_cmdline(0, 4, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
396 log_debug("'%s'", line);
397 assert_se(streq(line, "foo…"));
398 line = mfree(line);
399
400 assert_se(get_process_cmdline(0, 5, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
401 log_debug("'%s'", line);
402 assert_se(streq(line, "foo …"));
403 line = mfree(line);
404
405 assert_se(get_process_cmdline(0, 6, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
406 log_debug("'%s'", line);
407 assert_se(streq(line, "foo b…"));
408 line = mfree(line);
409
410 assert_se(get_process_cmdline(0, 7, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
411 log_debug("'%s'", line);
412 assert_se(streq(line, "foo ba…"));
413 line = mfree(line);
414
415 assert_se(get_process_cmdline(0, 8, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
416 log_debug("'%s'", line);
417 assert_se(streq(line, "foo bar…"));
418 line = mfree(line);
419
420 assert_se(get_process_cmdline(0, 9, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
421 log_debug("'%s'", line);
422 assert_se(streq(line, "foo bar …"));
423 line = mfree(line);
424
425 assert_se(get_process_cmdline(0, 10, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
426 log_debug("'%s'", line);
427 assert_se(streq(line, "foo bar q…"));
428 line = mfree(line);
429
430 assert_se(get_process_cmdline(0, 11, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
431 log_debug("'%s'", line);
432 assert_se(streq(line, "foo bar qu…"));
433 line = mfree(line);
434
435 assert_se(get_process_cmdline(0, 12, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
436 log_debug("'%s'", line);
437 assert_se(streq(line, "foo bar quux"));
438 line = mfree(line);
439
440 assert_se(get_process_cmdline(0, 13, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
441 log_debug("'%s'", line);
442 assert_se(streq(line, "foo bar quux"));
443 line = mfree(line);
444
445 assert_se(get_process_cmdline(0, 14, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
446 log_debug("'%s'", line);
447 assert_se(streq(line, "foo bar quux"));
448 line = mfree(line);
449
450 assert_se(get_process_cmdline(0, 1000, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
451 log_debug("'%s'", line);
452 assert_se(streq(line, "foo bar quux"));
453 line = mfree(line);
454
455 assert_se(ftruncate(fd, 0) >= 0);
456 assert_se(prctl(PR_SET_NAME, "aaaa bbbb cccc") >= 0);
457
458 assert_se(get_process_cmdline(0, SIZE_MAX, 0, &line) == -ENOENT);
459
460 assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
461 log_debug("'%s'", line);
462 assert_se(streq(line, "[aaaa bbbb cccc]"));
463 line = mfree(line);
464
465 assert_se(get_process_cmdline(0, 10, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
466 log_debug("'%s'", line);
467 assert_se(streq(line, "[aaaa bbb…"));
468 line = mfree(line);
469
470 assert_se(get_process_cmdline(0, 11, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
471 log_debug("'%s'", line);
472 assert_se(streq(line, "[aaaa bbbb…"));
473 line = mfree(line);
474
475 assert_se(get_process_cmdline(0, 12, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
476 log_debug("'%s'", line);
477 assert_se(streq(line, "[aaaa bbbb …"));
478 line = mfree(line);
479
480 /* Test with multiple arguments that do require quoting */
481
482 #define CMDLINE1 "foo\0'bar'\0\"bar$\"\0x y z\0!``\0"
483 #define EXPECT1 "foo \"'bar'\" \"\\\"bar\\$\\\"\" \"x y z\" \"!\\`\\`\" \"\""
484 #define EXPECT1p "foo $'\\'bar\\'' $'\"bar$\"' $'x y z' $'!``' \"\""
485 assert_se(lseek(fd, SEEK_SET, 0) == 0);
486 assert_se(write(fd, CMDLINE1, sizeof CMDLINE1) == sizeof CMDLINE1);
487 assert_se(ftruncate(fd, sizeof CMDLINE1) == 0);
488
489 assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_QUOTE, &line) >= 0);
490 log_debug("got: ==%s==", line);
491 log_debug("exp: ==%s==", EXPECT1);
492 assert_se(streq(line, EXPECT1));
493 line = mfree(line);
494
495 assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_QUOTE_POSIX, &line) >= 0);
496 log_debug("got: ==%s==", line);
497 log_debug("exp: ==%s==", EXPECT1p);
498 assert_se(streq(line, EXPECT1p));
499 line = mfree(line);
500
501 #define CMDLINE2 "foo\0\1\2\3\0\0"
502 #define EXPECT2 "foo \"\\001\\002\\003\" \"\" \"\""
503 #define EXPECT2p "foo $'\\001\\002\\003' \"\" \"\""
504 assert_se(lseek(fd, SEEK_SET, 0) == 0);
505 assert_se(write(fd, CMDLINE2, sizeof CMDLINE2) == sizeof CMDLINE2);
506 assert_se(ftruncate(fd, sizeof CMDLINE2) == 0);
507
508 assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_QUOTE, &line) >= 0);
509 log_debug("got: ==%s==", line);
510 log_debug("exp: ==%s==", EXPECT2);
511 assert_se(streq(line, EXPECT2));
512 line = mfree(line);
513
514 assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_QUOTE_POSIX, &line) >= 0);
515 log_debug("got: ==%s==", line);
516 log_debug("exp: ==%s==", EXPECT2p);
517 assert_se(streq(line, EXPECT2p));
518 line = mfree(line);
519
520 safe_close(fd);
521 _exit(EXIT_SUCCESS);
522 }
523
524 static void test_rename_process_now(const char *p, int ret) {
525 _cleanup_free_ char *comm = NULL, *cmdline = NULL;
526 int r;
527
528 log_info("/* %s */", __func__);
529
530 r = rename_process(p);
531 assert_se(r == ret ||
532 (ret == 0 && r >= 0) ||
533 (ret > 0 && r > 0));
534
535 log_debug_errno(r, "rename_process(%s): %m", p);
536
537 if (r < 0)
538 return;
539
540 #if HAVE_VALGRIND_VALGRIND_H
541 /* see above, valgrind is weird, we can't verify what we are doing here */
542 if (RUNNING_ON_VALGRIND)
543 return;
544 #endif
545
546 assert_se(get_process_comm(0, &comm) >= 0);
547 log_debug("comm = <%s>", comm);
548 assert_se(strneq(comm, p, TASK_COMM_LEN-1));
549 /* We expect comm to be at most 16 bytes (TASK_COMM_LEN). The kernel may raise this limit in the
550 * future. We'd only check the initial part, at least until we recompile, but this will still pass. */
551
552 r = get_process_cmdline(0, SIZE_MAX, 0, &cmdline);
553 assert_se(r >= 0);
554 /* we cannot expect cmdline to be renamed properly without privileges */
555 if (geteuid() == 0) {
556 if (r == 0 && detect_container() > 0)
557 log_info("cmdline = <%s> (not verified, Running in unprivileged container?)", cmdline);
558 else {
559 log_info("cmdline = <%s> (expected <%.*s>)", cmdline, (int) strlen("test-process-util"), p);
560
561 bool skip = cmdline[0] == '"'; /* A shortcut to check if the string is quoted */
562
563 assert_se(strneq(cmdline + skip, p, strlen("test-process-util")));
564 assert_se(startswith(cmdline + skip, p));
565 }
566 } else
567 log_info("cmdline = <%s> (not verified)", cmdline);
568 }
569
570 static void test_rename_process_one(const char *p, int ret) {
571 siginfo_t si;
572 pid_t pid;
573
574 log_info("/* %s */", __func__);
575
576 pid = fork();
577 assert_se(pid >= 0);
578
579 if (pid == 0) {
580 /* child */
581 test_rename_process_now(p, ret);
582 _exit(EXIT_SUCCESS);
583 }
584
585 assert_se(wait_for_terminate(pid, &si) >= 0);
586 assert_se(si.si_code == CLD_EXITED);
587 assert_se(si.si_status == EXIT_SUCCESS);
588 }
589
590 static void test_rename_process_multi(void) {
591 pid_t pid;
592
593 pid = fork();
594 assert_se(pid >= 0);
595
596 if (pid > 0) {
597 siginfo_t si;
598
599 assert_se(wait_for_terminate(pid, &si) >= 0);
600 assert_se(si.si_code == CLD_EXITED);
601 assert_se(si.si_status == EXIT_SUCCESS);
602
603 return;
604 }
605
606 /* child */
607 test_rename_process_now("one", 1);
608 test_rename_process_now("more", 0); /* longer than "one", hence truncated */
609 (void) setresuid(99, 99, 99); /* change uid when running privileged */
610 test_rename_process_now("time!", 0);
611 test_rename_process_now("0", 1); /* shorter than "one", should fit */
612 test_rename_process_one("", -EINVAL);
613 test_rename_process_one(NULL, -EINVAL);
614 _exit(EXIT_SUCCESS);
615 }
616
617 static void test_rename_process(void) {
618 test_rename_process_one(NULL, -EINVAL);
619 test_rename_process_one("", -EINVAL);
620 test_rename_process_one("foo", 1); /* should always fit */
621 test_rename_process_one("this is a really really long process name, followed by some more words", 0); /* unlikely to fit */
622 test_rename_process_one("1234567", 1); /* should always fit */
623 test_rename_process_multi(); /* multiple invocations and dropped privileges */
624 }
625
626 static void test_getpid_cached(void) {
627 siginfo_t si;
628 pid_t a, b, c, d, e, f, child;
629
630 log_info("/* %s */", __func__);
631
632 a = raw_getpid();
633 b = getpid_cached();
634 c = getpid();
635
636 assert_se(a == b && a == c);
637
638 child = fork();
639 assert_se(child >= 0);
640
641 if (child == 0) {
642 /* In child */
643 a = raw_getpid();
644 b = getpid_cached();
645 c = getpid();
646
647 assert_se(a == b && a == c);
648 _exit(EXIT_SUCCESS);
649 }
650
651 d = raw_getpid();
652 e = getpid_cached();
653 f = getpid();
654
655 assert_se(a == d && a == e && a == f);
656
657 assert_se(wait_for_terminate(child, &si) >= 0);
658 assert_se(si.si_status == 0);
659 assert_se(si.si_code == CLD_EXITED);
660 }
661
662 static void test_getpid_measure(void) {
663 usec_t t, q;
664
665 unsigned long long iterations = slow_tests_enabled() ? 1000000 : 1000;
666
667 log_info("/* %s (%llu iterations) */", __func__, iterations);
668
669 t = now(CLOCK_MONOTONIC);
670 for (unsigned long long i = 0; i < iterations; i++)
671 (void) getpid();
672 q = now(CLOCK_MONOTONIC) - t;
673
674 log_info(" glibc getpid(): %lf µs each\n", (double) q / iterations);
675
676 iterations *= 50; /* _cached() is about 50 times faster, so we need more iterations */
677
678 t = now(CLOCK_MONOTONIC);
679 for (unsigned long long i = 0; i < iterations; i++)
680 (void) getpid_cached();
681 q = now(CLOCK_MONOTONIC) - t;
682
683 log_info("getpid_cached(): %lf µs each\n", (double) q / iterations);
684 }
685
686 static void test_safe_fork(void) {
687 siginfo_t status;
688 pid_t pid;
689 int r;
690
691 log_info("/* %s */", __func__);
692
693 BLOCK_SIGNALS(SIGCHLD);
694
695 r = safe_fork("(test-child)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_NULL_STDIO|FORK_REOPEN_LOG, &pid);
696 assert_se(r >= 0);
697
698 if (r == 0) {
699 /* child */
700 usleep(100 * USEC_PER_MSEC);
701
702 _exit(88);
703 }
704
705 assert_se(wait_for_terminate(pid, &status) >= 0);
706 assert_se(status.si_code == CLD_EXITED);
707 assert_se(status.si_status == 88);
708 }
709
710 static void test_pid_to_ptr(void) {
711
712 assert_se(PTR_TO_PID(NULL) == 0);
713 assert_se(PID_TO_PTR(0) == NULL);
714
715 assert_se(PTR_TO_PID(PID_TO_PTR(1)) == 1);
716 assert_se(PTR_TO_PID(PID_TO_PTR(2)) == 2);
717 assert_se(PTR_TO_PID(PID_TO_PTR(-1)) == -1);
718 assert_se(PTR_TO_PID(PID_TO_PTR(-2)) == -2);
719
720 assert_se(PTR_TO_PID(PID_TO_PTR(INT16_MAX)) == INT16_MAX);
721 assert_se(PTR_TO_PID(PID_TO_PTR(INT16_MIN)) == INT16_MIN);
722
723 assert_se(PTR_TO_PID(PID_TO_PTR(INT32_MAX)) == INT32_MAX);
724 assert_se(PTR_TO_PID(PID_TO_PTR(INT32_MIN)) == INT32_MIN);
725 }
726
727 static void test_ioprio_class_from_to_string_one(const char *val, int expected) {
728 assert_se(ioprio_class_from_string(val) == expected);
729 if (expected >= 0) {
730 _cleanup_free_ char *s = NULL;
731 unsigned ret;
732
733 assert_se(ioprio_class_to_string_alloc(expected, &s) == 0);
734 /* We sometimes get a class number and sometimes a number back */
735 assert_se(streq(s, val) ||
736 safe_atou(val, &ret) == 0);
737 }
738 }
739
740 static void test_ioprio_class_from_to_string(void) {
741 log_info("/* %s */", __func__);
742
743 test_ioprio_class_from_to_string_one("none", IOPRIO_CLASS_NONE);
744 test_ioprio_class_from_to_string_one("realtime", IOPRIO_CLASS_RT);
745 test_ioprio_class_from_to_string_one("best-effort", IOPRIO_CLASS_BE);
746 test_ioprio_class_from_to_string_one("idle", IOPRIO_CLASS_IDLE);
747 test_ioprio_class_from_to_string_one("0", 0);
748 test_ioprio_class_from_to_string_one("1", 1);
749 test_ioprio_class_from_to_string_one("7", 7);
750 test_ioprio_class_from_to_string_one("8", 8);
751 test_ioprio_class_from_to_string_one("9", -EINVAL);
752 test_ioprio_class_from_to_string_one("-1", -EINVAL);
753 }
754
755 static void test_setpriority_closest(void) {
756 int r;
757
758 log_info("/* %s */", __func__);
759
760 r = safe_fork("(test-setprio)",
761 FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_WAIT|FORK_LOG, NULL);
762 assert_se(r >= 0);
763
764 if (r == 0) {
765 bool full_test;
766 int p, q;
767 /* child */
768
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
772 * the full test */
773 assert_se(ERRNO_IS_PRIVILEGE(errno));
774 full_test = false;
775 } else {
776 assert_se(setresgid(GID_NOBODY, GID_NOBODY, GID_NOBODY) >= 0);
777 assert_se(setresuid(UID_NOBODY, UID_NOBODY, UID_NOBODY) >= 0);
778 full_test = true;
779 }
780
781 errno = 0;
782 p = getpriority(PRIO_PROCESS, 0);
783 assert_se(errno == 0);
784
785 /* It should always be possible to set our nice level to the current one */
786 assert_se(setpriority_closest(p) > 0);
787
788 errno = 0;
789 q = getpriority(PRIO_PROCESS, 0);
790 assert_se(errno == 0 && p == q);
791
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);
795
796 errno = 0;
797 q = getpriority(PRIO_PROCESS, 0);
798 assert_se(errno == 0 && p == q);
799 }
800
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);
804
805 errno = 0;
806 q = getpriority(PRIO_PROCESS, 0);
807 assert_se(errno == 0 && p == q);
808 }
809
810 if (full_test) {
811 /* These two should work, given the RLIMIT_NICE we set above */
812 assert_se(setpriority_closest(-10) > 0);
813 errno = 0;
814 q = getpriority(PRIO_PROCESS, 0);
815 assert_se(errno == 0 && q == -10);
816
817 assert_se(setpriority_closest(-9) > 0);
818 errno = 0;
819 q = getpriority(PRIO_PROCESS, 0);
820 assert_se(errno == 0 && q == -9);
821
822 /* This should succeed but should be clamped to the limit */
823 assert_se(setpriority_closest(-11) == 0);
824 errno = 0;
825 q = getpriority(PRIO_PROCESS, 0);
826 assert_se(errno == 0 && q == -10);
827
828 assert_se(setpriority_closest(-8) > 0);
829 errno = 0;
830 q = getpriority(PRIO_PROCESS, 0);
831 assert_se(errno == 0 && q == -8);
832
833 /* This should succeed but should be clamped to the limit */
834 assert_se(setpriority_closest(-12) == 0);
835 errno = 0;
836 q = getpriority(PRIO_PROCESS, 0);
837 assert_se(errno == 0 && q == -10);
838 }
839
840 _exit(EXIT_SUCCESS);
841 }
842 }
843
844 static void test_get_process_ppid(void) {
845 uint64_t limit;
846 int r;
847
848 log_info("/* %s */", __func__);
849
850 assert_se(get_process_ppid(1, NULL) == -EADDRNOTAVAIL);
851
852 /* the process with the PID above the global limit definitely doesn't exist. Verify that */
853 assert_se(procfs_tasks_get_limit(&limit) >= 0);
854 assert_se(limit >= INT_MAX || get_process_ppid(limit+1, NULL) == -ESRCH);
855
856 for (pid_t pid = 0;;) {
857 _cleanup_free_ char *c1 = NULL, *c2 = NULL;
858 pid_t ppid;
859
860 r = get_process_ppid(pid, &ppid);
861 if (r == -EADDRNOTAVAIL) {
862 log_info("No further parent PID");
863 break;
864 }
865
866 assert_se(r >= 0);
867
868 assert_se(get_process_cmdline(pid, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &c1) >= 0);
869 assert_se(get_process_cmdline(ppid, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &c2) >= 0);
870
871 log_info("Parent of " PID_FMT " (%s) is " PID_FMT " (%s).", pid, c1, ppid, c2);
872
873 pid = ppid;
874 }
875 }
876
877 int main(int argc, char *argv[]) {
878 log_show_color(true);
879 test_setup_logging(LOG_INFO);
880
881 save_argc_argv(argc, argv);
882
883 if (argc > 1) {
884 pid_t pid = 0;
885
886 (void) parse_pid(argv[1], &pid);
887 test_get_process_comm(pid);
888 } else {
889 TEST_REQ_RUNNING_SYSTEMD(test_get_process_comm(1));
890 test_get_process_comm(getpid());
891 }
892
893 test_get_process_comm_escape();
894 test_get_process_cmdline();
895 test_pid_is_unwaited();
896 test_pid_is_alive();
897 test_personality();
898 test_get_process_cmdline_harder();
899 test_rename_process();
900 test_getpid_cached();
901 test_getpid_measure();
902 test_safe_fork();
903 test_pid_to_ptr();
904 test_ioprio_class_from_to_string();
905 test_setpriority_closest();
906 test_get_process_ppid();
907
908 return 0;
909 }