]> git.ipfire.org Git - people/ms/systemd.git/blame - execute.c
mount-setup: mount all cgroup controllers by default but make it non-fatal
[people/ms/systemd.git] / execute.c
CommitLineData
5cb5a6ff
LP
1/*-*- Mode: C; c-basic-offset: 8 -*-*/
2
a7334b09
LP
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
5cb5a6ff 22#include <assert.h>
034c6ed7
LP
23#include <dirent.h>
24#include <errno.h>
25#include <fcntl.h>
26#include <unistd.h>
44d8db9e 27#include <string.h>
309bff19 28#include <signal.h>
071830ff
LP
29#include <sys/socket.h>
30#include <sys/un.h>
94f04347 31#include <sys/prctl.h>
66ee3769 32#include <linux/sched.h>
451a074f
LP
33#include <sys/types.h>
34#include <sys/stat.h>
81a2b7ce
LP
35#include <grp.h>
36#include <pwd.h>
5cb5a6ff
LP
37
38#include "execute.h"
39#include "strv.h"
40#include "macro.h"
41#include "util.h"
acbb0225 42#include "log.h"
9eba9da4 43#include "ioprio.h"
94f04347 44#include "securebits.h"
8e274523 45#include "cgroup.h"
5cb5a6ff 46
02a51aba
LP
47/* This assumes there is a 'tty' group */
48#define TTY_MODE 0620
49
034c6ed7
LP
50static int shift_fds(int fds[], unsigned n_fds) {
51 int start, restart_from;
52
53 if (n_fds <= 0)
54 return 0;
55
a0d40ac5
LP
56 /* Modifies the fds array! (sorts it) */
57
034c6ed7
LP
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
e1f5e051 76 close_nointr_nofail(fds[i]);
034c6ed7
LP
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
c2748801 94static int flags_fds(const int fds[], unsigned n_fds, bool nonblock) {
47a71eed 95 unsigned i;
e2c76839 96 int r;
47a71eed
LP
97
98 if (n_fds <= 0)
99 return 0;
100
101 assert(fds);
102
451a074f 103 /* Drops/Sets O_NONBLOCK and FD_CLOEXEC from the file flags */
47a71eed
LP
104
105 for (i = 0; i < n_fds; i++) {
47a71eed 106
e2c76839
LP
107 if ((r = fd_nonblock(fds[i], nonblock)) < 0)
108 return r;
47a71eed 109
451a074f
LP
110 /* We unconditionally drop FD_CLOEXEC from the fds,
111 * since after all we want to pass these fds to our
112 * children */
47a71eed 113
e2c76839
LP
114 if ((r = fd_cloexec(fds[i], false)) < 0)
115 return r;
47a71eed
LP
116 }
117
118 return 0;
119}
120
80876c20
LP
121static 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
130static int open_null_as(int flags, int nfd) {
131 int fd, r;
071830ff 132
80876c20 133 assert(nfd >= 0);
071830ff 134
80876c20 135 if ((fd = open("/dev/null", flags|O_NOCTTY)) < 0)
071830ff
LP
136 return -errno;
137
80876c20
LP
138 if (fd != nfd) {
139 r = dup2(fd, nfd) < 0 ? -errno : nfd;
e1f5e051 140 close_nointr_nofail(fd);
80876c20
LP
141 } else
142 r = nfd;
071830ff 143
80876c20 144 return r;
071830ff
LP
145}
146
80876c20
LP
147static 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;
071830ff
LP
153
154 assert(context);
80876c20
LP
155 assert(output < _EXEC_OUTPUT_MAX);
156 assert(ident);
157 assert(nfd >= 0);
071830ff 158
80876c20
LP
159 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
160 return -errno;
071830ff 161
80876c20
LP
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);
071830ff 165
80876c20
LP
166 if (connect(fd, &sa.sa, sizeof(sa)) < 0) {
167 close_nointr_nofail(fd);
168 return -errno;
169 }
071830ff 170
80876c20
LP
171 if (shutdown(fd, SHUT_RD) < 0) {
172 close_nointr_nofail(fd);
173 return -errno;
174 }
071830ff 175
80876c20
LP
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;
e1f5e051 194 close_nointr_nofail(fd);
80876c20
LP
195 } else
196 r = nfd;
071830ff 197
80876c20
LP
198 return r;
199}
200static int open_terminal_as(const char *path, mode_t mode, int nfd) {
201 int fd, r;
071830ff 202
80876c20
LP
203 assert(path);
204 assert(nfd >= 0);
071830ff 205
80876c20
LP
206 if ((fd = open_terminal(path, mode | O_NOCTTY)) < 0)
207 return fd;
071830ff 208
80876c20
LP
209 if (fd != nfd) {
210 r = dup2(fd, nfd) < 0 ? -errno : nfd;
211 close_nointr_nofail(fd);
212 } else
213 r = nfd;
071830ff 214
80876c20
LP
215 return r;
216}
071830ff 217
80876c20
LP
218static 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}
071830ff 224
4f2d528d 225static int fixup_input(const ExecContext *context, int socket_fd) {
80876c20 226 assert(context);
071830ff 227
4f2d528d
LP
228 if (socket_fd < 0 && context->std_input == EXEC_INPUT_SOCKET)
229 return EXEC_INPUT_NULL;
230
231 return context->std_input;
232}
233
234static 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
243static 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
252static 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) {
071830ff 260
80876c20
LP
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;
071830ff 268
80876c20
LP
269 if ((fd = acquire_terminal(
270 tty_path(context),
4f2d528d
LP
271 i == EXEC_INPUT_TTY_FAIL,
272 i == EXEC_INPUT_TTY_FORCE)) < 0)
80876c20
LP
273 return fd;
274
275 if (fd != STDIN_FILENO) {
276 r = dup2(fd, STDIN_FILENO) < 0 ? -errno : STDIN_FILENO;
071830ff 277 close_nointr_nofail(fd);
80876c20
LP
278 } else
279 r = STDIN_FILENO;
280
281 return r;
282 }
283
4f2d528d
LP
284 case EXEC_INPUT_SOCKET:
285 return dup2(socket_fd, STDIN_FILENO) < 0 ? -errno : STDIN_FILENO;
286
80876c20
LP
287 default:
288 assert_not_reached("Unknown input type");
289 }
290}
291
4f2d528d
LP
292static int setup_output(const ExecContext *context, int socket_fd, const char *ident) {
293 ExecOutput o;
294 ExecInput i;
295
80876c20
LP
296 assert(context);
297 assert(ident);
298
4f2d528d
LP
299 i = fixup_input(context, socket_fd);
300 o = fixup_output(context, socket_fd);
301
80876c20
LP
302 /* This expects the input is already set up */
303
4f2d528d 304 switch (o) {
071830ff 305
80876c20
LP
306 case EXEC_OUTPUT_INHERIT:
307
308 /* If the input is connected to a terminal, inherit that... */
4f2d528d 309 if (is_terminal_input(i) || i == EXEC_INPUT_SOCKET)
80876c20 310 return dup2(STDIN_FILENO, STDOUT_FILENO) < 0 ? -errno : STDOUT_FILENO;
071830ff 311
4f2d528d 312 return STDIN_FILENO;
80876c20
LP
313
314 case EXEC_OUTPUT_NULL:
315 return open_null_as(O_WRONLY, STDOUT_FILENO);
316
4f2d528d
LP
317 case EXEC_OUTPUT_TTY:
318 if (is_terminal_input(i))
80876c20
LP
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);
94f04347 323
80876c20
LP
324 case EXEC_OUTPUT_SYSLOG:
325 case EXEC_OUTPUT_KERNEL:
4f2d528d
LP
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;
80876c20 331
94f04347
LP
332 default:
333 assert_not_reached("Unknown output type");
071830ff 334 }
94f04347
LP
335}
336
4f2d528d
LP
337static int setup_error(const ExecContext *context, int socket_fd, const char *ident) {
338 ExecOutput o, e;
339 ExecInput i;
340
94f04347 341 assert(context);
02a51aba 342 assert(ident);
071830ff 343
4f2d528d
LP
344 i = fixup_input(context, socket_fd);
345 o = fixup_output(context, socket_fd);
346 e = fixup_error(context, socket_fd);
347
80876c20 348 /* This expects the input and output are already set up */
94f04347 349
80876c20
LP
350 /* Don't change the stderr file descriptor if we inherit all
351 * the way and are not on a tty */
4f2d528d
LP
352 if (e == EXEC_OUTPUT_INHERIT &&
353 o == EXEC_OUTPUT_INHERIT &&
354 !is_terminal_input(i))
80876c20 355 return STDERR_FILENO;
94f04347 356
80876c20 357 /* Duplicate form stdout if possible */
4f2d528d 358 if (e == o || e == EXEC_OUTPUT_INHERIT)
80876c20 359 return dup2(STDOUT_FILENO, STDERR_FILENO) < 0 ? -errno : STDERR_FILENO;
94f04347 360
4f2d528d 361 switch (e) {
80876c20
LP
362
363 case EXEC_OUTPUT_NULL:
364 return open_null_as(O_WRONLY, STDERR_FILENO);
365
366 case EXEC_OUTPUT_TTY:
4f2d528d 367 if (is_terminal_input(i))
80876c20
LP
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:
4f2d528d
LP
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;
94f04347
LP
380
381 default:
80876c20 382 assert_not_reached("Unknown error type");
94f04347 383 }
071830ff
LP
384}
385
02a51aba
LP
386static int chown_terminal(int fd, uid_t uid) {
387 struct stat st;
388
389 assert(fd >= 0);
02a51aba
LP
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
d8b4e2e9 398 if (st.st_uid != uid || (st.st_mode & 0777) != TTY_MODE)
02a51aba
LP
399 return -EPERM;
400
401 return 0;
402}
403
80876c20
LP
404static 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
02a51aba
LP
432 if (chown_terminal(fd, getuid()) < 0) {
433 r = EXIT_STDIN;
434 goto fail;
435 }
436
80876c20
LP
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
455fail:
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
468static 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
81a2b7ce
LP
512static 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
542static 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
588static 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
661static 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. */
693ced48
LP
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 }
81a2b7ce
LP
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
9fb86720 720int exec_spawn(ExecCommand *command,
9e2f7c11 721 char **argv,
81a2b7ce 722 const ExecContext *context,
c2748801 723 int fds[], unsigned n_fds,
81a2b7ce
LP
724 bool apply_permissions,
725 bool apply_chroot,
80876c20 726 bool confirm_spawn,
8e274523 727 CGroupBonding *cgroup_bondings,
81a2b7ce
LP
728 pid_t *ret) {
729
034c6ed7 730 pid_t pid;
8e274523 731 int r;
1a63a750 732 char *line;
4f2d528d 733 int socket_fd;
034c6ed7 734
5cb5a6ff
LP
735 assert(command);
736 assert(context);
737 assert(ret);
034c6ed7
LP
738 assert(fds || n_fds <= 0);
739
4f2d528d
LP
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
9e2f7c11
LP
754 if (!argv)
755 argv = command->argv;
756
757 if (!(line = exec_command_line(argv)))
1a63a750
LP
758 return -ENOMEM;
759
760 log_debug("About to execute: %s", line);
761 free(line);
acbb0225 762
8e274523
LP
763 if (cgroup_bondings)
764 if ((r = cgroup_bonding_realize_list(cgroup_bondings)))
765 return r;
766
034c6ed7
LP
767 if ((pid = fork()) < 0)
768 return -errno;
769
770 if (pid == 0) {
8e274523 771 int i;
309bff19 772 sigset_t ss;
81a2b7ce
LP
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;
80876c20
LP
778 int saved_stdout = -1, saved_stdin = -1;
779 bool keep_stdout = false, keep_stdin = false;
309bff19 780
034c6ed7 781 /* child */
5cb5a6ff 782
7b683879
LP
783 reset_all_signal_handlers();
784
309bff19
LP
785 if (sigemptyset(&ss) < 0 ||
786 sigprocmask(SIG_SETMASK, &ss, NULL) < 0) {
787 r = EXIT_SIGNAL_MASK;
788 goto fail;
789 }
790
80876c20
LP
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. */
9e2f7c11 807 if (!(line = exec_command_line(argv))) {
80876c20 808 r = EXIT_MEMORY;
ee2b4894
LP
809 goto fail;
810 }
80876c20
LP
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;
ee2b4894
LP
820 goto fail;
821 }
80876c20
LP
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;
034c6ed7
LP
828 }
829
80876c20 830 if (!keep_stdin)
4f2d528d 831 if (setup_input(context, socket_fd) < 0) {
80876c20
LP
832 r = EXIT_STDIN;
833 goto fail;
834 }
9eba9da4 835
80876c20 836 if (!keep_stdout)
4f2d528d 837 if (setup_output(context, socket_fd, file_name_from_path(command->path)) < 0) {
80876c20
LP
838 r = EXIT_STDOUT;
839 goto fail;
840 }
94f04347 841
4f2d528d 842 if (setup_error(context, socket_fd, file_name_from_path(command->path)) < 0) {
80876c20 843 r = EXIT_STDERR;
071830ff
LP
844 goto fail;
845 }
846
8e274523
LP
847 if (cgroup_bondings)
848 if ((r = cgroup_bonding_install_list(cgroup_bondings, 0)) < 0) {
849 r = EXIT_CGROUP;
850 goto fail;
851 }
852
fb33a393
LP
853 if (context->oom_adjust_set) {
854 char t[16];
034c6ed7 855
fb33a393
LP
856 snprintf(t, sizeof(t), "%i", context->oom_adjust);
857 char_array_0(t);
034c6ed7 858
fb33a393
LP
859 if (write_one_line_file("/proc/self/oom_adj", t) < 0) {
860 r = EXIT_OOM_ADJUST;
861 goto fail;
862 }
034c6ed7
LP
863 }
864
fb33a393
LP
865 if (context->nice_set)
866 if (setpriority(PRIO_PROCESS, 0, context->nice) < 0) {
867 r = EXIT_NICE;
868 goto fail;
869 }
870
94f04347
LP
871 if (context->cpu_sched_set) {
872 struct sched_param param;
873
874 zero(param);
875 param.sched_priority = context->cpu_sched_priority;
876
38b48754
LP
877 if (sched_setscheduler(0, context->cpu_sched_policy |
878 (context->cpu_sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0), &param) < 0) {
94f04347
LP
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
9eba9da4
LP
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
94f04347
LP
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
81a2b7ce
LP
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 }
d8b4e2e9
LP
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 }
81a2b7ce
LP
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
a0d40ac5 953 if (close_all_fds(fds, n_fds) < 0 ||
47a71eed 954 shift_fds(fds, n_fds) < 0 ||
451a074f 955 flags_fds(fds, n_fds, context->non_blocking) < 0) {
034c6ed7
LP
956 r = EXIT_FDS;
957 goto fail;
958 }
959
81a2b7ce 960 if (apply_permissions) {
034c6ed7 961
81a2b7ce
LP
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 }
034c6ed7 970 }
034c6ed7 971
81a2b7ce
LP
972 if (context->user)
973 if (enforce_user(context, uid) < 0) {
974 r = EXIT_USER;
975 goto fail;
976 }
977
693ced48
LP
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 }
81a2b7ce
LP
987
988 if (context->capabilities)
989 if (cap_set_proc(context->capabilities) < 0) {
990 r = EXIT_CAPABILITIES;
991 goto fail;
992 }
94f04347
LP
993 }
994
81a2b7ce
LP
995 if (!(our_env = new0(char*, 6))) {
996 r = EXIT_MEMORY;
997 goto fail;
998 }
034c6ed7 999
81a2b7ce
LP
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 }
034c6ed7 1006
81a2b7ce
LP
1007 if (home)
1008 if (asprintf(our_env + n_env++, "HOME=%s", home) < 0) {
1009 r = EXIT_MEMORY;
1010 goto fail;
1011 }
034c6ed7 1012
81a2b7ce
LP
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 }
034c6ed7 1019
81a2b7ce
LP
1020 if (!(final_env = strv_env_merge(environ, our_env, context->environment, NULL))) {
1021 r = EXIT_MEMORY;
1022 goto fail;
1023 }
034c6ed7 1024
9e2f7c11 1025 execve(command->path, argv, final_env);
034c6ed7
LP
1026 r = EXIT_EXEC;
1027
1028 fail:
81a2b7ce
LP
1029 strv_free(our_env);
1030 strv_free(final_env);
1031
80876c20
LP
1032 if (saved_stdin >= 0)
1033 close_nointr_nofail(saved_stdin);
1034
1035 if (saved_stdout >= 0)
1036 close_nointr_nofail(saved_stdout);
1037
034c6ed7
LP
1038 _exit(r);
1039 }
1040
80876c20
LP
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 }
2da3263a 1051
81a2b7ce 1052 log_debug("Forked %s as %llu", command->path, (unsigned long long) pid);
2da3263a 1053
9fb86720
LP
1054 command->exec_status.pid = pid;
1055 command->exec_status.start_timestamp = now(CLOCK_REALTIME);
1056
034c6ed7 1057 *ret = pid;
5cb5a6ff
LP
1058 return 0;
1059}
1060
034c6ed7
LP
1061void exec_context_init(ExecContext *c) {
1062 assert(c);
1063
1064 c->umask = 0002;
034c6ed7 1065 c->oom_adjust = 0;
9eba9da4 1066 c->oom_adjust_set = false;
034c6ed7 1067 c->nice = 0;
9eba9da4
LP
1068 c->nice_set = false;
1069 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0);
1070 c->ioprio_set = false;
94f04347
LP
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;
ee2b4894
LP
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;
071830ff 1081
80876c20
LP
1082 c->std_input = 0;
1083 c->std_output = 0;
1084 c->std_error = 0;
071830ff 1085 c->syslog_priority = LOG_DAEMON|LOG_INFO;
94f04347
LP
1086
1087 c->secure_bits = 0;
1088 c->capability_bounding_set_drop = 0;
034c6ed7
LP
1089}
1090
1091void exec_context_done(ExecContext *c) {
5cb5a6ff
LP
1092 unsigned l;
1093
1094 assert(c);
1095
1096 strv_free(c->environment);
034c6ed7 1097 c->environment = NULL;
5cb5a6ff 1098
034c6ed7 1099 for (l = 0; l < ELEMENTSOF(c->rlimit); l++) {
5cb5a6ff 1100 free(c->rlimit[l]);
034c6ed7
LP
1101 c->rlimit[l] = NULL;
1102 }
1103
9eba9da4
LP
1104 free(c->working_directory);
1105 c->working_directory = NULL;
1106 free(c->root_directory);
1107 c->root_directory = NULL;
5cb5a6ff 1108
80876c20
LP
1109 free(c->tty_path);
1110 c->tty_path = NULL;
1111
071830ff
LP
1112 free(c->syslog_identifier);
1113 c->syslog_identifier = NULL;
1114
5cb5a6ff 1115 free(c->user);
034c6ed7
LP
1116 c->user = NULL;
1117
5cb5a6ff 1118 free(c->group);
034c6ed7
LP
1119 c->group = NULL;
1120
1121 strv_free(c->supplementary_groups);
1122 c->supplementary_groups = NULL;
94f04347
LP
1123
1124 if (c->capabilities) {
1125 cap_free(c->capabilities);
1126 c->capabilities = NULL;
1127 }
5cb5a6ff
LP
1128}
1129
43d0fcbd
LP
1130void 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
1140void 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
5cb5a6ff
LP
1147void exec_command_free_list(ExecCommand *c) {
1148 ExecCommand *i;
1149
1150 while ((i = c)) {
034c6ed7 1151 LIST_REMOVE(ExecCommand, command, c, i);
43d0fcbd 1152 exec_command_done(i);
5cb5a6ff
LP
1153 free(i);
1154 }
1155}
1156
034c6ed7
LP
1157void 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
5cb5a6ff 1166void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) {
94f04347
LP
1167 char ** e;
1168 unsigned i;
9eba9da4 1169
5cb5a6ff
LP
1170 assert(c);
1171 assert(f);
1172
1173 if (!prefix)
1174 prefix = "";
1175
1176 fprintf(f,
94f04347
LP
1177 "%sUMask: %04o\n"
1178 "%sWorkingDirectory: %s\n"
451a074f 1179 "%sRootDirectory: %s\n"
80876c20 1180 "%sNonBlocking: %s\n",
5cb5a6ff 1181 prefix, c->umask,
9eba9da4 1182 prefix, c->working_directory ? c->working_directory : "/",
451a074f 1183 prefix, c->root_directory ? c->root_directory : "/",
80876c20 1184 prefix, yes_no(c->non_blocking));
fb33a393 1185
94f04347
LP
1186 if (c->environment)
1187 for (e = c->environment; *e; e++)
1188 fprintf(f, "%sEnvironment: %s\n", prefix, *e);
1189
fb33a393
LP
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);
9eba9da4 1199
94f04347
LP
1200 for (i = 0; i < RLIM_NLIMITS; i++)
1201 if (c->rlimit[i])
ea430986 1202 fprintf(f, "%s%s: %llu\n", prefix, rlimit_to_string(i), (unsigned long long) c->rlimit[i]->rlim_max);
94f04347 1203
9eba9da4
LP
1204 if (c->ioprio_set)
1205 fprintf(f,
1206 "%sIOSchedulingClass: %s\n"
1207 "%sIOPriority: %i\n",
94f04347 1208 prefix, ioprio_class_to_string(IOPRIO_PRIO_CLASS(c->ioprio)),
9eba9da4 1209 prefix, (int) IOPRIO_PRIO_DATA(c->ioprio));
94f04347
LP
1210
1211 if (c->cpu_sched_set)
1212 fprintf(f,
1213 "%sCPUSchedulingPolicy: %s\n"
38b48754
LP
1214 "%sCPUSchedulingPriority: %i\n"
1215 "%sCPUSchedulingResetOnFork: %s\n",
94f04347 1216 prefix, sched_policy_to_string(c->cpu_sched_policy),
38b48754
LP
1217 prefix, c->cpu_sched_priority,
1218 prefix, yes_no(c->cpu_sched_reset_on_fork));
94f04347
LP
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,
80876c20
LP
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);
94f04347 1243
80876c20
LP
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)
94f04347
LP
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 }
5cb5a6ff
LP
1302}
1303
034c6ed7
LP
1304void exec_status_fill(ExecStatus *s, pid_t pid, int code, int status) {
1305 assert(s);
5cb5a6ff 1306
034c6ed7 1307 s->pid = pid;
9fb86720
LP
1308 s->exit_timestamp = now(CLOCK_REALTIME);
1309
034c6ed7
LP
1310 s->code = code;
1311 s->status = status;
9fb86720
LP
1312}
1313
1314void 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);
5cb5a6ff 1343}
44d8db9e 1344
9e2f7c11 1345char *exec_command_line(char **argv) {
44d8db9e
LP
1346 size_t k;
1347 char *n, *p, **a;
1348 bool first = true;
1349
9e2f7c11 1350 assert(argv);
44d8db9e 1351
9164977d 1352 k = 1;
9e2f7c11 1353 STRV_FOREACH(a, argv)
44d8db9e
LP
1354 k += strlen(*a)+3;
1355
1356 if (!(n = new(char, k)))
1357 return NULL;
1358
1359 p = n;
9e2f7c11 1360 STRV_FOREACH(a, argv) {
44d8db9e
LP
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
9164977d
LP
1376 *p = 0;
1377
44d8db9e
LP
1378 /* FIXME: this doesn't really handle arguments that have
1379 * spaces and ticks in them */
1380
1381 return n;
1382}
1383
1384void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix) {
9fb86720
LP
1385 char *p2;
1386 const char *prefix2;
1387
44d8db9e
LP
1388 char *cmd;
1389
1390 assert(c);
1391 assert(f);
1392
1393 if (!prefix)
1394 prefix = "";
9fb86720
LP
1395 p2 = strappend(prefix, "\t");
1396 prefix2 = p2 ? p2 : prefix;
44d8db9e 1397
9e2f7c11 1398 cmd = exec_command_line(c->argv);
44d8db9e
LP
1399
1400 fprintf(f,
1401 "%sCommand Line: %s\n",
1402 prefix, cmd ? cmd : strerror(ENOMEM));
1403
1404 free(cmd);
9fb86720
LP
1405
1406 exec_status_dump(&c->exec_status, f, prefix2);
1407
1408 free(p2);
44d8db9e
LP
1409}
1410
1411void 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}
94f04347 1420
a6a80b4f
LP
1421void 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
26fd040d
LP
1435int 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
80876c20 1463const char* exit_status_to_string(ExitStatus status) {
b8d3418f
LP
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) {
80876c20
LP
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
1568static 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",
4f2d528d
LP
1572 [EXEC_INPUT_TTY_FAIL] = "tty-fail",
1573 [EXEC_INPUT_SOCKET] = "socket"
80876c20
LP
1574};
1575
94f04347 1576static const char* const exec_output_table[_EXEC_OUTPUT_MAX] = {
80876c20 1577 [EXEC_OUTPUT_INHERIT] = "inherit",
94f04347 1578 [EXEC_OUTPUT_NULL] = "null",
80876c20 1579 [EXEC_OUTPUT_TTY] = "tty",
94f04347 1580 [EXEC_OUTPUT_SYSLOG] = "syslog",
4f2d528d
LP
1581 [EXEC_OUTPUT_KERNEL] = "kernel",
1582 [EXEC_OUTPUT_SOCKET] = "socket"
94f04347
LP
1583};
1584
1585DEFINE_STRING_TABLE_LOOKUP(exec_output, ExecOutput);
1586
94f04347 1587DEFINE_STRING_TABLE_LOOKUP(exec_input, ExecInput);