| 1 | /* |
| 2 | * QEMU monitor |
| 3 | * |
| 4 | * Copyright (c) 2003-2004 Fabrice Bellard |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | #ifndef MONITOR_INTERNAL_H |
| 26 | #define MONITOR_INTERNAL_H |
| 27 | |
| 28 | #include "chardev/char-fe.h" |
| 29 | #include "monitor/monitor.h" |
| 30 | #include "qapi/qapi-emit-events.h" |
| 31 | #include "qapi/qapi-types-control.h" |
| 32 | #include "qapi/qapi-types-qom.h" |
| 33 | #include "qapi/qmp-registry.h" |
| 34 | #include "qobject/json-parser.h" |
| 35 | #include "qemu/readline.h" |
| 36 | #include "system/iothread.h" |
| 37 | |
| 38 | struct MonitorClass { |
| 39 | ObjectClass parent_class; |
| 40 | |
| 41 | /* |
| 42 | * If non-NULL, the monitor is able to send event |
| 43 | * notifications back to the client |
| 44 | */ |
| 45 | void (*emit_event)(Monitor *mon, QAPIEvent event, QDict *qdict); |
| 46 | /* |
| 47 | * If non-NULL, perform any actions needed to prepare |
| 48 | * the monitor to accept further client input |
| 49 | */ |
| 50 | void (*accept_input)(Monitor *mon); |
| 51 | |
| 52 | /* |
| 53 | * If non-NULL and returns true, then an I/O thread |
| 54 | * is required for processing the monitor |
| 55 | */ |
| 56 | bool (*requires_iothread)(const Monitor *mon); |
| 57 | }; |
| 58 | |
| 59 | struct Monitor { |
| 60 | Object parent; |
| 61 | char *chardev_id; |
| 62 | CharFrontend chr; |
| 63 | int suspend_cnt; /* Needs to be accessed atomically */ |
| 64 | QEMUBH *accept_input_bh; /* persistent BH for monitor_accept_input */ |
| 65 | QTAILQ_ENTRY(Monitor) entry; |
| 66 | |
| 67 | /* |
| 68 | * The per-monitor lock. We can't access guest memory when holding |
| 69 | * the lock. |
| 70 | */ |
| 71 | QemuMutex mon_lock; |
| 72 | |
| 73 | /* |
| 74 | * Members that are protected by the per-monitor lock |
| 75 | */ |
| 76 | QLIST_HEAD(, mon_fd_t) fds; |
| 77 | GString *outbuf; |
| 78 | guint out_watch; |
| 79 | int mux_out; |
| 80 | }; |
| 81 | |
| 82 | struct MonitorQMPClass { |
| 83 | MonitorClass parent_class; |
| 84 | }; |
| 85 | |
| 86 | struct MonitorQMP { |
| 87 | Monitor parent_obj; |
| 88 | JSONMessageParser parser; |
| 89 | bool pretty; |
| 90 | MonitorQMPCloseAction close_action; |
| 91 | bool setup_pending; /* iothread BH has not yet set up chardev handlers */ |
| 92 | bool delete_pending; /* close_action has started 'delete' process */ |
| 93 | /* |
| 94 | * When a client connects, we're in capabilities negotiation mode. |
| 95 | * @commands is &qmp_cap_negotiation_commands then. When command |
| 96 | * qmp_capabilities succeeds, we go into command mode, and |
| 97 | * @command becomes &qmp_commands. |
| 98 | */ |
| 99 | const QmpCommandList *commands; |
| 100 | bool capab_offered[QMP_CAPABILITY__MAX]; /* capabilities offered */ |
| 101 | bool capab[QMP_CAPABILITY__MAX]; /* offered and accepted */ |
| 102 | /* |
| 103 | * Protects qmp request/response queue. |
| 104 | * Take monitor_lock first when you need both. |
| 105 | */ |
| 106 | QemuMutex qmp_queue_lock; |
| 107 | /* Input queue that holds all the parsed QMP requests */ |
| 108 | GQueue *qmp_requests; |
| 109 | }; |
| 110 | |
| 111 | typedef QTAILQ_HEAD(MonitorList, Monitor) MonitorList; |
| 112 | extern IOThread *mon_iothread; |
| 113 | extern Coroutine *qmp_dispatcher_co; |
| 114 | extern bool qmp_dispatcher_co_shutdown; |
| 115 | extern QmpCommandList qmp_commands, qmp_cap_negotiation_commands; |
| 116 | extern QemuMutex monitor_lock; |
| 117 | extern MonitorList mon_list; |
| 118 | |
| 119 | bool monitor_requires_iothread(const Monitor *mon); |
| 120 | int monitor_can_read(void *opaque); |
| 121 | void monitor_cancel_out_watch(Monitor *mon); |
| 122 | void monitor_list_append(Monitor *mon); |
| 123 | void monitor_fdsets_cleanup(void); |
| 124 | |
| 125 | void qmp_send_response(MonitorQMP *mon, const QDict *rsp); |
| 126 | void monitor_data_destroy_qmp(MonitorQMP *mon); |
| 127 | void coroutine_fn monitor_qmp_dispatcher_co(void *data); |
| 128 | void qmp_dispatcher_co_wake(void); |
| 129 | |
| 130 | #endif |