]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-process-util.c
2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
5 Copyright 2013 Thomas H.P. Andersen
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.
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.
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/>.
22 #include <sys/mount.h>
23 #include <sys/personality.h>
24 #include <sys/prctl.h>
26 #include <sys/types.h>
29 #ifdef HAVE_VALGRIND_VALGRIND_H
30 #include <valgrind/valgrind.h>
33 #include "alloc-util.h"
34 #include "architecture.h"
38 #include "parse-util.h"
39 #include "process-util.h"
40 #include "stdio-util.h"
41 #include "string-util.h"
42 #include "terminal-util.h"
43 #include "test-helper.h"
47 static void test_get_process_comm(pid_t pid
) {
49 _cleanup_free_
char *a
= NULL
, *c
= NULL
, *d
= NULL
, *f
= NULL
, *i
= NULL
;
50 _cleanup_free_
char *env
= NULL
;
51 char path
[strlen("/proc//comm") + DECIMAL_STR_MAX(pid_t
)];
58 xsprintf(path
, "/proc/"PID_FMT
"/comm", pid
);
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
);
64 log_warning("%s not exist.", path
);
66 assert_se(get_process_cmdline(pid
, 0, true, &c
) >= 0);
67 log_info("PID"PID_FMT
" cmdline: '%s'", pid
, c
);
69 assert_se(get_process_cmdline(pid
, 8, false, &d
) >= 0);
70 log_info("PID"PID_FMT
" cmdline truncated to 8: '%s'", pid
, 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
);
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);
80 assert_se(is_kernel_thread(pid
) == 0 || pid
!= 1);
82 r
= get_process_exe(pid
, &f
);
83 assert_se(r
>= 0 || r
== -EACCES
);
84 log_info("PID"PID_FMT
" exe: '%s'", pid
, strna(f
));
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);
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);
94 r
= get_process_environ(pid
, &env
);
95 assert_se(r
>= 0 || r
== -EACCES
);
96 log_info("PID"PID_FMT
" strlen(environ): %zi", pid
, env
? (ssize_t
)strlen(env
) : (ssize_t
)-errno
);
98 if (!detect_container())
99 assert_se(get_ctty_devnr(pid
, &h
) == -ENXIO
|| pid
!= 1);
101 getenv_for_pid(pid
, "PATH", &i
);
102 log_info("PID"PID_FMT
" $PATH: '%s'", pid
, strna(i
));
105 static void test_pid_is_unwaited(void) {
115 waitpid(pid
, &status
, 0);
116 assert_se(!pid_is_unwaited(pid
));
118 assert_se(pid_is_unwaited(getpid_cached()));
119 assert_se(!pid_is_unwaited(-1));
122 static void test_pid_is_alive(void) {
132 waitpid(pid
, &status
, 0);
133 assert_se(!pid_is_alive(pid
));
135 assert_se(pid_is_alive(getpid_cached()));
136 assert_se(!pid_is_alive(-1));
139 static void test_personality(void) {
141 assert_se(personality_to_string(PER_LINUX
));
142 assert_se(!personality_to_string(PERSONALITY_INVALID
));
144 assert_se(streq(personality_to_string(PER_LINUX
), architecture_to_string(native_architecture())));
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
);
150 assert_se(streq_ptr(personality_to_string(PER_LINUX
), "x86-64"));
151 assert_se(streq_ptr(personality_to_string(PER_LINUX32
), "x86"));
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
);
158 assert_se(personality_from_string(personality_to_string(PER_LINUX32
)) == PER_LINUX32
);
162 static 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
;
171 #ifdef HAVE_VALGRIND_VALGRIND_H
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
)
183 (void) wait_for_terminate(pid
, &si
);
185 assert_se(si
.si_code
== CLD_EXITED
);
186 assert_se(si
.si_status
== 0);
192 assert_se(unshare(CLONE_NEWNS
) >= 0);
194 fd
= mkostemp(path
, O_CLOEXEC
);
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
);
204 assert_se(unlink(path
) >= 0);
206 assert_se(prctl(PR_SET_NAME
, "testa") >= 0);
208 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line
) == -ENOENT
);
210 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line
) >= 0);
211 assert_se(streq(line
, "[testa]"));
214 assert_se(get_process_cmdline(getpid_cached(), 1, true, &line
) >= 0);
215 assert_se(streq(line
, ""));
218 assert_se(get_process_cmdline(getpid_cached(), 2, true, &line
) >= 0);
219 assert_se(streq(line
, "["));
222 assert_se(get_process_cmdline(getpid_cached(), 3, true, &line
) >= 0);
223 assert_se(streq(line
, "[."));
226 assert_se(get_process_cmdline(getpid_cached(), 4, true, &line
) >= 0);
227 assert_se(streq(line
, "[.."));
230 assert_se(get_process_cmdline(getpid_cached(), 5, true, &line
) >= 0);
231 assert_se(streq(line
, "[..."));
234 assert_se(get_process_cmdline(getpid_cached(), 6, true, &line
) >= 0);
235 assert_se(streq(line
, "[...]"));
238 assert_se(get_process_cmdline(getpid_cached(), 7, true, &line
) >= 0);
239 assert_se(streq(line
, "[t...]"));
242 assert_se(get_process_cmdline(getpid_cached(), 8, true, &line
) >= 0);
243 assert_se(streq(line
, "[testa]"));
246 assert_se(write(fd
, "\0\0\0\0\0\0\0\0\0", 10) == 10);
248 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line
) == -ENOENT
);
250 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line
) >= 0);
251 assert_se(streq(line
, "[testa]"));
254 assert_se(write(fd
, "foo\0bar\0\0\0\0\0", 10) == 10);
256 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line
) >= 0);
257 assert_se(streq(line
, "foo bar"));
260 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line
) >= 0);
261 assert_se(streq(line
, "foo bar"));
264 assert_se(write(fd
, "quux", 4) == 4);
265 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line
) >= 0);
266 assert_se(streq(line
, "foo bar quux"));
269 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line
) >= 0);
270 assert_se(streq(line
, "foo bar quux"));
273 assert_se(get_process_cmdline(getpid_cached(), 1, true, &line
) >= 0);
274 assert_se(streq(line
, ""));
277 assert_se(get_process_cmdline(getpid_cached(), 2, true, &line
) >= 0);
278 assert_se(streq(line
, "."));
281 assert_se(get_process_cmdline(getpid_cached(), 3, true, &line
) >= 0);
282 assert_se(streq(line
, ".."));
285 assert_se(get_process_cmdline(getpid_cached(), 4, true, &line
) >= 0);
286 assert_se(streq(line
, "..."));
289 assert_se(get_process_cmdline(getpid_cached(), 5, true, &line
) >= 0);
290 assert_se(streq(line
, "f..."));
293 assert_se(get_process_cmdline(getpid_cached(), 6, true, &line
) >= 0);
294 assert_se(streq(line
, "fo..."));
297 assert_se(get_process_cmdline(getpid_cached(), 7, true, &line
) >= 0);
298 assert_se(streq(line
, "foo..."));
301 assert_se(get_process_cmdline(getpid_cached(), 8, true, &line
) >= 0);
302 assert_se(streq(line
, "foo..."));
305 assert_se(get_process_cmdline(getpid_cached(), 9, true, &line
) >= 0);
306 assert_se(streq(line
, "foo b..."));
309 assert_se(get_process_cmdline(getpid_cached(), 10, true, &line
) >= 0);
310 assert_se(streq(line
, "foo ba..."));
313 assert_se(get_process_cmdline(getpid_cached(), 11, true, &line
) >= 0);
314 assert_se(streq(line
, "foo bar..."));
317 assert_se(get_process_cmdline(getpid_cached(), 12, true, &line
) >= 0);
318 assert_se(streq(line
, "foo bar..."));
321 assert_se(get_process_cmdline(getpid_cached(), 13, true, &line
) >= 0);
322 assert_se(streq(line
, "foo bar quux"));
325 assert_se(get_process_cmdline(getpid_cached(), 14, true, &line
) >= 0);
326 assert_se(streq(line
, "foo bar quux"));
329 assert_se(get_process_cmdline(getpid_cached(), 1000, true, &line
) >= 0);
330 assert_se(streq(line
, "foo bar quux"));
333 assert_se(ftruncate(fd
, 0) >= 0);
334 assert_se(prctl(PR_SET_NAME
, "aaaa bbbb cccc") >= 0);
336 assert_se(get_process_cmdline(getpid_cached(), 0, false, &line
) == -ENOENT
);
338 assert_se(get_process_cmdline(getpid_cached(), 0, true, &line
) >= 0);
339 assert_se(streq(line
, "[aaaa bbbb cccc]"));
342 assert_se(get_process_cmdline(getpid_cached(), 10, true, &line
) >= 0);
343 assert_se(streq(line
, "[aaaa...]"));
346 assert_se(get_process_cmdline(getpid_cached(), 11, true, &line
) >= 0);
347 assert_se(streq(line
, "[aaaa...]"));
350 assert_se(get_process_cmdline(getpid_cached(), 12, true, &line
) >= 0);
351 assert_se(streq(line
, "[aaaa b...]"));
358 static void test_rename_process_now(const char *p
, int ret
) {
359 _cleanup_free_
char *comm
= NULL
, *cmdline
= NULL
;
362 r
= rename_process(p
);
363 assert_se(r
== ret
||
364 (ret
== 0 && r
>= 0) ||
370 #ifdef HAVE_VALGRIND_VALGRIND_H
371 /* see above, valgrind is weird, we can't verify what we are doing here */
372 if (RUNNING_ON_VALGRIND
)
376 assert_se(get_process_comm(0, &comm
) >= 0);
377 log_info("comm = <%s>", comm
);
378 assert_se(strneq(comm
, p
, 15));
380 assert_se(get_process_cmdline(0, 0, false, &cmdline
) >= 0);
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
));
387 log_info("cmdline = <%s> (not verified)", cmdline
);
390 static void test_rename_process_one(const char *p
, int ret
) {
399 test_rename_process_now(p
, ret
);
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
);
408 static void test_rename_process_multi(void) {
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
);
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
);
435 static 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 */
441 test_rename_process_multi(); /* multiple invocations and dropped privileges */
444 static void test_getpid_cached(void) {
446 pid_t a
, b
, c
, d
, e
, f
, child
;
452 assert_se(a
== b
&& a
== c
);
455 assert_se(child
>= 0);
463 assert_se(a
== b
&& a
== c
);
471 assert_se(a
== d
&& a
== e
&& a
== f
);
473 assert_se(wait_for_terminate(child
, &si
) >= 0);
474 assert_se(si
.si_status
== 0);
475 assert_se(si
.si_code
== CLD_EXITED
);
478 #define MEASURE_ITERATIONS (10000000LLU)
480 static void test_getpid_measure(void) {
481 unsigned long long i
;
484 t
= now(CLOCK_MONOTONIC
);
485 for (i
= 0; i
< MEASURE_ITERATIONS
; i
++)
487 q
= now(CLOCK_MONOTONIC
) - t
;
489 log_info(" glibc getpid(): %llu/s\n", (unsigned long long) (MEASURE_ITERATIONS
*USEC_PER_SEC
/q
));
491 t
= now(CLOCK_MONOTONIC
);
492 for (i
= 0; i
< MEASURE_ITERATIONS
; i
++)
493 (void) getpid_cached();
494 q
= now(CLOCK_MONOTONIC
) - t
;
496 log_info("getpid_cached(): %llu/s\n", (unsigned long long) (MEASURE_ITERATIONS
*USEC_PER_SEC
/q
));
499 int main(int argc
, char *argv
[]) {
501 log_set_max_level(LOG_DEBUG
);
502 log_parse_environment();
511 (void) parse_pid(argv
[1], &pid
);
512 test_get_process_comm(pid
);
514 TEST_REQ_RUNNING_SYSTEMD(test_get_process_comm(1));
515 test_get_process_comm(getpid());
518 test_pid_is_unwaited();
521 test_get_process_cmdline_harder();
522 test_rename_process();
523 test_getpid_cached();
524 test_getpid_measure();