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 (i != EXEC_INPUT_NULL)
305 return dup2(STDIN_FILENO, STDOUT_FILENO) < 0 ? -errno : STDOUT_FILENO;
307 /* For PID 1 stdout is always connected to /dev/null,
308 * hence reopen the console if out parent is PID1. */
310 return open_terminal_as(tty_path(context), O_WRONLY, STDOUT_FILENO);
312 return 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_KMSG:
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->std_input, socket_fd);
345 o = fixup_output(context->std_output, socket_fd);
346 e = fixup_output(context->std_error, 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 i != EXEC_INPUT_NULL &&
356 return STDERR_FILENO;
358 /* Duplicate form stdout if possible */
359 if (e == o || e == EXEC_OUTPUT_INHERIT)
360 return dup2(STDOUT_FILENO, STDERR_FILENO) < 0 ? -errno : STDERR_FILENO;
364 case EXEC_OUTPUT_NULL:
365 return open_null_as(O_WRONLY, STDERR_FILENO);
367 case EXEC_OUTPUT_TTY:
368 if (is_terminal_input(i))
369 return dup2(STDIN_FILENO, STDERR_FILENO) < 0 ? -errno : STDERR_FILENO;
371 /* We don't reset the terminal if this is just about output */
372 return open_terminal_as(tty_path(context), O_WRONLY, STDERR_FILENO);
374 case EXEC_OUTPUT_SYSLOG:
375 case EXEC_OUTPUT_KMSG:
376 return connect_logger_as(context, e, ident, STDERR_FILENO);
378 case EXEC_OUTPUT_SOCKET:
379 assert(socket_fd >= 0);
380 return dup2(socket_fd, STDERR_FILENO) < 0 ? -errno : STDERR_FILENO;
383 assert_not_reached("Unknown error type");
387 static int chown_terminal(int fd, uid_t uid) {
392 /* This might fail. What matters are the results. */
393 (void) fchown(fd, uid, -1);
394 (void) fchmod(fd, TTY_MODE);
396 if (fstat(fd, &st) < 0)
399 if (st.st_uid != uid || (st.st_mode & 0777) != TTY_MODE)
405 static int setup_confirm_stdio(const ExecContext *context,
407 int *_saved_stdout) {
408 int fd = -1, saved_stdin, saved_stdout = -1, r;
411 assert(_saved_stdin);
412 assert(_saved_stdout);
414 /* This returns positive EXIT_xxx return values instead of
415 * negative errno style values! */
417 if ((saved_stdin = fcntl(STDIN_FILENO, F_DUPFD, 3)) < 0)
420 if ((saved_stdout = fcntl(STDOUT_FILENO, F_DUPFD, 3)) < 0) {
425 if ((fd = acquire_terminal(
427 context->std_input == EXEC_INPUT_TTY_FAIL,
428 context->std_input == EXEC_INPUT_TTY_FORCE,
434 if (chown_terminal(fd, getuid()) < 0) {
439 if (dup2(fd, STDIN_FILENO) < 0) {
444 if (dup2(fd, STDOUT_FILENO) < 0) {
450 close_nointr_nofail(fd);
452 *_saved_stdin = saved_stdin;
453 *_saved_stdout = saved_stdout;
458 if (saved_stdout >= 0)
459 close_nointr_nofail(saved_stdout);
461 if (saved_stdin >= 0)
462 close_nointr_nofail(saved_stdin);
465 close_nointr_nofail(fd);
470 static int restore_confirm_stdio(const ExecContext *context,
478 assert(*saved_stdin >= 0);
479 assert(saved_stdout);
480 assert(*saved_stdout >= 0);
482 /* This returns positive EXIT_xxx return values instead of
483 * negative errno style values! */
485 if (is_terminal_input(context->std_input)) {
487 /* The service wants terminal input. */
491 context->std_output == EXEC_OUTPUT_INHERIT ||
492 context->std_output == EXEC_OUTPUT_TTY;
495 /* If the service doesn't want a controlling terminal,
496 * then we need to get rid entirely of what we have
499 if (release_terminal() < 0)
502 if (dup2(*saved_stdin, STDIN_FILENO) < 0)
505 if (dup2(*saved_stdout, STDOUT_FILENO) < 0)
508 *keep_stdout = *keep_stdin = false;
514 static int get_group_creds(const char *groupname, gid_t *gid) {
521 /* We enforce some special rules for gid=0: in order to avoid
522 * NSS lookups for root we hardcode its data. */
524 if (streq(groupname, "root") || streq(groupname, "0")) {
529 if (safe_atolu(groupname, &lu) >= 0) {
531 g = getgrgid((gid_t) lu);
534 g = getgrnam(groupname);
538 return errno != 0 ? -errno : -ESRCH;
544 static int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home) {
554 /* We enforce some special rules for uid=0: in order to avoid
555 * NSS lookups for root we hardcode its data. */
557 if (streq(*username, "root") || streq(*username, "0")) {
565 if (safe_atolu(*username, &lu) >= 0) {
567 p = getpwuid((uid_t) lu);
569 /* If there are multiple users with the same id, make
570 * sure to leave $USER to the configured value instead
571 * of the first occurence in the database. However if
572 * the uid was configured by a numeric uid, then let's
573 * pick the real username from /etc/passwd. */
575 *username = p->pw_name;
578 p = getpwnam(*username);
582 return errno != 0 ? -errno : -ESRCH;
590 static int enforce_groups(const ExecContext *context, const char *username, gid_t gid) {
591 bool keep_groups = false;
596 /* Lookup and ser GID and supplementary group list. Here too
597 * we avoid NSS lookups for gid=0. */
599 if (context->group || username) {
602 if ((r = get_group_creds(context->group, &gid)) < 0)
605 /* First step, initialize groups from /etc/groups */
606 if (username && gid != 0) {
607 if (initgroups(username, gid) < 0)
613 /* Second step, set our gids */
614 if (setresgid(gid, gid, gid) < 0)
618 if (context->supplementary_groups) {
623 /* Final step, initialize any manually set supplementary groups */
624 ngroups_max = (int) sysconf(_SC_NGROUPS_MAX);
626 if (!(gids = new(gid_t, ngroups_max)))
630 if ((k = getgroups(ngroups_max, gids)) < 0) {
637 STRV_FOREACH(i, context->supplementary_groups) {
639 if (k >= ngroups_max) {
644 if ((r = get_group_creds(*i, gids+k)) < 0) {
652 if (setgroups(k, gids) < 0) {
663 static int enforce_user(const ExecContext *context, uid_t uid) {
667 /* Sets (but doesn't lookup) the uid and make sure we keep the
668 * capabilities while doing so. */
670 if (context->capabilities) {
672 static const cap_value_t bits[] = {
673 CAP_SETUID, /* Necessary so that we can run setresuid() below */
674 CAP_SETPCAP /* Necessary so that we can set PR_SET_SECUREBITS later on */
677 /* First step: If we need to keep capabilities but
678 * drop privileges we need to make sure we keep our
679 * caps, whiel we drop priviliges. */
681 int sb = context->secure_bits|SECURE_KEEP_CAPS;
683 if (prctl(PR_GET_SECUREBITS) != sb)
684 if (prctl(PR_SET_SECUREBITS, sb) < 0)
688 /* Second step: set the capabilites. This will reduce
689 * the capabilities to the minimum we need. */
691 if (!(d = cap_dup(context->capabilities)))
694 if (cap_set_flag(d, CAP_EFFECTIVE, ELEMENTSOF(bits), bits, CAP_SET) < 0 ||
695 cap_set_flag(d, CAP_PERMITTED, ELEMENTSOF(bits), bits, CAP_SET) < 0) {
701 if (cap_set_proc(d) < 0) {
710 /* Third step: actually set the uids */
711 if (setresuid(uid, uid, uid) < 0)
714 /* At this point we should have all necessary capabilities but
715 are otherwise a normal user. However, the caps might got
716 corrupted due to the setresuid() so we need clean them up
717 later. This is done outside of this call. */
722 int exec_spawn(ExecCommand *command,
724 const ExecContext *context,
725 int fds[], unsigned n_fds,
727 bool apply_permissions,
730 CGroupBonding *cgroup_bondings,
741 assert(fds || n_fds <= 0);
743 if (context->std_input == EXEC_INPUT_SOCKET ||
744 context->std_output == EXEC_OUTPUT_SOCKET ||
745 context->std_error == EXEC_OUTPUT_SOCKET) {
758 argv = command->argv;
760 if (!(line = exec_command_line(argv)))
763 log_debug("About to execute: %s", line);
767 if ((r = cgroup_bonding_realize_list(cgroup_bondings)))
770 if ((pid = fork()) < 0)
776 const char *username = NULL, *home = NULL;
777 uid_t uid = (uid_t) -1;
778 gid_t gid = (gid_t) -1;
779 char **our_env = NULL, **final_env = NULL;
781 int saved_stdout = -1, saved_stdin = -1;
782 bool keep_stdout = false, keep_stdin = false;
786 /* We reset exactly these signals, since they are the
787 * only ones we set to SIG_IGN in the main daemon. All
788 * others we leave untouched because we set them to
789 * SIG_DFL or a valid handler initially, both of which
790 * will be demoted to SIG_DFL. */
791 default_signals(SIGNALS_CRASH_HANDLER,
794 if (sigemptyset(&ss) < 0 ||
795 sigprocmask(SIG_SETMASK, &ss, NULL) < 0) {
796 r = EXIT_SIGNAL_MASK;
800 if (!context->no_setsid)
809 /* Set up terminal for the question */
810 if ((r = setup_confirm_stdio(context,
811 &saved_stdin, &saved_stdout)))
814 /* Now ask the question. */
815 if (!(line = exec_command_line(argv))) {
820 r = ask(&response, "yns", "Execute %s? [Yes, No, Skip] ", line);
823 if (r < 0 || response == 'n') {
826 } else if (response == 's') {
831 /* Release terminal for the question */
832 if ((r = restore_confirm_stdio(context,
833 &saved_stdin, &saved_stdout,
834 &keep_stdin, &keep_stdout)))
839 if (setup_input(context, socket_fd) < 0) {
845 if (setup_output(context, socket_fd, file_name_from_path(command->path)) < 0) {
850 if (setup_error(context, socket_fd, file_name_from_path(command->path)) < 0) {
856 if ((r = cgroup_bonding_install_list(cgroup_bondings, 0)) < 0) {
861 if (context->oom_adjust_set) {
864 snprintf(t, sizeof(t), "%i", context->oom_adjust);
867 if (write_one_line_file("/proc/self/oom_adj", t) < 0) {
873 if (context->nice_set)
874 if (setpriority(PRIO_PROCESS, 0, context->nice) < 0) {
879 if (context->cpu_sched_set) {
880 struct sched_param param;
883 param.sched_priority = context->cpu_sched_priority;
885 if (sched_setscheduler(0, context->cpu_sched_policy |
886 (context->cpu_sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0), ¶m) < 0) {
887 r = EXIT_SETSCHEDULER;
892 if (context->cpu_affinity_set)
893 if (sched_setaffinity(0, sizeof(context->cpu_affinity), &context->cpu_affinity) < 0) {
894 r = EXIT_CPUAFFINITY;
898 if (context->ioprio_set)
899 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, context->ioprio) < 0) {
904 if (context->timer_slack_ns_set)
905 if (prctl(PR_SET_TIMERSLACK, context->timer_slack_ns_set) < 0) {
910 if (strv_length(context->read_write_dirs) > 0 ||
911 strv_length(context->read_only_dirs) > 0 ||
912 strv_length(context->inaccessible_dirs) > 0 ||
913 context->mount_flags != MS_SHARED ||
914 context->private_tmp)
915 if ((r = setup_namespace(
916 context->read_write_dirs,
917 context->read_only_dirs,
918 context->inaccessible_dirs,
919 context->private_tmp,
920 context->mount_flags)) < 0)
924 username = context->user;
925 if (get_user_creds(&username, &uid, &gid, &home) < 0) {
930 if (is_terminal_input(context->std_input))
931 if (chown_terminal(STDIN_FILENO, uid) < 0) {
937 if (apply_permissions)
938 if (enforce_groups(context, username, uid) < 0) {
943 umask(context->umask);
946 if (context->root_directory)
947 if (chroot(context->root_directory) < 0) {
952 if (chdir(context->working_directory ? context->working_directory : "/") < 0) {
960 if (asprintf(&d, "%s/%s",
961 context->root_directory ? context->root_directory : "",
962 context->working_directory ? context->working_directory : "") < 0) {
976 if (close_all_fds(fds, n_fds) < 0 ||
977 shift_fds(fds, n_fds) < 0 ||
978 flags_fds(fds, n_fds, context->non_blocking) < 0) {
983 if (apply_permissions) {
985 for (i = 0; i < RLIMIT_NLIMITS; i++) {
986 if (!context->rlimit[i])
989 if (setrlimit(i, context->rlimit[i]) < 0) {
996 if (enforce_user(context, uid) < 0) {
1001 /* PR_GET_SECUREBITS is not priviliged, while
1002 * PR_SET_SECUREBITS is. So to suppress
1003 * potential EPERMs we'll try not to call
1004 * PR_SET_SECUREBITS unless necessary. */
1005 if (prctl(PR_GET_SECUREBITS) != context->secure_bits)
1006 if (prctl(PR_SET_SECUREBITS, context->secure_bits) < 0) {
1007 r = EXIT_SECUREBITS;
1011 if (context->capabilities)
1012 if (cap_set_proc(context->capabilities) < 0) {
1013 r = EXIT_CAPABILITIES;
1018 if (!(our_env = new0(char*, 6))) {
1024 if (asprintf(our_env + n_env++, "LISTEN_PID=%llu", (unsigned long long) getpid()) < 0 ||
1025 asprintf(our_env + n_env++, "LISTEN_FDS=%u", n_fds) < 0) {
1031 if (asprintf(our_env + n_env++, "HOME=%s", home) < 0) {
1037 if (asprintf(our_env + n_env++, "LOGNAME=%s", username) < 0 ||
1038 asprintf(our_env + n_env++, "USER=%s", username) < 0) {
1045 if (!(final_env = strv_env_merge(environment, our_env, context->environment, NULL))) {
1050 execve(command->path, argv, final_env);
1055 strv_free(final_env);
1057 if (saved_stdin >= 0)
1058 close_nointr_nofail(saved_stdin);
1060 if (saved_stdout >= 0)
1061 close_nointr_nofail(saved_stdout);
1066 /* We add the new process to the cgroup both in the child (so
1067 * that we can be sure that no user code is ever executed
1068 * outside of the cgroup) and in the parent (so that we can be
1069 * sure that when we kill the cgroup the process will be
1071 if (cgroup_bondings)
1072 cgroup_bonding_install_list(cgroup_bondings, pid);
1074 log_debug("Forked %s as %llu", command->path, (unsigned long long) pid);
1076 command->exec_status.pid = pid;
1077 command->exec_status.start_timestamp = now(CLOCK_REALTIME);
1083 void exec_context_init(ExecContext *c) {
1087 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0);
1088 c->cpu_sched_policy = SCHED_OTHER;
1089 c->syslog_priority = LOG_DAEMON|LOG_INFO;
1090 c->mount_flags = MS_SHARED;
1093 void exec_context_done(ExecContext *c) {
1098 strv_free(c->environment);
1099 c->environment = NULL;
1101 for (l = 0; l < ELEMENTSOF(c->rlimit); l++) {
1103 c->rlimit[l] = NULL;
1106 free(c->working_directory);
1107 c->working_directory = NULL;
1108 free(c->root_directory);
1109 c->root_directory = NULL;
1114 free(c->syslog_identifier);
1115 c->syslog_identifier = NULL;
1123 strv_free(c->supplementary_groups);
1124 c->supplementary_groups = NULL;
1126 if (c->capabilities) {
1127 cap_free(c->capabilities);
1128 c->capabilities = NULL;
1131 strv_free(c->read_only_dirs);
1132 c->read_only_dirs = NULL;
1134 strv_free(c->read_write_dirs);
1135 c->read_write_dirs = NULL;
1137 strv_free(c->inaccessible_dirs);
1138 c->inaccessible_dirs = NULL;
1141 void exec_command_done(ExecCommand *c) {
1151 void exec_command_done_array(ExecCommand *c, unsigned n) {
1154 for (i = 0; i < n; i++)
1155 exec_command_done(c+i);
1158 void exec_command_free_list(ExecCommand *c) {
1162 LIST_REMOVE(ExecCommand, command, c, i);
1163 exec_command_done(i);
1168 void exec_command_free_array(ExecCommand **c, unsigned n) {
1171 for (i = 0; i < n; i++) {
1172 exec_command_free_list(c[i]);
1177 static void strv_fprintf(FILE *f, char **l) {
1183 fprintf(f, " %s", *g);
1186 void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) {
1198 "%sWorkingDirectory: %s\n"
1199 "%sRootDirectory: %s\n"
1200 "%sNonBlocking: %s\n"
1201 "%sPrivateTmp: %s\n",
1203 prefix, c->working_directory ? c->working_directory : "/",
1204 prefix, c->root_directory ? c->root_directory : "/",
1205 prefix, yes_no(c->non_blocking),
1206 prefix, yes_no(c->private_tmp));
1209 for (e = c->environment; *e; e++)
1210 fprintf(f, "%sEnvironment: %s\n", prefix, *e);
1217 if (c->oom_adjust_set)
1219 "%sOOMAdjust: %i\n",
1220 prefix, c->oom_adjust);
1222 for (i = 0; i < RLIM_NLIMITS; i++)
1224 fprintf(f, "%s%s: %llu\n", prefix, rlimit_to_string(i), (unsigned long long) c->rlimit[i]->rlim_max);
1228 "%sIOSchedulingClass: %s\n"
1229 "%sIOPriority: %i\n",
1230 prefix, ioprio_class_to_string(IOPRIO_PRIO_CLASS(c->ioprio)),
1231 prefix, (int) IOPRIO_PRIO_DATA(c->ioprio));
1233 if (c->cpu_sched_set)
1235 "%sCPUSchedulingPolicy: %s\n"
1236 "%sCPUSchedulingPriority: %i\n"
1237 "%sCPUSchedulingResetOnFork: %s\n",
1238 prefix, sched_policy_to_string(c->cpu_sched_policy),
1239 prefix, c->cpu_sched_priority,
1240 prefix, yes_no(c->cpu_sched_reset_on_fork));
1242 if (c->cpu_affinity_set) {
1243 fprintf(f, "%sCPUAffinity:", prefix);
1244 for (i = 0; i < CPU_SETSIZE; i++)
1245 if (CPU_ISSET(i, &c->cpu_affinity))
1246 fprintf(f, " %i", i);
1250 if (c->timer_slack_ns_set)
1251 fprintf(f, "%sTimerSlackNS: %lu\n", prefix, c->timer_slack_ns);
1254 "%sStandardInput: %s\n"
1255 "%sStandardOutput: %s\n"
1256 "%sStandardError: %s\n",
1257 prefix, exec_input_to_string(c->std_input),
1258 prefix, exec_output_to_string(c->std_output),
1259 prefix, exec_output_to_string(c->std_error));
1264 prefix, c->tty_path);
1266 if (c->std_output == EXEC_OUTPUT_SYSLOG || c->std_output == EXEC_OUTPUT_KMSG ||
1267 c->std_error == EXEC_OUTPUT_SYSLOG || c->std_error == EXEC_OUTPUT_KMSG)
1269 "%sSyslogFacility: %s\n"
1270 "%sSyslogLevel: %s\n",
1271 prefix, log_facility_to_string(LOG_FAC(c->syslog_priority)),
1272 prefix, log_level_to_string(LOG_PRI(c->syslog_priority)));
1274 if (c->capabilities) {
1276 if ((t = cap_to_text(c->capabilities, NULL))) {
1277 fprintf(f, "%sCapabilities: %s\n",
1284 fprintf(f, "%sSecure Bits:%s%s%s%s%s%s\n",
1286 (c->secure_bits & SECURE_KEEP_CAPS) ? " keep-caps" : "",
1287 (c->secure_bits & SECURE_KEEP_CAPS_LOCKED) ? " keep-caps-locked" : "",
1288 (c->secure_bits & SECURE_NO_SETUID_FIXUP) ? " no-setuid-fixup" : "",
1289 (c->secure_bits & SECURE_NO_SETUID_FIXUP_LOCKED) ? " no-setuid-fixup-locked" : "",
1290 (c->secure_bits & SECURE_NOROOT) ? " noroot" : "",
1291 (c->secure_bits & SECURE_NOROOT_LOCKED) ? "noroot-locked" : "");
1293 if (c->capability_bounding_set_drop) {
1294 fprintf(f, "%sCapabilityBoundingSetDrop:", prefix);
1296 for (i = 0; i <= CAP_LAST_CAP; i++)
1297 if (c->capability_bounding_set_drop & (1 << i)) {
1300 if ((t = cap_to_name(i))) {
1301 fprintf(f, " %s", t);
1310 fprintf(f, "%sUser: %s", prefix, c->user);
1312 fprintf(f, "%sGroup: %s", prefix, c->group);
1314 if (strv_length(c->supplementary_groups) > 0) {
1315 fprintf(f, "%sSupplementaryGroups:", prefix);
1316 strv_fprintf(f, c->supplementary_groups);
1320 if (strv_length(c->read_write_dirs) > 0) {
1321 fprintf(f, "%sReadWriteDirs:", prefix);
1322 strv_fprintf(f, c->read_write_dirs);
1326 if (strv_length(c->read_only_dirs) > 0) {
1327 fprintf(f, "%sReadOnlyDirs:", prefix);
1328 strv_fprintf(f, c->read_only_dirs);
1332 if (strv_length(c->inaccessible_dirs) > 0) {
1333 fprintf(f, "%sInaccessibleDirs:", prefix);
1334 strv_fprintf(f, c->inaccessible_dirs);
1339 void exec_status_fill(ExecStatus *s, pid_t pid, int code, int status) {
1343 s->exit_timestamp = now(CLOCK_REALTIME);
1349 void exec_status_dump(ExecStatus *s, FILE *f, const char *prefix) {
1350 char buf[FORMAT_TIMESTAMP_MAX];
1363 prefix, (unsigned long long) s->pid);
1365 if (s->start_timestamp > 0)
1367 "%sStart Timestamp: %s\n",
1368 prefix, format_timestamp(buf, sizeof(buf), s->start_timestamp));
1370 if (s->exit_timestamp > 0)
1372 "%sExit Timestamp: %s\n"
1374 "%sExit Status: %i\n",
1375 prefix, format_timestamp(buf, sizeof(buf), s->exit_timestamp),
1376 prefix, sigchld_code_to_string(s->code),
1380 char *exec_command_line(char **argv) {
1388 STRV_FOREACH(a, argv)
1391 if (!(n = new(char, k)))
1395 STRV_FOREACH(a, argv) {
1402 if (strpbrk(*a, WHITESPACE)) {
1413 /* FIXME: this doesn't really handle arguments that have
1414 * spaces and ticks in them */
1419 void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix) {
1421 const char *prefix2;
1430 p2 = strappend(prefix, "\t");
1431 prefix2 = p2 ? p2 : prefix;
1433 cmd = exec_command_line(c->argv);
1436 "%sCommand Line: %s\n",
1437 prefix, cmd ? cmd : strerror(ENOMEM));
1441 exec_status_dump(&c->exec_status, f, prefix2);
1446 void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix) {
1452 LIST_FOREACH(command, c, c)
1453 exec_command_dump(c, f, prefix);
1456 void exec_command_append_list(ExecCommand **l, ExecCommand *e) {
1463 /* It's kinda important that we keep the order here */
1464 LIST_FIND_TAIL(ExecCommand, command, *l, end);
1465 LIST_INSERT_AFTER(ExecCommand, command, *l, end, e);
1470 int exec_command_set(ExecCommand *c, const char *path, ...) {
1478 l = strv_new_ap(path, ap);
1484 if (!(p = strdup(path))) {
1498 const char* exit_status_to_string(ExitStatus status) {
1500 /* We cast to int here, so that -Wenum doesn't complain that
1501 * EXIT_SUCCESS/EXIT_FAILURE aren't in the enum */
1503 switch ((int) status) {
1511 case EXIT_INVALIDARGUMENT:
1512 return "INVALIDARGUMENT";
1514 case EXIT_NOTIMPLEMENTED:
1515 return "NOTIMPLEMENTED";
1517 case EXIT_NOPERMISSION:
1518 return "NOPERMISSION";
1520 case EXIT_NOTINSTALLED:
1521 return "NOTINSSTALLED";
1523 case EXIT_NOTCONFIGURED:
1524 return "NOTCONFIGURED";
1526 case EXIT_NOTRUNNING:
1527 return "NOTRUNNING";
1547 case EXIT_OOM_ADJUST:
1548 return "OOM_ADJUST";
1550 case EXIT_SIGNAL_MASK:
1551 return "SIGNAL_MASK";
1565 case EXIT_TIMERSLACK:
1566 return "TIMERSLACK";
1568 case EXIT_SECUREBITS:
1569 return "SECUREBITS";
1571 case EXIT_SETSCHEDULER:
1572 return "SETSCHEDULER";
1574 case EXIT_CPUAFFINITY:
1575 return "CPUAFFINITY";
1583 case EXIT_CAPABILITIES:
1584 return "CAPABILITIES";
1603 static const char* const exec_input_table[_EXEC_INPUT_MAX] = {
1604 [EXEC_INPUT_NULL] = "null",
1605 [EXEC_INPUT_TTY] = "tty",
1606 [EXEC_INPUT_TTY_FORCE] = "tty-force",
1607 [EXEC_INPUT_TTY_FAIL] = "tty-fail",
1608 [EXEC_INPUT_SOCKET] = "socket"
1611 static const char* const exec_output_table[_EXEC_OUTPUT_MAX] = {
1612 [EXEC_OUTPUT_INHERIT] = "inherit",
1613 [EXEC_OUTPUT_NULL] = "null",
1614 [EXEC_OUTPUT_TTY] = "tty",
1615 [EXEC_OUTPUT_SYSLOG] = "syslog",
1616 [EXEC_OUTPUT_KMSG] = "kmsg",
1617 [EXEC_OUTPUT_SOCKET] = "socket"
1620 DEFINE_STRING_TABLE_LOOKUP(exec_output, ExecOutput);
1622 DEFINE_STRING_TABLE_LOOKUP(exec_input, ExecInput);