hv_fcopy_daemon.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. static struct option long_options[] = {
  124. {"help", no_argument, 0, 'h' },
  125. {"no-daemon", no_argument, 0, 'n' },
  126. {0, 0, 0, 0 }
  127. };
  128. while ((opt = getopt_long(argc, argv, "hn", long_options,
  129. &long_index)) != -1) {
  130. switch (opt) {
  131. case 'n':
  132. daemonize = 0;
  133. break;
  134. case 'h':
  135. default:
  136. print_usage(argv);
  137. exit(EXIT_FAILURE);
  138. }
  139. }
  140. if (daemonize && daemon(1, 0)) {
  141. syslog(LOG_ERR, "daemon() failed; error: %s", strerror(errno));
  142. exit(EXIT_FAILURE);
  143. }
  144. openlog("HV_FCOPY", 0, LOG_USER);
  145. syslog(LOG_INFO, "HV_FCOPY starting; pid is:%d", getpid());
  146. fcopy_fd = open("/dev/vmbus/hv_fcopy", O_RDWR);
  147. if (fcopy_fd < 0) {
  148. syslog(LOG_ERR, "open /dev/vmbus/hv_fcopy failed; error: %d %s",
  149. errno, strerror(errno));
  150. exit(EXIT_FAILURE);
  151. }
  152. /*
  153. * Register with the kernel.
  154. */
  155. if ((write(fcopy_fd, &version, sizeof(int))) != sizeof(int)) {
  156. syslog(LOG_ERR, "Registration failed: %s", strerror(errno));
  157. exit(EXIT_FAILURE);
  158. }
  159. while (1) {
  160. /*
  161. * In this loop we process fcopy messages after the
  162. * handshake is complete.
  163. */
  164. len = pread(fcopy_fd, buffer, (4096 * 2), 0);
  165. if (len < 0) {
  166. syslog(LOG_ERR, "pread failed: %s", strerror(errno));
  167. exit(EXIT_FAILURE);
  168. }
  169. in_msg = (struct hv_fcopy_hdr *)buffer;
  170. switch (in_msg->operation) {
  171. case START_FILE_COPY:
  172. error = hv_start_fcopy((struct hv_start_fcopy *)in_msg);
  173. break;
  174. case WRITE_TO_FILE:
  175. error = hv_copy_data((struct hv_do_fcopy *)in_msg);
  176. break;
  177. case COMPLETE_FCOPY:
  178. error = hv_copy_finished();
  179. break;
  180. case CANCEL_FCOPY:
  181. error = hv_copy_cancel();
  182. break;
  183. default:
  184. syslog(LOG_ERR, "Unknown operation: %d",
  185. in_msg->operation);
  186. }
  187. if (pwrite(fcopy_fd, &error, sizeof(int), 0) != sizeof(int)) {
  188. syslog(LOG_ERR, "pwrite failed: %s", strerror(errno));
  189. exit(EXIT_FAILURE);
  190. }
  191. }
  192. }