monitor: use class methods for monitor_accept_input
This removes the need for using monitor_is_qmp() to check the subclass type, which is an anti-pattern. Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com> Tested-by: Peter Krempa <pkrempa@redhat.com> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com> Message-ID: <20260706135824.2623960-15-berrange@redhat.com> Signed-off-by: Markus Armbruster <armbru@redhat.com>
Daniel P. Berrangé committed
Jul 6, 2026 at 14:58 UTC
da85de2934e0600328d2f68e3fb56c634d4e4298
3 files changed
+24
-9
monitor/hmp.c
+16
@@ -70,6 +70,7 @@ static void monitor_hmp_set_readline(Object *obj, bool val, Error **errp)
70
71
int monitor_hmp_vprintf(Monitor *mon, const char *fmt, va_list ap)
72
G_GNUC_PRINTF(2, 0);
73
+static void monitor_hmp_accept_input(Monitor *mon);
74
75
static void monitor_hmp_class_init(ObjectClass *cls, const void *data)
76
{
@@ -80,6 +81,7 @@ static void monitor_hmp_class_init(ObjectClass *cls, const void *data)
81
monitor_hmp_set_readline);
82
83
moncls->vprintf = monitor_hmp_vprintf;
84
+ moncls->accept_input = monitor_hmp_accept_input;
85
}
86
87
static void monitor_hmp_init(Object *obj)
@@ -100,6 +102,20 @@ int monitor_hmp_vprintf(Monitor *mon, const char *fmt, va_list ap)
102
return monitor_puts(mon, buf);
103
}
104
105
+static void monitor_hmp_accept_input(Monitor *mon)
106
+{
107
+ qemu_mutex_lock(&mon->mon_lock);
108
+ if (mon->reset_seen) {
109
+ MonitorHMP *hmp = MONITOR_HMP(mon);
110
+ assert(hmp->rs);
111
+ readline_restart(hmp->rs);
112
+ qemu_mutex_unlock(&mon->mon_lock);
113
+ readline_show_prompt(hmp->rs);
114
+ } else {
115
+ qemu_mutex_unlock(&mon->mon_lock);
116
+ }
117
+}
118
+
119
static void monitor_command_cb(void *opaque, const char *cmdline,
120
void *readline_opaque)
121
{
monitor/monitor-internal.h
+5
@@ -116,6 +116,11 @@ struct MonitorClass {
116
* notifications back to the client
117
*/
118
void (*emit_event)(Monitor *mon, QAPIEvent event, QDict *qdict);
119
+ /*
120
+ * If non-NULL, perform any actions needed to prepare
121
+ * the monitor to accept further client input
122
+ */
123
+ void (*accept_input)(Monitor *mon);
124
};
125
126
struct Monitor {
monitor/monitor.c
+3
-9
@@ -578,16 +578,10 @@ int monitor_suspend(Monitor *mon)
578
static void monitor_accept_input(void *opaque)
579
{
580
Monitor *mon = opaque;
581
+ MonitorClass *cls = MONITOR_GET_CLASS(mon);
582
582
- qemu_mutex_lock(&mon->mon_lock);
583
- if (!monitor_is_qmp(mon) && mon->reset_seen) {
584
- MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, parent_obj);
585
- assert(hmp_mon->rs);
586
- readline_restart(hmp_mon->rs);
587
- qemu_mutex_unlock(&mon->mon_lock);
588
- readline_show_prompt(hmp_mon->rs);
589
- } else {
590
- qemu_mutex_unlock(&mon->mon_lock);
583
+ if (cls->accept_input) {
584
+ cls->accept_input(mon);
585
}
586
587
qemu_chr_fe_accept_input(&mon->chr);