@samitouri / QOSamiQemu / commits / b465a08b0c

monitor: eliminate monitor_is_hmp_non_interactive method

The monitor_is_hmp_non_interactive method is used by monitor_suspend and monitor_resume, to make them a no-op if the HMP does not use readline. There are only a handful of callers of suspend/resume and they can be made to skip the call when readline is not present. 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-23-berrange@redhat.com> Signed-off-by: Markus Armbruster <armbru@redhat.com>

Daniel P. Berrangé committed Jul 6, 2026 at 14:58 UTC b465a08b0cf5634a5b9d88765b41d3d20800fcb0
5 files changed +18 -35
include/monitor/monitor.h
+1 -1
@@ -37,7 +37,7 @@ int monitor_new(MonitorOptions *opts, bool allow_hmp, Error **errp);
37 int monitor_new_opts(QemuOpts *opts, Error **errp);
38 void monitor_cleanup(void);
39
40 -int monitor_suspend(Monitor *mon);
40 +void monitor_suspend(Monitor *mon);
41 void monitor_resume(Monitor *mon);
42
43 int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp);
migration/migration-hmp-cmds.c
+4 -1
@@ -19,6 +19,7 @@
19 #include "monitor/hmp.h"
20 #include "monitor/hmp-completion.h"
21 #include "monitor/monitor.h"
22 +#include "monitor/monitor-internal.h"
23 #include "qapi/error.h"
24 #include "qapi/qapi-commands-migration.h"
25 #include "qapi/qapi-visit-migration.h"
@@ -853,12 +854,14 @@ void hmp_migrate(Monitor *mon, const QDict *qdict)
854
855 if (!detach) {
856 HMPMigrationStatus *status;
857 + MonitorHMP *hmp = MONITOR_HMP(mon);
858
857 - if (monitor_suspend(mon) < 0) {
859 + if (!hmp->use_readline) {
860 monitor_printf(mon, "terminal does not allow synchronous "
861 "migration, continuing detached\n");
862 return;
863 }
864 + monitor_suspend(mon);
865
866 status = g_malloc0(sizeof(*status));
867 status->mon = mon;
monitor/hmp-cmds.c
+4 -1
@@ -129,7 +129,10 @@ void hmp_info_version(Monitor *mon, const QDict *qdict)
129
130 void hmp_quit(Monitor *mon, const QDict *qdict)
131 {
132 - monitor_suspend(mon);
132 + MonitorHMP *hmp = MONITOR_HMP(mon);
133 + if (hmp->use_readline) {
134 + monitor_suspend(mon);
135 + }
136 qmp_quit(NULL);
137 }
138
monitor/hmp.c
+8 -3
@@ -1528,13 +1528,16 @@ static void monitor_read(void *opaque, const uint8_t *buf, int size)
1528 static void monitor_event(void *opaque, QEMUChrEvent event)
1529 {
1530 Monitor *mon = opaque;
1531 + MonitorHMP *hmp = MONITOR_HMP(mon);
1532
1533 switch (event) {
1534 case CHR_EVENT_MUX_IN:
1535 qemu_mutex_lock(&mon->mon_lock);
1536 if (mon->mux_out) {
1537 mon->mux_out = 0;
1537 - monitor_resume(mon);
1538 + if (hmp->use_readline) {
1539 + monitor_resume(mon);
1540 + }
1541 }
1542 qemu_mutex_unlock(&mon->mon_lock);
1543 break;
@@ -1547,7 +1550,9 @@ static void monitor_event(void *opaque, QEMUChrEvent event)
1550 } else {
1551 monitor_flush_locked(mon);
1552 }
1550 - monitor_suspend(mon);
1553 + if (hmp->use_readline) {
1554 + monitor_suspend(mon);
1555 + }
1556 mon->mux_out = 1;
1557 }
1558 qemu_mutex_unlock(&mon->mon_lock);
@@ -1558,7 +1563,7 @@ static void monitor_event(void *opaque, QEMUChrEvent event)
1563 "information\n", QEMU_VERSION);
1564 qemu_mutex_lock(&mon->mon_lock);
1565 mon->reset_seen = 1;
1561 - if (!mon->mux_out) {
1566 + if (!mon->mux_out && hmp->use_readline) {
1567 /* Suspend-resume forces the prompt to be printed. */
1568 monitor_suspend(mon);
1569 monitor_resume(mon);
monitor/monitor.c
+1 -29
@@ -157,25 +157,6 @@ bool monitor_requires_iothread(const Monitor *mon)
157 return cls->requires_iothread && cls->requires_iothread(mon);
158 }
159
160 -/**
161 - * Is @mon is using readline?
162 - * Note: not all HMP monitors use readline, e.g., gdbserver has a
163 - * non-interactive HMP monitor, so readline is not used there.
164 - */
165 -static inline bool monitor_uses_readline(const MonitorHMP *mon)
166 -{
167 - return mon->use_readline;
168 -}
169 -
170 -static inline bool monitor_is_hmp_non_interactive(const Monitor *mon)
171 -{
172 - if (!object_dynamic_cast(OBJECT(mon), TYPE_MONITOR_HMP)) {
173 - return false;
174 - }
175 -
176 - return !monitor_uses_readline(container_of(mon, MonitorHMP, parent_obj));
177 -}
178 -
160 static gboolean monitor_unblocked(void *do_not_use, GIOCondition cond,
161 void *opaque)
162 {
@@ -550,12 +531,8 @@ static gboolean qapi_event_throttle_equal(const void *a, const void *b)
531 return TRUE;
532 }
533
553 -int monitor_suspend(Monitor *mon)
534 +void monitor_suspend(Monitor *mon)
535 {
555 - if (monitor_is_hmp_non_interactive(mon)) {
556 - return -ENOTTY;
557 - }
558 -
536 qatomic_inc(&mon->suspend_cnt);
537
538 if (monitor_requires_iothread(mon)) {
@@ -567,7 +544,6 @@ int monitor_suspend(Monitor *mon)
544 }
545
546 trace_monitor_suspend(mon, 1);
570 - return 0;
547 }
548
549 static void monitor_accept_input(void *opaque)
@@ -584,10 +560,6 @@ static void monitor_accept_input(void *opaque)
560
561 void monitor_resume(Monitor *mon)
562 {
587 - if (monitor_is_hmp_non_interactive(mon)) {
588 - return;
589 - }
590 -
563 if (qatomic_dec_fetch(&mon->suspend_cnt) == 0) {
564 AioContext *ctx;
565