hv_fcopy_daemon.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. #include <getopt.h>
  35. static int target_fd;
  36. static char target_fname[W_MAX_PATH];
  37. static int hv_start_fcopy(struct hv_start_fcopy *smsg)
  38. {
  39. int error = HV_E_FAIL;
  40. char *q, *p;
  41. p = (char *)smsg->path_name;
  42. snprintf(target_fname, sizeof(target_fname), "%s/%s",
  43. (char *)smsg->path_name, (char *)smsg->file_name);
  44. syslog(LOG_INFO, "Target file name: %s", target_fname);
  45. /*
  46. * Check to see if the path is already in place; if not,
  47. * create if required.
  48. */
  49. while ((q = strchr(p, '/')) != NULL) {
  50. if (q == p) {
  51. p++;
  52. continue;
  53. }
  54. *q = '\0';
  55. if (access((char *)smsg->path_name, F_OK)) {
  56. if (smsg->copy_flags & CREATE_PATH) {
  57. if (mkdir((char *)smsg->path_name, 0755)) {
  58. syslog(LOG_ERR, "Failed to create %s",
  59. (char *)smsg->path_name);
  60. goto done;
  61. }
  62. } else {
  63. syslog(LOG_ERR, "Invalid path: %s",
  64. (char *)smsg->path_name);
  65. goto done;
  66. }
  67. }
  68. p = q + 1;
  69. *q = '/';
  70. }
  71. if (!access(target_fname, F_OK)) {
  72. syslog(LOG_INFO, "File: %s exists", target_fname);
  73. if (!(smsg->copy_flags & OVER_WRITE)) {
  74. error = HV_ERROR_ALREADY_EXISTS;
  75. goto done;
  76. }
  77. }
  78. target_fd = open(target_fname,
  79. O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0744);
  80. if (target_fd == -1) {
  81. syslog(LOG_INFO, "Open Failed: %s", strerror(errno));
  82. goto done;
  83. }
  84. error = 0;
  85. done:
  86. return error;
  87. }
  88. static int hv_copy_data(struct hv_do_fcopy *cpmsg)
  89. {
  90. ssize_t bytes_written;
  91. bytes_written = pwrite(target_fd, cpmsg->data, cpmsg->size,
  92. cpmsg->offset);
  93. if (bytes_written != cpmsg->size)
  94. return HV_E_FAIL;
  95. return 0;
  96. }
  97. static int hv_copy_finished(void)
  98. {
  99. close(target_fd);
  100. return 0;
  101. }
  102. static int hv_copy_cancel(void)
  103. {
  104. close(target_fd);
  105. unlink(target_fname);
  106. return 0;
  107. }
  108. void print_usage(char *argv[])
  109. {
  110. fprintf(stderr, "Usage: %s [options]\n"
  111. "Options are:\n"
  112. " -n, --no-daemon stay in foreground, don't daemonize\n"
  113. " -h, --help print this help\n", argv[0]);
  114. }
  115. int main(int argc, char *argv[])
  116. {
  117. int fcopy_fd, len;
  118. int error;
  119. int daemonize = 1, long_index = 0, opt;
  120. int version = FCOPY_CURRENT_VERSION;
  121. char *buffer[4096 * 2];
  122. struct hv_fcopy_hdr *in_msg;
  123. int in_handshake = 1;
  124. __u32 kernel_modver;
  125. static struct option long_options[] = {
  126. {"help", no_argument, 0, 'h' },
  127. {"no-daemon", no_argument, 0, 'n' },
  128. {0, 0, 0, 0 }
  129. };
  130. while ((opt = getopt_long(argc, argv, "hn", long_options,
  131. &long_index)) != -1) {
  132. switch (opt) {
  133. case 'n':
  134. daemonize = 0;
  135. break;
  136. case 'h':
  137. default:
  138. print_usage(argv);
  139. exit(EXIT_FAILURE);
  140. }
  141. }
  142. if (daemonize && daemon(1, 0)) {
  143. syslog(LOG_ERR, "daemon() failed; error: %s", strerror(errno));
  144. exit(EXIT_FAILURE);
  145. }
  146. openlog("HV_FCOPY", 0, LOG_USER);
  147. syslog(LOG_INFO, "HV_FCOPY starting; pid is:%d", getpid());
  148. fcopy_fd = open("/dev/vmbus/hv_fcopy", O_RDWR);
  149. if (fcopy_fd < 0) {
  150. syslog(LOG_ERR, "open /dev/vmbus/hv_fcopy failed; error: %d %s",
  151. errno, strerror(errno));
  152. exit(EXIT_FAILURE);
  153. }
  154. /*
  155. * Register with the kernel.
  156. */
  157. if ((write(fcopy_fd, &version, sizeof(int))) != sizeof(int)) {
  158. syslog(LOG_ERR, "Registration failed: %s", strerror(errno));
  159. exit(EXIT_FAILURE);
  160. }
  161. while (1) {
  162. /*
  163. * In this loop we process fcopy messages after the
  164. * handshake is complete.
  165. */
  166. len = pread(fcopy_fd, buffer, (4096 * 2), 0);
  167. if (len < 0) {
  168. syslog(LOG_ERR, "pread failed: %s", strerror(errno));
  169. exit(EXIT_FAILURE);
  170. }
  171. if (in_handshake) {
  172. if (len != sizeof(kernel_modver)) {
  173. syslog(LOG_ERR, "invalid version negotiation");
  174. exit(EXIT_FAILURE);
  175. }
  176. kernel_modver = *(__u32 *)buffer;
  177. in_handshake = 0;
  178. syslog(LOG_INFO, "HV_FCOPY: kernel module version: %d",
  179. kernel_modver);
  180. continue;
  181. }
  182. in_msg = (struct hv_fcopy_hdr *)buffer;
  183. switch (in_msg->operation) {
  184. case START_FILE_COPY:
  185. error = hv_start_fcopy((struct hv_start_fcopy *)in_msg);
  186. break;
  187. case WRITE_TO_FILE:
  188. error = hv_copy_data((struct hv_do_fcopy *)in_msg);
  189. break;
  190. case COMPLETE_FCOPY:
  191. error = hv_copy_finished();
  192. break;
  193. case CANCEL_FCOPY:
  194. error = hv_copy_cancel();
  195. break;
  196. default:
  197. syslog(LOG_ERR, "Unknown operation: %d",
  198. in_msg->operation);
  199. }
  200. if (pwrite(fcopy_fd, &error, sizeof(int), 0) != sizeof(int)) {
  201. syslog(LOG_ERR, "pwrite failed: %s", strerror(errno));
  202. exit(EXIT_FAILURE);
  203. }
  204. }
  205. }