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