fd.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * QEMU live migration via generic fd
  3. *
  4. * Copyright Red Hat, Inc. 2009-2016
  5. *
  6. * Authors:
  7. * Chris Lalancette <clalance@redhat.com>
  8. * Daniel P. Berrange <berrange@redhat.com>
  9. *
  10. * This work is licensed under the terms of the GNU GPL, version 2. See
  11. * the COPYING file in the top-level directory.
  12. *
  13. * Contributions after 2012-01-13 are licensed under the terms of the
  14. * GNU GPL, version 2 or (at your option) any later version.
  15. */
  16. #include "qemu/osdep.h"
  17. #include "channel.h"
  18. #include "fd.h"
  19. #include "file.h"
  20. #include "migration.h"
  21. #include "monitor/monitor.h"
  22. #include "qemu/error-report.h"
  23. #include "qemu/sockets.h"
  24. #include "io/channel-util.h"
  25. #include "trace.h"
  26. static bool fd_is_pipe(int fd)
  27. {
  28. struct stat statbuf;
  29. if (fstat(fd, &statbuf) == -1) {
  30. return false;
  31. }
  32. return S_ISFIFO(statbuf.st_mode);
  33. }
  34. static bool migration_fd_valid(int fd)
  35. {
  36. if (fd_is_socket(fd)) {
  37. return true;
  38. }
  39. if (fd_is_pipe(fd)) {
  40. return true;
  41. }
  42. return false;
  43. }
  44. void fd_start_outgoing_migration(MigrationState *s, const char *fdname, Error **errp)
  45. {
  46. QIOChannel *ioc;
  47. int fd = monitor_get_fd(monitor_cur(), fdname, errp);
  48. if (fd == -1) {
  49. return;
  50. }
  51. if (!migration_fd_valid(fd)) {
  52. warn_report("fd: migration to a file is deprecated."
  53. " Use file: instead.");
  54. }
  55. trace_migration_fd_outgoing(fd);
  56. ioc = qio_channel_new_fd(fd, errp);
  57. if (!ioc) {
  58. close(fd);
  59. return;
  60. }
  61. qio_channel_set_name(ioc, "migration-fd-outgoing");
  62. migration_channel_connect(s, ioc, NULL, NULL);
  63. object_unref(OBJECT(ioc));
  64. }
  65. static gboolean fd_accept_incoming_migration(QIOChannel *ioc,
  66. GIOCondition condition,
  67. gpointer opaque)
  68. {
  69. migration_channel_process_incoming(ioc);
  70. object_unref(OBJECT(ioc));
  71. return G_SOURCE_REMOVE;
  72. }
  73. void fd_start_incoming_migration(const char *fdname, Error **errp)
  74. {
  75. QIOChannel *ioc;
  76. int fd = monitor_fd_param(monitor_cur(), fdname, errp);
  77. if (fd == -1) {
  78. return;
  79. }
  80. if (!migration_fd_valid(fd)) {
  81. warn_report("fd: migration to a file is deprecated."
  82. " Use file: instead.");
  83. }
  84. trace_migration_fd_incoming(fd);
  85. ioc = qio_channel_new_fd(fd, errp);
  86. if (!ioc) {
  87. close(fd);
  88. return;
  89. }
  90. qio_channel_set_name(ioc, "migration-fd-incoming");
  91. qio_channel_add_watch_full(ioc, G_IO_IN,
  92. fd_accept_incoming_migration,
  93. NULL, NULL,
  94. g_main_context_get_thread_default());
  95. }