]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ | |
2 | ||
3 | #include <sched.h> | |
4 | #include <sys/mount.h> | |
5 | #include <sys/personality.h> | |
6 | #include <sys/prctl.h> | |
7 | #include <sys/stat.h> | |
8 | #include <sys/types.h> | |
9 | #include <sys/wait.h> | |
10 | #include <unistd.h> | |
11 | #if HAVE_VALGRIND_VALGRIND_H | |
12 | #include <valgrind/valgrind.h> | |
13 | #endif | |
14 | ||
15 | #include "alloc-util.h" | |
16 | #include "architecture.h" | |
17 | #include "fd-util.h" | |
18 | #include "log.h" | |
19 | #include "macro.h" | |
20 | #include "missing.h" | |
21 | #include "parse-util.h" | |
22 | #include "process-util.h" | |
23 | #include "signal-util.h" | |
24 | #include "stdio-util.h" | |
25 | #include "string-util.h" | |
26 | #include "terminal-util.h" | |
27 | #include "test-helper.h" | |
28 | #include "tests.h" | |
29 | #include "util.h" | |
30 | #include "virt.h" | |
31 | ||
32 | static void test_get_process_comm(pid_t pid) { | |
33 | struct stat st; | |
34 | _cleanup_free_ char *a = NULL, *c = NULL, *d = NULL, *f = NULL, *i = NULL; | |
35 | _cleanup_free_ char *env = NULL; | |
36 | char path[STRLEN("/proc//comm") + DECIMAL_STR_MAX(pid_t)]; | |
37 | pid_t e; | |
38 | uid_t u; | |
39 | gid_t g; | |
40 | dev_t h; | |
41 | int r; | |
42 | ||
43 | xsprintf(path, "/proc/"PID_FMT"/comm", pid); | |
44 | ||
45 | if (stat(path, &st) == 0) { | |
46 | assert_se(get_process_comm(pid, &a) >= 0); | |
47 | log_info("PID"PID_FMT" comm: '%s'", pid, a); | |
48 | } else | |
49 | log_warning("%s not exist.", path); | |
50 | ||
51 | assert_se(get_process_cmdline(pid, 0, true, &c) >= 0); | |
52 | log_info("PID"PID_FMT" cmdline: '%s'", pid, c); | |
53 | ||
54 | assert_se(get_process_cmdline(pid, 8, false, &d) >= 0); | |
55 | log_info("PID"PID_FMT" cmdline truncated to 8: '%s'", pid, d); | |
56 | ||
57 | free(d); | |
58 | assert_se(get_process_cmdline(pid, 1, false, &d) >= 0); | |
59 | log_info("PID"PID_FMT" cmdline truncated to 1: '%s'", pid, d); | |
60 | ||
61 | assert_se(get_process_ppid(pid, &e) >= 0); | |
62 | log_info("PID"PID_FMT" PPID: "PID_FMT, pid, e); | |
63 | assert_se(pid == 1 ? e == 0 : e > 0); | |
64 | ||
65 | assert_se(is_kernel_thread(pid) == 0 || pid != 1); | |
66 | ||
67 | r = get_process_exe(pid, &f); | |
68 | assert_se(r >= 0 || r == -EACCES); | |
69 | log_info("PID"PID_FMT" exe: '%s'", pid, strna(f)); | |
70 | ||
71 | assert_se(get_process_uid(pid, &u) == 0); | |
72 | log_info("PID"PID_FMT" UID: "UID_FMT, pid, u); | |
73 | ||
74 | assert_se(get_process_gid(pid, &g) == 0); | |
75 | log_info("PID"PID_FMT" GID: "GID_FMT, pid, g); | |
76 | ||
77 | r = get_process_environ(pid, &env); | |
78 | assert_se(r >= 0 || r == -EACCES); | |
79 | log_info("PID"PID_FMT" strlen(environ): %zi", pid, env ? (ssize_t)strlen(env) : (ssize_t)-errno); | |
80 | ||
81 | if (!detect_container()) | |
82 | assert_se(get_ctty_devnr(pid, &h) == -ENXIO || pid != 1); | |
83 | ||
84 | (void) getenv_for_pid(pid, "PATH", &i); | |
85 | log_info("PID"PID_FMT" $PATH: '%s'", pid, strna(i)); | |
86 | } | |
87 | ||
88 | static void test_get_process_comm_escape_one(const char *input, const char *output) { | |
89 | _cleanup_free_ char *n = NULL; | |
90 | ||
91 | log_info("input: <%s> — output: <%s>", input, output); | |
92 | ||
93 | assert_se(prctl(PR_SET_NAME, input) >= 0); | |
94 | assert_se(get_process_comm(0, &n) >= 0); | |
95 | ||
96 | log_info("got: <%s>", n); | |
97 | ||
98 | assert_se(streq_ptr(n, output)); | |
99 | } | |
100 | ||
101 | static void test_get_process_comm_escape(void) { | |
102 | _cleanup_free_ char *saved = NULL; | |
103 | ||
104 | assert_se(get_process_comm(0, &saved) >= 0); | |
105 | ||
106 | test_get_process_comm_escape_one("", ""); | |
107 | test_get_process_comm_escape_one("foo", "foo"); | |
108 | test_get_process_comm_escape_one("012345678901234", "012345678901234"); | |
109 | test_get_process_comm_escape_one("0123456789012345", "012345678901234"); | |
110 | test_get_process_comm_escape_one("äöüß", "\\303\\244\\303…"); | |
111 | test_get_process_comm_escape_one("xäöüß", "x\\303\\244…"); | |
112 | test_get_process_comm_escape_one("xxäöüß", "xx\\303\\244…"); | |
113 | test_get_process_comm_escape_one("xxxäöüß", "xxx\\303\\244…"); | |
114 | test_get_process_comm_escape_one("xxxxäöüß", "xxxx\\303\\244…"); | |
115 | test_get_process_comm_escape_one("xxxxxäöüß", "xxxxx\\303…"); | |
116 | ||
117 | assert_se(prctl(PR_SET_NAME, saved) >= 0); | |
118 | } | |
119 | ||
120 | static void test_pid_is_unwaited(void) { | |
121 | pid_t pid; | |
122 | ||
123 | pid = fork(); | |
124 | assert_se(pid >= 0); | |
125 | if (pid == 0) { | |
126 | _exit(EXIT_SUCCESS); | |
127 | } else { | |
128 | int status; | |
129 | ||
130 | waitpid(pid, &status, 0); | |
131 | assert_se(!pid_is_unwaited(pid)); | |
132 | } | |
133 | assert_se(pid_is_unwaited(getpid_cached())); | |
134 | assert_se(!pid_is_unwaited(-1)); | |
135 | } | |
136 | ||
137 | static void test_pid_is_alive(void) { | |
138 | pid_t pid; | |
139 | ||
140 | pid = fork(); | |
141 | assert_se(pid >= 0); | |
142 | if (pid == 0) { | |
143 | _exit(EXIT_SUCCESS); | |
144 | } else { | |
145 | int status; | |
146 | ||
147 | waitpid(pid, &status, 0); | |
148 | assert_se(!pid_is_alive(pid)); | |
149 | } | |
150 | assert_se(pid_is_alive(getpid_cached())); | |
151 | assert_se(!pid_is_alive(-1)); | |
152 | } | |
153 | ||
154 | static void test_personality(void) { | |
155 | ||
156 | assert_se(personality_to_string(PER_LINUX)); | |
157 | assert_se(!personality_to_string(PERSONALITY_INVALID)); | |
158 | ||
159 | assert_se(streq(personality_to_string(PER_LINUX), architecture_to_string(native_architecture()))); | |
160 | ||
161 | assert_se(personality_from_string(personality_to_string(PER_LINUX)) == PER_LINUX); | |
162 | assert_se(personality_from_string(architecture_to_string(native_architecture())) == PER_LINUX); | |
163 | ||
164 | #ifdef __x86_64__ | |
165 | assert_se(streq_ptr(personality_to_string(PER_LINUX), "x86-64")); | |
166 | assert_se(streq_ptr(personality_to_string(PER_LINUX32), "x86")); | |
167 | ||
168 | assert_se(personality_from_string("x86-64") == PER_LINUX); | |
169 | assert_se(personality_from_string("x86") == PER_LINUX32); | |
170 | assert_se(personality_from_string("ia64") == PERSONALITY_INVALID); | |
171 | assert_se(personality_from_string(NULL) == PERSONALITY_INVALID); | |
172 | ||
173 | assert_se(personality_from_string(personality_to_string(PER_LINUX32)) == PER_LINUX32); | |
174 | #endif | |
175 | } | |
176 | ||
177 | static void test_get_process_cmdline_harder(void) { | |
178 | char path[] = "/tmp/test-cmdlineXXXXXX"; | |
179 | _cleanup_close_ int fd = -1; | |
180 | _cleanup_free_ char *line = NULL; | |
181 | pid_t pid; | |
182 | ||
183 | if (geteuid() != 0) { | |
184 | log_info("Skipping %s: not root", __func__); | |
185 | return; | |
186 | } | |
187 | ||
188 | if (!have_namespaces()) { | |
189 | log_notice("Testing without namespaces, skipping %s", __func__); | |
190 | return; | |
191 | } | |
192 | ||
193 | #if HAVE_VALGRIND_VALGRIND_H | |
194 | /* valgrind patches open(/proc//cmdline) | |
195 | * so, test_get_process_cmdline_harder fails always | |
196 | * See https://github.com/systemd/systemd/pull/3555#issuecomment-226564908 */ | |
197 | if (RUNNING_ON_VALGRIND) { | |
198 | log_info("Skipping %s: running on valgrind", __func__); | |
199 | return; | |
200 | } | |
201 | #endif | |
202 | ||
203 | pid = fork(); | |
204 | if (pid > 0) { | |
205 | siginfo_t si; | |
206 | ||
207 | (void) wait_for_terminate(pid, &si); | |
208 | ||
209 | assert_se(si.si_code == CLD_EXITED); | |
210 | assert_se(si.si_status == 0); | |
211 | ||
212 | return; | |
213 | } | |
214 | ||
215 | assert_se(pid == 0); | |
216 | assert_se(unshare(CLONE_NEWNS) >= 0); | |
217 | ||
218 | if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL) < 0) { | |
219 | log_warning_errno(errno, "mount(..., \"/\", MS_SLAVE|MS_REC, ...) failed: %m"); | |
220 | assert_se(IN_SET(errno, EPERM, EACCES)); | |
221 | return; | |
222 | } | |
223 | ||
224 | fd = mkostemp(path, O_CLOEXEC); | |
225 | assert_se(fd >= 0); | |
226 | ||
227 | /* Note that we don't unmount the following bind-mount at the end of the test because the kernel | |
228 | * will clear up its /proc/PID/ hierarchy automatically as soon as the test stops. */ | |
229 | if (mount(path, "/proc/self/cmdline", "bind", MS_BIND, NULL) < 0) { | |
230 | /* This happens under selinux… Abort the test in this case. */ | |
231 | log_warning_errno(errno, "mount(..., \"/proc/self/cmdline\", \"bind\", ...) failed: %m"); | |
232 | assert_se(IN_SET(errno, EPERM, EACCES)); | |
233 | return; | |
234 | } | |
235 | ||
236 | assert_se(unlink(path) >= 0); | |
237 | ||
238 | assert_se(prctl(PR_SET_NAME, "testa") >= 0); | |
239 | ||
240 | assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) == -ENOENT); | |
241 | ||
242 | assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0); | |
243 | assert_se(streq(line, "[testa]")); | |
244 | line = mfree(line); | |
245 | ||
246 | assert_se(get_process_cmdline(getpid_cached(), 1, true, &line) >= 0); | |
247 | assert_se(streq(line, "")); | |
248 | line = mfree(line); | |
249 | ||
250 | assert_se(get_process_cmdline(getpid_cached(), 2, true, &line) >= 0); | |
251 | assert_se(streq(line, "[")); | |
252 | line = mfree(line); | |
253 | ||
254 | assert_se(get_process_cmdline(getpid_cached(), 3, true, &line) >= 0); | |
255 | assert_se(streq(line, "[.")); | |
256 | line = mfree(line); | |
257 | ||
258 | assert_se(get_process_cmdline(getpid_cached(), 4, true, &line) >= 0); | |
259 | assert_se(streq(line, "[..")); | |
260 | line = mfree(line); | |
261 | ||
262 | assert_se(get_process_cmdline(getpid_cached(), 5, true, &line) >= 0); | |
263 | assert_se(streq(line, "[...")); | |
264 | line = mfree(line); | |
265 | ||
266 | assert_se(get_process_cmdline(getpid_cached(), 6, true, &line) >= 0); | |
267 | assert_se(streq(line, "[...]")); | |
268 | line = mfree(line); | |
269 | ||
270 | assert_se(get_process_cmdline(getpid_cached(), 7, true, &line) >= 0); | |
271 | assert_se(streq(line, "[t...]")); | |
272 | line = mfree(line); | |
273 | ||
274 | assert_se(get_process_cmdline(getpid_cached(), 8, true, &line) >= 0); | |
275 | assert_se(streq(line, "[testa]")); | |
276 | line = mfree(line); | |
277 | ||
278 | assert_se(write(fd, "\0\0\0\0\0\0\0\0\0", 10) == 10); | |
279 | ||
280 | assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) == -ENOENT); | |
281 | ||
282 | assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0); | |
283 | assert_se(streq(line, "[testa]")); | |
284 | line = mfree(line); | |
285 | ||
286 | assert_se(write(fd, "foo\0bar\0\0\0\0\0", 10) == 10); | |
287 | ||
288 | assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) >= 0); | |
289 | assert_se(streq(line, "foo bar")); | |
290 | line = mfree(line); | |
291 | ||
292 | assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0); | |
293 | assert_se(streq(line, "foo bar")); | |
294 | line = mfree(line); | |
295 | ||
296 | assert_se(write(fd, "quux", 4) == 4); | |
297 | assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) >= 0); | |
298 | assert_se(streq(line, "foo bar quux")); | |
299 | line = mfree(line); | |
300 | ||
301 | assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0); | |
302 | assert_se(streq(line, "foo bar quux")); | |
303 | line = mfree(line); | |
304 | ||
305 | assert_se(get_process_cmdline(getpid_cached(), 1, true, &line) >= 0); | |
306 | assert_se(streq(line, "")); | |
307 | line = mfree(line); | |
308 | ||
309 | assert_se(get_process_cmdline(getpid_cached(), 2, true, &line) >= 0); | |
310 | assert_se(streq(line, ".")); | |
311 | line = mfree(line); | |
312 | ||
313 | assert_se(get_process_cmdline(getpid_cached(), 3, true, &line) >= 0); | |
314 | assert_se(streq(line, "..")); | |
315 | line = mfree(line); | |
316 | ||
317 | assert_se(get_process_cmdline(getpid_cached(), 4, true, &line) >= 0); | |
318 | assert_se(streq(line, "...")); | |
319 | line = mfree(line); | |
320 | ||
321 | assert_se(get_process_cmdline(getpid_cached(), 5, true, &line) >= 0); | |
322 | assert_se(streq(line, "f...")); | |
323 | line = mfree(line); | |
324 | ||
325 | assert_se(get_process_cmdline(getpid_cached(), 6, true, &line) >= 0); | |
326 | assert_se(streq(line, "fo...")); | |
327 | line = mfree(line); | |
328 | ||
329 | assert_se(get_process_cmdline(getpid_cached(), 7, true, &line) >= 0); | |
330 | assert_se(streq(line, "foo...")); | |
331 | line = mfree(line); | |
332 | ||
333 | assert_se(get_process_cmdline(getpid_cached(), 8, true, &line) >= 0); | |
334 | assert_se(streq(line, "foo...")); | |
335 | line = mfree(line); | |
336 | ||
337 | assert_se(get_process_cmdline(getpid_cached(), 9, true, &line) >= 0); | |
338 | assert_se(streq(line, "foo b...")); | |
339 | line = mfree(line); | |
340 | ||
341 | assert_se(get_process_cmdline(getpid_cached(), 10, true, &line) >= 0); | |
342 | assert_se(streq(line, "foo ba...")); | |
343 | line = mfree(line); | |
344 | ||
345 | assert_se(get_process_cmdline(getpid_cached(), 11, true, &line) >= 0); | |
346 | assert_se(streq(line, "foo bar...")); | |
347 | line = mfree(line); | |
348 | ||
349 | assert_se(get_process_cmdline(getpid_cached(), 12, true, &line) >= 0); | |
350 | assert_se(streq(line, "foo bar...")); | |
351 | line = mfree(line); | |
352 | ||
353 | assert_se(get_process_cmdline(getpid_cached(), 13, true, &line) >= 0); | |
354 | assert_se(streq(line, "foo bar quux")); | |
355 | line = mfree(line); | |
356 | ||
357 | assert_se(get_process_cmdline(getpid_cached(), 14, true, &line) >= 0); | |
358 | assert_se(streq(line, "foo bar quux")); | |
359 | line = mfree(line); | |
360 | ||
361 | assert_se(get_process_cmdline(getpid_cached(), 1000, true, &line) >= 0); | |
362 | assert_se(streq(line, "foo bar quux")); | |
363 | line = mfree(line); | |
364 | ||
365 | assert_se(ftruncate(fd, 0) >= 0); | |
366 | assert_se(prctl(PR_SET_NAME, "aaaa bbbb cccc") >= 0); | |
367 | ||
368 | assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) == -ENOENT); | |
369 | ||
370 | assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0); | |
371 | assert_se(streq(line, "[aaaa bbbb cccc]")); | |
372 | line = mfree(line); | |
373 | ||
374 | assert_se(get_process_cmdline(getpid_cached(), 10, true, &line) >= 0); | |
375 | assert_se(streq(line, "[aaaa...]")); | |
376 | line = mfree(line); | |
377 | ||
378 | assert_se(get_process_cmdline(getpid_cached(), 11, true, &line) >= 0); | |
379 | assert_se(streq(line, "[aaaa...]")); | |
380 | line = mfree(line); | |
381 | ||
382 | assert_se(get_process_cmdline(getpid_cached(), 12, true, &line) >= 0); | |
383 | assert_se(streq(line, "[aaaa b...]")); | |
384 | line = mfree(line); | |
385 | ||
386 | safe_close(fd); | |
387 | _exit(EXIT_SUCCESS); | |
388 | } | |
389 | ||
390 | static void test_rename_process_now(const char *p, int ret) { | |
391 | _cleanup_free_ char *comm = NULL, *cmdline = NULL; | |
392 | int r; | |
393 | ||
394 | r = rename_process(p); | |
395 | assert_se(r == ret || | |
396 | (ret == 0 && r >= 0) || | |
397 | (ret > 0 && r > 0)); | |
398 | ||
399 | if (r < 0) | |
400 | return; | |
401 | ||
402 | #if HAVE_VALGRIND_VALGRIND_H | |
403 | /* see above, valgrind is weird, we can't verify what we are doing here */ | |
404 | if (RUNNING_ON_VALGRIND) | |
405 | return; | |
406 | #endif | |
407 | ||
408 | assert_se(get_process_comm(0, &comm) >= 0); | |
409 | log_info("comm = <%s>", comm); | |
410 | assert_se(strneq(comm, p, TASK_COMM_LEN-1)); | |
411 | ||
412 | r = get_process_cmdline(0, 0, false, &cmdline); | |
413 | assert_se(r >= 0); | |
414 | /* we cannot expect cmdline to be renamed properly without privileges */ | |
415 | if (geteuid() == 0) { | |
416 | if (r == 0 && detect_container() > 0) | |
417 | log_info("cmdline = <%s> (not verified, Running in unprivileged container?)", cmdline); | |
418 | else { | |
419 | log_info("cmdline = <%s>", cmdline); | |
420 | assert_se(strneq(p, cmdline, STRLEN("test-process-util"))); | |
421 | assert_se(startswith(p, cmdline)); | |
422 | } | |
423 | } else | |
424 | log_info("cmdline = <%s> (not verified)", cmdline); | |
425 | } | |
426 | ||
427 | static void test_rename_process_one(const char *p, int ret) { | |
428 | siginfo_t si; | |
429 | pid_t pid; | |
430 | ||
431 | pid = fork(); | |
432 | assert_se(pid >= 0); | |
433 | ||
434 | if (pid == 0) { | |
435 | /* child */ | |
436 | test_rename_process_now(p, ret); | |
437 | _exit(EXIT_SUCCESS); | |
438 | } | |
439 | ||
440 | assert_se(wait_for_terminate(pid, &si) >= 0); | |
441 | assert_se(si.si_code == CLD_EXITED); | |
442 | assert_se(si.si_status == EXIT_SUCCESS); | |
443 | } | |
444 | ||
445 | static void test_rename_process_multi(void) { | |
446 | pid_t pid; | |
447 | ||
448 | pid = fork(); | |
449 | assert_se(pid >= 0); | |
450 | ||
451 | if (pid > 0) { | |
452 | siginfo_t si; | |
453 | ||
454 | assert_se(wait_for_terminate(pid, &si) >= 0); | |
455 | assert_se(si.si_code == CLD_EXITED); | |
456 | assert_se(si.si_status == EXIT_SUCCESS); | |
457 | ||
458 | return; | |
459 | } | |
460 | ||
461 | /* child */ | |
462 | test_rename_process_now("one", 1); | |
463 | test_rename_process_now("more", 0); /* longer than "one", hence truncated */ | |
464 | (void) setresuid(99, 99, 99); /* change uid when running privileged */ | |
465 | test_rename_process_now("time!", 0); | |
466 | test_rename_process_now("0", 1); /* shorter than "one", should fit */ | |
467 | test_rename_process_one("", -EINVAL); | |
468 | test_rename_process_one(NULL, -EINVAL); | |
469 | _exit(EXIT_SUCCESS); | |
470 | } | |
471 | ||
472 | static void test_rename_process(void) { | |
473 | test_rename_process_one(NULL, -EINVAL); | |
474 | test_rename_process_one("", -EINVAL); | |
475 | test_rename_process_one("foo", 1); /* should always fit */ | |
476 | test_rename_process_one("this is a really really long process name, followed by some more words", 0); /* unlikely to fit */ | |
477 | test_rename_process_one("1234567", 1); /* should always fit */ | |
478 | test_rename_process_multi(); /* multiple invocations and dropped privileges */ | |
479 | } | |
480 | ||
481 | static void test_getpid_cached(void) { | |
482 | siginfo_t si; | |
483 | pid_t a, b, c, d, e, f, child; | |
484 | ||
485 | a = raw_getpid(); | |
486 | b = getpid_cached(); | |
487 | c = getpid(); | |
488 | ||
489 | assert_se(a == b && a == c); | |
490 | ||
491 | child = fork(); | |
492 | assert_se(child >= 0); | |
493 | ||
494 | if (child == 0) { | |
495 | /* In child */ | |
496 | a = raw_getpid(); | |
497 | b = getpid_cached(); | |
498 | c = getpid(); | |
499 | ||
500 | assert_se(a == b && a == c); | |
501 | _exit(EXIT_SUCCESS); | |
502 | } | |
503 | ||
504 | d = raw_getpid(); | |
505 | e = getpid_cached(); | |
506 | f = getpid(); | |
507 | ||
508 | assert_se(a == d && a == e && a == f); | |
509 | ||
510 | assert_se(wait_for_terminate(child, &si) >= 0); | |
511 | assert_se(si.si_status == 0); | |
512 | assert_se(si.si_code == CLD_EXITED); | |
513 | } | |
514 | ||
515 | #define MEASURE_ITERATIONS (10000000LLU) | |
516 | ||
517 | static void test_getpid_measure(void) { | |
518 | unsigned long long i; | |
519 | usec_t t, q; | |
520 | ||
521 | t = now(CLOCK_MONOTONIC); | |
522 | for (i = 0; i < MEASURE_ITERATIONS; i++) | |
523 | (void) getpid(); | |
524 | q = now(CLOCK_MONOTONIC) - t; | |
525 | ||
526 | log_info(" glibc getpid(): %llu/s\n", (unsigned long long) (MEASURE_ITERATIONS*USEC_PER_SEC/q)); | |
527 | ||
528 | t = now(CLOCK_MONOTONIC); | |
529 | for (i = 0; i < MEASURE_ITERATIONS; i++) | |
530 | (void) getpid_cached(); | |
531 | q = now(CLOCK_MONOTONIC) - t; | |
532 | ||
533 | log_info("getpid_cached(): %llu/s\n", (unsigned long long) (MEASURE_ITERATIONS*USEC_PER_SEC/q)); | |
534 | } | |
535 | ||
536 | static void test_safe_fork(void) { | |
537 | siginfo_t status; | |
538 | pid_t pid; | |
539 | int r; | |
540 | ||
541 | BLOCK_SIGNALS(SIGCHLD); | |
542 | ||
543 | r = safe_fork("(test-child)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_NULL_STDIO|FORK_REOPEN_LOG, &pid); | |
544 | assert_se(r >= 0); | |
545 | ||
546 | if (r == 0) { | |
547 | /* child */ | |
548 | usleep(100 * USEC_PER_MSEC); | |
549 | ||
550 | _exit(88); | |
551 | } | |
552 | ||
553 | assert_se(wait_for_terminate(pid, &status) >= 0); | |
554 | assert_se(status.si_code == CLD_EXITED); | |
555 | assert_se(status.si_status == 88); | |
556 | } | |
557 | ||
558 | static void test_pid_to_ptr(void) { | |
559 | ||
560 | assert_se(PTR_TO_PID(NULL) == 0); | |
561 | assert_se(PID_TO_PTR(0) == NULL); | |
562 | ||
563 | assert_se(PTR_TO_PID(PID_TO_PTR(1)) == 1); | |
564 | assert_se(PTR_TO_PID(PID_TO_PTR(2)) == 2); | |
565 | assert_se(PTR_TO_PID(PID_TO_PTR(-1)) == -1); | |
566 | assert_se(PTR_TO_PID(PID_TO_PTR(-2)) == -2); | |
567 | ||
568 | assert_se(PTR_TO_PID(PID_TO_PTR(INT16_MAX)) == INT16_MAX); | |
569 | assert_se(PTR_TO_PID(PID_TO_PTR(INT16_MIN)) == INT16_MIN); | |
570 | ||
571 | #if SIZEOF_PID_T >= 4 | |
572 | assert_se(PTR_TO_PID(PID_TO_PTR(INT32_MAX)) == INT32_MAX); | |
573 | assert_se(PTR_TO_PID(PID_TO_PTR(INT32_MIN)) == INT32_MIN); | |
574 | #endif | |
575 | } | |
576 | ||
577 | static void test_ioprio_class_from_to_string_one(const char *val, int expected) { | |
578 | assert_se(ioprio_class_from_string(val) == expected); | |
579 | if (expected >= 0) { | |
580 | _cleanup_free_ char *s = NULL; | |
581 | unsigned ret; | |
582 | ||
583 | assert_se(ioprio_class_to_string_alloc(expected, &s) == 0); | |
584 | /* We sometimes get a class number and sometimes a number back */ | |
585 | assert_se(streq(s, val) || | |
586 | safe_atou(val, &ret) == 0); | |
587 | } | |
588 | } | |
589 | ||
590 | static void test_ioprio_class_from_to_string(void) { | |
591 | test_ioprio_class_from_to_string_one("none", IOPRIO_CLASS_NONE); | |
592 | test_ioprio_class_from_to_string_one("realtime", IOPRIO_CLASS_RT); | |
593 | test_ioprio_class_from_to_string_one("best-effort", IOPRIO_CLASS_BE); | |
594 | test_ioprio_class_from_to_string_one("idle", IOPRIO_CLASS_IDLE); | |
595 | test_ioprio_class_from_to_string_one("0", 0); | |
596 | test_ioprio_class_from_to_string_one("1", 1); | |
597 | test_ioprio_class_from_to_string_one("7", 7); | |
598 | test_ioprio_class_from_to_string_one("8", 8); | |
599 | test_ioprio_class_from_to_string_one("9", -1); | |
600 | test_ioprio_class_from_to_string_one("-1", -1); | |
601 | } | |
602 | ||
603 | int main(int argc, char *argv[]) { | |
604 | test_setup_logging(LOG_DEBUG); | |
605 | ||
606 | saved_argc = argc; | |
607 | saved_argv = argv; | |
608 | ||
609 | if (argc > 1) { | |
610 | pid_t pid = 0; | |
611 | ||
612 | (void) parse_pid(argv[1], &pid); | |
613 | test_get_process_comm(pid); | |
614 | } else { | |
615 | TEST_REQ_RUNNING_SYSTEMD(test_get_process_comm(1)); | |
616 | test_get_process_comm(getpid()); | |
617 | } | |
618 | ||
619 | test_get_process_comm_escape(); | |
620 | test_pid_is_unwaited(); | |
621 | test_pid_is_alive(); | |
622 | test_personality(); | |
623 | test_get_process_cmdline_harder(); | |
624 | test_rename_process(); | |
625 | test_getpid_cached(); | |
626 | test_getpid_measure(); | |
627 | test_safe_fork(); | |
628 | test_pid_to_ptr(); | |
629 | test_ioprio_class_from_to_string(); | |
630 | ||
631 | return 0; | |
632 | } |