* [PATCH v2] util: Don't stop on unrelated values when looking for --fd in close_open_files()
@ 2024-08-20 22:28 Stefano Brivio
2024-08-21 2:56 ` David Gibson
0 siblings, 1 reply; 2+ messages in thread
From: Stefano Brivio @ 2024-08-20 22:28 UTC (permalink / raw)
To: passt-dev
Seen with krun: we get a file descriptor via --fd, but we close it and
happily use the same number for TCP files.
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.
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.
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.
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.
Link: https://github.com/slp/krun/issues/17#issuecomment-2294943828
Fixes: baccfb95ce0e ("conf: Stop parsing options at first non-option argument")
Fixes: 09603cab28f9 ("passt, util: Close any open file that the parent might have leaked")
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
---
v2: Reset optind to 0 before any further call to getopt_long() in
conf()
conf.c | 6 +++---
util.c | 2 +-
2 files changed, 4 insertions(+), 4 deletions(-)
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 = c->tcp.fwd_out.mode = FWD_UNSET;
c->udp.fwd_in.mode = c->udp.fwd_out.mode = FWD_UNSET;
- optind = 1;
+ optind = 0;
do {
name = getopt_long(argc, argv, optstring, options, NULL);
@@ -1648,7 +1648,7 @@ void conf(struct ctx *c, int argc, char **argv)
* settings)
*/
udp_portmap_clear();
- optind = 1;
+ optind = 0;
do {
name = getopt_long(argc, argv, optstring, options, NULL);
@@ -1720,7 +1720,7 @@ void conf(struct ctx *c, int argc, char **argv)
nl_sock_init(c, true);
/* ...and outbound port options now that namespaces are set up. */
- optind = 1;
+ optind = 0;
do {
name = getopt_long(argc, argv, optstring, options, NULL);
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;
do {
- name = getopt_long(argc, argv, "+:F", optfd, NULL);
+ name = getopt_long(argc, argv, "-:F:", optfd, NULL);
if (name == 'F') {
errno = 0;
--
@@ -710,7 +710,7 @@ void close_open_files(int argc, char **argv)
int name, rc;
do {
- name = getopt_long(argc, argv, "+:F", optfd, NULL);
+ name = getopt_long(argc, argv, "-:F:", optfd, NULL);
if (name == 'F') {
errno = 0;
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v2] util: Don't stop on unrelated values when looking for --fd in close_open_files()
2024-08-20 22:28 [PATCH v2] util: Don't stop on unrelated values when looking for --fd in close_open_files() Stefano Brivio
@ 2024-08-21 2:56 ` David Gibson
0 siblings, 0 replies; 2+ messages in thread
From: David Gibson @ 2024-08-21 2:56 UTC (permalink / raw)
To: Stefano Brivio; +Cc: passt-dev
[-- Attachment #1: Type: text/plain, Size: 3141 bytes --]
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.
>
> 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.
>
> 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.
>
> 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.
>
> 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.
>
> Link: https://github.com/slp/krun/issues/17#issuecomment-2294943828
> Fixes: baccfb95ce0e ("conf: Stop parsing options at first non-option argument")
> Fixes: 09603cab28f9 ("passt, util: Close any open file that the parent might have leaked")
> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
> ---
> v2: Reset optind to 0 before any further call to getopt_long() in
> conf()
>
> conf.c | 6 +++---
> util.c | 2 +-
> 2 files changed, 4 insertions(+), 4 deletions(-)
>
> 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 = c->tcp.fwd_out.mode = FWD_UNSET;
> c->udp.fwd_in.mode = c->udp.fwd_out.mode = FWD_UNSET;
>
> - optind = 1;
> + optind = 0;
> do {
> name = getopt_long(argc, argv, optstring, options, NULL);
>
> @@ -1648,7 +1648,7 @@ void conf(struct ctx *c, int argc, char **argv)
> * settings)
> */
> udp_portmap_clear();
> - optind = 1;
> + optind = 0;
> do {
> name = getopt_long(argc, argv, optstring, options, NULL);
>
> @@ -1720,7 +1720,7 @@ void conf(struct ctx *c, int argc, char **argv)
> nl_sock_init(c, true);
>
> /* ...and outbound port options now that namespaces are set up. */
> - optind = 1;
> + optind = 0;
> do {
> name = getopt_long(argc, argv, optstring, options, NULL);
>
> 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;
>
> do {
> - name = getopt_long(argc, argv, "+:F", optfd, NULL);
> + name = getopt_long(argc, argv, "-:F:", optfd, NULL);
>
> if (name == 'F') {
> errno = 0;
--
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
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-08-21 3:03 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-08-20 22:28 [PATCH v2] util: Don't stop on unrelated values when looking for --fd in close_open_files() Stefano Brivio
2024-08-21 2:56 ` David Gibson
Code repositories for project(s) associated with this public inbox
https://passt.top/passt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).