execute.c revision 6796073e330f49923c5134652ee085e29fc88002
/*-*- 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 <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <grp.h>
#include <poll.h>
#include <glob.h>
#include <utmpx.h>
#include <sys/personality.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 "sd-messages.h"
#include "rm-rf.h"
#include "strv.h"
#include "macro.h"
#include "capability.h"
#include "util.h"
#include "log.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 "smack-util.h"
#include "bus-endpoint.h"
#include "cap-list.h"
#include "formats-util.h"
#include "process-util.h"
#include "terminal-util.h"
#include "signal-util.h"
#ifdef HAVE_APPARMOR
#include "apparmor-util.h"
#endif
#ifdef HAVE_SECCOMP
#include "seccomp-util.h"
#endif
#include "execute.h"
/* 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;
if (nfd < 0)
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++) {
if (r < 0)
return r;
/* We unconditionally drop FD_CLOEXEC from the fds,
* since after all we want to pass these fds to our
* children */
r = fd_cloexec(fds[i], false);
if (r < 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;
}
union sockaddr_union sa = {
};
int r;
if (gid != GID_INVALID) {
if (r < 0)
return -errno;
}
if (uid != UID_INVALID) {
if (r < 0) {
r = -errno;
goto restore_gid;
}
}
if (r < 0)
r = -errno;
/* If we fail to restore the uid or gid, things will likely
fail later on. This should only happen if an LSM interferes. */
if (uid != UID_INVALID)
if (gid != GID_INVALID)
return r;
}
static int connect_logger_as(const ExecContext *context, ExecOutput output, const char *ident, const char *unit_id, int nfd, uid_t uid, gid_t gid) {
int fd, r;
assert(ident);
if (fd < 0)
return -errno;
if (r < 0)
return r;
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;
if (fd < 0)
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(Unit *unit, const ExecContext *context, int fileno, int socket_fd, const char *ident, bool apply_tty_stdin, uid_t uid, gid_t gid) {
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) {
log_unit_error_errno(unit, r, "Failed to connect %s to the journal socket, ignoring: %m", fileno == STDOUT_FILENO ? "stdout" : "stderr");
}
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. */
r = get_group_creds(&g, &gid);
if (r < 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) {
if (k < 0) {
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. */
if (log_get_max_level() < LOG_DEBUG)
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 */
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 */
log_error_errno(r, "Error: Failed to setresuid() in sd-pam: %m");
/* 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 */
/* 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 {
}
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(const 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(const 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. */
if (r > 0)
/* Wait for systemd to react to the signal above. */
}
safe_close(idle_pipe[0]);
}
}
static int build_environment(
const 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;
}
static bool exec_needs_mount_namespace(
const ExecContext *context,
const ExecParameters *params,
ExecRuntime *runtime) {
return true;
if (context->mount_flags != 0)
return true;
return true;
if (params->bus_endpoint_path)
return true;
if (context->private_devices ||
return true;
return false;
}
static int exec_child(
const ExecContext *context,
const ExecParameters *params,
char **argv,
int socket_fd,
char **files_env,
int *exit_status) {
_cleanup_strv_free_ char **our_env = NULL, **pam_env = NULL, **final_env = NULL, **final_argv = NULL;
unsigned n_dont_close = 0;
int i, r;
bool needs_mount_namespace;
/* 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. */
(void) default_signals(SIGNALS_CRASH_HANDLER,
SIGNALS_IGNORE, -1);
if (context->ignore_sigpipe)
r = reset_signal_mask();
if (r < 0) {
return r;
}
/* 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 (params->bus_endpoint_fd >= 0)
if (runtime) {
if (runtime->netns_storage_socket[0] >= 0)
}
if (r < 0) {
*exit_status = EXIT_FDS;
return r;
}
if (setsid() < 0) {
return -errno;
}
if (params->confirm_spawn) {
char response;
if (r == -ETIMEDOUT)
write_confirm_message("Confirmation question timed out, assuming positive response.\n");
else if (r < 0)
write_confirm_message("Couldn't ask confirmation question, assuming positive response: %s\n", strerror(-r));
else if (response == 's') {
write_confirm_message("Skipping execution.\n");
return -ECANCELED;
} else if (response == 'n') {
write_confirm_message("Failing execution.\n");
*exit_status = 0;
return 0;
}
}
if (r < 0) {
*exit_status = EXIT_USER;
return r;
}
}
* must sure to drop O_NONBLOCK */
if (socket_fd >= 0)
fd_nonblock(socket_fd, false);
if (r < 0) {
return r;
}
r = setup_output(unit, context, STDOUT_FILENO, socket_fd, basename(command->path), params->apply_tty_stdin, uid, gid);
if (r < 0) {
return r;
}
r = setup_output(unit, context, STDERR_FILENO, socket_fd, basename(command->path), params->apply_tty_stdin, uid, gid);
if (r < 0) {
return r;
}
if (params->cgroup_path) {
if (r < 0) {
return r;
}
}
if (context->oom_score_adjust_set) {
/* When we can't make this change due to EPERM, then
* let's silently skip over it. User namespaces
* prohibit write access to this file, and we
* shouldn't trip up over that. */
r = write_string_file("/proc/self/oom_score_adj", t, 0);
log_open();
log_unit_debug_errno(unit, r, "Failed to adjust OOM setting, assuming containerized execution, ignoring: %m");
log_close();
} else if (r < 0) {
return -errno;
}
}
*exit_status = EXIT_NICE;
return -errno;
}
if (context->cpu_sched_set) {
struct sched_param param = {
};
r = sched_setscheduler(0,
SCHED_RESET_ON_FORK : 0),
¶m);
if (r < 0) {
return -errno;
}
}
return -errno;
}
if (context->ioprio_set)
return -errno;
}
return -errno;
}
return -errno;
}
if (r < 0) {
return r;
}
}
if (r < 0) {
return r;
}
}
/* If delegation is enabled we'll pass ownership of the cgroup
* (but only in systemd's own controller hierarchy!) to the
* user of the new process. */
if (r < 0) {
return r;
}
if (r < 0) {
return r;
}
}
char **rt;
_cleanup_free_ char *p;
if (!p) {
return -ENOMEM;
}
if (r < 0) {
return r;
}
if (r < 0) {
return r;
}
}
}
if (params->apply_permissions) {
if (r < 0) {
return r;
}
}
#ifdef HAVE_PAM
if (r < 0) {
*exit_status = EXIT_PAM;
return r;
}
}
#endif
if (r < 0) {
return r;
}
}
if (needs_mount_namespace) {
/* 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)
}
r = setup_namespace(
tmp,
var,
/* If we couldn't set up the namespace this is
* probably due to a missing capability. In this case,
* silently proceeed. */
log_open();
log_unit_debug_errno(unit, r, "Failed to set up namespace, assuming containerized execution, ignoring: %m");
log_close();
} else if (r < 0) {
return r;
}
}
if (params->apply_chroot) {
return -errno;
}
return -errno;
}
} else {
_cleanup_free_ char *d = NULL;
if (asprintf(&d, "%s/%s",
return -ENOMEM;
}
if (chdir(d) < 0 &&
return -errno;
}
}
#ifdef HAVE_SELINUX
if (params->apply_permissions && mac_selinux_use() && params->selinux_context_net && socket_fd >= 0) {
r = mac_selinux_get_child_mls_label(socket_fd, command->path, context->selinux_context, &mac_selinux_context_net);
if (r < 0) {
return r;
}
}
#endif
/* 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. The custom
* endpoint fd was needed to upload the policy and can
* now be closed as well. */
if (r >= 0)
if (r >= 0)
if (r < 0) {
*exit_status = EXIT_FDS;
return r;
}
if (params->apply_permissions) {
for (i = 0; i < _RLIMIT_MAX; i++) {
continue;
return -errno;
}
}
if (context->capability_bounding_set_drop) {
if (r < 0) {
return r;
}
}
#ifdef HAVE_SMACK
if (context->smack_process_label) {
if (r < 0) {
return r;
}
}
#ifdef SMACK_DEFAULT_PROCESS_LABEL
else {
if (r < 0 && r != -ENODATA && r != -EOPNOTSUPP) {
return r;
}
if (r < 0) {
return r;
}
}
#endif
#endif
if (r < 0) {
*exit_status = EXIT_USER;
return r;
}
}
/* 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. */
return -errno;
}
if (context->capabilities)
return -errno;
}
if (context->no_new_privileges)
return -errno;
}
#ifdef HAVE_SECCOMP
if (context->address_families_whitelist ||
if (r < 0) {
return r;
}
}
if (context->syscall_whitelist ||
r = apply_seccomp(context);
if (r < 0) {
return r;
}
}
#endif
#ifdef HAVE_SELINUX
if (mac_selinux_use()) {
if (exec_context) {
r = setexeccon(exec_context);
if (r < 0) {
return r;
}
}
}
#endif
#ifdef HAVE_APPARMOR
if (r < 0 && !context->apparmor_profile_ignore) {
return -errno;
}
}
#endif
}
if (r < 0) {
return r;
}
NULL);
if (!final_env) {
return -ENOMEM;
}
if (!final_argv) {
return -ENOMEM;
}
_cleanup_free_ char *line;
if (line) {
log_open();
NULL);
log_close();
}
}
*exit_status = EXIT_EXEC;
return -errno;
}
const ExecContext *context,
const ExecParameters *params,
int socket_fd, r;
char **argv;
return -EINVAL;
}
} else {
socket_fd = -1;
}
if (r < 0)
if (!line)
return log_oom();
NULL);
if (pid < 0)
if (pid == 0) {
int exit_status;
r = exec_child(unit,
argv,
&exit_status);
if (r < 0) {
log_open();
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 (params->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->runtime_directory_mode = 0755;
}
void exec_context_done(ExecContext *c) {
unsigned l;
assert(c);
for (l = 0; l < ELEMENTSOF(c->rlimit); l++)
if (c->capabilities) {
cap_free(c->capabilities);
c->capabilities = NULL;
}
if (c->cpuset)
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->bus_endpoint = 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. */
(void) rm_rf(p, REMOVE_ROOT);
}
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);
}
ExecCommand *i;
while ((i = c)) {
LIST_REMOVE(command, c, i);
free(i);
}
return NULL;
}
void exec_command_free_array(ExecCommand **c, unsigned n) {
unsigned i;
for (i = 0; i < n; i++)
c[i] = exec_command_free_list(c[i]);
}
typedef struct InvalidEnvInfo {
const char *path;
static void invalid_env(const char *p, void *userdata) {
}
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) {
InvalidEnvInfo info = {
};
}
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++)
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 != PERSONALITY_INVALID)
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",
}
bool exec_context_maintains_privileges(ExecContext *c) {
assert(c);
/* Returns true if the process forked off would run run under
* an unchanged UID or as root. */
if (!c->user)
return true;
return true;
return false;
}
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;
}
_cleanup_strv_free_ char **l = NULL;
int r;
assert(c);
if (!l)
return -ENOMEM;
r = strv_extend_strv(&c->argv, l);
if (r < 0)
return r;
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)
return NULL;
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(Unit *u, ExecRuntime **rt, const char *key, const char *value, FDSet *fds) {
int r;
char *copy;
r = exec_runtime_allocate(rt);
if (r < 0)
return log_oom();
if (!copy)
return log_oom();
char *copy;
r = exec_runtime_allocate(rt);
if (r < 0)
return log_oom();
if (!copy)
return log_oom();
int fd;
r = exec_runtime_allocate(rt);
if (r < 0)
return log_oom();
else {
}
int fd;
r = exec_runtime_allocate(rt);
if (r < 0)
return log_oom();
else {
}
} else
return 0;
return 1;
}
static void *remove_tmpdir_thread(void *p) {
_cleanup_free_ char *path = p;
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"
};
static const char* const exec_utmp_mode_table[_EXEC_UTMP_MODE_MAX] = {
[EXEC_UTMP_INIT] = "init",
[EXEC_UTMP_LOGIN] = "login",
[EXEC_UTMP_USER] = "user",
};