From nobody Fri May 3 14:36:26 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1547041192067549.8109269360668; Wed, 9 Jan 2019 05:39:52 -0800 (PST) Received: from localhost ([127.0.0.1]:41737 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ghE5C-0006ez-0i for importer@patchew.org; Wed, 09 Jan 2019 08:39:46 -0500 Received: from eggs.gnu.org ([209.51.188.92]:50203) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ghDpV-0003PP-5m for qemu-devel@nongnu.org; Wed, 09 Jan 2019 08:23:34 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ghDpQ-0000zd-BJ for qemu-devel@nongnu.org; Wed, 09 Jan 2019 08:23:30 -0500 Received: from mx1.redhat.com ([209.132.183.28]:48118) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ghDp7-0000nJ-JA; Wed, 09 Jan 2019 08:23:10 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B94B0144050; Wed, 9 Jan 2019 13:23:07 +0000 (UTC) Received: from foo.home.annexia.org (ovpn-116-21.ams2.redhat.com [10.36.116.21]) by smtp.corp.redhat.com (Postfix) with ESMTP id 69FA960638; Wed, 9 Jan 2019 13:23:06 +0000 (UTC) From: "Richard W.M. Jones" To: eblake@redhat.com Date: Wed, 9 Jan 2019 13:23:02 +0000 Message-Id: <20190109132302.22634-2-rjones@redhat.com> In-Reply-To: <20190109132302.22634-1-rjones@redhat.com> References: <20190109132302.22634-1-rjones@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Wed, 09 Jan 2019 13:23:07 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3] qemu-io: Add generic function for reinitializing optind. X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, qemu-devel@nongnu.org, qemu-block@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" On FreeBSD 11.2: $ nbdkit memory size=3D1M --run './qemu-io -f raw -c "aio_write 0 512" $n= bd' Parsing error: non-numeric argument, or extraneous/unrecognized suffix --= aio_write After main option parsing, we reinitialize optind so we can parse each command. However reinitializing optind to 0 does not work on FreeBSD. What happens when you do this is optind remains 0 after the option parsing loop, and the result is we try to parse argv[optind] =3D=3D argv[0] =3D=3D "aio_write" as if it was the first parameter. The FreeBSD manual page says: In order to use getopt() to evaluate multiple sets of arguments, or to evaluate a single set of arguments multiple times, the variable optreset must be set to 1 before the second and each additional set of calls to getopt(), and the variable optind must be reinitialized. (From the rest of the man page it is clear that optind must be reinitialized to 1). The glibc man page says: A program that scans multiple argument vectors, or rescans the same vector more than once, and wants to make use of GNU extensions such as '+' and '-' at the start of optstring, or changes the value of POSIXLY_CORRECT between scans, must reinitialize getopt() by resetting optind to 0, rather than the traditional value of 1. (Resetting to 0 forces the invocation of an internal initialization routine that rechecks POSIXLY_CORRECT and checks for GNU extensions in optstring.) This commit introduces an OS-portability function called qemu_reset_optind which provides a way of resetting optind that works on FreeBSD, while keeping it the same on other OSes. Note that the qemu codebase sets optind in many other places, but in those other places it's setting a local variable and not using getopt. This change is only needed in places where we are using getopt and the associated global variable optind. Signed-off-by: Richard W.M. Jones --- include/qemu/osdep.h | 18 ++++++++++++++++++ qemu-img.c | 2 +- qemu-io-cmds.c | 2 +- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h index 3bf48bcdec..ea4ce4db9e 100644 --- a/include/qemu/osdep.h +++ b/include/qemu/osdep.h @@ -92,6 +92,7 @@ extern int daemon(int, int); #include #include #include +#include #include #include #include @@ -587,4 +588,21 @@ extern int qemu_icache_linesize_log; extern int qemu_dcache_linesize; extern int qemu_dcache_linesize_log; =20 +/** + * qemu_reset_optind: + * + * After using getopt or getopt_long, if you need to parse another set + * of options, then you must reset optind. Unfortunately the way to + * do this varies between implementations of getopt. + */ +static inline void qemu_reset_optind(void) +{ +#ifdef __FreeBSD__ + optind =3D 1; + optreset =3D 1; +#else + optind =3D 0; +#endif +} + #endif diff --git a/qemu-img.c b/qemu-img.c index ad04f59565..25288c4d18 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -4962,7 +4962,7 @@ int main(int argc, char **argv) return 0; } argv +=3D optind; - optind =3D 0; + qemu_reset_optind(); =20 if (!trace_init_backends()) { exit(1); diff --git a/qemu-io-cmds.c b/qemu-io-cmds.c index 2c39124036..ee8f56e46a 100644 --- a/qemu-io-cmds.c +++ b/qemu-io-cmds.c @@ -114,7 +114,7 @@ static int command(BlockBackend *blk, const cmdinfo_t *= ct, int argc, } } =20 - optind =3D 0; + qemu_reset_optind(); return ct->cfunc(blk, argc, argv); } =20 --=20 2.20.1