exec.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * QEMU live migration
  3. *
  4. * Copyright IBM, Corp. 2008
  5. * Copyright Dell MessageOne 2008
  6. * Copyright Red Hat, Inc. 2015-2016
  7. *
  8. * Authors:
  9. * Anthony Liguori <aliguori@us.ibm.com>
  10. * Charles Duffy <charles_duffy@messageone.com>
  11. * Daniel P. Berrange <berrange@redhat.com>
  12. *
  13. * This work is licensed under the terms of the GNU GPL, version 2. See
  14. * the COPYING file in the top-level directory.
  15. *
  16. * Contributions after 2012-01-13 are licensed under the terms of the
  17. * GNU GPL, version 2 or (at your option) any later version.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "qemu/error-report.h"
  21. #include "channel.h"
  22. #include "exec.h"
  23. #include "migration.h"
  24. #include "io/channel-command.h"
  25. #include "trace.h"
  26. #include "qemu/cutils.h"
  27. #ifdef WIN32
  28. const char *exec_get_cmd_path(void);
  29. const char *exec_get_cmd_path(void)
  30. {
  31. g_autofree char *detected_path = g_new(char, MAX_PATH);
  32. if (GetSystemDirectoryA(detected_path, MAX_PATH) == 0) {
  33. warn_report("Could not detect cmd.exe path, using default.");
  34. return "C:\\Windows\\System32\\cmd.exe";
  35. }
  36. pstrcat(detected_path, MAX_PATH, "\\cmd.exe");
  37. return g_steal_pointer(&detected_path);
  38. }
  39. #endif
  40. void exec_start_outgoing_migration(MigrationState *s, const char *command, Error **errp)
  41. {
  42. QIOChannel *ioc;
  43. #ifdef WIN32
  44. const char *argv[] = { exec_get_cmd_path(), "/c", command, NULL };
  45. #else
  46. const char *argv[] = { "/bin/sh", "-c", command, NULL };
  47. #endif
  48. trace_migration_exec_outgoing(command);
  49. ioc = QIO_CHANNEL(qio_channel_command_new_spawn(argv,
  50. O_RDWR,
  51. errp));
  52. if (!ioc) {
  53. return;
  54. }
  55. qio_channel_set_name(ioc, "migration-exec-outgoing");
  56. migration_channel_connect(s, ioc, NULL, NULL);
  57. object_unref(OBJECT(ioc));
  58. }
  59. static gboolean exec_accept_incoming_migration(QIOChannel *ioc,
  60. GIOCondition condition,
  61. gpointer opaque)
  62. {
  63. migration_channel_process_incoming(ioc);
  64. object_unref(OBJECT(ioc));
  65. return G_SOURCE_REMOVE;
  66. }
  67. void exec_start_incoming_migration(const char *command, Error **errp)
  68. {
  69. QIOChannel *ioc;
  70. #ifdef WIN32
  71. const char *argv[] = { exec_get_cmd_path(), "/c", command, NULL };
  72. #else
  73. const char *argv[] = { "/bin/sh", "-c", command, NULL };
  74. #endif
  75. trace_migration_exec_incoming(command);
  76. ioc = QIO_CHANNEL(qio_channel_command_new_spawn(argv,
  77. O_RDWR,
  78. errp));
  79. if (!ioc) {
  80. return;
  81. }
  82. qio_channel_set_name(ioc, "migration-exec-incoming");
  83. qio_channel_add_watch_full(ioc, G_IO_IN,
  84. exec_accept_incoming_migration,
  85. NULL, NULL,
  86. g_main_context_get_thread_default());
  87. }