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