]> git.karo-electronics.de Git - karo-tx-linux.git/commitdiff
kvm tools: Add instance enumerator
authorSasha Levin <levinsasha928@gmail.com>
Sun, 3 Jul 2011 15:38:08 +0000 (18:38 +0300)
committerPekka Enberg <penberg@kernel.org>
Sun, 3 Jul 2011 18:07:44 +0000 (21:07 +0300)
Add API kvm__enumerate_instances() to enumerate running guest
instances.

Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
tools/kvm/kvm.c

index 378e035af48f1242326a7567b3c15b92f8f9017b..075c8d823617905bda77bb92db740d3aea686011 100644 (file)
@@ -29,6 +29,7 @@
 #include <time.h>
 #include <sys/eventfd.h>
 #include <asm/unistd.h>
+#include <dirent.h>
 
 #define DEFINE_KVM_EXIT_REASON(reason) [reason] = #reason
 #define KVM_PID_FILE_PATH      "/.kvm-tools/"
@@ -164,6 +165,30 @@ int kvm__get_pid_by_instance(const char *name)
        return pid;
 }
 
+int kvm__enumerate_instances(void (*callback)(const char *name, int pid))
+{
+       char full_name[PATH_MAX];
+       int pid;
+       DIR *dir;
+       struct dirent entry, *result;
+
+       sprintf(full_name, "%s/%s", HOME_DIR, KVM_PID_FILE_PATH);
+       dir = opendir(full_name);
+
+       for (;;) {
+               readdir_r(dir, &entry, &result);
+               if (result == NULL)
+                       break;
+               if (entry.d_type == DT_REG) {
+                       entry.d_name[strlen(entry.d_name)-4] = 0;
+                       pid = kvm__get_pid_by_instance(entry.d_name);
+                       callback(entry.d_name, pid);
+               }
+       }
+
+       return 0;
+}
+
 void kvm__delete(struct kvm *kvm)
 {
        kvm__stop_timer(kvm);