]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-process-util.c
build-sys: use #if Y instead of #ifdef Y everywhere
[thirdparty/systemd.git] / src / test / test-process-util.c
CommitLineData
0b452006
RC
1/***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5 Copyright 2013 Thomas H.P. Andersen
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19***/
20
69281c49
LP
21#include <sched.h>
22#include <sys/mount.h>
26fbedd7 23#include <sys/personality.h>
69281c49 24#include <sys/prctl.h>
0b452006 25#include <sys/stat.h>
07630cea 26#include <sys/types.h>
0b452006
RC
27#include <sys/wait.h>
28#include <unistd.h>
349cc4a5 29#if HAVE_VALGRIND_VALGRIND_H
b3d69149
EV
30#include <valgrind/valgrind.h>
31#endif
0b452006 32
b5efdb8a 33#include "alloc-util.h"
26fbedd7 34#include "architecture.h"
69281c49 35#include "fd-util.h"
0b452006 36#include "log.h"
0b452006 37#include "macro.h"
18dade5a 38#include "parse-util.h"
07630cea 39#include "process-util.h"
9a140c35 40#include "stdio-util.h"
07630cea 41#include "string-util.h"
288a74cc 42#include "terminal-util.h"
76c19e9f 43#include "test-helper.h"
07630cea
LP
44#include "util.h"
45#include "virt.h"
0b452006 46
9a140c35 47static void test_get_process_comm(pid_t pid) {
0b452006 48 struct stat st;
ba19c6e1 49 _cleanup_free_ char *a = NULL, *c = NULL, *d = NULL, *f = NULL, *i = NULL;
0b452006 50 _cleanup_free_ char *env = NULL;
9a140c35 51 char path[strlen("/proc//comm") + DECIMAL_STR_MAX(pid_t)];
0b452006
RC
52 pid_t e;
53 uid_t u;
54 gid_t g;
55 dev_t h;
56 int r;
0b452006 57
9a140c35
ZJS
58 xsprintf(path, "/proc/"PID_FMT"/comm", pid);
59
60 if (stat(path, &st) == 0) {
61 assert_se(get_process_comm(pid, &a) >= 0);
62 log_info("PID"PID_FMT" comm: '%s'", pid, a);
cfeaa44a 63 } else
9a140c35 64 log_warning("%s not exist.", path);
0b452006 65
9a140c35
ZJS
66 assert_se(get_process_cmdline(pid, 0, true, &c) >= 0);
67 log_info("PID"PID_FMT" cmdline: '%s'", pid, c);
0b452006 68
9a140c35 69 assert_se(get_process_cmdline(pid, 8, false, &d) >= 0);
69281c49
LP
70 log_info("PID"PID_FMT" cmdline truncated to 8: '%s'", pid, d);
71
72 free(d);
73 assert_se(get_process_cmdline(pid, 1, false, &d) >= 0);
74 log_info("PID"PID_FMT" cmdline truncated to 1: '%s'", pid, d);
0b452006 75
9a140c35
ZJS
76 assert_se(get_process_ppid(pid, &e) >= 0);
77 log_info("PID"PID_FMT" PPID: "PID_FMT, pid, e);
78 assert_se(pid == 1 ? e == 0 : e > 0);
0b452006 79
9a140c35 80 assert_se(is_kernel_thread(pid) == 0 || pid != 1);
0b452006 81
9a140c35 82 r = get_process_exe(pid, &f);
0b452006 83 assert_se(r >= 0 || r == -EACCES);
9a140c35 84 log_info("PID"PID_FMT" exe: '%s'", pid, strna(f));
0b452006 85
9a140c35
ZJS
86 assert_se(get_process_uid(pid, &u) == 0);
87 log_info("PID"PID_FMT" UID: "UID_FMT, pid, u);
88 assert_se(u == 0 || pid != 1);
0b452006 89
9a140c35
ZJS
90 assert_se(get_process_gid(pid, &g) == 0);
91 log_info("PID"PID_FMT" GID: "GID_FMT, pid, g);
92 assert_se(g == 0 || pid != 1);
0b452006 93
9a140c35 94 r = get_process_environ(pid, &env);
0b452006 95 assert_se(r >= 0 || r == -EACCES);
9a140c35 96 log_info("PID"PID_FMT" strlen(environ): %zi", pid, env ? (ssize_t)strlen(env) : (ssize_t)-errno);
0b452006 97
75f86906 98 if (!detect_container())
9a140c35 99 assert_se(get_ctty_devnr(pid, &h) == -ENXIO || pid != 1);
0b452006 100
9a140c35
ZJS
101 getenv_for_pid(pid, "PATH", &i);
102 log_info("PID"PID_FMT" $PATH: '%s'", pid, strna(i));
0b452006
RC
103}
104
105static void test_pid_is_unwaited(void) {
106 pid_t pid;
107
108 pid = fork();
109 assert_se(pid >= 0);
110 if (pid == 0) {
111 _exit(EXIT_SUCCESS);
112 } else {
113 int status;
114
115 waitpid(pid, &status, 0);
116 assert_se(!pid_is_unwaited(pid));
117 }
df0ff127 118 assert_se(pid_is_unwaited(getpid_cached()));
0b452006
RC
119 assert_se(!pid_is_unwaited(-1));
120}
121
122static void test_pid_is_alive(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_alive(pid));
134 }
df0ff127 135 assert_se(pid_is_alive(getpid_cached()));
0b452006
RC
136 assert_se(!pid_is_alive(-1));
137}
138
26fbedd7
LP
139static void test_personality(void) {
140
141 assert_se(personality_to_string(PER_LINUX));
142 assert_se(!personality_to_string(PERSONALITY_INVALID));
143
144 assert_se(streq(personality_to_string(PER_LINUX), architecture_to_string(native_architecture())));
145
146 assert_se(personality_from_string(personality_to_string(PER_LINUX)) == PER_LINUX);
147 assert_se(personality_from_string(architecture_to_string(native_architecture())) == PER_LINUX);
148
149#ifdef __x86_64__
150 assert_se(streq_ptr(personality_to_string(PER_LINUX), "x86-64"));
151 assert_se(streq_ptr(personality_to_string(PER_LINUX32), "x86"));
152
153 assert_se(personality_from_string("x86-64") == PER_LINUX);
154 assert_se(personality_from_string("x86") == PER_LINUX32);
155 assert_se(personality_from_string("ia64") == PERSONALITY_INVALID);
156 assert_se(personality_from_string(NULL) == PERSONALITY_INVALID);
157
158 assert_se(personality_from_string(personality_to_string(PER_LINUX32)) == PER_LINUX32);
159#endif
160}
161
69281c49
LP
162static void test_get_process_cmdline_harder(void) {
163 char path[] = "/tmp/test-cmdlineXXXXXX";
164 _cleanup_close_ int fd = -1;
165 _cleanup_free_ char *line = NULL;
166 pid_t pid;
167
168 if (geteuid() != 0)
169 return;
170
349cc4a5 171#if HAVE_VALGRIND_VALGRIND_H
b3d69149
EV
172 /* valgrind patches open(/proc//cmdline)
173 * so, test_get_process_cmdline_harder fails always
174 * See https://github.com/systemd/systemd/pull/3555#issuecomment-226564908 */
175 if (RUNNING_ON_VALGRIND)
176 return;
177#endif
178
69281c49
LP
179 pid = fork();
180 if (pid > 0) {
181 siginfo_t si;
182
183 (void) wait_for_terminate(pid, &si);
184
185 assert_se(si.si_code == CLD_EXITED);
186 assert_se(si.si_status == 0);
187
188 return;
189 }
190
191 assert_se(pid == 0);
192 assert_se(unshare(CLONE_NEWNS) >= 0);
193
194 fd = mkostemp(path, O_CLOEXEC);
195 assert_se(fd >= 0);
347ebd02
ZJS
196
197 if (mount(path, "/proc/self/cmdline", "bind", MS_BIND, NULL) < 0) {
198 /* This happens under selinux… Abort the test in this case. */
199 log_warning_errno(errno, "mount(..., \"/proc/self/cmdline\", \"bind\", ...) failed: %m");
200 assert(errno == EACCES);
201 return;
202 }
203
69281c49
LP
204 assert_se(unlink(path) >= 0);
205
206 assert_se(prctl(PR_SET_NAME, "testa") >= 0);
207
df0ff127 208 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) == -ENOENT);
69281c49 209
df0ff127 210 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0);
69281c49
LP
211 assert_se(streq(line, "[testa]"));
212 line = mfree(line);
213
df0ff127 214 assert_se(get_process_cmdline(getpid_cached(), 1, true, &line) >= 0);
69281c49
LP
215 assert_se(streq(line, ""));
216 line = mfree(line);
217
df0ff127 218 assert_se(get_process_cmdline(getpid_cached(), 2, true, &line) >= 0);
69281c49
LP
219 assert_se(streq(line, "["));
220 line = mfree(line);
221
df0ff127 222 assert_se(get_process_cmdline(getpid_cached(), 3, true, &line) >= 0);
69281c49
LP
223 assert_se(streq(line, "[."));
224 line = mfree(line);
225
df0ff127 226 assert_se(get_process_cmdline(getpid_cached(), 4, true, &line) >= 0);
69281c49
LP
227 assert_se(streq(line, "[.."));
228 line = mfree(line);
229
df0ff127 230 assert_se(get_process_cmdline(getpid_cached(), 5, true, &line) >= 0);
69281c49
LP
231 assert_se(streq(line, "[..."));
232 line = mfree(line);
233
df0ff127 234 assert_se(get_process_cmdline(getpid_cached(), 6, true, &line) >= 0);
69281c49
LP
235 assert_se(streq(line, "[...]"));
236 line = mfree(line);
237
df0ff127 238 assert_se(get_process_cmdline(getpid_cached(), 7, true, &line) >= 0);
69281c49
LP
239 assert_se(streq(line, "[t...]"));
240 line = mfree(line);
241
df0ff127 242 assert_se(get_process_cmdline(getpid_cached(), 8, true, &line) >= 0);
69281c49
LP
243 assert_se(streq(line, "[testa]"));
244 line = mfree(line);
245
246 assert_se(write(fd, "\0\0\0\0\0\0\0\0\0", 10) == 10);
247
df0ff127 248 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) == -ENOENT);
69281c49 249
df0ff127 250 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0);
69281c49
LP
251 assert_se(streq(line, "[testa]"));
252 line = mfree(line);
253
254 assert_se(write(fd, "foo\0bar\0\0\0\0\0", 10) == 10);
255
df0ff127 256 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) >= 0);
69281c49
LP
257 assert_se(streq(line, "foo bar"));
258 line = mfree(line);
259
df0ff127 260 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0);
69281c49
LP
261 assert_se(streq(line, "foo bar"));
262 line = mfree(line);
263
264 assert_se(write(fd, "quux", 4) == 4);
df0ff127 265 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) >= 0);
69281c49
LP
266 assert_se(streq(line, "foo bar quux"));
267 line = mfree(line);
268
df0ff127 269 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0);
69281c49
LP
270 assert_se(streq(line, "foo bar quux"));
271 line = mfree(line);
272
df0ff127 273 assert_se(get_process_cmdline(getpid_cached(), 1, true, &line) >= 0);
69281c49
LP
274 assert_se(streq(line, ""));
275 line = mfree(line);
276
df0ff127 277 assert_se(get_process_cmdline(getpid_cached(), 2, true, &line) >= 0);
69281c49
LP
278 assert_se(streq(line, "."));
279 line = mfree(line);
280
df0ff127 281 assert_se(get_process_cmdline(getpid_cached(), 3, true, &line) >= 0);
69281c49
LP
282 assert_se(streq(line, ".."));
283 line = mfree(line);
284
df0ff127 285 assert_se(get_process_cmdline(getpid_cached(), 4, true, &line) >= 0);
69281c49
LP
286 assert_se(streq(line, "..."));
287 line = mfree(line);
288
df0ff127 289 assert_se(get_process_cmdline(getpid_cached(), 5, true, &line) >= 0);
69281c49
LP
290 assert_se(streq(line, "f..."));
291 line = mfree(line);
292
df0ff127 293 assert_se(get_process_cmdline(getpid_cached(), 6, true, &line) >= 0);
69281c49
LP
294 assert_se(streq(line, "fo..."));
295 line = mfree(line);
296
df0ff127 297 assert_se(get_process_cmdline(getpid_cached(), 7, true, &line) >= 0);
69281c49
LP
298 assert_se(streq(line, "foo..."));
299 line = mfree(line);
300
df0ff127 301 assert_se(get_process_cmdline(getpid_cached(), 8, true, &line) >= 0);
69281c49
LP
302 assert_se(streq(line, "foo..."));
303 line = mfree(line);
304
df0ff127 305 assert_se(get_process_cmdline(getpid_cached(), 9, true, &line) >= 0);
69281c49
LP
306 assert_se(streq(line, "foo b..."));
307 line = mfree(line);
308
df0ff127 309 assert_se(get_process_cmdline(getpid_cached(), 10, true, &line) >= 0);
69281c49
LP
310 assert_se(streq(line, "foo ba..."));
311 line = mfree(line);
312
df0ff127 313 assert_se(get_process_cmdline(getpid_cached(), 11, true, &line) >= 0);
69281c49
LP
314 assert_se(streq(line, "foo bar..."));
315 line = mfree(line);
316
df0ff127 317 assert_se(get_process_cmdline(getpid_cached(), 12, true, &line) >= 0);
69281c49
LP
318 assert_se(streq(line, "foo bar..."));
319 line = mfree(line);
320
df0ff127 321 assert_se(get_process_cmdline(getpid_cached(), 13, true, &line) >= 0);
69281c49
LP
322 assert_se(streq(line, "foo bar quux"));
323 line = mfree(line);
324
df0ff127 325 assert_se(get_process_cmdline(getpid_cached(), 14, true, &line) >= 0);
69281c49
LP
326 assert_se(streq(line, "foo bar quux"));
327 line = mfree(line);
328
df0ff127 329 assert_se(get_process_cmdline(getpid_cached(), 1000, true, &line) >= 0);
69281c49
LP
330 assert_se(streq(line, "foo bar quux"));
331 line = mfree(line);
332
333 assert_se(ftruncate(fd, 0) >= 0);
334 assert_se(prctl(PR_SET_NAME, "aaaa bbbb cccc") >= 0);
335
df0ff127 336 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) == -ENOENT);
69281c49 337
df0ff127 338 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0);
69281c49
LP
339 assert_se(streq(line, "[aaaa bbbb cccc]"));
340 line = mfree(line);
341
df0ff127 342 assert_se(get_process_cmdline(getpid_cached(), 10, true, &line) >= 0);
69281c49
LP
343 assert_se(streq(line, "[aaaa...]"));
344 line = mfree(line);
345
df0ff127 346 assert_se(get_process_cmdline(getpid_cached(), 11, true, &line) >= 0);
69281c49
LP
347 assert_se(streq(line, "[aaaa...]"));
348 line = mfree(line);
349
df0ff127 350 assert_se(get_process_cmdline(getpid_cached(), 12, true, &line) >= 0);
69281c49
LP
351 assert_se(streq(line, "[aaaa b...]"));
352 line = mfree(line);
353
354 safe_close(fd);
355 _exit(0);
356}
357
049c884a 358static void test_rename_process_now(const char *p, int ret) {
9bfaffd5 359 _cleanup_free_ char *comm = NULL, *cmdline = NULL;
9bfaffd5
LP
360 int r;
361
9bfaffd5 362 r = rename_process(p);
9bfaffd5
LP
363 assert_se(r == ret ||
364 (ret == 0 && r >= 0) ||
365 (ret > 0 && r > 0));
366
367 if (r < 0)
049c884a 368 return;
9bfaffd5 369
349cc4a5 370#if HAVE_VALGRIND_VALGRIND_H
9bfaffd5
LP
371 /* see above, valgrind is weird, we can't verify what we are doing here */
372 if (RUNNING_ON_VALGRIND)
049c884a 373 return;
9bfaffd5
LP
374#endif
375
376 assert_se(get_process_comm(0, &comm) >= 0);
377 log_info("comm = <%s>", comm);
378 assert_se(strneq(comm, p, 15));
379
380 assert_se(get_process_cmdline(0, 0, false, &cmdline) >= 0);
049c884a
JW
381 /* we cannot expect cmdline to be renamed properly without privileges */
382 if (geteuid() == 0) {
383 log_info("cmdline = <%s>", cmdline);
384 assert_se(strneq(p, cmdline, strlen("test-process-util")));
385 assert_se(startswith(p, cmdline));
386 } else
387 log_info("cmdline = <%s> (not verified)", cmdline);
388}
389
390static void test_rename_process_one(const char *p, int ret) {
391 siginfo_t si;
392 pid_t pid;
393
394 pid = fork();
395 assert_se(pid >= 0);
396
397 if (pid == 0) {
398 /* child */
399 test_rename_process_now(p, ret);
400 _exit(EXIT_SUCCESS);
401 }
402
403 assert_se(wait_for_terminate(pid, &si) >= 0);
404 assert_se(si.si_code == CLD_EXITED);
405 assert_se(si.si_status == EXIT_SUCCESS);
406}
407
408static void test_rename_process_multi(void) {
409 pid_t pid;
410
411 pid = fork();
412 assert_se(pid >= 0);
9bfaffd5 413
049c884a
JW
414 if (pid > 0) {
415 siginfo_t si;
416
417 assert_se(wait_for_terminate(pid, &si) >= 0);
418 assert_se(si.si_code == CLD_EXITED);
419 assert_se(si.si_status == EXIT_SUCCESS);
420
421 return;
422 }
423
424 /* child */
425 test_rename_process_now("one", 1);
426 test_rename_process_now("more", 0); /* longer than "one", hence truncated */
427 setresuid(99, 99, 99);
428 test_rename_process_now("time!", 0);
429 test_rename_process_now("0", 1); /* shorter than "one", should fit */
430 test_rename_process_one("", -EINVAL);
431 test_rename_process_one(NULL, -EINVAL);
9bfaffd5
LP
432 _exit(EXIT_SUCCESS);
433}
434
435static void test_rename_process(void) {
436 test_rename_process_one(NULL, -EINVAL);
437 test_rename_process_one("", -EINVAL);
438 test_rename_process_one("foo", 1); /* should always fit */
439 test_rename_process_one("this is a really really long process name, followed by some more words", 0); /* unlikely to fit */
440 test_rename_process_one("1234567", 1); /* should always fit */
049c884a 441 test_rename_process_multi(); /* multiple invocations and dropped privileges */
9bfaffd5
LP
442}
443
5c30a6d2
LP
444static void test_getpid_cached(void) {
445 siginfo_t si;
446 pid_t a, b, c, d, e, f, child;
447
448 a = raw_getpid();
449 b = getpid_cached();
450 c = getpid();
451
452 assert_se(a == b && a == c);
453
454 child = fork();
455 assert_se(child >= 0);
456
457 if (child == 0) {
458 /* In child */
459 a = raw_getpid();
460 b = getpid_cached();
461 c = getpid();
462
463 assert_se(a == b && a == c);
464 _exit(0);
465 }
466
467 d = raw_getpid();
468 e = getpid_cached();
469 f = getpid();
470
471 assert_se(a == d && a == e && a == f);
472
473 assert_se(wait_for_terminate(child, &si) >= 0);
474 assert_se(si.si_status == 0);
475 assert_se(si.si_code == CLD_EXITED);
476}
477
478#define MEASURE_ITERATIONS (10000000LLU)
479
480static void test_getpid_measure(void) {
481 unsigned long long i;
482 usec_t t, q;
483
484 t = now(CLOCK_MONOTONIC);
485 for (i = 0; i < MEASURE_ITERATIONS; i++)
486 (void) getpid();
487 q = now(CLOCK_MONOTONIC) - t;
488
489 log_info(" glibc getpid(): %llu/s\n", (unsigned long long) (MEASURE_ITERATIONS*USEC_PER_SEC/q));
490
491 t = now(CLOCK_MONOTONIC);
492 for (i = 0; i < MEASURE_ITERATIONS; i++)
493 (void) getpid_cached();
494 q = now(CLOCK_MONOTONIC) - t;
495
496 log_info("getpid_cached(): %llu/s\n", (unsigned long long) (MEASURE_ITERATIONS*USEC_PER_SEC/q));
497}
498
0b452006 499int main(int argc, char *argv[]) {
9bfaffd5
LP
500
501 log_set_max_level(LOG_DEBUG);
0b452006
RC
502 log_parse_environment();
503 log_open();
504
9bfaffd5
LP
505 saved_argc = argc;
506 saved_argv = argv;
507
18dade5a
ZJS
508 if (argc > 1) {
509 pid_t pid = 0;
510
511 (void) parse_pid(argv[1], &pid);
512 test_get_process_comm(pid);
513 } else {
76c19e9f 514 TEST_REQ_RUNNING_SYSTEMD(test_get_process_comm(1));
18dade5a
ZJS
515 test_get_process_comm(getpid());
516 }
517
0b452006
RC
518 test_pid_is_unwaited();
519 test_pid_is_alive();
26fbedd7 520 test_personality();
69281c49 521 test_get_process_cmdline_harder();
9bfaffd5 522 test_rename_process();
5c30a6d2
LP
523 test_getpid_cached();
524 test_getpid_measure();
0b452006
RC
525
526 return 0;
527}