]> git.proxmox.com Git - systemd.git/blame - src/test/test-process-util.c
New upstream version 240
[systemd.git] / src / test / test-process-util.c
CommitLineData
52ad194e 1/* SPDX-License-Identifier: LGPL-2.1+ */
e3bff60a 2
5a920b42
MP
3#include <sched.h>
4#include <sys/mount.h>
aa27b158 5#include <sys/personality.h>
5a920b42 6#include <sys/prctl.h>
e3bff60a 7#include <sys/stat.h>
db2df898 8#include <sys/types.h>
e3bff60a
MP
9#include <sys/wait.h>
10#include <unistd.h>
f5e65279 11#if HAVE_VALGRIND_VALGRIND_H
5a920b42
MP
12#include <valgrind/valgrind.h>
13#endif
e3bff60a 14
db2df898 15#include "alloc-util.h"
aa27b158 16#include "architecture.h"
5a920b42 17#include "fd-util.h"
e3bff60a 18#include "log.h"
e3bff60a 19#include "macro.h"
6e866b33 20#include "missing.h"
5a920b42 21#include "parse-util.h"
db2df898 22#include "process-util.h"
1d42b86d 23#include "signal-util.h"
5a920b42 24#include "stdio-util.h"
db2df898 25#include "string-util.h"
e3bff60a 26#include "terminal-util.h"
8a584da2 27#include "test-helper.h"
6e866b33 28#include "tests.h"
db2df898
MP
29#include "util.h"
30#include "virt.h"
e3bff60a 31
5a920b42 32static void test_get_process_comm(pid_t pid) {
e3bff60a 33 struct stat st;
5a920b42 34 _cleanup_free_ char *a = NULL, *c = NULL, *d = NULL, *f = NULL, *i = NULL;
e3bff60a 35 _cleanup_free_ char *env = NULL;
52ad194e 36 char path[STRLEN("/proc//comm") + DECIMAL_STR_MAX(pid_t)];
e3bff60a
MP
37 pid_t e;
38 uid_t u;
39 gid_t g;
40 dev_t h;
41 int r;
e3bff60a 42
5a920b42 43 xsprintf(path, "/proc/"PID_FMT"/comm", pid);
e3bff60a 44
5a920b42
MP
45 if (stat(path, &st) == 0) {
46 assert_se(get_process_comm(pid, &a) >= 0);
47 log_info("PID"PID_FMT" comm: '%s'", pid, a);
48 } else
49 log_warning("%s not exist.", path);
e3bff60a 50
5a920b42
MP
51 assert_se(get_process_cmdline(pid, 0, true, &c) >= 0);
52 log_info("PID"PID_FMT" cmdline: '%s'", pid, c);
e3bff60a 53
5a920b42
MP
54 assert_se(get_process_cmdline(pid, 8, false, &d) >= 0);
55 log_info("PID"PID_FMT" cmdline truncated to 8: '%s'", pid, d);
e3bff60a 56
5a920b42
MP
57 free(d);
58 assert_se(get_process_cmdline(pid, 1, false, &d) >= 0);
59 log_info("PID"PID_FMT" cmdline truncated to 1: '%s'", pid, d);
e3bff60a 60
5a920b42
MP
61 assert_se(get_process_ppid(pid, &e) >= 0);
62 log_info("PID"PID_FMT" PPID: "PID_FMT, pid, e);
63 assert_se(pid == 1 ? e == 0 : e > 0);
e3bff60a 64
5a920b42 65 assert_se(is_kernel_thread(pid) == 0 || pid != 1);
e3bff60a 66
5a920b42 67 r = get_process_exe(pid, &f);
e3bff60a 68 assert_se(r >= 0 || r == -EACCES);
5a920b42 69 log_info("PID"PID_FMT" exe: '%s'", pid, strna(f));
e3bff60a 70
5a920b42
MP
71 assert_se(get_process_uid(pid, &u) == 0);
72 log_info("PID"PID_FMT" UID: "UID_FMT, pid, u);
73 assert_se(u == 0 || pid != 1);
e3bff60a 74
5a920b42
MP
75 assert_se(get_process_gid(pid, &g) == 0);
76 log_info("PID"PID_FMT" GID: "GID_FMT, pid, g);
77 assert_se(g == 0 || pid != 1);
78
79 r = get_process_environ(pid, &env);
e3bff60a 80 assert_se(r >= 0 || r == -EACCES);
5a920b42 81 log_info("PID"PID_FMT" strlen(environ): %zi", pid, env ? (ssize_t)strlen(env) : (ssize_t)-errno);
e3bff60a 82
6300502b 83 if (!detect_container())
5a920b42 84 assert_se(get_ctty_devnr(pid, &h) == -ENXIO || pid != 1);
e3bff60a 85
98393f85 86 (void) getenv_for_pid(pid, "PATH", &i);
5a920b42 87 log_info("PID"PID_FMT" $PATH: '%s'", pid, strna(i));
e3bff60a
MP
88}
89
b012e921
MB
90static void test_get_process_comm_escape_one(const char *input, const char *output) {
91 _cleanup_free_ char *n = NULL;
92
93 log_info("input: <%s> — output: <%s>", input, output);
94
95 assert_se(prctl(PR_SET_NAME, input) >= 0);
96 assert_se(get_process_comm(0, &n) >= 0);
97
98 log_info("got: <%s>", n);
99
100 assert_se(streq_ptr(n, output));
101}
102
103static void test_get_process_comm_escape(void) {
104 _cleanup_free_ char *saved = NULL;
105
106 assert_se(get_process_comm(0, &saved) >= 0);
107
108 test_get_process_comm_escape_one("", "");
109 test_get_process_comm_escape_one("foo", "foo");
110 test_get_process_comm_escape_one("012345678901234", "012345678901234");
111 test_get_process_comm_escape_one("0123456789012345", "012345678901234");
112 test_get_process_comm_escape_one("äöüß", "\\303\\244\\303…");
113 test_get_process_comm_escape_one("xäöüß", "x\\303\\244…");
114 test_get_process_comm_escape_one("xxäöüß", "xx\\303\\244…");
115 test_get_process_comm_escape_one("xxxäöüß", "xxx\\303\\244…");
116 test_get_process_comm_escape_one("xxxxäöüß", "xxxx\\303\\244…");
117 test_get_process_comm_escape_one("xxxxxäöüß", "xxxxx\\303…");
118
119 assert_se(prctl(PR_SET_NAME, saved) >= 0);
120}
121
e3bff60a
MP
122static void test_pid_is_unwaited(void) {
123 pid_t pid;
124
125 pid = fork();
126 assert_se(pid >= 0);
127 if (pid == 0) {
128 _exit(EXIT_SUCCESS);
129 } else {
130 int status;
131
132 waitpid(pid, &status, 0);
133 assert_se(!pid_is_unwaited(pid));
134 }
f5e65279 135 assert_se(pid_is_unwaited(getpid_cached()));
e3bff60a
MP
136 assert_se(!pid_is_unwaited(-1));
137}
138
139static void test_pid_is_alive(void) {
140 pid_t pid;
141
142 pid = fork();
143 assert_se(pid >= 0);
144 if (pid == 0) {
145 _exit(EXIT_SUCCESS);
146 } else {
147 int status;
148
149 waitpid(pid, &status, 0);
150 assert_se(!pid_is_alive(pid));
151 }
f5e65279 152 assert_se(pid_is_alive(getpid_cached()));
e3bff60a
MP
153 assert_se(!pid_is_alive(-1));
154}
155
aa27b158
MP
156static void test_personality(void) {
157
158 assert_se(personality_to_string(PER_LINUX));
159 assert_se(!personality_to_string(PERSONALITY_INVALID));
160
161 assert_se(streq(personality_to_string(PER_LINUX), architecture_to_string(native_architecture())));
162
163 assert_se(personality_from_string(personality_to_string(PER_LINUX)) == PER_LINUX);
164 assert_se(personality_from_string(architecture_to_string(native_architecture())) == PER_LINUX);
165
166#ifdef __x86_64__
167 assert_se(streq_ptr(personality_to_string(PER_LINUX), "x86-64"));
168 assert_se(streq_ptr(personality_to_string(PER_LINUX32), "x86"));
169
170 assert_se(personality_from_string("x86-64") == PER_LINUX);
171 assert_se(personality_from_string("x86") == PER_LINUX32);
172 assert_se(personality_from_string("ia64") == PERSONALITY_INVALID);
173 assert_se(personality_from_string(NULL) == PERSONALITY_INVALID);
174
175 assert_se(personality_from_string(personality_to_string(PER_LINUX32)) == PER_LINUX32);
176#endif
177}
178
5a920b42
MP
179static void test_get_process_cmdline_harder(void) {
180 char path[] = "/tmp/test-cmdlineXXXXXX";
181 _cleanup_close_ int fd = -1;
182 _cleanup_free_ char *line = NULL;
183 pid_t pid;
184
6e866b33
MB
185 if (geteuid() != 0) {
186 log_info("Skipping %s: not root", __func__);
5a920b42 187 return;
6e866b33
MB
188 }
189
190 if (!have_namespaces()) {
191 log_notice("Testing without namespaces, skipping %s", __func__);
192 return;
193 }
5a920b42 194
f5e65279 195#if HAVE_VALGRIND_VALGRIND_H
5a920b42
MP
196 /* valgrind patches open(/proc//cmdline)
197 * so, test_get_process_cmdline_harder fails always
198 * See https://github.com/systemd/systemd/pull/3555#issuecomment-226564908 */
6e866b33
MB
199 if (RUNNING_ON_VALGRIND) {
200 log_info("Skipping %s: running on valgrind", __func__);
5a920b42 201 return;
6e866b33 202 }
5a920b42
MP
203#endif
204
205 pid = fork();
206 if (pid > 0) {
207 siginfo_t si;
208
209 (void) wait_for_terminate(pid, &si);
210
211 assert_se(si.si_code == CLD_EXITED);
212 assert_se(si.si_status == 0);
213
214 return;
215 }
216
217 assert_se(pid == 0);
218 assert_se(unshare(CLONE_NEWNS) >= 0);
219
6e866b33
MB
220 if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL) < 0) {
221 log_warning_errno(errno, "mount(..., \"/\", MS_SLAVE|MS_REC, ...) failed: %m");
222 assert_se(IN_SET(errno, EPERM, EACCES));
223 return;
224 }
b012e921 225
5a920b42
MP
226 fd = mkostemp(path, O_CLOEXEC);
227 assert_se(fd >= 0);
2897b343 228
6e866b33
MB
229 /* Note that we don't unmount the following bind-mount at the end of the test because the kernel
230 * will clear up its /proc/PID/ hierarchy automatically as soon as the test stops. */
2897b343
MP
231 if (mount(path, "/proc/self/cmdline", "bind", MS_BIND, NULL) < 0) {
232 /* This happens under selinux… Abort the test in this case. */
233 log_warning_errno(errno, "mount(..., \"/proc/self/cmdline\", \"bind\", ...) failed: %m");
6e866b33 234 assert_se(IN_SET(errno, EPERM, EACCES));
2897b343
MP
235 return;
236 }
237
5a920b42
MP
238 assert_se(unlink(path) >= 0);
239
240 assert_se(prctl(PR_SET_NAME, "testa") >= 0);
241
f5e65279 242 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) == -ENOENT);
5a920b42 243
f5e65279 244 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0);
5a920b42
MP
245 assert_se(streq(line, "[testa]"));
246 line = mfree(line);
247
f5e65279 248 assert_se(get_process_cmdline(getpid_cached(), 1, true, &line) >= 0);
5a920b42
MP
249 assert_se(streq(line, ""));
250 line = mfree(line);
251
f5e65279 252 assert_se(get_process_cmdline(getpid_cached(), 2, true, &line) >= 0);
5a920b42
MP
253 assert_se(streq(line, "["));
254 line = mfree(line);
255
f5e65279 256 assert_se(get_process_cmdline(getpid_cached(), 3, true, &line) >= 0);
5a920b42
MP
257 assert_se(streq(line, "[."));
258 line = mfree(line);
259
f5e65279 260 assert_se(get_process_cmdline(getpid_cached(), 4, true, &line) >= 0);
5a920b42
MP
261 assert_se(streq(line, "[.."));
262 line = mfree(line);
263
f5e65279 264 assert_se(get_process_cmdline(getpid_cached(), 5, true, &line) >= 0);
5a920b42
MP
265 assert_se(streq(line, "[..."));
266 line = mfree(line);
267
f5e65279 268 assert_se(get_process_cmdline(getpid_cached(), 6, true, &line) >= 0);
5a920b42
MP
269 assert_se(streq(line, "[...]"));
270 line = mfree(line);
271
f5e65279 272 assert_se(get_process_cmdline(getpid_cached(), 7, true, &line) >= 0);
5a920b42
MP
273 assert_se(streq(line, "[t...]"));
274 line = mfree(line);
275
f5e65279 276 assert_se(get_process_cmdline(getpid_cached(), 8, true, &line) >= 0);
5a920b42
MP
277 assert_se(streq(line, "[testa]"));
278 line = mfree(line);
279
280 assert_se(write(fd, "\0\0\0\0\0\0\0\0\0", 10) == 10);
281
f5e65279 282 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) == -ENOENT);
5a920b42 283
f5e65279 284 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0);
5a920b42
MP
285 assert_se(streq(line, "[testa]"));
286 line = mfree(line);
287
288 assert_se(write(fd, "foo\0bar\0\0\0\0\0", 10) == 10);
289
f5e65279 290 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) >= 0);
5a920b42
MP
291 assert_se(streq(line, "foo bar"));
292 line = mfree(line);
293
f5e65279 294 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0);
5a920b42
MP
295 assert_se(streq(line, "foo bar"));
296 line = mfree(line);
297
298 assert_se(write(fd, "quux", 4) == 4);
f5e65279 299 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) >= 0);
5a920b42
MP
300 assert_se(streq(line, "foo bar quux"));
301 line = mfree(line);
302
f5e65279 303 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0);
5a920b42
MP
304 assert_se(streq(line, "foo bar quux"));
305 line = mfree(line);
306
f5e65279 307 assert_se(get_process_cmdline(getpid_cached(), 1, true, &line) >= 0);
5a920b42
MP
308 assert_se(streq(line, ""));
309 line = mfree(line);
310
f5e65279 311 assert_se(get_process_cmdline(getpid_cached(), 2, true, &line) >= 0);
5a920b42
MP
312 assert_se(streq(line, "."));
313 line = mfree(line);
314
f5e65279 315 assert_se(get_process_cmdline(getpid_cached(), 3, true, &line) >= 0);
5a920b42
MP
316 assert_se(streq(line, ".."));
317 line = mfree(line);
318
f5e65279 319 assert_se(get_process_cmdline(getpid_cached(), 4, true, &line) >= 0);
5a920b42
MP
320 assert_se(streq(line, "..."));
321 line = mfree(line);
322
f5e65279 323 assert_se(get_process_cmdline(getpid_cached(), 5, true, &line) >= 0);
5a920b42
MP
324 assert_se(streq(line, "f..."));
325 line = mfree(line);
326
f5e65279 327 assert_se(get_process_cmdline(getpid_cached(), 6, true, &line) >= 0);
5a920b42
MP
328 assert_se(streq(line, "fo..."));
329 line = mfree(line);
330
f5e65279 331 assert_se(get_process_cmdline(getpid_cached(), 7, true, &line) >= 0);
5a920b42
MP
332 assert_se(streq(line, "foo..."));
333 line = mfree(line);
334
f5e65279 335 assert_se(get_process_cmdline(getpid_cached(), 8, true, &line) >= 0);
5a920b42
MP
336 assert_se(streq(line, "foo..."));
337 line = mfree(line);
338
f5e65279 339 assert_se(get_process_cmdline(getpid_cached(), 9, true, &line) >= 0);
5a920b42
MP
340 assert_se(streq(line, "foo b..."));
341 line = mfree(line);
342
f5e65279 343 assert_se(get_process_cmdline(getpid_cached(), 10, true, &line) >= 0);
5a920b42
MP
344 assert_se(streq(line, "foo ba..."));
345 line = mfree(line);
346
f5e65279 347 assert_se(get_process_cmdline(getpid_cached(), 11, true, &line) >= 0);
5a920b42
MP
348 assert_se(streq(line, "foo bar..."));
349 line = mfree(line);
350
f5e65279 351 assert_se(get_process_cmdline(getpid_cached(), 12, true, &line) >= 0);
5a920b42
MP
352 assert_se(streq(line, "foo bar..."));
353 line = mfree(line);
354
f5e65279 355 assert_se(get_process_cmdline(getpid_cached(), 13, true, &line) >= 0);
5a920b42
MP
356 assert_se(streq(line, "foo bar quux"));
357 line = mfree(line);
358
f5e65279 359 assert_se(get_process_cmdline(getpid_cached(), 14, true, &line) >= 0);
5a920b42
MP
360 assert_se(streq(line, "foo bar quux"));
361 line = mfree(line);
362
f5e65279 363 assert_se(get_process_cmdline(getpid_cached(), 1000, true, &line) >= 0);
5a920b42
MP
364 assert_se(streq(line, "foo bar quux"));
365 line = mfree(line);
366
367 assert_se(ftruncate(fd, 0) >= 0);
368 assert_se(prctl(PR_SET_NAME, "aaaa bbbb cccc") >= 0);
369
f5e65279 370 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) == -ENOENT);
5a920b42 371
f5e65279 372 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0);
5a920b42
MP
373 assert_se(streq(line, "[aaaa bbbb cccc]"));
374 line = mfree(line);
375
f5e65279 376 assert_se(get_process_cmdline(getpid_cached(), 10, true, &line) >= 0);
5a920b42
MP
377 assert_se(streq(line, "[aaaa...]"));
378 line = mfree(line);
379
f5e65279 380 assert_se(get_process_cmdline(getpid_cached(), 11, true, &line) >= 0);
5a920b42
MP
381 assert_se(streq(line, "[aaaa...]"));
382 line = mfree(line);
383
f5e65279 384 assert_se(get_process_cmdline(getpid_cached(), 12, true, &line) >= 0);
5a920b42
MP
385 assert_se(streq(line, "[aaaa b...]"));
386 line = mfree(line);
387
388 safe_close(fd);
1d42b86d 389 _exit(EXIT_SUCCESS);
5a920b42
MP
390}
391
f5e65279 392static void test_rename_process_now(const char *p, int ret) {
2897b343 393 _cleanup_free_ char *comm = NULL, *cmdline = NULL;
2897b343
MP
394 int r;
395
2897b343 396 r = rename_process(p);
2897b343
MP
397 assert_se(r == ret ||
398 (ret == 0 && r >= 0) ||
399 (ret > 0 && r > 0));
400
401 if (r < 0)
f5e65279 402 return;
2897b343 403
f5e65279 404#if HAVE_VALGRIND_VALGRIND_H
2897b343
MP
405 /* see above, valgrind is weird, we can't verify what we are doing here */
406 if (RUNNING_ON_VALGRIND)
f5e65279 407 return;
2897b343
MP
408#endif
409
410 assert_se(get_process_comm(0, &comm) >= 0);
411 log_info("comm = <%s>", comm);
b012e921 412 assert_se(strneq(comm, p, TASK_COMM_LEN-1));
2897b343 413
6e866b33
MB
414 r = get_process_cmdline(0, 0, false, &cmdline);
415 assert_se(r >= 0);
f5e65279
MB
416 /* we cannot expect cmdline to be renamed properly without privileges */
417 if (geteuid() == 0) {
6e866b33
MB
418 if (r == 0 && detect_container() > 0)
419 log_info("cmdline = <%s> (not verified, Running in unprivileged container?)", cmdline);
420 else {
421 log_info("cmdline = <%s>", cmdline);
422 assert_se(strneq(p, cmdline, STRLEN("test-process-util")));
423 assert_se(startswith(p, cmdline));
424 }
f5e65279
MB
425 } else
426 log_info("cmdline = <%s> (not verified)", cmdline);
427}
428
429static void test_rename_process_one(const char *p, int ret) {
430 siginfo_t si;
431 pid_t pid;
432
433 pid = fork();
434 assert_se(pid >= 0);
435
436 if (pid == 0) {
437 /* child */
438 test_rename_process_now(p, ret);
439 _exit(EXIT_SUCCESS);
440 }
441
442 assert_se(wait_for_terminate(pid, &si) >= 0);
443 assert_se(si.si_code == CLD_EXITED);
444 assert_se(si.si_status == EXIT_SUCCESS);
445}
446
447static void test_rename_process_multi(void) {
448 pid_t pid;
2897b343 449
f5e65279
MB
450 pid = fork();
451 assert_se(pid >= 0);
452
453 if (pid > 0) {
454 siginfo_t si;
455
456 assert_se(wait_for_terminate(pid, &si) >= 0);
457 assert_se(si.si_code == CLD_EXITED);
458 assert_se(si.si_status == EXIT_SUCCESS);
459
460 return;
461 }
462
463 /* child */
464 test_rename_process_now("one", 1);
465 test_rename_process_now("more", 0); /* longer than "one", hence truncated */
52ad194e 466 (void) setresuid(99, 99, 99); /* change uid when running privileged */
f5e65279
MB
467 test_rename_process_now("time!", 0);
468 test_rename_process_now("0", 1); /* shorter than "one", should fit */
469 test_rename_process_one("", -EINVAL);
470 test_rename_process_one(NULL, -EINVAL);
2897b343
MP
471 _exit(EXIT_SUCCESS);
472}
473
474static void test_rename_process(void) {
475 test_rename_process_one(NULL, -EINVAL);
476 test_rename_process_one("", -EINVAL);
477 test_rename_process_one("foo", 1); /* should always fit */
478 test_rename_process_one("this is a really really long process name, followed by some more words", 0); /* unlikely to fit */
479 test_rename_process_one("1234567", 1); /* should always fit */
f5e65279
MB
480 test_rename_process_multi(); /* multiple invocations and dropped privileges */
481}
482
483static void test_getpid_cached(void) {
484 siginfo_t si;
485 pid_t a, b, c, d, e, f, child;
486
487 a = raw_getpid();
488 b = getpid_cached();
489 c = getpid();
490
491 assert_se(a == b && a == c);
492
493 child = fork();
494 assert_se(child >= 0);
495
496 if (child == 0) {
497 /* In child */
498 a = raw_getpid();
499 b = getpid_cached();
500 c = getpid();
501
502 assert_se(a == b && a == c);
1d42b86d 503 _exit(EXIT_SUCCESS);
f5e65279
MB
504 }
505
506 d = raw_getpid();
507 e = getpid_cached();
508 f = getpid();
509
510 assert_se(a == d && a == e && a == f);
511
512 assert_se(wait_for_terminate(child, &si) >= 0);
513 assert_se(si.si_status == 0);
514 assert_se(si.si_code == CLD_EXITED);
515}
516
517#define MEASURE_ITERATIONS (10000000LLU)
518
519static void test_getpid_measure(void) {
520 unsigned long long i;
521 usec_t t, q;
522
523 t = now(CLOCK_MONOTONIC);
524 for (i = 0; i < MEASURE_ITERATIONS; i++)
525 (void) getpid();
526 q = now(CLOCK_MONOTONIC) - t;
527
528 log_info(" glibc getpid(): %llu/s\n", (unsigned long long) (MEASURE_ITERATIONS*USEC_PER_SEC/q));
529
530 t = now(CLOCK_MONOTONIC);
531 for (i = 0; i < MEASURE_ITERATIONS; i++)
532 (void) getpid_cached();
533 q = now(CLOCK_MONOTONIC) - t;
534
535 log_info("getpid_cached(): %llu/s\n", (unsigned long long) (MEASURE_ITERATIONS*USEC_PER_SEC/q));
2897b343
MP
536}
537
1d42b86d
MB
538static void test_safe_fork(void) {
539 siginfo_t status;
540 pid_t pid;
541 int r;
542
543 BLOCK_SIGNALS(SIGCHLD);
544
545 r = safe_fork("(test-child)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_NULL_STDIO|FORK_REOPEN_LOG, &pid);
546 assert_se(r >= 0);
547
548 if (r == 0) {
549 /* child */
550 usleep(100 * USEC_PER_MSEC);
551
552 _exit(88);
553 }
554
555 assert_se(wait_for_terminate(pid, &status) >= 0);
556 assert_se(status.si_code == CLD_EXITED);
557 assert_se(status.si_status == 88);
558}
559
560static void test_pid_to_ptr(void) {
561
562 assert_se(PTR_TO_PID(NULL) == 0);
563 assert_se(PID_TO_PTR(0) == NULL);
564
565 assert_se(PTR_TO_PID(PID_TO_PTR(1)) == 1);
566 assert_se(PTR_TO_PID(PID_TO_PTR(2)) == 2);
567 assert_se(PTR_TO_PID(PID_TO_PTR(-1)) == -1);
568 assert_se(PTR_TO_PID(PID_TO_PTR(-2)) == -2);
569
570 assert_se(PTR_TO_PID(PID_TO_PTR(INT16_MAX)) == INT16_MAX);
571 assert_se(PTR_TO_PID(PID_TO_PTR(INT16_MIN)) == INT16_MIN);
572
573#if SIZEOF_PID_T >= 4
574 assert_se(PTR_TO_PID(PID_TO_PTR(INT32_MAX)) == INT32_MAX);
575 assert_se(PTR_TO_PID(PID_TO_PTR(INT32_MIN)) == INT32_MIN);
576#endif
577}
578
b012e921
MB
579static void test_ioprio_class_from_to_string_one(const char *val, int expected) {
580 assert_se(ioprio_class_from_string(val) == expected);
581 if (expected >= 0) {
582 _cleanup_free_ char *s = NULL;
583 unsigned ret;
2897b343 584
b012e921
MB
585 assert_se(ioprio_class_to_string_alloc(expected, &s) == 0);
586 /* We sometimes get a class number and sometimes a number back */
587 assert_se(streq(s, val) ||
588 safe_atou(val, &ret) == 0);
589 }
590}
591
592static void test_ioprio_class_from_to_string(void) {
593 test_ioprio_class_from_to_string_one("none", IOPRIO_CLASS_NONE);
594 test_ioprio_class_from_to_string_one("realtime", IOPRIO_CLASS_RT);
595 test_ioprio_class_from_to_string_one("best-effort", IOPRIO_CLASS_BE);
596 test_ioprio_class_from_to_string_one("idle", IOPRIO_CLASS_IDLE);
597 test_ioprio_class_from_to_string_one("0", 0);
598 test_ioprio_class_from_to_string_one("1", 1);
599 test_ioprio_class_from_to_string_one("7", 7);
600 test_ioprio_class_from_to_string_one("8", 8);
601 test_ioprio_class_from_to_string_one("9", -1);
602 test_ioprio_class_from_to_string_one("-1", -1);
603}
604
605int main(int argc, char *argv[]) {
6e866b33 606 test_setup_logging(LOG_DEBUG);
e3bff60a 607
2897b343
MP
608 saved_argc = argc;
609 saved_argv = argv;
610
5a920b42
MP
611 if (argc > 1) {
612 pid_t pid = 0;
613
614 (void) parse_pid(argv[1], &pid);
615 test_get_process_comm(pid);
616 } else {
8a584da2 617 TEST_REQ_RUNNING_SYSTEMD(test_get_process_comm(1));
5a920b42
MP
618 test_get_process_comm(getpid());
619 }
620
b012e921 621 test_get_process_comm_escape();
e3bff60a
MP
622 test_pid_is_unwaited();
623 test_pid_is_alive();
aa27b158 624 test_personality();
5a920b42 625 test_get_process_cmdline_harder();
2897b343 626 test_rename_process();
f5e65279
MB
627 test_getpid_cached();
628 test_getpid_measure();
1d42b86d
MB
629 test_safe_fork();
630 test_pid_to_ptr();
b012e921 631 test_ioprio_class_from_to_string();
e3bff60a
MP
632
633 return 0;
634}