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 fixup_input(const ExecContext *context, int socket_fd) {
228 if (socket_fd < 0 && context->std_input == EXEC_INPUT_SOCKET)
229 return EXEC_INPUT_NULL;
231 return context->std_input;
234 static int fixup_output(const ExecContext *context, int socket_fd) {
237 if (socket_fd < 0 && context->std_output == EXEC_OUTPUT_SOCKET)
238 return EXEC_OUTPUT_INHERIT;
240 return context->std_output;
243 static int fixup_error(const ExecContext *context, int socket_fd) {
246 if (socket_fd < 0 && context->std_error == EXEC_OUTPUT_SOCKET)
247 return EXEC_OUTPUT_INHERIT;
249 return context->std_error;
252 static int setup_input(const ExecContext *context, int socket_fd) {
257 i = fixup_input(context, socket_fd);
261 case EXEC_INPUT_NULL:
262 return open_null_as(O_RDONLY, STDIN_FILENO);
265 case EXEC_INPUT_TTY_FORCE:
266 case EXEC_INPUT_TTY_FAIL: {
269 if ((fd = acquire_terminal(
271 i == EXEC_INPUT_TTY_FAIL,
272 i == EXEC_INPUT_TTY_FORCE)) < 0)
275 if (fd != STDIN_FILENO) {
276 r = dup2(fd, STDIN_FILENO) < 0 ? -errno : STDIN_FILENO;
277 close_nointr_nofail(fd);
284 case EXEC_INPUT_SOCKET:
285 return dup2(socket_fd, STDIN_FILENO) < 0 ? -errno : STDIN_FILENO;
288 assert_not_reached("Unknown input type");
292 static int setup_output(const ExecContext *context, int socket_fd, const char *ident) {
299 i = fixup_input(context, socket_fd);
300 o = fixup_output(context, socket_fd);
302 /* This expects the input is already set up */
306 case EXEC_OUTPUT_INHERIT:
308 /* If the input is connected to a terminal, inherit that... */
309 if (is_terminal_input(i) || i == EXEC_INPUT_SOCKET)
310 return dup2(STDIN_FILENO, STDOUT_FILENO) < 0 ? -errno : STDOUT_FILENO;
314 case EXEC_OUTPUT_NULL:
315 return open_null_as(O_WRONLY, STDOUT_FILENO);
317 case EXEC_OUTPUT_TTY:
318 if (is_terminal_input(i))
319 return dup2(STDIN_FILENO, STDOUT_FILENO) < 0 ? -errno : STDOUT_FILENO;
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);
324 case EXEC_OUTPUT_SYSLOG:
325 case EXEC_OUTPUT_KERNEL:
326 return connect_logger_as(context, o, ident, STDOUT_FILENO);
328 case EXEC_OUTPUT_SOCKET:
329 assert(socket_fd >= 0);
330 return dup2(socket_fd, STDOUT_FILENO) < 0 ? -errno : STDOUT_FILENO;
333 assert_not_reached("Unknown output type");
337 static int setup_error(const ExecContext *context, int socket_fd, const char *ident) {
344 i = fixup_input(context, socket_fd);
345 o = fixup_output(context, socket_fd);
346 e = fixup_error(context, socket_fd);
348 /* This expects the input and output are already set up */
350 /* Don't change the stderr file descriptor if we inherit all
351 * the way and are not on a tty */
352 if (e == EXEC_OUTPUT_INHERIT &&
353 o == EXEC_OUTPUT_INHERIT &&
354 !is_terminal_input(i))
355 return STDERR_FILENO;
357 /* Duplicate form stdout if possible */
358 if (e == o || e == EXEC_OUTPUT_INHERIT)
359 return dup2(STDOUT_FILENO, STDERR_FILENO) < 0 ? -errno : STDERR_FILENO;
363 case EXEC_OUTPUT_NULL:
364 return open_null_as(O_WRONLY, STDERR_FILENO);
366 case EXEC_OUTPUT_TTY:
367 if (is_terminal_input(i))
368 return dup2(STDIN_FILENO, STDERR_FILENO) < 0 ? -errno : STDERR_FILENO;
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);
373 case EXEC_OUTPUT_SYSLOG:
374 case EXEC_OUTPUT_KERNEL:
375 return connect_logger_as(context, e, ident, STDERR_FILENO);
377 case EXEC_OUTPUT_SOCKET:
378 assert(socket_fd >= 0);
379 return dup2(socket_fd, STDERR_FILENO) < 0 ? -errno : STDERR_FILENO;
382 assert_not_reached("Unknown error type");
386 static int chown_terminal(int fd, uid_t uid) {
391 /* This might fail. What matters are the results. */
393 fchmod(fd, TTY_MODE);
395 if (fstat(fd, &st) < 0)
398 if (st.st_uid != uid || (st.st_mode & 0777) != TTY_MODE)
404 static int setup_confirm_stdio(const ExecContext *context,
406 int *_saved_stdout) {
407 int fd = -1, saved_stdin, saved_stdout = -1, r;
410 assert(_saved_stdin);
411 assert(_saved_stdout);
413 /* This returns positive EXIT_xxx return values instead of
414 * negative errno style values! */
416 if ((saved_stdin = fcntl(STDIN_FILENO, F_DUPFD, 3)) < 0)
419 if ((saved_stdout = fcntl(STDOUT_FILENO, F_DUPFD, 3)) < 0) {
424 if ((fd = acquire_terminal(
426 context->std_input == EXEC_INPUT_TTY_FAIL,
427 context->std_input == EXEC_INPUT_TTY_FORCE)) < 0) {
432 if (chown_terminal(fd, getuid()) < 0) {
437 if (dup2(fd, STDIN_FILENO) < 0) {
442 if (dup2(fd, STDOUT_FILENO) < 0) {
448 close_nointr_nofail(fd);
450 *_saved_stdin = saved_stdin;
451 *_saved_stdout = saved_stdout;
456 if (saved_stdout >= 0)
457 close_nointr_nofail(saved_stdout);
459 if (saved_stdin >= 0)
460 close_nointr_nofail(saved_stdin);
463 close_nointr_nofail(fd);
468 static int restore_conform_stdio(const ExecContext *context,
476 assert(*saved_stdin >= 0);
477 assert(saved_stdout);
478 assert(*saved_stdout >= 0);
480 /* This returns positive EXIT_xxx return values instead of
481 * negative errno style values! */
483 if (is_terminal_input(context->std_input)) {
485 /* The service wants terminal input. */
489 context->std_output == EXEC_OUTPUT_INHERIT ||
490 context->std_output == EXEC_OUTPUT_TTY;
493 /* If the service doesn't want a controlling terminal,
494 * then we need to get rid entirely of what we have
497 if (release_terminal() < 0)
500 if (dup2(*saved_stdin, STDIN_FILENO) < 0)
503 if (dup2(*saved_stdout, STDOUT_FILENO) < 0)
506 *keep_stdout = *keep_stdin = false;
512 static int get_group_creds(const char *groupname, gid_t *gid) {
519 /* We enforce some special rules for gid=0: in order to avoid
520 * NSS lookups for root we hardcode its data. */
522 if (streq(groupname, "root") || streq(groupname, "0")) {
527 if (safe_atolu(groupname, &lu) >= 0) {
529 g = getgrgid((gid_t) lu);
532 g = getgrnam(groupname);
536 return errno != 0 ? -errno : -ESRCH;
542 static int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home) {
552 /* We enforce some special rules for uid=0: in order to avoid
553 * NSS lookups for root we hardcode its data. */
555 if (streq(*username, "root") || streq(*username, "0")) {
563 if (safe_atolu(*username, &lu) >= 0) {
565 p = getpwuid((uid_t) lu);
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. */
573 *username = p->pw_name;
576 p = getpwnam(*username);
580 return errno != 0 ? -errno : -ESRCH;
588 static int enforce_groups(const ExecContext *context, const char *username, gid_t gid) {
589 bool keep_groups = false;
594 /* Lookup and ser GID and supplementary group list. Here too
595 * we avoid NSS lookups for gid=0. */
597 if (context->group || username) {
600 if ((r = get_group_creds(context->group, &gid)) < 0)
603 /* First step, initialize groups from /etc/groups */
604 if (username && gid != 0) {
605 if (initgroups(username, gid) < 0)
611 /* Second step, set our gids */
612 if (setresgid(gid, gid, gid) < 0)
616 if (context->supplementary_groups) {
621 /* Final step, initialize any manually set supplementary groups */
622 ngroups_max = (int) sysconf(_SC_NGROUPS_MAX);
624 if (!(gids = new(gid_t, ngroups_max)))
628 if ((k = getgroups(ngroups_max, gids)) < 0) {
635 STRV_FOREACH(i, context->supplementary_groups) {
637 if (k >= ngroups_max) {
642 if ((r = get_group_creds(*i, gids+k)) < 0) {
650 if (setgroups(k, gids) < 0) {
661 static int enforce_user(const ExecContext *context, uid_t uid) {
665 /* Sets (but doesn't lookup) the uid and make sure we keep the
666 * capabilities while doing so. */
668 if (context->capabilities) {
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 */
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. */
679 int sb = context->secure_bits|SECURE_KEEP_CAPS;
681 if (prctl(PR_GET_SECUREBITS) != sb)
682 if (prctl(PR_SET_SECUREBITS, sb) < 0)
686 /* Second step: set the capabilites. This will reduce
687 * the capabilities to the minimum we need. */
689 if (!(d = cap_dup(context->capabilities)))
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) {
699 if (cap_set_proc(d) < 0) {
708 /* Third step: actually set the uids */
709 if (setresuid(uid, uid, uid) < 0)
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. */
720 int exec_spawn(ExecCommand *command,
722 const ExecContext *context,
723 int fds[], unsigned n_fds,
724 bool apply_permissions,
727 CGroupBonding *cgroup_bondings,
738 assert(fds || n_fds <= 0);
740 if (context->std_input == EXEC_INPUT_SOCKET ||
741 context->std_output == EXEC_OUTPUT_SOCKET ||
742 context->std_error == EXEC_OUTPUT_SOCKET) {
755 argv = command->argv;
757 if (!(line = exec_command_line(argv)))
760 log_debug("About to execute: %s", line);
764 if ((r = cgroup_bonding_realize_list(cgroup_bondings)))
767 if ((pid = fork()) < 0)
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;
778 int saved_stdout = -1, saved_stdin = -1;
779 bool keep_stdout = false, keep_stdin = false;
783 reset_all_signal_handlers();
785 if (sigemptyset(&ss) < 0 ||
786 sigprocmask(SIG_SETMASK, &ss, NULL) < 0) {
787 r = EXIT_SIGNAL_MASK;
791 if (!context->no_setsid)
797 umask(context->umask);
802 /* Set up terminal for the question */
803 if ((r = setup_confirm_stdio(context,
804 &saved_stdin, &saved_stdout)))
807 /* Now ask the question. */
808 if (!(line = exec_command_line(argv))) {
813 r = ask(&response, "yns", "Execute %s? [Yes, No, Skip] ", line);
816 if (r < 0 || response == 'n') {
819 } else if (response == 's') {
824 /* Release terminal for the question */
825 if ((r = restore_conform_stdio(context,
826 &saved_stdin, &saved_stdout,
827 &keep_stdin, &keep_stdout)))
832 if (setup_input(context, socket_fd) < 0) {
838 if (setup_output(context, socket_fd, file_name_from_path(command->path)) < 0) {
843 if (setup_error(context, socket_fd, file_name_from_path(command->path)) < 0) {
849 if ((r = cgroup_bonding_install_list(cgroup_bondings, 0)) < 0) {
854 if (context->oom_adjust_set) {
857 snprintf(t, sizeof(t), "%i", context->oom_adjust);
860 if (write_one_line_file("/proc/self/oom_adj", t) < 0) {
866 if (context->nice_set)
867 if (setpriority(PRIO_PROCESS, 0, context->nice) < 0) {
872 if (context->cpu_sched_set) {
873 struct sched_param param;
876 param.sched_priority = context->cpu_sched_priority;
878 if (sched_setscheduler(0, context->cpu_sched_policy |
879 (context->cpu_sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0), ¶m) < 0) {
880 r = EXIT_SETSCHEDULER;
885 if (context->cpu_affinity_set)
886 if (sched_setaffinity(0, sizeof(context->cpu_affinity), &context->cpu_affinity) < 0) {
887 r = EXIT_CPUAFFINITY;
891 if (context->ioprio_set)
892 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, context->ioprio) < 0) {
897 if (context->timer_slack_ns_set)
898 if (prctl(PR_SET_TIMERSLACK, context->timer_slack_ns_set) < 0) {
904 username = context->user;
905 if (get_user_creds(&username, &uid, &gid, &home) < 0) {
910 if (is_terminal_input(context->std_input))
911 if (chown_terminal(STDIN_FILENO, uid) < 0) {
917 if (apply_permissions)
918 if (enforce_groups(context, username, uid) < 0) {
924 if (context->root_directory)
925 if (chroot(context->root_directory) < 0) {
930 if (chdir(context->working_directory ? context->working_directory : "/") < 0) {
938 if (asprintf(&d, "%s/%s",
939 context->root_directory ? context->root_directory : "",
940 context->working_directory ? context->working_directory : "") < 0) {
954 if (close_all_fds(fds, n_fds) < 0 ||
955 shift_fds(fds, n_fds) < 0 ||
956 flags_fds(fds, n_fds, context->non_blocking) < 0) {
961 if (apply_permissions) {
963 for (i = 0; i < RLIMIT_NLIMITS; i++) {
964 if (!context->rlimit[i])
967 if (setrlimit(i, context->rlimit[i]) < 0) {
974 if (enforce_user(context, uid) < 0) {
979 /* PR_GET_SECUREBITS is not priviliged, while
980 * PR_SET_SECUREBITS is. So to suppress
981 * potential EPERMs we'll try not to call
982 * PR_SET_SECUREBITS unless necessary. */
983 if (prctl(PR_GET_SECUREBITS) != context->secure_bits)
984 if (prctl(PR_SET_SECUREBITS, context->secure_bits) < 0) {
989 if (context->capabilities)
990 if (cap_set_proc(context->capabilities) < 0) {
991 r = EXIT_CAPABILITIES;
996 if (!(our_env = new0(char*, 6))) {
1002 if (asprintf(our_env + n_env++, "LISTEN_PID=%llu", (unsigned long long) getpid()) < 0 ||
1003 asprintf(our_env + n_env++, "LISTEN_FDS=%u", n_fds) < 0) {
1009 if (asprintf(our_env + n_env++, "HOME=%s", home) < 0) {
1015 if (asprintf(our_env + n_env++, "LOGNAME=%s", username) < 0 ||
1016 asprintf(our_env + n_env++, "USER=%s", username) < 0) {
1021 if (!(final_env = strv_env_merge(environ, our_env, context->environment, NULL))) {
1026 execve(command->path, argv, final_env);
1031 strv_free(final_env);
1033 if (saved_stdin >= 0)
1034 close_nointr_nofail(saved_stdin);
1036 if (saved_stdout >= 0)
1037 close_nointr_nofail(saved_stdout);
1042 /* We add the new process to the cgroup both in the child (so
1043 * that we can be sure that no user code is ever executed
1044 * outside of the cgroup) and in the parent (so that we can be
1045 * sure that when we kill the cgroup the process will be
1047 if (cgroup_bondings)
1048 if ((r = cgroup_bonding_install_list(cgroup_bondings, pid)) < 0) {
1053 log_debug("Forked %s as %llu", command->path, (unsigned long long) pid);
1055 command->exec_status.pid = pid;
1056 command->exec_status.start_timestamp = now(CLOCK_REALTIME);
1062 void exec_context_init(ExecContext *c) {
1067 c->oom_adjust_set = false;
1069 c->nice_set = false;
1070 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0);
1071 c->ioprio_set = false;
1072 c->cpu_sched_policy = SCHED_OTHER;
1073 c->cpu_sched_priority = 0;
1074 c->cpu_sched_set = false;
1075 CPU_ZERO(&c->cpu_affinity);
1076 c->cpu_affinity_set = false;
1077 c->timer_slack_ns = 0;
1078 c->timer_slack_ns_set = false;
1080 c->cpu_sched_reset_on_fork = false;
1081 c->non_blocking = false;
1086 c->syslog_priority = LOG_DAEMON|LOG_INFO;
1089 c->capability_bounding_set_drop = 0;
1092 void exec_context_done(ExecContext *c) {
1097 strv_free(c->environment);
1098 c->environment = NULL;
1100 for (l = 0; l < ELEMENTSOF(c->rlimit); l++) {
1102 c->rlimit[l] = NULL;
1105 free(c->working_directory);
1106 c->working_directory = NULL;
1107 free(c->root_directory);
1108 c->root_directory = NULL;
1113 free(c->syslog_identifier);
1114 c->syslog_identifier = NULL;
1122 strv_free(c->supplementary_groups);
1123 c->supplementary_groups = NULL;
1125 if (c->capabilities) {
1126 cap_free(c->capabilities);
1127 c->capabilities = NULL;
1131 void exec_command_done(ExecCommand *c) {
1141 void exec_command_done_array(ExecCommand *c, unsigned n) {
1144 for (i = 0; i < n; i++)
1145 exec_command_done(c+i);
1148 void exec_command_free_list(ExecCommand *c) {
1152 LIST_REMOVE(ExecCommand, command, c, i);
1153 exec_command_done(i);
1158 void exec_command_free_array(ExecCommand **c, unsigned n) {
1161 for (i = 0; i < n; i++) {
1162 exec_command_free_list(c[i]);
1167 void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) {
1179 "%sWorkingDirectory: %s\n"
1180 "%sRootDirectory: %s\n"
1181 "%sNonBlocking: %s\n",
1183 prefix, c->working_directory ? c->working_directory : "/",
1184 prefix, c->root_directory ? c->root_directory : "/",
1185 prefix, yes_no(c->non_blocking));
1188 for (e = c->environment; *e; e++)
1189 fprintf(f, "%sEnvironment: %s\n", prefix, *e);
1196 if (c->oom_adjust_set)
1198 "%sOOMAdjust: %i\n",
1199 prefix, c->oom_adjust);
1201 for (i = 0; i < RLIM_NLIMITS; i++)
1203 fprintf(f, "%s%s: %llu\n", prefix, rlimit_to_string(i), (unsigned long long) c->rlimit[i]->rlim_max);
1207 "%sIOSchedulingClass: %s\n"
1208 "%sIOPriority: %i\n",
1209 prefix, ioprio_class_to_string(IOPRIO_PRIO_CLASS(c->ioprio)),
1210 prefix, (int) IOPRIO_PRIO_DATA(c->ioprio));
1212 if (c->cpu_sched_set)
1214 "%sCPUSchedulingPolicy: %s\n"
1215 "%sCPUSchedulingPriority: %i\n"
1216 "%sCPUSchedulingResetOnFork: %s\n",
1217 prefix, sched_policy_to_string(c->cpu_sched_policy),
1218 prefix, c->cpu_sched_priority,
1219 prefix, yes_no(c->cpu_sched_reset_on_fork));
1221 if (c->cpu_affinity_set) {
1222 fprintf(f, "%sCPUAffinity:", prefix);
1223 for (i = 0; i < CPU_SETSIZE; i++)
1224 if (CPU_ISSET(i, &c->cpu_affinity))
1225 fprintf(f, " %i", i);
1229 if (c->timer_slack_ns_set)
1230 fprintf(f, "%sTimerSlackNS: %lu\n", prefix, c->timer_slack_ns);
1233 "%sStandardInput: %s\n"
1234 "%sStandardOutput: %s\n"
1235 "%sStandardError: %s\n",
1236 prefix, exec_input_to_string(c->std_input),
1237 prefix, exec_output_to_string(c->std_output),
1238 prefix, exec_output_to_string(c->std_error));
1243 prefix, c->tty_path);
1245 if (c->std_output == EXEC_OUTPUT_SYSLOG || c->std_output == EXEC_OUTPUT_KERNEL ||
1246 c->std_error == EXEC_OUTPUT_SYSLOG || c->std_error == EXEC_OUTPUT_KERNEL)
1248 "%sSyslogFacility: %s\n"
1249 "%sSyslogLevel: %s\n",
1250 prefix, log_facility_to_string(LOG_FAC(c->syslog_priority)),
1251 prefix, log_level_to_string(LOG_PRI(c->syslog_priority)));
1253 if (c->capabilities) {
1255 if ((t = cap_to_text(c->capabilities, NULL))) {
1256 fprintf(f, "%sCapabilities: %s\n",
1263 fprintf(f, "%sSecure Bits:%s%s%s%s%s%s\n",
1265 (c->secure_bits & SECURE_KEEP_CAPS) ? " keep-caps" : "",
1266 (c->secure_bits & SECURE_KEEP_CAPS_LOCKED) ? " keep-caps-locked" : "",
1267 (c->secure_bits & SECURE_NO_SETUID_FIXUP) ? " no-setuid-fixup" : "",
1268 (c->secure_bits & SECURE_NO_SETUID_FIXUP_LOCKED) ? " no-setuid-fixup-locked" : "",
1269 (c->secure_bits & SECURE_NOROOT) ? " noroot" : "",
1270 (c->secure_bits & SECURE_NOROOT_LOCKED) ? "noroot-locked" : "");
1272 if (c->capability_bounding_set_drop) {
1273 fprintf(f, "%sCapabilityBoundingSetDrop:", prefix);
1275 for (i = 0; i <= CAP_LAST_CAP; i++)
1276 if (c->capability_bounding_set_drop & (1 << i)) {
1279 if ((t = cap_to_name(i))) {
1280 fprintf(f, " %s", t);
1289 fprintf(f, "%sUser: %s", prefix, c->user);
1291 fprintf(f, "%sGroup: %s", prefix, c->group);
1293 if (c->supplementary_groups) {
1296 fprintf(f, "%sSupplementaryGroups:", prefix);
1298 STRV_FOREACH(g, c->supplementary_groups)
1299 fprintf(f, " %s", *g);
1305 void exec_status_fill(ExecStatus *s, pid_t pid, int code, int status) {
1309 s->exit_timestamp = now(CLOCK_REALTIME);
1315 void exec_status_dump(ExecStatus *s, FILE *f, const char *prefix) {
1316 char buf[FORMAT_TIMESTAMP_MAX];
1329 prefix, (unsigned long long) s->pid);
1331 if (s->start_timestamp > 0)
1333 "%sStart Timestamp: %s\n",
1334 prefix, format_timestamp(buf, sizeof(buf), s->start_timestamp));
1336 if (s->exit_timestamp > 0)
1338 "%sExit Timestamp: %s\n"
1340 "%sExit Status: %i\n",
1341 prefix, format_timestamp(buf, sizeof(buf), s->exit_timestamp),
1342 prefix, sigchld_code_to_string(s->code),
1346 char *exec_command_line(char **argv) {
1354 STRV_FOREACH(a, argv)
1357 if (!(n = new(char, k)))
1361 STRV_FOREACH(a, argv) {
1368 if (strpbrk(*a, WHITESPACE)) {
1379 /* FIXME: this doesn't really handle arguments that have
1380 * spaces and ticks in them */
1385 void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix) {
1387 const char *prefix2;
1396 p2 = strappend(prefix, "\t");
1397 prefix2 = p2 ? p2 : prefix;
1399 cmd = exec_command_line(c->argv);
1402 "%sCommand Line: %s\n",
1403 prefix, cmd ? cmd : strerror(ENOMEM));
1407 exec_status_dump(&c->exec_status, f, prefix2);
1412 void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix) {
1418 LIST_FOREACH(command, c, c)
1419 exec_command_dump(c, f, prefix);
1422 void exec_command_append_list(ExecCommand **l, ExecCommand *e) {
1429 /* It's kinda important that we keep the order here */
1430 LIST_FIND_TAIL(ExecCommand, command, *l, end);
1431 LIST_INSERT_AFTER(ExecCommand, command, *l, end, e);
1436 int exec_command_set(ExecCommand *c, const char *path, ...) {
1444 l = strv_new_ap(path, ap);
1450 if (!(p = strdup(path))) {
1464 const char* exit_status_to_string(ExitStatus status) {
1466 /* We cast to int here, so that -Wenum doesn't complain that
1467 * EXIT_SUCCESS/EXIT_FAILURE aren't in the enum */
1469 switch ((int) status) {
1477 case EXIT_INVALIDARGUMENT:
1478 return "INVALIDARGUMENT";
1480 case EXIT_NOTIMPLEMENTED:
1481 return "NOTIMPLEMENTED";
1483 case EXIT_NOPERMISSION:
1484 return "NOPERMISSION";
1486 case EXIT_NOTINSTALLED:
1487 return "NOTINSSTALLED";
1489 case EXIT_NOTCONFIGURED:
1490 return "NOTCONFIGURED";
1492 case EXIT_NOTRUNNING:
1493 return "NOTRUNNING";
1513 case EXIT_OOM_ADJUST:
1514 return "OOM_ADJUST";
1516 case EXIT_SIGNAL_MASK:
1517 return "SIGNAL_MASK";
1531 case EXIT_TIMERSLACK:
1532 return "TIMERSLACK";
1534 case EXIT_SECUREBITS:
1535 return "SECUREBITS";
1537 case EXIT_SETSCHEDULER:
1538 return "SETSCHEDULER";
1540 case EXIT_CPUAFFINITY:
1541 return "CPUAFFINITY";
1549 case EXIT_CAPABILITIES:
1550 return "CAPABILITIES";
1569 static const char* const exec_input_table[_EXEC_INPUT_MAX] = {
1570 [EXEC_INPUT_NULL] = "null",
1571 [EXEC_INPUT_TTY] = "tty",
1572 [EXEC_INPUT_TTY_FORCE] = "tty-force",
1573 [EXEC_INPUT_TTY_FAIL] = "tty-fail",
1574 [EXEC_INPUT_SOCKET] = "socket"
1577 static const char* const exec_output_table[_EXEC_OUTPUT_MAX] = {
1578 [EXEC_OUTPUT_INHERIT] = "inherit",
1579 [EXEC_OUTPUT_NULL] = "null",
1580 [EXEC_OUTPUT_TTY] = "tty",
1581 [EXEC_OUTPUT_SYSLOG] = "syslog",
1582 [EXEC_OUTPUT_KERNEL] = "kernel",
1583 [EXEC_OUTPUT_SOCKET] = "socket"
1586 DEFINE_STRING_TABLE_LOOKUP(exec_output, ExecOutput);
1588 DEFINE_STRING_TABLE_LOOKUP(exec_input, ExecInput);