]> git.karo-electronics.de Git - karo-tx-linux.git/commitdiff
kvm tools: Respect paused status in ./lkvm list
authorAsias He <asias.hejun@gmail.com>
Thu, 22 Dec 2011 02:10:44 +0000 (10:10 +0800)
committerPekka Enberg <penberg@kernel.org>
Thu, 22 Dec 2011 14:06:52 +0000 (16:06 +0200)
'./lkvm list' does not give the correct vm status if the vm is paused
currently.

This patch fixes this by using KVM_IPC_VMSTATE IPC cmd to query the vm
status.

Suppose the guest is paused by ./lkvm pause:

Before:

asias@hj$ ./lkvm list
   PID NAME                 STATE
------------------------------------
 3036 guest-3036           running
      default              shut off

After:

asias@hj$ ./lkvm list
   PID NAME                 STATE
------------------------------------
 3036 guest-3036           paused
      default              shut off

Signed-off-by: Asias He <asias.hejun@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
tools/kvm/builtin-list.c

index ec1c067af19d21e0f943dc0ec8b91a9b40dee230..e74b636f91a00da5bd4b78a84f71eab168000cb0 100644 (file)
@@ -16,6 +16,11 @@ struct pid_cmd {
        u32 len;
 };
 
+struct vmstate_cmd {
+       u32 type;
+       u32 len;
+};
+
 static bool run;
 static bool rootfs;
 
@@ -32,6 +37,7 @@ static const struct option list_options[] = {
 };
 
 #define KVM_INSTANCE_RUNNING   "running"
+#define KVM_INSTANCE_PAUSED    "paused"
 #define KVM_INSTANCE_SHUTOFF   "shut off"
 
 void kvm_list_help(void)
@@ -56,12 +62,34 @@ static pid_t get_pid(int sock)
        return pid;
 }
 
+static int get_vmstate(int sock)
+{
+       struct vmstate_cmd cmd = {KVM_IPC_VMSTATE, 0};
+       int r;
+       int vmstate;
+
+       r = write(sock, &cmd, sizeof(cmd));
+       if (r < 0)
+               return r;
+
+       r = read(sock, &vmstate, sizeof(vmstate));
+       if (r < 0)
+               return r;
+
+       return vmstate;
+
+}
+
 static int print_guest(const char *name, int sock)
 {
        char proc_name[PATH_MAX];
        char *comm = NULL;
        FILE *fd;
-       pid_t pid = get_pid(sock);
+       pid_t pid;
+       int vmstate;
+
+       pid = get_pid(sock);
+       vmstate = get_vmstate(sock);
 
        sprintf(proc_name, "/proc/%d/stat", pid);
        fd = fopen(proc_name, "r");
@@ -70,7 +98,10 @@ static int print_guest(const char *name, int sock)
        if (fscanf(fd, "%*u (%as)", &comm) == 0)
                goto cleanup;
 
-       printf("%5d %-20s %s\n", pid, name, KVM_INSTANCE_RUNNING);
+       if (vmstate == KVM_VMSTATE_PAUSED)
+               printf("%5d %-20s %s\n", pid, name, KVM_INSTANCE_PAUSED);
+       else
+               printf("%5d %-20s %s\n", pid, name, KVM_INSTANCE_RUNNING);
 
        free(comm);