[libvirt] [PATCH v2 02/10] Add qemuMonitorJSONGetObjectProperty() method for QMP qom-get command

John Ferlan jferlan at redhat.com
Thu Jul 11 15:35:28 UTC 2013


On 07/11/2013 10:05 AM, Michal Privoznik wrote:
> On 08.07.2013 21:20, John Ferlan wrote:
>> Add a new qemuMonitorJSONGetObjectProperty() method to support invocation
>> of the 'qom-get' JSON monitor command with a provided path, property, and
>> expected data type return. The qemuMonitorJSONObjectProperty is similar to
>> virTypedParameter; however, a future patch will extend it a bit to include
>> a void pointer to balloon driver statistic data.
>>
>> The provided test will execute a qom-get on "/machine/i440fx" which will
>> return a property "realized".
>> ---
>>  src/qemu/qemu_monitor_json.c | 86 ++++++++++++++++++++++++++++++++++++++++++++
>>  src/qemu/qemu_monitor_json.h | 34 ++++++++++++++++++
>>  tests/qemumonitorjsontest.c  | 48 +++++++++++++++++++++++++
>>  3 files changed, 168 insertions(+)
>>
>> diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
>> index fc2b65f..db107f1 100644
>> --- a/src/qemu/qemu_monitor_json.c
>> +++ b/src/qemu/qemu_monitor_json.c
>> @@ -4642,6 +4642,92 @@ void qemuMonitorJSONListPathFree(qemuMonitorJSONListPathPtr paths)
>>      VIR_FREE(paths);
>>  }
>>  
>> +
>> +int qemuMonitorJSONGetObjectProperty(qemuMonitorPtr mon,
>> +                                     const char *path,
>> +                                     const char *property,
>> +                                     qemuMonitorJSONObjectPropertyPtr prop)
>> +{
>> +    int ret;
>> +    virJSONValuePtr cmd;
>> +    virJSONValuePtr reply = NULL;
>> +    virJSONValuePtr data;
>> +    const char *tmp;
>> +
>> +    if (!(cmd = qemuMonitorJSONMakeCommand("qom-get",
>> +                                           "s:path", path,
>> +                                           "s:property", property,
>> +                                           NULL)))
>> +        return -1;
>> +
>> +    ret = qemuMonitorJSONCommand(mon, cmd, &reply);
>> +
>> +    if (ret == 0)
>> +        ret = qemuMonitorJSONCheckError(cmd, reply);
>> +
>> +    if (ret < 0)
>> +        goto cleanup;
>> +
>> +    ret = -1;
>> +
>> +    if (!(data = virJSONValueObjectGet(reply, "return"))) {
>> +        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
>> +                       _("qom-get reply was missing return data"));
>> +        goto cleanup;
>> +    }
>> +
>> +    switch (prop->type) {
> 
> switch ((qemuMonitorJSONObjectPropertyType) prop->type) {
> 
>> +    /* Simple cases of boolean, int, long, uint, ulong, double, and string
>> +     * will receive return value as part of {"return": xxx} statement
>> +     */
>> +    case QEMU_MONITOR_OBJECT_PROPERTY_BOOLEAN:
>> +        ret = virJSONValueGetBoolean(data, &prop->val.b);
>> +        break;
>> +    case QEMU_MONITOR_OBJECT_PROPERTY_INT:
>> +        ret = virJSONValueGetNumberInt(data, &prop->val.i);
>> +        break;
>> +    case QEMU_MONITOR_OBJECT_PROPERTY_LONG:
>> +        ret = virJSONValueGetNumberLong(data, &prop->val.l);
>> +        break;
>> +    case QEMU_MONITOR_OBJECT_PROPERTY_UINT:
>> +        ret = virJSONValueGetNumberUint(data, &prop->val.ui);
>> +        break;
>> +    case QEMU_MONITOR_OBJECT_PROPERTY_ULONG:
>> +        ret = virJSONValueGetNumberUlong(data, &prop->val.ul);
>> +        break;
>> +    case QEMU_MONITOR_OBJECT_PROPERTY_DOUBLE:
>> +        ret = virJSONValueGetNumberDouble(data, &prop->val.d);
>> +        break;
>> +    case QEMU_MONITOR_OBJECT_PROPERTY_STRING:
>> +        tmp = virJSONValueGetString(data);
>> +        if (tmp && VIR_STRDUP(prop->val.str, tmp) < 0)
>> +            goto cleanup;
>> +        if (tmp)
>> +            ret = 0;
>> +        break;
>> +    case QEMU_MONITOR_OBJECT_PROPERTY_LAST:
>> +    default:
> 
> Drop 'default' as it prevents compiler to check completeness coverage of enum in this switch(). The _LAST can be dropped as well among with virReportError() and subsequent cleanup then.
> 

Dropping *_LAST and error report/cleanup caused build failure:

  CC       libvirt_driver_qemu_impl_la-qemu_monitor_json.lo
qemu/qemu_monitor_json.c: In function 'qemuMonitorJSONGetObjectProperty':
qemu/qemu_monitor_json.c:4642:5: error: enumeration value
'QEMU_MONITOR_OBJECT_PROPERTY_LAST' not handled in switch [-Werror=switch]
qemu/qemu_monitor_json.c: At top level:
cc1: error: unrecognized command line option
"-Wno-unused-command-line-argument" [-Werror]
cc1: all warnings being treated as errors


>> +        virReportError(VIR_ERR_INTERNAL_ERROR,
>> +                       _("qom-get invalid object property type %d"),
>> +                       prop->type);
>> +        goto cleanup;
>> +    }
>> +
>> +    if (ret == -1) {
>> +        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
>> +                       _("qom-get reply was missing return data"));
>> +        goto cleanup;
>> +    }
>> +
>> +    ret = 0;
>> +cleanup:
>> +    virJSONValueFree(cmd);
>> +    virJSONValueFree(reply);
>> +
>> +    return ret;
>> +}
>> +
>> +
>>  int qemuMonitorJSONGetObjectProps(qemuMonitorPtr mon,
>>                                    const char *type,
>>                                    char ***props)
>> diff --git a/src/qemu/qemu_monitor_json.h b/src/qemu/qemu_monitor_json.h
>> index e7ce145..e6bbd62 100644
>> --- a/src/qemu/qemu_monitor_json.h
>> +++ b/src/qemu/qemu_monitor_json.h
>> @@ -348,6 +348,40 @@ int qemuMonitorJSONGetObjectListPaths(qemuMonitorPtr mon,
>>  
>>  void qemuMonitorJSONListPathFree(qemuMonitorJSONListPathPtr paths);
>>  
>> +/* Flags for the 'type' field in _qemuMonitorJSONObjectProperty */
>> +typedef enum {
>> +    QEMU_MONITOR_OBJECT_PROPERTY_BOOLEAN=1,
>> +    QEMU_MONITOR_OBJECT_PROPERTY_INT,
>> +    QEMU_MONITOR_OBJECT_PROPERTY_LONG,
>> +    QEMU_MONITOR_OBJECT_PROPERTY_UINT,
>> +    QEMU_MONITOR_OBJECT_PROPERTY_ULONG,
>> +    QEMU_MONITOR_OBJECT_PROPERTY_DOUBLE,
>> +    QEMU_MONITOR_OBJECT_PROPERTY_STRING,
>> +
>> +    QEMU_MONITOR_OBJECT_PROPERTY_LAST
>> +} qemuMonitorJSONObjectPropertyType;
>> +
>> +typedef struct _qemuMonitorJSONObjectProperty qemuMonitorJSONObjectProperty;
>> +typedef qemuMonitorJSONObjectProperty *qemuMonitorJSONObjectPropertyPtr;
>> +struct _qemuMonitorJSONObjectProperty {
>> +    int type;    /* qemuMonitorJSONObjectPropertyType */
>> +    union {
>> +        bool b;
>> +        int i;
> 
> Huh, syntax-check fails at this ^^^ line thinking @i is a loop variable. I guess you'll need to add an exception to cfg.mk:
> 

Yep, saw that - made the update today to follow another example to
change "int i;" to "int iv;" rather than messing with the rule.  Other
places with use "val.i" were changed to "val.iv".

See "_virLockManagerParam" in src/locking/lock_driver.h


> diff --git a/cfg.mk b/cfg.mk
> index c6a097e..2936280 100644
> --- a/cfg.mk
> +++ b/cfg.mk
> @@ -985,4 +985,4 @@ exclude_file_name_regexp--sc_prohibit_include_public_headers_brackets = \
>    ^(python/|tools/|examples/|include/libvirt/(virterror|libvirt-(qemu|lxc))\.h$$)
>  
>  exclude_file_name_regexp--sc_prohibit_int_ijk = \
> -  ^(src/remote_protocol-structs|src/remote/remote_protocol.x|cfg.mk|include/)$
> +  ^(src/remote_protocol-structs|src/remote/remote_protocol.x|cfg.mk|include/|src/qemu/qemu_monitor_json.h)$
> 
> 
> 
>> +        long long l;
>> +        unsigned int ui;
>> +        unsigned long long ul;
>> +        double d;
>> +        char *str;
>> +    } val;
>> +};
>> +
>> +int qemuMonitorJSONGetObjectProperty(qemuMonitorPtr mon,
>> +                                     const char *path,
>> +                                     const char *property,
>> +                                     qemuMonitorJSONObjectPropertyPtr prop)
>> +    ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4);
>> +
>>  int qemuMonitorJSONGetObjectProps(qemuMonitorPtr mon,
>>                                    const char *type,
>>                                    char ***props)
> 
> 
> Michal
> 




More information about the libvir-list mailing list