.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * An implementation of host to guest copy functionality for Linux. |
---|
3 | 4 | * |
---|
4 | 5 | * Copyright (C) 2014, Microsoft, Inc. |
---|
5 | 6 | * |
---|
6 | 7 | * 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 | 8 | */ |
---|
18 | 9 | |
---|
19 | 10 | |
---|
.. | .. |
---|
89 | 80 | |
---|
90 | 81 | error = 0; |
---|
91 | 82 | done: |
---|
| 83 | + if (error) |
---|
| 84 | + target_fname[0] = '\0'; |
---|
92 | 85 | return error; |
---|
93 | 86 | } |
---|
94 | 87 | |
---|
.. | .. |
---|
117 | 110 | return ret; |
---|
118 | 111 | } |
---|
119 | 112 | |
---|
| 113 | +/* |
---|
| 114 | + * Reset target_fname to "" in the two below functions for hibernation: if |
---|
| 115 | + * the fcopy operation is aborted by hibernation, the daemon should remove the |
---|
| 116 | + * partially-copied file; to achieve this, the hv_utils driver always fakes a |
---|
| 117 | + * CANCEL_FCOPY message upon suspend, and later when the VM resumes back, |
---|
| 118 | + * the daemon calls hv_copy_cancel() to remove the file; if a file is copied |
---|
| 119 | + * successfully before suspend, hv_copy_finished() must reset target_fname to |
---|
| 120 | + * avoid that the file can be incorrectly removed upon resume, since the faked |
---|
| 121 | + * CANCEL_FCOPY message is spurious in this case. |
---|
| 122 | + */ |
---|
120 | 123 | static int hv_copy_finished(void) |
---|
121 | 124 | { |
---|
122 | 125 | close(target_fd); |
---|
| 126 | + target_fname[0] = '\0'; |
---|
123 | 127 | return 0; |
---|
124 | 128 | } |
---|
125 | 129 | static int hv_copy_cancel(void) |
---|
126 | 130 | { |
---|
127 | 131 | close(target_fd); |
---|
128 | | - unlink(target_fname); |
---|
| 132 | + if (strlen(target_fname) > 0) { |
---|
| 133 | + unlink(target_fname); |
---|
| 134 | + target_fname[0] = '\0'; |
---|
| 135 | + } |
---|
129 | 136 | return 0; |
---|
130 | 137 | |
---|
131 | 138 | } |
---|
.. | .. |
---|
140 | 147 | |
---|
141 | 148 | int main(int argc, char *argv[]) |
---|
142 | 149 | { |
---|
143 | | - int fcopy_fd; |
---|
| 150 | + int fcopy_fd = -1; |
---|
144 | 151 | int error; |
---|
145 | 152 | int daemonize = 1, long_index = 0, opt; |
---|
146 | 153 | int version = FCOPY_CURRENT_VERSION; |
---|
.. | .. |
---|
150 | 157 | struct hv_do_fcopy copy; |
---|
151 | 158 | __u32 kernel_modver; |
---|
152 | 159 | } buffer = { }; |
---|
153 | | - int in_handshake = 1; |
---|
| 160 | + int in_handshake; |
---|
154 | 161 | |
---|
155 | 162 | static struct option long_options[] = { |
---|
156 | 163 | {"help", no_argument, 0, 'h' }, |
---|
.. | .. |
---|
179 | 186 | openlog("HV_FCOPY", 0, LOG_USER); |
---|
180 | 187 | syslog(LOG_INFO, "starting; pid is:%d", getpid()); |
---|
181 | 188 | |
---|
| 189 | +reopen_fcopy_fd: |
---|
| 190 | + if (fcopy_fd != -1) |
---|
| 191 | + close(fcopy_fd); |
---|
| 192 | + /* Remove any possible partially-copied file on error */ |
---|
| 193 | + hv_copy_cancel(); |
---|
| 194 | + in_handshake = 1; |
---|
182 | 195 | fcopy_fd = open("/dev/vmbus/hv_fcopy", O_RDWR); |
---|
183 | 196 | |
---|
184 | 197 | if (fcopy_fd < 0) { |
---|
.. | .. |
---|
205 | 218 | len = pread(fcopy_fd, &buffer, sizeof(buffer), 0); |
---|
206 | 219 | if (len < 0) { |
---|
207 | 220 | syslog(LOG_ERR, "pread failed: %s", strerror(errno)); |
---|
208 | | - exit(EXIT_FAILURE); |
---|
| 221 | + goto reopen_fcopy_fd; |
---|
209 | 222 | } |
---|
210 | 223 | |
---|
211 | 224 | if (in_handshake) { |
---|
.. | .. |
---|
240 | 253 | |
---|
241 | 254 | } |
---|
242 | 255 | |
---|
| 256 | + /* |
---|
| 257 | + * pwrite() may return an error due to the faked CANCEL_FCOPY |
---|
| 258 | + * message upon hibernation. Ignore the error by resetting the |
---|
| 259 | + * dev file, i.e. closing and re-opening it. |
---|
| 260 | + */ |
---|
243 | 261 | if (pwrite(fcopy_fd, &error, sizeof(int), 0) != sizeof(int)) { |
---|
244 | 262 | syslog(LOG_ERR, "pwrite failed: %s", strerror(errno)); |
---|
245 | | - exit(EXIT_FAILURE); |
---|
| 263 | + goto reopen_fcopy_fd; |
---|
246 | 264 | } |
---|
247 | 265 | } |
---|
248 | 266 | } |
---|