From patchwork Wed Sep 25 14:32:43 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lei Li X-Patchwork-Id: 277886 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id CF9D92C0087 for ; Thu, 26 Sep 2013 00:35:39 +1000 (EST) Received: from localhost ([::1]:53048 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VOqBl-0005kJ-TY for incoming@patchwork.ozlabs.org; Wed, 25 Sep 2013 10:35:37 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50206) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VOq9Z-0003DI-0b for qemu-devel@nongnu.org; Wed, 25 Sep 2013 10:33:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VOq9P-0006J6-Ud for qemu-devel@nongnu.org; Wed, 25 Sep 2013 10:33:20 -0400 Received: from e28smtp04.in.ibm.com ([122.248.162.4]:52269) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VOq9P-0006Ik-A1 for qemu-devel@nongnu.org; Wed, 25 Sep 2013 10:33:11 -0400 Received: from /spool/local by e28smtp04.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 25 Sep 2013 20:03:06 +0530 Received: from d28dlp01.in.ibm.com (9.184.220.126) by e28smtp04.in.ibm.com (192.168.1.134) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Wed, 25 Sep 2013 20:03:03 +0530 Received: from d28relay02.in.ibm.com (d28relay02.in.ibm.com [9.184.220.59]) by d28dlp01.in.ibm.com (Postfix) with ESMTP id 9CFD3E0053 for ; Wed, 25 Sep 2013 20:04:07 +0530 (IST) Received: from d28av02.in.ibm.com (d28av02.in.ibm.com [9.184.220.64]) by d28relay02.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id r8PEZMYD38338708 for ; Wed, 25 Sep 2013 20:05:23 +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 r8PEX1Za003611 for ; Wed, 25 Sep 2013 20:03:02 +0530 Received: from localhost.localdomain.com ([9.77.178.88]) by d28av02.in.ibm.com (8.14.4/8.14.4/NCO v10.0 AVin) with ESMTP id r8PEWo4f003070; Wed, 25 Sep 2013 20:03:00 +0530 From: Lei Li To: qemu-devel@nongnu.org Date: Wed, 25 Sep 2013 22:32:43 +0800 Message-Id: <1380119568-5530-4-git-send-email-lilei@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.7.6 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> X-TM-AS-MML: No X-Content-Scanned: Fidelis XPS MAILER x-cbid: 13092514-5564-0000-0000-000009E0FC47 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 122.248.162.4 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 Subject: [Qemu-devel] [PATCH 3/8] migration-local: add send_pipefd() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This patch adds send_pipefd() to pass the pipe file descriptor to destination process. Signed-off-by: Lei Li --- include/migration/qemu-file.h | 1 + migration-local.c | 57 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 0 deletions(-) diff --git a/include/migration/qemu-file.h b/include/migration/qemu-file.h index 39ad0bd..5fad65f 100644 --- a/include/migration/qemu-file.h +++ b/include/migration/qemu-file.h @@ -101,6 +101,7 @@ QEMUFile *qemu_fopen_socket(int fd, const char *mode); 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 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 d90b2ff..3875b27 100644 --- a/migration-local.c +++ b/migration-local.c @@ -132,3 +132,60 @@ QEMUFile *qemu_fopen_pipe(int pipefd, const char *mode) return s->file; } + +union MsgControl { + struct cmsghdr cmsg; + char control[CMSG_SPACE(sizeof(int))]; +}; + +/* + * Pass a pipe file descriptor to another process. + * + * Return negative value If pipefd < 0. Return 0 on + * success. + * + */ + +int send_pipefd(int sockfd, int pipefd) +{ + struct cmsghdr *cmsg; + struct iovec iov[1]; + struct msghdr msg; + union MsgControl msg_control; + + char msbuf[1] = { 0x01 }; + int ret; + + iov[0].iov_base = msbuf; + iov[0].iov_len = sizeof(msbuf); + + msg.msg_iov = iov; + msg.msg_iovlen = 1; + + if (pipefd < 0) { + msg.msg_control = NULL; + msg.msg_controllen = 0; + /* Negative status means error */ + msbuf[0] = pipefd; + } else { + msg.msg_control = &msg_control; + msg.msg_controllen = sizeof(msg_control); + + cmsg = &msg_control.cmsg; + cmsg->cmsg_level = SOL_SOCKET; + cmsg->cmsg_type = SCM_RIGHTS; + cmsg->cmsg_len = CMSG_LEN(sizeof(pipefd)); + + /* The pipefd to be passed */ + memcpy(CMSG_DATA(cmsg), &pipefd, sizeof(pipefd)); + } + + do { + ret = sendmsg(sockfd, &msg, 0); + } while (ret < 0 && errno == EINTR); + + if (ret < 0) { + return ret; + } + return 0; +}