From patchwork Wed Jan 20 12:01:31 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= X-Patchwork-Id: 8071471 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id F074D9F6FA for ; Wed, 20 Jan 2016 12:02:20 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 4AB9520437 for ; Wed, 20 Jan 2016 12:02:20 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id B1C31203B1 for ; Wed, 20 Jan 2016 12:02:14 +0000 (UTC) Received: from localhost ([::1]:42425 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aLrSo-0004gg-6R for patchwork-qemu-devel@patchwork.kernel.org; Wed, 20 Jan 2016 07:02:14 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:45090) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aLrSL-0004Bp-6S for qemu-devel@nongnu.org; Wed, 20 Jan 2016 07:01:46 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aLrSK-0006W0-9w for qemu-devel@nongnu.org; Wed, 20 Jan 2016 07:01:45 -0500 Received: from mx1.redhat.com ([209.132.183.28]:40775) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aLrSK-0006Vh-5C for qemu-devel@nongnu.org; Wed, 20 Jan 2016 07:01:44 -0500 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (Postfix) with ESMTPS id BA6B38E74E; Wed, 20 Jan 2016 12:01:43 +0000 (UTC) Received: from t530wlan.home.berrange.com.com (vpn1-6-168.ams2.redhat.com [10.36.6.168]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u0KC1cpR004554; Wed, 20 Jan 2016 07:01:42 -0500 From: "Daniel P. Berrange" To: qemu-devel@nongnu.org Date: Wed, 20 Jan 2016 12:01:31 +0000 Message-Id: <1453291293-2821-4-git-send-email-berrange@redhat.com> In-Reply-To: <1453291293-2821-1-git-send-email-berrange@redhat.com> References: <1453291293-2821-1-git-send-email-berrange@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Peter Maydell Subject: [Qemu-devel] [PULL v1 3/5] io: some fixes to handling of /dev/null when running commands X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The /dev/null file handle was leaked in a couple of places. There is also the possibility that both readfd and writefd point to the same /dev/null file handle, so care must be taken not to close the same file handle twice. Reviewed-by: Paolo Bonzini Signed-off-by: Daniel P. Berrange --- io/channel-command.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/io/channel-command.c b/io/channel-command.c index a220fe8..a9c67aa 100644 --- a/io/channel-command.c +++ b/io/channel-command.c @@ -66,7 +66,7 @@ qio_channel_command_new_spawn(const char *const argv[], if (stdinnull || stdoutnull) { devnull = open("/dev/null", O_RDWR); - if (!devnull) { + if (devnull < 0) { error_setg_errno(errp, errno, "Unable to open /dev/null"); goto error; @@ -98,6 +98,9 @@ qio_channel_command_new_spawn(const char *const argv[], close(stdoutfd[0]); close(stdoutfd[1]); } + if (devnull != -1) { + close(devnull); + } execv(argv[0], (char * const *)argv); _exit(1); @@ -117,6 +120,9 @@ qio_channel_command_new_spawn(const char *const argv[], return ioc; error: + if (devnull != -1) { + close(devnull); + } if (stdinfd[0] != -1) { close(stdinfd[0]); } @@ -202,12 +208,12 @@ static void qio_channel_command_finalize(Object *obj) QIOChannelCommand *ioc = QIO_CHANNEL_COMMAND(obj); if (ioc->readfd != -1) { close(ioc->readfd); - ioc->readfd = -1; } - if (ioc->writefd != -1) { + if (ioc->writefd != -1 && + ioc->writefd != ioc->readfd) { close(ioc->writefd); - ioc->writefd = -1; } + ioc->writefd = ioc->readfd = -1; if (ioc->pid > 0) { #ifndef WIN32 qio_channel_command_abort(ioc, NULL); @@ -299,12 +305,16 @@ static int qio_channel_command_close(QIOChannel *ioc, /* We close FDs before killing, because that * gives a better chance of clean shutdown */ - if (close(cioc->writefd) < 0) { + if (cioc->readfd != -1 && + close(cioc->readfd) < 0) { rv = -1; } - if (close(cioc->readfd) < 0) { + if (cioc->writefd != -1 && + cioc->writefd != cioc->readfd && + close(cioc->writefd) < 0) { rv = -1; } + cioc->writefd = cioc->readfd = -1; #ifndef WIN32 if (qio_channel_command_abort(cioc, errp) < 0) { return -1;