hv_fcopy_daemon.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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,
  83. O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0744);
  84. if (target_fd == -1) {
  85. syslog(LOG_INFO, "Open Failed: %s", strerror(errno));
  86. goto done;
  87. }
  88. error = 0;
  89. done:
  90. return error;
  91. }
  92. static int hv_copy_data(struct hv_do_fcopy *cpmsg)
  93. {
  94. ssize_t bytes_written;
  95. bytes_written = pwrite(target_fd, cpmsg->data, cpmsg->size,
  96. cpmsg->offset);
  97. if (bytes_written != cpmsg->size)
  98. return HV_E_FAIL;
  99. return 0;
  100. }
  101. static int hv_copy_finished(void)
  102. {
  103. close(target_fd);
  104. return 0;
  105. }
  106. static int hv_copy_cancel(void)
  107. {
  108. close(target_fd);
  109. unlink(target_fname);
  110. return 0;
  111. }
  112. int main(void)
  113. {
  114. int fd, fcopy_fd, len;
  115. int error;
  116. int version = FCOPY_CURRENT_VERSION;
  117. char *buffer[4096 * 2];
  118. struct hv_fcopy_hdr *in_msg;
  119. if (daemon(1, 0)) {
  120. syslog(LOG_ERR, "daemon() failed; error: %s", strerror(errno));
  121. exit(EXIT_FAILURE);
  122. }
  123. openlog("HV_FCOPY", 0, LOG_USER);
  124. syslog(LOG_INFO, "HV_FCOPY starting; pid is:%d", getpid());
  125. fcopy_fd = open("/dev/vmbus/hv_fcopy", O_RDWR);
  126. if (fcopy_fd < 0) {
  127. syslog(LOG_ERR, "open /dev/vmbus/hv_fcopy failed; error: %d %s",
  128. errno, strerror(errno));
  129. exit(EXIT_FAILURE);
  130. }
  131. /*
  132. * Register with the kernel.
  133. */
  134. if ((write(fcopy_fd, &version, sizeof(int))) != sizeof(int)) {
  135. syslog(LOG_ERR, "Registration failed: %s", strerror(errno));
  136. exit(EXIT_FAILURE);
  137. }
  138. while (1) {
  139. /*
  140. * In this loop we process fcopy messages after the
  141. * handshake is complete.
  142. */
  143. len = pread(fcopy_fd, buffer, (4096 * 2), 0);
  144. if (len < 0) {
  145. syslog(LOG_ERR, "pread failed: %s", strerror(errno));
  146. exit(EXIT_FAILURE);
  147. }
  148. in_msg = (struct hv_fcopy_hdr *)buffer;
  149. switch (in_msg->operation) {
  150. case START_FILE_COPY:
  151. error = hv_start_fcopy((struct hv_start_fcopy *)in_msg);
  152. break;
  153. case WRITE_TO_FILE:
  154. error = hv_copy_data((struct hv_do_fcopy *)in_msg);
  155. break;
  156. case COMPLETE_FCOPY:
  157. error = hv_copy_finished();
  158. break;
  159. case CANCEL_FCOPY:
  160. error = hv_copy_cancel();
  161. break;
  162. default:
  163. syslog(LOG_ERR, "Unknown operation: %d",
  164. in_msg->operation);
  165. }
  166. if (pwrite(fcopy_fd, &error, sizeof(int), 0) != sizeof(int)) {
  167. syslog(LOG_ERR, "pwrite failed: %s", strerror(errno));
  168. exit(EXIT_FAILURE);
  169. }
  170. }
  171. }