]>
Commit | Line | Data |
---|---|---|
d8ca685a MR |
1 | /* |
2 | * QEMU Guest Agent win32-specific command implementations | |
3 | * | |
4 | * Copyright IBM Corp. 2012 | |
5 | * | |
6 | * Authors: | |
7 | * Michael Roth <[email protected]> | |
aa59637e | 8 | * Gal Hammer <[email protected]> |
d8ca685a MR |
9 | * |
10 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
11 | * See the COPYING file in the top-level directory. | |
12 | */ | |
13 | ||
14 | #include <glib.h> | |
aa59637e GH |
15 | #include <wtypes.h> |
16 | #include <powrprof.h> | |
fa193594 OK |
17 | #include <stdio.h> |
18 | #include <string.h> | |
d6c5528b KA |
19 | #include <winsock2.h> |
20 | #include <ws2tcpip.h> | |
21 | #include <iptypes.h> | |
22 | #include <iphlpapi.h> | |
d8ca685a | 23 | #include "qga/guest-agent-core.h" |
64c00317 | 24 | #include "qga/vss-win32.h" |
d8ca685a | 25 | #include "qga-qmp-commands.h" |
7b1b5d19 | 26 | #include "qapi/qmp/qerror.h" |
fa193594 | 27 | #include "qemu/queue.h" |
d6c5528b | 28 | #include "qemu/host-utils.h" |
d8ca685a | 29 | |
546b60d0 MR |
30 | #ifndef SHTDN_REASON_FLAG_PLANNED |
31 | #define SHTDN_REASON_FLAG_PLANNED 0x80000000 | |
32 | #endif | |
33 | ||
3f2a6087 LL |
34 | /* multiple of 100 nanoseconds elapsed between windows baseline |
35 | * (1/1/1601) and Unix Epoch (1/1/1970), accounting for leap years */ | |
36 | #define W32_FT_OFFSET (10000000ULL * 60 * 60 * 24 * \ | |
37 | (365 * (1970 - 1601) + \ | |
38 | (1970 - 1601) / 4 - 3)) | |
39 | ||
fa193594 OK |
40 | #define INVALID_SET_FILE_POINTER ((DWORD)-1) |
41 | ||
42 | typedef struct GuestFileHandle { | |
43 | int64_t id; | |
44 | HANDLE fh; | |
45 | QTAILQ_ENTRY(GuestFileHandle) next; | |
46 | } GuestFileHandle; | |
47 | ||
48 | static struct { | |
49 | QTAILQ_HEAD(, GuestFileHandle) filehandles; | |
50 | } guest_file_state; | |
51 | ||
52 | ||
53 | typedef struct OpenFlags { | |
54 | const char *forms; | |
55 | DWORD desired_access; | |
56 | DWORD creation_disposition; | |
57 | } OpenFlags; | |
58 | static OpenFlags guest_file_open_modes[] = { | |
59 | {"r", GENERIC_READ, OPEN_EXISTING}, | |
60 | {"rb", GENERIC_READ, OPEN_EXISTING}, | |
61 | {"w", GENERIC_WRITE, CREATE_ALWAYS}, | |
62 | {"wb", GENERIC_WRITE, CREATE_ALWAYS}, | |
63 | {"a", GENERIC_WRITE, OPEN_ALWAYS }, | |
64 | {"r+", GENERIC_WRITE|GENERIC_READ, OPEN_EXISTING}, | |
65 | {"rb+", GENERIC_WRITE|GENERIC_READ, OPEN_EXISTING}, | |
66 | {"r+b", GENERIC_WRITE|GENERIC_READ, OPEN_EXISTING}, | |
67 | {"w+", GENERIC_WRITE|GENERIC_READ, CREATE_ALWAYS}, | |
68 | {"wb+", GENERIC_WRITE|GENERIC_READ, CREATE_ALWAYS}, | |
69 | {"w+b", GENERIC_WRITE|GENERIC_READ, CREATE_ALWAYS}, | |
70 | {"a+", GENERIC_WRITE|GENERIC_READ, OPEN_ALWAYS }, | |
71 | {"ab+", GENERIC_WRITE|GENERIC_READ, OPEN_ALWAYS }, | |
72 | {"a+b", GENERIC_WRITE|GENERIC_READ, OPEN_ALWAYS } | |
73 | }; | |
74 | ||
75 | static OpenFlags *find_open_flag(const char *mode_str) | |
76 | { | |
77 | int mode; | |
78 | Error **errp = NULL; | |
79 | ||
80 | for (mode = 0; mode < ARRAY_SIZE(guest_file_open_modes); ++mode) { | |
81 | OpenFlags *flags = guest_file_open_modes + mode; | |
82 | ||
83 | if (strcmp(flags->forms, mode_str) == 0) { | |
84 | return flags; | |
85 | } | |
86 | } | |
87 | ||
88 | error_setg(errp, "invalid file open mode '%s'", mode_str); | |
89 | return NULL; | |
90 | } | |
91 | ||
92 | static int64_t guest_file_handle_add(HANDLE fh, Error **errp) | |
93 | { | |
94 | GuestFileHandle *gfh; | |
95 | int64_t handle; | |
96 | ||
97 | handle = ga_get_fd_handle(ga_state, errp); | |
98 | if (handle < 0) { | |
99 | return -1; | |
100 | } | |
101 | gfh = g_malloc0(sizeof(GuestFileHandle)); | |
102 | gfh->id = handle; | |
103 | gfh->fh = fh; | |
104 | QTAILQ_INSERT_TAIL(&guest_file_state.filehandles, gfh, next); | |
105 | ||
106 | return handle; | |
107 | } | |
108 | ||
109 | static GuestFileHandle *guest_file_handle_find(int64_t id, Error **errp) | |
110 | { | |
111 | GuestFileHandle *gfh; | |
112 | QTAILQ_FOREACH(gfh, &guest_file_state.filehandles, next) { | |
113 | if (gfh->id == id) { | |
114 | return gfh; | |
115 | } | |
116 | } | |
117 | error_setg(errp, "handle '%" PRId64 "' has not been found", id); | |
118 | return NULL; | |
119 | } | |
120 | ||
121 | int64_t qmp_guest_file_open(const char *path, bool has_mode, | |
122 | const char *mode, Error **errp) | |
123 | { | |
124 | int64_t fd; | |
125 | HANDLE fh; | |
126 | HANDLE templ_file = NULL; | |
127 | DWORD share_mode = FILE_SHARE_READ; | |
128 | DWORD flags_and_attr = FILE_ATTRIBUTE_NORMAL; | |
129 | LPSECURITY_ATTRIBUTES sa_attr = NULL; | |
130 | OpenFlags *guest_flags; | |
131 | ||
132 | if (!has_mode) { | |
133 | mode = "r"; | |
134 | } | |
135 | slog("guest-file-open called, filepath: %s, mode: %s", path, mode); | |
136 | guest_flags = find_open_flag(mode); | |
137 | if (guest_flags == NULL) { | |
138 | error_setg(errp, "invalid file open mode"); | |
139 | return -1; | |
140 | } | |
141 | ||
142 | fh = CreateFile(path, guest_flags->desired_access, share_mode, sa_attr, | |
143 | guest_flags->creation_disposition, flags_and_attr, | |
144 | templ_file); | |
145 | if (fh == INVALID_HANDLE_VALUE) { | |
146 | error_setg_win32(errp, GetLastError(), "failed to open file '%s'", | |
147 | path); | |
148 | return -1; | |
149 | } | |
150 | ||
151 | fd = guest_file_handle_add(fh, errp); | |
152 | if (fd < 0) { | |
153 | CloseHandle(&fh); | |
154 | error_setg(errp, "failed to add handle to qmp handle table"); | |
155 | return -1; | |
156 | } | |
157 | ||
158 | slog("guest-file-open, handle: % " PRId64, fd); | |
159 | return fd; | |
160 | } | |
161 | ||
162 | void qmp_guest_file_close(int64_t handle, Error **errp) | |
163 | { | |
164 | bool ret; | |
165 | GuestFileHandle *gfh = guest_file_handle_find(handle, errp); | |
166 | slog("guest-file-close called, handle: %" PRId64, handle); | |
167 | if (gfh == NULL) { | |
168 | return; | |
169 | } | |
170 | ret = CloseHandle(gfh->fh); | |
171 | if (!ret) { | |
172 | error_setg_win32(errp, GetLastError(), "failed close handle"); | |
173 | return; | |
174 | } | |
175 | ||
176 | QTAILQ_REMOVE(&guest_file_state.filehandles, gfh, next); | |
177 | g_free(gfh); | |
178 | } | |
179 | ||
77dbc81b | 180 | static void acquire_privilege(const char *name, Error **errp) |
d8ca685a | 181 | { |
374044f0 | 182 | HANDLE token = NULL; |
546b60d0 | 183 | TOKEN_PRIVILEGES priv; |
aa59637e GH |
184 | Error *local_err = NULL; |
185 | ||
aa59637e GH |
186 | if (OpenProcessToken(GetCurrentProcess(), |
187 | TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &token)) | |
188 | { | |
189 | if (!LookupPrivilegeValue(NULL, name, &priv.Privileges[0].Luid)) { | |
c6bd8c70 MA |
190 | error_setg(&local_err, QERR_QGA_COMMAND_FAILED, |
191 | "no luid for requested privilege"); | |
aa59637e GH |
192 | goto out; |
193 | } | |
194 | ||
195 | priv.PrivilegeCount = 1; | |
196 | priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; | |
197 | ||
198 | if (!AdjustTokenPrivileges(token, FALSE, &priv, 0, NULL, 0)) { | |
c6bd8c70 MA |
199 | error_setg(&local_err, QERR_QGA_COMMAND_FAILED, |
200 | "unable to acquire requested privilege"); | |
aa59637e GH |
201 | goto out; |
202 | } | |
203 | ||
aa59637e | 204 | } else { |
c6bd8c70 MA |
205 | error_setg(&local_err, QERR_QGA_COMMAND_FAILED, |
206 | "failed to open privilege token"); | |
aa59637e GH |
207 | } |
208 | ||
209 | out: | |
374044f0 GA |
210 | if (token) { |
211 | CloseHandle(token); | |
212 | } | |
aa59637e | 213 | if (local_err) { |
77dbc81b | 214 | error_propagate(errp, local_err); |
aa59637e GH |
215 | } |
216 | } | |
217 | ||
77dbc81b MA |
218 | static void execute_async(DWORD WINAPI (*func)(LPVOID), LPVOID opaque, |
219 | Error **errp) | |
aa59637e GH |
220 | { |
221 | Error *local_err = NULL; | |
222 | ||
aa59637e GH |
223 | HANDLE thread = CreateThread(NULL, 0, func, opaque, 0, NULL); |
224 | if (!thread) { | |
c6bd8c70 MA |
225 | error_setg(&local_err, QERR_QGA_COMMAND_FAILED, |
226 | "failed to dispatch asynchronous command"); | |
77dbc81b | 227 | error_propagate(errp, local_err); |
aa59637e GH |
228 | } |
229 | } | |
230 | ||
77dbc81b | 231 | void qmp_guest_shutdown(bool has_mode, const char *mode, Error **errp) |
aa59637e | 232 | { |
0f230bf7 | 233 | Error *local_err = NULL; |
546b60d0 MR |
234 | UINT shutdown_flag = EWX_FORCE; |
235 | ||
236 | slog("guest-shutdown called, mode: %s", mode); | |
237 | ||
238 | if (!has_mode || strcmp(mode, "powerdown") == 0) { | |
239 | shutdown_flag |= EWX_POWEROFF; | |
240 | } else if (strcmp(mode, "halt") == 0) { | |
241 | shutdown_flag |= EWX_SHUTDOWN; | |
242 | } else if (strcmp(mode, "reboot") == 0) { | |
243 | shutdown_flag |= EWX_REBOOT; | |
244 | } else { | |
c6bd8c70 MA |
245 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "mode", |
246 | "halt|powerdown|reboot"); | |
546b60d0 MR |
247 | return; |
248 | } | |
249 | ||
250 | /* Request a shutdown privilege, but try to shut down the system | |
251 | anyway. */ | |
0f230bf7 MA |
252 | acquire_privilege(SE_SHUTDOWN_NAME, &local_err); |
253 | if (local_err) { | |
254 | error_propagate(errp, local_err); | |
aa59637e | 255 | return; |
546b60d0 MR |
256 | } |
257 | ||
258 | if (!ExitWindowsEx(shutdown_flag, SHTDN_REASON_FLAG_PLANNED)) { | |
16f4e8fa | 259 | slog("guest-shutdown failed: %lu", GetLastError()); |
c6bd8c70 | 260 | error_setg(errp, QERR_UNDEFINED_ERROR); |
546b60d0 | 261 | } |
d8ca685a MR |
262 | } |
263 | ||
d8ca685a | 264 | GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count, |
77dbc81b | 265 | int64_t count, Error **errp) |
d8ca685a | 266 | { |
fa193594 OK |
267 | GuestFileRead *read_data = NULL; |
268 | guchar *buf; | |
269 | HANDLE fh; | |
270 | bool is_ok; | |
271 | DWORD read_count; | |
272 | GuestFileHandle *gfh = guest_file_handle_find(handle, errp); | |
273 | ||
274 | if (!gfh) { | |
275 | return NULL; | |
276 | } | |
277 | if (!has_count) { | |
278 | count = QGA_READ_COUNT_DEFAULT; | |
279 | } else if (count < 0) { | |
280 | error_setg(errp, "value '%" PRId64 | |
281 | "' is invalid for argument count", count); | |
282 | return NULL; | |
283 | } | |
284 | ||
285 | fh = gfh->fh; | |
286 | buf = g_malloc0(count+1); | |
287 | is_ok = ReadFile(fh, buf, count, &read_count, NULL); | |
288 | if (!is_ok) { | |
289 | error_setg_win32(errp, GetLastError(), "failed to read file"); | |
290 | slog("guest-file-read failed, handle %" PRId64, handle); | |
291 | } else { | |
292 | buf[read_count] = 0; | |
293 | read_data = g_malloc0(sizeof(GuestFileRead)); | |
294 | read_data->count = (size_t)read_count; | |
295 | read_data->eof = read_count == 0; | |
296 | ||
297 | if (read_count != 0) { | |
298 | read_data->buf_b64 = g_base64_encode(buf, read_count); | |
299 | } | |
300 | } | |
301 | g_free(buf); | |
302 | ||
303 | return read_data; | |
d8ca685a MR |
304 | } |
305 | ||
306 | GuestFileWrite *qmp_guest_file_write(int64_t handle, const char *buf_b64, | |
77dbc81b MA |
307 | bool has_count, int64_t count, |
308 | Error **errp) | |
d8ca685a | 309 | { |
fa193594 OK |
310 | GuestFileWrite *write_data = NULL; |
311 | guchar *buf; | |
312 | gsize buf_len; | |
313 | bool is_ok; | |
314 | DWORD write_count; | |
315 | GuestFileHandle *gfh = guest_file_handle_find(handle, errp); | |
316 | HANDLE fh; | |
317 | ||
318 | if (!gfh) { | |
319 | return NULL; | |
320 | } | |
321 | fh = gfh->fh; | |
322 | buf = g_base64_decode(buf_b64, &buf_len); | |
323 | ||
324 | if (!has_count) { | |
325 | count = buf_len; | |
326 | } else if (count < 0 || count > buf_len) { | |
327 | error_setg(errp, "value '%" PRId64 | |
328 | "' is invalid for argument count", count); | |
329 | goto done; | |
330 | } | |
331 | ||
332 | is_ok = WriteFile(fh, buf, count, &write_count, NULL); | |
333 | if (!is_ok) { | |
334 | error_setg_win32(errp, GetLastError(), "failed to write to file"); | |
335 | slog("guest-file-write-failed, handle: %" PRId64, handle); | |
336 | } else { | |
337 | write_data = g_malloc0(sizeof(GuestFileWrite)); | |
338 | write_data->count = (size_t) write_count; | |
339 | } | |
340 | ||
341 | done: | |
342 | g_free(buf); | |
343 | return write_data; | |
d8ca685a MR |
344 | } |
345 | ||
346 | GuestFileSeek *qmp_guest_file_seek(int64_t handle, int64_t offset, | |
77dbc81b | 347 | int64_t whence, Error **errp) |
d8ca685a | 348 | { |
fa193594 OK |
349 | GuestFileHandle *gfh; |
350 | GuestFileSeek *seek_data; | |
351 | HANDLE fh; | |
352 | LARGE_INTEGER new_pos, off_pos; | |
353 | off_pos.QuadPart = offset; | |
354 | BOOL res; | |
355 | gfh = guest_file_handle_find(handle, errp); | |
356 | if (!gfh) { | |
357 | return NULL; | |
358 | } | |
359 | ||
360 | fh = gfh->fh; | |
361 | res = SetFilePointerEx(fh, off_pos, &new_pos, whence); | |
362 | if (!res) { | |
363 | error_setg_win32(errp, GetLastError(), "failed to seek file"); | |
364 | return NULL; | |
365 | } | |
366 | seek_data = g_new0(GuestFileSeek, 1); | |
367 | seek_data->position = new_pos.QuadPart; | |
368 | return seek_data; | |
d8ca685a MR |
369 | } |
370 | ||
77dbc81b | 371 | void qmp_guest_file_flush(int64_t handle, Error **errp) |
d8ca685a | 372 | { |
fa193594 OK |
373 | HANDLE fh; |
374 | GuestFileHandle *gfh = guest_file_handle_find(handle, errp); | |
375 | if (!gfh) { | |
376 | return; | |
377 | } | |
378 | ||
379 | fh = gfh->fh; | |
380 | if (!FlushFileBuffers(fh)) { | |
381 | error_setg_win32(errp, GetLastError(), "failed to flush file"); | |
382 | } | |
383 | } | |
384 | ||
385 | static void guest_file_init(void) | |
386 | { | |
387 | QTAILQ_INIT(&guest_file_state.filehandles); | |
d8ca685a MR |
388 | } |
389 | ||
46d4c572 TS |
390 | GuestFilesystemInfoList *qmp_guest_get_fsinfo(Error **errp) |
391 | { | |
c6bd8c70 | 392 | error_setg(errp, QERR_UNSUPPORTED); |
46d4c572 TS |
393 | return NULL; |
394 | } | |
395 | ||
d8ca685a MR |
396 | /* |
397 | * Return status of freeze/thaw | |
398 | */ | |
77dbc81b | 399 | GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **errp) |
d8ca685a | 400 | { |
64c00317 | 401 | if (!vss_initialized()) { |
c6bd8c70 | 402 | error_setg(errp, QERR_UNSUPPORTED); |
64c00317 TS |
403 | return 0; |
404 | } | |
405 | ||
406 | if (ga_is_frozen(ga_state)) { | |
407 | return GUEST_FSFREEZE_STATUS_FROZEN; | |
408 | } | |
409 | ||
410 | return GUEST_FSFREEZE_STATUS_THAWED; | |
d8ca685a MR |
411 | } |
412 | ||
413 | /* | |
64c00317 TS |
414 | * Freeze local file systems using Volume Shadow-copy Service. |
415 | * The frozen state is limited for up to 10 seconds by VSS. | |
d8ca685a | 416 | */ |
77dbc81b | 417 | int64_t qmp_guest_fsfreeze_freeze(Error **errp) |
d8ca685a | 418 | { |
64c00317 TS |
419 | int i; |
420 | Error *local_err = NULL; | |
421 | ||
422 | if (!vss_initialized()) { | |
c6bd8c70 | 423 | error_setg(errp, QERR_UNSUPPORTED); |
64c00317 TS |
424 | return 0; |
425 | } | |
426 | ||
427 | slog("guest-fsfreeze called"); | |
428 | ||
429 | /* cannot risk guest agent blocking itself on a write in this state */ | |
430 | ga_set_frozen(ga_state); | |
431 | ||
0f230bf7 MA |
432 | qga_vss_fsfreeze(&i, &local_err, true); |
433 | if (local_err) { | |
434 | error_propagate(errp, local_err); | |
64c00317 TS |
435 | goto error; |
436 | } | |
437 | ||
438 | return i; | |
439 | ||
440 | error: | |
0f230bf7 | 441 | local_err = NULL; |
64c00317 | 442 | qmp_guest_fsfreeze_thaw(&local_err); |
84d18f06 | 443 | if (local_err) { |
64c00317 TS |
444 | g_debug("cleanup thaw: %s", error_get_pretty(local_err)); |
445 | error_free(local_err); | |
446 | } | |
d8ca685a MR |
447 | return 0; |
448 | } | |
449 | ||
e99bce20 TS |
450 | int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints, |
451 | strList *mountpoints, | |
452 | Error **errp) | |
453 | { | |
c6bd8c70 | 454 | error_setg(errp, QERR_UNSUPPORTED); |
e99bce20 TS |
455 | |
456 | return 0; | |
457 | } | |
458 | ||
d8ca685a | 459 | /* |
64c00317 | 460 | * Thaw local file systems using Volume Shadow-copy Service. |
d8ca685a | 461 | */ |
77dbc81b | 462 | int64_t qmp_guest_fsfreeze_thaw(Error **errp) |
d8ca685a | 463 | { |
64c00317 TS |
464 | int i; |
465 | ||
466 | if (!vss_initialized()) { | |
c6bd8c70 | 467 | error_setg(errp, QERR_UNSUPPORTED); |
64c00317 TS |
468 | return 0; |
469 | } | |
470 | ||
77dbc81b | 471 | qga_vss_fsfreeze(&i, errp, false); |
64c00317 TS |
472 | |
473 | ga_unset_frozen(ga_state); | |
474 | return i; | |
475 | } | |
476 | ||
477 | static void guest_fsfreeze_cleanup(void) | |
478 | { | |
479 | Error *err = NULL; | |
480 | ||
481 | if (!vss_initialized()) { | |
482 | return; | |
483 | } | |
484 | ||
485 | if (ga_is_frozen(ga_state) == GUEST_FSFREEZE_STATUS_FROZEN) { | |
486 | qmp_guest_fsfreeze_thaw(&err); | |
487 | if (err) { | |
488 | slog("failed to clean up frozen filesystems: %s", | |
489 | error_get_pretty(err)); | |
490 | error_free(err); | |
491 | } | |
492 | } | |
493 | ||
494 | vss_deinit(true); | |
d8ca685a MR |
495 | } |
496 | ||
eab5fd59 PB |
497 | /* |
498 | * Walk list of mounted file systems in the guest, and discard unused | |
499 | * areas. | |
500 | */ | |
e82855d9 JO |
501 | GuestFilesystemTrimResponse * |
502 | qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **errp) | |
eab5fd59 | 503 | { |
c6bd8c70 | 504 | error_setg(errp, QERR_UNSUPPORTED); |
e82855d9 | 505 | return NULL; |
eab5fd59 PB |
506 | } |
507 | ||
aa59637e | 508 | typedef enum { |
f54603b6 MR |
509 | GUEST_SUSPEND_MODE_DISK, |
510 | GUEST_SUSPEND_MODE_RAM | |
aa59637e GH |
511 | } GuestSuspendMode; |
512 | ||
77dbc81b | 513 | static void check_suspend_mode(GuestSuspendMode mode, Error **errp) |
aa59637e GH |
514 | { |
515 | SYSTEM_POWER_CAPABILITIES sys_pwr_caps; | |
516 | Error *local_err = NULL; | |
517 | ||
aa59637e GH |
518 | ZeroMemory(&sys_pwr_caps, sizeof(sys_pwr_caps)); |
519 | if (!GetPwrCapabilities(&sys_pwr_caps)) { | |
c6bd8c70 MA |
520 | error_setg(&local_err, QERR_QGA_COMMAND_FAILED, |
521 | "failed to determine guest suspend capabilities"); | |
aa59637e GH |
522 | goto out; |
523 | } | |
524 | ||
f54603b6 MR |
525 | switch (mode) { |
526 | case GUEST_SUSPEND_MODE_DISK: | |
527 | if (!sys_pwr_caps.SystemS4) { | |
c6bd8c70 MA |
528 | error_setg(&local_err, QERR_QGA_COMMAND_FAILED, |
529 | "suspend-to-disk not supported by OS"); | |
aa59637e | 530 | } |
f54603b6 MR |
531 | break; |
532 | case GUEST_SUSPEND_MODE_RAM: | |
533 | if (!sys_pwr_caps.SystemS3) { | |
c6bd8c70 MA |
534 | error_setg(&local_err, QERR_QGA_COMMAND_FAILED, |
535 | "suspend-to-ram not supported by OS"); | |
f54603b6 MR |
536 | } |
537 | break; | |
538 | default: | |
c6bd8c70 MA |
539 | error_setg(&local_err, QERR_INVALID_PARAMETER_VALUE, "mode", |
540 | "GuestSuspendMode"); | |
aa59637e GH |
541 | } |
542 | ||
aa59637e GH |
543 | out: |
544 | if (local_err) { | |
77dbc81b | 545 | error_propagate(errp, local_err); |
aa59637e GH |
546 | } |
547 | } | |
548 | ||
549 | static DWORD WINAPI do_suspend(LPVOID opaque) | |
550 | { | |
551 | GuestSuspendMode *mode = opaque; | |
552 | DWORD ret = 0; | |
553 | ||
554 | if (!SetSuspendState(*mode == GUEST_SUSPEND_MODE_DISK, TRUE, TRUE)) { | |
16f4e8fa | 555 | slog("failed to suspend guest, %lu", GetLastError()); |
aa59637e GH |
556 | ret = -1; |
557 | } | |
558 | g_free(mode); | |
559 | return ret; | |
560 | } | |
561 | ||
77dbc81b | 562 | void qmp_guest_suspend_disk(Error **errp) |
11d0f125 | 563 | { |
0f230bf7 | 564 | Error *local_err = NULL; |
aa59637e GH |
565 | GuestSuspendMode *mode = g_malloc(sizeof(GuestSuspendMode)); |
566 | ||
567 | *mode = GUEST_SUSPEND_MODE_DISK; | |
0f230bf7 MA |
568 | check_suspend_mode(*mode, &local_err); |
569 | acquire_privilege(SE_SHUTDOWN_NAME, &local_err); | |
570 | execute_async(do_suspend, mode, &local_err); | |
aa59637e | 571 | |
0f230bf7 MA |
572 | if (local_err) { |
573 | error_propagate(errp, local_err); | |
aa59637e GH |
574 | g_free(mode); |
575 | } | |
11d0f125 LC |
576 | } |
577 | ||
77dbc81b | 578 | void qmp_guest_suspend_ram(Error **errp) |
fbf42210 | 579 | { |
0f230bf7 | 580 | Error *local_err = NULL; |
f54603b6 MR |
581 | GuestSuspendMode *mode = g_malloc(sizeof(GuestSuspendMode)); |
582 | ||
583 | *mode = GUEST_SUSPEND_MODE_RAM; | |
0f230bf7 MA |
584 | check_suspend_mode(*mode, &local_err); |
585 | acquire_privilege(SE_SHUTDOWN_NAME, &local_err); | |
586 | execute_async(do_suspend, mode, &local_err); | |
f54603b6 | 587 | |
0f230bf7 MA |
588 | if (local_err) { |
589 | error_propagate(errp, local_err); | |
f54603b6 MR |
590 | g_free(mode); |
591 | } | |
fbf42210 LC |
592 | } |
593 | ||
77dbc81b | 594 | void qmp_guest_suspend_hybrid(Error **errp) |
95f4f404 | 595 | { |
c6bd8c70 | 596 | error_setg(errp, QERR_UNSUPPORTED); |
95f4f404 LC |
597 | } |
598 | ||
d6c5528b | 599 | static IP_ADAPTER_ADDRESSES *guest_get_adapters_addresses(Error **errp) |
3424fc9f | 600 | { |
d6c5528b KA |
601 | IP_ADAPTER_ADDRESSES *adptr_addrs = NULL; |
602 | ULONG adptr_addrs_len = 0; | |
603 | DWORD ret; | |
604 | ||
605 | /* Call the first time to get the adptr_addrs_len. */ | |
606 | GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, | |
607 | NULL, adptr_addrs, &adptr_addrs_len); | |
608 | ||
609 | adptr_addrs = g_malloc(adptr_addrs_len); | |
610 | ret = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, | |
611 | NULL, adptr_addrs, &adptr_addrs_len); | |
612 | if (ret != ERROR_SUCCESS) { | |
613 | error_setg_win32(errp, ret, "failed to get adapters addresses"); | |
614 | g_free(adptr_addrs); | |
615 | adptr_addrs = NULL; | |
616 | } | |
617 | return adptr_addrs; | |
618 | } | |
619 | ||
620 | static char *guest_wctomb_dup(WCHAR *wstr) | |
621 | { | |
622 | char *str; | |
623 | size_t i; | |
624 | ||
625 | i = wcslen(wstr) + 1; | |
626 | str = g_malloc(i); | |
627 | WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, | |
628 | wstr, -1, str, i, NULL, NULL); | |
629 | return str; | |
630 | } | |
631 | ||
632 | static char *guest_addr_to_str(IP_ADAPTER_UNICAST_ADDRESS *ip_addr, | |
633 | Error **errp) | |
634 | { | |
635 | char addr_str[INET6_ADDRSTRLEN + INET_ADDRSTRLEN]; | |
636 | DWORD len; | |
637 | int ret; | |
638 | ||
639 | if (ip_addr->Address.lpSockaddr->sa_family == AF_INET || | |
640 | ip_addr->Address.lpSockaddr->sa_family == AF_INET6) { | |
641 | len = sizeof(addr_str); | |
642 | ret = WSAAddressToString(ip_addr->Address.lpSockaddr, | |
643 | ip_addr->Address.iSockaddrLength, | |
644 | NULL, | |
645 | addr_str, | |
646 | &len); | |
647 | if (ret != 0) { | |
648 | error_setg_win32(errp, WSAGetLastError(), | |
649 | "failed address presentation form conversion"); | |
650 | return NULL; | |
651 | } | |
652 | return g_strdup(addr_str); | |
653 | } | |
3424fc9f MP |
654 | return NULL; |
655 | } | |
656 | ||
d6c5528b KA |
657 | #if (_WIN32_WINNT >= 0x0600) |
658 | static int64_t guest_ip_prefix(IP_ADAPTER_UNICAST_ADDRESS *ip_addr) | |
659 | { | |
660 | /* For Windows Vista/2008 and newer, use the OnLinkPrefixLength | |
661 | * field to obtain the prefix. | |
662 | */ | |
663 | return ip_addr->OnLinkPrefixLength; | |
664 | } | |
665 | #else | |
666 | /* When using the Windows XP and 2003 build environment, do the best we can to | |
667 | * figure out the prefix. | |
668 | */ | |
669 | static IP_ADAPTER_INFO *guest_get_adapters_info(void) | |
670 | { | |
671 | IP_ADAPTER_INFO *adptr_info = NULL; | |
672 | ULONG adptr_info_len = 0; | |
673 | DWORD ret; | |
674 | ||
675 | /* Call the first time to get the adptr_info_len. */ | |
676 | GetAdaptersInfo(adptr_info, &adptr_info_len); | |
677 | ||
678 | adptr_info = g_malloc(adptr_info_len); | |
679 | ret = GetAdaptersInfo(adptr_info, &adptr_info_len); | |
680 | if (ret != ERROR_SUCCESS) { | |
681 | g_free(adptr_info); | |
682 | adptr_info = NULL; | |
683 | } | |
684 | return adptr_info; | |
685 | } | |
686 | ||
687 | static int64_t guest_ip_prefix(IP_ADAPTER_UNICAST_ADDRESS *ip_addr) | |
688 | { | |
689 | int64_t prefix = -1; /* Use for AF_INET6 and unknown/undetermined values. */ | |
690 | IP_ADAPTER_INFO *adptr_info, *info; | |
691 | IP_ADDR_STRING *ip; | |
692 | struct in_addr *p; | |
693 | ||
694 | if (ip_addr->Address.lpSockaddr->sa_family != AF_INET) { | |
695 | return prefix; | |
696 | } | |
697 | adptr_info = guest_get_adapters_info(); | |
698 | if (adptr_info == NULL) { | |
699 | return prefix; | |
700 | } | |
701 | ||
702 | /* Match up the passed in ip_addr with one found in adaptr_info. | |
703 | * The matching one in adptr_info will have the netmask. | |
704 | */ | |
705 | p = &((struct sockaddr_in *)ip_addr->Address.lpSockaddr)->sin_addr; | |
706 | for (info = adptr_info; info; info = info->Next) { | |
707 | for (ip = &info->IpAddressList; ip; ip = ip->Next) { | |
708 | if (p->S_un.S_addr == inet_addr(ip->IpAddress.String)) { | |
709 | prefix = ctpop32(inet_addr(ip->IpMask.String)); | |
710 | goto out; | |
711 | } | |
712 | } | |
713 | } | |
714 | out: | |
715 | g_free(adptr_info); | |
716 | return prefix; | |
717 | } | |
718 | #endif | |
719 | ||
720 | GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp) | |
721 | { | |
722 | IP_ADAPTER_ADDRESSES *adptr_addrs, *addr; | |
723 | IP_ADAPTER_UNICAST_ADDRESS *ip_addr = NULL; | |
724 | GuestNetworkInterfaceList *head = NULL, *cur_item = NULL; | |
725 | GuestIpAddressList *head_addr, *cur_addr; | |
726 | GuestNetworkInterfaceList *info; | |
727 | GuestIpAddressList *address_item = NULL; | |
728 | unsigned char *mac_addr; | |
729 | char *addr_str; | |
730 | WORD wsa_version; | |
731 | WSADATA wsa_data; | |
732 | int ret; | |
733 | ||
734 | adptr_addrs = guest_get_adapters_addresses(errp); | |
735 | if (adptr_addrs == NULL) { | |
736 | return NULL; | |
737 | } | |
738 | ||
739 | /* Make WSA APIs available. */ | |
740 | wsa_version = MAKEWORD(2, 2); | |
741 | ret = WSAStartup(wsa_version, &wsa_data); | |
742 | if (ret != 0) { | |
743 | error_setg_win32(errp, ret, "failed socket startup"); | |
744 | goto out; | |
745 | } | |
746 | ||
747 | for (addr = adptr_addrs; addr; addr = addr->Next) { | |
748 | info = g_malloc0(sizeof(*info)); | |
749 | ||
750 | if (cur_item == NULL) { | |
751 | head = cur_item = info; | |
752 | } else { | |
753 | cur_item->next = info; | |
754 | cur_item = info; | |
755 | } | |
756 | ||
757 | info->value = g_malloc0(sizeof(*info->value)); | |
758 | info->value->name = guest_wctomb_dup(addr->FriendlyName); | |
759 | ||
760 | if (addr->PhysicalAddressLength != 0) { | |
761 | mac_addr = addr->PhysicalAddress; | |
762 | ||
763 | info->value->hardware_address = | |
764 | g_strdup_printf("%02x:%02x:%02x:%02x:%02x:%02x", | |
765 | (int) mac_addr[0], (int) mac_addr[1], | |
766 | (int) mac_addr[2], (int) mac_addr[3], | |
767 | (int) mac_addr[4], (int) mac_addr[5]); | |
768 | ||
769 | info->value->has_hardware_address = true; | |
770 | } | |
771 | ||
772 | head_addr = NULL; | |
773 | cur_addr = NULL; | |
774 | for (ip_addr = addr->FirstUnicastAddress; | |
775 | ip_addr; | |
776 | ip_addr = ip_addr->Next) { | |
777 | addr_str = guest_addr_to_str(ip_addr, errp); | |
778 | if (addr_str == NULL) { | |
779 | continue; | |
780 | } | |
781 | ||
782 | address_item = g_malloc0(sizeof(*address_item)); | |
783 | ||
784 | if (!cur_addr) { | |
785 | head_addr = cur_addr = address_item; | |
786 | } else { | |
787 | cur_addr->next = address_item; | |
788 | cur_addr = address_item; | |
789 | } | |
790 | ||
791 | address_item->value = g_malloc0(sizeof(*address_item->value)); | |
792 | address_item->value->ip_address = addr_str; | |
793 | address_item->value->prefix = guest_ip_prefix(ip_addr); | |
794 | if (ip_addr->Address.lpSockaddr->sa_family == AF_INET) { | |
795 | address_item->value->ip_address_type = | |
796 | GUEST_IP_ADDRESS_TYPE_IPV4; | |
797 | } else if (ip_addr->Address.lpSockaddr->sa_family == AF_INET6) { | |
798 | address_item->value->ip_address_type = | |
799 | GUEST_IP_ADDRESS_TYPE_IPV6; | |
800 | } | |
801 | } | |
802 | if (head_addr) { | |
803 | info->value->has_ip_addresses = true; | |
804 | info->value->ip_addresses = head_addr; | |
805 | } | |
806 | } | |
807 | WSACleanup(); | |
808 | out: | |
809 | g_free(adptr_addrs); | |
810 | return head; | |
811 | } | |
812 | ||
6912e6a9 LL |
813 | int64_t qmp_guest_get_time(Error **errp) |
814 | { | |
3f2a6087 LL |
815 | SYSTEMTIME ts = {0}; |
816 | int64_t time_ns; | |
817 | FILETIME tf; | |
818 | ||
819 | GetSystemTime(&ts); | |
820 | if (ts.wYear < 1601 || ts.wYear > 30827) { | |
821 | error_setg(errp, "Failed to get time"); | |
822 | return -1; | |
823 | } | |
824 | ||
825 | if (!SystemTimeToFileTime(&ts, &tf)) { | |
826 | error_setg(errp, "Failed to convert system time: %d", (int)GetLastError()); | |
827 | return -1; | |
828 | } | |
829 | ||
830 | time_ns = ((((int64_t)tf.dwHighDateTime << 32) | tf.dwLowDateTime) | |
831 | - W32_FT_OFFSET) * 100; | |
832 | ||
833 | return time_ns; | |
6912e6a9 LL |
834 | } |
835 | ||
2c958923 | 836 | void qmp_guest_set_time(bool has_time, int64_t time_ns, Error **errp) |
a1bca57f | 837 | { |
0f230bf7 | 838 | Error *local_err = NULL; |
b8f954fe LL |
839 | SYSTEMTIME ts; |
840 | FILETIME tf; | |
841 | LONGLONG time; | |
842 | ||
ee17cbdc MP |
843 | if (!has_time) { |
844 | /* Unfortunately, Windows libraries don't provide an easy way to access | |
845 | * RTC yet: | |
846 | * | |
847 | * https://msdn.microsoft.com/en-us/library/aa908981.aspx | |
848 | */ | |
849 | error_setg(errp, "Time argument is required on this platform"); | |
850 | return; | |
851 | } | |
852 | ||
853 | /* Validate time passed by user. */ | |
854 | if (time_ns < 0 || time_ns / 100 > INT64_MAX - W32_FT_OFFSET) { | |
855 | error_setg(errp, "Time %" PRId64 "is invalid", time_ns); | |
856 | return; | |
857 | } | |
b8f954fe | 858 | |
ee17cbdc | 859 | time = time_ns / 100 + W32_FT_OFFSET; |
b8f954fe | 860 | |
ee17cbdc MP |
861 | tf.dwLowDateTime = (DWORD) time; |
862 | tf.dwHighDateTime = (DWORD) (time >> 32); | |
b8f954fe | 863 | |
ee17cbdc MP |
864 | if (!FileTimeToSystemTime(&tf, &ts)) { |
865 | error_setg(errp, "Failed to convert system time %d", | |
866 | (int)GetLastError()); | |
867 | return; | |
b8f954fe LL |
868 | } |
869 | ||
0f230bf7 MA |
870 | acquire_privilege(SE_SYSTEMTIME_NAME, &local_err); |
871 | if (local_err) { | |
872 | error_propagate(errp, local_err); | |
b8f954fe LL |
873 | return; |
874 | } | |
875 | ||
876 | if (!SetSystemTime(&ts)) { | |
877 | error_setg(errp, "Failed to set time to guest: %d", (int)GetLastError()); | |
878 | return; | |
879 | } | |
a1bca57f LL |
880 | } |
881 | ||
70e133a7 LE |
882 | GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp) |
883 | { | |
c6bd8c70 | 884 | error_setg(errp, QERR_UNSUPPORTED); |
70e133a7 LE |
885 | return NULL; |
886 | } | |
887 | ||
888 | int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp) | |
889 | { | |
c6bd8c70 | 890 | error_setg(errp, QERR_UNSUPPORTED); |
70e133a7 LE |
891 | return -1; |
892 | } | |
893 | ||
215a2771 DB |
894 | void qmp_guest_set_user_password(const char *username, |
895 | const char *password, | |
896 | bool crypted, | |
897 | Error **errp) | |
898 | { | |
c6bd8c70 | 899 | error_setg(errp, QERR_UNSUPPORTED); |
215a2771 DB |
900 | } |
901 | ||
a065aaa9 HZ |
902 | GuestMemoryBlockList *qmp_guest_get_memory_blocks(Error **errp) |
903 | { | |
c6bd8c70 | 904 | error_setg(errp, QERR_UNSUPPORTED); |
a065aaa9 HZ |
905 | return NULL; |
906 | } | |
907 | ||
908 | GuestMemoryBlockResponseList * | |
909 | qmp_guest_set_memory_blocks(GuestMemoryBlockList *mem_blks, Error **errp) | |
910 | { | |
c6bd8c70 | 911 | error_setg(errp, QERR_UNSUPPORTED); |
a065aaa9 HZ |
912 | return NULL; |
913 | } | |
914 | ||
915 | GuestMemoryBlockInfo *qmp_guest_get_memory_block_info(Error **errp) | |
916 | { | |
c6bd8c70 | 917 | error_setg(errp, QERR_UNSUPPORTED); |
a065aaa9 HZ |
918 | return NULL; |
919 | } | |
920 | ||
1281c08a TS |
921 | /* add unsupported commands to the blacklist */ |
922 | GList *ga_command_blacklist_init(GList *blacklist) | |
923 | { | |
924 | const char *list_unsupported[] = { | |
d6c5528b | 925 | "guest-suspend-hybrid", |
1281c08a | 926 | "guest-get-vcpus", "guest-set-vcpus", |
215a2771 | 927 | "guest-set-user-password", |
0dd38a03 HZ |
928 | "guest-get-memory-blocks", "guest-set-memory-blocks", |
929 | "guest-get-memory-block-size", | |
1281c08a TS |
930 | "guest-fsfreeze-freeze-list", "guest-get-fsinfo", |
931 | "guest-fstrim", NULL}; | |
932 | char **p = (char **)list_unsupported; | |
933 | ||
934 | while (*p) { | |
935 | blacklist = g_list_append(blacklist, *p++); | |
936 | } | |
937 | ||
938 | if (!vss_init(true)) { | |
c69403fc | 939 | g_debug("vss_init failed, vss commands are going to be disabled"); |
1281c08a TS |
940 | const char *list[] = { |
941 | "guest-get-fsinfo", "guest-fsfreeze-status", | |
942 | "guest-fsfreeze-freeze", "guest-fsfreeze-thaw", NULL}; | |
943 | p = (char **)list; | |
944 | ||
945 | while (*p) { | |
946 | blacklist = g_list_append(blacklist, *p++); | |
947 | } | |
948 | } | |
949 | ||
950 | return blacklist; | |
951 | } | |
952 | ||
d8ca685a MR |
953 | /* register init/cleanup routines for stateful command groups */ |
954 | void ga_command_state_init(GAState *s, GACommandState *cs) | |
955 | { | |
1281c08a | 956 | if (!vss_initialized()) { |
64c00317 TS |
957 | ga_command_state_add(cs, NULL, guest_fsfreeze_cleanup); |
958 | } | |
fa193594 | 959 | ga_command_state_add(cs, guest_file_init, NULL); |
d8ca685a | 960 | } |