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>
37 #include <sys/mount.h>
46 #include "securebits.h"
48 #include "namespace.h"
50 /* This assumes there is a 'tty' group */
53 static int shift_fds(int fds[], unsigned n_fds) {
54 int start, restart_from;
59 /* Modifies the fds array! (sorts it) */
69 for (i = start; i < (int) n_fds; i++) {
72 /* Already at right index? */
76 if ((nfd = fcntl(fds[i], F_DUPFD, i+3)) < 0)
79 close_nointr_nofail(fds[i]);
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)
97 static int flags_fds(const int fds[], unsigned n_fds, bool nonblock) {
106 /* Drops/Sets O_NONBLOCK and FD_CLOEXEC from the file flags */
108 for (i = 0; i < n_fds; i++) {
110 if ((r = fd_nonblock(fds[i], nonblock)) < 0)
113 /* We unconditionally drop FD_CLOEXEC from the fds,
114 * since after all we want to pass these fds to our
117 if ((r = fd_cloexec(fds[i], false)) < 0)
124 static const char *tty_path(const ExecContext *context) {
127 if (context->tty_path)
128 return context->tty_path;
130 return "/dev/console";
133 static int open_null_as(int flags, int nfd) {
138 if ((fd = open("/dev/null", flags|O_NOCTTY)) < 0)
142 r = dup2(fd, nfd) < 0 ? -errno : nfd;
143 close_nointr_nofail(fd);
150 static int connect_logger_as(const ExecContext *context, ExecOutput output, const char *ident, int nfd) {
154 struct sockaddr_un un;
158 assert(output < _EXEC_OUTPUT_MAX);
162 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
166 sa.sa.sa_family = AF_UNIX;
167 strncpy(sa.un.sun_path+1, LOGGER_SOCKET, sizeof(sa.un.sun_path)-1);
169 if (connect(fd, &sa.sa, sizeof(sa)) < 0) {
170 close_nointr_nofail(fd);
174 if (shutdown(fd, SHUT_RD) < 0) {
175 close_nointr_nofail(fd);
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. */
192 output == EXEC_OUTPUT_KMSG ? "kmsg" : "syslog",
193 context->syslog_priority,
194 context->syslog_identifier ? context->syslog_identifier : ident,
195 !context->syslog_no_prefix);
198 r = dup2(fd, nfd) < 0 ? -errno : nfd;
199 close_nointr_nofail(fd);
205 static int open_terminal_as(const char *path, mode_t mode, int nfd) {
211 if ((fd = open_terminal(path, mode | O_NOCTTY)) < 0)
215 r = dup2(fd, nfd) < 0 ? -errno : nfd;
216 close_nointr_nofail(fd);
223 static bool is_terminal_input(ExecInput i) {
225 i == EXEC_INPUT_TTY ||
226 i == EXEC_INPUT_TTY_FORCE ||
227 i == EXEC_INPUT_TTY_FAIL;
230 static int fixup_input(ExecInput std_input, int socket_fd) {
232 if (std_input == EXEC_INPUT_SOCKET && socket_fd < 0)
233 return EXEC_INPUT_NULL;
238 static int fixup_output(ExecOutput std_output, int socket_fd) {
240 if (std_output == EXEC_OUTPUT_SOCKET && socket_fd < 0)
241 return EXEC_OUTPUT_INHERIT;
246 static int setup_input(const ExecContext *context, int socket_fd) {
251 i = fixup_input(context->std_input, socket_fd);
255 case EXEC_INPUT_NULL:
256 return open_null_as(O_RDONLY, STDIN_FILENO);
259 case EXEC_INPUT_TTY_FORCE:
260 case EXEC_INPUT_TTY_FAIL: {
263 if ((fd = acquire_terminal(
265 i == EXEC_INPUT_TTY_FAIL,
266 i == EXEC_INPUT_TTY_FORCE,
270 if (fd != STDIN_FILENO) {
271 r = dup2(fd, STDIN_FILENO) < 0 ? -errno : STDIN_FILENO;
272 close_nointr_nofail(fd);
279 case EXEC_INPUT_SOCKET:
280 return dup2(socket_fd, STDIN_FILENO) < 0 ? -errno : STDIN_FILENO;
283 assert_not_reached("Unknown input type");
287 static int setup_output(const ExecContext *context, int socket_fd, const char *ident) {
294 i = fixup_input(context->std_input, socket_fd);
295 o = fixup_output(context->std_output, socket_fd);
297 /* This expects the input is already set up */
301 case EXEC_OUTPUT_INHERIT:
303 /* If the input is connected to a terminal, inherit that... */
304 if (is_terminal_input(i) || i == EXEC_INPUT_SOCKET)
305 return dup2(STDIN_FILENO, STDOUT_FILENO) < 0 ? -errno : STDOUT_FILENO;
309 case EXEC_OUTPUT_NULL:
310 return open_null_as(O_WRONLY, STDOUT_FILENO);
312 case EXEC_OUTPUT_TTY:
313 if (is_terminal_input(i))
314 return dup2(STDIN_FILENO, STDOUT_FILENO) < 0 ? -errno : STDOUT_FILENO;
316 /* We don't reset the terminal if this is just about output */
317 return open_terminal_as(tty_path(context), O_WRONLY, STDOUT_FILENO);
319 case EXEC_OUTPUT_SYSLOG:
320 case EXEC_OUTPUT_KMSG:
321 return connect_logger_as(context, o, ident, STDOUT_FILENO);
323 case EXEC_OUTPUT_SOCKET:
324 assert(socket_fd >= 0);
325 return dup2(socket_fd, STDOUT_FILENO) < 0 ? -errno : STDOUT_FILENO;
328 assert_not_reached("Unknown output type");
332 static int setup_error(const ExecContext *context, int socket_fd, const char *ident) {
339 i = fixup_input(context->std_input, socket_fd);
340 o = fixup_output(context->std_output, socket_fd);
341 e = fixup_output(context->std_error, socket_fd);
343 /* This expects the input and output are already set up */
345 /* Don't change the stderr file descriptor if we inherit all
346 * the way and are not on a tty */
347 if (e == EXEC_OUTPUT_INHERIT &&
348 o == EXEC_OUTPUT_INHERIT &&
349 !is_terminal_input(i))
350 return STDERR_FILENO;
352 /* Duplicate form stdout if possible */
353 if (e == o || e == EXEC_OUTPUT_INHERIT)
354 return dup2(STDOUT_FILENO, STDERR_FILENO) < 0 ? -errno : STDERR_FILENO;
358 case EXEC_OUTPUT_NULL:
359 return open_null_as(O_WRONLY, STDERR_FILENO);
361 case EXEC_OUTPUT_TTY:
362 if (is_terminal_input(i))
363 return dup2(STDIN_FILENO, STDERR_FILENO) < 0 ? -errno : STDERR_FILENO;
365 /* We don't reset the terminal if this is just about output */
366 return open_terminal_as(tty_path(context), O_WRONLY, STDERR_FILENO);
368 case EXEC_OUTPUT_SYSLOG:
369 case EXEC_OUTPUT_KMSG:
370 return connect_logger_as(context, e, ident, STDERR_FILENO);
372 case EXEC_OUTPUT_SOCKET:
373 assert(socket_fd >= 0);
374 return dup2(socket_fd, STDERR_FILENO) < 0 ? -errno : STDERR_FILENO;
377 assert_not_reached("Unknown error type");
381 static int chown_terminal(int fd, uid_t uid) {
386 /* This might fail. What matters are the results. */
387 (void) fchown(fd, uid, -1);
388 (void) fchmod(fd, TTY_MODE);
390 if (fstat(fd, &st) < 0)
393 if (st.st_uid != uid || (st.st_mode & 0777) != TTY_MODE)
399 static int setup_confirm_stdio(const ExecContext *context,
401 int *_saved_stdout) {
402 int fd = -1, saved_stdin, saved_stdout = -1, r;
405 assert(_saved_stdin);
406 assert(_saved_stdout);
408 /* This returns positive EXIT_xxx return values instead of
409 * negative errno style values! */
411 if ((saved_stdin = fcntl(STDIN_FILENO, F_DUPFD, 3)) < 0)
414 if ((saved_stdout = fcntl(STDOUT_FILENO, F_DUPFD, 3)) < 0) {
419 if ((fd = acquire_terminal(
421 context->std_input == EXEC_INPUT_TTY_FAIL,
422 context->std_input == EXEC_INPUT_TTY_FORCE,
428 if (chown_terminal(fd, getuid()) < 0) {
433 if (dup2(fd, STDIN_FILENO) < 0) {
438 if (dup2(fd, STDOUT_FILENO) < 0) {
444 close_nointr_nofail(fd);
446 *_saved_stdin = saved_stdin;
447 *_saved_stdout = saved_stdout;
452 if (saved_stdout >= 0)
453 close_nointr_nofail(saved_stdout);
455 if (saved_stdin >= 0)
456 close_nointr_nofail(saved_stdin);
459 close_nointr_nofail(fd);
464 static int restore_confirm_stdio(const ExecContext *context,
472 assert(*saved_stdin >= 0);
473 assert(saved_stdout);
474 assert(*saved_stdout >= 0);
476 /* This returns positive EXIT_xxx return values instead of
477 * negative errno style values! */
479 if (is_terminal_input(context->std_input)) {
481 /* The service wants terminal input. */
485 context->std_output == EXEC_OUTPUT_INHERIT ||
486 context->std_output == EXEC_OUTPUT_TTY;
489 /* If the service doesn't want a controlling terminal,
490 * then we need to get rid entirely of what we have
493 if (release_terminal() < 0)
496 if (dup2(*saved_stdin, STDIN_FILENO) < 0)
499 if (dup2(*saved_stdout, STDOUT_FILENO) < 0)
502 *keep_stdout = *keep_stdin = false;
508 static int get_group_creds(const char *groupname, gid_t *gid) {
515 /* We enforce some special rules for gid=0: in order to avoid
516 * NSS lookups for root we hardcode its data. */
518 if (streq(groupname, "root") || streq(groupname, "0")) {
523 if (safe_atolu(groupname, &lu) >= 0) {
525 g = getgrgid((gid_t) lu);
528 g = getgrnam(groupname);
532 return errno != 0 ? -errno : -ESRCH;
538 static int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home) {
548 /* We enforce some special rules for uid=0: in order to avoid
549 * NSS lookups for root we hardcode its data. */
551 if (streq(*username, "root") || streq(*username, "0")) {
559 if (safe_atolu(*username, &lu) >= 0) {
561 p = getpwuid((uid_t) lu);
563 /* If there are multiple users with the same id, make
564 * sure to leave $USER to the configured value instead
565 * of the first occurence in the database. However if
566 * the uid was configured by a numeric uid, then let's
567 * pick the real username from /etc/passwd. */
569 *username = p->pw_name;
572 p = getpwnam(*username);
576 return errno != 0 ? -errno : -ESRCH;
584 static int enforce_groups(const ExecContext *context, const char *username, gid_t gid) {
585 bool keep_groups = false;
590 /* Lookup and ser GID and supplementary group list. Here too
591 * we avoid NSS lookups for gid=0. */
593 if (context->group || username) {
596 if ((r = get_group_creds(context->group, &gid)) < 0)
599 /* First step, initialize groups from /etc/groups */
600 if (username && gid != 0) {
601 if (initgroups(username, gid) < 0)
607 /* Second step, set our gids */
608 if (setresgid(gid, gid, gid) < 0)
612 if (context->supplementary_groups) {
617 /* Final step, initialize any manually set supplementary groups */
618 ngroups_max = (int) sysconf(_SC_NGROUPS_MAX);
620 if (!(gids = new(gid_t, ngroups_max)))
624 if ((k = getgroups(ngroups_max, gids)) < 0) {
631 STRV_FOREACH(i, context->supplementary_groups) {
633 if (k >= ngroups_max) {
638 if ((r = get_group_creds(*i, gids+k)) < 0) {
646 if (setgroups(k, gids) < 0) {
657 static int enforce_user(const ExecContext *context, uid_t uid) {
661 /* Sets (but doesn't lookup) the uid and make sure we keep the
662 * capabilities while doing so. */
664 if (context->capabilities) {
666 static const cap_value_t bits[] = {
667 CAP_SETUID, /* Necessary so that we can run setresuid() below */
668 CAP_SETPCAP /* Necessary so that we can set PR_SET_SECUREBITS later on */
671 /* First step: If we need to keep capabilities but
672 * drop privileges we need to make sure we keep our
673 * caps, whiel we drop priviliges. */
675 int sb = context->secure_bits|SECURE_KEEP_CAPS;
677 if (prctl(PR_GET_SECUREBITS) != sb)
678 if (prctl(PR_SET_SECUREBITS, sb) < 0)
682 /* Second step: set the capabilites. This will reduce
683 * the capabilities to the minimum we need. */
685 if (!(d = cap_dup(context->capabilities)))
688 if (cap_set_flag(d, CAP_EFFECTIVE, ELEMENTSOF(bits), bits, CAP_SET) < 0 ||
689 cap_set_flag(d, CAP_PERMITTED, ELEMENTSOF(bits), bits, CAP_SET) < 0) {
695 if (cap_set_proc(d) < 0) {
704 /* Third step: actually set the uids */
705 if (setresuid(uid, uid, uid) < 0)
708 /* At this point we should have all necessary capabilities but
709 are otherwise a normal user. However, the caps might got
710 corrupted due to the setresuid() so we need clean them up
711 later. This is done outside of this call. */
716 int exec_spawn(ExecCommand *command,
718 const ExecContext *context,
719 int fds[], unsigned n_fds,
721 bool apply_permissions,
724 CGroupBonding *cgroup_bondings,
735 assert(fds || n_fds <= 0);
737 if (context->std_input == EXEC_INPUT_SOCKET ||
738 context->std_output == EXEC_OUTPUT_SOCKET ||
739 context->std_error == EXEC_OUTPUT_SOCKET) {
752 argv = command->argv;
754 if (!(line = exec_command_line(argv)))
757 log_debug("About to execute: %s", line);
761 if ((r = cgroup_bonding_realize_list(cgroup_bondings)))
764 if ((pid = fork()) < 0)
770 const char *username = NULL, *home = NULL;
771 uid_t uid = (uid_t) -1;
772 gid_t gid = (gid_t) -1;
773 char **our_env = NULL, **final_env = NULL;
775 int saved_stdout = -1, saved_stdin = -1;
776 bool keep_stdout = false, keep_stdin = false;
780 reset_all_signal_handlers();
782 if (sigemptyset(&ss) < 0 ||
783 sigprocmask(SIG_SETMASK, &ss, NULL) < 0) {
784 r = EXIT_SIGNAL_MASK;
788 if (!context->no_setsid)
797 /* Set up terminal for the question */
798 if ((r = setup_confirm_stdio(context,
799 &saved_stdin, &saved_stdout)))
802 /* Now ask the question. */
803 if (!(line = exec_command_line(argv))) {
808 r = ask(&response, "yns", "Execute %s? [Yes, No, Skip] ", line);
811 if (r < 0 || response == 'n') {
814 } else if (response == 's') {
819 /* Release terminal for the question */
820 if ((r = restore_confirm_stdio(context,
821 &saved_stdin, &saved_stdout,
822 &keep_stdin, &keep_stdout)))
827 if (setup_input(context, socket_fd) < 0) {
833 if (setup_output(context, socket_fd, file_name_from_path(command->path)) < 0) {
838 if (setup_error(context, socket_fd, file_name_from_path(command->path)) < 0) {
844 if ((r = cgroup_bonding_install_list(cgroup_bondings, 0)) < 0) {
849 if (context->oom_adjust_set) {
852 snprintf(t, sizeof(t), "%i", context->oom_adjust);
855 if (write_one_line_file("/proc/self/oom_adj", t) < 0) {
861 if (context->nice_set)
862 if (setpriority(PRIO_PROCESS, 0, context->nice) < 0) {
867 if (context->cpu_sched_set) {
868 struct sched_param param;
871 param.sched_priority = context->cpu_sched_priority;
873 if (sched_setscheduler(0, context->cpu_sched_policy |
874 (context->cpu_sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0), ¶m) < 0) {
875 r = EXIT_SETSCHEDULER;
880 if (context->cpu_affinity_set)
881 if (sched_setaffinity(0, sizeof(context->cpu_affinity), &context->cpu_affinity) < 0) {
882 r = EXIT_CPUAFFINITY;
886 if (context->ioprio_set)
887 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, context->ioprio) < 0) {
892 if (context->timer_slack_ns_set)
893 if (prctl(PR_SET_TIMERSLACK, context->timer_slack_ns_set) < 0) {
898 if (strv_length(context->read_write_dirs) > 0 ||
899 strv_length(context->read_only_dirs) > 0 ||
900 strv_length(context->inaccessible_dirs) > 0 ||
901 context->mount_flags != MS_SHARED ||
902 context->private_tmp)
903 if ((r = setup_namespace(
904 context->read_write_dirs,
905 context->read_only_dirs,
906 context->inaccessible_dirs,
907 context->private_tmp,
908 context->mount_flags)) < 0)
912 username = context->user;
913 if (get_user_creds(&username, &uid, &gid, &home) < 0) {
918 if (is_terminal_input(context->std_input))
919 if (chown_terminal(STDIN_FILENO, uid) < 0) {
925 if (apply_permissions)
926 if (enforce_groups(context, username, uid) < 0) {
931 umask(context->umask);
934 if (context->root_directory)
935 if (chroot(context->root_directory) < 0) {
940 if (chdir(context->working_directory ? context->working_directory : "/") < 0) {
948 if (asprintf(&d, "%s/%s",
949 context->root_directory ? context->root_directory : "",
950 context->working_directory ? context->working_directory : "") < 0) {
964 if (close_all_fds(fds, n_fds) < 0 ||
965 shift_fds(fds, n_fds) < 0 ||
966 flags_fds(fds, n_fds, context->non_blocking) < 0) {
971 if (apply_permissions) {
973 for (i = 0; i < RLIMIT_NLIMITS; i++) {
974 if (!context->rlimit[i])
977 if (setrlimit(i, context->rlimit[i]) < 0) {
984 if (enforce_user(context, uid) < 0) {
989 /* PR_GET_SECUREBITS is not priviliged, while
990 * PR_SET_SECUREBITS is. So to suppress
991 * potential EPERMs we'll try not to call
992 * PR_SET_SECUREBITS unless necessary. */
993 if (prctl(PR_GET_SECUREBITS) != context->secure_bits)
994 if (prctl(PR_SET_SECUREBITS, context->secure_bits) < 0) {
999 if (context->capabilities)
1000 if (cap_set_proc(context->capabilities) < 0) {
1001 r = EXIT_CAPABILITIES;
1006 if (!(our_env = new0(char*, 6))) {
1012 if (asprintf(our_env + n_env++, "LISTEN_PID=%llu", (unsigned long long) getpid()) < 0 ||
1013 asprintf(our_env + n_env++, "LISTEN_FDS=%u", n_fds) < 0) {
1019 if (asprintf(our_env + n_env++, "HOME=%s", home) < 0) {
1025 if (asprintf(our_env + n_env++, "LOGNAME=%s", username) < 0 ||
1026 asprintf(our_env + n_env++, "USER=%s", username) < 0) {
1033 if (!(final_env = strv_env_merge(environment, our_env, context->environment, NULL))) {
1038 execve(command->path, argv, final_env);
1043 strv_free(final_env);
1045 if (saved_stdin >= 0)
1046 close_nointr_nofail(saved_stdin);
1048 if (saved_stdout >= 0)
1049 close_nointr_nofail(saved_stdout);
1054 /* We add the new process to the cgroup both in the child (so
1055 * that we can be sure that no user code is ever executed
1056 * outside of the cgroup) and in the parent (so that we can be
1057 * sure that when we kill the cgroup the process will be
1059 if (cgroup_bondings)
1060 cgroup_bonding_install_list(cgroup_bondings, pid);
1062 log_debug("Forked %s as %llu", command->path, (unsigned long long) pid);
1064 command->exec_status.pid = pid;
1065 command->exec_status.start_timestamp = now(CLOCK_REALTIME);
1071 void exec_context_init(ExecContext *c) {
1075 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0);
1076 c->cpu_sched_policy = SCHED_OTHER;
1077 c->syslog_priority = LOG_DAEMON|LOG_INFO;
1078 c->mount_flags = MS_SHARED;
1080 c->std_input = EXEC_INPUT_NULL;
1081 c->std_output = EXEC_OUTPUT_SYSLOG;
1082 c->std_error = EXEC_OUTPUT_SYSLOG;
1085 void exec_context_done(ExecContext *c) {
1090 strv_free(c->environment);
1091 c->environment = NULL;
1093 for (l = 0; l < ELEMENTSOF(c->rlimit); l++) {
1095 c->rlimit[l] = NULL;
1098 free(c->working_directory);
1099 c->working_directory = NULL;
1100 free(c->root_directory);
1101 c->root_directory = NULL;
1106 free(c->syslog_identifier);
1107 c->syslog_identifier = NULL;
1115 strv_free(c->supplementary_groups);
1116 c->supplementary_groups = NULL;
1118 if (c->capabilities) {
1119 cap_free(c->capabilities);
1120 c->capabilities = NULL;
1123 strv_free(c->read_only_dirs);
1124 c->read_only_dirs = NULL;
1126 strv_free(c->read_write_dirs);
1127 c->read_write_dirs = NULL;
1129 strv_free(c->inaccessible_dirs);
1130 c->inaccessible_dirs = NULL;
1133 void exec_command_done(ExecCommand *c) {
1143 void exec_command_done_array(ExecCommand *c, unsigned n) {
1146 for (i = 0; i < n; i++)
1147 exec_command_done(c+i);
1150 void exec_command_free_list(ExecCommand *c) {
1154 LIST_REMOVE(ExecCommand, command, c, i);
1155 exec_command_done(i);
1160 void exec_command_free_array(ExecCommand **c, unsigned n) {
1163 for (i = 0; i < n; i++) {
1164 exec_command_free_list(c[i]);
1169 static void strv_fprintf(FILE *f, char **l) {
1175 fprintf(f, " %s", *g);
1178 void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) {
1190 "%sWorkingDirectory: %s\n"
1191 "%sRootDirectory: %s\n"
1192 "%sNonBlocking: %s\n"
1193 "%sPrivateTmp: %s\n",
1195 prefix, c->working_directory ? c->working_directory : "/",
1196 prefix, c->root_directory ? c->root_directory : "/",
1197 prefix, yes_no(c->non_blocking),
1198 prefix, yes_no(c->private_tmp));
1201 for (e = c->environment; *e; e++)
1202 fprintf(f, "%sEnvironment: %s\n", prefix, *e);
1209 if (c->oom_adjust_set)
1211 "%sOOMAdjust: %i\n",
1212 prefix, c->oom_adjust);
1214 for (i = 0; i < RLIM_NLIMITS; i++)
1216 fprintf(f, "%s%s: %llu\n", prefix, rlimit_to_string(i), (unsigned long long) c->rlimit[i]->rlim_max);
1220 "%sIOSchedulingClass: %s\n"
1221 "%sIOPriority: %i\n",
1222 prefix, ioprio_class_to_string(IOPRIO_PRIO_CLASS(c->ioprio)),
1223 prefix, (int) IOPRIO_PRIO_DATA(c->ioprio));
1225 if (c->cpu_sched_set)
1227 "%sCPUSchedulingPolicy: %s\n"
1228 "%sCPUSchedulingPriority: %i\n"
1229 "%sCPUSchedulingResetOnFork: %s\n",
1230 prefix, sched_policy_to_string(c->cpu_sched_policy),
1231 prefix, c->cpu_sched_priority,
1232 prefix, yes_no(c->cpu_sched_reset_on_fork));
1234 if (c->cpu_affinity_set) {
1235 fprintf(f, "%sCPUAffinity:", prefix);
1236 for (i = 0; i < CPU_SETSIZE; i++)
1237 if (CPU_ISSET(i, &c->cpu_affinity))
1238 fprintf(f, " %i", i);
1242 if (c->timer_slack_ns_set)
1243 fprintf(f, "%sTimerSlackNS: %lu\n", prefix, c->timer_slack_ns);
1246 "%sStandardInput: %s\n"
1247 "%sStandardOutput: %s\n"
1248 "%sStandardError: %s\n",
1249 prefix, exec_input_to_string(c->std_input),
1250 prefix, exec_output_to_string(c->std_output),
1251 prefix, exec_output_to_string(c->std_error));
1256 prefix, c->tty_path);
1258 if (c->std_output == EXEC_OUTPUT_SYSLOG || c->std_output == EXEC_OUTPUT_KMSG ||
1259 c->std_error == EXEC_OUTPUT_SYSLOG || c->std_error == EXEC_OUTPUT_KMSG)
1261 "%sSyslogFacility: %s\n"
1262 "%sSyslogLevel: %s\n",
1263 prefix, log_facility_to_string(LOG_FAC(c->syslog_priority)),
1264 prefix, log_level_to_string(LOG_PRI(c->syslog_priority)));
1266 if (c->capabilities) {
1268 if ((t = cap_to_text(c->capabilities, NULL))) {
1269 fprintf(f, "%sCapabilities: %s\n",
1276 fprintf(f, "%sSecure Bits:%s%s%s%s%s%s\n",
1278 (c->secure_bits & SECURE_KEEP_CAPS) ? " keep-caps" : "",
1279 (c->secure_bits & SECURE_KEEP_CAPS_LOCKED) ? " keep-caps-locked" : "",
1280 (c->secure_bits & SECURE_NO_SETUID_FIXUP) ? " no-setuid-fixup" : "",
1281 (c->secure_bits & SECURE_NO_SETUID_FIXUP_LOCKED) ? " no-setuid-fixup-locked" : "",
1282 (c->secure_bits & SECURE_NOROOT) ? " noroot" : "",
1283 (c->secure_bits & SECURE_NOROOT_LOCKED) ? "noroot-locked" : "");
1285 if (c->capability_bounding_set_drop) {
1286 fprintf(f, "%sCapabilityBoundingSetDrop:", prefix);
1288 for (i = 0; i <= CAP_LAST_CAP; i++)
1289 if (c->capability_bounding_set_drop & (1 << i)) {
1292 if ((t = cap_to_name(i))) {
1293 fprintf(f, " %s", t);
1302 fprintf(f, "%sUser: %s", prefix, c->user);
1304 fprintf(f, "%sGroup: %s", prefix, c->group);
1306 if (strv_length(c->supplementary_groups) > 0) {
1307 fprintf(f, "%sSupplementaryGroups:", prefix);
1308 strv_fprintf(f, c->supplementary_groups);
1312 if (strv_length(c->read_write_dirs) > 0) {
1313 fprintf(f, "%sReadWriteDirs:", prefix);
1314 strv_fprintf(f, c->read_write_dirs);
1318 if (strv_length(c->read_only_dirs) > 0) {
1319 fprintf(f, "%sReadOnlyDirs:", prefix);
1320 strv_fprintf(f, c->read_only_dirs);
1324 if (strv_length(c->inaccessible_dirs) > 0) {
1325 fprintf(f, "%sInaccessibleDirs:", prefix);
1326 strv_fprintf(f, c->inaccessible_dirs);
1331 void exec_status_fill(ExecStatus *s, pid_t pid, int code, int status) {
1335 s->exit_timestamp = now(CLOCK_REALTIME);
1341 void exec_status_dump(ExecStatus *s, FILE *f, const char *prefix) {
1342 char buf[FORMAT_TIMESTAMP_MAX];
1355 prefix, (unsigned long long) s->pid);
1357 if (s->start_timestamp > 0)
1359 "%sStart Timestamp: %s\n",
1360 prefix, format_timestamp(buf, sizeof(buf), s->start_timestamp));
1362 if (s->exit_timestamp > 0)
1364 "%sExit Timestamp: %s\n"
1366 "%sExit Status: %i\n",
1367 prefix, format_timestamp(buf, sizeof(buf), s->exit_timestamp),
1368 prefix, sigchld_code_to_string(s->code),
1372 char *exec_command_line(char **argv) {
1380 STRV_FOREACH(a, argv)
1383 if (!(n = new(char, k)))
1387 STRV_FOREACH(a, argv) {
1394 if (strpbrk(*a, WHITESPACE)) {
1405 /* FIXME: this doesn't really handle arguments that have
1406 * spaces and ticks in them */
1411 void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix) {
1413 const char *prefix2;
1422 p2 = strappend(prefix, "\t");
1423 prefix2 = p2 ? p2 : prefix;
1425 cmd = exec_command_line(c->argv);
1428 "%sCommand Line: %s\n",
1429 prefix, cmd ? cmd : strerror(ENOMEM));
1433 exec_status_dump(&c->exec_status, f, prefix2);
1438 void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix) {
1444 LIST_FOREACH(command, c, c)
1445 exec_command_dump(c, f, prefix);
1448 void exec_command_append_list(ExecCommand **l, ExecCommand *e) {
1455 /* It's kinda important that we keep the order here */
1456 LIST_FIND_TAIL(ExecCommand, command, *l, end);
1457 LIST_INSERT_AFTER(ExecCommand, command, *l, end, e);
1462 int exec_command_set(ExecCommand *c, const char *path, ...) {
1470 l = strv_new_ap(path, ap);
1476 if (!(p = strdup(path))) {
1490 const char* exit_status_to_string(ExitStatus status) {
1492 /* We cast to int here, so that -Wenum doesn't complain that
1493 * EXIT_SUCCESS/EXIT_FAILURE aren't in the enum */
1495 switch ((int) status) {
1503 case EXIT_INVALIDARGUMENT:
1504 return "INVALIDARGUMENT";
1506 case EXIT_NOTIMPLEMENTED:
1507 return "NOTIMPLEMENTED";
1509 case EXIT_NOPERMISSION:
1510 return "NOPERMISSION";
1512 case EXIT_NOTINSTALLED:
1513 return "NOTINSSTALLED";
1515 case EXIT_NOTCONFIGURED:
1516 return "NOTCONFIGURED";
1518 case EXIT_NOTRUNNING:
1519 return "NOTRUNNING";
1539 case EXIT_OOM_ADJUST:
1540 return "OOM_ADJUST";
1542 case EXIT_SIGNAL_MASK:
1543 return "SIGNAL_MASK";
1557 case EXIT_TIMERSLACK:
1558 return "TIMERSLACK";
1560 case EXIT_SECUREBITS:
1561 return "SECUREBITS";
1563 case EXIT_SETSCHEDULER:
1564 return "SETSCHEDULER";
1566 case EXIT_CPUAFFINITY:
1567 return "CPUAFFINITY";
1575 case EXIT_CAPABILITIES:
1576 return "CAPABILITIES";
1595 static const char* const exec_input_table[_EXEC_INPUT_MAX] = {
1596 [EXEC_INPUT_NULL] = "null",
1597 [EXEC_INPUT_TTY] = "tty",
1598 [EXEC_INPUT_TTY_FORCE] = "tty-force",
1599 [EXEC_INPUT_TTY_FAIL] = "tty-fail",
1600 [EXEC_INPUT_SOCKET] = "socket"
1603 static const char* const exec_output_table[_EXEC_OUTPUT_MAX] = {
1604 [EXEC_OUTPUT_INHERIT] = "inherit",
1605 [EXEC_OUTPUT_NULL] = "null",
1606 [EXEC_OUTPUT_TTY] = "tty",
1607 [EXEC_OUTPUT_SYSLOG] = "syslog",
1608 [EXEC_OUTPUT_KMSG] = "kmsg",
1609 [EXEC_OUTPUT_SOCKET] = "socket"
1612 DEFINE_STRING_TABLE_LOOKUP(exec_output, ExecOutput);
1614 DEFINE_STRING_TABLE_LOOKUP(exec_input, ExecInput);