]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-process-util.c
Merge pull request #3828 from keszybz/drop-systemd-vconsole-setup-service
[thirdparty/systemd.git] / src / test / test-process-util.c
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
21 #include <sched.h>
22 #include <sys/mount.h>
23 #include <sys/personality.h>
24 #include <sys/prctl.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <sys/wait.h>
28 #include <unistd.h>
29 #ifdef HAVE_VALGRIND_VALGRIND_H
30 #include <valgrind/valgrind.h>
31 #endif
32
33 #include "alloc-util.h"
34 #include "architecture.h"
35 #include "fd-util.h"
36 #include "log.h"
37 #include "macro.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"
44 #include "util.h"
45 #include "virt.h"
46
47 static void test_get_process_comm(pid_t pid) {
48 struct stat st;
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)];
52 pid_t e;
53 uid_t u;
54 gid_t g;
55 dev_t h;
56 int r;
57
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);
63 } else
64 log_warning("%s not exist.", path);
65
66 assert_se(get_process_cmdline(pid, 0, true, &c) >= 0);
67 log_info("PID"PID_FMT" cmdline: '%s'", pid, c);
68
69 assert_se(get_process_cmdline(pid, 8, false, &d) >= 0);
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);
75
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);
79
80 assert_se(is_kernel_thread(pid) == 0 || pid != 1);
81
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));
85
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);
89
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);
93
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);
97
98 if (!detect_container())
99 assert_se(get_ctty_devnr(pid, &h) == -ENXIO || pid != 1);
100
101 getenv_for_pid(pid, "PATH", &i);
102 log_info("PID"PID_FMT" $PATH: '%s'", pid, strna(i));
103 }
104
105 static 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 }
118 assert_se(pid_is_unwaited(getpid()));
119 assert_se(!pid_is_unwaited(-1));
120 }
121
122 static 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 }
135 assert_se(pid_is_alive(getpid()));
136 assert_se(!pid_is_alive(-1));
137 }
138
139 static 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
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;
166 pid_t pid;
167
168 if (geteuid() != 0)
169 return;
170
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)
176 return;
177 #endif
178
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);
196 assert_se(mount(path, "/proc/self/cmdline", "bind", MS_BIND, NULL) >= 0);
197 assert_se(unlink(path) >= 0);
198
199 assert_se(prctl(PR_SET_NAME, "testa") >= 0);
200
201 assert_se(get_process_cmdline(getpid(), 0, false, &line) == -ENOENT);
202
203 assert_se(get_process_cmdline(getpid(), 0, true, &line) >= 0);
204 assert_se(streq(line, "[testa]"));
205 line = mfree(line);
206
207 assert_se(get_process_cmdline(getpid(), 1, true, &line) >= 0);
208 assert_se(streq(line, ""));
209 line = mfree(line);
210
211 assert_se(get_process_cmdline(getpid(), 2, true, &line) >= 0);
212 assert_se(streq(line, "["));
213 line = mfree(line);
214
215 assert_se(get_process_cmdline(getpid(), 3, true, &line) >= 0);
216 assert_se(streq(line, "[."));
217 line = mfree(line);
218
219 assert_se(get_process_cmdline(getpid(), 4, true, &line) >= 0);
220 assert_se(streq(line, "[.."));
221 line = mfree(line);
222
223 assert_se(get_process_cmdline(getpid(), 5, true, &line) >= 0);
224 assert_se(streq(line, "[..."));
225 line = mfree(line);
226
227 assert_se(get_process_cmdline(getpid(), 6, true, &line) >= 0);
228 assert_se(streq(line, "[...]"));
229 line = mfree(line);
230
231 assert_se(get_process_cmdline(getpid(), 7, true, &line) >= 0);
232 assert_se(streq(line, "[t...]"));
233 line = mfree(line);
234
235 assert_se(get_process_cmdline(getpid(), 8, true, &line) >= 0);
236 assert_se(streq(line, "[testa]"));
237 line = mfree(line);
238
239 assert_se(write(fd, "\0\0\0\0\0\0\0\0\0", 10) == 10);
240
241 assert_se(get_process_cmdline(getpid(), 0, false, &line) == -ENOENT);
242
243 assert_se(get_process_cmdline(getpid(), 0, true, &line) >= 0);
244 assert_se(streq(line, "[testa]"));
245 line = mfree(line);
246
247 assert_se(write(fd, "foo\0bar\0\0\0\0\0", 10) == 10);
248
249 assert_se(get_process_cmdline(getpid(), 0, false, &line) >= 0);
250 assert_se(streq(line, "foo bar"));
251 line = mfree(line);
252
253 assert_se(get_process_cmdline(getpid(), 0, true, &line) >= 0);
254 assert_se(streq(line, "foo bar"));
255 line = mfree(line);
256
257 assert_se(write(fd, "quux", 4) == 4);
258 assert_se(get_process_cmdline(getpid(), 0, false, &line) >= 0);
259 assert_se(streq(line, "foo bar quux"));
260 line = mfree(line);
261
262 assert_se(get_process_cmdline(getpid(), 0, true, &line) >= 0);
263 assert_se(streq(line, "foo bar quux"));
264 line = mfree(line);
265
266 assert_se(get_process_cmdline(getpid(), 1, true, &line) >= 0);
267 assert_se(streq(line, ""));
268 line = mfree(line);
269
270 assert_se(get_process_cmdline(getpid(), 2, true, &line) >= 0);
271 assert_se(streq(line, "."));
272 line = mfree(line);
273
274 assert_se(get_process_cmdline(getpid(), 3, true, &line) >= 0);
275 assert_se(streq(line, ".."));
276 line = mfree(line);
277
278 assert_se(get_process_cmdline(getpid(), 4, true, &line) >= 0);
279 assert_se(streq(line, "..."));
280 line = mfree(line);
281
282 assert_se(get_process_cmdline(getpid(), 5, true, &line) >= 0);
283 assert_se(streq(line, "f..."));
284 line = mfree(line);
285
286 assert_se(get_process_cmdline(getpid(), 6, true, &line) >= 0);
287 assert_se(streq(line, "fo..."));
288 line = mfree(line);
289
290 assert_se(get_process_cmdline(getpid(), 7, true, &line) >= 0);
291 assert_se(streq(line, "foo..."));
292 line = mfree(line);
293
294 assert_se(get_process_cmdline(getpid(), 8, true, &line) >= 0);
295 assert_se(streq(line, "foo..."));
296 line = mfree(line);
297
298 assert_se(get_process_cmdline(getpid(), 9, true, &line) >= 0);
299 assert_se(streq(line, "foo b..."));
300 line = mfree(line);
301
302 assert_se(get_process_cmdline(getpid(), 10, true, &line) >= 0);
303 assert_se(streq(line, "foo ba..."));
304 line = mfree(line);
305
306 assert_se(get_process_cmdline(getpid(), 11, true, &line) >= 0);
307 assert_se(streq(line, "foo bar..."));
308 line = mfree(line);
309
310 assert_se(get_process_cmdline(getpid(), 12, true, &line) >= 0);
311 assert_se(streq(line, "foo bar..."));
312 line = mfree(line);
313
314 assert_se(get_process_cmdline(getpid(), 13, true, &line) >= 0);
315 assert_se(streq(line, "foo bar quux"));
316 line = mfree(line);
317
318 assert_se(get_process_cmdline(getpid(), 14, true, &line) >= 0);
319 assert_se(streq(line, "foo bar quux"));
320 line = mfree(line);
321
322 assert_se(get_process_cmdline(getpid(), 1000, true, &line) >= 0);
323 assert_se(streq(line, "foo bar quux"));
324 line = mfree(line);
325
326 assert_se(ftruncate(fd, 0) >= 0);
327 assert_se(prctl(PR_SET_NAME, "aaaa bbbb cccc") >= 0);
328
329 assert_se(get_process_cmdline(getpid(), 0, false, &line) == -ENOENT);
330
331 assert_se(get_process_cmdline(getpid(), 0, true, &line) >= 0);
332 assert_se(streq(line, "[aaaa bbbb cccc]"));
333 line = mfree(line);
334
335 assert_se(get_process_cmdline(getpid(), 10, true, &line) >= 0);
336 assert_se(streq(line, "[aaaa...]"));
337 line = mfree(line);
338
339 assert_se(get_process_cmdline(getpid(), 11, true, &line) >= 0);
340 assert_se(streq(line, "[aaaa...]"));
341 line = mfree(line);
342
343 assert_se(get_process_cmdline(getpid(), 12, true, &line) >= 0);
344 assert_se(streq(line, "[aaaa b...]"));
345 line = mfree(line);
346
347 safe_close(fd);
348 _exit(0);
349 }
350
351 int main(int argc, char *argv[]) {
352 log_parse_environment();
353 log_open();
354
355 if (argc > 1) {
356 pid_t pid = 0;
357
358 (void) parse_pid(argv[1], &pid);
359 test_get_process_comm(pid);
360 } else {
361 TEST_REQ_RUNNING_SYSTEMD(test_get_process_comm(1));
362 test_get_process_comm(getpid());
363 }
364
365 test_pid_is_unwaited();
366 test_pid_is_alive();
367 test_personality();
368 test_get_process_cmdline_harder();
369
370 return 0;
371 }