From patchwork Mon Apr 18 00:12:50 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amos Jianjun Kong X-Patchwork-Id: 714161 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.3) with ESMTP id p3I0DW8G010919 for ; Mon, 18 Apr 2011 00:13:32 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752439Ab1DRANM (ORCPT ); Sun, 17 Apr 2011 20:13:12 -0400 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 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p3I0Ctbd029891 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Sun, 17 Apr 2011 20:12:55 -0400 Received: from localhost6.localdomain6 (vpn2-11-98.sin2.redhat.com [10.67.11.98]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id p3I0Cpt2021258; Sun, 17 Apr 2011 20:12:52 -0400 Subject: [PATCH] kvm tools: Add robust error handling for fork/waitpid() To: kvm@vger.kernel.org From: Amos Kong Cc: penberg@kernel.org, asias.hejun@gmail.com, mingo@elte.hu Date: Mon, 18 Apr 2011 08:12:50 +0800 Message-ID: <20110418001250.4934.17958.stgit@localhost6.localdomain6> In-Reply-To: <20110416095543.GA7305@elte.hu> References: <20110416095543.GA7305@elte.hu> User-Agent: StGit/0.15 MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter1.kernel.org [140.211.167.41]); Mon, 18 Apr 2011 00:13:35 +0000 (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 --- 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 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));