]>
git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - tools/hv/hv_fcopy_daemon.c
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>
38 static char target_fname
[W_MAX_PATH
];
40 static int hv_start_fcopy(struct hv_start_fcopy
*smsg
)
42 int error
= HV_E_FAIL
;
46 * If possile append a path seperator to the path.
48 if (strlen((char *)smsg
->path_name
) < (W_MAX_PATH
- 2))
49 strcat((char *)smsg
->path_name
, "/");
51 p
= (char *)smsg
->path_name
;
52 snprintf(target_fname
, sizeof(target_fname
), "%s/%s",
53 (char *)smsg
->path_name
, smsg
->file_name
);
55 syslog(LOG_INFO
, "Target file name: %s", target_fname
);
57 * Check to see if the path is already in place; if not,
60 while ((q
= strchr(p
, '/')) != NULL
) {
66 if (access((char *)smsg
->path_name
, F_OK
)) {
67 if (smsg
->copy_flags
& CREATE_PATH
) {
68 if (mkdir((char *)smsg
->path_name
, 0755)) {
69 syslog(LOG_ERR
, "Failed to create %s",
70 (char *)smsg
->path_name
);
74 syslog(LOG_ERR
, "Invalid path: %s",
75 (char *)smsg
->path_name
);
83 if (!access(target_fname
, F_OK
)) {
84 syslog(LOG_INFO
, "File: %s exists", target_fname
);
85 if (!smsg
->copy_flags
& OVER_WRITE
)
89 target_fd
= open(target_fname
, O_RDWR
| O_CREAT
| O_CLOEXEC
, 0744);
90 if (target_fd
== -1) {
91 syslog(LOG_INFO
, "Open Failed: %s", strerror(errno
));
100 static int hv_copy_data(struct hv_do_fcopy
*cpmsg
)
102 ssize_t bytes_written
;
104 bytes_written
= pwrite(target_fd
, cpmsg
->data
, cpmsg
->size
,
107 if (bytes_written
!= cpmsg
->size
)
113 static int hv_copy_finished(void)
118 static int hv_copy_cancel(void)
121 unlink(target_fname
);
128 int fd
, fcopy_fd
, len
;
130 int version
= FCOPY_CURRENT_VERSION
;
131 char *buffer
[4096 * 2];
132 struct hv_fcopy_hdr
*in_msg
;
135 syslog(LOG_ERR
, "daemon() failed; error: %s", strerror(errno
));
139 openlog("HV_FCOPY", 0, LOG_USER
);
140 syslog(LOG_INFO
, "HV_FCOPY starting; pid is:%d", getpid());
142 fcopy_fd
= open("/dev/vmbus/hv_fcopy", O_RDWR
);
145 syslog(LOG_ERR
, "open /dev/vmbus/hv_fcopy failed; error: %d %s",
146 errno
, strerror(errno
));
151 * Register with the kernel.
153 if ((write(fcopy_fd
, &version
, sizeof(int))) != sizeof(int)) {
154 syslog(LOG_ERR
, "Registration failed: %s", strerror(errno
));
160 * In this loop we process fcopy messages after the
161 * handshake is complete.
163 len
= pread(fcopy_fd
, buffer
, (4096 * 2), 0);
165 syslog(LOG_ERR
, "pread failed: %s", strerror(errno
));
168 in_msg
= (struct hv_fcopy_hdr
*)buffer
;
170 switch (in_msg
->operation
) {
171 case START_FILE_COPY
:
172 error
= hv_start_fcopy((struct hv_start_fcopy
*)in_msg
);
175 error
= hv_copy_data((struct hv_do_fcopy
*)in_msg
);
178 error
= hv_copy_finished();
181 error
= hv_copy_cancel();
185 syslog(LOG_ERR
, "Unknown operation: %d",
190 if (pwrite(fcopy_fd
, &error
, sizeof(int), 0) != sizeof(int)) {
191 syslog(LOG_ERR
, "pwrite failed: %s", strerror(errno
));