]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/nspawn/nspawn.c
core: use local variable m instead of u->manager
[thirdparty/systemd.git] / src / nspawn / nspawn.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
88213476 2
349cc4a5 3#if HAVE_BLKID
6b5cf3ea 4#include <blkid.h>
8fe0087e 5#endif
88213476 6#include <errno.h>
88213476 7#include <getopt.h>
0e7ac751 8#include <grp.h>
1b9e5b12 9#include <linux/loop.h>
0e7ac751 10#include <pwd.h>
8fe0087e 11#include <sched.h>
349cc4a5 12#if HAVE_SELINUX
8fe0087e 13#include <selinux/selinux.h>
1b9e5b12 14#endif
8fe0087e
LP
15#include <signal.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <sys/file.h>
20#include <sys/mount.h>
21#include <sys/personality.h>
22#include <sys/prctl.h>
23#include <sys/types.h>
6916b164 24#include <sys/wait.h>
8fe0087e 25#include <unistd.h>
1b9e5b12 26
b053cd5f 27#include "sd-bus.h"
1f0cd86b 28#include "sd-daemon.h"
1f0cd86b 29#include "sd-id128.h"
8fe0087e 30
b5efdb8a 31#include "alloc-util.h"
8fe0087e
LP
32#include "barrier.h"
33#include "base-filesystem.h"
34#include "blkid-util.h"
35#include "btrfs-util.h"
b8ea7a6e 36#include "bus-error.h"
b053cd5f 37#include "bus-util.h"
8fe0087e 38#include "cap-list.h"
430f0182 39#include "capability-util.h"
04d391da 40#include "cgroup-util.h"
8fe0087e 41#include "copy.h"
d107bb7d 42#include "cpu-set-util.h"
4fc9982c 43#include "dev-setup.h"
2d845785 44#include "dissect-image.h"
8fe0087e 45#include "env-util.h"
3ffd4af2 46#include "fd-util.h"
842f3b0f 47#include "fdset.h"
a5c32cff 48#include "fileio.h"
f97b34a6 49#include "format-util.h"
f4f15635 50#include "fs-util.h"
1b9e5b12 51#include "gpt.h"
4623e8e6 52#include "hexdecoct.h"
8fe0087e 53#include "hostname-util.h"
910fd145 54#include "id128-util.h"
8fe0087e 55#include "log.h"
2d845785 56#include "loop-util.h"
8fe0087e 57#include "loopback-setup.h"
1b9cebf6 58#include "machine-image.h"
8fe0087e
LP
59#include "macro.h"
60#include "missing.h"
61#include "mkdir.h"
4349cd7c 62#include "mount-util.h"
8fe0087e 63#include "netlink-util.h"
07630cea 64#include "nspawn-cgroup.h"
3603efde 65#include "nspawn-def.h"
07630cea
LP
66#include "nspawn-expose-ports.h"
67#include "nspawn-mount.h"
68#include "nspawn-network.h"
7336138e 69#include "nspawn-patch-uid.h"
07630cea 70#include "nspawn-register.h"
910fd145 71#include "nspawn-seccomp.h"
07630cea
LP
72#include "nspawn-settings.h"
73#include "nspawn-setuid.h"
7732f92b 74#include "nspawn-stub-pid1.h"
d58ad743 75#include "os-util.h"
50ebcf6c 76#include "pager.h"
6bedfcbb 77#include "parse-util.h"
8fe0087e 78#include "path-util.h"
0b452006 79#include "process-util.h"
8fe0087e
LP
80#include "ptyfwd.h"
81#include "random-util.h"
8869a0b4 82#include "raw-clone.h"
bf428efb 83#include "rlimit-util.h"
8fe0087e 84#include "rm-rf.h"
68b02049 85#include "selinux-util.h"
8fe0087e 86#include "signal-util.h"
2583fbea 87#include "socket-util.h"
8fcde012 88#include "stat-util.h"
15a5e950 89#include "stdio-util.h"
5c828e66 90#include "string-table.h"
07630cea 91#include "string-util.h"
8fe0087e
LP
92#include "strv.h"
93#include "terminal-util.h"
affb60b1 94#include "umask-util.h"
b1d4f8e1 95#include "user-util.h"
8fe0087e 96#include "util.h"
e9642be2 97
62b1e758
YW
98#if HAVE_SPLIT_USR
99#define STATIC_RESOLV_CONF "/lib/systemd/resolv.conf"
100#else
101#define STATIC_RESOLV_CONF "/usr/lib/systemd/resolv.conf"
102#endif
103
9c1e04d0
AP
104/* nspawn is listening on the socket at the path in the constant nspawn_notify_socket_path
105 * nspawn_notify_socket_path is relative to the container
106 * the init process in the container pid can send messages to nspawn following the sd_notify(3) protocol */
107#define NSPAWN_NOTIFY_SOCKET_PATH "/run/systemd/nspawn/notify"
0e7ac751 108
2a49b612
ZJS
109#define EXIT_FORCE_RESTART 133
110
113cea80
DH
111typedef enum ContainerStatus {
112 CONTAINER_TERMINATED,
113 CONTAINER_REBOOTED
114} ContainerStatus;
115
88213476 116static char *arg_directory = NULL;
ec16945e 117static char *arg_template = NULL;
5f932eb9 118static char *arg_chdir = NULL;
b53ede69
PW
119static char *arg_pivot_root_new = NULL;
120static char *arg_pivot_root_old = NULL;
687d0825 121static char *arg_user = NULL;
9444b1f2 122static sd_id128_t arg_uuid = {};
3a9530e5
LP
123static char *arg_machine = NULL; /* The name used by the host to refer to this */
124static char *arg_hostname = NULL; /* The name the payload sees by default */
c74e630d
LP
125static const char *arg_selinux_context = NULL;
126static const char *arg_selinux_apifs_context = NULL;
9444b1f2 127static const char *arg_slice = NULL;
ff01d048 128static bool arg_private_network = false;
bc2f673e 129static bool arg_read_only = false;
7732f92b 130static StartMode arg_start_mode = START_PID1;
ec16945e 131static bool arg_ephemeral = false;
57fb9fb5 132static LinkJournal arg_link_journal = LINK_AUTO;
574edc90 133static bool arg_link_journal_try = false;
520e0d54 134static uint64_t arg_caps_retain =
50b52222
LP
135 (1ULL << CAP_AUDIT_CONTROL) |
136 (1ULL << CAP_AUDIT_WRITE) |
5076f0cc
LP
137 (1ULL << CAP_CHOWN) |
138 (1ULL << CAP_DAC_OVERRIDE) |
139 (1ULL << CAP_DAC_READ_SEARCH) |
140 (1ULL << CAP_FOWNER) |
141 (1ULL << CAP_FSETID) |
142 (1ULL << CAP_IPC_OWNER) |
143 (1ULL << CAP_KILL) |
144 (1ULL << CAP_LEASE) |
145 (1ULL << CAP_LINUX_IMMUTABLE) |
50b52222 146 (1ULL << CAP_MKNOD) |
5076f0cc
LP
147 (1ULL << CAP_NET_BIND_SERVICE) |
148 (1ULL << CAP_NET_BROADCAST) |
149 (1ULL << CAP_NET_RAW) |
5076f0cc 150 (1ULL << CAP_SETFCAP) |
50b52222 151 (1ULL << CAP_SETGID) |
5076f0cc
LP
152 (1ULL << CAP_SETPCAP) |
153 (1ULL << CAP_SETUID) |
154 (1ULL << CAP_SYS_ADMIN) |
50b52222 155 (1ULL << CAP_SYS_BOOT) |
5076f0cc
LP
156 (1ULL << CAP_SYS_CHROOT) |
157 (1ULL << CAP_SYS_NICE) |
158 (1ULL << CAP_SYS_PTRACE) |
d87be9b0 159 (1ULL << CAP_SYS_RESOURCE) |
50b52222 160 (1ULL << CAP_SYS_TTY_CONFIG);
5a8af538 161static CustomMount *arg_custom_mounts = NULL;
88614c8a 162static size_t arg_n_custom_mounts = 0;
f4889f65 163static char **arg_setenv = NULL;
284c0b91 164static bool arg_quiet = false;
eb91eb18 165static bool arg_register = true;
89f7c846 166static bool arg_keep_unit = false;
aa28aefe 167static char **arg_network_interfaces = NULL;
c74e630d 168static char **arg_network_macvlan = NULL;
4bbfe7ad 169static char **arg_network_ipvlan = NULL;
69c79d3c 170static bool arg_network_veth = false;
f6d6bad1 171static char **arg_network_veth_extra = NULL;
f757855e 172static char *arg_network_bridge = NULL;
22b28dfd 173static char *arg_network_zone = NULL;
d7bea6b6 174static char *arg_network_namespace_path = NULL;
050f7277 175static unsigned long arg_personality = PERSONALITY_INVALID;
ec16945e 176static char *arg_image = NULL;
f757855e 177static VolatileMode arg_volatile_mode = VOLATILE_NO;
6d0b55c2 178static ExposePort *arg_expose_ports = NULL;
f36933fe 179static char **arg_property = NULL;
0de7acce 180static UserNamespaceMode arg_userns_mode = USER_NAMESPACE_NO;
6dac160c 181static uid_t arg_uid_shift = UID_INVALID, arg_uid_range = 0x10000U;
0de7acce 182static bool arg_userns_chown = false;
c6c8f6e2 183static int arg_kill_signal = 0;
5da38d07 184static CGroupUnified arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_UNKNOWN;
f757855e
LP
185static SettingsMask arg_settings_mask = 0;
186static int arg_settings_trusted = -1;
187static char **arg_parameters = NULL;
6aadfa4c 188static const char *arg_container_service_name = "systemd-nspawn";
9c1e04d0 189static bool arg_notify_ready = false;
5a8ff0e6 190static bool arg_use_cgns = true;
0c582db0 191static unsigned long arg_clone_ns_flags = CLONE_NEWIPC|CLONE_NEWPID|CLONE_NEWUTS;
1099ceeb 192static MountSettingsMask arg_mount_settings = MOUNT_APPLY_APIVFS_RO|MOUNT_APPLY_TMPFS_TMP;
4623e8e6
LP
193static void *arg_root_hash = NULL;
194static size_t arg_root_hash_size = 0;
960e4569
LP
195static char **arg_syscall_whitelist = NULL;
196static char **arg_syscall_blacklist = NULL;
bf428efb 197static struct rlimit *arg_rlimit[_RLIMIT_MAX] = {};
66edd963 198static bool arg_no_new_privileges = false;
81f345df
LP
199static int arg_oom_score_adjust = 0;
200static bool arg_oom_score_adjust_set = false;
d107bb7d
LP
201static cpu_set_t *arg_cpuset = NULL;
202static unsigned arg_cpuset_ncpus = 0;
09d423e9 203static ResolvConfMode arg_resolv_conf = RESOLV_CONF_AUTO;
1688841f 204static TimezoneMode arg_timezone = TIMEZONE_AUTO;
88213476 205
37ec0fdd
LP
206static int help(void) {
207 _cleanup_free_ char *link = NULL;
208 int r;
209
50ebcf6c
LP
210 (void) pager_open(false, false);
211
37ec0fdd
LP
212 r = terminal_urlify_man("systemd-nspawn", "1", &link);
213 if (r < 0)
214 return log_oom();
215
88213476 216 printf("%s [OPTIONS...] [PATH] [ARGUMENTS...]\n\n"
a7e2e50d 217 "Spawn a command or OS in a light-weight container.\n\n"
a8828ed9
DW
218 " -h --help Show this help\n"
219 " --version Print version string\n"
69c79d3c 220 " -q --quiet Do not show status information\n"
1b9e5b12 221 " -D --directory=PATH Root directory for the container\n"
ec16945e
LP
222 " --template=PATH Initialize root directory from template directory,\n"
223 " if missing\n"
224 " -x --ephemeral Run container with snapshot of root directory, and\n"
225 " remove it after exit\n"
226 " -i --image=PATH File system device or disk image for the container\n"
4623e8e6 227 " --root-hash=HASH Specify verity root hash\n"
7732f92b 228 " -a --as-pid2 Maintain a stub init as PID1, invoke binary as PID2\n"
a8828ed9 229 " -b --boot Boot up full system (i.e. invoke init)\n"
5f932eb9 230 " --chdir=PATH Set working directory in the container\n"
b53ede69
PW
231 " --pivot-root=PATH[:PATH]\n"
232 " Pivot root to given directory in the container\n"
a8828ed9 233 " -u --user=USER Run the command under specified user or uid\n"
a8828ed9 234 " -M --machine=NAME Set the machine name for the container\n"
3a9530e5 235 " --hostname=NAME Override the hostname for the container\n"
69c79d3c 236 " --uuid=UUID Set a specific machine UUID for the container\n"
a8828ed9 237 " -S --slice=SLICE Place the container in the specified slice\n"
f36933fe 238 " --property=NAME=VALUE Set scope unit property\n"
90b4a64d 239 " -U --private-users=pick Run within user namespace, autoselect UID/GID range\n"
03cfe0d5 240 " --private-users[=UIDBASE[:NUIDS]]\n"
90b4a64d 241 " Similar, but with user configured UID/GID range\n"
24597ee0 242 " --private-users-chown Adjust OS tree ownership to private UID/GID range\n"
69c79d3c
LP
243 " --private-network Disable network in container\n"
244 " --network-interface=INTERFACE\n"
245 " Assign an existing network interface to the\n"
246 " container\n"
c74e630d
LP
247 " --network-macvlan=INTERFACE\n"
248 " Create a macvlan network interface based on an\n"
249 " existing network interface to the container\n"
4bbfe7ad
TG
250 " --network-ipvlan=INTERFACE\n"
251 " Create a ipvlan network interface based on an\n"
252 " existing network interface to the container\n"
a8eaaee7 253 " -n --network-veth Add a virtual Ethernet connection between host\n"
69c79d3c 254 " and container\n"
f6d6bad1
LP
255 " --network-veth-extra=HOSTIF[:CONTAINERIF]\n"
256 " Add an additional virtual Ethernet link between\n"
257 " host and container\n"
ab046dde 258 " --network-bridge=INTERFACE\n"
90b4a64d
ZJS
259 " Add a virtual Ethernet connection to the container\n"
260 " and attach it to an existing bridge on the host\n"
261 " --network-zone=NAME Similar, but attach the new interface to an\n"
262 " an automatically managed bridge interface\n"
d7bea6b6
DP
263 " --network-namespace-path=PATH\n"
264 " Set network namespace to the one represented by\n"
265 " the specified kernel namespace file node\n"
6d0b55c2 266 " -p --port=[PROTOCOL:]HOSTPORT[:CONTAINERPORT]\n"
ab5e3a1b 267 " Expose a container IP port on the host\n"
82adf6af
LP
268 " -Z --selinux-context=SECLABEL\n"
269 " Set the SELinux security context to be used by\n"
270 " processes in the container\n"
271 " -L --selinux-apifs-context=SECLABEL\n"
272 " Set the SELinux security context to be used by\n"
273 " API/tmpfs file systems in the container\n"
a8828ed9
DW
274 " --capability=CAP In addition to the default, retain specified\n"
275 " capability\n"
276 " --drop-capability=CAP Drop the specified capability from the default set\n"
960e4569
LP
277 " --system-call-filter=LIST|~LIST\n"
278 " Permit/prohibit specific system calls\n"
bf428efb 279 " --rlimit=NAME=LIMIT Set a resource limit for the payload\n"
81f345df
LP
280 " --oom-score-adjust=VALUE\n"
281 " Adjust the OOM score value for the payload\n"
d107bb7d 282 " --cpu-affinity=CPUS Adjust the CPU affinity of the container\n"
c6c8f6e2 283 " --kill-signal=SIGNAL Select signal to use for shutting down PID 1\n"
2b26a728
LP
284 " --link-journal=MODE Link up guest journal, one of no, auto, guest, \n"
285 " host, try-guest, try-host\n"
574edc90 286 " -j Equivalent to --link-journal=try-guest\n"
09d423e9 287 " --resolv-conf=MODE Select mode of /etc/resolv.conf initialization\n"
1688841f 288 " --timezone=MODE Select mode of /etc/localtime initialization\n"
69c79d3c 289 " --read-only Mount the root directory read-only\n"
5e5bfa6e
EY
290 " --bind=PATH[:PATH[:OPTIONS]]\n"
291 " Bind mount a file or directory from the host into\n"
a8828ed9 292 " the container\n"
5e5bfa6e
EY
293 " --bind-ro=PATH[:PATH[:OPTIONS]\n"
294 " Similar, but creates a read-only bind mount\n"
06c17c39 295 " --tmpfs=PATH:[OPTIONS] Mount an empty tmpfs to the specified directory\n"
5a8af538
LP
296 " --overlay=PATH[:PATH...]:PATH\n"
297 " Create an overlay mount from the host to \n"
298 " the container\n"
299 " --overlay-ro=PATH[:PATH...]:PATH\n"
300 " Similar, but creates a read-only overlay mount\n"
a5f1cb3b 301 " -E --setenv=NAME=VALUE Pass an environment variable to PID 1\n"
eb91eb18 302 " --register=BOOLEAN Register container as machine\n"
89f7c846 303 " --keep-unit Do not register a scope for the machine, reuse\n"
4d9f07b4 304 " the service unit nspawn is running in\n"
6d0b55c2 305 " --volatile[=MODE] Run the system in volatile mode\n"
f757855e 306 " --settings=BOOLEAN Load additional settings from .nspawn file\n"
90b4a64d 307 " --notify-ready=BOOLEAN Receive notifications from the child init process\n"
37ec0fdd
LP
308 "\nSee the %s for details.\n"
309 , program_invocation_short_name
310 , link
311 );
312
313 return 0;
88213476
LP
314}
315
86c0dd4a 316static int custom_mount_check_all(void) {
88614c8a 317 size_t i;
5a8af538 318
5a8af538
LP
319 for (i = 0; i < arg_n_custom_mounts; i++) {
320 CustomMount *m = &arg_custom_mounts[i];
321
0de7acce 322 if (path_equal(m->destination, "/") && arg_userns_mode != USER_NAMESPACE_NO) {
0e7ac751
LP
323
324 if (arg_userns_chown) {
325 log_error("--private-users-chown may not be combined with custom root mounts.");
326 return -EINVAL;
327 } else if (arg_uid_shift == UID_INVALID) {
328 log_error("--private-users with automatic UID shift may not be combined with custom root mounts.");
329 return -EINVAL;
330 }
825d5287 331 }
5a8af538
LP
332 }
333
334 return 0;
335}
336
8199d554 337static int detect_unified_cgroup_hierarchy_from_environment(void) {
efdb0237 338 const char *e;
415fc41c 339 int r;
5da38d07 340
efdb0237
LP
341 /* Allow the user to control whether the unified hierarchy is used */
342 e = getenv("UNIFIED_CGROUP_HIERARCHY");
343 if (e) {
344 r = parse_boolean(e);
345 if (r < 0)
346 return log_error_errno(r, "Failed to parse $UNIFIED_CGROUP_HIERARCHY.");
5da38d07
TH
347 if (r > 0)
348 arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_ALL;
349 else
350 arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_NONE;
efdb0237
LP
351 }
352
8199d554
LP
353 return 0;
354}
355
356static int detect_unified_cgroup_hierarchy_from_image(const char *directory) {
357 int r;
358
359 /* Let's inherit the mode to use from the host system, but let's take into consideration what systemd in the
360 * image actually supports. */
b4cccbc1
LP
361 r = cg_all_unified();
362 if (r < 0)
363 return log_error_errno(r, "Failed to determine whether we are in all unified mode.");
364 if (r > 0) {
a8725a06
ZJS
365 /* Unified cgroup hierarchy support was added in 230. Unfortunately the detection
366 * routine only detects 231, so we'll have a false negative here for 230. */
367 r = systemd_installation_has_version(directory, 230);
368 if (r < 0)
369 return log_error_errno(r, "Failed to determine systemd version in container: %m");
370 if (r > 0)
371 arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_ALL;
372 else
373 arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_NONE;
c22800e4 374 } else if (cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER) > 0) {
2977724b
TH
375 /* Mixed cgroup hierarchy support was added in 233 */
376 r = systemd_installation_has_version(directory, 233);
0fd9563f
ZJS
377 if (r < 0)
378 return log_error_errno(r, "Failed to determine systemd version in container: %m");
379 if (r > 0)
380 arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_SYSTEMD;
381 else
382 arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_NONE;
383 } else
5da38d07 384 arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_NONE;
efdb0237 385
8199d554
LP
386 log_debug("Using %s hierarchy for container.",
387 arg_unified_cgroup_hierarchy == CGROUP_UNIFIED_NONE ? "legacy" :
388 arg_unified_cgroup_hierarchy == CGROUP_UNIFIED_SYSTEMD ? "hybrid" : "unified");
389
efdb0237
LP
390 return 0;
391}
392
0c582db0
LB
393static void parse_share_ns_env(const char *name, unsigned long ns_flag) {
394 int r;
395
396 r = getenv_bool(name);
397 if (r == -ENXIO)
398 return;
399 if (r < 0)
400 log_warning_errno(r, "Failed to parse %s from environment, defaulting to false.", name);
401 arg_clone_ns_flags = (arg_clone_ns_flags & ~ns_flag) | (r > 0 ? 0 : ns_flag);
402}
403
4f086aab 404static void parse_mount_settings_env(void) {
4f086aab 405 const char *e;
1099ceeb
LP
406 int r;
407
408 r = getenv_bool("SYSTEMD_NSPAWN_TMPFS_TMP");
409 if (r >= 0)
410 SET_FLAG(arg_mount_settings, MOUNT_APPLY_TMPFS_TMP, r > 0);
411 else if (r != -ENXIO)
412 log_warning_errno(r, "Failed to parse $SYSTEMD_NSPAWN_TMPFS_TMP, ignoring: %m");
4f086aab
SU
413
414 e = getenv("SYSTEMD_NSPAWN_API_VFS_WRITABLE");
415 if (!e)
416 return;
417
418 if (streq(e, "network")) {
419 arg_mount_settings |= MOUNT_APPLY_APIVFS_RO|MOUNT_APPLY_APIVFS_NETNS;
420 return;
421 }
422
423 r = parse_boolean(e);
424 if (r < 0) {
425 log_warning_errno(r, "Failed to parse SYSTEMD_NSPAWN_API_VFS_WRITABLE from environment, ignoring.");
426 return;
ab8ee0f2 427 }
4f086aab 428
ab8ee0f2
ZJS
429 SET_FLAG(arg_mount_settings, MOUNT_APPLY_APIVFS_RO, r == 0);
430 SET_FLAG(arg_mount_settings, MOUNT_APPLY_APIVFS_NETNS, false);
4f086aab
SU
431}
432
88213476 433static int parse_argv(int argc, char *argv[]) {
a41fe3a2 434 enum {
acbeb427
ZJS
435 ARG_VERSION = 0x100,
436 ARG_PRIVATE_NETWORK,
bc2f673e 437 ARG_UUID,
5076f0cc 438 ARG_READ_ONLY,
57fb9fb5 439 ARG_CAPABILITY,
420c7379 440 ARG_DROP_CAPABILITY,
17fe0523
LP
441 ARG_LINK_JOURNAL,
442 ARG_BIND,
f4889f65 443 ARG_BIND_RO,
06c17c39 444 ARG_TMPFS,
5a8af538
LP
445 ARG_OVERLAY,
446 ARG_OVERLAY_RO,
eb91eb18 447 ARG_SHARE_SYSTEM,
89f7c846 448 ARG_REGISTER,
aa28aefe 449 ARG_KEEP_UNIT,
69c79d3c 450 ARG_NETWORK_INTERFACE,
c74e630d 451 ARG_NETWORK_MACVLAN,
4bbfe7ad 452 ARG_NETWORK_IPVLAN,
ab046dde 453 ARG_NETWORK_BRIDGE,
22b28dfd 454 ARG_NETWORK_ZONE,
f6d6bad1 455 ARG_NETWORK_VETH_EXTRA,
d7bea6b6 456 ARG_NETWORK_NAMESPACE_PATH,
6afc95b7 457 ARG_PERSONALITY,
4d9f07b4 458 ARG_VOLATILE,
ec16945e 459 ARG_TEMPLATE,
f36933fe 460 ARG_PROPERTY,
6dac160c 461 ARG_PRIVATE_USERS,
c6c8f6e2 462 ARG_KILL_SIGNAL,
f757855e 463 ARG_SETTINGS,
5f932eb9 464 ARG_CHDIR,
b53ede69 465 ARG_PIVOT_ROOT,
7336138e 466 ARG_PRIVATE_USERS_CHOWN,
9c1e04d0 467 ARG_NOTIFY_READY,
4623e8e6 468 ARG_ROOT_HASH,
960e4569 469 ARG_SYSTEM_CALL_FILTER,
bf428efb 470 ARG_RLIMIT,
3a9530e5 471 ARG_HOSTNAME,
66edd963 472 ARG_NO_NEW_PRIVILEGES,
81f345df 473 ARG_OOM_SCORE_ADJUST,
d107bb7d 474 ARG_CPU_AFFINITY,
09d423e9 475 ARG_RESOLV_CONF,
1688841f 476 ARG_TIMEZONE,
a41fe3a2
LP
477 };
478
88213476 479 static const struct option options[] = {
d7bea6b6
DP
480 { "help", no_argument, NULL, 'h' },
481 { "version", no_argument, NULL, ARG_VERSION },
482 { "directory", required_argument, NULL, 'D' },
483 { "template", required_argument, NULL, ARG_TEMPLATE },
484 { "ephemeral", no_argument, NULL, 'x' },
485 { "user", required_argument, NULL, 'u' },
486 { "private-network", no_argument, NULL, ARG_PRIVATE_NETWORK },
487 { "as-pid2", no_argument, NULL, 'a' },
488 { "boot", no_argument, NULL, 'b' },
489 { "uuid", required_argument, NULL, ARG_UUID },
490 { "read-only", no_argument, NULL, ARG_READ_ONLY },
491 { "capability", required_argument, NULL, ARG_CAPABILITY },
492 { "drop-capability", required_argument, NULL, ARG_DROP_CAPABILITY },
66edd963 493 { "no-new-privileges", required_argument, NULL, ARG_NO_NEW_PRIVILEGES },
d7bea6b6
DP
494 { "link-journal", required_argument, NULL, ARG_LINK_JOURNAL },
495 { "bind", required_argument, NULL, ARG_BIND },
496 { "bind-ro", required_argument, NULL, ARG_BIND_RO },
497 { "tmpfs", required_argument, NULL, ARG_TMPFS },
498 { "overlay", required_argument, NULL, ARG_OVERLAY },
499 { "overlay-ro", required_argument, NULL, ARG_OVERLAY_RO },
500 { "machine", required_argument, NULL, 'M' },
3a9530e5 501 { "hostname", required_argument, NULL, ARG_HOSTNAME },
d7bea6b6
DP
502 { "slice", required_argument, NULL, 'S' },
503 { "setenv", required_argument, NULL, 'E' },
504 { "selinux-context", required_argument, NULL, 'Z' },
505 { "selinux-apifs-context", required_argument, NULL, 'L' },
506 { "quiet", no_argument, NULL, 'q' },
507 { "share-system", no_argument, NULL, ARG_SHARE_SYSTEM }, /* not documented */
508 { "register", required_argument, NULL, ARG_REGISTER },
509 { "keep-unit", no_argument, NULL, ARG_KEEP_UNIT },
510 { "network-interface", required_argument, NULL, ARG_NETWORK_INTERFACE },
511 { "network-macvlan", required_argument, NULL, ARG_NETWORK_MACVLAN },
512 { "network-ipvlan", required_argument, NULL, ARG_NETWORK_IPVLAN },
513 { "network-veth", no_argument, NULL, 'n' },
514 { "network-veth-extra", required_argument, NULL, ARG_NETWORK_VETH_EXTRA },
515 { "network-bridge", required_argument, NULL, ARG_NETWORK_BRIDGE },
516 { "network-zone", required_argument, NULL, ARG_NETWORK_ZONE },
517 { "network-namespace-path", required_argument, NULL, ARG_NETWORK_NAMESPACE_PATH },
518 { "personality", required_argument, NULL, ARG_PERSONALITY },
519 { "image", required_argument, NULL, 'i' },
520 { "volatile", optional_argument, NULL, ARG_VOLATILE },
521 { "port", required_argument, NULL, 'p' },
522 { "property", required_argument, NULL, ARG_PROPERTY },
523 { "private-users", optional_argument, NULL, ARG_PRIVATE_USERS },
524 { "private-users-chown", optional_argument, NULL, ARG_PRIVATE_USERS_CHOWN },
525 { "kill-signal", required_argument, NULL, ARG_KILL_SIGNAL },
526 { "settings", required_argument, NULL, ARG_SETTINGS },
527 { "chdir", required_argument, NULL, ARG_CHDIR },
528 { "pivot-root", required_argument, NULL, ARG_PIVOT_ROOT },
529 { "notify-ready", required_argument, NULL, ARG_NOTIFY_READY },
530 { "root-hash", required_argument, NULL, ARG_ROOT_HASH },
531 { "system-call-filter", required_argument, NULL, ARG_SYSTEM_CALL_FILTER },
bf428efb 532 { "rlimit", required_argument, NULL, ARG_RLIMIT },
81f345df 533 { "oom-score-adjust", required_argument, NULL, ARG_OOM_SCORE_ADJUST },
d107bb7d 534 { "cpu-affinity", required_argument, NULL, ARG_CPU_AFFINITY },
09d423e9 535 { "resolv-conf", required_argument, NULL, ARG_RESOLV_CONF },
1688841f 536 { "timezone", required_argument, NULL, ARG_TIMEZONE },
eb9da376 537 {}
88213476
LP
538 };
539
9444b1f2 540 int c, r;
6aadfa4c 541 const char *p, *e;
a42c8b54 542 uint64_t plus = 0, minus = 0;
f757855e 543 bool mask_all_settings = false, mask_no_settings = false;
88213476
LP
544
545 assert(argc >= 0);
546 assert(argv);
547
2e1f244e 548 while ((c = getopt_long(argc, argv, "+hD:u:abL:M:jS:Z:qi:xp:nUE:", options, NULL)) >= 0)
88213476
LP
549 switch (c) {
550
551 case 'h':
37ec0fdd 552 return help();
88213476 553
acbeb427 554 case ARG_VERSION:
3f6fd1ba 555 return version();
acbeb427 556
88213476 557 case 'D':
0f03c2a4 558 r = parse_path_argument_and_warn(optarg, false, &arg_directory);
ec16945e 559 if (r < 0)
0f03c2a4 560 return r;
ec16945e
LP
561 break;
562
563 case ARG_TEMPLATE:
0f03c2a4 564 r = parse_path_argument_and_warn(optarg, false, &arg_template);
ec16945e 565 if (r < 0)
0f03c2a4 566 return r;
88213476
LP
567 break;
568
1b9e5b12 569 case 'i':
0f03c2a4 570 r = parse_path_argument_and_warn(optarg, false, &arg_image);
ec16945e 571 if (r < 0)
0f03c2a4 572 return r;
ec16945e
LP
573 break;
574
575 case 'x':
576 arg_ephemeral = true;
a2f577fc 577 arg_settings_mask |= SETTING_EPHEMERAL;
1b9e5b12
LP
578 break;
579
687d0825 580 case 'u':
2fc09a9c
DM
581 r = free_and_strdup(&arg_user, optarg);
582 if (r < 0)
7027ff61 583 return log_oom();
687d0825 584
f757855e 585 arg_settings_mask |= SETTING_USER;
687d0825
MV
586 break;
587
22b28dfd
LP
588 case ARG_NETWORK_ZONE: {
589 char *j;
590
591 j = strappend("vz-", optarg);
592 if (!j)
593 return log_oom();
594
595 if (!ifname_valid(j)) {
596 log_error("Network zone name not valid: %s", j);
597 free(j);
598 return -EINVAL;
599 }
600
df1fac6d 601 free_and_replace(arg_network_zone, j);
22b28dfd
LP
602
603 arg_network_veth = true;
604 arg_private_network = true;
605 arg_settings_mask |= SETTING_NETWORK;
606 break;
607 }
608
ab046dde 609 case ARG_NETWORK_BRIDGE:
ef76dff2
LP
610
611 if (!ifname_valid(optarg)) {
612 log_error("Bridge interface name not valid: %s", optarg);
613 return -EINVAL;
614 }
615
f757855e
LP
616 r = free_and_strdup(&arg_network_bridge, optarg);
617 if (r < 0)
618 return log_oom();
ab046dde 619
4831981d 620 _fallthrough_;
0dfaa006 621 case 'n':
69c79d3c
LP
622 arg_network_veth = true;
623 arg_private_network = true;
f757855e 624 arg_settings_mask |= SETTING_NETWORK;
69c79d3c
LP
625 break;
626
f6d6bad1
LP
627 case ARG_NETWORK_VETH_EXTRA:
628 r = veth_extra_parse(&arg_network_veth_extra, optarg);
629 if (r < 0)
630 return log_error_errno(r, "Failed to parse --network-veth-extra= parameter: %s", optarg);
631
632 arg_private_network = true;
633 arg_settings_mask |= SETTING_NETWORK;
634 break;
635
aa28aefe 636 case ARG_NETWORK_INTERFACE:
ef76dff2
LP
637 if (!ifname_valid(optarg)) {
638 log_error("Network interface name not valid: %s", optarg);
639 return -EINVAL;
640 }
641
c74e630d
LP
642 if (strv_extend(&arg_network_interfaces, optarg) < 0)
643 return log_oom();
644
645 arg_private_network = true;
f757855e 646 arg_settings_mask |= SETTING_NETWORK;
c74e630d
LP
647 break;
648
649 case ARG_NETWORK_MACVLAN:
ef76dff2
LP
650
651 if (!ifname_valid(optarg)) {
652 log_error("MACVLAN network interface name not valid: %s", optarg);
653 return -EINVAL;
654 }
655
c74e630d 656 if (strv_extend(&arg_network_macvlan, optarg) < 0)
aa28aefe
LP
657 return log_oom();
658
4bbfe7ad 659 arg_private_network = true;
f757855e 660 arg_settings_mask |= SETTING_NETWORK;
4bbfe7ad
TG
661 break;
662
663 case ARG_NETWORK_IPVLAN:
ef76dff2
LP
664
665 if (!ifname_valid(optarg)) {
666 log_error("IPVLAN network interface name not valid: %s", optarg);
667 return -EINVAL;
668 }
669
4bbfe7ad
TG
670 if (strv_extend(&arg_network_ipvlan, optarg) < 0)
671 return log_oom();
672
4831981d 673 _fallthrough_;
ff01d048
LP
674 case ARG_PRIVATE_NETWORK:
675 arg_private_network = true;
f757855e 676 arg_settings_mask |= SETTING_NETWORK;
a41fe3a2
LP
677 break;
678
d7bea6b6
DP
679 case ARG_NETWORK_NAMESPACE_PATH:
680 r = parse_path_argument_and_warn(optarg, false, &arg_network_namespace_path);
681 if (r < 0)
682 return r;
683
684 break;
685
0f0dbc46 686 case 'b':
7732f92b
LP
687 if (arg_start_mode == START_PID2) {
688 log_error("--boot and --as-pid2 may not be combined.");
689 return -EINVAL;
690 }
691
692 arg_start_mode = START_BOOT;
693 arg_settings_mask |= SETTING_START_MODE;
694 break;
695
696 case 'a':
697 if (arg_start_mode == START_BOOT) {
698 log_error("--boot and --as-pid2 may not be combined.");
699 return -EINVAL;
700 }
701
702 arg_start_mode = START_PID2;
703 arg_settings_mask |= SETTING_START_MODE;
0f0dbc46
LP
704 break;
705
144f0fc0 706 case ARG_UUID:
9444b1f2 707 r = sd_id128_from_string(optarg, &arg_uuid);
317feb4d
LP
708 if (r < 0)
709 return log_error_errno(r, "Invalid UUID: %s", optarg);
710
711 if (sd_id128_is_null(arg_uuid)) {
712 log_error("Machine UUID may not be all zeroes.");
713 return -EINVAL;
aa96c6cb 714 }
f757855e
LP
715
716 arg_settings_mask |= SETTING_MACHINE_ID;
9444b1f2 717 break;
aa96c6cb 718
9444b1f2 719 case 'S':
c74e630d 720 arg_slice = optarg;
144f0fc0
LP
721 break;
722
7027ff61 723 case 'M':
c1521918 724 if (isempty(optarg))
97b11eed 725 arg_machine = mfree(arg_machine);
c1521918 726 else {
0c3c4284 727 if (!machine_name_is_valid(optarg)) {
eb91eb18
LP
728 log_error("Invalid machine name: %s", optarg);
729 return -EINVAL;
730 }
7027ff61 731
0c3c4284
LP
732 r = free_and_strdup(&arg_machine, optarg);
733 if (r < 0)
eb91eb18 734 return log_oom();
eb91eb18 735 }
9ce6d1b3 736 break;
7027ff61 737
3a9530e5
LP
738 case ARG_HOSTNAME:
739 if (isempty(optarg))
740 arg_hostname = mfree(arg_hostname);
741 else {
742 if (!hostname_is_valid(optarg, false)) {
743 log_error("Invalid hostname: %s", optarg);
744 return -EINVAL;
745 }
746
747 r = free_and_strdup(&arg_hostname, optarg);
748 if (r < 0)
749 return log_oom();
750 }
751
752 arg_settings_mask |= SETTING_HOSTNAME;
753 break;
754
82adf6af
LP
755 case 'Z':
756 arg_selinux_context = optarg;
a8828ed9
DW
757 break;
758
82adf6af
LP
759 case 'L':
760 arg_selinux_apifs_context = optarg;
a8828ed9
DW
761 break;
762
bc2f673e
LP
763 case ARG_READ_ONLY:
764 arg_read_only = true;
f757855e 765 arg_settings_mask |= SETTING_READ_ONLY;
bc2f673e
LP
766 break;
767
420c7379
LP
768 case ARG_CAPABILITY:
769 case ARG_DROP_CAPABILITY: {
6cbe4ed1 770 p = optarg;
9ed794a3 771 for (;;) {
6cbe4ed1 772 _cleanup_free_ char *t = NULL;
5076f0cc 773
6cbe4ed1
SS
774 r = extract_first_word(&p, &t, ",", 0);
775 if (r < 0)
776 return log_error_errno(r, "Failed to parse capability %s.", t);
5076f0cc 777
6cbe4ed1
SS
778 if (r == 0)
779 break;
5076f0cc 780
39ed67d1
LP
781 if (streq(t, "all")) {
782 if (c == ARG_CAPABILITY)
a42c8b54 783 plus = (uint64_t) -1;
39ed67d1 784 else
a42c8b54 785 minus = (uint64_t) -1;
39ed67d1 786 } else {
2822da4f
LP
787 int cap;
788
789 cap = capability_from_name(t);
790 if (cap < 0) {
39ed67d1
LP
791 log_error("Failed to parse capability %s.", t);
792 return -EINVAL;
793 }
794
795 if (c == ARG_CAPABILITY)
a42c8b54 796 plus |= 1ULL << (uint64_t) cap;
39ed67d1 797 else
a42c8b54 798 minus |= 1ULL << (uint64_t) cap;
5076f0cc 799 }
5076f0cc
LP
800 }
801
f757855e 802 arg_settings_mask |= SETTING_CAPABILITY;
5076f0cc
LP
803 break;
804 }
805
66edd963
LP
806 case ARG_NO_NEW_PRIVILEGES:
807 r = parse_boolean(optarg);
808 if (r < 0)
809 return log_error_errno(r, "Failed to parse --no-new-privileges= argument: %s", optarg);
810
811 arg_no_new_privileges = r;
812 arg_settings_mask |= SETTING_NO_NEW_PRIVILEGES;
813 break;
814
57fb9fb5
LP
815 case 'j':
816 arg_link_journal = LINK_GUEST;
574edc90 817 arg_link_journal_try = true;
4e1d6aa9 818 arg_settings_mask |= SETTING_LINK_JOURNAL;
57fb9fb5
LP
819 break;
820
821 case ARG_LINK_JOURNAL:
4e1d6aa9
LP
822 r = parse_link_journal(optarg, &arg_link_journal, &arg_link_journal_try);
823 if (r < 0) {
824 log_error_errno(r, "Failed to parse link journal mode %s", optarg);
57fb9fb5
LP
825 return -EINVAL;
826 }
827
4e1d6aa9 828 arg_settings_mask |= SETTING_LINK_JOURNAL;
57fb9fb5
LP
829 break;
830
17fe0523 831 case ARG_BIND:
f757855e
LP
832 case ARG_BIND_RO:
833 r = bind_mount_parse(&arg_custom_mounts, &arg_n_custom_mounts, optarg, c == ARG_BIND_RO);
834 if (r < 0)
835 return log_error_errno(r, "Failed to parse --bind(-ro)= argument %s: %m", optarg);
17fe0523 836
f757855e 837 arg_settings_mask |= SETTING_CUSTOM_MOUNTS;
17fe0523 838 break;
06c17c39 839
f757855e
LP
840 case ARG_TMPFS:
841 r = tmpfs_mount_parse(&arg_custom_mounts, &arg_n_custom_mounts, optarg);
842 if (r < 0)
843 return log_error_errno(r, "Failed to parse --tmpfs= argument %s: %m", optarg);
5a8af538 844
f757855e 845 arg_settings_mask |= SETTING_CUSTOM_MOUNTS;
5a8af538 846 break;
5a8af538
LP
847
848 case ARG_OVERLAY:
ad85779a
LP
849 case ARG_OVERLAY_RO:
850 r = overlay_mount_parse(&arg_custom_mounts, &arg_n_custom_mounts, optarg, c == ARG_OVERLAY_RO);
851 if (r == -EADDRNOTAVAIL)
852 return log_error_errno(r, "--overlay(-ro)= needs at least two colon-separated directories specified.");
853 if (r < 0)
854 return log_error_errno(r, "Failed to parse --overlay(-ro)= argument %s: %m", optarg);
06c17c39 855
f757855e 856 arg_settings_mask |= SETTING_CUSTOM_MOUNTS;
06c17c39 857 break;
06c17c39 858
a5f1cb3b 859 case 'E': {
f4889f65
LP
860 char **n;
861
862 if (!env_assignment_is_valid(optarg)) {
863 log_error("Environment variable assignment '%s' is not valid.", optarg);
864 return -EINVAL;
865 }
866
867 n = strv_env_set(arg_setenv, optarg);
868 if (!n)
869 return log_oom();
870
130d3d22 871 strv_free_and_replace(arg_setenv, n);
f757855e 872 arg_settings_mask |= SETTING_ENVIRONMENT;
f4889f65
LP
873 break;
874 }
875
284c0b91
LP
876 case 'q':
877 arg_quiet = true;
878 break;
879
8a96d94e 880 case ARG_SHARE_SYSTEM:
a6b5216c 881 /* We don't officially support this anymore, except for compat reasons. People should use the
0c582db0 882 * $SYSTEMD_NSPAWN_SHARE_* environment variables instead. */
63d1c29f 883 log_warning("Please do not use --share-system anymore, use $SYSTEMD_NSPAWN_SHARE_* instead.");
0c582db0 884 arg_clone_ns_flags = 0;
8a96d94e
LP
885 break;
886
eb91eb18
LP
887 case ARG_REGISTER:
888 r = parse_boolean(optarg);
889 if (r < 0) {
890 log_error("Failed to parse --register= argument: %s", optarg);
891 return r;
892 }
893
894 arg_register = r;
895 break;
896
89f7c846
LP
897 case ARG_KEEP_UNIT:
898 arg_keep_unit = true;
899 break;
900
6afc95b7
LP
901 case ARG_PERSONALITY:
902
ac45f971 903 arg_personality = personality_from_string(optarg);
050f7277 904 if (arg_personality == PERSONALITY_INVALID) {
6afc95b7
LP
905 log_error("Unknown or unsupported personality '%s'.", optarg);
906 return -EINVAL;
907 }
908
f757855e 909 arg_settings_mask |= SETTING_PERSONALITY;
6afc95b7
LP
910 break;
911
4d9f07b4
LP
912 case ARG_VOLATILE:
913
914 if (!optarg)
f757855e 915 arg_volatile_mode = VOLATILE_YES;
5c828e66
LP
916 else if (streq(optarg, "help")) {
917 DUMP_STRING_TABLE(volatile_mode, VolatileMode, _VOLATILE_MODE_MAX);
918 return 0;
919 } else {
f757855e 920 VolatileMode m;
4d9f07b4 921
f757855e
LP
922 m = volatile_mode_from_string(optarg);
923 if (m < 0) {
924 log_error("Failed to parse --volatile= argument: %s", optarg);
6d0b55c2 925 return -EINVAL;
f757855e
LP
926 } else
927 arg_volatile_mode = m;
6d0b55c2
LP
928 }
929
f757855e
LP
930 arg_settings_mask |= SETTING_VOLATILE_MODE;
931 break;
6d0b55c2 932
f757855e
LP
933 case 'p':
934 r = expose_port_parse(&arg_expose_ports, optarg);
935 if (r == -EEXIST)
936 return log_error_errno(r, "Duplicate port specification: %s", optarg);
937 if (r < 0)
938 return log_error_errno(r, "Failed to parse host port %s: %m", optarg);
6d0b55c2 939
f757855e 940 arg_settings_mask |= SETTING_EXPOSE_PORTS;
6d0b55c2 941 break;
6d0b55c2 942
f36933fe
LP
943 case ARG_PROPERTY:
944 if (strv_extend(&arg_property, optarg) < 0)
945 return log_oom();
946
947 break;
948
ae209204
ZJS
949 case ARG_PRIVATE_USERS: {
950 int boolean = -1;
0de7acce 951
ae209204
ZJS
952 if (!optarg)
953 boolean = true;
954 else if (!in_charset(optarg, DIGITS))
955 /* do *not* parse numbers as booleans */
956 boolean = parse_boolean(optarg);
957
958 if (boolean == false) {
0de7acce
LP
959 /* no: User namespacing off */
960 arg_userns_mode = USER_NAMESPACE_NO;
961 arg_uid_shift = UID_INVALID;
962 arg_uid_range = UINT32_C(0x10000);
ae209204 963 } else if (boolean == true) {
0de7acce
LP
964 /* yes: User namespacing on, UID range is read from root dir */
965 arg_userns_mode = USER_NAMESPACE_FIXED;
966 arg_uid_shift = UID_INVALID;
967 arg_uid_range = UINT32_C(0x10000);
968 } else if (streq(optarg, "pick")) {
969 /* pick: User namespacing on, UID range is picked randomly */
970 arg_userns_mode = USER_NAMESPACE_PICK;
971 arg_uid_shift = UID_INVALID;
972 arg_uid_range = UINT32_C(0x10000);
973 } else {
6c2058b3 974 _cleanup_free_ char *buffer = NULL;
6dac160c
LP
975 const char *range, *shift;
976
0de7acce
LP
977 /* anything else: User namespacing on, UID range is explicitly configured */
978
6dac160c
LP
979 range = strchr(optarg, ':');
980 if (range) {
6c2058b3
ZJS
981 buffer = strndup(optarg, range - optarg);
982 if (!buffer)
983 return log_oom();
984 shift = buffer;
6dac160c
LP
985
986 range++;
bfd292ec
ZJS
987 r = safe_atou32(range, &arg_uid_range);
988 if (r < 0)
be715731 989 return log_error_errno(r, "Failed to parse UID range \"%s\": %m", range);
6dac160c
LP
990 } else
991 shift = optarg;
992
be715731
ZJS
993 r = parse_uid(shift, &arg_uid_shift);
994 if (r < 0)
995 return log_error_errno(r, "Failed to parse UID \"%s\": %m", optarg);
0de7acce
LP
996
997 arg_userns_mode = USER_NAMESPACE_FIXED;
6dac160c
LP
998 }
999
be715731
ZJS
1000 if (arg_uid_range <= 0) {
1001 log_error("UID range cannot be 0.");
1002 return -EINVAL;
1003 }
1004
0de7acce 1005 arg_settings_mask |= SETTING_USERNS;
6dac160c 1006 break;
ae209204 1007 }
6dac160c 1008
0de7acce 1009 case 'U':
ccabee0d
LP
1010 if (userns_supported()) {
1011 arg_userns_mode = USER_NAMESPACE_PICK;
1012 arg_uid_shift = UID_INVALID;
1013 arg_uid_range = UINT32_C(0x10000);
1014
1015 arg_settings_mask |= SETTING_USERNS;
6dac160c
LP
1016 }
1017
7336138e
LP
1018 break;
1019
0de7acce 1020 case ARG_PRIVATE_USERS_CHOWN:
19aac838 1021 arg_userns_chown = true;
0de7acce
LP
1022
1023 arg_settings_mask |= SETTING_USERNS;
6dac160c
LP
1024 break;
1025
c6c8f6e2 1026 case ARG_KILL_SIGNAL:
5c828e66
LP
1027 if (streq(optarg, "help")) {
1028 DUMP_STRING_TABLE(signal, int, _NSIG);
1029 return 0;
1030 }
1031
29a3db75 1032 arg_kill_signal = signal_from_string(optarg);
c6c8f6e2
LP
1033 if (arg_kill_signal < 0) {
1034 log_error("Cannot parse signal: %s", optarg);
1035 return -EINVAL;
1036 }
1037
f757855e
LP
1038 arg_settings_mask |= SETTING_KILL_SIGNAL;
1039 break;
1040
1041 case ARG_SETTINGS:
1042
1043 /* no → do not read files
1044 * yes → read files, do not override cmdline, trust only subset
1045 * override → read files, override cmdline, trust only subset
1046 * trusted → read files, do not override cmdline, trust all
1047 */
1048
1049 r = parse_boolean(optarg);
1050 if (r < 0) {
1051 if (streq(optarg, "trusted")) {
1052 mask_all_settings = false;
1053 mask_no_settings = false;
1054 arg_settings_trusted = true;
1055
1056 } else if (streq(optarg, "override")) {
1057 mask_all_settings = false;
1058 mask_no_settings = true;
1059 arg_settings_trusted = -1;
1060 } else
1061 return log_error_errno(r, "Failed to parse --settings= argument: %s", optarg);
1062 } else if (r > 0) {
1063 /* yes */
1064 mask_all_settings = false;
1065 mask_no_settings = false;
1066 arg_settings_trusted = -1;
1067 } else {
1068 /* no */
1069 mask_all_settings = true;
1070 mask_no_settings = false;
1071 arg_settings_trusted = false;
1072 }
1073
c6c8f6e2
LP
1074 break;
1075
5f932eb9
LP
1076 case ARG_CHDIR:
1077 if (!path_is_absolute(optarg)) {
1078 log_error("Working directory %s is not an absolute path.", optarg);
1079 return -EINVAL;
1080 }
1081
1082 r = free_and_strdup(&arg_chdir, optarg);
1083 if (r < 0)
1084 return log_oom();
1085
1086 arg_settings_mask |= SETTING_WORKING_DIRECTORY;
1087 break;
1088
b53ede69
PW
1089 case ARG_PIVOT_ROOT:
1090 r = pivot_root_parse(&arg_pivot_root_new, &arg_pivot_root_old, optarg);
1091 if (r < 0)
1092 return log_error_errno(r, "Failed to parse --pivot-root= argument %s: %m", optarg);
1093
1094 arg_settings_mask |= SETTING_PIVOT_ROOT;
1095 break;
1096
9c1e04d0
AP
1097 case ARG_NOTIFY_READY:
1098 r = parse_boolean(optarg);
1099 if (r < 0) {
1100 log_error("%s is not a valid notify mode. Valid modes are: yes, no, and ready.", optarg);
1101 return -EINVAL;
1102 }
1103 arg_notify_ready = r;
1104 arg_settings_mask |= SETTING_NOTIFY_READY;
1105 break;
1106
4623e8e6
LP
1107 case ARG_ROOT_HASH: {
1108 void *k;
1109 size_t l;
1110
1111 r = unhexmem(optarg, strlen(optarg), &k, &l);
1112 if (r < 0)
1113 return log_error_errno(r, "Failed to parse root hash: %s", optarg);
1114 if (l < sizeof(sd_id128_t)) {
1115 log_error("Root hash must be at least 128bit long: %s", optarg);
1116 free(k);
1117 return -EINVAL;
1118 }
1119
1120 free(arg_root_hash);
1121 arg_root_hash = k;
1122 arg_root_hash_size = l;
1123 break;
1124 }
1125
960e4569
LP
1126 case ARG_SYSTEM_CALL_FILTER: {
1127 bool negative;
1128 const char *items;
1129
1130 negative = optarg[0] == '~';
1131 items = negative ? optarg + 1 : optarg;
1132
1133 for (;;) {
1134 _cleanup_free_ char *word = NULL;
1135
1136 r = extract_first_word(&items, &word, NULL, 0);
1137 if (r == 0)
1138 break;
1139 if (r == -ENOMEM)
1140 return log_oom();
1141 if (r < 0)
1142 return log_error_errno(r, "Failed to parse system call filter: %m");
1143
1144 if (negative)
1145 r = strv_extend(&arg_syscall_blacklist, word);
1146 else
1147 r = strv_extend(&arg_syscall_whitelist, word);
1148 if (r < 0)
1149 return log_oom();
1150 }
1151
1152 arg_settings_mask |= SETTING_SYSCALL_FILTER;
1153 break;
1154 }
1155
bf428efb
LP
1156 case ARG_RLIMIT: {
1157 const char *eq;
1158 char *name;
1159 int rl;
1160
5c828e66
LP
1161 if (streq(optarg, "help")) {
1162 DUMP_STRING_TABLE(rlimit, int, _RLIMIT_MAX);
1163 return 0;
1164 }
1165
bf428efb
LP
1166 eq = strchr(optarg, '=');
1167 if (!eq) {
1168 log_error("--rlimit= expects an '=' assignment.");
1169 return -EINVAL;
1170 }
1171
1172 name = strndup(optarg, eq - optarg);
1173 if (!name)
1174 return log_oom();
1175
1176 rl = rlimit_from_string_harder(name);
1177 if (rl < 0) {
1178 log_error("Unknown resource limit: %s", name);
1179 return -EINVAL;
1180 }
1181
1182 if (!arg_rlimit[rl]) {
1183 arg_rlimit[rl] = new0(struct rlimit, 1);
1184 if (!arg_rlimit[rl])
1185 return log_oom();
1186 }
1187
1188 r = rlimit_parse(rl, eq + 1, arg_rlimit[rl]);
1189 if (r < 0)
1190 return log_error_errno(r, "Failed to parse resource limit: %s", eq + 1);
1191
1192 arg_settings_mask |= SETTING_RLIMIT_FIRST << rl;
1193 break;
1194 }
1195
81f345df
LP
1196 case ARG_OOM_SCORE_ADJUST:
1197 r = parse_oom_score_adjust(optarg, &arg_oom_score_adjust);
1198 if (r < 0)
1199 return log_error_errno(r, "Failed to parse --oom-score-adjust= parameter: %s", optarg);
1200
1201 arg_oom_score_adjust_set = true;
1202 arg_settings_mask |= SETTING_OOM_SCORE_ADJUST;
1203 break;
1204
d107bb7d
LP
1205 case ARG_CPU_AFFINITY: {
1206 _cleanup_cpu_free_ cpu_set_t *cpuset = NULL;
1207
1208 r = parse_cpu_set(optarg, &cpuset);
1209 if (r < 0)
1210 return log_error_errno(r, "Failed to parse CPU affinity mask: %s", optarg);
1211
1212 if (arg_cpuset)
1213 CPU_FREE(arg_cpuset);
1214
1215 arg_cpuset = TAKE_PTR(cpuset);
1216 arg_cpuset_ncpus = r;
1217 arg_settings_mask |= SETTING_CPU_AFFINITY;
1218 break;
1219 }
1220
09d423e9
LP
1221 case ARG_RESOLV_CONF:
1222 if (streq(optarg, "help")) {
1223 DUMP_STRING_TABLE(resolv_conf_mode, ResolvConfMode, _RESOLV_CONF_MODE_MAX);
1224 return 0;
1225 }
1226
1227 arg_resolv_conf = resolv_conf_mode_from_string(optarg);
1228 if (arg_resolv_conf < 0) {
1229 log_error("Failed to parse /etc/resolv.conf mode: %s", optarg);
1230 return -EINVAL;
1231 }
1232
1233 arg_settings_mask |= SETTING_RESOLV_CONF;
1234 break;
1235
1688841f
LP
1236 case ARG_TIMEZONE:
1237 if (streq(optarg, "help")) {
1238 DUMP_STRING_TABLE(timezone_mode, TimezoneMode, _TIMEZONE_MODE_MAX);
1239 return 0;
1240 }
1241
1242 arg_timezone = timezone_mode_from_string(optarg);
1243 if (arg_timezone < 0) {
1244 log_error("Failed to parse /etc/localtime mode: %s", optarg);
1245 return -EINVAL;
1246 }
1247
1248 arg_settings_mask |= SETTING_TIMEZONE;
1249 break;
1250
88213476
LP
1251 case '?':
1252 return -EINVAL;
1253
1254 default:
eb9da376 1255 assert_not_reached("Unhandled option");
88213476 1256 }
88213476 1257
d7bea6b6
DP
1258 /* If --network-namespace-path is given with any other network-related option,
1259 * we need to error out, to avoid conflicts between different network options. */
1260 if (arg_network_namespace_path &&
1261 (arg_network_interfaces || arg_network_macvlan ||
1262 arg_network_ipvlan || arg_network_veth_extra ||
1263 arg_network_bridge || arg_network_zone ||
1264 arg_network_veth || arg_private_network)) {
1265 log_error("--network-namespace-path cannot be combined with other network options.");
1266 return -EINVAL;
1267 }
1268
0c582db0
LB
1269 parse_share_ns_env("SYSTEMD_NSPAWN_SHARE_NS_IPC", CLONE_NEWIPC);
1270 parse_share_ns_env("SYSTEMD_NSPAWN_SHARE_NS_PID", CLONE_NEWPID);
1271 parse_share_ns_env("SYSTEMD_NSPAWN_SHARE_NS_UTS", CLONE_NEWUTS);
1272 parse_share_ns_env("SYSTEMD_NSPAWN_SHARE_SYSTEM", CLONE_NEWIPC|CLONE_NEWPID|CLONE_NEWUTS);
a6b5216c 1273
4f086aab
SU
1274 if (arg_userns_mode != USER_NAMESPACE_NO)
1275 arg_mount_settings |= MOUNT_USE_USERNS;
1276
1277 if (arg_private_network)
1278 arg_mount_settings |= MOUNT_APPLY_APIVFS_NETNS;
1279
1280 parse_mount_settings_env();
1281
48a8d337
LB
1282 if (!(arg_clone_ns_flags & CLONE_NEWPID) ||
1283 !(arg_clone_ns_flags & CLONE_NEWUTS)) {
eb91eb18 1284 arg_register = false;
0c582db0
LB
1285 if (arg_start_mode != START_PID1) {
1286 log_error("--boot cannot be used without namespacing.");
1287 return -EINVAL;
1288 }
1289 }
eb91eb18 1290
0de7acce 1291 if (arg_userns_mode == USER_NAMESPACE_PICK)
0e7ac751
LP
1292 arg_userns_chown = true;
1293
cd2dfc6f 1294 if (arg_keep_unit && arg_register && cg_pid_get_owner_uid(0, NULL) >= 0) {
8d9c2bca
AJ
1295 /* Save the user from accidentally registering either user-$SESSION.scope or user@.service.
1296 * The latter is not technically a user session, but we don't need to labour the point. */
cd2dfc6f 1297 log_error("--keep-unit --register=yes may not be used when invoked from a user session.");
89f7c846
LP
1298 return -EINVAL;
1299 }
1300
1b9e5b12
LP
1301 if (arg_directory && arg_image) {
1302 log_error("--directory= and --image= may not be combined.");
1303 return -EINVAL;
1304 }
1305
ec16945e
LP
1306 if (arg_template && arg_image) {
1307 log_error("--template= and --image= may not be combined.");
1308 return -EINVAL;
1309 }
1310
8cd328d8
LP
1311 if (arg_ephemeral && arg_template && !arg_directory) {
1312 /* User asked for ephemeral execution but specified --template= instead of --directory=. Semantically
1313 * such an invocation makes some sense, see https://github.com/systemd/systemd/issues/3667. Let's
1314 * accept this here, and silently make "--ephemeral --template=" equivalent to "--ephemeral
1315 * --directory=". */
1316
ae2a15bc 1317 arg_directory = TAKE_PTR(arg_template);
8cd328d8
LP
1318 }
1319
ec16945e
LP
1320 if (arg_template && !(arg_directory || arg_machine)) {
1321 log_error("--template= needs --directory= or --machine=.");
1322 return -EINVAL;
1323 }
1324
1325 if (arg_ephemeral && arg_template) {
1326 log_error("--ephemeral and --template= may not be combined.");
1327 return -EINVAL;
1328 }
1329
df9a75e4
LP
1330 if (arg_ephemeral && !IN_SET(arg_link_journal, LINK_NO, LINK_AUTO)) {
1331 log_error("--ephemeral and --link-journal= may not be combined.");
1332 return -EINVAL;
1333 }
1334
ccabee0d 1335 if (arg_userns_mode != USER_NAMESPACE_NO && !userns_supported()) {
7336138e
LP
1336 log_error("--private-users= is not supported, kernel compiled without user namespace support.");
1337 return -EOPNOTSUPP;
1338 }
1339
1340 if (arg_userns_chown && arg_read_only) {
1341 log_error("--read-only and --private-users-chown may not be combined.");
1342 return -EINVAL;
1343 }
f757855e 1344
22b28dfd
LP
1345 if (arg_network_bridge && arg_network_zone) {
1346 log_error("--network-bridge= and --network-zone= may not be combined.");
1347 return -EINVAL;
1348 }
1349
f757855e
LP
1350 if (argc > optind) {
1351 arg_parameters = strv_copy(argv + optind);
1352 if (!arg_parameters)
1353 return log_oom();
1354
7732f92b 1355 arg_settings_mask |= SETTING_START_MODE;
f757855e
LP
1356 }
1357
1358 /* Load all settings from .nspawn files */
1359 if (mask_no_settings)
1360 arg_settings_mask = 0;
1361
1362 /* Don't load any settings from .nspawn files */
1363 if (mask_all_settings)
1364 arg_settings_mask = _SETTINGS_MASK_ALL;
1365
520e0d54 1366 arg_caps_retain = (arg_caps_retain | plus | (arg_private_network ? 1ULL << CAP_NET_ADMIN : 0)) & ~minus;
f757855e 1367
399e391f
ZJS
1368 r = cg_unified_flush();
1369 if (r < 0)
1370 return log_error_errno(r, "Failed to determine whether the unified cgroups hierarchy is used: %m");
1371
6aadfa4c
ILG
1372 e = getenv("SYSTEMD_NSPAWN_CONTAINER_SERVICE");
1373 if (e)
1374 arg_container_service_name = e;
1375
5a8ff0e6
CB
1376 r = getenv_bool("SYSTEMD_NSPAWN_USE_CGNS");
1377 if (r < 0)
1378 arg_use_cgns = cg_ns_supported();
1379 else
1380 arg_use_cgns = r;
1381
86c0dd4a
LP
1382 r = custom_mount_check_all();
1383 if (r < 0)
1384 return r;
1385
f757855e
LP
1386 return 1;
1387}
1388
1389static int verify_arguments(void) {
4f086aab
SU
1390 if (arg_userns_mode != USER_NAMESPACE_NO && (arg_mount_settings & MOUNT_APPLY_APIVFS_NETNS) && !arg_private_network) {
1391 log_error("Invalid namespacing settings. Mounting sysfs with --private-users requires --private-network.");
1392 return -EINVAL;
1393 }
1394
1395 if (arg_userns_mode != USER_NAMESPACE_NO && !(arg_mount_settings & MOUNT_APPLY_APIVFS_RO)) {
1396 log_error("Cannot combine --private-users with read-write mounts.");
1397 return -EINVAL;
1398 }
f757855e
LP
1399
1400 if (arg_volatile_mode != VOLATILE_NO && arg_read_only) {
4d9f07b4
LP
1401 log_error("Cannot combine --read-only with --volatile. Note that --volatile already implies a read-only base hierarchy.");
1402 return -EINVAL;
1403 }
1404
6d0b55c2
LP
1405 if (arg_expose_ports && !arg_private_network) {
1406 log_error("Cannot use --port= without private networking.");
1407 return -EINVAL;
1408 }
1409
349cc4a5 1410#if ! HAVE_LIBIPTC
1c1ea217
EV
1411 if (arg_expose_ports) {
1412 log_error("--port= is not supported, compiled without libiptc support.");
1413 return -EOPNOTSUPP;
1414 }
1415#endif
1416
7732f92b 1417 if (arg_start_mode == START_BOOT && arg_kill_signal <= 0)
c6c8f6e2
LP
1418 arg_kill_signal = SIGRTMIN+3;
1419
f757855e 1420 return 0;
88213476
LP
1421}
1422
03cfe0d5
LP
1423static int userns_lchown(const char *p, uid_t uid, gid_t gid) {
1424 assert(p);
1425
0de7acce 1426 if (arg_userns_mode == USER_NAMESPACE_NO)
03cfe0d5
LP
1427 return 0;
1428
1429 if (uid == UID_INVALID && gid == GID_INVALID)
1430 return 0;
1431
1432 if (uid != UID_INVALID) {
1433 uid += arg_uid_shift;
1434
1435 if (uid < arg_uid_shift || uid >= arg_uid_shift + arg_uid_range)
1436 return -EOVERFLOW;
1437 }
1438
1439 if (gid != GID_INVALID) {
1440 gid += (gid_t) arg_uid_shift;
1441
1442 if (gid < (gid_t) arg_uid_shift || gid >= (gid_t) (arg_uid_shift + arg_uid_range))
1443 return -EOVERFLOW;
1444 }
1445
1446 if (lchown(p, uid, gid) < 0)
1447 return -errno;
b12afc8c
LP
1448
1449 return 0;
1450}
1451
03cfe0d5
LP
1452static int userns_mkdir(const char *root, const char *path, mode_t mode, uid_t uid, gid_t gid) {
1453 const char *q;
dae8b82e 1454 int r;
03cfe0d5
LP
1455
1456 q = prefix_roota(root, path);
dae8b82e
ZJS
1457 r = mkdir_errno_wrapper(q, mode);
1458 if (r == -EEXIST)
1459 return 0;
1460 if (r < 0)
1461 return r;
03cfe0d5
LP
1462
1463 return userns_lchown(q, uid, gid);
1464}
1465
1688841f
LP
1466static const char *timezone_from_path(const char *path) {
1467 const char *z;
1468
1469 z = path_startswith(path, "../usr/share/zoneinfo/");
1470 if (z)
1471 return z;
1472
1473 z = path_startswith(path, "/usr/share/zoneinfo/");
1474 if (z)
1475 return z;
1476
1477 return NULL;
1478}
1479
e58a1277 1480static int setup_timezone(const char *dest) {
1688841f
LP
1481 _cleanup_free_ char *p = NULL, *etc = NULL;
1482 const char *where, *check;
1483 TimezoneMode m;
d4036145 1484 int r;
f8440af5 1485
e58a1277
LP
1486 assert(dest);
1487
1688841f 1488 if (IN_SET(arg_timezone, TIMEZONE_AUTO, TIMEZONE_SYMLINK)) {
1688841f
LP
1489 r = readlink_malloc("/etc/localtime", &p);
1490 if (r == -ENOENT && arg_timezone == TIMEZONE_AUTO)
1491 m = arg_read_only && arg_volatile_mode != VOLATILE_YES ? TIMEZONE_OFF : TIMEZONE_DELETE;
1492 else if (r == -EINVAL && arg_timezone == TIMEZONE_AUTO) /* regular file? */
1493 m = arg_read_only && arg_volatile_mode != VOLATILE_YES ? TIMEZONE_BIND : TIMEZONE_COPY;
1494 else if (r < 0) {
1495 log_warning_errno(r, "Failed to read host's /etc/localtime symlink, not updating container timezone: %m");
1496 /* To handle warning, delete /etc/localtime and replace it with a symbolic link to a time zone data
1497 * file.
1498 *
1499 * Example:
1500 * ln -s /usr/share/zoneinfo/UTC /etc/localtime
1501 */
1502 return 0;
1503 } else if (arg_timezone == TIMEZONE_AUTO)
1504 m = arg_read_only && arg_volatile_mode != VOLATILE_YES ? TIMEZONE_BIND : TIMEZONE_SYMLINK;
1505 else
1506 m = arg_timezone;
1507 } else
1508 m = arg_timezone;
1509
1510 if (m == TIMEZONE_OFF)
1511 return 0;
1512
1513 r = chase_symlinks("/etc", dest, CHASE_PREFIX_ROOT, &etc);
d4036145 1514 if (r < 0) {
1688841f 1515 log_warning_errno(r, "Failed to resolve /etc path in container, ignoring: %m");
d4036145
LP
1516 return 0;
1517 }
1518
1688841f
LP
1519 where = strjoina(etc, "/localtime");
1520
1521 switch (m) {
1522
1523 case TIMEZONE_DELETE:
1524 if (unlink(where) < 0)
1525 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno, "Failed to remove '%s', ignoring: %m", where);
1526
d4036145 1527 return 0;
d4036145 1528
1688841f
LP
1529 case TIMEZONE_SYMLINK: {
1530 _cleanup_free_ char *q = NULL;
1531 const char *z, *what;
4d1c38b8 1532
1688841f
LP
1533 z = timezone_from_path(p);
1534 if (!z) {
1535 log_warning("/etc/localtime does not point into /usr/share/zoneinfo/, not updating container timezone.");
d4036145 1536 return 0;
1688841f 1537 }
d4036145 1538
1688841f
LP
1539 r = readlink_malloc(where, &q);
1540 if (r >= 0 && streq_ptr(timezone_from_path(q), z))
1541 return 0; /* Already pointing to the right place? Then do nothing .. */
1542
1543 check = strjoina(dest, "/usr/share/zoneinfo/", z);
1544 r = chase_symlinks(check, dest, 0, NULL);
1545 if (r < 0)
1546 log_debug_errno(r, "Timezone %s does not exist (or is not accessible) in container, not creating symlink: %m", z);
1547 else {
1548 if (unlink(where) < 0 && errno != ENOENT) {
1549 log_full_errno(IN_SET(errno, EROFS, EACCES, EPERM) ? LOG_DEBUG : LOG_WARNING, /* Don't complain on read-only images */
1550 errno, "Failed to remove existing timezone info %s in container, ignoring: %m", where);
1551 return 0;
1552 }
1553
1554 what = strjoina("../usr/share/zoneinfo/", z);
1555 if (symlink(what, where) < 0) {
1556 log_full_errno(IN_SET(errno, EROFS, EACCES, EPERM) ? LOG_DEBUG : LOG_WARNING,
1557 errno, "Failed to correct timezone of container, ignoring: %m");
1558 return 0;
1559 }
1560
1561 break;
1562 }
1563
1564 _fallthrough_;
d4036145 1565 }
68fb0892 1566
1688841f
LP
1567 case TIMEZONE_BIND: {
1568 _cleanup_free_ char *resolved = NULL;
1569 int found;
1570
1571 found = chase_symlinks(where, dest, CHASE_NONEXISTENT, &resolved);
1572 if (found < 0) {
1573 log_warning_errno(found, "Failed to resolve /etc/localtime path in container, ignoring: %m");
1574 return 0;
1575 }
1576
1577 if (found == 0) /* missing? */
1578 (void) touch(resolved);
1579
1580 r = mount_verbose(LOG_WARNING, "/etc/localtime", resolved, NULL, MS_BIND, NULL);
1581 if (r >= 0)
1582 return mount_verbose(LOG_ERR, NULL, resolved, NULL, MS_BIND|MS_REMOUNT|MS_RDONLY|MS_NOSUID|MS_NODEV, NULL);
1583
1584 _fallthrough_;
79d80fc1 1585 }
4d9f07b4 1586
1688841f
LP
1587 case TIMEZONE_COPY:
1588 /* If mounting failed, try to copy */
1589 r = copy_file_atomic("/etc/localtime", where, 0644, 0, COPY_REFLINK|COPY_REPLACE);
1590 if (r < 0) {
1591 log_full_errno(IN_SET(r, -EROFS, -EACCES, -EPERM) ? LOG_DEBUG : LOG_WARNING, r,
1592 "Failed to copy /etc/localtime to %s, ignoring: %m", where);
1593 return 0;
1594 }
1595
1596 break;
1597
1598 default:
1599 assert_not_reached("unexpected mode");
d4036145 1600 }
e58a1277 1601
1688841f 1602 /* Fix permissions of the symlink or file copy we just created */
03cfe0d5
LP
1603 r = userns_lchown(where, 0, 0);
1604 if (r < 0)
1688841f 1605 log_warning_errno(r, "Failed to chown /etc/localtime, ignoring: %m");
03cfe0d5 1606
e58a1277 1607 return 0;
88213476
LP
1608}
1609
09d423e9
LP
1610static int have_resolv_conf(const char *path) {
1611 assert(path);
1612
1613 if (access(path, F_OK) < 0) {
1614 if (errno == ENOENT)
1615 return 0;
1616
1617 return log_debug_errno(errno, "Failed to determine whether '%s' is available: %m", path);
1618 }
1619
1620 return 1;
1621}
1622
7357272e 1623static int resolved_listening(void) {
b8ea7a6e 1624 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
b053cd5f 1625 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
7357272e 1626 _cleanup_free_ char *dns_stub_listener_mode = NULL;
b053cd5f
LP
1627 int r;
1628
7357272e 1629 /* Check if resolved is listening */
b053cd5f
LP
1630
1631 r = sd_bus_open_system(&bus);
1632 if (r < 0)
b8ea7a6e 1633 return log_debug_errno(r, "Failed to open system bus: %m");
b053cd5f 1634
7357272e 1635 r = bus_name_has_owner(bus, "org.freedesktop.resolve1", NULL);
b8ea7a6e
LP
1636 if (r < 0)
1637 return log_debug_errno(r, "Failed to check whether the 'org.freedesktop.resolve1' bus name is taken: %m");
1638 if (r == 0)
1639 return 0;
7357272e
DM
1640
1641 r = sd_bus_get_property_string(bus,
1642 "org.freedesktop.resolve1",
1643 "/org/freedesktop/resolve1",
1644 "org.freedesktop.resolve1.Manager",
1645 "DNSStubListener",
b8ea7a6e 1646 &error,
7357272e
DM
1647 &dns_stub_listener_mode);
1648 if (r < 0)
b8ea7a6e 1649 return log_debug_errno(r, "Failed to query DNSStubListener property: %s", bus_error_message(&error, r));
7357272e
DM
1650
1651 return STR_IN_SET(dns_stub_listener_mode, "udp", "yes");
b053cd5f
LP
1652}
1653
2547bb41 1654static int setup_resolv_conf(const char *dest) {
09d423e9
LP
1655 _cleanup_free_ char *etc = NULL;
1656 const char *where, *what;
1657 ResolvConfMode m;
1658 int r;
2547bb41
LP
1659
1660 assert(dest);
1661
09d423e9
LP
1662 if (arg_resolv_conf == RESOLV_CONF_AUTO) {
1663 if (arg_private_network)
1664 m = RESOLV_CONF_OFF;
1665 else if (have_resolv_conf(STATIC_RESOLV_CONF) > 0 && resolved_listening() > 0)
27b620b7 1666 m = arg_read_only && arg_volatile_mode != VOLATILE_YES ? RESOLV_CONF_BIND_STATIC : RESOLV_CONF_COPY_STATIC;
09d423e9
LP
1667 else if (have_resolv_conf("/etc/resolv.conf") > 0)
1668 m = arg_read_only && arg_volatile_mode != VOLATILE_YES ? RESOLV_CONF_BIND_HOST : RESOLV_CONF_COPY_HOST;
1669 else
1670 m = arg_read_only && arg_volatile_mode != VOLATILE_YES ? RESOLV_CONF_OFF : RESOLV_CONF_DELETE;
1671 } else
1672 m = arg_resolv_conf;
1673
1674 if (m == RESOLV_CONF_OFF)
2547bb41
LP
1675 return 0;
1676
87447ae4
LP
1677 r = chase_symlinks("/etc", dest, CHASE_PREFIX_ROOT, &etc);
1678 if (r < 0) {
1679 log_warning_errno(r, "Failed to resolve /etc path in container, ignoring: %m");
1680 return 0;
1681 }
1682
1683 where = strjoina(etc, "/resolv.conf");
09d423e9
LP
1684
1685 if (m == RESOLV_CONF_DELETE) {
1686 if (unlink(where) < 0)
1687 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno, "Failed to remove '%s', ignoring: %m", where);
1688
87447ae4
LP
1689 return 0;
1690 }
79d80fc1 1691
09d423e9
LP
1692 if (IN_SET(m, RESOLV_CONF_BIND_STATIC, RESOLV_CONF_COPY_STATIC))
1693 what = STATIC_RESOLV_CONF;
1694 else
1695 what = "/etc/resolv.conf";
87447ae4 1696
09d423e9
LP
1697 if (IN_SET(m, RESOLV_CONF_BIND_HOST, RESOLV_CONF_BIND_STATIC)) {
1698 _cleanup_free_ char *resolved = NULL;
1699 int found;
1700
1701 found = chase_symlinks(where, dest, CHASE_NONEXISTENT, &resolved);
1702 if (found < 0) {
1703 log_warning_errno(found, "Failed to resolve /etc/resolv.conf path in container, ignoring: %m");
1704 return 0;
1705 }
3539724c 1706
87447ae4
LP
1707 if (found == 0) /* missing? */
1708 (void) touch(resolved);
5367354d 1709
09d423e9 1710 r = mount_verbose(LOG_WARNING, what, resolved, NULL, MS_BIND, NULL);
60e76d48 1711 if (r >= 0)
87447ae4 1712 return mount_verbose(LOG_ERR, NULL, resolved, NULL, MS_BIND|MS_REMOUNT|MS_RDONLY|MS_NOSUID|MS_NODEV, NULL);
3539724c
LP
1713 }
1714
1715 /* If that didn't work, let's copy the file */
09d423e9 1716 r = copy_file(what, where, O_TRUNC|O_NOFOLLOW, 0644, 0, COPY_REFLINK);
79d80fc1 1717 if (r < 0) {
3539724c
LP
1718 /* If the file already exists as symlink, let's suppress the warning, under the assumption that
1719 * resolved or something similar runs inside and the symlink points there.
68a313c5 1720 *
3539724c 1721 * If the disk image is read-only, there's also no point in complaining.
68a313c5 1722 */
09d423e9 1723 log_full_errno(!IN_SET(RESOLV_CONF_COPY_HOST, RESOLV_CONF_COPY_STATIC) && IN_SET(r, -ELOOP, -EROFS, -EACCES, -EPERM) ? LOG_DEBUG : LOG_WARNING, r,
3539724c 1724 "Failed to copy /etc/resolv.conf to %s, ignoring: %m", where);
79d80fc1
TG
1725 return 0;
1726 }
2547bb41 1727
03cfe0d5
LP
1728 r = userns_lchown(where, 0, 0);
1729 if (r < 0)
3539724c 1730 log_warning_errno(r, "Failed to chown /etc/resolv.conf, ignoring: %m");
03cfe0d5 1731
2547bb41
LP
1732 return 0;
1733}
1734
1e4f1671 1735static int setup_boot_id(void) {
cdde6ba6
LP
1736 _cleanup_(unlink_and_freep) char *from = NULL;
1737 _cleanup_free_ char *path = NULL;
3bbaff3e 1738 sd_id128_t rnd = SD_ID128_NULL;
cdde6ba6 1739 const char *to;
04bc4a3f
LP
1740 int r;
1741
04bc4a3f
LP
1742 /* Generate a new randomized boot ID, so that each boot-up of
1743 * the container gets a new one */
1744
cdde6ba6
LP
1745 r = tempfn_random_child(NULL, "proc-sys-kernel-random-boot-id", &path);
1746 if (r < 0)
1747 return log_error_errno(r, "Failed to generate random boot ID path: %m");
04bc4a3f
LP
1748
1749 r = sd_id128_randomize(&rnd);
f647962d
MS
1750 if (r < 0)
1751 return log_error_errno(r, "Failed to generate random boot id: %m");
04bc4a3f 1752
cdde6ba6 1753 r = id128_write(path, ID128_UUID, rnd, false);
f647962d
MS
1754 if (r < 0)
1755 return log_error_errno(r, "Failed to write boot id: %m");
04bc4a3f 1756
cdde6ba6
LP
1757 from = TAKE_PTR(path);
1758 to = "/proc/sys/kernel/random/boot_id";
1759
60e76d48 1760 r = mount_verbose(LOG_ERR, from, to, NULL, MS_BIND, NULL);
cdde6ba6
LP
1761 if (r < 0)
1762 return r;
04bc4a3f 1763
cdde6ba6 1764 return mount_verbose(LOG_ERR, NULL, to, NULL, MS_BIND|MS_REMOUNT|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV, NULL);
04bc4a3f
LP
1765}
1766
e58a1277 1767static int copy_devnodes(const char *dest) {
88213476
LP
1768 static const char devnodes[] =
1769 "null\0"
1770 "zero\0"
1771 "full\0"
1772 "random\0"
1773 "urandom\0"
85614d66
TG
1774 "tty\0"
1775 "net/tun\0";
88213476
LP
1776
1777 const char *d;
e58a1277 1778 int r = 0;
7fd1b19b 1779 _cleanup_umask_ mode_t u;
a258bf26
LP
1780
1781 assert(dest);
124640f1
LP
1782
1783 u = umask(0000);
88213476 1784
03cfe0d5
LP
1785 /* Create /dev/net, so that we can create /dev/net/tun in it */
1786 if (userns_mkdir(dest, "/dev/net", 0755, 0, 0) < 0)
1787 return log_error_errno(r, "Failed to create /dev/net directory: %m");
1788
88213476 1789 NULSTR_FOREACH(d, devnodes) {
7fd1b19b 1790 _cleanup_free_ char *from = NULL, *to = NULL;
7f112f50 1791 struct stat st;
88213476 1792
7f112f50 1793 from = strappend("/dev/", d);
8967f291
LP
1794 if (!from)
1795 return log_oom();
1796
03cfe0d5 1797 to = prefix_root(dest, from);
8967f291
LP
1798 if (!to)
1799 return log_oom();
88213476
LP
1800
1801 if (stat(from, &st) < 0) {
1802
4a62c710
MS
1803 if (errno != ENOENT)
1804 return log_error_errno(errno, "Failed to stat %s: %m", from);
88213476 1805
a258bf26 1806 } else if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode)) {
88213476 1807
03cfe0d5 1808 log_error("%s is not a char or block device, cannot copy.", from);
7f112f50 1809 return -EIO;
a258bf26 1810
85614d66 1811 } else {
8dfce114
LP
1812 _cleanup_free_ char *sl = NULL, *prefixed = NULL, *dn = NULL, *t = NULL;
1813
81f5049b 1814 if (mknod(to, st.st_mode, st.st_rdev) < 0) {
8dbf71ec 1815 /* Explicitly warn the user when /dev is already populated. */
41eb4362 1816 if (errno == EEXIST)
8dbf71ec 1817 log_notice("%s/dev is pre-mounted and pre-populated. If a pre-mounted /dev is provided it needs to be an unpopulated file system.", dest);
81f5049b
AC
1818 if (errno != EPERM)
1819 return log_error_errno(errno, "mknod(%s) failed: %m", to);
1820
8dfce114 1821 /* Some systems abusively restrict mknod but allow bind mounts. */
81f5049b
AC
1822 r = touch(to);
1823 if (r < 0)
1824 return log_error_errno(r, "touch (%s) failed: %m", to);
60e76d48
ZJS
1825 r = mount_verbose(LOG_DEBUG, from, to, NULL, MS_BIND, NULL);
1826 if (r < 0)
1827 return log_error_errno(r, "Both mknod and bind mount (%s) failed: %m", to);
81f5049b 1828 }
6278cf60 1829
03cfe0d5
LP
1830 r = userns_lchown(to, 0, 0);
1831 if (r < 0)
1832 return log_error_errno(r, "chown() of device node %s failed: %m", to);
8dfce114
LP
1833
1834 dn = strjoin("/dev/", S_ISCHR(st.st_mode) ? "char" : "block");
1835 if (!dn)
1836 return log_oom();
1837
1838 r = userns_mkdir(dest, dn, 0755, 0, 0);
1839 if (r < 0)
1840 return log_error_errno(r, "Failed to create '%s': %m", dn);
1841
1842 if (asprintf(&sl, "%s/%u:%u", dn, major(st.st_rdev), minor(st.st_rdev)) < 0)
1843 return log_oom();
1844
1845 prefixed = prefix_root(dest, sl);
1846 if (!prefixed)
1847 return log_oom();
1848
1849 t = strjoin("../", d);
1850 if (!t)
1851 return log_oom();
1852
1853 if (symlink(t, prefixed) < 0)
1854 log_debug_errno(errno, "Failed to symlink '%s' to '%s': %m", t, prefixed);
88213476 1855 }
88213476
LP
1856 }
1857
e58a1277
LP
1858 return r;
1859}
88213476 1860
03cfe0d5
LP
1861static int setup_pts(const char *dest) {
1862 _cleanup_free_ char *options = NULL;
1863 const char *p;
709f6e46 1864 int r;
03cfe0d5 1865
349cc4a5 1866#if HAVE_SELINUX
03cfe0d5
LP
1867 if (arg_selinux_apifs_context)
1868 (void) asprintf(&options,
3dce8915 1869 "newinstance,ptmxmode=0666,mode=620,gid=" GID_FMT ",context=\"%s\"",
03cfe0d5
LP
1870 arg_uid_shift + TTY_GID,
1871 arg_selinux_apifs_context);
1872 else
1873#endif
1874 (void) asprintf(&options,
3dce8915 1875 "newinstance,ptmxmode=0666,mode=620,gid=" GID_FMT,
03cfe0d5 1876 arg_uid_shift + TTY_GID);
f2d88580 1877
03cfe0d5 1878 if (!options)
f2d88580
LP
1879 return log_oom();
1880
03cfe0d5 1881 /* Mount /dev/pts itself */
cc9fce65 1882 p = prefix_roota(dest, "/dev/pts");
dae8b82e
ZJS
1883 r = mkdir_errno_wrapper(p, 0755);
1884 if (r < 0)
1885 return log_error_errno(r, "Failed to create /dev/pts: %m");
1886
60e76d48
ZJS
1887 r = mount_verbose(LOG_ERR, "devpts", p, "devpts", MS_NOSUID|MS_NOEXEC, options);
1888 if (r < 0)
1889 return r;
709f6e46
MS
1890 r = userns_lchown(p, 0, 0);
1891 if (r < 0)
1892 return log_error_errno(r, "Failed to chown /dev/pts: %m");
03cfe0d5
LP
1893
1894 /* Create /dev/ptmx symlink */
1895 p = prefix_roota(dest, "/dev/ptmx");
4a62c710
MS
1896 if (symlink("pts/ptmx", p) < 0)
1897 return log_error_errno(errno, "Failed to create /dev/ptmx symlink: %m");
709f6e46
MS
1898 r = userns_lchown(p, 0, 0);
1899 if (r < 0)
1900 return log_error_errno(r, "Failed to chown /dev/ptmx: %m");
f2d88580 1901
03cfe0d5
LP
1902 /* And fix /dev/pts/ptmx ownership */
1903 p = prefix_roota(dest, "/dev/pts/ptmx");
709f6e46
MS
1904 r = userns_lchown(p, 0, 0);
1905 if (r < 0)
1906 return log_error_errno(r, "Failed to chown /dev/pts/ptmx: %m");
6278cf60 1907
f2d88580
LP
1908 return 0;
1909}
1910
e58a1277 1911static int setup_dev_console(const char *dest, const char *console) {
eb0f0863
LP
1912 _cleanup_umask_ mode_t u;
1913 const char *to;
e58a1277 1914 int r;
e58a1277
LP
1915
1916 assert(dest);
1917 assert(console);
1918
1919 u = umask(0000);
1920
03cfe0d5 1921 r = chmod_and_chown(console, 0600, arg_uid_shift, arg_uid_shift);
f647962d
MS
1922 if (r < 0)
1923 return log_error_errno(r, "Failed to correct access mode for TTY: %m");
88213476 1924
a258bf26
LP
1925 /* We need to bind mount the right tty to /dev/console since
1926 * ptys can only exist on pts file systems. To have something
81f5049b 1927 * to bind mount things on we create a empty regular file. */
a258bf26 1928
03cfe0d5 1929 to = prefix_roota(dest, "/dev/console");
81f5049b
AC
1930 r = touch(to);
1931 if (r < 0)
1932 return log_error_errno(r, "touch() for /dev/console failed: %m");
a258bf26 1933
60e76d48 1934 return mount_verbose(LOG_ERR, console, to, NULL, MS_BIND, NULL);
e58a1277
LP
1935}
1936
8e5430c4
LP
1937static int setup_keyring(void) {
1938 key_serial_t keyring;
1939
1940 /* Allocate a new session keyring for the container. This makes sure the keyring of the session systemd-nspawn
1941 * was invoked from doesn't leak into the container. Note that by default we block keyctl() and request_key()
1942 * anyway via seccomp so doing this operation isn't strictly necessary, but in case people explicitly whitelist
1943 * these system calls let's make sure we don't leak anything into the container. */
1944
1945 keyring = keyctl(KEYCTL_JOIN_SESSION_KEYRING, 0, 0, 0, 0);
1946 if (keyring == -1) {
1947 if (errno == ENOSYS)
1948 log_debug_errno(errno, "Kernel keyring not supported, ignoring.");
1949 else if (IN_SET(errno, EACCES, EPERM))
1950 log_debug_errno(errno, "Kernel keyring access prohibited, ignoring.");
1951 else
1952 return log_error_errno(errno, "Setting up kernel keyring failed: %m");
1953 }
1954
1955 return 0;
1956}
1957
1e4f1671 1958static int setup_kmsg(int kmsg_socket) {
9ec5a93c
LP
1959 _cleanup_(unlink_and_freep) char *from = NULL;
1960 _cleanup_free_ char *fifo = NULL;
1961 _cleanup_close_ int fd = -1;
7fd1b19b 1962 _cleanup_umask_ mode_t u;
9ec5a93c
LP
1963 const char *to;
1964 int r;
e58a1277 1965
e58a1277 1966 assert(kmsg_socket >= 0);
a258bf26 1967
e58a1277 1968 u = umask(0000);
a258bf26 1969
9ec5a93c
LP
1970 /* We create the kmsg FIFO as as temporary file in /tmp, but immediately delete it after bind mounting it to
1971 * /proc/kmsg. While FIFOs on the reading side behave very similar to /proc/kmsg, their writing side behaves
1972 * differently from /dev/kmsg in that writing blocks when nothing is reading. In order to avoid any problems
1973 * with containers deadlocking due to this we simply make /dev/kmsg unavailable to the container. */
1974
1975 r = tempfn_random_child(NULL, "proc-kmsg", &fifo);
1976 if (r < 0)
1977 return log_error_errno(r, "Failed to generate kmsg path: %m");
e58a1277 1978
9ec5a93c 1979 if (mkfifo(fifo, 0600) < 0)
03cfe0d5 1980 return log_error_errno(errno, "mkfifo() for /run/kmsg failed: %m");
9ec5a93c
LP
1981
1982 from = TAKE_PTR(fifo);
1983 to = "/proc/kmsg";
1984
60e76d48
ZJS
1985 r = mount_verbose(LOG_ERR, from, to, NULL, MS_BIND, NULL);
1986 if (r < 0)
1987 return r;
e58a1277 1988
669fc4e5 1989 fd = open(from, O_RDWR|O_NONBLOCK|O_CLOEXEC);
4a62c710
MS
1990 if (fd < 0)
1991 return log_error_errno(errno, "Failed to open fifo: %m");
e58a1277 1992
9ec5a93c 1993 /* Store away the fd in the socket, so that it stays open as long as we run the child */
3ee897d6 1994 r = send_one_fd(kmsg_socket, fd, 0);
d9603714
DH
1995 if (r < 0)
1996 return log_error_errno(r, "Failed to send FIFO fd: %m");
a258bf26 1997
25ea79fe 1998 return 0;
88213476
LP
1999}
2000
1c4baffc 2001static int on_address_change(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) {
6d0b55c2
LP
2002 union in_addr_union *exposed = userdata;
2003
2004 assert(rtnl);
2005 assert(m);
2006 assert(exposed);
2007
7a8f6325 2008 expose_port_execute(rtnl, arg_expose_ports, exposed);
6d0b55c2
LP
2009 return 0;
2010}
2011
3a74cea5 2012static int setup_hostname(void) {
c818eef1 2013 int r;
3a74cea5 2014
0c582db0 2015 if ((arg_clone_ns_flags & CLONE_NEWUTS) == 0)
eb91eb18
LP
2016 return 0;
2017
c818eef1
LP
2018 r = sethostname_idempotent(arg_hostname ?: arg_machine);
2019 if (r < 0)
2020 return log_error_errno(r, "Failed to set hostname: %m");
3a74cea5 2021
7027ff61 2022 return 0;
3a74cea5
LP
2023}
2024
57fb9fb5 2025static int setup_journal(const char *directory) {
e01ff70a 2026 sd_id128_t this_id;
0f5e1382 2027 _cleanup_free_ char *d = NULL;
e01ff70a 2028 const char *p, *q;
8054d749 2029 bool try;
369ca6da 2030 char id[33], *dirname;
57fb9fb5
LP
2031 int r;
2032
df9a75e4
LP
2033 /* Don't link journals in ephemeral mode */
2034 if (arg_ephemeral)
2035 return 0;
2036
8054d749
LP
2037 if (arg_link_journal == LINK_NO)
2038 return 0;
2039
2040 try = arg_link_journal_try || arg_link_journal == LINK_AUTO;
2041
4d680aee 2042 r = sd_id128_get_machine(&this_id);
f647962d
MS
2043 if (r < 0)
2044 return log_error_errno(r, "Failed to retrieve machine ID: %m");
4d680aee 2045
e01ff70a 2046 if (sd_id128_equal(arg_uuid, this_id)) {
8054d749 2047 log_full(try ? LOG_WARNING : LOG_ERR,
e192a281 2048 "Host and machine ids are equal (%s): refusing to link journals", sd_id128_to_string(arg_uuid, id));
8054d749 2049 if (try)
4d680aee 2050 return 0;
df9a75e4 2051 return -EEXIST;
4d680aee
ZJS
2052 }
2053
369ca6da
ZJS
2054 FOREACH_STRING(dirname, "/var", "/var/log", "/var/log/journal") {
2055 r = userns_mkdir(directory, dirname, 0755, 0, 0);
2056 if (r < 0) {
2057 bool ignore = r == -EROFS && try;
2058 log_full_errno(ignore ? LOG_DEBUG : LOG_ERR, r,
2059 "Failed to create %s%s: %m", dirname, ignore ? ", ignoring" : "");
2060 return ignore ? 0 : r;
2061 }
2062 }
03cfe0d5 2063
e01ff70a
MS
2064 (void) sd_id128_to_string(arg_uuid, id);
2065
03cfe0d5
LP
2066 p = strjoina("/var/log/journal/", id);
2067 q = prefix_roota(directory, p);
27407a01 2068
e1873695 2069 if (path_is_mount_point(p, NULL, 0) > 0) {
8054d749
LP
2070 if (try)
2071 return 0;
27407a01 2072
8054d749
LP
2073 log_error("%s: already a mount point, refusing to use for journal", p);
2074 return -EEXIST;
57fb9fb5
LP
2075 }
2076
e1873695 2077 if (path_is_mount_point(q, NULL, 0) > 0) {
8054d749
LP
2078 if (try)
2079 return 0;
57fb9fb5 2080
8054d749
LP
2081 log_error("%s: already a mount point, refusing to use for journal", q);
2082 return -EEXIST;
57fb9fb5
LP
2083 }
2084
2085 r = readlink_and_make_absolute(p, &d);
2086 if (r >= 0) {
3742095b 2087 if (IN_SET(arg_link_journal, LINK_GUEST, LINK_AUTO) &&
57fb9fb5
LP
2088 path_equal(d, q)) {
2089
03cfe0d5 2090 r = userns_mkdir(directory, p, 0755, 0, 0);
27407a01 2091 if (r < 0)
709f6e46 2092 log_warning_errno(r, "Failed to create directory %s: %m", q);
27407a01 2093 return 0;
57fb9fb5
LP
2094 }
2095
4a62c710
MS
2096 if (unlink(p) < 0)
2097 return log_error_errno(errno, "Failed to remove symlink %s: %m", p);
57fb9fb5
LP
2098 } else if (r == -EINVAL) {
2099
2100 if (arg_link_journal == LINK_GUEST &&
2101 rmdir(p) < 0) {
2102
27407a01
ZJS
2103 if (errno == ENOTDIR) {
2104 log_error("%s already exists and is neither a symlink nor a directory", p);
2105 return r;
4314d33f
MS
2106 } else
2107 return log_error_errno(errno, "Failed to remove %s: %m", p);
57fb9fb5 2108 }
4314d33f
MS
2109 } else if (r != -ENOENT)
2110 return log_error_errno(r, "readlink(%s) failed: %m", p);
57fb9fb5
LP
2111
2112 if (arg_link_journal == LINK_GUEST) {
2113
2114 if (symlink(q, p) < 0) {
8054d749 2115 if (try) {
56f64d95 2116 log_debug_errno(errno, "Failed to symlink %s to %s, skipping journal setup: %m", q, p);
574edc90 2117 return 0;
4314d33f
MS
2118 } else
2119 return log_error_errno(errno, "Failed to symlink %s to %s: %m", q, p);
57fb9fb5
LP
2120 }
2121
03cfe0d5 2122 r = userns_mkdir(directory, p, 0755, 0, 0);
27407a01 2123 if (r < 0)
709f6e46 2124 log_warning_errno(r, "Failed to create directory %s: %m", q);
27407a01 2125 return 0;
57fb9fb5
LP
2126 }
2127
2128 if (arg_link_journal == LINK_HOST) {
ccddd104 2129 /* don't create parents here — if the host doesn't have
574edc90 2130 * permanent journal set up, don't force it here */
ba8e6c4d 2131
dae8b82e
ZJS
2132 r = mkdir_errno_wrapper(p, 0755);
2133 if (r < 0 && r != -EEXIST) {
8054d749 2134 if (try) {
dae8b82e 2135 log_debug_errno(r, "Failed to create %s, skipping journal setup: %m", p);
574edc90 2136 return 0;
4314d33f 2137 } else
dae8b82e 2138 return log_error_errno(r, "Failed to create %s: %m", p);
57fb9fb5
LP
2139 }
2140
27407a01
ZJS
2141 } else if (access(p, F_OK) < 0)
2142 return 0;
57fb9fb5 2143
cdb2b9d0
LP
2144 if (dir_is_empty(q) == 0)
2145 log_warning("%s is not empty, proceeding anyway.", q);
2146
03cfe0d5 2147 r = userns_mkdir(directory, p, 0755, 0, 0);
709f6e46
MS
2148 if (r < 0)
2149 return log_error_errno(r, "Failed to create %s: %m", q);
57fb9fb5 2150
60e76d48
ZJS
2151 r = mount_verbose(LOG_DEBUG, p, q, NULL, MS_BIND, NULL);
2152 if (r < 0)
4a62c710 2153 return log_error_errno(errno, "Failed to bind mount journal from host into guest: %m");
57fb9fb5 2154
27407a01 2155 return 0;
57fb9fb5
LP
2156}
2157
88213476 2158static int drop_capabilities(void) {
520e0d54 2159 return capability_bounding_set_drop(arg_caps_retain, false);
88213476
LP
2160}
2161
db999e0f
LP
2162static int reset_audit_loginuid(void) {
2163 _cleanup_free_ char *p = NULL;
2164 int r;
2165
0c582db0 2166 if ((arg_clone_ns_flags & CLONE_NEWPID) == 0)
db999e0f
LP
2167 return 0;
2168
2169 r = read_one_line_file("/proc/self/loginuid", &p);
13e8ceb8 2170 if (r == -ENOENT)
db999e0f 2171 return 0;
f647962d
MS
2172 if (r < 0)
2173 return log_error_errno(r, "Failed to read /proc/self/loginuid: %m");
db999e0f
LP
2174
2175 /* Already reset? */
2176 if (streq(p, "4294967295"))
2177 return 0;
2178
57512c89 2179 r = write_string_file("/proc/self/loginuid", "4294967295", WRITE_STRING_FILE_DISABLE_BUFFER);
db999e0f 2180 if (r < 0) {
10a87006
LP
2181 log_error_errno(r,
2182 "Failed to reset audit login UID. This probably means that your kernel is too\n"
2183 "old and you have audit enabled. Note that the auditing subsystem is known to\n"
2184 "be incompatible with containers on old kernels. Please make sure to upgrade\n"
2185 "your kernel or to off auditing with 'audit=0' on the kernel command line before\n"
2186 "using systemd-nspawn. Sleeping for 5s... (%m)");
77b6e194 2187
db999e0f 2188 sleep(5);
77b6e194 2189 }
db999e0f
LP
2190
2191 return 0;
77b6e194
LP
2192}
2193
785890ac
LP
2194static int setup_propagate(const char *root) {
2195 const char *p, *q;
709f6e46 2196 int r;
785890ac
LP
2197
2198 (void) mkdir_p("/run/systemd/nspawn/", 0755);
2199 (void) mkdir_p("/run/systemd/nspawn/propagate", 0600);
63c372cb 2200 p = strjoina("/run/systemd/nspawn/propagate/", arg_machine);
785890ac
LP
2201 (void) mkdir_p(p, 0600);
2202
709f6e46
MS
2203 r = userns_mkdir(root, "/run/systemd", 0755, 0, 0);
2204 if (r < 0)
2205 return log_error_errno(r, "Failed to create /run/systemd: %m");
03cfe0d5 2206
709f6e46
MS
2207 r = userns_mkdir(root, "/run/systemd/nspawn", 0755, 0, 0);
2208 if (r < 0)
2209 return log_error_errno(r, "Failed to create /run/systemd/nspawn: %m");
03cfe0d5 2210
709f6e46
MS
2211 r = userns_mkdir(root, "/run/systemd/nspawn/incoming", 0600, 0, 0);
2212 if (r < 0)
2213 return log_error_errno(r, "Failed to create /run/systemd/nspawn/incoming: %m");
785890ac 2214
03cfe0d5 2215 q = prefix_roota(root, "/run/systemd/nspawn/incoming");
60e76d48
ZJS
2216 r = mount_verbose(LOG_ERR, p, q, NULL, MS_BIND, NULL);
2217 if (r < 0)
2218 return r;
785890ac 2219
60e76d48
ZJS
2220 r = mount_verbose(LOG_ERR, NULL, q, NULL, MS_BIND|MS_REMOUNT|MS_RDONLY, NULL);
2221 if (r < 0)
2222 return r;
785890ac 2223
19caffac
AC
2224 /* machined will MS_MOVE into that directory, and that's only
2225 * supported for non-shared mounts. */
60e76d48 2226 return mount_verbose(LOG_ERR, NULL, q, NULL, MS_SLAVE, NULL);
785890ac
LP
2227}
2228
317feb4d 2229static int setup_machine_id(const char *directory) {
691675ba
LP
2230 const char *etc_machine_id;
2231 sd_id128_t id;
3bbaff3e 2232 int r;
e01ff70a 2233
317feb4d
LP
2234 /* If the UUID in the container is already set, then that's what counts, and we use. If it isn't set, and the
2235 * caller passed --uuid=, then we'll pass it in the $container_uuid env var to PID 1 of the container. The
2236 * assumption is that PID 1 will then write it to /etc/machine-id to make it persistent. If --uuid= is not
2237 * passed we generate a random UUID, and pass it via $container_uuid. In effect this means that /etc/machine-id
2238 * in the container and our idea of the container UUID will always be in sync (at least if PID 1 in the
2239 * container behaves nicely). */
2240
e01ff70a
MS
2241 etc_machine_id = prefix_roota(directory, "/etc/machine-id");
2242
691675ba 2243 r = id128_read(etc_machine_id, ID128_PLAIN, &id);
317feb4d
LP
2244 if (r < 0) {
2245 if (!IN_SET(r, -ENOENT, -ENOMEDIUM)) /* If the file is missing or empty, we don't mind */
2246 return log_error_errno(r, "Failed to read machine ID from container image: %m");
691675ba 2247
317feb4d
LP
2248 if (sd_id128_is_null(arg_uuid)) {
2249 r = sd_id128_randomize(&arg_uuid);
2250 if (r < 0)
2251 return log_error_errno(r, "Failed to acquire randomized machine UUID: %m");
2252 }
2253 } else {
2254 if (sd_id128_is_null(id)) {
2255 log_error("Machine ID in container image is zero, refusing.");
2256 return -EINVAL;
2257 }
e01ff70a 2258
317feb4d
LP
2259 arg_uuid = id;
2260 }
691675ba 2261
e01ff70a
MS
2262 return 0;
2263}
2264
7336138e
LP
2265static int recursive_chown(const char *directory, uid_t shift, uid_t range) {
2266 int r;
2267
2268 assert(directory);
2269
0de7acce 2270 if (arg_userns_mode == USER_NAMESPACE_NO || !arg_userns_chown)
7336138e
LP
2271 return 0;
2272
2273 r = path_patch_uid(directory, arg_uid_shift, arg_uid_range);
2274 if (r == -EOPNOTSUPP)
2275 return log_error_errno(r, "Automatic UID/GID adjusting is only supported for UID/GID ranges starting at multiples of 2^16 with a range of 2^16.");
2276 if (r == -EBADE)
2277 return log_error_errno(r, "Upper 16 bits of root directory UID and GID do not match.");
2278 if (r < 0)
2279 return log_error_errno(r, "Failed to adjust UID/GID shift of OS tree: %m");
2280 if (r == 0)
2281 log_debug("Root directory of image is already owned by the right UID/GID range, skipping recursive chown operation.");
2282 else
2283 log_debug("Patched directory tree to match UID/GID range.");
2284
2285 return r;
2286}
2287
113cea80 2288/*
6d416b9c
LS
2289 * Return values:
2290 * < 0 : wait_for_terminate() failed to get the state of the
2291 * container, the container was terminated by a signal, or
2292 * failed for an unknown reason. No change is made to the
2293 * container argument.
2294 * > 0 : The program executed in the container terminated with an
2295 * error. The exit code of the program executed in the
919699ec
LP
2296 * container is returned. The container argument has been set
2297 * to CONTAINER_TERMINATED.
6d416b9c
LS
2298 * 0 : The container is being rebooted, has been shut down or exited
2299 * successfully. The container argument has been set to either
2300 * CONTAINER_TERMINATED or CONTAINER_REBOOTED.
113cea80 2301 *
6d416b9c
LS
2302 * That is, success is indicated by a return value of zero, and an
2303 * error is indicated by a non-zero value.
113cea80
DH
2304 */
2305static int wait_for_container(pid_t pid, ContainerStatus *container) {
113cea80 2306 siginfo_t status;
919699ec 2307 int r;
113cea80
DH
2308
2309 r = wait_for_terminate(pid, &status);
f647962d
MS
2310 if (r < 0)
2311 return log_warning_errno(r, "Failed to wait for container: %m");
113cea80
DH
2312
2313 switch (status.si_code) {
fddbb89c 2314
113cea80 2315 case CLD_EXITED:
b5a2179b 2316 if (status.si_status == 0)
919699ec 2317 log_full(arg_quiet ? LOG_DEBUG : LOG_INFO, "Container %s exited successfully.", arg_machine);
b5a2179b 2318 else
919699ec 2319 log_full(arg_quiet ? LOG_DEBUG : LOG_INFO, "Container %s failed with error code %i.", arg_machine, status.si_status);
fddbb89c 2320
919699ec
LP
2321 *container = CONTAINER_TERMINATED;
2322 return status.si_status;
113cea80
DH
2323
2324 case CLD_KILLED:
2325 if (status.si_status == SIGINT) {
919699ec 2326 log_full(arg_quiet ? LOG_DEBUG : LOG_INFO, "Container %s has been shut down.", arg_machine);
113cea80 2327 *container = CONTAINER_TERMINATED;
919699ec
LP
2328 return 0;
2329
113cea80 2330 } else if (status.si_status == SIGHUP) {
919699ec 2331 log_full(arg_quiet ? LOG_DEBUG : LOG_INFO, "Container %s is being rebooted.", arg_machine);
113cea80 2332 *container = CONTAINER_REBOOTED;
919699ec 2333 return 0;
113cea80 2334 }
919699ec 2335
4831981d 2336 _fallthrough_;
113cea80 2337 case CLD_DUMPED:
fddbb89c 2338 log_error("Container %s terminated by signal %s.", arg_machine, signal_to_string(status.si_status));
919699ec 2339 return -EIO;
113cea80
DH
2340
2341 default:
fddbb89c 2342 log_error("Container %s failed due to unknown reason.", arg_machine);
919699ec 2343 return -EIO;
113cea80 2344 }
113cea80
DH
2345}
2346
023fb90b
LP
2347static int on_orderly_shutdown(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
2348 pid_t pid;
2349
4a0b58c4 2350 pid = PTR_TO_PID(userdata);
023fb90b 2351 if (pid > 0) {
c6c8f6e2 2352 if (kill(pid, arg_kill_signal) >= 0) {
023fb90b
LP
2353 log_info("Trying to halt container. Send SIGTERM again to trigger immediate termination.");
2354 sd_event_source_set_userdata(s, NULL);
2355 return 0;
2356 }
2357 }
2358
2359 sd_event_exit(sd_event_source_get_event(s), 0);
2360 return 0;
2361}
2362
6916b164 2363static int on_sigchld(sd_event_source *s, const struct signalfd_siginfo *ssi, void *userdata) {
abdb9b08
LP
2364 pid_t pid;
2365
2366 assert(s);
2367 assert(ssi);
2368
2369 pid = PTR_TO_PID(userdata);
2370
6916b164
AU
2371 for (;;) {
2372 siginfo_t si = {};
abdb9b08 2373
6916b164
AU
2374 if (waitid(P_ALL, 0, &si, WNOHANG|WNOWAIT|WEXITED) < 0)
2375 return log_error_errno(errno, "Failed to waitid(): %m");
2376 if (si.si_pid == 0) /* No pending children. */
2377 break;
abdb9b08 2378 if (si.si_pid == pid) {
6916b164
AU
2379 /* The main process we care for has exited. Return from
2380 * signal handler but leave the zombie. */
2381 sd_event_exit(sd_event_source_get_event(s), 0);
2382 break;
2383 }
abdb9b08 2384
6916b164
AU
2385 /* Reap all other children. */
2386 (void) waitid(P_PID, si.si_pid, &si, WNOHANG|WEXITED);
2387 }
2388
2389 return 0;
2390}
2391
abdb9b08
LP
2392static int on_request_stop(sd_bus_message *m, void *userdata, sd_bus_error *error) {
2393 pid_t pid;
2394
2395 assert(m);
2396
2397 pid = PTR_TO_PID(userdata);
2398
2399 if (arg_kill_signal > 0) {
2400 log_info("Container termination requested. Attempting to halt container.");
2401 (void) kill(pid, arg_kill_signal);
2402 } else {
2403 log_info("Container termination requested. Exiting.");
2404 sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m)), 0);
2405 }
2406
2407 return 0;
2408}
2409
ec16945e 2410static int determine_names(void) {
1b9cebf6 2411 int r;
ec16945e 2412
c1521918
LP
2413 if (arg_template && !arg_directory && arg_machine) {
2414
2415 /* If --template= was specified then we should not
2416 * search for a machine, but instead create a new one
2417 * in /var/lib/machine. */
2418
605405c6 2419 arg_directory = strjoin("/var/lib/machines/", arg_machine);
c1521918
LP
2420 if (!arg_directory)
2421 return log_oom();
2422 }
2423
ec16945e 2424 if (!arg_image && !arg_directory) {
1b9cebf6
LP
2425 if (arg_machine) {
2426 _cleanup_(image_unrefp) Image *i = NULL;
2427
5ef46e5f 2428 r = image_find(IMAGE_MACHINE, arg_machine, &i);
3a6ce860
LP
2429 if (r == -ENOENT)
2430 return log_error_errno(r, "No image for machine '%s'.", arg_machine);
1b9cebf6
LP
2431 if (r < 0)
2432 return log_error_errno(r, "Failed to find image for machine '%s': %m", arg_machine);
1b9cebf6 2433
eb38edce 2434 if (IN_SET(i->type, IMAGE_RAW, IMAGE_BLOCK))
0f03c2a4 2435 r = free_and_strdup(&arg_image, i->path);
1b9cebf6 2436 else
0f03c2a4 2437 r = free_and_strdup(&arg_directory, i->path);
1b9cebf6 2438 if (r < 0)
0f3be6ca 2439 return log_oom();
1b9cebf6 2440
aee327b8
LP
2441 if (!arg_ephemeral)
2442 arg_read_only = arg_read_only || i->read_only;
d7249575
LP
2443 } else {
2444 r = safe_getcwd(&arg_directory);
2445 if (r < 0)
2446 return log_error_errno(r, "Failed to determine current directory: %m");
2447 }
ec16945e 2448
0f3be6ca 2449 if (!arg_directory && !arg_image) {
1b9cebf6 2450 log_error("Failed to determine path, please use -D or -i.");
ec16945e
LP
2451 return -EINVAL;
2452 }
2453 }
2454
2455 if (!arg_machine) {
b9ba4dab
LP
2456 if (arg_directory && path_equal(arg_directory, "/"))
2457 arg_machine = gethostname_malloc();
4827ab48
LP
2458 else {
2459 if (arg_image) {
2460 char *e;
2461
2462 arg_machine = strdup(basename(arg_image));
2463
2464 /* Truncate suffix if there is one */
2465 e = endswith(arg_machine, ".raw");
2466 if (e)
2467 *e = 0;
2468 } else
2469 arg_machine = strdup(basename(arg_directory));
2470 }
ec16945e
LP
2471 if (!arg_machine)
2472 return log_oom();
2473
ae691c1d 2474 hostname_cleanup(arg_machine);
ec16945e
LP
2475 if (!machine_name_is_valid(arg_machine)) {
2476 log_error("Failed to determine machine name automatically, please use -M.");
2477 return -EINVAL;
2478 }
b9ba4dab
LP
2479
2480 if (arg_ephemeral) {
2481 char *b;
2482
2483 /* Add a random suffix when this is an
2484 * ephemeral machine, so that we can run many
2485 * instances at once without manually having
2486 * to specify -M each time. */
2487
2488 if (asprintf(&b, "%s-%016" PRIx64, arg_machine, random_u64()) < 0)
2489 return log_oom();
2490
2491 free(arg_machine);
2492 arg_machine = b;
2493 }
ec16945e
LP
2494 }
2495
2496 return 0;
2497}
2498
8d4aa2bb 2499static int chase_symlinks_and_update(char **p, unsigned flags) {
3f342ec4
LP
2500 char *chased;
2501 int r;
2502
2503 assert(p);
2504
2505 if (!*p)
2506 return 0;
2507
8d4aa2bb 2508 r = chase_symlinks(*p, NULL, flags, &chased);
3f342ec4
LP
2509 if (r < 0)
2510 return log_error_errno(r, "Failed to resolve path %s: %m", *p);
2511
8405dcf7
ZJS
2512 free_and_replace(*p, chased);
2513 return r; /* r might be an fd here in case we ever use CHASE_OPEN in flags */
3f342ec4
LP
2514}
2515
03cfe0d5 2516static int determine_uid_shift(const char *directory) {
6dac160c
LP
2517 int r;
2518
0de7acce 2519 if (arg_userns_mode == USER_NAMESPACE_NO) {
03cfe0d5 2520 arg_uid_shift = 0;
6dac160c 2521 return 0;
03cfe0d5 2522 }
6dac160c
LP
2523
2524 if (arg_uid_shift == UID_INVALID) {
2525 struct stat st;
2526
03cfe0d5 2527 r = stat(directory, &st);
6dac160c 2528 if (r < 0)
03cfe0d5 2529 return log_error_errno(errno, "Failed to determine UID base of %s: %m", directory);
6dac160c
LP
2530
2531 arg_uid_shift = st.st_uid & UINT32_C(0xffff0000);
2532
2533 if (arg_uid_shift != (st.st_gid & UINT32_C(0xffff0000))) {
03cfe0d5 2534 log_error("UID and GID base of %s don't match.", directory);
6dac160c
LP
2535 return -EINVAL;
2536 }
2537
2538 arg_uid_range = UINT32_C(0x10000);
2539 }
2540
2541 if (arg_uid_shift > (uid_t) -1 - arg_uid_range) {
2542 log_error("UID base too high for UID range.");
2543 return -EINVAL;
2544 }
2545
6dac160c
LP
2546 return 0;
2547}
2548
03cfe0d5
LP
2549static int inner_child(
2550 Barrier *barrier,
2551 const char *directory,
2552 bool secondary,
2553 int kmsg_socket,
2554 int rtnl_socket,
f757855e 2555 FDSet *fds) {
69c79d3c 2556
03cfe0d5 2557 _cleanup_free_ char *home = NULL;
e01ff70a 2558 char as_uuid[37];
88614c8a 2559 size_t n_env = 1;
03cfe0d5 2560 const char *envp[] = {
0c300adf 2561 "PATH=" DEFAULT_PATH_COMPAT,
6aadfa4c 2562 NULL, /* container */
03cfe0d5
LP
2563 NULL, /* TERM */
2564 NULL, /* HOME */
2565 NULL, /* USER */
2566 NULL, /* LOGNAME */
2567 NULL, /* container_uuid */
2568 NULL, /* LISTEN_FDS */
2569 NULL, /* LISTEN_PID */
9c1e04d0 2570 NULL, /* NOTIFY_SOCKET */
03cfe0d5
LP
2571 NULL
2572 };
1a68e1e5 2573 const char *exec_target;
2371271c 2574 _cleanup_strv_free_ char **env_use = NULL;
03cfe0d5 2575 int r;
88213476 2576
b37469d7
LP
2577 /* This is the "inner" child process, i.e. the one forked off by the "outer" child process, which is the one
2578 * the container manager itself forked off. At the time of clone() it gained its own CLONE_NEWNS, CLONE_NEWPID,
2579 * CLONE_NEWUTS, CLONE_NEWIPC, CLONE_NEWUSER namespaces. Note that it has its own CLONE_NEWNS namespace,
2580 * separate from the CLONE_NEWNS created for the "outer" child, and also separate from the host's CLONE_NEWNS
2581 * namespace. The reason for having two levels of CLONE_NEWNS namespaces is that the "inner" one is owned by
2582 * the CLONE_NEWUSER namespace of the container, while the "outer" one is owned by the host's CLONE_NEWUSER
2583 * namespace.
2584 *
2585 * Note at this point we have no CLONE_NEWNET namespace yet. We'll acquire that one later through
2586 * unshare(). See below. */
2587
03cfe0d5
LP
2588 assert(barrier);
2589 assert(directory);
2590 assert(kmsg_socket >= 0);
88213476 2591
0de7acce 2592 if (arg_userns_mode != USER_NAMESPACE_NO) {
03cfe0d5
LP
2593 /* Tell the parent, that it now can write the UID map. */
2594 (void) barrier_place(barrier); /* #1 */
7027ff61 2595
03cfe0d5
LP
2596 /* Wait until the parent wrote the UID map */
2597 if (!barrier_place_and_sync(barrier)) { /* #2 */
2598 log_error("Parent died too early");
2599 return -ESRCH;
2600 }
88213476
LP
2601 }
2602
6d66bd3b
EV
2603 r = reset_uid_gid();
2604 if (r < 0)
2605 return log_error_errno(r, "Couldn't become new root: %m");
2606
0de7acce 2607 r = mount_all(NULL,
4f086aab 2608 arg_mount_settings | MOUNT_IN_USERNS,
0de7acce 2609 arg_uid_shift,
0de7acce 2610 arg_selinux_apifs_context);
03cfe0d5
LP
2611 if (r < 0)
2612 return r;
2613
04413780
ZJS
2614 if (!arg_network_namespace_path && arg_private_network) {
2615 r = unshare(CLONE_NEWNET);
2616 if (r < 0)
2617 return log_error_errno(errno, "Failed to unshare network namespace: %m");
75116558
PS
2618
2619 /* Tell the parent that it can setup network interfaces. */
2620 (void) barrier_place(barrier); /* #3 */
04413780
ZJS
2621 }
2622
4f086aab 2623 r = mount_sysfs(NULL, arg_mount_settings);
d8fc6a00
LP
2624 if (r < 0)
2625 return r;
2626
03cfe0d5
LP
2627 /* Wait until we are cgroup-ified, so that we
2628 * can mount the right cgroup path writable */
75116558 2629 if (!barrier_place_and_sync(barrier)) { /* #4 */
03cfe0d5
LP
2630 log_error("Parent died too early");
2631 return -ESRCH;
88213476
LP
2632 }
2633
5a8ff0e6 2634 if (arg_use_cgns && cg_ns_supported()) {
0996ef00
CB
2635 r = unshare(CLONE_NEWCGROUP);
2636 if (r < 0)
04413780 2637 return log_error_errno(errno, "Failed to unshare cgroup namespace: %m");
0996ef00
CB
2638 r = mount_cgroups(
2639 "",
2640 arg_unified_cgroup_hierarchy,
2641 arg_userns_mode != USER_NAMESPACE_NO,
2642 arg_uid_shift,
2643 arg_uid_range,
5a8ff0e6 2644 arg_selinux_apifs_context,
ada54120 2645 true);
0996ef00
CB
2646 if (r < 0)
2647 return r;
2648 } else {
2649 r = mount_systemd_cgroup_writable("", arg_unified_cgroup_hierarchy);
2650 if (r < 0)
2651 return r;
2652 }
ec16945e 2653
1e4f1671 2654 r = setup_boot_id();
03cfe0d5
LP
2655 if (r < 0)
2656 return r;
ec16945e 2657
1e4f1671 2658 r = setup_kmsg(kmsg_socket);
03cfe0d5
LP
2659 if (r < 0)
2660 return r;
2661 kmsg_socket = safe_close(kmsg_socket);
ec16945e 2662
03cfe0d5
LP
2663 if (setsid() < 0)
2664 return log_error_errno(errno, "setsid() failed: %m");
2665
2666 if (arg_private_network)
2667 loopback_setup();
2668
7a8f6325
LP
2669 if (arg_expose_ports) {
2670 r = expose_port_send_rtnl(rtnl_socket);
2671 if (r < 0)
2672 return r;
2673 rtnl_socket = safe_close(rtnl_socket);
2674 }
03cfe0d5 2675
81f345df
LP
2676 if (arg_oom_score_adjust_set) {
2677 r = set_oom_score_adjust(arg_oom_score_adjust);
2678 if (r < 0)
2679 return log_error_errno(r, "Failed to adjust OOM score: %m");
2680 }
2681
d107bb7d
LP
2682 if (arg_cpuset)
2683 if (sched_setaffinity(0, CPU_ALLOC_SIZE(arg_cpuset_ncpus), arg_cpuset) < 0)
2684 return log_error_errno(errno, "Failed to set CPU affinity: %m");
2685
709f6e46
MS
2686 r = drop_capabilities();
2687 if (r < 0)
2688 return log_error_errno(r, "drop_capabilities() failed: %m");
03cfe0d5 2689
c818eef1 2690 (void) setup_hostname();
03cfe0d5 2691
050f7277 2692 if (arg_personality != PERSONALITY_INVALID) {
21022b9d
LP
2693 r = safe_personality(arg_personality);
2694 if (r < 0)
2695 return log_error_errno(r, "personality() failed: %m");
03cfe0d5 2696 } else if (secondary) {
21022b9d
LP
2697 r = safe_personality(PER_LINUX32);
2698 if (r < 0)
2699 return log_error_errno(r, "personality() failed: %m");
03cfe0d5
LP
2700 }
2701
349cc4a5 2702#if HAVE_SELINUX
03cfe0d5 2703 if (arg_selinux_context)
2ed96880 2704 if (setexeccon(arg_selinux_context) < 0)
03cfe0d5
LP
2705 return log_error_errno(errno, "setexeccon(\"%s\") failed: %m", arg_selinux_context);
2706#endif
2707
ee645080 2708 r = change_uid_gid(arg_user, &home);
03cfe0d5
LP
2709 if (r < 0)
2710 return r;
2711
66edd963
LP
2712 if (arg_no_new_privileges)
2713 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0)
2714 return log_error_errno(errno, "Failed to disable new privileges: %m");
2715
6aadfa4c
ILG
2716 /* LXC sets container=lxc, so follow the scheme here */
2717 envp[n_env++] = strjoina("container=", arg_container_service_name);
2718
03cfe0d5
LP
2719 envp[n_env] = strv_find_prefix(environ, "TERM=");
2720 if (envp[n_env])
313cefa1 2721 n_env++;
03cfe0d5
LP
2722
2723 if ((asprintf((char**)(envp + n_env++), "HOME=%s", home ? home: "/root") < 0) ||
2724 (asprintf((char**)(envp + n_env++), "USER=%s", arg_user ? arg_user : "root") < 0) ||
2725 (asprintf((char**)(envp + n_env++), "LOGNAME=%s", arg_user ? arg_user : "root") < 0))
2726 return log_oom();
2727
3bbaff3e 2728 assert(!sd_id128_is_null(arg_uuid));
03cfe0d5 2729
691675ba 2730 if (asprintf((char**)(envp + n_env++), "container_uuid=%s", id128_to_uuid_string(arg_uuid, as_uuid)) < 0)
e01ff70a 2731 return log_oom();
03cfe0d5
LP
2732
2733 if (fdset_size(fds) > 0) {
2734 r = fdset_cloexec(fds, false);
2735 if (r < 0)
2736 return log_error_errno(r, "Failed to unset O_CLOEXEC for file descriptors.");
2737
2738 if ((asprintf((char **)(envp + n_env++), "LISTEN_FDS=%u", fdset_size(fds)) < 0) ||
2739 (asprintf((char **)(envp + n_env++), "LISTEN_PID=1") < 0))
2740 return log_oom();
2741 }
9c1e04d0
AP
2742 if (asprintf((char **)(envp + n_env++), "NOTIFY_SOCKET=%s", NSPAWN_NOTIFY_SOCKET_PATH) < 0)
2743 return log_oom();
03cfe0d5 2744
2371271c
TG
2745 env_use = strv_env_merge(2, envp, arg_setenv);
2746 if (!env_use)
2747 return log_oom();
03cfe0d5
LP
2748
2749 /* Let the parent know that we are ready and
2750 * wait until the parent is ready with the
2751 * setup, too... */
75116558 2752 if (!barrier_place_and_sync(barrier)) { /* #5 */
03cfe0d5
LP
2753 log_error("Parent died too early");
2754 return -ESRCH;
2755 }
2756
5f932eb9
LP
2757 if (arg_chdir)
2758 if (chdir(arg_chdir) < 0)
2759 return log_error_errno(errno, "Failed to change to specified working directory %s: %m", arg_chdir);
2760
7732f92b 2761 if (arg_start_mode == START_PID2) {
75bf701f 2762 r = stub_pid1(arg_uuid);
7732f92b
LP
2763 if (r < 0)
2764 return r;
2765 }
2766
8ca082b4
LP
2767 /* Now, explicitly close the log, so that we then can close all remaining fds. Closing the log explicitly first
2768 * has the benefit that the logging subsystem knows about it, and is thus ready to be reopened should we need
2769 * it again. Note that the other fds closed here are at least the locking and barrier fds. */
03cfe0d5 2770 log_close();
8ca082b4
LP
2771 log_set_open_when_needed(true);
2772
03cfe0d5
LP
2773 (void) fdset_close_others(fds);
2774
7732f92b 2775 if (arg_start_mode == START_BOOT) {
03cfe0d5
LP
2776 char **a;
2777 size_t m;
2778
2779 /* Automatically search for the init system */
2780
75f32f04
ZJS
2781 m = strv_length(arg_parameters);
2782 a = newa(char*, m + 2);
2783 memcpy_safe(a + 1, arg_parameters, m * sizeof(char*));
2784 a[1 + m] = NULL;
03cfe0d5 2785
ced58da7 2786 a[0] = (char*) "/usr/lib/systemd/systemd";
03cfe0d5
LP
2787 execve(a[0], a, env_use);
2788
ced58da7 2789 a[0] = (char*) "/lib/systemd/systemd";
03cfe0d5
LP
2790 execve(a[0], a, env_use);
2791
ced58da7 2792 a[0] = (char*) "/sbin/init";
03cfe0d5 2793 execve(a[0], a, env_use);
ced58da7
LP
2794
2795 exec_target = "/usr/lib/systemd/systemd, /lib/systemd/systemd, /sbin/init";
1a68e1e5 2796 } else if (!strv_isempty(arg_parameters)) {
b6b180b7
LP
2797 const char *dollar_path;
2798
1a68e1e5 2799 exec_target = arg_parameters[0];
b6b180b7
LP
2800
2801 /* Use the user supplied search $PATH if there is one, or DEFAULT_PATH_COMPAT if not to search the
2802 * binary. */
2803 dollar_path = strv_env_get(env_use, "PATH");
2804 if (dollar_path) {
2805 if (putenv((char*) dollar_path) != 0)
2806 return log_error_errno(errno, "Failed to update $PATH: %m");
2807 }
2808
f757855e 2809 execvpe(arg_parameters[0], arg_parameters, env_use);
1a68e1e5 2810 } else {
5f932eb9 2811 if (!arg_chdir)
d929b0f9
ZJS
2812 /* If we cannot change the directory, we'll end up in /, that is expected. */
2813 (void) chdir(home ?: "/root");
5f932eb9 2814
03cfe0d5
LP
2815 execle("/bin/bash", "-bash", NULL, env_use);
2816 execle("/bin/sh", "-sh", NULL, env_use);
ced58da7
LP
2817
2818 exec_target = "/bin/bash, /bin/sh";
03cfe0d5
LP
2819 }
2820
8ca082b4 2821 return log_error_errno(errno, "execv(%s) failed: %m", exec_target);
03cfe0d5
LP
2822}
2823
9c1e04d0 2824static int setup_sd_notify_child(void) {
271f518f 2825 _cleanup_close_ int fd = -1;
9c1e04d0 2826 union sockaddr_union sa = {
44ed5214
LP
2827 .un.sun_family = AF_UNIX,
2828 .un.sun_path = NSPAWN_NOTIFY_SOCKET_PATH,
9c1e04d0
AP
2829 };
2830 int r;
2831
2832 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
2833 if (fd < 0)
2834 return log_error_errno(errno, "Failed to allocate notification socket: %m");
2835
2836 (void) mkdir_parents(NSPAWN_NOTIFY_SOCKET_PATH, 0755);
fbda85b0 2837 (void) sockaddr_un_unlink(&sa.un);
9c1e04d0 2838
9c1e04d0 2839 r = bind(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un));
271f518f 2840 if (r < 0)
44ed5214 2841 return log_error_errno(errno, "bind(" NSPAWN_NOTIFY_SOCKET_PATH ") failed: %m");
9c1e04d0 2842
adc7d9f0 2843 r = userns_lchown(NSPAWN_NOTIFY_SOCKET_PATH, 0, 0);
271f518f 2844 if (r < 0)
adc7d9f0 2845 return log_error_errno(r, "Failed to chown " NSPAWN_NOTIFY_SOCKET_PATH ": %m");
adc7d9f0 2846
2ff48e98 2847 r = setsockopt_int(fd, SOL_SOCKET, SO_PASSCRED, true);
271f518f 2848 if (r < 0)
2ff48e98 2849 return log_error_errno(r, "SO_PASSCRED failed: %m");
9c1e04d0 2850
271f518f 2851 return TAKE_FD(fd);
9c1e04d0
AP
2852}
2853
03cfe0d5
LP
2854static int outer_child(
2855 Barrier *barrier,
2856 const char *directory,
2857 const char *console,
2d845785 2858 DissectedImage *dissected_image,
03cfe0d5
LP
2859 bool interactive,
2860 bool secondary,
2861 int pid_socket,
e01ff70a 2862 int uuid_socket,
9c1e04d0 2863 int notify_socket,
03cfe0d5
LP
2864 int kmsg_socket,
2865 int rtnl_socket,
825d5287 2866 int uid_shift_socket,
8199d554 2867 int unified_cgroup_hierarchy_socket,
d7bea6b6
DP
2868 FDSet *fds,
2869 int netns_fd) {
03cfe0d5 2870
bf428efb
LP
2871 _cleanup_close_ int fd = -1;
2872 int r, which_failed;
03cfe0d5
LP
2873 pid_t pid;
2874 ssize_t l;
03cfe0d5 2875
b37469d7
LP
2876 /* This is the "outer" child process, i.e the one forked off by the container manager itself. It already has
2877 * its own CLONE_NEWNS namespace (which was created by the clone()). It still lives in the host's CLONE_NEWPID,
2878 * CLONE_NEWUTS, CLONE_NEWIPC, CLONE_NEWUSER and CLONE_NEWNET namespaces. After it completed a number of
2879 * initializations a second child (the "inner" one) is forked off it, and it exits. */
2880
03cfe0d5
LP
2881 assert(barrier);
2882 assert(directory);
2883 assert(console);
2884 assert(pid_socket >= 0);
e01ff70a 2885 assert(uuid_socket >= 0);
9c1e04d0 2886 assert(notify_socket >= 0);
03cfe0d5
LP
2887 assert(kmsg_socket >= 0);
2888
2889 if (prctl(PR_SET_PDEATHSIG, SIGKILL) < 0)
2890 return log_error_errno(errno, "PR_SET_PDEATHSIG failed: %m");
2891
2892 if (interactive) {
2b33ab09 2893 int terminal;
03cfe0d5 2894
2b33ab09
LP
2895 terminal = open_terminal(console, O_RDWR);
2896 if (terminal < 0)
2897 return log_error_errno(terminal, "Failed to open console: %m");
03cfe0d5 2898
17cac366
LP
2899 /* Make sure we can continue logging to the original stderr, even if stderr points elsewhere now */
2900 r = log_dup_console();
2901 if (r < 0)
2902 return log_error_errno(r, "Failed to duplicate stderr: %m");
2903
2b33ab09
LP
2904 r = rearrange_stdio(terminal, terminal, terminal); /* invalidates 'terminal' on success and failure */
2905 if (r < 0)
2906 return log_error_errno(r, "Failed to move console to stdin/stdout/stderr: %m");
03cfe0d5
LP
2907 }
2908
2909 r = reset_audit_loginuid();
2910 if (r < 0)
2911 return r;
2912
2913 /* Mark everything as slave, so that we still
2914 * receive mounts from the real root, but don't
2915 * propagate mounts to the real root. */
60e76d48
ZJS
2916 r = mount_verbose(LOG_ERR, NULL, "/", NULL, MS_SLAVE|MS_REC, NULL);
2917 if (r < 0)
2918 return r;
03cfe0d5 2919
2d845785 2920 if (dissected_image) {
2d3a5a73
LP
2921 /* If we are operating on a disk image, then mount its root directory now, but leave out the rest. We
2922 * can read the UID shift from it if we need to. Further down we'll mount the rest, but then with the
2923 * uid shift known. That way we can mount VFAT file systems shifted to the right place right away. This
2924 * makes sure ESP partitions and userns are compatible. */
2925
2926 r = dissected_image_mount(dissected_image, directory, arg_uid_shift,
03bcb6d4
LP
2927 DISSECT_IMAGE_MOUNT_ROOT_ONLY|DISSECT_IMAGE_DISCARD_ON_LOOP|
2928 (arg_read_only ? DISSECT_IMAGE_READ_ONLY : 0)|
2929 (arg_start_mode == START_BOOT ? DISSECT_IMAGE_VALIDATE_OS : 0));
2d845785
LP
2930 if (r < 0)
2931 return r;
2932 }
03cfe0d5 2933
391567f4
LP
2934 r = determine_uid_shift(directory);
2935 if (r < 0)
2936 return r;
2937
0de7acce 2938 if (arg_userns_mode != USER_NAMESPACE_NO) {
0e7ac751 2939 /* Let the parent know which UID shift we read from the image */
825d5287
RM
2940 l = send(uid_shift_socket, &arg_uid_shift, sizeof(arg_uid_shift), MSG_NOSIGNAL);
2941 if (l < 0)
2942 return log_error_errno(errno, "Failed to send UID shift: %m");
2943 if (l != sizeof(arg_uid_shift)) {
2944 log_error("Short write while sending UID shift.");
2945 return -EIO;
2946 }
0e7ac751 2947
0de7acce 2948 if (arg_userns_mode == USER_NAMESPACE_PICK) {
0e7ac751
LP
2949 /* When we are supposed to pick the UID shift, the parent will check now whether the UID shift
2950 * we just read from the image is available. If yes, it will send the UID shift back to us, if
2951 * not it will pick a different one, and send it back to us. */
2952
2953 l = recv(uid_shift_socket, &arg_uid_shift, sizeof(arg_uid_shift), 0);
2954 if (l < 0)
2955 return log_error_errno(errno, "Failed to recv UID shift: %m");
2956 if (l != sizeof(arg_uid_shift)) {
595bfe7d 2957 log_error("Short read while receiving UID shift.");
0e7ac751
LP
2958 return -EIO;
2959 }
2960 }
2961
ff6c6cc1
LP
2962 log_full(arg_quiet ? LOG_DEBUG : LOG_INFO,
2963 "Selected user namespace base " UID_FMT " and range " UID_FMT ".", arg_uid_shift, arg_uid_range);
825d5287
RM
2964 }
2965
2d3a5a73
LP
2966 if (dissected_image) {
2967 /* Now we know the uid shift, let's now mount everything else that might be in the image. */
2968 r = dissected_image_mount(dissected_image, directory, arg_uid_shift,
2969 DISSECT_IMAGE_MOUNT_NON_ROOT_ONLY|DISSECT_IMAGE_DISCARD_ON_LOOP|(arg_read_only ? DISSECT_IMAGE_READ_ONLY : 0));
2970 if (r < 0)
2971 return r;
2972 }
2973
8199d554
LP
2974 if (arg_unified_cgroup_hierarchy == CGROUP_UNIFIED_UNKNOWN) {
2975 /* OK, we don't know yet which cgroup mode to use yet. Let's figure it out, and tell the parent. */
2976
2977 r = detect_unified_cgroup_hierarchy_from_image(directory);
2978 if (r < 0)
2979 return r;
2980
2981 l = send(unified_cgroup_hierarchy_socket, &arg_unified_cgroup_hierarchy, sizeof(arg_unified_cgroup_hierarchy), MSG_NOSIGNAL);
2982 if (l < 0)
2983 return log_error_errno(errno, "Failed to send cgroup mode: %m");
2984 if (l != sizeof(arg_unified_cgroup_hierarchy)) {
b8b846d7 2985 log_error("Short write while sending cgroup mode.");
8199d554
LP
2986 return -EIO;
2987 }
2988
2989 unified_cgroup_hierarchy_socket = safe_close(unified_cgroup_hierarchy_socket);
2990 }
2991
03cfe0d5 2992 /* Turn directory into bind mount */
60e76d48
ZJS
2993 r = mount_verbose(LOG_ERR, directory, directory, NULL, MS_BIND|MS_REC, NULL);
2994 if (r < 0)
2995 return r;
03cfe0d5 2996
b53ede69
PW
2997 r = setup_pivot_root(
2998 directory,
2999 arg_pivot_root_new,
3000 arg_pivot_root_old);
3001 if (r < 0)
3002 return r;
3003
0de7acce
LP
3004 r = setup_volatile(
3005 directory,
3006 arg_volatile_mode,
3007 arg_userns_mode != USER_NAMESPACE_NO,
3008 arg_uid_shift,
3009 arg_uid_range,
3010 arg_selinux_context);
03cfe0d5
LP
3011 if (r < 0)
3012 return r;
3013
0de7acce
LP
3014 r = setup_volatile_state(
3015 directory,
3016 arg_volatile_mode,
3017 arg_userns_mode != USER_NAMESPACE_NO,
3018 arg_uid_shift,
3019 arg_uid_range,
3020 arg_selinux_context);
03cfe0d5
LP
3021 if (r < 0)
3022 return r;
3023
4ad14eff
LP
3024 /* Mark everything as shared so our mounts get propagated down. This is
3025 * required to make new bind mounts available in systemd services
3026 * inside the containter that create a new mount namespace.
3027 * See https://github.com/systemd/systemd/issues/3860
3028 * Further submounts (such as /dev) done after this will inherit the
13e785f7 3029 * shared propagation mode. */
4ad14eff
LP
3030 r = mount_verbose(LOG_ERR, NULL, directory, NULL, MS_SHARED|MS_REC, NULL);
3031 if (r < 0)
3032 return r;
3033
3034 r = recursive_chown(directory, arg_uid_shift, arg_uid_range);
3035 if (r < 0)
3036 return r;
3037
03cfe0d5
LP
3038 r = base_filesystem_create(directory, arg_uid_shift, (gid_t) arg_uid_shift);
3039 if (r < 0)
3040 return r;
3041
03cfe0d5 3042 if (arg_read_only) {
6b7c9f8b 3043 r = bind_remount_recursive(directory, true, NULL);
03cfe0d5
LP
3044 if (r < 0)
3045 return log_error_errno(r, "Failed to make tree read-only: %m");
3046 }
3047
0de7acce 3048 r = mount_all(directory,
4f086aab 3049 arg_mount_settings,
0de7acce 3050 arg_uid_shift,
0de7acce 3051 arg_selinux_apifs_context);
03cfe0d5
LP
3052 if (r < 0)
3053 return r;
3054
07fa00f9
LP
3055 r = copy_devnodes(directory);
3056 if (r < 0)
03cfe0d5
LP
3057 return r;
3058
3059 dev_setup(directory, arg_uid_shift, arg_uid_shift);
3060
07fa00f9
LP
3061 r = setup_pts(directory);
3062 if (r < 0)
03cfe0d5
LP
3063 return r;
3064
3065 r = setup_propagate(directory);
3066 if (r < 0)
3067 return r;
3068
3069 r = setup_dev_console(directory, console);
3070 if (r < 0)
3071 return r;
3072
8e5430c4
LP
3073 r = setup_keyring();
3074 if (r < 0)
3075 return r;
3076
960e4569 3077 r = setup_seccomp(arg_caps_retain, arg_syscall_whitelist, arg_syscall_blacklist);
03cfe0d5
LP
3078 if (r < 0)
3079 return r;
3080
3081 r = setup_timezone(directory);
3082 if (r < 0)
3083 return r;
3084
3085 r = setup_resolv_conf(directory);
3086 if (r < 0)
3087 return r;
3088
e01ff70a
MS
3089 r = setup_machine_id(directory);
3090 if (r < 0)
3091 return r;
3092
03cfe0d5
LP
3093 r = setup_journal(directory);
3094 if (r < 0)
3095 return r;
3096
0de7acce
LP
3097 r = mount_custom(
3098 directory,
3099 arg_custom_mounts,
3100 arg_n_custom_mounts,
3101 arg_userns_mode != USER_NAMESPACE_NO,
3102 arg_uid_shift,
3103 arg_uid_range,
3104 arg_selinux_apifs_context);
03cfe0d5
LP
3105 if (r < 0)
3106 return r;
3107
5a8ff0e6 3108 if (!arg_use_cgns || !cg_ns_supported()) {
0996ef00
CB
3109 r = mount_cgroups(
3110 directory,
3111 arg_unified_cgroup_hierarchy,
3112 arg_userns_mode != USER_NAMESPACE_NO,
3113 arg_uid_shift,
3114 arg_uid_range,
5a8ff0e6 3115 arg_selinux_apifs_context,
ada54120 3116 false);
0996ef00
CB
3117 if (r < 0)
3118 return r;
3119 }
03cfe0d5
LP
3120
3121 r = mount_move_root(directory);
3122 if (r < 0)
3123 return log_error_errno(r, "Failed to move root directory: %m");
3124
9c1e04d0
AP
3125 fd = setup_sd_notify_child();
3126 if (fd < 0)
3127 return fd;
3128
bf428efb
LP
3129 r = setrlimit_closest_all((const struct rlimit *const*) arg_rlimit, &which_failed);
3130 if (r < 0)
3131 return log_error_errno(r, "Failed to apply resource limit RLIMIT_%s: %m", rlimit_to_string(which_failed));
3132
03cfe0d5 3133 pid = raw_clone(SIGCHLD|CLONE_NEWNS|
0c582db0 3134 arg_clone_ns_flags |
8869a0b4 3135 (arg_userns_mode != USER_NAMESPACE_NO ? CLONE_NEWUSER : 0));
03cfe0d5
LP
3136 if (pid < 0)
3137 return log_error_errno(errno, "Failed to fork inner child: %m");
03cfe0d5
LP
3138 if (pid == 0) {
3139 pid_socket = safe_close(pid_socket);
e01ff70a 3140 uuid_socket = safe_close(uuid_socket);
9c1e04d0 3141 notify_socket = safe_close(notify_socket);
825d5287 3142 uid_shift_socket = safe_close(uid_shift_socket);
03cfe0d5
LP
3143
3144 /* The inner child has all namespaces that are
3145 * requested, so that we all are owned by the user if
3146 * user namespaces are turned on. */
3147
d7bea6b6
DP
3148 if (arg_network_namespace_path) {
3149 r = namespace_enter(-1, -1, netns_fd, -1, -1);
3150 if (r < 0)
e2d39e54 3151 return log_error_errno(r, "Failed to join network namespace: %m");
d7bea6b6
DP
3152 }
3153
f757855e 3154 r = inner_child(barrier, directory, secondary, kmsg_socket, rtnl_socket, fds);
03cfe0d5
LP
3155 if (r < 0)
3156 _exit(EXIT_FAILURE);
3157
3158 _exit(EXIT_SUCCESS);
3159 }
3160
3161 l = send(pid_socket, &pid, sizeof(pid), MSG_NOSIGNAL);
3162 if (l < 0)
3163 return log_error_errno(errno, "Failed to send PID: %m");
3164 if (l != sizeof(pid)) {
3165 log_error("Short write while sending PID.");
3166 return -EIO;
3167 }
3168
e01ff70a
MS
3169 l = send(uuid_socket, &arg_uuid, sizeof(arg_uuid), MSG_NOSIGNAL);
3170 if (l < 0)
3171 return log_error_errno(errno, "Failed to send machine ID: %m");
3172 if (l != sizeof(arg_uuid)) {
3173 log_error("Short write while sending machine ID.");
3174 return -EIO;
3175 }
3176
9c1e04d0
AP
3177 l = send_one_fd(notify_socket, fd, 0);
3178 if (l < 0)
3179 return log_error_errno(errno, "Failed to send notify fd: %m");
3180
03cfe0d5 3181 pid_socket = safe_close(pid_socket);
e01ff70a 3182 uuid_socket = safe_close(uuid_socket);
9c1e04d0 3183 notify_socket = safe_close(notify_socket);
327e26d6
KN
3184 kmsg_socket = safe_close(kmsg_socket);
3185 rtnl_socket = safe_close(rtnl_socket);
d7bea6b6 3186 netns_fd = safe_close(netns_fd);
03cfe0d5
LP
3187
3188 return 0;
3189}
3190
0e7ac751 3191static int uid_shift_pick(uid_t *shift, LockFile *ret_lock_file) {
d381c8a6 3192 bool tried_hashed = false;
0e7ac751
LP
3193 unsigned n_tries = 100;
3194 uid_t candidate;
3195 int r;
3196
3197 assert(shift);
3198 assert(ret_lock_file);
0de7acce 3199 assert(arg_userns_mode == USER_NAMESPACE_PICK);
0e7ac751
LP
3200 assert(arg_uid_range == 0x10000U);
3201
3202 candidate = *shift;
3203
3204 (void) mkdir("/run/systemd/nspawn-uid", 0755);
3205
3206 for (;;) {
fbd0b64f 3207 char lock_path[STRLEN("/run/systemd/nspawn-uid/") + DECIMAL_STR_MAX(uid_t) + 1];
8e766630 3208 _cleanup_(release_lock_file) LockFile lf = LOCK_FILE_INIT;
0e7ac751
LP
3209
3210 if (--n_tries <= 0)
3211 return -EBUSY;
3212
87d5e4f2 3213 if (candidate < CONTAINER_UID_BASE_MIN || candidate > CONTAINER_UID_BASE_MAX)
0e7ac751
LP
3214 goto next;
3215 if ((candidate & UINT32_C(0xFFFF)) != 0)
3216 goto next;
3217
3218 xsprintf(lock_path, "/run/systemd/nspawn-uid/" UID_FMT, candidate);
3219 r = make_lock_file(lock_path, LOCK_EX|LOCK_NB, &lf);
3220 if (r == -EBUSY) /* Range already taken by another nspawn instance */
3221 goto next;
3222 if (r < 0)
3223 return r;
3224
3225 /* Make some superficial checks whether the range is currently known in the user database */
3226 if (getpwuid(candidate))
3227 goto next;
3228 if (getpwuid(candidate + UINT32_C(0xFFFE)))
3229 goto next;
3230 if (getgrgid(candidate))
3231 goto next;
3232 if (getgrgid(candidate + UINT32_C(0xFFFE)))
3233 goto next;
3234
3235 *ret_lock_file = lf;
3236 lf = (struct LockFile) LOCK_FILE_INIT;
3237 *shift = candidate;
3238 return 0;
3239
3240 next:
d381c8a6
LP
3241 if (arg_machine && !tried_hashed) {
3242 /* Try to hash the base from the container name */
3243
3244 static const uint8_t hash_key[] = {
3245 0xe1, 0x56, 0xe0, 0xf0, 0x4a, 0xf0, 0x41, 0xaf,
3246 0x96, 0x41, 0xcf, 0x41, 0x33, 0x94, 0xff, 0x72
3247 };
3248
3249 candidate = (uid_t) siphash24(arg_machine, strlen(arg_machine), hash_key);
3250
3251 tried_hashed = true;
3252 } else
3253 random_bytes(&candidate, sizeof(candidate));
3254
87d5e4f2 3255 candidate = (candidate % (CONTAINER_UID_BASE_MAX - CONTAINER_UID_BASE_MIN)) + CONTAINER_UID_BASE_MIN;
0e7ac751
LP
3256 candidate &= (uid_t) UINT32_C(0xFFFF0000);
3257 }
3258}
3259
03cfe0d5 3260static int setup_uid_map(pid_t pid) {
fbd0b64f 3261 char uid_map[STRLEN("/proc//uid_map") + DECIMAL_STR_MAX(uid_t) + 1], line[DECIMAL_STR_MAX(uid_t)*3+3+1];
03cfe0d5
LP
3262 int r;
3263
3264 assert(pid > 1);
3265
3266 xsprintf(uid_map, "/proc/" PID_FMT "/uid_map", pid);
3267 xsprintf(line, UID_FMT " " UID_FMT " " UID_FMT "\n", 0, arg_uid_shift, arg_uid_range);
57512c89 3268 r = write_string_file(uid_map, line, WRITE_STRING_FILE_DISABLE_BUFFER);
03cfe0d5
LP
3269 if (r < 0)
3270 return log_error_errno(r, "Failed to write UID map: %m");
3271
3272 /* We always assign the same UID and GID ranges */
3273 xsprintf(uid_map, "/proc/" PID_FMT "/gid_map", pid);
57512c89 3274 r = write_string_file(uid_map, line, WRITE_STRING_FILE_DISABLE_BUFFER);
03cfe0d5
LP
3275 if (r < 0)
3276 return log_error_errno(r, "Failed to write GID map: %m");
3277
3278 return 0;
3279}
3280
9c1e04d0 3281static int nspawn_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
9c1e04d0
AP
3282 char buf[NOTIFY_BUFFER_MAX+1];
3283 char *p = NULL;
3284 struct iovec iovec = {
3285 .iov_base = buf,
3286 .iov_len = sizeof(buf)-1,
3287 };
3288 union {
3289 struct cmsghdr cmsghdr;
3290 uint8_t buf[CMSG_SPACE(sizeof(struct ucred)) +
3291 CMSG_SPACE(sizeof(int) * NOTIFY_FD_MAX)];
3292 } control = {};
3293 struct msghdr msghdr = {
3294 .msg_iov = &iovec,
3295 .msg_iovlen = 1,
3296 .msg_control = &control,
3297 .msg_controllen = sizeof(control),
3298 };
3299 struct cmsghdr *cmsg;
3300 struct ucred *ucred = NULL;
3301 ssize_t n;
3302 pid_t inner_child_pid;
3303 _cleanup_strv_free_ char **tags = NULL;
3304
3305 assert(userdata);
3306
3307 inner_child_pid = PTR_TO_PID(userdata);
3308
3309 if (revents != EPOLLIN) {
3310 log_warning("Got unexpected poll event for notify fd.");
3311 return 0;
3312 }
3313
3314 n = recvmsg(fd, &msghdr, MSG_DONTWAIT|MSG_CMSG_CLOEXEC);
3315 if (n < 0) {
3742095b 3316 if (IN_SET(errno, EAGAIN, EINTR))
9c1e04d0
AP
3317 return 0;
3318
3319 return log_warning_errno(errno, "Couldn't read notification socket: %m");
3320 }
3321 cmsg_close_all(&msghdr);
3322
3323 CMSG_FOREACH(cmsg, &msghdr) {
3324 if (cmsg->cmsg_level == SOL_SOCKET &&
3325 cmsg->cmsg_type == SCM_CREDENTIALS &&
3326 cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred))) {
3327
3328 ucred = (struct ucred*) CMSG_DATA(cmsg);
3329 }
3330 }
3331
3332 if (!ucred || ucred->pid != inner_child_pid) {
8cb57430 3333 log_debug("Received notify message without valid credentials. Ignoring.");
9c1e04d0
AP
3334 return 0;
3335 }
3336
3337 if ((size_t) n >= sizeof(buf)) {
3338 log_warning("Received notify message exceeded maximum size. Ignoring.");
3339 return 0;
3340 }
3341
3342 buf[n] = 0;
3343 tags = strv_split(buf, "\n\r");
3344 if (!tags)
3345 return log_oom();
3346
3347 if (strv_find(tags, "READY=1"))
3348 sd_notifyf(false, "READY=1\n");
3349
3350 p = strv_find_startswith(tags, "STATUS=");
3351 if (p)
3352 sd_notifyf(false, "STATUS=Container running: %s", p);
3353
3354 return 0;
3355}
3356
5773024d 3357static int setup_sd_notify_parent(sd_event *event, int fd, pid_t *inner_child_pid, sd_event_source **notify_event_source) {
9c1e04d0 3358 int r;
9c1e04d0 3359
5773024d 3360 r = sd_event_add_io(event, notify_event_source, fd, EPOLLIN, nspawn_dispatch_notify_fd, inner_child_pid);
9c1e04d0
AP
3361 if (r < 0)
3362 return log_error_errno(r, "Failed to allocate notify event source: %m");
3363
5773024d 3364 (void) sd_event_source_set_description(*notify_event_source, "nspawn-notify");
9c1e04d0
AP
3365
3366 return 0;
3367}
3368
5d961407
LP
3369static int merge_settings(Settings *settings, const char *path) {
3370 int rl;
f757855e 3371
5d961407
LP
3372 assert(settings);
3373 assert(path);
f757855e 3374
5d961407
LP
3375 /* Copy over bits from the settings, unless they have been explicitly masked by command line switches. Note
3376 * that this steals the fields of the Settings* structure, and hence modifies it. */
f757855e 3377
7732f92b
LP
3378 if ((arg_settings_mask & SETTING_START_MODE) == 0 &&
3379 settings->start_mode >= 0) {
3380 arg_start_mode = settings->start_mode;
130d3d22 3381 strv_free_and_replace(arg_parameters, settings->parameters);
f757855e
LP
3382 }
3383
a2f577fc
JL
3384 if ((arg_settings_mask & SETTING_EPHEMERAL) == 0)
3385 arg_ephemeral = settings->ephemeral;
3386
b53ede69
PW
3387 if ((arg_settings_mask & SETTING_PIVOT_ROOT) == 0 &&
3388 settings->pivot_root_new) {
3389 free_and_replace(arg_pivot_root_new, settings->pivot_root_new);
3390 free_and_replace(arg_pivot_root_old, settings->pivot_root_old);
3391 }
3392
5f932eb9 3393 if ((arg_settings_mask & SETTING_WORKING_DIRECTORY) == 0 &&
1cc6c93a
YW
3394 settings->working_directory)
3395 free_and_replace(arg_chdir, settings->working_directory);
5f932eb9 3396
f757855e 3397 if ((arg_settings_mask & SETTING_ENVIRONMENT) == 0 &&
130d3d22
YW
3398 settings->environment)
3399 strv_free_and_replace(arg_setenv, settings->environment);
f757855e
LP
3400
3401 if ((arg_settings_mask & SETTING_USER) == 0 &&
1cc6c93a
YW
3402 settings->user)
3403 free_and_replace(arg_user, settings->user);
f757855e
LP
3404
3405 if ((arg_settings_mask & SETTING_CAPABILITY) == 0) {
0e265674 3406 uint64_t plus;
f757855e 3407
0e265674
LP
3408 plus = settings->capability;
3409 if (settings_private_network(settings))
3410 plus |= (1ULL << CAP_NET_ADMIN);
3411
3412 if (!arg_settings_trusted && plus != 0) {
3413 if (settings->capability != 0)
5d961407 3414 log_warning("Ignoring Capability= setting, file %s is not trusted.", path);
0e265674 3415 } else
520e0d54 3416 arg_caps_retain |= plus;
f757855e 3417
520e0d54 3418 arg_caps_retain &= ~settings->drop_capability;
f757855e
LP
3419 }
3420
3421 if ((arg_settings_mask & SETTING_KILL_SIGNAL) == 0 &&
3422 settings->kill_signal > 0)
3423 arg_kill_signal = settings->kill_signal;
3424
3425 if ((arg_settings_mask & SETTING_PERSONALITY) == 0 &&
3426 settings->personality != PERSONALITY_INVALID)
3427 arg_personality = settings->personality;
3428
3429 if ((arg_settings_mask & SETTING_MACHINE_ID) == 0 &&
3430 !sd_id128_is_null(settings->machine_id)) {
3431
3432 if (!arg_settings_trusted)
5d961407 3433 log_warning("Ignoring MachineID= setting, file %s is not trusted.", path);
f757855e
LP
3434 else
3435 arg_uuid = settings->machine_id;
3436 }
3437
3438 if ((arg_settings_mask & SETTING_READ_ONLY) == 0 &&
3439 settings->read_only >= 0)
3440 arg_read_only = settings->read_only;
3441
3442 if ((arg_settings_mask & SETTING_VOLATILE_MODE) == 0 &&
3443 settings->volatile_mode != _VOLATILE_MODE_INVALID)
3444 arg_volatile_mode = settings->volatile_mode;
3445
3446 if ((arg_settings_mask & SETTING_CUSTOM_MOUNTS) == 0 &&
3447 settings->n_custom_mounts > 0) {
3448
3449 if (!arg_settings_trusted)
5d961407 3450 log_warning("Ignoring TemporaryFileSystem=, Bind= and BindReadOnly= settings, file %s is not trusted.", path);
f757855e
LP
3451 else {
3452 custom_mount_free_all(arg_custom_mounts, arg_n_custom_mounts);
1cc6c93a 3453 arg_custom_mounts = TAKE_PTR(settings->custom_mounts);
f757855e 3454 arg_n_custom_mounts = settings->n_custom_mounts;
f757855e
LP
3455 settings->n_custom_mounts = 0;
3456 }
3457 }
3458
3459 if ((arg_settings_mask & SETTING_NETWORK) == 0 &&
3460 (settings->private_network >= 0 ||
3461 settings->network_veth >= 0 ||
3462 settings->network_bridge ||
22b28dfd 3463 settings->network_zone ||
f757855e
LP
3464 settings->network_interfaces ||
3465 settings->network_macvlan ||
f6d6bad1
LP
3466 settings->network_ipvlan ||
3467 settings->network_veth_extra)) {
f757855e
LP
3468
3469 if (!arg_settings_trusted)
5d961407 3470 log_warning("Ignoring network settings, file %s is not trusted.", path);
f757855e 3471 else {
f6d6bad1 3472 arg_network_veth = settings_network_veth(settings);
0e265674
LP
3473 arg_private_network = settings_private_network(settings);
3474
130d3d22
YW
3475 strv_free_and_replace(arg_network_interfaces, settings->network_interfaces);
3476 strv_free_and_replace(arg_network_macvlan, settings->network_macvlan);
3477 strv_free_and_replace(arg_network_ipvlan, settings->network_ipvlan);
3478 strv_free_and_replace(arg_network_veth_extra, settings->network_veth_extra);
f6d6bad1 3479
1cc6c93a
YW
3480 free_and_replace(arg_network_bridge, settings->network_bridge);
3481 free_and_replace(arg_network_zone, settings->network_zone);
f757855e
LP
3482 }
3483 }
3484
3485 if ((arg_settings_mask & SETTING_EXPOSE_PORTS) == 0 &&
3486 settings->expose_ports) {
3487
3488 if (!arg_settings_trusted)
5d961407 3489 log_warning("Ignoring Port= setting, file %s is not trusted.", path);
f757855e
LP
3490 else {
3491 expose_port_free_all(arg_expose_ports);
1cc6c93a 3492 arg_expose_ports = TAKE_PTR(settings->expose_ports);
f757855e
LP
3493 }
3494 }
3495
0de7acce
LP
3496 if ((arg_settings_mask & SETTING_USERNS) == 0 &&
3497 settings->userns_mode != _USER_NAMESPACE_MODE_INVALID) {
3498
3499 if (!arg_settings_trusted)
5d961407 3500 log_warning("Ignoring PrivateUsers= and PrivateUsersChown= settings, file %s is not trusted.", path);
0de7acce
LP
3501 else {
3502 arg_userns_mode = settings->userns_mode;
3503 arg_uid_shift = settings->uid_shift;
3504 arg_uid_range = settings->uid_range;
3505 arg_userns_chown = settings->userns_chown;
3506 }
3507 }
3508
9c1e04d0
AP
3509 if ((arg_settings_mask & SETTING_NOTIFY_READY) == 0)
3510 arg_notify_ready = settings->notify_ready;
3511
960e4569
LP
3512 if ((arg_settings_mask & SETTING_SYSCALL_FILTER) == 0) {
3513
3514 if (!arg_settings_trusted && !strv_isempty(arg_syscall_whitelist))
5d961407 3515 log_warning("Ignoring SystemCallFilter= settings, file %s is not trusted.", path);
960e4569 3516 else {
130d3d22
YW
3517 strv_free_and_replace(arg_syscall_whitelist, settings->syscall_whitelist);
3518 strv_free_and_replace(arg_syscall_blacklist, settings->syscall_blacklist);
960e4569
LP
3519 }
3520 }
3521
bf428efb
LP
3522 for (rl = 0; rl < _RLIMIT_MAX; rl ++) {
3523 if ((arg_settings_mask & (SETTING_RLIMIT_FIRST << rl)))
3524 continue;
3525
3526 if (!settings->rlimit[rl])
3527 continue;
3528
3529 if (!arg_settings_trusted) {
5d961407 3530 log_warning("Ignoring Limit%s= setting, file '%s' is not trusted.", rlimit_to_string(rl), path);
bf428efb
LP
3531 continue;
3532 }
3533
3534 free_and_replace(arg_rlimit[rl], settings->rlimit[rl]);
3535 }
3536
3a9530e5
LP
3537 if ((arg_settings_mask & SETTING_HOSTNAME) == 0 &&
3538 settings->hostname)
3539 free_and_replace(arg_hostname, settings->hostname);
3540
66edd963
LP
3541 if ((arg_settings_mask & SETTING_NO_NEW_PRIVILEGES) == 0 &&
3542 settings->no_new_privileges >= 0)
3543 arg_no_new_privileges = settings->no_new_privileges;
3544
81f345df
LP
3545 if ((arg_settings_mask & SETTING_OOM_SCORE_ADJUST) == 0 &&
3546 settings->oom_score_adjust_set) {
3547
3548 if (!arg_settings_trusted)
5d961407 3549 log_warning("Ignoring OOMScoreAdjust= setting, file '%s' is not trusted.", path);
81f345df
LP
3550 else {
3551 arg_oom_score_adjust = settings->oom_score_adjust;
3552 arg_oom_score_adjust_set = true;
3553 }
3554 }
3555
d107bb7d
LP
3556 if ((arg_settings_mask & SETTING_CPU_AFFINITY) == 0 &&
3557 settings->cpuset) {
3558
3559 if (!arg_settings_trusted)
5d961407 3560 log_warning("Ignoring CPUAffinity= setting, file '%s' is not trusted.", path);
d107bb7d
LP
3561 else {
3562 if (arg_cpuset)
3563 CPU_FREE(arg_cpuset);
3564 arg_cpuset = TAKE_PTR(settings->cpuset);
3565 arg_cpuset_ncpus = settings->cpuset_ncpus;
3566 }
3567 }
3568
09d423e9
LP
3569 if ((arg_settings_mask & SETTING_RESOLV_CONF) == 0 &&
3570 settings->resolv_conf != _RESOLV_CONF_MODE_INVALID)
3571 arg_resolv_conf = settings->resolv_conf;
3572
4e1d6aa9
LP
3573 if ((arg_settings_mask & SETTING_LINK_JOURNAL) == 0 &&
3574 settings->link_journal != _LINK_JOURNAL_INVALID) {
3575
3576 if (!arg_settings_trusted)
3577 log_warning("Ignoring journal link setting, file '%s' is not trusted.", path);
3578 else {
3579 arg_link_journal = settings->link_journal;
3580 arg_link_journal_try = settings->link_journal_try;
3581 }
3582 }
3583
1688841f
LP
3584 if ((arg_settings_mask & SETTING_TIMEZONE) == 0 &&
3585 settings->timezone != _TIMEZONE_MODE_INVALID)
3586 arg_timezone = settings->timezone;
3587
f757855e
LP
3588 return 0;
3589}
3590
5d961407
LP
3591static int load_settings(void) {
3592 _cleanup_(settings_freep) Settings *settings = NULL;
3593 _cleanup_fclose_ FILE *f = NULL;
3594 _cleanup_free_ char *p = NULL;
3595 const char *fn, *i;
3596 int r;
3597
3598 /* If all settings are masked, there's no point in looking for
3599 * the settings file */
3600 if ((arg_settings_mask & _SETTINGS_MASK_ALL) == _SETTINGS_MASK_ALL)
3601 return 0;
3602
3603 fn = strjoina(arg_machine, ".nspawn");
3604
3605 /* We first look in the admin's directories in /etc and /run */
3606 FOREACH_STRING(i, "/etc/systemd/nspawn", "/run/systemd/nspawn") {
3607 _cleanup_free_ char *j = NULL;
3608
3609 j = strjoin(i, "/", fn);
3610 if (!j)
3611 return log_oom();
3612
3613 f = fopen(j, "re");
3614 if (f) {
3615 p = TAKE_PTR(j);
3616
3617 /* By default, we trust configuration from /etc and /run */
3618 if (arg_settings_trusted < 0)
3619 arg_settings_trusted = true;
3620
3621 break;
3622 }
3623
3624 if (errno != ENOENT)
3625 return log_error_errno(errno, "Failed to open %s: %m", j);
3626 }
3627
3628 if (!f) {
3629 /* After that, let's look for a file next to the
3630 * actual image we shall boot. */
3631
3632 if (arg_image) {
3633 p = file_in_same_dir(arg_image, fn);
3634 if (!p)
3635 return log_oom();
3636 } else if (arg_directory) {
3637 p = file_in_same_dir(arg_directory, fn);
3638 if (!p)
3639 return log_oom();
3640 }
3641
3642 if (p) {
3643 f = fopen(p, "re");
3644 if (!f && errno != ENOENT)
3645 return log_error_errno(errno, "Failed to open %s: %m", p);
3646
3647 /* By default, we do not trust configuration from /var/lib/machines */
3648 if (arg_settings_trusted < 0)
3649 arg_settings_trusted = false;
3650 }
3651 }
3652
3653 if (!f)
3654 return 0;
3655
3656 log_debug("Settings are trusted: %s", yes_no(arg_settings_trusted));
3657
3658 r = settings_load(f, p, &settings);
3659 if (r < 0)
3660 return r;
3661
3662 return merge_settings(settings, p);
3663}
3664
b0067625
ZJS
3665static int run(int master,
3666 const char* console,
2d845785 3667 DissectedImage *dissected_image,
b0067625
ZJS
3668 bool interactive,
3669 bool secondary,
3670 FDSet *fds,
3671 char veth_name[IFNAMSIZ], bool *veth_created,
3672 union in_addr_union *exposed,
3673 pid_t *pid, int *ret) {
3674
3675 static const struct sigaction sa = {
3676 .sa_handler = nop_signal_handler,
e28c7cd0 3677 .sa_flags = SA_NOCLDSTOP|SA_RESTART,
b0067625
ZJS
3678 };
3679
8e766630 3680 _cleanup_(release_lock_file) LockFile uid_shift_lock = LOCK_FILE_INIT;
b0067625
ZJS
3681 _cleanup_close_ int etc_passwd_lock = -1;
3682 _cleanup_close_pair_ int
3683 kmsg_socket_pair[2] = { -1, -1 },
3684 rtnl_socket_pair[2] = { -1, -1 },
3685 pid_socket_pair[2] = { -1, -1 },
3686 uuid_socket_pair[2] = { -1, -1 },
3687 notify_socket_pair[2] = { -1, -1 },
8199d554
LP
3688 uid_shift_socket_pair[2] = { -1, -1 },
3689 unified_cgroup_hierarchy_socket_pair[2] = { -1, -1};
3690
b0067625
ZJS
3691 _cleanup_close_ int notify_socket= -1;
3692 _cleanup_(barrier_destroy) Barrier barrier = BARRIER_NULL;
5773024d 3693 _cleanup_(sd_event_source_unrefp) sd_event_source *notify_event_source = NULL;
b0067625
ZJS
3694 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
3695 _cleanup_(pty_forward_freep) PTYForward *forward = NULL;
3696 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
abdb9b08 3697 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
b0067625
ZJS
3698 ContainerStatus container_status = 0;
3699 char last_char = 0;
3700 int ifi = 0, r;
3701 ssize_t l;
3702 sigset_t mask_chld;
d7bea6b6 3703 _cleanup_close_ int netns_fd = -1;
b0067625
ZJS
3704
3705 assert_se(sigemptyset(&mask_chld) == 0);
3706 assert_se(sigaddset(&mask_chld, SIGCHLD) == 0);
3707
3708 if (arg_userns_mode == USER_NAMESPACE_PICK) {
3709 /* When we shall pick the UID/GID range, let's first lock /etc/passwd, so that we can safely
3710 * check with getpwuid() if the specific user already exists. Note that /etc might be
3711 * read-only, in which case this will fail with EROFS. But that's really OK, as in that case we
3712 * can be reasonably sure that no users are going to be added. Note that getpwuid() checks are
3713 * really just an extra safety net. We kinda assume that the UID range we allocate from is
3714 * really ours. */
3715
3716 etc_passwd_lock = take_etc_passwd_lock(NULL);
3717 if (etc_passwd_lock < 0 && etc_passwd_lock != -EROFS)
3718 return log_error_errno(etc_passwd_lock, "Failed to take /etc/passwd lock: %m");
3719 }
3720
3721 r = barrier_create(&barrier);
3722 if (r < 0)
3723 return log_error_errno(r, "Cannot initialize IPC barrier: %m");
3724
3725 if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, kmsg_socket_pair) < 0)
3726 return log_error_errno(errno, "Failed to create kmsg socket pair: %m");
3727
3728 if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, rtnl_socket_pair) < 0)
3729 return log_error_errno(errno, "Failed to create rtnl socket pair: %m");
3730
3731 if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, pid_socket_pair) < 0)
3732 return log_error_errno(errno, "Failed to create pid socket pair: %m");
3733
3734 if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, uuid_socket_pair) < 0)
3735 return log_error_errno(errno, "Failed to create id socket pair: %m");
3736
3737 if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, notify_socket_pair) < 0)
3738 return log_error_errno(errno, "Failed to create notify socket pair: %m");
3739
3740 if (arg_userns_mode != USER_NAMESPACE_NO)
3741 if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, uid_shift_socket_pair) < 0)
3742 return log_error_errno(errno, "Failed to create uid shift socket pair: %m");
3743
8199d554
LP
3744 if (arg_unified_cgroup_hierarchy == CGROUP_UNIFIED_UNKNOWN)
3745 if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, unified_cgroup_hierarchy_socket_pair) < 0)
3746 return log_error_errno(errno, "Failed to create unified cgroup socket pair: %m");
3747
b0067625
ZJS
3748 /* Child can be killed before execv(), so handle SIGCHLD in order to interrupt
3749 * parent's blocking calls and give it a chance to call wait() and terminate. */
3750 r = sigprocmask(SIG_UNBLOCK, &mask_chld, NULL);
3751 if (r < 0)
3752 return log_error_errno(errno, "Failed to change the signal mask: %m");
3753
3754 r = sigaction(SIGCHLD, &sa, NULL);
3755 if (r < 0)
3756 return log_error_errno(errno, "Failed to install SIGCHLD handler: %m");
3757
d7bea6b6
DP
3758 if (arg_network_namespace_path) {
3759 netns_fd = open(arg_network_namespace_path, O_RDONLY|O_NOCTTY|O_CLOEXEC);
3760 if (netns_fd < 0)
3761 return log_error_errno(errno, "Cannot open file %s: %m", arg_network_namespace_path);
3762
3763 r = fd_is_network_ns(netns_fd);
6619ad88
LP
3764 if (r == -EUCLEAN)
3765 log_debug_errno(r, "Cannot determine if passed network namespace path '%s' really refers to a network namespace, assuming it does.", arg_network_namespace_path);
3766 else if (r < 0)
d7bea6b6 3767 return log_error_errno(r, "Failed to check %s fs type: %m", arg_network_namespace_path);
6619ad88
LP
3768 else if (r == 0) {
3769 log_error("Path %s doesn't refer to a network namespace, refusing.", arg_network_namespace_path);
d7bea6b6
DP
3770 return -EINVAL;
3771 }
3772 }
3773
b0067625
ZJS
3774 *pid = raw_clone(SIGCHLD|CLONE_NEWNS);
3775 if (*pid < 0)
3776 return log_error_errno(errno, "clone() failed%s: %m",
3777 errno == EINVAL ?
3778 ", do you have namespace support enabled in your kernel? (You need UTS, IPC, PID and NET namespacing built in)" : "");
3779
3780 if (*pid == 0) {
3781 /* The outer child only has a file system namespace. */
3782 barrier_set_role(&barrier, BARRIER_CHILD);
3783
3784 master = safe_close(master);
3785
3786 kmsg_socket_pair[0] = safe_close(kmsg_socket_pair[0]);
3787 rtnl_socket_pair[0] = safe_close(rtnl_socket_pair[0]);
3788 pid_socket_pair[0] = safe_close(pid_socket_pair[0]);
3789 uuid_socket_pair[0] = safe_close(uuid_socket_pair[0]);
3790 notify_socket_pair[0] = safe_close(notify_socket_pair[0]);
3791 uid_shift_socket_pair[0] = safe_close(uid_shift_socket_pair[0]);
8199d554 3792 unified_cgroup_hierarchy_socket_pair[0] = safe_close(unified_cgroup_hierarchy_socket_pair[0]);
b0067625
ZJS
3793
3794 (void) reset_all_signal_handlers();
3795 (void) reset_signal_mask();
3796
3797 r = outer_child(&barrier,
3798 arg_directory,
3799 console,
2d845785 3800 dissected_image,
b0067625
ZJS
3801 interactive,
3802 secondary,
3803 pid_socket_pair[1],
3804 uuid_socket_pair[1],
3805 notify_socket_pair[1],
3806 kmsg_socket_pair[1],
3807 rtnl_socket_pair[1],
3808 uid_shift_socket_pair[1],
8199d554 3809 unified_cgroup_hierarchy_socket_pair[1],
d7bea6b6
DP
3810 fds,
3811 netns_fd);
b0067625
ZJS
3812 if (r < 0)
3813 _exit(EXIT_FAILURE);
3814
3815 _exit(EXIT_SUCCESS);
3816 }
3817
3818 barrier_set_role(&barrier, BARRIER_PARENT);
3819
3820 fds = fdset_free(fds);
3821
3822 kmsg_socket_pair[1] = safe_close(kmsg_socket_pair[1]);
3823 rtnl_socket_pair[1] = safe_close(rtnl_socket_pair[1]);
3824 pid_socket_pair[1] = safe_close(pid_socket_pair[1]);
3825 uuid_socket_pair[1] = safe_close(uuid_socket_pair[1]);
3826 notify_socket_pair[1] = safe_close(notify_socket_pair[1]);
3827 uid_shift_socket_pair[1] = safe_close(uid_shift_socket_pair[1]);
8199d554 3828 unified_cgroup_hierarchy_socket_pair[1] = safe_close(unified_cgroup_hierarchy_socket_pair[1]);
b0067625
ZJS
3829
3830 if (arg_userns_mode != USER_NAMESPACE_NO) {
3831 /* The child just let us know the UID shift it might have read from the image. */
3832 l = recv(uid_shift_socket_pair[0], &arg_uid_shift, sizeof arg_uid_shift, 0);
3833 if (l < 0)
3834 return log_error_errno(errno, "Failed to read UID shift: %m");
b0067625
ZJS
3835 if (l != sizeof arg_uid_shift) {
3836 log_error("Short read while reading UID shift.");
3837 return -EIO;
3838 }
3839
3840 if (arg_userns_mode == USER_NAMESPACE_PICK) {
3841 /* If we are supposed to pick the UID shift, let's try to use the shift read from the
3842 * image, but if that's already in use, pick a new one, and report back to the child,
3843 * which one we now picked. */
3844
3845 r = uid_shift_pick(&arg_uid_shift, &uid_shift_lock);
3846 if (r < 0)
3847 return log_error_errno(r, "Failed to pick suitable UID/GID range: %m");
3848
3849 l = send(uid_shift_socket_pair[0], &arg_uid_shift, sizeof arg_uid_shift, MSG_NOSIGNAL);
3850 if (l < 0)
3851 return log_error_errno(errno, "Failed to send UID shift: %m");
3852 if (l != sizeof arg_uid_shift) {
3853 log_error("Short write while writing UID shift.");
3854 return -EIO;
3855 }
3856 }
3857 }
3858
8199d554
LP
3859 if (arg_unified_cgroup_hierarchy == CGROUP_UNIFIED_UNKNOWN) {
3860 /* The child let us know the support cgroup mode it might have read from the image. */
3861 l = recv(unified_cgroup_hierarchy_socket_pair[0], &arg_unified_cgroup_hierarchy, sizeof(arg_unified_cgroup_hierarchy), 0);
3862 if (l < 0)
3863 return log_error_errno(errno, "Failed to read cgroup mode: %m");
3864 if (l != sizeof(arg_unified_cgroup_hierarchy)) {
3865 log_error("Short read while reading cgroup mode.");
3866 return -EIO;
3867 }
3868 }
3869
b0067625 3870 /* Wait for the outer child. */
d2e0ac3d
LP
3871 r = wait_for_terminate_and_check("(sd-namespace)", *pid, WAIT_LOG_ABNORMAL);
3872 if (r < 0)
3873 return r;
3874 if (r != EXIT_SUCCESS)
3875 return -EIO;
b0067625
ZJS
3876
3877 /* And now retrieve the PID of the inner child. */
3878 l = recv(pid_socket_pair[0], pid, sizeof *pid, 0);
3879 if (l < 0)
3880 return log_error_errno(errno, "Failed to read inner child PID: %m");
3881 if (l != sizeof *pid) {
3882 log_error("Short read while reading inner child PID.");
3883 return -EIO;
3884 }
3885
3886 /* We also retrieve container UUID in case it was generated by outer child */
3887 l = recv(uuid_socket_pair[0], &arg_uuid, sizeof arg_uuid, 0);
3888 if (l < 0)
3889 return log_error_errno(errno, "Failed to read container machine ID: %m");
3890 if (l != sizeof(arg_uuid)) {
3891 log_error("Short read while reading container machined ID.");
3892 return -EIO;
3893 }
3894
3895 /* We also retrieve the socket used for notifications generated by outer child */
3896 notify_socket = receive_one_fd(notify_socket_pair[0], 0);
3897 if (notify_socket < 0)
3898 return log_error_errno(notify_socket,
3899 "Failed to receive notification socket from the outer child: %m");
3900
3901 log_debug("Init process invoked as PID "PID_FMT, *pid);
3902
3903 if (arg_userns_mode != USER_NAMESPACE_NO) {
3904 if (!barrier_place_and_sync(&barrier)) { /* #1 */
3905 log_error("Child died too early.");
3906 return -ESRCH;
3907 }
3908
3909 r = setup_uid_map(*pid);
3910 if (r < 0)
3911 return r;
3912
3913 (void) barrier_place(&barrier); /* #2 */
3914 }
3915
3916 if (arg_private_network) {
75116558
PS
3917 if (!arg_network_namespace_path) {
3918 /* Wait until the child has unshared its network namespace. */
3919 if (!barrier_place_and_sync(&barrier)) { /* #3 */
3920 log_error("Child died too early");
3921 return -ESRCH;
3922 }
3923 }
3924
b0067625
ZJS
3925 r = move_network_interfaces(*pid, arg_network_interfaces);
3926 if (r < 0)
3927 return r;
3928
3929 if (arg_network_veth) {
3930 r = setup_veth(arg_machine, *pid, veth_name,
3931 arg_network_bridge || arg_network_zone);
3932 if (r < 0)
3933 return r;
3934 else if (r > 0)
3935 ifi = r;
3936
3937 if (arg_network_bridge) {
3938 /* Add the interface to a bridge */
3939 r = setup_bridge(veth_name, arg_network_bridge, false);
3940 if (r < 0)
3941 return r;
3942 if (r > 0)
3943 ifi = r;
3944 } else if (arg_network_zone) {
3945 /* Add the interface to a bridge, possibly creating it */
3946 r = setup_bridge(veth_name, arg_network_zone, true);
3947 if (r < 0)
3948 return r;
3949 if (r > 0)
3950 ifi = r;
3951 }
3952 }
3953
3954 r = setup_veth_extra(arg_machine, *pid, arg_network_veth_extra);
3955 if (r < 0)
3956 return r;
3957
3958 /* We created the primary and extra veth links now; let's remember this, so that we know to
3959 remove them later on. Note that we don't bother with removing veth links that were created
3960 here when their setup failed half-way, because in that case the kernel should be able to
3961 remove them on its own, since they cannot be referenced by anything yet. */
3962 *veth_created = true;
3963
3964 r = setup_macvlan(arg_machine, *pid, arg_network_macvlan);
3965 if (r < 0)
3966 return r;
3967
3968 r = setup_ipvlan(arg_machine, *pid, arg_network_ipvlan);
3969 if (r < 0)
3970 return r;
3971 }
3972
abdb9b08
LP
3973 if (arg_register || !arg_keep_unit) {
3974 r = sd_bus_default_system(&bus);
3975 if (r < 0)
3976 return log_error_errno(r, "Failed to open system bus: %m");
e5a2d8b5
LP
3977
3978 r = sd_bus_set_close_on_exit(bus, false);
3979 if (r < 0)
3980 return log_error_errno(r, "Failed to disable close-on-exit behaviour: %m");
abdb9b08
LP
3981 }
3982
3983 if (!arg_keep_unit) {
3984 /* When a new scope is created for this container, then we'll be registered as its controller, in which
3985 * case PID 1 will send us a friendly RequestStop signal, when it is asked to terminate the
3986 * scope. Let's hook into that, and cleanly shut down the container, and print a friendly message. */
3987
75152a4d
LP
3988 r = sd_bus_match_signal_async(
3989 bus,
3990 NULL,
3991 "org.freedesktop.systemd1",
3992 NULL,
3993 "org.freedesktop.systemd1.Scope",
3994 "RequestStop",
3995 on_request_stop, NULL, PID_TO_PTR(*pid));
abdb9b08 3996 if (r < 0)
75152a4d 3997 return log_error_errno(r, "Failed to request RequestStop match: %m");
abdb9b08
LP
3998 }
3999
b0067625
ZJS
4000 if (arg_register) {
4001 r = register_machine(
abdb9b08 4002 bus,
b0067625
ZJS
4003 arg_machine,
4004 *pid,
4005 arg_directory,
4006 arg_uuid,
4007 ifi,
4008 arg_slice,
4009 arg_custom_mounts, arg_n_custom_mounts,
4010 arg_kill_signal,
4011 arg_property,
4012 arg_keep_unit,
4013 arg_container_service_name);
4014 if (r < 0)
4015 return r;
abdb9b08 4016
cd2dfc6f
LP
4017 } else if (!arg_keep_unit) {
4018 r = allocate_scope(
abdb9b08 4019 bus,
cd2dfc6f
LP
4020 arg_machine,
4021 *pid,
4022 arg_slice,
4023 arg_custom_mounts, arg_n_custom_mounts,
4024 arg_kill_signal,
4025 arg_property);
4026 if (r < 0)
4027 return r;
4028
4029 } else if (arg_slice || arg_property)
4030 log_notice("Machine and scope registration turned off, --slice= and --property= settings will have no effect.");
b0067625 4031
f0bef277 4032 r = sync_cgroup(*pid, arg_unified_cgroup_hierarchy, arg_uid_shift);
b0067625
ZJS
4033 if (r < 0)
4034 return r;
4035
720f0a2f
LP
4036 r = create_subcgroup(*pid, arg_keep_unit, arg_unified_cgroup_hierarchy);
4037 if (r < 0)
4038 return r;
b0067625 4039
de54e02d 4040 r = chown_cgroup(*pid, arg_unified_cgroup_hierarchy, arg_uid_shift);
b0067625
ZJS
4041 if (r < 0)
4042 return r;
4043
4044 /* Notify the child that the parent is ready with all
4045 * its setup (including cgroup-ification), and that
4046 * the child can now hand over control to the code to
4047 * run inside the container. */
75116558 4048 (void) barrier_place(&barrier); /* #4 */
b0067625
ZJS
4049
4050 /* Block SIGCHLD here, before notifying child.
4051 * process_pty() will handle it with the other signals. */
4052 assert_se(sigprocmask(SIG_BLOCK, &mask_chld, NULL) >= 0);
4053
4054 /* Reset signal to default */
4055 r = default_signals(SIGCHLD, -1);
4056 if (r < 0)
4057 return log_error_errno(r, "Failed to reset SIGCHLD: %m");
4058
4059 r = sd_event_new(&event);
4060 if (r < 0)
4061 return log_error_errno(r, "Failed to get default event source: %m");
4062
8fd010bb
LP
4063 (void) sd_event_set_watchdog(event, true);
4064
abdb9b08
LP
4065 if (bus) {
4066 r = sd_bus_attach_event(bus, event, 0);
4067 if (r < 0)
4068 return log_error_errno(r, "Failed to attach bus to event loop: %m");
4069 }
4070
5773024d 4071 r = setup_sd_notify_parent(event, notify_socket, PID_TO_PTR(*pid), &notify_event_source);
b0067625
ZJS
4072 if (r < 0)
4073 return r;
4074
4075 /* Let the child know that we are ready and wait that the child is completely ready now. */
75116558 4076 if (!barrier_place_and_sync(&barrier)) { /* #5 */
b0067625
ZJS
4077 log_error("Child died too early.");
4078 return -ESRCH;
4079 }
4080
4081 /* At this point we have made use of the UID we picked, and thus nss-mymachines
4082 * will make them appear in getpwuid(), thus we can release the /etc/passwd lock. */
4083 etc_passwd_lock = safe_close(etc_passwd_lock);
4084
4085 sd_notifyf(false,
4086 "STATUS=Container running.\n"
4087 "X_NSPAWN_LEADER_PID=" PID_FMT, *pid);
4088 if (!arg_notify_ready)
919f5ae0 4089 (void) sd_notify(false, "READY=1\n");
b0067625
ZJS
4090
4091 if (arg_kill_signal > 0) {
4092 /* Try to kill the init system on SIGINT or SIGTERM */
919f5ae0
LP
4093 (void) sd_event_add_signal(event, NULL, SIGINT, on_orderly_shutdown, PID_TO_PTR(*pid));
4094 (void) sd_event_add_signal(event, NULL, SIGTERM, on_orderly_shutdown, PID_TO_PTR(*pid));
b0067625
ZJS
4095 } else {
4096 /* Immediately exit */
919f5ae0
LP
4097 (void) sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
4098 (void) sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
b0067625
ZJS
4099 }
4100
6916b164 4101 /* Exit when the child exits */
919f5ae0 4102 (void) sd_event_add_signal(event, NULL, SIGCHLD, on_sigchld, PID_TO_PTR(*pid));
b0067625
ZJS
4103
4104 if (arg_expose_ports) {
4105 r = expose_port_watch_rtnl(event, rtnl_socket_pair[0], on_address_change, exposed, &rtnl);
4106 if (r < 0)
4107 return r;
4108
4109 (void) expose_port_execute(rtnl, arg_expose_ports, exposed);
4110 }
4111
4112 rtnl_socket_pair[0] = safe_close(rtnl_socket_pair[0]);
4113
4114 r = pty_forward_new(event, master,
4115 PTY_FORWARD_IGNORE_VHANGUP | (interactive ? 0 : PTY_FORWARD_READ_ONLY),
4116 &forward);
4117 if (r < 0)
4118 return log_error_errno(r, "Failed to create PTY forwarder: %m");
4119
4120 r = sd_event_loop(event);
4121 if (r < 0)
4122 return log_error_errno(r, "Failed to run event loop: %m");
4123
4124 pty_forward_get_last_char(forward, &last_char);
4125
4126 forward = pty_forward_free(forward);
4127
4128 if (!arg_quiet && last_char != '\n')
4129 putc('\n', stdout);
4130
4131 /* Kill if it is not dead yet anyway */
1d78fea2
LP
4132 if (bus) {
4133 if (arg_register)
4134 terminate_machine(bus, arg_machine);
4135 else if (!arg_keep_unit)
4136 terminate_scope(bus, arg_machine);
4137 }
b0067625
ZJS
4138
4139 /* Normally redundant, but better safe than sorry */
c67b0082 4140 (void) kill(*pid, SIGKILL);
b0067625
ZJS
4141
4142 r = wait_for_container(*pid, &container_status);
4143 *pid = 0;
4144
4145 if (r < 0)
4146 /* We failed to wait for the container, or the container exited abnormally. */
4147 return r;
4148 if (r > 0 || container_status == CONTAINER_TERMINATED) {
27e29a1e
ZJS
4149 /* r > 0 → The container exited with a non-zero status.
4150 * As a special case, we need to replace 133 with a different value,
4151 * because 133 is special-cased in the service file to reboot the container.
4152 * otherwise → The container exited with zero status and a reboot was not requested.
4153 */
2a49b612 4154 if (r == EXIT_FORCE_RESTART)
27e29a1e 4155 r = EXIT_FAILURE; /* replace 133 with the general failure code */
b0067625 4156 *ret = r;
b0067625
ZJS
4157 return 0; /* finito */
4158 }
4159
4160 /* CONTAINER_REBOOTED, loop again */
4161
4162 if (arg_keep_unit) {
4163 /* Special handling if we are running as a service: instead of simply
4164 * restarting the machine we want to restart the entire service, so let's
4165 * inform systemd about this with the special exit code 133. The service
4166 * file uses RestartForceExitStatus=133 so that this results in a full
4167 * nspawn restart. This is necessary since we might have cgroup parameters
4168 * set we want to have flushed out. */
2a49b612
ZJS
4169 *ret = EXIT_FORCE_RESTART;
4170 return 0; /* finito */
b0067625
ZJS
4171 }
4172
4173 expose_port_flush(arg_expose_ports, exposed);
4174
4175 (void) remove_veth_links(veth_name, arg_network_veth_extra);
4176 *veth_created = false;
4177 return 1; /* loop again */
4178}
4179
bf428efb 4180static int initialize_rlimits(void) {
bf428efb
LP
4181 /* The default resource limits the kernel passes to PID 1, as per kernel 4.16. Let's pass our container payload
4182 * the same values as the kernel originally passed to PID 1, in order to minimize differences between host and
4183 * container execution environments. */
4184
4185 static const struct rlimit kernel_defaults[_RLIMIT_MAX] = {
4186 [RLIMIT_AS] = { RLIM_INFINITY, RLIM_INFINITY },
4187 [RLIMIT_CORE] = { 0, RLIM_INFINITY },
4188 [RLIMIT_CPU] = { RLIM_INFINITY, RLIM_INFINITY },
4189 [RLIMIT_DATA] = { RLIM_INFINITY, RLIM_INFINITY },
4190 [RLIMIT_FSIZE] = { RLIM_INFINITY, RLIM_INFINITY },
4191 [RLIMIT_LOCKS] = { RLIM_INFINITY, RLIM_INFINITY },
4192 [RLIMIT_MEMLOCK] = { 65536, 65536 },
4193 [RLIMIT_MSGQUEUE] = { 819200, 819200 },
4194 [RLIMIT_NICE] = { 0, 0 },
4195 [RLIMIT_NOFILE] = { 1024, 4096 },
4196 [RLIMIT_RSS] = { RLIM_INFINITY, RLIM_INFINITY },
4197 [RLIMIT_RTPRIO] = { 0, 0 },
4198 [RLIMIT_RTTIME] = { RLIM_INFINITY, RLIM_INFINITY },
4199 [RLIMIT_STACK] = { 8388608, RLIM_INFINITY },
4200
4201 /* The kernel scales the default for RLIMIT_NPROC and RLIMIT_SIGPENDING based on the system's amount of
4202 * RAM. To provide best compatibility we'll read these limits off PID 1 instead of hardcoding them
4203 * here. This is safe as we know that PID 1 doesn't change these two limits and thus the original
4204 * kernel's initialization should still be valid during runtime — at least if PID 1 is systemd. Note
4205 * that PID 1 changes a number of other resource limits during early initialization which is why we
4206 * don't read the other limits from PID 1 but prefer the static table above. */
4207 };
4208
4209 int rl;
4210
4211 for (rl = 0; rl < _RLIMIT_MAX; rl++) {
bf428efb
LP
4212 /* Let's only fill in what the user hasn't explicitly configured anyway */
4213 if ((arg_settings_mask & (SETTING_RLIMIT_FIRST << rl)) == 0) {
4214 const struct rlimit *v;
4215 struct rlimit buffer;
4216
4217 if (IN_SET(rl, RLIMIT_NPROC, RLIMIT_SIGPENDING)) {
4218 /* For these two let's read the limits off PID 1. See above for an explanation. */
4219
4220 if (prlimit(1, rl, NULL, &buffer) < 0)
4221 return log_error_errno(errno, "Failed to read resource limit RLIMIT_%s of PID 1: %m", rlimit_to_string(rl));
4222
4223 v = &buffer;
4224 } else
4225 v = kernel_defaults + rl;
4226
4227 arg_rlimit[rl] = newdup(struct rlimit, v, 1);
4228 if (!arg_rlimit[rl])
4229 return log_oom();
4230 }
4231
4232 if (DEBUG_LOGGING) {
4233 _cleanup_free_ char *k = NULL;
4234
4235 (void) rlimit_format(arg_rlimit[rl], &k);
4236 log_debug("Setting RLIMIT_%s to %s.", rlimit_to_string(rl), k);
4237 }
4238 }
4239
4240 return 0;
4241}
4242
03cfe0d5 4243int main(int argc, char *argv[]) {
2d845785
LP
4244 _cleanup_free_ char *console = NULL;
4245 _cleanup_close_ int master = -1;
03cfe0d5 4246 _cleanup_fdset_free_ FDSet *fds = NULL;
2d845785 4247 int r, n_fd_passed, ret = EXIT_SUCCESS;
5aa3eba5 4248 char veth_name[IFNAMSIZ] = "";
17cbb288 4249 bool secondary = false, remove_directory = false, remove_image = false;
03cfe0d5 4250 pid_t pid = 0;
03cfe0d5 4251 union in_addr_union exposed = {};
8e766630 4252 _cleanup_(release_lock_file) LockFile tree_global_lock = LOCK_FILE_INIT, tree_local_lock = LOCK_FILE_INIT;
c67b0082
LP
4253 bool interactive, veth_created = false, remove_tmprootdir = false;
4254 char tmprootdir[] = "/tmp/nspawn-root-XXXXXX";
2d845785 4255 _cleanup_(loop_device_unrefp) LoopDevice *loop = NULL;
18b5886e
LP
4256 _cleanup_(decrypted_image_unrefp) DecryptedImage *decrypted_image = NULL;
4257 _cleanup_(dissected_image_unrefp) DissectedImage *dissected_image = NULL;
03cfe0d5
LP
4258
4259 log_parse_environment();
4260 log_open();
415fc41c 4261
7732f92b
LP
4262 /* Make sure rename_process() in the stub init process can work */
4263 saved_argv = argv;
4264 saved_argc = argc;
4265
03cfe0d5
LP
4266 r = parse_argv(argc, argv);
4267 if (r <= 0)
4268 goto finish;
4269
fba868fa
LP
4270 r = must_be_root();
4271 if (r < 0)
03cfe0d5 4272 goto finish;
fba868fa 4273
bf428efb
LP
4274 r = initialize_rlimits();
4275 if (r < 0)
4276 goto finish;
4277
f757855e
LP
4278 r = determine_names();
4279 if (r < 0)
4280 goto finish;
4281
4282 r = load_settings();
4283 if (r < 0)
4284 goto finish;
4285
4286 r = verify_arguments();
4287 if (r < 0)
4288 goto finish;
03cfe0d5 4289
8199d554
LP
4290 r = detect_unified_cgroup_hierarchy_from_environment();
4291 if (r < 0)
4292 goto finish;
4293
03cfe0d5
LP
4294 n_fd_passed = sd_listen_fds(false);
4295 if (n_fd_passed > 0) {
4296 r = fdset_new_listen_fds(&fds, false);
4297 if (r < 0) {
4298 log_error_errno(r, "Failed to collect file descriptors: %m");
4299 goto finish;
4300 }
4301 }
4302
83e803a9
ZJS
4303 /* The "default" umask. This is appropriate for most file and directory
4304 * operations performed by nspawn, and is the umask that will be used for
4305 * the child. Functions like copy_devnodes() change the umask temporarily. */
4306 umask(0022);
4307
03cfe0d5
LP
4308 if (arg_directory) {
4309 assert(!arg_image);
4310
4311 if (path_equal(arg_directory, "/") && !arg_ephemeral) {
4312 log_error("Spawning container on root directory is not supported. Consider using --ephemeral.");
4313 r = -EINVAL;
4314 goto finish;
4315 }
4316
4317 if (arg_ephemeral) {
4318 _cleanup_free_ char *np = NULL;
4319
8d4aa2bb 4320 r = chase_symlinks_and_update(&arg_directory, 0);
3f342ec4
LP
4321 if (r < 0)
4322 goto finish;
4323
03cfe0d5
LP
4324 /* If the specified path is a mount point we
4325 * generate the new snapshot immediately
4326 * inside it under a random name. However if
4327 * the specified is not a mount point we
4328 * create the new snapshot in the parent
4329 * directory, just next to it. */
e1873695 4330 r = path_is_mount_point(arg_directory, NULL, 0);
03cfe0d5
LP
4331 if (r < 0) {
4332 log_error_errno(r, "Failed to determine whether directory %s is mount point: %m", arg_directory);
4333 goto finish;
4334 }
4335 if (r > 0)
770b5ce4 4336 r = tempfn_random_child(arg_directory, "machine.", &np);
03cfe0d5 4337 else
770b5ce4 4338 r = tempfn_random(arg_directory, "machine.", &np);
03cfe0d5 4339 if (r < 0) {
0f3be6ca 4340 log_error_errno(r, "Failed to generate name for directory snapshot: %m");
03cfe0d5
LP
4341 goto finish;
4342 }
4343
4344 r = image_path_lock(np, (arg_read_only ? LOCK_SH : LOCK_EX) | LOCK_NB, &tree_global_lock, &tree_local_lock);
4345 if (r < 0) {
4346 log_error_errno(r, "Failed to lock %s: %m", np);
4347 goto finish;
4348 }
4349
17cbb288
LP
4350 r = btrfs_subvol_snapshot(arg_directory, np,
4351 (arg_read_only ? BTRFS_SNAPSHOT_READ_ONLY : 0) |
4352 BTRFS_SNAPSHOT_FALLBACK_COPY |
4353 BTRFS_SNAPSHOT_FALLBACK_DIRECTORY |
4354 BTRFS_SNAPSHOT_RECURSIVE |
4355 BTRFS_SNAPSHOT_QUOTA);
03cfe0d5
LP
4356 if (r < 0) {
4357 log_error_errno(r, "Failed to create snapshot %s from %s: %m", np, arg_directory);
4358 goto finish;
ec16945e
LP
4359 }
4360
1cc6c93a 4361 free_and_replace(arg_directory, np);
ec16945e 4362
17cbb288 4363 remove_directory = true;
30535c16
LP
4364
4365 } else {
cb638b5e 4366 r = chase_symlinks_and_update(&arg_directory, arg_template ? CHASE_NONEXISTENT : 0);
8d4aa2bb
LP
4367 if (r < 0)
4368 goto finish;
4369
30535c16
LP
4370 r = image_path_lock(arg_directory, (arg_read_only ? LOCK_SH : LOCK_EX) | LOCK_NB, &tree_global_lock, &tree_local_lock);
4371 if (r == -EBUSY) {
4372 log_error_errno(r, "Directory tree %s is currently busy.", arg_directory);
4373 goto finish;
4374 }
4375 if (r < 0) {
4376 log_error_errno(r, "Failed to lock %s: %m", arg_directory);
476b8254 4377 goto finish;
30535c16
LP
4378 }
4379
4380 if (arg_template) {
8d4aa2bb 4381 r = chase_symlinks_and_update(&arg_template, 0);
3f342ec4
LP
4382 if (r < 0)
4383 goto finish;
4384
17cbb288
LP
4385 r = btrfs_subvol_snapshot(arg_template, arg_directory,
4386 (arg_read_only ? BTRFS_SNAPSHOT_READ_ONLY : 0) |
4387 BTRFS_SNAPSHOT_FALLBACK_COPY |
4388 BTRFS_SNAPSHOT_FALLBACK_DIRECTORY |
4389 BTRFS_SNAPSHOT_FALLBACK_IMMUTABLE |
4390 BTRFS_SNAPSHOT_RECURSIVE |
4391 BTRFS_SNAPSHOT_QUOTA);
ff6c6cc1
LP
4392 if (r == -EEXIST)
4393 log_full(arg_quiet ? LOG_DEBUG : LOG_INFO,
4394 "Directory %s already exists, not populating from template %s.", arg_directory, arg_template);
4395 else if (r < 0) {
83521414 4396 log_error_errno(r, "Couldn't create snapshot %s from %s: %m", arg_directory, arg_template);
30535c16 4397 goto finish;
ff6c6cc1
LP
4398 } else
4399 log_full(arg_quiet ? LOG_DEBUG : LOG_INFO,
4400 "Populated %s from template %s.", arg_directory, arg_template);
30535c16 4401 }
ec16945e
LP
4402 }
4403
7732f92b 4404 if (arg_start_mode == START_BOOT) {
a5201ed6 4405 const char *p;
c9fe05e0 4406
a5201ed6
LP
4407 if (arg_pivot_root_new)
4408 p = prefix_roota(arg_directory, arg_pivot_root_new);
4409 else
4410 p = arg_directory;
c9fe05e0
AR
4411
4412 if (path_is_os_tree(p) <= 0) {
4413 log_error("Directory %s doesn't look like an OS root directory (os-release file is missing). Refusing.", p);
ec16945e 4414 r = -EINVAL;
1b9e5b12
LP
4415 goto finish;
4416 }
4417 } else {
c9fe05e0
AR
4418 const char *p, *q;
4419
a5201ed6
LP
4420 if (arg_pivot_root_new)
4421 p = prefix_roota(arg_directory, arg_pivot_root_new);
4422 else
4423 p = arg_directory;
c9fe05e0
AR
4424
4425 q = strjoina(p, "/usr/");
1b9e5b12 4426
c9fe05e0
AR
4427 if (laccess(q, F_OK) < 0) {
4428 log_error("Directory %s doesn't look like it has an OS tree. Refusing.", p);
ec16945e 4429 r = -EINVAL;
1b9e5b12 4430 goto finish;
1b9e5b12
LP
4431 }
4432 }
ec16945e 4433
6b9132a9 4434 } else {
ec16945e
LP
4435 assert(arg_image);
4436 assert(!arg_template);
4437
8d4aa2bb 4438 r = chase_symlinks_and_update(&arg_image, 0);
3f342ec4
LP
4439 if (r < 0)
4440 goto finish;
4441
0f3be6ca
LP
4442 if (arg_ephemeral) {
4443 _cleanup_free_ char *np = NULL;
4444
4445 r = tempfn_random(arg_image, "machine.", &np);
4446 if (r < 0) {
4447 log_error_errno(r, "Failed to generate name for image snapshot: %m");
4448 goto finish;
4449 }
4450
4451 r = image_path_lock(np, (arg_read_only ? LOCK_SH : LOCK_EX) | LOCK_NB, &tree_global_lock, &tree_local_lock);
4452 if (r < 0) {
4453 r = log_error_errno(r, "Failed to create image lock: %m");
4454 goto finish;
4455 }
4456
1c876927 4457 r = copy_file(arg_image, np, O_EXCL, arg_read_only ? 0400 : 0600, FS_NOCOW_FL, COPY_REFLINK);
0f3be6ca
LP
4458 if (r < 0) {
4459 r = log_error_errno(r, "Failed to copy image file: %m");
4460 goto finish;
4461 }
4462
1cc6c93a 4463 free_and_replace(arg_image, np);
0f3be6ca
LP
4464
4465 remove_image = true;
4466 } else {
4467 r = image_path_lock(arg_image, (arg_read_only ? LOCK_SH : LOCK_EX) | LOCK_NB, &tree_global_lock, &tree_local_lock);
4468 if (r == -EBUSY) {
4469 r = log_error_errno(r, "Disk image %s is currently busy.", arg_image);
4470 goto finish;
4471 }
4472 if (r < 0) {
4473 r = log_error_errno(r, "Failed to create image lock: %m");
4474 goto finish;
4475 }
4623e8e6 4476
78ebe980
LP
4477 if (!arg_root_hash) {
4478 r = root_hash_load(arg_image, &arg_root_hash, &arg_root_hash_size);
4479 if (r < 0) {
4480 log_error_errno(r, "Failed to load root hash file for %s: %m", arg_image);
4481 goto finish;
4482 }
4483 }
30535c16
LP
4484 }
4485
c67b0082 4486 if (!mkdtemp(tmprootdir)) {
0f3be6ca 4487 r = log_error_errno(errno, "Failed to create temporary directory: %m");
6b9132a9 4488 goto finish;
1b9e5b12 4489 }
6b9132a9 4490
c67b0082
LP
4491 remove_tmprootdir = true;
4492
4493 arg_directory = strdup(tmprootdir);
1b9e5b12
LP
4494 if (!arg_directory) {
4495 r = log_oom();
4496 goto finish;
6b9132a9 4497 }
88213476 4498
2d845785
LP
4499 r = loop_device_make_by_path(arg_image, arg_read_only ? O_RDONLY : O_RDWR, &loop);
4500 if (r < 0) {
4501 log_error_errno(r, "Failed to set up loopback block device: %m");
842f3b0f
LP
4502 goto finish;
4503 }
1b9e5b12 4504
4526113f 4505 r = dissect_image_and_warn(
e0f9e7bd 4506 loop->fd,
4526113f 4507 arg_image,
e0f9e7bd
LP
4508 arg_root_hash, arg_root_hash_size,
4509 DISSECT_IMAGE_REQUIRE_ROOT,
4510 &dissected_image);
2d845785 4511 if (r == -ENOPKG) {
4526113f 4512 /* dissected_image_and_warn() already printed a brief error message. Extend on that with more details */
2d845785
LP
4513 log_notice("Note that the disk image needs to\n"
4514 " a) either contain only a single MBR partition of type 0x83 that is marked bootable\n"
4515 " b) or contain a single GPT partition of type 0FC63DAF-8483-4772-8E79-3D69D8477DE4\n"
4516 " c) or follow http://www.freedesktop.org/wiki/Specifications/DiscoverablePartitionsSpec/\n"
4517 " d) or contain a file system without a partition table\n"
4518 "in order to be bootable with systemd-nspawn.");
1b9e5b12 4519 goto finish;
2d845785 4520 }
4526113f 4521 if (r < 0)
842f3b0f 4522 goto finish;
1b9e5b12 4523
4623e8e6
LP
4524 if (!arg_root_hash && dissected_image->can_verity)
4525 log_notice("Note: image %s contains verity information, but no root hash specified! Proceeding without integrity checking.", arg_image);
4526
4527 r = dissected_image_decrypt_interactively(dissected_image, NULL, arg_root_hash, arg_root_hash_size, 0, &decrypted_image);
1b9e5b12
LP
4528 if (r < 0)
4529 goto finish;
0f3be6ca
LP
4530
4531 /* Now that we mounted the image, let's try to remove it again, if it is ephemeral */
4532 if (remove_image && unlink(arg_image) >= 0)
4533 remove_image = false;
842f3b0f 4534 }
842f3b0f 4535
86c0dd4a 4536 r = custom_mount_prepare_all(arg_directory, arg_custom_mounts, arg_n_custom_mounts);
5a8af538
LP
4537 if (r < 0)
4538 goto finish;
4539
03cfe0d5
LP
4540 interactive =
4541 isatty(STDIN_FILENO) > 0 &&
4542 isatty(STDOUT_FILENO) > 0;
9c857b9d 4543
669fc4e5 4544 master = posix_openpt(O_RDWR|O_NOCTTY|O_CLOEXEC|O_NONBLOCK);
db7feb7e 4545 if (master < 0) {
ec16945e 4546 r = log_error_errno(errno, "Failed to acquire pseudo tty: %m");
a258bf26
LP
4547 goto finish;
4548 }
4549
611b312b
LP
4550 r = ptsname_malloc(master, &console);
4551 if (r < 0) {
4552 r = log_error_errno(r, "Failed to determine tty name: %m");
a258bf26 4553 goto finish;
68b02049
DW
4554 }
4555
4556 if (arg_selinux_apifs_context) {
4557 r = mac_selinux_apply(console, arg_selinux_apifs_context);
4558 if (r < 0)
4559 goto finish;
a258bf26
LP
4560 }
4561
a258bf26 4562 if (unlockpt(master) < 0) {
ec16945e 4563 r = log_error_errno(errno, "Failed to unlock tty: %m");
a258bf26
LP
4564 goto finish;
4565 }
4566
9c857b9d
LP
4567 if (!arg_quiet)
4568 log_info("Spawning container %s on %s.\nPress ^] three times within 1s to kill container.",
4569 arg_machine, arg_image ?: arg_directory);
4570
72c0a2c2 4571 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGCHLD, SIGWINCH, SIGTERM, SIGINT, -1) >= 0);
a258bf26 4572
66edd963 4573 if (prctl(PR_SET_CHILD_SUBREAPER, 1, 0, 0, 0) < 0) {
03cfe0d5
LP
4574 r = log_error_errno(errno, "Failed to become subreaper: %m");
4575 goto finish;
4576 }
4577
d87be9b0 4578 for (;;) {
b0067625
ZJS
4579 r = run(master,
4580 console,
2d845785 4581 dissected_image,
b0067625
ZJS
4582 interactive, secondary,
4583 fds,
4584 veth_name, &veth_created,
4585 &exposed,
4586 &pid, &ret);
4587 if (r <= 0)
d87be9b0 4588 break;
d87be9b0 4589 }
88213476
LP
4590
4591finish:
af4ec430 4592 sd_notify(false,
2a49b612
ZJS
4593 r == 0 && ret == EXIT_FORCE_RESTART ? "STOPPING=1\nSTATUS=Restarting..." :
4594 "STOPPING=1\nSTATUS=Terminating...");
af4ec430 4595
9444b1f2 4596 if (pid > 0)
c67b0082 4597 (void) kill(pid, SIGKILL);
88213476 4598
503546da 4599 /* Try to flush whatever is still queued in the pty */
6a0f896b 4600 if (master >= 0) {
1c876927 4601 (void) copy_bytes(master, STDOUT_FILENO, (uint64_t) -1, 0);
6a0f896b
LP
4602 master = safe_close(master);
4603 }
4604
4605 if (pid > 0)
4606 (void) wait_for_terminate(pid, NULL);
503546da 4607
50ebcf6c
LP
4608 pager_close();
4609
17cbb288 4610 if (remove_directory && arg_directory) {
ec16945e
LP
4611 int k;
4612
17cbb288 4613 k = rm_rf(arg_directory, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME);
ec16945e 4614 if (k < 0)
17cbb288 4615 log_warning_errno(k, "Cannot remove '%s', ignoring: %m", arg_directory);
ec16945e
LP
4616 }
4617
0f3be6ca
LP
4618 if (remove_image && arg_image) {
4619 if (unlink(arg_image) < 0)
4620 log_warning_errno(errno, "Can't remove image file '%s', ignoring: %m", arg_image);
4621 }
4622
c67b0082
LP
4623 if (remove_tmprootdir) {
4624 if (rmdir(tmprootdir) < 0)
4625 log_debug_errno(errno, "Can't remove temporary root directory '%s', ignoring: %m", tmprootdir);
4626 }
4627
785890ac
LP
4628 if (arg_machine) {
4629 const char *p;
4630
63c372cb 4631 p = strjoina("/run/systemd/nspawn/propagate/", arg_machine);
c6878637 4632 (void) rm_rf(p, REMOVE_ROOT);
785890ac
LP
4633 }
4634
7a8f6325 4635 expose_port_flush(arg_expose_ports, &exposed);
7513c5b8
LP
4636
4637 if (veth_created)
4638 (void) remove_veth_links(veth_name, arg_network_veth_extra);
22b28dfd 4639 (void) remove_bridge(arg_network_zone);
f757855e 4640
04d391da 4641 free(arg_directory);
ec16945e
LP
4642 free(arg_template);
4643 free(arg_image);
7027ff61 4644 free(arg_machine);
3a9530e5 4645 free(arg_hostname);
c74e630d 4646 free(arg_user);
b53ede69
PW
4647 free(arg_pivot_root_new);
4648 free(arg_pivot_root_old);
5f932eb9 4649 free(arg_chdir);
c74e630d 4650 strv_free(arg_setenv);
f757855e 4651 free(arg_network_bridge);
c74e630d
LP
4652 strv_free(arg_network_interfaces);
4653 strv_free(arg_network_macvlan);
4bbfe7ad 4654 strv_free(arg_network_ipvlan);
f6d6bad1 4655 strv_free(arg_network_veth_extra);
f757855e 4656 strv_free(arg_parameters);
df1fac6d
LP
4657 free(arg_network_zone);
4658 free(arg_network_namespace_path);
4659 strv_free(arg_property);
f757855e
LP
4660 custom_mount_free_all(arg_custom_mounts, arg_n_custom_mounts);
4661 expose_port_free_all(arg_expose_ports);
4623e8e6 4662 free(arg_root_hash);
bf428efb 4663 rlimit_free_all(arg_rlimit);
df1fac6d
LP
4664 strv_free(arg_syscall_whitelist);
4665 strv_free(arg_syscall_blacklist);
d107bb7d 4666 arg_cpuset = cpu_set_mfree(arg_cpuset);
6d0b55c2 4667
ec16945e 4668 return r < 0 ? EXIT_FAILURE : ret;
88213476 4669}