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