]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-process-util.c
Merge pull request #21646 from mrc0mmand/lgtm-followups
[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 struct dirent *de;
139
140 assert_se(d = opendir("/proc"));
141
142 FOREACH_DIRENT(de, d, return) {
143 pid_t pid;
144
145 if (de->d_type != DT_DIR)
146 continue;
147
148 if (parse_pid(de->d_name, &pid) < 0)
149 continue;
150
151 test_get_process_cmdline_one(pid);
152 }
153 }
154
155 static void test_get_process_comm_escape_one(const char *input, const char *output) {
156 _cleanup_free_ char *n = NULL;
157
158 log_debug("input: <%s> — output: <%s>", input, output);
159
160 assert_se(prctl(PR_SET_NAME, input) >= 0);
161 assert_se(get_process_comm(0, &n) >= 0);
162
163 log_debug("got: <%s>", n);
164
165 assert_se(streq_ptr(n, output));
166 }
167
168 TEST(get_process_comm_escape) {
169 _cleanup_free_ char *saved = NULL;
170
171 assert_se(get_process_comm(0, &saved) >= 0);
172
173 test_get_process_comm_escape_one("", "");
174 test_get_process_comm_escape_one("foo", "foo");
175 test_get_process_comm_escape_one("012345678901234", "012345678901234");
176 test_get_process_comm_escape_one("0123456789012345", "012345678901234");
177 test_get_process_comm_escape_one("äöüß", "\\303\\244\\303\\266\\303\\274\\303\\237");
178 test_get_process_comm_escape_one("xäöüß", "x\\303\\244\\303\\266\\303\\274\\303\\237");
179 test_get_process_comm_escape_one("xxäöüß", "xx\\303\\244\\303\\266\\303\\274\\303\\237");
180 test_get_process_comm_escape_one("xxxäöüß", "xxx\\303\\244\\303\\266\\303\\274\\303\\237");
181 test_get_process_comm_escape_one("xxxxäöüß", "xxxx\\303\\244\\303\\266\\303\\274\\303\\237");
182 test_get_process_comm_escape_one("xxxxxäöüß", "xxxxx\\303\\244\\303\\266\\303\\274\\303\\237");
183
184 assert_se(prctl(PR_SET_NAME, saved) >= 0);
185 }
186
187 TEST(pid_is_unwaited) {
188 pid_t pid;
189
190 pid = fork();
191 assert_se(pid >= 0);
192 if (pid == 0) {
193 _exit(EXIT_SUCCESS);
194 } else {
195 int status;
196
197 waitpid(pid, &status, 0);
198 assert_se(!pid_is_unwaited(pid));
199 }
200 assert_se(pid_is_unwaited(getpid_cached()));
201 assert_se(!pid_is_unwaited(-1));
202 }
203
204 TEST(pid_is_alive) {
205 pid_t pid;
206
207 pid = fork();
208 assert_se(pid >= 0);
209 if (pid == 0) {
210 _exit(EXIT_SUCCESS);
211 } else {
212 int status;
213
214 waitpid(pid, &status, 0);
215 assert_se(!pid_is_alive(pid));
216 }
217 assert_se(pid_is_alive(getpid_cached()));
218 assert_se(!pid_is_alive(-1));
219 }
220
221 TEST(personality) {
222 assert_se(personality_to_string(PER_LINUX));
223 assert_se(!personality_to_string(PERSONALITY_INVALID));
224
225 assert_se(streq(personality_to_string(PER_LINUX), architecture_to_string(native_architecture())));
226
227 assert_se(personality_from_string(personality_to_string(PER_LINUX)) == PER_LINUX);
228 assert_se(personality_from_string(architecture_to_string(native_architecture())) == PER_LINUX);
229
230 #ifdef __x86_64__
231 assert_se(streq_ptr(personality_to_string(PER_LINUX), "x86-64"));
232 assert_se(streq_ptr(personality_to_string(PER_LINUX32), "x86"));
233
234 assert_se(personality_from_string("x86-64") == PER_LINUX);
235 assert_se(personality_from_string("x86") == PER_LINUX32);
236 assert_se(personality_from_string("ia64") == PERSONALITY_INVALID);
237 assert_se(personality_from_string(NULL) == PERSONALITY_INVALID);
238
239 assert_se(personality_from_string(personality_to_string(PER_LINUX32)) == PER_LINUX32);
240 #endif
241 }
242
243 TEST(get_process_cmdline_harder) {
244 char path[] = "/tmp/test-cmdlineXXXXXX";
245 _cleanup_close_ int fd = -1;
246 _cleanup_free_ char *line = NULL;
247 pid_t pid;
248
249 if (geteuid() != 0) {
250 log_info("Skipping %s: not root", __func__);
251 return;
252 }
253
254 if (!have_namespaces()) {
255 log_notice("Testing without namespaces, skipping %s", __func__);
256 return;
257 }
258
259 #if HAVE_VALGRIND_VALGRIND_H
260 /* valgrind patches open(/proc//cmdline)
261 * so, test_get_process_cmdline_harder fails always
262 * See https://github.com/systemd/systemd/pull/3555#issuecomment-226564908 */
263 if (RUNNING_ON_VALGRIND) {
264 log_info("Skipping %s: running on valgrind", __func__);
265 return;
266 }
267 #endif
268
269 pid = fork();
270 if (pid > 0) {
271 siginfo_t si;
272
273 (void) wait_for_terminate(pid, &si);
274
275 assert_se(si.si_code == CLD_EXITED);
276 assert_se(si.si_status == 0);
277
278 return;
279 }
280
281 assert_se(pid == 0);
282 assert_se(unshare(CLONE_NEWNS) >= 0);
283
284 if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL) < 0) {
285 log_warning_errno(errno, "mount(..., \"/\", MS_SLAVE|MS_REC, ...) failed: %m");
286 assert_se(IN_SET(errno, EPERM, EACCES));
287 return;
288 }
289
290 fd = mkostemp(path, O_CLOEXEC);
291 assert_se(fd >= 0);
292
293 /* Note that we don't unmount the following bind-mount at the end of the test because the kernel
294 * will clear up its /proc/PID/ hierarchy automatically as soon as the test stops. */
295 if (mount(path, "/proc/self/cmdline", "bind", MS_BIND, NULL) < 0) {
296 /* This happens under selinux… Abort the test in this case. */
297 log_warning_errno(errno, "mount(..., \"/proc/self/cmdline\", \"bind\", ...) failed: %m");
298 assert_se(IN_SET(errno, EPERM, EACCES));
299 return;
300 }
301
302 /* Set RLIMIT_STACK to infinity to test we don't try to allocate unncessarily large values to read
303 * the cmdline. */
304 if (setrlimit(RLIMIT_STACK, &RLIMIT_MAKE_CONST(RLIM_INFINITY)) < 0)
305 log_warning("Testing without RLIMIT_STACK=infinity");
306
307 assert_se(unlink(path) >= 0);
308
309 assert_se(prctl(PR_SET_NAME, "testa") >= 0);
310
311 assert_se(get_process_cmdline(0, SIZE_MAX, 0, &line) == -ENOENT);
312
313 assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
314 log_debug("'%s'", line);
315 assert_se(streq(line, "[testa]"));
316 line = mfree(line);
317
318 assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK | PROCESS_CMDLINE_QUOTE, &line) >= 0);
319 log_debug("'%s'", line);
320 assert_se(streq(line, "\"[testa]\"")); /* quoting is enabled here */
321 line = mfree(line);
322
323 assert_se(get_process_cmdline(0, 0, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
324 log_debug("'%s'", line);
325 assert_se(streq(line, ""));
326 line = mfree(line);
327
328 assert_se(get_process_cmdline(0, 1, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
329 assert_se(streq(line, "…"));
330 line = mfree(line);
331
332 assert_se(get_process_cmdline(0, 2, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
333 assert_se(streq(line, "[…"));
334 line = mfree(line);
335
336 assert_se(get_process_cmdline(0, 3, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
337 assert_se(streq(line, "[t…"));
338 line = mfree(line);
339
340 assert_se(get_process_cmdline(0, 4, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
341 assert_se(streq(line, "[te…"));
342 line = mfree(line);
343
344 assert_se(get_process_cmdline(0, 5, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
345 assert_se(streq(line, "[tes…"));
346 line = mfree(line);
347
348 assert_se(get_process_cmdline(0, 6, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
349 assert_se(streq(line, "[test…"));
350 line = mfree(line);
351
352 assert_se(get_process_cmdline(0, 7, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
353 assert_se(streq(line, "[testa]"));
354 line = mfree(line);
355
356 assert_se(get_process_cmdline(0, 8, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
357 assert_se(streq(line, "[testa]"));
358 line = mfree(line);
359
360 /* Test with multiple arguments that don't require quoting */
361
362 assert_se(write(fd, "foo\0bar", 8) == 8);
363
364 assert_se(get_process_cmdline(0, SIZE_MAX, 0, &line) >= 0);
365 log_debug("'%s'", line);
366 assert_se(streq(line, "foo bar"));
367 line = mfree(line);
368
369 assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
370 assert_se(streq(line, "foo bar"));
371 line = mfree(line);
372
373 assert_se(write(fd, "quux", 4) == 4);
374 assert_se(get_process_cmdline(0, SIZE_MAX, 0, &line) >= 0);
375 log_debug("'%s'", line);
376 assert_se(streq(line, "foo bar quux"));
377 line = mfree(line);
378
379 assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
380 log_debug("'%s'", line);
381 assert_se(streq(line, "foo bar quux"));
382 line = mfree(line);
383
384 assert_se(get_process_cmdline(0, 1, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
385 log_debug("'%s'", line);
386 assert_se(streq(line, "…"));
387 line = mfree(line);
388
389 assert_se(get_process_cmdline(0, 2, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
390 log_debug("'%s'", line);
391 assert_se(streq(line, "f…"));
392 line = mfree(line);
393
394 assert_se(get_process_cmdline(0, 3, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
395 log_debug("'%s'", line);
396 assert_se(streq(line, "fo…"));
397 line = mfree(line);
398
399 assert_se(get_process_cmdline(0, 4, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
400 log_debug("'%s'", line);
401 assert_se(streq(line, "foo…"));
402 line = mfree(line);
403
404 assert_se(get_process_cmdline(0, 5, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
405 log_debug("'%s'", line);
406 assert_se(streq(line, "foo …"));
407 line = mfree(line);
408
409 assert_se(get_process_cmdline(0, 6, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
410 log_debug("'%s'", line);
411 assert_se(streq(line, "foo b…"));
412 line = mfree(line);
413
414 assert_se(get_process_cmdline(0, 7, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
415 log_debug("'%s'", line);
416 assert_se(streq(line, "foo ba…"));
417 line = mfree(line);
418
419 assert_se(get_process_cmdline(0, 8, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
420 log_debug("'%s'", line);
421 assert_se(streq(line, "foo bar…"));
422 line = mfree(line);
423
424 assert_se(get_process_cmdline(0, 9, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
425 log_debug("'%s'", line);
426 assert_se(streq(line, "foo bar …"));
427 line = mfree(line);
428
429 assert_se(get_process_cmdline(0, 10, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
430 log_debug("'%s'", line);
431 assert_se(streq(line, "foo bar q…"));
432 line = mfree(line);
433
434 assert_se(get_process_cmdline(0, 11, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
435 log_debug("'%s'", line);
436 assert_se(streq(line, "foo bar qu…"));
437 line = mfree(line);
438
439 assert_se(get_process_cmdline(0, 12, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
440 log_debug("'%s'", line);
441 assert_se(streq(line, "foo bar quux"));
442 line = mfree(line);
443
444 assert_se(get_process_cmdline(0, 13, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
445 log_debug("'%s'", line);
446 assert_se(streq(line, "foo bar quux"));
447 line = mfree(line);
448
449 assert_se(get_process_cmdline(0, 14, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
450 log_debug("'%s'", line);
451 assert_se(streq(line, "foo bar quux"));
452 line = mfree(line);
453
454 assert_se(get_process_cmdline(0, 1000, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
455 log_debug("'%s'", line);
456 assert_se(streq(line, "foo bar quux"));
457 line = mfree(line);
458
459 assert_se(ftruncate(fd, 0) >= 0);
460 assert_se(prctl(PR_SET_NAME, "aaaa bbbb cccc") >= 0);
461
462 assert_se(get_process_cmdline(0, SIZE_MAX, 0, &line) == -ENOENT);
463
464 assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
465 log_debug("'%s'", line);
466 assert_se(streq(line, "[aaaa bbbb cccc]"));
467 line = mfree(line);
468
469 assert_se(get_process_cmdline(0, 10, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
470 log_debug("'%s'", line);
471 assert_se(streq(line, "[aaaa bbb…"));
472 line = mfree(line);
473
474 assert_se(get_process_cmdline(0, 11, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
475 log_debug("'%s'", line);
476 assert_se(streq(line, "[aaaa bbbb…"));
477 line = mfree(line);
478
479 assert_se(get_process_cmdline(0, 12, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
480 log_debug("'%s'", line);
481 assert_se(streq(line, "[aaaa bbbb …"));
482 line = mfree(line);
483
484 /* Test with multiple arguments that do require quoting */
485
486 #define CMDLINE1 "foo\0'bar'\0\"bar$\"\0x y z\0!``\0"
487 #define EXPECT1 "foo \"'bar'\" \"\\\"bar\\$\\\"\" \"x y z\" \"!\\`\\`\""
488 #define EXPECT1p "foo $'\\'bar\\'' $'\"bar$\"' $'x y z' $'!``'"
489 assert_se(lseek(fd, SEEK_SET, 0) == 0);
490 assert_se(write(fd, CMDLINE1, sizeof CMDLINE1) == sizeof CMDLINE1);
491 assert_se(ftruncate(fd, sizeof CMDLINE1) == 0);
492
493 assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_QUOTE, &line) >= 0);
494 log_debug("got: ==%s==", line);
495 log_debug("exp: ==%s==", EXPECT1);
496 assert_se(streq(line, EXPECT1));
497 line = mfree(line);
498
499 assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_QUOTE_POSIX, &line) >= 0);
500 log_debug("got: ==%s==", line);
501 log_debug("exp: ==%s==", EXPECT1p);
502 assert_se(streq(line, EXPECT1p));
503 line = mfree(line);
504
505 #define CMDLINE2 "foo\0\1\2\3\0\0"
506 #define EXPECT2 "foo \"\\001\\002\\003\""
507 #define EXPECT2p "foo $'\\001\\002\\003'"
508 assert_se(lseek(fd, SEEK_SET, 0) == 0);
509 assert_se(write(fd, CMDLINE2, sizeof CMDLINE2) == sizeof CMDLINE2);
510 assert_se(ftruncate(fd, sizeof CMDLINE2) == 0);
511
512 assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_QUOTE, &line) >= 0);
513 log_debug("got: ==%s==", line);
514 log_debug("exp: ==%s==", EXPECT2);
515 assert_se(streq(line, EXPECT2));
516 line = mfree(line);
517
518 assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_QUOTE_POSIX, &line) >= 0);
519 log_debug("got: ==%s==", line);
520 log_debug("exp: ==%s==", EXPECT2p);
521 assert_se(streq(line, EXPECT2p));
522 line = mfree(line);
523
524 safe_close(fd);
525 _exit(EXIT_SUCCESS);
526 }
527
528 static void test_rename_process_now(const char *p, int ret) {
529 _cleanup_free_ char *comm = NULL, *cmdline = NULL;
530 int r;
531
532 log_info("/* %s */", __func__);
533
534 r = rename_process(p);
535 assert_se(r == ret ||
536 (ret == 0 && r >= 0) ||
537 (ret > 0 && r > 0));
538
539 log_debug_errno(r, "rename_process(%s): %m", p);
540
541 if (r < 0)
542 return;
543
544 #if HAVE_VALGRIND_VALGRIND_H
545 /* see above, valgrind is weird, we can't verify what we are doing here */
546 if (RUNNING_ON_VALGRIND)
547 return;
548 #endif
549
550 assert_se(get_process_comm(0, &comm) >= 0);
551 log_debug("comm = <%s>", comm);
552 assert_se(strneq(comm, p, TASK_COMM_LEN-1));
553 /* We expect comm to be at most 16 bytes (TASK_COMM_LEN). The kernel may raise this limit in the
554 * future. We'd only check the initial part, at least until we recompile, but this will still pass. */
555
556 r = get_process_cmdline(0, SIZE_MAX, 0, &cmdline);
557 assert_se(r >= 0);
558 /* we cannot expect cmdline to be renamed properly without privileges */
559 if (geteuid() == 0) {
560 if (r == 0 && detect_container() > 0)
561 log_info("cmdline = <%s> (not verified, Running in unprivileged container?)", cmdline);
562 else {
563 log_info("cmdline = <%s> (expected <%.*s>)", cmdline, (int) strlen("test-process-util"), p);
564
565 bool skip = cmdline[0] == '"'; /* A shortcut to check if the string is quoted */
566
567 assert_se(strneq(cmdline + skip, p, strlen("test-process-util")));
568 assert_se(startswith(cmdline + skip, p));
569 }
570 } else
571 log_info("cmdline = <%s> (not verified)", cmdline);
572 }
573
574 static void test_rename_process_one(const char *p, int ret) {
575 siginfo_t si;
576 pid_t pid;
577
578 log_info("/* %s */", __func__);
579
580 pid = fork();
581 assert_se(pid >= 0);
582
583 if (pid == 0) {
584 /* child */
585 test_rename_process_now(p, ret);
586 _exit(EXIT_SUCCESS);
587 }
588
589 assert_se(wait_for_terminate(pid, &si) >= 0);
590 assert_se(si.si_code == CLD_EXITED);
591 assert_se(si.si_status == EXIT_SUCCESS);
592 }
593
594 TEST(rename_process_multi) {
595 pid_t pid;
596
597 pid = fork();
598 assert_se(pid >= 0);
599
600 if (pid > 0) {
601 siginfo_t si;
602
603 assert_se(wait_for_terminate(pid, &si) >= 0);
604 assert_se(si.si_code == CLD_EXITED);
605 assert_se(si.si_status == EXIT_SUCCESS);
606
607 return;
608 }
609
610 /* child */
611 test_rename_process_now("one", 1);
612 test_rename_process_now("more", 0); /* longer than "one", hence truncated */
613 (void) setresuid(99, 99, 99); /* change uid when running privileged */
614 test_rename_process_now("time!", 0);
615 test_rename_process_now("0", 1); /* shorter than "one", should fit */
616 test_rename_process_one("", -EINVAL);
617 test_rename_process_one(NULL, -EINVAL);
618 _exit(EXIT_SUCCESS);
619 }
620
621 TEST(rename_process) {
622 test_rename_process_one(NULL, -EINVAL);
623 test_rename_process_one("", -EINVAL);
624 test_rename_process_one("foo", 1); /* should always fit */
625 test_rename_process_one("this is a really really long process name, followed by some more words", 0); /* unlikely to fit */
626 test_rename_process_one("1234567", 1); /* should always fit */
627 }
628
629 TEST(getpid_cached) {
630 siginfo_t si;
631 pid_t a, b, c, d, e, f, child;
632
633 a = raw_getpid();
634 b = getpid_cached();
635 c = getpid();
636
637 assert_se(a == b && a == c);
638
639 child = fork();
640 assert_se(child >= 0);
641
642 if (child == 0) {
643 /* In child */
644 a = raw_getpid();
645 b = getpid_cached();
646 c = getpid();
647
648 assert_se(a == b && a == c);
649 _exit(EXIT_SUCCESS);
650 }
651
652 d = raw_getpid();
653 e = getpid_cached();
654 f = getpid();
655
656 assert_se(a == d && a == e && a == f);
657
658 assert_se(wait_for_terminate(child, &si) >= 0);
659 assert_se(si.si_status == 0);
660 assert_se(si.si_code == CLD_EXITED);
661 }
662
663 TEST(getpid_measure) {
664 usec_t t, q;
665
666 unsigned long long iterations = slow_tests_enabled() ? 1000000 : 1000;
667
668 log_info("/* %s (%llu iterations) */", __func__, iterations);
669
670 t = now(CLOCK_MONOTONIC);
671 for (unsigned long long i = 0; i < iterations; i++)
672 (void) getpid();
673 q = now(CLOCK_MONOTONIC) - t;
674
675 log_info(" glibc getpid(): %lf µs each\n", (double) q / iterations);
676
677 iterations *= 50; /* _cached() is about 50 times faster, so we need more iterations */
678
679 t = now(CLOCK_MONOTONIC);
680 for (unsigned long long i = 0; i < iterations; i++)
681 (void) getpid_cached();
682 q = now(CLOCK_MONOTONIC) - t;
683
684 log_info("getpid_cached(): %lf µs each\n", (double) q / iterations);
685 }
686
687 TEST(safe_fork) {
688 siginfo_t status;
689 pid_t pid;
690 int r;
691
692 BLOCK_SIGNALS(SIGCHLD);
693
694 r = safe_fork("(test-child)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_NULL_STDIO|FORK_REOPEN_LOG, &pid);
695 assert_se(r >= 0);
696
697 if (r == 0) {
698 /* child */
699 usleep(100 * USEC_PER_MSEC);
700
701 _exit(88);
702 }
703
704 assert_se(wait_for_terminate(pid, &status) >= 0);
705 assert_se(status.si_code == CLD_EXITED);
706 assert_se(status.si_status == 88);
707 }
708
709 TEST(pid_to_ptr) {
710 assert_se(PTR_TO_PID(NULL) == 0);
711 assert_se(PID_TO_PTR(0) == NULL);
712
713 assert_se(PTR_TO_PID(PID_TO_PTR(1)) == 1);
714 assert_se(PTR_TO_PID(PID_TO_PTR(2)) == 2);
715 assert_se(PTR_TO_PID(PID_TO_PTR(-1)) == -1);
716 assert_se(PTR_TO_PID(PID_TO_PTR(-2)) == -2);
717
718 assert_se(PTR_TO_PID(PID_TO_PTR(INT16_MAX)) == INT16_MAX);
719 assert_se(PTR_TO_PID(PID_TO_PTR(INT16_MIN)) == INT16_MIN);
720
721 assert_se(PTR_TO_PID(PID_TO_PTR(INT32_MAX)) == INT32_MAX);
722 assert_se(PTR_TO_PID(PID_TO_PTR(INT32_MIN)) == INT32_MIN);
723 }
724
725 static void test_ioprio_class_from_to_string_one(const char *val, int expected, int normalized) {
726 assert_se(ioprio_class_from_string(val) == expected);
727 if (expected >= 0) {
728 _cleanup_free_ char *s = NULL;
729 unsigned ret;
730 int combined;
731
732 assert_se(ioprio_class_to_string_alloc(expected, &s) == 0);
733 /* We sometimes get a class number and sometimes a name back */
734 assert_se(streq(s, val) ||
735 safe_atou(val, &ret) == 0);
736
737 /* Make sure normalization works, i.e. NONE → BE gets normalized */
738 combined = ioprio_normalize(ioprio_prio_value(expected, 0));
739 assert_se(ioprio_prio_class(combined) == normalized);
740 assert_se(expected != IOPRIO_CLASS_NONE || ioprio_prio_data(combined) == 4);
741 }
742 }
743
744 TEST(ioprio_class_from_to_string) {
745 test_ioprio_class_from_to_string_one("none", IOPRIO_CLASS_NONE, IOPRIO_CLASS_BE);
746 test_ioprio_class_from_to_string_one("realtime", IOPRIO_CLASS_RT, IOPRIO_CLASS_RT);
747 test_ioprio_class_from_to_string_one("best-effort", IOPRIO_CLASS_BE, IOPRIO_CLASS_BE);
748 test_ioprio_class_from_to_string_one("idle", IOPRIO_CLASS_IDLE, IOPRIO_CLASS_IDLE);
749 test_ioprio_class_from_to_string_one("0", IOPRIO_CLASS_NONE, IOPRIO_CLASS_BE);
750 test_ioprio_class_from_to_string_one("1", 1, 1);
751 test_ioprio_class_from_to_string_one("7", 7, 7);
752 test_ioprio_class_from_to_string_one("8", 8, 8);
753 test_ioprio_class_from_to_string_one("9", -EINVAL, -EINVAL);
754 test_ioprio_class_from_to_string_one("-1", -EINVAL, -EINVAL);
755 }
756
757 TEST(setpriority_closest) {
758 int r;
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 TEST(get_process_ppid) {
845 uint64_t limit;
846 int r;
847
848 assert_se(get_process_ppid(1, NULL) == -EADDRNOTAVAIL);
849
850 /* the process with the PID above the global limit definitely doesn't exist. Verify that */
851 assert_se(procfs_get_pid_max(&limit) >= 0);
852 log_debug("kernel.pid_max = %"PRIu64, limit);
853
854 if (limit < INT_MAX) {
855 r = get_process_ppid(limit + 1, NULL);
856 log_debug_errno(r, "get_process_limit(%"PRIu64") → %d/%m", limit + 1, r);
857 assert(r == -ESRCH);
858 }
859
860 for (pid_t pid = 0;;) {
861 _cleanup_free_ char *c1 = NULL, *c2 = NULL;
862 pid_t ppid;
863
864 r = get_process_ppid(pid, &ppid);
865 if (r == -EADDRNOTAVAIL) {
866 log_info("No further parent PID");
867 break;
868 }
869
870 assert_se(r >= 0);
871
872 assert_se(get_process_cmdline(pid, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &c1) >= 0);
873 assert_se(get_process_cmdline(ppid, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &c2) >= 0);
874
875 log_info("Parent of " PID_FMT " (%s) is " PID_FMT " (%s).", pid, c1, ppid, c2);
876
877 pid = ppid;
878 }
879 }
880
881 TEST(set_oom_score_adjust) {
882 int a, b, r;
883
884 assert_se(get_oom_score_adjust(&a) >= 0);
885
886 r = set_oom_score_adjust(OOM_SCORE_ADJ_MIN);
887 assert_se(r >= 0 || ERRNO_IS_PRIVILEGE(r));
888
889 if (r >= 0) {
890 assert_se(get_oom_score_adjust(&b) >= 0);
891 assert_se(b == OOM_SCORE_ADJ_MIN);
892 }
893
894 assert_se(set_oom_score_adjust(a) >= 0);
895 assert_se(get_oom_score_adjust(&b) >= 0);
896 assert_se(b == a);
897 }
898
899 DEFINE_CUSTOM_TEST_MAIN(LOG_INFO, log_show_color(true), /* no outro */);