hv_fcopy_daemon.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * An implementation of host to guest copy functionality for Linux.
  3. *
  4. * Copyright (C) 2014, Microsoft, Inc.
  5. *
  6. * Author : K. Y. Srinivasan <kys@microsoft.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License version 2 as published
  10. * by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  15. * NON INFRINGEMENT. See the GNU General Public License for more
  16. * details.
  17. */
  18. #include <sys/types.h>
  19. #include <sys/socket.h>
  20. #include <sys/poll.h>
  21. #include <linux/types.h>
  22. #include <linux/kdev_t.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <unistd.h>
  26. #include <string.h>
  27. #include <ctype.h>
  28. #include <errno.h>
  29. #include <linux/hyperv.h>
  30. #include <syslog.h>
  31. #include <sys/stat.h>
  32. #include <fcntl.h>
  33. #include <dirent.h>
  34. static int target_fd;
  35. static char target_fname[W_MAX_PATH];
  36. static int hv_start_fcopy(struct hv_start_fcopy *smsg)
  37. {
  38. int error = HV_E_FAIL;
  39. char *q, *p;
  40. /*
  41. * If possile append a path seperator to the path.
  42. */
  43. if (strlen((char *)smsg->path_name) < (W_MAX_PATH - 2))
  44. strcat((char *)smsg->path_name, "/");
  45. p = (char *)smsg->path_name;
  46. snprintf(target_fname, sizeof(target_fname), "%s/%s",
  47. (char *)smsg->path_name, smsg->file_name);
  48. syslog(LOG_INFO, "Target file name: %s", target_fname);
  49. /*
  50. * Check to see if the path is already in place; if not,
  51. * create if required.
  52. */
  53. while ((q = strchr(p, '/')) != NULL) {
  54. if (q == p) {
  55. p++;
  56. continue;
  57. }
  58. *q = '\0';
  59. if (access((char *)smsg->path_name, F_OK)) {
  60. if (smsg->copy_flags & CREATE_PATH) {
  61. if (mkdir((char *)smsg->path_name, 0755)) {
  62. syslog(LOG_ERR, "Failed to create %s",
  63. (char *)smsg->path_name);
  64. goto done;
  65. }
  66. } else {
  67. syslog(LOG_ERR, "Invalid path: %s",
  68. (char *)smsg->path_name);
  69. goto done;
  70. }
  71. }
  72. p = q + 1;
  73. *q = '/';
  74. }
  75. if (!access(target_fname, F_OK)) {
  76. syslog(LOG_INFO, "File: %s exists", target_fname);
  77. if (!(smsg->copy_flags & OVER_WRITE)) {
  78. error = HV_ERROR_ALREADY_EXISTS;
  79. goto done;
  80. }
  81. }
  82. target_fd = open(target_fname, O_RDWR | O_CREAT | O_CLOEXEC, 0744);
  83. if (target_fd == -1) {
  84. syslog(LOG_INFO, "Open Failed: %s", strerror(errno));
  85. goto done;
  86. }
  87. error = 0;
  88. done:
  89. return error;
  90. }
  91. static int hv_copy_data(struct hv_do_fcopy *cpmsg)
  92. {
  93. ssize_t bytes_written;
  94. bytes_written = pwrite(target_fd, cpmsg->data, cpmsg->size,
  95. cpmsg->offset);
  96. if (bytes_written != cpmsg->size)
  97. return HV_E_FAIL;
  98. return 0;
  99. }
  100. static int hv_copy_finished(void)
  101. {
  102. close(target_fd);
  103. return 0;
  104. }
  105. static int hv_copy_cancel(void)
  106. {
  107. close(target_fd);
  108. unlink(target_fname);
  109. return 0;
  110. }
  111. int main(void)
  112. {
  113. int fd, fcopy_fd, len;
  114. int error;
  115. int version = FCOPY_CURRENT_VERSION;
  116. char *buffer[4096 * 2];
  117. struct hv_fcopy_hdr *in_msg;
  118. if (daemon(1, 0)) {
  119. syslog(LOG_ERR, "daemon() failed; error: %s", strerror(errno));
  120. exit(EXIT_FAILURE);
  121. }
  122. openlog("HV_FCOPY", 0, LOG_USER);
  123. syslog(LOG_INFO, "HV_FCOPY starting; pid is:%d", getpid());
  124. fcopy_fd = open("/dev/vmbus/hv_fcopy", O_RDWR);
  125. if (fcopy_fd < 0) {
  126. syslog(LOG_ERR, "open /dev/vmbus/hv_fcopy failed; error: %d %s",
  127. errno, strerror(errno));
  128. exit(EXIT_FAILURE);
  129. }
  130. /*
  131. * Register with the kernel.
  132. */
  133. if ((write(fcopy_fd, &version, sizeof(int))) != sizeof(int)) {
  134. syslog(LOG_ERR, "Registration failed: %s", strerror(errno));
  135. exit(EXIT_FAILURE);
  136. }
  137. while (1) {
  138. /*
  139. * In this loop we process fcopy messages after the
  140. * handshake is complete.
  141. */
  142. len = pread(fcopy_fd, buffer, (4096 * 2), 0);
  143. if (len < 0) {
  144. syslog(LOG_ERR, "pread failed: %s", strerror(errno));
  145. exit(EXIT_FAILURE);
  146. }
  147. in_msg = (struct hv_fcopy_hdr *)buffer;
  148. switch (in_msg->operation) {
  149. case START_FILE_COPY:
  150. error = hv_start_fcopy((struct hv_start_fcopy *)in_msg);
  151. break;
  152. case WRITE_TO_FILE:
  153. error = hv_copy_data((struct hv_do_fcopy *)in_msg);
  154. break;
  155. case COMPLETE_FCOPY:
  156. error = hv_copy_finished();
  157. break;
  158. case CANCEL_FCOPY:
  159. error = hv_copy_cancel();
  160. break;
  161. default:
  162. syslog(LOG_ERR, "Unknown operation: %d",
  163. in_msg->operation);
  164. }
  165. if (pwrite(fcopy_fd, &error, sizeof(int), 0) != sizeof(int)) {
  166. syslog(LOG_ERR, "pwrite failed: %s", strerror(errno));
  167. exit(EXIT_FAILURE);
  168. }
  169. }
  170. }