yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-vhost-user-support-receiving-file-descriptors-in-sla.patch

26ba25
From 426cb49d423f5386b2d869009e29d7061e51b998 Mon Sep 17 00:00:00 2001
26ba25
From: "plai@redhat.com" <plai@redhat.com>
26ba25
Date: Thu, 21 Jun 2018 18:54:38 +0200
26ba25
Subject: [PATCH 159/268] vhost-user: support receiving file descriptors in
26ba25
 slave_read
26ba25
26ba25
RH-Author: plai@redhat.com
26ba25
Message-id: <1529607285-9942-4-git-send-email-plai@redhat.com>
26ba25
Patchwork-id: 80933
26ba25
O-Subject: [RHEL7.6 PATCH BZ 1526645 03/10] vhost-user: support receiving file descriptors in slave_read
26ba25
Bugzilla: 1526645
26ba25
RH-Acked-by: Michael S. Tsirkin <mst@redhat.com>
26ba25
RH-Acked-by: Maxime Coquelin <maxime.coquelin@redhat.com>
26ba25
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
26ba25
26ba25
From: Tiwei Bie <tiwei.bie@intel.com>
26ba25
26ba25
Signed-off-by: Tiwei Bie <tiwei.bie@intel.com>
26ba25
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
26ba25
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
26ba25
(cherry picked from commit 1f3a4519b1c107b5db2434b30638353978366b4d)
26ba25
Signed-off-by: Paul Lai <plai@redhat.com>
26ba25
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
26ba25
---
26ba25
 hw/virtio/vhost-user.c | 41 ++++++++++++++++++++++++++++++++++++++++-
26ba25
 1 file changed, 40 insertions(+), 1 deletion(-)
26ba25
26ba25
diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c
26ba25
index 38da869..85d8fd2 100644
26ba25
--- a/hw/virtio/vhost-user.c
26ba25
+++ b/hw/virtio/vhost-user.c
26ba25
@@ -852,14 +852,44 @@ static void slave_read(void *opaque)
26ba25
     VhostUserHeader hdr = { 0, };
26ba25
     VhostUserPayload payload = { 0, };
26ba25
     int size, ret = 0;
26ba25
+    struct iovec iov;
26ba25
+    struct msghdr msgh;
26ba25
+    int fd = -1;
26ba25
+    char control[CMSG_SPACE(sizeof(fd))];
26ba25
+    struct cmsghdr *cmsg;
26ba25
+    size_t fdsize;
26ba25
+
26ba25
+    memset(&msgh, 0, sizeof(msgh));
26ba25
+    msgh.msg_iov = &iov;
26ba25
+    msgh.msg_iovlen = 1;
26ba25
+    msgh.msg_control = control;
26ba25
+    msgh.msg_controllen = sizeof(control);
26ba25
 
26ba25
     /* Read header */
26ba25
-    size = read(u->slave_fd, &hdr, VHOST_USER_HDR_SIZE);
26ba25
+    iov.iov_base = &hd;;
26ba25
+    iov.iov_len = VHOST_USER_HDR_SIZE;
26ba25
+
26ba25
+    size = recvmsg(u->slave_fd, &msgh, 0);
26ba25
     if (size != VHOST_USER_HDR_SIZE) {
26ba25
         error_report("Failed to read from slave.");
26ba25
         goto err;
26ba25
     }
26ba25
 
26ba25
+    if (msgh.msg_flags & MSG_CTRUNC) {
26ba25
+        error_report("Truncated message.");
26ba25
+        goto err;
26ba25
+    }
26ba25
+
26ba25
+    for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
26ba25
+         cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
26ba25
+            if (cmsg->cmsg_level == SOL_SOCKET &&
26ba25
+                cmsg->cmsg_type == SCM_RIGHTS) {
26ba25
+                    fdsize = cmsg->cmsg_len - CMSG_LEN(0);
26ba25
+                    memcpy(&fd, CMSG_DATA(cmsg), fdsize);
26ba25
+                    break;
26ba25
+            }
26ba25
+    }
26ba25
+
26ba25
     if (hdr.size > VHOST_USER_PAYLOAD_SIZE) {
26ba25
         error_report("Failed to read msg header."
26ba25
                 " Size %d exceeds the maximum %zu.", hdr.size,
26ba25
@@ -883,9 +913,15 @@ static void slave_read(void *opaque)
26ba25
         break;
26ba25
     default:
26ba25
         error_report("Received unexpected msg type.");
26ba25
+        if (fd != -1) {
26ba25
+            close(fd);
26ba25
+        }
26ba25
         ret = -EINVAL;
26ba25
     }
26ba25
 
26ba25
+    /* Message handlers need to make sure that fd will be consumed. */
26ba25
+    fd = -1;
26ba25
+
26ba25
     /*
26ba25
      * REPLY_ACK feature handling. Other reply types has to be managed
26ba25
      * directly in their request handlers.
26ba25
@@ -918,6 +954,9 @@ err:
26ba25
     qemu_set_fd_handler(u->slave_fd, NULL, NULL, NULL);
26ba25
     close(u->slave_fd);
26ba25
     u->slave_fd = -1;
26ba25
+    if (fd != -1) {
26ba25
+        close(fd);
26ba25
+    }
26ba25
     return;
26ba25
 }
26ba25
 
26ba25
-- 
26ba25
1.8.3.1
26ba25