From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([140.186.70.92]:54861) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1R0bsq-0005mr-Rm for qemu-devel@nongnu.org; Mon, 05 Sep 2011 12:18:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1R0bsm-0005G2-1w for qemu-devel@nongnu.org; Mon, 05 Sep 2011 12:18:52 -0400 Received: from e28smtp01.in.ibm.com ([122.248.162.1]:51682) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1R0bsl-0005DA-AK for qemu-devel@nongnu.org; Mon, 05 Sep 2011 12:18:47 -0400 Received: from d28relay01.in.ibm.com (d28relay01.in.ibm.com [9.184.220.58]) by e28smtp01.in.ibm.com (8.14.4/8.13.1) with ESMTP id p85GIdCq013057 for ; Mon, 5 Sep 2011 21:48:39 +0530 Received: from d28av05.in.ibm.com (d28av05.in.ibm.com [9.184.220.67]) by d28relay01.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id p85GIcRM2994306 for ; Mon, 5 Sep 2011 21:48:38 +0530 Received: from d28av05.in.ibm.com (loopback [127.0.0.1]) by d28av05.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id p85GIc9a029154 for ; Tue, 6 Sep 2011 02:18:38 +1000 From: "M. Mohan Kumar" Date: Mon, 5 Sep 2011 21:48:25 +0530 Message-Id: <1315239516-4451-5-git-send-email-mohan@in.ibm.com> In-Reply-To: <1315239516-4451-1-git-send-email-mohan@in.ibm.com> References: <1315239516-4451-1-git-send-email-mohan@in.ibm.com> Subject: [Qemu-devel] [PATCH V12 04/15] hw/9pfs: qemu interfaces for chroot environment List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org, Anthony Liguori , Stefan Hajnoczi QEMU side interfaces to communicate with chroot worker process. Signed-off-by: M. Mohan Kumar [malahal@us.ibm.com: Handle when qemu process can not receive fd because it already reached max fds] --- Makefile.objs | 2 +- hw/9pfs/virtio-9p-chroot.c | 103 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 1 deletions(-) create mode 100644 hw/9pfs/virtio-9p-chroot.c diff --git a/Makefile.objs b/Makefile.objs index 01e9350..fa8a755 100644 --- a/Makefile.objs +++ b/Makefile.objs @@ -308,7 +308,7 @@ hw-obj-$(CONFIG_SOUND) += $(sound-obj-y) 9pfs-nested-$(CONFIG_VIRTFS) += virtio-9p-coth.o cofs.o codir.o cofile.o 9pfs-nested-$(CONFIG_VIRTFS) += coxattr.o virtio-9p-handle.o 9pfs-nested-$(CONFIG_VIRTFS) += virtio-9p-synth.o -9pfs-nested-$(CONFIG_VIRTFS) += virtio-9p-chroot-worker.o +9pfs-nested-$(CONFIG_VIRTFS) += virtio-9p-chroot-worker.o virtio-9p-chroot.o hw-obj-$(CONFIG_REALLY_VIRTFS) += $(addprefix 9pfs/, $(9pfs-nested-y)) $(addprefix 9pfs/, $(9pfs-nested-y)): QEMU_CFLAGS+=$(GLIB_CFLAGS) diff --git a/hw/9pfs/virtio-9p-chroot.c b/hw/9pfs/virtio-9p-chroot.c new file mode 100644 index 0000000..63de410 --- /dev/null +++ b/hw/9pfs/virtio-9p-chroot.c @@ -0,0 +1,103 @@ +/* + * Virtio 9p chroot environment for contained access to exported path + * Code handles qemu side interfaces to communicate with chroot worker process + * Copyright IBM, Corp. 2011 + * + * Authors: + * M. Mohan Kumar + * + * This work is licensed under the terms of the GNU GPL, version 2. See + * the copying file in the top-level directory + * + */ + +#include +#include +#include +#include "qemu_socket.h" +#include "qemu-thread.h" +#include "qerror.h" +#include "virtio-9p.h" +#include "virtio-9p-chroot.h" + +/* + * Return received file descriptor on success and -errno on failure. + * sock_error is set to 1 whenever there is error in socket IO + */ +static int v9fs_receivefd(int sockfd, int *sock_error) +{ + struct msghdr msg = { }; + struct iovec iov; + union MsgControl msg_control; + struct cmsghdr *cmsg; + int retval, data, fd; + + iov.iov_base = &data; + iov.iov_len = sizeof(data); + + *sock_error = 0; + memset(&msg, 0, sizeof(msg)); + msg.msg_iov = &iov; + msg.msg_iovlen = 1; + msg.msg_control = &msg_control; + msg.msg_controllen = sizeof(msg_control); + + do { + retval = recvmsg(sockfd, &msg, 0); + } while (retval < 0 && errno == EINTR); + if (retval <= 0) { + *sock_error = 1; + return -EIO; + } + + /* + * data is set to V9FS_FD_VALID, if ancillary data is sent. If this + * request doesn't need ancillary data (fd) or an error occurred, + * data is set to negative errno value. + */ + if (data != V9FS_FD_VALID) { + return data; + } + + /* + * File descriptor (fd) is sent in the ancillary data. Check if we + * indeed received it. One of the reasons to fail to receive it is if + * we exceeded the maximum number of file descriptors! + */ + for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) { + if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) || + cmsg->cmsg_level != SOL_SOCKET || + cmsg->cmsg_type != SCM_RIGHTS) { + continue; + } + fd = *((int *)CMSG_DATA(cmsg)); + return fd; + } + + return -ENFILE; /* Ancillary data sent but not received */ +} + +/* + * V9fsFileObjectRequest is written into the socket by QEMU process. + * Then this request is read by chroot process using v9fs_read_request function + */ +static int v9fs_write_request(int sockfd, V9fsFileObjectRequest *request) +{ + int retval; + retval = qemu_write_full(sockfd, request, sizeof(*request)); + if (retval != sizeof(*request)) { + return -EIO; + } + return 0; +} + +/* + * This patch adds v9fs_receivefd and v9fs_write_request functions, + * but there is no caller. To avoid compiler warning message, + * refer these two functions + */ +void chroot_dummy(void) +{ + (void)v9fs_receivefd; + (void)v9fs_write_request; +} -- 1.7.6