From nobody Mon Feb 9 09:51:46 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) client-ip=63.128.21.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1612178932; cv=none; d=zohomail.com; s=zohoarc; b=J9EC/ofK61hNpYi6KLWIfyN1f1Y6cFcSvS0GnEKkBGGlypUSZYsH544WXDznPBpPohIh0RGNeTxKISMPuBKh4Uc06Ds4xcY7cHvtQH1xnJyE5eAAMTj+GNIGKHTcuO3vb5oJRkzYoTxDtLRyN7v3MGS6YYtkDsWyQ7kuMnSHbcc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1612178932; 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; bh=pjLtvqA9/XOl7EeXDRWQ+nnFOG4PrAijXxvl10Jgp1w=; b=NbuSy9+f7p4zFV44ZNCY351Q/uSQnby1WI+wJfiGZuzf49WZJE2G+xZbIcrRI4Xp+430Nh7+3hmjA5CKMtHkXhIpa/NIPBQPf2M50Nj7FSPGp4fn62L5uYqTTcOAD+vm74SFTxwPtYbwTXYGrCUy+R3BxUKly6scEkkmFh8dQ9c= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by mx.zohomail.com with SMTPS id 1612178932197462.11912016236283; Mon, 1 Feb 2021 03:28:52 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-454-iCcHPQqAO0Wvamyp0ZQH9w-1; Mon, 01 Feb 2021 06:28:48 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C8151AFA88; Mon, 1 Feb 2021 11:28:40 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A4D152C313; Mon, 1 Feb 2021 11:28:40 +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 728534BB7B; Mon, 1 Feb 2021 11:28:40 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 111BSIq6003304 for ; Mon, 1 Feb 2021 06:28:18 -0500 Received: by smtp.corp.redhat.com (Postfix) id 44DAC6A908; Mon, 1 Feb 2021 11:28:18 +0000 (UTC) Received: from work.redhat.com (ovpn-113-167.ams2.redhat.com [10.36.113.167]) by smtp.corp.redhat.com (Postfix) with ESMTP id 574491840B; Mon, 1 Feb 2021 11:28:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612178931; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=pjLtvqA9/XOl7EeXDRWQ+nnFOG4PrAijXxvl10Jgp1w=; b=AQgPd2nQTiRFqRewwRxUEyYusHD6GTX0/1M/g156arZr3qO5LOOxXpv3xne65bGbcYc3Ov FPBxAvNITro8b2C0gf1v5Tl3QD9foRKGHNWu2uVeU4iLO+NWMAhHPLfZLMi/kOWjk3isrk wvnYu34xEr/H+YV6Gc7gSSfhegjFtSE= X-MC-Unique: iCcHPQqAO0Wvamyp0ZQH9w-1 From: Tim Wiederhake To: libvir-list@redhat.com Subject: [libvirt PATCH v2 07/20] commandhelper: Factor out parseArguments Date: Mon, 1 Feb 2021 12:27:51 +0100 Message-Id: <20210201112804.144366-8-twiederh@redhat.com> In-Reply-To: <20210201112804.144366-1-twiederh@redhat.com> References: <20210201112804.144366-1-twiederh@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: libvir-list@redhat.com Cc: Peter Krempa , Tim Wiederhake 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Signed-off-by: Tim Wiederhake Reviewed-by: Peter Krempa --- tests/commandhelper.c | 83 +++++++++++++++++++++++++++++-------------- 1 file changed, 57 insertions(+), 26 deletions(-) diff --git a/tests/commandhelper.c b/tests/commandhelper.c index 24373d6c07..f7e406ec83 100644 --- a/tests/commandhelper.c +++ b/tests/commandhelper.c @@ -35,6 +35,51 @@ extern char **environ; =20 # define VIR_FROM_THIS VIR_FROM_NONE =20 +struct Arguments { + int readfds[3]; + int numreadfds; + bool daemonize_check; + bool close_stdin; +}; + +static struct Arguments *parseArguments(int argc, char** argv) +{ + struct Arguments* args =3D NULL; + int ret =3D -1; + size_t i; + + if (!(args =3D calloc(1, sizeof(*args)))) + goto cleanup; + + args->numreadfds =3D 1; + args->readfds[0] =3D STDIN_FILENO; + + for (i =3D 1; i < argc; i++) { + if (STREQ(argv[i - 1], "--readfd")) { + char c; + + if (1 !=3D sscanf(argv[i], "%u%c", + &args->readfds[args->numreadfds++], &c)) { + printf("Could not parse fd %s\n", argv[i]); + goto cleanup; + } + } else if (STREQ(argv[i], "--check-daemonize")) { + args->daemonize_check =3D true; + } else if (STREQ(argv[i], "--close-stdin")) { + args->close_stdin =3D true; + } + } + + ret =3D 0; + + cleanup: + if (ret =3D=3D 0) + return args; + + free(args); + return NULL; +} + static int envsort(const void *a, const void *b) { const char *astr =3D *(const char**)a; @@ -53,39 +98,23 @@ static int envsort(const void *a, const void *b) } =20 int main(int argc, char **argv) { + struct Arguments *args =3D parseArguments(argc, argv); size_t i, n; int open_max; char **newenv =3D NULL; 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]; char *buffers[3] =3D {NULL, NULL, NULL}; size_t buflen[3] =3D {0, 0, 0}; - char c; - bool daemonize_check =3D false; - bool close_stdin =3D false; size_t daemonize_retries =3D 3; char buf[1024]; ssize_t got; =20 - if (!log) + if (!log || !args) goto cleanup; =20 - for (i =3D 1; i < argc; i++) { - if (STREQ(argv[i - 1], "--readfd") && - sscanf(argv[i], "%u%c", &readfds[numreadfds++], &c) !=3D 1) { - printf("Could not parse fd %s\n", argv[i]); - goto cleanup; - } else if (STREQ(argv[i], "--check-daemonize")) { - daemonize_check =3D true; - } else if (STREQ(argv[i], "--close-stdin")) { - close_stdin =3D true; - } - } - for (i =3D 1; i < argc; i++) { fprintf(log, "ARG:%s\n", argv[i]); } @@ -126,7 +155,7 @@ int main(int argc, char **argv) { while (true) { bool daemonized =3D getpgrp() !=3D getppid(); =20 - if (daemonize_check && !daemonized && daemonize_retries-- > 0) { + if (args->daemonize_check && !daemonized && daemonize_retries-- > = 0) { usleep(100*1000); continue; } @@ -154,7 +183,7 @@ int main(int argc, char **argv) { =20 fprintf(log, "UMASK:%04o\n", umask(0)); =20 - if (close_stdin) { + if (args->close_stdin) { if (freopen("/dev/null", "r", stdin) !=3D stdin) goto cleanup; usleep(100*1000); @@ -165,8 +194,8 @@ int main(int argc, char **argv) { fprintf(stderr, "BEGIN STDERR\n"); fflush(stderr); =20 - for (i =3D 0; i < numreadfds; i++) { - fds[i].fd =3D readfds[i]; + for (i =3D 0; i < args->numreadfds; i++) { + fds[i].fd =3D args->readfds[i]; fds[i].events =3D POLLIN; fds[i].revents =3D 0; } @@ -174,12 +203,12 @@ int main(int argc, char **argv) { for (;;) { unsigned ctr =3D 0; =20 - if (poll(fds, numreadfds, -1) < 0) { + if (poll(fds, args->numreadfds, -1) < 0) { printf("poll failed: %s\n", strerror(errno)); goto cleanup; } =20 - for (i =3D 0; i < numreadfds; i++) { + for (i =3D 0; i < args->numreadfds; i++) { short revents =3D POLLIN | POLLHUP | POLLERR; =20 # ifdef __APPLE__ @@ -210,7 +239,7 @@ int main(int argc, char **argv) { } } } - for (i =3D 0; i < numreadfds; i++) { + for (i =3D 0; i < args->numreadfds; i++) { if (fds[i].events) { ctr++; break; @@ -220,7 +249,7 @@ int main(int argc, char **argv) { break; } =20 - for (i =3D 0; i < numreadfds; i++) { + for (i =3D 0; i < args->numreadfds; i++) { if (fwrite(buffers[i], 1, buflen[i], stdout) !=3D buflen[i]) goto cleanup; if (fwrite(buffers[i], 1, buflen[i], stderr) !=3D buflen[i]) @@ -237,6 +266,8 @@ int main(int argc, char **argv) { cleanup: for (i =3D 0; i < G_N_ELEMENTS(buffers); i++) free(buffers[i]); + if (args) + free(args); if (newenv) free(newenv); if (log) --=20 2.26.2