hv_fcopy_daemon.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * An implementation of host to guest copy functionality for Linux.
  4. *
  5. * Copyright (C) 2014, Microsoft, Inc.
  6. *
  7. * Author : K. Y. Srinivasan <kys@microsoft.com>
  8. */
  9. #include <sys/types.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #include <string.h>
  14. #include <errno.h>
  15. #include <linux/hyperv.h>
  16. #include <linux/limits.h>
  17. #include <syslog.h>
  18. #include <sys/stat.h>
  19. #include <fcntl.h>
  20. #include <getopt.h>
  21. static int target_fd;
  22. static char target_fname[PATH_MAX];
  23. static unsigned long long filesize;
  24. static int hv_start_fcopy(struct hv_start_fcopy *smsg)
  25. {
  26. int error = HV_E_FAIL;
  27. char *q, *p;
  28. filesize = 0;
  29. p = (char *)smsg->path_name;
  30. snprintf(target_fname, sizeof(target_fname), "%s/%s",
  31. (char *)smsg->path_name, (char *)smsg->file_name);
  32. syslog(LOG_INFO, "Target file name: %s", target_fname);
  33. /*
  34. * Check to see if the path is already in place; if not,
  35. * create if required.
  36. */
  37. while ((q = strchr(p, '/')) != NULL) {
  38. if (q == p) {
  39. p++;
  40. continue;
  41. }
  42. *q = '\0';
  43. if (access((char *)smsg->path_name, F_OK)) {
  44. if (smsg->copy_flags & CREATE_PATH) {
  45. if (mkdir((char *)smsg->path_name, 0755)) {
  46. syslog(LOG_ERR, "Failed to create %s",
  47. (char *)smsg->path_name);
  48. goto done;
  49. }
  50. } else {
  51. syslog(LOG_ERR, "Invalid path: %s",
  52. (char *)smsg->path_name);
  53. goto done;
  54. }
  55. }
  56. p = q + 1;
  57. *q = '/';
  58. }
  59. if (!access(target_fname, F_OK)) {
  60. syslog(LOG_INFO, "File: %s exists", target_fname);
  61. if (!(smsg->copy_flags & OVER_WRITE)) {
  62. error = HV_ERROR_ALREADY_EXISTS;
  63. goto done;
  64. }
  65. }
  66. target_fd = open(target_fname,
  67. O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0744);
  68. if (target_fd == -1) {
  69. syslog(LOG_INFO, "Open Failed: %s", strerror(errno));
  70. goto done;
  71. }
  72. error = 0;
  73. done:
  74. return error;
  75. }
  76. static int hv_copy_data(struct hv_do_fcopy *cpmsg)
  77. {
  78. ssize_t bytes_written;
  79. int ret = 0;
  80. bytes_written = pwrite(target_fd, cpmsg->data, cpmsg->size,
  81. cpmsg->offset);
  82. filesize += cpmsg->size;
  83. if (bytes_written != cpmsg->size) {
  84. switch (errno) {
  85. case ENOSPC:
  86. ret = HV_ERROR_DISK_FULL;
  87. break;
  88. default:
  89. ret = HV_E_FAIL;
  90. break;
  91. }
  92. syslog(LOG_ERR, "pwrite failed to write %llu bytes: %ld (%s)",
  93. filesize, (long)bytes_written, strerror(errno));
  94. }
  95. return ret;
  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;
  118. int error;
  119. int daemonize = 1, long_index = 0, opt;
  120. int version = FCOPY_CURRENT_VERSION;
  121. union {
  122. struct hv_fcopy_hdr hdr;
  123. struct hv_start_fcopy start;
  124. struct hv_do_fcopy copy;
  125. __u32 kernel_modver;
  126. } buffer = { };
  127. int in_handshake = 1;
  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, "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. ssize_t len;
  170. len = pread(fcopy_fd, &buffer, sizeof(buffer), 0);
  171. if (len < 0) {
  172. syslog(LOG_ERR, "pread failed: %s", strerror(errno));
  173. exit(EXIT_FAILURE);
  174. }
  175. if (in_handshake) {
  176. if (len != sizeof(buffer.kernel_modver)) {
  177. syslog(LOG_ERR, "invalid version negotiation");
  178. exit(EXIT_FAILURE);
  179. }
  180. in_handshake = 0;
  181. syslog(LOG_INFO, "kernel module version: %u",
  182. buffer.kernel_modver);
  183. continue;
  184. }
  185. switch (buffer.hdr.operation) {
  186. case START_FILE_COPY:
  187. error = hv_start_fcopy(&buffer.start);
  188. break;
  189. case WRITE_TO_FILE:
  190. error = hv_copy_data(&buffer.copy);
  191. break;
  192. case COMPLETE_FCOPY:
  193. error = hv_copy_finished();
  194. break;
  195. case CANCEL_FCOPY:
  196. error = hv_copy_cancel();
  197. break;
  198. default:
  199. error = HV_E_FAIL;
  200. syslog(LOG_ERR, "Unknown operation: %d",
  201. buffer.hdr.operation);
  202. }
  203. if (pwrite(fcopy_fd, &error, sizeof(int), 0) != sizeof(int)) {
  204. syslog(LOG_ERR, "pwrite failed: %s", strerror(errno));
  205. exit(EXIT_FAILURE);
  206. }
  207. }
  208. }