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