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