Re: [PATCH v2 27/31] qom: Add user_creatable_add_from_str()

2021-03-02 Thread Kevin Wolf
Am 26.02.2021 um 23:21 hat Eric Blake geschrieben:
> On 2/24/21 7:52 AM, Kevin Wolf wrote:
> > This is a version of user_creatable_process_cmdline() with an Error
> > parameter that never calls exit() and is therefore usable in HMP.
> > 
> > Signed-off-by: Kevin Wolf 
> > ---
> >  include/qom/object_interfaces.h | 16 
> >  qom/object_interfaces.c | 29 -
> >  2 files changed, 40 insertions(+), 5 deletions(-)
> > 
> 
> > +/**
> > + * user_creatable_add_from_str:
> > + * @optarg: the object definition string as passed on the command line
> > + * @errp: if an error occurs, a pointer to an area to store the error
> > + *
> > + * Create an instance of the user creatable object by parsing optarg
> > + * with a keyval parser and implicit key 'qom-type', converting the
> > + * result to ObjectOptions and calling into qmp_object_add().
> > + *
> > + * If a help option is given, print help instead.
> > + *
> > + * Returns: true when an object was successfully created, false when an 
> > error
> > + * occurred (*errp is set then) or help was printed (*errp is not set).
> > + */
> > +bool user_creatable_add_from_str(const char *optarg, Error **errp);
> 
> This could be used to fix the exit status 2 issue in qemu-img convert,
> if you rearrange the series a bit.

Thanks for the suggestion, with this reordering the fix became quite
simple.

Kevin




Re: [PATCH v2 27/31] qom: Add user_creatable_add_from_str()

2021-02-26 Thread Eric Blake
On 2/24/21 7:52 AM, Kevin Wolf wrote:
> This is a version of user_creatable_process_cmdline() with an Error
> parameter that never calls exit() and is therefore usable in HMP.
> 
> Signed-off-by: Kevin Wolf 
> ---
>  include/qom/object_interfaces.h | 16 
>  qom/object_interfaces.c | 29 -
>  2 files changed, 40 insertions(+), 5 deletions(-)
> 

> +/**
> + * user_creatable_add_from_str:
> + * @optarg: the object definition string as passed on the command line
> + * @errp: if an error occurs, a pointer to an area to store the error
> + *
> + * Create an instance of the user creatable object by parsing optarg
> + * with a keyval parser and implicit key 'qom-type', converting the
> + * result to ObjectOptions and calling into qmp_object_add().
> + *
> + * If a help option is given, print help instead.
> + *
> + * Returns: true when an object was successfully created, false when an error
> + * occurred (*errp is set then) or help was printed (*errp is not set).
> + */
> +bool user_creatable_add_from_str(const char *optarg, Error **errp);

This could be used to fix the exit status 2 issue in qemu-img convert,
if you rearrange the series a bit.

Reviewed-by: Eric Blake 

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.   +1-919-301-3226
Virtualization:  qemu.org | libvirt.org




[PATCH v2 27/31] qom: Add user_creatable_add_from_str()

2021-02-24 Thread Kevin Wolf
This is a version of user_creatable_process_cmdline() with an Error
parameter that never calls exit() and is therefore usable in HMP.

Signed-off-by: Kevin Wolf 
---
 include/qom/object_interfaces.h | 16 
 qom/object_interfaces.c | 29 -
 2 files changed, 40 insertions(+), 5 deletions(-)

diff --git a/include/qom/object_interfaces.h b/include/qom/object_interfaces.h
index 1e6c51b541..07511e6cff 100644
--- a/include/qom/object_interfaces.h
+++ b/include/qom/object_interfaces.h
@@ -144,6 +144,22 @@ typedef bool (*user_creatable_add_opts_predicate)(const 
char *type);
 int user_creatable_add_opts_foreach(void *opaque,
 QemuOpts *opts, Error **errp);
 
+/**
+ * user_creatable_add_from_str:
+ * @optarg: the object definition string as passed on the command line
+ * @errp: if an error occurs, a pointer to an area to store the error
+ *
+ * Create an instance of the user creatable object by parsing optarg
+ * with a keyval parser and implicit key 'qom-type', converting the
+ * result to ObjectOptions and calling into qmp_object_add().
+ *
+ * If a help option is given, print help instead.
+ *
+ * Returns: true when an object was successfully created, false when an error
+ * occurred (*errp is set then) or help was printed (*errp is not set).
+ */
+bool user_creatable_add_from_str(const char *optarg, Error **errp);
+
 /**
  * user_creatable_process_cmdline:
  * @optarg: the object definition string as passed on the command line
diff --git a/qom/object_interfaces.c b/qom/object_interfaces.c
index efb48249d5..54f0dadfea 100644
--- a/qom/object_interfaces.c
+++ b/qom/object_interfaces.c
@@ -282,26 +282,45 @@ static void user_creatable_print_help_from_qdict(QDict 
*args)
 }
 }
 
-void user_creatable_process_cmdline(const char *optarg)
+bool user_creatable_add_from_str(const char *optarg, Error **errp)
 {
+ERRP_GUARD();
 QDict *args;
 bool help;
 Visitor *v;
 ObjectOptions *options;
 
-args = keyval_parse(optarg, "qom-type", &help, &error_fatal);
+args = keyval_parse(optarg, "qom-type", &help, errp);
+if (*errp) {
+return false;
+}
 if (help) {
 user_creatable_print_help_from_qdict(args);
-exit(EXIT_SUCCESS);
+qobject_unref(args);
+return false;
 }
 
 v = qobject_input_visitor_new_keyval(QOBJECT(args));
-visit_type_ObjectOptions(v, NULL, &options, &error_fatal);
+visit_type_ObjectOptions(v, NULL, &options, errp);
 visit_free(v);
 qobject_unref(args);
 
-user_creatable_add_qapi(options, &error_fatal);
+if (*errp) {
+goto out;
+}
+
+user_creatable_add_qapi(options, errp);
+out:
 qapi_free_ObjectOptions(options);
+return !*errp;
+}
+
+void user_creatable_process_cmdline(const char *optarg)
+{
+if (!user_creatable_add_from_str(optarg, &error_fatal)) {
+/* Help was printed */
+exit(EXIT_SUCCESS);
+}
 }
 
 bool user_creatable_del(const char *id, Error **errp)
-- 
2.29.2