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