2 * Global State configuration
4 * Copyright (c) 2014-2017 Red Hat Inc
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.
13 #include "qemu/osdep.h"
14 #include "qemu/cutils.h"
15 #include "qemu/error-report.h"
16 #include "sysemu/runstate.h"
17 #include "qapi/error.h"
18 #include "migration.h"
19 #include "migration/global_state.h"
20 #include "migration/vmstate.h"
25 uint8_t runstate[100];
30 static GlobalState global_state;
32 int global_state_store(void)
34 if (!runstate_store((char *)global_state.runstate,
35 sizeof(global_state.runstate))) {
36 error_report("runstate name too big: %s", global_state.runstate);
37 trace_migrate_state_too_big();
43 void global_state_store_running(void)
45 const char *state = RunState_str(RUN_STATE_RUNNING);
46 assert(strlen(state) < sizeof(global_state.runstate));
47 strncpy((char *)global_state.runstate,
48 state, sizeof(global_state.runstate));
51 bool global_state_received(void)
53 return global_state.received;
56 RunState global_state_get_runstate(void)
58 return global_state.state;
61 static bool global_state_needed(void *opaque)
63 GlobalState *s = opaque;
64 char *runstate = (char *)s->runstate;
66 /* If it is not optional, it is mandatory */
68 if (migrate_get_current()->store_global_state) {
72 /* If state is running or paused, it is not needed */
74 if (strcmp(runstate, "running") == 0 ||
75 strcmp(runstate, "paused") == 0) {
79 /* for any other state it is needed */
83 static int global_state_post_load(void *opaque, int version_id)
85 GlobalState *s = opaque;
86 Error *local_err = NULL;
88 char *runstate = (char *)s->runstate;
91 trace_migrate_global_state_post_load(runstate);
93 if (strnlen((char *)s->runstate,
94 sizeof(s->runstate)) == sizeof(s->runstate)) {
96 * This condition should never happen during migration, because
97 * all runstate names are shorter than 100 bytes (the size of
98 * s->runstate). However, a malicious stream could overflow
99 * the qapi_enum_parse() call, so we force the last character
102 s->runstate[sizeof(s->runstate) - 1] = '\0';
104 r = qapi_enum_parse(&RunState_lookup, runstate, -1, &local_err);
108 error_report_err(local_err);
117 static int global_state_pre_save(void *opaque)
119 GlobalState *s = opaque;
121 trace_migrate_global_state_pre_save((char *)s->runstate);
122 s->size = strnlen((char *)s->runstate, sizeof(s->runstate)) + 1;
123 assert(s->size <= sizeof(s->runstate));
128 static const VMStateDescription vmstate_globalstate = {
129 .name = "globalstate",
131 .minimum_version_id = 1,
132 .post_load = global_state_post_load,
133 .pre_save = global_state_pre_save,
134 .needed = global_state_needed,
135 .fields = (VMStateField[]) {
136 VMSTATE_UINT32(size, GlobalState),
137 VMSTATE_BUFFER(runstate, GlobalState),
138 VMSTATE_END_OF_LIST()
142 void register_global_state(void)
144 /* We would use it independently that we receive it */
145 strcpy((char *)&global_state.runstate, "");
146 global_state.received = false;
147 vmstate_register(NULL, 0, &vmstate_globalstate, &global_state);