1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 /* When we include libgen.h because we need dirname() we immediately
9 * undefine basename() since libgen.h defines it as a macro to the POSIX
10 * version which is really broken. We prefer GNU basename(). */
14 #include "alloc-util.h"
15 #include "bus-common-errors.h"
16 #include "bus-internal.h"
17 #include "bus-label.h"
23 #include "format-util.h"
25 #include "in-addr-util.h"
26 #include "local-addresses.h"
27 #include "machine-dbus.h"
31 #include "path-util.h"
32 #include "process-util.h"
33 #include "signal-util.h"
35 #include "terminal-util.h"
36 #include "user-util.h"
38 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_class
, machine_class
, MachineClass
);
39 static BUS_DEFINE_PROPERTY_GET2(property_get_state
, "s", Machine
, machine_get_state
, machine_state_to_string
);
41 static int property_get_netif(
44 const char *interface
,
46 sd_bus_message
*reply
,
48 sd_bus_error
*error
) {
50 Machine
*m
= userdata
;
56 assert_cc(sizeof(int) == sizeof(int32_t));
58 return sd_bus_message_append_array(reply
, 'i', m
->netif
, m
->n_netif
* sizeof(int));
61 int bus_machine_method_terminate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
62 Machine
*m
= userdata
;
68 r
= bus_verify_polkit_async(
71 "org.freedesktop.machine1.manage-machines",
75 &m
->manager
->polkit_registry
,
80 return 1; /* Will call us back */
86 return sd_bus_reply_method_return(message
, NULL
);
89 int bus_machine_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
90 Machine
*m
= userdata
;
99 r
= sd_bus_message_read(message
, "si", &swho
, &signo
);
106 who
= kill_who_from_string(swho
);
108 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid kill parameter '%s'", swho
);
111 if (!SIGNAL_VALID(signo
))
112 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid signal %i", signo
);
114 r
= bus_verify_polkit_async(
117 "org.freedesktop.machine1.manage-machines",
121 &m
->manager
->polkit_registry
,
126 return 1; /* Will call us back */
128 r
= machine_kill(m
, who
, signo
);
132 return sd_bus_reply_method_return(message
, NULL
);
135 int bus_machine_method_get_addresses(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
136 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
137 Machine
*m
= userdata
;
143 r
= sd_bus_message_new_method_return(message
, &reply
);
147 r
= sd_bus_message_open_container(reply
, 'a', "(iay)");
154 _cleanup_free_
struct local_address
*addresses
= NULL
;
155 struct local_address
*a
;
158 n
= local_addresses(NULL
, 0, AF_UNSPEC
, &addresses
);
162 for (a
= addresses
, i
= 0; i
< n
; a
++, i
++) {
164 r
= sd_bus_message_open_container(reply
, 'r', "iay");
168 r
= sd_bus_message_append(reply
, "i", addresses
[i
].family
);
172 r
= sd_bus_message_append_array(reply
, 'y', &addresses
[i
].address
, FAMILY_ADDRESS_SIZE(addresses
[i
].family
));
176 r
= sd_bus_message_close_container(reply
);
184 case MACHINE_CONTAINER
: {
185 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
186 _cleanup_free_
char *us
= NULL
, *them
= NULL
;
187 _cleanup_close_
int netns_fd
= -1;
191 r
= readlink_malloc("/proc/self/ns/net", &us
);
195 p
= procfs_file_alloca(m
->leader
, "ns/net");
196 r
= readlink_malloc(p
, &them
);
201 return sd_bus_error_setf(error
, BUS_ERROR_NO_PRIVATE_NETWORKING
, "Machine %s does not use private networking", m
->name
);
203 r
= namespace_open(m
->leader
, NULL
, NULL
, &netns_fd
, NULL
, NULL
);
207 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
, 0, pair
) < 0)
210 r
= safe_fork("(sd-addr)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
, &child
);
212 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
214 _cleanup_free_
struct local_address
*addresses
= NULL
;
215 struct local_address
*a
;
218 pair
[0] = safe_close(pair
[0]);
220 r
= namespace_enter(-1, -1, netns_fd
, -1, -1);
224 n
= local_addresses(NULL
, 0, AF_UNSPEC
, &addresses
);
228 for (a
= addresses
, i
= 0; i
< n
; a
++, i
++) {
229 struct iovec iov
[2] = {
230 { .iov_base
= &a
->family
, .iov_len
= sizeof(a
->family
) },
231 { .iov_base
= &a
->address
, .iov_len
= FAMILY_ADDRESS_SIZE(a
->family
) },
234 r
= writev(pair
[1], iov
, 2);
239 pair
[1] = safe_close(pair
[1]);
244 pair
[1] = safe_close(pair
[1]);
249 union in_addr_union in_addr
;
256 iov
[0] = (struct iovec
) { .iov_base
= &family
, .iov_len
= sizeof(family
) };
257 iov
[1] = (struct iovec
) { .iov_base
= &in_addr
, .iov_len
= sizeof(in_addr
) };
259 n
= recvmsg(pair
[0], &mh
, 0);
262 if ((size_t) n
< sizeof(family
))
265 r
= sd_bus_message_open_container(reply
, 'r', "iay");
269 r
= sd_bus_message_append(reply
, "i", family
);
276 if (n
!= sizeof(struct in_addr
) + sizeof(family
))
279 r
= sd_bus_message_append_array(reply
, 'y', &in_addr
.in
, sizeof(in_addr
.in
));
283 if (n
!= sizeof(struct in6_addr
) + sizeof(family
))
286 r
= sd_bus_message_append_array(reply
, 'y', &in_addr
.in6
, sizeof(in_addr
.in6
));
292 r
= sd_bus_message_close_container(reply
);
297 r
= wait_for_terminate_and_check("(sd-addr)", child
, 0);
299 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
300 if (r
!= EXIT_SUCCESS
)
301 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
306 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Requesting IP address data is only supported on container machines.");
309 r
= sd_bus_message_close_container(reply
);
313 return sd_bus_send(NULL
, reply
, NULL
);
316 #define EXIT_NOT_FOUND 2
318 int bus_machine_method_get_os_release(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
319 _cleanup_strv_free_
char **l
= NULL
;
320 Machine
*m
= userdata
;
329 r
= load_os_release_pairs(NULL
, &l
);
335 case MACHINE_CONTAINER
: {
336 _cleanup_close_
int mntns_fd
= -1, root_fd
= -1;
337 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
338 _cleanup_fclose_
FILE *f
= NULL
;
341 r
= namespace_open(m
->leader
, NULL
, &mntns_fd
, NULL
, NULL
, &root_fd
);
345 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
, 0, pair
) < 0)
348 r
= safe_fork("(sd-osrel)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
, &child
);
350 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
354 pair
[0] = safe_close(pair
[0]);
356 r
= namespace_enter(-1, mntns_fd
, -1, -1, root_fd
);
360 r
= open_os_release(NULL
, NULL
, &fd
);
362 _exit(EXIT_NOT_FOUND
);
366 r
= copy_bytes(fd
, pair
[1], (uint64_t) -1, 0);
373 pair
[1] = safe_close(pair
[1]);
375 f
= fdopen(pair
[0], "re");
381 r
= load_env_file_pairs(f
, "/etc/os-release", NULL
, &l
);
385 r
= wait_for_terminate_and_check("(sd-osrel)", child
, 0);
387 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
388 if (r
== EXIT_NOT_FOUND
)
389 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Machine does not contain OS release information");
390 if (r
!= EXIT_SUCCESS
)
391 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
397 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Requesting OS release data is only supported on container machines.");
400 return bus_reply_pair_array(message
, l
);
403 int bus_machine_method_open_pty(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
404 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
405 _cleanup_free_
char *pty_name
= NULL
;
406 _cleanup_close_
int master
= -1;
407 Machine
*m
= userdata
;
413 r
= bus_verify_polkit_async(
416 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-open-pty" : "org.freedesktop.machine1.open-pty",
420 &m
->manager
->polkit_registry
,
425 return 1; /* Will call us back */
427 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
431 r
= ptsname_namespace(master
, &pty_name
);
435 r
= sd_bus_message_new_method_return(message
, &reply
);
439 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
443 return sd_bus_send(NULL
, reply
, NULL
);
446 static int container_bus_new(Machine
*m
, sd_bus_error
*error
, sd_bus
**ret
) {
458 case MACHINE_CONTAINER
: {
459 _cleanup_(sd_bus_unrefp
) sd_bus
*bus
= NULL
;
462 r
= sd_bus_new(&bus
);
466 if (asprintf(&address
, "x-machine-kernel:pid=%1$" PID_PRI
";x-machine-unix:pid=%1$" PID_PRI
, m
->leader
) < 0)
469 bus
->address
= address
;
470 bus
->bus_client
= true;
471 bus
->trusted
= false;
472 bus
->is_system
= true;
474 r
= sd_bus_start(bus
);
476 return sd_bus_error_set_errnof(error
, r
, "There is no system bus in container %s.", m
->name
);
480 *ret
= TAKE_PTR(bus
);
491 int bus_machine_method_open_login(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
492 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
493 _cleanup_free_
char *pty_name
= NULL
;
494 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*allocated_bus
= NULL
;
495 _cleanup_close_
int master
= -1;
496 sd_bus
*container_bus
= NULL
;
497 Machine
*m
= userdata
;
498 const char *p
, *getty
;
504 r
= bus_verify_polkit_async(
507 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-login" : "org.freedesktop.machine1.login",
511 &m
->manager
->polkit_registry
,
516 return 1; /* Will call us back */
518 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
522 r
= ptsname_namespace(master
, &pty_name
);
526 p
= path_startswith(pty_name
, "/dev/pts/");
528 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "PTS name %s is invalid", pty_name
);
530 r
= container_bus_new(m
, error
, &allocated_bus
);
534 container_bus
= allocated_bus
?: m
->manager
->bus
;
536 getty
= strjoina("container-getty@", p
, ".service");
538 r
= sd_bus_call_method(
540 "org.freedesktop.systemd1",
541 "/org/freedesktop/systemd1",
542 "org.freedesktop.systemd1.Manager",
545 "ss", getty
, "replace");
549 r
= sd_bus_message_new_method_return(message
, &reply
);
553 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
557 return sd_bus_send(NULL
, reply
, NULL
);
560 int bus_machine_method_open_shell(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
561 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
, *tm
= NULL
;
562 _cleanup_free_
char *pty_name
= NULL
;
563 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*allocated_bus
= NULL
;
564 sd_bus
*container_bus
= NULL
;
565 _cleanup_close_
int master
= -1, slave
= -1;
566 _cleanup_strv_free_
char **env
= NULL
, **args_wire
= NULL
, **args
= NULL
;
567 Machine
*m
= userdata
;
568 const char *p
, *unit
, *user
, *path
, *description
, *utmp_id
;
574 r
= sd_bus_message_read(message
, "ss", &user
, &path
);
577 user
= empty_to_null(user
);
578 r
= sd_bus_message_read_strv(message
, &args_wire
);
584 args
= new0(char*, 3 + 1);
587 args
[0] = strdup("sh");
590 args
[1] = strdup("-c");
593 r
= asprintf(&args
[2],
594 "shell=$(getent passwd %s 2>/dev/null | { IFS=: read _ _ _ _ _ _ x; echo \"$x\"; })\n"\
595 "exec \"${shell:-/bin/sh}\" -l", /* -l is means --login */
596 isempty(user
) ? "root" : user
);
602 if (!path_is_absolute(path
))
603 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Specified path '%s' is not absolute", path
);
604 args
= TAKE_PTR(args_wire
);
605 if (strv_isempty(args
)) {
606 args
= strv_free(args
);
608 args
= strv_new(path
);
614 r
= sd_bus_message_read_strv(message
, &env
);
617 if (!strv_env_is_valid(env
))
618 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid environment assignments");
620 r
= bus_verify_polkit_async(
623 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-shell" : "org.freedesktop.machine1.shell",
627 &m
->manager
->polkit_registry
,
632 return 1; /* Will call us back */
634 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
638 r
= ptsname_namespace(master
, &pty_name
);
642 p
= path_startswith(pty_name
, "/dev/pts/");
645 slave
= machine_open_terminal(m
, pty_name
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
649 utmp_id
= path_startswith(pty_name
, "/dev/");
652 r
= container_bus_new(m
, error
, &allocated_bus
);
656 container_bus
= allocated_bus
?: m
->manager
->bus
;
658 r
= sd_bus_message_new_method_call(
661 "org.freedesktop.systemd1",
662 "/org/freedesktop/systemd1",
663 "org.freedesktop.systemd1.Manager",
664 "StartTransientUnit");
669 unit
= strjoina("container-shell@", p
, ".service");
670 r
= sd_bus_message_append(tm
, "ss", unit
, "fail");
675 r
= sd_bus_message_open_container(tm
, 'a', "(sv)");
679 description
= strjoina("Shell for User ", isempty(user
) ? "root" : user
);
680 r
= sd_bus_message_append(tm
,
681 "(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)",
682 "Description", "s", description
,
683 "StandardInputFileDescriptor", "h", slave
,
684 "StandardOutputFileDescriptor", "h", slave
,
685 "StandardErrorFileDescriptor", "h", slave
,
686 "SendSIGHUP", "b", true,
687 "IgnoreSIGPIPE", "b", false,
688 "KillMode", "s", "mixed",
689 "TTYReset", "b", true,
690 "UtmpIdentifier", "s", utmp_id
,
691 "UtmpMode", "s", "user",
692 "PAMName", "s", "login",
693 "WorkingDirectory", "s", "-~");
697 r
= sd_bus_message_append(tm
, "(sv)", "User", "s", isempty(user
) ? "root" : user
);
701 if (!strv_isempty(env
)) {
702 r
= sd_bus_message_open_container(tm
, 'r', "sv");
706 r
= sd_bus_message_append(tm
, "s", "Environment");
710 r
= sd_bus_message_open_container(tm
, 'v', "as");
714 r
= sd_bus_message_append_strv(tm
, env
);
718 r
= sd_bus_message_close_container(tm
);
722 r
= sd_bus_message_close_container(tm
);
728 r
= sd_bus_message_open_container(tm
, 'r', "sv");
732 r
= sd_bus_message_append(tm
, "s", "ExecStart");
736 r
= sd_bus_message_open_container(tm
, 'v', "a(sasb)");
740 r
= sd_bus_message_open_container(tm
, 'a', "(sasb)");
744 r
= sd_bus_message_open_container(tm
, 'r', "sasb");
748 r
= sd_bus_message_append(tm
, "s", path
);
752 r
= sd_bus_message_append_strv(tm
, args
);
756 r
= sd_bus_message_append(tm
, "b", true);
760 r
= sd_bus_message_close_container(tm
);
764 r
= sd_bus_message_close_container(tm
);
768 r
= sd_bus_message_close_container(tm
);
772 r
= sd_bus_message_close_container(tm
);
776 r
= sd_bus_message_close_container(tm
);
780 /* Auxiliary units */
781 r
= sd_bus_message_append(tm
, "a(sa(sv))", 0);
785 r
= sd_bus_call(container_bus
, tm
, 0, error
, NULL
);
789 slave
= safe_close(slave
);
791 r
= sd_bus_message_new_method_return(message
, &reply
);
795 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
799 return sd_bus_send(NULL
, reply
, NULL
);
802 int bus_machine_method_bind_mount(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
803 _cleanup_close_pair_
int errno_pipe_fd
[2] = { -1, -1 };
804 char mount_slave
[] = "/tmp/propagate.XXXXXX", *mount_tmp
, *mount_outside
, *p
;
805 bool mount_slave_created
= false, mount_slave_mounted
= false,
806 mount_tmp_created
= false, mount_tmp_mounted
= false,
807 mount_outside_created
= false, mount_outside_mounted
= false;
808 _cleanup_free_
char *chased_src
= NULL
;
809 int read_only
, make_file_or_directory
;
810 const char *dest
, *src
;
811 Machine
*m
= userdata
;
820 if (m
->class != MACHINE_CONTAINER
)
821 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Bind mounting is only supported on container machines.");
823 r
= sd_bus_message_read(message
, "ssbb", &src
, &dest
, &read_only
, &make_file_or_directory
);
827 if (!path_is_absolute(src
) || !path_is_normalized(src
))
828 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Source path must be absolute and not contain ../.");
832 else if (!path_is_absolute(dest
) || !path_is_normalized(dest
))
833 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Destination path must be absolute and not contain ../.");
835 r
= bus_verify_polkit_async(
838 "org.freedesktop.machine1.manage-machines",
842 &m
->manager
->polkit_registry
,
847 return 1; /* Will call us back */
849 r
= machine_get_uid_shift(m
, &uid
);
853 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Can't bind mount on container with user namespacing applied.");
855 /* One day, when bind mounting /proc/self/fd/n works across
856 * namespace boundaries we should rework this logic to make
859 p
= strjoina("/run/systemd/nspawn/propagate/", m
->name
, "/");
860 if (laccess(p
, F_OK
) < 0)
861 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Container does not allow propagation of mount points.");
863 r
= chase_symlinks(src
, NULL
, CHASE_TRAIL_SLASH
, &chased_src
);
865 return sd_bus_error_set_errnof(error
, r
, "Failed to resolve source path: %m");
867 if (lstat(chased_src
, &st
) < 0)
868 return sd_bus_error_set_errnof(error
, errno
, "Failed to stat() source path: %m");
869 if (S_ISLNK(st
.st_mode
)) /* This shouldn't really happen, given that we just chased the symlinks above, but let's better be safe… */
870 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Source directory can't be a symbolic link");
872 /* Our goal is to install a new bind mount into the container,
873 possibly read-only. This is irritatingly complex
874 unfortunately, currently.
876 First, we start by creating a private playground in /tmp,
877 that we can mount MS_SLAVE. (Which is necessary, since
878 MS_MOVE cannot be applied to mounts with MS_SHARED parent
881 if (!mkdtemp(mount_slave
))
882 return sd_bus_error_set_errnof(error
, errno
, "Failed to create playground %s: %m", mount_slave
);
884 mount_slave_created
= true;
886 if (mount(mount_slave
, mount_slave
, NULL
, MS_BIND
, NULL
) < 0) {
887 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to make bind mount %s: %m", mount_slave
);
891 mount_slave_mounted
= true;
893 if (mount(NULL
, mount_slave
, NULL
, MS_SLAVE
, NULL
) < 0) {
894 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to remount slave %s: %m", mount_slave
);
898 /* Second, we mount the source file or directory to a directory inside of our MS_SLAVE playground. */
899 mount_tmp
= strjoina(mount_slave
, "/mount");
900 if (S_ISDIR(st
.st_mode
))
901 r
= mkdir_errno_wrapper(mount_tmp
, 0700);
903 r
= touch(mount_tmp
);
905 sd_bus_error_set_errnof(error
, errno
, "Failed to create temporary mount point %s: %m", mount_tmp
);
909 mount_tmp_created
= true;
911 if (mount(chased_src
, mount_tmp
, NULL
, MS_BIND
, NULL
) < 0) {
912 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to mount %s: %m", chased_src
);
916 mount_tmp_mounted
= true;
918 /* Third, we remount the new bind mount read-only if requested. */
920 if (mount(NULL
, mount_tmp
, NULL
, MS_BIND
|MS_REMOUNT
|MS_RDONLY
, NULL
) < 0) {
921 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to remount read-only %s: %m", mount_tmp
);
925 /* Fourth, we move the new bind mount into the propagation directory. This way it will appear there read-only
928 mount_outside
= strjoina("/run/systemd/nspawn/propagate/", m
->name
, "/XXXXXX");
929 if (S_ISDIR(st
.st_mode
))
930 r
= mkdtemp(mount_outside
) ? 0 : -errno
;
932 r
= mkostemp_safe(mount_outside
);
936 sd_bus_error_set_errnof(error
, errno
, "Cannot create propagation file or directory %s: %m", mount_outside
);
940 mount_outside_created
= true;
942 if (mount(mount_tmp
, mount_outside
, NULL
, MS_MOVE
, NULL
) < 0) {
943 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to move %s to %s: %m", mount_tmp
, mount_outside
);
947 mount_outside_mounted
= true;
948 mount_tmp_mounted
= false;
950 if (S_ISDIR(st
.st_mode
))
951 (void) rmdir(mount_tmp
);
953 (void) unlink(mount_tmp
);
954 mount_tmp_created
= false;
956 (void) umount(mount_slave
);
957 mount_slave_mounted
= false;
959 (void) rmdir(mount_slave
);
960 mount_slave_created
= false;
962 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0) {
963 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to create pipe: %m");
967 r
= safe_fork("(sd-bindmnt)", FORK_RESET_SIGNALS
, &child
);
969 sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
973 const char *mount_inside
;
977 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
979 q
= procfs_file_alloca(m
->leader
, "ns/mnt");
980 mntfd
= open(q
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
982 r
= log_error_errno(errno
, "Failed to open mount namespace of leader: %m");
986 if (setns(mntfd
, CLONE_NEWNS
) < 0) {
987 r
= log_error_errno(errno
, "Failed to join namespace of leader: %m");
991 if (make_file_or_directory
) {
992 if (S_ISDIR(st
.st_mode
))
993 (void) mkdir_p(dest
, 0755);
995 (void) mkdir_parents(dest
, 0755);
996 safe_close(open(dest
, O_CREAT
|O_EXCL
|O_WRONLY
|O_CLOEXEC
|O_NOCTTY
, 0600));
1000 /* Fifth, move the mount to the right place inside */
1001 mount_inside
= strjoina("/run/systemd/nspawn/incoming/", basename(mount_outside
));
1002 if (mount(mount_inside
, dest
, NULL
, MS_MOVE
, NULL
) < 0) {
1003 r
= log_error_errno(errno
, "Failed to mount: %m");
1007 _exit(EXIT_SUCCESS
);
1010 (void) write(errno_pipe_fd
[1], &r
, sizeof(r
));
1011 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1013 _exit(EXIT_FAILURE
);
1016 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1018 r
= wait_for_terminate_and_check("(sd-bindmnt)", child
, 0);
1020 r
= sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
1023 if (r
!= EXIT_SUCCESS
) {
1024 if (read(errno_pipe_fd
[0], &r
, sizeof(r
)) == sizeof(r
))
1025 r
= sd_bus_error_set_errnof(error
, r
, "Failed to mount: %m");
1027 r
= sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child failed.");
1031 r
= sd_bus_reply_method_return(message
, NULL
);
1034 if (mount_outside_mounted
)
1035 (void) umount(mount_outside
);
1036 if (mount_outside_created
) {
1037 if (S_ISDIR(st
.st_mode
))
1038 (void) rmdir(mount_outside
);
1040 (void) unlink(mount_outside
);
1043 if (mount_tmp_mounted
)
1044 (void) umount(mount_tmp
);
1045 if (mount_tmp_created
) {
1046 if (S_ISDIR(st
.st_mode
))
1047 (void) rmdir(mount_tmp
);
1049 (void) unlink(mount_tmp
);
1052 if (mount_slave_mounted
)
1053 (void) umount(mount_slave
);
1054 if (mount_slave_created
)
1055 (void) rmdir(mount_slave
);
1060 int bus_machine_method_copy(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1061 const char *src
, *dest
, *host_path
, *container_path
, *host_basename
, *container_basename
, *container_dirname
;
1062 _cleanup_close_pair_
int errno_pipe_fd
[2] = { -1, -1 };
1063 CopyFlags copy_flags
= COPY_REFLINK
|COPY_MERGE
;
1064 _cleanup_close_
int hostfd
= -1;
1065 Machine
*m
= userdata
;
1075 if (m
->manager
->n_operations
>= OPERATIONS_MAX
)
1076 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
, "Too many ongoing copies.");
1078 if (m
->class != MACHINE_CONTAINER
)
1079 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Copying files is only supported on container machines.");
1081 r
= sd_bus_message_read(message
, "ss", &src
, &dest
);
1085 if (!path_is_absolute(src
))
1086 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Source path must be absolute.");
1090 else if (!path_is_absolute(dest
))
1091 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Destination path must be absolute.");
1093 r
= bus_verify_polkit_async(
1096 "org.freedesktop.machine1.manage-machines",
1100 &m
->manager
->polkit_registry
,
1105 return 1; /* Will call us back */
1107 r
= machine_get_uid_shift(m
, &uid_shift
);
1111 copy_from
= strstr(sd_bus_message_get_member(message
), "CopyFrom");
1114 container_path
= src
;
1118 container_path
= dest
;
1121 host_basename
= basename(host_path
);
1123 container_basename
= basename(container_path
);
1124 t
= strdupa(container_path
);
1125 container_dirname
= dirname(t
);
1127 hostfd
= open_parent(host_path
, O_CLOEXEC
, 0);
1129 return sd_bus_error_set_errnof(error
, hostfd
, "Failed to open host directory %s: %m", host_path
);
1131 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0)
1132 return sd_bus_error_set_errnof(error
, errno
, "Failed to create pipe: %m");
1134 r
= safe_fork("(sd-copy)", FORK_RESET_SIGNALS
, &child
);
1136 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
1142 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
1144 q
= procfs_file_alloca(m
->leader
, "ns/mnt");
1145 mntfd
= open(q
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
1147 r
= log_error_errno(errno
, "Failed to open mount namespace of leader: %m");
1151 if (setns(mntfd
, CLONE_NEWNS
) < 0) {
1152 r
= log_error_errno(errno
, "Failed to join namespace of leader: %m");
1156 containerfd
= open(container_dirname
, O_CLOEXEC
|O_RDONLY
|O_NOCTTY
|O_DIRECTORY
);
1157 if (containerfd
< 0) {
1158 r
= log_error_errno(errno
, "Failed to open destination directory: %m");
1162 /* Run the actual copy operation. Note that when an UID shift is set we'll either clamp the UID/GID to
1163 * 0 or to the actual UID shift depending on the direction we copy. If no UID shift is set we'll copy
1164 * the UID/GIDs as they are. */
1166 r
= copy_tree_at(containerfd
, container_basename
, hostfd
, host_basename
, uid_shift
== 0 ? UID_INVALID
: 0, uid_shift
== 0 ? GID_INVALID
: 0, copy_flags
);
1168 r
= copy_tree_at(hostfd
, host_basename
, containerfd
, container_basename
, uid_shift
== 0 ? UID_INVALID
: uid_shift
, uid_shift
== 0 ? GID_INVALID
: uid_shift
, copy_flags
);
1170 hostfd
= safe_close(hostfd
);
1171 containerfd
= safe_close(containerfd
);
1174 r
= log_error_errno(r
, "Failed to copy tree: %m");
1178 _exit(EXIT_SUCCESS
);
1181 (void) write(errno_pipe_fd
[1], &r
, sizeof(r
));
1182 _exit(EXIT_FAILURE
);
1185 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1187 /* Copying might take a while, hence install a watch on the child, and return */
1189 r
= operation_new(m
->manager
, m
, child
, message
, errno_pipe_fd
[0], NULL
);
1191 (void) sigkill_wait(child
);
1194 errno_pipe_fd
[0] = -1;
1199 int bus_machine_method_open_root_directory(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1200 _cleanup_close_
int fd
= -1;
1201 Machine
*m
= userdata
;
1207 r
= bus_verify_polkit_async(
1210 "org.freedesktop.machine1.manage-machines",
1214 &m
->manager
->polkit_registry
,
1219 return 1; /* Will call us back */
1224 fd
= open("/", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
1230 case MACHINE_CONTAINER
: {
1231 _cleanup_close_
int mntns_fd
= -1, root_fd
= -1;
1232 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
1235 r
= namespace_open(m
->leader
, NULL
, &mntns_fd
, NULL
, NULL
, &root_fd
);
1239 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, pair
) < 0)
1242 r
= safe_fork("(sd-openroot)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
, &child
);
1244 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
1246 _cleanup_close_
int dfd
= -1;
1248 pair
[0] = safe_close(pair
[0]);
1250 r
= namespace_enter(-1, mntns_fd
, -1, -1, root_fd
);
1252 _exit(EXIT_FAILURE
);
1254 dfd
= open("/", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
1256 _exit(EXIT_FAILURE
);
1258 r
= send_one_fd(pair
[1], dfd
, 0);
1259 dfd
= safe_close(dfd
);
1261 _exit(EXIT_FAILURE
);
1263 _exit(EXIT_SUCCESS
);
1266 pair
[1] = safe_close(pair
[1]);
1268 r
= wait_for_terminate_and_check("(sd-openroot)", child
, 0);
1270 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
1271 if (r
!= EXIT_SUCCESS
)
1272 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
1274 fd
= receive_one_fd(pair
[0], MSG_DONTWAIT
);
1282 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Opening the root directory is only supported on container machines.");
1285 return sd_bus_reply_method_return(message
, "h", fd
);
1288 int bus_machine_method_get_uid_shift(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1289 Machine
*m
= userdata
;
1296 /* You wonder why this is a method and not a property? Well, properties are not supposed to return errors, but
1297 * we kinda have to for this. */
1299 if (m
->class == MACHINE_HOST
)
1300 return sd_bus_reply_method_return(message
, "u", UINT32_C(0));
1302 if (m
->class != MACHINE_CONTAINER
)
1303 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "UID/GID shift may only be determined for container machines.");
1305 r
= machine_get_uid_shift(m
, &shift
);
1307 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Machine %s uses a complex UID/GID mapping, cannot determine shift", m
->name
);
1311 return sd_bus_reply_method_return(message
, "u", (uint32_t) shift
);
1314 const sd_bus_vtable machine_vtable
[] = {
1315 SD_BUS_VTABLE_START(0),
1316 SD_BUS_PROPERTY("Name", "s", NULL
, offsetof(Machine
, name
), SD_BUS_VTABLE_PROPERTY_CONST
),
1317 SD_BUS_PROPERTY("Id", "ay", bus_property_get_id128
, offsetof(Machine
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
1318 BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(Machine
, timestamp
), SD_BUS_VTABLE_PROPERTY_CONST
),
1319 SD_BUS_PROPERTY("Service", "s", NULL
, offsetof(Machine
, service
), SD_BUS_VTABLE_PROPERTY_CONST
),
1320 SD_BUS_PROPERTY("Unit", "s", NULL
, offsetof(Machine
, unit
), SD_BUS_VTABLE_PROPERTY_CONST
),
1321 SD_BUS_PROPERTY("Scope", "s", NULL
, offsetof(Machine
, unit
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
1322 SD_BUS_PROPERTY("Leader", "u", NULL
, offsetof(Machine
, leader
), SD_BUS_VTABLE_PROPERTY_CONST
),
1323 SD_BUS_PROPERTY("Class", "s", property_get_class
, offsetof(Machine
, class), SD_BUS_VTABLE_PROPERTY_CONST
),
1324 SD_BUS_PROPERTY("RootDirectory", "s", NULL
, offsetof(Machine
, root_directory
), SD_BUS_VTABLE_PROPERTY_CONST
),
1325 SD_BUS_PROPERTY("NetworkInterfaces", "ai", property_get_netif
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
1326 SD_BUS_PROPERTY("State", "s", property_get_state
, 0, 0),
1327 SD_BUS_METHOD("Terminate", NULL
, NULL
, bus_machine_method_terminate
, SD_BUS_VTABLE_UNPRIVILEGED
),
1328 SD_BUS_METHOD("Kill", "si", NULL
, bus_machine_method_kill
, SD_BUS_VTABLE_UNPRIVILEGED
),
1329 SD_BUS_METHOD("GetAddresses", NULL
, "a(iay)", bus_machine_method_get_addresses
, SD_BUS_VTABLE_UNPRIVILEGED
),
1330 SD_BUS_METHOD("GetOSRelease", NULL
, "a{ss}", bus_machine_method_get_os_release
, SD_BUS_VTABLE_UNPRIVILEGED
),
1331 SD_BUS_METHOD("GetUIDShift", NULL
, "u", bus_machine_method_get_uid_shift
, SD_BUS_VTABLE_UNPRIVILEGED
),
1332 SD_BUS_METHOD("OpenPTY", NULL
, "hs", bus_machine_method_open_pty
, SD_BUS_VTABLE_UNPRIVILEGED
),
1333 SD_BUS_METHOD("OpenLogin", NULL
, "hs", bus_machine_method_open_login
, SD_BUS_VTABLE_UNPRIVILEGED
),
1334 SD_BUS_METHOD("OpenShell", "ssasas", "hs", bus_machine_method_open_shell
, SD_BUS_VTABLE_UNPRIVILEGED
),
1335 SD_BUS_METHOD("BindMount", "ssbb", NULL
, bus_machine_method_bind_mount
, SD_BUS_VTABLE_UNPRIVILEGED
),
1336 SD_BUS_METHOD("CopyFrom", "ss", NULL
, bus_machine_method_copy
, SD_BUS_VTABLE_UNPRIVILEGED
),
1337 SD_BUS_METHOD("CopyTo", "ss", NULL
, bus_machine_method_copy
, SD_BUS_VTABLE_UNPRIVILEGED
),
1338 SD_BUS_METHOD("OpenRootDirectory", NULL
, "h", bus_machine_method_open_root_directory
, SD_BUS_VTABLE_UNPRIVILEGED
),
1342 int machine_object_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
1343 Manager
*m
= userdata
;
1353 if (streq(path
, "/org/freedesktop/machine1/machine/self")) {
1354 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1355 sd_bus_message
*message
;
1358 message
= sd_bus_get_current_message(bus
);
1362 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
1366 r
= sd_bus_creds_get_pid(creds
, &pid
);
1370 r
= manager_get_machine_by_pid(m
, pid
, &machine
);
1374 _cleanup_free_
char *e
= NULL
;
1377 p
= startswith(path
, "/org/freedesktop/machine1/machine/");
1381 e
= bus_label_unescape(p
);
1385 machine
= hashmap_get(m
->machines
, e
);
1394 char *machine_bus_path(Machine
*m
) {
1395 _cleanup_free_
char *e
= NULL
;
1399 e
= bus_label_escape(m
->name
);
1403 return strappend("/org/freedesktop/machine1/machine/", e
);
1406 int machine_node_enumerator(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
1407 _cleanup_strv_free_
char **l
= NULL
;
1408 Machine
*machine
= NULL
;
1409 Manager
*m
= userdata
;
1417 HASHMAP_FOREACH(machine
, m
->machines
, i
) {
1420 p
= machine_bus_path(machine
);
1424 r
= strv_consume(&l
, p
);
1429 *nodes
= TAKE_PTR(l
);
1434 int machine_send_signal(Machine
*m
, bool new_machine
) {
1435 _cleanup_free_
char *p
= NULL
;
1439 p
= machine_bus_path(m
);
1443 return sd_bus_emit_signal(
1445 "/org/freedesktop/machine1",
1446 "org.freedesktop.machine1.Manager",
1447 new_machine
? "MachineNew" : "MachineRemoved",
1451 int machine_send_create_reply(Machine
*m
, sd_bus_error
*error
) {
1452 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*c
= NULL
;
1453 _cleanup_free_
char *p
= NULL
;
1457 if (!m
->create_message
)
1460 c
= TAKE_PTR(m
->create_message
);
1463 return sd_bus_reply_method_error(c
, error
);
1465 /* Update the machine state file before we notify the client
1466 * about the result. */
1469 p
= machine_bus_path(m
);
1473 return sd_bus_reply_method_return(c
, "o", p
);