qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Lei Li <lilei@linux.vnet.ibm.com>
To: qemu-devel@nongnu.org
Cc: aarcange@redhat.com, Lei Li <lilei@linux.vnet.ibm.com>,
	quintela@redhat.com, mdroth@linux.vnet.ibm.com,
	mrhines@linux.vnet.ibm.com, anthony@codemonkey.ws,
	lagarcia@br.ibm.com, pbonzini@redhat.com, rcj@linux.vnet.ibm.com
Subject: [Qemu-devel] [PATCH 4/8] migration-local: add recv_pipefd()
Date: Wed, 25 Sep 2013 22:32:44 +0800	[thread overview]
Message-ID: <1380119568-5530-5-git-send-email-lilei@linux.vnet.ibm.com> (raw)
In-Reply-To: <1380119568-5530-1-git-send-email-lilei@linux.vnet.ibm.com>

This patch adds recv_pipefd() to receive the pipe file descriptor
that passed by source process.

Signed-off-by: Lei Li <lilei@linux.vnet.ibm.com>
---
 include/migration/qemu-file.h |    1 +
 migration-local.c             |   56 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+), 0 deletions(-)

diff --git a/include/migration/qemu-file.h b/include/migration/qemu-file.h
index 5fad65f..e4df258 100644
--- a/include/migration/qemu-file.h
+++ b/include/migration/qemu-file.h
@@ -102,6 +102,7 @@ QEMUFile *qemu_popen_cmd(const char *command, const char *mode);
 QEMUFile *qemu_fopen_pipe(int sockfd, const char *mode);
 
 int send_pipefd(int sockfd, int pipefd);
+int recv_pipefd(int sockfd);
 int qemu_get_fd(QEMUFile *f);
 int qemu_fclose(QEMUFile *f);
 int64_t qemu_ftell(QEMUFile *f);
diff --git a/migration-local.c b/migration-local.c
index 3875b27..40b8f34 100644
--- a/migration-local.c
+++ b/migration-local.c
@@ -189,3 +189,59 @@ int send_pipefd(int sockfd, int pipefd)
     }
     return 0;
 }
+
+/*
+ * Receive a pipe file descriptor from a source process
+ * via unix socket.
+ *
+ * Return negative value if there has been an recvmsg error or
+ * no fd to be reveived. Return 0 if the connection closed by
+ * source. Return file descriptor on success.
+ *
+ */
+int recv_pipefd(int sockfd)
+{
+    int pipefd;
+    struct cmsghdr *cmsg;
+    struct msghdr msg;
+    struct iovec iov[1];
+    char req[1];
+    int ret;
+    union MsgControl msg_control;
+
+    iov[0].iov_base = req;
+    iov[0].iov_len = sizeof(req);
+
+    memset(&msg, 0, sizeof(msg));
+    msg.msg_iov = iov;
+    msg.msg_iovlen = 1;
+    msg.msg_control = &msg_control;
+    msg.msg_controllen = sizeof(msg_control);
+
+    do {
+        ret = recvmsg(sockfd, &msg, 0);
+    } while (ret < 0 && errno == EINTR);
+    if (ret <= 0) {
+        return ret;
+    }
+
+    /* req 0x01 means there is a file descriptor to receive */
+    if (req[0] != 0x01) {
+        return -1;
+    }
+
+    cmsg = CMSG_FIRSTHDR(&msg);
+    while (cmsg) {
+        if (cmsg->cmsg_level == SOL_SOCKET &&
+            cmsg->cmsg_type == SCM_RIGHTS &&
+            cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
+
+            /* The file descriptor to be received */
+            memcpy(&pipefd, CMSG_DATA(cmsg), sizeof(pipefd));
+            return pipefd;
+        }
+        cmsg = CMSG_NXTHDR(&msg, cmsg);
+    }
+
+    return -1;
+}
-- 
1.7.7.6

  parent reply	other threads:[~2013-09-25 14:33 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-25 14:32 [Qemu-devel] [PATCH 0/8 RFC] migration: Introduce side channel for RAM Lei Li
2013-09-25 14:32 ` [Qemu-devel] [PATCH 1/8] migration-local: add pipe protocol for QEMUFileOps Lei Li
2013-09-25 14:32 ` [Qemu-devel] [PATCH 2/8] migration-loca: add qemu_fopen_pipe() Lei Li
2013-09-25 14:32 ` [Qemu-devel] [PATCH 3/8] migration-local: add send_pipefd() Lei Li
2013-09-25 14:32 ` Lei Li [this message]
2013-09-25 14:32 ` [Qemu-devel] [PATCH 5/8] QAPI: introduce magration capability unix_page_flipping Lei Li
2013-09-25 14:32 ` [Qemu-devel] [PATCH 6/8] migration: add migrate_unix_page_flipping() Lei Li
2013-09-25 14:32 ` [Qemu-devel] [PATCH 7/8] migration-unix: side channel support on unix outgoing Lei Li
2013-09-25 14:32 ` [Qemu-devel] [PATCH 8/8] migration-unix: side channel support on unix incoming Lei Li
2013-09-25 15:02 ` [Qemu-devel] [PATCH 0/8 RFC] migration: Introduce side channel for RAM Paolo Bonzini
2013-09-26 12:44   ` Lei Li
2013-09-26 12:54     ` Paolo Bonzini
2013-10-03  4:03     ` Lei Li
2013-10-03  8:23       ` Paolo Bonzini
2013-10-03 10:28         ` Lei Li
2013-10-03 10:34           ` Paolo Bonzini
2013-10-03 13:29             ` Lei Li
2013-10-03 13:34               ` Paolo Bonzini
2013-10-03 13:37                 ` Lei Li

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=1380119568-5530-5-git-send-email-lilei@linux.vnet.ibm.com \
    --to=lilei@linux.vnet.ibm.com \
    --cc=aarcange@redhat.com \
    --cc=anthony@codemonkey.ws \
    --cc=lagarcia@br.ibm.com \
    --cc=mdroth@linux.vnet.ibm.com \
    --cc=mrhines@linux.vnet.ibm.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=quintela@redhat.com \
    --cc=rcj@linux.vnet.ibm.com \
    /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).