execute.c revision 1b6d7fa742e303611dff8d7ebfa86ee5fb8b7dc7
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <grp.h>
#include <pwd.h>
#include <glob.h>
#include <sys/personality.h>
#include <libgen.h>
#ifdef HAVE_PAM
#include <security/pam_appl.h>
#endif
#ifdef HAVE_SELINUX
#endif
#ifdef HAVE_SECCOMP
#include <seccomp.h>
#endif
#ifdef HAVE_APPARMOR
#include <sys/apparmor.h>
#endif
#include "execute.h"
#include "strv.h"
#include "macro.h"
#include "capability.h"
#include "util.h"
#include "log.h"
#include "sd-messages.h"
#include "ioprio.h"
#include "securebits.h"
#include "namespace.h"
#include "exit-status.h"
#include "missing.h"
#include "utmp-wtmp.h"
#include "def.h"
#include "path-util.h"
#include "env-util.h"
#include "fileio.h"
#include "unit.h"
#include "async.h"
#include "selinux-util.h"
#include "errno-list.h"
#include "af-list.h"
#include "mkdir.h"
#include "apparmor-util.h"
#ifdef HAVE_SECCOMP
#include "seccomp-util.h"
#endif
/* This assumes there is a 'tty' group */
#define TTY_MODE 0620
int start, restart_from;
if (n_fds <= 0)
return 0;
/* Modifies the fds array! (sorts it) */
start = 0;
for (;;) {
int i;
restart_from = -1;
int nfd;
/* Already at right index? */
if (fds[i] == i+3)
continue;
return -errno;
safe_close(fds[i]);
/* Hmm, the fd we wanted isn't free? Then
* let's remember that and try again from here*/
restart_from = i;
}
if (restart_from < 0)
break;
}
return 0;
}
unsigned i;
int r;
if (n_fds <= 0)
return 0;
for (i = 0; i < n_fds; i++) {
return r;
/* We unconditionally drop FD_CLOEXEC from the fds,
* since after all we want to pass these fds to our
* children */
if ((r = fd_cloexec(fds[i], false)) < 0)
return r;
}
return 0;
}
}
if (context->tty_vhangup)
}
static bool is_terminal_output(ExecOutput o) {
return
o == EXEC_OUTPUT_TTY ||
o == EXEC_OUTPUT_SYSLOG_AND_CONSOLE ||
o == EXEC_OUTPUT_KMSG_AND_CONSOLE ||
}
int fd, r;
if (fd < 0)
return -errno;
safe_close(fd);
} else
r = nfd;
return r;
}
static int connect_logger_as(const ExecContext *context, ExecOutput output, const char *ident, const char *unit_id, int nfd) {
int fd, r;
union sockaddr_union sa = {
};
assert(ident);
if (fd < 0)
return -errno;
if (r < 0) {
safe_close(fd);
return -errno;
}
safe_close(fd);
return -errno;
}
"%s\n"
"%s\n"
"%i\n"
"%i\n"
"%i\n"
"%i\n"
"%i\n",
safe_close(fd);
} else
r = nfd;
return r;
}
int fd, r;
return fd;
safe_close(fd);
} else
r = nfd;
return r;
}
static bool is_terminal_input(ExecInput i) {
return
i == EXEC_INPUT_TTY ||
i == EXEC_INPUT_TTY_FORCE ||
i == EXEC_INPUT_TTY_FAIL;
}
return EXEC_INPUT_NULL;
return EXEC_INPUT_NULL;
return std_input;
}
return EXEC_OUTPUT_INHERIT;
return std_output;
}
ExecInput i;
switch (i) {
case EXEC_INPUT_NULL:
case EXEC_INPUT_TTY:
case EXEC_INPUT_TTY_FORCE:
case EXEC_INPUT_TTY_FAIL: {
int fd, r;
i == EXEC_INPUT_TTY_FAIL,
i == EXEC_INPUT_TTY_FORCE,
false,
if (fd < 0)
return fd;
if (fd != STDIN_FILENO) {
safe_close(fd);
} else
r = STDIN_FILENO;
return r;
}
case EXEC_INPUT_SOCKET:
default:
assert_not_reached("Unknown input type");
}
}
static int setup_output(const ExecContext *context, int fileno, int socket_fd, const char *ident, const char *unit_id, bool apply_tty_stdin) {
ExecOutput o;
ExecInput i;
int r;
assert(ident);
if (fileno == STDERR_FILENO) {
ExecOutput e;
/* This expects the input and output are already set up */
/* Don't change the stderr file descriptor if we inherit all
* the way and are not on a tty */
if (e == EXEC_OUTPUT_INHERIT &&
o == EXEC_OUTPUT_INHERIT &&
i == EXEC_INPUT_NULL &&
getppid () != 1)
return fileno;
/* Duplicate from stdout if possible */
if (e == o || e == EXEC_OUTPUT_INHERIT)
o = e;
} else if (o == EXEC_OUTPUT_INHERIT) {
/* If input got downgraded, inherit the original value */
if (i != EXEC_INPUT_NULL)
/* If we are not started from PID 1 we just inherit STDOUT from our parent process. */
if (getppid() != 1)
return fileno;
}
switch (o) {
case EXEC_OUTPUT_NULL:
case EXEC_OUTPUT_TTY:
if (is_terminal_input(i))
/* We don't reset the terminal if this is just about output */
case EXEC_OUTPUT_SYSLOG:
case EXEC_OUTPUT_KMSG:
case EXEC_OUTPUT_JOURNAL:
if (r < 0) {
"MESSAGE=Failed to connect std%s of %s to the journal socket: %s",
"ERRNO=%d", -r,
NULL);
}
return r;
case EXEC_OUTPUT_SOCKET:
default:
assert_not_reached("Unknown error type");
}
}
/* This might fail. What matters are the results. */
return -errno;
return -EPERM;
return 0;
}
static int setup_confirm_stdio(int *_saved_stdin,
int *_saved_stdout) {
if (saved_stdin < 0)
return -errno;
if (saved_stdout < 0) {
r = errno;
goto fail;
}
false,
false,
false,
if (fd < 0) {
r = fd;
goto fail;
}
if (r < 0)
goto fail;
r = -errno;
goto fail;
}
r = -errno;
goto fail;
}
if (fd >= 2)
safe_close(fd);
return 0;
fail:
safe_close(fd);
return r;
}
if (fd < 0)
return fd;
return 0;
}
static int restore_confirm_stdio(int *saved_stdin,
int *saved_stdout) {
int r = 0;
if (*saved_stdin >= 0)
r = -errno;
if (*saved_stdout >= 0)
r = -errno;
return r;
}
if (r < 0)
return r;
if (!line)
return -ENOMEM;
return r;
}
bool keep_groups = false;
int r;
/* Lookup and set GID and supplementary group list. Here too
* we avoid NSS lookups for gid=0. */
if ((r = get_group_creds(&g, &gid)) < 0)
return r;
}
return -errno;
keep_groups = true;
}
/* Second step, set our gids */
return -errno;
}
if (context->supplementary_groups) {
int ngroups_max, k;
char **i;
/* Final step, initialize any manually set supplementary groups */
return -ENOMEM;
if (keep_groups) {
return -errno;
}
} else
k = 0;
const char *g;
if (k >= ngroups_max) {
return -E2BIG;
}
g = *i;
r = get_group_creds(&g, gids+k);
if (r < 0) {
return r;
}
k++;
}
return -errno;
}
}
return 0;
}
/* Sets (but doesn't lookup) the uid and make sure we keep the
* capabilities while doing so. */
if (context->capabilities) {
static const cap_value_t bits[] = {
CAP_SETUID, /* Necessary so that we can run setresuid() below */
CAP_SETPCAP /* Necessary so that we can set PR_SET_SECUREBITS later on */
};
/* First step: If we need to keep capabilities but
* drop privileges we need to make sure we keep our
* caps, while we drop privileges. */
if (uid != 0) {
return -errno;
}
/* Second step: set the capabilities. This will reduce
* the capabilities to the minimum we need. */
if (!d)
return -errno;
return -errno;
if (cap_set_proc(d) < 0)
return -errno;
}
/* Third step: actually set the uids */
return -errno;
/* At this point we should have all necessary capabilities but
are otherwise a normal user. However, the caps might got
corrupted due to the setresuid() so we need clean them up
later. This is done outside of this call. */
return 0;
}
#ifdef HAVE_PAM
static int null_conv(
int num_msg,
const struct pam_message **msg,
struct pam_response **resp,
void *appdata_ptr) {
/* We don't support conversations */
return PAM_CONV_ERR;
}
static int setup_pam(
const char *name,
const char *user,
const char *tty,
char ***pam_env,
.appdata_ptr = NULL
};
int pam_code = PAM_SUCCESS;
int err;
char **e = NULL;
bool close_session = false;
int flags = 0;
/* We set up PAM in the parent process, then fork. The child
* will then stay around until killed via PR_GET_PDEATHSIG or
* systemd via the cgroup logic. It will then remove the PAM
* session again. The parent process will exec() the actual
* daemon. We do things this way to ensure that the main PID
* of the daemon is the one we initially fork()ed. */
flags |= PAM_SILENT;
if (pam_code != PAM_SUCCESS) {
goto fail;
}
if (tty) {
if (pam_code != PAM_SUCCESS)
goto fail;
}
if (pam_code != PAM_SUCCESS)
goto fail;
if (pam_code != PAM_SUCCESS)
goto fail;
close_session = true;
e = pam_getenvlist(handle);
if (!e) {
goto fail;
}
/* Block SIGTERM, so that we know that it won't get lost in
* the child */
if (sigemptyset(&ss) < 0 ||
goto fail;
parent_pid = getpid();
if (pam_pid < 0)
goto fail;
if (pam_pid == 0) {
int sig;
int r = EXIT_PAM;
/* The child's job is to reset the PAM session on
* termination */
/* This string must fit in 10 chars (i.e. the length
rename_process("(sd-pam)");
/* Make sure we don't keep open the passed fds in this
child. We assume that otherwise only those fds are
open here that have been opened by PAM. */
/* Drop privileges - we don't need any to pam_close_session
* and this will make PR_SET_PDEATHSIG work in most cases.
* If this fails, ignore the error - but expect sd-pam threads
* to fail to exit normally */
/* Wait until our parent died. This will only work if
* the above setresuid() succeeds, otherwise the kernel
* will not allow unprivileged parents kill their privileged
* children this way. We rely on the control groups kill logic
* to do the rest for us. */
goto child_finish;
/* Check if our parent process might already have
* died? */
if (getppid() == parent_pid) {
for (;;) {
continue;
goto child_finish;
}
break;
}
}
/* If our parent died we'll end the session */
if (getppid() != parent_pid) {
if (pam_code != PAM_SUCCESS)
goto child_finish;
}
r = 0;
_exit(r);
}
/* If the child was forked off successfully it will do all the
* cleanups, so forget about the handle here. */
/* Unblock SIGTERM again in the parent */
goto fail;
/* We close the log explicitly here, since the PAM modules
* might have opened it, but we don't want this fd around. */
closelog();
*pam_env = e;
e = NULL;
return 0;
fail:
if (pam_code != PAM_SUCCESS) {
} else {
log_error("PAM failed: %m");
}
if (handle) {
if (close_session)
}
strv_free(e);
closelog();
if (pam_pid > 1) {
}
return err;
}
#endif
static void rename_process_from_path(const char *path) {
char process_name[11];
const char *p;
size_t l;
/* This resulting string must fit in 10 chars (i.e. the length
if (isempty(p)) {
rename_process("(...)");
return;
}
l = strlen(p);
if (l > 8) {
/* The end of the process name is usually more
* interesting, since the first bit might just be
* "systemd-" */
p = p + l - 8;
l = 8;
}
process_name[0] = '(';
}
#ifdef HAVE_SECCOMP
static int apply_seccomp(ExecContext *c) {
Iterator i;
void *id;
int r;
assert(c);
if (!seccomp)
return -ENOMEM;
if (c->syscall_archs) {
if (r == -EEXIST)
continue;
if (r < 0)
goto finish;
}
} else {
if (r < 0)
goto finish;
}
if (r < 0)
goto finish;
}
if (r < 0)
goto finish;
r = seccomp_load(seccomp);
return r;
}
static int apply_address_families(ExecContext *c) {
Iterator i;
int r;
assert(c);
if (!seccomp)
return -ENOMEM;
if (r < 0)
goto finish;
if (c->address_families_whitelist) {
void *afp;
/* If this is a whitelist, we first block the address
* families that are out of range and then everything
* that is not in the set. First, we find the lowest
* and highest address family in the set. */
continue;
}
if (first == 0) {
/* No entries in the valid range, block everything */
r = seccomp_rule_add(
0);
if (r < 0)
goto finish;
} else {
/* Block everything below the first entry */
r = seccomp_rule_add(
1,
if (r < 0)
goto finish;
/* Block everything above the last entry */
r = seccomp_rule_add(
1,
if (r < 0)
goto finish;
/* Block everything between the first and last
* entry */
continue;
r = seccomp_rule_add(
1,
if (r < 0)
goto finish;
}
}
} else {
void *af;
/* If this is a blacklist, then generate one rule for
* each address family that are then combined in OR
* checks. */
r = seccomp_rule_add(
1,
if (r < 0)
goto finish;
}
}
if (r < 0)
goto finish;
r = seccomp_load(seccomp);
return r;
}
#endif
if (idle_pipe[0] >= 0) {
int r;
/* Signal systemd that we are bored and want to continue. */
/* Wait for systemd to react to the signal above. */
}
safe_close(idle_pipe[0]);
}
}
static int build_environment(
ExecContext *c,
unsigned n_fds,
const char *home,
const char *username,
const char *shell,
char ***ret) {
unsigned n_env = 0;
char *x;
assert(c);
if (!our_env)
return -ENOMEM;
if (n_fds > 0) {
return -ENOMEM;
return -ENOMEM;
}
if (watchdog_usec > 0) {
return -ENOMEM;
return -ENOMEM;
}
if (home) {
if (!x)
return -ENOMEM;
}
if (username) {
if (!x)
return -ENOMEM;
if (!x)
return -ENOMEM;
}
if (shell) {
if (!x)
return -ENOMEM;
}
if (is_terminal_input(c->std_input) ||
c->std_output == EXEC_OUTPUT_TTY ||
c->std_error == EXEC_OUTPUT_TTY ||
c->tty_path) {
if (!x)
return -ENOMEM;
}
return 0;
}
char **argv,
char **environment,
bool apply_permissions,
bool apply_chroot,
bool apply_tty_stdin,
bool confirm_spawn,
const char *cgroup_path,
const char *runtime_prefix,
const char *unit_id,
int idle_pipe[4],
int socket_fd;
char *line;
int r;
if (n_fds != 1)
return -EINVAL;
n_fds = 0;
} else
socket_fd = -1;
if (r < 0) {
"MESSAGE=Failed to load environment files: %s", strerror(-r),
"ERRNO=%d", -r,
NULL);
return r;
}
if (!argv)
if (!line)
return log_oom();
"MESSAGE=About to execute: %s", line,
NULL);
if (pid < 0)
return -errno;
if (pid == 0) {
_cleanup_strv_free_ char **our_env = NULL, **pam_env = NULL, **final_env = NULL, **final_argv = NULL;
unsigned n_dont_close = 0;
int i, err;
/* child */
/* We reset exactly these signals, since they are the
* only ones we set to SIG_IGN in the main daemon. All
* others we leave untouched because we set them to
* SIG_DFL or a valid handler initially, both of which
* will be demoted to SIG_DFL. */
SIGNALS_IGNORE, -1);
if (context->ignore_sigpipe)
err = reset_signal_mask();
if (err < 0) {
r = EXIT_SIGNAL_MASK;
goto fail_child;
}
if (idle_pipe)
/* Close sockets very early to make sure we don't
* block init reexecution because it cannot bind its
* sockets */
if (socket_fd >= 0)
if (n_fds > 0) {
n_dont_close += n_fds;
}
if (runtime) {
if (runtime->netns_storage_socket[0] >= 0)
}
if (err < 0) {
r = EXIT_FDS;
goto fail_child;
}
if (setsid() < 0) {
r = EXIT_SETSID;
goto fail_child;
}
if (confirm_spawn) {
char response;
write_confirm_message("Confirmation question timed out, assuming positive response.\n");
else if (err < 0)
write_confirm_message("Couldn't ask confirmation question, assuming positive response: %s\n", strerror(-err));
else if (response == 's') {
write_confirm_message("Skipping execution.\n");
r = EXIT_CONFIRM;
goto fail_child;
} else if (response == 'n') {
write_confirm_message("Failing execution.\n");
err = r = 0;
goto fail_child;
}
}
* must sure to drop O_NONBLOCK */
if (socket_fd >= 0)
fd_nonblock(socket_fd, false);
if (err < 0) {
r = EXIT_STDIN;
goto fail_child;
}
err = setup_output(context, STDOUT_FILENO, socket_fd, basename(command->path), unit_id, apply_tty_stdin);
if (err < 0) {
r = EXIT_STDOUT;
goto fail_child;
}
err = setup_output(context, STDERR_FILENO, socket_fd, basename(command->path), unit_id, apply_tty_stdin);
if (err < 0) {
r = EXIT_STDERR;
goto fail_child;
}
if (cgroup_path) {
if (err < 0) {
r = EXIT_CGROUP;
goto fail_child;
}
}
if (context->oom_score_adjust_set) {
char t[16];
char_array_0(t);
if (write_string_file("/proc/self/oom_score_adj", t) < 0) {
r = EXIT_OOM_ADJUST;
goto fail_child;
}
}
r = EXIT_NICE;
goto fail_child;
}
if (context->cpu_sched_set) {
struct sched_param param = {
};
r = sched_setscheduler(0,
SCHED_RESET_ON_FORK : 0),
¶m);
if (r < 0) {
r = EXIT_SETSCHEDULER;
goto fail_child;
}
}
r = EXIT_CPUAFFINITY;
goto fail_child;
}
if (context->ioprio_set)
r = EXIT_IOPRIO;
goto fail_child;
}
r = EXIT_TIMERSLACK;
goto fail_child;
}
r = EXIT_PERSONALITY;
goto fail_child;
}
if (err < 0) {
r = EXIT_USER;
goto fail_child;
}
if (err < 0) {
r = EXIT_STDIN;
goto fail_child;
}
}
}
#ifdef HAVE_PAM
if (err < 0) {
r = EXIT_CGROUP;
goto fail_child;
}
if (err < 0) {
r = EXIT_CGROUP;
goto fail_child;
}
}
#endif
char **rt;
_cleanup_free_ char *p;
if (!p) {
goto fail_child;
}
if (err < 0) {
goto fail_child;
}
}
}
if (apply_permissions) {
if (err < 0) {
r = EXIT_GROUP;
goto fail_child;
}
}
#ifdef HAVE_PAM
if (err < 0) {
r = EXIT_PAM;
goto fail_child;
}
}
#endif
if (err < 0) {
r = EXIT_NETWORK;
goto fail_child;
}
}
context->mount_flags != 0 ||
/* The runtime struct only contains the parent
* of the private /tmp, which is
* non-accessible to world users. Inside of it
* there's a /tmp that is sticky, and that's
* the one we want to use here. */
if (runtime->var_tmp_dir)
}
tmp,
var,
if (err < 0) {
r = EXIT_NAMESPACE;
goto fail_child;
}
}
if (apply_chroot) {
if (context->root_directory)
r = EXIT_CHROOT;
goto fail_child;
}
r = EXIT_CHDIR;
goto fail_child;
}
} else {
_cleanup_free_ char *d = NULL;
if (asprintf(&d, "%s/%s",
r = EXIT_MEMORY;
goto fail_child;
}
if (chdir(d) < 0) {
r = EXIT_CHDIR;
goto fail_child;
}
}
/* We repeat the fd closing here, to make sure that
* nothing is leaked from the PAM modules. Note that
* we are more aggressive this time since socket_fd
* and the netns fds we don#t need anymore. */
if (err >= 0)
if (err >= 0)
if (err < 0) {
r = EXIT_FDS;
goto fail_child;
}
if (apply_permissions) {
for (i = 0; i < _RLIMIT_MAX; i++) {
continue;
r = EXIT_LIMITS;
goto fail_child;
}
}
if (context->capability_bounding_set_drop) {
if (err < 0) {
r = EXIT_CAPABILITIES;
goto fail_child;
}
}
if (err < 0) {
r = EXIT_USER;
goto fail_child;
}
}
/* PR_GET_SECUREBITS is not privileged, while
* PR_SET_SECUREBITS is. So to suppress
* potential EPERMs we'll try not to call
* PR_SET_SECUREBITS unless necessary. */
r = EXIT_SECUREBITS;
goto fail_child;
}
if (context->capabilities)
r = EXIT_CAPABILITIES;
goto fail_child;
}
if (context->no_new_privileges)
goto fail_child;
}
#ifdef HAVE_SECCOMP
if (context->address_families_whitelist ||
if (err < 0) {
goto fail_child;
}
}
if (context->syscall_whitelist ||
if (err < 0) {
r = EXIT_SECCOMP;
goto fail_child;
}
}
#endif
#ifdef HAVE_SELINUX
r = EXIT_SELINUX_CONTEXT;
goto fail_child;
}
}
#endif
#ifdef HAVE_APPARMOR
goto fail_child;
}
}
#endif
}
if (r < 0) {
r = EXIT_MEMORY;
goto fail_child;
}
NULL);
if (!final_env) {
r = EXIT_MEMORY;
goto fail_child;
}
if (!final_argv) {
r = EXIT_MEMORY;
goto fail_child;
}
if (line) {
log_open();
"MESSAGE=Executing: %s", line,
NULL);
log_close();
}
}
r = EXIT_EXEC;
if (r != 0) {
log_open();
"MESSAGE=Failed at step %s spawning %s: %s",
"ERRNO=%d", -err,
NULL);
log_close();
}
_exit(r);
}
"MESSAGE=Forked %s as "PID_FMT,
NULL);
/* We add the new process to the cgroup both in the child (so
* that we can be sure that no user code is ever executed
* outside of the cgroup) and in the parent (so that we can be
* sure that when we kill the cgroup the process will be
* killed too). */
if (cgroup_path)
return 0;
}
void exec_context_init(ExecContext *c) {
assert(c);
c->umask = 0022;
c->cpu_sched_policy = SCHED_OTHER;
c->syslog_level_prefix = true;
c->ignore_sigpipe = true;
c->personality = 0xffffffffUL;
c->runtime_directory_mode = 0755;
}
void exec_context_done(ExecContext *c) {
unsigned l;
assert(c);
strv_free(c->environment);
c->environment = NULL;
c->environment_files = NULL;
for (l = 0; l < ELEMENTSOF(c->rlimit); l++) {
}
free(c->working_directory);
c->working_directory = NULL;
free(c->root_directory);
c->root_directory = NULL;
free(c->syslog_identifier);
c->syslog_identifier = NULL;
c->supplementary_groups = NULL;
if (c->capabilities) {
cap_free(c->capabilities);
c->capabilities = NULL;
}
strv_free(c->read_only_dirs);
c->read_only_dirs = NULL;
strv_free(c->read_write_dirs);
c->read_write_dirs = NULL;
c->inaccessible_dirs = NULL;
if (c->cpuset)
free(c->selinux_context);
c->selinux_context = NULL;
free(c->apparmor_profile);
c->apparmor_profile = NULL;
set_free(c->syscall_filter);
c->syscall_filter = NULL;
set_free(c->syscall_archs);
c->syscall_archs = NULL;
set_free(c->address_families);
c->address_families = NULL;
c->runtime_directory = NULL;
}
char **i;
assert(c);
if (!runtime_prefix)
return 0;
STRV_FOREACH(i, c->runtime_directory) {
_cleanup_free_ char *p;
if (!p)
return -ENOMEM;
/* We execute this synchronously, since we need to be
* sure this is gone when we start the service
* next. */
rm_rf_dangerous(p, false, true, false);
}
return 0;
}
void exec_command_done(ExecCommand *c) {
assert(c);
}
void exec_command_done_array(ExecCommand *c, unsigned n) {
unsigned i;
for (i = 0; i < n; i++)
exec_command_done(c+i);
}
void exec_command_free_list(ExecCommand *c) {
ExecCommand *i;
while ((i = c)) {
LIST_REMOVE(command, c, i);
free(i);
}
}
void exec_command_free_array(ExecCommand **c, unsigned n) {
unsigned i;
for (i = 0; i < n; i++) {
exec_command_free_list(c[i]);
c[i] = NULL;
}
}
int exec_context_load_environment(const ExecContext *c, char ***l) {
char **i, **r = NULL;
assert(c);
assert(l);
STRV_FOREACH(i, c->environment_files) {
char *fn;
int k;
bool ignore = false;
char **p;
int count, n;
fn = *i;
if (fn[0] == '-') {
ignore = true;
fn ++;
}
if (!path_is_absolute(fn)) {
if (ignore)
continue;
strv_free(r);
return -EINVAL;
}
/* Filename supports globbing, take all matching files */
errno = 0;
if (ignore)
continue;
strv_free(r);
}
if (count == 0) {
if (ignore)
continue;
strv_free(r);
return -EINVAL;
}
for (n = 0; n < count; n++) {
if (k < 0) {
if (ignore)
continue;
strv_free(r);
return k;
}
/* Log invalid environment variables with filename */
if (p)
if (r == NULL)
r = p;
else {
char **m;
m = strv_env_merge(2, r, p);
strv_free(r);
strv_free(p);
if (!m)
return -ENOMEM;
r = m;
}
}
}
*l = r;
return 0;
}
static bool tty_may_match_dev_console(const char *tty) {
char *console;
tty += 5;
/* trivial identity? */
return true;
/* if we could not resolve, assume it may */
if (!console)
return true;
/* "tty0" means the active VC, so it may be the same sometimes */
}
}
static void strv_fprintf(FILE *f, char **l) {
char **g;
assert(f);
STRV_FOREACH(g, l)
fprintf(f, " %s", *g);
}
char **e;
unsigned i;
assert(c);
assert(f);
fprintf(f,
"%sUMask: %04o\n"
"%sWorkingDirectory: %s\n"
"%sRootDirectory: %s\n"
"%sNonBlocking: %s\n"
"%sPrivateTmp: %s\n"
"%sPrivateNetwork: %s\n"
"%sPrivateDevices: %s\n"
"%sProtectHome: %s\n"
"%sProtectSystem: %s\n"
"%sIgnoreSIGPIPE: %s\n",
STRV_FOREACH(e, c->environment)
STRV_FOREACH(e, c->environment_files)
if (c->nice_set)
fprintf(f,
"%sNice: %i\n",
if (c->oom_score_adjust_set)
fprintf(f,
"%sOOMScoreAdjust: %i\n",
prefix, c->oom_score_adjust);
for (i = 0; i < RLIM_NLIMITS; i++)
if (c->rlimit[i])
if (c->ioprio_set) {
fprintf(f,
"%sIOSchedulingClass: %s\n"
"%sIOPriority: %i\n",
}
if (c->cpu_sched_set) {
fprintf(f,
"%sCPUSchedulingPolicy: %s\n"
"%sCPUSchedulingPriority: %i\n"
"%sCPUSchedulingResetOnFork: %s\n",
prefix, c->cpu_sched_priority,
}
if (c->cpuset) {
for (i = 0; i < c->cpuset_ncpus; i++)
fprintf(f, " %u", i);
fputs("\n", f);
}
if (c->timer_slack_nsec != NSEC_INFINITY)
fprintf(f,
"%sStandardInput: %s\n"
"%sStandardOutput: %s\n"
"%sStandardError: %s\n",
if (c->tty_path)
fprintf(f,
"%sTTYPath: %s\n"
"%sTTYReset: %s\n"
"%sTTYVHangup: %s\n"
"%sTTYVTDisallocate: %s\n",
if (c->std_output == EXEC_OUTPUT_SYSLOG ||
c->std_output == EXEC_OUTPUT_KMSG ||
c->std_output == EXEC_OUTPUT_JOURNAL ||
c->std_output == EXEC_OUTPUT_SYSLOG_AND_CONSOLE ||
c->std_output == EXEC_OUTPUT_KMSG_AND_CONSOLE ||
c->std_error == EXEC_OUTPUT_SYSLOG ||
c->std_error == EXEC_OUTPUT_KMSG ||
c->std_error == EXEC_OUTPUT_JOURNAL ||
c->std_error == EXEC_OUTPUT_SYSLOG_AND_CONSOLE ||
c->std_error == EXEC_OUTPUT_KMSG_AND_CONSOLE ||
c->std_error == EXEC_OUTPUT_JOURNAL_AND_CONSOLE) {
fprintf(f,
"%sSyslogFacility: %s\n"
"%sSyslogLevel: %s\n",
}
if (c->capabilities) {
_cleanup_cap_free_charp_ char *t;
if (t)
}
if (c->secure_bits)
fprintf(f, "%sSecure Bits:%s%s%s%s%s%s\n",
if (c->capability_bounding_set_drop) {
unsigned long l;
for (l = 0; l <= cap_last_cap(); l++)
_cleanup_cap_free_charp_ char *t;
t = cap_to_name(l);
if (t)
fprintf(f, " %s", t);
}
fputs("\n", f);
}
if (c->user)
if (c->group)
if (strv_length(c->supplementary_groups) > 0) {
strv_fprintf(f, c->supplementary_groups);
fputs("\n", f);
}
if (c->pam_name)
if (strv_length(c->read_write_dirs) > 0) {
strv_fprintf(f, c->read_write_dirs);
fputs("\n", f);
}
if (strv_length(c->read_only_dirs) > 0) {
strv_fprintf(f, c->read_only_dirs);
fputs("\n", f);
}
if (strv_length(c->inaccessible_dirs) > 0) {
strv_fprintf(f, c->inaccessible_dirs);
fputs("\n", f);
}
if (c->utmp_id)
fprintf(f,
"%sUtmpIdentifier: %s\n",
if (c->selinux_context)
fprintf(f,
"%sSELinuxContext: %s%s\n",
if (c->personality != 0xffffffffUL)
fprintf(f,
"%sPersonality: %s\n",
if (c->syscall_filter) {
#ifdef HAVE_SECCOMP
Iterator j;
void *id;
bool first = true;
#endif
fprintf(f,
"%sSystemCallFilter: ",
prefix);
if (!c->syscall_whitelist)
fputc('~', f);
#ifdef HAVE_SECCOMP
if (first)
first = false;
else
fputc(' ', f);
}
#endif
fputc('\n', f);
}
if (c->syscall_archs) {
#ifdef HAVE_SECCOMP
Iterator j;
void *id;
#endif
fprintf(f,
"%sSystemCallArchitectures:",
prefix);
#ifdef HAVE_SECCOMP
#endif
fputc('\n', f);
}
if (c->syscall_errno != 0)
fprintf(f,
"%sSystemCallErrorNumber: %s\n",
if (c->apparmor_profile)
fprintf(f,
"%sAppArmorProfile: %s%s\n",
}
assert(s);
zero(*s);
}
assert(s);
zero(*s);
if (context) {
}
}
char buf[FORMAT_TIMESTAMP_MAX];
assert(s);
assert(f);
if (s->pid <= 0)
return;
fprintf(f,
if (s->start_timestamp.realtime > 0)
fprintf(f,
"%sStart Timestamp: %s\n",
if (s->exit_timestamp.realtime > 0)
fprintf(f,
"%sExit Timestamp: %s\n"
"%sExit Code: %s\n"
"%sExit Status: %i\n",
}
char *exec_command_line(char **argv) {
size_t k;
char *n, *p, **a;
bool first = true;
k = 1;
STRV_FOREACH(a, argv)
k += strlen(*a)+3;
if (!(n = new(char, k)))
return NULL;
p = n;
STRV_FOREACH(a, argv) {
if (!first)
*(p++) = ' ';
else
first = false;
if (strpbrk(*a, WHITESPACE)) {
*(p++) = '\'';
p = stpcpy(p, *a);
*(p++) = '\'';
} else
p = stpcpy(p, *a);
}
*p = 0;
/* FIXME: this doesn't really handle arguments that have
* spaces and ticks in them */
return n;
}
const char *prefix2;
assert(c);
assert(f);
fprintf(f,
"%sCommand Line: %s\n",
}
assert(f);
LIST_FOREACH(command, c, c)
exec_command_dump(c, f, prefix);
}
assert(l);
assert(e);
if (*l) {
/* It's kind of important, that we keep the order here */
} else
*l = e;
}
char **l, *p;
assert(c);
if (!l)
return -ENOMEM;
if (!p) {
strv_free(l);
return -ENOMEM;
}
c->path = p;
c->argv = l;
return 0;
}
if (*rt)
return 0;
if (!*rt)
return -ENOMEM;
return 0;
}
int r;
assert(c);
if (*rt)
return 1;
if (!c->private_network && !c->private_tmp)
return 0;
r = exec_runtime_allocate(rt);
if (r < 0)
return r;
return -errno;
}
if (r < 0)
return r;
}
return 1;
}
assert(r);
r->n_ref++;
return r;
}
if (!r)
return NULL;
r->n_ref--;
if (r->n_ref <= 0) {
free(r->var_tmp_dir);
free(r);
}
return NULL;
}
assert(u);
assert(f);
if (!rt)
return 0;
if (rt->var_tmp_dir)
if (rt->netns_storage_socket[0] >= 0) {
int copy;
if (copy < 0)
return copy;
}
int copy;
if (copy < 0)
return copy;
}
return 0;
}
int exec_runtime_deserialize_item(ExecRuntime **rt, Unit *u, const char *key, const char *value, FDSet *fds) {
int r;
char *copy;
r = exec_runtime_allocate(rt);
if (r < 0)
return r;
if (!copy)
return log_oom();
char *copy;
r = exec_runtime_allocate(rt);
if (r < 0)
return r;
if (!copy)
return log_oom();
int fd;
r = exec_runtime_allocate(rt);
if (r < 0)
return r;
else {
}
int fd;
r = exec_runtime_allocate(rt);
if (r < 0)
return r;
else {
}
} else
return 0;
return 1;
}
static void *remove_tmpdir_thread(void *p) {
_cleanup_free_ char *path = p;
rm_rf_dangerous(path, false, true, false);
return NULL;
}
int r;
if (!rt)
return;
/* If there are multiple users of this, let's leave the stuff around */
return;
if (r < 0) {
}
}
if (rt->var_tmp_dir) {
if (r < 0) {
}
}
}
static const char* const exec_input_table[_EXEC_INPUT_MAX] = {
[EXEC_INPUT_NULL] = "null",
[EXEC_INPUT_TTY] = "tty",
[EXEC_INPUT_TTY_FORCE] = "tty-force",
[EXEC_INPUT_TTY_FAIL] = "tty-fail",
[EXEC_INPUT_SOCKET] = "socket"
};
static const char* const exec_output_table[_EXEC_OUTPUT_MAX] = {
[EXEC_OUTPUT_INHERIT] = "inherit",
[EXEC_OUTPUT_NULL] = "null",
[EXEC_OUTPUT_TTY] = "tty",
[EXEC_OUTPUT_SYSLOG] = "syslog",
[EXEC_OUTPUT_SYSLOG_AND_CONSOLE] = "syslog+console",
[EXEC_OUTPUT_KMSG] = "kmsg",
[EXEC_OUTPUT_KMSG_AND_CONSOLE] = "kmsg+console",
[EXEC_OUTPUT_JOURNAL] = "journal",
[EXEC_OUTPUT_JOURNAL_AND_CONSOLE] = "journal+console",
[EXEC_OUTPUT_SOCKET] = "socket"
};