[PATCH 1/2] kvm tools: convert callback to int and deal with the return value

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

 



callback function maybe fail, so we should deal with the return value.

Signed-off-by: Liming Wang <walimisdev@xxxxxxxxx>
---
 tools/kvm/builtin-debug.c   |    7 +++----
 tools/kvm/builtin-list.c    |    9 ++++-----
 tools/kvm/builtin-pause.c   |    7 +++----
 tools/kvm/builtin-resume.c  |    7 +++----
 tools/kvm/builtin-stop.c    |    7 +++----
 tools/kvm/include/kvm/kvm.h |    2 +-
 tools/kvm/kvm.c             |    9 ++++++---
 7 files changed, 23 insertions(+), 25 deletions(-)

diff --git a/tools/kvm/builtin-debug.c b/tools/kvm/builtin-debug.c
index 153badd..4cf3b7c 100644
--- a/tools/kvm/builtin-debug.c
+++ b/tools/kvm/builtin-debug.c
@@ -7,9 +7,9 @@
 #include <string.h>
 #include <signal.h>
 
-static void do_debug(const char *name, int pid)
+static int do_debug(const char *name, int pid)
 {
-	kill(pid, SIGQUIT);
+	return kill(pid, SIGQUIT);
 }
 
 int kvm_cmd_debug(int argc, const char **argv, const char *prefix)
@@ -20,8 +20,7 @@ int kvm_cmd_debug(int argc, const char **argv, const char *prefix)
 		die("Usage: kvm debug [instance name]\n");
 
 	if (strcmp(argv[0], "all") == 0) {
-		kvm__enumerate_instances(do_debug);
-		return 0;
+		return kvm__enumerate_instances(do_debug);
 	}
 
 	pid = kvm__get_pid_by_instance(argv[0]);
diff --git a/tools/kvm/builtin-list.c b/tools/kvm/builtin-list.c
index 89a0465..43f37f2 100644
--- a/tools/kvm/builtin-list.c
+++ b/tools/kvm/builtin-list.c
@@ -10,7 +10,7 @@
 
 #define PROCESS_NAME "kvm"
 
-static void print_guest(const char *name, int pid)
+static int print_guest(const char *name, int pid)
 {
 	char proc_name[PATH_MAX];
 	char *comm = NULL;
@@ -31,7 +31,7 @@ static void print_guest(const char *name, int pid)
 
 	fclose(fd);
 
-	return;
+	return 0;
 
 cleanup:
 	if (fd)
@@ -40,11 +40,10 @@ cleanup:
 		free(comm);
 
 	kvm__remove_pidfile(name);
+	return 0;
 }
 
 int kvm_cmd_list(int argc, const char **argv, const char *prefix)
 {
-	kvm__enumerate_instances(print_guest);
-
-	return 0;
+	return kvm__enumerate_instances(print_guest);
 }
diff --git a/tools/kvm/builtin-pause.c b/tools/kvm/builtin-pause.c
index 827b3b4..f5805e6 100644
--- a/tools/kvm/builtin-pause.c
+++ b/tools/kvm/builtin-pause.c
@@ -7,9 +7,9 @@
 #include <kvm/builtin-pause.h>
 #include <kvm/kvm.h>
 
-static void do_pause(const char *name, int pid)
+static int do_pause(const char *name, int pid)
 {
-	kill(pid, SIGUSR2);
+	return kill(pid, SIGUSR2);
 }
 
 int kvm_cmd_pause(int argc, const char **argv, const char *prefix)
@@ -20,8 +20,7 @@ int kvm_cmd_pause(int argc, const char **argv, const char *prefix)
 		die("Usage: kvm pause [instance name]\n");
 
 	if (strcmp(argv[0], "all") == 0) {
-		kvm__enumerate_instances(do_pause);
-		return 0;
+		return kvm__enumerate_instances(do_pause);
 	}
 
 	pid = kvm__get_pid_by_instance(argv[0]);
diff --git a/tools/kvm/builtin-resume.c b/tools/kvm/builtin-resume.c
index 4a85918..a9bf6c5 100644
--- a/tools/kvm/builtin-resume.c
+++ b/tools/kvm/builtin-resume.c
@@ -7,9 +7,9 @@
 #include <kvm/builtin-resume.h>
 #include <kvm/kvm.h>
 
-static void do_resume(const char *name, int pid)
+static int do_resume(const char *name, int pid)
 {
-	kill(pid, SIGKVMRESUME);
+	return kill(pid, SIGKVMRESUME);
 }
 
 int kvm_cmd_resume(int argc, const char **argv, const char *prefix)
@@ -20,8 +20,7 @@ int kvm_cmd_resume(int argc, const char **argv, const char *prefix)
 		die("Usage: kvm resume [instance name]\n");
 
 	if (strcmp(argv[0], "all") == 0) {
-		kvm__enumerate_instances(do_resume);
-		return 0;
+		return kvm__enumerate_instances(do_resume);
 	}
 
 	pid = kvm__get_pid_by_instance(argv[0]);
diff --git a/tools/kvm/builtin-stop.c b/tools/kvm/builtin-stop.c
index 7dd2015..46be393 100644
--- a/tools/kvm/builtin-stop.c
+++ b/tools/kvm/builtin-stop.c
@@ -7,9 +7,9 @@
 #include <string.h>
 #include <signal.h>
 
-static void do_stop(const char *name, int pid)
+static int do_stop(const char *name, int pid)
 {
-	kill(pid, SIGKVMSTOP);
+	return kill(pid, SIGKVMSTOP);
 }
 
 int kvm_cmd_stop(int argc, const char **argv, const char *prefix)
@@ -20,8 +20,7 @@ int kvm_cmd_stop(int argc, const char **argv, const char *prefix)
 		die("Usage: kvm stop [instance name]\n");
 
 	if (strcmp(argv[0], "all") == 0) {
-		kvm__enumerate_instances(do_stop);
-		return 0;
+		return kvm__enumerate_instances(do_stop);
 	}
 
 	pid = kvm__get_pid_by_instance(argv[0]);
diff --git a/tools/kvm/include/kvm/kvm.h b/tools/kvm/include/kvm/kvm.h
index 4f12d11..1cbe3c7 100644
--- a/tools/kvm/include/kvm/kvm.h
+++ b/tools/kvm/include/kvm/kvm.h
@@ -72,7 +72,7 @@ void kvm__pause(void);
 void kvm__continue(void);
 void kvm__notify_paused(void);
 int kvm__get_pid_by_instance(const char *name);
-int kvm__enumerate_instances(void (*callback)(const char *name, int pid));
+int kvm__enumerate_instances(int (*callback)(const char *name, int pid));
 void kvm__remove_pidfile(const char *name);
 
 /*
diff --git a/tools/kvm/kvm.c b/tools/kvm/kvm.c
index 107a922..740851d 100644
--- a/tools/kvm/kvm.c
+++ b/tools/kvm/kvm.c
@@ -164,12 +164,13 @@ int kvm__get_pid_by_instance(const char *name)
 	return pid;
 }
 
-int kvm__enumerate_instances(void (*callback)(const char *name, int pid))
+int kvm__enumerate_instances(int (*callback)(const char *name, int pid))
 {
 	char full_name[PATH_MAX];
 	int pid;
 	DIR *dir;
 	struct dirent entry, *result;
+	int ret = 0;
 
 	sprintf(full_name, "%s/%s", HOME_DIR, KVM_PID_FILE_PATH);
 	dir = opendir(full_name);
@@ -181,13 +182,15 @@ int kvm__enumerate_instances(void (*callback)(const char *name, int pid))
 		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);
+			ret = callback(entry.d_name, pid);
+			if (ret < 0)
+				break;
 		}
 	}
 
 	closedir(dir);
 
-	return 0;
+	return ret;
 }
 
 void kvm__delete(struct kvm *kvm)
-- 
1.7.0.4

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