storage-daemon: include current command line option in the errors
v2 cover storage-daemon: include current command line option in the errors
Use the location management facilities that the emulator uses, so that the current command line option appears in the error message.
Before:
$ storage-daemon/qemu-storage-daemon --nbd key..= qemu-storage-daemon: Invalid parameter 'key..'
After:
$ storage-daemon/qemu-storage-daemon --nbd key..= qemu-storage-daemon: --nbd key..=: Invalid parameter 'key..'
The first patch tweaks the command line parsing so that argv is not reordered by getopt_long and optind is only advanced by one option+argument on every call to getopt_long. This is required by loc_set_cmdline.
Paolo Bonzini (2):
storage-daemon: report unexpected arguments on the fly
storage-daemon: include current command line option in the errors
storage-daemon/qemu-storage-daemon.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-)
Signature · 1 line
2.26.2
PATCH v2 1/2 storage-daemon: report unexpected arguments on the fly
If the first character of optstring is '-', then each nonoption argv element is handled as if it were the argument of an option with character code 1. This removes the reordering of the argv array, and enables usage of loc_set_cmdline to provide better error messages.
--- storage-daemon/qemu-storage-daemon.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
storage-daemon/qemu-storage-daemon.c +4 -5
@@ -174,7 +174,7 @@ static void process_options(int argc, char *argv[])* they are given on the command lines. This means that things must be * defined first before they can be referenced in another option. */ -while ((c =getopt_long (argc, argv,"hT:V" , long_options,NULL )) !=-1 ) { +while ((c =getopt_long (argc, argv,"-hT:V" , long_options,NULL )) !=-1 ) {switch (c) {case '?' :exit (EXIT_FAILURE );
@@ -275,14 +275,13 @@ static void process_options(int argc, char *argv[])qobject_unref (args);break ; } +case 1 : +error_report ("Unexpected argument: %s" , optarg); +exit (EXIT_FAILURE );default :g_assert_not_reached (); } } -if (optind != argc) { -error_report ("Unexpected argument: %s" , argv[optind]); -exit (EXIT_FAILURE ); - } }int main (int argc,char *argv[])
Signature · 1 line
2.26.2
Re: [PATCH v2 1/2] storage-daemon: report unexpected arguments on the fly
Quoted 9 lines · Paolo Bonzini
If the first character of optstring is '-', then each nonoption argv element is handled as if it were the argument of an option with character code 1. This removes the reordering of the argv array, and enables usage of
loc_set_cmdlineto provide better error messages.Signed-off-by Paolo Bonzini <[email protected]>--- storage-daemon/qemu-storage-daemon.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
Nice. The man page for 'getopt_long' is unclear whether setting POSIXLY_CORRECT in the environment would break this (that is, setting POSIXLY_CORRECT has the same effect as a leading '+'; but you can't have both leading '+' and leading '-' and when both are set, it is not clear which one wins). But that's a corner case that I don't think will ever bite us in real life.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org
Re: [PATCH v2 1/2] storage-daemon: report unexpected arguments on the fly
Quoted 6 lines · Eric Blake
Quoted 5 lines · Paolo Bonzini
If the first character of optstring is '-', then each nonoption argv element is handled as if it were the argument of an option with character code 1. This removes the reordering of the argv array, and enables usage of
loc_set_cmdlineto provide better error messages.
Quoted 7 lines
Quoted 4 lines
Signed-off-by Paolo Bonzini <[email protected]>--- storage-daemon/qemu-storage-daemon.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)Nice. The man page for '
getopt_long' is unclear whether settingPOSIXLY_CORRECTin the environment would break this
It doesn't. In fact, with this patch the behavior is the same as for POSIXLY_CORRECT, though for unrelated reasons, and interestingly enough I think the POSIXLY_CORRECT behavior is an improvement for qemu-storage-daemon.
Unpatched:
$ qemu-storage-daemon foo --object iothread qemu-storage-daemon: Parameter 'id' is missing
$ POSIXLY_CORRECT=1 qemu-storage-daemon foo --object iothread qemu-storage-daemon: Unexpected argument: foo
Patched:
$ storage-daemon/qemu-storage-daemon foo --object iothread qemu-storage-daemon: foo: Unexpected argument $ POSIXLY_CORRECT=1 storage-daemon/qemu-storage-daemon foo --object iothread qemu-storage-daemon: foo: Unexpected argument
Paolo
Re: [PATCH v2 1/2] storage-daemon: report unexpected arguments on the fly
In reply to Eric BlakeQuoted 11 lines · Eric Blake
Quoted 9 lines · Paolo Bonzini
If the first character of optstring is '-', then each nonoption argv element is handled as if it were the argument of an option with character code 1. This removes the reordering of the argv array, and enables usage of
loc_set_cmdlineto provide better error messages.Signed-off-by Paolo Bonzini <[email protected]>--- storage-daemon/qemu-storage-daemon.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
Quoted 8 lines
Nice. The man page for '
getopt_long' is unclear whether settingPOSIXLY_CORRECTin the environment would break this (that is, settingPOSIXLY_CORRECThas the same effect as a leading '+'; but you can't have both leading '+' and leading '-' and when both are set, it is not clear which one wins). But that's a corner case that I don't think will ever bite us in real life.Reviewed-by Eric Blake <[email protected]>
I'd consider environment overruling the programmer's express intent a bug.
GLibc's _getopt_initialize():
/* Determine how to handle the ordering of options and nonoptions. */
if (optstring[0] == '-')
{
d->__ordering = RETURN_IN_ORDER;
++optstring;
}
else if (optstring[0] == '+')
{
d->__ordering = REQUIRE_ORDER;
++optstring;
}
else if (posixly_correct || !!getenv ("POSIXLY_CORRECT"))
d->__ordering = REQUIRE_ORDER;
else
d->__ordering = PERMUTE;No surprises here.
PATCH v2 2/2 storage-daemon: include current command line option in the errors
In reply to Paolo BonziniUse the location management facilities that the emulator uses, so that the current command line option appears in the error message.
Before:
$ storage-daemon/qemu-storage-daemon --nbd key..= qemu-storage-daemon: Invalid parameter 'key..'
After:
$ storage-daemon/qemu-storage-daemon --nbd key..= qemu-storage-daemon: --nbd key..=: Invalid parameter 'key..'
--- storage-daemon/qemu-storage-daemon.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)
storage-daemon/qemu-storage-daemon.c +17 -2
@@ -152,6 +152,20 @@ static void init_qmp_commands(void) qmp_marshal_qmp_capabilities, QCO_ALLOW_PRECONFIG); } +static int getopt_set_loc (int argc,char **argv,const char *optstring, +const struct option *longopts) +{ +int c, save_index; + + optarg =NULL ; + save_index = optind; + c =getopt_long (argc, argv, optstring, longopts,NULL ); +if (optarg) { +loc_set_cmdline (argv, save_index,MAX (1 , optind - save_index)); + } +return c; +} +static void process_options (int argc,char *argv[]) {int c;
@@ -174,7 +188,7 @@ static void process_options(int argc, char *argv[])* they are given on the command lines. This means that things must be * defined first before they can be referenced in another option. */ -while ((c =getopt_long (argc, argv,"-hT:V" , long_options,NULL )) !=-1 ) { +while ((c =getopt_set_loc (argc, argv,"-hT:V" , long_options)) !=-1 ) {switch (c) {case '?' :exit (EXIT_FAILURE );
@@ -275,12 +289,13 @@ static void process_options(int argc, char *argv[])break ; }case 1 : -error_report ("Unexpected argument: %s" , optarg); +error_report ("Unexpected argument" );exit (EXIT_FAILURE );default :g_assert_not_reached (); } } +loc_set_none (); }int main (int argc,char *argv[])
Signature · 1 line
2.26.2
Quoted 8 lines · Paolo Bonzini
Use the location management facilities that the emulator uses, so that the current command line option appears in the error message.
Before:
$ storage-daemon/qemu-storage-daemon --nbd key..= qemu-storage-daemon: Invalid parameter 'key..'
Quoted 7 lines
After:
$ storage-daemon/qemu-storage-daemon --nbd key..= qemu-storage-daemon: --nbd key..=: Invalid parameter 'key..'Reviewed-by Eric Blake <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>
I have a similar patch in an unfinished branch. You win :)
Quoted 16 lines
--- storage-daemon/qemu-storage-daemon.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)storage-daemon/qemu-storage-daemon.c +4
-0@@ -152,6 +152,20 @@ static void init_qmp_commands(void) qmp_marshal_qmp_capabilities, QCO_ALLOW_PRECONFIG); } +static int getopt_set_loc (int argc,char **argv,const char *optstring, +const struct option *longopts) +{ +int c, save_index;
storage-daemon/qemu-storage-daemon.c static void init_qmp_commands(void) · line 168
Quoted 10 lines
storage-daemon/qemu-storage-daemon.c +10
-0+ + optarg =NULL ; + save_index = optind; + c =getopt_long (argc, argv, optstring, longopts,NULL ); +if (optarg) { +loc_set_cmdline (argv, save_index,MAX (1 , optind - save_index)); + } +return c; +} +
I think this function is more widely applicable:
$ git-grep -l getopt_long | xargs grep -l error_report
qemu-img.c
qemu-io.c
qemu-nbd.c
scsi/qemu-pr-helper.c
storage-daemon/qemu-storage-daemon.cQuoted 27 lines
storage-daemon/qemu-storage-daemon.c +3
-2static void process_options (int argc,char *argv[]) {int c;@@ -174,7 +188,7 @@ static void process_options(int argc, char *argv[])* they are given on the command lines. This means that things must be * defined first before they can be referenced in another option. */ -while ((c =getopt_long (argc, argv,"-hT:V" , long_options,NULL )) !=-1 ) { +while ((c =getopt_set_loc (argc, argv,"-hT:V" , long_options)) !=-1 ) {switch (c) {case '?' :exit (EXIT_FAILURE );@@ -275,12 +289,13 @@ static void process_options(int argc, char *argv[])break ; }case 1 : -error_report ("Unexpected argument: %s" , optarg); +error_report ("Unexpected argument" );exit (EXIT_FAILURE );default :g_assert_not_reached (); } } +loc_set_none (); }int main (int argc,char *argv[])
Kevin Wolf
AppliedAm 01.03.2021 um 16:28 hat Paolo Bonzini geschrieben:
Quoted 10 lines
Use the location management facilities that the emulator uses, so that the current command line option appears in the error message.
Before:
$ storage-daemon/qemu-storage-daemon --nbd key..= qemu-storage-daemon: Invalid parameter 'key..'After:
Quoted 7 lines
$ storage-daemon/qemu-storage-daemon --nbd key..= qemu-storage-daemon: --nbd key..=: Invalid parameter 'key..'The first patch tweaks the command line parsing so that argv is not reordered by
getopt_longand optind is only advanced by one option+argument on every call togetopt_long. This is required byloc_set_cmdline.
Thanks, very useful to know about the "-" switch in getopts.
Applied to the block branch.
Kevin