From patchwork Mon Aug 26 03:13:46 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wayne Xia X-Patchwork-Id: 269802 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 366E82C00A6 for ; Mon, 26 Aug 2013 13:16:51 +1000 (EST) Received: from localhost ([::1]:48491 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VDnIP-0006dK-3I for incoming@patchwork.ozlabs.org; Sun, 25 Aug 2013 23:16:49 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60468) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VDnHs-0006UK-HE for qemu-devel@nongnu.org; Sun, 25 Aug 2013 23:16:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VDnHk-0006Kx-7e for qemu-devel@nongnu.org; Sun, 25 Aug 2013 23:16:16 -0400 Received: from e28smtp02.in.ibm.com ([122.248.162.2]:58634) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VDnHj-0006K3-JZ for qemu-devel@nongnu.org; Sun, 25 Aug 2013 23:16:08 -0400 Received: from /spool/local by e28smtp02.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 26 Aug 2013 08:35:45 +0530 Received: from d28dlp01.in.ibm.com (9.184.220.126) by e28smtp02.in.ibm.com (192.168.1.132) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 26 Aug 2013 08:35:42 +0530 Received: from d28relay05.in.ibm.com (d28relay05.in.ibm.com [9.184.220.62]) by d28dlp01.in.ibm.com (Postfix) with ESMTP id 592BBE0053 for ; Mon, 26 Aug 2013 08:46:34 +0530 (IST) Received: from d28av05.in.ibm.com (d28av05.in.ibm.com [9.184.220.67]) by d28relay05.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id r7Q3FvLs48562224 for ; Mon, 26 Aug 2013 08:45:57 +0530 Received: from d28av05.in.ibm.com (localhost [127.0.0.1]) by d28av05.in.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id r7Q3Fxgo026319 for ; Mon, 26 Aug 2013 08:46:00 +0530 Received: from RH63Wenchao (wenchaox.cn.ibm.com [9.115.122.153]) by d28av05.in.ibm.com (8.14.4/8.14.4/NCO v10.0 AVin) with ESMTP id r7Q3DrmS021933; Mon, 26 Aug 2013 08:45:58 +0530 From: Wenchao Xia To: qemu-devel@nongnu.org Date: Mon, 26 Aug 2013 11:13:46 +0800 Message-Id: <1377486828-10788-2-git-send-email-xiawenc@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1377486828-10788-1-git-send-email-xiawenc@linux.vnet.ibm.com> References: <1377486828-10788-1-git-send-email-xiawenc@linux.vnet.ibm.com> X-TM-AS-MML: No X-Content-Scanned: Fidelis XPS MAILER x-cbid: 13082603-5816-0000-0000-00000993AF48 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 122.248.162.2 Cc: pbonzini@redhat.com, Wenchao Xia , armbru@redhat.com, anthony@codemonkey.ws, lcapitulino@redhat.com Subject: [Qemu-devel] [PATCH 1/3] qemu-iotests: add unix socket help program 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 program can do a sendmsg call to transfer fd with unix socket, which is not supported in python2. The built binary will not be deleted in clean, but it is a existing issue in ./tests, which should be solved in another patch. Signed-off-by: Wenchao Xia --- configure | 2 +- tests/Makefile | 4 +- tests/qemu-iotests/socket_scm_helper.c | 119 ++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 tests/qemu-iotests/socket_scm_helper.c diff --git a/configure b/configure index 18fa608..0a18038 100755 --- a/configure +++ b/configure @@ -4507,7 +4507,7 @@ if [ "$dtc_internal" = "yes" ]; then fi # build tree in object directory in case the source is not in the current directory -DIRS="tests tests/tcg tests/tcg/cris tests/tcg/lm32 tests/libqos tests/qapi-schema tests/tcg/xtensa" +DIRS="tests tests/tcg tests/tcg/cris tests/tcg/lm32 tests/libqos tests/qapi-schema tests/tcg/xtensa tests/qemu-iotests" DIRS="$DIRS pc-bios/optionrom pc-bios/spapr-rtas pc-bios/s390-ccw" DIRS="$DIRS roms/seabios roms/vgabios" DIRS="$DIRS qapi-generated" diff --git a/tests/Makefile b/tests/Makefile index baba9e9..57131db 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -172,6 +172,8 @@ tests/boot-order-test$(EXESUF): tests/boot-order-test.o $(libqos-obj-y) tests/tmp105-test$(EXESUF): tests/tmp105-test.o $(libqos-omap-obj-y) tests/i440fx-test$(EXESUF): tests/i440fx-test.o $(libqos-pc-obj-y) tests/fw_cfg-test$(EXESUF): tests/fw_cfg-test.o $(libqos-pc-obj-y) +tests/qemu-iotests/socket_scm_helper(EXESUF): tests/qemu-iotests/socket_scm_helper.o + $(call LINK, $^) # QTest rules @@ -250,7 +252,7 @@ check-report.html: check-report.xml # Other tests .PHONY: check-tests/qemu-iotests-quick.sh -check-tests/qemu-iotests-quick.sh: tests/qemu-iotests-quick.sh qemu-img$(EXESUF) qemu-io$(EXESUF) +check-tests/qemu-iotests-quick.sh: tests/qemu-iotests-quick.sh qemu-img$(EXESUF) qemu-io$(EXESUF) tests/qemu-iotests/socket_scm_helper$(EXESUF) $< .PHONY: check-tests/test-qapi.py diff --git a/tests/qemu-iotests/socket_scm_helper.c b/tests/qemu-iotests/socket_scm_helper.c new file mode 100644 index 0000000..1955a3e --- /dev/null +++ b/tests/qemu-iotests/socket_scm_helper.c @@ -0,0 +1,119 @@ +/* + * SCM_RIGHT with unix socket help program for test + * + * Copyright IBM, Inc. 2013 + * + * Authors: + * Wenchao Xia + * + * This work is licensed under the terms of the GNU LGPL, version 2 or later. + * See the COPYING.LIB file in the top-level directory. + */ + +#include +#include +#include +#include +#include + +/* #define SOCKET_SCM_DEBUG */ + +static int send_fd(int fd, int fd_to_send, const char *data, size_t len) +{ + struct msghdr msg; + struct iovec iov[1]; + int ret; + char control[CMSG_SPACE(sizeof(int))]; + struct cmsghdr *cmsg; + + if (fd < 0) { + fprintf(stderr, "Socket fd is invalid.\n"); + return -1; + } + if (fd_to_send < 0) { + fprintf(stderr, "Fd to send is invalid.\n"); + return -1; + } + if (data == NULL || len <= 0) { + fprintf(stderr, "Data buffer must be valid.\n"); + return -1; + } + + memset(&msg, 0, sizeof(msg)); + memset(control, 0, sizeof(control)); + + iov[0].iov_base = (void *)data; + iov[0].iov_len = len; + + msg.msg_iov = iov; + msg.msg_iovlen = 1; + + msg.msg_control = control; + msg.msg_controllen = sizeof(control); + + cmsg = CMSG_FIRSTHDR(&msg); + + cmsg->cmsg_len = CMSG_LEN(sizeof(int)); + cmsg->cmsg_level = SOL_SOCKET; + cmsg->cmsg_type = SCM_RIGHTS; + memcpy(CMSG_DATA(cmsg), &fd, sizeof(int)); + + do { + ret = sendmsg(fd, &msg, 0); + } while (ret < 0 && errno == EINTR); + + if (ret < 0) { + fprintf(stderr, "Failed to send msg, reason: %s.\n", strerror(errno)); + } + + return ret; +} + +/* + * To make things simple, the caller need to specify: + * 1. socket fd. + * 2. fd to send. + * 3. msg to send. + */ +int main(int argc, char **argv, char **envp) +{ + int sock, fd, ret, buflen; + const char *buf; +#ifdef SOCKET_SCM_DEBUG + int i; + for (i = 0; i < argc; i++) { + fprintf(stderr, "Parameter %d: %s.\n", i, argv[i]); + } +#endif + + if (argc < 4) { + fprintf(stderr, + "Invalid parameter, use it as:\n" + "%s SOCKET_FD FD_TO_SEND MSG.\n", + argv[0]); + return 1; + } + + errno = 0; + sock = strtol(argv[1], NULL, 10); + if (errno) { + fprintf(stderr, "Failed in strtol for socket fd, reason: %s.\n", + strerror(errno)); + return 1; + } + fd = strtol(argv[2], NULL, 10); + if (errno) { + fprintf(stderr, "Failed in strtol for fd to send, reason: %s.\n", + strerror(errno)); + return 1; + } + + buf = argv[3]; + buflen = strlen(buf); + + ret = send_fd(sock, fd, buf, buflen); + if (ret < 0) { + return 1; + } + return 0; +}