]> git.proxmox.com Git - mirror_qemu.git/blame - migration/global_state.c
vnc: fix memleak of the "vnc-worker-output" name
[mirror_qemu.git] / migration / global_state.c
CommitLineData
84a899de
JQ
1/*
2 * Global State configuration
3 *
4 * Copyright (c) 2014-2017 Red Hat Inc
5 *
6 * Authors:
7 * Juan Quintela <quintela@redhat.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
11 */
12
13#include "qemu/osdep.h"
14#include "qemu/cutils.h"
15#include "qemu/error-report.h"
16#include "qapi/error.h"
5272298c 17#include "migration.h"
84a899de
JQ
18#include "migration/global_state.h"
19#include "migration/vmstate.h"
84a899de
JQ
20#include "trace.h"
21
22typedef struct {
84a899de
JQ
23 uint32_t size;
24 uint8_t runstate[100];
25 RunState state;
26 bool received;
27} GlobalState;
28
29static GlobalState global_state;
30
31int global_state_store(void)
32{
33 if (!runstate_store((char *)global_state.runstate,
34 sizeof(global_state.runstate))) {
35 error_report("runstate name too big: %s", global_state.runstate);
36 trace_migrate_state_too_big();
37 return -EINVAL;
38 }
39 return 0;
40}
41
42void global_state_store_running(void)
43{
977c736f 44 const char *state = RunState_str(RUN_STATE_RUNNING);
84a899de
JQ
45 strncpy((char *)global_state.runstate,
46 state, sizeof(global_state.runstate));
47}
48
49bool global_state_received(void)
50{
51 return global_state.received;
52}
53
54RunState global_state_get_runstate(void)
55{
56 return global_state.state;
57}
58
84a899de
JQ
59static bool global_state_needed(void *opaque)
60{
61 GlobalState *s = opaque;
62 char *runstate = (char *)s->runstate;
63
64 /* If it is not optional, it is mandatory */
65
5272298c 66 if (migrate_get_current()->store_global_state) {
84a899de
JQ
67 return true;
68 }
69
70 /* If state is running or paused, it is not needed */
71
72 if (strcmp(runstate, "running") == 0 ||
73 strcmp(runstate, "paused") == 0) {
74 return false;
75 }
76
77 /* for any other state it is needed */
78 return true;
79}
80
81static int global_state_post_load(void *opaque, int version_id)
82{
83 GlobalState *s = opaque;
84 Error *local_err = NULL;
85 int r;
86 char *runstate = (char *)s->runstate;
87
88 s->received = true;
89 trace_migrate_global_state_post_load(runstate);
90
f7abe0ec 91 r = qapi_enum_parse(&RunState_lookup, runstate, -1, &local_err);
84a899de
JQ
92
93 if (r == -1) {
94 if (local_err) {
95 error_report_err(local_err);
96 }
97 return -EINVAL;
98 }
99 s->state = r;
100
101 return 0;
102}
103
44b1ff31 104static int global_state_pre_save(void *opaque)
84a899de
JQ
105{
106 GlobalState *s = opaque;
107
108 trace_migrate_global_state_pre_save((char *)s->runstate);
109 s->size = strlen((char *)s->runstate) + 1;
44b1ff31
DDAG
110
111 return 0;
84a899de
JQ
112}
113
114static const VMStateDescription vmstate_globalstate = {
115 .name = "globalstate",
116 .version_id = 1,
117 .minimum_version_id = 1,
118 .post_load = global_state_post_load,
119 .pre_save = global_state_pre_save,
120 .needed = global_state_needed,
121 .fields = (VMStateField[]) {
122 VMSTATE_UINT32(size, GlobalState),
123 VMSTATE_BUFFER(runstate, GlobalState),
124 VMSTATE_END_OF_LIST()
125 },
126};
127
128void register_global_state(void)
129{
130 /* We would use it independently that we receive it */
131 strcpy((char *)&global_state.runstate, "");
132 global_state.received = false;
133 vmstate_register(NULL, 0, &vmstate_globalstate, &global_state);
134}