From f32ee7f16206334c90d2c92517617c08f436ca97 Mon Sep 17 00:00:00 2001
From: Stefan Hajnoczi <stefanha@redhat.com>
Date: Tue, 16 Jul 2019 13:22:13 +0200
Subject: [PATCH 20/23] vl: add qemu_add_vm_change_state_handler_prio()
RH-Author: Stefan Hajnoczi <stefanha@redhat.com>
Message-id: <20190716132215.18503-2-stefanha@redhat.com>
Patchwork-id: 89536
O-Subject: [RHEL-7.8 RHEL-7.7.z qemu-kvm-rhev PATCH 1/3] vl: add qemu_add_vm_change_state_handler_prio()
Bugzilla: 1673546
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
RH-Acked-by: John Snow <jsnow@redhat.com>
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
Add an API for registering vm change state handlers with a well-defined
ordering. This is necessary when handlers depend on each other.
Small coding style fixes are included to make checkpatch.pl happy.
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
(cherry picked from commit 60dbc5a1c5176269669ffc26c081ab2cfb7f12f7)
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
Conflicts:
vl.c
The QTAILQ macros require an explicit name for the head type.
---
include/sysemu/sysemu.h | 2 ++
vl.c | 61 +++++++++++++++++++++++++++++++++++++++----------
2 files changed, 51 insertions(+), 12 deletions(-)
diff --git a/include/sysemu/sysemu.h b/include/sysemu/sysemu.h
index 2a6f4a5..723c7a9 100644
--- a/include/sysemu/sysemu.h
+++ b/include/sysemu/sysemu.h
@@ -29,6 +29,8 @@ typedef void VMChangeStateHandler(void *opaque, int running, RunState state);
VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
void *opaque);
+VMChangeStateEntry *qemu_add_vm_change_state_handler_prio(
+ VMChangeStateHandler *cb, void *opaque, int priority);
void qemu_del_vm_change_state_handler(VMChangeStateEntry *e);
void vm_state_notify(int running, RunState state);
diff --git a/vl.c b/vl.c
index 5b337e1..6529d68 100644
--- a/vl.c
+++ b/vl.c
@@ -1658,28 +1658,58 @@ static int machine_help_func(QemuOpts *opts, MachineState *machine)
struct vm_change_state_entry {
VMChangeStateHandler *cb;
void *opaque;
- QLIST_ENTRY (vm_change_state_entry) entries;
+ QTAILQ_ENTRY(vm_change_state_entry) entries;
+ int priority;
};
-static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
+static QTAILQ_HEAD(VMChangeStateHead,
+ vm_change_state_entry) vm_change_state_head;
-VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
- void *opaque)
+/**
+ * qemu_add_vm_change_state_handler_prio:
+ * @cb: the callback to invoke
+ * @opaque: user data passed to the callback
+ * @priority: low priorities execute first when the vm runs and the reverse is
+ * true when the vm stops
+ *
+ * Register a callback function that is invoked when the vm starts or stops
+ * running.
+ *
+ * Returns: an entry to be freed using qemu_del_vm_change_state_handler()
+ */
+VMChangeStateEntry *qemu_add_vm_change_state_handler_prio(
+ VMChangeStateHandler *cb, void *opaque, int priority)
{
VMChangeStateEntry *e;
+ VMChangeStateEntry *other;
- e = g_malloc0(sizeof (*e));
-
+ e = g_malloc0(sizeof(*e));
e->cb = cb;
e->opaque = opaque;
- QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
+ e->priority = priority;
+
+ /* Keep list sorted in ascending priority order */
+ QTAILQ_FOREACH(other, &vm_change_state_head, entries) {
+ if (priority < other->priority) {
+ QTAILQ_INSERT_BEFORE(other, e, entries);
+ return e;
+ }
+ }
+
+ QTAILQ_INSERT_TAIL(&vm_change_state_head, e, entries);
return e;
}
+VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
+ void *opaque)
+{
+ return qemu_add_vm_change_state_handler_prio(cb, opaque, 0);
+}
+
void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
{
- QLIST_REMOVE (e, entries);
- g_free (e);
+ QTAILQ_REMOVE(&vm_change_state_head, e, entries);
+ g_free(e);
}
void vm_state_notify(int running, RunState state)
@@ -1688,8 +1718,15 @@ void vm_state_notify(int running, RunState state)
trace_vm_state_notify(running, state);
- QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
- e->cb(e->opaque, running, state);
+ if (running) {
+ QTAILQ_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
+ e->cb(e->opaque, running, state);
+ }
+ } else {
+ QTAILQ_FOREACH_REVERSE_SAFE(e, &vm_change_state_head,
+ VMChangeStateHead, entries, next) {
+ e->cb(e->opaque, running, state);
+ }
}
}
@@ -3194,7 +3231,7 @@ int main(int argc, char **argv, char **envp)
}
rtc_clock = QEMU_CLOCK_HOST;
- QLIST_INIT (&vm_change_state_head);
+ QTAILQ_INIT(&vm_change_state_head);
os_setup_early_signal_handling();
cpu_model = NULL;
--
1.8.3.1