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