This patch adds an option to provide information about redirection of terminal redirection to a PTY device within 'kvm stat'. Usage: 'kvm stat -p [term] -n [instance_name]' Will print information about redirection of terminal 'term' int instance 'instance_name'. Cc: Osier Yang <jyang@xxxxxxxxxx> Signed-off-by: Sasha Levin <levinsasha928@xxxxxxxxx> --- tools/kvm/Documentation/kvm-stat.txt | 2 + tools/kvm/builtin-stat.c | 39 ++++++++++++++++++++++++++++++--- tools/kvm/include/kvm/kvm-ipc.h | 1 + tools/kvm/include/kvm/term.h | 7 ++++++ tools/kvm/term.c | 25 ++++++++++++++++++++- 5 files changed, 69 insertions(+), 5 deletions(-) diff --git a/tools/kvm/Documentation/kvm-stat.txt b/tools/kvm/Documentation/kvm-stat.txt index ce5ab54..5284aa9 100644 --- a/tools/kvm/Documentation/kvm-stat.txt +++ b/tools/kvm/Documentation/kvm-stat.txt @@ -17,3 +17,5 @@ For a list of running instances see 'kvm list'. Commands: --memory, -m Display memory statistics + --pty, -p Display information about terminal's pty + device. diff --git a/tools/kvm/builtin-stat.c b/tools/kvm/builtin-stat.c index e28eb5b..2a46900 100644 --- a/tools/kvm/builtin-stat.c +++ b/tools/kvm/builtin-stat.c @@ -4,6 +4,8 @@ #include <kvm/kvm.h> #include <kvm/parse-options.h> #include <kvm/kvm-ipc.h> +#include <kvm/term.h> +#include <kvm/read-write.h> #include <sys/select.h> #include <stdio.h> @@ -18,6 +20,7 @@ struct stat_cmd { }; static bool mem; +static int pty = -1; static bool all; static int instance; static const char *instance_name; @@ -30,6 +33,7 @@ static const char * const stat_usage[] = { static const struct option stat_options[] = { OPT_GROUP("Commands options:"), OPT_BOOLEAN('m', "memory", &mem, "Display memory statistics"), + OPT_INTEGER('p', "PTY info", &pty, "Display PTY path for given terminal"), OPT_GROUP("Instance options:"), OPT_BOOLEAN('a', "all", &all, "All instances"), OPT_STRING('n', "name", &instance_name, "name", "Instance name"), @@ -104,15 +108,40 @@ static int do_memstat(const char *name, int sock) return 0; } +static int do_pty(const char *name, int sock) +{ + struct pty_cmd cmd = {KVM_IPC_TRM_PTY, 0, pty}; + int r; + char pty_path[PATH_MAX] = {0}; + + r = xwrite(sock, &cmd, sizeof(cmd)); + if (r < 0) + return r; + + r = xread(sock, pty_path, PATH_MAX); + if (r < 0) + return r; + + printf("Instance %s mapped term %d to: %s\n", name, pty, pty_path); + + return 0; +} + int kvm_cmd_stat(int argc, const char **argv, const char *prefix) { parse_stat_options(argc, argv); - if (!mem) + if (!mem && pty == -1) usage_with_options(stat_usage, stat_options); - if (mem && all) - return kvm__enumerate_instances(do_memstat); + if (all) { + if (mem) + kvm__enumerate_instances(do_memstat); + if (pty != -1) + kvm__enumerate_instances(do_pty); + + return 0; + } if (instance_name == NULL && instance == 0) @@ -125,7 +154,9 @@ int kvm_cmd_stat(int argc, const char **argv, const char *prefix) die("Failed locating instance"); if (mem) - return do_memstat(instance_name, instance); + do_memstat(instance_name, instance); + if (pty != -1) + do_pty(instance_name, instance); return 0; } diff --git a/tools/kvm/include/kvm/kvm-ipc.h b/tools/kvm/include/kvm/kvm-ipc.h index 731767f..1d9599b 100644 --- a/tools/kvm/include/kvm/kvm-ipc.h +++ b/tools/kvm/include/kvm/kvm-ipc.h @@ -17,6 +17,7 @@ enum { KVM_IPC_RESUME = 5, KVM_IPC_STOP = 6, KVM_IPC_PID = 7, + KVM_IPC_TRM_PTY = 8, }; int kvm_ipc__register_handler(u32 type, void (*cb)(int fd, u32 type, u32 len, u8 *msg)); diff --git a/tools/kvm/include/kvm/term.h b/tools/kvm/include/kvm/term.h index 37ec731..06d5b4e 100644 --- a/tools/kvm/include/kvm/term.h +++ b/tools/kvm/include/kvm/term.h @@ -2,10 +2,17 @@ #define KVM__TERM_H #include <sys/uio.h> +#include <linux/types.h> #define CONSOLE_8250 1 #define CONSOLE_VIRTIO 2 +struct pty_cmd { + u32 type; + u32 len; + int pty; +}; + int term_putc_iov(int who, struct iovec *iov, int iovcnt, int term); int term_getc_iov(int who, struct iovec *iov, int iovcnt, int term); int term_putc(int who, char *addr, int cnt, int term); diff --git a/tools/kvm/term.c b/tools/kvm/term.c index fb5d71c..4e0d946 100644 --- a/tools/kvm/term.c +++ b/tools/kvm/term.c @@ -13,7 +13,7 @@ #include "kvm/util.h" #include "kvm/kvm.h" #include "kvm/kvm-cpu.h" - +#include "kvm/kvm-ipc.h" #define TERM_FD_IN 0 #define TERM_FD_OUT 1 @@ -27,6 +27,7 @@ bool term_got_escape = false; int active_console; int term_fds[4][2]; +char *term_names[4]; int term_getc(int who, int term) { @@ -142,11 +143,31 @@ void term_set_tty(int term) close(slave); + term_names[term] = strdup(new_pty); + pr_info("Assigned terminal %d to pty %s\n", term, new_pty); term_fds[term][TERM_FD_IN] = term_fds[term][TERM_FD_OUT] = master; } +static void term_info(int fd, u32 type, u32 len, u8 *msg) +{ + int r, id; + const char *res = "[none]"; + + r = read(fd, &id, sizeof(id)); + if (r < 0) + pr_warning("Failed reading term"); + + if (term_names[id]) + res = term_names[id]; + + r = write(fd, res, strlen(res) + 1); + + if (r < 0) + pr_warning("Failed sending term info"); +} + void term_init(void) { struct termios term; @@ -165,6 +186,8 @@ void term_init(void) term_fds[i][TERM_FD_OUT] = STDOUT_FILENO; } + kvm_ipc__register_handler(KVM_IPC_TRM_PTY, term_info); + signal(SIGTERM, term_sig_cleanup); atexit(term_cleanup); } -- 1.7.7.1 -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html