blob: 9445d8f264a4612fb4ebd2b5c4251b999a21a6c3 [file] [log] [blame]
K. Y. Srinivasan013254762014-02-16 11:34:30 -08001/*
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
19
20#include <sys/types.h>
21#include <sys/socket.h>
22#include <sys/poll.h>
23#include <linux/types.h>
24#include <linux/kdev_t.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <unistd.h>
28#include <string.h>
29#include <ctype.h>
30#include <errno.h>
31#include <linux/hyperv.h>
32#include <syslog.h>
33#include <sys/stat.h>
34#include <fcntl.h>
35#include <dirent.h>
Vitaly Kuznetsov170f4be2014-10-22 18:07:11 +020036#include <getopt.h>
K. Y. Srinivasan013254762014-02-16 11:34:30 -080037
38static int target_fd;
39static char target_fname[W_MAX_PATH];
40
41static int hv_start_fcopy(struct hv_start_fcopy *smsg)
42{
43 int error = HV_E_FAIL;
44 char *q, *p;
45
K. Y. Srinivasan013254762014-02-16 11:34:30 -080046 p = (char *)smsg->path_name;
47 snprintf(target_fname, sizeof(target_fname), "%s/%s",
Vitaly Kuznetsovaba474b2015-01-09 22:18:54 -080048 (char *)smsg->path_name, (char *)smsg->file_name);
K. Y. Srinivasan013254762014-02-16 11:34:30 -080049
50 syslog(LOG_INFO, "Target file name: %s", target_fname);
51 /*
52 * Check to see if the path is already in place; if not,
53 * create if required.
54 */
55 while ((q = strchr(p, '/')) != NULL) {
56 if (q == p) {
57 p++;
58 continue;
59 }
60 *q = '\0';
61 if (access((char *)smsg->path_name, F_OK)) {
62 if (smsg->copy_flags & CREATE_PATH) {
63 if (mkdir((char *)smsg->path_name, 0755)) {
64 syslog(LOG_ERR, "Failed to create %s",
65 (char *)smsg->path_name);
66 goto done;
67 }
68 } else {
69 syslog(LOG_ERR, "Invalid path: %s",
70 (char *)smsg->path_name);
71 goto done;
72 }
73 }
74 p = q + 1;
75 *q = '/';
76 }
77
78 if (!access(target_fname, F_OK)) {
79 syslog(LOG_INFO, "File: %s exists", target_fname);
K. Y. Srinivasan314672a2014-04-09 17:24:16 -070080 if (!(smsg->copy_flags & OVER_WRITE)) {
81 error = HV_ERROR_ALREADY_EXISTS;
K. Y. Srinivasan013254762014-02-16 11:34:30 -080082 goto done;
K. Y. Srinivasan314672a2014-04-09 17:24:16 -070083 }
K. Y. Srinivasan013254762014-02-16 11:34:30 -080084 }
85
Yue Zhange013ac32014-06-27 18:19:48 -070086 target_fd = open(target_fname,
87 O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0744);
K. Y. Srinivasan013254762014-02-16 11:34:30 -080088 if (target_fd == -1) {
89 syslog(LOG_INFO, "Open Failed: %s", strerror(errno));
90 goto done;
91 }
92
93 error = 0;
94done:
95 return error;
96}
97
98static int hv_copy_data(struct hv_do_fcopy *cpmsg)
99{
100 ssize_t bytes_written;
101
102 bytes_written = pwrite(target_fd, cpmsg->data, cpmsg->size,
103 cpmsg->offset);
104
105 if (bytes_written != cpmsg->size)
106 return HV_E_FAIL;
107
108 return 0;
109}
110
111static int hv_copy_finished(void)
112{
113 close(target_fd);
114 return 0;
115}
116static int hv_copy_cancel(void)
117{
118 close(target_fd);
119 unlink(target_fname);
120 return 0;
121
122}
123
Vitaly Kuznetsov170f4be2014-10-22 18:07:11 +0200124void print_usage(char *argv[])
125{
126 fprintf(stderr, "Usage: %s [options]\n"
127 "Options are:\n"
128 " -n, --no-daemon stay in foreground, don't daemonize\n"
129 " -h, --help print this help\n", argv[0]);
130}
131
132int main(int argc, char *argv[])
K. Y. Srinivasan013254762014-02-16 11:34:30 -0800133{
Vitaly Kuznetsovaba474b2015-01-09 22:18:54 -0800134 int fcopy_fd, len;
K. Y. Srinivasan013254762014-02-16 11:34:30 -0800135 int error;
Vitaly Kuznetsov170f4be2014-10-22 18:07:11 +0200136 int daemonize = 1, long_index = 0, opt;
K. Y. Srinivasan013254762014-02-16 11:34:30 -0800137 int version = FCOPY_CURRENT_VERSION;
138 char *buffer[4096 * 2];
139 struct hv_fcopy_hdr *in_msg;
140
Vitaly Kuznetsov170f4be2014-10-22 18:07:11 +0200141 static struct option long_options[] = {
142 {"help", no_argument, 0, 'h' },
143 {"no-daemon", no_argument, 0, 'n' },
144 {0, 0, 0, 0 }
145 };
146
147 while ((opt = getopt_long(argc, argv, "hn", long_options,
148 &long_index)) != -1) {
149 switch (opt) {
150 case 'n':
151 daemonize = 0;
152 break;
153 case 'h':
154 default:
155 print_usage(argv);
156 exit(EXIT_FAILURE);
157 }
158 }
159
160 if (daemonize && daemon(1, 0)) {
K. Y. Srinivasan013254762014-02-16 11:34:30 -0800161 syslog(LOG_ERR, "daemon() failed; error: %s", strerror(errno));
162 exit(EXIT_FAILURE);
163 }
164
165 openlog("HV_FCOPY", 0, LOG_USER);
166 syslog(LOG_INFO, "HV_FCOPY starting; pid is:%d", getpid());
167
168 fcopy_fd = open("/dev/vmbus/hv_fcopy", O_RDWR);
169
170 if (fcopy_fd < 0) {
171 syslog(LOG_ERR, "open /dev/vmbus/hv_fcopy failed; error: %d %s",
172 errno, strerror(errno));
173 exit(EXIT_FAILURE);
174 }
175
176 /*
177 * Register with the kernel.
178 */
179 if ((write(fcopy_fd, &version, sizeof(int))) != sizeof(int)) {
180 syslog(LOG_ERR, "Registration failed: %s", strerror(errno));
181 exit(EXIT_FAILURE);
182 }
183
184 while (1) {
185 /*
186 * In this loop we process fcopy messages after the
187 * handshake is complete.
188 */
189 len = pread(fcopy_fd, buffer, (4096 * 2), 0);
190 if (len < 0) {
191 syslog(LOG_ERR, "pread failed: %s", strerror(errno));
192 exit(EXIT_FAILURE);
193 }
194 in_msg = (struct hv_fcopy_hdr *)buffer;
195
196 switch (in_msg->operation) {
197 case START_FILE_COPY:
198 error = hv_start_fcopy((struct hv_start_fcopy *)in_msg);
199 break;
200 case WRITE_TO_FILE:
201 error = hv_copy_data((struct hv_do_fcopy *)in_msg);
202 break;
203 case COMPLETE_FCOPY:
204 error = hv_copy_finished();
205 break;
206 case CANCEL_FCOPY:
207 error = hv_copy_cancel();
208 break;
209
210 default:
211 syslog(LOG_ERR, "Unknown operation: %d",
212 in_msg->operation);
213
214 }
215
216 if (pwrite(fcopy_fd, &error, sizeof(int), 0) != sizeof(int)) {
217 syslog(LOG_ERR, "pwrite failed: %s", strerror(errno));
218 exit(EXIT_FAILURE);
219 }
220 }
221}