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