[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH V3 05/13] hw/9pfs: Open and create files
From: |
M. Mohan Kumar |
Subject: |
[Qemu-devel] [PATCH V3 05/13] hw/9pfs: Open and create files |
Date: |
Mon, 21 Nov 2011 19:06:10 +0530 |
From: "M. Mohan Kumar" <address@hidden>
Add interfaces to open and create files for proxy file system driver.
Signed-off-by: M. Mohan Kumar <address@hidden>
---
fsdev/virtfs-proxy-helper.c | 133 ++++++++++++++++++++++++++++++-
hw/9pfs/virtio-9p-proxy.c | 187 +++++++++++++++++++++++++++++++++++++++++--
hw/9pfs/virtio-9p-proxy.h | 14 +++
3 files changed, 326 insertions(+), 8 deletions(-)
diff --git a/fsdev/virtfs-proxy-helper.c b/fsdev/virtfs-proxy-helper.c
index dc222d4..867fdcc 100644
--- a/fsdev/virtfs-proxy-helper.c
+++ b/fsdev/virtfs-proxy-helper.c
@@ -9,6 +9,7 @@
* the COPYING file in the top-level directory.
*/
#include <stdio.h>
+#include <sys/socket.h>
#include <string.h>
#include <sys/un.h>
#include <limits.h>
@@ -28,6 +29,7 @@
#include "qemu-common.h"
#include "virtio-9p-marshal.h"
#include "hw/9pfs/virtio-9p-proxy.h"
+#include "fsdev/virtio-9p-marshal.h"
#define PROGNAME "virtfs-proxy-helper"
@@ -172,6 +174,114 @@ static int read_request(int sockfd, struct iovec *iovec)
return header.type;
}
+static void send_fd(int sockfd, int fd)
+{
+ struct msghdr msg = { };
+ struct iovec iov;
+ struct cmsghdr *cmsg;
+ int retval, data;
+ union MsgControl msg_control;
+
+ iov.iov_base = &data;
+ iov.iov_len = sizeof(data);
+
+ memset(&msg, 0, sizeof(msg));
+ msg.msg_iov = &iov;
+ msg.msg_iovlen = 1;
+ /* No ancillary data on error */
+ if (fd < 0) {
+ /* fd is really negative errno if the request failed */
+ data = fd;
+ } else {
+ data = V9FS_FD_VALID;
+ msg.msg_control = &msg_control;
+ msg.msg_controllen = sizeof(msg_control);
+
+ cmsg = &msg_control.cmsg;
+ cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
+ cmsg->cmsg_level = SOL_SOCKET;
+ cmsg->cmsg_type = SCM_RIGHTS;
+ memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
+ }
+
+ do {
+ retval = sendmsg(sockfd, &msg, 0);
+ } while (retval < 0 && errno == EINTR);
+ if (retval < 0) {
+ do_perror("sendmsg");
+ exit(1);
+ }
+ if (fd >= 0) {
+ close(fd);
+ }
+}
+
+/*
+ * from man 7 capabilities, section
+ * Effect of User ID Changes on Capabilities:
+ * 4. If the file system user ID is changed from 0 to nonzero (see setfsuid(2))
+ * then the following capabilities are cleared from the effective set:
+ * CAP_CHOWN, CAP_DAC_OVERRIDE, CAP_DAC_READ_SEARCH, CAP_FOWNER, CAP_FSETID,
+ * CAP_LINUX_IMMUTABLE (since Linux 2.2.30), CAP_MAC_OVERRIDE, and CAP_MKNOD
+ * (since Linux 2.2.30). If the file system UID is changed from nonzero to 0,
+ * then any of these capabilities that are enabled in the permitted set
+ * are enabled in the effective set.
+ */
+static int setfsugid(int uid, int gid)
+{
+ setfsgid(gid);
+ setfsuid(uid);
+ return cap_set();
+}
+
+/*
+ * create a file and send fd on success
+ * return -errno on error
+ */
+static int do_create(struct iovec *iovec)
+{
+ V9fsString path;
+ int flags, fd, mode, uid, gid, cur_uid, cur_gid;
+
+ if (proxy_unmarshal(iovec, 1, HDR_SZ, "sdddd",
+ &path, &flags, &mode, &uid, &gid) < 0) {
+ return -EOVERFLOW;
+ }
+ cur_uid = geteuid();
+ cur_gid = getegid();
+ if (setfsugid(uid, gid) < 0) {
+ v9fs_string_free(&path);
+ return -EPERM;
+ }
+ fd = open(path.data, flags, mode);
+ if (fd < 0) {
+ fd = -errno;
+ }
+ v9fs_string_free(&path);
+ setfsugid(cur_uid, cur_gid);
+ return fd;
+}
+
+/*
+ * open a file and send fd on success
+ * return -errno on error
+ */
+static int do_open(struct iovec *iovec)
+{
+ V9fsString path;
+ int flags, fd;
+
+ if (proxy_unmarshal(iovec, 1, HDR_SZ, "sd", &path, &flags) < 0) {
+ return -EOVERFLOW;
+ }
+ fd = open(path.data, flags);
+ if (fd < 0) {
+ fd = -errno;
+ }
+ v9fs_string_free(&path);
+ return fd;
+}
+
static void usage(char *prog)
{
fprintf(stderr, "usage: %s\n"
@@ -183,15 +293,34 @@ static void usage(char *prog)
static int process_requests(int sock)
{
- int type;
+ int type, retval = 0;
struct iovec iovec;
iovec.iov_base = g_malloc(BUFF_SZ);
iovec.iov_len = BUFF_SZ;
while (1) {
type = read_request(sock, &iovec);
- if (type <= 0) {
+ switch (type) {
+ case T_OPEN:
+ retval = do_open(&iovec);
+ break;
+ case T_CREATE:
+ retval = do_create(&iovec);
+ break;
+ default:
goto error;
+ break;
+ }
+
+ /* Send response */
+ switch (type) {
+ case T_OPEN:
+ case T_CREATE:
+ send_fd(sock, retval);
+ break;
+ default:
+ goto error;
+ break;
}
}
(void)socket_write;
diff --git a/hw/9pfs/virtio-9p-proxy.c b/hw/9pfs/virtio-9p-proxy.c
index 0e539e3..3472337 100644
--- a/hw/9pfs/virtio-9p-proxy.c
+++ b/hw/9pfs/virtio-9p-proxy.c
@@ -22,6 +22,153 @@ typedef struct V9fsProxy {
struct iovec iovec;
} V9fsProxy;
+/*
+ * 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 */
+}
+
+/*
+ * Proxy->header and proxy->request written to socket by QEMU process.
+ * This request read by proxy helper process
+ * returns 0 on success and -errno on error
+ */
+static int v9fs_request(V9fsProxy *proxy, int type,
+ void *response, const char *fmt, ...)
+{
+ int retval;
+ ProxyHeader header;
+ va_list ap;
+ V9fsString *path;
+ int sock_error, flags, mode, uid, gid;
+ struct iovec *iovec = NULL;
+
+ qemu_mutex_lock(&proxy->mutex);
+
+ if (proxy->sockfd == -1) {
+ goto error;
+ }
+ iovec = &proxy->iovec;
+
+ va_start(ap, fmt);
+ switch (type) {
+ case T_OPEN:
+ path = va_arg(ap, V9fsString *);
+ flags = va_arg(ap, int);
+ header.size = proxy_marshal(iovec, 1, HDR_SZ, "sd", path, flags);
+ if (header.size < 0) {
+ goto out_overflow;
+ }
+ header.type = T_OPEN;
+ proxy_marshal(iovec, 1, 0, "dd", header.type, header.size);
+ header.size += HDR_SZ;
+ break;
+ case T_CREATE:
+ path = va_arg(ap, V9fsString *);
+ flags = va_arg(ap, int);
+ mode = va_arg(ap, int);
+ uid = va_arg(ap, int);
+ gid = va_arg(ap, int);
+ header.size = proxy_marshal(iovec, 1, HDR_SZ, "sdddd", path,
+ flags, mode, uid, gid);
+ if (header.size < 0) {
+ goto out_overflow;
+ }
+ header.type = T_CREATE;
+ proxy_marshal(iovec, 1, 0, "dd", header.type, header.size);
+ header.size += HDR_SZ;
+ break;
+ default:
+ error_report("Invalid type %d\n", type);
+ va_end(ap);
+ goto close_error;
+ break;
+ }
+ va_end(ap);
+
+ retval = qemu_write_full(proxy->sockfd, iovec->iov_base, header.size);
+ if (retval != header.size) {
+ goto close_error;
+ }
+
+ switch (type) {
+ case T_OPEN:
+ case T_CREATE:
+ /*
+ * A file descriptor is returned as response for
+ * T_OPEN,T_CREATE on success
+ */
+ retval = v9fs_receivefd(proxy->sockfd, &sock_error);
+ if (sock_error) {
+ goto close_error;
+ }
+ break;
+ }
+ qemu_mutex_unlock(&proxy->mutex);
+ return retval;
+out_overflow:
+ va_end(ap);
+ qemu_mutex_unlock(&proxy->mutex);
+ return -EOVERFLOW;
+close_error:
+ close(proxy->sockfd);
+ proxy->sockfd = -1;
+error:
+ qemu_mutex_unlock(&proxy->mutex);
+ return -EIO;
+}
+
static int proxy_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat
*stbuf)
{
errno = EOPNOTSUPP;
@@ -48,16 +195,33 @@ static int proxy_closedir(FsContext *ctx, V9fsFidOpenState
*fs)
static int proxy_open(FsContext *ctx, V9fsPath *fs_path,
int flags, V9fsFidOpenState *fs)
{
- fs->fd = -1;
+ fs->fd = v9fs_request(ctx->private, T_OPEN, NULL, "sd", fs_path, flags);
+ if (fs->fd < 0) {
+ errno = -fs->fd;
+ fs->fd = -1;
+ }
return fs->fd;
}
static int proxy_opendir(FsContext *ctx,
V9fsPath *fs_path, V9fsFidOpenState *fs)
{
+ int serrno, fd;
+
fs->dir = NULL;
- errno = EOPNOTSUPP;
- return -1;
+ fd = v9fs_request(ctx->private, T_OPEN, NULL, "sd", fs_path, O_DIRECTORY);
+ if (fd < 0) {
+ errno = -fd;
+ return -1;
+ }
+ fs->dir = fdopendir(fd);
+ if (!fs->dir) {
+ serrno = errno;
+ close(fd);
+ errno = serrno;
+ return -1;
+ }
+ return 0;
}
static void proxy_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
@@ -159,9 +323,20 @@ static int proxy_fstat(FsContext *fs_ctx,
static int proxy_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
int flags, FsCred *credp, V9fsFidOpenState *fs)
{
- fs->fd = -1;
- errno = EOPNOTSUPP;
- return -1;
+ V9fsString fullname;
+
+ v9fs_string_init(&fullname);
+ v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
+
+ fs->fd = v9fs_request(fs_ctx->private, T_CREATE, NULL, "sdddd",
+ &fullname, flags, credp->fc_mode,
+ credp->fc_uid, credp->fc_gid);
+ v9fs_string_free(&fullname);
+ if (fs->fd < 0) {
+ errno = -fs->fd;
+ fs->fd = -1;
+ }
+ return fs->fd;
}
diff --git a/hw/9pfs/virtio-9p-proxy.h b/hw/9pfs/virtio-9p-proxy.h
index 7be44bd..27f2507 100644
--- a/hw/9pfs/virtio-9p-proxy.h
+++ b/hw/9pfs/virtio-9p-proxy.h
@@ -2,14 +2,28 @@
#define _QEMU_VIRTIO_9P_PROXY_H
#define BUFF_SZ (64 * 1024)
+#define V9FS_FD_VALID INT_MAX
#define proxy_unmarshal(in_sg, in_elem, offset, fmt, args...) \
v9fs_unmarshal(in_sg, in_elem, offset, 0, fmt, ##args)
#define proxy_marshal(out_sg, out_elem, offset, fmt, args...) \
v9fs_marshal(out_sg, out_elem, offset, 0, fmt, ##args)
+union MsgControl {
+ struct cmsghdr cmsg;
+ char control[CMSG_SPACE(sizeof(int))];
+};
+
typedef struct {
int type;
int size;
} ProxyHeader;
+
+#define HDR_SZ (sizeof(ProxyHeader))
+
+enum {
+ T_OPEN = 1,
+ T_CREATE,
+};
+
#endif
--
1.7.6
- [Qemu-devel] [PATCH V3 00/13] Proxy FS driver for VirtFS, M. Mohan Kumar, 2011/11/21
- [Qemu-devel] [PATCH V3 10/13] hw/9pfs: Proxy getversion, M. Mohan Kumar, 2011/11/21
- [Qemu-devel] [PATCH V3 07/13] hw/9pfs: Add stat/readlink/statfs for proxy FS, M. Mohan Kumar, 2011/11/21
- [Qemu-devel] [PATCH V3 04/13] hw/9pfs: File system helper process for qemu 9p proxy FS, M. Mohan Kumar, 2011/11/21
- [Qemu-devel] [PATCH V3 03/13] hw/9pfs: Add new proxy filesystem driver, M. Mohan Kumar, 2011/11/21
- [Qemu-devel] [PATCH V3 13/13] hw/9pfs: Add support to use named socket for proxy FS, M. Mohan Kumar, 2011/11/21
- [Qemu-devel] [PATCH V3 06/13] hw/9pfs: Create other filesystem objects, M. Mohan Kumar, 2011/11/21
- [Qemu-devel] [PATCH V3 08/13] hw/9pfs: File ownership and others, M. Mohan Kumar, 2011/11/21
- [Qemu-devel] [PATCH V3 05/13] hw/9pfs: Open and create files,
M. Mohan Kumar <=
- [Qemu-devel] [PATCH V3 01/13] hw/9pfs: Move pdu_marshal/unmarshal code to a seperate file, M. Mohan Kumar, 2011/11/21
- [Qemu-devel] [PATCH V3 12/13] hw/9pfs: man page for proxy helper, M. Mohan Kumar, 2011/11/21
- [Qemu-devel] [PATCH V3 11/13] hw/9pfs: Documentation changes related to proxy fs, M. Mohan Kumar, 2011/11/21
- [Qemu-devel] [PATCH V3 02/13] hw/9pfs: Add validation to marshal code, M. Mohan Kumar, 2011/11/21
- [Qemu-devel] [PATCH V3 09/13] hw/9pfs: xattr interfaces in proxy filesystem driver, M. Mohan Kumar, 2011/11/21