chiark / gitweb /
1a7871b4e030b374b4699a0bc8f91362a91b209c
[elogind.git] / src / execute.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <assert.h>
23 #include <dirent.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <signal.h>
29 #include <sys/socket.h>
30 #include <sys/un.h>
31 #include <sys/prctl.h>
32 #include <linux/sched.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <grp.h>
36 #include <pwd.h>
37 #include <sys/mount.h>
38 #include <linux/fs.h>
39
40 #include "execute.h"
41 #include "strv.h"
42 #include "macro.h"
43 #include "util.h"
44 #include "log.h"
45 #include "ioprio.h"
46 #include "securebits.h"
47 #include "cgroup.h"
48 #include "namespace.h"
49 #include "tcpwrap.h"
50
51 /* This assumes there is a 'tty' group */
52 #define TTY_MODE 0620
53
54 static int shift_fds(int fds[], unsigned n_fds) {
55         int start, restart_from;
56
57         if (n_fds <= 0)
58                 return 0;
59
60         /* Modifies the fds array! (sorts it) */
61
62         assert(fds);
63
64         start = 0;
65         for (;;) {
66                 int i;
67
68                 restart_from = -1;
69
70                 for (i = start; i < (int) n_fds; i++) {
71                         int nfd;
72
73                         /* Already at right index? */
74                         if (fds[i] == i+3)
75                                 continue;
76
77                         if ((nfd = fcntl(fds[i], F_DUPFD, i+3)) < 0)
78                                 return -errno;
79
80                         close_nointr_nofail(fds[i]);
81                         fds[i] = nfd;
82
83                         /* Hmm, the fd we wanted isn't free? Then
84                          * let's remember that and try again from here*/
85                         if (nfd != i+3 && restart_from < 0)
86                                 restart_from = i;
87                 }
88
89                 if (restart_from < 0)
90                         break;
91
92                 start = restart_from;
93         }
94
95         return 0;
96 }
97
98 static int flags_fds(const int fds[], unsigned n_fds, bool nonblock) {
99         unsigned i;
100         int r;
101
102         if (n_fds <= 0)
103                 return 0;
104
105         assert(fds);
106
107         /* Drops/Sets O_NONBLOCK and FD_CLOEXEC from the file flags */
108
109         for (i = 0; i < n_fds; i++) {
110
111                 if ((r = fd_nonblock(fds[i], nonblock)) < 0)
112                         return r;
113
114                 /* We unconditionally drop FD_CLOEXEC from the fds,
115                  * since after all we want to pass these fds to our
116                  * children */
117
118                 if ((r = fd_cloexec(fds[i], false)) < 0)
119                         return r;
120         }
121
122         return 0;
123 }
124
125 static const char *tty_path(const ExecContext *context) {
126         assert(context);
127
128         if (context->tty_path)
129                 return context->tty_path;
130
131         return "/dev/console";
132 }
133
134 static int open_null_as(int flags, int nfd) {
135         int fd, r;
136
137         assert(nfd >= 0);
138
139         if ((fd = open("/dev/null", flags|O_NOCTTY)) < 0)
140                 return -errno;
141
142         if (fd != nfd) {
143                 r = dup2(fd, nfd) < 0 ? -errno : nfd;
144                 close_nointr_nofail(fd);
145         } else
146                 r = nfd;
147
148         return r;
149 }
150
151 static int connect_logger_as(const ExecContext *context, ExecOutput output, const char *ident, int nfd) {
152         int fd, r;
153         union {
154                 struct sockaddr sa;
155                 struct sockaddr_un un;
156         } sa;
157
158         assert(context);
159         assert(output < _EXEC_OUTPUT_MAX);
160         assert(ident);
161         assert(nfd >= 0);
162
163         if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
164                 return -errno;
165
166         zero(sa);
167         sa.sa.sa_family = AF_UNIX;
168         strncpy(sa.un.sun_path+1, LOGGER_SOCKET, sizeof(sa.un.sun_path)-1);
169
170         if (connect(fd, &sa.sa, sizeof(sa)) < 0) {
171                 close_nointr_nofail(fd);
172                 return -errno;
173         }
174
175         if (shutdown(fd, SHUT_RD) < 0) {
176                 close_nointr_nofail(fd);
177                 return -errno;
178         }
179
180         /* We speak a very simple protocol between log server
181          * and client: one line for the log destination (kmsg
182          * or syslog), followed by the priority field,
183          * followed by the process name. Since we replaced
184          * stdin/stderr we simple use stdio to write to
185          * it. Note that we use stderr, to minimize buffer
186          * flushing issues. */
187
188         dprintf(fd,
189                 "%s\n"
190                 "%i\n"
191                 "%s\n"
192                 "%i\n",
193                 output == EXEC_OUTPUT_KMSG ? "kmsg" : "syslog",
194                 context->syslog_priority,
195                 context->syslog_identifier ? context->syslog_identifier : ident,
196                 !context->syslog_no_prefix);
197
198         if (fd != nfd) {
199                 r = dup2(fd, nfd) < 0 ? -errno : nfd;
200                 close_nointr_nofail(fd);
201         } else
202                 r = nfd;
203
204         return r;
205 }
206 static int open_terminal_as(const char *path, mode_t mode, int nfd) {
207         int fd, r;
208
209         assert(path);
210         assert(nfd >= 0);
211
212         if ((fd = open_terminal(path, mode | O_NOCTTY)) < 0)
213                 return fd;
214
215         if (fd != nfd) {
216                 r = dup2(fd, nfd) < 0 ? -errno : nfd;
217                 close_nointr_nofail(fd);
218         } else
219                 r = nfd;
220
221         return r;
222 }
223
224 static bool is_terminal_input(ExecInput i) {
225         return
226                 i == EXEC_INPUT_TTY ||
227                 i == EXEC_INPUT_TTY_FORCE ||
228                 i == EXEC_INPUT_TTY_FAIL;
229 }
230
231 static int fixup_input(ExecInput std_input, int socket_fd) {
232
233         if (std_input == EXEC_INPUT_SOCKET && socket_fd < 0)
234                 return EXEC_INPUT_NULL;
235
236         return std_input;
237 }
238
239 static int fixup_output(ExecOutput std_output, int socket_fd) {
240
241         if (std_output == EXEC_OUTPUT_SOCKET && socket_fd < 0)
242                 return EXEC_OUTPUT_INHERIT;
243
244         return std_output;
245 }
246
247 static int setup_input(const ExecContext *context, int socket_fd) {
248         ExecInput i;
249
250         assert(context);
251
252         i = fixup_input(context->std_input, socket_fd);
253
254         switch (i) {
255
256         case EXEC_INPUT_NULL:
257                 return open_null_as(O_RDONLY, STDIN_FILENO);
258
259         case EXEC_INPUT_TTY:
260         case EXEC_INPUT_TTY_FORCE:
261         case EXEC_INPUT_TTY_FAIL: {
262                 int fd, r;
263
264                 if ((fd = acquire_terminal(
265                                      tty_path(context),
266                                      i == EXEC_INPUT_TTY_FAIL,
267                                      i == EXEC_INPUT_TTY_FORCE,
268                                      false)) < 0)
269                         return fd;
270
271                 if (fd != STDIN_FILENO) {
272                         r = dup2(fd, STDIN_FILENO) < 0 ? -errno : STDIN_FILENO;
273                         close_nointr_nofail(fd);
274                 } else
275                         r = STDIN_FILENO;
276
277                 return r;
278         }
279
280         case EXEC_INPUT_SOCKET:
281                 return dup2(socket_fd, STDIN_FILENO) < 0 ? -errno : STDIN_FILENO;
282
283         default:
284                 assert_not_reached("Unknown input type");
285         }
286 }
287
288 static int setup_output(const ExecContext *context, int socket_fd, const char *ident) {
289         ExecOutput o;
290         ExecInput i;
291
292         assert(context);
293         assert(ident);
294
295         i = fixup_input(context->std_input, socket_fd);
296         o = fixup_output(context->std_output, socket_fd);
297
298         /* This expects the input is already set up */
299
300         switch (o) {
301
302         case EXEC_OUTPUT_INHERIT:
303
304                 /* If the input is connected to a terminal, inherit that... */
305                 if (i != EXEC_INPUT_NULL)
306                         return dup2(STDIN_FILENO, STDOUT_FILENO) < 0 ? -errno : STDOUT_FILENO;
307
308                 /* For PID 1 stdout is always connected to /dev/null,
309                  * hence reopen the console if out parent is PID1. */
310                 if (getppid() == 1)
311                         return open_terminal_as(tty_path(context), O_WRONLY, STDOUT_FILENO);
312
313                 return STDOUT_FILENO;
314
315         case EXEC_OUTPUT_NULL:
316                 return open_null_as(O_WRONLY, STDOUT_FILENO);
317
318         case EXEC_OUTPUT_TTY:
319                 if (is_terminal_input(i))
320                         return dup2(STDIN_FILENO, STDOUT_FILENO) < 0 ? -errno : STDOUT_FILENO;
321
322                 /* We don't reset the terminal if this is just about output */
323                 return open_terminal_as(tty_path(context), O_WRONLY, STDOUT_FILENO);
324
325         case EXEC_OUTPUT_SYSLOG:
326         case EXEC_OUTPUT_KMSG:
327                 return connect_logger_as(context, o, ident, STDOUT_FILENO);
328
329         case EXEC_OUTPUT_SOCKET:
330                 assert(socket_fd >= 0);
331                 return dup2(socket_fd, STDOUT_FILENO) < 0 ? -errno : STDOUT_FILENO;
332
333         default:
334                 assert_not_reached("Unknown output type");
335         }
336 }
337
338 static int setup_error(const ExecContext *context, int socket_fd, const char *ident) {
339         ExecOutput o, e;
340         ExecInput i;
341
342         assert(context);
343         assert(ident);
344
345         i = fixup_input(context->std_input, socket_fd);
346         o = fixup_output(context->std_output, socket_fd);
347         e = fixup_output(context->std_error, socket_fd);
348
349         /* This expects the input and output are already set up */
350
351         /* Don't change the stderr file descriptor if we inherit all
352          * the way and are not on a tty */
353         if (e == EXEC_OUTPUT_INHERIT &&
354             o == EXEC_OUTPUT_INHERIT &&
355             i != EXEC_INPUT_NULL &&
356             getppid () != 1)
357                 return STDERR_FILENO;
358
359         /* Duplicate form stdout if possible */
360         if (e == o || e == EXEC_OUTPUT_INHERIT)
361                 return dup2(STDOUT_FILENO, STDERR_FILENO) < 0 ? -errno : STDERR_FILENO;
362
363         switch (e) {
364
365         case EXEC_OUTPUT_NULL:
366                 return open_null_as(O_WRONLY, STDERR_FILENO);
367
368         case EXEC_OUTPUT_TTY:
369                 if (is_terminal_input(i))
370                         return dup2(STDIN_FILENO, STDERR_FILENO) < 0 ? -errno : STDERR_FILENO;
371
372                 /* We don't reset the terminal if this is just about output */
373                 return open_terminal_as(tty_path(context), O_WRONLY, STDERR_FILENO);
374
375         case EXEC_OUTPUT_SYSLOG:
376         case EXEC_OUTPUT_KMSG:
377                 return connect_logger_as(context, e, ident, STDERR_FILENO);
378
379         case EXEC_OUTPUT_SOCKET:
380                 assert(socket_fd >= 0);
381                 return dup2(socket_fd, STDERR_FILENO) < 0 ? -errno : STDERR_FILENO;
382
383         default:
384                 assert_not_reached("Unknown error type");
385         }
386 }
387
388 static int chown_terminal(int fd, uid_t uid) {
389         struct stat st;
390
391         assert(fd >= 0);
392
393         /* This might fail. What matters are the results. */
394         (void) fchown(fd, uid, -1);
395         (void) fchmod(fd, TTY_MODE);
396
397         if (fstat(fd, &st) < 0)
398                 return -errno;
399
400         if (st.st_uid != uid || (st.st_mode & 0777) != TTY_MODE)
401                 return -EPERM;
402
403         return 0;
404 }
405
406 static int setup_confirm_stdio(const ExecContext *context,
407                                int *_saved_stdin,
408                                int *_saved_stdout) {
409         int fd = -1, saved_stdin, saved_stdout = -1, r;
410
411         assert(context);
412         assert(_saved_stdin);
413         assert(_saved_stdout);
414
415         /* This returns positive EXIT_xxx return values instead of
416          * negative errno style values! */
417
418         if ((saved_stdin = fcntl(STDIN_FILENO, F_DUPFD, 3)) < 0)
419                 return EXIT_STDIN;
420
421         if ((saved_stdout = fcntl(STDOUT_FILENO, F_DUPFD, 3)) < 0) {
422                 r = EXIT_STDOUT;
423                 goto fail;
424         }
425
426         if ((fd = acquire_terminal(
427                              tty_path(context),
428                              context->std_input == EXEC_INPUT_TTY_FAIL,
429                              context->std_input == EXEC_INPUT_TTY_FORCE,
430                              false)) < 0) {
431                 r = EXIT_STDIN;
432                 goto fail;
433         }
434
435         if (chown_terminal(fd, getuid()) < 0) {
436                 r = EXIT_STDIN;
437                 goto fail;
438         }
439
440         if (dup2(fd, STDIN_FILENO) < 0) {
441                 r = EXIT_STDIN;
442                 goto fail;
443         }
444
445         if (dup2(fd, STDOUT_FILENO) < 0) {
446                 r = EXIT_STDOUT;
447                 goto fail;
448         }
449
450         if (fd >= 2)
451                 close_nointr_nofail(fd);
452
453         *_saved_stdin = saved_stdin;
454         *_saved_stdout = saved_stdout;
455
456         return 0;
457
458 fail:
459         if (saved_stdout >= 0)
460                 close_nointr_nofail(saved_stdout);
461
462         if (saved_stdin >= 0)
463                 close_nointr_nofail(saved_stdin);
464
465         if (fd >= 0)
466                 close_nointr_nofail(fd);
467
468         return r;
469 }
470
471 static int restore_confirm_stdio(const ExecContext *context,
472                                  int *saved_stdin,
473                                  int *saved_stdout,
474                                  bool *keep_stdin,
475                                  bool *keep_stdout) {
476
477         assert(context);
478         assert(saved_stdin);
479         assert(*saved_stdin >= 0);
480         assert(saved_stdout);
481         assert(*saved_stdout >= 0);
482
483         /* This returns positive EXIT_xxx return values instead of
484          * negative errno style values! */
485
486         if (is_terminal_input(context->std_input)) {
487
488                 /* The service wants terminal input. */
489
490                 *keep_stdin = true;
491                 *keep_stdout =
492                         context->std_output == EXEC_OUTPUT_INHERIT ||
493                         context->std_output == EXEC_OUTPUT_TTY;
494
495         } else {
496                 /* If the service doesn't want a controlling terminal,
497                  * then we need to get rid entirely of what we have
498                  * already. */
499
500                 if (release_terminal() < 0)
501                         return EXIT_STDIN;
502
503                 if (dup2(*saved_stdin, STDIN_FILENO) < 0)
504                         return EXIT_STDIN;
505
506                 if (dup2(*saved_stdout, STDOUT_FILENO) < 0)
507                         return EXIT_STDOUT;
508
509                 *keep_stdout = *keep_stdin = false;
510         }
511
512         return 0;
513 }
514
515 static int get_group_creds(const char *groupname, gid_t *gid) {
516         struct group *g;
517         unsigned long lu;
518
519         assert(groupname);
520         assert(gid);
521
522         /* We enforce some special rules for gid=0: in order to avoid
523          * NSS lookups for root we hardcode its data. */
524
525         if (streq(groupname, "root") || streq(groupname, "0")) {
526                 *gid = 0;
527                 return 0;
528         }
529
530         if (safe_atolu(groupname, &lu) >= 0) {
531                 errno = 0;
532                 g = getgrgid((gid_t) lu);
533         } else {
534                 errno = 0;
535                 g = getgrnam(groupname);
536         }
537
538         if (!g)
539                 return errno != 0 ? -errno : -ESRCH;
540
541         *gid = g->gr_gid;
542         return 0;
543 }
544
545 static int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home) {
546         struct passwd *p;
547         unsigned long lu;
548
549         assert(username);
550         assert(*username);
551         assert(uid);
552         assert(gid);
553         assert(home);
554
555         /* We enforce some special rules for uid=0: in order to avoid
556          * NSS lookups for root we hardcode its data. */
557
558         if (streq(*username, "root") || streq(*username, "0")) {
559                 *username = "root";
560                 *uid = 0;
561                 *gid = 0;
562                 *home = "/root";
563                 return 0;
564         }
565
566         if (safe_atolu(*username, &lu) >= 0) {
567                 errno = 0;
568                 p = getpwuid((uid_t) lu);
569
570                 /* If there are multiple users with the same id, make
571                  * sure to leave $USER to the configured value instead
572                  * of the first occurence in the database. However if
573                  * the uid was configured by a numeric uid, then let's
574                  * pick the real username from /etc/passwd. */
575                 if (*username && p)
576                         *username = p->pw_name;
577         } else {
578                 errno = 0;
579                 p = getpwnam(*username);
580         }
581
582         if (!p)
583                 return errno != 0 ? -errno : -ESRCH;
584
585         *uid = p->pw_uid;
586         *gid = p->pw_gid;
587         *home = p->pw_dir;
588         return 0;
589 }
590
591 static int enforce_groups(const ExecContext *context, const char *username, gid_t gid) {
592         bool keep_groups = false;
593         int r;
594
595         assert(context);
596
597         /* Lookup and ser GID and supplementary group list. Here too
598          * we avoid NSS lookups for gid=0. */
599
600         if (context->group || username) {
601
602                 if (context->group)
603                         if ((r = get_group_creds(context->group, &gid)) < 0)
604                                 return r;
605
606                 /* First step, initialize groups from /etc/groups */
607                 if (username && gid != 0) {
608                         if (initgroups(username, gid) < 0)
609                                 return -errno;
610
611                         keep_groups = true;
612                 }
613
614                 /* Second step, set our gids */
615                 if (setresgid(gid, gid, gid) < 0)
616                         return -errno;
617         }
618
619         if (context->supplementary_groups) {
620                 int ngroups_max, k;
621                 gid_t *gids;
622                 char **i;
623
624                 /* Final step, initialize any manually set supplementary groups */
625                 ngroups_max = (int) sysconf(_SC_NGROUPS_MAX);
626
627                 if (!(gids = new(gid_t, ngroups_max)))
628                         return -ENOMEM;
629
630                 if (keep_groups) {
631                         if ((k = getgroups(ngroups_max, gids)) < 0) {
632                                 free(gids);
633                                 return -errno;
634                         }
635                 } else
636                         k = 0;
637
638                 STRV_FOREACH(i, context->supplementary_groups) {
639
640                         if (k >= ngroups_max) {
641                                 free(gids);
642                                 return -E2BIG;
643                         }
644
645                         if ((r = get_group_creds(*i, gids+k)) < 0) {
646                                 free(gids);
647                                 return r;
648                         }
649
650                         k++;
651                 }
652
653                 if (setgroups(k, gids) < 0) {
654                         free(gids);
655                         return -errno;
656                 }
657
658                 free(gids);
659         }
660
661         return 0;
662 }
663
664 static int enforce_user(const ExecContext *context, uid_t uid) {
665         int r;
666         assert(context);
667
668         /* Sets (but doesn't lookup) the uid and make sure we keep the
669          * capabilities while doing so. */
670
671         if (context->capabilities) {
672                 cap_t d;
673                 static const cap_value_t bits[] = {
674                         CAP_SETUID,   /* Necessary so that we can run setresuid() below */
675                         CAP_SETPCAP   /* Necessary so that we can set PR_SET_SECUREBITS later on */
676                 };
677
678                 /* First step: If we need to keep capabilities but
679                  * drop privileges we need to make sure we keep our
680                  * caps, whiel we drop priviliges. */
681                 if (uid != 0) {
682                         int sb = context->secure_bits|SECURE_KEEP_CAPS;
683
684                         if (prctl(PR_GET_SECUREBITS) != sb)
685                                 if (prctl(PR_SET_SECUREBITS, sb) < 0)
686                                         return -errno;
687                 }
688
689                 /* Second step: set the capabilites. This will reduce
690                  * the capabilities to the minimum we need. */
691
692                 if (!(d = cap_dup(context->capabilities)))
693                         return -errno;
694
695                 if (cap_set_flag(d, CAP_EFFECTIVE, ELEMENTSOF(bits), bits, CAP_SET) < 0 ||
696                     cap_set_flag(d, CAP_PERMITTED, ELEMENTSOF(bits), bits, CAP_SET) < 0) {
697                         r = -errno;
698                         cap_free(d);
699                         return r;
700                 }
701
702                 if (cap_set_proc(d) < 0) {
703                         r = -errno;
704                         cap_free(d);
705                         return r;
706                 }
707
708                 cap_free(d);
709         }
710
711         /* Third step: actually set the uids */
712         if (setresuid(uid, uid, uid) < 0)
713                 return -errno;
714
715         /* At this point we should have all necessary capabilities but
716            are otherwise a normal user. However, the caps might got
717            corrupted due to the setresuid() so we need clean them up
718            later. This is done outside of this call. */
719
720         return 0;
721 }
722
723 int exec_spawn(ExecCommand *command,
724                char **argv,
725                const ExecContext *context,
726                int fds[], unsigned n_fds,
727                char **environment,
728                bool apply_permissions,
729                bool apply_chroot,
730                bool confirm_spawn,
731                CGroupBonding *cgroup_bondings,
732                pid_t *ret) {
733
734         pid_t pid;
735         int r;
736         char *line;
737         int socket_fd;
738
739         assert(command);
740         assert(context);
741         assert(ret);
742         assert(fds || n_fds <= 0);
743
744         if (context->std_input == EXEC_INPUT_SOCKET ||
745             context->std_output == EXEC_OUTPUT_SOCKET ||
746             context->std_error == EXEC_OUTPUT_SOCKET) {
747
748                 if (n_fds != 1)
749                         return -EINVAL;
750
751                 socket_fd = fds[0];
752
753                 fds = NULL;
754                 n_fds = 0;
755         } else
756                 socket_fd = -1;
757
758         if (!argv)
759                 argv = command->argv;
760
761         if (!(line = exec_command_line(argv)))
762                 return -ENOMEM;
763
764         log_debug("About to execute: %s", line);
765         free(line);
766
767         if (cgroup_bondings)
768                 if ((r = cgroup_bonding_realize_list(cgroup_bondings)))
769                         return r;
770
771         if ((pid = fork()) < 0)
772                 return -errno;
773
774         if (pid == 0) {
775                 int i;
776                 sigset_t ss;
777                 const char *username = NULL, *home = NULL;
778                 uid_t uid = (uid_t) -1;
779                 gid_t gid = (gid_t) -1;
780                 char **our_env = NULL, **final_env = NULL;
781                 unsigned n_env = 0;
782                 int saved_stdout = -1, saved_stdin = -1;
783                 bool keep_stdout = false, keep_stdin = false;
784
785                 /* child */
786
787                 /* We reset exactly these signals, since they are the
788                  * only ones we set to SIG_IGN in the main daemon. All
789                  * others we leave untouched because we set them to
790                  * SIG_DFL or a valid handler initially, both of which
791                  * will be demoted to SIG_DFL. */
792                 default_signals(SIGNALS_CRASH_HANDLER,
793                                 SIGNALS_IGNORE, -1);
794
795                 if (sigemptyset(&ss) < 0 ||
796                     sigprocmask(SIG_SETMASK, &ss, NULL) < 0) {
797                         r = EXIT_SIGNAL_MASK;
798                         goto fail;
799                 }
800
801                 if (!context->no_setsid)
802                         if (setsid() < 0) {
803                                 r = EXIT_SETSID;
804                                 goto fail;
805                         }
806
807                 if (socket_fd >= 0 && context->tcpwrap_name)
808                         if (!socket_tcpwrap(socket_fd, context->tcpwrap_name)) {
809                                 r = EXIT_TCPWRAP;
810                                 goto fail;
811                         }
812
813                 if (confirm_spawn) {
814                         char response;
815
816                         /* Set up terminal for the question */
817                         if ((r = setup_confirm_stdio(context,
818                                                      &saved_stdin, &saved_stdout)))
819                                 goto fail;
820
821                         /* Now ask the question. */
822                         if (!(line = exec_command_line(argv))) {
823                                 r = EXIT_MEMORY;
824                                 goto fail;
825                         }
826
827                         r = ask(&response, "yns", "Execute %s? [Yes, No, Skip] ", line);
828                         free(line);
829
830                         if (r < 0 || response == 'n') {
831                                 r = EXIT_CONFIRM;
832                                 goto fail;
833                         } else if (response == 's') {
834                                 r = 0;
835                                 goto fail;
836                         }
837
838                         /* Release terminal for the question */
839                         if ((r = restore_confirm_stdio(context,
840                                                        &saved_stdin, &saved_stdout,
841                                                        &keep_stdin, &keep_stdout)))
842                                 goto fail;
843                 }
844
845                 if (!keep_stdin)
846                         if (setup_input(context, socket_fd) < 0) {
847                                 r = EXIT_STDIN;
848                                 goto fail;
849                         }
850
851                 if (!keep_stdout)
852                         if (setup_output(context, socket_fd, file_name_from_path(command->path)) < 0) {
853                                 r = EXIT_STDOUT;
854                                 goto fail;
855                         }
856
857                 if (setup_error(context, socket_fd, file_name_from_path(command->path)) < 0) {
858                         r = EXIT_STDERR;
859                         goto fail;
860                 }
861
862                 if (cgroup_bondings)
863                         if ((r = cgroup_bonding_install_list(cgroup_bondings, 0)) < 0) {
864                                 r = EXIT_CGROUP;
865                                 goto fail;
866                         }
867
868                 if (context->oom_adjust_set) {
869                         char t[16];
870
871                         snprintf(t, sizeof(t), "%i", context->oom_adjust);
872                         char_array_0(t);
873
874                         if (write_one_line_file("/proc/self/oom_adj", t) < 0) {
875                                 r = EXIT_OOM_ADJUST;
876                                 goto fail;
877                         }
878                 }
879
880                 if (context->nice_set)
881                         if (setpriority(PRIO_PROCESS, 0, context->nice) < 0) {
882                                 r = EXIT_NICE;
883                                 goto fail;
884                         }
885
886                 if (context->cpu_sched_set) {
887                         struct sched_param param;
888
889                         zero(param);
890                         param.sched_priority = context->cpu_sched_priority;
891
892                         if (sched_setscheduler(0, context->cpu_sched_policy |
893                                                (context->cpu_sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0), &param) < 0) {
894                                 r = EXIT_SETSCHEDULER;
895                                 goto fail;
896                         }
897                 }
898
899                 if (context->cpu_affinity_set)
900                         if (sched_setaffinity(0, sizeof(context->cpu_affinity), &context->cpu_affinity) < 0) {
901                                 r = EXIT_CPUAFFINITY;
902                                 goto fail;
903                         }
904
905                 if (context->ioprio_set)
906                         if (ioprio_set(IOPRIO_WHO_PROCESS, 0, context->ioprio) < 0) {
907                                 r = EXIT_IOPRIO;
908                                 goto fail;
909                         }
910
911                 if (context->timer_slack_ns_set)
912                         if (prctl(PR_SET_TIMERSLACK, context->timer_slack_ns_set) < 0) {
913                                 r = EXIT_TIMERSLACK;
914                                 goto fail;
915                         }
916
917                 if (context->user) {
918                         username = context->user;
919                         if (get_user_creds(&username, &uid, &gid, &home) < 0) {
920                                 r = EXIT_USER;
921                                 goto fail;
922                         }
923
924                         if (is_terminal_input(context->std_input))
925                                 if (chown_terminal(STDIN_FILENO, uid) < 0) {
926                                         r = EXIT_STDIN;
927                                         goto fail;
928                                 }
929                 }
930
931                 if (apply_permissions)
932                         if (enforce_groups(context, username, uid) < 0) {
933                                 r = EXIT_GROUP;
934                                 goto fail;
935                         }
936
937                 umask(context->umask);
938
939                 if (strv_length(context->read_write_dirs) > 0 ||
940                     strv_length(context->read_only_dirs) > 0 ||
941                     strv_length(context->inaccessible_dirs) > 0 ||
942                     context->mount_flags != MS_SHARED ||
943                     context->private_tmp)
944                         if ((r = setup_namespace(
945                                              context->read_write_dirs,
946                                              context->read_only_dirs,
947                                              context->inaccessible_dirs,
948                                              context->private_tmp,
949                                              context->mount_flags)) < 0)
950                                 goto fail;
951
952                 if (apply_chroot) {
953                         if (context->root_directory)
954                                 if (chroot(context->root_directory) < 0) {
955                                         r = EXIT_CHROOT;
956                                         goto fail;
957                                 }
958
959                         if (chdir(context->working_directory ? context->working_directory : "/") < 0) {
960                                 r = EXIT_CHDIR;
961                                 goto fail;
962                         }
963                 } else {
964
965                         char *d;
966
967                         if (asprintf(&d, "%s/%s",
968                                      context->root_directory ? context->root_directory : "",
969                                      context->working_directory ? context->working_directory : "") < 0) {
970                                 r = EXIT_MEMORY;
971                                 goto fail;
972                         }
973
974                         if (chdir(d) < 0) {
975                                 free(d);
976                                 r = EXIT_CHDIR;
977                                 goto fail;
978                         }
979
980                         free(d);
981                 }
982
983                 if (close_all_fds(fds, n_fds) < 0 ||
984                     shift_fds(fds, n_fds) < 0 ||
985                     flags_fds(fds, n_fds, context->non_blocking) < 0) {
986                         r = EXIT_FDS;
987                         goto fail;
988                 }
989
990                 if (apply_permissions) {
991
992                         for (i = 0; i < RLIMIT_NLIMITS; i++) {
993                                 if (!context->rlimit[i])
994                                         continue;
995
996                                 if (setrlimit(i, context->rlimit[i]) < 0) {
997                                         r = EXIT_LIMITS;
998                                         goto fail;
999                                 }
1000                         }
1001
1002                         if (context->user)
1003                                 if (enforce_user(context, uid) < 0) {
1004                                         r = EXIT_USER;
1005                                         goto fail;
1006                                 }
1007
1008                         /* PR_GET_SECUREBITS is not priviliged, while
1009                          * PR_SET_SECUREBITS is. So to suppress
1010                          * potential EPERMs we'll try not to call
1011                          * PR_SET_SECUREBITS unless necessary. */
1012                         if (prctl(PR_GET_SECUREBITS) != context->secure_bits)
1013                                 if (prctl(PR_SET_SECUREBITS, context->secure_bits) < 0) {
1014                                         r = EXIT_SECUREBITS;
1015                                         goto fail;
1016                                 }
1017
1018                         if (context->capabilities)
1019                                 if (cap_set_proc(context->capabilities) < 0) {
1020                                         r = EXIT_CAPABILITIES;
1021                                         goto fail;
1022                                 }
1023                 }
1024
1025                 if (!(our_env = new0(char*, 6))) {
1026                         r = EXIT_MEMORY;
1027                         goto fail;
1028                 }
1029
1030                 if (n_fds > 0)
1031                         if (asprintf(our_env + n_env++, "LISTEN_PID=%llu", (unsigned long long) getpid()) < 0 ||
1032                             asprintf(our_env + n_env++, "LISTEN_FDS=%u", n_fds) < 0) {
1033                                 r = EXIT_MEMORY;
1034                                 goto fail;
1035                         }
1036
1037                 if (home)
1038                         if (asprintf(our_env + n_env++, "HOME=%s", home) < 0) {
1039                                 r = EXIT_MEMORY;
1040                                 goto fail;
1041                         }
1042
1043                 if (username)
1044                         if (asprintf(our_env + n_env++, "LOGNAME=%s", username) < 0 ||
1045                             asprintf(our_env + n_env++, "USER=%s", username) < 0) {
1046                                 r = EXIT_MEMORY;
1047                                 goto fail;
1048                         }
1049
1050                 assert(n_env <= 6);
1051
1052                 if (!(final_env = strv_env_merge(environment, our_env, context->environment, NULL))) {
1053                         r = EXIT_MEMORY;
1054                         goto fail;
1055                 }
1056
1057                 execve(command->path, argv, final_env);
1058                 r = EXIT_EXEC;
1059
1060         fail:
1061                 strv_free(our_env);
1062                 strv_free(final_env);
1063
1064                 if (saved_stdin >= 0)
1065                         close_nointr_nofail(saved_stdin);
1066
1067                 if (saved_stdout >= 0)
1068                         close_nointr_nofail(saved_stdout);
1069
1070                 _exit(r);
1071         }
1072
1073         /* We add the new process to the cgroup both in the child (so
1074          * that we can be sure that no user code is ever executed
1075          * outside of the cgroup) and in the parent (so that we can be
1076          * sure that when we kill the cgroup the process will be
1077          * killed too). */
1078         if (cgroup_bondings)
1079                 cgroup_bonding_install_list(cgroup_bondings, pid);
1080
1081         log_debug("Forked %s as %llu", command->path, (unsigned long long) pid);
1082
1083         command->exec_status.pid = pid;
1084         command->exec_status.start_timestamp = now(CLOCK_REALTIME);
1085
1086         *ret = pid;
1087         return 0;
1088 }
1089
1090 void exec_context_init(ExecContext *c) {
1091         assert(c);
1092
1093         c->umask = 0002;
1094         c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0);
1095         c->cpu_sched_policy = SCHED_OTHER;
1096         c->syslog_priority = LOG_DAEMON|LOG_INFO;
1097         c->mount_flags = MS_SHARED;
1098 }
1099
1100 void exec_context_done(ExecContext *c) {
1101         unsigned l;
1102
1103         assert(c);
1104
1105         strv_free(c->environment);
1106         c->environment = NULL;
1107
1108         for (l = 0; l < ELEMENTSOF(c->rlimit); l++) {
1109                 free(c->rlimit[l]);
1110                 c->rlimit[l] = NULL;
1111         }
1112
1113         free(c->working_directory);
1114         c->working_directory = NULL;
1115         free(c->root_directory);
1116         c->root_directory = NULL;
1117
1118         free(c->tty_path);
1119         c->tty_path = NULL;
1120
1121         free(c->tcpwrap_name);
1122         c->tcpwrap_name = NULL;
1123
1124         free(c->syslog_identifier);
1125         c->syslog_identifier = NULL;
1126
1127         free(c->user);
1128         c->user = NULL;
1129
1130         free(c->group);
1131         c->group = NULL;
1132
1133         strv_free(c->supplementary_groups);
1134         c->supplementary_groups = NULL;
1135
1136         if (c->capabilities) {
1137                 cap_free(c->capabilities);
1138                 c->capabilities = NULL;
1139         }
1140
1141         strv_free(c->read_only_dirs);
1142         c->read_only_dirs = NULL;
1143
1144         strv_free(c->read_write_dirs);
1145         c->read_write_dirs = NULL;
1146
1147         strv_free(c->inaccessible_dirs);
1148         c->inaccessible_dirs = NULL;
1149 }
1150
1151 void exec_command_done(ExecCommand *c) {
1152         assert(c);
1153
1154         free(c->path);
1155         c->path = NULL;
1156
1157         strv_free(c->argv);
1158         c->argv = NULL;
1159 }
1160
1161 void exec_command_done_array(ExecCommand *c, unsigned n) {
1162         unsigned i;
1163
1164         for (i = 0; i < n; i++)
1165                 exec_command_done(c+i);
1166 }
1167
1168 void exec_command_free_list(ExecCommand *c) {
1169         ExecCommand *i;
1170
1171         while ((i = c)) {
1172                 LIST_REMOVE(ExecCommand, command, c, i);
1173                 exec_command_done(i);
1174                 free(i);
1175         }
1176 }
1177
1178 void exec_command_free_array(ExecCommand **c, unsigned n) {
1179         unsigned i;
1180
1181         for (i = 0; i < n; i++) {
1182                 exec_command_free_list(c[i]);
1183                 c[i] = NULL;
1184         }
1185 }
1186
1187 static void strv_fprintf(FILE *f, char **l) {
1188         char **g;
1189
1190         assert(f);
1191
1192         STRV_FOREACH(g, l)
1193                 fprintf(f, " %s", *g);
1194 }
1195
1196 void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) {
1197         char ** e;
1198         unsigned i;
1199
1200         assert(c);
1201         assert(f);
1202
1203         if (!prefix)
1204                 prefix = "";
1205
1206         fprintf(f,
1207                 "%sUMask: %04o\n"
1208                 "%sWorkingDirectory: %s\n"
1209                 "%sRootDirectory: %s\n"
1210                 "%sNonBlocking: %s\n"
1211                 "%sPrivateTmp: %s\n",
1212                 prefix, c->umask,
1213                 prefix, c->working_directory ? c->working_directory : "/",
1214                 prefix, c->root_directory ? c->root_directory : "/",
1215                 prefix, yes_no(c->non_blocking),
1216                 prefix, yes_no(c->private_tmp));
1217
1218         if (c->environment)
1219                 for (e = c->environment; *e; e++)
1220                         fprintf(f, "%sEnvironment: %s\n", prefix, *e);
1221
1222         if (c->tcpwrap_name)
1223                 fprintf(f,
1224                         "%sTCPWrapName: %s\n",
1225                         prefix, c->tcpwrap_name);
1226
1227         if (c->nice_set)
1228                 fprintf(f,
1229                         "%sNice: %i\n",
1230                         prefix, c->nice);
1231
1232         if (c->oom_adjust_set)
1233                 fprintf(f,
1234                         "%sOOMAdjust: %i\n",
1235                         prefix, c->oom_adjust);
1236
1237         for (i = 0; i < RLIM_NLIMITS; i++)
1238                 if (c->rlimit[i])
1239                         fprintf(f, "%s%s: %llu\n", prefix, rlimit_to_string(i), (unsigned long long) c->rlimit[i]->rlim_max);
1240
1241         if (c->ioprio_set)
1242                 fprintf(f,
1243                         "%sIOSchedulingClass: %s\n"
1244                         "%sIOPriority: %i\n",
1245                         prefix, ioprio_class_to_string(IOPRIO_PRIO_CLASS(c->ioprio)),
1246                         prefix, (int) IOPRIO_PRIO_DATA(c->ioprio));
1247
1248         if (c->cpu_sched_set)
1249                 fprintf(f,
1250                         "%sCPUSchedulingPolicy: %s\n"
1251                         "%sCPUSchedulingPriority: %i\n"
1252                         "%sCPUSchedulingResetOnFork: %s\n",
1253                         prefix, sched_policy_to_string(c->cpu_sched_policy),
1254                         prefix, c->cpu_sched_priority,
1255                         prefix, yes_no(c->cpu_sched_reset_on_fork));
1256
1257         if (c->cpu_affinity_set) {
1258                 fprintf(f, "%sCPUAffinity:", prefix);
1259                 for (i = 0; i < CPU_SETSIZE; i++)
1260                         if (CPU_ISSET(i, &c->cpu_affinity))
1261                                 fprintf(f, " %i", i);
1262                 fputs("\n", f);
1263         }
1264
1265         if (c->timer_slack_ns_set)
1266                 fprintf(f, "%sTimerSlackNS: %lu\n", prefix, c->timer_slack_ns);
1267
1268         fprintf(f,
1269                 "%sStandardInput: %s\n"
1270                 "%sStandardOutput: %s\n"
1271                 "%sStandardError: %s\n",
1272                 prefix, exec_input_to_string(c->std_input),
1273                 prefix, exec_output_to_string(c->std_output),
1274                 prefix, exec_output_to_string(c->std_error));
1275
1276         if (c->tty_path)
1277                 fprintf(f,
1278                         "%sTTYPath: %s\n",
1279                         prefix, c->tty_path);
1280
1281         if (c->std_output == EXEC_OUTPUT_SYSLOG || c->std_output == EXEC_OUTPUT_KMSG ||
1282             c->std_error == EXEC_OUTPUT_SYSLOG || c->std_error == EXEC_OUTPUT_KMSG)
1283                 fprintf(f,
1284                         "%sSyslogFacility: %s\n"
1285                         "%sSyslogLevel: %s\n",
1286                         prefix, log_facility_to_string(LOG_FAC(c->syslog_priority)),
1287                         prefix, log_level_to_string(LOG_PRI(c->syslog_priority)));
1288
1289         if (c->capabilities) {
1290                 char *t;
1291                 if ((t = cap_to_text(c->capabilities, NULL))) {
1292                         fprintf(f, "%sCapabilities: %s\n",
1293                                 prefix, t);
1294                         cap_free(t);
1295                 }
1296         }
1297
1298         if (c->secure_bits)
1299                 fprintf(f, "%sSecure Bits:%s%s%s%s%s%s\n",
1300                         prefix,
1301                         (c->secure_bits & SECURE_KEEP_CAPS) ? " keep-caps" : "",
1302                         (c->secure_bits & SECURE_KEEP_CAPS_LOCKED) ? " keep-caps-locked" : "",
1303                         (c->secure_bits & SECURE_NO_SETUID_FIXUP) ? " no-setuid-fixup" : "",
1304                         (c->secure_bits & SECURE_NO_SETUID_FIXUP_LOCKED) ? " no-setuid-fixup-locked" : "",
1305                         (c->secure_bits & SECURE_NOROOT) ? " noroot" : "",
1306                         (c->secure_bits & SECURE_NOROOT_LOCKED) ? "noroot-locked" : "");
1307
1308         if (c->capability_bounding_set_drop) {
1309                 fprintf(f, "%sCapabilityBoundingSetDrop:", prefix);
1310
1311                 for (i = 0; i <= CAP_LAST_CAP; i++)
1312                         if (c->capability_bounding_set_drop & (1 << i)) {
1313                                 char *t;
1314
1315                                 if ((t = cap_to_name(i))) {
1316                                         fprintf(f, " %s", t);
1317                                         free(t);
1318                                 }
1319                         }
1320
1321                 fputs("\n", f);
1322         }
1323
1324         if (c->user)
1325                 fprintf(f, "%sUser: %s", prefix, c->user);
1326         if (c->group)
1327                 fprintf(f, "%sGroup: %s", prefix, c->group);
1328
1329         if (strv_length(c->supplementary_groups) > 0) {
1330                 fprintf(f, "%sSupplementaryGroups:", prefix);
1331                 strv_fprintf(f, c->supplementary_groups);
1332                 fputs("\n", f);
1333         }
1334
1335         if (strv_length(c->read_write_dirs) > 0) {
1336                 fprintf(f, "%sReadWriteDirs:", prefix);
1337                 strv_fprintf(f, c->read_write_dirs);
1338                 fputs("\n", f);
1339         }
1340
1341         if (strv_length(c->read_only_dirs) > 0) {
1342                 fprintf(f, "%sReadOnlyDirs:", prefix);
1343                 strv_fprintf(f, c->read_only_dirs);
1344                 fputs("\n", f);
1345         }
1346
1347         if (strv_length(c->inaccessible_dirs) > 0) {
1348                 fprintf(f, "%sInaccessibleDirs:", prefix);
1349                 strv_fprintf(f, c->inaccessible_dirs);
1350                 fputs("\n", f);
1351         }
1352 }
1353
1354 void exec_status_fill(ExecStatus *s, pid_t pid, int code, int status) {
1355         assert(s);
1356
1357         s->pid = pid;
1358         s->exit_timestamp = now(CLOCK_REALTIME);
1359
1360         s->code = code;
1361         s->status = status;
1362 }
1363
1364 void exec_status_dump(ExecStatus *s, FILE *f, const char *prefix) {
1365         char buf[FORMAT_TIMESTAMP_MAX];
1366
1367         assert(s);
1368         assert(f);
1369
1370         if (!prefix)
1371                 prefix = "";
1372
1373         if (s->pid <= 0)
1374                 return;
1375
1376         fprintf(f,
1377                 "%sPID: %llu\n",
1378                 prefix, (unsigned long long) s->pid);
1379
1380         if (s->start_timestamp > 0)
1381                 fprintf(f,
1382                         "%sStart Timestamp: %s\n",
1383                         prefix, format_timestamp(buf, sizeof(buf), s->start_timestamp));
1384
1385         if (s->exit_timestamp > 0)
1386                 fprintf(f,
1387                         "%sExit Timestamp: %s\n"
1388                         "%sExit Code: %s\n"
1389                         "%sExit Status: %i\n",
1390                         prefix, format_timestamp(buf, sizeof(buf), s->exit_timestamp),
1391                         prefix, sigchld_code_to_string(s->code),
1392                         prefix, s->status);
1393 }
1394
1395 char *exec_command_line(char **argv) {
1396         size_t k;
1397         char *n, *p, **a;
1398         bool first = true;
1399
1400         assert(argv);
1401
1402         k = 1;
1403         STRV_FOREACH(a, argv)
1404                 k += strlen(*a)+3;
1405
1406         if (!(n = new(char, k)))
1407                 return NULL;
1408
1409         p = n;
1410         STRV_FOREACH(a, argv) {
1411
1412                 if (!first)
1413                         *(p++) = ' ';
1414                 else
1415                         first = false;
1416
1417                 if (strpbrk(*a, WHITESPACE)) {
1418                         *(p++) = '\'';
1419                         p = stpcpy(p, *a);
1420                         *(p++) = '\'';
1421                 } else
1422                         p = stpcpy(p, *a);
1423
1424         }
1425
1426         *p = 0;
1427
1428         /* FIXME: this doesn't really handle arguments that have
1429          * spaces and ticks in them */
1430
1431         return n;
1432 }
1433
1434 void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix) {
1435         char *p2;
1436         const char *prefix2;
1437
1438         char *cmd;
1439
1440         assert(c);
1441         assert(f);
1442
1443         if (!prefix)
1444                 prefix = "";
1445         p2 = strappend(prefix, "\t");
1446         prefix2 = p2 ? p2 : prefix;
1447
1448         cmd = exec_command_line(c->argv);
1449
1450         fprintf(f,
1451                 "%sCommand Line: %s\n",
1452                 prefix, cmd ? cmd : strerror(ENOMEM));
1453
1454         free(cmd);
1455
1456         exec_status_dump(&c->exec_status, f, prefix2);
1457
1458         free(p2);
1459 }
1460
1461 void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix) {
1462         assert(f);
1463
1464         if (!prefix)
1465                 prefix = "";
1466
1467         LIST_FOREACH(command, c, c)
1468                 exec_command_dump(c, f, prefix);
1469 }
1470
1471 void exec_command_append_list(ExecCommand **l, ExecCommand *e) {
1472         ExecCommand *end;
1473
1474         assert(l);
1475         assert(e);
1476
1477         if (*l) {
1478                 /* It's kinda important that we keep the order here */
1479                 LIST_FIND_TAIL(ExecCommand, command, *l, end);
1480                 LIST_INSERT_AFTER(ExecCommand, command, *l, end, e);
1481         } else
1482               *l = e;
1483 }
1484
1485 int exec_command_set(ExecCommand *c, const char *path, ...) {
1486         va_list ap;
1487         char **l, *p;
1488
1489         assert(c);
1490         assert(path);
1491
1492         va_start(ap, path);
1493         l = strv_new_ap(path, ap);
1494         va_end(ap);
1495
1496         if (!l)
1497                 return -ENOMEM;
1498
1499         if (!(p = strdup(path))) {
1500                 strv_free(l);
1501                 return -ENOMEM;
1502         }
1503
1504         free(c->path);
1505         c->path = p;
1506
1507         strv_free(c->argv);
1508         c->argv = l;
1509
1510         return 0;
1511 }
1512
1513 const char* exit_status_to_string(ExitStatus status) {
1514
1515         /* We cast to int here, so that -Wenum doesn't complain that
1516          * EXIT_SUCCESS/EXIT_FAILURE aren't in the enum */
1517
1518         switch ((int) status) {
1519
1520         case EXIT_SUCCESS:
1521                 return "SUCCESS";
1522
1523         case EXIT_FAILURE:
1524                 return "FAILURE";
1525
1526         case EXIT_INVALIDARGUMENT:
1527                 return "INVALIDARGUMENT";
1528
1529         case EXIT_NOTIMPLEMENTED:
1530                 return "NOTIMPLEMENTED";
1531
1532         case EXIT_NOPERMISSION:
1533                 return "NOPERMISSION";
1534
1535         case EXIT_NOTINSTALLED:
1536                 return "NOTINSSTALLED";
1537
1538         case EXIT_NOTCONFIGURED:
1539                 return "NOTCONFIGURED";
1540
1541         case EXIT_NOTRUNNING:
1542                 return "NOTRUNNING";
1543
1544         case EXIT_CHDIR:
1545                 return "CHDIR";
1546
1547         case EXIT_NICE:
1548                 return "NICE";
1549
1550         case EXIT_FDS:
1551                 return "FDS";
1552
1553         case EXIT_EXEC:
1554                 return "EXEC";
1555
1556         case EXIT_MEMORY:
1557                 return "MEMORY";
1558
1559         case EXIT_LIMITS:
1560                 return "LIMITS";
1561
1562         case EXIT_OOM_ADJUST:
1563                 return "OOM_ADJUST";
1564
1565         case EXIT_SIGNAL_MASK:
1566                 return "SIGNAL_MASK";
1567
1568         case EXIT_STDIN:
1569                 return "STDIN";
1570
1571         case EXIT_STDOUT:
1572                 return "STDOUT";
1573
1574         case EXIT_CHROOT:
1575                 return "CHROOT";
1576
1577         case EXIT_IOPRIO:
1578                 return "IOPRIO";
1579
1580         case EXIT_TIMERSLACK:
1581                 return "TIMERSLACK";
1582
1583         case EXIT_SECUREBITS:
1584                 return "SECUREBITS";
1585
1586         case EXIT_SETSCHEDULER:
1587                 return "SETSCHEDULER";
1588
1589         case EXIT_CPUAFFINITY:
1590                 return "CPUAFFINITY";
1591
1592         case EXIT_GROUP:
1593                 return "GROUP";
1594
1595         case EXIT_USER:
1596                 return "USER";
1597
1598         case EXIT_CAPABILITIES:
1599                 return "CAPABILITIES";
1600
1601         case EXIT_CGROUP:
1602                 return "CGROUP";
1603
1604         case EXIT_SETSID:
1605                 return "SETSID";
1606
1607         case EXIT_CONFIRM:
1608                 return "CONFIRM";
1609
1610         case EXIT_STDERR:
1611                 return "STDERR";
1612
1613         case EXIT_TCPWRAP:
1614                 return "TCPWRAP";
1615
1616         default:
1617                 return NULL;
1618         }
1619 }
1620
1621 static const char* const exec_input_table[_EXEC_INPUT_MAX] = {
1622         [EXEC_INPUT_NULL] = "null",
1623         [EXEC_INPUT_TTY] = "tty",
1624         [EXEC_INPUT_TTY_FORCE] = "tty-force",
1625         [EXEC_INPUT_TTY_FAIL] = "tty-fail",
1626         [EXEC_INPUT_SOCKET] = "socket"
1627 };
1628
1629 static const char* const exec_output_table[_EXEC_OUTPUT_MAX] = {
1630         [EXEC_OUTPUT_INHERIT] = "inherit",
1631         [EXEC_OUTPUT_NULL] = "null",
1632         [EXEC_OUTPUT_TTY] = "tty",
1633         [EXEC_OUTPUT_SYSLOG] = "syslog",
1634         [EXEC_OUTPUT_KMSG] = "kmsg",
1635         [EXEC_OUTPUT_SOCKET] = "socket"
1636 };
1637
1638 DEFINE_STRING_TABLE_LOOKUP(exec_output, ExecOutput);
1639
1640 DEFINE_STRING_TABLE_LOOKUP(exec_input, ExecInput);