]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/process-util.c
Merge pull request #5409 from keszybz/test-env-util-memleak
[thirdparty/systemd.git] / src / basic / process-util.c
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
20 #include <ctype.h>
21 #include <errno.h>
22 #include <limits.h>
23 #include <linux/oom.h>
24 #include <sched.h>
25 #include <signal.h>
26 #include <stdbool.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/mman.h>
31 #include <sys/personality.h>
32 #include <sys/prctl.h>
33 #include <sys/types.h>
34 #include <sys/wait.h>
35 #include <syslog.h>
36 #include <unistd.h>
37 #ifdef HAVE_VALGRIND_VALGRIND_H
38 #include <valgrind/valgrind.h>
39 #endif
40
41 #include "alloc-util.h"
42 #include "architecture.h"
43 #include "escape.h"
44 #include "fd-util.h"
45 #include "fileio.h"
46 #include "fs-util.h"
47 #include "ioprio.h"
48 #include "log.h"
49 #include "macro.h"
50 #include "missing.h"
51 #include "process-util.h"
52 #include "raw-clone.h"
53 #include "signal-util.h"
54 #include "stat-util.h"
55 #include "string-table.h"
56 #include "string-util.h"
57 #include "user-util.h"
58 #include "util.h"
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");
69
70 r = read_one_line_file(p, &line);
71 if (r == -ENOENT)
72 return -ESRCH;
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;
106 bool space = false;
107 char *k, *ans = NULL;
108 const char *p;
109 int c;
110
111 assert(line);
112 assert(pid >= 0);
113
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
121 * comm_fallback is false). Returns 0 and sets *line otherwise. */
122
123 p = procfs_file_alloca(pid, "cmdline");
124
125 f = fopen(p, "re");
126 if (!f) {
127 if (errno == ENOENT)
128 return -ESRCH;
129 return -errno;
130 }
131
132 if (max_length == 1) {
133
134 /* If there's only room for one byte, return the empty string */
135 ans = new0(char, 1);
136 if (!ans)
137 return -ENOMEM;
138
139 *line = ans;
140 return 0;
141
142 } else if (max_length == 0) {
143 size_t len = 0, allocated = 0;
144
145 while ((c = getc(f)) != EOF) {
146
147 if (!GREEDY_REALLOC(ans, allocated, len+3)) {
148 free(ans);
149 return -ENOMEM;
150 }
151
152 if (isprint(c)) {
153 if (space) {
154 ans[len++] = ' ';
155 space = false;
156 }
157
158 ans[len++] = c;
159 } else if (len > 0)
160 space = true;
161 }
162
163 if (len > 0)
164 ans[len] = '\0';
165 else
166 ans = mfree(ans);
167
168 } else {
169 bool dotdotdot = false;
170 size_t left;
171
172 ans = new(char, max_length);
173 if (!ans)
174 return -ENOMEM;
175
176 k = ans;
177 left = max_length;
178 while ((c = getc(f)) != EOF) {
179
180 if (isprint(c)) {
181
182 if (space) {
183 if (left <= 2) {
184 dotdotdot = true;
185 break;
186 }
187
188 *(k++) = ' ';
189 left--;
190 space = false;
191 }
192
193 if (left <= 1) {
194 dotdotdot = true;
195 break;
196 }
197
198 *(k++) = (char) c;
199 left--;
200 } else if (k > ans)
201 space = true;
202 }
203
204 if (dotdotdot) {
205 if (max_length <= 4) {
206 k = ans;
207 left = max_length;
208 } else {
209 k = ans + max_length - 4;
210 left = 4;
211
212 /* Eat up final spaces */
213 while (k > ans && isspace(k[-1])) {
214 k--;
215 left++;
216 }
217 }
218
219 strncpy(k, "...", left-1);
220 k[left-1] = 0;
221 } else
222 *k = 0;
223 }
224
225 /* Kernel threads have no argv[] */
226 if (isempty(ans)) {
227 _cleanup_free_ char *t = NULL;
228 int h;
229
230 free(ans);
231
232 if (!comm_fallback)
233 return -ENOENT;
234
235 h = get_process_comm(pid, &t);
236 if (h < 0)
237 return h;
238
239 if (max_length == 0)
240 ans = strjoin("[", t, "]");
241 else {
242 size_t l;
243
244 l = strlen(t);
245
246 if (l + 3 <= max_length)
247 ans = strjoin("[", t, "]");
248 else if (max_length <= 6) {
249
250 ans = new(char, max_length);
251 if (!ans)
252 return -ENOMEM;
253
254 memcpy(ans, "[...]", max_length-1);
255 ans[max_length-1] = 0;
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
267 ans = strjoin("[", t, "...]");
268 }
269 }
270 if (!ans)
271 return -ENOMEM;
272 }
273
274 *line = ans;
275 return 0;
276 }
277
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 */
294
295 l = strlen(name);
296
297 /* First step, change the comm field. */
298 (void) prctl(PR_SET_NAME, name);
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
314 * the end. This is the best option for changing /proc/self/cmdline.*/
315 if (mm_size < l+1) {
316 size_t nn_size;
317 char *nn;
318
319 /* Let's not bother with this if we don't have euid == 0. Strictly speaking if people do weird stuff
320 * with capabilities this could work even for euid != 0, but our own code generally doesn't do that,
321 * hence let's use this as quick bypass check, to avoid calling mmap() if PR_SET_MM_ARG_START fails
322 * with EPERM later on anyway. After all geteuid() is dead cheap to call, but mmap() is not. */
323 if (geteuid() != 0) {
324 log_debug("Skipping PR_SET_MM_ARG_START, as we don't have privileges.");
325 goto use_saved_argv;
326 }
327
328 nn_size = PAGE_ALIGN(l+1);
329 nn = mmap(NULL, nn_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
330 if (nn == MAP_FAILED) {
331 log_debug_errno(errno, "mmap() failed: %m");
332 goto use_saved_argv;
333 }
334
335 strncpy(nn, name, nn_size);
336
337 /* Now, let's tell the kernel about this new memory */
338 if (prctl(PR_SET_MM, PR_SET_MM_ARG_START, (unsigned long) nn, 0, 0) < 0) {
339 log_debug_errno(errno, "PR_SET_MM_ARG_START failed, proceeding without: %m");
340 (void) munmap(nn, nn_size);
341 goto use_saved_argv;
342 }
343
344 /* And update the end pointer to the new end, too. If this fails, we don't really know what to do, it's
345 * pretty unlikely that we can rollback, hence we'll just accept the failure, and continue. */
346 if (prctl(PR_SET_MM, PR_SET_MM_ARG_END, (unsigned long) nn + l + 1, 0, 0) < 0)
347 log_debug_errno(errno, "PR_SET_MM_ARG_END failed, proceeding without: %m");
348
349 if (mm)
350 (void) munmap(mm, mm_size);
351
352 mm = nn;
353 mm_size = nn_size;
354 } else
355 strncpy(mm, name, mm_size);
356
357 use_saved_argv:
358 /* Fourth step: in all cases we'll also update the original argv[], so that our own code gets it right too if
359 * it still looks here */
360
361 if (saved_argc > 0) {
362 int i;
363
364 if (saved_argv[0]) {
365 size_t k;
366
367 k = strlen(saved_argv[0]);
368 strncpy(saved_argv[0], name, k);
369 if (l > k)
370 truncated = true;
371 }
372
373 for (i = 1; i < saved_argc; i++) {
374 if (!saved_argv[i])
375 break;
376
377 memzero(saved_argv[i], strlen(saved_argv[i]));
378 }
379 }
380
381 return !truncated;
382 }
383
384 int is_kernel_thread(pid_t pid) {
385 const char *p;
386 size_t count;
387 char c;
388 bool eof;
389 FILE *f;
390
391 if (pid == 0 || pid == 1) /* pid 1, and we ourselves certainly aren't a kernel thread */
392 return 0;
393
394 assert(pid > 1);
395
396 p = procfs_file_alloca(pid, "cmdline");
397 f = fopen(p, "re");
398 if (!f) {
399 if (errno == ENOENT)
400 return -ESRCH;
401 return -errno;
402 }
403
404 count = fread(&c, 1, 1, f);
405 eof = feof(f);
406 fclose(f);
407
408 /* Kernel threads have an empty cmdline */
409
410 if (count <= 0)
411 return eof ? 1 : -errno;
412
413 return 0;
414 }
415
416 int get_process_capeff(pid_t pid, char **capeff) {
417 const char *p;
418 int r;
419
420 assert(capeff);
421 assert(pid >= 0);
422
423 p = procfs_file_alloca(pid, "status");
424
425 r = get_proc_field(p, "CapEff", WHITESPACE, capeff);
426 if (r == -ENOENT)
427 return -ESRCH;
428
429 return r;
430 }
431
432 static int get_process_link_contents(const char *proc_file, char **name) {
433 int r;
434
435 assert(proc_file);
436 assert(name);
437
438 r = readlink_malloc(proc_file, name);
439 if (r == -ENOENT)
440 return -ESRCH;
441 if (r < 0)
442 return r;
443
444 return 0;
445 }
446
447 int get_process_exe(pid_t pid, char **name) {
448 const char *p;
449 char *d;
450 int r;
451
452 assert(pid >= 0);
453
454 p = procfs_file_alloca(pid, "exe");
455 r = get_process_link_contents(p, name);
456 if (r < 0)
457 return r;
458
459 d = endswith(*name, " (deleted)");
460 if (d)
461 *d = '\0';
462
463 return 0;
464 }
465
466 static int get_process_id(pid_t pid, const char *field, uid_t *uid) {
467 _cleanup_fclose_ FILE *f = NULL;
468 char line[LINE_MAX];
469 const char *p;
470
471 assert(field);
472 assert(uid);
473
474 p = procfs_file_alloca(pid, "status");
475 f = fopen(p, "re");
476 if (!f) {
477 if (errno == ENOENT)
478 return -ESRCH;
479 return -errno;
480 }
481
482 FOREACH_LINE(line, f, return -errno) {
483 char *l;
484
485 l = strstrip(line);
486
487 if (startswith(l, field)) {
488 l += strlen(field);
489 l += strspn(l, WHITESPACE);
490
491 l[strcspn(l, WHITESPACE)] = 0;
492
493 return parse_uid(l, uid);
494 }
495 }
496
497 return -EIO;
498 }
499
500 int get_process_uid(pid_t pid, uid_t *uid) {
501 return get_process_id(pid, "Uid:", uid);
502 }
503
504 int get_process_gid(pid_t pid, gid_t *gid) {
505 assert_cc(sizeof(uid_t) == sizeof(gid_t));
506 return get_process_id(pid, "Gid:", gid);
507 }
508
509 int get_process_cwd(pid_t pid, char **cwd) {
510 const char *p;
511
512 assert(pid >= 0);
513
514 p = procfs_file_alloca(pid, "cwd");
515
516 return get_process_link_contents(p, cwd);
517 }
518
519 int get_process_root(pid_t pid, char **root) {
520 const char *p;
521
522 assert(pid >= 0);
523
524 p = procfs_file_alloca(pid, "root");
525
526 return get_process_link_contents(p, root);
527 }
528
529 int get_process_environ(pid_t pid, char **env) {
530 _cleanup_fclose_ FILE *f = NULL;
531 _cleanup_free_ char *outcome = NULL;
532 int c;
533 const char *p;
534 size_t allocated = 0, sz = 0;
535
536 assert(pid >= 0);
537 assert(env);
538
539 p = procfs_file_alloca(pid, "environ");
540
541 f = fopen(p, "re");
542 if (!f) {
543 if (errno == ENOENT)
544 return -ESRCH;
545 return -errno;
546 }
547
548 while ((c = fgetc(f)) != EOF) {
549 if (!GREEDY_REALLOC(outcome, allocated, sz + 5))
550 return -ENOMEM;
551
552 if (c == '\0')
553 outcome[sz++] = '\n';
554 else
555 sz += cescape_char(c, outcome + sz);
556 }
557
558 if (!outcome) {
559 outcome = strdup("");
560 if (!outcome)
561 return -ENOMEM;
562 } else
563 outcome[sz] = '\0';
564
565 *env = outcome;
566 outcome = NULL;
567
568 return 0;
569 }
570
571 int get_process_ppid(pid_t pid, pid_t *_ppid) {
572 int r;
573 _cleanup_free_ char *line = NULL;
574 long unsigned ppid;
575 const char *p;
576
577 assert(pid >= 0);
578 assert(_ppid);
579
580 if (pid == 0) {
581 *_ppid = getppid();
582 return 0;
583 }
584
585 p = procfs_file_alloca(pid, "stat");
586 r = read_one_line_file(p, &line);
587 if (r == -ENOENT)
588 return -ESRCH;
589 if (r < 0)
590 return r;
591
592 /* Let's skip the pid and comm fields. The latter is enclosed
593 * in () but does not escape any () in its value, so let's
594 * skip over it manually */
595
596 p = strrchr(line, ')');
597 if (!p)
598 return -EIO;
599
600 p++;
601
602 if (sscanf(p, " "
603 "%*c " /* state */
604 "%lu ", /* ppid */
605 &ppid) != 1)
606 return -EIO;
607
608 if ((long unsigned) (pid_t) ppid != ppid)
609 return -ERANGE;
610
611 *_ppid = (pid_t) ppid;
612
613 return 0;
614 }
615
616 int wait_for_terminate(pid_t pid, siginfo_t *status) {
617 siginfo_t dummy;
618
619 assert(pid >= 1);
620
621 if (!status)
622 status = &dummy;
623
624 for (;;) {
625 zero(*status);
626
627 if (waitid(P_PID, pid, status, WEXITED) < 0) {
628
629 if (errno == EINTR)
630 continue;
631
632 return negative_errno();
633 }
634
635 return 0;
636 }
637 }
638
639 /*
640 * Return values:
641 * < 0 : wait_for_terminate() failed to get the state of the
642 * process, the process was terminated by a signal, or
643 * failed for an unknown reason.
644 * >=0 : The process terminated normally, and its exit code is
645 * returned.
646 *
647 * That is, success is indicated by a return value of zero, and an
648 * error is indicated by a non-zero value.
649 *
650 * A warning is emitted if the process terminates abnormally,
651 * and also if it returns non-zero unless check_exit_code is true.
652 */
653 int wait_for_terminate_and_warn(const char *name, pid_t pid, bool check_exit_code) {
654 int r;
655 siginfo_t status;
656
657 assert(name);
658 assert(pid > 1);
659
660 r = wait_for_terminate(pid, &status);
661 if (r < 0)
662 return log_warning_errno(r, "Failed to wait for %s: %m", name);
663
664 if (status.si_code == CLD_EXITED) {
665 if (status.si_status != 0)
666 log_full(check_exit_code ? LOG_WARNING : LOG_DEBUG,
667 "%s failed with error code %i.", name, status.si_status);
668 else
669 log_debug("%s succeeded.", name);
670
671 return status.si_status;
672 } else if (status.si_code == CLD_KILLED ||
673 status.si_code == CLD_DUMPED) {
674
675 log_warning("%s terminated by signal %s.", name, signal_to_string(status.si_status));
676 return -EPROTO;
677 }
678
679 log_warning("%s failed due to unknown reason.", name);
680 return -EPROTO;
681 }
682
683 void sigkill_wait(pid_t pid) {
684 assert(pid > 1);
685
686 if (kill(pid, SIGKILL) > 0)
687 (void) wait_for_terminate(pid, NULL);
688 }
689
690 void sigkill_waitp(pid_t *pid) {
691 if (!pid)
692 return;
693 if (*pid <= 1)
694 return;
695
696 sigkill_wait(*pid);
697 }
698
699 int kill_and_sigcont(pid_t pid, int sig) {
700 int r;
701
702 r = kill(pid, sig) < 0 ? -errno : 0;
703
704 /* If this worked, also send SIGCONT, unless we already just sent a SIGCONT, or SIGKILL was sent which isn't
705 * affected by a process being suspended anyway. */
706 if (r >= 0 && !IN_SET(sig, SIGCONT, SIGKILL))
707 (void) kill(pid, SIGCONT);
708
709 return r;
710 }
711
712 int getenv_for_pid(pid_t pid, const char *field, char **_value) {
713 _cleanup_fclose_ FILE *f = NULL;
714 char *value = NULL;
715 int r;
716 bool done = false;
717 size_t l;
718 const char *path;
719
720 assert(pid >= 0);
721 assert(field);
722 assert(_value);
723
724 path = procfs_file_alloca(pid, "environ");
725
726 f = fopen(path, "re");
727 if (!f) {
728 if (errno == ENOENT)
729 return -ESRCH;
730 return -errno;
731 }
732
733 l = strlen(field);
734 r = 0;
735
736 do {
737 char line[LINE_MAX];
738 unsigned i;
739
740 for (i = 0; i < sizeof(line)-1; i++) {
741 int c;
742
743 c = getc(f);
744 if (_unlikely_(c == EOF)) {
745 done = true;
746 break;
747 } else if (c == 0)
748 break;
749
750 line[i] = c;
751 }
752 line[i] = 0;
753
754 if (strneq(line, field, l) && line[l] == '=') {
755 value = strdup(line + l + 1);
756 if (!value)
757 return -ENOMEM;
758
759 r = 1;
760 break;
761 }
762
763 } while (!done);
764
765 *_value = value;
766 return r;
767 }
768
769 bool pid_is_unwaited(pid_t pid) {
770 /* Checks whether a PID is still valid at all, including a zombie */
771
772 if (pid < 0)
773 return false;
774
775 if (pid <= 1) /* If we or PID 1 would be dead and have been waited for, this code would not be running */
776 return true;
777
778 if (kill(pid, 0) >= 0)
779 return true;
780
781 return errno != ESRCH;
782 }
783
784 bool pid_is_alive(pid_t pid) {
785 int r;
786
787 /* Checks whether a PID is still valid and not a zombie */
788
789 if (pid < 0)
790 return false;
791
792 if (pid <= 1) /* If we or PID 1 would be a zombie, this code would not be running */
793 return true;
794
795 r = get_process_state(pid);
796 if (r == -ESRCH || r == 'Z')
797 return false;
798
799 return true;
800 }
801
802 int pid_from_same_root_fs(pid_t pid) {
803 const char *root;
804
805 if (pid < 0)
806 return 0;
807
808 root = procfs_file_alloca(pid, "root");
809
810 return files_same(root, "/proc/1/root");
811 }
812
813 bool is_main_thread(void) {
814 static thread_local int cached = 0;
815
816 if (_unlikely_(cached == 0))
817 cached = getpid() == gettid() ? 1 : -1;
818
819 return cached > 0;
820 }
821
822 noreturn void freeze(void) {
823
824 log_close();
825
826 /* Make sure nobody waits for us on a socket anymore */
827 close_all_fds(NULL, 0);
828
829 sync();
830
831 for (;;)
832 pause();
833 }
834
835 bool oom_score_adjust_is_valid(int oa) {
836 return oa >= OOM_SCORE_ADJ_MIN && oa <= OOM_SCORE_ADJ_MAX;
837 }
838
839 unsigned long personality_from_string(const char *p) {
840 int architecture;
841
842 if (!p)
843 return PERSONALITY_INVALID;
844
845 /* Parse a personality specifier. We use our own identifiers that indicate specific ABIs, rather than just
846 * hints regarding the register size, since we want to keep things open for multiple locally supported ABIs for
847 * the same register size. */
848
849 architecture = architecture_from_string(p);
850 if (architecture < 0)
851 return PERSONALITY_INVALID;
852
853 if (architecture == native_architecture())
854 return PER_LINUX;
855 #ifdef SECONDARY_ARCHITECTURE
856 if (architecture == SECONDARY_ARCHITECTURE)
857 return PER_LINUX32;
858 #endif
859
860 return PERSONALITY_INVALID;
861 }
862
863 const char* personality_to_string(unsigned long p) {
864 int architecture = _ARCHITECTURE_INVALID;
865
866 if (p == PER_LINUX)
867 architecture = native_architecture();
868 #ifdef SECONDARY_ARCHITECTURE
869 else if (p == PER_LINUX32)
870 architecture = SECONDARY_ARCHITECTURE;
871 #endif
872
873 if (architecture < 0)
874 return NULL;
875
876 return architecture_to_string(architecture);
877 }
878
879 void valgrind_summary_hack(void) {
880 #ifdef HAVE_VALGRIND_VALGRIND_H
881 if (getpid() == 1 && RUNNING_ON_VALGRIND) {
882 pid_t pid;
883 pid = raw_clone(SIGCHLD);
884 if (pid < 0)
885 log_emergency_errno(errno, "Failed to fork off valgrind helper: %m");
886 else if (pid == 0)
887 exit(EXIT_SUCCESS);
888 else {
889 log_info("Spawned valgrind helper as PID "PID_FMT".", pid);
890 (void) wait_for_terminate(pid, NULL);
891 }
892 }
893 #endif
894 }
895
896 int pid_compare_func(const void *a, const void *b) {
897 const pid_t *p = a, *q = b;
898
899 /* Suitable for usage in qsort() */
900
901 if (*p < *q)
902 return -1;
903 if (*p > *q)
904 return 1;
905 return 0;
906 }
907
908 static const char *const ioprio_class_table[] = {
909 [IOPRIO_CLASS_NONE] = "none",
910 [IOPRIO_CLASS_RT] = "realtime",
911 [IOPRIO_CLASS_BE] = "best-effort",
912 [IOPRIO_CLASS_IDLE] = "idle"
913 };
914
915 DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(ioprio_class, int, INT_MAX);
916
917 static const char *const sigchld_code_table[] = {
918 [CLD_EXITED] = "exited",
919 [CLD_KILLED] = "killed",
920 [CLD_DUMPED] = "dumped",
921 [CLD_TRAPPED] = "trapped",
922 [CLD_STOPPED] = "stopped",
923 [CLD_CONTINUED] = "continued",
924 };
925
926 DEFINE_STRING_TABLE_LOOKUP(sigchld_code, int);
927
928 static const char* const sched_policy_table[] = {
929 [SCHED_OTHER] = "other",
930 [SCHED_BATCH] = "batch",
931 [SCHED_IDLE] = "idle",
932 [SCHED_FIFO] = "fifo",
933 [SCHED_RR] = "rr"
934 };
935
936 DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(sched_policy, int, INT_MAX);