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