From nobody Mon Feb 9 02:12:40 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1562874145; cv=none; d=zoho.com; s=zohoarc; b=KEea6PIisLhip8XmaTLnSHT+nv54Y+foHl/Qvb8kv95z3nZTyOJU3OYmqE6lzwCu/n4jhoqoV4Y7mcilfTGS4q6MayYx4//rj9dsRLijOXsqRwvnPOlR8fDbFo1VaHP4xMtqAN3XdszQv0nXIAX3yhvROazurrDzNNsZFNNYTho= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1562874145; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=r2ifdwyvHdkUClzPqL8c7ZnhNN7IeHIibB+iiFf7Nfo=; b=QtNAI53c4JqVfg2vd6tVJNorMypjPF3wiOwr8sdRFTuGtppB3k49bUBP9fjnrL4FEBSGnRPkiz1k6eUHAOFmNf2HhDk2qfNUeK/nCsx8jXlpeYoGZ3yUCENRxg2cPZAbGDq0pwdqZVU0/9O2cWTYbnoOPhzx7NBPIsnd9VRwi5Q= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1562874145508606.536623823793; Thu, 11 Jul 2019 12:42:25 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id E76FDC0578F2; Thu, 11 Jul 2019 19:42:23 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B80CE6092E; Thu, 11 Jul 2019 19:42:23 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 7A57B18184B9; Thu, 11 Jul 2019 19:42:23 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x6BJg8tg032117 for ; Thu, 11 Jul 2019 15:42:08 -0400 Received: by smtp.corp.redhat.com (Postfix) id 4CCBD1001B03; Thu, 11 Jul 2019 19:42:08 +0000 (UTC) Received: from mx1.redhat.com (ext-mx13.extmail.prod.ext.phx2.redhat.com [10.5.110.42]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6E4481001281; Thu, 11 Jul 2019 19:42:07 +0000 (UTC) Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B26F9308622D; Thu, 11 Jul 2019 19:42:06 +0000 (UTC) Received: from pps.filterd (m0098393.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x6BJflhV049000; Thu, 11 Jul 2019 15:42:06 -0400 Received: from ppma03dal.us.ibm.com (b.bd.3ea9.ip4.static.sl-reverse.com [169.62.189.11]) by mx0a-001b2d01.pphosted.com with ESMTP id 2tp987mvgg-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 11 Jul 2019 15:42:06 -0400 Received: from pps.filterd (ppma03dal.us.ibm.com [127.0.0.1]) by ppma03dal.us.ibm.com (8.16.0.27/8.16.0.27) with SMTP id x6BJdQqI014282; Thu, 11 Jul 2019 19:42:05 GMT Received: from b03cxnp07029.gho.boulder.ibm.com (b03cxnp07029.gho.boulder.ibm.com [9.17.130.16]) by ppma03dal.us.ibm.com with ESMTP id 2tjk9769a6-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 11 Jul 2019 19:42:05 +0000 Received: from b03ledav004.gho.boulder.ibm.com (b03ledav004.gho.boulder.ibm.com [9.17.130.235]) by b03cxnp07029.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id x6BJg43m57868726 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 11 Jul 2019 19:42:04 GMT Received: from b03ledav004.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id F1D5B7805E; Thu, 11 Jul 2019 19:42:03 +0000 (GMT) Received: from b03ledav004.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 87E377805F; Thu, 11 Jul 2019 19:42:03 +0000 (GMT) Received: from sbct-3.pok.ibm.com (unknown [9.47.158.153]) by b03ledav004.gho.boulder.ibm.com (Postfix) with ESMTP; Thu, 11 Jul 2019 19:42:03 +0000 (GMT) From: Stefan Berger To: libvir-list@redhat.com Date: Thu, 11 Jul 2019 15:41:46 -0400 Message-Id: <20190711194151.3728114-19-stefanb@linux.vnet.ibm.com> In-Reply-To: <20190711194151.3728114-1-stefanb@linux.vnet.ibm.com> References: <20190711194151.3728114-1-stefanb@linux.vnet.ibm.com> MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:, , definitions=2019-07-11_05:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=3 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1907110216 X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.42]); Thu, 11 Jul 2019 19:42:06 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.42]); Thu, 11 Jul 2019 19:42:06 +0000 (UTC) for IP:'148.163.156.1' DOMAIN:'mx0a-001b2d01.pphosted.com' HELO:'mx0a-001b2d01.pphosted.com' FROM:'stefanb@linux.vnet.ibm.com' RCPT:'' X-RedHat-Spam-Score: -0.698 (RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE) 148.163.156.1 mx0a-001b2d01.pphosted.com 148.163.156.1 mx0a-001b2d01.pphosted.com X-Scanned-By: MIMEDefang 2.84 on 10.5.110.42 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-loop: libvir-list@redhat.com Cc: marcandre.lureau@redhat.com, Stefan Berger Subject: [libvirt] [PATCH v4 18/23] tests: Extend command test to transfer large data to process on multiple fds X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Thu, 11 Jul 2019 19:42:24 +0000 (UTC) Content-Type: text/plain; charset="utf-8" Add a test case to commandtest.c to test the transfer of data to a process who received the read-end of pipes' file descriptors. Transfer large (128 kb) byte streams. Extend the commandhelper.c with support for --readfd command line parameter and convert the data receive loop to use poll and receive data on multiple file descriptors (up to 3) and read data into distinct buffers that we grow while adding more (string) data. Signed-off-by: Stefan Berger --- tests/commandhelper.c | 64 +++++++++++++++++++++--- tests/commandtest.c | 112 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 170 insertions(+), 6 deletions(-) diff --git a/tests/commandhelper.c b/tests/commandhelper.c index 32ebeeaef2..8ae9ef06ba 100644 --- a/tests/commandhelper.c +++ b/tests/commandhelper.c @@ -23,6 +23,7 @@ #include #include #include +#include =20 #include "internal.h" #define NO_LIBVIRT @@ -62,13 +63,23 @@ int main(int argc, char **argv) { char *cwd; FILE *log =3D fopen(abs_builddir "/commandhelper.log", "w"); int ret =3D EXIT_FAILURE; + int readfds[3] =3D { STDIN_FILENO, }; + int numreadfds =3D 1; + struct pollfd fds[3]; + int numpollfds =3D 0; + char *buffers[3] =3D {NULL, NULL, NULL}; + size_t buflen[3] =3D {0, 0, 0}; =20 if (!log) return ret; =20 - for (i =3D 1; i < argc; i++) + for (i =3D 1; i < argc; i++) { fprintf(log, "ARG:%s\n", argv[i]); =20 + if (STREQ(argv[i - 1], "--readfd")) + readfds[numreadfds++]=3D atoi(argv[i]); + } + origenv =3D environ; n =3D 0; while (*origenv !=3D NULL) { @@ -134,15 +145,54 @@ int main(int argc, char **argv) { fprintf(stderr, "BEGIN STDERR\n"); fflush(stderr); =20 + for (i =3D 0; i < numreadfds; i++) { + fds[numpollfds].fd =3D readfds[i]; + fds[numpollfds].events =3D POLLIN; + fds[numpollfds].revents =3D 0; + numpollfds++; + } + for (;;) { - got =3D read(STDIN_FILENO, buf, sizeof(buf)); - if (got < 0) + unsigned ctr =3D 0; + + if (poll(fds, numpollfds, -1) < 0) goto cleanup; - if (got =3D=3D 0) + + for (i =3D 0; i < numpollfds; i++) { + if (fds[i].revents & (POLLIN | POLLHUP | POLLERR)) { + fds[i].revents =3D 0; + + got =3D read(fds[i].fd, buf, sizeof(buf)); + if (got < 0) + goto cleanup; + if (got =3D=3D 0) { + /* do not want to hear from this fd anymore */ + fds[i].events =3D 0; + } else { + buffers[i] =3D realloc(buffers[i], buflen[i] + got); + if (!buf[i]) { + fprintf(stdout, "Out of memory!\n"); + goto cleanup; + } + memcpy(buffers[i] + buflen[i], buf, got); + buflen[i] +=3D got; + } + } + } + for (i =3D 0; i < numpollfds; i++) { + if (fds[i].events) { + ctr++; + break; + } + } + if (ctr =3D=3D 0) break; - if (write(STDOUT_FILENO, buf, got) !=3D got) + } + + for (i =3D 0; i < numpollfds; i++) { + if (write(STDOUT_FILENO, buffers[i], buflen[i]) !=3D buflen[i]) goto cleanup; - if (write(STDERR_FILENO, buf, got) !=3D got) + if (write(STDERR_FILENO, buffers[i], buflen[i]) !=3D buflen[i]) goto cleanup; } =20 @@ -154,6 +204,8 @@ int main(int argc, char **argv) { ret =3D EXIT_SUCCESS; =20 cleanup: + for (i =3D 0; i < ARRAY_CARDINALITY(buffers); i++) + free(buffers[i]); fclose(log); free(newenv); return ret; diff --git a/tests/commandtest.c b/tests/commandtest.c index 146cc4c1bf..1e646b1faa 100644 --- a/tests/commandtest.c +++ b/tests/commandtest.c @@ -1196,6 +1196,117 @@ static int test26(const void *unused ATTRIBUTE_UNUS= ED) return ret; } =20 +static int test27(const void *unused ATTRIBUTE_UNUSED) +{ + virCommandPtr cmd =3D virCommandNew(abs_builddir "/commandhelper"); + int pipe1[2]; + int pipe2[2]; + int ret =3D -1; + size_t buflen =3D 1024 * 128; + char *buffer0 =3D malloc(buflen); + char *buffer1 =3D malloc(buflen); + char *buffer2 =3D malloc(buflen); + char *outactual =3D NULL; + char *erractual =3D NULL; + char *outexpect =3D NULL; +#define TEST27_OUTEXPECT_TEMP "BEGIN STDOUT\n" \ + "%s%s%s" \ + "END STDOUT\n" + char *errexpect =3D NULL; +#define TEST27_ERREXPECT_TEMP "BEGIN STDERR\n" \ + "%s%s%s" \ + "END STDERR\n" + + if (!buffer0 || !buffer1 || !buffer2) { + printf("Could not allocate memory for buffer\n"); + goto cleanup; + } + memset(buffer0, 'G', buflen - 2); + buffer0[buflen - 2] =3D '\n'; + buffer0[buflen - 1] =3D 0; + + memset(buffer1, '1', buflen - 2); + buffer1[buflen - 2] =3D '\n'; + buffer1[buflen - 1] =3D 0; + + memset(buffer2, '2', buflen - 2); + buffer2[buflen - 2] =3D '\n'; + buffer2[buflen - 1] =3D 0; + + if (virAsprintf(&outexpect, TEST27_OUTEXPECT_TEMP, + buffer0, buffer1, buffer2) < 0 || + virAsprintf(&errexpect, TEST27_ERREXPECT_TEMP, + buffer0, buffer1, buffer2) < 0) { + printf("Could not virAsprintf expected output\n"); + goto cleanup; + } + + if (pipe(pipe1) < 0 || pipe(pipe2) < 0) { + printf("Could not create pipe: %s\n", strerror(errno)); + goto cleanup; + } + + if (virCommandSetSendBuffer(cmd, pipe1[1], + (unsigned char *)buffer1, buflen - 1) < 0 || + virCommandSetSendBuffer(cmd, pipe2[1], + (unsigned char *)buffer2, buflen - 1) < 0) { + printf("Could not set send buffers\n"); + goto cleanup; + } + pipe1[1] =3D 0; + pipe2[1] =3D 0; + buffer1 =3D NULL; + buffer2 =3D NULL; + + virCommandAddArg(cmd, "--readfd"); + virCommandAddArgFormat(cmd, "%d", pipe1[0]); + virCommandPassFD(cmd, pipe1[0], 0); + + virCommandAddArg(cmd, "--readfd"); + virCommandAddArgFormat(cmd, "%d", pipe2[0]); + virCommandPassFD(cmd, pipe2[0], 0); + + virCommandSetInputBuffer(cmd, buffer0); + virCommandSetOutputBuffer(cmd, &outactual); + virCommandSetErrorBuffer(cmd, &erractual); + + if (virCommandRun(cmd, NULL) < 0) { + printf("Cannot run child %s\n", virGetLastErrorMessage()); + goto cleanup; + } + + virCommandFree(cmd); + + if (!outactual || !erractual) + goto cleanup; + + if (STRNEQ(outactual, outexpect)) { + virTestDifference(stderr, outexpect, outactual); + goto cleanup; + } + if (STRNEQ(erractual, errexpect)) { + virTestDifference(stderr, errexpect, erractual); + goto cleanup; + } + + ret =3D 0; + + cleanup: + VIR_FORCE_CLOSE(pipe1[0]); + VIR_FORCE_CLOSE(pipe2[0]); + VIR_FORCE_CLOSE(pipe1[1]); + VIR_FORCE_CLOSE(pipe2[1]); + VIR_FREE(buffer0); + VIR_FREE(buffer1); + VIR_FREE(buffer2); + VIR_FREE(outactual); + VIR_FREE(erractual); + VIR_FREE(outexpect); + VIR_FREE(errexpect); + + return ret; +} + static void virCommandThreadWorker(void *opaque) { virCommandTestDataPtr test =3D opaque; @@ -1350,6 +1461,7 @@ mymain(void) DO_TEST(test24); DO_TEST(test25); DO_TEST(test26); + DO_TEST(test27); =20 virMutexLock(&test->lock); if (test->running) { --=20 2.20.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list