X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fmachine%2Fmachinectl.c;h=64caf7c378c9e5d497659bd897b8f14d3353f6d5;hp=cde0c6af137a848b24d1b4ec184b5802bc67e8b6;hb=6389e747d5b09b18e00d85b9b13c1be2ff884015;hpb=56159e0d918e9a9be07988133bb2847779325de0 diff --git a/src/machine/machinectl.c b/src/machine/machinectl.c index cde0c6af1..64caf7c37 100644 --- a/src/machine/machinectl.c +++ b/src/machine/machinectl.c @@ -129,6 +129,8 @@ typedef struct ImageInfo { const char *name; const char *type; bool read_only; + usec_t crtime; + usec_t mtime; } ImageInfo; static int compare_image_info(const void *a, const void *b) { @@ -140,14 +142,14 @@ static int compare_image_info(const void *a, const void *b) { static int list_images(int argc, char *argv[], void *userdata) { _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; - size_t max_name = strlen("NAME"), max_type = strlen("TYPE"); + size_t max_name = strlen("NAME"), max_type = strlen("TYPE"), max_crtime = strlen("CREATED"), max_mtime = strlen("MODIFIED"); _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; _cleanup_free_ ImageInfo *images = NULL; size_t n_images = 0, n_allocated = 0, j; const char *name, *type, *object; sd_bus *bus = userdata; - int read_only; - int r; + uint64_t crtime, mtime; + int read_only, r; assert(bus); @@ -167,11 +169,13 @@ static int list_images(int argc, char *argv[], void *userdata) { return r; } - r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssbo)"); + r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssbtto)"); if (r < 0) return bus_log_parse_error(r); - while ((r = sd_bus_message_read(reply, "(ssbo)", &name, &type, &read_only, &object)) > 0) { + while ((r = sd_bus_message_read(reply, "(ssbtto)", &name, &type, &read_only, &crtime, &mtime, &object)) > 0) { + char buf[FORMAT_TIMESTAMP_MAX]; + size_t l; if (name[0] == '.' && !arg_all) continue; @@ -182,12 +186,28 @@ static int list_images(int argc, char *argv[], void *userdata) { images[n_images].name = name; images[n_images].type = type; images[n_images].read_only = read_only; + images[n_images].crtime = crtime; + images[n_images].mtime = mtime; + + l = strlen(name); + if (l > max_name) + max_name = l; - if (strlen(name) > max_name) - max_name = strlen(name); + l = strlen(type); + if (l > max_type) + max_type = l; + + if (crtime != 0) { + l = strlen(format_timestamp(buf, sizeof(buf), crtime)); + if (l > max_crtime) + max_crtime = l; + } - if (strlen(type) > max_type) - max_type = strlen(type); + if (mtime != 0) { + l = strlen(format_timestamp(buf, sizeof(buf), mtime)); + if (l > max_mtime) + max_mtime = l; + } n_images++; } @@ -201,13 +221,22 @@ static int list_images(int argc, char *argv[], void *userdata) { qsort_safe(images, n_images, sizeof(ImageInfo), compare_image_info); if (arg_legend) - printf("%-*s %-*s %-3s\n", (int) max_name, "NAME", (int) max_type, "TYPE", "RO"); + printf("%-*s %-*s %-3s %-*s %-*s\n", + (int) max_name, "NAME", + (int) max_type, "TYPE", + "RO", + (int) max_crtime, "CREATED", + (int) max_mtime, "MODIFIED"); for (j = 0; j < n_images; j++) { - printf("%-*s %-*s %-3s\n", + char crtime_buf[FORMAT_TIMESTAMP_MAX], mtime_buf[FORMAT_TIMESTAMP_MAX]; + + printf("%-*s %-*s %-3s %-*s %-*s\n", (int) max_name, images[j].name, (int) max_type, images[j].type, - yes_no(images[j].read_only)); + yes_no(images[j].read_only), + (int) max_crtime, images[j].crtime != 0 ? format_timestamp(crtime_buf, sizeof(crtime_buf), images[j].crtime) : "-", + (int) max_mtime, images[j].mtime != 0 ? format_timestamp(mtime_buf, sizeof(mtime_buf), images[j].mtime) : "-"); } if (r < 0) @@ -1010,110 +1039,21 @@ finish: return r; } -static int openpt_in_namespace(pid_t pid, int flags) { - _cleanup_close_pair_ 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; - - assert(pid > 0); - - r = namespace_open(pid, &pidnsfd, &mntnsfd, NULL, &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) { - pair[0] = safe_close(pair[0]); - - r = namespace_enter(pidnsfd, mntnsfd, -1, 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); - } - - pair[1] = safe_close(pair[1]); - - r = wait_for_terminate(child, &si); - if (r < 0) - return r; - if (si.si_code != CLD_EXITED || si.si_status != EXIT_SUCCESS) - return -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(int argc, char *argv[], void *userdata) { _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; - _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; - _cleanup_bus_close_unref_ sd_bus *container_bus = NULL; + _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL; _cleanup_(pty_forward_freep) PTYForward *forward = NULL; _cleanup_event_unref_ sd_event *event = NULL; - _cleanup_close_ int master = -1; - _cleanup_free_ char *getty = NULL; + int master = -1, r, ret = 0; sd_bus *bus = userdata; - const char *pty, *p; - pid_t leader; + const char *pty; sigset_t mask; - int r, ret = 0; + char last_char = 0; assert(bus); - if (arg_transport != BUS_TRANSPORT_LOCAL) { + if (arg_transport != BUS_TRANSPORT_LOCAL && + arg_transport != BUS_TRANSPORT_MACHINE) { log_error("Login only supported on local machines."); return -ENOTSUP; } @@ -1126,48 +1066,32 @@ static int login_machine(int argc, char *argv[], void *userdata) { if (r < 0) return log_error_errno(r, "Failed to attach bus to event loop: %m"); - r = machine_get_leader(bus, argv[1], &leader); + r = sd_bus_message_new_method_call(bus, + &m, + "org.freedesktop.machine1", + "/org/freedesktop/machine1", + "org.freedesktop.machine1.Manager", + "OpenMachineLogin"); if (r < 0) - return r; - - master = openpt_in_namespace(leader, O_RDWR|O_NOCTTY|O_CLOEXEC|O_NDELAY); - if (master < 0) - return log_error_errno(master, "Failed to acquire pseudo tty: %m"); - - pty = ptsname(master); - if (!pty) - return log_error_errno(errno, "Failed to get pty name: %m"); - - p = startswith(pty, "/dev/pts/"); - if (!p) { - log_error("Invalid pty name %s.", pty); - return -EIO; - } + return bus_log_create_error(r); - r = sd_bus_open_system_container(&container_bus, argv[1]); + r = sd_bus_message_set_allow_interactive_authorization(m, true); if (r < 0) - return log_error_errno(r, "Failed to get container bus: %m"); - - getty = strjoin("container-getty@", p, ".service", NULL); - if (!getty) - return log_oom(); + return bus_log_create_error(r); - if (unlockpt(master) < 0) - return log_error_errno(errno, "Failed to unlock tty: %m"); + r = sd_bus_message_append(m, "s", argv[1]); + if (r < 0) + return bus_log_create_error(r); - r = sd_bus_call_method(container_bus, - "org.freedesktop.systemd1", - "/org/freedesktop/systemd1", - "org.freedesktop.systemd1.Manager", - "StartUnit", - &error, &reply, - "ss", getty, "replace"); + r = sd_bus_call(bus, m, 0, &error, &reply); if (r < 0) { - log_error("Failed to start getty service: %s", bus_error_message(&error, r)); + log_error("Failed to get machine PTY: %s", bus_error_message(&error, -r)); return r; } - container_bus = sd_bus_unref(container_bus); + r = sd_bus_message_read(reply, "hs", &master, &pty); + if (r < 0) + return bus_log_parse_error(r); assert_se(sigemptyset(&mask) == 0); sigset_add_many(&mask, SIGWINCH, SIGTERM, SIGINT, -1); @@ -1178,7 +1102,7 @@ static int login_machine(int argc, char *argv[], void *userdata) { sd_event_add_signal(event, NULL, SIGINT, NULL, NULL); sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL); - r = pty_forward_new(event, master, &forward); + r = pty_forward_new(event, master, true, &forward); if (r < 0) return log_error_errno(r, "Failed to create PTY forwarder: %m"); @@ -1186,9 +1110,12 @@ static int login_machine(int argc, char *argv[], void *userdata) { if (r < 0) return log_error_errno(r, "Failed to run event loop: %m"); + pty_forward_last_char(forward, &last_char); + forward = pty_forward_free(forward); - fputc('\n', stdout); + if (last_char != '\n') + fputc('\n', stdout); log_info("Connection to container %s terminated.", argv[1]); @@ -1323,7 +1250,7 @@ static int parse_argv(int argc, char *argv[]) { break; case 'M': - arg_transport = BUS_TRANSPORT_CONTAINER; + arg_transport = BUS_TRANSPORT_MACHINE; arg_host = optarg; break;