]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-process-util.c
process-util: add helper get_process_threads()
[thirdparty/systemd.git] / src / test / test-process-util.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
0b452006 2
f5947a5e 3#include <fcntl.h>
bb2d1d8e 4#include <linux/oom.h>
6aa90884 5#include <pthread.h>
69281c49 6#include <sys/mount.h>
26fbedd7 7#include <sys/personality.h>
69281c49 8#include <sys/prctl.h>
0b452006 9#include <sys/stat.h>
07630cea 10#include <sys/types.h>
0b452006
RC
11#include <sys/wait.h>
12#include <unistd.h>
349cc4a5 13#if HAVE_VALGRIND_VALGRIND_H
b3d69149
EV
14#include <valgrind/valgrind.h>
15#endif
0b452006 16
b5efdb8a 17#include "alloc-util.h"
26fbedd7 18#include "architecture.h"
510c7a95 19#include "dirent-util.h"
0c4d1e6d
LP
20#include "errno-list.h"
21#include "errno-util.h"
69281c49 22#include "fd-util.h"
032b3afb 23#include "ioprio-util.h"
0b452006 24#include "log.h"
0b452006 25#include "macro.h"
f5947a5e
YW
26#include "missing_sched.h"
27#include "missing_syscall.h"
18dade5a 28#include "parse-util.h"
07630cea 29#include "process-util.h"
0c4d1e6d 30#include "procfs-util.h"
75997c3f 31#include "rlimit-util.h"
4c253ed1 32#include "signal-util.h"
9a140c35 33#include "stdio-util.h"
07630cea 34#include "string-util.h"
288a74cc 35#include "terminal-util.h"
6d7c4033 36#include "tests.h"
75997c3f 37#include "user-util.h"
07630cea 38#include "virt.h"
0b452006 39
c462e63e 40static void test_get_process_comm_one(pid_t pid) {
0b452006 41 struct stat st;
ba19c6e1 42 _cleanup_free_ char *a = NULL, *c = NULL, *d = NULL, *f = NULL, *i = NULL;
0b452006 43 _cleanup_free_ char *env = NULL;
fbd0b64f 44 char path[STRLEN("/proc//comm") + DECIMAL_STR_MAX(pid_t)];
0b452006
RC
45 pid_t e;
46 uid_t u;
47 gid_t g;
48 dev_t h;
49 int r;
0b452006 50
daceaabe
ZJS
51 log_info("/* %s */", __func__);
52
9a140c35
ZJS
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);
cfeaa44a 58 } else
9a140c35 59 log_warning("%s not exist.", path);
0b452006 60
09c1dcee 61 assert_se(get_process_cmdline(pid, 0, PROCESS_CMDLINE_COMM_FALLBACK, &c) >= 0);
9a140c35 62 log_info("PID"PID_FMT" cmdline: '%s'", pid, c);
0b452006 63
09c1dcee 64 assert_se(get_process_cmdline(pid, 8, 0, &d) >= 0);
69281c49
LP
65 log_info("PID"PID_FMT" cmdline truncated to 8: '%s'", pid, d);
66
67 free(d);
09c1dcee 68 assert_se(get_process_cmdline(pid, 1, 0, &d) >= 0);
69281c49 69 log_info("PID"PID_FMT" cmdline truncated to 1: '%s'", pid, d);
0b452006 70
0c4d1e6d
LP
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 }
0b452006 77
9a140c35 78 assert_se(is_kernel_thread(pid) == 0 || pid != 1);
0b452006 79
9a140c35 80 r = get_process_exe(pid, &f);
0b452006 81 assert_se(r >= 0 || r == -EACCES);
9a140c35 82 log_info("PID"PID_FMT" exe: '%s'", pid, strna(f));
0b452006 83
9a140c35
ZJS
84 assert_se(get_process_uid(pid, &u) == 0);
85 log_info("PID"PID_FMT" UID: "UID_FMT, pid, u);
0b452006 86
9a140c35
ZJS
87 assert_se(get_process_gid(pid, &g) == 0);
88 log_info("PID"PID_FMT" GID: "GID_FMT, pid, g);
0b452006 89
9a140c35 90 r = get_process_environ(pid, &env);
0b452006 91 assert_se(r >= 0 || r == -EACCES);
9a140c35 92 log_info("PID"PID_FMT" strlen(environ): %zi", pid, env ? (ssize_t)strlen(env) : (ssize_t)-errno);
0b452006 93
75f86906 94 if (!detect_container())
9a140c35 95 assert_se(get_ctty_devnr(pid, &h) == -ENXIO || pid != 1);
0b452006 96
6fb05b07 97 (void) getenv_for_pid(pid, "PATH", &i);
9a140c35 98 log_info("PID"PID_FMT" $PATH: '%s'", pid, strna(i));
0b452006
RC
99}
100
c462e63e
JJ
101TEST(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
510c7a95
ZJS
113static 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
c462e63e 136TEST(get_process_cmdline) {
510c7a95 137 _cleanup_closedir_ DIR *d = NULL;
510c7a95 138
510c7a95
ZJS
139 assert_se(d = opendir("/proc"));
140
141 FOREACH_DIRENT(de, d, return) {
142 pid_t pid;
143
510c7a95
ZJS
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
ce268825
LP
154static void test_get_process_comm_escape_one(const char *input, const char *output) {
155 _cleanup_free_ char *n = NULL;
156
daceaabe 157 log_debug("input: <%s> — output: <%s>", input, output);
ce268825
LP
158
159 assert_se(prctl(PR_SET_NAME, input) >= 0);
160 assert_se(get_process_comm(0, &n) >= 0);
161
daceaabe 162 log_debug("got: <%s>", n);
ce268825
LP
163
164 assert_se(streq_ptr(n, output));
165}
166
c462e63e 167TEST(get_process_comm_escape) {
ce268825
LP
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");
0e85cbcf
ZJS
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");
ce268825
LP
182
183 assert_se(prctl(PR_SET_NAME, saved) >= 0);
184}
185
c462e63e 186TEST(pid_is_unwaited) {
0b452006
RC
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 }
df0ff127 199 assert_se(pid_is_unwaited(getpid_cached()));
0b452006
RC
200 assert_se(!pid_is_unwaited(-1));
201}
202
c462e63e 203TEST(pid_is_alive) {
0b452006
RC
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 }
df0ff127 216 assert_se(pid_is_alive(getpid_cached()));
0b452006
RC
217 assert_se(!pid_is_alive(-1));
218}
219
c462e63e 220TEST(personality) {
26fbedd7
LP
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
c462e63e 242TEST(get_process_cmdline_harder) {
69281c49 243 char path[] = "/tmp/test-cmdlineXXXXXX";
254d1313 244 _cleanup_close_ int fd = -EBADF;
69281c49
LP
245 _cleanup_free_ char *line = NULL;
246 pid_t pid;
247
92f6a1b6
YW
248 if (geteuid() != 0) {
249 log_info("Skipping %s: not root", __func__);
69281c49 250 return;
92f6a1b6 251 }
69281c49 252
5f00dc4d
LP
253 if (!have_namespaces()) {
254 log_notice("Testing without namespaces, skipping %s", __func__);
255 return;
256 }
257
349cc4a5 258#if HAVE_VALGRIND_VALGRIND_H
b3d69149
EV
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 */
92f6a1b6
YW
262 if (RUNNING_ON_VALGRIND) {
263 log_info("Skipping %s: running on valgrind", __func__);
b3d69149 264 return;
92f6a1b6 265 }
b3d69149
EV
266#endif
267
69281c49
LP
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
0ffa4c7c
EV
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 }
c58fd466 288
69281c49
LP
289 fd = mkostemp(path, O_CLOEXEC);
290 assert_se(fd >= 0);
347ebd02 291
0ffa4c7c
EV
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. */
347ebd02
ZJS
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");
0ffa4c7c 297 assert_se(IN_SET(errno, EPERM, EACCES));
347ebd02
ZJS
298 return;
299 }
300
7b7a060e
AZ
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
69281c49
LP
306 assert_se(unlink(path) >= 0);
307
308 assert_se(prctl(PR_SET_NAME, "testa") >= 0);
309
679b0b0a 310 assert_se(get_process_cmdline(0, SIZE_MAX, 0, &line) == -ENOENT);
69281c49 311
679b0b0a 312 assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
daceaabe 313 log_debug("'%s'", line);
69281c49
LP
314 assert_se(streq(line, "[testa]"));
315 line = mfree(line);
316
61977664 317 assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK | PROCESS_CMDLINE_QUOTE, &line) >= 0);
daceaabe 318 log_debug("'%s'", line);
61977664
ZJS
319 assert_se(streq(line, "\"[testa]\"")); /* quoting is enabled here */
320 line = mfree(line);
321
679b0b0a 322 assert_se(get_process_cmdline(0, 0, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
daceaabe 323 log_debug("'%s'", line);
69281c49
LP
324 assert_se(streq(line, ""));
325 line = mfree(line);
326
679b0b0a 327 assert_se(get_process_cmdline(0, 1, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
bc28751e
ZJS
328 assert_se(streq(line, "…"));
329 line = mfree(line);
330
679b0b0a 331 assert_se(get_process_cmdline(0, 2, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
bc28751e 332 assert_se(streq(line, "[…"));
69281c49
LP
333 line = mfree(line);
334
679b0b0a 335 assert_se(get_process_cmdline(0, 3, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
bc28751e 336 assert_se(streq(line, "[t…"));
69281c49
LP
337 line = mfree(line);
338
679b0b0a 339 assert_se(get_process_cmdline(0, 4, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
bc28751e 340 assert_se(streq(line, "[te…"));
69281c49
LP
341 line = mfree(line);
342
679b0b0a 343 assert_se(get_process_cmdline(0, 5, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
bc28751e 344 assert_se(streq(line, "[tes…"));
69281c49
LP
345 line = mfree(line);
346
679b0b0a 347 assert_se(get_process_cmdline(0, 6, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
bc28751e 348 assert_se(streq(line, "[test…"));
69281c49
LP
349 line = mfree(line);
350
679b0b0a 351 assert_se(get_process_cmdline(0, 7, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
69281c49
LP
352 assert_se(streq(line, "[testa]"));
353 line = mfree(line);
354
679b0b0a 355 assert_se(get_process_cmdline(0, 8, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
69281c49
LP
356 assert_se(streq(line, "[testa]"));
357 line = mfree(line);
358
61977664
ZJS
359 /* Test with multiple arguments that don't require quoting */
360
bc28751e 361 assert_se(write(fd, "foo\0bar", 8) == 8);
69281c49 362
679b0b0a 363 assert_se(get_process_cmdline(0, SIZE_MAX, 0, &line) >= 0);
daceaabe 364 log_debug("'%s'", line);
69281c49
LP
365 assert_se(streq(line, "foo bar"));
366 line = mfree(line);
367
679b0b0a 368 assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
69281c49
LP
369 assert_se(streq(line, "foo bar"));
370 line = mfree(line);
371
372 assert_se(write(fd, "quux", 4) == 4);
679b0b0a 373 assert_se(get_process_cmdline(0, SIZE_MAX, 0, &line) >= 0);
daceaabe 374 log_debug("'%s'", line);
69281c49
LP
375 assert_se(streq(line, "foo bar quux"));
376 line = mfree(line);
377
679b0b0a 378 assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
daceaabe 379 log_debug("'%s'", line);
69281c49
LP
380 assert_se(streq(line, "foo bar quux"));
381 line = mfree(line);
382
679b0b0a 383 assert_se(get_process_cmdline(0, 1, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
daceaabe 384 log_debug("'%s'", line);
bc28751e 385 assert_se(streq(line, "…"));
69281c49
LP
386 line = mfree(line);
387
679b0b0a 388 assert_se(get_process_cmdline(0, 2, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
daceaabe 389 log_debug("'%s'", line);
bc28751e 390 assert_se(streq(line, "f…"));
69281c49
LP
391 line = mfree(line);
392
679b0b0a 393 assert_se(get_process_cmdline(0, 3, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
daceaabe 394 log_debug("'%s'", line);
bc28751e 395 assert_se(streq(line, "fo…"));
69281c49
LP
396 line = mfree(line);
397
679b0b0a 398 assert_se(get_process_cmdline(0, 4, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
daceaabe 399 log_debug("'%s'", line);
bc28751e 400 assert_se(streq(line, "foo…"));
69281c49
LP
401 line = mfree(line);
402
679b0b0a 403 assert_se(get_process_cmdline(0, 5, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
daceaabe 404 log_debug("'%s'", line);
bc28751e 405 assert_se(streq(line, "foo …"));
69281c49
LP
406 line = mfree(line);
407
679b0b0a 408 assert_se(get_process_cmdline(0, 6, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
daceaabe 409 log_debug("'%s'", line);
bc28751e 410 assert_se(streq(line, "foo b…"));
69281c49
LP
411 line = mfree(line);
412
679b0b0a 413 assert_se(get_process_cmdline(0, 7, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
daceaabe 414 log_debug("'%s'", line);
bc28751e 415 assert_se(streq(line, "foo ba…"));
69281c49
LP
416 line = mfree(line);
417
679b0b0a 418 assert_se(get_process_cmdline(0, 8, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
daceaabe 419 log_debug("'%s'", line);
bc28751e 420 assert_se(streq(line, "foo bar…"));
69281c49
LP
421 line = mfree(line);
422
679b0b0a 423 assert_se(get_process_cmdline(0, 9, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
daceaabe 424 log_debug("'%s'", line);
bc28751e 425 assert_se(streq(line, "foo bar …"));
69281c49
LP
426 line = mfree(line);
427
679b0b0a 428 assert_se(get_process_cmdline(0, 10, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
daceaabe 429 log_debug("'%s'", line);
bc28751e 430 assert_se(streq(line, "foo bar q…"));
69281c49
LP
431 line = mfree(line);
432
679b0b0a 433 assert_se(get_process_cmdline(0, 11, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
daceaabe 434 log_debug("'%s'", line);
bc28751e 435 assert_se(streq(line, "foo bar qu…"));
69281c49
LP
436 line = mfree(line);
437
679b0b0a 438 assert_se(get_process_cmdline(0, 12, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
daceaabe 439 log_debug("'%s'", line);
bc28751e 440 assert_se(streq(line, "foo bar quux"));
69281c49
LP
441 line = mfree(line);
442
679b0b0a 443 assert_se(get_process_cmdline(0, 13, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
daceaabe 444 log_debug("'%s'", line);
69281c49
LP
445 assert_se(streq(line, "foo bar quux"));
446 line = mfree(line);
447
679b0b0a 448 assert_se(get_process_cmdline(0, 14, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
daceaabe 449 log_debug("'%s'", line);
69281c49
LP
450 assert_se(streq(line, "foo bar quux"));
451 line = mfree(line);
452
679b0b0a 453 assert_se(get_process_cmdline(0, 1000, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
daceaabe 454 log_debug("'%s'", line);
69281c49
LP
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
679b0b0a 461 assert_se(get_process_cmdline(0, SIZE_MAX, 0, &line) == -ENOENT);
69281c49 462
679b0b0a 463 assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
daceaabe 464 log_debug("'%s'", line);
69281c49
LP
465 assert_se(streq(line, "[aaaa bbbb cccc]"));
466 line = mfree(line);
467
679b0b0a 468 assert_se(get_process_cmdline(0, 10, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
daceaabe 469 log_debug("'%s'", line);
bc28751e 470 assert_se(streq(line, "[aaaa bbb…"));
69281c49
LP
471 line = mfree(line);
472
679b0b0a 473 assert_se(get_process_cmdline(0, 11, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
daceaabe 474 log_debug("'%s'", line);
bc28751e 475 assert_se(streq(line, "[aaaa bbbb…"));
69281c49
LP
476 line = mfree(line);
477
679b0b0a 478 assert_se(get_process_cmdline(0, 12, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
daceaabe 479 log_debug("'%s'", line);
bc28751e 480 assert_se(streq(line, "[aaaa bbbb …"));
69281c49
LP
481 line = mfree(line);
482
61977664
ZJS
483 /* Test with multiple arguments that do require quoting */
484
485#define CMDLINE1 "foo\0'bar'\0\"bar$\"\0x y z\0!``\0"
aa9de5b1
YW
486#define EXPECT1 "foo \"'bar'\" \"\\\"bar\\$\\\"\" \"x y z\" \"!\\`\\`\""
487#define EXPECT1p "foo $'\\'bar\\'' $'\"bar$\"' $'x y z' $'!``'"
61977664
ZJS
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);
daceaabe
ZJS
493 log_debug("got: ==%s==", line);
494 log_debug("exp: ==%s==", EXPECT1);
61977664
ZJS
495 assert_se(streq(line, EXPECT1));
496 line = mfree(line);
497
99009ed0
ZJS
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
61977664 504#define CMDLINE2 "foo\0\1\2\3\0\0"
aa9de5b1
YW
505#define EXPECT2 "foo \"\\001\\002\\003\""
506#define EXPECT2p "foo $'\\001\\002\\003'"
61977664
ZJS
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);
daceaabe
ZJS
512 log_debug("got: ==%s==", line);
513 log_debug("exp: ==%s==", EXPECT2);
61977664
ZJS
514 assert_se(streq(line, EXPECT2));
515 line = mfree(line);
516
99009ed0
ZJS
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
69281c49 523 safe_close(fd);
a45d7127 524 _exit(EXIT_SUCCESS);
69281c49
LP
525}
526
c462e63e 527TEST(getpid_cached) {
5c30a6d2
LP
528 siginfo_t si;
529 pid_t a, b, c, d, e, f, child;
530
531 a = raw_getpid();
532 b = getpid_cached();
533 c = getpid();
534
535 assert_se(a == b && a == c);
536
537 child = fork();
538 assert_se(child >= 0);
539
540 if (child == 0) {
541 /* In child */
542 a = raw_getpid();
543 b = getpid_cached();
544 c = getpid();
545
546 assert_se(a == b && a == c);
a45d7127 547 _exit(EXIT_SUCCESS);
5c30a6d2
LP
548 }
549
550 d = raw_getpid();
551 e = getpid_cached();
552 f = getpid();
553
554 assert_se(a == d && a == e && a == f);
555
556 assert_se(wait_for_terminate(child, &si) >= 0);
557 assert_se(si.si_status == 0);
558 assert_se(si.si_code == CLD_EXITED);
559}
560
c462e63e 561TEST(getpid_measure) {
5c30a6d2
LP
562 usec_t t, q;
563
07468a16
ZJS
564 unsigned long long iterations = slow_tests_enabled() ? 1000000 : 1000;
565
566 log_info("/* %s (%llu iterations) */", __func__, iterations);
daceaabe 567
5c30a6d2 568 t = now(CLOCK_MONOTONIC);
07468a16 569 for (unsigned long long i = 0; i < iterations; i++)
5c30a6d2
LP
570 (void) getpid();
571 q = now(CLOCK_MONOTONIC) - t;
572
07468a16
ZJS
573 log_info(" glibc getpid(): %lf µs each\n", (double) q / iterations);
574
575 iterations *= 50; /* _cached() is about 50 times faster, so we need more iterations */
5c30a6d2
LP
576
577 t = now(CLOCK_MONOTONIC);
07468a16 578 for (unsigned long long i = 0; i < iterations; i++)
5c30a6d2
LP
579 (void) getpid_cached();
580 q = now(CLOCK_MONOTONIC) - t;
581
07468a16 582 log_info("getpid_cached(): %lf µs each\n", (double) q / iterations);
5c30a6d2
LP
583}
584
c462e63e 585TEST(safe_fork) {
4c253ed1
LP
586 siginfo_t status;
587 pid_t pid;
588 int r;
589
590 BLOCK_SIGNALS(SIGCHLD);
591
592 r = safe_fork("(test-child)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_NULL_STDIO|FORK_REOPEN_LOG, &pid);
593 assert_se(r >= 0);
594
595 if (r == 0) {
596 /* child */
597 usleep(100 * USEC_PER_MSEC);
598
599 _exit(88);
600 }
601
602 assert_se(wait_for_terminate(pid, &status) >= 0);
603 assert_se(status.si_code == CLD_EXITED);
604 assert_se(status.si_status == 88);
605}
606
c462e63e 607TEST(pid_to_ptr) {
eb5eb254
LP
608 assert_se(PTR_TO_PID(NULL) == 0);
609 assert_se(PID_TO_PTR(0) == NULL);
610
611 assert_se(PTR_TO_PID(PID_TO_PTR(1)) == 1);
612 assert_se(PTR_TO_PID(PID_TO_PTR(2)) == 2);
613 assert_se(PTR_TO_PID(PID_TO_PTR(-1)) == -1);
614 assert_se(PTR_TO_PID(PID_TO_PTR(-2)) == -2);
615
616 assert_se(PTR_TO_PID(PID_TO_PTR(INT16_MAX)) == INT16_MAX);
617 assert_se(PTR_TO_PID(PID_TO_PTR(INT16_MIN)) == INT16_MIN);
618
eb5eb254
LP
619 assert_se(PTR_TO_PID(PID_TO_PTR(INT32_MAX)) == INT32_MAX);
620 assert_se(PTR_TO_PID(PID_TO_PTR(INT32_MIN)) == INT32_MIN);
eb5eb254
LP
621}
622
543497fa 623static void test_ioprio_class_from_to_string_one(const char *val, int expected, int normalized) {
10062bbc
ZJS
624 assert_se(ioprio_class_from_string(val) == expected);
625 if (expected >= 0) {
626 _cleanup_free_ char *s = NULL;
627 unsigned ret;
543497fa 628 int combined;
10062bbc
ZJS
629
630 assert_se(ioprio_class_to_string_alloc(expected, &s) == 0);
543497fa 631 /* We sometimes get a class number and sometimes a name back */
10062bbc
ZJS
632 assert_se(streq(s, val) ||
633 safe_atou(val, &ret) == 0);
543497fa
LP
634
635 /* Make sure normalization works, i.e. NONE → BE gets normalized */
636 combined = ioprio_normalize(ioprio_prio_value(expected, 0));
637 assert_se(ioprio_prio_class(combined) == normalized);
638 assert_se(expected != IOPRIO_CLASS_NONE || ioprio_prio_data(combined) == 4);
10062bbc
ZJS
639 }
640}
9bfaffd5 641
c462e63e 642TEST(ioprio_class_from_to_string) {
543497fa
LP
643 test_ioprio_class_from_to_string_one("none", IOPRIO_CLASS_NONE, IOPRIO_CLASS_BE);
644 test_ioprio_class_from_to_string_one("realtime", IOPRIO_CLASS_RT, IOPRIO_CLASS_RT);
645 test_ioprio_class_from_to_string_one("best-effort", IOPRIO_CLASS_BE, IOPRIO_CLASS_BE);
646 test_ioprio_class_from_to_string_one("idle", IOPRIO_CLASS_IDLE, IOPRIO_CLASS_IDLE);
647 test_ioprio_class_from_to_string_one("0", IOPRIO_CLASS_NONE, IOPRIO_CLASS_BE);
648 test_ioprio_class_from_to_string_one("1", 1, 1);
649 test_ioprio_class_from_to_string_one("7", 7, 7);
650 test_ioprio_class_from_to_string_one("8", 8, 8);
651 test_ioprio_class_from_to_string_one("9", -EINVAL, -EINVAL);
652 test_ioprio_class_from_to_string_one("-1", -EINVAL, -EINVAL);
10062bbc
ZJS
653}
654
c462e63e 655TEST(setpriority_closest) {
75997c3f
LP
656 int r;
657
daceaabe
ZJS
658 r = safe_fork("(test-setprio)",
659 FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_WAIT|FORK_LOG, NULL);
75997c3f
LP
660 assert_se(r >= 0);
661
662 if (r == 0) {
663 bool full_test;
664 int p, q;
665 /* child */
666
667 /* rlimit of 30 equals nice level of -10 */
668 if (setrlimit(RLIMIT_NICE, &RLIMIT_MAKE_CONST(30)) < 0) {
6a6078a5 669 /* If this fails we are probably unprivileged or in a userns of some kind, let's skip
75997c3f
LP
670 * the full test */
671 assert_se(ERRNO_IS_PRIVILEGE(errno));
672 full_test = false;
673 } else {
674 assert_se(setresgid(GID_NOBODY, GID_NOBODY, GID_NOBODY) >= 0);
675 assert_se(setresuid(UID_NOBODY, UID_NOBODY, UID_NOBODY) >= 0);
676 full_test = true;
677 }
678
679 errno = 0;
680 p = getpriority(PRIO_PROCESS, 0);
681 assert_se(errno == 0);
682
683 /* It should always be possible to set our nice level to the current one */
684 assert_se(setpriority_closest(p) > 0);
685
686 errno = 0;
687 q = getpriority(PRIO_PROCESS, 0);
688 assert_se(errno == 0 && p == q);
689
cd990847 690 /* It should also be possible to set the nice level to one higher */
75997c3f
LP
691 if (p < PRIO_MAX-1) {
692 assert_se(setpriority_closest(++p) > 0);
693
694 errno = 0;
695 q = getpriority(PRIO_PROCESS, 0);
696 assert_se(errno == 0 && p == q);
697 }
698
cd990847 699 /* It should also be possible to set the nice level to two higher */
75997c3f
LP
700 if (p < PRIO_MAX-1) {
701 assert_se(setpriority_closest(++p) > 0);
702
703 errno = 0;
704 q = getpriority(PRIO_PROCESS, 0);
705 assert_se(errno == 0 && p == q);
706 }
707
708 if (full_test) {
709 /* These two should work, given the RLIMIT_NICE we set above */
710 assert_se(setpriority_closest(-10) > 0);
711 errno = 0;
712 q = getpriority(PRIO_PROCESS, 0);
713 assert_se(errno == 0 && q == -10);
714
715 assert_se(setpriority_closest(-9) > 0);
716 errno = 0;
717 q = getpriority(PRIO_PROCESS, 0);
718 assert_se(errno == 0 && q == -9);
719
720 /* This should succeed but should be clamped to the limit */
721 assert_se(setpriority_closest(-11) == 0);
722 errno = 0;
723 q = getpriority(PRIO_PROCESS, 0);
724 assert_se(errno == 0 && q == -10);
725
726 assert_se(setpriority_closest(-8) > 0);
727 errno = 0;
728 q = getpriority(PRIO_PROCESS, 0);
729 assert_se(errno == 0 && q == -8);
730
731 /* This should succeed but should be clamped to the limit */
732 assert_se(setpriority_closest(-12) == 0);
733 errno = 0;
734 q = getpriority(PRIO_PROCESS, 0);
735 assert_se(errno == 0 && q == -10);
736 }
737
738 _exit(EXIT_SUCCESS);
739 }
740}
741
c462e63e 742TEST(get_process_ppid) {
0c4d1e6d
LP
743 uint64_t limit;
744 int r;
745
0c4d1e6d
LP
746 assert_se(get_process_ppid(1, NULL) == -EADDRNOTAVAIL);
747
748 /* the process with the PID above the global limit definitely doesn't exist. Verify that */
c3dead53
ZJS
749 assert_se(procfs_get_pid_max(&limit) >= 0);
750 log_debug("kernel.pid_max = %"PRIu64, limit);
751
752 if (limit < INT_MAX) {
753 r = get_process_ppid(limit + 1, NULL);
754 log_debug_errno(r, "get_process_limit(%"PRIu64") → %d/%m", limit + 1, r);
755 assert(r == -ESRCH);
756 }
0c4d1e6d
LP
757
758 for (pid_t pid = 0;;) {
759 _cleanup_free_ char *c1 = NULL, *c2 = NULL;
760 pid_t ppid;
761
762 r = get_process_ppid(pid, &ppid);
763 if (r == -EADDRNOTAVAIL) {
764 log_info("No further parent PID");
765 break;
766 }
767
768 assert_se(r >= 0);
769
770 assert_se(get_process_cmdline(pid, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &c1) >= 0);
771 assert_se(get_process_cmdline(ppid, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &c2) >= 0);
772
773 log_info("Parent of " PID_FMT " (%s) is " PID_FMT " (%s).", pid, c1, ppid, c2);
774
775 pid = ppid;
776 }
777}
778
c462e63e 779TEST(set_oom_score_adjust) {
bb2d1d8e
LP
780 int a, b, r;
781
782 assert_se(get_oom_score_adjust(&a) >= 0);
783
784 r = set_oom_score_adjust(OOM_SCORE_ADJ_MIN);
785 assert_se(r >= 0 || ERRNO_IS_PRIVILEGE(r));
786
787 if (r >= 0) {
788 assert_se(get_oom_score_adjust(&b) >= 0);
789 assert_se(b == OOM_SCORE_ADJ_MIN);
790 }
791
792 assert_se(set_oom_score_adjust(a) >= 0);
793 assert_se(get_oom_score_adjust(&b) >= 0);
794 assert_se(b == a);
795}
796
6aa90884
LP
797static void* dummy_thread(void *p) {
798 int fd = PTR_TO_FD(p);
799 char x;
800
801 /* let main thread know we are ready */
802 assert_se(write(fd, &(const char) { 'x' }, 1) == 1);
803
804 /* wait for the main thread to tell us to shut down */
805 assert_se(read(fd, &x, 1) == 1);
806 return NULL;
807}
808
809TEST(get_process_threads) {
810 int r;
811
812 /* Run this test in a child, so that we can guarantee there's exactly one thread around in the child */
813 r = safe_fork("(nthreads)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_REOPEN_LOG|FORK_WAIT|FORK_LOG, NULL);
814 assert_se(r >= 0);
815
816 if (r == 0) {
817 _cleanup_close_pair_ int pfd[2] = PIPE_EBADF, ppfd[2] = PIPE_EBADF;
818 pthread_t t, tt;
819 char x;
820
821 assert_se(socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, pfd) >= 0);
822 assert_se(socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, ppfd) >= 0);
823
824 assert_se(get_process_threads(0) == 1);
825 assert_se(pthread_create(&t, NULL, &dummy_thread, FD_TO_PTR(pfd[0])) == 0);
826 assert_se(read(pfd[1], &x, 1) == 1);
827 assert_se(get_process_threads(0) == 2);
828 assert_se(pthread_create(&tt, NULL, &dummy_thread, FD_TO_PTR(ppfd[0])) == 0);
829 assert_se(read(ppfd[1], &x, 1) == 1);
830 assert_se(get_process_threads(0) == 3);
831
832 assert_se(write(pfd[1], &(const char) { 'x' }, 1) == 1);
833 assert_se(pthread_join(t, NULL) == 0);
834
835 /* the value reported via /proc/ is decreased asynchronously, and there appears to be no nice
836 * way to sync on it. Hence we do the weak >= 2 check, even though == 2 is what we'd actually
837 * like to check here */
838 assert_se(get_process_threads(0) >= 2);
839
840 assert_se(write(ppfd[1], &(const char) { 'x' }, 1) == 1);
841 assert_se(pthread_join(tt, NULL) == 0);
842
843 /* similar here */
844 assert_se(get_process_threads(0) >= 1);
845
846 _exit(EXIT_SUCCESS);
847 }
848}
849
99839c7e
LP
850static int intro(void) {
851 log_show_color(true);
852 return EXIT_SUCCESS;
853}
854
e85fdacc 855DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);