2 * An implementation of host to guest copy functionality for Linux.
4 * Copyright (C) 2014, Microsoft, Inc.
6 * Author : K. Y. Srinivasan <kys@microsoft.com>
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.
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
20 #include <sys/types.h>
21 #include <sys/socket.h>
23 #include <linux/types.h>
24 #include <linux/kdev_t.h>
31 #include <linux/hyperv.h>
39 static char target_fname[W_MAX_PATH];
41 static int hv_start_fcopy(struct hv_start_fcopy *smsg)
43 int error = HV_E_FAIL;
47 * If possile append a path seperator to the path.
49 if (strlen((char *)smsg->path_name) < (W_MAX_PATH - 2))
50 strcat((char *)smsg->path_name, "/");
52 p = (char *)smsg->path_name;
53 snprintf(target_fname, sizeof(target_fname), "%s/%s",
54 (char *)smsg->path_name, smsg->file_name);
56 syslog(LOG_INFO, "Target file name: %s", target_fname);
58 * Check to see if the path is already in place; if not,
61 while ((q = strchr(p, '/')) != NULL) {
67 if (access((char *)smsg->path_name, F_OK)) {
68 if (smsg->copy_flags & CREATE_PATH) {
69 if (mkdir((char *)smsg->path_name, 0755)) {
70 syslog(LOG_ERR, "Failed to create %s",
71 (char *)smsg->path_name);
75 syslog(LOG_ERR, "Invalid path: %s",
76 (char *)smsg->path_name);
84 if (!access(target_fname, F_OK)) {
85 syslog(LOG_INFO, "File: %s exists", target_fname);
86 if (!(smsg->copy_flags & OVER_WRITE)) {
87 error = HV_ERROR_ALREADY_EXISTS;
92 target_fd = open(target_fname,
93 O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0744);
94 if (target_fd == -1) {
95 syslog(LOG_INFO, "Open Failed: %s", strerror(errno));
104 static int hv_copy_data(struct hv_do_fcopy *cpmsg)
106 ssize_t bytes_written;
108 bytes_written = pwrite(target_fd, cpmsg->data, cpmsg->size,
111 if (bytes_written != cpmsg->size)
117 static int hv_copy_finished(void)
122 static int hv_copy_cancel(void)
125 unlink(target_fname);
130 void print_usage(char *argv[])
132 fprintf(stderr, "Usage: %s [options]\n"
134 " -n, --no-daemon stay in foreground, don't daemonize\n"
135 " -h, --help print this help\n", argv[0]);
138 int main(int argc, char *argv[])
140 int fd, fcopy_fd, len;
142 int daemonize = 1, long_index = 0, opt;
143 int version = FCOPY_CURRENT_VERSION;
144 char *buffer[4096 * 2];
145 struct hv_fcopy_hdr *in_msg;
147 static struct option long_options[] = {
148 {"help", no_argument, 0, 'h' },
149 {"no-daemon", no_argument, 0, 'n' },
153 while ((opt = getopt_long(argc, argv, "hn", long_options,
154 &long_index)) != -1) {
166 if (daemonize && daemon(1, 0)) {
167 syslog(LOG_ERR, "daemon() failed; error: %s", strerror(errno));
171 openlog("HV_FCOPY", 0, LOG_USER);
172 syslog(LOG_INFO, "HV_FCOPY starting; pid is:%d", getpid());
174 fcopy_fd = open("/dev/vmbus/hv_fcopy", O_RDWR);
177 syslog(LOG_ERR, "open /dev/vmbus/hv_fcopy failed; error: %d %s",
178 errno, strerror(errno));
183 * Register with the kernel.
185 if ((write(fcopy_fd, &version, sizeof(int))) != sizeof(int)) {
186 syslog(LOG_ERR, "Registration failed: %s", strerror(errno));
192 * In this loop we process fcopy messages after the
193 * handshake is complete.
195 len = pread(fcopy_fd, buffer, (4096 * 2), 0);
197 syslog(LOG_ERR, "pread failed: %s", strerror(errno));
200 in_msg = (struct hv_fcopy_hdr *)buffer;
202 switch (in_msg->operation) {
203 case START_FILE_COPY:
204 error = hv_start_fcopy((struct hv_start_fcopy *)in_msg);
207 error = hv_copy_data((struct hv_do_fcopy *)in_msg);
210 error = hv_copy_finished();
213 error = hv_copy_cancel();
217 syslog(LOG_ERR, "Unknown operation: %d",
222 if (pwrite(fcopy_fd, &error, sizeof(int), 0) != sizeof(int)) {
223 syslog(LOG_ERR, "pwrite failed: %s", strerror(errno));