From patchwork Thu Jun 14 15:55:03 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Corey Bryant X-Patchwork-Id: 164959 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 20F8CB6FF4 for ; Fri, 15 Jun 2012 01:56:15 +1000 (EST) Received: from localhost ([::1]:50907 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SfCP7-0006g8-31 for incoming@patchwork.ozlabs.org; Thu, 14 Jun 2012 11:56:13 -0400 Received: from eggs.gnu.org ([208.118.235.92]:60121) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SfCOu-0006X0-NE for qemu-devel@nongnu.org; Thu, 14 Jun 2012 11:56:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SfCOo-0004HH-Mp for qemu-devel@nongnu.org; Thu, 14 Jun 2012 11:56:00 -0400 Received: from e38.co.us.ibm.com ([32.97.110.159]:52673) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SfCOo-0004GC-GD for qemu-devel@nongnu.org; Thu, 14 Jun 2012 11:55:54 -0400 Received: from /spool/local by e38.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 14 Jun 2012 09:55:49 -0600 Received: from d01dlp03.pok.ibm.com (9.56.224.17) by e38.co.us.ibm.com (192.168.1.138) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Thu, 14 Jun 2012 09:55:23 -0600 Received: from d01relay04.pok.ibm.com (d01relay04.pok.ibm.com [9.56.227.236]) by d01dlp03.pok.ibm.com (Postfix) with ESMTP id EA5F9C90052 for ; Thu, 14 Jun 2012 11:55:19 -0400 (EDT) Received: from d03av03.boulder.ibm.com (d03av03.boulder.ibm.com [9.17.195.169]) by d01relay04.pok.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id q5EFtJuJ225322 for ; Thu, 14 Jun 2012 11:55:20 -0400 Received: from d03av03.boulder.ibm.com (loopback [127.0.0.1]) by d03av03.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id q5EFs8mu017645 for ; Thu, 14 Jun 2012 09:54:09 -0600 Received: from localhost ([9.80.103.203]) by d03av03.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id q5EFs6T9017393; Thu, 14 Jun 2012 09:54:07 -0600 From: Corey Bryant To: qemu-devel@nongnu.org Date: Thu, 14 Jun 2012 11:55:03 -0400 Message-Id: <1339689305-27031-4-git-send-email-coreyb@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.10.2 In-Reply-To: <1339689305-27031-1-git-send-email-coreyb@linux.vnet.ibm.com> References: <1339689305-27031-1-git-send-email-coreyb@linux.vnet.ibm.com> X-Content-Scanned: Fidelis XPS MAILER x-cbid: 12061415-5518-0000-0000-0000052B3318 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 32.97.110.159 Cc: kwolf@redhat.com, aliguori@us.ibm.com, stefanha@linux.vnet.ibm.com, libvir-list@redhat.com, lcapitulino@redhat.com, pbonzini@redhat.com, eblake@redhat.com Subject: [Qemu-devel] [PATCH v3 3/5] osdep: Enable qemu_open to dup pre-opened fd 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This patch adds support to qemu_open to dup(fd) a pre-opened file descriptor if the filename is of the format /dev/fd/X. This can be used when QEMU is restricted from opening files, and the management application opens files on QEMU's behalf. If the fd was passed to the monitor with the pass-fd command, it must be explicitly closed with the 'closefd' command when it is no longer required, in order to prevent fd leaks. Signed-off-by: Corey Bryant --- v2: -Get rid of file_open and move dup code to qemu_open (kwolf@redhat.com) -Use strtol wrapper instead of atoi (kwolf@redhat.com) v3: -Add note about fd leakage (eblake@redhat.com) osdep.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/osdep.c b/osdep.c index 3e6bada..c17cdcb 100644 --- a/osdep.c +++ b/osdep.c @@ -82,6 +82,19 @@ int qemu_open(const char *name, int flags, ...) int ret; int mode = 0; +#ifndef _WIN32 + const char *p; + + /* Attempt dup of fd for pre-opened file */ + if (strstart(name, "/dev/fd/", &p)) { + ret = qemu_parse_fd(p); + if (ret == -1) { + return -1; + } + return dup(ret); + } +#endif + if (flags & O_CREAT) { va_list ap;