hv_fcopy_daemon.c 5.0 KB

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