diff mbox

[07/17] migration-local: add send_pipefd()

Message ID 1382412341-1173-8-git-send-email-lilei@linux.vnet.ibm.com
State New
Headers show

Commit Message

Lei Li Oct. 22, 2013, 3:25 a.m. UTC
This patch adds send_pipefd() to pass the pipe file descriptor
to destination process.

Signed-off-by: Lei Li <lilei@linux.vnet.ibm.com>
---
 migration-local.c |   53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 53 insertions(+), 0 deletions(-)
diff mbox

Patch

diff --git a/migration-local.c b/migration-local.c
index 6214ff9..f2871d8 100644
--- a/migration-local.c
+++ b/migration-local.c
@@ -165,3 +165,56 @@  fail:
     g_free(s);
     return NULL;
 }
+
+
+/*
+ * Pass a pipe file descriptor to another process.
+ *
+ * Return negative value If pipefd < 0. Return 0 on
+ * success.
+ *
+ */
+static int send_pipefd(int sockfd, int pipefd)
+{
+    struct msghdr msg;
+    struct iovec iov[1];
+    ssize_t ret;
+
+    union {
+      struct cmsghdr cm;
+      char control[CMSG_SPACE(sizeof(int))];
+    } control_un;
+    struct cmsghdr *cmptr;
+    char req[1] = { 0x01 };
+
+    if (pipefd < 0) {
+        msg.msg_control = NULL;
+        msg.msg_controllen = 0;
+        /* Negative status means error */
+        req[0] = pipefd;
+    } else {
+        msg.msg_control = control_un.control;
+        msg.msg_controllen = sizeof(control_un.control);
+
+        cmptr = CMSG_FIRSTHDR(&msg);
+        cmptr->cmsg_len = CMSG_LEN(sizeof(int));
+        cmptr->cmsg_level = SOL_SOCKET;
+        cmptr->cmsg_type = SCM_RIGHTS;
+        *((int *) CMSG_DATA(cmptr)) = pipefd;
+
+        msg.msg_name = NULL;
+        msg.msg_namelen = 0;
+
+        iov[0].iov_base = req;
+        iov[0].iov_len = sizeof(req);
+        msg.msg_iov = iov;
+        msg.msg_iovlen = 1;
+    }
+
+    ret = sendmsg(sockfd, &msg, 0);
+    if (ret <= 0) {
+        DPRINTF("sendmsg error: %s\n", strerror(errno));
+    }
+
+    return ret;
+}