4 * Copyright (c) Lukas Straub <lukasstraub2@web.de>
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
10 #include "qemu/osdep.h"
11 #include "qapi/error.h"
12 #include "qemu/thread.h"
13 #include "qemu/queue.h"
14 #include "qemu/lockable.h"
15 #include "qapi/qapi-commands-yank.h"
16 #include "qapi/qapi-visit-yank.h"
17 #include "qapi/clone-visitor.h"
18 #include "io/channel.h"
19 #include "qemu/yank.h"
21 struct YankFuncAndParam
{
24 QLIST_ENTRY(YankFuncAndParam
) next
;
27 struct YankInstanceEntry
{
28 YankInstance
*instance
;
29 QLIST_HEAD(, YankFuncAndParam
) yankfns
;
30 QLIST_ENTRY(YankInstanceEntry
) next
;
33 typedef struct YankFuncAndParam YankFuncAndParam
;
34 typedef struct YankInstanceEntry YankInstanceEntry
;
37 * This lock protects the yank_instance_list below. Because it's taken by
38 * OOB-capable commands, it must be "fast", i.e. it may only be held for a
39 * bounded, short time. See docs/devel/qapi-code-gen.txt for additional
42 static QemuMutex yank_lock
;
44 static QLIST_HEAD(, YankInstanceEntry
) yank_instance_list
45 = QLIST_HEAD_INITIALIZER(yank_instance_list
);
47 static bool yank_instance_equal(const YankInstance
*a
, const YankInstance
*b
)
49 if (a
->type
!= b
->type
) {
54 case YANK_INSTANCE_TYPE_BLOCK_NODE
:
55 return g_str_equal(a
->u
.block_node
.node_name
,
56 b
->u
.block_node
.node_name
);
58 case YANK_INSTANCE_TYPE_CHARDEV
:
59 return g_str_equal(a
->u
.chardev
.id
, b
->u
.chardev
.id
);
61 case YANK_INSTANCE_TYPE_MIGRATION
:
69 static YankInstanceEntry
*yank_find_entry(const YankInstance
*instance
)
71 YankInstanceEntry
*entry
;
73 QLIST_FOREACH(entry
, &yank_instance_list
, next
) {
74 if (yank_instance_equal(entry
->instance
, instance
)) {
81 bool yank_register_instance(const YankInstance
*instance
, Error
**errp
)
83 YankInstanceEntry
*entry
;
85 QEMU_LOCK_GUARD(&yank_lock
);
87 if (yank_find_entry(instance
)) {
88 error_setg(errp
, "duplicate yank instance");
92 entry
= g_new0(YankInstanceEntry
, 1);
93 entry
->instance
= QAPI_CLONE(YankInstance
, instance
);
94 QLIST_INIT(&entry
->yankfns
);
95 QLIST_INSERT_HEAD(&yank_instance_list
, entry
, next
);
100 void yank_unregister_instance(const YankInstance
*instance
)
102 YankInstanceEntry
*entry
;
104 QEMU_LOCK_GUARD(&yank_lock
);
105 entry
= yank_find_entry(instance
);
108 assert(QLIST_EMPTY(&entry
->yankfns
));
109 QLIST_REMOVE(entry
, next
);
110 qapi_free_YankInstance(entry
->instance
);
114 void yank_register_function(const YankInstance
*instance
,
118 YankInstanceEntry
*entry
;
119 YankFuncAndParam
*func_entry
;
121 QEMU_LOCK_GUARD(&yank_lock
);
122 entry
= yank_find_entry(instance
);
125 func_entry
= g_new0(YankFuncAndParam
, 1);
126 func_entry
->func
= func
;
127 func_entry
->opaque
= opaque
;
129 QLIST_INSERT_HEAD(&entry
->yankfns
, func_entry
, next
);
132 void yank_unregister_function(const YankInstance
*instance
,
136 YankInstanceEntry
*entry
;
137 YankFuncAndParam
*func_entry
;
139 QEMU_LOCK_GUARD(&yank_lock
);
140 entry
= yank_find_entry(instance
);
143 QLIST_FOREACH(func_entry
, &entry
->yankfns
, next
) {
144 if (func_entry
->func
== func
&& func_entry
->opaque
== opaque
) {
145 QLIST_REMOVE(func_entry
, next
);
154 void yank_generic_iochannel(void *opaque
)
156 QIOChannel
*ioc
= QIO_CHANNEL(opaque
);
158 qio_channel_shutdown(ioc
, QIO_CHANNEL_SHUTDOWN_BOTH
, NULL
);
161 void qmp_yank(YankInstanceList
*instances
,
164 YankInstanceList
*tail
;
165 YankInstanceEntry
*entry
;
166 YankFuncAndParam
*func_entry
;
168 QEMU_LOCK_GUARD(&yank_lock
);
169 for (tail
= instances
; tail
; tail
= tail
->next
) {
170 entry
= yank_find_entry(tail
->value
);
172 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
, "Instance not found");
176 for (tail
= instances
; tail
; tail
= tail
->next
) {
177 entry
= yank_find_entry(tail
->value
);
179 QLIST_FOREACH(func_entry
, &entry
->yankfns
, next
) {
180 func_entry
->func(func_entry
->opaque
);
185 YankInstanceList
*qmp_query_yank(Error
**errp
)
187 YankInstanceEntry
*entry
;
188 YankInstanceList
*ret
;
192 QEMU_LOCK_GUARD(&yank_lock
);
193 QLIST_FOREACH(entry
, &yank_instance_list
, next
) {
194 YankInstanceList
*new_entry
;
195 new_entry
= g_new0(YankInstanceList
, 1);
196 new_entry
->value
= QAPI_CLONE(YankInstance
, entry
->instance
);
197 new_entry
->next
= ret
;
204 static void __attribute__((__constructor__
)) yank_init(void)
206 qemu_mutex_init(&yank_lock
);