2 This file is part of systemd.
4 Copyright 2011 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/mount.h>
25 /* When we include libgen.h because we need dirname() we immediately
26 * undefine basename() since libgen.h defines it as a macro to the POSIX
27 * version which is really broken. We prefer GNU basename(). */
31 #include "alloc-util.h"
32 #include "bus-common-errors.h"
33 #include "bus-internal.h"
34 #include "bus-label.h"
40 #include "format-util.h"
42 #include "in-addr-util.h"
43 #include "local-addresses.h"
44 #include "machine-dbus.h"
47 #include "path-util.h"
48 #include "process-util.h"
49 #include "signal-util.h"
51 #include "terminal-util.h"
52 #include "user-util.h"
54 static int property_get_state(
57 const char *interface
,
59 sd_bus_message
*reply
,
61 sd_bus_error
*error
) {
63 Machine
*m
= userdata
;
71 state
= machine_state_to_string(machine_get_state(m
));
73 r
= sd_bus_message_append_basic(reply
, 's', state
);
80 static int property_get_netif(
83 const char *interface
,
85 sd_bus_message
*reply
,
87 sd_bus_error
*error
) {
89 Machine
*m
= userdata
;
95 assert_cc(sizeof(int) == sizeof(int32_t));
97 return sd_bus_message_append_array(reply
, 'i', m
->netif
, m
->n_netif
* sizeof(int));
100 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_class
, machine_class
, MachineClass
);
102 int bus_machine_method_terminate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
103 Machine
*m
= userdata
;
109 r
= bus_verify_polkit_async(
112 "org.freedesktop.machine1.manage-machines",
116 &m
->manager
->polkit_registry
,
121 return 1; /* Will call us back */
127 return sd_bus_reply_method_return(message
, NULL
);
130 int bus_machine_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
131 Machine
*m
= userdata
;
140 r
= sd_bus_message_read(message
, "si", &swho
, &signo
);
147 who
= kill_who_from_string(swho
);
149 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid kill parameter '%s'", swho
);
152 if (!SIGNAL_VALID(signo
))
153 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid signal %i", signo
);
155 r
= bus_verify_polkit_async(
158 "org.freedesktop.machine1.manage-machines",
162 &m
->manager
->polkit_registry
,
167 return 1; /* Will call us back */
169 r
= machine_kill(m
, who
, signo
);
173 return sd_bus_reply_method_return(message
, NULL
);
176 int bus_machine_method_get_addresses(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
177 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
178 Machine
*m
= userdata
;
184 r
= sd_bus_message_new_method_return(message
, &reply
);
188 r
= sd_bus_message_open_container(reply
, 'a', "(iay)");
195 _cleanup_free_
struct local_address
*addresses
= NULL
;
196 struct local_address
*a
;
199 n
= local_addresses(NULL
, 0, AF_UNSPEC
, &addresses
);
203 for (a
= addresses
, i
= 0; i
< n
; a
++, i
++) {
205 r
= sd_bus_message_open_container(reply
, 'r', "iay");
209 r
= sd_bus_message_append(reply
, "i", addresses
[i
].family
);
213 r
= sd_bus_message_append_array(reply
, 'y', &addresses
[i
].address
, FAMILY_ADDRESS_SIZE(addresses
[i
].family
));
217 r
= sd_bus_message_close_container(reply
);
225 case MACHINE_CONTAINER
: {
226 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
227 _cleanup_free_
char *us
= NULL
, *them
= NULL
;
228 _cleanup_close_
int netns_fd
= -1;
233 r
= readlink_malloc("/proc/self/ns/net", &us
);
237 p
= procfs_file_alloca(m
->leader
, "ns/net");
238 r
= readlink_malloc(p
, &them
);
243 return sd_bus_error_setf(error
, BUS_ERROR_NO_PRIVATE_NETWORKING
, "Machine %s does not use private networking", m
->name
);
245 r
= namespace_open(m
->leader
, NULL
, NULL
, &netns_fd
, NULL
, NULL
);
249 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
, 0, pair
) < 0)
254 return sd_bus_error_set_errnof(error
, errno
, "Failed to fork(): %m");
257 _cleanup_free_
struct local_address
*addresses
= NULL
;
258 struct local_address
*a
;
261 pair
[0] = safe_close(pair
[0]);
263 r
= namespace_enter(-1, -1, netns_fd
, -1, -1);
267 n
= local_addresses(NULL
, 0, AF_UNSPEC
, &addresses
);
271 for (a
= addresses
, i
= 0; i
< n
; a
++, i
++) {
272 struct iovec iov
[2] = {
273 { .iov_base
= &a
->family
, .iov_len
= sizeof(a
->family
) },
274 { .iov_base
= &a
->address
, .iov_len
= FAMILY_ADDRESS_SIZE(a
->family
) },
277 r
= writev(pair
[1], iov
, 2);
282 pair
[1] = safe_close(pair
[1]);
287 pair
[1] = safe_close(pair
[1]);
292 union in_addr_union in_addr
;
299 iov
[0] = (struct iovec
) { .iov_base
= &family
, .iov_len
= sizeof(family
) };
300 iov
[1] = (struct iovec
) { .iov_base
= &in_addr
, .iov_len
= sizeof(in_addr
) };
302 n
= recvmsg(pair
[0], &mh
, 0);
305 if ((size_t) n
< sizeof(family
))
308 r
= sd_bus_message_open_container(reply
, 'r', "iay");
312 r
= sd_bus_message_append(reply
, "i", family
);
319 if (n
!= sizeof(struct in_addr
) + sizeof(family
))
322 r
= sd_bus_message_append_array(reply
, 'y', &in_addr
.in
, sizeof(in_addr
.in
));
326 if (n
!= sizeof(struct in6_addr
) + sizeof(family
))
329 r
= sd_bus_message_append_array(reply
, 'y', &in_addr
.in6
, sizeof(in_addr
.in6
));
335 r
= sd_bus_message_close_container(reply
);
340 r
= wait_for_terminate(child
, &si
);
342 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
343 if (si
.si_code
!= CLD_EXITED
|| si
.si_status
!= EXIT_SUCCESS
)
344 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
349 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Requesting IP address data is only supported on container machines.");
352 r
= sd_bus_message_close_container(reply
);
356 return sd_bus_send(NULL
, reply
, NULL
);
359 #define EXIT_NOT_FOUND 2
361 int bus_machine_method_get_os_release(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
362 _cleanup_strv_free_
char **l
= NULL
;
363 Machine
*m
= userdata
;
372 r
= load_env_file_pairs(NULL
, "/etc/os-release", NULL
, &l
);
378 case MACHINE_CONTAINER
: {
379 _cleanup_close_
int mntns_fd
= -1, root_fd
= -1;
380 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
381 _cleanup_fclose_
FILE *f
= NULL
;
385 r
= namespace_open(m
->leader
, NULL
, &mntns_fd
, NULL
, NULL
, &root_fd
);
389 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
, 0, pair
) < 0)
394 return sd_bus_error_set_errnof(error
, errno
, "Failed to fork(): %m");
399 pair
[0] = safe_close(pair
[0]);
401 r
= namespace_enter(-1, mntns_fd
, -1, -1, root_fd
);
405 fd
= open("/etc/os-release", O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
406 if (fd
< 0 && errno
== ENOENT
) {
407 fd
= open("/usr/lib/os-release", O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
408 if (fd
< 0 && errno
== ENOENT
)
409 _exit(EXIT_NOT_FOUND
);
414 r
= copy_bytes(fd
, pair
[1], (uint64_t) -1, false);
421 pair
[1] = safe_close(pair
[1]);
423 f
= fdopen(pair
[0], "re");
429 r
= load_env_file_pairs(f
, "/etc/os-release", NULL
, &l
);
433 r
= wait_for_terminate(child
, &si
);
435 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
436 if (si
.si_code
== CLD_EXITED
&& si
.si_status
== EXIT_NOT_FOUND
)
437 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Machine does not contain OS release information");
438 if (si
.si_code
!= CLD_EXITED
|| si
.si_status
!= EXIT_SUCCESS
)
439 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
445 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Requesting OS release data is only supported on container machines.");
448 return bus_reply_pair_array(message
, l
);
451 int bus_machine_method_open_pty(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
452 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
453 _cleanup_free_
char *pty_name
= NULL
;
454 _cleanup_close_
int master
= -1;
455 Machine
*m
= userdata
;
461 r
= bus_verify_polkit_async(
464 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-open-pty" : "org.freedesktop.machine1.open-pty",
468 &m
->manager
->polkit_registry
,
473 return 1; /* Will call us back */
475 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
479 r
= ptsname_namespace(master
, &pty_name
);
483 r
= sd_bus_message_new_method_return(message
, &reply
);
487 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
491 return sd_bus_send(NULL
, reply
, NULL
);
494 static int container_bus_new(Machine
*m
, sd_bus_error
*error
, sd_bus
**ret
) {
506 case MACHINE_CONTAINER
: {
507 _cleanup_(sd_bus_unrefp
) sd_bus
*bus
= NULL
;
510 r
= sd_bus_new(&bus
);
514 if (asprintf(&address
, "x-machine-kernel:pid=%1$" PID_PRI
";x-machine-unix:pid=%1$" PID_PRI
, m
->leader
) < 0)
517 bus
->address
= address
;
518 bus
->bus_client
= true;
519 bus
->trusted
= false;
520 bus
->is_system
= true;
522 r
= sd_bus_start(bus
);
524 return sd_bus_error_set_errnof(error
, r
, "There is no system bus in container %s.", m
->name
);
540 int bus_machine_method_open_login(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
541 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
542 _cleanup_free_
char *pty_name
= NULL
;
543 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*allocated_bus
= NULL
;
544 _cleanup_close_
int master
= -1;
545 sd_bus
*container_bus
= NULL
;
546 Machine
*m
= userdata
;
547 const char *p
, *getty
;
553 r
= bus_verify_polkit_async(
556 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-login" : "org.freedesktop.machine1.login",
560 &m
->manager
->polkit_registry
,
565 return 1; /* Will call us back */
567 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
571 r
= ptsname_namespace(master
, &pty_name
);
575 p
= path_startswith(pty_name
, "/dev/pts/");
577 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "PTS name %s is invalid", pty_name
);
579 r
= container_bus_new(m
, error
, &allocated_bus
);
583 container_bus
= allocated_bus
?: m
->manager
->bus
;
585 getty
= strjoina("container-getty@", p
, ".service");
587 r
= sd_bus_call_method(
589 "org.freedesktop.systemd1",
590 "/org/freedesktop/systemd1",
591 "org.freedesktop.systemd1.Manager",
594 "ss", getty
, "replace");
598 r
= sd_bus_message_new_method_return(message
, &reply
);
602 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
606 return sd_bus_send(NULL
, reply
, NULL
);
609 int bus_machine_method_open_shell(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
610 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
, *tm
= NULL
;
611 _cleanup_free_
char *pty_name
= NULL
;
612 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*allocated_bus
= NULL
;
613 sd_bus
*container_bus
= NULL
;
614 _cleanup_close_
int master
= -1, slave
= -1;
615 _cleanup_strv_free_
char **env
= NULL
, **args
= NULL
;
616 Machine
*m
= userdata
;
617 const char *p
, *unit
, *user
, *path
, *description
, *utmp_id
;
623 r
= sd_bus_message_read(message
, "ss", &user
, &path
);
626 user
= empty_to_null(user
);
629 if (!path_is_absolute(path
))
630 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Specified path '%s' is not absolute", path
);
632 r
= sd_bus_message_read_strv(message
, &args
);
635 if (strv_isempty(args
)) {
636 args
= strv_free(args
);
638 args
= strv_new(path
, NULL
);
642 args
[0][0] = '-'; /* Tell /bin/sh that this shall be a login shell */
645 r
= sd_bus_message_read_strv(message
, &env
);
648 if (!strv_env_is_valid(env
))
649 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid environment assignments");
651 r
= bus_verify_polkit_async(
654 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-shell" : "org.freedesktop.machine1.shell",
658 &m
->manager
->polkit_registry
,
663 return 1; /* Will call us back */
665 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
669 r
= ptsname_namespace(master
, &pty_name
);
673 p
= path_startswith(pty_name
, "/dev/pts/");
676 slave
= machine_open_terminal(m
, pty_name
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
680 utmp_id
= path_startswith(pty_name
, "/dev/");
683 r
= container_bus_new(m
, error
, &allocated_bus
);
687 container_bus
= allocated_bus
?: m
->manager
->bus
;
689 r
= sd_bus_message_new_method_call(
692 "org.freedesktop.systemd1",
693 "/org/freedesktop/systemd1",
694 "org.freedesktop.systemd1.Manager",
695 "StartTransientUnit");
700 unit
= strjoina("container-shell@", p
, ".service");
701 r
= sd_bus_message_append(tm
, "ss", unit
, "fail");
706 r
= sd_bus_message_open_container(tm
, 'a', "(sv)");
710 description
= strjoina("Shell for User ", isempty(user
) ? "root" : user
);
711 r
= sd_bus_message_append(tm
,
712 "(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)",
713 "Description", "s", description
,
714 "StandardInputFileDescriptor", "h", slave
,
715 "StandardOutputFileDescriptor", "h", slave
,
716 "StandardErrorFileDescriptor", "h", slave
,
717 "SendSIGHUP", "b", true,
718 "IgnoreSIGPIPE", "b", false,
719 "KillMode", "s", "mixed",
720 "TTYReset", "b", true,
721 "UtmpIdentifier", "s", utmp_id
,
722 "UtmpMode", "s", "user",
723 "PAMName", "s", "login",
724 "WorkingDirectory", "s", "-~");
728 r
= sd_bus_message_append(tm
, "(sv)", "User", "s", isempty(user
) ? "root" : user
);
732 if (!strv_isempty(env
)) {
733 r
= sd_bus_message_open_container(tm
, 'r', "sv");
737 r
= sd_bus_message_append(tm
, "s", "Environment");
741 r
= sd_bus_message_open_container(tm
, 'v', "as");
745 r
= sd_bus_message_append_strv(tm
, env
);
749 r
= sd_bus_message_close_container(tm
);
753 r
= sd_bus_message_close_container(tm
);
759 r
= sd_bus_message_open_container(tm
, 'r', "sv");
763 r
= sd_bus_message_append(tm
, "s", "ExecStart");
767 r
= sd_bus_message_open_container(tm
, 'v', "a(sasb)");
771 r
= sd_bus_message_open_container(tm
, 'a', "(sasb)");
775 r
= sd_bus_message_open_container(tm
, 'r', "sasb");
779 r
= sd_bus_message_append(tm
, "s", path
);
783 r
= sd_bus_message_append_strv(tm
, args
);
787 r
= sd_bus_message_append(tm
, "b", true);
791 r
= sd_bus_message_close_container(tm
);
795 r
= sd_bus_message_close_container(tm
);
799 r
= sd_bus_message_close_container(tm
);
803 r
= sd_bus_message_close_container(tm
);
807 r
= sd_bus_message_close_container(tm
);
811 /* Auxiliary units */
812 r
= sd_bus_message_append(tm
, "a(sa(sv))", 0);
816 r
= sd_bus_call(container_bus
, tm
, 0, error
, NULL
);
820 slave
= safe_close(slave
);
822 r
= sd_bus_message_new_method_return(message
, &reply
);
826 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
830 return sd_bus_send(NULL
, reply
, NULL
);
833 int bus_machine_method_bind_mount(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
834 _cleanup_close_pair_
int errno_pipe_fd
[2] = { -1, -1 };
835 char mount_slave
[] = "/tmp/propagate.XXXXXX", *mount_tmp
, *mount_outside
, *p
;
836 bool mount_slave_created
= false, mount_slave_mounted
= false,
837 mount_tmp_created
= false, mount_tmp_mounted
= false,
838 mount_outside_created
= false, mount_outside_mounted
= false;
839 const char *dest
, *src
;
840 Machine
*m
= userdata
;
841 int read_only
, make_directory
;
849 if (m
->class != MACHINE_CONTAINER
)
850 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Bind mounting is only supported on container machines.");
852 r
= sd_bus_message_read(message
, "ssbb", &src
, &dest
, &read_only
, &make_directory
);
856 if (!path_is_absolute(src
) || !path_is_safe(src
))
857 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Source path must be absolute and not contain ../.");
861 else if (!path_is_absolute(dest
) || !path_is_safe(dest
))
862 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Destination path must be absolute and not contain ../.");
864 r
= bus_verify_polkit_async(
867 "org.freedesktop.machine1.manage-machines",
871 &m
->manager
->polkit_registry
,
876 return 1; /* Will call us back */
878 /* One day, when bind mounting /proc/self/fd/n works across
879 * namespace boundaries we should rework this logic to make
882 p
= strjoina("/run/systemd/nspawn/propagate/", m
->name
, "/");
883 if (laccess(p
, F_OK
) < 0)
884 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Container does not allow propagation of mount points.");
886 /* Our goal is to install a new bind mount into the container,
887 possibly read-only. This is irritatingly complex
888 unfortunately, currently.
890 First, we start by creating a private playground in /tmp,
891 that we can mount MS_SLAVE. (Which is necessary, since
892 MS_MOVE cannot be applied to mounts with MS_SHARED parent
895 if (!mkdtemp(mount_slave
))
896 return sd_bus_error_set_errnof(error
, errno
, "Failed to create playground %s: %m", mount_slave
);
898 mount_slave_created
= true;
900 if (mount(mount_slave
, mount_slave
, NULL
, MS_BIND
, NULL
) < 0) {
901 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to make bind mount %s: %m", mount_slave
);
905 mount_slave_mounted
= true;
907 if (mount(NULL
, mount_slave
, NULL
, MS_SLAVE
, NULL
) < 0) {
908 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to remount slave %s: %m", mount_slave
);
912 /* Second, we mount the source directory to a directory inside
913 of our MS_SLAVE playground. */
914 mount_tmp
= strjoina(mount_slave
, "/mount");
915 if (mkdir(mount_tmp
, 0700) < 0) {
916 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to create temporary mount point %s: %m", mount_tmp
);
920 mount_tmp_created
= true;
922 if (mount(src
, mount_tmp
, NULL
, MS_BIND
, NULL
) < 0) {
923 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to overmount %s: %m", mount_tmp
);
927 mount_tmp_mounted
= true;
929 /* Third, we remount the new bind mount read-only if requested. */
931 if (mount(NULL
, mount_tmp
, NULL
, MS_BIND
|MS_REMOUNT
|MS_RDONLY
, NULL
) < 0) {
932 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to remount read-only %s: %m", mount_tmp
);
936 /* Fourth, we move the new bind mount into the propagation
937 * directory. This way it will appear there read-only
940 mount_outside
= strjoina("/run/systemd/nspawn/propagate/", m
->name
, "/XXXXXX");
941 if (!mkdtemp(mount_outside
)) {
942 r
= sd_bus_error_set_errnof(error
, errno
, "Cannot create propagation directory %s: %m", mount_outside
);
946 mount_outside_created
= true;
948 if (mount(mount_tmp
, mount_outside
, NULL
, MS_MOVE
, NULL
) < 0) {
949 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to move %s to %s: %m", mount_tmp
, mount_outside
);
953 mount_outside_mounted
= true;
954 mount_tmp_mounted
= false;
956 (void) rmdir(mount_tmp
);
957 mount_tmp_created
= false;
959 (void) umount(mount_slave
);
960 mount_slave_mounted
= false;
962 (void) rmdir(mount_slave
);
963 mount_slave_created
= false;
965 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0) {
966 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to create pipe: %m");
972 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to fork(): %m");
977 const char *mount_inside
;
981 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
983 q
= procfs_file_alloca(m
->leader
, "ns/mnt");
984 mntfd
= open(q
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
986 r
= log_error_errno(errno
, "Failed to open mount namespace of leader: %m");
990 if (setns(mntfd
, CLONE_NEWNS
) < 0) {
991 r
= log_error_errno(errno
, "Failed to join namespace of leader: %m");
996 (void) mkdir_p(dest
, 0755);
998 /* Fifth, move the mount to the right place inside */
999 mount_inside
= strjoina("/run/systemd/nspawn/incoming/", basename(mount_outside
));
1000 if (mount(mount_inside
, dest
, NULL
, MS_MOVE
, NULL
) < 0) {
1001 r
= log_error_errno(errno
, "Failed to mount: %m");
1005 _exit(EXIT_SUCCESS
);
1008 (void) write(errno_pipe_fd
[1], &r
, sizeof(r
));
1009 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1011 _exit(EXIT_FAILURE
);
1014 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1016 r
= wait_for_terminate(child
, &si
);
1018 r
= sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
1021 if (si
.si_code
!= CLD_EXITED
) {
1022 r
= sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
1025 if (si
.si_status
!= EXIT_SUCCESS
) {
1027 if (read(errno_pipe_fd
[0], &r
, sizeof(r
)) == sizeof(r
))
1028 r
= sd_bus_error_set_errnof(error
, r
, "Failed to mount: %m");
1030 r
= sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child failed.");
1034 r
= sd_bus_reply_method_return(message
, NULL
);
1037 if (mount_outside_mounted
)
1038 umount(mount_outside
);
1039 if (mount_outside_created
)
1040 rmdir(mount_outside
);
1042 if (mount_tmp_mounted
)
1044 if (mount_tmp_created
)
1047 if (mount_slave_mounted
)
1048 umount(mount_slave
);
1049 if (mount_slave_created
)
1055 int bus_machine_method_copy(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1056 const char *src
, *dest
, *host_path
, *container_path
, *host_basename
, *host_dirname
, *container_basename
, *container_dirname
;
1057 _cleanup_close_pair_
int errno_pipe_fd
[2] = { -1, -1 };
1058 _cleanup_close_
int hostfd
= -1;
1059 Machine
*m
= userdata
;
1068 if (m
->manager
->n_operations
>= OPERATIONS_MAX
)
1069 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
, "Too many ongoing copies.");
1071 if (m
->class != MACHINE_CONTAINER
)
1072 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Copying files is only supported on container machines.");
1074 r
= sd_bus_message_read(message
, "ss", &src
, &dest
);
1078 if (!path_is_absolute(src
))
1079 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Source path must be absolute.");
1083 else if (!path_is_absolute(dest
))
1084 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Destination path must be absolute.");
1086 r
= bus_verify_polkit_async(
1089 "org.freedesktop.machine1.manage-machines",
1093 &m
->manager
->polkit_registry
,
1098 return 1; /* Will call us back */
1100 copy_from
= strstr(sd_bus_message_get_member(message
), "CopyFrom");
1103 container_path
= src
;
1107 container_path
= dest
;
1110 host_basename
= basename(host_path
);
1111 t
= strdupa(host_path
);
1112 host_dirname
= dirname(t
);
1114 container_basename
= basename(container_path
);
1115 t
= strdupa(container_path
);
1116 container_dirname
= dirname(t
);
1118 hostfd
= open(host_dirname
, O_CLOEXEC
|O_RDONLY
|O_NOCTTY
|O_DIRECTORY
);
1120 return sd_bus_error_set_errnof(error
, errno
, "Failed to open host directory %s: %m", host_dirname
);
1122 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0)
1123 return sd_bus_error_set_errnof(error
, errno
, "Failed to create pipe: %m");
1127 return sd_bus_error_set_errnof(error
, errno
, "Failed to fork(): %m");
1134 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
1136 q
= procfs_file_alloca(m
->leader
, "ns/mnt");
1137 mntfd
= open(q
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
1139 r
= log_error_errno(errno
, "Failed to open mount namespace of leader: %m");
1143 if (setns(mntfd
, CLONE_NEWNS
) < 0) {
1144 r
= log_error_errno(errno
, "Failed to join namespace of leader: %m");
1148 containerfd
= open(container_dirname
, O_CLOEXEC
|O_RDONLY
|O_NOCTTY
|O_DIRECTORY
);
1149 if (containerfd
< 0) {
1150 r
= log_error_errno(errno
, "Failed top open destination directory: %m");
1155 r
= copy_tree_at(containerfd
, container_basename
, hostfd
, host_basename
, true);
1157 r
= copy_tree_at(hostfd
, host_basename
, containerfd
, container_basename
, true);
1159 hostfd
= safe_close(hostfd
);
1160 containerfd
= safe_close(containerfd
);
1163 r
= log_error_errno(r
, "Failed to copy tree: %m");
1167 _exit(EXIT_SUCCESS
);
1170 (void) write(errno_pipe_fd
[1], &r
, sizeof(r
));
1171 _exit(EXIT_FAILURE
);
1174 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1176 /* Copying might take a while, hence install a watch on the child, and return */
1178 r
= operation_new(m
->manager
, m
, child
, message
, errno_pipe_fd
[0], NULL
);
1180 (void) sigkill_wait(child
);
1183 errno_pipe_fd
[0] = -1;
1188 int bus_machine_method_open_root_directory(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1189 _cleanup_close_
int fd
= -1;
1190 Machine
*m
= userdata
;
1196 r
= bus_verify_polkit_async(
1199 "org.freedesktop.machine1.manage-machines",
1203 &m
->manager
->polkit_registry
,
1208 return 1; /* Will call us back */
1213 fd
= open("/", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
1219 case MACHINE_CONTAINER
: {
1220 _cleanup_close_
int mntns_fd
= -1, root_fd
= -1;
1221 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
1225 r
= namespace_open(m
->leader
, NULL
, &mntns_fd
, NULL
, NULL
, &root_fd
);
1229 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, pair
) < 0)
1234 return sd_bus_error_set_errnof(error
, errno
, "Failed to fork(): %m");
1237 _cleanup_close_
int dfd
= -1;
1239 pair
[0] = safe_close(pair
[0]);
1241 r
= namespace_enter(-1, mntns_fd
, -1, -1, root_fd
);
1243 _exit(EXIT_FAILURE
);
1245 dfd
= open("/", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
1247 _exit(EXIT_FAILURE
);
1249 r
= send_one_fd(pair
[1], dfd
, 0);
1250 dfd
= safe_close(dfd
);
1252 _exit(EXIT_FAILURE
);
1254 _exit(EXIT_SUCCESS
);
1257 pair
[1] = safe_close(pair
[1]);
1259 r
= wait_for_terminate(child
, &si
);
1261 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
1262 if (si
.si_code
!= CLD_EXITED
|| si
.si_status
!= EXIT_SUCCESS
)
1263 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
1265 fd
= receive_one_fd(pair
[0], MSG_DONTWAIT
);
1273 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Opening the root directory is only supported on container machines.");
1276 return sd_bus_reply_method_return(message
, "h", fd
);
1279 const sd_bus_vtable machine_vtable
[] = {
1280 SD_BUS_VTABLE_START(0),
1281 SD_BUS_PROPERTY("Name", "s", NULL
, offsetof(Machine
, name
), SD_BUS_VTABLE_PROPERTY_CONST
),
1282 SD_BUS_PROPERTY("Id", "ay", bus_property_get_id128
, offsetof(Machine
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
1283 BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(Machine
, timestamp
), SD_BUS_VTABLE_PROPERTY_CONST
),
1284 SD_BUS_PROPERTY("Service", "s", NULL
, offsetof(Machine
, service
), SD_BUS_VTABLE_PROPERTY_CONST
),
1285 SD_BUS_PROPERTY("Unit", "s", NULL
, offsetof(Machine
, unit
), SD_BUS_VTABLE_PROPERTY_CONST
),
1286 SD_BUS_PROPERTY("Scope", "s", NULL
, offsetof(Machine
, unit
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
1287 SD_BUS_PROPERTY("Leader", "u", NULL
, offsetof(Machine
, leader
), SD_BUS_VTABLE_PROPERTY_CONST
),
1288 SD_BUS_PROPERTY("Class", "s", property_get_class
, offsetof(Machine
, class), SD_BUS_VTABLE_PROPERTY_CONST
),
1289 SD_BUS_PROPERTY("RootDirectory", "s", NULL
, offsetof(Machine
, root_directory
), SD_BUS_VTABLE_PROPERTY_CONST
),
1290 SD_BUS_PROPERTY("NetworkInterfaces", "ai", property_get_netif
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
1291 SD_BUS_PROPERTY("State", "s", property_get_state
, 0, 0),
1292 SD_BUS_METHOD("Terminate", NULL
, NULL
, bus_machine_method_terminate
, SD_BUS_VTABLE_UNPRIVILEGED
),
1293 SD_BUS_METHOD("Kill", "si", NULL
, bus_machine_method_kill
, SD_BUS_VTABLE_UNPRIVILEGED
),
1294 SD_BUS_METHOD("GetAddresses", NULL
, "a(iay)", bus_machine_method_get_addresses
, SD_BUS_VTABLE_UNPRIVILEGED
),
1295 SD_BUS_METHOD("GetOSRelease", NULL
, "a{ss}", bus_machine_method_get_os_release
, SD_BUS_VTABLE_UNPRIVILEGED
),
1296 SD_BUS_METHOD("OpenPTY", NULL
, "hs", bus_machine_method_open_pty
, SD_BUS_VTABLE_UNPRIVILEGED
),
1297 SD_BUS_METHOD("OpenLogin", NULL
, "hs", bus_machine_method_open_login
, SD_BUS_VTABLE_UNPRIVILEGED
),
1298 SD_BUS_METHOD("OpenShell", "ssasas", "hs", bus_machine_method_open_shell
, SD_BUS_VTABLE_UNPRIVILEGED
),
1299 SD_BUS_METHOD("BindMount", "ssbb", NULL
, bus_machine_method_bind_mount
, SD_BUS_VTABLE_UNPRIVILEGED
),
1300 SD_BUS_METHOD("CopyFrom", "ss", NULL
, bus_machine_method_copy
, SD_BUS_VTABLE_UNPRIVILEGED
),
1301 SD_BUS_METHOD("CopyTo", "ss", NULL
, bus_machine_method_copy
, SD_BUS_VTABLE_UNPRIVILEGED
),
1302 SD_BUS_METHOD("OpenRootDirectory", NULL
, "h", bus_machine_method_open_root_directory
, SD_BUS_VTABLE_UNPRIVILEGED
),
1306 int machine_object_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
1307 Manager
*m
= userdata
;
1317 if (streq(path
, "/org/freedesktop/machine1/machine/self")) {
1318 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1319 sd_bus_message
*message
;
1322 message
= sd_bus_get_current_message(bus
);
1326 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
1330 r
= sd_bus_creds_get_pid(creds
, &pid
);
1334 r
= manager_get_machine_by_pid(m
, pid
, &machine
);
1338 _cleanup_free_
char *e
= NULL
;
1341 p
= startswith(path
, "/org/freedesktop/machine1/machine/");
1345 e
= bus_label_unescape(p
);
1349 machine
= hashmap_get(m
->machines
, e
);
1358 char *machine_bus_path(Machine
*m
) {
1359 _cleanup_free_
char *e
= NULL
;
1363 e
= bus_label_escape(m
->name
);
1367 return strappend("/org/freedesktop/machine1/machine/", e
);
1370 int machine_node_enumerator(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
1371 _cleanup_strv_free_
char **l
= NULL
;
1372 Machine
*machine
= NULL
;
1373 Manager
*m
= userdata
;
1381 HASHMAP_FOREACH(machine
, m
->machines
, i
) {
1384 p
= machine_bus_path(machine
);
1388 r
= strv_consume(&l
, p
);
1399 int machine_send_signal(Machine
*m
, bool new_machine
) {
1400 _cleanup_free_
char *p
= NULL
;
1404 p
= machine_bus_path(m
);
1408 return sd_bus_emit_signal(
1410 "/org/freedesktop/machine1",
1411 "org.freedesktop.machine1.Manager",
1412 new_machine
? "MachineNew" : "MachineRemoved",
1416 int machine_send_create_reply(Machine
*m
, sd_bus_error
*error
) {
1417 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*c
= NULL
;
1418 _cleanup_free_
char *p
= NULL
;
1422 if (!m
->create_message
)
1425 c
= m
->create_message
;
1426 m
->create_message
= NULL
;
1429 return sd_bus_reply_method_error(c
, error
);
1431 /* Update the machine state file before we notify the client
1432 * about the result. */
1435 p
= machine_bus_path(m
);
1439 return sd_bus_reply_method_return(c
, "o", p
);