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