HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/nspawn/nspawn.c
Git blob: 1c687382baea8193c023ec86840c44042195bef9
Lines 5001–6000 of 6,878
if ((arg_settings_mask & SETTING_NOTIFY_READY) == 0 &&
settings->notify_ready >= 0)
arg_notify_ready = settings->notify_ready;
if ((arg_settings_mask & SETTING_SYSCALL_FILTER) == 0) {
if (!strv_isempty(settings->syscall_allow_list) || !strv_isempty(settings->syscall_deny_list)) {
if (!arg_settings_trusted && !strv_isempty(settings->syscall_allow_list))
log_warning("Ignoring SystemCallFilter= settings, file %s is not trusted.", path);
else {
strv_free_and_replace(arg_syscall_allow_list, settings->syscall_allow_list);
strv_free_and_replace(arg_syscall_deny_list, settings->syscall_deny_list);
}
}
#if HAVE_SECCOMP
if (settings->seccomp) {
if (!arg_settings_trusted)
log_warning("Ignoring SECCOMP filter, file %s is not trusted.", path);
else {
sym_seccomp_release(arg_seccomp);
arg_seccomp = TAKE_PTR(settings->seccomp);
}
}
#endif
}
for (rl = 0; rl < _RLIMIT_MAX; rl++) {
if ((arg_settings_mask & (SETTING_RLIMIT_FIRST << rl)))
continue;
if (!settings->rlimit[rl])
continue;
if (!arg_settings_trusted) {
log_warning("Ignoring Limit%s= setting, file '%s' is not trusted.", rlimit_to_string(rl), path);
continue;
}
free_and_replace(arg_rlimit[rl], settings->rlimit[rl]);
}
if ((arg_settings_mask & SETTING_HOSTNAME) == 0 &&
settings->hostname)
free_and_replace(arg_hostname, settings->hostname);
if ((arg_settings_mask & SETTING_NO_NEW_PRIVILEGES) == 0 &&
settings->no_new_privileges >= 0)
arg_no_new_privileges = settings->no_new_privileges;
if ((arg_settings_mask & SETTING_OOM_SCORE_ADJUST) == 0 &&
settings->oom_score_adjust_set) {
if (!arg_settings_trusted)
log_warning("Ignoring OOMScoreAdjust= setting, file '%s' is not trusted.", path);
else {
arg_oom_score_adjust = settings->oom_score_adjust;
arg_oom_score_adjust_set = true;
}
}
if ((arg_settings_mask & SETTING_CPU_AFFINITY) == 0 &&
settings->cpu_set.set) {
if (!arg_settings_trusted)
log_warning("Ignoring CPUAffinity= setting, file '%s' is not trusted.", path);
else
cpu_set_done_and_replace(arg_cpu_set, settings->cpu_set);
}
if ((arg_settings_mask & SETTING_RESOLV_CONF) == 0 &&
settings->resolv_conf != _RESOLV_CONF_MODE_INVALID)
arg_resolv_conf = settings->resolv_conf;
if ((arg_settings_mask & SETTING_LINK_JOURNAL) == 0 &&
settings->link_journal != _LINK_JOURNAL_INVALID) {
if (!arg_settings_trusted)
log_warning("Ignoring journal link setting, file '%s' is not trusted.", path);
else {
arg_link_journal = settings->link_journal;
arg_link_journal_try = settings->link_journal_try;
}
}
if ((arg_settings_mask & SETTING_TIMEZONE) == 0 &&
settings->timezone != _TIMEZONE_MODE_INVALID)
arg_timezone = settings->timezone;
if ((arg_settings_mask & SETTING_SLICE) == 0 &&
settings->slice) {
if (!arg_settings_trusted)
log_warning("Ignoring slice setting, file '%s' is not trusted.", path);
else
free_and_replace(arg_slice, settings->slice);
}
if ((arg_settings_mask & SETTING_USE_CGNS) == 0 &&
settings->use_cgns >= 0) {
if (!arg_settings_trusted)
log_warning("Ignoring cgroup namespace setting, file '%s' is not trusted.", path);
else
arg_use_cgns = settings->use_cgns;
}
if ((arg_settings_mask & SETTING_CLONE_NS_FLAGS) == 0 &&
settings->clone_ns_flags != ULONG_MAX) {
if (!arg_settings_trusted)
log_warning("Ignoring namespace setting, file '%s' is not trusted.", path);
else
arg_clone_ns_flags = settings->clone_ns_flags;
}
if ((arg_settings_mask & SETTING_CONSOLE_MODE) == 0 &&
settings->console_mode >= 0) {
if (!arg_settings_trusted)
log_warning("Ignoring console mode setting, file '%s' is not trusted.", path);
else
arg_console_mode = settings->console_mode;
}
if ((arg_settings_mask & SETTING_SUPPRESS_SYNC) == 0 &&
settings->suppress_sync >= 0)
arg_suppress_sync = settings->suppress_sync;
if (!FLAGS_SET(arg_settings_mask, SETTING_RESTRICT_ADDRESS_FAMILIES) &&
(settings->restrict_address_families || settings->restrict_address_families_is_allowlist)) {
set_free_and_replace(arg_restrict_address_families, settings->restrict_address_families);
arg_restrict_address_families_is_allowlist = settings->restrict_address_families_is_allowlist;
}
/* The following properties can only be set through the OCI settings logic, not from the command line, hence we
* don't consult arg_settings_mask for them. */
sd_bus_message_unref(arg_property_message);
arg_property_message = TAKE_PTR(settings->properties);
arg_console_width = settings->console_width;
arg_console_height = settings->console_height;
device_node_array_free(arg_extra_nodes, arg_n_extra_nodes);
arg_extra_nodes = TAKE_PTR(settings->extra_nodes);
arg_n_extra_nodes = settings->n_extra_nodes;
settings->n_extra_nodes = 0;
return 0;
}
static int load_settings(void) {
_cleanup_(settings_freep) Settings *settings = NULL;
_cleanup_fclose_ FILE *f = NULL;
_cleanup_free_ char *p = NULL;
int r;
if (arg_oci_bundle)
return 0;
/* If all settings are masked, there's no point in looking for
* the settings file */
if (FLAGS_SET(arg_settings_mask, _SETTINGS_MASK_ALL))
return 0;
/* We first look in the admin's directories in /etc/ and /run/ */
static const uint64_t lookup_dir_system[] = {
SD_PATH_SYSTEM_CONFIGURATION,
SD_PATH_SYSTEM_RUNTIME,
_SD_PATH_INVALID,
};
static const uint64_t lookup_dir_user[] = {
SD_PATH_USER_CONFIGURATION,
SD_PATH_USER_RUNTIME,
_SD_PATH_INVALID,
};
const uint64_t *q = arg_runtime_scope == RUNTIME_SCOPE_SYSTEM ? lookup_dir_system : lookup_dir_user;
for (; *q != _SD_PATH_INVALID; q++) {
_cleanup_free_ char *cd = NULL;
r = sd_path_lookup(*q, "systemd/nspawn", &cd);
if (r < 0) {
log_warning_errno(r, "Failed to determine settings directory, ignoring: %m");
continue;
}
_cleanup_free_ char *j = NULL;
j = path_join(cd, arg_settings_filename);
if (!j)
return log_oom();
f = fopen(j, "re");
if (f) {
p = TAKE_PTR(j);
log_debug("Found settings file: %s", p);
/* By default, we trust configuration from /etc and /run */
if (arg_settings_trusted < 0)
arg_settings_trusted = true;
break;
}
if (errno != ENOENT)
return log_error_errno(errno, "Failed to open %s: %m", j);
}
if (!f) {
/* After that, let's look for a file next to the
* actual image we shall boot. */
if (arg_image) {
r = file_in_same_dir(arg_image, arg_settings_filename, &p);
if (r < 0)
return log_error_errno(r, "Failed to generate settings path from image path: %m");
} else if (arg_directory) {
r = file_in_same_dir(arg_directory, arg_settings_filename, &p);
if (r < 0 && r != -EADDRNOTAVAIL) /* if directory is root fs, don't complain */
return log_error_errno(r, "Failed to generate settings path from directory path: %m");
} else if (arg_mstack) {
r = file_in_same_dir(arg_mstack, arg_settings_filename, &p);
if (r < 0)
return log_error_errno(r, "Failed to generate settings path from mstack path: %m");
}
if (p) {
f = fopen(p, "re");
if (!f && errno != ENOENT)
return log_error_errno(errno, "Failed to open %s: %m", p);
if (f)
log_debug("Found settings file: %s", p);
/* By default, we do not trust configuration from /var/lib/machines */
if (arg_settings_trusted < 0)
arg_settings_trusted = false;
}
}
if (!f)
return 0;
log_debug("Settings are trusted: %s", yes_no(arg_settings_trusted));
r = settings_load(f, p, &settings);
if (r < 0)
return r;
return merge_settings(settings, p);
}
static int load_oci_bundle(void) {
_cleanup_(settings_freep) Settings *settings = NULL;
int r;
if (!arg_oci_bundle)
return 0;
/* By default let's trust OCI bundles */
if (arg_settings_trusted < 0)
arg_settings_trusted = true;
r = oci_load(NULL, arg_oci_bundle, &settings);
if (r < 0)
return r;
return merge_settings(settings, arg_oci_bundle);
}
static int run_container(
const char *runtime_dir,
const char *directory,
int mount_fd,
DissectedImage *dissected_image,
MStack *mstack,
int userns_fd,
FDSet *fds,
char **fdnames,
char veth_name[IFNAMSIZ],
bool *veth_created,
struct ExposeArgs *expose_args,
int *master,
PidRef *pid,
int *ret) {
_cleanup_(release_lock_file) LockFile uid_shift_lock = LOCK_FILE_INIT;
_cleanup_close_ int etc_passwd_lock = -EBADF;
_cleanup_close_pair_ int
fd_inner_socket_pair[2] = EBADF_PAIR,
fd_outer_socket_pair[2] = EBADF_PAIR;
_cleanup_close_ int notify_socket = -EBADF, mntns_fd = -EBADF, fd_kmsg_fifo = -EBADF;
_cleanup_(barrier_destroy) Barrier barrier = BARRIER_NULL;
_cleanup_(sd_event_source_unrefp) sd_event_source *notify_event_source = NULL;
_cleanup_(umount_and_rmdir_and_freep) char *unix_export_host_dir = NULL;
_cleanup_(sd_event_unrefp) sd_event *event = NULL;
_cleanup_(pty_forward_freep) PTYForward *forward = NULL;
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
_cleanup_free_ uid_t *bind_user_uid = NULL;
size_t n_bind_user_uid = 0;
ContainerStatus container_status = 0;
int ifi = 0, r;
ssize_t l;
sigset_t mask_chld;
_cleanup_close_ int child_netns_fd = -EBADF;
assert_se(sigemptyset(&mask_chld) == 0);
assert_se(sigaddset(&mask_chld, SIGCHLD) == 0);
assert(runtime_dir);
assert(directory);
assert(veth_name);
assert(veth_created);
assert(expose_args);
assert(master);
assert(pid);
assert(ret);
/* Set up the unix export host directory on the host first */
r = setup_unix_export_dir_outside(runtime_dir, &unix_export_host_dir);
if (r < 0)
return r;
if (arg_userns_mode == USER_NAMESPACE_PICK) {
/* When we shall pick the UID/GID range, let's first lock /etc/passwd, so that we can safely
* check with getpwuid() if the specific user already exists. Note that /etc might be
* read-only, in which case this will fail with EROFS. But that's really OK, as in that case we
* can be reasonably sure that no users are going to be added. Note that getpwuid() checks are
* really just an extra safety net. We kinda assume that the UID range we allocate from is
* really ours. */
etc_passwd_lock = take_etc_passwd_lock(NULL);
if (etc_passwd_lock < 0 && etc_passwd_lock != -EROFS)
return log_error_errno(etc_passwd_lock, "Failed to take /etc/passwd lock: %m");
}
r = barrier_create(&barrier);
if (r < 0)
return log_error_errno(r, "Cannot initialize IPC barrier: %m");
if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, fd_inner_socket_pair) < 0)
return log_error_errno(errno, "Failed to create inner socket pair: %m");
if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, fd_outer_socket_pair) < 0)
return log_error_errno(errno, "Failed to create outer socket pair: %m");
/* Child can be killed before execv(), so handle SIGCHLD in order to interrupt
* parent's blocking calls and give it a chance to call wait() and terminate. */
r = sigprocmask(SIG_UNBLOCK, &mask_chld, NULL);
if (r < 0)
return log_error_errno(errno, "Failed to change the signal mask: %m");
r = sigaction(SIGCHLD, &sigaction_nop_nocldstop, NULL);
if (r < 0)
return log_error_errno(errno, "Failed to install SIGCHLD handler: %m");
if (arg_network_namespace_path) {
child_netns_fd = open(arg_network_namespace_path, O_RDONLY|O_NOCTTY|O_CLOEXEC);
if (child_netns_fd < 0)
return log_error_errno(errno, "Cannot open file %s: %m", arg_network_namespace_path);
r = fd_is_namespace(child_netns_fd, NAMESPACE_NET);
if (r < 0)
return log_error_errno(r, "Failed to check %s fs type: %m", arg_network_namespace_path);
if (r == 0)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Path %s doesn't refer to a network namespace, refusing.", arg_network_namespace_path);
}
bool in_child;
if (arg_userns_mode != USER_NAMESPACE_MANAGED) {
assert(userns_fd < 0);
/* If we have no user namespace then we'll clone and create a new mount namespace right-away. */
pid_t _pid = raw_clone(SIGCHLD|CLONE_NEWNS);
if (_pid < 0)
return log_error_errno(errno, "clone() failed%s: %m",
errno == EINVAL ?
", do you have namespace support enabled in your kernel? (You need UTS, IPC, PID and NET namespacing built in)" : "");
if (_pid != 0) {
r = pidref_set_pid(pid, _pid);
if (r < 0)
return log_error_errno(r, "Failed to allocate pidfd: %m");
}
in_child = _pid == 0;
} else {
assert(userns_fd >= 0);
/* If we have a user namespace then we'll clone() first, and then join the user namespace,
* and then open the mount namespace, so that it is owned by the user namespace */
pid_t _pid = raw_clone(SIGCHLD);
if (_pid < 0)
return log_error_errno(errno, "clone() failed: %m");
if (_pid != 0) {
r = pidref_set_pid(pid, _pid);
if (r < 0)
return log_error_errno(r, "Failed to allocate pidfd: %m");
}
in_child = _pid == 0;
if (in_child) {
if (setns(userns_fd, CLONE_NEWUSER) < 0) {
log_error_errno(errno, "Failed to join allocate user namespace: %m");
_exit(EXIT_FAILURE);
}
r = reset_uid_gid();
if (r < 0) {
log_error_errno(r, "Failed to reset UID/GID to root: %m");
_exit(EXIT_FAILURE);
}
if (unshare(CLONE_NEWNS) < 0) {
log_error_errno(errno, "Failed to unshare file system namespace: %m");
_exit(EXIT_FAILURE);
}
}
}
if (in_child) {
/* The outer child only has a file system namespace. */
barrier_set_role(&barrier, BARRIER_CHILD);
fd_inner_socket_pair[0] = safe_close(fd_inner_socket_pair[0]);
fd_outer_socket_pair[0] = safe_close(fd_outer_socket_pair[0]);
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
r = outer_child(&barrier,
directory,
mount_fd,
dissected_image,
mstack,
fd_outer_socket_pair[1],
fd_inner_socket_pair[1],
fds,
fdnames,
child_netns_fd,
unix_export_host_dir);
if (r < 0)
_exit(EXIT_FAILURE);
_exit(EXIT_SUCCESS);
}
barrier_set_role(&barrier, BARRIER_PARENT);
fdset_close(fds, /* async= */ false);
fd_inner_socket_pair[1] = safe_close(fd_inner_socket_pair[1]);
fd_outer_socket_pair[1] = safe_close(fd_outer_socket_pair[1]);
if (arg_userns_mode != USER_NAMESPACE_NO) {
mntns_fd = receive_one_fd(fd_outer_socket_pair[0], 0);
if (mntns_fd < 0)
return log_error_errno(mntns_fd, "Failed to receive mount namespace fd from outer child: %m");
/* The child just let us know the UID shift it might have read from the image. */
l = recv(fd_outer_socket_pair[0], &arg_uid_shift, sizeof arg_uid_shift, 0);
if (l < 0)
return log_error_errno(errno, "Failed to read UID shift: %m");
if (l != sizeof arg_uid_shift)
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short read while reading UID shift.");
if (arg_userns_mode == USER_NAMESPACE_PICK) {
/* If we are supposed to pick the UID shift, let's try to use the shift read from the
* image, but if that's already in use, pick a new one, and report back to the child,
* which one we now picked. */
r = uid_shift_pick(&arg_uid_shift, &uid_shift_lock);
if (r < 0)
return log_error_errno(r, "Failed to pick suitable UID/GID range: %m");
l = send(fd_outer_socket_pair[0], &arg_uid_shift, sizeof arg_uid_shift, MSG_NOSIGNAL);
if (l < 0)
return log_error_errno(errno, "Failed to send UID shift: %m");
if (l != sizeof arg_uid_shift)
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short write while writing UID shift.");
}
}
/* Wait for the outer child. */
r = pidref_wait_for_terminate_and_check("(sd-namespace)", pid, WAIT_LOG_ABNORMAL);
if (r < 0)
return r;
pidref_done(pid);
if (r != EXIT_SUCCESS)
return -EIO;
/* And now retrieve the PID of the inner child. */
pid_t _pid;
l = recv(fd_outer_socket_pair[0], &_pid, sizeof _pid, 0);
if (l < 0)
return log_error_errno(errno, "Failed to read inner child PID: %m");
if (l != sizeof _pid)
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short read while reading inner child PID.");
r = pidref_set_pid(pid, _pid);
if (r < 0)
return log_error_errno(r, "Failed to allocate pidfd: %m");
/* We also retrieve container UUID in case it was generated by outer child */
l = recv(fd_outer_socket_pair[0], &arg_uuid, sizeof arg_uuid, 0);
if (l < 0)
return log_error_errno(errno, "Failed to read container machine ID: %m");
if (l != sizeof(arg_uuid))
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short read while reading container machined ID.");
/* We also retrieve the socket used for notifications generated by outer child */
notify_socket = receive_one_fd(fd_outer_socket_pair[0], 0);
if (notify_socket < 0)
return log_error_errno(notify_socket,
"Failed to receive notification socket from the outer child: %m");
log_debug("Init process invoked as PID "PID_FMT, pid->pid);
if (arg_userns_mode != USER_NAMESPACE_NO) {
if (!barrier_place_and_sync(&barrier)) /* #1 */
return log_error_errno(SYNTHETIC_ERRNO(ESRCH), "Child died too early.");
if (arg_userns_mode != USER_NAMESPACE_MANAGED) {
r = setup_uid_map(pid, bind_user_uid, n_bind_user_uid);
if (r < 0)
return r;
}
(void) barrier_place(&barrier); /* #2 */
}
if (arg_private_network) {
if (!arg_network_namespace_path) {
/* Wait until the child has unshared its network namespace. */
if (!barrier_place_and_sync(&barrier)) /* #3 */
return log_error_errno(SYNTHETIC_ERRNO(ESRCH), "Child died too early");
/* Make sure we have an open file descriptor to the child's network namespace so it
* stays alive even if the child exits. */
assert(child_netns_fd < 0);
child_netns_fd = receive_one_fd(fd_inner_socket_pair[0], 0);
if (child_netns_fd < 0)
return log_error_errno(child_netns_fd, "Failed to receive child network namespace: %m");
}
r = move_network_interfaces(child_netns_fd, arg_network_interfaces);
if (r < 0)
return r;
if (arg_network_veth) {
if (arg_userns_mode != USER_NAMESPACE_MANAGED) {
r = setup_veth(arg_machine, pid, veth_name,
arg_network_bridge || arg_network_zone, &arg_network_provided_mac);
if (r < 0)
return r;
else if (r > 0)
ifi = r;
} else {
_cleanup_free_ char *host_ifname = NULL;
r = nsresource_add_netif_veth(
/* vl= */ NULL,
userns_fd,
child_netns_fd,
/* namespace_ifname= */ NULL,
&host_ifname,
/* ret_namespace_ifname= */ NULL);
if (r < 0)
return log_error_errno(r, "Failed to add network interface to container: %m");
ifi = if_nametoindex(host_ifname);
if (ifi == 0)
return log_error_errno(errno, "Failed to resolve interface '%s': %m", host_ifname);
if (strlen(host_ifname) >= IFNAMSIZ)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Host interface name too long?");
strcpy(veth_name, host_ifname);
}
if (arg_network_bridge) {
/* Add the interface to a bridge */
r = setup_bridge(veth_name, arg_network_bridge, false);
if (r < 0)
return r;
if (r > 0)
ifi = r;
} else if (arg_network_zone) {
/* Add the interface to a bridge, possibly creating it */
r = setup_bridge(veth_name, arg_network_zone, true);
if (r < 0)
return r;
if (r > 0)
ifi = r;
}
}
r = setup_veth_extra(arg_machine, pid, arg_network_veth_extra);
if (r < 0)
return r;
/* We created the primary and extra veth links now; let's remember this, so that we know to
remove them later on. Note that we don't bother with removing veth links that were created
here when their setup failed half-way, because in that case the kernel should be able to
remove them on its own, since they cannot be referenced by anything yet. */
*veth_created = true;
r = setup_macvlan(arg_machine, pid, arg_network_macvlan);
if (r < 0)
return r;
r = setup_ipvlan(arg_machine, pid, arg_network_ipvlan);
if (r < 0)
return r;
}
/* Registration always happens on the system bus */
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *system_bus = NULL;
if (arg_register != 0 || (arg_runtime_scope == RUNTIME_SCOPE_SYSTEM && !arg_keep_unit)) {
r = sd_bus_default_system(&system_bus);
if (r < 0)
return log_error_errno(r, "Failed to open system bus: %m");
r = sd_bus_set_close_on_exit(system_bus, false);
if (r < 0)
return log_error_errno(r, "Failed to disable close-on-exit behaviour: %m");
(void) sd_bus_set_allow_interactive_authorization(system_bus, arg_ask_password);
}
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *user_bus = NULL;
_cleanup_(sd_bus_unrefp) sd_bus *runtime_bus = NULL;
if (arg_register != 0 || !arg_keep_unit) {
if (arg_runtime_scope == RUNTIME_SCOPE_SYSTEM)
runtime_bus = sd_bus_ref(system_bus);
else {
r = sd_bus_default_user(&user_bus);
if (r < 0)
return log_error_errno(r, "Failed to open user bus: %m");
r = sd_bus_set_close_on_exit(user_bus, false);
if (r < 0)
return log_error_errno(r, "Failed to disable close-on-exit behaviour: %m");
runtime_bus = sd_bus_ref(user_bus);
}
}
/* Scope allocation happens on the user bus if we are unpriv, otherwise system bus. */
if (arg_keep_unit) {
/* If we are not supposed to allocate a unit, then let's move the process now, so that we can
* register things while being in the right cgroup location already. Otherwise, let's move
* the process later, once we have unit and hence cgroup. */
r = create_subcgroup(
pid,
arg_keep_unit,
arg_uid_shift,
userns_fd,
arg_userns_mode);
if (r < 0)
return r;
} else {
/* When a new scope is created for this container, then we'll be registered as its controller, in which
* case PID 1 will send us a friendly RequestStop signal, when it is asked to terminate the
* scope. Let's hook into that, and cleanly shut down the container, and print a friendly message. */
r = sd_bus_match_signal_async(
runtime_bus,
/* ret= */ NULL,
"org.freedesktop.systemd1",
/* path= */ NULL,
"org.freedesktop.systemd1.Scope",
"RequestStop",
on_request_stop,
/* install_callback= */ NULL,
pid);
if (r < 0)
return log_error_errno(r, "Failed to request RequestStop match: %m");
}
bool scope_allocated = false;
if (!arg_keep_unit) {
AllocateScopeFlags flags = ALLOCATE_SCOPE_ALLOW_PIDFD;
r = allocate_scope(
runtime_bus,
arg_machine,
pid,
arg_slice,
arg_custom_mounts, arg_n_custom_mounts,
arg_kill_signal,
arg_property,
arg_property_message,
arg_start_mode,
flags);
if (r < 0)
return r;
scope_allocated = true;
}
MachineRegistrationContext machine_ctx = {
.scope = arg_runtime_scope == RUNTIME_SCOPE_SYSTEM ? RUNTIME_SCOPE_SYSTEM : _RUNTIME_SCOPE_INVALID,
.system_bus = system_bus,
.user_bus = runtime_bus,
};
if (arg_register != 0) {
const MachineRegistration reg = {
.name = arg_machine,
.id = arg_uuid,
.service = arg_container_service_name,
.class = "container",
.pidref = pid,
.root_directory = arg_directory,
.local_ifindex = ifi,
};
r = register_machine_with_fallback_and_log(
&machine_ctx,
®,
/* graceful= */ arg_register < 0);
if (r < 0)
return r;
}
if (arg_keep_unit && (arg_slice || arg_property))
log_notice("Machine and scope registration turned off, --slice= and --property= settings will have no effect.");
if (!arg_keep_unit) {
r = create_subcgroup(
pid,
arg_keep_unit,
arg_uid_shift,
userns_fd,
arg_userns_mode);
if (r < 0)
return r;
}
/* Notify the child that the parent is ready with all its setup (including cgroup-ification), and
* that the child can now hand over control to the code to run inside the container. */
(void) barrier_place(&barrier); /* #4 */
/* Block SIGCHLD here, before notifying child.
* process_pty() will handle it with the other signals. */
assert_se(sigprocmask(SIG_BLOCK, &mask_chld, NULL) >= 0);
/* Reset signal to default */
r = default_signals(SIGCHLD);
if (r < 0)
return log_error_errno(r, "Failed to reset SIGCHLD: %m");
r = sd_event_new(&event);
if (r < 0)
return log_error_errno(r, "Failed to get default event source: %m");
(void) sd_event_set_watchdog(event, true);
if (system_bus) {
r = sd_bus_attach_event(system_bus, event, 0);
if (r < 0)
return log_error_errno(r, "Failed to attach system bus to event loop: %m");
}
if (user_bus) {
r = sd_bus_attach_event(user_bus, event, 0);
if (r < 0)
return log_error_errno(r, "Failed to attach user bus to event loop: %m");
}
r = setup_notify_parent(event, notify_socket, pid, ¬ify_event_source);
if (r < 0)
return r;
/* Wait that the child is completely ready now, and has mounted their own copies of procfs and so on,
* before we take the fully visible instances away. */
if (!barrier_sync(&barrier)) /* #5.1 */
return log_error_errno(SYNTHETIC_ERRNO(ESRCH), "Child died too early.");
if (!IN_SET(arg_userns_mode, USER_NAMESPACE_NO, USER_NAMESPACE_MANAGED)) {
r = wipe_fully_visible_api_fs(mntns_fd);
if (r < 0)
return r;
mntns_fd = safe_close(mntns_fd);
}
/* And now let the child know that we completed removing the procfs instances, and it can start the
* payload. */
if (!barrier_place(&barrier)) /* #5.2 */
return log_error_errno(SYNTHETIC_ERRNO(ESRCH), "Child died too early.");
/* At this point we have made use of the UID we picked, and thus nss-systemd/systemd-machined.service
* will make them appear in getpwuid(), thus we can release the /etc/passwd lock. */
etc_passwd_lock = safe_close(etc_passwd_lock);
(void) sd_notifyf(/* unset_environment= */ false,
"STATUS=Container started.\n"
"X_NSPAWN_LEADER_PID=" PID_FMT, pid->pid);
if (!arg_notify_ready) {
r = sd_notify(/* unset_environment= */ false, "READY=1\n");
if (r < 0)
log_warning_errno(r, "Failed to send readiness notification, ignoring: %m");
}
/* All operations that might need Polkit authorizations (i.e. userns registration, mounts, and
* machine registration are complete now, get rid of the agent again, so that we retain exclusive
* control of the TTY from now on. */
polkit_agent_close();
/* Note: we do not use SD_EVENT_SIGNAL_PROCMASK or sd_event_set_signal_exit(), since we want the
* signals to be block continuously, even if we destroy the event loop and allocate a new one on
* container reboot. */
if (arg_kill_signal > 0) {
/* Try to kill the init system on SIGINT or SIGTERM */
(void) sd_event_add_signal(event, NULL, SIGINT, on_orderly_shutdown, pid);
(void) sd_event_add_signal(event, NULL, SIGTERM, on_orderly_shutdown, pid);
} else {
/* Immediately exit */
(void) sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
(void) sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
}
(void) sd_event_add_signal(event, NULL, SIGRTMIN+18, sigrtmin18_handler, NULL);
r = sd_event_add_memory_pressure(event, NULL, NULL, NULL);
if (r < 0)
log_debug_errno(r, "Failed to allocate memory pressure event source, ignoring: %m");
/* Exit when the child exits */
(void) sd_event_add_signal(event, NULL, SIGCHLD, on_sigchld, pid);
/* Retrieve the kmsg fifo allocated by inner child */
fd_kmsg_fifo = receive_one_fd(fd_inner_socket_pair[0], 0);
if (fd_kmsg_fifo < 0)
return log_error_errno(fd_kmsg_fifo, "Failed to receive kmsg fifo from inner child: %m");
if (arg_expose_ports) {
r = expose_port_watch_rtnl(event, fd_inner_socket_pair[0], on_address_change, expose_args, &rtnl);
if (r < 0)
return r;
(void) expose_port_execute(rtnl, expose_args->nfnl, arg_expose_ports, AF_INET, &expose_args->address4);
(void) expose_port_execute(rtnl, expose_args->nfnl, arg_expose_ports, AF_INET6, &expose_args->address6);
}
_cleanup_(osc_context_closep) sd_id128_t osc_context_id = SD_ID128_NULL;
if (IN_SET(arg_console_mode, CONSOLE_INTERACTIVE, CONSOLE_READ_ONLY) && !terminal_is_dumb()) {
r = osc_context_open_container(arg_machine, /* ret_seq= */ NULL, &osc_context_id);
if (r < 0)
return r;
}
if (arg_console_mode != CONSOLE_PIPE) {
_cleanup_close_ int fd = -EBADF;
PTYForwardFlags flags = 0;
/* Retrieve the master pty allocated by inner child */
fd = receive_one_fd(fd_inner_socket_pair[0], 0);
if (fd < 0)
return log_error_errno(fd, "Failed to receive master pty from the inner child: %m");
switch (arg_console_mode) {
case CONSOLE_READ_ONLY:
flags |= PTY_FORWARD_READ_ONLY;
_fallthrough_;
case CONSOLE_INTERACTIVE:
flags |= PTY_FORWARD_IGNORE_VHANGUP;
r = pty_forward_new(event, fd, flags, &forward);
if (r < 0)
return log_error_errno(r, "Failed to create PTY forwarder: %m");
if (arg_console_width != UINT_MAX || arg_console_height != UINT_MAX)
(void) pty_forward_set_width_height(
forward,
arg_console_width,
arg_console_height);
if (!arg_background) {
_cleanup_free_ char *bg = NULL;
r = terminal_tint_color(220 /* blue */, &bg);
if (r < 0)
log_debug_errno(r, "Failed to determine terminal background color, not tinting.");
else
(void) pty_forward_set_background_color(forward, bg);
} else if (!isempty(arg_background))
(void) pty_forward_set_background_color(forward, arg_background);
(void) pty_forward_set_window_title(forward, GLYPH_BLUE_CIRCLE, /* hostname= */ NULL,
STRV_MAKE("Container", arg_machine));
pty_forward_set_hotkey_handler(forward, ptyfwd_hotkey, pid);
break;
default:
assert(arg_console_mode == CONSOLE_PASSIVE);
}
*master = TAKE_FD(fd);
}
fd_inner_socket_pair[0] = safe_close(fd_inner_socket_pair[0]);
r = sd_event_loop(event);
if (r < 0)
return log_error_errno(r, "Failed to run event loop: %m");
/* Kill if it is not dead yet anyway */
if (scope_allocated)
terminate_scope(runtime_bus, arg_machine);
/* Normally redundant, but better safe than sorry */
(void) pidref_kill(pid, SIGKILL);
fd_kmsg_fifo = safe_close(fd_kmsg_fifo);
if (arg_private_network && arg_userns_mode != USER_NAMESPACE_MANAGED) {
r = move_back_network_interfaces(child_netns_fd, arg_network_interfaces);
if (r < 0)
return r;
r = remove_macvlan(child_netns_fd, arg_network_macvlan);
if (r < 0)
return r;
}
r = wait_for_container(pid, &container_status);
/* Tell machined that we are gone. */
unregister_machine_with_fallback_and_log(&machine_ctx, arg_machine);
if (r < 0)
/* We failed to wait for the container, or the container exited abnormally. */
return r;
if (r > 0 || container_status == CONTAINER_TERMINATED) {
/* r > 0 → The container exited with a non-zero status.
* As a special case, we need to replace 133 with a different value,
* because 133 is special-cased in the service file to reboot the container.
* otherwise → The container exited with zero status and a reboot was not requested.
*/
if (r == EXIT_FORCE_RESTART)
r = EXIT_FAILURE; /* replace 133 with the general failure code */
*ret = r;
return 0; /* finito */
}
/* CONTAINER_REBOOTED, loop again */
if (arg_keep_unit) {
/* Special handling if we are running as a service: instead of simply
* restarting the machine we want to restart the entire service, so let's
* inform systemd about this with the special exit code 133. The service
* file uses RestartForceExitStatus=133 so that this results in a full
* nspawn restart. This is necessary since we might have cgroup parameters
* set we want to have flushed out. */
*ret = EXIT_FORCE_RESTART;
return 0; /* finito */
}
expose_port_flush(expose_args->nfnl, arg_expose_ports, AF_INET, &expose_args->address4);
expose_port_flush(expose_args->nfnl, arg_expose_ports, AF_INET6, &expose_args->address6);
(void) remove_veth_links(veth_name, arg_network_veth_extra);
*veth_created = false;
return 1; /* loop again */
}
static int initialize_rlimits(void) {
/* The default resource limits the kernel passes to PID 1, as per kernel 5.16. Let's pass our container payload
* the same values as the kernel originally passed to PID 1, in order to minimize differences between host and
* container execution environments. */
static const struct rlimit kernel_defaults[_RLIMIT_MAX] = {
[RLIMIT_AS] = { RLIM_INFINITY, RLIM_INFINITY },
[RLIMIT_CORE] = { 0, RLIM_INFINITY },
[RLIMIT_CPU] = { RLIM_INFINITY, RLIM_INFINITY },
[RLIMIT_DATA] = { RLIM_INFINITY, RLIM_INFINITY },
[RLIMIT_FSIZE] = { RLIM_INFINITY, RLIM_INFINITY },
[RLIMIT_LOCKS] = { RLIM_INFINITY, RLIM_INFINITY },
[RLIMIT_MEMLOCK] = { DEFAULT_RLIMIT_MEMLOCK, DEFAULT_RLIMIT_MEMLOCK },
[RLIMIT_MSGQUEUE] = { 819200, 819200 },
[RLIMIT_NICE] = { 0, 0 },
[RLIMIT_NOFILE] = { 1024, 4096 },
[RLIMIT_RSS] = { RLIM_INFINITY, RLIM_INFINITY },
[RLIMIT_RTPRIO] = { 0, 0 },
[RLIMIT_RTTIME] = { RLIM_INFINITY, RLIM_INFINITY },
[RLIMIT_STACK] = { 8388608, RLIM_INFINITY },
/* The kernel scales the default for RLIMIT_NPROC and RLIMIT_SIGPENDING based on the system's amount of
* RAM. To provide best compatibility we'll read these limits off PID 1 instead of hardcoding them
* here. This is safe as we know that PID 1 doesn't change these two limits and thus the original