2
0

migration-exec.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. * Contributions after 2012-01-13 are licensed under the terms of the
  15. * GNU GPL, version 2 or (at your option) any later version.
  16. */
  17. #include "qemu-common.h"
  18. #include "qemu/sockets.h"
  19. #include "migration/migration.h"
  20. #include "migration/qemu-file.h"
  21. #include "block/block.h"
  22. #include <sys/types.h>
  23. #include <sys/wait.h>
  24. //#define DEBUG_MIGRATION_EXEC
  25. #ifdef DEBUG_MIGRATION_EXEC
  26. #define DPRINTF(fmt, ...) \
  27. do { printf("migration-exec: " fmt, ## __VA_ARGS__); } while (0)
  28. #else
  29. #define DPRINTF(fmt, ...) \
  30. do { } while (0)
  31. #endif
  32. static int file_errno(MigrationState *s)
  33. {
  34. return errno;
  35. }
  36. static int file_write(MigrationState *s, const void * buf, size_t size)
  37. {
  38. return write(s->fd, buf, size);
  39. }
  40. static int exec_close(MigrationState *s)
  41. {
  42. int ret = 0;
  43. DPRINTF("exec_close\n");
  44. ret = qemu_fclose(s->opaque);
  45. s->opaque = NULL;
  46. s->fd = -1;
  47. if (ret >= 0 && !(WIFEXITED(ret) && WEXITSTATUS(ret) == 0)) {
  48. /* close succeeded, but non-zero exit code: */
  49. ret = -EIO; /* fake errno value */
  50. }
  51. return ret;
  52. }
  53. void exec_start_outgoing_migration(MigrationState *s, const char *command, Error **errp)
  54. {
  55. FILE *f;
  56. f = popen(command, "w");
  57. if (f == NULL) {
  58. error_setg_errno(errp, errno, "failed to popen the migration target");
  59. return;
  60. }
  61. s->fd = fileno(f);
  62. assert(s->fd != -1);
  63. s->opaque = qemu_popen(f, "w");
  64. s->close = exec_close;
  65. s->get_error = file_errno;
  66. s->write = file_write;
  67. migrate_fd_connect(s);
  68. }
  69. static void exec_accept_incoming_migration(void *opaque)
  70. {
  71. QEMUFile *f = opaque;
  72. qemu_set_fd_handler2(qemu_get_fd(f), NULL, NULL, NULL, NULL);
  73. process_incoming_migration(f);
  74. }
  75. void exec_start_incoming_migration(const char *command, Error **errp)
  76. {
  77. QEMUFile *f;
  78. DPRINTF("Attempting to start an incoming migration\n");
  79. f = qemu_popen_cmd(command, "r");
  80. if(f == NULL) {
  81. error_setg_errno(errp, errno, "failed to popen the migration source");
  82. return;
  83. }
  84. qemu_set_fd_handler2(qemu_get_fd(f), NULL,
  85. exec_accept_incoming_migration, NULL, f);
  86. }