xref: /qemu/monitor/qmp.c (revision 7e3c0deab1b76f37ac0b3199324db976a6cd1b2c)
1*7e3c0deaSKevin Wolf /*
2*7e3c0deaSKevin Wolf  * QEMU monitor
3*7e3c0deaSKevin Wolf  *
4*7e3c0deaSKevin Wolf  * Copyright (c) 2003-2004 Fabrice Bellard
5*7e3c0deaSKevin Wolf  *
6*7e3c0deaSKevin Wolf  * Permission is hereby granted, free of charge, to any person obtaining a copy
7*7e3c0deaSKevin Wolf  * of this software and associated documentation files (the "Software"), to deal
8*7e3c0deaSKevin Wolf  * in the Software without restriction, including without limitation the rights
9*7e3c0deaSKevin Wolf  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10*7e3c0deaSKevin Wolf  * copies of the Software, and to permit persons to whom the Software is
11*7e3c0deaSKevin Wolf  * furnished to do so, subject to the following conditions:
12*7e3c0deaSKevin Wolf  *
13*7e3c0deaSKevin Wolf  * The above copyright notice and this permission notice shall be included in
14*7e3c0deaSKevin Wolf  * all copies or substantial portions of the Software.
15*7e3c0deaSKevin Wolf  *
16*7e3c0deaSKevin Wolf  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*7e3c0deaSKevin Wolf  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*7e3c0deaSKevin Wolf  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19*7e3c0deaSKevin Wolf  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*7e3c0deaSKevin Wolf  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*7e3c0deaSKevin Wolf  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22*7e3c0deaSKevin Wolf  * THE SOFTWARE.
23*7e3c0deaSKevin Wolf  */
24*7e3c0deaSKevin Wolf 
25*7e3c0deaSKevin Wolf #include "qemu/osdep.h"
26*7e3c0deaSKevin Wolf 
27*7e3c0deaSKevin Wolf #include "chardev/char-io.h"
28*7e3c0deaSKevin Wolf #include "monitor-internal.h"
29*7e3c0deaSKevin Wolf #include "qapi/error.h"
30*7e3c0deaSKevin Wolf #include "qapi/qapi-commands-misc.h"
31*7e3c0deaSKevin Wolf #include "qapi/qmp/qdict.h"
32*7e3c0deaSKevin Wolf #include "qapi/qmp/qjson.h"
33*7e3c0deaSKevin Wolf #include "qapi/qmp/qlist.h"
34*7e3c0deaSKevin Wolf #include "qapi/qmp/qstring.h"
35*7e3c0deaSKevin Wolf #include "trace.h"
36*7e3c0deaSKevin Wolf 
37*7e3c0deaSKevin Wolf struct QMPRequest {
38*7e3c0deaSKevin Wolf     /* Owner of the request */
39*7e3c0deaSKevin Wolf     MonitorQMP *mon;
40*7e3c0deaSKevin Wolf     /*
41*7e3c0deaSKevin Wolf      * Request object to be handled or Error to be reported
42*7e3c0deaSKevin Wolf      * (exactly one of them is non-null)
43*7e3c0deaSKevin Wolf      */
44*7e3c0deaSKevin Wolf     QObject *req;
45*7e3c0deaSKevin Wolf     Error *err;
46*7e3c0deaSKevin Wolf };
47*7e3c0deaSKevin Wolf typedef struct QMPRequest QMPRequest;
48*7e3c0deaSKevin Wolf 
49*7e3c0deaSKevin Wolf QmpCommandList qmp_commands, qmp_cap_negotiation_commands;
50*7e3c0deaSKevin Wolf 
51*7e3c0deaSKevin Wolf static bool qmp_oob_enabled(MonitorQMP *mon)
52*7e3c0deaSKevin Wolf {
53*7e3c0deaSKevin Wolf     return mon->capab[QMP_CAPABILITY_OOB];
54*7e3c0deaSKevin Wolf }
55*7e3c0deaSKevin Wolf 
56*7e3c0deaSKevin Wolf static void monitor_qmp_caps_reset(MonitorQMP *mon)
57*7e3c0deaSKevin Wolf {
58*7e3c0deaSKevin Wolf     memset(mon->capab_offered, 0, sizeof(mon->capab_offered));
59*7e3c0deaSKevin Wolf     memset(mon->capab, 0, sizeof(mon->capab));
60*7e3c0deaSKevin Wolf     mon->capab_offered[QMP_CAPABILITY_OOB] = mon->common.use_io_thread;
61*7e3c0deaSKevin Wolf }
62*7e3c0deaSKevin Wolf 
63*7e3c0deaSKevin Wolf static void qmp_request_free(QMPRequest *req)
64*7e3c0deaSKevin Wolf {
65*7e3c0deaSKevin Wolf     qobject_unref(req->req);
66*7e3c0deaSKevin Wolf     error_free(req->err);
67*7e3c0deaSKevin Wolf     g_free(req);
68*7e3c0deaSKevin Wolf }
69*7e3c0deaSKevin Wolf 
70*7e3c0deaSKevin Wolf /* Caller must hold mon->qmp.qmp_queue_lock */
71*7e3c0deaSKevin Wolf static void monitor_qmp_cleanup_req_queue_locked(MonitorQMP *mon)
72*7e3c0deaSKevin Wolf {
73*7e3c0deaSKevin Wolf     while (!g_queue_is_empty(mon->qmp_requests)) {
74*7e3c0deaSKevin Wolf         qmp_request_free(g_queue_pop_head(mon->qmp_requests));
75*7e3c0deaSKevin Wolf     }
76*7e3c0deaSKevin Wolf }
77*7e3c0deaSKevin Wolf 
78*7e3c0deaSKevin Wolf static void monitor_qmp_cleanup_queues(MonitorQMP *mon)
79*7e3c0deaSKevin Wolf {
80*7e3c0deaSKevin Wolf     qemu_mutex_lock(&mon->qmp_queue_lock);
81*7e3c0deaSKevin Wolf     monitor_qmp_cleanup_req_queue_locked(mon);
82*7e3c0deaSKevin Wolf     qemu_mutex_unlock(&mon->qmp_queue_lock);
83*7e3c0deaSKevin Wolf }
84*7e3c0deaSKevin Wolf 
85*7e3c0deaSKevin Wolf void qmp_send_response(MonitorQMP *mon, const QDict *rsp)
86*7e3c0deaSKevin Wolf {
87*7e3c0deaSKevin Wolf     const QObject *data = QOBJECT(rsp);
88*7e3c0deaSKevin Wolf     QString *json;
89*7e3c0deaSKevin Wolf 
90*7e3c0deaSKevin Wolf     json = mon->common.flags & MONITOR_USE_PRETTY ?
91*7e3c0deaSKevin Wolf            qobject_to_json_pretty(data) : qobject_to_json(data);
92*7e3c0deaSKevin Wolf     assert(json != NULL);
93*7e3c0deaSKevin Wolf 
94*7e3c0deaSKevin Wolf     qstring_append_chr(json, '\n');
95*7e3c0deaSKevin Wolf     monitor_puts(&mon->common, qstring_get_str(json));
96*7e3c0deaSKevin Wolf 
97*7e3c0deaSKevin Wolf     qobject_unref(json);
98*7e3c0deaSKevin Wolf }
99*7e3c0deaSKevin Wolf 
100*7e3c0deaSKevin Wolf /*
101*7e3c0deaSKevin Wolf  * Emit QMP response @rsp with ID @id to @mon.
102*7e3c0deaSKevin Wolf  * Null @rsp can only happen for commands with QCO_NO_SUCCESS_RESP.
103*7e3c0deaSKevin Wolf  * Nothing is emitted then.
104*7e3c0deaSKevin Wolf  */
105*7e3c0deaSKevin Wolf static void monitor_qmp_respond(MonitorQMP *mon, QDict *rsp)
106*7e3c0deaSKevin Wolf {
107*7e3c0deaSKevin Wolf     if (rsp) {
108*7e3c0deaSKevin Wolf         qmp_send_response(mon, rsp);
109*7e3c0deaSKevin Wolf     }
110*7e3c0deaSKevin Wolf }
111*7e3c0deaSKevin Wolf 
112*7e3c0deaSKevin Wolf static void monitor_qmp_dispatch(MonitorQMP *mon, QObject *req)
113*7e3c0deaSKevin Wolf {
114*7e3c0deaSKevin Wolf     Monitor *old_mon;
115*7e3c0deaSKevin Wolf     QDict *rsp;
116*7e3c0deaSKevin Wolf     QDict *error;
117*7e3c0deaSKevin Wolf 
118*7e3c0deaSKevin Wolf     old_mon = cur_mon;
119*7e3c0deaSKevin Wolf     cur_mon = &mon->common;
120*7e3c0deaSKevin Wolf 
121*7e3c0deaSKevin Wolf     rsp = qmp_dispatch(mon->commands, req, qmp_oob_enabled(mon));
122*7e3c0deaSKevin Wolf 
123*7e3c0deaSKevin Wolf     cur_mon = old_mon;
124*7e3c0deaSKevin Wolf 
125*7e3c0deaSKevin Wolf     if (mon->commands == &qmp_cap_negotiation_commands) {
126*7e3c0deaSKevin Wolf         error = qdict_get_qdict(rsp, "error");
127*7e3c0deaSKevin Wolf         if (error
128*7e3c0deaSKevin Wolf             && !g_strcmp0(qdict_get_try_str(error, "class"),
129*7e3c0deaSKevin Wolf                     QapiErrorClass_str(ERROR_CLASS_COMMAND_NOT_FOUND))) {
130*7e3c0deaSKevin Wolf             /* Provide a more useful error message */
131*7e3c0deaSKevin Wolf             qdict_del(error, "desc");
132*7e3c0deaSKevin Wolf             qdict_put_str(error, "desc", "Expecting capabilities negotiation"
133*7e3c0deaSKevin Wolf                           " with 'qmp_capabilities'");
134*7e3c0deaSKevin Wolf         }
135*7e3c0deaSKevin Wolf     }
136*7e3c0deaSKevin Wolf 
137*7e3c0deaSKevin Wolf     monitor_qmp_respond(mon, rsp);
138*7e3c0deaSKevin Wolf     qobject_unref(rsp);
139*7e3c0deaSKevin Wolf }
140*7e3c0deaSKevin Wolf 
141*7e3c0deaSKevin Wolf /*
142*7e3c0deaSKevin Wolf  * Pop a QMP request from a monitor request queue.
143*7e3c0deaSKevin Wolf  * Return the request, or NULL all request queues are empty.
144*7e3c0deaSKevin Wolf  * We are using round-robin fashion to pop the request, to avoid
145*7e3c0deaSKevin Wolf  * processing commands only on a very busy monitor.  To achieve that,
146*7e3c0deaSKevin Wolf  * when we process one request on a specific monitor, we put that
147*7e3c0deaSKevin Wolf  * monitor to the end of mon_list queue.
148*7e3c0deaSKevin Wolf  *
149*7e3c0deaSKevin Wolf  * Note: if the function returned with non-NULL, then the caller will
150*7e3c0deaSKevin Wolf  * be with qmp_mon->qmp_queue_lock held, and the caller is responsible
151*7e3c0deaSKevin Wolf  * to release it.
152*7e3c0deaSKevin Wolf  */
153*7e3c0deaSKevin Wolf static QMPRequest *monitor_qmp_requests_pop_any_with_lock(void)
154*7e3c0deaSKevin Wolf {
155*7e3c0deaSKevin Wolf     QMPRequest *req_obj = NULL;
156*7e3c0deaSKevin Wolf     Monitor *mon;
157*7e3c0deaSKevin Wolf     MonitorQMP *qmp_mon;
158*7e3c0deaSKevin Wolf 
159*7e3c0deaSKevin Wolf     qemu_mutex_lock(&monitor_lock);
160*7e3c0deaSKevin Wolf 
161*7e3c0deaSKevin Wolf     QTAILQ_FOREACH(mon, &mon_list, entry) {
162*7e3c0deaSKevin Wolf         if (!monitor_is_qmp(mon)) {
163*7e3c0deaSKevin Wolf             continue;
164*7e3c0deaSKevin Wolf         }
165*7e3c0deaSKevin Wolf 
166*7e3c0deaSKevin Wolf         qmp_mon = container_of(mon, MonitorQMP, common);
167*7e3c0deaSKevin Wolf         qemu_mutex_lock(&qmp_mon->qmp_queue_lock);
168*7e3c0deaSKevin Wolf         req_obj = g_queue_pop_head(qmp_mon->qmp_requests);
169*7e3c0deaSKevin Wolf         if (req_obj) {
170*7e3c0deaSKevin Wolf             /* With the lock of corresponding queue held */
171*7e3c0deaSKevin Wolf             break;
172*7e3c0deaSKevin Wolf         }
173*7e3c0deaSKevin Wolf         qemu_mutex_unlock(&qmp_mon->qmp_queue_lock);
174*7e3c0deaSKevin Wolf     }
175*7e3c0deaSKevin Wolf 
176*7e3c0deaSKevin Wolf     if (req_obj) {
177*7e3c0deaSKevin Wolf         /*
178*7e3c0deaSKevin Wolf          * We found one request on the monitor. Degrade this monitor's
179*7e3c0deaSKevin Wolf          * priority to lowest by re-inserting it to end of queue.
180*7e3c0deaSKevin Wolf          */
181*7e3c0deaSKevin Wolf         QTAILQ_REMOVE(&mon_list, mon, entry);
182*7e3c0deaSKevin Wolf         QTAILQ_INSERT_TAIL(&mon_list, mon, entry);
183*7e3c0deaSKevin Wolf     }
184*7e3c0deaSKevin Wolf 
185*7e3c0deaSKevin Wolf     qemu_mutex_unlock(&monitor_lock);
186*7e3c0deaSKevin Wolf 
187*7e3c0deaSKevin Wolf     return req_obj;
188*7e3c0deaSKevin Wolf }
189*7e3c0deaSKevin Wolf 
190*7e3c0deaSKevin Wolf void monitor_qmp_bh_dispatcher(void *data)
191*7e3c0deaSKevin Wolf {
192*7e3c0deaSKevin Wolf     QMPRequest *req_obj = monitor_qmp_requests_pop_any_with_lock();
193*7e3c0deaSKevin Wolf     QDict *rsp;
194*7e3c0deaSKevin Wolf     bool need_resume;
195*7e3c0deaSKevin Wolf     MonitorQMP *mon;
196*7e3c0deaSKevin Wolf 
197*7e3c0deaSKevin Wolf     if (!req_obj) {
198*7e3c0deaSKevin Wolf         return;
199*7e3c0deaSKevin Wolf     }
200*7e3c0deaSKevin Wolf 
201*7e3c0deaSKevin Wolf     mon = req_obj->mon;
202*7e3c0deaSKevin Wolf     /*  qmp_oob_enabled() might change after "qmp_capabilities" */
203*7e3c0deaSKevin Wolf     need_resume = !qmp_oob_enabled(mon) ||
204*7e3c0deaSKevin Wolf         mon->qmp_requests->length == QMP_REQ_QUEUE_LEN_MAX - 1;
205*7e3c0deaSKevin Wolf     qemu_mutex_unlock(&mon->qmp_queue_lock);
206*7e3c0deaSKevin Wolf     if (req_obj->req) {
207*7e3c0deaSKevin Wolf         QDict *qdict = qobject_to(QDict, req_obj->req);
208*7e3c0deaSKevin Wolf         QObject *id = qdict ? qdict_get(qdict, "id") : NULL;
209*7e3c0deaSKevin Wolf         trace_monitor_qmp_cmd_in_band(qobject_get_try_str(id) ?: "");
210*7e3c0deaSKevin Wolf         monitor_qmp_dispatch(mon, req_obj->req);
211*7e3c0deaSKevin Wolf     } else {
212*7e3c0deaSKevin Wolf         assert(req_obj->err);
213*7e3c0deaSKevin Wolf         rsp = qmp_error_response(req_obj->err);
214*7e3c0deaSKevin Wolf         req_obj->err = NULL;
215*7e3c0deaSKevin Wolf         monitor_qmp_respond(mon, rsp);
216*7e3c0deaSKevin Wolf         qobject_unref(rsp);
217*7e3c0deaSKevin Wolf     }
218*7e3c0deaSKevin Wolf 
219*7e3c0deaSKevin Wolf     if (need_resume) {
220*7e3c0deaSKevin Wolf         /* Pairs with the monitor_suspend() in handle_qmp_command() */
221*7e3c0deaSKevin Wolf         monitor_resume(&mon->common);
222*7e3c0deaSKevin Wolf     }
223*7e3c0deaSKevin Wolf     qmp_request_free(req_obj);
224*7e3c0deaSKevin Wolf 
225*7e3c0deaSKevin Wolf     /* Reschedule instead of looping so the main loop stays responsive */
226*7e3c0deaSKevin Wolf     qemu_bh_schedule(qmp_dispatcher_bh);
227*7e3c0deaSKevin Wolf }
228*7e3c0deaSKevin Wolf 
229*7e3c0deaSKevin Wolf static void handle_qmp_command(void *opaque, QObject *req, Error *err)
230*7e3c0deaSKevin Wolf {
231*7e3c0deaSKevin Wolf     MonitorQMP *mon = opaque;
232*7e3c0deaSKevin Wolf     QObject *id = NULL;
233*7e3c0deaSKevin Wolf     QDict *qdict;
234*7e3c0deaSKevin Wolf     QMPRequest *req_obj;
235*7e3c0deaSKevin Wolf 
236*7e3c0deaSKevin Wolf     assert(!req != !err);
237*7e3c0deaSKevin Wolf 
238*7e3c0deaSKevin Wolf     qdict = qobject_to(QDict, req);
239*7e3c0deaSKevin Wolf     if (qdict) {
240*7e3c0deaSKevin Wolf         id = qdict_get(qdict, "id");
241*7e3c0deaSKevin Wolf     } /* else will fail qmp_dispatch() */
242*7e3c0deaSKevin Wolf 
243*7e3c0deaSKevin Wolf     if (req && trace_event_get_state_backends(TRACE_HANDLE_QMP_COMMAND)) {
244*7e3c0deaSKevin Wolf         QString *req_json = qobject_to_json(req);
245*7e3c0deaSKevin Wolf         trace_handle_qmp_command(mon, qstring_get_str(req_json));
246*7e3c0deaSKevin Wolf         qobject_unref(req_json);
247*7e3c0deaSKevin Wolf     }
248*7e3c0deaSKevin Wolf 
249*7e3c0deaSKevin Wolf     if (qdict && qmp_is_oob(qdict)) {
250*7e3c0deaSKevin Wolf         /* OOB commands are executed immediately */
251*7e3c0deaSKevin Wolf         trace_monitor_qmp_cmd_out_of_band(qobject_get_try_str(id) ?: "");
252*7e3c0deaSKevin Wolf         monitor_qmp_dispatch(mon, req);
253*7e3c0deaSKevin Wolf         qobject_unref(req);
254*7e3c0deaSKevin Wolf         return;
255*7e3c0deaSKevin Wolf     }
256*7e3c0deaSKevin Wolf 
257*7e3c0deaSKevin Wolf     req_obj = g_new0(QMPRequest, 1);
258*7e3c0deaSKevin Wolf     req_obj->mon = mon;
259*7e3c0deaSKevin Wolf     req_obj->req = req;
260*7e3c0deaSKevin Wolf     req_obj->err = err;
261*7e3c0deaSKevin Wolf 
262*7e3c0deaSKevin Wolf     /* Protect qmp_requests and fetching its length. */
263*7e3c0deaSKevin Wolf     qemu_mutex_lock(&mon->qmp_queue_lock);
264*7e3c0deaSKevin Wolf 
265*7e3c0deaSKevin Wolf     /*
266*7e3c0deaSKevin Wolf      * Suspend the monitor when we can't queue more requests after
267*7e3c0deaSKevin Wolf      * this one.  Dequeuing in monitor_qmp_bh_dispatcher() will resume
268*7e3c0deaSKevin Wolf      * it.  Note that when OOB is disabled, we queue at most one
269*7e3c0deaSKevin Wolf      * command, for backward compatibility.
270*7e3c0deaSKevin Wolf      */
271*7e3c0deaSKevin Wolf     if (!qmp_oob_enabled(mon) ||
272*7e3c0deaSKevin Wolf         mon->qmp_requests->length == QMP_REQ_QUEUE_LEN_MAX - 1) {
273*7e3c0deaSKevin Wolf         monitor_suspend(&mon->common);
274*7e3c0deaSKevin Wolf     }
275*7e3c0deaSKevin Wolf 
276*7e3c0deaSKevin Wolf     /*
277*7e3c0deaSKevin Wolf      * Put the request to the end of queue so that requests will be
278*7e3c0deaSKevin Wolf      * handled in time order.  Ownership for req_obj, req,
279*7e3c0deaSKevin Wolf      * etc. will be delivered to the handler side.
280*7e3c0deaSKevin Wolf      */
281*7e3c0deaSKevin Wolf     assert(mon->qmp_requests->length < QMP_REQ_QUEUE_LEN_MAX);
282*7e3c0deaSKevin Wolf     g_queue_push_tail(mon->qmp_requests, req_obj);
283*7e3c0deaSKevin Wolf     qemu_mutex_unlock(&mon->qmp_queue_lock);
284*7e3c0deaSKevin Wolf 
285*7e3c0deaSKevin Wolf     /* Kick the dispatcher routine */
286*7e3c0deaSKevin Wolf     qemu_bh_schedule(qmp_dispatcher_bh);
287*7e3c0deaSKevin Wolf }
288*7e3c0deaSKevin Wolf 
289*7e3c0deaSKevin Wolf static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
290*7e3c0deaSKevin Wolf {
291*7e3c0deaSKevin Wolf     MonitorQMP *mon = opaque;
292*7e3c0deaSKevin Wolf 
293*7e3c0deaSKevin Wolf     json_message_parser_feed(&mon->parser, (const char *) buf, size);
294*7e3c0deaSKevin Wolf }
295*7e3c0deaSKevin Wolf 
296*7e3c0deaSKevin Wolf static QDict *qmp_greeting(MonitorQMP *mon)
297*7e3c0deaSKevin Wolf {
298*7e3c0deaSKevin Wolf     QList *cap_list = qlist_new();
299*7e3c0deaSKevin Wolf     QObject *ver = NULL;
300*7e3c0deaSKevin Wolf     QMPCapability cap;
301*7e3c0deaSKevin Wolf 
302*7e3c0deaSKevin Wolf     qmp_marshal_query_version(NULL, &ver, NULL);
303*7e3c0deaSKevin Wolf 
304*7e3c0deaSKevin Wolf     for (cap = 0; cap < QMP_CAPABILITY__MAX; cap++) {
305*7e3c0deaSKevin Wolf         if (mon->capab_offered[cap]) {
306*7e3c0deaSKevin Wolf             qlist_append_str(cap_list, QMPCapability_str(cap));
307*7e3c0deaSKevin Wolf         }
308*7e3c0deaSKevin Wolf     }
309*7e3c0deaSKevin Wolf 
310*7e3c0deaSKevin Wolf     return qdict_from_jsonf_nofail(
311*7e3c0deaSKevin Wolf         "{'QMP': {'version': %p, 'capabilities': %p}}",
312*7e3c0deaSKevin Wolf         ver, cap_list);
313*7e3c0deaSKevin Wolf }
314*7e3c0deaSKevin Wolf 
315*7e3c0deaSKevin Wolf static void monitor_qmp_event(void *opaque, int event)
316*7e3c0deaSKevin Wolf {
317*7e3c0deaSKevin Wolf     QDict *data;
318*7e3c0deaSKevin Wolf     MonitorQMP *mon = opaque;
319*7e3c0deaSKevin Wolf 
320*7e3c0deaSKevin Wolf     switch (event) {
321*7e3c0deaSKevin Wolf     case CHR_EVENT_OPENED:
322*7e3c0deaSKevin Wolf         mon->commands = &qmp_cap_negotiation_commands;
323*7e3c0deaSKevin Wolf         monitor_qmp_caps_reset(mon);
324*7e3c0deaSKevin Wolf         data = qmp_greeting(mon);
325*7e3c0deaSKevin Wolf         qmp_send_response(mon, data);
326*7e3c0deaSKevin Wolf         qobject_unref(data);
327*7e3c0deaSKevin Wolf         mon_refcount++;
328*7e3c0deaSKevin Wolf         break;
329*7e3c0deaSKevin Wolf     case CHR_EVENT_CLOSED:
330*7e3c0deaSKevin Wolf         /*
331*7e3c0deaSKevin Wolf          * Note: this is only useful when the output of the chardev
332*7e3c0deaSKevin Wolf          * backend is still open.  For example, when the backend is
333*7e3c0deaSKevin Wolf          * stdio, it's possible that stdout is still open when stdin
334*7e3c0deaSKevin Wolf          * is closed.
335*7e3c0deaSKevin Wolf          */
336*7e3c0deaSKevin Wolf         monitor_qmp_cleanup_queues(mon);
337*7e3c0deaSKevin Wolf         json_message_parser_destroy(&mon->parser);
338*7e3c0deaSKevin Wolf         json_message_parser_init(&mon->parser, handle_qmp_command,
339*7e3c0deaSKevin Wolf                                  mon, NULL);
340*7e3c0deaSKevin Wolf         mon_refcount--;
341*7e3c0deaSKevin Wolf         monitor_fdsets_cleanup();
342*7e3c0deaSKevin Wolf         break;
343*7e3c0deaSKevin Wolf     }
344*7e3c0deaSKevin Wolf }
345*7e3c0deaSKevin Wolf 
346*7e3c0deaSKevin Wolf void monitor_data_destroy_qmp(MonitorQMP *mon)
347*7e3c0deaSKevin Wolf {
348*7e3c0deaSKevin Wolf     json_message_parser_destroy(&mon->parser);
349*7e3c0deaSKevin Wolf     qemu_mutex_destroy(&mon->qmp_queue_lock);
350*7e3c0deaSKevin Wolf     monitor_qmp_cleanup_req_queue_locked(mon);
351*7e3c0deaSKevin Wolf     g_queue_free(mon->qmp_requests);
352*7e3c0deaSKevin Wolf }
353*7e3c0deaSKevin Wolf 
354*7e3c0deaSKevin Wolf static void monitor_qmp_setup_handlers_bh(void *opaque)
355*7e3c0deaSKevin Wolf {
356*7e3c0deaSKevin Wolf     MonitorQMP *mon = opaque;
357*7e3c0deaSKevin Wolf     GMainContext *context;
358*7e3c0deaSKevin Wolf 
359*7e3c0deaSKevin Wolf     assert(mon->common.use_io_thread);
360*7e3c0deaSKevin Wolf     context = iothread_get_g_main_context(mon_iothread);
361*7e3c0deaSKevin Wolf     assert(context);
362*7e3c0deaSKevin Wolf     qemu_chr_fe_set_handlers(&mon->common.chr, monitor_can_read,
363*7e3c0deaSKevin Wolf                              monitor_qmp_read, monitor_qmp_event,
364*7e3c0deaSKevin Wolf                              NULL, &mon->common, context, true);
365*7e3c0deaSKevin Wolf     monitor_list_append(&mon->common);
366*7e3c0deaSKevin Wolf }
367*7e3c0deaSKevin Wolf 
368*7e3c0deaSKevin Wolf void monitor_init_qmp(Chardev *chr, int flags)
369*7e3c0deaSKevin Wolf {
370*7e3c0deaSKevin Wolf     MonitorQMP *mon = g_new0(MonitorQMP, 1);
371*7e3c0deaSKevin Wolf 
372*7e3c0deaSKevin Wolf     /* Only HMP supports readline */
373*7e3c0deaSKevin Wolf     assert(!(flags & MONITOR_USE_READLINE));
374*7e3c0deaSKevin Wolf 
375*7e3c0deaSKevin Wolf     /* Note: we run QMP monitor in I/O thread when @chr supports that */
376*7e3c0deaSKevin Wolf     monitor_data_init(&mon->common, flags, false,
377*7e3c0deaSKevin Wolf                       qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_GCONTEXT));
378*7e3c0deaSKevin Wolf 
379*7e3c0deaSKevin Wolf     qemu_mutex_init(&mon->qmp_queue_lock);
380*7e3c0deaSKevin Wolf     mon->qmp_requests = g_queue_new();
381*7e3c0deaSKevin Wolf 
382*7e3c0deaSKevin Wolf     qemu_chr_fe_init(&mon->common.chr, chr, &error_abort);
383*7e3c0deaSKevin Wolf     qemu_chr_fe_set_echo(&mon->common.chr, true);
384*7e3c0deaSKevin Wolf 
385*7e3c0deaSKevin Wolf     json_message_parser_init(&mon->parser, handle_qmp_command, mon, NULL);
386*7e3c0deaSKevin Wolf     if (mon->common.use_io_thread) {
387*7e3c0deaSKevin Wolf         /*
388*7e3c0deaSKevin Wolf          * Make sure the old iowatch is gone.  It's possible when
389*7e3c0deaSKevin Wolf          * e.g. the chardev is in client mode, with wait=on.
390*7e3c0deaSKevin Wolf          */
391*7e3c0deaSKevin Wolf         remove_fd_in_watch(chr);
392*7e3c0deaSKevin Wolf         /*
393*7e3c0deaSKevin Wolf          * We can't call qemu_chr_fe_set_handlers() directly here
394*7e3c0deaSKevin Wolf          * since chardev might be running in the monitor I/O
395*7e3c0deaSKevin Wolf          * thread.  Schedule a bottom half.
396*7e3c0deaSKevin Wolf          */
397*7e3c0deaSKevin Wolf         aio_bh_schedule_oneshot(iothread_get_aio_context(mon_iothread),
398*7e3c0deaSKevin Wolf                                 monitor_qmp_setup_handlers_bh, mon);
399*7e3c0deaSKevin Wolf         /* The bottom half will add @mon to @mon_list */
400*7e3c0deaSKevin Wolf     } else {
401*7e3c0deaSKevin Wolf         qemu_chr_fe_set_handlers(&mon->common.chr, monitor_can_read,
402*7e3c0deaSKevin Wolf                                  monitor_qmp_read, monitor_qmp_event,
403*7e3c0deaSKevin Wolf                                  NULL, &mon->common, NULL, true);
404*7e3c0deaSKevin Wolf         monitor_list_append(&mon->common);
405*7e3c0deaSKevin Wolf     }
406*7e3c0deaSKevin Wolf }
407