: QMP related fixes
cover : QMP related fixes
Should be applied on top of feature negotiation series.
changelog:
----------
v0 -> v1:
- Document qobject_from_jsonf() new semantics
PATCH 1/4 qjson: Improve debugging
Add an assert() to qobject_from_jsonf() to assure that the returned QObject is not NULL. Currently this is duplicated in the callers.
--- qjson.c | 5 +++++ 1 files changed, 5 insertions(+), 0 deletions(-)
qjson.c +5 -0
@@ -53,6 +53,10 @@ QObject *qobject_from_json(const char *string)return qobject_from_jsonv (string,NULL ); } +/* +* IMPORTANT: This function aborts on error, thus it must not +* be used with untrusted arguments. +*/ QObject *qobject_from_jsonf (const char *string, ...) {QObject *obj;
@@ -62,6 +66,7 @@ QObject *qobject_from_jsonf(const char *string, ...) obj =qobject_from_jsonv (string, &ap);va_end (ap); +assert (obj !=NULL );return obj; }
Signature · 1 line
1.6.6
Anthony Liguori
AppliedRe: [Qemu-devel] [PATCH 1/4] qjson: Improve debugging
Quoted 5 lines · Luiz Capitulino
Add an
assert()toqobject_from_jsonf()to assure that the returned QObject is not NULL. Currently this is duplicated in the callers.Signed-off-by Luiz Capitulino<[email protected]>
Applied all. Thanks.
Regards,
Anthony Liguori
Quoted 28 lines
--- qjson.c | 5 +++++ 1 files changed, 5 insertions(+), 0 deletions(-)qjson.c +5
-0@@ -53,6 +53,10 @@ QObject *qobject_from_json(const char *string)return qobject_from_jsonv (string,NULL ); } +/* +* IMPORTANT: This function aborts on error, thus it must not +* be used with untrusted arguments. +*/ QObject *qobject_from_jsonf (const char *string, ...) {QObject *obj;@@ -62,6 +66,7 @@ QObject *qobject_from_jsonf(const char *string, ...) obj =qobject_from_jsonv (string,&ap);va_end (ap); +assert (obj !=NULL );return obj; }
PATCH 2/4 Monitor: remove unneeded checks
In reply to Luiz CapitulinoIt's not needed to check the return of qobject_from_jsonf() anymore, as an assert() has been added there.
--- block.c | 3 --- hw/pci-hotplug.c | 1 - migration.c | 3 --- monitor.c | 5 ----- 4 files changed, 0 insertions(+), 12 deletions(-)
block.c +0 -3
@@ -1259,7 +1259,6 @@ void bdrv_info(Monitor *mon, QObject **ret_data)"'removable': %i, 'locked': %i }" , bs->device_name, type, bs->removable, bs->locked); -assert (bs_obj !=NULL );if (bs->drv) {QObject *obj;
@@ -1270,7 +1269,6 @@ void bdrv_info(Monitor *mon, QObject **ret_data) bs->filename, bs->read_only, bs->drv->format_name, bdrv_is_encrypted(bs)); -assert (obj !=NULL );if (bs->backing_file[0 ] !='\0' ) {QDict *qdict =qobject_to_qdict (obj); qdict_put(qdict, "backing_file",
@@ -1356,7 +1354,6 @@ void bdrv_info_stats(Monitor *mon, QObject **ret_data) bs->device_name, bs->rd_bytes, bs->wr_bytes, bs->rd_ops, bs->wr_ops); -assert (obj !=NULL );qlist_append_obj (devices, obj); }
hw/pci-hotplug.c +0 -1
@@ -285,7 +285,6 @@ void pci_device_hot_add(Monitor *mon, const QDict *qdict, QObject **ret_data)qobject_from_jsonf ("{ 'domain': 0, 'bus': %d, 'slot': %d, " "'function': %d }" ,pci_bus_num (dev->bus),PCI_SLOT (dev->devfn),PCI_FUNC (dev->devfn)); -assert (*ret_data !=NULL ); }else monitor_printf (mon,"failed to add %s\n" , opts); }
migration.c +0 -3
@@ -183,8 +183,6 @@ static void migrate_put_status(QDict *qdict, const char *name, obj =qobject_from_jsonf ("{ 'transferred': %" PRId64", " "'remaining': %" PRId64", " "'total': %" PRId64" }" , trans, rem, total); -assert (obj !=NULL ); -qdict_put_obj (qdict, name, obj); }
@@ -258,7 +256,6 @@ void do_info_migrate(Monitor *mon, QObject **ret_data) *ret_data =qobject_from_jsonf ("{ 'status': 'cancelled' }" );break ; } -assert (*ret_data !=NULL ); } }
monitor.c +0 -5
@@ -351,8 +351,6 @@ static void timestamp_put(QDict *qdict) obj =qobject_from_jsonf ("{ 'seconds': %" PRId64", " "'microseconds': %" PRId64" }" , (int64_t ) tv.tv_sec, (int64_t ) tv.tv_usec); -assert (obj !=NULL ); -qdict_put_obj (qdict,"timestamp" , obj); }
@@ -897,7 +895,6 @@ static void do_info_cpus(Monitor *mon, QObject **ret_data) obj =qobject_from_jsonf ("{ 'CPU': %d, 'current': %i, 'halted': %i }" , env->cpu_index, env == mon->mon_cpu, env->halted); -assert (obj !=NULL ); cpu =qobject_to_qdict (obj);
@@ -4412,8 +4409,6 @@ static void monitor_control_event(void *opaque, int event)json_message_parser_init (&mon->mc->parser, handle_qmp_command); data =get_qmp_greeting (); -assert (data !=NULL ); -monitor_json_emitter (mon, data);qobject_decref (data); }
Signature · 1 line
1.6.6
PATCH 3/4 QError: Don't abort on multiple faults
In reply to Luiz CapitulinoIdeally, Monitor code should report an error only once and return the error information up the call chain.
To assure that this happens as expected and that no error is lost, we have an assert() in qemu_error_internal().
However, we still have not fully converted handlers using monitor_printf() to report errors. As there can be multiple monitor_printf() calls on an error, the assertion is easily triggered when debugging is enabled; and we will get a memory leak if it's not.
The solution to this problem is to allow multiple faults by only reporting the first one, and to release the additional error objects.
A better mechanism to report multiple errors to programmers is underway.
--- monitor.c | 9 +++++++-- 1 files changed, 7 insertions(+), 2 deletions(-)
monitor.c +7 -2
@@ -4625,8 +4625,13 @@ void qemu_error_internal(const char *file, int linenr, const char *func,QDECREF (qerror);break ;case ERR_SINK_MONITOR : -assert (qemu_error_sink->mon->error ==NULL ); - qemu_error_sink->mon->error = qerror; +/* report only the first error */ +if (!qemu_error_sink->mon->error) { + qemu_error_sink->mon->error = qerror; + }else { +/* XXX: warn the programmer */ +QDECREF (qerror); + }break ; } }
Signature · 1 line
1.6.6
PATCH 4/4 QMP: Don't leak on connection close
In reply to Luiz CapitulinoQMP's chardev event callback doesn't call json_message_parser_destroy() on CHR_EVENT_CLOSED. As the call to json_message_parser_init() on CHR_EVENT_OPENED allocates memory, we'are leaking on close.
Fix that by just calling json_message_parser_destroy() on CHR_EVENT_CLOSED.
--- monitor.c | 12 ++++++++---- 1 files changed, 8 insertions(+), 4 deletions(-)
monitor.c +8 -4
@@ -4401,16 +4401,20 @@ static QObject *get_qmp_greeting(void)*/ static void monitor_control_event (void *opaque,int event) { -if (event ==CHR_EVENT_OPENED ) { -QObject *data; -Monitor *mon = opaque; +QObject *data; +Monitor *mon = opaque; +switch (event) { +case CHR_EVENT_OPENED : mon->mc->command_mode =0 ;json_message_parser_init (&mon->mc->parser, handle_qmp_command); - data =get_qmp_greeting ();monitor_json_emitter (mon, data);qobject_decref (data); +break ; +case CHR_EVENT_CLOSED : +json_message_parser_destroy (&mon->mc->parser); +break ; } }
Signature · 1 line
1.6.6
Quoted 8 lines · Luiz Capitulino
Should be applied on top of feature negotiation series.
changelog:
----------v0 -> v1:
- Document
qobject_from_jsonf()new semantics
Looks good.