qemu/qapi/qmp-dispatch.c
<<
>>
Prefs
   1/*
   2 * Core Definitions for QAPI/QMP Dispatch
   3 *
   4 * Copyright IBM, Corp. 2011
   5 *
   6 * Authors:
   7 *  Anthony Liguori   <aliguori@us.ibm.com>
   8 *
   9 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  10 * See the COPYING.LIB file in the top-level directory.
  11 *
  12 */
  13
  14#include "qemu/osdep.h"
  15#include "qapi/error.h"
  16#include "qapi/qmp/dispatch.h"
  17#include "qapi/qmp/json-parser.h"
  18#include "qapi/qmp/qdict.h"
  19#include "qapi/qmp/qjson.h"
  20#include "qapi/qmp/qbool.h"
  21
  22QDict *qmp_dispatch_check_obj(const QObject *request, Error **errp)
  23{
  24    const QDictEntry *ent;
  25    const char *arg_name;
  26    const QObject *arg_obj;
  27    bool has_exec_key = false;
  28    QDict *dict = NULL;
  29
  30    dict = qobject_to(QDict, request);
  31    if (!dict) {
  32        error_setg(errp, "QMP input must be a JSON object");
  33        return NULL;
  34    }
  35
  36    for (ent = qdict_first(dict); ent;
  37         ent = qdict_next(dict, ent)) {
  38        arg_name = qdict_entry_key(ent);
  39        arg_obj = qdict_entry_value(ent);
  40
  41        if (!strcmp(arg_name, "execute")) {
  42            if (qobject_type(arg_obj) != QTYPE_QSTRING) {
  43                error_setg(errp,
  44                           "QMP input member 'execute' must be a string");
  45                return NULL;
  46            }
  47            has_exec_key = true;
  48        } else if (!strcmp(arg_name, "arguments")) {
  49            if (qobject_type(arg_obj) != QTYPE_QDICT) {
  50                error_setg(errp,
  51                           "QMP input member 'arguments' must be an object");
  52                return NULL;
  53            }
  54        } else if (!strcmp(arg_name, "id")) {
  55            continue;
  56        } else if (!strcmp(arg_name, "control")) {
  57            if (qobject_type(arg_obj) != QTYPE_QDICT) {
  58                error_setg(errp,
  59                           "QMP input member 'control' must be a dict");
  60                return NULL;
  61            }
  62        } else {
  63            error_setg(errp, "QMP input member '%s' is unexpected",
  64                       arg_name);
  65            return NULL;
  66        }
  67    }
  68
  69    if (!has_exec_key) {
  70        error_setg(errp, "QMP input lacks member 'execute'");
  71        return NULL;
  72    }
  73
  74    return dict;
  75}
  76
  77static QObject *do_qmp_dispatch(QmpCommandList *cmds, QObject *request,
  78                                Error **errp)
  79{
  80    Error *local_err = NULL;
  81    const char *command;
  82    QDict *args, *dict;
  83    QmpCommand *cmd;
  84    QObject *ret = NULL;
  85
  86    dict = qmp_dispatch_check_obj(request, errp);
  87    if (!dict) {
  88        return NULL;
  89    }
  90
  91    command = qdict_get_str(dict, "execute");
  92    cmd = qmp_find_command(cmds, command);
  93    if (cmd == NULL) {
  94        error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
  95                  "The command %s has not been found", command);
  96        return NULL;
  97    }
  98    if (!cmd->enabled) {
  99        error_setg(errp, "The command %s has been disabled for this instance",
 100                   command);
 101        return NULL;
 102    }
 103
 104    if (!qdict_haskey(dict, "arguments")) {
 105        args = qdict_new();
 106    } else {
 107        args = qdict_get_qdict(dict, "arguments");
 108        QINCREF(args);
 109    }
 110
 111    cmd->fn(args, &ret, &local_err);
 112    if (local_err) {
 113        error_propagate(errp, local_err);
 114    } else if (cmd->options & QCO_NO_SUCCESS_RESP) {
 115        g_assert(!ret);
 116    } else if (!ret) {
 117        ret = QOBJECT(qdict_new());
 118    }
 119
 120    QDECREF(args);
 121
 122    return ret;
 123}
 124
 125QDict *qmp_error_response(Error *err)
 126{
 127    QDict *rsp;
 128
 129    rsp = qdict_from_jsonf_nofail("{ 'error': { 'class': %s, 'desc': %s } }",
 130                                  QapiErrorClass_str(error_get_class(err)),
 131                                  error_get_pretty(err));
 132    error_free(err);
 133    return rsp;
 134}
 135
 136/*
 137 * Detect whether a request should be run out-of-band, by quickly
 138 * peeking at whether we have: { "control": { "run-oob": true } }. By
 139 * default commands are run in-band.
 140 */
 141bool qmp_is_oob(QDict *dict)
 142{
 143    QBool *bool_obj;
 144
 145    dict = qdict_get_qdict(dict, "control");
 146    if (!dict) {
 147        return false;
 148    }
 149
 150    bool_obj = qobject_to(QBool, qdict_get(dict, "run-oob"));
 151    if (!bool_obj) {
 152        return false;
 153    }
 154
 155    return qbool_get_bool(bool_obj);
 156}
 157
 158QObject *qmp_dispatch(QmpCommandList *cmds, QObject *request)
 159{
 160    Error *err = NULL;
 161    QObject *ret;
 162    QDict *rsp;
 163
 164    ret = do_qmp_dispatch(cmds, request, &err);
 165
 166    if (err) {
 167        rsp = qmp_error_response(err);
 168    } else if (ret) {
 169        rsp = qdict_new();
 170        qdict_put_obj(rsp, "return", ret);
 171    } else {
 172        rsp = NULL;
 173    }
 174
 175    return QOBJECT(rsp);
 176}
 177