GNU Linux-libre 4.14.290-gnu1
[releases.git] / tools / hv / hv_fcopy_daemon.c
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>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <linux/hyperv.h>
26 #include <linux/limits.h>
27 #include <syslog.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <getopt.h>
31
32 static int target_fd;
33 static char target_fname[PATH_MAX];
34 static unsigned long long filesize;
35
36 static int hv_start_fcopy(struct hv_start_fcopy *smsg)
37 {
38         int error = HV_E_FAIL;
39         char *q, *p;
40
41         filesize = 0;
42         p = (char *)smsg->path_name;
43         snprintf(target_fname, sizeof(target_fname), "%s/%s",
44                  (char *)smsg->path_name, (char *)smsg->file_name);
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);
76                 if (!(smsg->copy_flags & OVER_WRITE)) {
77                         error = HV_ERROR_ALREADY_EXISTS;
78                         goto done;
79                 }
80         }
81
82         target_fd = open(target_fname,
83                          O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0744);
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;
97         int ret = 0;
98
99         bytes_written = pwrite(target_fd, cpmsg->data, cpmsg->size,
100                                 cpmsg->offset);
101
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         }
115
116         return ret;
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
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[])
141 {
142         int fcopy_fd;
143         int error;
144         int daemonize = 1, long_index = 0, opt;
145         int version = FCOPY_CURRENT_VERSION;
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 = { };
152         int in_handshake = 1;
153
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)) {
174                 syslog(LOG_ERR, "daemon() failed; error: %s", strerror(errno));
175                 exit(EXIT_FAILURE);
176         }
177
178         openlog("HV_FCOPY", 0, LOG_USER);
179         syslog(LOG_INFO, "starting; pid is:%d", getpid());
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                  */
202                 ssize_t len;
203
204                 len = pread(fcopy_fd, &buffer, sizeof(buffer), 0);
205                 if (len < 0) {
206                         syslog(LOG_ERR, "pread failed: %s", strerror(errno));
207                         exit(EXIT_FAILURE);
208                 }
209
210                 if (in_handshake) {
211                         if (len != sizeof(buffer.kernel_modver)) {
212                                 syslog(LOG_ERR, "invalid version negotiation");
213                                 exit(EXIT_FAILURE);
214                         }
215                         in_handshake = 0;
216                         syslog(LOG_INFO, "kernel module version: %u",
217                                buffer.kernel_modver);
218                         continue;
219                 }
220
221                 switch (buffer.hdr.operation) {
222                 case START_FILE_COPY:
223                         error = hv_start_fcopy(&buffer.start);
224                         break;
225                 case WRITE_TO_FILE:
226                         error = hv_copy_data(&buffer.copy);
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                         error = HV_E_FAIL;
237                         syslog(LOG_ERR, "Unknown operation: %d",
238                                 buffer.hdr.operation);
239
240                 }
241
242                 if (pwrite(fcopy_fd, &error, sizeof(int), 0) != sizeof(int)) {
243                         syslog(LOG_ERR, "pwrite failed: %s", strerror(errno));
244                         exit(EXIT_FAILURE);
245                 }
246         }
247 }