]>
Commit | Line | Data |
---|---|---|
0b452006 RC |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2010 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
4f5dd394 | 20 | #include <ctype.h> |
0b452006 | 21 | #include <errno.h> |
11c3a366 TA |
22 | #include <limits.h> |
23 | #include <linux/oom.h> | |
7b3e062c | 24 | #include <sched.h> |
0b452006 | 25 | #include <signal.h> |
4f5dd394 LP |
26 | #include <stdbool.h> |
27 | #include <stdio.h> | |
11c3a366 | 28 | #include <stdlib.h> |
4f5dd394 | 29 | #include <string.h> |
9bfaffd5 | 30 | #include <sys/mman.h> |
7b3e062c | 31 | #include <sys/personality.h> |
405f8907 | 32 | #include <sys/prctl.h> |
4f5dd394 LP |
33 | #include <sys/types.h> |
34 | #include <sys/wait.h> | |
11c3a366 | 35 | #include <syslog.h> |
4f5dd394 | 36 | #include <unistd.h> |
dcadc967 EV |
37 | #ifdef HAVE_VALGRIND_VALGRIND_H |
38 | #include <valgrind/valgrind.h> | |
39 | #endif | |
0b452006 | 40 | |
b5efdb8a | 41 | #include "alloc-util.h" |
6e5f1b57 | 42 | #include "architecture.h" |
4f5dd394 | 43 | #include "escape.h" |
3ffd4af2 | 44 | #include "fd-util.h" |
0b452006 | 45 | #include "fileio.h" |
f4f15635 | 46 | #include "fs-util.h" |
7b3e062c | 47 | #include "ioprio.h" |
0b452006 | 48 | #include "log.h" |
11c3a366 TA |
49 | #include "macro.h" |
50 | #include "missing.h" | |
93cc7779 | 51 | #include "process-util.h" |
8869a0b4 | 52 | #include "raw-clone.h" |
93cc7779 | 53 | #include "signal-util.h" |
1359fffa | 54 | #include "stat-util.h" |
7b3e062c | 55 | #include "string-table.h" |
07630cea | 56 | #include "string-util.h" |
b1d4f8e1 | 57 | #include "user-util.h" |
4f5dd394 | 58 | #include "util.h" |
0b452006 RC |
59 | |
60 | int get_process_state(pid_t pid) { | |
61 | const char *p; | |
62 | char state; | |
63 | int r; | |
64 | _cleanup_free_ char *line = NULL; | |
65 | ||
66 | assert(pid >= 0); | |
67 | ||
68 | p = procfs_file_alloca(pid, "stat"); | |
a644184a | 69 | |
0b452006 | 70 | r = read_one_line_file(p, &line); |
a644184a LP |
71 | if (r == -ENOENT) |
72 | return -ESRCH; | |
0b452006 RC |
73 | if (r < 0) |
74 | return r; | |
75 | ||
76 | p = strrchr(line, ')'); | |
77 | if (!p) | |
78 | return -EIO; | |
79 | ||
80 | p++; | |
81 | ||
82 | if (sscanf(p, " %c", &state) != 1) | |
83 | return -EIO; | |
84 | ||
85 | return (unsigned char) state; | |
86 | } | |
87 | ||
88 | int get_process_comm(pid_t pid, char **name) { | |
89 | const char *p; | |
90 | int r; | |
91 | ||
92 | assert(name); | |
93 | assert(pid >= 0); | |
94 | ||
95 | p = procfs_file_alloca(pid, "comm"); | |
96 | ||
97 | r = read_one_line_file(p, name); | |
98 | if (r == -ENOENT) | |
99 | return -ESRCH; | |
100 | ||
101 | return r; | |
102 | } | |
103 | ||
104 | int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line) { | |
105 | _cleanup_fclose_ FILE *f = NULL; | |
ba4cd7e2 | 106 | bool space = false; |
c0534780 | 107 | char *k, *ans = NULL; |
0b452006 RC |
108 | const char *p; |
109 | int c; | |
110 | ||
111 | assert(line); | |
112 | assert(pid >= 0); | |
113 | ||
69281c49 LP |
114 | /* Retrieves a process' command line. Replaces unprintable characters while doing so by whitespace (coalescing |
115 | * multiple sequential ones into one). If max_length is != 0 will return a string of the specified size at most | |
116 | * (the trailing NUL byte does count towards the length here!), abbreviated with a "..." ellipsis. If | |
117 | * comm_fallback is true and the process has no command line set (the case for kernel threads), or has a | |
118 | * command line that resolves to the empty string will return the "comm" name of the process instead. | |
119 | * | |
120 | * Returns -ESRCH if the process doesn't exist, and -ENOENT if the process has no command line (and | |
c0534780 | 121 | * comm_fallback is false). Returns 0 and sets *line otherwise. */ |
69281c49 | 122 | |
0b452006 RC |
123 | p = procfs_file_alloca(pid, "cmdline"); |
124 | ||
125 | f = fopen(p, "re"); | |
a644184a LP |
126 | if (!f) { |
127 | if (errno == ENOENT) | |
128 | return -ESRCH; | |
0b452006 | 129 | return -errno; |
a644184a | 130 | } |
0b452006 | 131 | |
69281c49 LP |
132 | if (max_length == 1) { |
133 | ||
134 | /* If there's only room for one byte, return the empty string */ | |
c0534780 ZJS |
135 | ans = new0(char, 1); |
136 | if (!ans) | |
69281c49 LP |
137 | return -ENOMEM; |
138 | ||
c0534780 | 139 | *line = ans; |
69281c49 LP |
140 | return 0; |
141 | ||
142 | } else if (max_length == 0) { | |
0b452006 RC |
143 | size_t len = 0, allocated = 0; |
144 | ||
145 | while ((c = getc(f)) != EOF) { | |
146 | ||
c0534780 ZJS |
147 | if (!GREEDY_REALLOC(ans, allocated, len+3)) { |
148 | free(ans); | |
0b452006 RC |
149 | return -ENOMEM; |
150 | } | |
151 | ||
ba4cd7e2 MP |
152 | if (isprint(c)) { |
153 | if (space) { | |
c0534780 | 154 | ans[len++] = ' '; |
ba4cd7e2 MP |
155 | space = false; |
156 | } | |
157 | ||
c0534780 | 158 | ans[len++] = c; |
69281c49 | 159 | } else if (len > 0) |
ba4cd7e2 MP |
160 | space = true; |
161 | } | |
0b452006 RC |
162 | |
163 | if (len > 0) | |
c0534780 | 164 | ans[len] = '\0'; |
b09df4e2 | 165 | else |
c0534780 | 166 | ans = mfree(ans); |
0b452006 RC |
167 | |
168 | } else { | |
69281c49 | 169 | bool dotdotdot = false; |
0b452006 RC |
170 | size_t left; |
171 | ||
c0534780 ZJS |
172 | ans = new(char, max_length); |
173 | if (!ans) | |
0b452006 RC |
174 | return -ENOMEM; |
175 | ||
c0534780 | 176 | k = ans; |
0b452006 RC |
177 | left = max_length; |
178 | while ((c = getc(f)) != EOF) { | |
179 | ||
180 | if (isprint(c)) { | |
69281c49 | 181 | |
0b452006 | 182 | if (space) { |
69281c49 LP |
183 | if (left <= 2) { |
184 | dotdotdot = true; | |
0b452006 | 185 | break; |
69281c49 | 186 | } |
0b452006 RC |
187 | |
188 | *(k++) = ' '; | |
189 | left--; | |
190 | space = false; | |
191 | } | |
192 | ||
69281c49 LP |
193 | if (left <= 1) { |
194 | dotdotdot = true; | |
0b452006 | 195 | break; |
69281c49 | 196 | } |
0b452006 RC |
197 | |
198 | *(k++) = (char) c; | |
199 | left--; | |
c0534780 | 200 | } else if (k > ans) |
0b452006 RC |
201 | space = true; |
202 | } | |
203 | ||
69281c49 LP |
204 | if (dotdotdot) { |
205 | if (max_length <= 4) { | |
c0534780 | 206 | k = ans; |
69281c49 LP |
207 | left = max_length; |
208 | } else { | |
c0534780 | 209 | k = ans + max_length - 4; |
69281c49 LP |
210 | left = 4; |
211 | ||
212 | /* Eat up final spaces */ | |
c0534780 | 213 | while (k > ans && isspace(k[-1])) { |
69281c49 LP |
214 | k--; |
215 | left++; | |
216 | } | |
217 | } | |
218 | ||
219 | strncpy(k, "...", left-1); | |
b09df4e2 | 220 | k[left-1] = 0; |
0b452006 RC |
221 | } else |
222 | *k = 0; | |
223 | } | |
224 | ||
225 | /* Kernel threads have no argv[] */ | |
c0534780 | 226 | if (isempty(ans)) { |
0b452006 RC |
227 | _cleanup_free_ char *t = NULL; |
228 | int h; | |
229 | ||
c0534780 | 230 | free(ans); |
0b452006 RC |
231 | |
232 | if (!comm_fallback) | |
233 | return -ENOENT; | |
234 | ||
235 | h = get_process_comm(pid, &t); | |
236 | if (h < 0) | |
237 | return h; | |
238 | ||
69281c49 | 239 | if (max_length == 0) |
c0534780 | 240 | ans = strjoin("[", t, "]"); |
69281c49 LP |
241 | else { |
242 | size_t l; | |
243 | ||
244 | l = strlen(t); | |
245 | ||
246 | if (l + 3 <= max_length) | |
c0534780 | 247 | ans = strjoin("[", t, "]"); |
69281c49 LP |
248 | else if (max_length <= 6) { |
249 | ||
c0534780 ZJS |
250 | ans = new(char, max_length); |
251 | if (!ans) | |
69281c49 LP |
252 | return -ENOMEM; |
253 | ||
c0534780 ZJS |
254 | memcpy(ans, "[...]", max_length-1); |
255 | ans[max_length-1] = 0; | |
69281c49 LP |
256 | } else { |
257 | char *e; | |
258 | ||
259 | t[max_length - 6] = 0; | |
260 | ||
261 | /* Chop off final spaces */ | |
262 | e = strchr(t, 0); | |
263 | while (e > t && isspace(e[-1])) | |
264 | e--; | |
265 | *e = 0; | |
266 | ||
c0534780 | 267 | ans = strjoin("[", t, "...]"); |
69281c49 LP |
268 | } |
269 | } | |
c0534780 | 270 | if (!ans) |
0b452006 RC |
271 | return -ENOMEM; |
272 | } | |
273 | ||
c0534780 | 274 | *line = ans; |
0b452006 RC |
275 | return 0; |
276 | } | |
277 | ||
9bfaffd5 LP |
278 | int rename_process(const char name[]) { |
279 | static size_t mm_size = 0; | |
280 | static char *mm = NULL; | |
281 | bool truncated = false; | |
282 | size_t l; | |
283 | ||
284 | /* This is a like a poor man's setproctitle(). It changes the comm field, argv[0], and also the glibc's | |
285 | * internally used name of the process. For the first one a limit of 16 chars applies; to the second one in | |
286 | * many cases one of 10 (i.e. length of "/sbin/init") — however if we have CAP_SYS_RESOURCES it is unbounded; | |
287 | * to the third one 7 (i.e. the length of "systemd". If you pass a longer string it will likely be | |
288 | * truncated. | |
289 | * | |
290 | * Returns 0 if a name was set but truncated, > 0 if it was set but not truncated. */ | |
291 | ||
292 | if (isempty(name)) | |
293 | return -EINVAL; /* let's not confuse users unnecessarily with an empty name */ | |
405f8907 | 294 | |
9bfaffd5 | 295 | l = strlen(name); |
405f8907 | 296 | |
9bfaffd5 | 297 | /* First step, change the comm field. */ |
79d62972 | 298 | (void) prctl(PR_SET_NAME, name); |
9bfaffd5 LP |
299 | if (l > 15) /* Linux process names can be 15 chars at max */ |
300 | truncated = true; | |
301 | ||
302 | /* Second step, change glibc's ID of the process name. */ | |
303 | if (program_invocation_name) { | |
304 | size_t k; | |
305 | ||
306 | k = strlen(program_invocation_name); | |
307 | strncpy(program_invocation_name, name, k); | |
308 | if (l > k) | |
309 | truncated = true; | |
310 | } | |
311 | ||
312 | /* Third step, completely replace the argv[] array the kernel maintains for us. This requires privileges, but | |
313 | * has the advantage that the argv[] array is exactly what we want it to be, and not filled up with zeros at | |
13e785f7 | 314 | * the end. This is the best option for changing /proc/self/cmdline. */ |
01f989c6 JW |
315 | |
316 | /* Let's not bother with this if we don't have euid == 0. Strictly speaking we should check for the | |
317 | * CAP_SYS_RESOURCE capability which is independent of the euid. In our own code the capability generally is | |
318 | * present only for euid == 0, hence let's use this as quick bypass check, to avoid calling mmap() if | |
319 | * PR_SET_MM_ARG_{START,END} fails with EPERM later on anyway. After all geteuid() is dead cheap to call, but | |
320 | * mmap() is not. */ | |
321 | if (geteuid() != 0) | |
322 | log_debug("Skipping PR_SET_MM, as we don't have privileges."); | |
323 | else if (mm_size < l+1) { | |
9bfaffd5 LP |
324 | size_t nn_size; |
325 | char *nn; | |
326 | ||
9bfaffd5 LP |
327 | nn_size = PAGE_ALIGN(l+1); |
328 | nn = mmap(NULL, nn_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); | |
329 | if (nn == MAP_FAILED) { | |
330 | log_debug_errno(errno, "mmap() failed: %m"); | |
331 | goto use_saved_argv; | |
332 | } | |
405f8907 | 333 | |
9bfaffd5 LP |
334 | strncpy(nn, name, nn_size); |
335 | ||
336 | /* Now, let's tell the kernel about this new memory */ | |
337 | if (prctl(PR_SET_MM, PR_SET_MM_ARG_START, (unsigned long) nn, 0, 0) < 0) { | |
338 | log_debug_errno(errno, "PR_SET_MM_ARG_START failed, proceeding without: %m"); | |
339 | (void) munmap(nn, nn_size); | |
340 | goto use_saved_argv; | |
341 | } | |
342 | ||
343 | /* And update the end pointer to the new end, too. If this fails, we don't really know what to do, it's | |
344 | * pretty unlikely that we can rollback, hence we'll just accept the failure, and continue. */ | |
345 | if (prctl(PR_SET_MM, PR_SET_MM_ARG_END, (unsigned long) nn + l + 1, 0, 0) < 0) | |
346 | log_debug_errno(errno, "PR_SET_MM_ARG_END failed, proceeding without: %m"); | |
347 | ||
348 | if (mm) | |
349 | (void) munmap(mm, mm_size); | |
350 | ||
351 | mm = nn; | |
352 | mm_size = nn_size; | |
01f989c6 | 353 | } else { |
9bfaffd5 LP |
354 | strncpy(mm, name, mm_size); |
355 | ||
01f989c6 JW |
356 | /* Update the end pointer, continuing regardless of any failure. */ |
357 | if (prctl(PR_SET_MM, PR_SET_MM_ARG_END, (unsigned long) mm + l + 1, 0, 0) < 0) | |
358 | log_debug_errno(errno, "PR_SET_MM_ARG_END failed, proceeding without: %m"); | |
359 | } | |
360 | ||
9bfaffd5 LP |
361 | use_saved_argv: |
362 | /* Fourth step: in all cases we'll also update the original argv[], so that our own code gets it right too if | |
363 | * it still looks here */ | |
405f8907 LP |
364 | |
365 | if (saved_argc > 0) { | |
366 | int i; | |
367 | ||
9bfaffd5 LP |
368 | if (saved_argv[0]) { |
369 | size_t k; | |
370 | ||
371 | k = strlen(saved_argv[0]); | |
372 | strncpy(saved_argv[0], name, k); | |
373 | if (l > k) | |
374 | truncated = true; | |
375 | } | |
405f8907 LP |
376 | |
377 | for (i = 1; i < saved_argc; i++) { | |
378 | if (!saved_argv[i]) | |
379 | break; | |
380 | ||
381 | memzero(saved_argv[i], strlen(saved_argv[i])); | |
382 | } | |
383 | } | |
9bfaffd5 LP |
384 | |
385 | return !truncated; | |
405f8907 LP |
386 | } |
387 | ||
0b452006 RC |
388 | int is_kernel_thread(pid_t pid) { |
389 | const char *p; | |
390 | size_t count; | |
391 | char c; | |
392 | bool eof; | |
393 | FILE *f; | |
394 | ||
6f8cbcdb | 395 | if (pid == 0 || pid == 1 || pid == getpid_cached()) /* pid 1, and we ourselves certainly aren't a kernel thread */ |
0b452006 RC |
396 | return 0; |
397 | ||
a6149b93 | 398 | assert(pid > 1); |
0b452006 RC |
399 | |
400 | p = procfs_file_alloca(pid, "cmdline"); | |
401 | f = fopen(p, "re"); | |
a644184a LP |
402 | if (!f) { |
403 | if (errno == ENOENT) | |
404 | return -ESRCH; | |
0b452006 | 405 | return -errno; |
a644184a | 406 | } |
0b452006 RC |
407 | |
408 | count = fread(&c, 1, 1, f); | |
409 | eof = feof(f); | |
410 | fclose(f); | |
411 | ||
412 | /* Kernel threads have an empty cmdline */ | |
413 | ||
414 | if (count <= 0) | |
415 | return eof ? 1 : -errno; | |
416 | ||
417 | return 0; | |
418 | } | |
419 | ||
420 | int get_process_capeff(pid_t pid, char **capeff) { | |
421 | const char *p; | |
a644184a | 422 | int r; |
0b452006 RC |
423 | |
424 | assert(capeff); | |
425 | assert(pid >= 0); | |
426 | ||
427 | p = procfs_file_alloca(pid, "status"); | |
428 | ||
c4cd1d4d | 429 | r = get_proc_field(p, "CapEff", WHITESPACE, capeff); |
a644184a LP |
430 | if (r == -ENOENT) |
431 | return -ESRCH; | |
432 | ||
433 | return r; | |
0b452006 RC |
434 | } |
435 | ||
436 | static int get_process_link_contents(const char *proc_file, char **name) { | |
437 | int r; | |
438 | ||
439 | assert(proc_file); | |
440 | assert(name); | |
441 | ||
442 | r = readlink_malloc(proc_file, name); | |
a644184a LP |
443 | if (r == -ENOENT) |
444 | return -ESRCH; | |
0b452006 | 445 | if (r < 0) |
a644184a | 446 | return r; |
0b452006 RC |
447 | |
448 | return 0; | |
449 | } | |
450 | ||
451 | int get_process_exe(pid_t pid, char **name) { | |
452 | const char *p; | |
453 | char *d; | |
454 | int r; | |
455 | ||
456 | assert(pid >= 0); | |
457 | ||
458 | p = procfs_file_alloca(pid, "exe"); | |
459 | r = get_process_link_contents(p, name); | |
460 | if (r < 0) | |
461 | return r; | |
462 | ||
463 | d = endswith(*name, " (deleted)"); | |
464 | if (d) | |
465 | *d = '\0'; | |
466 | ||
467 | return 0; | |
468 | } | |
469 | ||
470 | static int get_process_id(pid_t pid, const char *field, uid_t *uid) { | |
471 | _cleanup_fclose_ FILE *f = NULL; | |
472 | char line[LINE_MAX]; | |
473 | const char *p; | |
474 | ||
475 | assert(field); | |
476 | assert(uid); | |
477 | ||
ee043777 | 478 | if (!pid_is_valid(pid)) |
6f8cbcdb LP |
479 | return -EINVAL; |
480 | ||
0b452006 RC |
481 | p = procfs_file_alloca(pid, "status"); |
482 | f = fopen(p, "re"); | |
a644184a LP |
483 | if (!f) { |
484 | if (errno == ENOENT) | |
485 | return -ESRCH; | |
0b452006 | 486 | return -errno; |
a644184a | 487 | } |
0b452006 RC |
488 | |
489 | FOREACH_LINE(line, f, return -errno) { | |
490 | char *l; | |
491 | ||
492 | l = strstrip(line); | |
493 | ||
494 | if (startswith(l, field)) { | |
495 | l += strlen(field); | |
496 | l += strspn(l, WHITESPACE); | |
497 | ||
498 | l[strcspn(l, WHITESPACE)] = 0; | |
499 | ||
500 | return parse_uid(l, uid); | |
501 | } | |
502 | } | |
503 | ||
504 | return -EIO; | |
505 | } | |
506 | ||
507 | int get_process_uid(pid_t pid, uid_t *uid) { | |
6f8cbcdb LP |
508 | |
509 | if (pid == 0 || pid == getpid_cached()) { | |
510 | *uid = getuid(); | |
511 | return 0; | |
512 | } | |
513 | ||
0b452006 RC |
514 | return get_process_id(pid, "Uid:", uid); |
515 | } | |
516 | ||
517 | int get_process_gid(pid_t pid, gid_t *gid) { | |
6f8cbcdb LP |
518 | |
519 | if (pid == 0 || pid == getpid_cached()) { | |
520 | *gid = getgid(); | |
521 | return 0; | |
522 | } | |
523 | ||
0b452006 RC |
524 | assert_cc(sizeof(uid_t) == sizeof(gid_t)); |
525 | return get_process_id(pid, "Gid:", gid); | |
526 | } | |
527 | ||
528 | int get_process_cwd(pid_t pid, char **cwd) { | |
529 | const char *p; | |
530 | ||
531 | assert(pid >= 0); | |
532 | ||
533 | p = procfs_file_alloca(pid, "cwd"); | |
534 | ||
535 | return get_process_link_contents(p, cwd); | |
536 | } | |
537 | ||
538 | int get_process_root(pid_t pid, char **root) { | |
539 | const char *p; | |
540 | ||
541 | assert(pid >= 0); | |
542 | ||
543 | p = procfs_file_alloca(pid, "root"); | |
544 | ||
545 | return get_process_link_contents(p, root); | |
546 | } | |
547 | ||
548 | int get_process_environ(pid_t pid, char **env) { | |
549 | _cleanup_fclose_ FILE *f = NULL; | |
550 | _cleanup_free_ char *outcome = NULL; | |
551 | int c; | |
552 | const char *p; | |
553 | size_t allocated = 0, sz = 0; | |
554 | ||
555 | assert(pid >= 0); | |
556 | assert(env); | |
557 | ||
558 | p = procfs_file_alloca(pid, "environ"); | |
559 | ||
560 | f = fopen(p, "re"); | |
a644184a LP |
561 | if (!f) { |
562 | if (errno == ENOENT) | |
563 | return -ESRCH; | |
0b452006 | 564 | return -errno; |
a644184a | 565 | } |
0b452006 RC |
566 | |
567 | while ((c = fgetc(f)) != EOF) { | |
568 | if (!GREEDY_REALLOC(outcome, allocated, sz + 5)) | |
569 | return -ENOMEM; | |
570 | ||
571 | if (c == '\0') | |
572 | outcome[sz++] = '\n'; | |
573 | else | |
574 | sz += cescape_char(c, outcome + sz); | |
575 | } | |
576 | ||
03c55bc0 LP |
577 | if (!outcome) { |
578 | outcome = strdup(""); | |
579 | if (!outcome) | |
580 | return -ENOMEM; | |
581 | } else | |
582 | outcome[sz] = '\0'; | |
de8763b6 | 583 | |
0b452006 RC |
584 | *env = outcome; |
585 | outcome = NULL; | |
586 | ||
587 | return 0; | |
588 | } | |
589 | ||
6bc73acb | 590 | int get_process_ppid(pid_t pid, pid_t *_ppid) { |
0b452006 RC |
591 | int r; |
592 | _cleanup_free_ char *line = NULL; | |
593 | long unsigned ppid; | |
594 | const char *p; | |
595 | ||
596 | assert(pid >= 0); | |
597 | assert(_ppid); | |
598 | ||
6f8cbcdb | 599 | if (pid == 0 || pid == getpid_cached()) { |
0b452006 RC |
600 | *_ppid = getppid(); |
601 | return 0; | |
602 | } | |
603 | ||
604 | p = procfs_file_alloca(pid, "stat"); | |
605 | r = read_one_line_file(p, &line); | |
a644184a LP |
606 | if (r == -ENOENT) |
607 | return -ESRCH; | |
0b452006 RC |
608 | if (r < 0) |
609 | return r; | |
610 | ||
611 | /* Let's skip the pid and comm fields. The latter is enclosed | |
612 | * in () but does not escape any () in its value, so let's | |
613 | * skip over it manually */ | |
614 | ||
615 | p = strrchr(line, ')'); | |
616 | if (!p) | |
617 | return -EIO; | |
618 | ||
619 | p++; | |
620 | ||
621 | if (sscanf(p, " " | |
622 | "%*c " /* state */ | |
623 | "%lu ", /* ppid */ | |
624 | &ppid) != 1) | |
625 | return -EIO; | |
626 | ||
627 | if ((long unsigned) (pid_t) ppid != ppid) | |
628 | return -ERANGE; | |
629 | ||
630 | *_ppid = (pid_t) ppid; | |
631 | ||
632 | return 0; | |
633 | } | |
634 | ||
635 | int wait_for_terminate(pid_t pid, siginfo_t *status) { | |
636 | siginfo_t dummy; | |
637 | ||
638 | assert(pid >= 1); | |
639 | ||
640 | if (!status) | |
641 | status = &dummy; | |
642 | ||
643 | for (;;) { | |
644 | zero(*status); | |
645 | ||
646 | if (waitid(P_PID, pid, status, WEXITED) < 0) { | |
647 | ||
648 | if (errno == EINTR) | |
649 | continue; | |
650 | ||
3f0083a2 | 651 | return negative_errno(); |
0b452006 RC |
652 | } |
653 | ||
654 | return 0; | |
655 | } | |
656 | } | |
657 | ||
658 | /* | |
659 | * Return values: | |
660 | * < 0 : wait_for_terminate() failed to get the state of the | |
661 | * process, the process was terminated by a signal, or | |
662 | * failed for an unknown reason. | |
663 | * >=0 : The process terminated normally, and its exit code is | |
664 | * returned. | |
665 | * | |
666 | * That is, success is indicated by a return value of zero, and an | |
667 | * error is indicated by a non-zero value. | |
668 | * | |
669 | * A warning is emitted if the process terminates abnormally, | |
670 | * and also if it returns non-zero unless check_exit_code is true. | |
671 | */ | |
672 | int wait_for_terminate_and_warn(const char *name, pid_t pid, bool check_exit_code) { | |
673 | int r; | |
674 | siginfo_t status; | |
675 | ||
676 | assert(name); | |
677 | assert(pid > 1); | |
678 | ||
679 | r = wait_for_terminate(pid, &status); | |
680 | if (r < 0) | |
681 | return log_warning_errno(r, "Failed to wait for %s: %m", name); | |
682 | ||
683 | if (status.si_code == CLD_EXITED) { | |
684 | if (status.si_status != 0) | |
685 | log_full(check_exit_code ? LOG_WARNING : LOG_DEBUG, | |
686 | "%s failed with error code %i.", name, status.si_status); | |
687 | else | |
688 | log_debug("%s succeeded.", name); | |
689 | ||
690 | return status.si_status; | |
3742095b | 691 | } else if (IN_SET(status.si_code, CLD_KILLED, CLD_DUMPED)) { |
0b452006 RC |
692 | |
693 | log_warning("%s terminated by signal %s.", name, signal_to_string(status.si_status)); | |
694 | return -EPROTO; | |
695 | } | |
696 | ||
697 | log_warning("%s failed due to unknown reason.", name); | |
698 | return -EPROTO; | |
699 | } | |
700 | ||
89c9030d LP |
701 | void sigkill_wait(pid_t pid) { |
702 | assert(pid > 1); | |
703 | ||
704 | if (kill(pid, SIGKILL) > 0) | |
705 | (void) wait_for_terminate(pid, NULL); | |
706 | } | |
707 | ||
708 | void sigkill_waitp(pid_t *pid) { | |
4d0d3d41 LP |
709 | if (!pid) |
710 | return; | |
711 | if (*pid <= 1) | |
712 | return; | |
713 | ||
89c9030d | 714 | sigkill_wait(*pid); |
4d0d3d41 LP |
715 | } |
716 | ||
0b452006 RC |
717 | int kill_and_sigcont(pid_t pid, int sig) { |
718 | int r; | |
719 | ||
720 | r = kill(pid, sig) < 0 ? -errno : 0; | |
721 | ||
26f417d3 LP |
722 | /* If this worked, also send SIGCONT, unless we already just sent a SIGCONT, or SIGKILL was sent which isn't |
723 | * affected by a process being suspended anyway. */ | |
a3d8d68c | 724 | if (r >= 0 && !IN_SET(sig, SIGCONT, SIGKILL)) |
26f417d3 | 725 | (void) kill(pid, SIGCONT); |
0b452006 RC |
726 | |
727 | return r; | |
728 | } | |
729 | ||
730 | int getenv_for_pid(pid_t pid, const char *field, char **_value) { | |
731 | _cleanup_fclose_ FILE *f = NULL; | |
732 | char *value = NULL; | |
733 | int r; | |
734 | bool done = false; | |
735 | size_t l; | |
736 | const char *path; | |
737 | ||
738 | assert(pid >= 0); | |
739 | assert(field); | |
740 | assert(_value); | |
741 | ||
742 | path = procfs_file_alloca(pid, "environ"); | |
743 | ||
744 | f = fopen(path, "re"); | |
a644184a LP |
745 | if (!f) { |
746 | if (errno == ENOENT) | |
747 | return -ESRCH; | |
0b452006 | 748 | return -errno; |
a644184a | 749 | } |
0b452006 RC |
750 | |
751 | l = strlen(field); | |
752 | r = 0; | |
753 | ||
754 | do { | |
755 | char line[LINE_MAX]; | |
756 | unsigned i; | |
757 | ||
758 | for (i = 0; i < sizeof(line)-1; i++) { | |
759 | int c; | |
760 | ||
761 | c = getc(f); | |
762 | if (_unlikely_(c == EOF)) { | |
763 | done = true; | |
764 | break; | |
765 | } else if (c == 0) | |
766 | break; | |
767 | ||
768 | line[i] = c; | |
769 | } | |
770 | line[i] = 0; | |
771 | ||
041b5ae1 | 772 | if (strneq(line, field, l) && line[l] == '=') { |
0b452006 RC |
773 | value = strdup(line + l + 1); |
774 | if (!value) | |
775 | return -ENOMEM; | |
776 | ||
777 | r = 1; | |
778 | break; | |
779 | } | |
780 | ||
781 | } while (!done); | |
782 | ||
783 | *_value = value; | |
784 | return r; | |
785 | } | |
786 | ||
787 | bool pid_is_unwaited(pid_t pid) { | |
788 | /* Checks whether a PID is still valid at all, including a zombie */ | |
789 | ||
ee043777 | 790 | if (!pid_is_valid(pid)) |
0b452006 RC |
791 | return false; |
792 | ||
5fd9b2c5 LP |
793 | if (pid <= 1) /* If we or PID 1 would be dead and have been waited for, this code would not be running */ |
794 | return true; | |
795 | ||
6f8cbcdb LP |
796 | if (pid == getpid_cached()) |
797 | return true; | |
798 | ||
0b452006 RC |
799 | if (kill(pid, 0) >= 0) |
800 | return true; | |
801 | ||
802 | return errno != ESRCH; | |
803 | } | |
804 | ||
805 | bool pid_is_alive(pid_t pid) { | |
806 | int r; | |
807 | ||
808 | /* Checks whether a PID is still valid and not a zombie */ | |
809 | ||
ee043777 | 810 | if (!pid_is_valid(pid)) |
0b452006 RC |
811 | return false; |
812 | ||
5fd9b2c5 LP |
813 | if (pid <= 1) /* If we or PID 1 would be a zombie, this code would not be running */ |
814 | return true; | |
815 | ||
6f8cbcdb LP |
816 | if (pid == getpid_cached()) |
817 | return true; | |
818 | ||
0b452006 | 819 | r = get_process_state(pid); |
a644184a | 820 | if (r == -ESRCH || r == 'Z') |
0b452006 RC |
821 | return false; |
822 | ||
823 | return true; | |
824 | } | |
d4510856 | 825 | |
1359fffa MS |
826 | int pid_from_same_root_fs(pid_t pid) { |
827 | const char *root; | |
828 | ||
ee043777 | 829 | if (!pid_is_valid(pid)) |
6f8cbcdb LP |
830 | return false; |
831 | ||
832 | if (pid == 0 || pid == getpid_cached()) | |
833 | return true; | |
1359fffa MS |
834 | |
835 | root = procfs_file_alloca(pid, "root"); | |
836 | ||
e3f791a2 | 837 | return files_same(root, "/proc/1/root", 0); |
1359fffa MS |
838 | } |
839 | ||
d4510856 LP |
840 | bool is_main_thread(void) { |
841 | static thread_local int cached = 0; | |
842 | ||
843 | if (_unlikely_(cached == 0)) | |
df0ff127 | 844 | cached = getpid_cached() == gettid() ? 1 : -1; |
d4510856 LP |
845 | |
846 | return cached > 0; | |
847 | } | |
7b3e062c LP |
848 | |
849 | noreturn void freeze(void) { | |
850 | ||
3da48d7a EV |
851 | log_close(); |
852 | ||
7b3e062c LP |
853 | /* Make sure nobody waits for us on a socket anymore */ |
854 | close_all_fds(NULL, 0); | |
855 | ||
856 | sync(); | |
857 | ||
858 | for (;;) | |
859 | pause(); | |
860 | } | |
861 | ||
862 | bool oom_score_adjust_is_valid(int oa) { | |
863 | return oa >= OOM_SCORE_ADJ_MIN && oa <= OOM_SCORE_ADJ_MAX; | |
864 | } | |
865 | ||
866 | unsigned long personality_from_string(const char *p) { | |
6e5f1b57 | 867 | int architecture; |
7b3e062c | 868 | |
0c0fea07 LP |
869 | if (!p) |
870 | return PERSONALITY_INVALID; | |
871 | ||
6e5f1b57 LP |
872 | /* Parse a personality specifier. We use our own identifiers that indicate specific ABIs, rather than just |
873 | * hints regarding the register size, since we want to keep things open for multiple locally supported ABIs for | |
874 | * the same register size. */ | |
875 | ||
876 | architecture = architecture_from_string(p); | |
877 | if (architecture < 0) | |
878 | return PERSONALITY_INVALID; | |
7b3e062c | 879 | |
0c0fea07 | 880 | if (architecture == native_architecture()) |
7b3e062c | 881 | return PER_LINUX; |
0c0fea07 LP |
882 | #ifdef SECONDARY_ARCHITECTURE |
883 | if (architecture == SECONDARY_ARCHITECTURE) | |
f2d1736c | 884 | return PER_LINUX32; |
7b3e062c LP |
885 | #endif |
886 | ||
887 | return PERSONALITY_INVALID; | |
888 | } | |
889 | ||
890 | const char* personality_to_string(unsigned long p) { | |
6e5f1b57 | 891 | int architecture = _ARCHITECTURE_INVALID; |
7b3e062c | 892 | |
7b3e062c | 893 | if (p == PER_LINUX) |
0c0fea07 LP |
894 | architecture = native_architecture(); |
895 | #ifdef SECONDARY_ARCHITECTURE | |
6e5f1b57 | 896 | else if (p == PER_LINUX32) |
0c0fea07 | 897 | architecture = SECONDARY_ARCHITECTURE; |
7b3e062c LP |
898 | #endif |
899 | ||
6e5f1b57 LP |
900 | if (architecture < 0) |
901 | return NULL; | |
902 | ||
903 | return architecture_to_string(architecture); | |
7b3e062c LP |
904 | } |
905 | ||
21022b9d LP |
906 | int safe_personality(unsigned long p) { |
907 | int ret; | |
908 | ||
909 | /* So here's the deal, personality() is weirdly defined by glibc. In some cases it returns a failure via errno, | |
910 | * and in others as negative return value containing an errno-like value. Let's work around this: this is a | |
911 | * wrapper that uses errno if it is set, and uses the return value otherwise. And then it sets both errno and | |
912 | * the return value indicating the same issue, so that we are definitely on the safe side. | |
913 | * | |
914 | * See https://github.com/systemd/systemd/issues/6737 */ | |
915 | ||
916 | errno = 0; | |
917 | ret = personality(p); | |
918 | if (ret < 0) { | |
919 | if (errno != 0) | |
920 | return -errno; | |
921 | ||
922 | errno = -ret; | |
923 | } | |
924 | ||
925 | return ret; | |
926 | } | |
927 | ||
e8132d63 LP |
928 | int opinionated_personality(unsigned long *ret) { |
929 | int current; | |
930 | ||
931 | /* Returns the current personality, or PERSONALITY_INVALID if we can't determine it. This function is a bit | |
932 | * opinionated though, and ignores all the finer-grained bits and exotic personalities, only distinguishing the | |
933 | * two most relevant personalities: PER_LINUX and PER_LINUX32. */ | |
934 | ||
21022b9d | 935 | current = safe_personality(PERSONALITY_INVALID); |
e8132d63 | 936 | if (current < 0) |
21022b9d | 937 | return current; |
e8132d63 LP |
938 | |
939 | if (((unsigned long) current & 0xffff) == PER_LINUX32) | |
940 | *ret = PER_LINUX32; | |
941 | else | |
942 | *ret = PER_LINUX; | |
943 | ||
944 | return 0; | |
945 | } | |
946 | ||
dcadc967 EV |
947 | void valgrind_summary_hack(void) { |
948 | #ifdef HAVE_VALGRIND_VALGRIND_H | |
df0ff127 | 949 | if (getpid_cached() == 1 && RUNNING_ON_VALGRIND) { |
dcadc967 | 950 | pid_t pid; |
8869a0b4 | 951 | pid = raw_clone(SIGCHLD); |
dcadc967 EV |
952 | if (pid < 0) |
953 | log_emergency_errno(errno, "Failed to fork off valgrind helper: %m"); | |
954 | else if (pid == 0) | |
955 | exit(EXIT_SUCCESS); | |
956 | else { | |
957 | log_info("Spawned valgrind helper as PID "PID_FMT".", pid); | |
958 | (void) wait_for_terminate(pid, NULL); | |
959 | } | |
960 | } | |
961 | #endif | |
962 | } | |
963 | ||
291d565a LP |
964 | int pid_compare_func(const void *a, const void *b) { |
965 | const pid_t *p = a, *q = b; | |
966 | ||
967 | /* Suitable for usage in qsort() */ | |
968 | ||
969 | if (*p < *q) | |
970 | return -1; | |
971 | if (*p > *q) | |
972 | return 1; | |
973 | return 0; | |
974 | } | |
975 | ||
7f452159 LP |
976 | int ioprio_parse_priority(const char *s, int *ret) { |
977 | int i, r; | |
978 | ||
979 | assert(s); | |
980 | assert(ret); | |
981 | ||
982 | r = safe_atoi(s, &i); | |
983 | if (r < 0) | |
984 | return r; | |
985 | ||
986 | if (!ioprio_priority_is_valid(i)) | |
987 | return -EINVAL; | |
988 | ||
989 | *ret = i; | |
990 | return 0; | |
991 | } | |
992 | ||
5c30a6d2 LP |
993 | /* The cached PID, possible values: |
994 | * | |
995 | * == UNSET [0] → cache not initialized yet | |
996 | * == BUSY [-1] → some thread is initializing it at the moment | |
997 | * any other → the cached PID | |
998 | */ | |
999 | ||
1000 | #define CACHED_PID_UNSET ((pid_t) 0) | |
1001 | #define CACHED_PID_BUSY ((pid_t) -1) | |
1002 | ||
1003 | static pid_t cached_pid = CACHED_PID_UNSET; | |
1004 | ||
1005 | static void reset_cached_pid(void) { | |
1006 | /* Invoked in the child after a fork(), i.e. at the first moment the PID changed */ | |
1007 | cached_pid = CACHED_PID_UNSET; | |
1008 | } | |
1009 | ||
1010 | /* We use glibc __register_atfork() + __dso_handle directly here, as they are not included in the glibc | |
1011 | * headers. __register_atfork() is mostly equivalent to pthread_atfork(), but doesn't require us to link against | |
1012 | * libpthread, as it is part of glibc anyway. */ | |
1013 | extern int __register_atfork(void (*prepare) (void), void (*parent) (void), void (*child) (void), void * __dso_handle); | |
1014 | extern void* __dso_handle __attribute__ ((__weak__)); | |
1015 | ||
1016 | pid_t getpid_cached(void) { | |
1017 | pid_t current_value; | |
1018 | ||
1019 | /* getpid_cached() is much like getpid(), but caches the value in local memory, to avoid having to invoke a | |
1020 | * system call each time. This restores glibc behaviour from before 2.24, when getpid() was unconditionally | |
1021 | * cached. Starting with 2.24 getpid() started to become prohibitively expensive when used for detecting when | |
1022 | * objects were used across fork()s. With this caching the old behaviour is somewhat restored. | |
1023 | * | |
1024 | * https://bugzilla.redhat.com/show_bug.cgi?id=1443976 | |
a4041e4f | 1025 | * https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=c579f48edba88380635ab98cb612030e3ed8691e |
5c30a6d2 LP |
1026 | */ |
1027 | ||
1028 | current_value = __sync_val_compare_and_swap(&cached_pid, CACHED_PID_UNSET, CACHED_PID_BUSY); | |
1029 | ||
1030 | switch (current_value) { | |
1031 | ||
1032 | case CACHED_PID_UNSET: { /* Not initialized yet, then do so now */ | |
1033 | pid_t new_pid; | |
1034 | ||
1035 | new_pid = getpid(); | |
1036 | ||
1037 | if (__register_atfork(NULL, NULL, reset_cached_pid, __dso_handle) != 0) { | |
1038 | /* OOM? Let's try again later */ | |
1039 | cached_pid = CACHED_PID_UNSET; | |
1040 | return new_pid; | |
1041 | } | |
1042 | ||
1043 | cached_pid = new_pid; | |
1044 | return new_pid; | |
1045 | } | |
1046 | ||
1047 | case CACHED_PID_BUSY: /* Somebody else is currently initializing */ | |
1048 | return getpid(); | |
1049 | ||
1050 | default: /* Properly initialized */ | |
1051 | return current_value; | |
1052 | } | |
1053 | } | |
1054 | ||
7b3e062c LP |
1055 | static const char *const ioprio_class_table[] = { |
1056 | [IOPRIO_CLASS_NONE] = "none", | |
1057 | [IOPRIO_CLASS_RT] = "realtime", | |
1058 | [IOPRIO_CLASS_BE] = "best-effort", | |
1059 | [IOPRIO_CLASS_IDLE] = "idle" | |
1060 | }; | |
1061 | ||
1062 | DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(ioprio_class, int, INT_MAX); | |
1063 | ||
1064 | static const char *const sigchld_code_table[] = { | |
1065 | [CLD_EXITED] = "exited", | |
1066 | [CLD_KILLED] = "killed", | |
1067 | [CLD_DUMPED] = "dumped", | |
1068 | [CLD_TRAPPED] = "trapped", | |
1069 | [CLD_STOPPED] = "stopped", | |
1070 | [CLD_CONTINUED] = "continued", | |
1071 | }; | |
1072 | ||
1073 | DEFINE_STRING_TABLE_LOOKUP(sigchld_code, int); | |
1074 | ||
1075 | static const char* const sched_policy_table[] = { | |
1076 | [SCHED_OTHER] = "other", | |
1077 | [SCHED_BATCH] = "batch", | |
1078 | [SCHED_IDLE] = "idle", | |
1079 | [SCHED_FIFO] = "fifo", | |
1080 | [SCHED_RR] = "rr" | |
1081 | }; | |
1082 | ||
1083 | DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(sched_policy, int, INT_MAX); |