From: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
To: qemu-devel@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>,
Anthony Liguori <aliguori@us.ibm.com>,
Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>,
libvir-list@redhat.com, Corey Bryant <coreyb@linux.vnet.ibm.com>
Subject: [Qemu-devel] [RFC 5/5] Example -open-hook-fd server
Date: Tue, 1 May 2012 16:31:47 +0100 [thread overview]
Message-ID: <1335886307-27586-6-git-send-email-stefanha@linux.vnet.ibm.com> (raw)
In-Reply-To: <1335886307-27586-1-git-send-email-stefanha@linux.vnet.ibm.com>
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
next prev parent reply other threads:[~2012-05-01 15:51 UTC|newest]
Thread overview: 37+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-05-01 15:31 [Qemu-devel] [RFC 0/5] block: File descriptor passing using -open-hook-fd Stefan Hajnoczi
2012-05-01 15:31 ` [Qemu-devel] [RFC 1/5] block: add open() wrapper that can be hooked by libvirt Stefan Hajnoczi
2012-05-01 15:31 ` [Qemu-devel] [RFC 2/5] block: add new command line parameter that and protocol description Stefan Hajnoczi
2012-05-02 8:58 ` Daniel P. Berrange
2012-05-02 9:03 ` Daniel P. Berrange
2012-05-01 15:31 ` [Qemu-devel] [RFC 3/5] block: plumb up open-hook-fd option Stefan Hajnoczi
2012-05-01 15:31 ` [Qemu-devel] [RFC 4/5] osdep: add qemu_recvmsg() wrapper Stefan Hajnoczi
2012-05-01 15:31 ` Stefan Hajnoczi [this message]
2012-05-01 16:04 ` [Qemu-devel] [libvirt] [RFC 5/5] Example -open-hook-fd server Stefan Hajnoczi
2012-05-01 20:25 ` [Qemu-devel] [RFC 0/5] block: File descriptor passing using -open-hook-fd Anthony Liguori
2012-05-01 20:56 ` [Qemu-devel] [libvirt] " Eric Blake
2012-05-01 21:52 ` Anthony Liguori
2012-05-02 16:40 ` Paolo Bonzini
2012-05-01 21:45 ` [Qemu-devel] " Corey Bryant
2012-05-01 21:53 ` Anthony Liguori
2012-05-01 22:15 ` [Qemu-devel] [libvirt] " Eric Blake
2012-05-01 22:21 ` Anthony Liguori
2012-05-07 16:10 ` Corey Bryant
2012-05-02 8:20 ` [Qemu-devel] " Kevin Wolf
2012-05-02 8:27 ` Stefan Hajnoczi
2012-05-02 9:38 ` Kevin Wolf
2012-05-02 8:53 ` [Qemu-devel] [libvirt] " Daniel P. Berrange
2012-05-02 9:45 ` Kevin Wolf
2012-05-02 9:56 ` Daniel P. Berrange
2012-05-02 19:25 ` Paolo Bonzini
2012-05-03 19:19 ` Anthony Liguori
2012-05-02 9:01 ` [Qemu-devel] " Daniel P. Berrange
2012-05-04 3:28 ` [Qemu-devel] [libvirt] " Zhi Yong Wu
2012-05-17 13:42 ` Stefan Hajnoczi
2012-05-17 13:57 ` Zhi Yong Wu
2012-05-17 14:02 ` Zhi Yong Wu
2012-05-18 10:38 ` Stefan Hajnoczi
2012-05-17 14:14 ` Eric Blake
2012-05-18 10:38 ` Stefan Hajnoczi
2012-07-09 20:00 ` Anthony Liguori
2012-07-09 20:29 ` Eric Blake
2012-07-09 20:46 ` Anthony Liguori
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1335886307-27586-6-git-send-email-stefanha@linux.vnet.ibm.com \
--to=stefanha@linux.vnet.ibm.com \
--cc=aliguori@us.ibm.com \
--cc=coreyb@linux.vnet.ibm.com \
--cc=kwolf@redhat.com \
--cc=libvir-list@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).