public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Amos Kong <kongjianjun@gmail.com>
To: kvm@vger.kernel.org
Cc: penberg@kernel.org, asias.hejun@gmail.com, mingo@elte.hu
Subject: [PATCH] kvm tools: Add robust error handling for fork/waitpid()
Date: Mon, 18 Apr 2011 08:12:50 +0800	[thread overview]
Message-ID: <20110418001250.4934.17958.stgit@localhost6.localdomain6> (raw)
In-Reply-To: <20110416095543.GA7305@elte.hu>

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


  reply	other threads:[~2011-04-18  0:13 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-04-16  9:55 [PATCH] kvm tools: Setup bridged network by a script Ingo Molnar
2011-04-18  0:12 ` Amos Kong [this message]
2011-04-18  6:34   ` [PATCH] kvm tools: Add robust error handling for fork/waitpid() Ingo Molnar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20110418001250.4934.17958.stgit@localhost6.localdomain6 \
    --to=kongjianjun@gmail.com \
    --cc=asias.hejun@gmail.com \
    --cc=kvm@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=penberg@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox