]>
Commit | Line | Data |
---|---|---|
01325476 S |
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 | ||
19 | ||
20 | #include <sys/types.h> | |
01325476 S |
21 | #include <stdio.h> |
22 | #include <stdlib.h> | |
23 | #include <unistd.h> | |
01325476 S |
24 | #include <errno.h> |
25 | #include <linux/hyperv.h> | |
3d89a727 | 26 | #include <linux/limits.h> |
01325476 S |
27 | #include <syslog.h> |
28 | #include <sys/stat.h> | |
29 | #include <fcntl.h> | |
170f4bea | 30 | #include <getopt.h> |
01325476 S |
31 | |
32 | static int target_fd; | |
3d89a727 | 33 | static char target_fname[PATH_MAX]; |
b4ed5d16 | 34 | static unsigned long long filesize; |
01325476 S |
35 | |
36 | static int hv_start_fcopy(struct hv_start_fcopy *smsg) | |
37 | { | |
38 | int error = HV_E_FAIL; | |
39 | char *q, *p; | |
40 | ||
b4ed5d16 | 41 | filesize = 0; |
01325476 S |
42 | p = (char *)smsg->path_name; |
43 | snprintf(target_fname, sizeof(target_fname), "%s/%s", | |
aba474b8 | 44 | (char *)smsg->path_name, (char *)smsg->file_name); |
01325476 S |
45 | |
46 | syslog(LOG_INFO, "Target file name: %s", target_fname); | |
47 | /* | |
48 | * Check to see if the path is already in place; if not, | |
49 | * create if required. | |
50 | */ | |
51 | while ((q = strchr(p, '/')) != NULL) { | |
52 | if (q == p) { | |
53 | p++; | |
54 | continue; | |
55 | } | |
56 | *q = '\0'; | |
57 | if (access((char *)smsg->path_name, F_OK)) { | |
58 | if (smsg->copy_flags & CREATE_PATH) { | |
59 | if (mkdir((char *)smsg->path_name, 0755)) { | |
60 | syslog(LOG_ERR, "Failed to create %s", | |
61 | (char *)smsg->path_name); | |
62 | goto done; | |
63 | } | |
64 | } else { | |
65 | syslog(LOG_ERR, "Invalid path: %s", | |
66 | (char *)smsg->path_name); | |
67 | goto done; | |
68 | } | |
69 | } | |
70 | p = q + 1; | |
71 | *q = '/'; | |
72 | } | |
73 | ||
74 | if (!access(target_fname, F_OK)) { | |
75 | syslog(LOG_INFO, "File: %s exists", target_fname); | |
314672a2 S |
76 | if (!(smsg->copy_flags & OVER_WRITE)) { |
77 | error = HV_ERROR_ALREADY_EXISTS; | |
01325476 | 78 | goto done; |
314672a2 | 79 | } |
01325476 S |
80 | } |
81 | ||
e013ac31 YZ |
82 | target_fd = open(target_fname, |
83 | O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0744); | |
01325476 S |
84 | if (target_fd == -1) { |
85 | syslog(LOG_INFO, "Open Failed: %s", strerror(errno)); | |
86 | goto done; | |
87 | } | |
88 | ||
89 | error = 0; | |
90 | done: | |
91 | return error; | |
92 | } | |
93 | ||
94 | static int hv_copy_data(struct hv_do_fcopy *cpmsg) | |
95 | { | |
96 | ssize_t bytes_written; | |
b4ed5d16 | 97 | int ret = 0; |
01325476 S |
98 | |
99 | bytes_written = pwrite(target_fd, cpmsg->data, cpmsg->size, | |
100 | cpmsg->offset); | |
101 | ||
b4ed5d16 OH |
102 | filesize += cpmsg->size; |
103 | if (bytes_written != cpmsg->size) { | |
104 | switch (errno) { | |
105 | case ENOSPC: | |
106 | ret = HV_ERROR_DISK_FULL; | |
107 | break; | |
108 | default: | |
109 | ret = HV_E_FAIL; | |
110 | break; | |
111 | } | |
112 | syslog(LOG_ERR, "pwrite failed to write %llu bytes: %ld (%s)", | |
113 | filesize, (long)bytes_written, strerror(errno)); | |
114 | } | |
01325476 | 115 | |
b4ed5d16 | 116 | return ret; |
01325476 S |
117 | } |
118 | ||
119 | static int hv_copy_finished(void) | |
120 | { | |
121 | close(target_fd); | |
122 | return 0; | |
123 | } | |
124 | static int hv_copy_cancel(void) | |
125 | { | |
126 | close(target_fd); | |
127 | unlink(target_fname); | |
128 | return 0; | |
129 | ||
130 | } | |
131 | ||
170f4bea VK |
132 | void print_usage(char *argv[]) |
133 | { | |
134 | fprintf(stderr, "Usage: %s [options]\n" | |
135 | "Options are:\n" | |
136 | " -n, --no-daemon stay in foreground, don't daemonize\n" | |
137 | " -h, --help print this help\n", argv[0]); | |
138 | } | |
139 | ||
140 | int main(int argc, char *argv[]) | |
01325476 | 141 | { |
3f2baa8a | 142 | int fcopy_fd; |
01325476 | 143 | int error; |
170f4bea | 144 | int daemonize = 1, long_index = 0, opt; |
01325476 | 145 | int version = FCOPY_CURRENT_VERSION; |
3f2baa8a OH |
146 | union { |
147 | struct hv_fcopy_hdr hdr; | |
148 | struct hv_start_fcopy start; | |
149 | struct hv_do_fcopy copy; | |
150 | __u32 kernel_modver; | |
151 | } buffer = { }; | |
a4d1ee5b | 152 | int in_handshake = 1; |
01325476 | 153 | |
170f4bea VK |
154 | static struct option long_options[] = { |
155 | {"help", no_argument, 0, 'h' }, | |
156 | {"no-daemon", no_argument, 0, 'n' }, | |
157 | {0, 0, 0, 0 } | |
158 | }; | |
159 | ||
160 | while ((opt = getopt_long(argc, argv, "hn", long_options, | |
161 | &long_index)) != -1) { | |
162 | switch (opt) { | |
163 | case 'n': | |
164 | daemonize = 0; | |
165 | break; | |
166 | case 'h': | |
167 | default: | |
168 | print_usage(argv); | |
169 | exit(EXIT_FAILURE); | |
170 | } | |
171 | } | |
172 | ||
173 | if (daemonize && daemon(1, 0)) { | |
01325476 S |
174 | syslog(LOG_ERR, "daemon() failed; error: %s", strerror(errno)); |
175 | exit(EXIT_FAILURE); | |
176 | } | |
177 | ||
178 | openlog("HV_FCOPY", 0, LOG_USER); | |
6dfb867c | 179 | syslog(LOG_INFO, "starting; pid is:%d", getpid()); |
01325476 S |
180 | |
181 | fcopy_fd = open("/dev/vmbus/hv_fcopy", O_RDWR); | |
182 | ||
183 | if (fcopy_fd < 0) { | |
184 | syslog(LOG_ERR, "open /dev/vmbus/hv_fcopy failed; error: %d %s", | |
185 | errno, strerror(errno)); | |
186 | exit(EXIT_FAILURE); | |
187 | } | |
188 | ||
189 | /* | |
190 | * Register with the kernel. | |
191 | */ | |
192 | if ((write(fcopy_fd, &version, sizeof(int))) != sizeof(int)) { | |
193 | syslog(LOG_ERR, "Registration failed: %s", strerror(errno)); | |
194 | exit(EXIT_FAILURE); | |
195 | } | |
196 | ||
197 | while (1) { | |
198 | /* | |
199 | * In this loop we process fcopy messages after the | |
200 | * handshake is complete. | |
201 | */ | |
3f2baa8a OH |
202 | ssize_t len; |
203 | ||
204 | len = pread(fcopy_fd, &buffer, sizeof(buffer), 0); | |
01325476 S |
205 | if (len < 0) { |
206 | syslog(LOG_ERR, "pread failed: %s", strerror(errno)); | |
207 | exit(EXIT_FAILURE); | |
208 | } | |
a4d1ee5b VK |
209 | |
210 | if (in_handshake) { | |
3f2baa8a | 211 | if (len != sizeof(buffer.kernel_modver)) { |
a4d1ee5b VK |
212 | syslog(LOG_ERR, "invalid version negotiation"); |
213 | exit(EXIT_FAILURE); | |
214 | } | |
a4d1ee5b | 215 | in_handshake = 0; |
3f2baa8a OH |
216 | syslog(LOG_INFO, "kernel module version: %u", |
217 | buffer.kernel_modver); | |
a4d1ee5b VK |
218 | continue; |
219 | } | |
220 | ||
3f2baa8a | 221 | switch (buffer.hdr.operation) { |
01325476 | 222 | case START_FILE_COPY: |
3f2baa8a | 223 | error = hv_start_fcopy(&buffer.start); |
01325476 S |
224 | break; |
225 | case WRITE_TO_FILE: | |
3f2baa8a | 226 | error = hv_copy_data(&buffer.copy); |
01325476 S |
227 | break; |
228 | case COMPLETE_FCOPY: | |
229 | error = hv_copy_finished(); | |
230 | break; | |
231 | case CANCEL_FCOPY: | |
232 | error = hv_copy_cancel(); | |
233 | break; | |
234 | ||
235 | default: | |
236 | syslog(LOG_ERR, "Unknown operation: %d", | |
3f2baa8a | 237 | buffer.hdr.operation); |
01325476 S |
238 | |
239 | } | |
240 | ||
241 | if (pwrite(fcopy_fd, &error, sizeof(int), 0) != sizeof(int)) { | |
242 | syslog(LOG_ERR, "pwrite failed: %s", strerror(errno)); | |
243 | exit(EXIT_FAILURE); | |
244 | } | |
245 | } | |
246 | } |