qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
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 4/5] osdep: add qemu_recvmsg() wrapper
Date: Tue,  1 May 2012 16:31:46 +0100	[thread overview]
Message-ID: <1335886307-27586-5-git-send-email-stefanha@linux.vnet.ibm.com> (raw)
In-Reply-To: <1335886307-27586-1-git-send-email-stefanha@linux.vnet.ibm.com>

Usually we need to set O_CLOEXEC, which is platform-specific.  Add a
wrapper like qemu_open() but for qemu_recvmsg().

Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
---
 block.c       |    5 +----
 osdep.c       |   46 ++++++++++++++++++++++++++++++++++++++++++++++
 qemu-common.h |    2 ++
 3 files changed, 49 insertions(+), 4 deletions(-)

diff --git a/block.c b/block.c
index d3bf443..e66d64f 100644
--- a/block.c
+++ b/block.c
@@ -176,10 +176,7 @@ int file_open(const char *filename, int flags, mode_t mode)
     msg.msg_control = &msg_control;
     msg.msg_controllen = sizeof(msg_control);
 
-    do {
-        ret = recvmsg(remote_file_fd, &msg, 0);
-    } while (ret == -1 && errno == EINTR);
-    if (ret != sizeof(OpenResponse)) {
+    if (qemu_recvmsg(remote_file_fd, &msg, 0) != sizeof(OpenResponse)) {
         errno = EPIPE;
         return -1;
     }
diff --git a/osdep.c b/osdep.c
index 3e6bada..834e78f 100644
--- a/osdep.c
+++ b/osdep.c
@@ -103,6 +103,52 @@ int qemu_open(const char *name, int flags, ...)
 }
 
 /*
+ * Receive a message from a socket
+ *
+ * This behaves like recvmsg(2) except that EINTR is handled internally and
+ * never returned.  Passed file descriptors will have O_CLOEXEC set.
+ */
+ssize_t qemu_recvmsg(int fd, struct msghdr *msg, int flags)
+{
+    ssize_t ret;
+
+#ifdef MSG_CMSG_CLOEXEC
+    /* Receive file descriptors with O_CLOEXEC */
+    flags |= MSG_CMSG_CLOEXEC;
+#endif
+
+    do {
+        ret = recvmsg(fd, msg, flags);
+    } while (ret == -1 && errno == EINTR);
+    if (ret < 0) {
+        return ret;
+    }
+
+#ifndef MSG_CMSG_CLOEXEC
+    /* As a fallback, set O_CLOEXEC in a way that is not thread-safe */
+    {
+        struct cmsghdr *cmsg;
+        for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
+            int new_fd;
+
+            if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
+                cmsg->cmsg_level != SOL_SOCKET ||
+                cmsg->cmsg_type != SCM_RIGHTS) {
+                continue;
+            }
+
+            new_fd = *((int *)CMSG_DATA(cmsg));
+            if (new_fd >= 0) {
+                qemu_set_cloexec(new_fd);
+            }
+        }
+    }
+#endif
+
+    return ret;
+}
+
+/*
  * A variant of write(2) which handles partial write.
  *
  * Return the number of bytes transferred.
diff --git a/qemu-common.h b/qemu-common.h
index 50f659a..e3a6c4d 100644
--- a/qemu-common.h
+++ b/qemu-common.h
@@ -184,6 +184,8 @@ const char *path(const char *pathname);
 void *qemu_oom_check(void *ptr);
 
 int qemu_open(const char *name, int flags, ...);
+struct msghdr;
+ssize_t qemu_recvmsg(int fd, struct msghdr *msg, int flags);
 ssize_t qemu_write_full(int fd, const void *buf, size_t count)
     QEMU_WARN_UNUSED_RESULT;
 ssize_t qemu_send_full(int fd, const void *buf, size_t count, int flags)
-- 
1.7.10

  parent reply	other threads:[~2012-05-01 15:46 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 ` Stefan Hajnoczi [this message]
2012-05-01 15:31 ` [Qemu-devel] [RFC 5/5] Example -open-hook-fd server Stefan Hajnoczi
2012-05-01 16:04   ` [Qemu-devel] [libvirt] " 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-5-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).