]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
0b452006 | 2 | |
69281c49 LP |
3 | #include <sched.h> |
4 | #include <sys/mount.h> | |
26fbedd7 | 5 | #include <sys/personality.h> |
69281c49 | 6 | #include <sys/prctl.h> |
0b452006 | 7 | #include <sys/stat.h> |
07630cea | 8 | #include <sys/types.h> |
0b452006 RC |
9 | #include <sys/wait.h> |
10 | #include <unistd.h> | |
349cc4a5 | 11 | #if HAVE_VALGRIND_VALGRIND_H |
b3d69149 EV |
12 | #include <valgrind/valgrind.h> |
13 | #endif | |
0b452006 | 14 | |
b5efdb8a | 15 | #include "alloc-util.h" |
26fbedd7 | 16 | #include "architecture.h" |
69281c49 | 17 | #include "fd-util.h" |
0b452006 | 18 | #include "log.h" |
0b452006 | 19 | #include "macro.h" |
36dd5ffd | 20 | #include "missing.h" |
18dade5a | 21 | #include "parse-util.h" |
07630cea | 22 | #include "process-util.h" |
4c253ed1 | 23 | #include "signal-util.h" |
9a140c35 | 24 | #include "stdio-util.h" |
07630cea | 25 | #include "string-util.h" |
288a74cc | 26 | #include "terminal-util.h" |
76c19e9f | 27 | #include "test-helper.h" |
6d7c4033 | 28 | #include "tests.h" |
07630cea LP |
29 | #include "util.h" |
30 | #include "virt.h" | |
0b452006 | 31 | |
9a140c35 | 32 | static void test_get_process_comm(pid_t pid) { |
0b452006 | 33 | struct stat st; |
ba19c6e1 | 34 | _cleanup_free_ char *a = NULL, *c = NULL, *d = NULL, *f = NULL, *i = NULL; |
0b452006 | 35 | _cleanup_free_ char *env = NULL; |
fbd0b64f | 36 | char path[STRLEN("/proc//comm") + DECIMAL_STR_MAX(pid_t)]; |
0b452006 RC |
37 | pid_t e; |
38 | uid_t u; | |
39 | gid_t g; | |
40 | dev_t h; | |
41 | int r; | |
0b452006 | 42 | |
9a140c35 ZJS |
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); | |
cfeaa44a | 48 | } else |
9a140c35 | 49 | log_warning("%s not exist.", path); |
0b452006 | 50 | |
09c1dcee | 51 | assert_se(get_process_cmdline(pid, 0, PROCESS_CMDLINE_COMM_FALLBACK, &c) >= 0); |
9a140c35 | 52 | log_info("PID"PID_FMT" cmdline: '%s'", pid, c); |
0b452006 | 53 | |
09c1dcee | 54 | assert_se(get_process_cmdline(pid, 8, 0, &d) >= 0); |
69281c49 LP |
55 | log_info("PID"PID_FMT" cmdline truncated to 8: '%s'", pid, d); |
56 | ||
57 | free(d); | |
09c1dcee | 58 | assert_se(get_process_cmdline(pid, 1, 0, &d) >= 0); |
69281c49 | 59 | log_info("PID"PID_FMT" cmdline truncated to 1: '%s'", pid, d); |
0b452006 | 60 | |
9a140c35 ZJS |
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); | |
0b452006 | 64 | |
9a140c35 | 65 | assert_se(is_kernel_thread(pid) == 0 || pid != 1); |
0b452006 | 66 | |
9a140c35 | 67 | r = get_process_exe(pid, &f); |
0b452006 | 68 | assert_se(r >= 0 || r == -EACCES); |
9a140c35 | 69 | log_info("PID"PID_FMT" exe: '%s'", pid, strna(f)); |
0b452006 | 70 | |
9a140c35 ZJS |
71 | assert_se(get_process_uid(pid, &u) == 0); |
72 | log_info("PID"PID_FMT" UID: "UID_FMT, pid, u); | |
0b452006 | 73 | |
9a140c35 ZJS |
74 | assert_se(get_process_gid(pid, &g) == 0); |
75 | log_info("PID"PID_FMT" GID: "GID_FMT, pid, g); | |
0b452006 | 76 | |
9a140c35 | 77 | r = get_process_environ(pid, &env); |
0b452006 | 78 | assert_se(r >= 0 || r == -EACCES); |
9a140c35 | 79 | log_info("PID"PID_FMT" strlen(environ): %zi", pid, env ? (ssize_t)strlen(env) : (ssize_t)-errno); |
0b452006 | 80 | |
75f86906 | 81 | if (!detect_container()) |
9a140c35 | 82 | assert_se(get_ctty_devnr(pid, &h) == -ENXIO || pid != 1); |
0b452006 | 83 | |
6fb05b07 | 84 | (void) getenv_for_pid(pid, "PATH", &i); |
9a140c35 | 85 | log_info("PID"PID_FMT" $PATH: '%s'", pid, strna(i)); |
0b452006 RC |
86 | } |
87 | ||
ce268825 LP |
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"); | |
0e85cbcf ZJS |
110 | test_get_process_comm_escape_one("äöüß", "\\303\\244\\303\\266\\303\\274\\303\\237"); |
111 | test_get_process_comm_escape_one("xäöüß", "x\\303\\244\\303\\266\\303\\274\\303\\237"); | |
112 | test_get_process_comm_escape_one("xxäöüß", "xx\\303\\244\\303\\266\\303\\274\\303\\237"); | |
113 | test_get_process_comm_escape_one("xxxäöüß", "xxx\\303\\244\\303\\266\\303\\274\\303\\237"); | |
114 | test_get_process_comm_escape_one("xxxxäöüß", "xxxx\\303\\244\\303\\266\\303\\274\\303\\237"); | |
115 | test_get_process_comm_escape_one("xxxxxäöüß", "xxxxx\\303\\244\\303\\266\\303\\274\\303\\237"); | |
ce268825 LP |
116 | |
117 | assert_se(prctl(PR_SET_NAME, saved) >= 0); | |
118 | } | |
119 | ||
0b452006 RC |
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 | } | |
df0ff127 | 133 | assert_se(pid_is_unwaited(getpid_cached())); |
0b452006 RC |
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 | } | |
df0ff127 | 150 | assert_se(pid_is_alive(getpid_cached())); |
0b452006 RC |
151 | assert_se(!pid_is_alive(-1)); |
152 | } | |
153 | ||
26fbedd7 LP |
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 | ||
69281c49 LP |
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 | ||
92f6a1b6 YW |
183 | if (geteuid() != 0) { |
184 | log_info("Skipping %s: not root", __func__); | |
69281c49 | 185 | return; |
92f6a1b6 | 186 | } |
69281c49 | 187 | |
5f00dc4d LP |
188 | if (!have_namespaces()) { |
189 | log_notice("Testing without namespaces, skipping %s", __func__); | |
190 | return; | |
191 | } | |
192 | ||
349cc4a5 | 193 | #if HAVE_VALGRIND_VALGRIND_H |
b3d69149 EV |
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 */ | |
92f6a1b6 YW |
197 | if (RUNNING_ON_VALGRIND) { |
198 | log_info("Skipping %s: running on valgrind", __func__); | |
b3d69149 | 199 | return; |
92f6a1b6 | 200 | } |
b3d69149 EV |
201 | #endif |
202 | ||
69281c49 LP |
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 | ||
0ffa4c7c EV |
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 | } | |
c58fd466 | 223 | |
69281c49 LP |
224 | fd = mkostemp(path, O_CLOEXEC); |
225 | assert_se(fd >= 0); | |
347ebd02 | 226 | |
0ffa4c7c EV |
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. */ | |
347ebd02 ZJS |
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"); | |
0ffa4c7c | 232 | assert_se(IN_SET(errno, EPERM, EACCES)); |
347ebd02 ZJS |
233 | return; |
234 | } | |
235 | ||
69281c49 LP |
236 | assert_se(unlink(path) >= 0); |
237 | ||
238 | assert_se(prctl(PR_SET_NAME, "testa") >= 0); | |
239 | ||
09c1dcee | 240 | assert_se(get_process_cmdline(getpid_cached(), SIZE_MAX, 0, &line) == -ENOENT); |
69281c49 | 241 | |
09c1dcee | 242 | assert_se(get_process_cmdline(getpid_cached(), SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0); |
69281c49 LP |
243 | assert_se(streq(line, "[testa]")); |
244 | line = mfree(line); | |
245 | ||
09c1dcee | 246 | assert_se(get_process_cmdline(getpid_cached(), 0, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0); |
bc28751e | 247 | log_info("'%s'", line); |
69281c49 LP |
248 | assert_se(streq(line, "")); |
249 | line = mfree(line); | |
250 | ||
09c1dcee | 251 | assert_se(get_process_cmdline(getpid_cached(), 1, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0); |
bc28751e ZJS |
252 | assert_se(streq(line, "…")); |
253 | line = mfree(line); | |
254 | ||
09c1dcee | 255 | assert_se(get_process_cmdline(getpid_cached(), 2, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0); |
bc28751e | 256 | assert_se(streq(line, "[…")); |
69281c49 LP |
257 | line = mfree(line); |
258 | ||
09c1dcee | 259 | assert_se(get_process_cmdline(getpid_cached(), 3, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0); |
bc28751e | 260 | assert_se(streq(line, "[t…")); |
69281c49 LP |
261 | line = mfree(line); |
262 | ||
09c1dcee | 263 | assert_se(get_process_cmdline(getpid_cached(), 4, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0); |
bc28751e | 264 | assert_se(streq(line, "[te…")); |
69281c49 LP |
265 | line = mfree(line); |
266 | ||
09c1dcee | 267 | assert_se(get_process_cmdline(getpid_cached(), 5, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0); |
bc28751e | 268 | assert_se(streq(line, "[tes…")); |
69281c49 LP |
269 | line = mfree(line); |
270 | ||
09c1dcee | 271 | assert_se(get_process_cmdline(getpid_cached(), 6, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0); |
bc28751e | 272 | assert_se(streq(line, "[test…")); |
69281c49 LP |
273 | line = mfree(line); |
274 | ||
09c1dcee | 275 | assert_se(get_process_cmdline(getpid_cached(), 7, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0); |
69281c49 LP |
276 | assert_se(streq(line, "[testa]")); |
277 | line = mfree(line); | |
278 | ||
09c1dcee | 279 | assert_se(get_process_cmdline(getpid_cached(), 8, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0); |
69281c49 LP |
280 | assert_se(streq(line, "[testa]")); |
281 | line = mfree(line); | |
282 | ||
bc28751e | 283 | assert_se(write(fd, "foo\0bar", 8) == 8); |
69281c49 | 284 | |
09c1dcee | 285 | assert_se(get_process_cmdline(getpid_cached(), SIZE_MAX, 0, &line) >= 0); |
bc28751e | 286 | log_info("'%s'", line); |
69281c49 LP |
287 | assert_se(streq(line, "foo bar")); |
288 | line = mfree(line); | |
289 | ||
09c1dcee | 290 | assert_se(get_process_cmdline(getpid_cached(), SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0); |
69281c49 LP |
291 | assert_se(streq(line, "foo bar")); |
292 | line = mfree(line); | |
293 | ||
294 | assert_se(write(fd, "quux", 4) == 4); | |
09c1dcee | 295 | assert_se(get_process_cmdline(getpid_cached(), SIZE_MAX, 0, &line) >= 0); |
bc28751e | 296 | log_info("'%s'", line); |
69281c49 LP |
297 | assert_se(streq(line, "foo bar quux")); |
298 | line = mfree(line); | |
299 | ||
09c1dcee | 300 | assert_se(get_process_cmdline(getpid_cached(), SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0); |
69281c49 LP |
301 | assert_se(streq(line, "foo bar quux")); |
302 | line = mfree(line); | |
303 | ||
09c1dcee | 304 | assert_se(get_process_cmdline(getpid_cached(), 1, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0); |
bc28751e | 305 | assert_se(streq(line, "…")); |
69281c49 LP |
306 | line = mfree(line); |
307 | ||
09c1dcee | 308 | assert_se(get_process_cmdline(getpid_cached(), 2, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0); |
bc28751e | 309 | assert_se(streq(line, "f…")); |
69281c49 LP |
310 | line = mfree(line); |
311 | ||
09c1dcee | 312 | assert_se(get_process_cmdline(getpid_cached(), 3, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0); |
bc28751e | 313 | assert_se(streq(line, "fo…")); |
69281c49 LP |
314 | line = mfree(line); |
315 | ||
09c1dcee | 316 | assert_se(get_process_cmdline(getpid_cached(), 4, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0); |
bc28751e | 317 | assert_se(streq(line, "foo…")); |
69281c49 LP |
318 | line = mfree(line); |
319 | ||
09c1dcee | 320 | assert_se(get_process_cmdline(getpid_cached(), 5, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0); |
bc28751e | 321 | assert_se(streq(line, "foo …")); |
69281c49 LP |
322 | line = mfree(line); |
323 | ||
09c1dcee | 324 | assert_se(get_process_cmdline(getpid_cached(), 6, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0); |
bc28751e | 325 | assert_se(streq(line, "foo b…")); |
69281c49 LP |
326 | line = mfree(line); |
327 | ||
09c1dcee | 328 | assert_se(get_process_cmdline(getpid_cached(), 7, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0); |
bc28751e | 329 | assert_se(streq(line, "foo ba…")); |
69281c49 LP |
330 | line = mfree(line); |
331 | ||
09c1dcee | 332 | assert_se(get_process_cmdline(getpid_cached(), 8, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0); |
bc28751e | 333 | assert_se(streq(line, "foo bar…")); |
69281c49 LP |
334 | line = mfree(line); |
335 | ||
09c1dcee | 336 | assert_se(get_process_cmdline(getpid_cached(), 9, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0); |
bc28751e | 337 | assert_se(streq(line, "foo bar …")); |
69281c49 LP |
338 | line = mfree(line); |
339 | ||
09c1dcee | 340 | assert_se(get_process_cmdline(getpid_cached(), 10, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0); |
bc28751e | 341 | assert_se(streq(line, "foo bar q…")); |
69281c49 LP |
342 | line = mfree(line); |
343 | ||
09c1dcee | 344 | assert_se(get_process_cmdline(getpid_cached(), 11, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0); |
bc28751e | 345 | assert_se(streq(line, "foo bar qu…")); |
69281c49 LP |
346 | line = mfree(line); |
347 | ||
09c1dcee | 348 | assert_se(get_process_cmdline(getpid_cached(), 12, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0); |
bc28751e | 349 | assert_se(streq(line, "foo bar quux")); |
69281c49 LP |
350 | line = mfree(line); |
351 | ||
09c1dcee | 352 | assert_se(get_process_cmdline(getpid_cached(), 13, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0); |
69281c49 LP |
353 | assert_se(streq(line, "foo bar quux")); |
354 | line = mfree(line); | |
355 | ||
09c1dcee | 356 | assert_se(get_process_cmdline(getpid_cached(), 14, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0); |
69281c49 LP |
357 | assert_se(streq(line, "foo bar quux")); |
358 | line = mfree(line); | |
359 | ||
09c1dcee | 360 | assert_se(get_process_cmdline(getpid_cached(), 1000, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0); |
69281c49 LP |
361 | assert_se(streq(line, "foo bar quux")); |
362 | line = mfree(line); | |
363 | ||
364 | assert_se(ftruncate(fd, 0) >= 0); | |
365 | assert_se(prctl(PR_SET_NAME, "aaaa bbbb cccc") >= 0); | |
366 | ||
09c1dcee | 367 | assert_se(get_process_cmdline(getpid_cached(), SIZE_MAX, 0, &line) == -ENOENT); |
69281c49 | 368 | |
09c1dcee | 369 | assert_se(get_process_cmdline(getpid_cached(), SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0); |
69281c49 LP |
370 | assert_se(streq(line, "[aaaa bbbb cccc]")); |
371 | line = mfree(line); | |
372 | ||
09c1dcee | 373 | assert_se(get_process_cmdline(getpid_cached(), 10, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0); |
bc28751e | 374 | assert_se(streq(line, "[aaaa bbb…")); |
69281c49 LP |
375 | line = mfree(line); |
376 | ||
09c1dcee | 377 | assert_se(get_process_cmdline(getpid_cached(), 11, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0); |
bc28751e | 378 | assert_se(streq(line, "[aaaa bbbb…")); |
69281c49 LP |
379 | line = mfree(line); |
380 | ||
09c1dcee | 381 | assert_se(get_process_cmdline(getpid_cached(), 12, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0); |
bc28751e | 382 | assert_se(streq(line, "[aaaa bbbb …")); |
69281c49 LP |
383 | line = mfree(line); |
384 | ||
385 | safe_close(fd); | |
a45d7127 | 386 | _exit(EXIT_SUCCESS); |
69281c49 LP |
387 | } |
388 | ||
049c884a | 389 | static void test_rename_process_now(const char *p, int ret) { |
9bfaffd5 | 390 | _cleanup_free_ char *comm = NULL, *cmdline = NULL; |
9bfaffd5 LP |
391 | int r; |
392 | ||
9bfaffd5 | 393 | r = rename_process(p); |
9bfaffd5 LP |
394 | assert_se(r == ret || |
395 | (ret == 0 && r >= 0) || | |
396 | (ret > 0 && r > 0)); | |
397 | ||
398 | if (r < 0) | |
049c884a | 399 | return; |
9bfaffd5 | 400 | |
349cc4a5 | 401 | #if HAVE_VALGRIND_VALGRIND_H |
9bfaffd5 LP |
402 | /* see above, valgrind is weird, we can't verify what we are doing here */ |
403 | if (RUNNING_ON_VALGRIND) | |
049c884a | 404 | return; |
9bfaffd5 LP |
405 | #endif |
406 | ||
407 | assert_se(get_process_comm(0, &comm) >= 0); | |
408 | log_info("comm = <%s>", comm); | |
92f14395 | 409 | assert_se(strneq(comm, p, TASK_COMM_LEN-1)); |
0e85cbcf ZJS |
410 | /* We expect comm to be at most 16 bytes (TASK_COMM_LEN). The kernel may raise this limit in the |
411 | * future. We'd only check the initial part, at least until we recompile, but this will still pass. */ | |
9bfaffd5 | 412 | |
09c1dcee | 413 | r = get_process_cmdline(0, SIZE_MAX, 0, &cmdline); |
767eab47 | 414 | assert_se(r >= 0); |
049c884a JW |
415 | /* we cannot expect cmdline to be renamed properly without privileges */ |
416 | if (geteuid() == 0) { | |
767eab47 YW |
417 | if (r == 0 && detect_container() > 0) |
418 | log_info("cmdline = <%s> (not verified, Running in unprivileged container?)", cmdline); | |
419 | else { | |
420 | log_info("cmdline = <%s>", cmdline); | |
421 | assert_se(strneq(p, cmdline, STRLEN("test-process-util"))); | |
422 | assert_se(startswith(p, cmdline)); | |
423 | } | |
049c884a JW |
424 | } else |
425 | log_info("cmdline = <%s> (not verified)", cmdline); | |
426 | } | |
427 | ||
428 | static void test_rename_process_one(const char *p, int ret) { | |
429 | siginfo_t si; | |
430 | pid_t pid; | |
431 | ||
432 | pid = fork(); | |
433 | assert_se(pid >= 0); | |
434 | ||
435 | if (pid == 0) { | |
436 | /* child */ | |
437 | test_rename_process_now(p, ret); | |
438 | _exit(EXIT_SUCCESS); | |
439 | } | |
440 | ||
441 | assert_se(wait_for_terminate(pid, &si) >= 0); | |
442 | assert_se(si.si_code == CLD_EXITED); | |
443 | assert_se(si.si_status == EXIT_SUCCESS); | |
444 | } | |
445 | ||
446 | static void test_rename_process_multi(void) { | |
447 | pid_t pid; | |
448 | ||
449 | pid = fork(); | |
450 | assert_se(pid >= 0); | |
9bfaffd5 | 451 | |
049c884a JW |
452 | if (pid > 0) { |
453 | siginfo_t si; | |
454 | ||
455 | assert_se(wait_for_terminate(pid, &si) >= 0); | |
456 | assert_se(si.si_code == CLD_EXITED); | |
457 | assert_se(si.si_status == EXIT_SUCCESS); | |
458 | ||
459 | return; | |
460 | } | |
461 | ||
462 | /* child */ | |
463 | test_rename_process_now("one", 1); | |
464 | test_rename_process_now("more", 0); /* longer than "one", hence truncated */ | |
c47a9f90 | 465 | (void) setresuid(99, 99, 99); /* change uid when running privileged */ |
049c884a JW |
466 | test_rename_process_now("time!", 0); |
467 | test_rename_process_now("0", 1); /* shorter than "one", should fit */ | |
468 | test_rename_process_one("", -EINVAL); | |
469 | test_rename_process_one(NULL, -EINVAL); | |
9bfaffd5 LP |
470 | _exit(EXIT_SUCCESS); |
471 | } | |
472 | ||
473 | static void test_rename_process(void) { | |
474 | test_rename_process_one(NULL, -EINVAL); | |
475 | test_rename_process_one("", -EINVAL); | |
476 | test_rename_process_one("foo", 1); /* should always fit */ | |
477 | test_rename_process_one("this is a really really long process name, followed by some more words", 0); /* unlikely to fit */ | |
478 | test_rename_process_one("1234567", 1); /* should always fit */ | |
049c884a | 479 | test_rename_process_multi(); /* multiple invocations and dropped privileges */ |
9bfaffd5 LP |
480 | } |
481 | ||
5c30a6d2 LP |
482 | static void test_getpid_cached(void) { |
483 | siginfo_t si; | |
484 | pid_t a, b, c, d, e, f, child; | |
485 | ||
486 | a = raw_getpid(); | |
487 | b = getpid_cached(); | |
488 | c = getpid(); | |
489 | ||
490 | assert_se(a == b && a == c); | |
491 | ||
492 | child = fork(); | |
493 | assert_se(child >= 0); | |
494 | ||
495 | if (child == 0) { | |
496 | /* In child */ | |
497 | a = raw_getpid(); | |
498 | b = getpid_cached(); | |
499 | c = getpid(); | |
500 | ||
501 | assert_se(a == b && a == c); | |
a45d7127 | 502 | _exit(EXIT_SUCCESS); |
5c30a6d2 LP |
503 | } |
504 | ||
505 | d = raw_getpid(); | |
506 | e = getpid_cached(); | |
507 | f = getpid(); | |
508 | ||
509 | assert_se(a == d && a == e && a == f); | |
510 | ||
511 | assert_se(wait_for_terminate(child, &si) >= 0); | |
512 | assert_se(si.si_status == 0); | |
513 | assert_se(si.si_code == CLD_EXITED); | |
514 | } | |
515 | ||
516 | #define MEASURE_ITERATIONS (10000000LLU) | |
517 | ||
518 | static void test_getpid_measure(void) { | |
519 | unsigned long long i; | |
520 | usec_t t, q; | |
521 | ||
522 | t = now(CLOCK_MONOTONIC); | |
523 | for (i = 0; i < MEASURE_ITERATIONS; i++) | |
524 | (void) getpid(); | |
525 | q = now(CLOCK_MONOTONIC) - t; | |
526 | ||
527 | log_info(" glibc getpid(): %llu/s\n", (unsigned long long) (MEASURE_ITERATIONS*USEC_PER_SEC/q)); | |
528 | ||
529 | t = now(CLOCK_MONOTONIC); | |
530 | for (i = 0; i < MEASURE_ITERATIONS; i++) | |
531 | (void) getpid_cached(); | |
532 | q = now(CLOCK_MONOTONIC) - t; | |
533 | ||
534 | log_info("getpid_cached(): %llu/s\n", (unsigned long long) (MEASURE_ITERATIONS*USEC_PER_SEC/q)); | |
535 | } | |
536 | ||
4c253ed1 LP |
537 | static void test_safe_fork(void) { |
538 | siginfo_t status; | |
539 | pid_t pid; | |
540 | int r; | |
541 | ||
542 | BLOCK_SIGNALS(SIGCHLD); | |
543 | ||
544 | r = safe_fork("(test-child)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_NULL_STDIO|FORK_REOPEN_LOG, &pid); | |
545 | assert_se(r >= 0); | |
546 | ||
547 | if (r == 0) { | |
548 | /* child */ | |
549 | usleep(100 * USEC_PER_MSEC); | |
550 | ||
551 | _exit(88); | |
552 | } | |
553 | ||
554 | assert_se(wait_for_terminate(pid, &status) >= 0); | |
555 | assert_se(status.si_code == CLD_EXITED); | |
556 | assert_se(status.si_status == 88); | |
557 | } | |
558 | ||
eb5eb254 LP |
559 | static void test_pid_to_ptr(void) { |
560 | ||
561 | assert_se(PTR_TO_PID(NULL) == 0); | |
562 | assert_se(PID_TO_PTR(0) == NULL); | |
563 | ||
564 | assert_se(PTR_TO_PID(PID_TO_PTR(1)) == 1); | |
565 | assert_se(PTR_TO_PID(PID_TO_PTR(2)) == 2); | |
566 | assert_se(PTR_TO_PID(PID_TO_PTR(-1)) == -1); | |
567 | assert_se(PTR_TO_PID(PID_TO_PTR(-2)) == -2); | |
568 | ||
569 | assert_se(PTR_TO_PID(PID_TO_PTR(INT16_MAX)) == INT16_MAX); | |
570 | assert_se(PTR_TO_PID(PID_TO_PTR(INT16_MIN)) == INT16_MIN); | |
571 | ||
572 | #if SIZEOF_PID_T >= 4 | |
573 | assert_se(PTR_TO_PID(PID_TO_PTR(INT32_MAX)) == INT32_MAX); | |
574 | assert_se(PTR_TO_PID(PID_TO_PTR(INT32_MIN)) == INT32_MIN); | |
575 | #endif | |
576 | } | |
577 | ||
10062bbc ZJS |
578 | static void test_ioprio_class_from_to_string_one(const char *val, int expected) { |
579 | assert_se(ioprio_class_from_string(val) == expected); | |
580 | if (expected >= 0) { | |
581 | _cleanup_free_ char *s = NULL; | |
582 | unsigned ret; | |
583 | ||
584 | assert_se(ioprio_class_to_string_alloc(expected, &s) == 0); | |
585 | /* We sometimes get a class number and sometimes a number back */ | |
586 | assert_se(streq(s, val) || | |
587 | safe_atou(val, &ret) == 0); | |
588 | } | |
589 | } | |
9bfaffd5 | 590 | |
10062bbc ZJS |
591 | static void test_ioprio_class_from_to_string(void) { |
592 | test_ioprio_class_from_to_string_one("none", IOPRIO_CLASS_NONE); | |
593 | test_ioprio_class_from_to_string_one("realtime", IOPRIO_CLASS_RT); | |
594 | test_ioprio_class_from_to_string_one("best-effort", IOPRIO_CLASS_BE); | |
595 | test_ioprio_class_from_to_string_one("idle", IOPRIO_CLASS_IDLE); | |
596 | test_ioprio_class_from_to_string_one("0", 0); | |
597 | test_ioprio_class_from_to_string_one("1", 1); | |
598 | test_ioprio_class_from_to_string_one("7", 7); | |
599 | test_ioprio_class_from_to_string_one("8", 8); | |
600 | test_ioprio_class_from_to_string_one("9", -1); | |
601 | test_ioprio_class_from_to_string_one("-1", -1); | |
602 | } | |
603 | ||
604 | int main(int argc, char *argv[]) { | |
6d7c4033 | 605 | test_setup_logging(LOG_DEBUG); |
0b452006 | 606 | |
36fea155 | 607 | save_argc_argv(argc, argv); |
9bfaffd5 | 608 | |
18dade5a ZJS |
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 { | |
76c19e9f | 615 | TEST_REQ_RUNNING_SYSTEMD(test_get_process_comm(1)); |
18dade5a ZJS |
616 | test_get_process_comm(getpid()); |
617 | } | |
618 | ||
ce268825 | 619 | test_get_process_comm_escape(); |
0b452006 RC |
620 | test_pid_is_unwaited(); |
621 | test_pid_is_alive(); | |
26fbedd7 | 622 | test_personality(); |
69281c49 | 623 | test_get_process_cmdline_harder(); |
9bfaffd5 | 624 | test_rename_process(); |
5c30a6d2 LP |
625 | test_getpid_cached(); |
626 | test_getpid_measure(); | |
4c253ed1 | 627 | test_safe_fork(); |
eb5eb254 | 628 | test_pid_to_ptr(); |
10062bbc | 629 | test_ioprio_class_from_to_string(); |
0b452006 RC |
630 | |
631 | return 0; | |
632 | } |