chiark / gitweb /
37b2f8412c87af5ec18c2553941e874325ce87b5
[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         c->std_input = EXEC_INPUT_NULL;
1092         c->std_output = EXEC_OUTPUT_SYSLOG;
1093         c->std_error = EXEC_OUTPUT_SYSLOG;
1094 }
1095
1096 void exec_context_done(ExecContext *c) {
1097         unsigned l;
1098
1099         assert(c);
1100
1101         strv_free(c->environment);
1102         c->environment = NULL;
1103
1104         for (l = 0; l < ELEMENTSOF(c->rlimit); l++) {
1105                 free(c->rlimit[l]);
1106                 c->rlimit[l] = NULL;
1107         }
1108
1109         free(c->working_directory);
1110         c->working_directory = NULL;
1111         free(c->root_directory);
1112         c->root_directory = NULL;
1113
1114         free(c->tty_path);
1115         c->tty_path = NULL;
1116
1117         free(c->syslog_identifier);
1118         c->syslog_identifier = NULL;
1119
1120         free(c->user);
1121         c->user = NULL;
1122
1123         free(c->group);
1124         c->group = NULL;
1125
1126         strv_free(c->supplementary_groups);
1127         c->supplementary_groups = NULL;
1128
1129         if (c->capabilities) {
1130                 cap_free(c->capabilities);
1131                 c->capabilities = NULL;
1132         }
1133
1134         strv_free(c->read_only_dirs);
1135         c->read_only_dirs = NULL;
1136
1137         strv_free(c->read_write_dirs);
1138         c->read_write_dirs = NULL;
1139
1140         strv_free(c->inaccessible_dirs);
1141         c->inaccessible_dirs = NULL;
1142 }
1143
1144 void exec_command_done(ExecCommand *c) {
1145         assert(c);
1146
1147         free(c->path);
1148         c->path = NULL;
1149
1150         strv_free(c->argv);
1151         c->argv = NULL;
1152 }
1153
1154 void exec_command_done_array(ExecCommand *c, unsigned n) {
1155         unsigned i;
1156
1157         for (i = 0; i < n; i++)
1158                 exec_command_done(c+i);
1159 }
1160
1161 void exec_command_free_list(ExecCommand *c) {
1162         ExecCommand *i;
1163
1164         while ((i = c)) {
1165                 LIST_REMOVE(ExecCommand, command, c, i);
1166                 exec_command_done(i);
1167                 free(i);
1168         }
1169 }
1170
1171 void exec_command_free_array(ExecCommand **c, unsigned n) {
1172         unsigned i;
1173
1174         for (i = 0; i < n; i++) {
1175                 exec_command_free_list(c[i]);
1176                 c[i] = NULL;
1177         }
1178 }
1179
1180 static void strv_fprintf(FILE *f, char **l) {
1181         char **g;
1182
1183         assert(f);
1184
1185         STRV_FOREACH(g, l)
1186                 fprintf(f, " %s", *g);
1187 }
1188
1189 void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) {
1190         char ** e;
1191         unsigned i;
1192
1193         assert(c);
1194         assert(f);
1195
1196         if (!prefix)
1197                 prefix = "";
1198
1199         fprintf(f,
1200                 "%sUMask: %04o\n"
1201                 "%sWorkingDirectory: %s\n"
1202                 "%sRootDirectory: %s\n"
1203                 "%sNonBlocking: %s\n"
1204                 "%sPrivateTmp: %s\n",
1205                 prefix, c->umask,
1206                 prefix, c->working_directory ? c->working_directory : "/",
1207                 prefix, c->root_directory ? c->root_directory : "/",
1208                 prefix, yes_no(c->non_blocking),
1209                 prefix, yes_no(c->private_tmp));
1210
1211         if (c->environment)
1212                 for (e = c->environment; *e; e++)
1213                         fprintf(f, "%sEnvironment: %s\n", prefix, *e);
1214
1215         if (c->nice_set)
1216                 fprintf(f,
1217                         "%sNice: %i\n",
1218                         prefix, c->nice);
1219
1220         if (c->oom_adjust_set)
1221                 fprintf(f,
1222                         "%sOOMAdjust: %i\n",
1223                         prefix, c->oom_adjust);
1224
1225         for (i = 0; i < RLIM_NLIMITS; i++)
1226                 if (c->rlimit[i])
1227                         fprintf(f, "%s%s: %llu\n", prefix, rlimit_to_string(i), (unsigned long long) c->rlimit[i]->rlim_max);
1228
1229         if (c->ioprio_set)
1230                 fprintf(f,
1231                         "%sIOSchedulingClass: %s\n"
1232                         "%sIOPriority: %i\n",
1233                         prefix, ioprio_class_to_string(IOPRIO_PRIO_CLASS(c->ioprio)),
1234                         prefix, (int) IOPRIO_PRIO_DATA(c->ioprio));
1235
1236         if (c->cpu_sched_set)
1237                 fprintf(f,
1238                         "%sCPUSchedulingPolicy: %s\n"
1239                         "%sCPUSchedulingPriority: %i\n"
1240                         "%sCPUSchedulingResetOnFork: %s\n",
1241                         prefix, sched_policy_to_string(c->cpu_sched_policy),
1242                         prefix, c->cpu_sched_priority,
1243                         prefix, yes_no(c->cpu_sched_reset_on_fork));
1244
1245         if (c->cpu_affinity_set) {
1246                 fprintf(f, "%sCPUAffinity:", prefix);
1247                 for (i = 0; i < CPU_SETSIZE; i++)
1248                         if (CPU_ISSET(i, &c->cpu_affinity))
1249                                 fprintf(f, " %i", i);
1250                 fputs("\n", f);
1251         }
1252
1253         if (c->timer_slack_ns_set)
1254                 fprintf(f, "%sTimerSlackNS: %lu\n", prefix, c->timer_slack_ns);
1255
1256         fprintf(f,
1257                 "%sStandardInput: %s\n"
1258                 "%sStandardOutput: %s\n"
1259                 "%sStandardError: %s\n",
1260                 prefix, exec_input_to_string(c->std_input),
1261                 prefix, exec_output_to_string(c->std_output),
1262                 prefix, exec_output_to_string(c->std_error));
1263
1264         if (c->tty_path)
1265                 fprintf(f,
1266                         "%sTTYPath: %s\n",
1267                         prefix, c->tty_path);
1268
1269         if (c->std_output == EXEC_OUTPUT_SYSLOG || c->std_output == EXEC_OUTPUT_KERNEL ||
1270             c->std_error == EXEC_OUTPUT_SYSLOG || c->std_error == EXEC_OUTPUT_KERNEL)
1271                 fprintf(f,
1272                         "%sSyslogFacility: %s\n"
1273                         "%sSyslogLevel: %s\n",
1274                         prefix, log_facility_to_string(LOG_FAC(c->syslog_priority)),
1275                         prefix, log_level_to_string(LOG_PRI(c->syslog_priority)));
1276
1277         if (c->capabilities) {
1278                 char *t;
1279                 if ((t = cap_to_text(c->capabilities, NULL))) {
1280                         fprintf(f, "%sCapabilities: %s\n",
1281                                 prefix, t);
1282                         cap_free(t);
1283                 }
1284         }
1285
1286         if (c->secure_bits)
1287                 fprintf(f, "%sSecure Bits:%s%s%s%s%s%s\n",
1288                         prefix,
1289                         (c->secure_bits & SECURE_KEEP_CAPS) ? " keep-caps" : "",
1290                         (c->secure_bits & SECURE_KEEP_CAPS_LOCKED) ? " keep-caps-locked" : "",
1291                         (c->secure_bits & SECURE_NO_SETUID_FIXUP) ? " no-setuid-fixup" : "",
1292                         (c->secure_bits & SECURE_NO_SETUID_FIXUP_LOCKED) ? " no-setuid-fixup-locked" : "",
1293                         (c->secure_bits & SECURE_NOROOT) ? " noroot" : "",
1294                         (c->secure_bits & SECURE_NOROOT_LOCKED) ? "noroot-locked" : "");
1295
1296         if (c->capability_bounding_set_drop) {
1297                 fprintf(f, "%sCapabilityBoundingSetDrop:", prefix);
1298
1299                 for (i = 0; i <= CAP_LAST_CAP; i++)
1300                         if (c->capability_bounding_set_drop & (1 << i)) {
1301                                 char *t;
1302
1303                                 if ((t = cap_to_name(i))) {
1304                                         fprintf(f, " %s", t);
1305                                         free(t);
1306                                 }
1307                         }
1308
1309                 fputs("\n", f);
1310         }
1311
1312         if (c->user)
1313                 fprintf(f, "%sUser: %s", prefix, c->user);
1314         if (c->group)
1315                 fprintf(f, "%sGroup: %s", prefix, c->group);
1316
1317         if (strv_length(c->supplementary_groups) > 0) {
1318                 fprintf(f, "%sSupplementaryGroups:", prefix);
1319                 strv_fprintf(f, c->supplementary_groups);
1320                 fputs("\n", f);
1321         }
1322
1323         if (strv_length(c->read_write_dirs) > 0) {
1324                 fprintf(f, "%sReadWriteDirs:", prefix);
1325                 strv_fprintf(f, c->read_write_dirs);
1326                 fputs("\n", f);
1327         }
1328
1329         if (strv_length(c->read_only_dirs) > 0) {
1330                 fprintf(f, "%sReadOnlyDirs:", prefix);
1331                 strv_fprintf(f, c->read_only_dirs);
1332                 fputs("\n", f);
1333         }
1334
1335         if (strv_length(c->inaccessible_dirs) > 0) {
1336                 fprintf(f, "%sInaccessibleDirs:", prefix);
1337                 strv_fprintf(f, c->inaccessible_dirs);
1338                 fputs("\n", f);
1339         }
1340 }
1341
1342 void exec_status_fill(ExecStatus *s, pid_t pid, int code, int status) {
1343         assert(s);
1344
1345         s->pid = pid;
1346         s->exit_timestamp = now(CLOCK_REALTIME);
1347
1348         s->code = code;
1349         s->status = status;
1350 }
1351
1352 void exec_status_dump(ExecStatus *s, FILE *f, const char *prefix) {
1353         char buf[FORMAT_TIMESTAMP_MAX];
1354
1355         assert(s);
1356         assert(f);
1357
1358         if (!prefix)
1359                 prefix = "";
1360
1361         if (s->pid <= 0)
1362                 return;
1363
1364         fprintf(f,
1365                 "%sPID: %llu\n",
1366                 prefix, (unsigned long long) s->pid);
1367
1368         if (s->start_timestamp > 0)
1369                 fprintf(f,
1370                         "%sStart Timestamp: %s\n",
1371                         prefix, format_timestamp(buf, sizeof(buf), s->start_timestamp));
1372
1373         if (s->exit_timestamp > 0)
1374                 fprintf(f,
1375                         "%sExit Timestamp: %s\n"
1376                         "%sExit Code: %s\n"
1377                         "%sExit Status: %i\n",
1378                         prefix, format_timestamp(buf, sizeof(buf), s->exit_timestamp),
1379                         prefix, sigchld_code_to_string(s->code),
1380                         prefix, s->status);
1381 }
1382
1383 char *exec_command_line(char **argv) {
1384         size_t k;
1385         char *n, *p, **a;
1386         bool first = true;
1387
1388         assert(argv);
1389
1390         k = 1;
1391         STRV_FOREACH(a, argv)
1392                 k += strlen(*a)+3;
1393
1394         if (!(n = new(char, k)))
1395                 return NULL;
1396
1397         p = n;
1398         STRV_FOREACH(a, argv) {
1399
1400                 if (!first)
1401                         *(p++) = ' ';
1402                 else
1403                         first = false;
1404
1405                 if (strpbrk(*a, WHITESPACE)) {
1406                         *(p++) = '\'';
1407                         p = stpcpy(p, *a);
1408                         *(p++) = '\'';
1409                 } else
1410                         p = stpcpy(p, *a);
1411
1412         }
1413
1414         *p = 0;
1415
1416         /* FIXME: this doesn't really handle arguments that have
1417          * spaces and ticks in them */
1418
1419         return n;
1420 }
1421
1422 void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix) {
1423         char *p2;
1424         const char *prefix2;
1425
1426         char *cmd;
1427
1428         assert(c);
1429         assert(f);
1430
1431         if (!prefix)
1432                 prefix = "";
1433         p2 = strappend(prefix, "\t");
1434         prefix2 = p2 ? p2 : prefix;
1435
1436         cmd = exec_command_line(c->argv);
1437
1438         fprintf(f,
1439                 "%sCommand Line: %s\n",
1440                 prefix, cmd ? cmd : strerror(ENOMEM));
1441
1442         free(cmd);
1443
1444         exec_status_dump(&c->exec_status, f, prefix2);
1445
1446         free(p2);
1447 }
1448
1449 void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix) {
1450         assert(f);
1451
1452         if (!prefix)
1453                 prefix = "";
1454
1455         LIST_FOREACH(command, c, c)
1456                 exec_command_dump(c, f, prefix);
1457 }
1458
1459 void exec_command_append_list(ExecCommand **l, ExecCommand *e) {
1460         ExecCommand *end;
1461
1462         assert(l);
1463         assert(e);
1464
1465         if (*l) {
1466                 /* It's kinda important that we keep the order here */
1467                 LIST_FIND_TAIL(ExecCommand, command, *l, end);
1468                 LIST_INSERT_AFTER(ExecCommand, command, *l, end, e);
1469         } else
1470               *l = e;
1471 }
1472
1473 int exec_command_set(ExecCommand *c, const char *path, ...) {
1474         va_list ap;
1475         char **l, *p;
1476
1477         assert(c);
1478         assert(path);
1479
1480         va_start(ap, path);
1481         l = strv_new_ap(path, ap);
1482         va_end(ap);
1483
1484         if (!l)
1485                 return -ENOMEM;
1486
1487         if (!(p = strdup(path))) {
1488                 strv_free(l);
1489                 return -ENOMEM;
1490         }
1491
1492         free(c->path);
1493         c->path = p;
1494
1495         strv_free(c->argv);
1496         c->argv = l;
1497
1498         return 0;
1499 }
1500
1501 const char* exit_status_to_string(ExitStatus status) {
1502
1503         /* We cast to int here, so that -Wenum doesn't complain that
1504          * EXIT_SUCCESS/EXIT_FAILURE aren't in the enum */
1505
1506         switch ((int) status) {
1507
1508         case EXIT_SUCCESS:
1509                 return "SUCCESS";
1510
1511         case EXIT_FAILURE:
1512                 return "FAILURE";
1513
1514         case EXIT_INVALIDARGUMENT:
1515                 return "INVALIDARGUMENT";
1516
1517         case EXIT_NOTIMPLEMENTED:
1518                 return "NOTIMPLEMENTED";
1519
1520         case EXIT_NOPERMISSION:
1521                 return "NOPERMISSION";
1522
1523         case EXIT_NOTINSTALLED:
1524                 return "NOTINSSTALLED";
1525
1526         case EXIT_NOTCONFIGURED:
1527                 return "NOTCONFIGURED";
1528
1529         case EXIT_NOTRUNNING:
1530                 return "NOTRUNNING";
1531
1532         case EXIT_CHDIR:
1533                 return "CHDIR";
1534
1535         case EXIT_NICE:
1536                 return "NICE";
1537
1538         case EXIT_FDS:
1539                 return "FDS";
1540
1541         case EXIT_EXEC:
1542                 return "EXEC";
1543
1544         case EXIT_MEMORY:
1545                 return "MEMORY";
1546
1547         case EXIT_LIMITS:
1548                 return "LIMITS";
1549
1550         case EXIT_OOM_ADJUST:
1551                 return "OOM_ADJUST";
1552
1553         case EXIT_SIGNAL_MASK:
1554                 return "SIGNAL_MASK";
1555
1556         case EXIT_STDIN:
1557                 return "STDIN";
1558
1559         case EXIT_STDOUT:
1560                 return "STDOUT";
1561
1562         case EXIT_CHROOT:
1563                 return "CHROOT";
1564
1565         case EXIT_IOPRIO:
1566                 return "IOPRIO";
1567
1568         case EXIT_TIMERSLACK:
1569                 return "TIMERSLACK";
1570
1571         case EXIT_SECUREBITS:
1572                 return "SECUREBITS";
1573
1574         case EXIT_SETSCHEDULER:
1575                 return "SETSCHEDULER";
1576
1577         case EXIT_CPUAFFINITY:
1578                 return "CPUAFFINITY";
1579
1580         case EXIT_GROUP:
1581                 return "GROUP";
1582
1583         case EXIT_USER:
1584                 return "USER";
1585
1586         case EXIT_CAPABILITIES:
1587                 return "CAPABILITIES";
1588
1589         case EXIT_CGROUP:
1590                 return "CGROUP";
1591
1592         case EXIT_SETSID:
1593                 return "SETSID";
1594
1595         case EXIT_CONFIRM:
1596                 return "CONFIRM";
1597
1598         case EXIT_STDERR:
1599                 return "STDERR";
1600
1601         default:
1602                 return NULL;
1603         }
1604 }
1605
1606 static const char* const exec_input_table[_EXEC_INPUT_MAX] = {
1607         [EXEC_INPUT_NULL] = "null",
1608         [EXEC_INPUT_TTY] = "tty",
1609         [EXEC_INPUT_TTY_FORCE] = "tty-force",
1610         [EXEC_INPUT_TTY_FAIL] = "tty-fail",
1611         [EXEC_INPUT_SOCKET] = "socket"
1612 };
1613
1614 static const char* const exec_output_table[_EXEC_OUTPUT_MAX] = {
1615         [EXEC_OUTPUT_INHERIT] = "inherit",
1616         [EXEC_OUTPUT_NULL] = "null",
1617         [EXEC_OUTPUT_TTY] = "tty",
1618         [EXEC_OUTPUT_SYSLOG] = "syslog",
1619         [EXEC_OUTPUT_KERNEL] = "kernel",
1620         [EXEC_OUTPUT_SOCKET] = "socket"
1621 };
1622
1623 DEFINE_STRING_TABLE_LOOKUP(exec_output, ExecOutput);
1624
1625 DEFINE_STRING_TABLE_LOOKUP(exec_input, ExecInput);