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

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