Merge tag 'armsoc-late' of git://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc
[deliverable/linux.git] / tools / hv / hv_fcopy_daemon.c
CommitLineData
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>
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>
170f4bea 36#include <getopt.h>
01325476
S
37
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
01325476
S
46 p = (char *)smsg->path_name;
47 snprintf(target_fname, sizeof(target_fname), "%s/%s",
aba474b8 48 (char *)smsg->path_name, (char *)smsg->file_name);
01325476
S
49
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);
314672a2
S
80 if (!(smsg->copy_flags & OVER_WRITE)) {
81 error = HV_ERROR_ALREADY_EXISTS;
01325476 82 goto done;
314672a2 83 }
01325476
S
84 }
85
e013ac31
YZ
86 target_fd = open(target_fname,
87 O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0744);
01325476
S
88 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
170f4bea
VK
124void 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[])
01325476 133{
aba474b8 134 int fcopy_fd, len;
01325476 135 int error;
170f4bea 136 int daemonize = 1, long_index = 0, opt;
01325476
S
137 int version = FCOPY_CURRENT_VERSION;
138 char *buffer[4096 * 2];
139 struct hv_fcopy_hdr *in_msg;
a4d1ee5b
VK
140 int in_handshake = 1;
141 __u32 kernel_modver;
01325476 142
170f4bea
VK
143 static struct option long_options[] = {
144 {"help", no_argument, 0, 'h' },
145 {"no-daemon", no_argument, 0, 'n' },
146 {0, 0, 0, 0 }
147 };
148
149 while ((opt = getopt_long(argc, argv, "hn", long_options,
150 &long_index)) != -1) {
151 switch (opt) {
152 case 'n':
153 daemonize = 0;
154 break;
155 case 'h':
156 default:
157 print_usage(argv);
158 exit(EXIT_FAILURE);
159 }
160 }
161
162 if (daemonize && daemon(1, 0)) {
01325476
S
163 syslog(LOG_ERR, "daemon() failed; error: %s", strerror(errno));
164 exit(EXIT_FAILURE);
165 }
166
167 openlog("HV_FCOPY", 0, LOG_USER);
168 syslog(LOG_INFO, "HV_FCOPY starting; pid is:%d", getpid());
169
170 fcopy_fd = open("/dev/vmbus/hv_fcopy", O_RDWR);
171
172 if (fcopy_fd < 0) {
173 syslog(LOG_ERR, "open /dev/vmbus/hv_fcopy failed; error: %d %s",
174 errno, strerror(errno));
175 exit(EXIT_FAILURE);
176 }
177
178 /*
179 * Register with the kernel.
180 */
181 if ((write(fcopy_fd, &version, sizeof(int))) != sizeof(int)) {
182 syslog(LOG_ERR, "Registration failed: %s", strerror(errno));
183 exit(EXIT_FAILURE);
184 }
185
186 while (1) {
187 /*
188 * In this loop we process fcopy messages after the
189 * handshake is complete.
190 */
191 len = pread(fcopy_fd, buffer, (4096 * 2), 0);
192 if (len < 0) {
193 syslog(LOG_ERR, "pread failed: %s", strerror(errno));
194 exit(EXIT_FAILURE);
195 }
a4d1ee5b
VK
196
197 if (in_handshake) {
198 if (len != sizeof(kernel_modver)) {
199 syslog(LOG_ERR, "invalid version negotiation");
200 exit(EXIT_FAILURE);
201 }
202 kernel_modver = *(__u32 *)buffer;
203 in_handshake = 0;
204 syslog(LOG_INFO, "HV_FCOPY: kernel module version: %d",
205 kernel_modver);
206 continue;
207 }
208
01325476
S
209 in_msg = (struct hv_fcopy_hdr *)buffer;
210
211 switch (in_msg->operation) {
212 case START_FILE_COPY:
213 error = hv_start_fcopy((struct hv_start_fcopy *)in_msg);
214 break;
215 case WRITE_TO_FILE:
216 error = hv_copy_data((struct hv_do_fcopy *)in_msg);
217 break;
218 case COMPLETE_FCOPY:
219 error = hv_copy_finished();
220 break;
221 case CANCEL_FCOPY:
222 error = hv_copy_cancel();
223 break;
224
225 default:
226 syslog(LOG_ERR, "Unknown operation: %d",
227 in_msg->operation);
228
229 }
230
231 if (pwrite(fcopy_fd, &error, sizeof(int), 0) != sizeof(int)) {
232 syslog(LOG_ERR, "pwrite failed: %s", strerror(errno));
233 exit(EXIT_FAILURE);
234 }
235 }
236}
This page took 0.207411 seconds and 5 git commands to generate.