/tools/hv/hv_fcopy_daemon.c

https://bitbucket.org/bdas/linux · C · 195 lines · 137 code · 27 blank · 31 comment · 24 complexity · 634c1951aa7a175b71e429914bdcb59b MD5 · raw file

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