]>
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 <mdroth@linux.vnet.ibm.com> | |
aa59637e | 8 | * Gal Hammer <ghammer@redhat.com> |
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> | |
a3ef3b22 OK |
23 | #ifdef CONFIG_QGA_NTDDSCSI |
24 | #include <winioctl.h> | |
25 | #include <ntddscsi.h> | |
26 | #endif | |
d8ca685a | 27 | #include "qga/guest-agent-core.h" |
64c00317 | 28 | #include "qga/vss-win32.h" |
d8ca685a | 29 | #include "qga-qmp-commands.h" |
7b1b5d19 | 30 | #include "qapi/qmp/qerror.h" |
fa193594 | 31 | #include "qemu/queue.h" |
d6c5528b | 32 | #include "qemu/host-utils.h" |
d8ca685a | 33 | |
546b60d0 MR |
34 | #ifndef SHTDN_REASON_FLAG_PLANNED |
35 | #define SHTDN_REASON_FLAG_PLANNED 0x80000000 | |
36 | #endif | |
37 | ||
3f2a6087 LL |
38 | /* multiple of 100 nanoseconds elapsed between windows baseline |
39 | * (1/1/1601) and Unix Epoch (1/1/1970), accounting for leap years */ | |
40 | #define W32_FT_OFFSET (10000000ULL * 60 * 60 * 24 * \ | |
41 | (365 * (1970 - 1601) + \ | |
42 | (1970 - 1601) / 4 - 3)) | |
43 | ||
fa193594 OK |
44 | #define INVALID_SET_FILE_POINTER ((DWORD)-1) |
45 | ||
46 | typedef struct GuestFileHandle { | |
47 | int64_t id; | |
48 | HANDLE fh; | |
49 | QTAILQ_ENTRY(GuestFileHandle) next; | |
50 | } GuestFileHandle; | |
51 | ||
52 | static struct { | |
53 | QTAILQ_HEAD(, GuestFileHandle) filehandles; | |
54 | } guest_file_state; | |
55 | ||
56 | ||
57 | typedef struct OpenFlags { | |
58 | const char *forms; | |
59 | DWORD desired_access; | |
60 | DWORD creation_disposition; | |
61 | } OpenFlags; | |
62 | static OpenFlags guest_file_open_modes[] = { | |
63 | {"r", GENERIC_READ, OPEN_EXISTING}, | |
64 | {"rb", GENERIC_READ, OPEN_EXISTING}, | |
65 | {"w", GENERIC_WRITE, CREATE_ALWAYS}, | |
66 | {"wb", GENERIC_WRITE, CREATE_ALWAYS}, | |
67 | {"a", GENERIC_WRITE, OPEN_ALWAYS }, | |
68 | {"r+", GENERIC_WRITE|GENERIC_READ, OPEN_EXISTING}, | |
69 | {"rb+", GENERIC_WRITE|GENERIC_READ, OPEN_EXISTING}, | |
70 | {"r+b", GENERIC_WRITE|GENERIC_READ, OPEN_EXISTING}, | |
71 | {"w+", GENERIC_WRITE|GENERIC_READ, CREATE_ALWAYS}, | |
72 | {"wb+", GENERIC_WRITE|GENERIC_READ, CREATE_ALWAYS}, | |
73 | {"w+b", GENERIC_WRITE|GENERIC_READ, CREATE_ALWAYS}, | |
74 | {"a+", GENERIC_WRITE|GENERIC_READ, OPEN_ALWAYS }, | |
75 | {"ab+", GENERIC_WRITE|GENERIC_READ, OPEN_ALWAYS }, | |
76 | {"a+b", GENERIC_WRITE|GENERIC_READ, OPEN_ALWAYS } | |
77 | }; | |
78 | ||
79 | static OpenFlags *find_open_flag(const char *mode_str) | |
80 | { | |
81 | int mode; | |
82 | Error **errp = NULL; | |
83 | ||
84 | for (mode = 0; mode < ARRAY_SIZE(guest_file_open_modes); ++mode) { | |
85 | OpenFlags *flags = guest_file_open_modes + mode; | |
86 | ||
87 | if (strcmp(flags->forms, mode_str) == 0) { | |
88 | return flags; | |
89 | } | |
90 | } | |
91 | ||
92 | error_setg(errp, "invalid file open mode '%s'", mode_str); | |
93 | return NULL; | |
94 | } | |
95 | ||
96 | static int64_t guest_file_handle_add(HANDLE fh, Error **errp) | |
97 | { | |
98 | GuestFileHandle *gfh; | |
99 | int64_t handle; | |
100 | ||
101 | handle = ga_get_fd_handle(ga_state, errp); | |
102 | if (handle < 0) { | |
103 | return -1; | |
104 | } | |
105 | gfh = g_malloc0(sizeof(GuestFileHandle)); | |
106 | gfh->id = handle; | |
107 | gfh->fh = fh; | |
108 | QTAILQ_INSERT_TAIL(&guest_file_state.filehandles, gfh, next); | |
109 | ||
110 | return handle; | |
111 | } | |
112 | ||
113 | static GuestFileHandle *guest_file_handle_find(int64_t id, Error **errp) | |
114 | { | |
115 | GuestFileHandle *gfh; | |
116 | QTAILQ_FOREACH(gfh, &guest_file_state.filehandles, next) { | |
117 | if (gfh->id == id) { | |
118 | return gfh; | |
119 | } | |
120 | } | |
121 | error_setg(errp, "handle '%" PRId64 "' has not been found", id); | |
122 | return NULL; | |
123 | } | |
124 | ||
125 | int64_t qmp_guest_file_open(const char *path, bool has_mode, | |
126 | const char *mode, Error **errp) | |
127 | { | |
128 | int64_t fd; | |
129 | HANDLE fh; | |
130 | HANDLE templ_file = NULL; | |
131 | DWORD share_mode = FILE_SHARE_READ; | |
132 | DWORD flags_and_attr = FILE_ATTRIBUTE_NORMAL; | |
133 | LPSECURITY_ATTRIBUTES sa_attr = NULL; | |
134 | OpenFlags *guest_flags; | |
135 | ||
136 | if (!has_mode) { | |
137 | mode = "r"; | |
138 | } | |
139 | slog("guest-file-open called, filepath: %s, mode: %s", path, mode); | |
140 | guest_flags = find_open_flag(mode); | |
141 | if (guest_flags == NULL) { | |
142 | error_setg(errp, "invalid file open mode"); | |
143 | return -1; | |
144 | } | |
145 | ||
146 | fh = CreateFile(path, guest_flags->desired_access, share_mode, sa_attr, | |
147 | guest_flags->creation_disposition, flags_and_attr, | |
148 | templ_file); | |
149 | if (fh == INVALID_HANDLE_VALUE) { | |
150 | error_setg_win32(errp, GetLastError(), "failed to open file '%s'", | |
151 | path); | |
152 | return -1; | |
153 | } | |
154 | ||
155 | fd = guest_file_handle_add(fh, errp); | |
156 | if (fd < 0) { | |
157 | CloseHandle(&fh); | |
158 | error_setg(errp, "failed to add handle to qmp handle table"); | |
159 | return -1; | |
160 | } | |
161 | ||
162 | slog("guest-file-open, handle: % " PRId64, fd); | |
163 | return fd; | |
164 | } | |
165 | ||
166 | void qmp_guest_file_close(int64_t handle, Error **errp) | |
167 | { | |
168 | bool ret; | |
169 | GuestFileHandle *gfh = guest_file_handle_find(handle, errp); | |
170 | slog("guest-file-close called, handle: %" PRId64, handle); | |
171 | if (gfh == NULL) { | |
172 | return; | |
173 | } | |
174 | ret = CloseHandle(gfh->fh); | |
175 | if (!ret) { | |
176 | error_setg_win32(errp, GetLastError(), "failed close handle"); | |
177 | return; | |
178 | } | |
179 | ||
180 | QTAILQ_REMOVE(&guest_file_state.filehandles, gfh, next); | |
181 | g_free(gfh); | |
182 | } | |
183 | ||
77dbc81b | 184 | static void acquire_privilege(const char *name, Error **errp) |
d8ca685a | 185 | { |
374044f0 | 186 | HANDLE token = NULL; |
546b60d0 | 187 | TOKEN_PRIVILEGES priv; |
aa59637e GH |
188 | Error *local_err = NULL; |
189 | ||
aa59637e GH |
190 | if (OpenProcessToken(GetCurrentProcess(), |
191 | TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &token)) | |
192 | { | |
193 | if (!LookupPrivilegeValue(NULL, name, &priv.Privileges[0].Luid)) { | |
c6bd8c70 MA |
194 | error_setg(&local_err, QERR_QGA_COMMAND_FAILED, |
195 | "no luid for requested privilege"); | |
aa59637e GH |
196 | goto out; |
197 | } | |
198 | ||
199 | priv.PrivilegeCount = 1; | |
200 | priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; | |
201 | ||
202 | if (!AdjustTokenPrivileges(token, FALSE, &priv, 0, NULL, 0)) { | |
c6bd8c70 MA |
203 | error_setg(&local_err, QERR_QGA_COMMAND_FAILED, |
204 | "unable to acquire requested privilege"); | |
aa59637e GH |
205 | goto out; |
206 | } | |
207 | ||
aa59637e | 208 | } else { |
c6bd8c70 MA |
209 | error_setg(&local_err, QERR_QGA_COMMAND_FAILED, |
210 | "failed to open privilege token"); | |
aa59637e GH |
211 | } |
212 | ||
213 | out: | |
374044f0 GA |
214 | if (token) { |
215 | CloseHandle(token); | |
216 | } | |
aa59637e | 217 | if (local_err) { |
77dbc81b | 218 | error_propagate(errp, local_err); |
aa59637e GH |
219 | } |
220 | } | |
221 | ||
77dbc81b MA |
222 | static void execute_async(DWORD WINAPI (*func)(LPVOID), LPVOID opaque, |
223 | Error **errp) | |
aa59637e GH |
224 | { |
225 | Error *local_err = NULL; | |
226 | ||
aa59637e GH |
227 | HANDLE thread = CreateThread(NULL, 0, func, opaque, 0, NULL); |
228 | if (!thread) { | |
c6bd8c70 MA |
229 | error_setg(&local_err, QERR_QGA_COMMAND_FAILED, |
230 | "failed to dispatch asynchronous command"); | |
77dbc81b | 231 | error_propagate(errp, local_err); |
aa59637e GH |
232 | } |
233 | } | |
234 | ||
77dbc81b | 235 | void qmp_guest_shutdown(bool has_mode, const char *mode, Error **errp) |
aa59637e | 236 | { |
0f230bf7 | 237 | Error *local_err = NULL; |
546b60d0 MR |
238 | UINT shutdown_flag = EWX_FORCE; |
239 | ||
240 | slog("guest-shutdown called, mode: %s", mode); | |
241 | ||
242 | if (!has_mode || strcmp(mode, "powerdown") == 0) { | |
243 | shutdown_flag |= EWX_POWEROFF; | |
244 | } else if (strcmp(mode, "halt") == 0) { | |
245 | shutdown_flag |= EWX_SHUTDOWN; | |
246 | } else if (strcmp(mode, "reboot") == 0) { | |
247 | shutdown_flag |= EWX_REBOOT; | |
248 | } else { | |
c6bd8c70 MA |
249 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "mode", |
250 | "halt|powerdown|reboot"); | |
546b60d0 MR |
251 | return; |
252 | } | |
253 | ||
254 | /* Request a shutdown privilege, but try to shut down the system | |
255 | anyway. */ | |
0f230bf7 MA |
256 | acquire_privilege(SE_SHUTDOWN_NAME, &local_err); |
257 | if (local_err) { | |
258 | error_propagate(errp, local_err); | |
aa59637e | 259 | return; |
546b60d0 MR |
260 | } |
261 | ||
262 | if (!ExitWindowsEx(shutdown_flag, SHTDN_REASON_FLAG_PLANNED)) { | |
16f4e8fa | 263 | slog("guest-shutdown failed: %lu", GetLastError()); |
c6bd8c70 | 264 | error_setg(errp, QERR_UNDEFINED_ERROR); |
546b60d0 | 265 | } |
d8ca685a MR |
266 | } |
267 | ||
d8ca685a | 268 | GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count, |
77dbc81b | 269 | int64_t count, Error **errp) |
d8ca685a | 270 | { |
fa193594 OK |
271 | GuestFileRead *read_data = NULL; |
272 | guchar *buf; | |
273 | HANDLE fh; | |
274 | bool is_ok; | |
275 | DWORD read_count; | |
276 | GuestFileHandle *gfh = guest_file_handle_find(handle, errp); | |
277 | ||
278 | if (!gfh) { | |
279 | return NULL; | |
280 | } | |
281 | if (!has_count) { | |
282 | count = QGA_READ_COUNT_DEFAULT; | |
283 | } else if (count < 0) { | |
284 | error_setg(errp, "value '%" PRId64 | |
285 | "' is invalid for argument count", count); | |
286 | return NULL; | |
287 | } | |
288 | ||
289 | fh = gfh->fh; | |
290 | buf = g_malloc0(count+1); | |
291 | is_ok = ReadFile(fh, buf, count, &read_count, NULL); | |
292 | if (!is_ok) { | |
293 | error_setg_win32(errp, GetLastError(), "failed to read file"); | |
294 | slog("guest-file-read failed, handle %" PRId64, handle); | |
295 | } else { | |
296 | buf[read_count] = 0; | |
297 | read_data = g_malloc0(sizeof(GuestFileRead)); | |
298 | read_data->count = (size_t)read_count; | |
299 | read_data->eof = read_count == 0; | |
300 | ||
301 | if (read_count != 0) { | |
302 | read_data->buf_b64 = g_base64_encode(buf, read_count); | |
303 | } | |
304 | } | |
305 | g_free(buf); | |
306 | ||
307 | return read_data; | |
d8ca685a MR |
308 | } |
309 | ||
310 | GuestFileWrite *qmp_guest_file_write(int64_t handle, const char *buf_b64, | |
77dbc81b MA |
311 | bool has_count, int64_t count, |
312 | Error **errp) | |
d8ca685a | 313 | { |
fa193594 OK |
314 | GuestFileWrite *write_data = NULL; |
315 | guchar *buf; | |
316 | gsize buf_len; | |
317 | bool is_ok; | |
318 | DWORD write_count; | |
319 | GuestFileHandle *gfh = guest_file_handle_find(handle, errp); | |
320 | HANDLE fh; | |
321 | ||
322 | if (!gfh) { | |
323 | return NULL; | |
324 | } | |
325 | fh = gfh->fh; | |
326 | buf = g_base64_decode(buf_b64, &buf_len); | |
327 | ||
328 | if (!has_count) { | |
329 | count = buf_len; | |
330 | } else if (count < 0 || count > buf_len) { | |
331 | error_setg(errp, "value '%" PRId64 | |
332 | "' is invalid for argument count", count); | |
333 | goto done; | |
334 | } | |
335 | ||
336 | is_ok = WriteFile(fh, buf, count, &write_count, NULL); | |
337 | if (!is_ok) { | |
338 | error_setg_win32(errp, GetLastError(), "failed to write to file"); | |
339 | slog("guest-file-write-failed, handle: %" PRId64, handle); | |
340 | } else { | |
341 | write_data = g_malloc0(sizeof(GuestFileWrite)); | |
342 | write_data->count = (size_t) write_count; | |
343 | } | |
344 | ||
345 | done: | |
346 | g_free(buf); | |
347 | return write_data; | |
d8ca685a MR |
348 | } |
349 | ||
350 | GuestFileSeek *qmp_guest_file_seek(int64_t handle, int64_t offset, | |
77dbc81b | 351 | int64_t whence, Error **errp) |
d8ca685a | 352 | { |
fa193594 OK |
353 | GuestFileHandle *gfh; |
354 | GuestFileSeek *seek_data; | |
355 | HANDLE fh; | |
356 | LARGE_INTEGER new_pos, off_pos; | |
357 | off_pos.QuadPart = offset; | |
358 | BOOL res; | |
359 | gfh = guest_file_handle_find(handle, errp); | |
360 | if (!gfh) { | |
361 | return NULL; | |
362 | } | |
363 | ||
364 | fh = gfh->fh; | |
365 | res = SetFilePointerEx(fh, off_pos, &new_pos, whence); | |
366 | if (!res) { | |
367 | error_setg_win32(errp, GetLastError(), "failed to seek file"); | |
368 | return NULL; | |
369 | } | |
370 | seek_data = g_new0(GuestFileSeek, 1); | |
371 | seek_data->position = new_pos.QuadPart; | |
372 | return seek_data; | |
d8ca685a MR |
373 | } |
374 | ||
77dbc81b | 375 | void qmp_guest_file_flush(int64_t handle, Error **errp) |
d8ca685a | 376 | { |
fa193594 OK |
377 | HANDLE fh; |
378 | GuestFileHandle *gfh = guest_file_handle_find(handle, errp); | |
379 | if (!gfh) { | |
380 | return; | |
381 | } | |
382 | ||
383 | fh = gfh->fh; | |
384 | if (!FlushFileBuffers(fh)) { | |
385 | error_setg_win32(errp, GetLastError(), "failed to flush file"); | |
386 | } | |
387 | } | |
388 | ||
389 | static void guest_file_init(void) | |
390 | { | |
391 | QTAILQ_INIT(&guest_file_state.filehandles); | |
d8ca685a MR |
392 | } |
393 | ||
a3ef3b22 OK |
394 | #ifdef CONFIG_QGA_NTDDSCSI |
395 | ||
396 | static STORAGE_BUS_TYPE win2qemu[] = { | |
397 | [BusTypeUnknown] = GUEST_DISK_BUS_TYPE_UNKNOWN, | |
398 | [BusTypeScsi] = GUEST_DISK_BUS_TYPE_SCSI, | |
399 | [BusTypeAtapi] = GUEST_DISK_BUS_TYPE_IDE, | |
400 | [BusTypeAta] = GUEST_DISK_BUS_TYPE_IDE, | |
401 | [BusType1394] = GUEST_DISK_BUS_TYPE_IEEE1394, | |
402 | [BusTypeSsa] = GUEST_DISK_BUS_TYPE_SSA, | |
403 | [BusTypeFibre] = GUEST_DISK_BUS_TYPE_SSA, | |
404 | [BusTypeUsb] = GUEST_DISK_BUS_TYPE_USB, | |
405 | [BusTypeRAID] = GUEST_DISK_BUS_TYPE_RAID, | |
406 | #if (_WIN32_WINNT >= 0x0600) | |
407 | [BusTypeiScsi] = GUEST_DISK_BUS_TYPE_ISCSI, | |
408 | [BusTypeSas] = GUEST_DISK_BUS_TYPE_SAS, | |
409 | [BusTypeSata] = GUEST_DISK_BUS_TYPE_SATA, | |
410 | [BusTypeSd] = GUEST_DISK_BUS_TYPE_SD, | |
411 | [BusTypeMmc] = GUEST_DISK_BUS_TYPE_MMC, | |
412 | #endif | |
413 | #if (_WIN32_WINNT >= 0x0601) | |
414 | [BusTypeVirtual] = GUEST_DISK_BUS_TYPE_VIRTUAL, | |
415 | [BusTypeFileBackedVirtual] = GUEST_DISK_BUS_TYPE_FILE_BACKED_VIRTUAL, | |
416 | #endif | |
417 | }; | |
418 | ||
419 | static GuestDiskBusType find_bus_type(STORAGE_BUS_TYPE bus) | |
420 | { | |
421 | if (bus > ARRAY_SIZE(win2qemu) || (int)bus < 0) { | |
422 | return GUEST_DISK_BUS_TYPE_UNKNOWN; | |
423 | } | |
424 | return win2qemu[(int)bus]; | |
425 | } | |
426 | ||
427 | static GuestPCIAddress *get_pci_info(char *guid, Error **errp) | |
428 | { | |
429 | return NULL; | |
430 | } | |
431 | ||
432 | static int get_disk_bus_type(HANDLE vol_h, Error **errp) | |
433 | { | |
434 | STORAGE_PROPERTY_QUERY query; | |
435 | STORAGE_DEVICE_DESCRIPTOR *dev_desc, buf; | |
436 | DWORD received; | |
437 | ||
438 | dev_desc = &buf; | |
439 | dev_desc->Size = sizeof(buf); | |
440 | query.PropertyId = StorageDeviceProperty; | |
441 | query.QueryType = PropertyStandardQuery; | |
442 | ||
443 | if (!DeviceIoControl(vol_h, IOCTL_STORAGE_QUERY_PROPERTY, &query, | |
444 | sizeof(STORAGE_PROPERTY_QUERY), dev_desc, | |
445 | dev_desc->Size, &received, NULL)) { | |
446 | error_setg_win32(errp, GetLastError(), "failed to get bus type"); | |
447 | return -1; | |
448 | } | |
449 | ||
450 | return dev_desc->BusType; | |
451 | } | |
452 | ||
453 | /* VSS provider works with volumes, thus there is no difference if | |
454 | * the volume consist of spanned disks. Info about the first disk in the | |
455 | * volume is returned for the spanned disk group (LVM) */ | |
456 | static GuestDiskAddressList *build_guest_disk_info(char *guid, Error **errp) | |
457 | { | |
458 | GuestDiskAddressList *list = NULL; | |
459 | GuestDiskAddress *disk; | |
460 | SCSI_ADDRESS addr, *scsi_ad; | |
461 | DWORD len; | |
462 | int bus; | |
463 | HANDLE vol_h; | |
464 | ||
465 | scsi_ad = &addr; | |
466 | char *name = g_strndup(guid, strlen(guid)-1); | |
467 | ||
468 | vol_h = CreateFile(name, 0, FILE_SHARE_READ, NULL, OPEN_EXISTING, | |
469 | 0, NULL); | |
470 | if (vol_h == INVALID_HANDLE_VALUE) { | |
471 | error_setg_win32(errp, GetLastError(), "failed to open volume"); | |
472 | goto out_free; | |
473 | } | |
474 | ||
475 | bus = get_disk_bus_type(vol_h, errp); | |
476 | if (bus < 0) { | |
477 | goto out_close; | |
478 | } | |
479 | ||
480 | disk = g_malloc0(sizeof(*disk)); | |
481 | disk->bus_type = find_bus_type(bus); | |
482 | if (bus == BusTypeScsi || bus == BusTypeAta || bus == BusTypeRAID | |
483 | #if (_WIN32_WINNT >= 0x0600) | |
484 | /* This bus type is not supported before Windows Server 2003 SP1 */ | |
485 | || bus == BusTypeSas | |
486 | #endif | |
487 | ) { | |
488 | /* We are able to use the same ioctls for different bus types | |
489 | * according to Microsoft docs | |
490 | * https://technet.microsoft.com/en-us/library/ee851589(v=ws.10).aspx */ | |
491 | if (DeviceIoControl(vol_h, IOCTL_SCSI_GET_ADDRESS, NULL, 0, scsi_ad, | |
492 | sizeof(SCSI_ADDRESS), &len, NULL)) { | |
493 | disk->unit = addr.Lun; | |
494 | disk->target = addr.TargetId; | |
495 | disk->bus = addr.PathId; | |
496 | disk->pci_controller = get_pci_info(name, errp); | |
497 | } | |
498 | /* We do not set error in this case, because we still have enough | |
499 | * information about volume. */ | |
500 | } else { | |
501 | disk->pci_controller = NULL; | |
502 | } | |
503 | ||
504 | list = g_malloc0(sizeof(*list)); | |
505 | list->value = disk; | |
506 | list->next = NULL; | |
507 | out_close: | |
508 | CloseHandle(vol_h); | |
509 | out_free: | |
510 | g_free(name); | |
511 | return list; | |
512 | } | |
513 | ||
514 | #else | |
515 | ||
516 | static GuestDiskAddressList *build_guest_disk_info(char *guid, Error **errp) | |
517 | { | |
518 | return NULL; | |
519 | } | |
520 | ||
521 | #endif /* CONFIG_QGA_NTDDSCSI */ | |
522 | ||
d2b3f390 OK |
523 | static GuestFilesystemInfo *build_guest_fsinfo(char *guid, Error **errp) |
524 | { | |
525 | DWORD info_size; | |
526 | char mnt, *mnt_point; | |
527 | char fs_name[32]; | |
528 | char vol_info[MAX_PATH+1]; | |
529 | size_t len; | |
530 | GuestFilesystemInfo *fs = NULL; | |
531 | ||
532 | GetVolumePathNamesForVolumeName(guid, (LPCH)&mnt, 0, &info_size); | |
533 | if (GetLastError() != ERROR_MORE_DATA) { | |
534 | error_setg_win32(errp, GetLastError(), "failed to get volume name"); | |
535 | return NULL; | |
536 | } | |
537 | ||
538 | mnt_point = g_malloc(info_size + 1); | |
539 | if (!GetVolumePathNamesForVolumeName(guid, mnt_point, info_size, | |
540 | &info_size)) { | |
541 | error_setg_win32(errp, GetLastError(), "failed to get volume name"); | |
542 | goto free; | |
543 | } | |
544 | ||
545 | len = strlen(mnt_point); | |
546 | mnt_point[len] = '\\'; | |
547 | mnt_point[len+1] = 0; | |
548 | if (!GetVolumeInformation(mnt_point, vol_info, sizeof(vol_info), NULL, NULL, | |
549 | NULL, (LPSTR)&fs_name, sizeof(fs_name))) { | |
550 | if (GetLastError() != ERROR_NOT_READY) { | |
551 | error_setg_win32(errp, GetLastError(), "failed to get volume info"); | |
552 | } | |
553 | goto free; | |
554 | } | |
555 | ||
556 | fs_name[sizeof(fs_name) - 1] = 0; | |
557 | fs = g_malloc(sizeof(*fs)); | |
558 | fs->name = g_strdup(guid); | |
559 | if (len == 0) { | |
560 | fs->mountpoint = g_strdup("System Reserved"); | |
561 | } else { | |
562 | fs->mountpoint = g_strndup(mnt_point, len); | |
563 | } | |
564 | fs->type = g_strdup(fs_name); | |
a3ef3b22 | 565 | fs->disk = build_guest_disk_info(guid, errp);; |
d2b3f390 OK |
566 | free: |
567 | g_free(mnt_point); | |
568 | return fs; | |
569 | } | |
570 | ||
46d4c572 TS |
571 | GuestFilesystemInfoList *qmp_guest_get_fsinfo(Error **errp) |
572 | { | |
ef0a03f2 OK |
573 | HANDLE vol_h; |
574 | GuestFilesystemInfoList *new, *ret = NULL; | |
575 | char guid[256]; | |
576 | ||
577 | vol_h = FindFirstVolume(guid, sizeof(guid)); | |
578 | if (vol_h == INVALID_HANDLE_VALUE) { | |
579 | error_setg_win32(errp, GetLastError(), "failed to find any volume"); | |
580 | return NULL; | |
581 | } | |
582 | ||
583 | do { | |
d2b3f390 OK |
584 | GuestFilesystemInfo *info = build_guest_fsinfo(guid, errp); |
585 | if (info == NULL) { | |
586 | continue; | |
587 | } | |
ef0a03f2 | 588 | new = g_malloc(sizeof(*ret)); |
d2b3f390 | 589 | new->value = info; |
ef0a03f2 OK |
590 | new->next = ret; |
591 | ret = new; | |
592 | } while (FindNextVolume(vol_h, guid, sizeof(guid))); | |
593 | ||
594 | if (GetLastError() != ERROR_NO_MORE_FILES) { | |
595 | error_setg_win32(errp, GetLastError(), "failed to find next volume"); | |
596 | } | |
597 | ||
598 | FindVolumeClose(vol_h); | |
599 | return ret; | |
46d4c572 TS |
600 | } |
601 | ||
d8ca685a MR |
602 | /* |
603 | * Return status of freeze/thaw | |
604 | */ | |
77dbc81b | 605 | GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **errp) |
d8ca685a | 606 | { |
64c00317 | 607 | if (!vss_initialized()) { |
c6bd8c70 | 608 | error_setg(errp, QERR_UNSUPPORTED); |
64c00317 TS |
609 | return 0; |
610 | } | |
611 | ||
612 | if (ga_is_frozen(ga_state)) { | |
613 | return GUEST_FSFREEZE_STATUS_FROZEN; | |
614 | } | |
615 | ||
616 | return GUEST_FSFREEZE_STATUS_THAWED; | |
d8ca685a MR |
617 | } |
618 | ||
619 | /* | |
64c00317 TS |
620 | * Freeze local file systems using Volume Shadow-copy Service. |
621 | * The frozen state is limited for up to 10 seconds by VSS. | |
d8ca685a | 622 | */ |
77dbc81b | 623 | int64_t qmp_guest_fsfreeze_freeze(Error **errp) |
d8ca685a | 624 | { |
64c00317 TS |
625 | int i; |
626 | Error *local_err = NULL; | |
627 | ||
628 | if (!vss_initialized()) { | |
c6bd8c70 | 629 | error_setg(errp, QERR_UNSUPPORTED); |
64c00317 TS |
630 | return 0; |
631 | } | |
632 | ||
633 | slog("guest-fsfreeze called"); | |
634 | ||
635 | /* cannot risk guest agent blocking itself on a write in this state */ | |
636 | ga_set_frozen(ga_state); | |
637 | ||
0f230bf7 MA |
638 | qga_vss_fsfreeze(&i, &local_err, true); |
639 | if (local_err) { | |
640 | error_propagate(errp, local_err); | |
64c00317 TS |
641 | goto error; |
642 | } | |
643 | ||
644 | return i; | |
645 | ||
646 | error: | |
0f230bf7 | 647 | local_err = NULL; |
64c00317 | 648 | qmp_guest_fsfreeze_thaw(&local_err); |
84d18f06 | 649 | if (local_err) { |
64c00317 TS |
650 | g_debug("cleanup thaw: %s", error_get_pretty(local_err)); |
651 | error_free(local_err); | |
652 | } | |
d8ca685a MR |
653 | return 0; |
654 | } | |
655 | ||
e99bce20 TS |
656 | int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints, |
657 | strList *mountpoints, | |
658 | Error **errp) | |
659 | { | |
c6bd8c70 | 660 | error_setg(errp, QERR_UNSUPPORTED); |
e99bce20 TS |
661 | |
662 | return 0; | |
663 | } | |
664 | ||
d8ca685a | 665 | /* |
64c00317 | 666 | * Thaw local file systems using Volume Shadow-copy Service. |
d8ca685a | 667 | */ |
77dbc81b | 668 | int64_t qmp_guest_fsfreeze_thaw(Error **errp) |
d8ca685a | 669 | { |
64c00317 TS |
670 | int i; |
671 | ||
672 | if (!vss_initialized()) { | |
c6bd8c70 | 673 | error_setg(errp, QERR_UNSUPPORTED); |
64c00317 TS |
674 | return 0; |
675 | } | |
676 | ||
77dbc81b | 677 | qga_vss_fsfreeze(&i, errp, false); |
64c00317 TS |
678 | |
679 | ga_unset_frozen(ga_state); | |
680 | return i; | |
681 | } | |
682 | ||
683 | static void guest_fsfreeze_cleanup(void) | |
684 | { | |
685 | Error *err = NULL; | |
686 | ||
687 | if (!vss_initialized()) { | |
688 | return; | |
689 | } | |
690 | ||
691 | if (ga_is_frozen(ga_state) == GUEST_FSFREEZE_STATUS_FROZEN) { | |
692 | qmp_guest_fsfreeze_thaw(&err); | |
693 | if (err) { | |
694 | slog("failed to clean up frozen filesystems: %s", | |
695 | error_get_pretty(err)); | |
696 | error_free(err); | |
697 | } | |
698 | } | |
699 | ||
700 | vss_deinit(true); | |
d8ca685a MR |
701 | } |
702 | ||
eab5fd59 PB |
703 | /* |
704 | * Walk list of mounted file systems in the guest, and discard unused | |
705 | * areas. | |
706 | */ | |
e82855d9 JO |
707 | GuestFilesystemTrimResponse * |
708 | qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **errp) | |
eab5fd59 | 709 | { |
c6bd8c70 | 710 | error_setg(errp, QERR_UNSUPPORTED); |
e82855d9 | 711 | return NULL; |
eab5fd59 PB |
712 | } |
713 | ||
aa59637e | 714 | typedef enum { |
f54603b6 MR |
715 | GUEST_SUSPEND_MODE_DISK, |
716 | GUEST_SUSPEND_MODE_RAM | |
aa59637e GH |
717 | } GuestSuspendMode; |
718 | ||
77dbc81b | 719 | static void check_suspend_mode(GuestSuspendMode mode, Error **errp) |
aa59637e GH |
720 | { |
721 | SYSTEM_POWER_CAPABILITIES sys_pwr_caps; | |
722 | Error *local_err = NULL; | |
723 | ||
aa59637e GH |
724 | ZeroMemory(&sys_pwr_caps, sizeof(sys_pwr_caps)); |
725 | if (!GetPwrCapabilities(&sys_pwr_caps)) { | |
c6bd8c70 MA |
726 | error_setg(&local_err, QERR_QGA_COMMAND_FAILED, |
727 | "failed to determine guest suspend capabilities"); | |
aa59637e GH |
728 | goto out; |
729 | } | |
730 | ||
f54603b6 MR |
731 | switch (mode) { |
732 | case GUEST_SUSPEND_MODE_DISK: | |
733 | if (!sys_pwr_caps.SystemS4) { | |
c6bd8c70 MA |
734 | error_setg(&local_err, QERR_QGA_COMMAND_FAILED, |
735 | "suspend-to-disk not supported by OS"); | |
aa59637e | 736 | } |
f54603b6 MR |
737 | break; |
738 | case GUEST_SUSPEND_MODE_RAM: | |
739 | if (!sys_pwr_caps.SystemS3) { | |
c6bd8c70 MA |
740 | error_setg(&local_err, QERR_QGA_COMMAND_FAILED, |
741 | "suspend-to-ram not supported by OS"); | |
f54603b6 MR |
742 | } |
743 | break; | |
744 | default: | |
c6bd8c70 MA |
745 | error_setg(&local_err, QERR_INVALID_PARAMETER_VALUE, "mode", |
746 | "GuestSuspendMode"); | |
aa59637e GH |
747 | } |
748 | ||
aa59637e GH |
749 | out: |
750 | if (local_err) { | |
77dbc81b | 751 | error_propagate(errp, local_err); |
aa59637e GH |
752 | } |
753 | } | |
754 | ||
755 | static DWORD WINAPI do_suspend(LPVOID opaque) | |
756 | { | |
757 | GuestSuspendMode *mode = opaque; | |
758 | DWORD ret = 0; | |
759 | ||
760 | if (!SetSuspendState(*mode == GUEST_SUSPEND_MODE_DISK, TRUE, TRUE)) { | |
16f4e8fa | 761 | slog("failed to suspend guest, %lu", GetLastError()); |
aa59637e GH |
762 | ret = -1; |
763 | } | |
764 | g_free(mode); | |
765 | return ret; | |
766 | } | |
767 | ||
77dbc81b | 768 | void qmp_guest_suspend_disk(Error **errp) |
11d0f125 | 769 | { |
0f230bf7 | 770 | Error *local_err = NULL; |
aa59637e GH |
771 | GuestSuspendMode *mode = g_malloc(sizeof(GuestSuspendMode)); |
772 | ||
773 | *mode = GUEST_SUSPEND_MODE_DISK; | |
0f230bf7 MA |
774 | check_suspend_mode(*mode, &local_err); |
775 | acquire_privilege(SE_SHUTDOWN_NAME, &local_err); | |
776 | execute_async(do_suspend, mode, &local_err); | |
aa59637e | 777 | |
0f230bf7 MA |
778 | if (local_err) { |
779 | error_propagate(errp, local_err); | |
aa59637e GH |
780 | g_free(mode); |
781 | } | |
11d0f125 LC |
782 | } |
783 | ||
77dbc81b | 784 | void qmp_guest_suspend_ram(Error **errp) |
fbf42210 | 785 | { |
0f230bf7 | 786 | Error *local_err = NULL; |
f54603b6 MR |
787 | GuestSuspendMode *mode = g_malloc(sizeof(GuestSuspendMode)); |
788 | ||
789 | *mode = GUEST_SUSPEND_MODE_RAM; | |
0f230bf7 MA |
790 | check_suspend_mode(*mode, &local_err); |
791 | acquire_privilege(SE_SHUTDOWN_NAME, &local_err); | |
792 | execute_async(do_suspend, mode, &local_err); | |
f54603b6 | 793 | |
0f230bf7 MA |
794 | if (local_err) { |
795 | error_propagate(errp, local_err); | |
f54603b6 MR |
796 | g_free(mode); |
797 | } | |
fbf42210 LC |
798 | } |
799 | ||
77dbc81b | 800 | void qmp_guest_suspend_hybrid(Error **errp) |
95f4f404 | 801 | { |
c6bd8c70 | 802 | error_setg(errp, QERR_UNSUPPORTED); |
95f4f404 LC |
803 | } |
804 | ||
d6c5528b | 805 | static IP_ADAPTER_ADDRESSES *guest_get_adapters_addresses(Error **errp) |
3424fc9f | 806 | { |
d6c5528b KA |
807 | IP_ADAPTER_ADDRESSES *adptr_addrs = NULL; |
808 | ULONG adptr_addrs_len = 0; | |
809 | DWORD ret; | |
810 | ||
811 | /* Call the first time to get the adptr_addrs_len. */ | |
812 | GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, | |
813 | NULL, adptr_addrs, &adptr_addrs_len); | |
814 | ||
815 | adptr_addrs = g_malloc(adptr_addrs_len); | |
816 | ret = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, | |
817 | NULL, adptr_addrs, &adptr_addrs_len); | |
818 | if (ret != ERROR_SUCCESS) { | |
819 | error_setg_win32(errp, ret, "failed to get adapters addresses"); | |
820 | g_free(adptr_addrs); | |
821 | adptr_addrs = NULL; | |
822 | } | |
823 | return adptr_addrs; | |
824 | } | |
825 | ||
826 | static char *guest_wctomb_dup(WCHAR *wstr) | |
827 | { | |
828 | char *str; | |
829 | size_t i; | |
830 | ||
831 | i = wcslen(wstr) + 1; | |
832 | str = g_malloc(i); | |
833 | WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, | |
834 | wstr, -1, str, i, NULL, NULL); | |
835 | return str; | |
836 | } | |
837 | ||
838 | static char *guest_addr_to_str(IP_ADAPTER_UNICAST_ADDRESS *ip_addr, | |
839 | Error **errp) | |
840 | { | |
841 | char addr_str[INET6_ADDRSTRLEN + INET_ADDRSTRLEN]; | |
842 | DWORD len; | |
843 | int ret; | |
844 | ||
845 | if (ip_addr->Address.lpSockaddr->sa_family == AF_INET || | |
846 | ip_addr->Address.lpSockaddr->sa_family == AF_INET6) { | |
847 | len = sizeof(addr_str); | |
848 | ret = WSAAddressToString(ip_addr->Address.lpSockaddr, | |
849 | ip_addr->Address.iSockaddrLength, | |
850 | NULL, | |
851 | addr_str, | |
852 | &len); | |
853 | if (ret != 0) { | |
854 | error_setg_win32(errp, WSAGetLastError(), | |
855 | "failed address presentation form conversion"); | |
856 | return NULL; | |
857 | } | |
858 | return g_strdup(addr_str); | |
859 | } | |
3424fc9f MP |
860 | return NULL; |
861 | } | |
862 | ||
d6c5528b KA |
863 | #if (_WIN32_WINNT >= 0x0600) |
864 | static int64_t guest_ip_prefix(IP_ADAPTER_UNICAST_ADDRESS *ip_addr) | |
865 | { | |
866 | /* For Windows Vista/2008 and newer, use the OnLinkPrefixLength | |
867 | * field to obtain the prefix. | |
868 | */ | |
869 | return ip_addr->OnLinkPrefixLength; | |
870 | } | |
871 | #else | |
872 | /* When using the Windows XP and 2003 build environment, do the best we can to | |
873 | * figure out the prefix. | |
874 | */ | |
875 | static IP_ADAPTER_INFO *guest_get_adapters_info(void) | |
876 | { | |
877 | IP_ADAPTER_INFO *adptr_info = NULL; | |
878 | ULONG adptr_info_len = 0; | |
879 | DWORD ret; | |
880 | ||
881 | /* Call the first time to get the adptr_info_len. */ | |
882 | GetAdaptersInfo(adptr_info, &adptr_info_len); | |
883 | ||
884 | adptr_info = g_malloc(adptr_info_len); | |
885 | ret = GetAdaptersInfo(adptr_info, &adptr_info_len); | |
886 | if (ret != ERROR_SUCCESS) { | |
887 | g_free(adptr_info); | |
888 | adptr_info = NULL; | |
889 | } | |
890 | return adptr_info; | |
891 | } | |
892 | ||
893 | static int64_t guest_ip_prefix(IP_ADAPTER_UNICAST_ADDRESS *ip_addr) | |
894 | { | |
895 | int64_t prefix = -1; /* Use for AF_INET6 and unknown/undetermined values. */ | |
896 | IP_ADAPTER_INFO *adptr_info, *info; | |
897 | IP_ADDR_STRING *ip; | |
898 | struct in_addr *p; | |
899 | ||
900 | if (ip_addr->Address.lpSockaddr->sa_family != AF_INET) { | |
901 | return prefix; | |
902 | } | |
903 | adptr_info = guest_get_adapters_info(); | |
904 | if (adptr_info == NULL) { | |
905 | return prefix; | |
906 | } | |
907 | ||
908 | /* Match up the passed in ip_addr with one found in adaptr_info. | |
909 | * The matching one in adptr_info will have the netmask. | |
910 | */ | |
911 | p = &((struct sockaddr_in *)ip_addr->Address.lpSockaddr)->sin_addr; | |
912 | for (info = adptr_info; info; info = info->Next) { | |
913 | for (ip = &info->IpAddressList; ip; ip = ip->Next) { | |
914 | if (p->S_un.S_addr == inet_addr(ip->IpAddress.String)) { | |
915 | prefix = ctpop32(inet_addr(ip->IpMask.String)); | |
916 | goto out; | |
917 | } | |
918 | } | |
919 | } | |
920 | out: | |
921 | g_free(adptr_info); | |
922 | return prefix; | |
923 | } | |
924 | #endif | |
925 | ||
926 | GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp) | |
927 | { | |
928 | IP_ADAPTER_ADDRESSES *adptr_addrs, *addr; | |
929 | IP_ADAPTER_UNICAST_ADDRESS *ip_addr = NULL; | |
930 | GuestNetworkInterfaceList *head = NULL, *cur_item = NULL; | |
931 | GuestIpAddressList *head_addr, *cur_addr; | |
932 | GuestNetworkInterfaceList *info; | |
933 | GuestIpAddressList *address_item = NULL; | |
934 | unsigned char *mac_addr; | |
935 | char *addr_str; | |
936 | WORD wsa_version; | |
937 | WSADATA wsa_data; | |
938 | int ret; | |
939 | ||
940 | adptr_addrs = guest_get_adapters_addresses(errp); | |
941 | if (adptr_addrs == NULL) { | |
942 | return NULL; | |
943 | } | |
944 | ||
945 | /* Make WSA APIs available. */ | |
946 | wsa_version = MAKEWORD(2, 2); | |
947 | ret = WSAStartup(wsa_version, &wsa_data); | |
948 | if (ret != 0) { | |
949 | error_setg_win32(errp, ret, "failed socket startup"); | |
950 | goto out; | |
951 | } | |
952 | ||
953 | for (addr = adptr_addrs; addr; addr = addr->Next) { | |
954 | info = g_malloc0(sizeof(*info)); | |
955 | ||
956 | if (cur_item == NULL) { | |
957 | head = cur_item = info; | |
958 | } else { | |
959 | cur_item->next = info; | |
960 | cur_item = info; | |
961 | } | |
962 | ||
963 | info->value = g_malloc0(sizeof(*info->value)); | |
964 | info->value->name = guest_wctomb_dup(addr->FriendlyName); | |
965 | ||
966 | if (addr->PhysicalAddressLength != 0) { | |
967 | mac_addr = addr->PhysicalAddress; | |
968 | ||
969 | info->value->hardware_address = | |
970 | g_strdup_printf("%02x:%02x:%02x:%02x:%02x:%02x", | |
971 | (int) mac_addr[0], (int) mac_addr[1], | |
972 | (int) mac_addr[2], (int) mac_addr[3], | |
973 | (int) mac_addr[4], (int) mac_addr[5]); | |
974 | ||
975 | info->value->has_hardware_address = true; | |
976 | } | |
977 | ||
978 | head_addr = NULL; | |
979 | cur_addr = NULL; | |
980 | for (ip_addr = addr->FirstUnicastAddress; | |
981 | ip_addr; | |
982 | ip_addr = ip_addr->Next) { | |
983 | addr_str = guest_addr_to_str(ip_addr, errp); | |
984 | if (addr_str == NULL) { | |
985 | continue; | |
986 | } | |
987 | ||
988 | address_item = g_malloc0(sizeof(*address_item)); | |
989 | ||
990 | if (!cur_addr) { | |
991 | head_addr = cur_addr = address_item; | |
992 | } else { | |
993 | cur_addr->next = address_item; | |
994 | cur_addr = address_item; | |
995 | } | |
996 | ||
997 | address_item->value = g_malloc0(sizeof(*address_item->value)); | |
998 | address_item->value->ip_address = addr_str; | |
999 | address_item->value->prefix = guest_ip_prefix(ip_addr); | |
1000 | if (ip_addr->Address.lpSockaddr->sa_family == AF_INET) { | |
1001 | address_item->value->ip_address_type = | |
1002 | GUEST_IP_ADDRESS_TYPE_IPV4; | |
1003 | } else if (ip_addr->Address.lpSockaddr->sa_family == AF_INET6) { | |
1004 | address_item->value->ip_address_type = | |
1005 | GUEST_IP_ADDRESS_TYPE_IPV6; | |
1006 | } | |
1007 | } | |
1008 | if (head_addr) { | |
1009 | info->value->has_ip_addresses = true; | |
1010 | info->value->ip_addresses = head_addr; | |
1011 | } | |
1012 | } | |
1013 | WSACleanup(); | |
1014 | out: | |
1015 | g_free(adptr_addrs); | |
1016 | return head; | |
1017 | } | |
1018 | ||
6912e6a9 LL |
1019 | int64_t qmp_guest_get_time(Error **errp) |
1020 | { | |
3f2a6087 LL |
1021 | SYSTEMTIME ts = {0}; |
1022 | int64_t time_ns; | |
1023 | FILETIME tf; | |
1024 | ||
1025 | GetSystemTime(&ts); | |
1026 | if (ts.wYear < 1601 || ts.wYear > 30827) { | |
1027 | error_setg(errp, "Failed to get time"); | |
1028 | return -1; | |
1029 | } | |
1030 | ||
1031 | if (!SystemTimeToFileTime(&ts, &tf)) { | |
1032 | error_setg(errp, "Failed to convert system time: %d", (int)GetLastError()); | |
1033 | return -1; | |
1034 | } | |
1035 | ||
1036 | time_ns = ((((int64_t)tf.dwHighDateTime << 32) | tf.dwLowDateTime) | |
1037 | - W32_FT_OFFSET) * 100; | |
1038 | ||
1039 | return time_ns; | |
6912e6a9 LL |
1040 | } |
1041 | ||
2c958923 | 1042 | void qmp_guest_set_time(bool has_time, int64_t time_ns, Error **errp) |
a1bca57f | 1043 | { |
0f230bf7 | 1044 | Error *local_err = NULL; |
b8f954fe LL |
1045 | SYSTEMTIME ts; |
1046 | FILETIME tf; | |
1047 | LONGLONG time; | |
1048 | ||
ee17cbdc MP |
1049 | if (!has_time) { |
1050 | /* Unfortunately, Windows libraries don't provide an easy way to access | |
1051 | * RTC yet: | |
1052 | * | |
1053 | * https://msdn.microsoft.com/en-us/library/aa908981.aspx | |
1054 | */ | |
1055 | error_setg(errp, "Time argument is required on this platform"); | |
1056 | return; | |
1057 | } | |
1058 | ||
1059 | /* Validate time passed by user. */ | |
1060 | if (time_ns < 0 || time_ns / 100 > INT64_MAX - W32_FT_OFFSET) { | |
1061 | error_setg(errp, "Time %" PRId64 "is invalid", time_ns); | |
1062 | return; | |
1063 | } | |
b8f954fe | 1064 | |
ee17cbdc | 1065 | time = time_ns / 100 + W32_FT_OFFSET; |
b8f954fe | 1066 | |
ee17cbdc MP |
1067 | tf.dwLowDateTime = (DWORD) time; |
1068 | tf.dwHighDateTime = (DWORD) (time >> 32); | |
b8f954fe | 1069 | |
ee17cbdc MP |
1070 | if (!FileTimeToSystemTime(&tf, &ts)) { |
1071 | error_setg(errp, "Failed to convert system time %d", | |
1072 | (int)GetLastError()); | |
1073 | return; | |
b8f954fe LL |
1074 | } |
1075 | ||
0f230bf7 MA |
1076 | acquire_privilege(SE_SYSTEMTIME_NAME, &local_err); |
1077 | if (local_err) { | |
1078 | error_propagate(errp, local_err); | |
b8f954fe LL |
1079 | return; |
1080 | } | |
1081 | ||
1082 | if (!SetSystemTime(&ts)) { | |
1083 | error_setg(errp, "Failed to set time to guest: %d", (int)GetLastError()); | |
1084 | return; | |
1085 | } | |
a1bca57f LL |
1086 | } |
1087 | ||
70e133a7 LE |
1088 | GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp) |
1089 | { | |
c6bd8c70 | 1090 | error_setg(errp, QERR_UNSUPPORTED); |
70e133a7 LE |
1091 | return NULL; |
1092 | } | |
1093 | ||
1094 | int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp) | |
1095 | { | |
c6bd8c70 | 1096 | error_setg(errp, QERR_UNSUPPORTED); |
70e133a7 LE |
1097 | return -1; |
1098 | } | |
1099 | ||
215a2771 DB |
1100 | void qmp_guest_set_user_password(const char *username, |
1101 | const char *password, | |
1102 | bool crypted, | |
1103 | Error **errp) | |
1104 | { | |
c6bd8c70 | 1105 | error_setg(errp, QERR_UNSUPPORTED); |
215a2771 DB |
1106 | } |
1107 | ||
a065aaa9 HZ |
1108 | GuestMemoryBlockList *qmp_guest_get_memory_blocks(Error **errp) |
1109 | { | |
c6bd8c70 | 1110 | error_setg(errp, QERR_UNSUPPORTED); |
a065aaa9 HZ |
1111 | return NULL; |
1112 | } | |
1113 | ||
1114 | GuestMemoryBlockResponseList * | |
1115 | qmp_guest_set_memory_blocks(GuestMemoryBlockList *mem_blks, Error **errp) | |
1116 | { | |
c6bd8c70 | 1117 | error_setg(errp, QERR_UNSUPPORTED); |
a065aaa9 HZ |
1118 | return NULL; |
1119 | } | |
1120 | ||
1121 | GuestMemoryBlockInfo *qmp_guest_get_memory_block_info(Error **errp) | |
1122 | { | |
c6bd8c70 | 1123 | error_setg(errp, QERR_UNSUPPORTED); |
a065aaa9 HZ |
1124 | return NULL; |
1125 | } | |
1126 | ||
1281c08a TS |
1127 | /* add unsupported commands to the blacklist */ |
1128 | GList *ga_command_blacklist_init(GList *blacklist) | |
1129 | { | |
1130 | const char *list_unsupported[] = { | |
d6c5528b | 1131 | "guest-suspend-hybrid", |
1281c08a | 1132 | "guest-get-vcpus", "guest-set-vcpus", |
215a2771 | 1133 | "guest-set-user-password", |
0dd38a03 HZ |
1134 | "guest-get-memory-blocks", "guest-set-memory-blocks", |
1135 | "guest-get-memory-block-size", | |
ef0a03f2 | 1136 | "guest-fsfreeze-freeze-list", |
1281c08a TS |
1137 | "guest-fstrim", NULL}; |
1138 | char **p = (char **)list_unsupported; | |
1139 | ||
1140 | while (*p) { | |
1141 | blacklist = g_list_append(blacklist, *p++); | |
1142 | } | |
1143 | ||
1144 | if (!vss_init(true)) { | |
c69403fc | 1145 | g_debug("vss_init failed, vss commands are going to be disabled"); |
1281c08a TS |
1146 | const char *list[] = { |
1147 | "guest-get-fsinfo", "guest-fsfreeze-status", | |
1148 | "guest-fsfreeze-freeze", "guest-fsfreeze-thaw", NULL}; | |
1149 | p = (char **)list; | |
1150 | ||
1151 | while (*p) { | |
1152 | blacklist = g_list_append(blacklist, *p++); | |
1153 | } | |
1154 | } | |
1155 | ||
1156 | return blacklist; | |
1157 | } | |
1158 | ||
d8ca685a MR |
1159 | /* register init/cleanup routines for stateful command groups */ |
1160 | void ga_command_state_init(GAState *s, GACommandState *cs) | |
1161 | { | |
1281c08a | 1162 | if (!vss_initialized()) { |
64c00317 TS |
1163 | ga_command_state_add(cs, NULL, guest_fsfreeze_cleanup); |
1164 | } | |
fa193594 | 1165 | ga_command_state_add(cs, guest_file_init, NULL); |
d8ca685a | 1166 | } |