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 static int shift_fds(int fds
[], unsigned n_fds
) {
48 int start
, restart_from
;
53 /* Modifies the fds array! (sorts it) */
63 for (i
= start
; i
< (int) n_fds
; i
++) {
66 /* Already at right index? */
70 if ((nfd
= fcntl(fds
[i
], F_DUPFD
, i
+3)) < 0)
73 assert_se(close_nointr(fds
[i
]) == 0);
76 /* Hmm, the fd we wanted isn't free? Then
77 * let's remember that and try again from here*/
78 if (nfd
!= i
+3 && restart_from
< 0)
91 static int flags_fds(int fds
[], unsigned n_fds
, bool nonblock
) {
100 /* Drops/Sets O_NONBLOCK and FD_CLOEXEC from the file flags */
102 for (i
= 0; i
< n_fds
; i
++) {
104 if ((r
= fd_nonblock(fds
[i
], nonblock
)) < 0)
107 /* We unconditionally drop FD_CLOEXEC from the fds,
108 * since after all we want to pass these fds to our
111 if ((r
= fd_cloexec(fds
[i
], false)) < 0)
118 static const char *tty_path(const ExecContext
*context
) {
121 if (context
->tty_path
)
122 return context
->tty_path
;
124 return "/dev/console";
127 static int open_null_as(int flags
, int nfd
) {
132 if ((fd
= open("/dev/null", flags
|O_NOCTTY
)) < 0)
136 r
= dup2(fd
, nfd
) < 0 ? -errno
: nfd
;
144 static int connect_logger_as(const ExecContext
*context
, ExecOutput output
, const char *ident
, int nfd
) {
148 struct sockaddr_un un
;
152 assert(output
< _EXEC_OUTPUT_MAX
);
156 if ((fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
160 sa
.sa
.sa_family
= AF_UNIX
;
161 strncpy(sa
.un
.sun_path
+1, LOGGER_SOCKET
, sizeof(sa
.un
.sun_path
)-1);
163 if (connect(fd
, &sa
.sa
, sizeof(sa
)) < 0) {
164 close_nointr_nofail(fd
);
168 if (shutdown(fd
, SHUT_RD
) < 0) {
169 close_nointr_nofail(fd
);
173 /* We speak a very simple protocol between log server
174 * and client: one line for the log destination (kmsg
175 * or syslog), followed by the priority field,
176 * followed by the process name. Since we replaced
177 * stdin/stderr we simple use stdio to write to
178 * it. Note that we use stderr, to minimize buffer
179 * flushing issues. */
185 output
== EXEC_OUTPUT_KERNEL
? "kmsg" : "syslog",
186 context
->syslog_priority
,
187 context
->syslog_identifier
? context
->syslog_identifier
: ident
);
190 r
= dup2(fd
, nfd
) < 0 ? -errno
: nfd
;
197 static int open_terminal_as(const char *path
, mode_t mode
, int nfd
) {
203 if ((fd
= open_terminal(path
, mode
| O_NOCTTY
)) < 0)
207 r
= dup2(fd
, nfd
) < 0 ? -errno
: nfd
;
208 close_nointr_nofail(fd
);
215 static bool is_terminal_input(ExecInput i
) {
217 i
== EXEC_INPUT_TTY
||
218 i
== EXEC_INPUT_TTY_FORCE
||
219 i
== EXEC_INPUT_TTY_FAIL
;
222 static int setup_input(const ExecContext
*context
) {
225 switch (context
->std_input
) {
227 case EXEC_INPUT_NULL
:
228 return open_null_as(O_RDONLY
, STDIN_FILENO
);
231 case EXEC_INPUT_TTY_FORCE
:
232 case EXEC_INPUT_TTY_FAIL
: {
235 if ((fd
= acquire_terminal(
237 context
->std_input
== EXEC_INPUT_TTY_FAIL
,
238 context
->std_input
== EXEC_INPUT_TTY_FORCE
)) < 0)
241 if (fd
!= STDIN_FILENO
) {
242 r
= dup2(fd
, STDIN_FILENO
) < 0 ? -errno
: STDIN_FILENO
;
243 close_nointr_nofail(fd
);
251 assert_not_reached("Unknown input type");
255 static int setup_output(const ExecContext
*context
, const char *ident
) {
259 /* This expects the input is already set up */
261 switch (context
->std_output
) {
263 case EXEC_OUTPUT_INHERIT
:
265 /* If the input is connected to a terminal, inherit that... */
266 if (is_terminal_input(context
->std_input
))
267 return dup2(STDIN_FILENO
, STDOUT_FILENO
) < 0 ? -errno
: STDOUT_FILENO
;
271 case EXEC_OUTPUT_NULL
:
272 return open_null_as(O_WRONLY
, STDOUT_FILENO
);
274 case EXEC_OUTPUT_TTY
: {
275 if (is_terminal_input(context
->std_input
))
276 return dup2(STDIN_FILENO
, STDOUT_FILENO
) < 0 ? -errno
: STDOUT_FILENO
;
278 /* We don't reset the terminal if this is just about output */
279 return open_terminal_as(tty_path(context
), O_WRONLY
, STDOUT_FILENO
);
282 case EXEC_OUTPUT_SYSLOG
:
283 case EXEC_OUTPUT_KERNEL
:
284 return connect_logger_as(context
, context
->std_output
, ident
, STDOUT_FILENO
);
287 assert_not_reached("Unknown output type");
291 static int setup_error(const ExecContext
*context
, const char *ident
) {
294 /* This expects the input and output are already set up */
296 /* Don't change the stderr file descriptor if we inherit all
297 * the way and are not on a tty */
298 if (context
->std_error
== EXEC_OUTPUT_INHERIT
&&
299 context
->std_output
== EXEC_OUTPUT_INHERIT
&&
300 !is_terminal_input(context
->std_input
))
301 return STDERR_FILENO
;
303 /* Duplicate form stdout if possible */
304 if (context
->std_error
== context
->std_output
||
305 context
->std_error
== EXEC_OUTPUT_INHERIT
)
306 return dup2(STDOUT_FILENO
, STDERR_FILENO
) < 0 ? -errno
: STDERR_FILENO
;
308 switch (context
->std_error
) {
310 case EXEC_OUTPUT_NULL
:
311 return open_null_as(O_WRONLY
, STDERR_FILENO
);
313 case EXEC_OUTPUT_TTY
:
314 if (is_terminal_input(context
->std_input
))
315 return dup2(STDIN_FILENO
, STDERR_FILENO
) < 0 ? -errno
: STDERR_FILENO
;
317 /* We don't reset the terminal if this is just about output */
318 return open_terminal_as(tty_path(context
), O_WRONLY
, STDERR_FILENO
);
320 case EXEC_OUTPUT_SYSLOG
:
321 case EXEC_OUTPUT_KERNEL
:
322 return connect_logger_as(context
, context
->std_error
, ident
, STDERR_FILENO
);
325 assert_not_reached("Unknown error type");
329 static int setup_confirm_stdio(const ExecContext
*context
,
331 int *_saved_stdout
) {
332 int fd
= -1, saved_stdin
, saved_stdout
= -1, r
;
335 assert(_saved_stdin
);
336 assert(_saved_stdout
);
338 /* This returns positive EXIT_xxx return values instead of
339 * negative errno style values! */
341 if ((saved_stdin
= fcntl(STDIN_FILENO
, F_DUPFD
, 3)) < 0)
344 if ((saved_stdout
= fcntl(STDOUT_FILENO
, F_DUPFD
, 3)) < 0) {
349 if ((fd
= acquire_terminal(
351 context
->std_input
== EXEC_INPUT_TTY_FAIL
,
352 context
->std_input
== EXEC_INPUT_TTY_FORCE
)) < 0) {
357 if (dup2(fd
, STDIN_FILENO
) < 0) {
362 if (dup2(fd
, STDOUT_FILENO
) < 0) {
368 close_nointr_nofail(fd
);
370 *_saved_stdin
= saved_stdin
;
371 *_saved_stdout
= saved_stdout
;
376 if (saved_stdout
>= 0)
377 close_nointr_nofail(saved_stdout
);
379 if (saved_stdin
>= 0)
380 close_nointr_nofail(saved_stdin
);
383 close_nointr_nofail(fd
);
388 static int restore_conform_stdio(const ExecContext
*context
,
396 assert(*saved_stdin
>= 0);
397 assert(saved_stdout
);
398 assert(*saved_stdout
>= 0);
400 /* This returns positive EXIT_xxx return values instead of
401 * negative errno style values! */
403 if (is_terminal_input(context
->std_input
)) {
405 /* The service wants terminal input. */
409 context
->std_output
== EXEC_OUTPUT_INHERIT
||
410 context
->std_output
== EXEC_OUTPUT_TTY
;
413 /* If the service doesn't want a controlling terminal,
414 * then we need to get rid entirely of what we have
417 if (release_terminal() < 0)
420 if (dup2(*saved_stdin
, STDIN_FILENO
) < 0)
423 if (dup2(*saved_stdout
, STDOUT_FILENO
) < 0)
426 *keep_stdout
= *keep_stdin
= false;
432 static int get_group_creds(const char *groupname
, gid_t
*gid
) {
439 /* We enforce some special rules for gid=0: in order to avoid
440 * NSS lookups for root we hardcode its data. */
442 if (streq(groupname
, "root") || streq(groupname
, "0")) {
447 if (safe_atolu(groupname
, &lu
) >= 0) {
449 g
= getgrgid((gid_t
) lu
);
452 g
= getgrnam(groupname
);
456 return errno
!= 0 ? -errno
: -ESRCH
;
462 static int get_user_creds(const char **username
, uid_t
*uid
, gid_t
*gid
, const char **home
) {
472 /* We enforce some special rules for uid=0: in order to avoid
473 * NSS lookups for root we hardcode its data. */
475 if (streq(*username
, "root") || streq(*username
, "0")) {
483 if (safe_atolu(*username
, &lu
) >= 0) {
485 p
= getpwuid((uid_t
) lu
);
487 /* If there are multiple users with the same id, make
488 * sure to leave $USER to the configured value instead
489 * of the first occurence in the database. However if
490 * the uid was configured by a numeric uid, then let's
491 * pick the real username from /etc/passwd. */
493 *username
= p
->pw_name
;
496 p
= getpwnam(*username
);
500 return errno
!= 0 ? -errno
: -ESRCH
;
508 static int enforce_groups(const ExecContext
*context
, const char *username
, gid_t gid
) {
509 bool keep_groups
= false;
514 /* Lookup and ser GID and supplementary group list. Here too
515 * we avoid NSS lookups for gid=0. */
517 if (context
->group
|| username
) {
520 if ((r
= get_group_creds(context
->group
, &gid
)) < 0)
523 /* First step, initialize groups from /etc/groups */
524 if (username
&& gid
!= 0) {
525 if (initgroups(username
, gid
) < 0)
531 /* Second step, set our gids */
532 if (setresgid(gid
, gid
, gid
) < 0)
536 if (context
->supplementary_groups
) {
541 /* Final step, initialize any manually set supplementary groups */
542 ngroups_max
= (int) sysconf(_SC_NGROUPS_MAX
);
544 if (!(gids
= new(gid_t
, ngroups_max
)))
548 if ((k
= getgroups(ngroups_max
, gids
)) < 0) {
555 STRV_FOREACH(i
, context
->supplementary_groups
) {
557 if (k
>= ngroups_max
) {
562 if ((r
= get_group_creds(*i
, gids
+k
)) < 0) {
570 if (setgroups(k
, gids
) < 0) {
581 static int enforce_user(const ExecContext
*context
, uid_t uid
) {
585 /* Sets (but doesn't lookup) the uid and make sure we keep the
586 * capabilities while doing so. */
588 if (context
->capabilities
) {
590 static const cap_value_t bits
[] = {
591 CAP_SETUID
, /* Necessary so that we can run setresuid() below */
592 CAP_SETPCAP
/* Necessary so that we can set PR_SET_SECUREBITS later on */
595 /* First step: If we need to keep capabilities but
596 * drop privileges we need to make sure we keep our
597 * caps, whiel we drop priviliges. */
599 int sb
= context
->secure_bits
|SECURE_KEEP_CAPS
;
601 if (prctl(PR_GET_SECUREBITS
) != sb
)
602 if (prctl(PR_SET_SECUREBITS
, sb
) < 0)
606 /* Second step: set the capabilites. This will reduce
607 * the capabilities to the minimum we need. */
609 if (!(d
= cap_dup(context
->capabilities
)))
612 if (cap_set_flag(d
, CAP_EFFECTIVE
, ELEMENTSOF(bits
), bits
, CAP_SET
) < 0 ||
613 cap_set_flag(d
, CAP_PERMITTED
, ELEMENTSOF(bits
), bits
, CAP_SET
) < 0) {
619 if (cap_set_proc(d
) < 0) {
628 /* Third step: actually set the uids */
629 if (setresuid(uid
, uid
, uid
) < 0)
632 /* At this point we should have all necessary capabilities but
633 are otherwise a normal user. However, the caps might got
634 corrupted due to the setresuid() so we need clean them up
635 later. This is done outside of this call. */
640 int exec_spawn(ExecCommand
*command
,
641 const ExecContext
*context
,
642 int *fds
, unsigned n_fds
,
643 bool apply_permissions
,
646 CGroupBonding
*cgroup_bondings
,
656 assert(fds
|| n_fds
<= 0);
658 if (!(line
= exec_command_line(command
)))
661 log_debug("About to execute: %s", line
);
665 if ((r
= cgroup_bonding_realize_list(cgroup_bondings
)))
668 if ((pid
= fork()) < 0)
674 const char *username
= NULL
, *home
= NULL
;
675 uid_t uid
= (uid_t
) -1;
676 gid_t gid
= (gid_t
) -1;
677 char **our_env
= NULL
, **final_env
= NULL
;
679 int saved_stdout
= -1, saved_stdin
= -1;
680 bool keep_stdout
= false, keep_stdin
= false;
684 if (sigemptyset(&ss
) < 0 ||
685 sigprocmask(SIG_SETMASK
, &ss
, NULL
) < 0) {
686 r
= EXIT_SIGNAL_MASK
;
695 umask(context
->umask
);
700 /* Set up terminal for the question */
701 if ((r
= setup_confirm_stdio(context
,
702 &saved_stdin
, &saved_stdout
)))
705 /* Now ask the question. */
706 if (!(line
= exec_command_line(command
))) {
711 r
= ask(&response
, "yns", "Execute %s? [Yes, No, Skip] ", line
);
714 if (r
< 0 || response
== 'n') {
717 } else if (response
== 's') {
722 /* Release terminal for the question */
723 if ((r
= restore_conform_stdio(context
,
724 &saved_stdin
, &saved_stdout
,
725 &keep_stdin
, &keep_stdout
)))
730 if (setup_input(context
) < 0) {
736 if (setup_output(context
, file_name_from_path(command
->path
)) < 0) {
741 if (setup_error(context
, file_name_from_path(command
->path
)) < 0) {
747 if ((r
= cgroup_bonding_install_list(cgroup_bondings
, 0)) < 0) {
752 if (context
->oom_adjust_set
) {
755 snprintf(t
, sizeof(t
), "%i", context
->oom_adjust
);
758 if (write_one_line_file("/proc/self/oom_adj", t
) < 0) {
764 if (context
->nice_set
)
765 if (setpriority(PRIO_PROCESS
, 0, context
->nice
) < 0) {
770 if (context
->cpu_sched_set
) {
771 struct sched_param param
;
774 param
.sched_priority
= context
->cpu_sched_priority
;
776 if (sched_setscheduler(0, context
->cpu_sched_policy
|
777 (context
->cpu_sched_reset_on_fork
? SCHED_RESET_ON_FORK
: 0), ¶m
) < 0) {
778 r
= EXIT_SETSCHEDULER
;
783 if (context
->cpu_affinity_set
)
784 if (sched_setaffinity(0, sizeof(context
->cpu_affinity
), &context
->cpu_affinity
) < 0) {
785 r
= EXIT_CPUAFFINITY
;
789 if (context
->ioprio_set
)
790 if (ioprio_set(IOPRIO_WHO_PROCESS
, 0, context
->ioprio
) < 0) {
795 if (context
->timer_slack_ns_set
)
796 if (prctl(PR_SET_TIMERSLACK
, context
->timer_slack_ns_set
) < 0) {
802 username
= context
->user
;
803 if (get_user_creds(&username
, &uid
, &gid
, &home
) < 0) {
809 if (apply_permissions
)
810 if (enforce_groups(context
, username
, uid
) < 0) {
816 if (context
->root_directory
)
817 if (chroot(context
->root_directory
) < 0) {
822 if (chdir(context
->working_directory
? context
->working_directory
: "/") < 0) {
830 if (asprintf(&d
, "%s/%s",
831 context
->root_directory
? context
->root_directory
: "",
832 context
->working_directory
? context
->working_directory
: "") < 0) {
846 if (close_all_fds(fds
, n_fds
) < 0 ||
847 shift_fds(fds
, n_fds
) < 0 ||
848 flags_fds(fds
, n_fds
, context
->non_blocking
) < 0) {
853 if (apply_permissions
) {
855 for (i
= 0; i
< RLIMIT_NLIMITS
; i
++) {
856 if (!context
->rlimit
[i
])
859 if (setrlimit(i
, context
->rlimit
[i
]) < 0) {
866 if (enforce_user(context
, uid
) < 0) {
871 /* PR_GET_SECUREBITS is not priviliged, while
872 * PR_SET_SECUREBITS is. So to suppress
873 * potential EPERMs we'll try not to call
874 * PR_SET_SECUREBITS unless necessary. */
875 if (prctl(PR_GET_SECUREBITS
) != context
->secure_bits
)
876 if (prctl(PR_SET_SECUREBITS
, context
->secure_bits
) < 0) {
881 if (context
->capabilities
)
882 if (cap_set_proc(context
->capabilities
) < 0) {
883 r
= EXIT_CAPABILITIES
;
888 if (!(our_env
= new0(char*, 6))) {
894 if (asprintf(our_env
+ n_env
++, "LISTEN_PID=%llu", (unsigned long long) getpid()) < 0 ||
895 asprintf(our_env
+ n_env
++, "LISTEN_FDS=%u", n_fds
) < 0) {
901 if (asprintf(our_env
+ n_env
++, "HOME=%s", home
) < 0) {
907 if (asprintf(our_env
+ n_env
++, "LOGNAME=%s", username
) < 0 ||
908 asprintf(our_env
+ n_env
++, "USER=%s", username
) < 0) {
913 if (!(final_env
= strv_env_merge(environ
, our_env
, context
->environment
, NULL
))) {
918 execve(command
->path
, command
->argv
, final_env
);
923 strv_free(final_env
);
925 if (saved_stdin
>= 0)
926 close_nointr_nofail(saved_stdin
);
928 if (saved_stdout
>= 0)
929 close_nointr_nofail(saved_stdout
);
934 /* We add the new process to the cgroup both in the child (so
935 * that we can be sure that no user code is ever executed
936 * outside of the cgroup) and in the parent (so that we can be
937 * sure that when we kill the cgroup the process will be
940 if ((r
= cgroup_bonding_install_list(cgroup_bondings
, pid
)) < 0) {
945 log_debug("Forked %s as %llu", command
->path
, (unsigned long long) pid
);
947 command
->exec_status
.pid
= pid
;
948 command
->exec_status
.start_timestamp
= now(CLOCK_REALTIME
);
954 void exec_context_init(ExecContext
*c
) {
959 c
->oom_adjust_set
= false;
962 c
->ioprio
= IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE
, 0);
963 c
->ioprio_set
= false;
964 c
->cpu_sched_policy
= SCHED_OTHER
;
965 c
->cpu_sched_priority
= 0;
966 c
->cpu_sched_set
= false;
967 CPU_ZERO(&c
->cpu_affinity
);
968 c
->cpu_affinity_set
= false;
969 c
->timer_slack_ns
= 0;
970 c
->timer_slack_ns_set
= false;
972 c
->cpu_sched_reset_on_fork
= false;
973 c
->non_blocking
= false;
978 c
->syslog_priority
= LOG_DAEMON
|LOG_INFO
;
981 c
->capability_bounding_set_drop
= 0;
984 void exec_context_done(ExecContext
*c
) {
989 strv_free(c
->environment
);
990 c
->environment
= NULL
;
992 for (l
= 0; l
< ELEMENTSOF(c
->rlimit
); l
++) {
997 free(c
->working_directory
);
998 c
->working_directory
= NULL
;
999 free(c
->root_directory
);
1000 c
->root_directory
= NULL
;
1005 free(c
->syslog_identifier
);
1006 c
->syslog_identifier
= NULL
;
1014 strv_free(c
->supplementary_groups
);
1015 c
->supplementary_groups
= NULL
;
1017 if (c
->capabilities
) {
1018 cap_free(c
->capabilities
);
1019 c
->capabilities
= NULL
;
1023 void exec_command_done(ExecCommand
*c
) {
1033 void exec_command_done_array(ExecCommand
*c
, unsigned n
) {
1036 for (i
= 0; i
< n
; i
++)
1037 exec_command_done(c
+i
);
1040 void exec_command_free_list(ExecCommand
*c
) {
1044 LIST_REMOVE(ExecCommand
, command
, c
, i
);
1045 exec_command_done(i
);
1050 void exec_command_free_array(ExecCommand
**c
, unsigned n
) {
1053 for (i
= 0; i
< n
; i
++) {
1054 exec_command_free_list(c
[i
]);
1059 void exec_context_dump(ExecContext
*c
, FILE* f
, const char *prefix
) {
1071 "%sWorkingDirectory: %s\n"
1072 "%sRootDirectory: %s\n"
1073 "%sNonBlocking: %s\n",
1075 prefix
, c
->working_directory
? c
->working_directory
: "/",
1076 prefix
, c
->root_directory
? c
->root_directory
: "/",
1077 prefix
, yes_no(c
->non_blocking
));
1080 for (e
= c
->environment
; *e
; e
++)
1081 fprintf(f
, "%sEnvironment: %s\n", prefix
, *e
);
1088 if (c
->oom_adjust_set
)
1090 "%sOOMAdjust: %i\n",
1091 prefix
, c
->oom_adjust
);
1093 for (i
= 0; i
< RLIM_NLIMITS
; i
++)
1095 fprintf(f
, "%s%s: %llu\n", prefix
, rlimit_to_string(i
), (unsigned long long) c
->rlimit
[i
]->rlim_max
);
1099 "%sIOSchedulingClass: %s\n"
1100 "%sIOPriority: %i\n",
1101 prefix
, ioprio_class_to_string(IOPRIO_PRIO_CLASS(c
->ioprio
)),
1102 prefix
, (int) IOPRIO_PRIO_DATA(c
->ioprio
));
1104 if (c
->cpu_sched_set
)
1106 "%sCPUSchedulingPolicy: %s\n"
1107 "%sCPUSchedulingPriority: %i\n"
1108 "%sCPUSchedulingResetOnFork: %s\n",
1109 prefix
, sched_policy_to_string(c
->cpu_sched_policy
),
1110 prefix
, c
->cpu_sched_priority
,
1111 prefix
, yes_no(c
->cpu_sched_reset_on_fork
));
1113 if (c
->cpu_affinity_set
) {
1114 fprintf(f
, "%sCPUAffinity:", prefix
);
1115 for (i
= 0; i
< CPU_SETSIZE
; i
++)
1116 if (CPU_ISSET(i
, &c
->cpu_affinity
))
1117 fprintf(f
, " %i", i
);
1121 if (c
->timer_slack_ns_set
)
1122 fprintf(f
, "%sTimerSlackNS: %lu\n", prefix
, c
->timer_slack_ns
);
1125 "%sStandardInput: %s\n"
1126 "%sStandardOutput: %s\n"
1127 "%sStandardError: %s\n",
1128 prefix
, exec_input_to_string(c
->std_input
),
1129 prefix
, exec_output_to_string(c
->std_output
),
1130 prefix
, exec_output_to_string(c
->std_error
));
1135 prefix
, c
->tty_path
);
1137 if (c
->std_output
== EXEC_OUTPUT_SYSLOG
|| c
->std_output
== EXEC_OUTPUT_KERNEL
||
1138 c
->std_error
== EXEC_OUTPUT_SYSLOG
|| c
->std_error
== EXEC_OUTPUT_KERNEL
)
1140 "%sSyslogFacility: %s\n"
1141 "%sSyslogLevel: %s\n",
1142 prefix
, log_facility_to_string(LOG_FAC(c
->syslog_priority
)),
1143 prefix
, log_level_to_string(LOG_PRI(c
->syslog_priority
)));
1145 if (c
->capabilities
) {
1147 if ((t
= cap_to_text(c
->capabilities
, NULL
))) {
1148 fprintf(f
, "%sCapabilities: %s\n",
1155 fprintf(f
, "%sSecure Bits:%s%s%s%s%s%s\n",
1157 (c
->secure_bits
& SECURE_KEEP_CAPS
) ? " keep-caps" : "",
1158 (c
->secure_bits
& SECURE_KEEP_CAPS_LOCKED
) ? " keep-caps-locked" : "",
1159 (c
->secure_bits
& SECURE_NO_SETUID_FIXUP
) ? " no-setuid-fixup" : "",
1160 (c
->secure_bits
& SECURE_NO_SETUID_FIXUP_LOCKED
) ? " no-setuid-fixup-locked" : "",
1161 (c
->secure_bits
& SECURE_NOROOT
) ? " noroot" : "",
1162 (c
->secure_bits
& SECURE_NOROOT_LOCKED
) ? "noroot-locked" : "");
1164 if (c
->capability_bounding_set_drop
) {
1165 fprintf(f
, "%sCapabilityBoundingSetDrop:", prefix
);
1167 for (i
= 0; i
<= CAP_LAST_CAP
; i
++)
1168 if (c
->capability_bounding_set_drop
& (1 << i
)) {
1171 if ((t
= cap_to_name(i
))) {
1172 fprintf(f
, " %s", t
);
1181 fprintf(f
, "%sUser: %s", prefix
, c
->user
);
1183 fprintf(f
, "%sGroup: %s", prefix
, c
->group
);
1185 if (c
->supplementary_groups
) {
1188 fprintf(f
, "%sSupplementaryGroups:", prefix
);
1190 STRV_FOREACH(g
, c
->supplementary_groups
)
1191 fprintf(f
, " %s", *g
);
1197 void exec_status_fill(ExecStatus
*s
, pid_t pid
, int code
, int status
) {
1201 s
->exit_timestamp
= now(CLOCK_REALTIME
);
1207 void exec_status_dump(ExecStatus
*s
, FILE *f
, const char *prefix
) {
1208 char buf
[FORMAT_TIMESTAMP_MAX
];
1221 prefix
, (unsigned long long) s
->pid
);
1223 if (s
->start_timestamp
> 0)
1225 "%sStart Timestamp: %s\n",
1226 prefix
, format_timestamp(buf
, sizeof(buf
), s
->start_timestamp
));
1228 if (s
->exit_timestamp
> 0)
1230 "%sExit Timestamp: %s\n"
1232 "%sExit Status: %i\n",
1233 prefix
, format_timestamp(buf
, sizeof(buf
), s
->exit_timestamp
),
1234 prefix
, sigchld_code_to_string(s
->code
),
1238 char *exec_command_line(ExecCommand
*c
) {
1247 STRV_FOREACH(a
, c
->argv
)
1250 if (!(n
= new(char, k
)))
1254 STRV_FOREACH(a
, c
->argv
) {
1261 if (strpbrk(*a
, WHITESPACE
)) {
1272 /* FIXME: this doesn't really handle arguments that have
1273 * spaces and ticks in them */
1278 void exec_command_dump(ExecCommand
*c
, FILE *f
, const char *prefix
) {
1280 const char *prefix2
;
1289 p2
= strappend(prefix
, "\t");
1290 prefix2
= p2
? p2
: prefix
;
1292 cmd
= exec_command_line(c
);
1295 "%sCommand Line: %s\n",
1296 prefix
, cmd
? cmd
: strerror(ENOMEM
));
1300 exec_status_dump(&c
->exec_status
, f
, prefix2
);
1305 void exec_command_dump_list(ExecCommand
*c
, FILE *f
, const char *prefix
) {
1311 LIST_FOREACH(command
, c
, c
)
1312 exec_command_dump(c
, f
, prefix
);
1315 void exec_command_append_list(ExecCommand
**l
, ExecCommand
*e
) {
1322 /* It's kinda important that we keep the order here */
1323 LIST_FIND_TAIL(ExecCommand
, command
, *l
, end
);
1324 LIST_INSERT_AFTER(ExecCommand
, command
, *l
, end
, e
);
1329 int exec_command_set(ExecCommand
*c
, const char *path
, ...) {
1337 l
= strv_new_ap(path
, ap
);
1343 if (!(p
= strdup(path
))) {
1357 const char* exit_status_to_string(ExitStatus status
) {
1366 case EXIT_INVALIDARGUMENT
:
1367 return "INVALIDARGUMENT";
1369 case EXIT_NOTIMPLEMENTED
:
1370 return "NOTIMPLEMENTED";
1372 case EXIT_NOPERMISSION
:
1373 return "NOPERMISSION";
1375 case EXIT_NOTINSTALLED
:
1376 return "NOTINSSTALLED";
1378 case EXIT_NOTCONFIGURED
:
1379 return "NOTCONFIGURED";
1381 case EXIT_NOTRUNNING
:
1382 return "NOTRUNNING";
1402 case EXIT_OOM_ADJUST
:
1403 return "OOM_ADJUST";
1405 case EXIT_SIGNAL_MASK
:
1406 return "SIGNAL_MASK";
1420 case EXIT_TIMERSLACK
:
1421 return "TIMERSLACK";
1423 case EXIT_SECUREBITS
:
1424 return "SECUREBITS";
1426 case EXIT_SETSCHEDULER
:
1427 return "SETSCHEDULER";
1429 case EXIT_CPUAFFINITY
:
1430 return "CPUAFFINITY";
1438 case EXIT_CAPABILITIES
:
1439 return "CAPABILITIES";
1458 static const char* const exec_input_table
[_EXEC_INPUT_MAX
] = {
1459 [EXEC_INPUT_NULL
] = "null",
1460 [EXEC_INPUT_TTY
] = "tty",
1461 [EXEC_INPUT_TTY_FORCE
] = "tty-force",
1462 [EXEC_INPUT_TTY_FAIL
] = "tty-fail"
1465 static const char* const exec_output_table
[_EXEC_OUTPUT_MAX
] = {
1466 [EXEC_OUTPUT_INHERIT
] = "inherit",
1467 [EXEC_OUTPUT_NULL
] = "null",
1468 [EXEC_OUTPUT_TTY
] = "tty",
1469 [EXEC_OUTPUT_SYSLOG
] = "syslog",
1470 [EXEC_OUTPUT_KERNEL
] = "kernel"
1473 DEFINE_STRING_TABLE_LOOKUP(exec_output
, ExecOutput
);
1475 DEFINE_STRING_TABLE_LOOKUP(exec_input
, ExecInput
);