Skip to contentSkip to conversation

storage-daemon: include current command line option in the errors

PATCH Superseded Paolo Bonzini 3 participants 4 messages Feb 26, 2021 – Mar 1, 2021

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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..'
---
 storage-daemon/qemu-storage-daemon.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
storage-daemon/qemu-storage-daemon.c +16 -1
index 9021a46b3a..a8f8d83f6f 100644
@@ -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);
@@ -283,6 +297,7 @@ static void process_options(int argc, char *argv[])
         error_report("Unexpected argument: %s", argv[optind]);
         exit(EXIT_FAILURE);
     }
+    loc_set_none();
 }
 
 int main(int argc, char *argv[])
Signature · 1 line
2.26.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 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..'

After:

Quoted 8 lines
  $ 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(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#2 lore raw
In reply to Paolo Bonzini

Am 26.02.2021 um 12:03 hat Paolo Bonzini geschrieben:

Quoted 26 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:

  $ 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 +0 -0
index 9021a46b3a..a8f8d83f6f 100644
@@ -152,6 +152,20 @@ static void init_qmp_commands(void)
                          qmp_marshal_qmp_capabilities, QCO_ALLOW_PRECONFIG);
 }
 

storage-daemon/qemu-storage-daemon.c static void init_qmp_commands(void) · line 164

Quoted 10 lines
storage-daemon/qemu-storage-daemon.c +10 -0
+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));

This save_index approach isn't perfect because getopt may skip non-option arguments and they will be included in the help text:

    $ build/storage-daemon/qemu-storage-daemon foo --object iothread
    qemu-storage-daemon: foo --object iothread: Parameter 'id' is missing
    $ build/storage-daemon/qemu-storage-daemon foo --object iothread,id=t
    qemu-storage-daemon: --object iothread,id=t foo: Unexpected argument: foo

However, without changing the interface of loc_set_cmdline(), getting the right index seems hard. Not sure what is the best way for fixing this or if it's worth the effort.

Kevin

Quoted 27 lines
storage-daemon/qemu-storage-daemon.c +6 -1
+    }
+    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);
@@ -283,6 +297,7 @@ static void process_options(int argc, char *argv[])
         error_report("Unexpected argument: %s", argv[optind]);
         exit(EXIT_FAILURE);
     }
+    loc_set_none();
 }
 
 int main(int argc, char *argv[])
Signature · 1 line
2.26.2
#3 lore raw
Quoted 6 lines · Kevin Wolf

This save_index approach isn't perfect because getopt may skip non-option arguments and they will be included in the help text:

     $ build/storage-daemon/qemu-storage-daemon foo --object iothread
     qemu-storage-daemon: foo --object iothread: Parameter 'id' is missing
Quoted 6 lines
     $ build/storage-daemon/qemu-storage-daemon foo --object iothread,id=t
     qemu-storage-daemon: --object iothread,id=t foo: Unexpected argument: foo

However, without changing the interface of loc_set_cmdline(), getting the right index seems hard. Not sure what is the best way for fixing this or if it's worth the effort.

We can do better by passing "-hT:V" to getopt_long. Then each non-option argument is returned directly, and everything works as getopt_long no longer needs to reorder argv.

Paolo