global_state.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #include "qemu/osdep.h"
  13. #include "qemu/cutils.h"
  14. #include "qemu/error-report.h"
  15. #include "sysemu/runstate.h"
  16. #include "qapi/error.h"
  17. #include "migration.h"
  18. #include "migration/global_state.h"
  19. #include "migration/vmstate.h"
  20. #include "trace.h"
  21. typedef struct {
  22. uint32_t size;
  23. uint8_t runstate[100];
  24. RunState state;
  25. bool received;
  26. } GlobalState;
  27. static GlobalState global_state;
  28. int global_state_store(void)
  29. {
  30. if (!runstate_store((char *)global_state.runstate,
  31. sizeof(global_state.runstate))) {
  32. error_report("runstate name too big: %s", global_state.runstate);
  33. trace_migrate_state_too_big();
  34. return -EINVAL;
  35. }
  36. return 0;
  37. }
  38. void global_state_store_running(void)
  39. {
  40. const char *state = RunState_str(RUN_STATE_RUNNING);
  41. assert(strlen(state) < sizeof(global_state.runstate));
  42. strncpy((char *)global_state.runstate,
  43. state, sizeof(global_state.runstate));
  44. }
  45. bool global_state_received(void)
  46. {
  47. return global_state.received;
  48. }
  49. RunState global_state_get_runstate(void)
  50. {
  51. return global_state.state;
  52. }
  53. static bool global_state_needed(void *opaque)
  54. {
  55. GlobalState *s = opaque;
  56. char *runstate = (char *)s->runstate;
  57. /* If it is not optional, it is mandatory */
  58. if (migrate_get_current()->store_global_state) {
  59. return true;
  60. }
  61. /* If state is running or paused, it is not needed */
  62. if (strcmp(runstate, "running") == 0 ||
  63. strcmp(runstate, "paused") == 0) {
  64. return false;
  65. }
  66. /* for any other state it is needed */
  67. return true;
  68. }
  69. static int global_state_post_load(void *opaque, int version_id)
  70. {
  71. GlobalState *s = opaque;
  72. Error *local_err = NULL;
  73. int r;
  74. char *runstate = (char *)s->runstate;
  75. s->received = true;
  76. trace_migrate_global_state_post_load(runstate);
  77. if (strnlen((char *)s->runstate,
  78. sizeof(s->runstate)) == sizeof(s->runstate)) {
  79. /*
  80. * This condition should never happen during migration, because
  81. * all runstate names are shorter than 100 bytes (the size of
  82. * s->runstate). However, a malicious stream could overflow
  83. * the qapi_enum_parse() call, so we force the last character
  84. * to a NUL byte.
  85. */
  86. s->runstate[sizeof(s->runstate) - 1] = '\0';
  87. }
  88. r = qapi_enum_parse(&RunState_lookup, runstate, -1, &local_err);
  89. if (r == -1) {
  90. if (local_err) {
  91. error_report_err(local_err);
  92. }
  93. return -EINVAL;
  94. }
  95. s->state = r;
  96. return 0;
  97. }
  98. static int global_state_pre_save(void *opaque)
  99. {
  100. GlobalState *s = opaque;
  101. trace_migrate_global_state_pre_save((char *)s->runstate);
  102. s->size = strnlen((char *)s->runstate, sizeof(s->runstate)) + 1;
  103. assert(s->size <= sizeof(s->runstate));
  104. return 0;
  105. }
  106. static const VMStateDescription vmstate_globalstate = {
  107. .name = "globalstate",
  108. .version_id = 1,
  109. .minimum_version_id = 1,
  110. .post_load = global_state_post_load,
  111. .pre_save = global_state_pre_save,
  112. .needed = global_state_needed,
  113. .fields = (VMStateField[]) {
  114. VMSTATE_UINT32(size, GlobalState),
  115. VMSTATE_BUFFER(runstate, GlobalState),
  116. VMSTATE_END_OF_LIST()
  117. },
  118. };
  119. void register_global_state(void)
  120. {
  121. /* We would use it independently that we receive it */
  122. strcpy((char *)&global_state.runstate, "");
  123. global_state.received = false;
  124. vmstate_register(NULL, 0, &vmstate_globalstate, &global_state);
  125. }