From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50222) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VOq9a-0003GK-Oe for qemu-devel@nongnu.org; Wed, 25 Sep 2013 10:33:31 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VOq9R-0006JV-O9 for qemu-devel@nongnu.org; Wed, 25 Sep 2013 10:33:22 -0400 Received: from e28smtp06.in.ibm.com ([122.248.162.6]:38275) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VOq9R-0006J2-3v for qemu-devel@nongnu.org; Wed, 25 Sep 2013 10:33:13 -0400 Received: from /spool/local by e28smtp06.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 25 Sep 2013 20:03:08 +0530 Received: from d28relay05.in.ibm.com (d28relay05.in.ibm.com [9.184.220.62]) by d28dlp01.in.ibm.com (Postfix) with ESMTP id 28DFAE0055 for ; Wed, 25 Sep 2013 20:04:10 +0530 (IST) Received: from d28av02.in.ibm.com (d28av02.in.ibm.com [9.184.220.64]) by d28relay05.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id r8PEX36h45285458 for ; Wed, 25 Sep 2013 20:03:03 +0530 Received: from d28av02.in.ibm.com (localhost [127.0.0.1]) by d28av02.in.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id r8PEX4na003812 for ; Wed, 25 Sep 2013 20:03:05 +0530 From: Lei Li Date: Wed, 25 Sep 2013 22:32:44 +0800 Message-Id: <1380119568-5530-5-git-send-email-lilei@linux.vnet.ibm.com> In-Reply-To: <1380119568-5530-1-git-send-email-lilei@linux.vnet.ibm.com> References: <1380119568-5530-1-git-send-email-lilei@linux.vnet.ibm.com> Subject: [Qemu-devel] [PATCH 4/8] migration-local: add recv_pipefd() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: aarcange@redhat.com, Lei Li , 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 This patch adds recv_pipefd() to receive the pipe file descriptor that passed by source process. Signed-off-by: Lei Li --- 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