qmp-cmds.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * QEMU Management Protocol commands
  3. *
  4. * Copyright IBM, Corp. 2011
  5. *
  6. * Authors:
  7. * Anthony Liguori <aliguori@us.ibm.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2. See
  10. * the COPYING file in the top-level directory.
  11. *
  12. * Contributions after 2012-01-13 are licensed under the terms of the
  13. * GNU GPL, version 2 or (at your option) any later version.
  14. */
  15. #include "qemu/osdep.h"
  16. #include "qemu/sockets.h"
  17. #include "monitor-internal.h"
  18. #include "monitor/qdev.h"
  19. #include "monitor/qmp-helpers.h"
  20. #include "system/system.h"
  21. #include "system/kvm.h"
  22. #include "system/runstate.h"
  23. #include "system/runstate-action.h"
  24. #include "system/block-backend.h"
  25. #include "qapi/error.h"
  26. #include "qapi/qapi-init-commands.h"
  27. #include "qapi/qapi-commands-control.h"
  28. #include "qapi/qapi-commands-misc.h"
  29. #include "qapi/qmp/qerror.h"
  30. #include "qapi/type-helpers.h"
  31. #include "hw/mem/memory-device.h"
  32. #include "hw/intc/intc.h"
  33. #include "migration/misc.h"
  34. NameInfo *qmp_query_name(Error **errp)
  35. {
  36. NameInfo *info = g_malloc0(sizeof(*info));
  37. info->name = g_strdup(qemu_name);
  38. return info;
  39. }
  40. void qmp_quit(Error **errp)
  41. {
  42. shutdown_action = SHUTDOWN_ACTION_POWEROFF;
  43. qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_QMP_QUIT);
  44. }
  45. void qmp_stop(Error **errp)
  46. {
  47. /* if there is a dump in background, we should wait until the dump
  48. * finished */
  49. if (qemu_system_dump_in_progress()) {
  50. error_setg(errp, "There is a dump in process, please wait.");
  51. return;
  52. }
  53. if (runstate_check(RUN_STATE_INMIGRATE)) {
  54. autostart = 0;
  55. } else {
  56. vm_stop(RUN_STATE_PAUSED);
  57. }
  58. }
  59. void qmp_cont(Error **errp)
  60. {
  61. BlockBackend *blk;
  62. BlockJob *job;
  63. Error *local_err = NULL;
  64. /* if there is a dump in background, we should wait until the dump
  65. * finished */
  66. if (qemu_system_dump_in_progress()) {
  67. error_setg(errp, "There is a dump in process, please wait.");
  68. return;
  69. }
  70. if (runstate_needs_reset()) {
  71. error_setg(errp, "Resetting the Virtual Machine is required");
  72. return;
  73. } else if (runstate_check(RUN_STATE_SUSPENDED)) {
  74. return;
  75. } else if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
  76. error_setg(errp, "Migration is not finalized yet");
  77. return;
  78. }
  79. for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
  80. blk_iostatus_reset(blk);
  81. }
  82. WITH_JOB_LOCK_GUARD() {
  83. for (job = block_job_next_locked(NULL); job;
  84. job = block_job_next_locked(job)) {
  85. block_job_iostatus_reset_locked(job);
  86. }
  87. }
  88. if (runstate_check(RUN_STATE_INMIGRATE)) {
  89. autostart = 1;
  90. } else {
  91. /*
  92. * Continuing after completed migration. Images have been
  93. * inactivated to allow the destination to take control. Need to
  94. * get control back now.
  95. */
  96. if (!migration_block_activate(&local_err)) {
  97. error_propagate(errp, local_err);
  98. return;
  99. }
  100. vm_start();
  101. }
  102. }
  103. void qmp_add_client(const char *protocol, const char *fdname,
  104. bool has_skipauth, bool skipauth, bool has_tls, bool tls,
  105. Error **errp)
  106. {
  107. static const struct {
  108. const char *name;
  109. bool (*add_client)(int fd, bool has_skipauth, bool skipauth,
  110. bool has_tls, bool tls, Error **errp);
  111. } protocol_table[] = {
  112. { "spice", qmp_add_client_spice },
  113. #ifdef CONFIG_VNC
  114. { "vnc", qmp_add_client_vnc },
  115. #endif
  116. #ifdef CONFIG_DBUS_DISPLAY
  117. { "@dbus-display", qmp_add_client_dbus_display },
  118. #endif
  119. };
  120. int fd, i;
  121. fd = monitor_get_fd(monitor_cur(), fdname, errp);
  122. if (fd < 0) {
  123. return;
  124. }
  125. if (!fd_is_socket(fd)) {
  126. error_setg(errp, "parameter @fdname must name a socket");
  127. close(fd);
  128. return;
  129. }
  130. for (i = 0; i < ARRAY_SIZE(protocol_table); i++) {
  131. if (!strcmp(protocol, protocol_table[i].name)) {
  132. if (!protocol_table[i].add_client(fd, has_skipauth, skipauth,
  133. has_tls, tls, errp)) {
  134. close(fd);
  135. }
  136. return;
  137. }
  138. }
  139. if (!qmp_add_client_char(fd, has_skipauth, skipauth, has_tls, tls,
  140. protocol, errp)) {
  141. close(fd);
  142. }
  143. }
  144. char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
  145. int64_t cpu_index, Error **errp)
  146. {
  147. char *output = NULL;
  148. MonitorHMP hmp = {};
  149. monitor_data_init(&hmp.common, false, true, false);
  150. if (has_cpu_index) {
  151. int ret = monitor_set_cpu(&hmp.common, cpu_index);
  152. if (ret < 0) {
  153. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
  154. "a CPU number");
  155. goto out;
  156. }
  157. }
  158. handle_hmp_command(&hmp, command_line);
  159. WITH_QEMU_LOCK_GUARD(&hmp.common.mon_lock) {
  160. output = g_strdup(hmp.common.outbuf->str);
  161. }
  162. out:
  163. monitor_data_destroy(&hmp.common);
  164. return output;
  165. }
  166. static void __attribute__((__constructor__)) monitor_init_qmp_commands(void)
  167. {
  168. /*
  169. * Two command lists:
  170. * - qmp_commands contains all QMP commands
  171. * - qmp_cap_negotiation_commands contains just
  172. * "qmp_capabilities", to enforce capability negotiation
  173. */
  174. qmp_init_marshal(&qmp_commands);
  175. qmp_register_command(&qmp_commands, "device_add",
  176. qmp_device_add, 0, 0);
  177. QTAILQ_INIT(&qmp_cap_negotiation_commands);
  178. qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
  179. qmp_marshal_qmp_capabilities,
  180. QCO_ALLOW_PRECONFIG, 0);
  181. }