hv_fcopy_daemon.c 5.4 KB

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