Skip to contentSkip to conversation

kvm tools: convert callback to int and deal with the return value

PATCH In review Liming Wang 3 participants 12 messages Aug 10, 2011 KVM

lore
Share & tools
lore
#0 lore raw

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

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

---
 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(-)
tools/kvm/builtin-debug.c +3 -4
index 153badd..4cf3b7c 100644
@@ -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]);
tools/kvm/builtin-list.c +4 -5
index 89a0465..43f37f2 100644
@@ -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);
 }
tools/kvm/builtin-pause.c +3 -4
index 827b3b4..f5805e6 100644
@@ -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]);
tools/kvm/builtin-resume.c +3 -4
index 4a85918..a9bf6c5 100644
@@ -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]);
tools/kvm/builtin-stop.c +3 -4
index 7dd2015..46be393 100644
@@ -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]);
tools/kvm/include/kvm/kvm.h +1 -1
index 4f12d11..1cbe3c7 100644
@@ -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);
 
 /*
tools/kvm/kvm.c +6 -3
index 107a922..740851d 100644
@@ -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)
Signature · 1 line
1.7.0.4
#1 lore raw

PATCH 2/2 kvm tools: handle failure of command

handle failure of calling command function, especially, only handle EPERM error now.

---
 tools/kvm/kvm-cmd.c |   11 ++++++++++-
 1 files changed, 10 insertions(+), 1 deletions(-)
tools/kvm/kvm-cmd.c +10 -1
index 63a2dbf..6cd4270 100644
@@ -15,6 +15,7 @@
 #include "kvm/builtin-help.h"
 #include "kvm/kvm-cmd.h"
 #include "kvm/builtin-run.h"
+#include "kvm/util.h"
 
 struct cmd_struct kvm_commands[] = {
 	{ "pause",	kvm_cmd_pause,		NULL,         0 },
@@ -59,6 +60,7 @@ int handle_command(struct cmd_struct *command, int argc, const char **argv)
 {
 	struct cmd_struct *p;
 	const char *prefix = NULL;
+	int ret = 0;
 
 	if (!argv || !*argv) {
 		p = kvm_get_command(command, "help");
@@ -74,5 +76,12 @@ int handle_command(struct cmd_struct *command, int argc, const char **argv)
 		return EINVAL;
 	}
 
-	return p->fn(argc - 1, &argv[1], prefix);
+	ret = p->fn(argc - 1, &argv[1], prefix);
+	if (ret < 0)
+	{
+		if (errno == EPERM)
+			die("Permission error - are you root?");
+	}
+
+	return ret;
 }
Signature · 1 line
1.7.0.4
#2 lore raw

Re: [PATCH 2/2] kvm tools: handle failure of command

Quoted 4 lines · Liming Wang

handle failure of calling command function, especially, only handle EPERM error now.

I applied the patch because I absolutely loved the error handling cleanups from the previous patch. However, out of curiosity, are you seeing EPERM with some command in particular?

Quoted 46 lines

---
 tools/kvm/kvm-cmd.c |   11 ++++++++++-

 1 files changed, 10 insertions(+), 1 deletions(-)
tools/kvm/kvm-cmd.c +10 -2
index 63a2dbf..6cd4270 100644
@@ -15,6 +15,7 @@
  #include "kvm/builtin-help.h"
  #include "kvm/kvm-cmd.h"
  #include "kvm/builtin-run.h"
+#include "kvm/util.h"
 
  struct cmd_struct kvm_commands[] = {
        { "pause",      kvm_cmd_pause,          NULL,         0 },
@@ -59,6 +60,7 @@ int handle_command(struct cmd_struct *command, int argc, const char **argv)
  {
        struct cmd_struct *p;
        const char *prefix = NULL;
+       int ret = 0;
 
        if (!argv || !*argv) {
                p = kvm_get_command(command, "help");
@@ -74,5 +76,12 @@ int handle_command(struct cmd_struct *command, int argc, const char **argv)
                return EINVAL;
        }
 
-       return p->fn(argc - 1, &argv[1], prefix);
+       ret = p->fn(argc - 1, &argv[1], prefix);
+       if (ret < 0)
+       {
+               if (errno == EPERM)
+                       die("Permission error - are you root?");
+       }
+
+       return ret;
  }
--

1.7.0.4

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

author
#3 lore raw

Re: [PATCH 2/2] kvm tools: handle failure of command

Quoted 9 lines · Pekka Enberg
Quoted 4 lines · Liming Wang

handle failure of calling command function, especially, only handle EPERM error now.

I applied the patch because I absolutely loved the error handling cleanups from the previous patch. However, out of curiosity, are you seeing EPERM with some command in particular?

Yes. Because I'm using ubuntu and if I don't use sudo to execute "kvm pause", pause command can't work and doesn't prompt anything. BTW, do you always login as "root" user?

walimis

Quoted 47 lines
Quoted 47 lines

---
爐ools/kvm/kvm-cmd.c | ? 11 ++++++++++-
?1 files changed, 10 insertions(+), 1 deletions(-)

tools/kvm/kvm-cmd.c +0 -0
index 63a2dbf..6cd4270 100644
@@ -15,6 +15,7 @@

?#include "kvm/builtin-help.h"
?#include "kvm/kvm-cmd.h"
?#include "kvm/builtin-run.h"
+#include "kvm/util.h"

爏truct cmd_struct kvm_commands[] = {
? ? ? 爗 "pause", ? ? 爇vm_cmd_pause, ? ? ? ? 燦ULL, ? ? ? ? 0 },

tools/kvm/kvm-cmd.c +0 -0
@@ -59,6 +60,7 @@ int handle_command(struct cmd_struct *command, int argc, const char **argv)

爗
? ? ? 爏truct cmd_struct *p;
? ? ? 燾onst char *prefix = NULL;
+ ? ? ? int ret = 0;

? ? ? 爄f (!argv || !*argv) {
? ? ? ? ? ? ? 爌 = kvm_get_command(command, "help");

tools/kvm/kvm-cmd.c +0 -0
@@ -74,5 +76,12 @@ int handle_command(struct cmd_struct *command, int argc, const char **argv)
 ? ? ? ? ? ? ? 爎eturn EINVAL;

? ? ? 爙

- ? ? ? return p->fn(argc - 1, &argv[1], prefix);
+ ? ? ? ret = p->fn(argc - 1, &argv[1], prefix);
+ ? ? ? if (ret < 0)
+ ? ? ? {
+ ? ? ? ? ? ? ? if (errno == EPERM)
+ ? ? ? ? ? ? ? ? ? ? ? die("Permission error - are you root?");
+ ? ? ? }
+
+ ? ? ? return ret;
爙
--
1.7.0.4

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at 爃ttp://vger.kernel.org/majordomo-info.html

#4 lore raw

Re: [PATCH 2/2] kvm tools: handle failure of command

Quoted 11 lines · walimis
Quoted 8 lines · Pekka Enberg
Quoted 4 lines · Liming Wang

handle failure of calling command function, especially, only handle EPERM error now.

I applied the patch because I absolutely loved the error handling cleanups from the previous patch. However, out of curiosity, are you seeing EPERM with some command in particular?

Yes. Because I'm using ubuntu and if I don't use sudo to execute "kvm pause", pause command can't work and doesn't prompt anything.

That's odd. I don't use sudo with the tool either. Sasha?

Quoted 1 line

BTW, do you always login as "root" user?

Depends on which image you're using.

Pekka

author
#5 lore raw

Re: [PATCH 2/2] kvm tools: handle failure of command

Quoted 6 lines · Pekka Enberg
Quoted 5 lines · walimis
Quoted 4 lines · Pekka Enberg
Quoted 3 lines · Liming Wang

handle failure of calling command function, especially, only handle EPERM error now.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

I applied the patch because I absolutely loved the error handling cleanups from the previous patch. However, out of curiosity, are you seeing EPERM with some command in particular?

Yes. Because I'm using ubuntu and if I don't use sudo to execute "kvm pause", pause command can't work and doesn't prompt anything.

That's odd. I don't use sudo with the tool either. Sasha?

If I don't use sudo to run "kvm run", kvm prompt me:
Fatal, could not open /dev/kvm: Permission denied

So I use sudo to execute "kvm run".

Quoted 3 lines
Quoted 1 line

BTW, do you always login as "root" user?

Depends on which image you're using.

I don't refer to the guest image. I mean that in your host PC, which user you login and operate as.

walimis

Quoted 2 lines

Pekka

#6 lore raw

Re: [PATCH 2/2] kvm tools: handle failure of command

Quoted 6 lines · walimis
Quoted 4 lines
Quoted 2 lines

Yes. Because I'm using ubuntu and if I don't use sudo to execute "kvm pause", pause command can't work and doesn't prompt anything.

That's odd. I don't use sudo with the tool either. Sasha?

If I don't use sudo to run "kvm run", kvm prompt me:
 Fatal, could not open /dev/kvm: Permission denied

You should probably just add yourself to the 'kvm' group:

https://help.ubuntu.com/community/KVM/Installation

Quoted 6 lines

So I use sudo to execute "kvm run".

Quoted 3 lines
Quoted 1 line

BTW, do you always login as "root" user?

Depends on which image you're using.

I don't refer to the guest image. I mean that in your host PC, which user you login and operate as.

Heh, I never login as 'root' to the host machine. :-)

Pekka

author
#7 lore raw

Re: [PATCH 2/2] kvm tools: handle failure of command

Quoted 11 lines · Pekka Enberg
Quoted 6 lines · walimis
Quoted 4 lines
Quoted 2 lines

Yes. Because I'm using ubuntu and if I don't use sudo to execute "kvm pause", pause command can't work and doesn't prompt anything.

That's odd. I don't use sudo with the tool either. Sasha?

If I don't use sudo to run "kvm run", kvm prompt me:
燜atal, could not open /dev/kvm: Permission denied

You should probably just add yourself to the 'kvm' group:

https://help.ubuntu.com/community/KVM/Installation

OK, that works for me to access /dev/kvm without sudo. Thanks. But another sudo requirement is to use tap interface:
Warning: Config tap device error. Are you root?

So how do you use tap interface in your environment?

walimis

Quoted 11 lines
Quoted 7 lines

So I use sudo to execute "kvm run".

Quoted 3 lines
Quoted 1 line

BTW, do you always login as "root" user?

Depends on which image you're using.

I don't refer to the guest image. I mean that in your host PC, which user you login and operate as.

Heh, I never login as 'root' to the host machine. :-)

Pekka

#8 lore raw

Re: [PATCH 2/2] kvm tools: handle failure of command

Quoted 9 lines · walimis
Quoted 8 lines · Pekka Enberg
Quoted 6 lines · walimis
Quoted 4 lines
Quoted 2 lines

Yes. Because I'm using ubuntu and if I don't use sudo to execute "kvm pause", pause command can't work and doesn't prompt anything.

That's odd. I don't use sudo with the tool either. Sasha?

If I don't use sudo to run "kvm run", kvm prompt me:
燜atal, could not open /dev/kvm: Permission denied

Quoted 8 lines
Quoted 3 lines

You should probably just add yourself to the 'kvm' group:

https://help.ubuntu.com/community/KVM/Installation

OK, that works for me to access /dev/kvm without sudo. Thanks. But another sudo requirement is to use tap interface:
 Warning: Config tap device error. Are you root?

So how do you use tap interface in your environment?

I don't use it. We enable userspace networking by default so all you need to do in the guest is to make sure dhcp is enabled and you should have a working network setup. Does that not work for you?

Pekka

author
#9 lore raw

Re: [PATCH 2/2] kvm tools: handle failure of command

Quoted 17 lines · Pekka Enberg
Quoted 16 lines · walimis
Quoted 11 lines · Pekka Enberg
Quoted 6 lines · walimis
Quoted 4 lines
Quoted 2 lines

Yes. Because I'm using ubuntu and if I don't use sudo to execute "kvm pause", pause command can't work and doesn't prompt anything.

That's odd. I don't use sudo with the tool either. Sasha?

If I don't use sudo to run "kvm run", kvm prompt me:
燜atal, could not open /dev/kvm: Permission denied

You should probably just add yourself to the 'kvm' group:

https://help.ubuntu.com/community/KVM/Installation

OK, that works for me to access /dev/kvm without sudo. Thanks. But another sudo requirement is to use tap interface:
 Warning: Config tap device error. Are you root?

Quoted 5 lines
Quoted 1 line

So how do you use tap interface in your environment?

I don't use it. We enable userspace networking by default so all you need to do in the guest is to make sure dhcp is enabled and you should have a working network setup. Does that not work for you?

I used to use tap to enable network for guest os, so that I can mount nfs rootfs. meanwhile, I can access the guest os from host os through the network. For kvm tools, I don't know how to do that through userspace networking. Maybe we need a brief explanation in README.

walimis

Quoted 2 lines

Pekka

#10 lore raw

Re: [PATCH 2/2] kvm tools: handle failure of command

Quoted 4 lines · walimis

I used to use tap to enable network for guest os, so that I can mount nfs rootfs. meanwhile, I can access the guest os from host os through the network. For kvm tools, I don't know how to do that through userspace networking. Maybe we need a brief explanation in README.

Right. I've never used that. Asias is that supported?

#11 lore raw

Re: [PATCH 2/2] kvm tools: handle failure of command

Quoted 8 lines · Pekka Enberg
Quoted 4 lines · walimis

I used to use tap to enable network for guest os, so that I can mount nfs rootfs. meanwhile, I can access the guest os from host os through the network. For kvm tools, I don't know how to do that through userspace networking. Maybe we need a brief explanation in README.

Right. I've never used that. Asias is that supported?

I have not tried nfs with our user mode network. But I think walimis can do it with

	sudo ./kvm --network tap

We need sudo because we need CAP_NET_ADMIN to open /dev/net/tun.

By default, the ip address in host side is:

192.168.33.1/24

In guest side, you need to config a ip address for our guest:

	$ sudo ifconfig eth0 192.168.33.15
	$ sudo route add default gw 192.168.33.1
	$ ping 192.168.33.1

That's it.

If you want to access the real world in guest, try NAT or Bridge the tap device.

Signature · 2 lines
Best Regards,
Asias He