+static int reboot_machine(sd_bus *bus, char **args, unsigned n) {
+ _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+ unsigned i;
+ int r;
+
+ assert(args);
+
+ if (arg_transport != BUS_TRANSPORT_LOCAL) {
+ log_error("Reboot only supported on local machines.");
+ return -ENOTSUP;
+ }
+
+ for (i = 1; i < n; i++) {
+ _cleanup_bus_message_unref_ sd_bus_message *reply = NULL, *reply2 = NULL;
+ const char *path;
+ uint32_t leader;
+
+ r = sd_bus_call_method(
+ bus,
+ "org.freedesktop.machine1",
+ "/org/freedesktop/machine1",
+ "org.freedesktop.machine1.Manager",
+ "GetMachine",
+ &error,
+ &reply,
+ "s", args[i]);
+
+ if (r < 0) {
+ log_error("Could not get path to machine: %s", bus_error_message(&error, -r));
+ return r;
+ }
+
+ r = sd_bus_message_read(reply, "o", &path);
+ if (r < 0)
+ return bus_log_parse_error(r);
+
+ r = sd_bus_get_property(
+ bus,
+ "org.freedesktop.machine1",
+ path,
+ "org.freedesktop.machine1.Machine",
+ "Leader",
+ &error,
+ &reply2,
+ "u");
+ if (r < 0) {
+ log_error("Failed to retrieve PID of leader: %s", strerror(-r));
+ return r;
+ }
+
+ r = sd_bus_message_read(reply2, "u", &leader);
+ if (r < 0)
+ return bus_log_parse_error(r);
+
+ if (kill(leader, SIGINT) < 0) {
+ log_error("Failed to kill init process " PID_FMT ": %m", (pid_t) leader);
+ return -errno;
+ }
+ }
+
+ return 0;
+}
+
+static int openpt_in_namespace(pid_t pid, int flags) {
+ _cleanup_close_pipe_ int pair[2] = { -1, -1 };
+ _cleanup_close_ int pidnsfd = -1, mntnsfd = -1, rootfd = -1;
+ union {
+ struct cmsghdr cmsghdr;
+ uint8_t buf[CMSG_SPACE(sizeof(int))];
+ } control = {};
+ struct msghdr mh = {
+ .msg_control = &control,
+ .msg_controllen = sizeof(control),
+ };
+ struct cmsghdr *cmsg;
+ int master = -1, r;
+ pid_t child;
+ siginfo_t si;
+
+ r = namespace_open(pid, &pidnsfd, &mntnsfd, &rootfd);
+ if (r < 0)
+ return r;
+
+ if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) < 0)
+ return -errno;
+
+ child = fork();
+ if (child < 0)
+ return -errno;
+
+ if (child == 0) {
+ close_nointr_nofail(pair[0]);
+ pair[0] = -1;
+
+ r = namespace_enter(pidnsfd, mntnsfd, rootfd);
+ if (r < 0)
+ _exit(EXIT_FAILURE);
+
+ master = posix_openpt(flags);
+ if (master < 0)
+ _exit(EXIT_FAILURE);
+
+ cmsg = CMSG_FIRSTHDR(&mh);
+ cmsg->cmsg_level = SOL_SOCKET;
+ cmsg->cmsg_type = SCM_RIGHTS;
+ cmsg->cmsg_len = CMSG_LEN(sizeof(int));
+ memcpy(CMSG_DATA(cmsg), &master, sizeof(int));
+
+ mh.msg_controllen = cmsg->cmsg_len;
+
+ if (sendmsg(pair[1], &mh, MSG_NOSIGNAL) < 0)
+ _exit(EXIT_FAILURE);
+
+ _exit(EXIT_SUCCESS);
+ }
+
+ close_nointr_nofail(pair[1]);
+ pair[1] = -1;
+
+ r = wait_for_terminate(child, &si);
+ if (r < 0 || si.si_code != CLD_EXITED || si.si_status != EXIT_SUCCESS) {
+
+ return r < 0 ? r : -EIO;
+ }
+
+ if (recvmsg(pair[0], &mh, MSG_NOSIGNAL|MSG_CMSG_CLOEXEC) < 0)
+ return -errno;
+
+ for (cmsg = CMSG_FIRSTHDR(&mh); cmsg; cmsg = CMSG_NXTHDR(&mh, cmsg))
+ if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
+ int *fds;
+ unsigned n_fds;
+
+ fds = (int*) CMSG_DATA(cmsg);
+ n_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
+
+ if (n_fds != 1) {
+ close_many(fds, n_fds);
+ return -EIO;
+ }
+
+ master = fds[0];
+ }
+
+ if (master < 0)
+ return -EIO;
+
+ return master;
+}
+
+static int login_machine(sd_bus *bus, char **args, unsigned n) {
+ _cleanup_bus_message_unref_ sd_bus_message *reply = NULL, *reply2 = NULL, *reply3 = NULL;
+ _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+ _cleanup_bus_unref_ sd_bus *container_bus = NULL;
+ _cleanup_close_ int master = -1;
+ _cleanup_free_ char *getty = NULL;
+ const char *path, *pty, *p;
+ uint32_t leader;
+ sigset_t mask;
+ int r;
+
+ assert(bus);
+ assert(args);
+
+ if (arg_transport != BUS_TRANSPORT_LOCAL) {
+ log_error("Login only supported on local machines.");
+ return -ENOTSUP;
+ }
+
+ r = sd_bus_call_method(
+ bus,
+ "org.freedesktop.machine1",
+ "/org/freedesktop/machine1",
+ "org.freedesktop.machine1.Manager",
+ "GetMachine",
+ &error,
+ &reply,
+ "s", args[1]);
+ if (r < 0) {
+ log_error("Could not get path to machine: %s", bus_error_message(&error, -r));
+ return r;
+ }
+
+ r = sd_bus_message_read(reply, "o", &path);
+ if (r < 0)
+ return bus_log_parse_error(r);
+
+ r = sd_bus_get_property(
+ bus,
+ "org.freedesktop.machine1",
+ path,
+ "org.freedesktop.machine1.Machine",
+ "Leader",
+ &error,
+ &reply2,
+ "u");
+ if (r < 0) {
+ log_error("Failed to retrieve PID of leader: %s", strerror(-r));
+ return r;
+ }
+
+ r = sd_bus_message_read(reply2, "u", &leader);
+ if (r < 0)
+ return bus_log_parse_error(r);
+
+ master = openpt_in_namespace(leader, O_RDWR|O_NOCTTY|O_CLOEXEC|O_NDELAY);
+ if (master < 0) {
+ log_error("Failed to acquire pseudo tty: %s", strerror(-master));
+ return master;
+ }
+
+ pty = ptsname(master);
+ if (!pty) {
+ log_error("Failed to get pty name: %m");
+ return -errno;
+ }
+
+ p = startswith(pty, "/dev/pts/");
+ if (!p) {
+ log_error("Invalid pty name %s.", pty);
+ return -EIO;
+ }
+
+ r = sd_bus_open_system_container(&container_bus, args[1]);
+ if (r < 0) {
+ log_error("Failed to get container bus: %s", strerror(-r));
+ return r;
+ }
+
+ getty = strjoin("container-getty@", p, ".service", NULL);
+ if (!getty)
+ return log_oom();
+
+ if (unlockpt(master) < 0) {
+ log_error("Failed to unlock tty: %m");
+ return -errno;
+ }
+
+ r = sd_bus_call_method(container_bus,
+ "org.freedesktop.systemd1",
+ "/org/freedesktop/systemd1",
+ "org.freedesktop.systemd1.Manager",
+ "StartUnit",
+ &error, &reply3,
+ "ss", getty, "replace");
+ if (r < 0) {
+ log_error("Failed to start getty service: %s", bus_error_message(&error, r));
+ return r;
+ }
+
+ container_bus = sd_bus_unref(container_bus);
+
+ assert_se(sigemptyset(&mask) == 0);
+ sigset_add_many(&mask, SIGWINCH, SIGTERM, SIGINT, -1);
+ assert_se(sigprocmask(SIG_BLOCK, &mask, NULL) == 0);
+
+ log_info("Connected to container %s. Press ^] three times within 1s to exit session.", args[1]);
+
+ r = process_pty(master, &mask, 0, 0);
+ if (r < 0) {
+ log_error("Failed to process pseudo tty: %s", strerror(-r));
+ return r;
+ }
+
+ fputc('\n', stdout);
+
+ log_info("Connection to container %s terminated.", args[1]);
+
+ return 0;
+}
+