Skip to contentSkip to conversation

kvm tools: Setup bridged network by a script

PATCH In review Ingo Molnar 2 participants 3 messages Apr 16, 2011 – Apr 18, 2011 KVM

lore
Share & tools
lore
#0 lore raw

Hi, i noticed this small detail:

+ if (strcmp(params->script, "none")) {
+ pid = fork();
+ if (pid == 0) {
+ execl(params->script, params->script, net_device.tap_name, NULL);
+ _exit(1);
+ } else {
+ waitpid(pid, &status, 0);
+ if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
+ warning("Fail to setup tap by %s", params->script);
+ goto fail;
+ }
+ }

this path does not seem to have robust error handling: both fork() and waitpid() can (and sometimes do) fail. If waitpid() fails then 'status' might be ununitialized as well, IIRC.

Thanks,

Ingo

#1 lore raw

PATCH 1/1 kvm tools: Add robust error handling for fork/waitpid()

Quoted 15 lines

Hi, i noticed this small detail:

(quoted diff) +12 -0
+       if (strcmp(params->script, "none")) {
+               pid = fork();
+               if (pid == 0) {
+                       execl(params->script, params->script, net_device.tap_name, NULL);
+                       _exit(1);
+               } else {
+                       waitpid(pid, &status, 0);
+                       if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
+                               warning("Fail to setup tap by %s", params->script);
+                               goto fail;
+                       }
+               }
Quoted 7 lines

this path does not seem to have robust error handling: both fork() and waitpid() can (and sometimes do) fail. If waitpid() fails then 'status' might be ununitialized as well, IIRC.

Thanks,

Ingo

---
 tools/kvm/virtio-net.c |   11 ++++++-----
 1 files changed, 6 insertions(+), 5 deletions(-)
tools/kvm/virtio-net.c +6 -5
index f8d7276..430769a 100644
@@ -311,13 +311,14 @@ static bool virtio_net__tap_init(const struct virtio_net_parameters *params)
 		if (pid == 0) {
 			execl(params->script, params->script, net_device.tap_name, NULL);
 			_exit(1);
-		} else {
-			waitpid(pid, &status, 0);
-			if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
-				warning("Fail to setup tap by %s", params->script);
-				goto fail;
+		} else if (pid > 0) {
+			while(waitpid(pid, &status, 0) != pid) {
 			}
 		}
+		if (pid < 0 || (WIFEXITED(status) && WEXITSTATUS(status) != 0)) {
+			warning("Fail to setup tap by %s", params->script);
+			goto fail;
+		}
 	} else {
 		memset(&ifr, 0, sizeof(ifr));
 
#2 lore raw

Re: [PATCH] kvm tools: Add robust error handling for fork/waitpid()

tools/kvm/virtio-net.c static bool virtio_net__tap_init(const struct virtio_net_parameters *params) · line 317

Quoted 4 lines · Amos Kong
tools/kvm/virtio-net.c +2 -0
+		} else if (pid > 0) {
+			while(waitpid(pid, &status, 0) != pid) {
 			}
 		}

Doesn't that look like an infinite loop when waitpid() returns an error?

Thanks,

Ingo