hv_fcopy_daemon.c 5.5 KB

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