]>
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>
30 #include "alloc-util.h"
31 #include "architecture.h"
35 #include "parse-util.h"
36 #include "process-util.h"
37 #include "stdio-util.h"
38 #include "string-util.h"
39 #include "terminal-util.h"
43 static void test_get_process_comm(pid_t pid
) {
45 _cleanup_free_
char *a
= NULL
, *c
= NULL
, *d
= NULL
, *f
= NULL
, *i
= NULL
, *cwd
= NULL
, *root
= NULL
;
46 _cleanup_free_
char *env
= NULL
;
47 char path
[strlen("/proc//comm") + DECIMAL_STR_MAX(pid_t
)];
54 xsprintf(path
, "/proc/"PID_FMT
"/comm", pid
);
56 if (stat(path
, &st
) == 0) {
57 assert_se(get_process_comm(pid
, &a
) >= 0);
58 log_info("PID"PID_FMT
" comm: '%s'", pid
, a
);
60 log_warning("%s not exist.", path
);
62 assert_se(get_process_cmdline(pid
, 0, true, &c
) >= 0);
63 log_info("PID"PID_FMT
" cmdline: '%s'", pid
, c
);
65 assert_se(get_process_cmdline(pid
, 8, false, &d
) >= 0);
66 log_info("PID"PID_FMT
" cmdline truncated to 8: '%s'", pid
, d
);
69 assert_se(get_process_cmdline(pid
, 1, false, &d
) >= 0);
70 log_info("PID"PID_FMT
" cmdline truncated to 1: '%s'", pid
, d
);
72 assert_se(get_process_ppid(pid
, &e
) >= 0);
73 log_info("PID"PID_FMT
" PPID: "PID_FMT
, pid
, e
);
74 assert_se(pid
== 1 ? e
== 0 : e
> 0);
76 assert_se(is_kernel_thread(pid
) == 0 || pid
!= 1);
78 r
= get_process_exe(pid
, &f
);
79 assert_se(r
>= 0 || r
== -EACCES
);
80 log_info("PID"PID_FMT
" exe: '%s'", pid
, strna(f
));
82 assert_se(get_process_uid(pid
, &u
) == 0);
83 log_info("PID"PID_FMT
" UID: "UID_FMT
, pid
, u
);
84 assert_se(u
== 0 || pid
!= 1);
86 assert_se(get_process_gid(pid
, &g
) == 0);
87 log_info("PID"PID_FMT
" GID: "GID_FMT
, pid
, g
);
88 assert_se(g
== 0 || pid
!= 1);
90 r
= get_process_environ(pid
, &env
);
91 assert_se(r
>= 0 || r
== -EACCES
);
92 log_info("PID"PID_FMT
" strlen(environ): %zi", pid
, env
? (ssize_t
)strlen(env
) : (ssize_t
)-errno
);
94 if (!detect_container())
95 assert_se(get_ctty_devnr(pid
, &h
) == -ENXIO
|| pid
!= 1);
97 getenv_for_pid(pid
, "PATH", &i
);
98 log_info("PID"PID_FMT
" $PATH: '%s'", pid
, strna(i
));
101 static void test_pid_is_unwaited(void) {
111 waitpid(pid
, &status
, 0);
112 assert_se(!pid_is_unwaited(pid
));
114 assert_se(pid_is_unwaited(getpid()));
115 assert_se(!pid_is_unwaited(-1));
118 static void test_pid_is_alive(void) {
128 waitpid(pid
, &status
, 0);
129 assert_se(!pid_is_alive(pid
));
131 assert_se(pid_is_alive(getpid()));
132 assert_se(!pid_is_alive(-1));
135 static void test_personality(void) {
137 assert_se(personality_to_string(PER_LINUX
));
138 assert_se(!personality_to_string(PERSONALITY_INVALID
));
140 assert_se(streq(personality_to_string(PER_LINUX
), architecture_to_string(native_architecture())));
142 assert_se(personality_from_string(personality_to_string(PER_LINUX
)) == PER_LINUX
);
143 assert_se(personality_from_string(architecture_to_string(native_architecture())) == PER_LINUX
);
146 assert_se(streq_ptr(personality_to_string(PER_LINUX
), "x86-64"));
147 assert_se(streq_ptr(personality_to_string(PER_LINUX32
), "x86"));
149 assert_se(personality_from_string("x86-64") == PER_LINUX
);
150 assert_se(personality_from_string("x86") == PER_LINUX32
);
151 assert_se(personality_from_string("ia64") == PERSONALITY_INVALID
);
152 assert_se(personality_from_string(NULL
) == PERSONALITY_INVALID
);
154 assert_se(personality_from_string(personality_to_string(PER_LINUX32
)) == PER_LINUX32
);
158 static void test_get_process_cmdline_harder(void) {
159 char path
[] = "/tmp/test-cmdlineXXXXXX";
160 _cleanup_close_
int fd
= -1;
161 _cleanup_free_
char *line
= NULL
;
171 (void) wait_for_terminate(pid
, &si
);
173 assert_se(si
.si_code
== CLD_EXITED
);
174 assert_se(si
.si_status
== 0);
180 assert_se(unshare(CLONE_NEWNS
) >= 0);
182 fd
= mkostemp(path
, O_CLOEXEC
);
184 assert_se(mount(path
, "/proc/self/cmdline", "bind", MS_BIND
, NULL
) >= 0);
185 assert_se(unlink(path
) >= 0);
187 assert_se(prctl(PR_SET_NAME
, "testa") >= 0);
189 assert_se(get_process_cmdline(getpid(), 0, false, &line
) == -ENOENT
);
191 assert_se(get_process_cmdline(getpid(), 0, true, &line
) >= 0);
192 assert_se(streq(line
, "[testa]"));
195 assert_se(get_process_cmdline(getpid(), 1, true, &line
) >= 0);
196 assert_se(streq(line
, ""));
199 assert_se(get_process_cmdline(getpid(), 2, true, &line
) >= 0);
200 assert_se(streq(line
, "["));
203 assert_se(get_process_cmdline(getpid(), 3, true, &line
) >= 0);
204 assert_se(streq(line
, "[."));
207 assert_se(get_process_cmdline(getpid(), 4, true, &line
) >= 0);
208 assert_se(streq(line
, "[.."));
211 assert_se(get_process_cmdline(getpid(), 5, true, &line
) >= 0);
212 assert_se(streq(line
, "[..."));
215 assert_se(get_process_cmdline(getpid(), 6, true, &line
) >= 0);
216 assert_se(streq(line
, "[...]"));
219 assert_se(get_process_cmdline(getpid(), 7, true, &line
) >= 0);
220 assert_se(streq(line
, "[t...]"));
223 assert_se(get_process_cmdline(getpid(), 8, true, &line
) >= 0);
224 assert_se(streq(line
, "[testa]"));
227 assert_se(write(fd
, "\0\0\0\0\0\0\0\0\0", 10) == 10);
229 assert_se(get_process_cmdline(getpid(), 0, false, &line
) == -ENOENT
);
231 assert_se(get_process_cmdline(getpid(), 0, true, &line
) >= 0);
232 assert_se(streq(line
, "[testa]"));
235 assert_se(write(fd
, "foo\0bar\0\0\0\0\0", 10) == 10);
237 assert_se(get_process_cmdline(getpid(), 0, false, &line
) >= 0);
238 assert_se(streq(line
, "foo bar"));
241 assert_se(get_process_cmdline(getpid(), 0, true, &line
) >= 0);
242 assert_se(streq(line
, "foo bar"));
245 assert_se(write(fd
, "quux", 4) == 4);
246 assert_se(get_process_cmdline(getpid(), 0, false, &line
) >= 0);
247 assert_se(streq(line
, "foo bar quux"));
250 assert_se(get_process_cmdline(getpid(), 0, true, &line
) >= 0);
251 assert_se(streq(line
, "foo bar quux"));
254 assert_se(get_process_cmdline(getpid(), 1, true, &line
) >= 0);
255 assert_se(streq(line
, ""));
258 assert_se(get_process_cmdline(getpid(), 2, true, &line
) >= 0);
259 assert_se(streq(line
, "."));
262 assert_se(get_process_cmdline(getpid(), 3, true, &line
) >= 0);
263 assert_se(streq(line
, ".."));
266 assert_se(get_process_cmdline(getpid(), 4, true, &line
) >= 0);
267 assert_se(streq(line
, "..."));
270 assert_se(get_process_cmdline(getpid(), 5, true, &line
) >= 0);
271 assert_se(streq(line
, "f..."));
274 assert_se(get_process_cmdline(getpid(), 6, true, &line
) >= 0);
275 assert_se(streq(line
, "fo..."));
278 assert_se(get_process_cmdline(getpid(), 7, true, &line
) >= 0);
279 assert_se(streq(line
, "foo..."));
282 assert_se(get_process_cmdline(getpid(), 8, true, &line
) >= 0);
283 assert_se(streq(line
, "foo..."));
286 assert_se(get_process_cmdline(getpid(), 9, true, &line
) >= 0);
287 assert_se(streq(line
, "foo b..."));
290 assert_se(get_process_cmdline(getpid(), 10, true, &line
) >= 0);
291 assert_se(streq(line
, "foo ba..."));
294 assert_se(get_process_cmdline(getpid(), 11, true, &line
) >= 0);
295 assert_se(streq(line
, "foo bar..."));
298 assert_se(get_process_cmdline(getpid(), 12, true, &line
) >= 0);
299 assert_se(streq(line
, "foo bar..."));
302 assert_se(get_process_cmdline(getpid(), 13, true, &line
) >= 0);
303 assert_se(streq(line
, "foo bar quux"));
306 assert_se(get_process_cmdline(getpid(), 14, true, &line
) >= 0);
307 assert_se(streq(line
, "foo bar quux"));
310 assert_se(get_process_cmdline(getpid(), 1000, true, &line
) >= 0);
311 assert_se(streq(line
, "foo bar quux"));
314 assert_se(ftruncate(fd
, 0) >= 0);
315 assert_se(prctl(PR_SET_NAME
, "aaaa bbbb cccc") >= 0);
317 assert_se(get_process_cmdline(getpid(), 0, false, &line
) == -ENOENT
);
319 assert_se(get_process_cmdline(getpid(), 0, true, &line
) >= 0);
320 assert_se(streq(line
, "[aaaa bbbb cccc]"));
323 assert_se(get_process_cmdline(getpid(), 10, true, &line
) >= 0);
324 assert_se(streq(line
, "[aaaa...]"));
327 assert_se(get_process_cmdline(getpid(), 11, true, &line
) >= 0);
328 assert_se(streq(line
, "[aaaa...]"));
331 assert_se(get_process_cmdline(getpid(), 12, true, &line
) >= 0);
332 assert_se(streq(line
, "[aaaa b...]"));
339 int main(int argc
, char *argv
[]) {
340 log_parse_environment();
346 (void) parse_pid(argv
[1], &pid
);
347 test_get_process_comm(pid
);
349 test_get_process_comm(1);
350 test_get_process_comm(getpid());
353 test_pid_is_unwaited();
356 test_get_process_cmdline_harder();