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