From mboxrd@z Thu Jan 1 00:00:00 1970 From: Amos Kong Subject: [PATCH] kvm tools: Add robust error handling for fork/waitpid() Date: Mon, 18 Apr 2011 08:12:50 +0800 Message-ID: <20110418001250.4934.17958.stgit@localhost6.localdomain6> References: <20110416095543.GA7305@elte.hu> Mime-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Cc: penberg@kernel.org, asias.hejun@gmail.com, mingo@elte.hu To: kvm@vger.kernel.org Return-path: Received: from mx1.redhat.com ([209.132.183.28]:32452 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751570Ab1DRANG (ORCPT ); Sun, 17 Apr 2011 20:13:06 -0400 In-Reply-To: <20110416095543.GA7305@elte.hu> Sender: kvm-owner@vger.kernel.org List-ID: > 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 --- 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));