migration-exec.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * QEMU live migration
  3. *
  4. * Copyright IBM, Corp. 2008
  5. * Copyright Dell MessageOne 2008
  6. *
  7. * Authors:
  8. * Anthony Liguori <aliguori@us.ibm.com>
  9. * Charles Duffy <charles_duffy@messageone.com>
  10. *
  11. * This work is licensed under the terms of the GNU GPL, version 2. See
  12. * the COPYING file in the top-level directory.
  13. *
  14. */
  15. #include "qemu-common.h"
  16. #include "qemu_socket.h"
  17. #include "migration.h"
  18. #include "qemu-char.h"
  19. #include "buffered_file.h"
  20. #include "block.h"
  21. #include <sys/types.h>
  22. #include <sys/wait.h>
  23. //#define DEBUG_MIGRATION_EXEC
  24. #ifdef DEBUG_MIGRATION_EXEC
  25. #define DPRINTF(fmt, ...) \
  26. do { printf("migration-exec: " fmt, ## __VA_ARGS__); } while (0)
  27. #else
  28. #define DPRINTF(fmt, ...) \
  29. do { } while (0)
  30. #endif
  31. static int file_errno(FdMigrationState *s)
  32. {
  33. return errno;
  34. }
  35. static int file_write(FdMigrationState *s, const void * buf, size_t size)
  36. {
  37. return write(s->fd, buf, size);
  38. }
  39. static int exec_close(FdMigrationState *s)
  40. {
  41. int ret = 0;
  42. DPRINTF("exec_close\n");
  43. if (s->opaque) {
  44. ret = qemu_fclose(s->opaque);
  45. s->opaque = NULL;
  46. s->fd = -1;
  47. if (ret != -1 &&
  48. WIFEXITED(ret)
  49. && WEXITSTATUS(ret) == 0) {
  50. ret = 0;
  51. } else {
  52. ret = -1;
  53. }
  54. }
  55. return ret;
  56. }
  57. MigrationState *exec_start_outgoing_migration(Monitor *mon,
  58. const char *command,
  59. int64_t bandwidth_limit,
  60. int detach,
  61. int blk,
  62. int inc)
  63. {
  64. FdMigrationState *s;
  65. FILE *f;
  66. s = qemu_mallocz(sizeof(*s));
  67. f = popen(command, "w");
  68. if (f == NULL) {
  69. DPRINTF("Unable to popen exec target\n");
  70. goto err_after_alloc;
  71. }
  72. s->fd = fileno(f);
  73. if (s->fd == -1) {
  74. DPRINTF("Unable to retrieve file descriptor for popen'd handle\n");
  75. goto err_after_open;
  76. }
  77. socket_set_nonblock(s->fd);
  78. s->opaque = qemu_popen(f, "w");
  79. s->close = exec_close;
  80. s->get_error = file_errno;
  81. s->write = file_write;
  82. s->mig_state.cancel = migrate_fd_cancel;
  83. s->mig_state.get_status = migrate_fd_get_status;
  84. s->mig_state.release = migrate_fd_release;
  85. s->mig_state.blk = blk;
  86. s->mig_state.shared = inc;
  87. s->state = MIG_STATE_ACTIVE;
  88. s->mon = NULL;
  89. s->bandwidth_limit = bandwidth_limit;
  90. if (!detach) {
  91. migrate_fd_monitor_suspend(s, mon);
  92. }
  93. migrate_fd_connect(s);
  94. return &s->mig_state;
  95. err_after_open:
  96. pclose(f);
  97. err_after_alloc:
  98. qemu_free(s);
  99. return NULL;
  100. }
  101. static void exec_accept_incoming_migration(void *opaque)
  102. {
  103. QEMUFile *f = opaque;
  104. process_incoming_migration(f);
  105. qemu_set_fd_handler2(qemu_stdio_fd(f), NULL, NULL, NULL, NULL);
  106. qemu_fclose(f);
  107. }
  108. int exec_start_incoming_migration(const char *command)
  109. {
  110. QEMUFile *f;
  111. DPRINTF("Attempting to start an incoming migration\n");
  112. f = qemu_popen_cmd(command, "r");
  113. if(f == NULL) {
  114. DPRINTF("Unable to apply qemu wrapper to popen file\n");
  115. return -errno;
  116. }
  117. qemu_set_fd_handler2(qemu_stdio_fd(f), NULL,
  118. exec_accept_incoming_migration, NULL, f);
  119. return 0;
  120. }