Michael Roth | ab02ab2 | 2011-07-19 14:50:37 -0500 | [diff] [blame] | 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 | |
Peter Maydell | cbf2115 | 2016-01-29 17:49:57 +0000 | [diff] [blame] | 14 | #include "qemu/osdep.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 15 | #include "qapi/error.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 16 | #include "qapi/qmp/types.h" |
| 17 | #include "qapi/qmp/dispatch.h" |
| 18 | #include "qapi/qmp/json-parser.h" |
Eric Blake | c7eb39c | 2016-06-09 10:48:32 -0600 | [diff] [blame] | 19 | #include "qapi/qmp/qjson.h" |
Luiz Capitulino | 93b91c5 | 2012-08-01 16:30:13 -0300 | [diff] [blame] | 20 | #include "qapi-types.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 21 | #include "qapi/qmp/qerror.h" |
Michael Roth | ab02ab2 | 2011-07-19 14:50:37 -0500 | [diff] [blame] | 22 | |
| 23 | static QDict *qmp_dispatch_check_obj(const QObject *request, Error **errp) |
| 24 | { |
| 25 | const QDictEntry *ent; |
| 26 | const char *arg_name; |
| 27 | const QObject *arg_obj; |
| 28 | bool has_exec_key = false; |
| 29 | QDict *dict = NULL; |
| 30 | |
Markus Armbruster | ca6b6e1 | 2017-02-17 21:38:18 +0100 | [diff] [blame] | 31 | dict = qobject_to_qdict(request); |
| 32 | if (!dict) { |
Markus Armbruster | 10e3783 | 2017-04-27 10:41:23 +0200 | [diff] [blame] | 33 | error_setg(errp, "QMP input must be a JSON object"); |
Michael Roth | ab02ab2 | 2011-07-19 14:50:37 -0500 | [diff] [blame] | 34 | return NULL; |
| 35 | } |
| 36 | |
Michael Roth | ab02ab2 | 2011-07-19 14:50:37 -0500 | [diff] [blame] | 37 | for (ent = qdict_first(dict); ent; |
| 38 | ent = qdict_next(dict, ent)) { |
| 39 | arg_name = qdict_entry_key(ent); |
| 40 | arg_obj = qdict_entry_value(ent); |
| 41 | |
| 42 | if (!strcmp(arg_name, "execute")) { |
| 43 | if (qobject_type(arg_obj) != QTYPE_QSTRING) { |
Markus Armbruster | 10e3783 | 2017-04-27 10:41:23 +0200 | [diff] [blame] | 44 | error_setg(errp, |
| 45 | "QMP input member 'execute' must be a string"); |
Michael Roth | ab02ab2 | 2011-07-19 14:50:37 -0500 | [diff] [blame] | 46 | return NULL; |
| 47 | } |
| 48 | has_exec_key = true; |
Markus Armbruster | 74d8c9d | 2017-03-03 13:32:21 +0100 | [diff] [blame] | 49 | } else if (!strcmp(arg_name, "arguments")) { |
| 50 | if (qobject_type(arg_obj) != QTYPE_QDICT) { |
Markus Armbruster | 10e3783 | 2017-04-27 10:41:23 +0200 | [diff] [blame] | 51 | error_setg(errp, |
| 52 | "QMP input member 'arguments' must be an object"); |
Markus Armbruster | 74d8c9d | 2017-03-03 13:32:21 +0100 | [diff] [blame] | 53 | return NULL; |
| 54 | } |
| 55 | } else { |
Markus Armbruster | 10e3783 | 2017-04-27 10:41:23 +0200 | [diff] [blame] | 56 | error_setg(errp, "QMP input member '%s' is unexpected", |
Markus Armbruster | 99fb0c5 | 2017-03-03 13:32:29 +0100 | [diff] [blame] | 57 | arg_name); |
Michael Roth | ab02ab2 | 2011-07-19 14:50:37 -0500 | [diff] [blame] | 58 | return NULL; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | if (!has_exec_key) { |
Markus Armbruster | 10e3783 | 2017-04-27 10:41:23 +0200 | [diff] [blame] | 63 | error_setg(errp, "QMP input lacks member 'execute'"); |
Michael Roth | ab02ab2 | 2011-07-19 14:50:37 -0500 | [diff] [blame] | 64 | return NULL; |
| 65 | } |
| 66 | |
| 67 | return dict; |
| 68 | } |
| 69 | |
Markus Armbruster | 1527bad | 2017-03-03 13:32:25 +0100 | [diff] [blame] | 70 | static QObject *do_qmp_dispatch(QmpCommandList *cmds, QObject *request, |
| 71 | Error **errp) |
Michael Roth | ab02ab2 | 2011-07-19 14:50:37 -0500 | [diff] [blame] | 72 | { |
Markus Armbruster | ee16ce9 | 2014-05-02 13:26:37 +0200 | [diff] [blame] | 73 | Error *local_err = NULL; |
Michael Roth | ab02ab2 | 2011-07-19 14:50:37 -0500 | [diff] [blame] | 74 | const char *command; |
| 75 | QDict *args, *dict; |
| 76 | QmpCommand *cmd; |
| 77 | QObject *ret = NULL; |
| 78 | |
Michael Roth | ab02ab2 | 2011-07-19 14:50:37 -0500 | [diff] [blame] | 79 | dict = qmp_dispatch_check_obj(request, errp); |
Markus Armbruster | 4af8be1 | 2014-05-02 13:26:35 +0200 | [diff] [blame] | 80 | if (!dict) { |
Michael Roth | ab02ab2 | 2011-07-19 14:50:37 -0500 | [diff] [blame] | 81 | return NULL; |
| 82 | } |
| 83 | |
| 84 | command = qdict_get_str(dict, "execute"); |
Markus Armbruster | 1527bad | 2017-03-03 13:32:25 +0100 | [diff] [blame] | 85 | cmd = qmp_find_command(cmds, command); |
Michael Roth | ab02ab2 | 2011-07-19 14:50:37 -0500 | [diff] [blame] | 86 | if (cmd == NULL) { |
Markus Armbruster | a6c90cb | 2015-01-13 16:16:35 +0100 | [diff] [blame] | 87 | error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND, |
| 88 | "The command %s has not been found", command); |
Michael Roth | ab02ab2 | 2011-07-19 14:50:37 -0500 | [diff] [blame] | 89 | return NULL; |
| 90 | } |
Michael Roth | abd6cf6 | 2011-12-06 22:03:42 -0600 | [diff] [blame] | 91 | if (!cmd->enabled) { |
Cole Robinson | f231b88 | 2014-03-21 19:42:26 -0400 | [diff] [blame] | 92 | error_setg(errp, "The command %s has been disabled for this instance", |
| 93 | command); |
Michael Roth | abd6cf6 | 2011-12-06 22:03:42 -0600 | [diff] [blame] | 94 | return NULL; |
| 95 | } |
Michael Roth | ab02ab2 | 2011-07-19 14:50:37 -0500 | [diff] [blame] | 96 | |
| 97 | if (!qdict_haskey(dict, "arguments")) { |
| 98 | args = qdict_new(); |
| 99 | } else { |
| 100 | args = qdict_get_qdict(dict, "arguments"); |
| 101 | QINCREF(args); |
| 102 | } |
| 103 | |
Eric Blake | 42a502a | 2016-04-28 15:45:11 -0600 | [diff] [blame] | 104 | cmd->fn(args, &ret, &local_err); |
| 105 | if (local_err) { |
| 106 | error_propagate(errp, local_err); |
| 107 | } else if (cmd->options & QCO_NO_SUCCESS_RESP) { |
| 108 | g_assert(!ret); |
| 109 | } else if (!ret) { |
| 110 | ret = QOBJECT(qdict_new()); |
Michael Roth | ab02ab2 | 2011-07-19 14:50:37 -0500 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | QDECREF(args); |
| 114 | |
| 115 | return ret; |
| 116 | } |
| 117 | |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 118 | QObject *qmp_build_error_object(Error *err) |
Luiz Capitulino | 93b91c5 | 2012-08-01 16:30:13 -0300 | [diff] [blame] | 119 | { |
| 120 | return qobject_from_jsonf("{ 'class': %s, 'desc': %s }", |
Eric Blake | f22a28b | 2015-11-18 01:53:00 -0700 | [diff] [blame] | 121 | QapiErrorClass_lookup[error_get_class(err)], |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 122 | error_get_pretty(err)); |
Luiz Capitulino | 93b91c5 | 2012-08-01 16:30:13 -0300 | [diff] [blame] | 123 | } |
| 124 | |
Markus Armbruster | 1527bad | 2017-03-03 13:32:25 +0100 | [diff] [blame] | 125 | QObject *qmp_dispatch(QmpCommandList *cmds, QObject *request) |
Michael Roth | ab02ab2 | 2011-07-19 14:50:37 -0500 | [diff] [blame] | 126 | { |
| 127 | Error *err = NULL; |
| 128 | QObject *ret; |
| 129 | QDict *rsp; |
| 130 | |
Markus Armbruster | 1527bad | 2017-03-03 13:32:25 +0100 | [diff] [blame] | 131 | ret = do_qmp_dispatch(cmds, request, &err); |
Michael Roth | ab02ab2 | 2011-07-19 14:50:37 -0500 | [diff] [blame] | 132 | |
| 133 | rsp = qdict_new(); |
| 134 | if (err) { |
Luiz Capitulino | 93b91c5 | 2012-08-01 16:30:13 -0300 | [diff] [blame] | 135 | qdict_put_obj(rsp, "error", qmp_build_error_object(err)); |
Michael Roth | ab02ab2 | 2011-07-19 14:50:37 -0500 | [diff] [blame] | 136 | error_free(err); |
| 137 | } else if (ret) { |
| 138 | qdict_put_obj(rsp, "return", ret); |
| 139 | } else { |
| 140 | QDECREF(rsp); |
| 141 | return NULL; |
| 142 | } |
| 143 | |
| 144 | return QOBJECT(rsp); |
| 145 | } |