diff mbox

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

Message ID 20110418001250.4934.17958.stgit@localhost6.localdomain6 (mailing list archive)
State New, archived
Headers show

Commit Message

Amos Jianjun Kong April 18, 2011, 12:12 a.m. UTC
> 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(-)


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

Comments

Ingo Molnar April 18, 2011, 6:34 a.m. UTC | #1
* 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
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

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