]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-process-util.c
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
)];
59 xsprintf(path
, "/proc/"PID_FMT
"/comm", pid
);
61 if (stat(path
, &st
) == 0) {
62 assert_se(get_process_comm(pid
, &a
) >= 0);
63 log_info("PID"PID_FMT
" comm: '%s'", pid
, a
);
65 log_warning("%s not exist.", path
);
67 assert_se(get_process_cmdline(pid
, 0, true, &c
) >= 0);
68 log_info("PID"PID_FMT
" cmdline: '%s'", pid
, c
);
70 assert_se(get_process_cmdline(pid
, 8, false, &d
) >= 0);
71 log_info("PID"PID_FMT
" cmdline truncated to 8: '%s'", pid
, d
);
74 assert_se(get_process_cmdline(pid
, 1, false, &d
) >= 0);
75 log_info("PID"PID_FMT
" cmdline truncated to 1: '%s'", pid
, d
);
77 assert_se(get_process_ppid(pid
, &e
) >= 0);
78 log_info("PID"PID_FMT
" PPID: "PID_FMT
, pid
, e
);
79 assert_se(pid
== 1 ? e
== 0 : e
> 0);
81 assert_se(is_kernel_thread(pid
) == 0 || pid
!= 1);
83 r
= get_process_exe(pid
, &f
);
84 assert_se(r
>= 0 || r
== -EACCES
);
85 log_info("PID"PID_FMT
" exe: '%s'", pid
, strna(f
));
87 assert_se(get_process_uid(pid
, &u
) == 0);
88 log_info("PID"PID_FMT
" UID: "UID_FMT
, pid
, u
);
89 assert_se(u
== 0 || pid
!= 1);
91 assert_se(get_process_gid(pid
, &g
) == 0);
92 log_info("PID"PID_FMT
" GID: "GID_FMT
, pid
, g
);
93 assert_se(g
== 0 || pid
!= 1);
95 r
= get_process_environ(pid
, &env
);
96 assert_se(r
>= 0 || r
== -EACCES
);
97 log_info("PID"PID_FMT
" strlen(environ): %zi", pid
, env
? (ssize_t
)strlen(env
) : (ssize_t
)-errno
);
99 if (!detect_container())
100 assert_se(get_ctty_devnr(pid
, &h
) == -ENXIO
|| pid
!= 1);
102 getenv_for_pid(pid
, "PATH", &i
);
103 log_info("PID"PID_FMT
" $PATH: '%s'", pid
, strna(i
));
106 static void test_pid_is_unwaited(void) {
116 waitpid(pid
, &status
, 0);
117 assert_se(!pid_is_unwaited(pid
));
119 assert_se(pid_is_unwaited(getpid_cached()));
120 assert_se(!pid_is_unwaited(-1));
123 static void test_pid_is_alive(void) {
133 waitpid(pid
, &status
, 0);
134 assert_se(!pid_is_alive(pid
));
136 assert_se(pid_is_alive(getpid_cached()));
137 assert_se(!pid_is_alive(-1));
140 static void test_personality(void) {
142 assert_se(personality_to_string(PER_LINUX
));
143 assert_se(!personality_to_string(PERSONALITY_INVALID
));
145 assert_se(streq(personality_to_string(PER_LINUX
), architecture_to_string(native_architecture())));
147 assert_se(personality_from_string(personality_to_string(PER_LINUX
)) == PER_LINUX
);
148 assert_se(personality_from_string(architecture_to_string(native_architecture())) == PER_LINUX
);
151 assert_se(streq_ptr(personality_to_string(PER_LINUX
), "x86-64"));
152 assert_se(streq_ptr(personality_to_string(PER_LINUX32
), "x86"));
154 assert_se(personality_from_string("x86-64") == PER_LINUX
);
155 assert_se(personality_from_string("x86") == PER_LINUX32
);
156 assert_se(personality_from_string("ia64") == PERSONALITY_INVALID
);
157 assert_se(personality_from_string(NULL
) == PERSONALITY_INVALID
);
159 assert_se(personality_from_string(personality_to_string(PER_LINUX32
)) == PER_LINUX32
);
163 static void test_get_process_cmdline_harder(void) {
164 char path
[] = "/tmp/test-cmdlineXXXXXX";
165 _cleanup_close_
int fd
= -1;
166 _cleanup_free_
char *line
= NULL
;
172 #if HAVE_VALGRIND_VALGRIND_H
173 /* valgrind patches open(/proc//cmdline)
174 * so, test_get_process_cmdline_harder fails always
175 * See https://github.com/systemd/systemd/pull/3555#issuecomment-226564908 */
176 if (RUNNING_ON_VALGRIND
)
184 (void) wait_for_terminate(pid
, &si
);
186 assert_se(si
.si_code
== CLD_EXITED
);
187 assert_se(si
.si_status
== 0);
193 assert_se(unshare(CLONE_NEWNS
) >= 0);
195 fd
= mkostemp(path
, O_CLOEXEC
);
198 if (mount(path
, "/proc/self/cmdline", "bind", MS_BIND
, NULL
) < 0) {
199 /* This happens under selinux… Abort the test in this case. */
200 log_warning_errno(errno
, "mount(..., \"/proc/self/cmdline\", \"bind\", ...) failed: %m");
201 assert(errno
== EACCES
);
205 assert_se(unlink(path
) >= 0);
207 assert_se(prctl(PR_SET_NAME
, "testa") >= 0);
209 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line
) == -ENOENT
);
211 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line
) >= 0);
212 assert_se(streq(line
, "[testa]"));
215 assert_se(get_process_cmdline(getpid_cached(), 1, true, &line
) >= 0);
216 assert_se(streq(line
, ""));
219 assert_se(get_process_cmdline(getpid_cached(), 2, true, &line
) >= 0);
220 assert_se(streq(line
, "["));
223 assert_se(get_process_cmdline(getpid_cached(), 3, true, &line
) >= 0);
224 assert_se(streq(line
, "[."));
227 assert_se(get_process_cmdline(getpid_cached(), 4, true, &line
) >= 0);
228 assert_se(streq(line
, "[.."));
231 assert_se(get_process_cmdline(getpid_cached(), 5, true, &line
) >= 0);
232 assert_se(streq(line
, "[..."));
235 assert_se(get_process_cmdline(getpid_cached(), 6, true, &line
) >= 0);
236 assert_se(streq(line
, "[...]"));
239 assert_se(get_process_cmdline(getpid_cached(), 7, true, &line
) >= 0);
240 assert_se(streq(line
, "[t...]"));
243 assert_se(get_process_cmdline(getpid_cached(), 8, true, &line
) >= 0);
244 assert_se(streq(line
, "[testa]"));
247 assert_se(write(fd
, "\0\0\0\0\0\0\0\0\0", 10) == 10);
249 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line
) == -ENOENT
);
251 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line
) >= 0);
252 assert_se(streq(line
, "[testa]"));
255 assert_se(write(fd
, "foo\0bar\0\0\0\0\0", 10) == 10);
257 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line
) >= 0);
258 assert_se(streq(line
, "foo bar"));
261 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line
) >= 0);
262 assert_se(streq(line
, "foo bar"));
265 assert_se(write(fd
, "quux", 4) == 4);
266 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line
) >= 0);
267 assert_se(streq(line
, "foo bar quux"));
270 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line
) >= 0);
271 assert_se(streq(line
, "foo bar quux"));
274 assert_se(get_process_cmdline(getpid_cached(), 1, true, &line
) >= 0);
275 assert_se(streq(line
, ""));
278 assert_se(get_process_cmdline(getpid_cached(), 2, true, &line
) >= 0);
279 assert_se(streq(line
, "."));
282 assert_se(get_process_cmdline(getpid_cached(), 3, true, &line
) >= 0);
283 assert_se(streq(line
, ".."));
286 assert_se(get_process_cmdline(getpid_cached(), 4, true, &line
) >= 0);
287 assert_se(streq(line
, "..."));
290 assert_se(get_process_cmdline(getpid_cached(), 5, true, &line
) >= 0);
291 assert_se(streq(line
, "f..."));
294 assert_se(get_process_cmdline(getpid_cached(), 6, true, &line
) >= 0);
295 assert_se(streq(line
, "fo..."));
298 assert_se(get_process_cmdline(getpid_cached(), 7, true, &line
) >= 0);
299 assert_se(streq(line
, "foo..."));
302 assert_se(get_process_cmdline(getpid_cached(), 8, true, &line
) >= 0);
303 assert_se(streq(line
, "foo..."));
306 assert_se(get_process_cmdline(getpid_cached(), 9, true, &line
) >= 0);
307 assert_se(streq(line
, "foo b..."));
310 assert_se(get_process_cmdline(getpid_cached(), 10, true, &line
) >= 0);
311 assert_se(streq(line
, "foo ba..."));
314 assert_se(get_process_cmdline(getpid_cached(), 11, true, &line
) >= 0);
315 assert_se(streq(line
, "foo bar..."));
318 assert_se(get_process_cmdline(getpid_cached(), 12, true, &line
) >= 0);
319 assert_se(streq(line
, "foo bar..."));
322 assert_se(get_process_cmdline(getpid_cached(), 13, true, &line
) >= 0);
323 assert_se(streq(line
, "foo bar quux"));
326 assert_se(get_process_cmdline(getpid_cached(), 14, true, &line
) >= 0);
327 assert_se(streq(line
, "foo bar quux"));
330 assert_se(get_process_cmdline(getpid_cached(), 1000, true, &line
) >= 0);
331 assert_se(streq(line
, "foo bar quux"));
334 assert_se(ftruncate(fd
, 0) >= 0);
335 assert_se(prctl(PR_SET_NAME
, "aaaa bbbb cccc") >= 0);
337 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line
) == -ENOENT
);
339 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line
) >= 0);
340 assert_se(streq(line
, "[aaaa bbbb cccc]"));
343 assert_se(get_process_cmdline(getpid_cached(), 10, true, &line
) >= 0);
344 assert_se(streq(line
, "[aaaa...]"));
347 assert_se(get_process_cmdline(getpid_cached(), 11, true, &line
) >= 0);
348 assert_se(streq(line
, "[aaaa...]"));
351 assert_se(get_process_cmdline(getpid_cached(), 12, true, &line
) >= 0);
352 assert_se(streq(line
, "[aaaa b...]"));
359 static void test_rename_process_now(const char *p
, int ret
) {
360 _cleanup_free_
char *comm
= NULL
, *cmdline
= NULL
;
363 r
= rename_process(p
);
364 assert_se(r
== ret
||
365 (ret
== 0 && r
>= 0) ||
371 #if HAVE_VALGRIND_VALGRIND_H
372 /* see above, valgrind is weird, we can't verify what we are doing here */
373 if (RUNNING_ON_VALGRIND
)
377 assert_se(get_process_comm(0, &comm
) >= 0);
378 log_info("comm = <%s>", comm
);
379 assert_se(strneq(comm
, p
, 15));
381 assert_se(get_process_cmdline(0, 0, false, &cmdline
) >= 0);
382 /* we cannot expect cmdline to be renamed properly without privileges */
383 if (geteuid() == 0) {
384 log_info("cmdline = <%s>", cmdline
);
385 assert_se(strneq(p
, cmdline
, strlen("test-process-util")));
386 assert_se(startswith(p
, cmdline
));
388 log_info("cmdline = <%s> (not verified)", cmdline
);
391 static void test_rename_process_one(const char *p
, int ret
) {
400 test_rename_process_now(p
, ret
);
404 assert_se(wait_for_terminate(pid
, &si
) >= 0);
405 assert_se(si
.si_code
== CLD_EXITED
);
406 assert_se(si
.si_status
== EXIT_SUCCESS
);
409 static void test_rename_process_multi(void) {
418 assert_se(wait_for_terminate(pid
, &si
) >= 0);
419 assert_se(si
.si_code
== CLD_EXITED
);
420 assert_se(si
.si_status
== EXIT_SUCCESS
);
426 test_rename_process_now("one", 1);
427 test_rename_process_now("more", 0); /* longer than "one", hence truncated */
428 setresuid(99, 99, 99);
429 test_rename_process_now("time!", 0);
430 test_rename_process_now("0", 1); /* shorter than "one", should fit */
431 test_rename_process_one("", -EINVAL
);
432 test_rename_process_one(NULL
, -EINVAL
);
436 static void test_rename_process(void) {
437 test_rename_process_one(NULL
, -EINVAL
);
438 test_rename_process_one("", -EINVAL
);
439 test_rename_process_one("foo", 1); /* should always fit */
440 test_rename_process_one("this is a really really long process name, followed by some more words", 0); /* unlikely to fit */
441 test_rename_process_one("1234567", 1); /* should always fit */
442 test_rename_process_multi(); /* multiple invocations and dropped privileges */
445 static void test_getpid_cached(void) {
447 pid_t a
, b
, c
, d
, e
, f
, child
;
453 assert_se(a
== b
&& a
== c
);
456 assert_se(child
>= 0);
464 assert_se(a
== b
&& a
== c
);
472 assert_se(a
== d
&& a
== e
&& a
== f
);
474 assert_se(wait_for_terminate(child
, &si
) >= 0);
475 assert_se(si
.si_status
== 0);
476 assert_se(si
.si_code
== CLD_EXITED
);
479 #define MEASURE_ITERATIONS (10000000LLU)
481 static void test_getpid_measure(void) {
482 unsigned long long i
;
485 t
= now(CLOCK_MONOTONIC
);
486 for (i
= 0; i
< MEASURE_ITERATIONS
; i
++)
488 q
= now(CLOCK_MONOTONIC
) - t
;
490 log_info(" glibc getpid(): %llu/s\n", (unsigned long long) (MEASURE_ITERATIONS
*USEC_PER_SEC
/q
));
492 t
= now(CLOCK_MONOTONIC
);
493 for (i
= 0; i
< MEASURE_ITERATIONS
; i
++)
494 (void) getpid_cached();
495 q
= now(CLOCK_MONOTONIC
) - t
;
497 log_info("getpid_cached(): %llu/s\n", (unsigned long long) (MEASURE_ITERATIONS
*USEC_PER_SEC
/q
));
500 int main(int argc
, char *argv
[]) {
502 log_set_max_level(LOG_DEBUG
);
503 log_parse_environment();
512 (void) parse_pid(argv
[1], &pid
);
513 test_get_process_comm(pid
);
515 TEST_REQ_RUNNING_SYSTEMD(test_get_process_comm(1));
516 test_get_process_comm(getpid());
519 test_pid_is_unwaited();
522 test_get_process_cmdline_harder();
523 test_rename_process();
524 test_getpid_cached();
525 test_getpid_measure();