From nobody Tue Feb 10 15:01:18 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.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 216.205.24.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=1611937023; cv=none; d=zohomail.com; s=zohoarc; b=LWuhmHHY3dMWpunrBLPMdx2Js5czTdJ0QP1SPAsxQ0YsfPiSJTOwZY2Infobb62blpO/b+k7QjgWuoXkNxLufTyn3mGgtyas5rmKdRSbF0G8n92Ft/9e4AE9DjboSlwNVFmSMOM0NBKinOuYuwMNiqSgsw1brb1cPIWfqX2Jj/A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1611937023; 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=rQfq8n0sMRPFvtx0r5qujm7mIyKwqIqxIYR7WeTd40A=; b=VHK/AuN8lamMfZY15CYURBMUY9eom0iqndeh3Nztf6TcETKXHkgJ9LBFb8TTgbj/2tvUdEWg0lprbh65SgKxvdTWfdIl+Ozg3WpLg5kmpOnJzyy0MBoSPM8kQh/VoQH961ZGSuvmJSrHqwZ9/+OcyLfw896JE0QCR3L0JNtofMU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.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 [216.205.24.124]) by mx.zohomail.com with SMTPS id 1611937023661250.5312806653634; Fri, 29 Jan 2021 08:17:03 -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-569-ExOgmZ4zPBGhUwRjCegOzg-1; Fri, 29 Jan 2021 11:16:59 -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 C2F43BBEF2; Fri, 29 Jan 2021 16:16:47 +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 9E2BE5D72E; Fri, 29 Jan 2021 16:16:47 +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 68FAA1809CA1; Fri, 29 Jan 2021 16:16:47 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 10TGGik5010294 for ; Fri, 29 Jan 2021 11:16:44 -0500 Received: by smtp.corp.redhat.com (Postfix) id 546B560CE0; Fri, 29 Jan 2021 16:16:44 +0000 (UTC) Received: from work.redhat.com (ovpn-113-143.ams2.redhat.com [10.36.113.143]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9A1D060871; Fri, 29 Jan 2021 16:16:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1611937022; 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=rQfq8n0sMRPFvtx0r5qujm7mIyKwqIqxIYR7WeTd40A=; b=N9I2T0Rt1xe4h3GTTgro8Jlox0jqYVuyixZnaaHROsViP7k8FeT4Z/XbMAfqSaTZUzQi2a chfIWPfaeB7R/lDhrLmzVzQgwPmZlu5vyAUIZuXioJnKPZtojh4t1FmCuhaaqr73tLeL5e Tf3UyqIwnl4B2HbAIC+QzEeQSe5qsVs= X-MC-Unique: ExOgmZ4zPBGhUwRjCegOzg-1 From: Tim Wiederhake To: libvir-list@redhat.com Subject: [libvirt PATCH 07/19] commandhelper: Factor out parseArguments Date: Fri, 29 Jan 2021 17:16:17 +0100 Message-Id: <20210129161629.604823-8-twiederh@redhat.com> In-Reply-To: <20210129161629.604823-1-twiederh@redhat.com> References: <20210129161629.604823-1-twiederh@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Cc: 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 --- tests/commandhelper.c | 83 +++++++++++++++++++++++++++++-------------- 1 file changed, 57 insertions(+), 26 deletions(-) diff --git a/tests/commandhelper.c b/tests/commandhelper.c index a77bee931f..c44322502f 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 *const*astrptr =3D a; @@ -43,39 +88,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]); } @@ -116,7 +145,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; } @@ -144,7 +173,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); @@ -155,8 +184,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; } @@ -164,12 +193,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__ @@ -200,7 +229,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; @@ -210,7 +239,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]) @@ -227,6 +256,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