2 * Global State configuration
4 * Copyright (c) 2014-2017 Red Hat Inc
7 * Juan Quintela <quintela@redhat.com>
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 "qapi/error.h"
17 #include "migration.h"
18 #include "migration/global_state.h"
19 #include "migration/vmstate.h"
24 uint8_t runstate
[100];
29 static GlobalState global_state
;
31 int global_state_store(void)
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();
42 void global_state_store_running(void)
44 const char *state
= RunState_str(RUN_STATE_RUNNING
);
45 strncpy((char *)global_state
.runstate
,
46 state
, sizeof(global_state
.runstate
));
49 bool global_state_received(void)
51 return global_state
.received
;
54 RunState
global_state_get_runstate(void)
56 return global_state
.state
;
59 static bool global_state_needed(void *opaque
)
61 GlobalState
*s
= opaque
;
62 char *runstate
= (char *)s
->runstate
;
64 /* If it is not optional, it is mandatory */
66 if (migrate_get_current()->store_global_state
) {
70 /* If state is running or paused, it is not needed */
72 if (strcmp(runstate
, "running") == 0 ||
73 strcmp(runstate
, "paused") == 0) {
77 /* for any other state it is needed */
81 static int global_state_post_load(void *opaque
, int version_id
)
83 GlobalState
*s
= opaque
;
84 Error
*local_err
= NULL
;
86 char *runstate
= (char *)s
->runstate
;
89 trace_migrate_global_state_post_load(runstate
);
91 r
= qapi_enum_parse(&RunState_lookup
, runstate
, -1, &local_err
);
95 error_report_err(local_err
);
104 static void global_state_pre_save(void *opaque
)
106 GlobalState
*s
= opaque
;
108 trace_migrate_global_state_pre_save((char *)s
->runstate
);
109 s
->size
= strlen((char *)s
->runstate
) + 1;
112 static const VMStateDescription vmstate_globalstate
= {
113 .name
= "globalstate",
115 .minimum_version_id
= 1,
116 .post_load
= global_state_post_load
,
117 .pre_save
= global_state_pre_save
,
118 .needed
= global_state_needed
,
119 .fields
= (VMStateField
[]) {
120 VMSTATE_UINT32(size
, GlobalState
),
121 VMSTATE_BUFFER(runstate
, GlobalState
),
122 VMSTATE_END_OF_LIST()
126 void register_global_state(void)
128 /* We would use it independently that we receive it */
129 strcpy((char *)&global_state
.runstate
, "");
130 global_state
.received
= false;
131 vmstate_register(NULL
, 0, &vmstate_globalstate
, &global_state
);