[PATCH 3/5] kvm tools: Add instance enumerator

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Add API kvm__enumerate_instances() to enumerate running guest
instances.

Signed-off-by: Sasha Levin <levinsasha928@xxxxxxxxx>
---
 tools/kvm/kvm.c |   25 +++++++++++++++++++++++++
 1 files changed, 25 insertions(+), 0 deletions(-)

diff --git a/tools/kvm/kvm.c b/tools/kvm/kvm.c
index 378e035..075c8d8 100644
--- a/tools/kvm/kvm.c
+++ b/tools/kvm/kvm.c
@@ -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);
-- 
1.7.6

--
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


[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]
  Powered by Linux