]>
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> |
7b3e062c | 30 | #include <sys/personality.h> |
405f8907 | 31 | #include <sys/prctl.h> |
4f5dd394 LP |
32 | #include <sys/types.h> |
33 | #include <sys/wait.h> | |
11c3a366 | 34 | #include <syslog.h> |
4f5dd394 | 35 | #include <unistd.h> |
dcadc967 EV |
36 | #ifdef HAVE_VALGRIND_VALGRIND_H |
37 | #include <valgrind/valgrind.h> | |
38 | #endif | |
0b452006 | 39 | |
b5efdb8a | 40 | #include "alloc-util.h" |
6e5f1b57 | 41 | #include "architecture.h" |
4f5dd394 | 42 | #include "escape.h" |
3ffd4af2 | 43 | #include "fd-util.h" |
0b452006 | 44 | #include "fileio.h" |
f4f15635 | 45 | #include "fs-util.h" |
7b3e062c | 46 | #include "ioprio.h" |
0b452006 | 47 | #include "log.h" |
11c3a366 TA |
48 | #include "macro.h" |
49 | #include "missing.h" | |
93cc7779 TA |
50 | #include "process-util.h" |
51 | #include "signal-util.h" | |
1359fffa | 52 | #include "stat-util.h" |
7b3e062c | 53 | #include "string-table.h" |
07630cea | 54 | #include "string-util.h" |
b1d4f8e1 | 55 | #include "user-util.h" |
4f5dd394 | 56 | #include "util.h" |
0b452006 RC |
57 | |
58 | int get_process_state(pid_t pid) { | |
59 | const char *p; | |
60 | char state; | |
61 | int r; | |
62 | _cleanup_free_ char *line = NULL; | |
63 | ||
64 | assert(pid >= 0); | |
65 | ||
66 | p = procfs_file_alloca(pid, "stat"); | |
a644184a | 67 | |
0b452006 | 68 | r = read_one_line_file(p, &line); |
a644184a LP |
69 | if (r == -ENOENT) |
70 | return -ESRCH; | |
0b452006 RC |
71 | if (r < 0) |
72 | return r; | |
73 | ||
74 | p = strrchr(line, ')'); | |
75 | if (!p) | |
76 | return -EIO; | |
77 | ||
78 | p++; | |
79 | ||
80 | if (sscanf(p, " %c", &state) != 1) | |
81 | return -EIO; | |
82 | ||
83 | return (unsigned char) state; | |
84 | } | |
85 | ||
86 | int get_process_comm(pid_t pid, char **name) { | |
87 | const char *p; | |
88 | int r; | |
89 | ||
90 | assert(name); | |
91 | assert(pid >= 0); | |
92 | ||
93 | p = procfs_file_alloca(pid, "comm"); | |
94 | ||
95 | r = read_one_line_file(p, name); | |
96 | if (r == -ENOENT) | |
97 | return -ESRCH; | |
98 | ||
99 | return r; | |
100 | } | |
101 | ||
102 | int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line) { | |
103 | _cleanup_fclose_ FILE *f = NULL; | |
104 | char *r = NULL, *k; | |
105 | const char *p; | |
106 | int c; | |
107 | ||
108 | assert(line); | |
109 | assert(pid >= 0); | |
110 | ||
111 | p = procfs_file_alloca(pid, "cmdline"); | |
112 | ||
113 | f = fopen(p, "re"); | |
a644184a LP |
114 | if (!f) { |
115 | if (errno == ENOENT) | |
116 | return -ESRCH; | |
0b452006 | 117 | return -errno; |
a644184a | 118 | } |
0b452006 RC |
119 | |
120 | if (max_length == 0) { | |
121 | size_t len = 0, allocated = 0; | |
122 | ||
123 | while ((c = getc(f)) != EOF) { | |
124 | ||
125 | if (!GREEDY_REALLOC(r, allocated, len+2)) { | |
126 | free(r); | |
127 | return -ENOMEM; | |
128 | } | |
129 | ||
130 | r[len++] = isprint(c) ? c : ' '; | |
131 | } | |
132 | ||
133 | if (len > 0) | |
134 | r[len-1] = 0; | |
135 | ||
136 | } else { | |
137 | bool space = false; | |
138 | size_t left; | |
139 | ||
140 | r = new(char, max_length); | |
141 | if (!r) | |
142 | return -ENOMEM; | |
143 | ||
144 | k = r; | |
145 | left = max_length; | |
146 | while ((c = getc(f)) != EOF) { | |
147 | ||
148 | if (isprint(c)) { | |
149 | if (space) { | |
150 | if (left <= 4) | |
151 | break; | |
152 | ||
153 | *(k++) = ' '; | |
154 | left--; | |
155 | space = false; | |
156 | } | |
157 | ||
158 | if (left <= 4) | |
159 | break; | |
160 | ||
161 | *(k++) = (char) c; | |
162 | left--; | |
163 | } else | |
164 | space = true; | |
165 | } | |
166 | ||
167 | if (left <= 4) { | |
168 | size_t n = MIN(left-1, 3U); | |
169 | memcpy(k, "...", n); | |
170 | k[n] = 0; | |
171 | } else | |
172 | *k = 0; | |
173 | } | |
174 | ||
175 | /* Kernel threads have no argv[] */ | |
176 | if (isempty(r)) { | |
177 | _cleanup_free_ char *t = NULL; | |
178 | int h; | |
179 | ||
180 | free(r); | |
181 | ||
182 | if (!comm_fallback) | |
183 | return -ENOENT; | |
184 | ||
185 | h = get_process_comm(pid, &t); | |
186 | if (h < 0) | |
187 | return h; | |
188 | ||
189 | r = strjoin("[", t, "]", NULL); | |
190 | if (!r) | |
191 | return -ENOMEM; | |
192 | } | |
193 | ||
194 | *line = r; | |
195 | return 0; | |
196 | } | |
197 | ||
405f8907 LP |
198 | void rename_process(const char name[8]) { |
199 | assert(name); | |
200 | ||
201 | /* This is a like a poor man's setproctitle(). It changes the | |
202 | * comm field, argv[0], and also the glibc's internally used | |
203 | * name of the process. For the first one a limit of 16 chars | |
204 | * applies, to the second one usually one of 10 (i.e. length | |
205 | * of "/sbin/init"), to the third one one of 7 (i.e. length of | |
206 | * "systemd"). If you pass a longer string it will be | |
207 | * truncated */ | |
208 | ||
79d62972 | 209 | (void) prctl(PR_SET_NAME, name); |
405f8907 LP |
210 | |
211 | if (program_invocation_name) | |
212 | strncpy(program_invocation_name, name, strlen(program_invocation_name)); | |
213 | ||
214 | if (saved_argc > 0) { | |
215 | int i; | |
216 | ||
217 | if (saved_argv[0]) | |
218 | strncpy(saved_argv[0], name, strlen(saved_argv[0])); | |
219 | ||
220 | for (i = 1; i < saved_argc; i++) { | |
221 | if (!saved_argv[i]) | |
222 | break; | |
223 | ||
224 | memzero(saved_argv[i], strlen(saved_argv[i])); | |
225 | } | |
226 | } | |
227 | } | |
228 | ||
0b452006 RC |
229 | int is_kernel_thread(pid_t pid) { |
230 | const char *p; | |
231 | size_t count; | |
232 | char c; | |
233 | bool eof; | |
234 | FILE *f; | |
235 | ||
a6149b93 | 236 | if (pid == 0 || pid == 1) /* pid 1, and we ourselves certainly aren't a kernel thread */ |
0b452006 RC |
237 | return 0; |
238 | ||
a6149b93 | 239 | assert(pid > 1); |
0b452006 RC |
240 | |
241 | p = procfs_file_alloca(pid, "cmdline"); | |
242 | f = fopen(p, "re"); | |
a644184a LP |
243 | if (!f) { |
244 | if (errno == ENOENT) | |
245 | return -ESRCH; | |
0b452006 | 246 | return -errno; |
a644184a | 247 | } |
0b452006 RC |
248 | |
249 | count = fread(&c, 1, 1, f); | |
250 | eof = feof(f); | |
251 | fclose(f); | |
252 | ||
253 | /* Kernel threads have an empty cmdline */ | |
254 | ||
255 | if (count <= 0) | |
256 | return eof ? 1 : -errno; | |
257 | ||
258 | return 0; | |
259 | } | |
260 | ||
261 | int get_process_capeff(pid_t pid, char **capeff) { | |
262 | const char *p; | |
a644184a | 263 | int r; |
0b452006 RC |
264 | |
265 | assert(capeff); | |
266 | assert(pid >= 0); | |
267 | ||
268 | p = procfs_file_alloca(pid, "status"); | |
269 | ||
c4cd1d4d | 270 | r = get_proc_field(p, "CapEff", WHITESPACE, capeff); |
a644184a LP |
271 | if (r == -ENOENT) |
272 | return -ESRCH; | |
273 | ||
274 | return r; | |
0b452006 RC |
275 | } |
276 | ||
277 | static int get_process_link_contents(const char *proc_file, char **name) { | |
278 | int r; | |
279 | ||
280 | assert(proc_file); | |
281 | assert(name); | |
282 | ||
283 | r = readlink_malloc(proc_file, name); | |
a644184a LP |
284 | if (r == -ENOENT) |
285 | return -ESRCH; | |
0b452006 | 286 | if (r < 0) |
a644184a | 287 | return r; |
0b452006 RC |
288 | |
289 | return 0; | |
290 | } | |
291 | ||
292 | int get_process_exe(pid_t pid, char **name) { | |
293 | const char *p; | |
294 | char *d; | |
295 | int r; | |
296 | ||
297 | assert(pid >= 0); | |
298 | ||
299 | p = procfs_file_alloca(pid, "exe"); | |
300 | r = get_process_link_contents(p, name); | |
301 | if (r < 0) | |
302 | return r; | |
303 | ||
304 | d = endswith(*name, " (deleted)"); | |
305 | if (d) | |
306 | *d = '\0'; | |
307 | ||
308 | return 0; | |
309 | } | |
310 | ||
311 | static int get_process_id(pid_t pid, const char *field, uid_t *uid) { | |
312 | _cleanup_fclose_ FILE *f = NULL; | |
313 | char line[LINE_MAX]; | |
314 | const char *p; | |
315 | ||
316 | assert(field); | |
317 | assert(uid); | |
318 | ||
319 | if (pid == 0) | |
320 | return getuid(); | |
321 | ||
322 | p = procfs_file_alloca(pid, "status"); | |
323 | f = fopen(p, "re"); | |
a644184a LP |
324 | if (!f) { |
325 | if (errno == ENOENT) | |
326 | return -ESRCH; | |
0b452006 | 327 | return -errno; |
a644184a | 328 | } |
0b452006 RC |
329 | |
330 | FOREACH_LINE(line, f, return -errno) { | |
331 | char *l; | |
332 | ||
333 | l = strstrip(line); | |
334 | ||
335 | if (startswith(l, field)) { | |
336 | l += strlen(field); | |
337 | l += strspn(l, WHITESPACE); | |
338 | ||
339 | l[strcspn(l, WHITESPACE)] = 0; | |
340 | ||
341 | return parse_uid(l, uid); | |
342 | } | |
343 | } | |
344 | ||
345 | return -EIO; | |
346 | } | |
347 | ||
348 | int get_process_uid(pid_t pid, uid_t *uid) { | |
349 | return get_process_id(pid, "Uid:", uid); | |
350 | } | |
351 | ||
352 | int get_process_gid(pid_t pid, gid_t *gid) { | |
353 | assert_cc(sizeof(uid_t) == sizeof(gid_t)); | |
354 | return get_process_id(pid, "Gid:", gid); | |
355 | } | |
356 | ||
357 | int get_process_cwd(pid_t pid, char **cwd) { | |
358 | const char *p; | |
359 | ||
360 | assert(pid >= 0); | |
361 | ||
362 | p = procfs_file_alloca(pid, "cwd"); | |
363 | ||
364 | return get_process_link_contents(p, cwd); | |
365 | } | |
366 | ||
367 | int get_process_root(pid_t pid, char **root) { | |
368 | const char *p; | |
369 | ||
370 | assert(pid >= 0); | |
371 | ||
372 | p = procfs_file_alloca(pid, "root"); | |
373 | ||
374 | return get_process_link_contents(p, root); | |
375 | } | |
376 | ||
377 | int get_process_environ(pid_t pid, char **env) { | |
378 | _cleanup_fclose_ FILE *f = NULL; | |
379 | _cleanup_free_ char *outcome = NULL; | |
380 | int c; | |
381 | const char *p; | |
382 | size_t allocated = 0, sz = 0; | |
383 | ||
384 | assert(pid >= 0); | |
385 | assert(env); | |
386 | ||
387 | p = procfs_file_alloca(pid, "environ"); | |
388 | ||
389 | f = fopen(p, "re"); | |
a644184a LP |
390 | if (!f) { |
391 | if (errno == ENOENT) | |
392 | return -ESRCH; | |
0b452006 | 393 | return -errno; |
a644184a | 394 | } |
0b452006 RC |
395 | |
396 | while ((c = fgetc(f)) != EOF) { | |
397 | if (!GREEDY_REALLOC(outcome, allocated, sz + 5)) | |
398 | return -ENOMEM; | |
399 | ||
400 | if (c == '\0') | |
401 | outcome[sz++] = '\n'; | |
402 | else | |
403 | sz += cescape_char(c, outcome + sz); | |
404 | } | |
405 | ||
03c55bc0 LP |
406 | if (!outcome) { |
407 | outcome = strdup(""); | |
408 | if (!outcome) | |
409 | return -ENOMEM; | |
410 | } else | |
411 | outcome[sz] = '\0'; | |
de8763b6 | 412 | |
0b452006 RC |
413 | *env = outcome; |
414 | outcome = NULL; | |
415 | ||
416 | return 0; | |
417 | } | |
418 | ||
6bc73acb | 419 | int get_process_ppid(pid_t pid, pid_t *_ppid) { |
0b452006 RC |
420 | int r; |
421 | _cleanup_free_ char *line = NULL; | |
422 | long unsigned ppid; | |
423 | const char *p; | |
424 | ||
425 | assert(pid >= 0); | |
426 | assert(_ppid); | |
427 | ||
428 | if (pid == 0) { | |
429 | *_ppid = getppid(); | |
430 | return 0; | |
431 | } | |
432 | ||
433 | p = procfs_file_alloca(pid, "stat"); | |
434 | r = read_one_line_file(p, &line); | |
a644184a LP |
435 | if (r == -ENOENT) |
436 | return -ESRCH; | |
0b452006 RC |
437 | if (r < 0) |
438 | return r; | |
439 | ||
440 | /* Let's skip the pid and comm fields. The latter is enclosed | |
441 | * in () but does not escape any () in its value, so let's | |
442 | * skip over it manually */ | |
443 | ||
444 | p = strrchr(line, ')'); | |
445 | if (!p) | |
446 | return -EIO; | |
447 | ||
448 | p++; | |
449 | ||
450 | if (sscanf(p, " " | |
451 | "%*c " /* state */ | |
452 | "%lu ", /* ppid */ | |
453 | &ppid) != 1) | |
454 | return -EIO; | |
455 | ||
456 | if ((long unsigned) (pid_t) ppid != ppid) | |
457 | return -ERANGE; | |
458 | ||
459 | *_ppid = (pid_t) ppid; | |
460 | ||
461 | return 0; | |
462 | } | |
463 | ||
464 | int wait_for_terminate(pid_t pid, siginfo_t *status) { | |
465 | siginfo_t dummy; | |
466 | ||
467 | assert(pid >= 1); | |
468 | ||
469 | if (!status) | |
470 | status = &dummy; | |
471 | ||
472 | for (;;) { | |
473 | zero(*status); | |
474 | ||
475 | if (waitid(P_PID, pid, status, WEXITED) < 0) { | |
476 | ||
477 | if (errno == EINTR) | |
478 | continue; | |
479 | ||
480 | return -errno; | |
481 | } | |
482 | ||
483 | return 0; | |
484 | } | |
485 | } | |
486 | ||
487 | /* | |
488 | * Return values: | |
489 | * < 0 : wait_for_terminate() failed to get the state of the | |
490 | * process, the process was terminated by a signal, or | |
491 | * failed for an unknown reason. | |
492 | * >=0 : The process terminated normally, and its exit code is | |
493 | * returned. | |
494 | * | |
495 | * That is, success is indicated by a return value of zero, and an | |
496 | * error is indicated by a non-zero value. | |
497 | * | |
498 | * A warning is emitted if the process terminates abnormally, | |
499 | * and also if it returns non-zero unless check_exit_code is true. | |
500 | */ | |
501 | int wait_for_terminate_and_warn(const char *name, pid_t pid, bool check_exit_code) { | |
502 | int r; | |
503 | siginfo_t status; | |
504 | ||
505 | assert(name); | |
506 | assert(pid > 1); | |
507 | ||
508 | r = wait_for_terminate(pid, &status); | |
509 | if (r < 0) | |
510 | return log_warning_errno(r, "Failed to wait for %s: %m", name); | |
511 | ||
512 | if (status.si_code == CLD_EXITED) { | |
513 | if (status.si_status != 0) | |
514 | log_full(check_exit_code ? LOG_WARNING : LOG_DEBUG, | |
515 | "%s failed with error code %i.", name, status.si_status); | |
516 | else | |
517 | log_debug("%s succeeded.", name); | |
518 | ||
519 | return status.si_status; | |
520 | } else if (status.si_code == CLD_KILLED || | |
521 | status.si_code == CLD_DUMPED) { | |
522 | ||
523 | log_warning("%s terminated by signal %s.", name, signal_to_string(status.si_status)); | |
524 | return -EPROTO; | |
525 | } | |
526 | ||
527 | log_warning("%s failed due to unknown reason.", name); | |
528 | return -EPROTO; | |
529 | } | |
530 | ||
4d0d3d41 LP |
531 | void sigkill_wait(pid_t *pid) { |
532 | if (!pid) | |
533 | return; | |
534 | if (*pid <= 1) | |
535 | return; | |
536 | ||
537 | if (kill(*pid, SIGKILL) > 0) | |
538 | (void) wait_for_terminate(*pid, NULL); | |
539 | } | |
540 | ||
0b452006 RC |
541 | int kill_and_sigcont(pid_t pid, int sig) { |
542 | int r; | |
543 | ||
544 | r = kill(pid, sig) < 0 ? -errno : 0; | |
545 | ||
546 | if (r >= 0) | |
547 | kill(pid, SIGCONT); | |
548 | ||
549 | return r; | |
550 | } | |
551 | ||
552 | int getenv_for_pid(pid_t pid, const char *field, char **_value) { | |
553 | _cleanup_fclose_ FILE *f = NULL; | |
554 | char *value = NULL; | |
555 | int r; | |
556 | bool done = false; | |
557 | size_t l; | |
558 | const char *path; | |
559 | ||
560 | assert(pid >= 0); | |
561 | assert(field); | |
562 | assert(_value); | |
563 | ||
564 | path = procfs_file_alloca(pid, "environ"); | |
565 | ||
566 | f = fopen(path, "re"); | |
a644184a LP |
567 | if (!f) { |
568 | if (errno == ENOENT) | |
569 | return -ESRCH; | |
0b452006 | 570 | return -errno; |
a644184a | 571 | } |
0b452006 RC |
572 | |
573 | l = strlen(field); | |
574 | r = 0; | |
575 | ||
576 | do { | |
577 | char line[LINE_MAX]; | |
578 | unsigned i; | |
579 | ||
580 | for (i = 0; i < sizeof(line)-1; i++) { | |
581 | int c; | |
582 | ||
583 | c = getc(f); | |
584 | if (_unlikely_(c == EOF)) { | |
585 | done = true; | |
586 | break; | |
587 | } else if (c == 0) | |
588 | break; | |
589 | ||
590 | line[i] = c; | |
591 | } | |
592 | line[i] = 0; | |
593 | ||
594 | if (memcmp(line, field, l) == 0 && line[l] == '=') { | |
595 | value = strdup(line + l + 1); | |
596 | if (!value) | |
597 | return -ENOMEM; | |
598 | ||
599 | r = 1; | |
600 | break; | |
601 | } | |
602 | ||
603 | } while (!done); | |
604 | ||
605 | *_value = value; | |
606 | return r; | |
607 | } | |
608 | ||
609 | bool pid_is_unwaited(pid_t pid) { | |
610 | /* Checks whether a PID is still valid at all, including a zombie */ | |
611 | ||
5fd9b2c5 | 612 | if (pid < 0) |
0b452006 RC |
613 | return false; |
614 | ||
5fd9b2c5 LP |
615 | if (pid <= 1) /* If we or PID 1 would be dead and have been waited for, this code would not be running */ |
616 | return true; | |
617 | ||
0b452006 RC |
618 | if (kill(pid, 0) >= 0) |
619 | return true; | |
620 | ||
621 | return errno != ESRCH; | |
622 | } | |
623 | ||
624 | bool pid_is_alive(pid_t pid) { | |
625 | int r; | |
626 | ||
627 | /* Checks whether a PID is still valid and not a zombie */ | |
628 | ||
5fd9b2c5 | 629 | if (pid < 0) |
0b452006 RC |
630 | return false; |
631 | ||
5fd9b2c5 LP |
632 | if (pid <= 1) /* If we or PID 1 would be a zombie, this code would not be running */ |
633 | return true; | |
634 | ||
0b452006 | 635 | r = get_process_state(pid); |
a644184a | 636 | if (r == -ESRCH || r == 'Z') |
0b452006 RC |
637 | return false; |
638 | ||
639 | return true; | |
640 | } | |
d4510856 | 641 | |
1359fffa MS |
642 | int pid_from_same_root_fs(pid_t pid) { |
643 | const char *root; | |
644 | ||
645 | if (pid < 0) | |
646 | return 0; | |
647 | ||
648 | root = procfs_file_alloca(pid, "root"); | |
649 | ||
650 | return files_same(root, "/proc/1/root"); | |
651 | } | |
652 | ||
d4510856 LP |
653 | bool is_main_thread(void) { |
654 | static thread_local int cached = 0; | |
655 | ||
656 | if (_unlikely_(cached == 0)) | |
657 | cached = getpid() == gettid() ? 1 : -1; | |
658 | ||
659 | return cached > 0; | |
660 | } | |
7b3e062c LP |
661 | |
662 | noreturn void freeze(void) { | |
663 | ||
664 | /* Make sure nobody waits for us on a socket anymore */ | |
665 | close_all_fds(NULL, 0); | |
666 | ||
667 | sync(); | |
668 | ||
669 | for (;;) | |
670 | pause(); | |
671 | } | |
672 | ||
673 | bool oom_score_adjust_is_valid(int oa) { | |
674 | return oa >= OOM_SCORE_ADJ_MIN && oa <= OOM_SCORE_ADJ_MAX; | |
675 | } | |
676 | ||
677 | unsigned long personality_from_string(const char *p) { | |
6e5f1b57 | 678 | int architecture; |
7b3e062c | 679 | |
0c0fea07 LP |
680 | if (!p) |
681 | return PERSONALITY_INVALID; | |
682 | ||
6e5f1b57 LP |
683 | /* Parse a personality specifier. We use our own identifiers that indicate specific ABIs, rather than just |
684 | * hints regarding the register size, since we want to keep things open for multiple locally supported ABIs for | |
685 | * the same register size. */ | |
686 | ||
687 | architecture = architecture_from_string(p); | |
688 | if (architecture < 0) | |
689 | return PERSONALITY_INVALID; | |
7b3e062c | 690 | |
0c0fea07 | 691 | if (architecture == native_architecture()) |
7b3e062c | 692 | return PER_LINUX; |
0c0fea07 LP |
693 | #ifdef SECONDARY_ARCHITECTURE |
694 | if (architecture == SECONDARY_ARCHITECTURE) | |
f2d1736c | 695 | return PER_LINUX32; |
7b3e062c LP |
696 | #endif |
697 | ||
698 | return PERSONALITY_INVALID; | |
699 | } | |
700 | ||
701 | const char* personality_to_string(unsigned long p) { | |
6e5f1b57 | 702 | int architecture = _ARCHITECTURE_INVALID; |
7b3e062c | 703 | |
7b3e062c | 704 | if (p == PER_LINUX) |
0c0fea07 LP |
705 | architecture = native_architecture(); |
706 | #ifdef SECONDARY_ARCHITECTURE | |
6e5f1b57 | 707 | else if (p == PER_LINUX32) |
0c0fea07 | 708 | architecture = SECONDARY_ARCHITECTURE; |
7b3e062c LP |
709 | #endif |
710 | ||
6e5f1b57 LP |
711 | if (architecture < 0) |
712 | return NULL; | |
713 | ||
714 | return architecture_to_string(architecture); | |
7b3e062c LP |
715 | } |
716 | ||
dcadc967 EV |
717 | void valgrind_summary_hack(void) { |
718 | #ifdef HAVE_VALGRIND_VALGRIND_H | |
719 | if (getpid() == 1 && RUNNING_ON_VALGRIND) { | |
720 | pid_t pid; | |
721 | pid = raw_clone(SIGCHLD, NULL); | |
722 | if (pid < 0) | |
723 | log_emergency_errno(errno, "Failed to fork off valgrind helper: %m"); | |
724 | else if (pid == 0) | |
725 | exit(EXIT_SUCCESS); | |
726 | else { | |
727 | log_info("Spawned valgrind helper as PID "PID_FMT".", pid); | |
728 | (void) wait_for_terminate(pid, NULL); | |
729 | } | |
730 | } | |
731 | #endif | |
732 | } | |
733 | ||
7b3e062c LP |
734 | static const char *const ioprio_class_table[] = { |
735 | [IOPRIO_CLASS_NONE] = "none", | |
736 | [IOPRIO_CLASS_RT] = "realtime", | |
737 | [IOPRIO_CLASS_BE] = "best-effort", | |
738 | [IOPRIO_CLASS_IDLE] = "idle" | |
739 | }; | |
740 | ||
741 | DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(ioprio_class, int, INT_MAX); | |
742 | ||
743 | static const char *const sigchld_code_table[] = { | |
744 | [CLD_EXITED] = "exited", | |
745 | [CLD_KILLED] = "killed", | |
746 | [CLD_DUMPED] = "dumped", | |
747 | [CLD_TRAPPED] = "trapped", | |
748 | [CLD_STOPPED] = "stopped", | |
749 | [CLD_CONTINUED] = "continued", | |
750 | }; | |
751 | ||
752 | DEFINE_STRING_TABLE_LOOKUP(sigchld_code, int); | |
753 | ||
754 | static const char* const sched_policy_table[] = { | |
755 | [SCHED_OTHER] = "other", | |
756 | [SCHED_BATCH] = "batch", | |
757 | [SCHED_IDLE] = "idle", | |
758 | [SCHED_FIFO] = "fifo", | |
759 | [SCHED_RR] = "rr" | |
760 | }; | |
761 | ||
762 | DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(sched_policy, int, INT_MAX); |