From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=none (p=none dis=none) header.from=gibson.dropbear.id.au Authentication-Results: passt.top; dkim=fail reason="key not found in DNS" header.d=gibson.dropbear.id.au header.i=@gibson.dropbear.id.au header.a=rsa-sha256 header.s=202312 header.b=MV/LP4MH; dkim-atps=neutral Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 8D3525A004E for ; Wed, 21 Aug 2024 05:03:26 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1724209403; bh=bdEPKrpnjQb14V+ewaZfC5r/Eokn78RMd23oyPD6Avs=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=MV/LP4MHPvqDLGYP3/TzJVNriFqXi5x7d8gnM4Og4rryiAIV8eBuBONfJMWNqtld5 eAFAdCV00yoodolGtnjQvfhTgf8icSaptYyQgpQKKEVd4KrZ5QgrBDNO+OGPhptHaY oO4eZktIPXP2l1hDQTJpXK0apB2b+dHNfkuwQmhB+40b7eI9kVVEvJyTdYR+ljKVw6 zwZCTVv+8MU90UEeP4la029+SsnH+on5o9QibPaLui4zeJRwLHQ3CnB2rrc+tc6qc1 4VxDDp8q8EYxo1YqoDcjz3G59012/R/GiBIWwji0WXbhlJgXPtNatBrtJAUqdU44Le AsyRdqvxqsyMQ== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4WpWP707kyz4x89; Wed, 21 Aug 2024 13:03:23 +1000 (AEST) Date: Wed, 21 Aug 2024 12:56:30 +1000 From: David Gibson To: Stefano Brivio Subject: Re: [PATCH v2] util: Don't stop on unrelated values when looking for --fd in close_open_files() Message-ID: References: <20240820222838.2449361-1-sbrivio@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="gpVxP6x8S/DnPJ+Z" Content-Disposition: inline In-Reply-To: <20240820222838.2449361-1-sbrivio@redhat.com> Message-ID-Hash: QHFMXZLKOBSB736K7BZLZ7CPB62AID6W X-Message-ID-Hash: QHFMXZLKOBSB736K7BZLZ7CPB62AID6W X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: passt-dev@passt.top X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: --gpVxP6x8S/DnPJ+Z Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Aug 21, 2024 at 12:28:38AM +0200, Stefano Brivio wrote: > Seen with krun: we get a file descriptor via --fd, but we close it and > happily use the same number for TCP files. >=20 > The issue is that if we also get other options before --fd, with > arguments, getopt_long() stops parsing them because it sees them as > non-option values. >=20 > Use the - modifier at the beginning of optstring (before :, which is > needed to avoid printing errors) instead of +, which means we'll > continue parsing after finding unrelated option values, but > getopt_long() won't reorder them anyway: they'll be passed with option > value '1', which we can ignore. >=20 > By the way, we also need to add : after F in the optstring, so that > we're able to parse the option when given as short name as well. >=20 > Now that we change the parsing mode between close_open_files() and > conf(), we need to reset optind to 0, not to 1, whenever we call > getopt_long() again in conf(), so that the internal initialisation > of getopt_long() evaluating GNU extensions is re-triggered. >=20 > Link: https://github.com/slp/krun/issues/17#issuecomment-2294943828 > Fixes: baccfb95ce0e ("conf: Stop parsing options at first non-option argu= ment") > Fixes: 09603cab28f9 ("passt, util: Close any open file that the parent mi= ght have leaked") > Signed-off-by: Stefano Brivio Reviewed-by: David Gibson > --- > v2: Reset optind to 0 before any further call to getopt_long() in > conf() >=20 > conf.c | 6 +++--- > util.c | 2 +- > 2 files changed, 4 insertions(+), 4 deletions(-) >=20 > diff --git a/conf.c b/conf.c > index ed097bd..ba82696 100644 > --- a/conf.c > +++ b/conf.c > @@ -1261,7 +1261,7 @@ void conf(struct ctx *c, int argc, char **argv) > c->tcp.fwd_in.mode =3D c->tcp.fwd_out.mode =3D FWD_UNSET; > c->udp.fwd_in.mode =3D c->udp.fwd_out.mode =3D FWD_UNSET; > =20 > - optind =3D 1; > + optind =3D 0; > do { > name =3D getopt_long(argc, argv, optstring, options, NULL); > =20 > @@ -1648,7 +1648,7 @@ void conf(struct ctx *c, int argc, char **argv) > * settings) > */ > udp_portmap_clear(); > - optind =3D 1; > + optind =3D 0; > do { > name =3D getopt_long(argc, argv, optstring, options, NULL); > =20 > @@ -1720,7 +1720,7 @@ void conf(struct ctx *c, int argc, char **argv) > nl_sock_init(c, true); > =20 > /* ...and outbound port options now that namespaces are set up. */ > - optind =3D 1; > + optind =3D 0; > do { > name =3D getopt_long(argc, argv, optstring, options, NULL); > =20 > diff --git a/util.c b/util.c > index 0b41404..3fce3c2 100644 > --- a/util.c > +++ b/util.c > @@ -710,7 +710,7 @@ void close_open_files(int argc, char **argv) > int name, rc; > =20 > do { > - name =3D getopt_long(argc, argv, "+:F", optfd, NULL); > + name =3D getopt_long(argc, argv, "-:F:", optfd, NULL); > =20 > if (name =3D=3D 'F') { > errno =3D 0; --=20 David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson --gpVxP6x8S/DnPJ+Z Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmbFV10ACgkQzQJF27ox 2GdEzw//bIBtQQrb/HVGls9yiFhMbPKBso9i8EYHbPLVA006MJYNUaEfLqPLfrVT R18KcaXe8CxhO4G4wZIqwTXGFhQcIyP9nIj/dPfyrVTlP7bGdAvkN6KUVKRX11e5 GwmGReOMcrqUg/ns+iQ+sSxapZGbnDt1BW1sislzQ+H74qOJanJi5i5Zpswv4sbF IR9BTx1Z01Aq+AXNMH3cUKGrErDormFXeSf0l1XVddOESQ+LgC4ubw3dYK0pBnJ0 QJ1b4b7hhnKPDowvaUDZJqnA6z+rxaPD/zLuTYLHa0Uqi9Gm7RGanznLm7ADT+GW lEch45p8XWtMoEk5JFTYk+PUd0xryNYJG6FoJWXW+RSD4i6RfPYy0+QFw1TYAHWF pvLMyKek1o/BlF5TL5G6Zvi+dty53z4RomDtvMtl3/DUUC2uqt6cgczfGPgHPadY TKP8t3UuJxfjJn1TT9+5wmrl3HMisDewZ83/IM859HxVYE5x/hQE+3WDnss9mez2 t6gQwI9kpSBk6n/ZVhhK9wbEQ3bVmWxmqfMyhRTlsumpM73zjPo8G6XqaQVy5SXc V/6aatJQWzxRRdeupDpxn+ZgJ0HFqJc7ZpoHhgqtfxxyezkA1Sy3ky+pRk50xoLD ssTwFBawqjjNs4ZuI2DwsMXg1dTFR4/BNY6S+RyH34NJm2zfkwI= =r/fq -----END PGP SIGNATURE----- --gpVxP6x8S/DnPJ+Z--