migration-exec.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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(MigrationState *s)
  32. {
  33. return errno;
  34. }
  35. static int file_write(MigrationState *s, const void * buf, size_t size)
  36. {
  37. return write(s->fd, buf, size);
  38. }
  39. static int exec_close(MigrationState *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. int exec_start_outgoing_migration(MigrationState *s, const char *command)
  58. {
  59. FILE *f;
  60. f = popen(command, "w");
  61. if (f == NULL) {
  62. DPRINTF("Unable to popen exec target\n");
  63. goto err_after_popen;
  64. }
  65. s->fd = fileno(f);
  66. if (s->fd == -1) {
  67. DPRINTF("Unable to retrieve file descriptor for popen'd handle\n");
  68. goto err_after_open;
  69. }
  70. socket_set_nonblock(s->fd);
  71. s->opaque = qemu_popen(f, "w");
  72. s->close = exec_close;
  73. s->get_error = file_errno;
  74. s->write = file_write;
  75. migrate_fd_connect(s);
  76. return 0;
  77. err_after_open:
  78. pclose(f);
  79. err_after_popen:
  80. return -1;
  81. }
  82. static void exec_accept_incoming_migration(void *opaque)
  83. {
  84. QEMUFile *f = opaque;
  85. process_incoming_migration(f);
  86. qemu_set_fd_handler2(qemu_stdio_fd(f), NULL, NULL, NULL, NULL);
  87. qemu_fclose(f);
  88. }
  89. int exec_start_incoming_migration(const char *command)
  90. {
  91. QEMUFile *f;
  92. DPRINTF("Attempting to start an incoming migration\n");
  93. f = qemu_popen_cmd(command, "r");
  94. if(f == NULL) {
  95. DPRINTF("Unable to apply qemu wrapper to popen file\n");
  96. return -errno;
  97. }
  98. qemu_set_fd_handler2(qemu_stdio_fd(f), NULL,
  99. exec_accept_incoming_migration, NULL, f);
  100. return 0;
  101. }