[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [RFC 5/5] Example -open-hook-fd server



This patch implements a demo server for the new -open-hook-fd feature.
It opens any filename given to it by QEMU and therefore adds no true
security.  But it serves as a good debugging tool to see what requests
QEMU is making.

  $ gcc -o test-fd-passing -Wall test-fd-passing.c
  $ ./test-fd-passing path/to/my/vm.img

Try:

  (qemu) change ide1-cd0 path/to/a/cdrom.iso

Signed-off-by: Stefan Hajnoczi <stefanha linux vnet ibm com>
---
 test-fd-passing.c |  147 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 147 insertions(+)
 create mode 100644 test-fd-passing.c

diff --git a/test-fd-passing.c b/test-fd-passing.c
new file mode 100644
index 0000000..43b2e86
--- /dev/null
+++ b/test-fd-passing.c
@@ -0,0 +1,147 @@
+/*
+ * QEMU -open-hook-fd test server
+ *
+ * Copyright IBM, Corp. 2012
+ *
+ * Authors:
+ *  Stefan Hajnoczi   <stefanha linux vnet ibm com>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ *
+ * gcc -o test-fd-passing -Wall test-fd-passing.c
+ */
+
+#define _GNU_SOURCE
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <spawn.h>
+
+typedef struct {
+    uint32_t message_len;
+    uint32_t type;
+    uint32_t flags;
+    uint32_t mode;
+    uint32_t filename_len;
+    uint8_t filename[0];
+} OpenRequest;
+
+typedef struct {
+    uint32_t message_len;
+    uint32_t type;
+    int32_t result;
+} OpenResponse;
+
+int main(int argc, char **argv)
+{
+    if (argc != 2) {
+        fprintf(stderr, "usage: %s <image-file>\n", argv[0]);
+        return EXIT_FAILURE;
+    }
+
+    int fds[2];
+    if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) != 0) {
+        perror("socketpair");
+        return EXIT_FAILURE;
+    }
+
+    char *fdstr = NULL;
+    asprintf(&fdstr, "%d", fds[1]);
+
+    char *drivestr = NULL;
+    asprintf(&drivestr, "file=%s,cache=none,if=virtio", argv[1]);
+
+    char *child_argv[] = {
+        "qemu-system-x86_64",
+        "-enable-kvm",
+        "-m", "1024",
+        "-drive", drivestr,
+        "-open-hook-fd", fdstr,
+        NULL,
+    };
+
+    pid_t child_pid;
+    if (posix_spawn(&child_pid, "x86_64-softmmu/qemu-system-x86_64",
+                    NULL, NULL, child_argv, environ) != 0) {
+        fprintf(stderr, "posix_spawn failed\n");
+        return EXIT_FAILURE;
+    }
+    free(drivestr);
+    free(fdstr);
+    close(fds[1]);
+
+    for (;;) {
+        OpenRequest req;
+        char filename[1024];
+
+        if (recv(fds[0], &req, sizeof(req), 0) != sizeof(req)) {
+            perror("recv");
+            return EXIT_FAILURE;
+        }
+
+        if (req.type != 1 /* OpenRequest */) {
+            fprintf(stderr, "Expected request type 1, got %u\n", req.type);
+            return EXIT_FAILURE;
+        }
+
+        if (req.filename_len > sizeof(filename) - 1) {
+            fprintf(stderr, "Filename length too large (%u)\n",
+                    req.filename_len);
+            return EXIT_FAILURE;
+        }
+
+        if (recv(fds[0], filename, req.filename_len, 0) != req.filename_len) {
+            perror("recv");
+            return EXIT_FAILURE;
+        }
+        filename[req.filename_len] = '\0';
+
+        fprintf(stderr, "open(\"%s\", %#x, %#o) = ",
+                filename, req.flags, req.mode);
+
+        int fd, ret;
+        fd = ret = open(filename, req.flags, req.mode);
+
+        fprintf(stderr, "%d (errno %d)\n", ret, errno);
+
+        OpenResponse resp = {
+            .message_len = sizeof(resp),
+            .type = 1,
+            .result = ret < 0 ? -errno : 0,
+        };
+        struct iovec iov = {
+            .iov_base = &resp,
+            .iov_len = sizeof(resp),
+        };
+        char buf[CMSG_SPACE(sizeof(int))];
+        struct msghdr msg = {
+            .msg_iov = &iov,
+            .msg_iovlen = 1,
+        };
+        if (ret >= 0) {
+            msg.msg_control = buf;
+            msg.msg_controllen = sizeof(buf);
+
+            struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
+            cmsg->cmsg_level = SOL_SOCKET;
+            cmsg->cmsg_type = SCM_RIGHTS;
+            cmsg->cmsg_len = CMSG_LEN(sizeof(int));
+
+            memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
+        }
+
+        ret = sendmsg(fds[0], &msg, 0);
+        if (ret < 0) {
+            perror("sendmsg");
+            return EXIT_FAILURE;
+        }
+        close(fd);
+    }
+}
-- 
1.7.10


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]