1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
29 #include <sys/socket.h>
31 #include <sys/prctl.h>
32 #include <linux/sched.h>
33 #include <sys/types.h>
44 #include "securebits.h"
47 /* This assumes there is a 'tty' group */
50 static int shift_fds(int fds[], unsigned n_fds) {
51 int start, restart_from;
56 /* Modifies the fds array! (sorts it) */
66 for (i = start; i < (int) n_fds; i++) {
69 /* Already at right index? */
73 if ((nfd = fcntl(fds[i], F_DUPFD, i+3)) < 0)
76 close_nointr_nofail(fds[i]);
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)
94 static int flags_fds(const int fds[], unsigned n_fds, bool nonblock) {
103 /* Drops/Sets O_NONBLOCK and FD_CLOEXEC from the file flags */
105 for (i = 0; i < n_fds; i++) {
107 if ((r = fd_nonblock(fds[i], nonblock)) < 0)
110 /* We unconditionally drop FD_CLOEXEC from the fds,
111 * since after all we want to pass these fds to our
114 if ((r = fd_cloexec(fds[i], false)) < 0)
121 static const char *tty_path(const ExecContext *context) {
124 if (context->tty_path)
125 return context->tty_path;
127 return "/dev/console";
130 static int open_null_as(int flags, int nfd) {
135 if ((fd = open("/dev/null", flags|O_NOCTTY)) < 0)
139 r = dup2(fd, nfd) < 0 ? -errno : nfd;
140 close_nointr_nofail(fd);
147 static int connect_logger_as(const ExecContext *context, ExecOutput output, const char *ident, int nfd) {
151 struct sockaddr_un un;
155 assert(output < _EXEC_OUTPUT_MAX);
159 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
163 sa.sa.sa_family = AF_UNIX;
164 strncpy(sa.un.sun_path+1, LOGGER_SOCKET, sizeof(sa.un.sun_path)-1);
166 if (connect(fd, &sa.sa, sizeof(sa)) < 0) {
167 close_nointr_nofail(fd);
171 if (shutdown(fd, SHUT_RD) < 0) {
172 close_nointr_nofail(fd);
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. */
188 output == EXEC_OUTPUT_KERNEL ? "kmsg" : "syslog",
189 context->syslog_priority,
190 context->syslog_identifier ? context->syslog_identifier : ident);
193 r = dup2(fd, nfd) < 0 ? -errno : nfd;
194 close_nointr_nofail(fd);
200 static int open_terminal_as(const char *path, mode_t mode, int nfd) {
206 if ((fd = open_terminal(path, mode | O_NOCTTY)) < 0)
210 r = dup2(fd, nfd) < 0 ? -errno : nfd;
211 close_nointr_nofail(fd);
218 static bool is_terminal_input(ExecInput i) {
220 i == EXEC_INPUT_TTY ||
221 i == EXEC_INPUT_TTY_FORCE ||
222 i == EXEC_INPUT_TTY_FAIL;
225 static int setup_input(const ExecContext *context) {
228 switch (context->std_input) {
230 case EXEC_INPUT_NULL:
231 return open_null_as(O_RDONLY, STDIN_FILENO);
234 case EXEC_INPUT_TTY_FORCE:
235 case EXEC_INPUT_TTY_FAIL: {
238 if ((fd = acquire_terminal(
240 context->std_input == EXEC_INPUT_TTY_FAIL,
241 context->std_input == EXEC_INPUT_TTY_FORCE)) < 0)
244 if (fd != STDIN_FILENO) {
245 r = dup2(fd, STDIN_FILENO) < 0 ? -errno : STDIN_FILENO;
246 close_nointr_nofail(fd);
254 assert_not_reached("Unknown input type");
258 static int setup_output(const ExecContext *context, const char *ident) {
262 /* This expects the input is already set up */
264 switch (context->std_output) {
266 case EXEC_OUTPUT_INHERIT:
268 /* If the input is connected to a terminal, inherit that... */
269 if (is_terminal_input(context->std_input))
270 return dup2(STDIN_FILENO, STDOUT_FILENO) < 0 ? -errno : STDOUT_FILENO;
274 case EXEC_OUTPUT_NULL:
275 return open_null_as(O_WRONLY, STDOUT_FILENO);
277 case EXEC_OUTPUT_TTY: {
278 if (is_terminal_input(context->std_input))
279 return dup2(STDIN_FILENO, STDOUT_FILENO) < 0 ? -errno : STDOUT_FILENO;
281 /* We don't reset the terminal if this is just about output */
282 return open_terminal_as(tty_path(context), O_WRONLY, STDOUT_FILENO);
285 case EXEC_OUTPUT_SYSLOG:
286 case EXEC_OUTPUT_KERNEL:
287 return connect_logger_as(context, context->std_output, ident, STDOUT_FILENO);
290 assert_not_reached("Unknown output type");
294 static int setup_error(const ExecContext *context, const char *ident) {
298 /* This expects the input and output are already set up */
300 /* Don't change the stderr file descriptor if we inherit all
301 * the way and are not on a tty */
302 if (context->std_error == EXEC_OUTPUT_INHERIT &&
303 context->std_output == EXEC_OUTPUT_INHERIT &&
304 !is_terminal_input(context->std_input))
305 return STDERR_FILENO;
307 /* Duplicate form stdout if possible */
308 if (context->std_error == context->std_output ||
309 context->std_error == EXEC_OUTPUT_INHERIT)
310 return dup2(STDOUT_FILENO, STDERR_FILENO) < 0 ? -errno : STDERR_FILENO;
312 switch (context->std_error) {
314 case EXEC_OUTPUT_NULL:
315 return open_null_as(O_WRONLY, STDERR_FILENO);
317 case EXEC_OUTPUT_TTY:
318 if (is_terminal_input(context->std_input))
319 return dup2(STDIN_FILENO, STDERR_FILENO) < 0 ? -errno : STDERR_FILENO;
321 /* We don't reset the terminal if this is just about output */
322 return open_terminal_as(tty_path(context), O_WRONLY, STDERR_FILENO);
324 case EXEC_OUTPUT_SYSLOG:
325 case EXEC_OUTPUT_KERNEL:
326 return connect_logger_as(context, context->std_error, ident, STDERR_FILENO);
329 assert_not_reached("Unknown error type");
333 static int chown_terminal(int fd, uid_t uid) {
338 /* This might fail. What matters are the results. */
340 fchmod(fd, TTY_MODE);
342 if (fstat(fd, &st) < 0)
345 if (st.st_uid != uid || (st.st_mode & 0777) != TTY_MODE)
351 static int setup_confirm_stdio(const ExecContext *context,
353 int *_saved_stdout) {
354 int fd = -1, saved_stdin, saved_stdout = -1, r;
357 assert(_saved_stdin);
358 assert(_saved_stdout);
360 /* This returns positive EXIT_xxx return values instead of
361 * negative errno style values! */
363 if ((saved_stdin = fcntl(STDIN_FILENO, F_DUPFD, 3)) < 0)
366 if ((saved_stdout = fcntl(STDOUT_FILENO, F_DUPFD, 3)) < 0) {
371 if ((fd = acquire_terminal(
373 context->std_input == EXEC_INPUT_TTY_FAIL,
374 context->std_input == EXEC_INPUT_TTY_FORCE)) < 0) {
379 if (chown_terminal(fd, getuid()) < 0) {
384 if (dup2(fd, STDIN_FILENO) < 0) {
389 if (dup2(fd, STDOUT_FILENO) < 0) {
395 close_nointr_nofail(fd);
397 *_saved_stdin = saved_stdin;
398 *_saved_stdout = saved_stdout;
403 if (saved_stdout >= 0)
404 close_nointr_nofail(saved_stdout);
406 if (saved_stdin >= 0)
407 close_nointr_nofail(saved_stdin);
410 close_nointr_nofail(fd);
415 static int restore_conform_stdio(const ExecContext *context,
423 assert(*saved_stdin >= 0);
424 assert(saved_stdout);
425 assert(*saved_stdout >= 0);
427 /* This returns positive EXIT_xxx return values instead of
428 * negative errno style values! */
430 if (is_terminal_input(context->std_input)) {
432 /* The service wants terminal input. */
436 context->std_output == EXEC_OUTPUT_INHERIT ||
437 context->std_output == EXEC_OUTPUT_TTY;
440 /* If the service doesn't want a controlling terminal,
441 * then we need to get rid entirely of what we have
444 if (release_terminal() < 0)
447 if (dup2(*saved_stdin, STDIN_FILENO) < 0)
450 if (dup2(*saved_stdout, STDOUT_FILENO) < 0)
453 *keep_stdout = *keep_stdin = false;
459 static int get_group_creds(const char *groupname, gid_t *gid) {
466 /* We enforce some special rules for gid=0: in order to avoid
467 * NSS lookups for root we hardcode its data. */
469 if (streq(groupname, "root") || streq(groupname, "0")) {
474 if (safe_atolu(groupname, &lu) >= 0) {
476 g = getgrgid((gid_t) lu);
479 g = getgrnam(groupname);
483 return errno != 0 ? -errno : -ESRCH;
489 static int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home) {
499 /* We enforce some special rules for uid=0: in order to avoid
500 * NSS lookups for root we hardcode its data. */
502 if (streq(*username, "root") || streq(*username, "0")) {
510 if (safe_atolu(*username, &lu) >= 0) {
512 p = getpwuid((uid_t) lu);
514 /* If there are multiple users with the same id, make
515 * sure to leave $USER to the configured value instead
516 * of the first occurence in the database. However if
517 * the uid was configured by a numeric uid, then let's
518 * pick the real username from /etc/passwd. */
520 *username = p->pw_name;
523 p = getpwnam(*username);
527 return errno != 0 ? -errno : -ESRCH;
535 static int enforce_groups(const ExecContext *context, const char *username, gid_t gid) {
536 bool keep_groups = false;
541 /* Lookup and ser GID and supplementary group list. Here too
542 * we avoid NSS lookups for gid=0. */
544 if (context->group || username) {
547 if ((r = get_group_creds(context->group, &gid)) < 0)
550 /* First step, initialize groups from /etc/groups */
551 if (username && gid != 0) {
552 if (initgroups(username, gid) < 0)
558 /* Second step, set our gids */
559 if (setresgid(gid, gid, gid) < 0)
563 if (context->supplementary_groups) {
568 /* Final step, initialize any manually set supplementary groups */
569 ngroups_max = (int) sysconf(_SC_NGROUPS_MAX);
571 if (!(gids = new(gid_t, ngroups_max)))
575 if ((k = getgroups(ngroups_max, gids)) < 0) {
582 STRV_FOREACH(i, context->supplementary_groups) {
584 if (k >= ngroups_max) {
589 if ((r = get_group_creds(*i, gids+k)) < 0) {
597 if (setgroups(k, gids) < 0) {
608 static int enforce_user(const ExecContext *context, uid_t uid) {
612 /* Sets (but doesn't lookup) the uid and make sure we keep the
613 * capabilities while doing so. */
615 if (context->capabilities) {
617 static const cap_value_t bits[] = {
618 CAP_SETUID, /* Necessary so that we can run setresuid() below */
619 CAP_SETPCAP /* Necessary so that we can set PR_SET_SECUREBITS later on */
622 /* First step: If we need to keep capabilities but
623 * drop privileges we need to make sure we keep our
624 * caps, whiel we drop priviliges. */
626 int sb = context->secure_bits|SECURE_KEEP_CAPS;
628 if (prctl(PR_GET_SECUREBITS) != sb)
629 if (prctl(PR_SET_SECUREBITS, sb) < 0)
633 /* Second step: set the capabilites. This will reduce
634 * the capabilities to the minimum we need. */
636 if (!(d = cap_dup(context->capabilities)))
639 if (cap_set_flag(d, CAP_EFFECTIVE, ELEMENTSOF(bits), bits, CAP_SET) < 0 ||
640 cap_set_flag(d, CAP_PERMITTED, ELEMENTSOF(bits), bits, CAP_SET) < 0) {
646 if (cap_set_proc(d) < 0) {
655 /* Third step: actually set the uids */
656 if (setresuid(uid, uid, uid) < 0)
659 /* At this point we should have all necessary capabilities but
660 are otherwise a normal user. However, the caps might got
661 corrupted due to the setresuid() so we need clean them up
662 later. This is done outside of this call. */
667 int exec_spawn(ExecCommand *command,
669 const ExecContext *context,
670 int fds[], unsigned n_fds,
671 bool apply_permissions,
674 CGroupBonding *cgroup_bondings,
684 assert(fds || n_fds <= 0);
687 argv = command->argv;
689 if (!(line = exec_command_line(argv)))
692 log_debug("About to execute: %s", line);
696 if ((r = cgroup_bonding_realize_list(cgroup_bondings)))
699 if ((pid = fork()) < 0)
705 const char *username = NULL, *home = NULL;
706 uid_t uid = (uid_t) -1;
707 gid_t gid = (gid_t) -1;
708 char **our_env = NULL, **final_env = NULL;
710 int saved_stdout = -1, saved_stdin = -1;
711 bool keep_stdout = false, keep_stdin = false;
715 reset_all_signal_handlers();
717 if (sigemptyset(&ss) < 0 ||
718 sigprocmask(SIG_SETMASK, &ss, NULL) < 0) {
719 r = EXIT_SIGNAL_MASK;
728 umask(context->umask);
733 /* Set up terminal for the question */
734 if ((r = setup_confirm_stdio(context,
735 &saved_stdin, &saved_stdout)))
738 /* Now ask the question. */
739 if (!(line = exec_command_line(argv))) {
744 r = ask(&response, "yns", "Execute %s? [Yes, No, Skip] ", line);
747 if (r < 0 || response == 'n') {
750 } else if (response == 's') {
755 /* Release terminal for the question */
756 if ((r = restore_conform_stdio(context,
757 &saved_stdin, &saved_stdout,
758 &keep_stdin, &keep_stdout)))
763 if (setup_input(context) < 0) {
769 if (setup_output(context, file_name_from_path(command->path)) < 0) {
774 if (setup_error(context, file_name_from_path(command->path)) < 0) {
780 if ((r = cgroup_bonding_install_list(cgroup_bondings, 0)) < 0) {
785 if (context->oom_adjust_set) {
788 snprintf(t, sizeof(t), "%i", context->oom_adjust);
791 if (write_one_line_file("/proc/self/oom_adj", t) < 0) {
797 if (context->nice_set)
798 if (setpriority(PRIO_PROCESS, 0, context->nice) < 0) {
803 if (context->cpu_sched_set) {
804 struct sched_param param;
807 param.sched_priority = context->cpu_sched_priority;
809 if (sched_setscheduler(0, context->cpu_sched_policy |
810 (context->cpu_sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0), ¶m) < 0) {
811 r = EXIT_SETSCHEDULER;
816 if (context->cpu_affinity_set)
817 if (sched_setaffinity(0, sizeof(context->cpu_affinity), &context->cpu_affinity) < 0) {
818 r = EXIT_CPUAFFINITY;
822 if (context->ioprio_set)
823 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, context->ioprio) < 0) {
828 if (context->timer_slack_ns_set)
829 if (prctl(PR_SET_TIMERSLACK, context->timer_slack_ns_set) < 0) {
835 username = context->user;
836 if (get_user_creds(&username, &uid, &gid, &home) < 0) {
841 if (is_terminal_input(context->std_input))
842 if (chown_terminal(STDIN_FILENO, uid) < 0) {
848 if (apply_permissions)
849 if (enforce_groups(context, username, uid) < 0) {
855 if (context->root_directory)
856 if (chroot(context->root_directory) < 0) {
861 if (chdir(context->working_directory ? context->working_directory : "/") < 0) {
869 if (asprintf(&d, "%s/%s",
870 context->root_directory ? context->root_directory : "",
871 context->working_directory ? context->working_directory : "") < 0) {
885 if (close_all_fds(fds, n_fds) < 0 ||
886 shift_fds(fds, n_fds) < 0 ||
887 flags_fds(fds, n_fds, context->non_blocking) < 0) {
892 if (apply_permissions) {
894 for (i = 0; i < RLIMIT_NLIMITS; i++) {
895 if (!context->rlimit[i])
898 if (setrlimit(i, context->rlimit[i]) < 0) {
905 if (enforce_user(context, uid) < 0) {
910 /* PR_GET_SECUREBITS is not priviliged, while
911 * PR_SET_SECUREBITS is. So to suppress
912 * potential EPERMs we'll try not to call
913 * PR_SET_SECUREBITS unless necessary. */
914 if (prctl(PR_GET_SECUREBITS) != context->secure_bits)
915 if (prctl(PR_SET_SECUREBITS, context->secure_bits) < 0) {
920 if (context->capabilities)
921 if (cap_set_proc(context->capabilities) < 0) {
922 r = EXIT_CAPABILITIES;
927 if (!(our_env = new0(char*, 6))) {
933 if (asprintf(our_env + n_env++, "LISTEN_PID=%llu", (unsigned long long) getpid()) < 0 ||
934 asprintf(our_env + n_env++, "LISTEN_FDS=%u", n_fds) < 0) {
940 if (asprintf(our_env + n_env++, "HOME=%s", home) < 0) {
946 if (asprintf(our_env + n_env++, "LOGNAME=%s", username) < 0 ||
947 asprintf(our_env + n_env++, "USER=%s", username) < 0) {
952 if (!(final_env = strv_env_merge(environ, our_env, context->environment, NULL))) {
957 execve(command->path, argv, final_env);
962 strv_free(final_env);
964 if (saved_stdin >= 0)
965 close_nointr_nofail(saved_stdin);
967 if (saved_stdout >= 0)
968 close_nointr_nofail(saved_stdout);
973 /* We add the new process to the cgroup both in the child (so
974 * that we can be sure that no user code is ever executed
975 * outside of the cgroup) and in the parent (so that we can be
976 * sure that when we kill the cgroup the process will be
979 if ((r = cgroup_bonding_install_list(cgroup_bondings, pid)) < 0) {
984 log_debug("Forked %s as %llu", command->path, (unsigned long long) pid);
986 command->exec_status.pid = pid;
987 command->exec_status.start_timestamp = now(CLOCK_REALTIME);
993 void exec_context_init(ExecContext *c) {
998 c->oom_adjust_set = false;
1000 c->nice_set = false;
1001 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0);
1002 c->ioprio_set = false;
1003 c->cpu_sched_policy = SCHED_OTHER;
1004 c->cpu_sched_priority = 0;
1005 c->cpu_sched_set = false;
1006 CPU_ZERO(&c->cpu_affinity);
1007 c->cpu_affinity_set = false;
1008 c->timer_slack_ns = 0;
1009 c->timer_slack_ns_set = false;
1011 c->cpu_sched_reset_on_fork = false;
1012 c->non_blocking = false;
1017 c->syslog_priority = LOG_DAEMON|LOG_INFO;
1020 c->capability_bounding_set_drop = 0;
1023 void exec_context_done(ExecContext *c) {
1028 strv_free(c->environment);
1029 c->environment = NULL;
1031 for (l = 0; l < ELEMENTSOF(c->rlimit); l++) {
1033 c->rlimit[l] = NULL;
1036 free(c->working_directory);
1037 c->working_directory = NULL;
1038 free(c->root_directory);
1039 c->root_directory = NULL;
1044 free(c->syslog_identifier);
1045 c->syslog_identifier = NULL;
1053 strv_free(c->supplementary_groups);
1054 c->supplementary_groups = NULL;
1056 if (c->capabilities) {
1057 cap_free(c->capabilities);
1058 c->capabilities = NULL;
1062 void exec_command_done(ExecCommand *c) {
1072 void exec_command_done_array(ExecCommand *c, unsigned n) {
1075 for (i = 0; i < n; i++)
1076 exec_command_done(c+i);
1079 void exec_command_free_list(ExecCommand *c) {
1083 LIST_REMOVE(ExecCommand, command, c, i);
1084 exec_command_done(i);
1089 void exec_command_free_array(ExecCommand **c, unsigned n) {
1092 for (i = 0; i < n; i++) {
1093 exec_command_free_list(c[i]);
1098 void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) {
1110 "%sWorkingDirectory: %s\n"
1111 "%sRootDirectory: %s\n"
1112 "%sNonBlocking: %s\n",
1114 prefix, c->working_directory ? c->working_directory : "/",
1115 prefix, c->root_directory ? c->root_directory : "/",
1116 prefix, yes_no(c->non_blocking));
1119 for (e = c->environment; *e; e++)
1120 fprintf(f, "%sEnvironment: %s\n", prefix, *e);
1127 if (c->oom_adjust_set)
1129 "%sOOMAdjust: %i\n",
1130 prefix, c->oom_adjust);
1132 for (i = 0; i < RLIM_NLIMITS; i++)
1134 fprintf(f, "%s%s: %llu\n", prefix, rlimit_to_string(i), (unsigned long long) c->rlimit[i]->rlim_max);
1138 "%sIOSchedulingClass: %s\n"
1139 "%sIOPriority: %i\n",
1140 prefix, ioprio_class_to_string(IOPRIO_PRIO_CLASS(c->ioprio)),
1141 prefix, (int) IOPRIO_PRIO_DATA(c->ioprio));
1143 if (c->cpu_sched_set)
1145 "%sCPUSchedulingPolicy: %s\n"
1146 "%sCPUSchedulingPriority: %i\n"
1147 "%sCPUSchedulingResetOnFork: %s\n",
1148 prefix, sched_policy_to_string(c->cpu_sched_policy),
1149 prefix, c->cpu_sched_priority,
1150 prefix, yes_no(c->cpu_sched_reset_on_fork));
1152 if (c->cpu_affinity_set) {
1153 fprintf(f, "%sCPUAffinity:", prefix);
1154 for (i = 0; i < CPU_SETSIZE; i++)
1155 if (CPU_ISSET(i, &c->cpu_affinity))
1156 fprintf(f, " %i", i);
1160 if (c->timer_slack_ns_set)
1161 fprintf(f, "%sTimerSlackNS: %lu\n", prefix, c->timer_slack_ns);
1164 "%sStandardInput: %s\n"
1165 "%sStandardOutput: %s\n"
1166 "%sStandardError: %s\n",
1167 prefix, exec_input_to_string(c->std_input),
1168 prefix, exec_output_to_string(c->std_output),
1169 prefix, exec_output_to_string(c->std_error));
1174 prefix, c->tty_path);
1176 if (c->std_output == EXEC_OUTPUT_SYSLOG || c->std_output == EXEC_OUTPUT_KERNEL ||
1177 c->std_error == EXEC_OUTPUT_SYSLOG || c->std_error == EXEC_OUTPUT_KERNEL)
1179 "%sSyslogFacility: %s\n"
1180 "%sSyslogLevel: %s\n",
1181 prefix, log_facility_to_string(LOG_FAC(c->syslog_priority)),
1182 prefix, log_level_to_string(LOG_PRI(c->syslog_priority)));
1184 if (c->capabilities) {
1186 if ((t = cap_to_text(c->capabilities, NULL))) {
1187 fprintf(f, "%sCapabilities: %s\n",
1194 fprintf(f, "%sSecure Bits:%s%s%s%s%s%s\n",
1196 (c->secure_bits & SECURE_KEEP_CAPS) ? " keep-caps" : "",
1197 (c->secure_bits & SECURE_KEEP_CAPS_LOCKED) ? " keep-caps-locked" : "",
1198 (c->secure_bits & SECURE_NO_SETUID_FIXUP) ? " no-setuid-fixup" : "",
1199 (c->secure_bits & SECURE_NO_SETUID_FIXUP_LOCKED) ? " no-setuid-fixup-locked" : "",
1200 (c->secure_bits & SECURE_NOROOT) ? " noroot" : "",
1201 (c->secure_bits & SECURE_NOROOT_LOCKED) ? "noroot-locked" : "");
1203 if (c->capability_bounding_set_drop) {
1204 fprintf(f, "%sCapabilityBoundingSetDrop:", prefix);
1206 for (i = 0; i <= CAP_LAST_CAP; i++)
1207 if (c->capability_bounding_set_drop & (1 << i)) {
1210 if ((t = cap_to_name(i))) {
1211 fprintf(f, " %s", t);
1220 fprintf(f, "%sUser: %s", prefix, c->user);
1222 fprintf(f, "%sGroup: %s", prefix, c->group);
1224 if (c->supplementary_groups) {
1227 fprintf(f, "%sSupplementaryGroups:", prefix);
1229 STRV_FOREACH(g, c->supplementary_groups)
1230 fprintf(f, " %s", *g);
1236 void exec_status_fill(ExecStatus *s, pid_t pid, int code, int status) {
1240 s->exit_timestamp = now(CLOCK_REALTIME);
1246 void exec_status_dump(ExecStatus *s, FILE *f, const char *prefix) {
1247 char buf[FORMAT_TIMESTAMP_MAX];
1260 prefix, (unsigned long long) s->pid);
1262 if (s->start_timestamp > 0)
1264 "%sStart Timestamp: %s\n",
1265 prefix, format_timestamp(buf, sizeof(buf), s->start_timestamp));
1267 if (s->exit_timestamp > 0)
1269 "%sExit Timestamp: %s\n"
1271 "%sExit Status: %i\n",
1272 prefix, format_timestamp(buf, sizeof(buf), s->exit_timestamp),
1273 prefix, sigchld_code_to_string(s->code),
1277 char *exec_command_line(char **argv) {
1285 STRV_FOREACH(a, argv)
1288 if (!(n = new(char, k)))
1292 STRV_FOREACH(a, argv) {
1299 if (strpbrk(*a, WHITESPACE)) {
1310 /* FIXME: this doesn't really handle arguments that have
1311 * spaces and ticks in them */
1316 void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix) {
1318 const char *prefix2;
1327 p2 = strappend(prefix, "\t");
1328 prefix2 = p2 ? p2 : prefix;
1330 cmd = exec_command_line(c->argv);
1333 "%sCommand Line: %s\n",
1334 prefix, cmd ? cmd : strerror(ENOMEM));
1338 exec_status_dump(&c->exec_status, f, prefix2);
1343 void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix) {
1349 LIST_FOREACH(command, c, c)
1350 exec_command_dump(c, f, prefix);
1353 void exec_command_append_list(ExecCommand **l, ExecCommand *e) {
1360 /* It's kinda important that we keep the order here */
1361 LIST_FIND_TAIL(ExecCommand, command, *l, end);
1362 LIST_INSERT_AFTER(ExecCommand, command, *l, end, e);
1367 int exec_command_set(ExecCommand *c, const char *path, ...) {
1375 l = strv_new_ap(path, ap);
1381 if (!(p = strdup(path))) {
1395 const char* exit_status_to_string(ExitStatus status) {
1397 /* We cast to int here, so that -Wenum doesn't complain that
1398 * EXIT_SUCCESS/EXIT_FAILURE aren't in the enum */
1400 switch ((int) status) {
1408 case EXIT_INVALIDARGUMENT:
1409 return "INVALIDARGUMENT";
1411 case EXIT_NOTIMPLEMENTED:
1412 return "NOTIMPLEMENTED";
1414 case EXIT_NOPERMISSION:
1415 return "NOPERMISSION";
1417 case EXIT_NOTINSTALLED:
1418 return "NOTINSSTALLED";
1420 case EXIT_NOTCONFIGURED:
1421 return "NOTCONFIGURED";
1423 case EXIT_NOTRUNNING:
1424 return "NOTRUNNING";
1444 case EXIT_OOM_ADJUST:
1445 return "OOM_ADJUST";
1447 case EXIT_SIGNAL_MASK:
1448 return "SIGNAL_MASK";
1462 case EXIT_TIMERSLACK:
1463 return "TIMERSLACK";
1465 case EXIT_SECUREBITS:
1466 return "SECUREBITS";
1468 case EXIT_SETSCHEDULER:
1469 return "SETSCHEDULER";
1471 case EXIT_CPUAFFINITY:
1472 return "CPUAFFINITY";
1480 case EXIT_CAPABILITIES:
1481 return "CAPABILITIES";
1500 static const char* const exec_input_table[_EXEC_INPUT_MAX] = {
1501 [EXEC_INPUT_NULL] = "null",
1502 [EXEC_INPUT_TTY] = "tty",
1503 [EXEC_INPUT_TTY_FORCE] = "tty-force",
1504 [EXEC_INPUT_TTY_FAIL] = "tty-fail"
1507 static const char* const exec_output_table[_EXEC_OUTPUT_MAX] = {
1508 [EXEC_OUTPUT_INHERIT] = "inherit",
1509 [EXEC_OUTPUT_NULL] = "null",
1510 [EXEC_OUTPUT_TTY] = "tty",
1511 [EXEC_OUTPUT_SYSLOG] = "syslog",
1512 [EXEC_OUTPUT_KERNEL] = "kernel"
1515 DEFINE_STRING_TABLE_LOOKUP(exec_output, ExecOutput);
1517 DEFINE_STRING_TABLE_LOOKUP(exec_input, ExecInput);