From patchwork Fri Jun 22 18:36:14 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Corey Bryant X-Patchwork-Id: 166656 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 02D8AB6FA1 for ; Sat, 23 Jun 2012 04:36:11 +1000 (EST) Received: from localhost ([::1]:54180 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Si8iG-0004eT-PP for incoming@patchwork.ozlabs.org; Fri, 22 Jun 2012 14:36:08 -0400 Received: from eggs.gnu.org ([208.118.235.92]:54181) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Si8hw-0004QD-Kj for qemu-devel@nongnu.org; Fri, 22 Jun 2012 14:35:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Si8ht-0007yo-VH for qemu-devel@nongnu.org; Fri, 22 Jun 2012 14:35:48 -0400 Received: from e39.co.us.ibm.com ([32.97.110.160]:43540) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Si8ht-0007yS-LK for qemu-devel@nongnu.org; Fri, 22 Jun 2012 14:35:45 -0400 Received: from /spool/local by e39.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Fri, 22 Jun 2012 12:35:42 -0600 Received: from d03dlp01.boulder.ibm.com (9.17.202.177) by e39.co.us.ibm.com (192.168.1.139) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Fri, 22 Jun 2012 12:35:39 -0600 Received: from d03relay02.boulder.ibm.com (d03relay02.boulder.ibm.com [9.17.195.227]) by d03dlp01.boulder.ibm.com (Postfix) with ESMTP id 12BBEC4000F for ; Fri, 22 Jun 2012 18:35:38 +0000 (WET) Received: from d03av06.boulder.ibm.com (d03av06.boulder.ibm.com [9.17.195.245]) by d03relay02.boulder.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id q5MIZXIA106340 for ; Fri, 22 Jun 2012 12:35:36 -0600 Received: from d03av06.boulder.ibm.com (loopback [127.0.0.1]) by d03av06.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id q5MIaT9S020869 for ; Fri, 22 Jun 2012 12:36:30 -0600 Received: from localhost ([9.80.103.203]) by d03av06.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id q5MIaSTT020786; Fri, 22 Jun 2012 12:36:29 -0600 From: Corey Bryant To: qemu-devel@nongnu.org Date: Fri, 22 Jun 2012 14:36:14 -0400 Message-Id: <1340390174-7493-8-git-send-email-coreyb@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.10.2 In-Reply-To: <1340390174-7493-1-git-send-email-coreyb@linux.vnet.ibm.com> References: <1340390174-7493-1-git-send-email-coreyb@linux.vnet.ibm.com> X-Content-Scanned: Fidelis XPS MAILER x-cbid: 12062218-4242-0000-0000-00000215B811 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 32.97.110.160 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 v4 7/7] 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) v4 -Moved patch to be later in series (lcapitulino@redhat.com) -Update qemu_open to check access mode flags and set flags that can be set (eblake@redhat.com, kwolf@redhat.com) cutils.c | 26 +++++++++++++---- main-loop.c | 6 ++-- osdep.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-common.h | 2 +- 4 files changed, 116 insertions(+), 9 deletions(-) diff --git a/cutils.c b/cutils.c index af308cd..f45d921 100644 --- a/cutils.c +++ b/cutils.c @@ -339,17 +339,33 @@ bool buffer_is_zero(const void *buf, size_t len) } #ifndef _WIN32 -/* Sets a specific flag */ -int fcntl_setfl(int fd, int flag) +/* Sets a specific flag on or off */ +int fcntl_setfl(int fd, int flag, int onoff) { int flags; + if (onoff != 0 && onoff != 1) { + return -EINVAL; + } + flags = fcntl(fd, F_GETFL); - if (flags == -1) + if (flags == -1) { return -errno; + } - if (fcntl(fd, F_SETFL, flags | flag) == -1) - return -errno; + if (onoff == 1) { + if ((flags & flag) != flag) { + if (fcntl(fd, F_SETFL, flags | flag) == -1) { + return -errno; + } + } + } else { + if ((flags & flag) == flag) { + if (fcntl(fd, F_SETFL, flags & ~flag) == -1) { + return -errno; + } + } + } return 0; } diff --git a/main-loop.c b/main-loop.c index eb3b6e6..644fcc3 100644 --- a/main-loop.c +++ b/main-loop.c @@ -75,11 +75,11 @@ static int qemu_event_init(void) if (err == -1) { return -errno; } - err = fcntl_setfl(fds[0], O_NONBLOCK); + err = fcntl_setfl(fds[0], O_NONBLOCK, 1); if (err < 0) { goto fail; } - err = fcntl_setfl(fds[1], O_NONBLOCK); + err = fcntl_setfl(fds[1], O_NONBLOCK, 1); if (err < 0) { goto fail; } @@ -154,7 +154,7 @@ static int qemu_signal_init(void) return -errno; } - fcntl_setfl(sigfd, O_NONBLOCK); + fcntl_setfl(sigfd, O_NONBLOCK, 1); qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL, (void *)(intptr_t)sigfd); diff --git a/osdep.c b/osdep.c index 3e6bada..a6fc758d 100644 --- a/osdep.c +++ b/osdep.c @@ -73,6 +73,63 @@ int qemu_madvise(void *addr, size_t len, int advice) #endif } +/* + * Dups an fd and sets the flags + */ +static int qemu_dup(int fd, int flags) +{ + int ret; + int serrno; + + if (flags & O_CLOEXEC) { + ret = fcntl(fd, F_DUPFD_CLOEXEC, 0); + if (ret == -1 && errno == EINVAL) { + ret = dup(fd); + if (ret == -1) { + goto fail; + } + if (fcntl_setfl(ret, O_CLOEXEC, (flags & O_CLOEXEC) ? 1 : 0) < 0) { + goto fail; + } + } + } else { + ret = dup(fd); + } + + if (ret == -1) { + goto fail; + } + + /* Truncate the file in the cases that open would truncate it */ + if (flags & O_TRUNC || + ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))) { + if (ftruncate(ret, 0) == -1) { + goto fail; + } + } + + if ((fcntl_setfl(ret, O_APPEND, (flags & O_APPEND) ? 1 : 0) < 0) || + (fcntl_setfl(ret, O_ASYNC, (flags & O_ASYNC) ? 1 : 0) < 0) || + (fcntl_setfl(ret, O_DIRECT, (flags & O_DIRECT) ? 1 : 0) < 0) || + (fcntl_setfl(ret, O_LARGEFILE, (flags & O_LARGEFILE) ? 1 : 0) < 0) || + (fcntl_setfl(ret, O_NDELAY, (flags & O_NDELAY) ? 1 : 0) < 0) || + (fcntl_setfl(ret, O_NOATIME, (flags & O_NOATIME) ? 1 : 0) < 0) || + (fcntl_setfl(ret, O_NOCTTY, (flags & O_NOCTTY) ? 1 : 0) < 0) || + (fcntl_setfl(ret, O_NONBLOCK, (flags & O_NONBLOCK) ? 1 : 0) < 0) || + (fcntl_setfl(ret, O_SYNC, (flags & O_SYNC) ? 1 : 0) < 0)) { + goto fail; + } + + return ret; + +fail: + serrno = errno; + if (ret != -1) { + close(ret); + } + errno = serrno; + return -1; +} /* * Opens a file with FD_CLOEXEC set @@ -82,6 +139,40 @@ 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)) { + int fd; + int eflags; + + fd = qemu_parse_fd(p); + if (fd == -1) { + return -1; + } + + /* Get the existing fd's flags */ + eflags = fcntl(fd, F_GETFL); + if (eflags == -1) { + return -1; + } + + if (((flags & O_RDWR) != (eflags & O_RDWR)) || + ((flags & O_RDONLY) != (eflags & O_RDONLY)) || + ((flags & O_WRONLY) != (eflags & O_WRONLY))) { + errno = EACCES; + return -1; + } + + if (fcntl_setfl(fd, O_CLOEXEC, 1) < 0) { + return -1; + } + + return qemu_dup(fd, flags); + } +#endif + if (flags & O_CREAT) { va_list ap; diff --git a/qemu-common.h b/qemu-common.h index 91e0562..99cbbc5 100644 --- a/qemu-common.h +++ b/qemu-common.h @@ -144,7 +144,7 @@ int qemu_strnlen(const char *s, int max_len); time_t mktimegm(struct tm *tm); int qemu_fls(int i); int qemu_fdatasync(int fd); -int fcntl_setfl(int fd, int flag); +int fcntl_setfl(int fd, int flag, int onoff); int qemu_parse_fd(const char *param); /*