char: don't exit on hmp 'chardev-add help'
PATCH 1/1 char: don't exit on hmp 'chardev-add help'
qemu_chr_new_from_opts() is used from both vl.c and hmp,
and it is quite confusing to see qemu suddenly exit after receiving a help option in hmp.
Create a separate function for printing help and let different callers react accordingly.
--- include/chardev/char.h | 9 +++++++++ chardev/char.c | 27 +++++++++++++++++++-------- vl.c | 4 ++++ 3 files changed, 32 insertions(+), 8 deletions(-)
include/chardev/char.h +9 -0
@@ -59,6 +59,15 @@ struct Chardev { };/** +* @qemu_chr_help: +* +* Print available chardevice backends if 'asked for help' in @opts +* +* Returns: true if there was a help option +*/ +bool qemu_chr_help (QemuOpts *opts); + +/** * @qemu_chr_new_from_opts: * * Create a new character backend from a QemuOpts list.
chardev/char.c +19 -8
@@ -604,14 +604,9 @@ ChardevBackend *qemu_chr_parse_opts(QemuOpts *opts, Error **errp)return backend; } -Chardev *qemu_chr_new_from_opts (QemuOpts *opts,Error **errp) +bool qemu_chr_help (QemuOpts *opts) { -const ChardevClass *cc; -Chardev *chr =NULL ; -ChardevBackend *backend =NULL ; -const char *name =chardev_alias_translate (qemu_opt_get (opts,"backend" )); -const char *id =qemu_opts_id (opts); -char *bid =NULL ; +const char *name =qemu_opt_get (opts,"backend" );if (name &&is_help_option (name)) {GString *str =g_string_new ("" );
@@ -620,7 +615,23 @@ Chardev *qemu_chr_new_from_opts(QemuOpts *opts, Error **errp)error_report ("Available chardev backend types: %s" , str->str);g_string_free (str, true); -exit (0 ); +return true; + } + +return false; +} + +Chardev *qemu_chr_new_from_opts (QemuOpts *opts,Error **errp) +{ +const ChardevClass *cc; +Chardev *chr =NULL ; +ChardevBackend *backend =NULL ; +const char *name =chardev_alias_translate (qemu_opt_get (opts,"backend" )); +const char *id =qemu_opts_id (opts); +char *bid =NULL ; + +if (qemu_chr_help (opts)) { +return NULL ; }if (id ==NULL ) {
vl.c +4 -0
@@ -2344,6 +2344,10 @@ static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp) {Error *local_err =NULL ; +if (qemu_chr_help (opts)) { +exit (0 ); + } +qemu_chr_new_from_opts (opts, &local_err);if (local_err) {error_report_err (local_err);
Signature · 1 line
2.7.4
Hi
Quoted 9 lines · Anton Nefedov
qemu_chr_new_from_opts()is used from bothvl.cand hmp,
and it is quite confusing to see qemu suddenly exit after receiving a help option in hmp.Create a separate function for printing help and let different callers react accordingly.
Signed-off-by Anton Nefedov <[email protected]>
---
Quoted 80 lines
include/chardev/char.h | 9 +++++++++ chardev/char.c | 27 +++++++++++++++++++-------- vl.c | 4 ++++ 3 files changed, 32 insertions(+), 8 deletions(-)include/chardev/char.h +9
-0@@ -59,6 +59,15 @@ struct Chardev { };/** +* @qemu_chr_help: +* +* Print available chardevice backends if 'asked for help' in @opts +* +* Returns: true if there was a help option +*/ +bool qemu_chr_help (QemuOpts *opts); + +/** * @qemu_chr_new_from_opts: * * Create a new character backend from a QemuOpts list. chardev/char.c +2
-7@@ -604,14 +604,9 @@ ChardevBackend *qemu_chr_parse_opts(QemuOpts *opts, Error **errp)return backend; } -Chardev *qemu_chr_new_from_opts (QemuOpts *opts,Error **errp) +bool qemu_chr_help (QemuOpts *opts) { -const ChardevClass *cc; -Chardev *chr =NULL ; -ChardevBackend *backend =NULL ; -const char *name =chardev_alias_translate (qemu_opt_get (opts, "backend")); -const char *id =qemu_opts_id (opts); -char *bid =NULL ; +const char *name =qemu_opt_get (opts,"backend" );if (name && is_help_option(name)) { GString *str = g_string_new("");chardev/char.c +17
-1@@ -620,7 +615,23 @@ Chardev *qemu_chr_new_from_opts(QemuOpts *opts, Error **errp)error_report ("Available chardev backend types: %s" , str->str);g_string_free (str, true); -exit (0 ); +return true; + } + +return false; +} + +Chardev *qemu_chr_new_from_opts (QemuOpts *opts,Error **errp) +{ +const ChardevClass *cc; +Chardev *chr =NULL ; +ChardevBackend *backend =NULL ; +const char *name =chardev_alias_translate (qemu_opt_get (opts,"backend" )); +const char *id =qemu_opts_id (opts); +char *bid =NULL ; + +if (qemu_chr_help (opts)) { +return NULL ; }if (id == NULL) {vl.c +0
-0
vl.c static int chardev_init_func(void *opaque, QemuOpts · line 2353
Quoted 10 lines
vl.c +4
-0@@ -2344,6 +2344,10 @@ static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp) {Error *local_err =NULL ; +if (qemu_chr_help (opts)) { +exit (0 ); + } +qemu_chr_new_from_opts (opts, &local_err);
It is a bit intriguing to have qemu_chr_help() in two code paths here, but since it should exit at first, it's not a big deal imho.
Quoted 7 lines
if (local_err) { error_report_err(local_err);--
2.7.4--
Marc-André Lureau
Quoted 70 lines · Anton Nefedov
qemu_chr_new_from_opts()is used from bothvl.cand hmp,
and it is quite confusing to see qemu suddenly exit after receiving a help option in hmp.Create a separate function for printing help and let different callers react accordingly.
Signed-off-by Anton Nefedov <[email protected]>--- include/chardev/char.h | 9 +++++++++ chardev/char.c | 27 +++++++++++++++++++-------- vl.c | 4 ++++ 3 files changed, 32 insertions(+), 8 deletions(-)include/chardev/char.h +9
-0@@ -59,6 +59,15 @@ struct Chardev { };/** +* @qemu_chr_help: +* +* Print available chardevice backends if 'asked for help' in @opts +* +* Returns: true if there was a help option +*/ +bool qemu_chr_help (QemuOpts *opts); + +/** * @qemu_chr_new_from_opts: * * Create a new character backend from a QemuOpts list. chardev/char.c +12
-8@@ -604,14 +604,9 @@ ChardevBackend *qemu_chr_parse_opts(QemuOpts *opts, Error **errp)return backend; } -Chardev *qemu_chr_new_from_opts (QemuOpts *opts,Error **errp) +bool qemu_chr_help (QemuOpts *opts) { -const ChardevClass *cc; -Chardev *chr =NULL ; -ChardevBackend *backend =NULL ; -const char *name =chardev_alias_translate (qemu_opt_get (opts,"backend" )); -const char *id =qemu_opts_id (opts); -char *bid =NULL ; +const char *name =qemu_opt_get (opts,"backend" );if (name &&is_help_option (name)) {GString *str =g_string_new ("" );@@ -620,7 +615,23 @@ Chardev *qemu_chr_new_from_opts(QemuOpts *opts, Error **errp)error_report ("Available chardev backend types: %s" , str->str);g_string_free (str, true); -exit (0 ); +return true; + } + +return false; +} + +Chardev *qemu_chr_new_from_opts (QemuOpts *opts,Error **errp) +{ +const ChardevClass *cc; +Chardev *chr =NULL ;
chardev/char.c Chardev *qemu_chr_new_from_opts(QemuOpts *opts, Error **errp) · line 637
Quoted 10 lines
chardev/char.c +7
-0+ChardevBackend *backend =NULL ; +const char *name =chardev_alias_translate (qemu_opt_get (opts,"backend" )); +const char *id =qemu_opts_id (opts); +char *bid =NULL ; + +if (qemu_chr_help (opts)) { +return NULL ; }if (id ==NULL ) {
This is good, if only as a refactoring.
At this point, however, you don't need anymore the qemu_chr_help call below.
Instead, qemu_chr_new_from_opts now has three possible results:
- NULL, no error: help printed
- NULL, error: error
- non-NULL: chardev created
vl.c static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp) · line 2351
Quoted 13 lines
vl.c +4
-0@@ -2344,6 +2344,10 @@ static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp) {Error *local_err =NULL ; +if (qemu_chr_help (opts)) { +exit (0 ); + } +qemu_chr_new_from_opts (opts, &local_err);
So I think you can instead do:
if (!qemu_chr_new_from_opts(opts, &local_err)) {
if (!local_err) {
exit(0);
}
error_report_err(local_err);
exit(1);
}
return 0;or something like that.
Paolo