17e3c0deaSKevin Wolf /* 27e3c0deaSKevin Wolf * QEMU monitor 37e3c0deaSKevin Wolf * 47e3c0deaSKevin Wolf * Copyright (c) 2003-2004 Fabrice Bellard 57e3c0deaSKevin Wolf * 67e3c0deaSKevin Wolf * Permission is hereby granted, free of charge, to any person obtaining a copy 77e3c0deaSKevin Wolf * of this software and associated documentation files (the "Software"), to deal 87e3c0deaSKevin Wolf * in the Software without restriction, including without limitation the rights 97e3c0deaSKevin Wolf * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 107e3c0deaSKevin Wolf * copies of the Software, and to permit persons to whom the Software is 117e3c0deaSKevin Wolf * furnished to do so, subject to the following conditions: 127e3c0deaSKevin Wolf * 137e3c0deaSKevin Wolf * The above copyright notice and this permission notice shall be included in 147e3c0deaSKevin Wolf * all copies or substantial portions of the Software. 157e3c0deaSKevin Wolf * 167e3c0deaSKevin Wolf * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 177e3c0deaSKevin Wolf * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 187e3c0deaSKevin Wolf * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 197e3c0deaSKevin Wolf * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 207e3c0deaSKevin Wolf * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 217e3c0deaSKevin Wolf * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 227e3c0deaSKevin Wolf * THE SOFTWARE. 237e3c0deaSKevin Wolf */ 247e3c0deaSKevin Wolf 257e3c0deaSKevin Wolf #include "qemu/osdep.h" 267e3c0deaSKevin Wolf 277e3c0deaSKevin Wolf #include "chardev/char-io.h" 287e3c0deaSKevin Wolf #include "monitor-internal.h" 297e3c0deaSKevin Wolf #include "qapi/error.h" 30*fa4dcf57SKevin Wolf #include "qapi/qapi-commands-control.h" 317e3c0deaSKevin Wolf #include "qapi/qmp/qdict.h" 327e3c0deaSKevin Wolf #include "qapi/qmp/qjson.h" 337e3c0deaSKevin Wolf #include "qapi/qmp/qlist.h" 347e3c0deaSKevin Wolf #include "qapi/qmp/qstring.h" 357e3c0deaSKevin Wolf #include "trace.h" 367e3c0deaSKevin Wolf 377e3c0deaSKevin Wolf struct QMPRequest { 387e3c0deaSKevin Wolf /* Owner of the request */ 397e3c0deaSKevin Wolf MonitorQMP *mon; 407e3c0deaSKevin Wolf /* 417e3c0deaSKevin Wolf * Request object to be handled or Error to be reported 427e3c0deaSKevin Wolf * (exactly one of them is non-null) 437e3c0deaSKevin Wolf */ 447e3c0deaSKevin Wolf QObject *req; 457e3c0deaSKevin Wolf Error *err; 467e3c0deaSKevin Wolf }; 477e3c0deaSKevin Wolf typedef struct QMPRequest QMPRequest; 487e3c0deaSKevin Wolf 497e3c0deaSKevin Wolf QmpCommandList qmp_commands, qmp_cap_negotiation_commands; 507e3c0deaSKevin Wolf 517e3c0deaSKevin Wolf static bool qmp_oob_enabled(MonitorQMP *mon) 527e3c0deaSKevin Wolf { 537e3c0deaSKevin Wolf return mon->capab[QMP_CAPABILITY_OOB]; 547e3c0deaSKevin Wolf } 557e3c0deaSKevin Wolf 567e3c0deaSKevin Wolf static void monitor_qmp_caps_reset(MonitorQMP *mon) 577e3c0deaSKevin Wolf { 587e3c0deaSKevin Wolf memset(mon->capab_offered, 0, sizeof(mon->capab_offered)); 597e3c0deaSKevin Wolf memset(mon->capab, 0, sizeof(mon->capab)); 607e3c0deaSKevin Wolf mon->capab_offered[QMP_CAPABILITY_OOB] = mon->common.use_io_thread; 617e3c0deaSKevin Wolf } 627e3c0deaSKevin Wolf 637e3c0deaSKevin Wolf static void qmp_request_free(QMPRequest *req) 647e3c0deaSKevin Wolf { 657e3c0deaSKevin Wolf qobject_unref(req->req); 667e3c0deaSKevin Wolf error_free(req->err); 677e3c0deaSKevin Wolf g_free(req); 687e3c0deaSKevin Wolf } 697e3c0deaSKevin Wolf 707e3c0deaSKevin Wolf /* Caller must hold mon->qmp.qmp_queue_lock */ 717e3c0deaSKevin Wolf static void monitor_qmp_cleanup_req_queue_locked(MonitorQMP *mon) 727e3c0deaSKevin Wolf { 737e3c0deaSKevin Wolf while (!g_queue_is_empty(mon->qmp_requests)) { 747e3c0deaSKevin Wolf qmp_request_free(g_queue_pop_head(mon->qmp_requests)); 757e3c0deaSKevin Wolf } 767e3c0deaSKevin Wolf } 777e3c0deaSKevin Wolf 782895aaa1SWolfgang Bumiller static void monitor_qmp_cleanup_queue_and_resume(MonitorQMP *mon) 797e3c0deaSKevin Wolf { 807e3c0deaSKevin Wolf qemu_mutex_lock(&mon->qmp_queue_lock); 812895aaa1SWolfgang Bumiller 822895aaa1SWolfgang Bumiller /* 832895aaa1SWolfgang Bumiller * Same condition as in monitor_qmp_bh_dispatcher(), but before 842895aaa1SWolfgang Bumiller * removing an element from the queue (hence no `- 1`). 852895aaa1SWolfgang Bumiller * Also, the queue should not be empty either, otherwise the 862895aaa1SWolfgang Bumiller * monitor hasn't been suspended yet (or was already resumed). 872895aaa1SWolfgang Bumiller */ 882895aaa1SWolfgang Bumiller bool need_resume = (!qmp_oob_enabled(mon) || 892895aaa1SWolfgang Bumiller mon->qmp_requests->length == QMP_REQ_QUEUE_LEN_MAX) 902895aaa1SWolfgang Bumiller && !g_queue_is_empty(mon->qmp_requests); 912895aaa1SWolfgang Bumiller 927e3c0deaSKevin Wolf monitor_qmp_cleanup_req_queue_locked(mon); 932895aaa1SWolfgang Bumiller 942895aaa1SWolfgang Bumiller if (need_resume) { 952895aaa1SWolfgang Bumiller /* 962895aaa1SWolfgang Bumiller * handle_qmp_command() suspended the monitor because the 972895aaa1SWolfgang Bumiller * request queue filled up, to be resumed when the queue has 982895aaa1SWolfgang Bumiller * space again. We just emptied it; resume the monitor. 992895aaa1SWolfgang Bumiller * 1002895aaa1SWolfgang Bumiller * Without this, the monitor would remain suspended forever 1012895aaa1SWolfgang Bumiller * when we get here while the monitor is suspended. An 1022895aaa1SWolfgang Bumiller * unfortunately timed CHR_EVENT_CLOSED can do the trick. 1032895aaa1SWolfgang Bumiller */ 1042895aaa1SWolfgang Bumiller monitor_resume(&mon->common); 1052895aaa1SWolfgang Bumiller } 1062895aaa1SWolfgang Bumiller 1077e3c0deaSKevin Wolf qemu_mutex_unlock(&mon->qmp_queue_lock); 1087e3c0deaSKevin Wolf } 1097e3c0deaSKevin Wolf 1107e3c0deaSKevin Wolf void qmp_send_response(MonitorQMP *mon, const QDict *rsp) 1117e3c0deaSKevin Wolf { 1127e3c0deaSKevin Wolf const QObject *data = QOBJECT(rsp); 1137e3c0deaSKevin Wolf QString *json; 1147e3c0deaSKevin Wolf 11592082416SKevin Wolf json = mon->pretty ? qobject_to_json_pretty(data) : qobject_to_json(data); 1167e3c0deaSKevin Wolf assert(json != NULL); 1177e3c0deaSKevin Wolf 1187e3c0deaSKevin Wolf qstring_append_chr(json, '\n'); 1197e3c0deaSKevin Wolf monitor_puts(&mon->common, qstring_get_str(json)); 1207e3c0deaSKevin Wolf 1217e3c0deaSKevin Wolf qobject_unref(json); 1227e3c0deaSKevin Wolf } 1237e3c0deaSKevin Wolf 1247e3c0deaSKevin Wolf /* 125eb707eacSMarkus Armbruster * Emit QMP response @rsp to @mon. 1267e3c0deaSKevin Wolf * Null @rsp can only happen for commands with QCO_NO_SUCCESS_RESP. 1277e3c0deaSKevin Wolf * Nothing is emitted then. 1287e3c0deaSKevin Wolf */ 1297e3c0deaSKevin Wolf static void monitor_qmp_respond(MonitorQMP *mon, QDict *rsp) 1307e3c0deaSKevin Wolf { 1317e3c0deaSKevin Wolf if (rsp) { 1327e3c0deaSKevin Wolf qmp_send_response(mon, rsp); 1337e3c0deaSKevin Wolf } 1347e3c0deaSKevin Wolf } 1357e3c0deaSKevin Wolf 1367e3c0deaSKevin Wolf static void monitor_qmp_dispatch(MonitorQMP *mon, QObject *req) 1377e3c0deaSKevin Wolf { 1387e3c0deaSKevin Wolf Monitor *old_mon; 1397e3c0deaSKevin Wolf QDict *rsp; 1407e3c0deaSKevin Wolf QDict *error; 1417e3c0deaSKevin Wolf 1427e3c0deaSKevin Wolf old_mon = cur_mon; 1437e3c0deaSKevin Wolf cur_mon = &mon->common; 1447e3c0deaSKevin Wolf 1457e3c0deaSKevin Wolf rsp = qmp_dispatch(mon->commands, req, qmp_oob_enabled(mon)); 1467e3c0deaSKevin Wolf 1477e3c0deaSKevin Wolf cur_mon = old_mon; 1487e3c0deaSKevin Wolf 1497e3c0deaSKevin Wolf if (mon->commands == &qmp_cap_negotiation_commands) { 1507e3c0deaSKevin Wolf error = qdict_get_qdict(rsp, "error"); 1517e3c0deaSKevin Wolf if (error 1527e3c0deaSKevin Wolf && !g_strcmp0(qdict_get_try_str(error, "class"), 1537e3c0deaSKevin Wolf QapiErrorClass_str(ERROR_CLASS_COMMAND_NOT_FOUND))) { 1547e3c0deaSKevin Wolf /* Provide a more useful error message */ 1557e3c0deaSKevin Wolf qdict_del(error, "desc"); 1567e3c0deaSKevin Wolf qdict_put_str(error, "desc", "Expecting capabilities negotiation" 1577e3c0deaSKevin Wolf " with 'qmp_capabilities'"); 1587e3c0deaSKevin Wolf } 1597e3c0deaSKevin Wolf } 1607e3c0deaSKevin Wolf 1617e3c0deaSKevin Wolf monitor_qmp_respond(mon, rsp); 1627e3c0deaSKevin Wolf qobject_unref(rsp); 1637e3c0deaSKevin Wolf } 1647e3c0deaSKevin Wolf 1657e3c0deaSKevin Wolf /* 1667e3c0deaSKevin Wolf * Pop a QMP request from a monitor request queue. 1677e3c0deaSKevin Wolf * Return the request, or NULL all request queues are empty. 1687e3c0deaSKevin Wolf * We are using round-robin fashion to pop the request, to avoid 1697e3c0deaSKevin Wolf * processing commands only on a very busy monitor. To achieve that, 1707e3c0deaSKevin Wolf * when we process one request on a specific monitor, we put that 1717e3c0deaSKevin Wolf * monitor to the end of mon_list queue. 1727e3c0deaSKevin Wolf * 1737e3c0deaSKevin Wolf * Note: if the function returned with non-NULL, then the caller will 1747e3c0deaSKevin Wolf * be with qmp_mon->qmp_queue_lock held, and the caller is responsible 1757e3c0deaSKevin Wolf * to release it. 1767e3c0deaSKevin Wolf */ 1777e3c0deaSKevin Wolf static QMPRequest *monitor_qmp_requests_pop_any_with_lock(void) 1787e3c0deaSKevin Wolf { 1797e3c0deaSKevin Wolf QMPRequest *req_obj = NULL; 1807e3c0deaSKevin Wolf Monitor *mon; 1817e3c0deaSKevin Wolf MonitorQMP *qmp_mon; 1827e3c0deaSKevin Wolf 1837e3c0deaSKevin Wolf qemu_mutex_lock(&monitor_lock); 1847e3c0deaSKevin Wolf 1857e3c0deaSKevin Wolf QTAILQ_FOREACH(mon, &mon_list, entry) { 1867e3c0deaSKevin Wolf if (!monitor_is_qmp(mon)) { 1877e3c0deaSKevin Wolf continue; 1887e3c0deaSKevin Wolf } 1897e3c0deaSKevin Wolf 1907e3c0deaSKevin Wolf qmp_mon = container_of(mon, MonitorQMP, common); 1917e3c0deaSKevin Wolf qemu_mutex_lock(&qmp_mon->qmp_queue_lock); 1927e3c0deaSKevin Wolf req_obj = g_queue_pop_head(qmp_mon->qmp_requests); 1937e3c0deaSKevin Wolf if (req_obj) { 1947e3c0deaSKevin Wolf /* With the lock of corresponding queue held */ 1957e3c0deaSKevin Wolf break; 1967e3c0deaSKevin Wolf } 1977e3c0deaSKevin Wolf qemu_mutex_unlock(&qmp_mon->qmp_queue_lock); 1987e3c0deaSKevin Wolf } 1997e3c0deaSKevin Wolf 2007e3c0deaSKevin Wolf if (req_obj) { 2017e3c0deaSKevin Wolf /* 2027e3c0deaSKevin Wolf * We found one request on the monitor. Degrade this monitor's 2037e3c0deaSKevin Wolf * priority to lowest by re-inserting it to end of queue. 2047e3c0deaSKevin Wolf */ 2057e3c0deaSKevin Wolf QTAILQ_REMOVE(&mon_list, mon, entry); 2067e3c0deaSKevin Wolf QTAILQ_INSERT_TAIL(&mon_list, mon, entry); 2077e3c0deaSKevin Wolf } 2087e3c0deaSKevin Wolf 2097e3c0deaSKevin Wolf qemu_mutex_unlock(&monitor_lock); 2107e3c0deaSKevin Wolf 2117e3c0deaSKevin Wolf return req_obj; 2127e3c0deaSKevin Wolf } 2137e3c0deaSKevin Wolf 2147e3c0deaSKevin Wolf void monitor_qmp_bh_dispatcher(void *data) 2157e3c0deaSKevin Wolf { 2167e3c0deaSKevin Wolf QMPRequest *req_obj = monitor_qmp_requests_pop_any_with_lock(); 2177e3c0deaSKevin Wolf QDict *rsp; 2187e3c0deaSKevin Wolf bool need_resume; 2197e3c0deaSKevin Wolf MonitorQMP *mon; 2207e3c0deaSKevin Wolf 2217e3c0deaSKevin Wolf if (!req_obj) { 2227e3c0deaSKevin Wolf return; 2237e3c0deaSKevin Wolf } 2247e3c0deaSKevin Wolf 2257e3c0deaSKevin Wolf mon = req_obj->mon; 2267e3c0deaSKevin Wolf /* qmp_oob_enabled() might change after "qmp_capabilities" */ 2277e3c0deaSKevin Wolf need_resume = !qmp_oob_enabled(mon) || 2287e3c0deaSKevin Wolf mon->qmp_requests->length == QMP_REQ_QUEUE_LEN_MAX - 1; 2297e3c0deaSKevin Wolf qemu_mutex_unlock(&mon->qmp_queue_lock); 2307e3c0deaSKevin Wolf if (req_obj->req) { 2317e3c0deaSKevin Wolf QDict *qdict = qobject_to(QDict, req_obj->req); 2327e3c0deaSKevin Wolf QObject *id = qdict ? qdict_get(qdict, "id") : NULL; 2337e3c0deaSKevin Wolf trace_monitor_qmp_cmd_in_band(qobject_get_try_str(id) ?: ""); 2347e3c0deaSKevin Wolf monitor_qmp_dispatch(mon, req_obj->req); 2357e3c0deaSKevin Wolf } else { 2367e3c0deaSKevin Wolf assert(req_obj->err); 2377e3c0deaSKevin Wolf rsp = qmp_error_response(req_obj->err); 2387e3c0deaSKevin Wolf req_obj->err = NULL; 2397e3c0deaSKevin Wolf monitor_qmp_respond(mon, rsp); 2407e3c0deaSKevin Wolf qobject_unref(rsp); 2417e3c0deaSKevin Wolf } 2427e3c0deaSKevin Wolf 2437e3c0deaSKevin Wolf if (need_resume) { 2447e3c0deaSKevin Wolf /* Pairs with the monitor_suspend() in handle_qmp_command() */ 2457e3c0deaSKevin Wolf monitor_resume(&mon->common); 2467e3c0deaSKevin Wolf } 2477e3c0deaSKevin Wolf qmp_request_free(req_obj); 2487e3c0deaSKevin Wolf 2497e3c0deaSKevin Wolf /* Reschedule instead of looping so the main loop stays responsive */ 2507e3c0deaSKevin Wolf qemu_bh_schedule(qmp_dispatcher_bh); 2517e3c0deaSKevin Wolf } 2527e3c0deaSKevin Wolf 2537e3c0deaSKevin Wolf static void handle_qmp_command(void *opaque, QObject *req, Error *err) 2547e3c0deaSKevin Wolf { 2557e3c0deaSKevin Wolf MonitorQMP *mon = opaque; 2567e3c0deaSKevin Wolf QObject *id = NULL; 2577e3c0deaSKevin Wolf QDict *qdict; 2587e3c0deaSKevin Wolf QMPRequest *req_obj; 2597e3c0deaSKevin Wolf 2607e3c0deaSKevin Wolf assert(!req != !err); 2617e3c0deaSKevin Wolf 2627e3c0deaSKevin Wolf qdict = qobject_to(QDict, req); 2637e3c0deaSKevin Wolf if (qdict) { 2647e3c0deaSKevin Wolf id = qdict_get(qdict, "id"); 2657e3c0deaSKevin Wolf } /* else will fail qmp_dispatch() */ 2667e3c0deaSKevin Wolf 2677e3c0deaSKevin Wolf if (req && trace_event_get_state_backends(TRACE_HANDLE_QMP_COMMAND)) { 2687e3c0deaSKevin Wolf QString *req_json = qobject_to_json(req); 2697e3c0deaSKevin Wolf trace_handle_qmp_command(mon, qstring_get_str(req_json)); 2707e3c0deaSKevin Wolf qobject_unref(req_json); 2717e3c0deaSKevin Wolf } 2727e3c0deaSKevin Wolf 2737e3c0deaSKevin Wolf if (qdict && qmp_is_oob(qdict)) { 2747e3c0deaSKevin Wolf /* OOB commands are executed immediately */ 2757e3c0deaSKevin Wolf trace_monitor_qmp_cmd_out_of_band(qobject_get_try_str(id) ?: ""); 2767e3c0deaSKevin Wolf monitor_qmp_dispatch(mon, req); 2777e3c0deaSKevin Wolf qobject_unref(req); 2787e3c0deaSKevin Wolf return; 2797e3c0deaSKevin Wolf } 2807e3c0deaSKevin Wolf 2817e3c0deaSKevin Wolf req_obj = g_new0(QMPRequest, 1); 2827e3c0deaSKevin Wolf req_obj->mon = mon; 2837e3c0deaSKevin Wolf req_obj->req = req; 2847e3c0deaSKevin Wolf req_obj->err = err; 2857e3c0deaSKevin Wolf 2867e3c0deaSKevin Wolf /* Protect qmp_requests and fetching its length. */ 2877e3c0deaSKevin Wolf qemu_mutex_lock(&mon->qmp_queue_lock); 2887e3c0deaSKevin Wolf 2897e3c0deaSKevin Wolf /* 2907e3c0deaSKevin Wolf * Suspend the monitor when we can't queue more requests after 2912895aaa1SWolfgang Bumiller * this one. Dequeuing in monitor_qmp_bh_dispatcher() or 2922895aaa1SWolfgang Bumiller * monitor_qmp_cleanup_queue_and_resume() will resume it. 2932895aaa1SWolfgang Bumiller * Note that when OOB is disabled, we queue at most one command, 2942895aaa1SWolfgang Bumiller * for backward compatibility. 2957e3c0deaSKevin Wolf */ 2967e3c0deaSKevin Wolf if (!qmp_oob_enabled(mon) || 2977e3c0deaSKevin Wolf mon->qmp_requests->length == QMP_REQ_QUEUE_LEN_MAX - 1) { 2987e3c0deaSKevin Wolf monitor_suspend(&mon->common); 2997e3c0deaSKevin Wolf } 3007e3c0deaSKevin Wolf 3017e3c0deaSKevin Wolf /* 3027e3c0deaSKevin Wolf * Put the request to the end of queue so that requests will be 3037e3c0deaSKevin Wolf * handled in time order. Ownership for req_obj, req, 3047e3c0deaSKevin Wolf * etc. will be delivered to the handler side. 3057e3c0deaSKevin Wolf */ 3067e3c0deaSKevin Wolf assert(mon->qmp_requests->length < QMP_REQ_QUEUE_LEN_MAX); 3077e3c0deaSKevin Wolf g_queue_push_tail(mon->qmp_requests, req_obj); 3087e3c0deaSKevin Wolf qemu_mutex_unlock(&mon->qmp_queue_lock); 3097e3c0deaSKevin Wolf 3107e3c0deaSKevin Wolf /* Kick the dispatcher routine */ 3117e3c0deaSKevin Wolf qemu_bh_schedule(qmp_dispatcher_bh); 3127e3c0deaSKevin Wolf } 3137e3c0deaSKevin Wolf 3147e3c0deaSKevin Wolf static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size) 3157e3c0deaSKevin Wolf { 3167e3c0deaSKevin Wolf MonitorQMP *mon = opaque; 3177e3c0deaSKevin Wolf 3187e3c0deaSKevin Wolf json_message_parser_feed(&mon->parser, (const char *) buf, size); 3197e3c0deaSKevin Wolf } 3207e3c0deaSKevin Wolf 3217e3c0deaSKevin Wolf static QDict *qmp_greeting(MonitorQMP *mon) 3227e3c0deaSKevin Wolf { 3237e3c0deaSKevin Wolf QList *cap_list = qlist_new(); 3247e3c0deaSKevin Wolf QObject *ver = NULL; 3257e3c0deaSKevin Wolf QMPCapability cap; 3267e3c0deaSKevin Wolf 3277e3c0deaSKevin Wolf qmp_marshal_query_version(NULL, &ver, NULL); 3287e3c0deaSKevin Wolf 3297e3c0deaSKevin Wolf for (cap = 0; cap < QMP_CAPABILITY__MAX; cap++) { 3307e3c0deaSKevin Wolf if (mon->capab_offered[cap]) { 3317e3c0deaSKevin Wolf qlist_append_str(cap_list, QMPCapability_str(cap)); 3327e3c0deaSKevin Wolf } 3337e3c0deaSKevin Wolf } 3347e3c0deaSKevin Wolf 3357e3c0deaSKevin Wolf return qdict_from_jsonf_nofail( 3367e3c0deaSKevin Wolf "{'QMP': {'version': %p, 'capabilities': %p}}", 3377e3c0deaSKevin Wolf ver, cap_list); 3387e3c0deaSKevin Wolf } 3397e3c0deaSKevin Wolf 340083b266fSPhilippe Mathieu-Daudé static void monitor_qmp_event(void *opaque, QEMUChrEvent event) 3417e3c0deaSKevin Wolf { 3427e3c0deaSKevin Wolf QDict *data; 3437e3c0deaSKevin Wolf MonitorQMP *mon = opaque; 3447e3c0deaSKevin Wolf 3457e3c0deaSKevin Wolf switch (event) { 3467e3c0deaSKevin Wolf case CHR_EVENT_OPENED: 3477e3c0deaSKevin Wolf mon->commands = &qmp_cap_negotiation_commands; 3487e3c0deaSKevin Wolf monitor_qmp_caps_reset(mon); 3497e3c0deaSKevin Wolf data = qmp_greeting(mon); 3507e3c0deaSKevin Wolf qmp_send_response(mon, data); 3517e3c0deaSKevin Wolf qobject_unref(data); 3527e3c0deaSKevin Wolf mon_refcount++; 3537e3c0deaSKevin Wolf break; 3547e3c0deaSKevin Wolf case CHR_EVENT_CLOSED: 3557e3c0deaSKevin Wolf /* 3567e3c0deaSKevin Wolf * Note: this is only useful when the output of the chardev 3577e3c0deaSKevin Wolf * backend is still open. For example, when the backend is 3587e3c0deaSKevin Wolf * stdio, it's possible that stdout is still open when stdin 3597e3c0deaSKevin Wolf * is closed. 3607e3c0deaSKevin Wolf */ 3612895aaa1SWolfgang Bumiller monitor_qmp_cleanup_queue_and_resume(mon); 3627e3c0deaSKevin Wolf json_message_parser_destroy(&mon->parser); 3637e3c0deaSKevin Wolf json_message_parser_init(&mon->parser, handle_qmp_command, 3647e3c0deaSKevin Wolf mon, NULL); 3657e3c0deaSKevin Wolf mon_refcount--; 3667e3c0deaSKevin Wolf monitor_fdsets_cleanup(); 3677e3c0deaSKevin Wolf break; 368ed7c5bb7SPhilippe Mathieu-Daudé case CHR_EVENT_BREAK: 369ed7c5bb7SPhilippe Mathieu-Daudé case CHR_EVENT_MUX_IN: 370ed7c5bb7SPhilippe Mathieu-Daudé case CHR_EVENT_MUX_OUT: 371ed7c5bb7SPhilippe Mathieu-Daudé /* Ignore */ 372ed7c5bb7SPhilippe Mathieu-Daudé break; 3737e3c0deaSKevin Wolf } 3747e3c0deaSKevin Wolf } 3757e3c0deaSKevin Wolf 3767e3c0deaSKevin Wolf void monitor_data_destroy_qmp(MonitorQMP *mon) 3777e3c0deaSKevin Wolf { 3787e3c0deaSKevin Wolf json_message_parser_destroy(&mon->parser); 3797e3c0deaSKevin Wolf qemu_mutex_destroy(&mon->qmp_queue_lock); 3807e3c0deaSKevin Wolf monitor_qmp_cleanup_req_queue_locked(mon); 3817e3c0deaSKevin Wolf g_queue_free(mon->qmp_requests); 3827e3c0deaSKevin Wolf } 3837e3c0deaSKevin Wolf 3847e3c0deaSKevin Wolf static void monitor_qmp_setup_handlers_bh(void *opaque) 3857e3c0deaSKevin Wolf { 3867e3c0deaSKevin Wolf MonitorQMP *mon = opaque; 3877e3c0deaSKevin Wolf GMainContext *context; 3887e3c0deaSKevin Wolf 3897e3c0deaSKevin Wolf assert(mon->common.use_io_thread); 3907e3c0deaSKevin Wolf context = iothread_get_g_main_context(mon_iothread); 3917e3c0deaSKevin Wolf assert(context); 3927e3c0deaSKevin Wolf qemu_chr_fe_set_handlers(&mon->common.chr, monitor_can_read, 3937e3c0deaSKevin Wolf monitor_qmp_read, monitor_qmp_event, 3947e3c0deaSKevin Wolf NULL, &mon->common, context, true); 3957e3c0deaSKevin Wolf monitor_list_append(&mon->common); 3967e3c0deaSKevin Wolf } 3977e3c0deaSKevin Wolf 398fbfc29e3SKevin Wolf void monitor_init_qmp(Chardev *chr, bool pretty) 3997e3c0deaSKevin Wolf { 4007e3c0deaSKevin Wolf MonitorQMP *mon = g_new0(MonitorQMP, 1); 4017e3c0deaSKevin Wolf 4027e3c0deaSKevin Wolf /* Note: we run QMP monitor in I/O thread when @chr supports that */ 40392082416SKevin Wolf monitor_data_init(&mon->common, true, false, 4047e3c0deaSKevin Wolf qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_GCONTEXT)); 4057e3c0deaSKevin Wolf 406fbfc29e3SKevin Wolf mon->pretty = pretty; 40792082416SKevin Wolf 4087e3c0deaSKevin Wolf qemu_mutex_init(&mon->qmp_queue_lock); 4097e3c0deaSKevin Wolf mon->qmp_requests = g_queue_new(); 4107e3c0deaSKevin Wolf 4117e3c0deaSKevin Wolf qemu_chr_fe_init(&mon->common.chr, chr, &error_abort); 4127e3c0deaSKevin Wolf qemu_chr_fe_set_echo(&mon->common.chr, true); 4137e3c0deaSKevin Wolf 4147e3c0deaSKevin Wolf json_message_parser_init(&mon->parser, handle_qmp_command, mon, NULL); 4157e3c0deaSKevin Wolf if (mon->common.use_io_thread) { 4167e3c0deaSKevin Wolf /* 4177e3c0deaSKevin Wolf * Make sure the old iowatch is gone. It's possible when 4187e3c0deaSKevin Wolf * e.g. the chardev is in client mode, with wait=on. 4197e3c0deaSKevin Wolf */ 4207e3c0deaSKevin Wolf remove_fd_in_watch(chr); 4217e3c0deaSKevin Wolf /* 4227e3c0deaSKevin Wolf * We can't call qemu_chr_fe_set_handlers() directly here 4237e3c0deaSKevin Wolf * since chardev might be running in the monitor I/O 4247e3c0deaSKevin Wolf * thread. Schedule a bottom half. 4257e3c0deaSKevin Wolf */ 4267e3c0deaSKevin Wolf aio_bh_schedule_oneshot(iothread_get_aio_context(mon_iothread), 4277e3c0deaSKevin Wolf monitor_qmp_setup_handlers_bh, mon); 4287e3c0deaSKevin Wolf /* The bottom half will add @mon to @mon_list */ 4297e3c0deaSKevin Wolf } else { 4307e3c0deaSKevin Wolf qemu_chr_fe_set_handlers(&mon->common.chr, monitor_can_read, 4317e3c0deaSKevin Wolf monitor_qmp_read, monitor_qmp_event, 4327e3c0deaSKevin Wolf NULL, &mon->common, NULL, true); 4337e3c0deaSKevin Wolf monitor_list_append(&mon->common); 4347e3c0deaSKevin Wolf } 4357e3c0deaSKevin Wolf } 436