aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Wolf <kwolf@redhat.com>2021-02-17 12:06:20 +0100
committerKevin Wolf <kwolf@redhat.com>2021-03-19 10:17:14 +0100
commitf375026606f4ae1486189cb758cd0dfa60b3c18f (patch)
tree8d37e6cbc10043a5a252c3cbf7257f18e8bcb680
parentc9231123907415d7737263b9ca6f125a8181463b (diff)
qom: Factor out user_creatable_process_cmdline()
The implementation for --object can be shared between qemu-storage-daemon and other binaries, so move it into a function in qom/object_interfaces.c that is accessible from everywhere. This also requires moving the implementation of qmp_object_add() into a new user_creatable_add_qapi(), because qom/qom-qmp-cmds.c is not linked for tools. user_creatable_print_help_from_qdict() can become static now. Signed-off-by: Kevin Wolf <kwolf@redhat.com> Acked-by: Paolo Bonzini <pbonzini@redhat.com> Acked-by: Peter Krempa <pkrempa@redhat.com> Reviewed-by: Eric Blake <eblake@redhat.com>
-rw-r--r--include/qom/object_interfaces.h41
-rw-r--r--qom/object_interfaces.c51
-rw-r--r--qom/qom-qmp-cmds.c20
-rw-r--r--storage-daemon/qemu-storage-daemon.c24
4 files changed, 81 insertions, 55 deletions
diff --git a/include/qom/object_interfaces.h b/include/qom/object_interfaces.h
index 5299603f50..1e6c51b541 100644
--- a/include/qom/object_interfaces.h
+++ b/include/qom/object_interfaces.h
@@ -2,6 +2,7 @@
#define OBJECT_INTERFACES_H
#include "qom/object.h"
+#include "qapi/qapi-types-qom.h"
#include "qapi/visitor.h"
#define TYPE_USER_CREATABLE "user-creatable"
@@ -87,6 +88,18 @@ Object *user_creatable_add_type(const char *type, const char *id,
Visitor *v, Error **errp);
/**
+ * user_creatable_add_qapi:
+ * @options: the object definition
+ * @errp: if an error occurs, a pointer to an area to store the error
+ *
+ * Create an instance of the user creatable object according to the
+ * options passed in @opts as described in the QAPI schema documentation.
+ *
+ * Returns: the newly created object or NULL on error
+ */
+void user_creatable_add_qapi(ObjectOptions *options, Error **errp);
+
+/**
* user_creatable_add_opts:
* @opts: the object definition
* @errp: if an error occurs, a pointer to an area to store the error
@@ -132,6 +145,21 @@ int user_creatable_add_opts_foreach(void *opaque,
QemuOpts *opts, Error **errp);
/**
+ * user_creatable_process_cmdline:
+ * @optarg: the object definition string as passed on the command line
+ *
+ * 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 and exit.
+ *
+ * This function is only meant to be called during command line parsing.
+ * It exits the process on failure or after printing help.
+ */
+void user_creatable_process_cmdline(const char *optarg);
+
+/**
* user_creatable_print_help:
* @type: the QOM type to be added
* @opts: options to create
@@ -146,19 +174,6 @@ int user_creatable_add_opts_foreach(void *opaque,
bool user_creatable_print_help(const char *type, QemuOpts *opts);
/**
- * user_creatable_print_help_from_qdict:
- * @args: options to create
- *
- * Prints help considering the other options given in @args (if "qom-type" is
- * given and valid, print properties for the type, otherwise print valid types)
- *
- * In contrast to user_creatable_print_help(), this function can't return that
- * no help was requested. It should only be called if we know that help is
- * requested and it will always print some help.
- */
-void user_creatable_print_help_from_qdict(QDict *args);
-
-/**
* user_creatable_del:
* @id: the unique ID for the object
* @errp: if an error occurs, a pointer to an area to store the error
diff --git a/qom/object_interfaces.c b/qom/object_interfaces.c
index 02c3934329..e10a5cf6f0 100644
--- a/qom/object_interfaces.c
+++ b/qom/object_interfaces.c
@@ -2,10 +2,13 @@
#include "qemu/cutils.h"
#include "qapi/error.h"
+#include "qapi/qapi-commands-qom.h"
+#include "qapi/qapi-visit-qom.h"
#include "qapi/qmp/qdict.h"
#include "qapi/qmp/qerror.h"
#include "qapi/qmp/qjson.h"
#include "qapi/qobject-input-visitor.h"
+#include "qapi/qobject-output-visitor.h"
#include "qom/object_interfaces.h"
#include "qemu/help_option.h"
#include "qemu/id.h"
@@ -113,6 +116,30 @@ out:
return obj;
}
+void user_creatable_add_qapi(ObjectOptions *options, Error **errp)
+{
+ Visitor *v;
+ QObject *qobj;
+ QDict *props;
+ Object *obj;
+
+ v = qobject_output_visitor_new(&qobj);
+ visit_type_ObjectOptions(v, NULL, &options, &error_abort);
+ visit_complete(v, &qobj);
+ visit_free(v);
+
+ props = qobject_to(QDict, qobj);
+ qdict_del(props, "qom-type");
+ qdict_del(props, "id");
+
+ v = qobject_input_visitor_new(QOBJECT(props));
+ obj = user_creatable_add_type(ObjectType_str(options->qom_type),
+ options->id, props, v, errp);
+ object_unref(obj);
+ qobject_unref(qobj);
+ visit_free(v);
+}
+
Object *user_creatable_add_opts(QemuOpts *opts, Error **errp)
{
Visitor *v;
@@ -256,7 +283,7 @@ bool user_creatable_print_help(const char *type, QemuOpts *opts)
return false;
}
-void user_creatable_print_help_from_qdict(QDict *args)
+static void user_creatable_print_help_from_qdict(QDict *args)
{
const char *type = qdict_get_try_str(args, "qom-type");
@@ -265,6 +292,28 @@ void user_creatable_print_help_from_qdict(QDict *args)
}
}
+void user_creatable_process_cmdline(const char *optarg)
+{
+ QDict *args;
+ bool help;
+ Visitor *v;
+ ObjectOptions *options;
+
+ args = keyval_parse(optarg, "qom-type", &help, &error_fatal);
+ if (help) {
+ user_creatable_print_help_from_qdict(args);
+ exit(EXIT_SUCCESS);
+ }
+
+ v = qobject_input_visitor_new_keyval(QOBJECT(args));
+ visit_type_ObjectOptions(v, NULL, &options, &error_fatal);
+ visit_free(v);
+ qobject_unref(args);
+
+ user_creatable_add_qapi(options, &error_fatal);
+ qapi_free_ObjectOptions(options);
+}
+
bool user_creatable_del(const char *id, Error **errp)
{
QemuOptsList *opts_list;
diff --git a/qom/qom-qmp-cmds.c b/qom/qom-qmp-cmds.c
index e577a96adf..2d6f41ecc7 100644
--- a/qom/qom-qmp-cmds.c
+++ b/qom/qom-qmp-cmds.c
@@ -228,25 +228,7 @@ ObjectPropertyInfoList *qmp_qom_list_properties(const char *typename,
void qmp_object_add(ObjectOptions *options, Error **errp)
{
- Visitor *v;
- QObject *qobj;
- QDict *props;
- Object *obj;
-
- v = qobject_output_visitor_new(&qobj);
- visit_type_ObjectOptions(v, NULL, &options, &error_abort);
- visit_complete(v, &qobj);
- visit_free(v);
-
- props = qobject_to(QDict, qobj);
- qdict_del(props, "qom-type");
- qdict_del(props, "id");
-
- v = qobject_input_visitor_new(QOBJECT(props));
- obj = user_creatable_add_type(ObjectType_str(options->qom_type),
- options->id, props, v, errp);
- object_unref(obj);
- visit_free(v);
+ user_creatable_add_qapi(options, errp);
}
void qmp_object_del(const char *id, Error **errp)
diff --git a/storage-daemon/qemu-storage-daemon.c b/storage-daemon/qemu-storage-daemon.c
index c793c423d5..268078ad2c 100644
--- a/storage-daemon/qemu-storage-daemon.c
+++ b/storage-daemon/qemu-storage-daemon.c
@@ -38,7 +38,6 @@
#include "qapi/qapi-visit-block-core.h"
#include "qapi/qapi-visit-block-export.h"
#include "qapi/qapi-visit-control.h"
-#include "qapi/qapi-visit-qom.h"
#include "qapi/qmp/qdict.h"
#include "qapi/qmp/qstring.h"
#include "qapi/qobject-input-visitor.h"
@@ -271,27 +270,8 @@ static void process_options(int argc, char *argv[])
break;
}
case OPTION_OBJECT:
- {
- QDict *args;
- bool help;
- Visitor *v;
- ObjectOptions *options;
-
- args = keyval_parse(optarg, "qom-type", &help, &error_fatal);
- if (help) {
- user_creatable_print_help_from_qdict(args);
- exit(EXIT_SUCCESS);
- }
-
- v = qobject_input_visitor_new_keyval(QOBJECT(args));
- visit_type_ObjectOptions(v, NULL, &options, &error_fatal);
- visit_free(v);
- qobject_unref(args);
-
- qmp_object_add(options, &error_fatal);
- qapi_free_ObjectOptions(options);
- break;
- }
+ user_creatable_process_cmdline(optarg);
+ break;
case OPTION_PIDFILE:
pid_file = optarg;
break;