1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
6 Copyright 2013 Thomas H.P. Andersen
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include <sys/mount.h>
24 #include <sys/personality.h>
25 #include <sys/prctl.h>
27 #include <sys/types.h>
30 #if HAVE_VALGRIND_VALGRIND_H
31 #include <valgrind/valgrind.h>
34 #include "alloc-util.h"
35 //#include "architecture.h"
39 #include "parse-util.h"
40 #include "process-util.h"
41 #include "stdio-util.h"
42 #include "string-util.h"
43 #include "terminal-util.h"
44 #include "test-helper.h"
48 static void test_get_process_comm(pid_t pid) {
50 _cleanup_free_ char *a = NULL, *c = NULL, *d = NULL, *f = NULL, *i = NULL;
51 _cleanup_free_ char *env = NULL;
52 char path[STRLEN("/proc//comm") + DECIMAL_STR_MAX(pid_t)];
53 #if 0 /// UNNEEDED by elogind
61 xsprintf(path, "/proc/"PID_FMT"/comm", pid);
63 if (stat(path, &st) == 0) {
64 assert_se(get_process_comm(pid, &a) >= 0);
65 log_info("PID"PID_FMT" comm: '%s'", pid, a);
67 log_warning("%s not exist.", path);
69 assert_se(get_process_cmdline(pid, 0, true, &c) >= 0);
70 log_info("PID"PID_FMT" cmdline: '%s'", pid, c);
72 assert_se(get_process_cmdline(pid, 8, false, &d) >= 0);
73 log_info("PID"PID_FMT" cmdline truncated to 8: '%s'", pid, d);
76 assert_se(get_process_cmdline(pid, 1, false, &d) >= 0);
77 log_info("PID"PID_FMT" cmdline truncated to 1: '%s'", pid, d);
79 #if 0 /// UNNEEDED by elogind
80 assert_se(get_process_ppid(pid, &e) >= 0);
81 log_info("PID"PID_FMT" PPID: "PID_FMT, pid, e);
82 assert_se(pid == 1 ? e == 0 : e > 0);
85 assert_se(is_kernel_thread(pid) == 0 || pid != 1);
87 r = get_process_exe(pid, &f);
88 assert_se(r >= 0 || r == -EACCES);
89 log_info("PID"PID_FMT" exe: '%s'", pid, strna(f));
91 #if 0 /// UNNEEDED by elogind
92 assert_se(get_process_uid(pid, &u) == 0);
93 log_info("PID"PID_FMT" UID: "UID_FMT, pid, u);
94 assert_se(u == 0 || pid != 1);
96 assert_se(get_process_gid(pid, &g) == 0);
97 log_info("PID"PID_FMT" GID: "GID_FMT, pid, g);
98 assert_se(g == 0 || pid != 1);
100 r = get_process_environ(pid, &env);
101 assert_se(r >= 0 || r == -EACCES);
102 log_info("PID"PID_FMT" strlen(environ): %zi", pid, env ? (ssize_t)strlen(env) : (ssize_t)-errno);
105 if (!detect_container())
106 assert_se(get_ctty_devnr(pid, &h) == -ENXIO || pid != 1);
108 getenv_for_pid(pid, "PATH", &i);
109 log_info("PID"PID_FMT" $PATH: '%s'", pid, strna(i));
112 static void test_pid_is_unwaited(void) {
122 waitpid(pid, &status, 0);
123 assert_se(!pid_is_unwaited(pid));
125 assert_se(pid_is_unwaited(getpid_cached()));
126 assert_se(!pid_is_unwaited(-1));
129 static void test_pid_is_alive(void) {
139 waitpid(pid, &status, 0);
140 assert_se(!pid_is_alive(pid));
142 assert_se(pid_is_alive(getpid_cached()));
143 assert_se(!pid_is_alive(-1));
146 #if 0 /// UNNEEDED by elogind
147 static void test_personality(void) {
149 assert_se(personality_to_string(PER_LINUX));
150 assert_se(!personality_to_string(PERSONALITY_INVALID));
152 assert_se(streq(personality_to_string(PER_LINUX), architecture_to_string(native_architecture())));
154 assert_se(personality_from_string(personality_to_string(PER_LINUX)) == PER_LINUX);
155 assert_se(personality_from_string(architecture_to_string(native_architecture())) == PER_LINUX);
158 assert_se(streq_ptr(personality_to_string(PER_LINUX), "x86-64"));
159 assert_se(streq_ptr(personality_to_string(PER_LINUX32), "x86"));
161 assert_se(personality_from_string("x86-64") == PER_LINUX);
162 assert_se(personality_from_string("x86") == PER_LINUX32);
163 assert_se(personality_from_string("ia64") == PERSONALITY_INVALID);
164 assert_se(personality_from_string(NULL) == PERSONALITY_INVALID);
166 assert_se(personality_from_string(personality_to_string(PER_LINUX32)) == PER_LINUX32);
171 static void test_get_process_cmdline_harder(void) {
172 char path[] = "/tmp/test-cmdlineXXXXXX";
173 _cleanup_close_ int fd = -1;
174 _cleanup_free_ char *line = NULL;
180 #if HAVE_VALGRIND_VALGRIND_H
181 /* valgrind patches open(/proc//cmdline)
182 * so, test_get_process_cmdline_harder fails always
183 * See https://github.com/systemd/systemd/pull/3555#issuecomment-226564908 */
184 if (RUNNING_ON_VALGRIND)
192 (void) wait_for_terminate(pid, &si);
194 assert_se(si.si_code == CLD_EXITED);
195 assert_se(si.si_status == 0);
201 assert_se(unshare(CLONE_NEWNS) >= 0);
203 fd = mkostemp(path, O_CLOEXEC);
206 if (mount(path, "/proc/self/cmdline", "bind", MS_BIND, NULL) < 0) {
207 /* This happens under selinux… Abort the test in this case. */
208 log_warning_errno(errno, "mount(..., \"/proc/self/cmdline\", \"bind\", ...) failed: %m");
209 assert(errno == EACCES);
213 assert_se(unlink(path) >= 0);
215 assert_se(prctl(PR_SET_NAME, "testa") >= 0);
217 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) == -ENOENT);
219 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0);
220 assert_se(streq(line, "[testa]"));
223 assert_se(get_process_cmdline(getpid_cached(), 1, true, &line) >= 0);
224 assert_se(streq(line, ""));
227 assert_se(get_process_cmdline(getpid_cached(), 2, true, &line) >= 0);
228 assert_se(streq(line, "["));
231 assert_se(get_process_cmdline(getpid_cached(), 3, true, &line) >= 0);
232 assert_se(streq(line, "[."));
235 assert_se(get_process_cmdline(getpid_cached(), 4, true, &line) >= 0);
236 assert_se(streq(line, "[.."));
239 assert_se(get_process_cmdline(getpid_cached(), 5, true, &line) >= 0);
240 assert_se(streq(line, "[..."));
243 assert_se(get_process_cmdline(getpid_cached(), 6, true, &line) >= 0);
244 assert_se(streq(line, "[...]"));
247 assert_se(get_process_cmdline(getpid_cached(), 7, true, &line) >= 0);
248 assert_se(streq(line, "[t...]"));
251 assert_se(get_process_cmdline(getpid_cached(), 8, true, &line) >= 0);
252 assert_se(streq(line, "[testa]"));
255 assert_se(write(fd, "\0\0\0\0\0\0\0\0\0", 10) == 10);
257 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) == -ENOENT);
259 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0);
260 assert_se(streq(line, "[testa]"));
263 assert_se(write(fd, "foo\0bar\0\0\0\0\0", 10) == 10);
265 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) >= 0);
266 assert_se(streq(line, "foo bar"));
269 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0);
270 assert_se(streq(line, "foo bar"));
273 assert_se(write(fd, "quux", 4) == 4);
274 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) >= 0);
275 assert_se(streq(line, "foo bar quux"));
278 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0);
279 assert_se(streq(line, "foo bar quux"));
282 assert_se(get_process_cmdline(getpid_cached(), 1, true, &line) >= 0);
283 assert_se(streq(line, ""));
286 assert_se(get_process_cmdline(getpid_cached(), 2, true, &line) >= 0);
287 assert_se(streq(line, "."));
290 assert_se(get_process_cmdline(getpid_cached(), 3, true, &line) >= 0);
291 assert_se(streq(line, ".."));
294 assert_se(get_process_cmdline(getpid_cached(), 4, true, &line) >= 0);
295 assert_se(streq(line, "..."));
298 assert_se(get_process_cmdline(getpid_cached(), 5, true, &line) >= 0);
299 assert_se(streq(line, "f..."));
302 assert_se(get_process_cmdline(getpid_cached(), 6, true, &line) >= 0);
303 assert_se(streq(line, "fo..."));
306 assert_se(get_process_cmdline(getpid_cached(), 7, true, &line) >= 0);
307 assert_se(streq(line, "foo..."));
310 assert_se(get_process_cmdline(getpid_cached(), 8, true, &line) >= 0);
311 assert_se(streq(line, "foo..."));
314 assert_se(get_process_cmdline(getpid_cached(), 9, true, &line) >= 0);
315 assert_se(streq(line, "foo b..."));
318 assert_se(get_process_cmdline(getpid_cached(), 10, true, &line) >= 0);
319 assert_se(streq(line, "foo ba..."));
322 assert_se(get_process_cmdline(getpid_cached(), 11, true, &line) >= 0);
323 assert_se(streq(line, "foo bar..."));
326 assert_se(get_process_cmdline(getpid_cached(), 12, true, &line) >= 0);
327 assert_se(streq(line, "foo bar..."));
330 assert_se(get_process_cmdline(getpid_cached(), 13, true, &line) >= 0);
331 assert_se(streq(line, "foo bar quux"));
334 assert_se(get_process_cmdline(getpid_cached(), 14, true, &line) >= 0);
335 assert_se(streq(line, "foo bar quux"));
338 assert_se(get_process_cmdline(getpid_cached(), 1000, true, &line) >= 0);
339 assert_se(streq(line, "foo bar quux"));
342 assert_se(ftruncate(fd, 0) >= 0);
343 assert_se(prctl(PR_SET_NAME, "aaaa bbbb cccc") >= 0);
345 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) == -ENOENT);
347 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0);
348 assert_se(streq(line, "[aaaa bbbb cccc]"));
351 assert_se(get_process_cmdline(getpid_cached(), 10, true, &line) >= 0);
352 assert_se(streq(line, "[aaaa...]"));
355 assert_se(get_process_cmdline(getpid_cached(), 11, true, &line) >= 0);
356 assert_se(streq(line, "[aaaa...]"));
359 assert_se(get_process_cmdline(getpid_cached(), 12, true, &line) >= 0);
360 assert_se(streq(line, "[aaaa b...]"));
367 #if 0 /// UNNEEDED by elogind
368 static void test_rename_process_now(const char *p, int ret) {
369 _cleanup_free_ char *comm = NULL, *cmdline = NULL;
372 r = rename_process(p);
373 assert_se(r == ret ||
374 (ret == 0 && r >= 0) ||
380 #if HAVE_VALGRIND_VALGRIND_H
381 /* see above, valgrind is weird, we can't verify what we are doing here */
382 if (RUNNING_ON_VALGRIND)
386 assert_se(get_process_comm(0, &comm) >= 0);
387 log_info("comm = <%s>", comm);
388 assert_se(strneq(comm, p, 15));
390 assert_se(get_process_cmdline(0, 0, false, &cmdline) >= 0);
391 /* we cannot expect cmdline to be renamed properly without privileges */
392 if (geteuid() == 0) {
393 log_info("cmdline = <%s>", cmdline);
394 assert_se(strneq(p, cmdline, STRLEN("test-process-util")));
395 assert_se(startswith(p, cmdline));
397 log_info("cmdline = <%s> (not verified)", cmdline);
400 static void test_rename_process_one(const char *p, int ret) {
409 test_rename_process_now(p, ret);
413 assert_se(wait_for_terminate(pid, &si) >= 0);
414 assert_se(si.si_code == CLD_EXITED);
415 assert_se(si.si_status == EXIT_SUCCESS);
418 static void test_rename_process_multi(void) {
427 assert_se(wait_for_terminate(pid, &si) >= 0);
428 assert_se(si.si_code == CLD_EXITED);
429 assert_se(si.si_status == EXIT_SUCCESS);
435 test_rename_process_now("one", 1);
436 test_rename_process_now("more", 0); /* longer than "one", hence truncated */
437 (void) setresuid(99, 99, 99); /* change uid when running privileged */
438 test_rename_process_now("time!", 0);
439 test_rename_process_now("0", 1); /* shorter than "one", should fit */
440 test_rename_process_one("", -EINVAL);
441 test_rename_process_one(NULL, -EINVAL);
445 static void test_rename_process(void) {
446 test_rename_process_one(NULL, -EINVAL);
447 test_rename_process_one("", -EINVAL);
448 test_rename_process_one("foo", 1); /* should always fit */
449 test_rename_process_one("this is a really really long process name, followed by some more words", 0); /* unlikely to fit */
450 test_rename_process_one("1234567", 1); /* should always fit */
451 test_rename_process_multi(); /* multiple invocations and dropped privileges */
455 static void test_getpid_cached(void) {
457 pid_t a, b, c, d, e, f, child;
463 assert_se(a == b && a == c);
466 assert_se(child >= 0);
474 assert_se(a == b && a == c);
482 assert_se(a == d && a == e && a == f);
484 assert_se(wait_for_terminate(child, &si) >= 0);
485 assert_se(si.si_status == 0);
486 assert_se(si.si_code == CLD_EXITED);
489 #define MEASURE_ITERATIONS (10000000LLU)
491 static void test_getpid_measure(void) {
492 unsigned long long i;
495 t = now(CLOCK_MONOTONIC);
496 for (i = 0; i < MEASURE_ITERATIONS; i++)
498 q = now(CLOCK_MONOTONIC) - t;
500 log_info(" glibc getpid(): %llu/s\n", (unsigned long long) (MEASURE_ITERATIONS*USEC_PER_SEC/q));
502 t = now(CLOCK_MONOTONIC);
503 for (i = 0; i < MEASURE_ITERATIONS; i++)
504 (void) getpid_cached();
505 q = now(CLOCK_MONOTONIC) - t;
507 log_info("getpid_cached(): %llu/s\n", (unsigned long long) (MEASURE_ITERATIONS*USEC_PER_SEC/q));
510 int main(int argc, char *argv[]) {
512 log_set_max_level(LOG_DEBUG);
513 log_parse_environment();
522 (void) parse_pid(argv[1], &pid);
523 test_get_process_comm(pid);
525 TEST_REQ_RUNNING_SYSTEMD(test_get_process_comm(1));
526 test_get_process_comm(getpid());
529 test_pid_is_unwaited();
531 #if 0 /// UNNEEDED by elogind
534 test_get_process_cmdline_harder();
535 #if 0 /// UNNEEDED by elogind
536 test_rename_process();
538 test_getpid_cached();
539 test_getpid_measure();