Add 'id', 'dynamic', and 'dead' fields to struct Monitor. The id field
stores the monitor identifier from MonitorOptions which was previously
parsed but discarded. The dynamic flag marks monitors created at runtime
via the upcoming monitor-add command, and the dead flag will be used for
deferred destruction during monitor-remove.
Extend monitor_init_qmp() to accept id and dynamic parameters so these
are set before the monitor is added to mon_list -- this is important for
the BH/iothread path where the monitor becomes visible asynchronously.
Add monitor_find_by_id() to look up monitors by identifier, and free the
id string in monitor_data_destroy().
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
include/monitor/monitor.h | 3 ++-
monitor/monitor-internal.h | 5 +++++
monitor/monitor.c | 15 ++++++++++++++-
monitor/qmp.c | 5 ++++-
4 files changed, 25 insertions(+), 3 deletions(-)
diff --git a/include/monitor/monitor.h b/include/monitor/monitor.h
index 296690e1f1..7a2bb603e4 100644
--- a/include/monitor/monitor.h
+++ b/include/monitor/monitor.h
@@ -19,7 +19,8 @@ bool monitor_cur_is_qmp(void);
void monitor_init_globals(void);
void monitor_init_globals_core(void);
-void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp);
+void monitor_init_qmp(Chardev *chr, bool pretty, const char *id,
+ bool dynamic, Error **errp);
void monitor_init_hmp(Chardev *chr, bool use_readline, Error **errp);
int monitor_init(MonitorOptions *opts, bool allow_hmp, Error **errp);
int monitor_init_opts(QemuOpts *opts, Error **errp);
diff --git a/monitor/monitor-internal.h b/monitor/monitor-internal.h
index feca111ae3..8f5fe7c111 100644
--- a/monitor/monitor-internal.h
+++ b/monitor/monitor-internal.h
@@ -98,7 +98,10 @@ struct Monitor {
bool is_qmp;
bool skip_flush;
bool use_io_thread;
+ bool dynamic; /* true if created via monitor-add */
+ bool dead; /* true if monitor-remove called, awaiting drain */
+ char *id; /* Monitor identifier (NULL for unnamed CLI monitors) */
char *mon_cpu_path;
QTAILQ_ENTRY(Monitor) entry;
@@ -181,6 +184,8 @@ void monitor_data_destroy_qmp(MonitorQMP *mon);
void coroutine_fn monitor_qmp_dispatcher_co(void *data);
void qmp_dispatcher_co_wake(void);
+Monitor *monitor_find_by_id(const char *id);
+
int get_monitor_def(Monitor *mon, int64_t *pval, const char *name);
void handle_hmp_command(MonitorHMP *mon, const char *cmdline);
int hmp_compare_cmd(const char *name, const char *list);
diff --git a/monitor/monitor.c b/monitor/monitor.c
index 00b93ed612..09e32b6364 100644
--- a/monitor/monitor.c
+++ b/monitor/monitor.c
@@ -622,6 +622,7 @@ void monitor_data_init(Monitor *mon, bool is_qmp, bool skip_flush,
void monitor_data_destroy(Monitor *mon)
{
+ g_free(mon->id);
g_free(mon->mon_cpu_path);
qemu_chr_fe_deinit(&mon->chr, false);
if (monitor_is_qmp(mon)) {
@@ -633,6 +634,18 @@ void monitor_data_destroy(Monitor *mon)
qemu_mutex_destroy(&mon->mon_lock);
}
+Monitor *monitor_find_by_id(const char *id)
+{
+ Monitor *mon;
+
+ QTAILQ_FOREACH(mon, &mon_list, entry) {
+ if (mon->id && strcmp(mon->id, id) == 0) {
+ return mon;
+ }
+ }
+ return NULL;
+}
+
void monitor_cleanup(void)
{
/*
@@ -732,7 +745,7 @@ int monitor_init(MonitorOptions *opts, bool allow_hmp, Error **errp)
switch (opts->mode) {
case MONITOR_MODE_CONTROL:
- monitor_init_qmp(chr, opts->pretty, errp);
+ monitor_init_qmp(chr, opts->pretty, opts->id, false, errp);
break;
case MONITOR_MODE_READLINE:
if (!allow_hmp) {
diff --git a/monitor/qmp.c b/monitor/qmp.c
index 687019811f..5fbc7af074 100644
--- a/monitor/qmp.c
+++ b/monitor/qmp.c
@@ -513,7 +513,8 @@ static void monitor_qmp_setup_handlers_bh(void *opaque)
monitor_list_append(&mon->common);
}
-void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp)
+void monitor_init_qmp(Chardev *chr, bool pretty, const char *id,
+ bool dynamic, Error **errp)
{
MonitorQMP *mon = g_new0(MonitorQMP, 1);
@@ -527,6 +528,8 @@ void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp)
monitor_data_init(&mon->common, true, false,
qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_GCONTEXT));
+ mon->common.id = g_strdup(id);
+ mon->common.dynamic = dynamic;
mon->pretty = pretty;
qemu_mutex_init(&mon->qmp_queue_lock);
--
2.47.3