public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
* Re: [PATCH] kvm tools: Setup bridged network by a script
@ 2011-04-16  9:55 Ingo Molnar
  2011-04-18  0:12 ` [PATCH] kvm tools: Add robust error handling for fork/waitpid() Amos Kong
  0 siblings, 1 reply; 3+ messages in thread
From: Ingo Molnar @ 2011-04-16  9:55 UTC (permalink / raw)
  To: Pekka Enberg; +Cc: KVM devel mailing list, Amos Kong


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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH] kvm tools: Add robust error handling for fork/waitpid()
  2011-04-16  9:55 [PATCH] kvm tools: Setup bridged network by a script Ingo Molnar
@ 2011-04-18  0:12 ` Amos Kong
  2011-04-18  6:34   ` Ingo Molnar
  0 siblings, 1 reply; 3+ messages in thread
From: Amos Kong @ 2011-04-18  0:12 UTC (permalink / raw)
  To: kvm; +Cc: penberg, asias.hejun, mingo

> 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

Signed-off-by: Amos Kong <kongjianjun@gmail.com>
---
 tools/kvm/virtio-net.c |   11 ++++++-----
 1 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/tools/kvm/virtio-net.c b/tools/kvm/virtio-net.c
index f8d7276..430769a 100644
--- a/tools/kvm/virtio-net.c
+++ b/tools/kvm/virtio-net.c
@@ -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));
 


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] kvm tools: Add robust error handling for fork/waitpid()
  2011-04-18  0:12 ` [PATCH] kvm tools: Add robust error handling for fork/waitpid() Amos Kong
@ 2011-04-18  6:34   ` Ingo Molnar
  0 siblings, 0 replies; 3+ messages in thread
From: Ingo Molnar @ 2011-04-18  6:34 UTC (permalink / raw)
  To: Amos Kong; +Cc: kvm, penberg, asias.hejun


* Amos Kong <kongjianjun@gmail.com> wrote:

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2011-04-18  6:34 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-04-16  9:55 [PATCH] kvm tools: Setup bridged network by a script Ingo Molnar
2011-04-18  0:12 ` [PATCH] kvm tools: Add robust error handling for fork/waitpid() Amos Kong
2011-04-18  6:34   ` Ingo Molnar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox