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> 8 * Gal Hammer <ghammer@redhat.com> 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 #include "qemu/osdep.h" 14 15 #include <wtypes.h> 16 #include <powrprof.h> 17 #include <winsock2.h> 18 #include <ws2tcpip.h> 19 #include <iptypes.h> 20 #include <iphlpapi.h> 21 #include <winioctl.h> 22 #include <ntddscsi.h> 23 #include <setupapi.h> 24 #include <cfgmgr32.h> 25 #include <initguid.h> 26 #include <devpropdef.h> 27 #include <lm.h> 28 #include <wtsapi32.h> 29 #include <wininet.h> 30 31 #include "guest-agent-core.h" 32 #include "vss-win32.h" 33 #include "qga-qapi-commands.h" 34 #include "qapi/error.h" 35 #include "qapi/qmp/qerror.h" 36 #include "qemu/queue.h" 37 #include "qemu/host-utils.h" 38 #include "qemu/base64.h" 39 #include "commands-common.h" 40 41 /* 42 * The following should be in devpkey.h, but it isn't. The key names were 43 * prefixed to avoid (future) name clashes. Once the definitions get into 44 * mingw the following lines can be removed. 45 */ 46 DEFINE_DEVPROPKEY(qga_DEVPKEY_NAME, 0xb725f130, 0x47ef, 0x101a, 0xa5, 47 0xf1, 0x02, 0x60, 0x8c, 0x9e, 0xeb, 0xac, 10); 48 /* DEVPROP_TYPE_STRING */ 49 DEFINE_DEVPROPKEY(qga_DEVPKEY_Device_HardwareIds, 0xa45c254e, 0xdf1c, 50 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 3); 51 /* DEVPROP_TYPE_STRING_LIST */ 52 DEFINE_DEVPROPKEY(qga_DEVPKEY_Device_DriverDate, 0xa8b865dd, 0x2e3d, 53 0x4094, 0xad, 0x97, 0xe5, 0x93, 0xa7, 0xc, 0x75, 0xd6, 2); 54 /* DEVPROP_TYPE_FILETIME */ 55 DEFINE_DEVPROPKEY(qga_DEVPKEY_Device_DriverVersion, 0xa8b865dd, 0x2e3d, 56 0x4094, 0xad, 0x97, 0xe5, 0x93, 0xa7, 0xc, 0x75, 0xd6, 3); 57 /* DEVPROP_TYPE_STRING */ 58 /* The CM_Get_DevNode_PropertyW prototype is only sometimes in cfgmgr32.h */ 59 #ifndef CM_Get_DevNode_Property 60 #pragma GCC diagnostic push 61 #pragma GCC diagnostic ignored "-Wredundant-decls" 62 CMAPI CONFIGRET WINAPI CM_Get_DevNode_PropertyW( 63 DEVINST dnDevInst, 64 CONST DEVPROPKEY * PropertyKey, 65 DEVPROPTYPE * PropertyType, 66 PBYTE PropertyBuffer, 67 PULONG PropertyBufferSize, 68 ULONG ulFlags 69 ); 70 #define CM_Get_DevNode_Property CM_Get_DevNode_PropertyW 71 #pragma GCC diagnostic pop 72 #endif 73 74 #ifndef SHTDN_REASON_FLAG_PLANNED 75 #define SHTDN_REASON_FLAG_PLANNED 0x80000000 76 #endif 77 78 /* multiple of 100 nanoseconds elapsed between windows baseline 79 * (1/1/1601) and Unix Epoch (1/1/1970), accounting for leap years */ 80 #define W32_FT_OFFSET (10000000ULL * 60 * 60 * 24 * \ 81 (365 * (1970 - 1601) + \ 82 (1970 - 1601) / 4 - 3)) 83 84 #define INVALID_SET_FILE_POINTER ((DWORD)-1) 85 86 struct GuestFileHandle { 87 int64_t id; 88 HANDLE fh; 89 QTAILQ_ENTRY(GuestFileHandle) next; 90 }; 91 92 static struct { 93 QTAILQ_HEAD(, GuestFileHandle) filehandles; 94 } guest_file_state = { 95 .filehandles = QTAILQ_HEAD_INITIALIZER(guest_file_state.filehandles), 96 }; 97 98 #define FILE_GENERIC_APPEND (FILE_GENERIC_WRITE & ~FILE_WRITE_DATA) 99 100 typedef struct OpenFlags { 101 const char *forms; 102 DWORD desired_access; 103 DWORD creation_disposition; 104 } OpenFlags; 105 static OpenFlags guest_file_open_modes[] = { 106 {"r", GENERIC_READ, OPEN_EXISTING}, 107 {"rb", GENERIC_READ, OPEN_EXISTING}, 108 {"w", GENERIC_WRITE, CREATE_ALWAYS}, 109 {"wb", GENERIC_WRITE, CREATE_ALWAYS}, 110 {"a", FILE_GENERIC_APPEND, OPEN_ALWAYS }, 111 {"r+", GENERIC_WRITE | GENERIC_READ, OPEN_EXISTING}, 112 {"rb+", GENERIC_WRITE | GENERIC_READ, OPEN_EXISTING}, 113 {"r+b", GENERIC_WRITE | GENERIC_READ, OPEN_EXISTING}, 114 {"w+", GENERIC_WRITE | GENERIC_READ, CREATE_ALWAYS}, 115 {"wb+", GENERIC_WRITE | GENERIC_READ, CREATE_ALWAYS}, 116 {"w+b", GENERIC_WRITE | GENERIC_READ, CREATE_ALWAYS}, 117 {"a+", FILE_GENERIC_APPEND | GENERIC_READ, OPEN_ALWAYS }, 118 {"ab+", FILE_GENERIC_APPEND | GENERIC_READ, OPEN_ALWAYS }, 119 {"a+b", FILE_GENERIC_APPEND | GENERIC_READ, OPEN_ALWAYS } 120 }; 121 122 #define debug_error(msg) do { \ 123 char *suffix = g_win32_error_message(GetLastError()); \ 124 g_debug("%s: %s", (msg), suffix); \ 125 g_free(suffix); \ 126 } while (0) 127 128 static OpenFlags *find_open_flag(const char *mode_str) 129 { 130 int mode; 131 Error **errp = NULL; 132 133 for (mode = 0; mode < ARRAY_SIZE(guest_file_open_modes); ++mode) { 134 OpenFlags *flags = guest_file_open_modes + mode; 135 136 if (strcmp(flags->forms, mode_str) == 0) { 137 return flags; 138 } 139 } 140 141 error_setg(errp, "invalid file open mode '%s'", mode_str); 142 return NULL; 143 } 144 145 static int64_t guest_file_handle_add(HANDLE fh, Error **errp) 146 { 147 GuestFileHandle *gfh; 148 int64_t handle; 149 150 handle = ga_get_fd_handle(ga_state, errp); 151 if (handle < 0) { 152 return -1; 153 } 154 gfh = g_new0(GuestFileHandle, 1); 155 gfh->id = handle; 156 gfh->fh = fh; 157 QTAILQ_INSERT_TAIL(&guest_file_state.filehandles, gfh, next); 158 159 return handle; 160 } 161 162 GuestFileHandle *guest_file_handle_find(int64_t id, Error **errp) 163 { 164 GuestFileHandle *gfh; 165 QTAILQ_FOREACH(gfh, &guest_file_state.filehandles, next) { 166 if (gfh->id == id) { 167 return gfh; 168 } 169 } 170 error_setg(errp, "handle '%" PRId64 "' has not been found", id); 171 return NULL; 172 } 173 174 static void handle_set_nonblocking(HANDLE fh) 175 { 176 DWORD file_type, pipe_state; 177 file_type = GetFileType(fh); 178 if (file_type != FILE_TYPE_PIPE) { 179 return; 180 } 181 /* If file_type == FILE_TYPE_PIPE, according to MSDN 182 * the specified file is socket or named pipe */ 183 if (!GetNamedPipeHandleState(fh, &pipe_state, NULL, 184 NULL, NULL, NULL, 0)) { 185 return; 186 } 187 /* The fd is named pipe fd */ 188 if (pipe_state & PIPE_NOWAIT) { 189 return; 190 } 191 192 pipe_state |= PIPE_NOWAIT; 193 SetNamedPipeHandleState(fh, &pipe_state, NULL, NULL); 194 } 195 196 int64_t qmp_guest_file_open(const char *path, bool has_mode, 197 const char *mode, Error **errp) 198 { 199 int64_t fd = -1; 200 HANDLE fh; 201 HANDLE templ_file = NULL; 202 DWORD share_mode = FILE_SHARE_READ; 203 DWORD flags_and_attr = FILE_ATTRIBUTE_NORMAL; 204 LPSECURITY_ATTRIBUTES sa_attr = NULL; 205 OpenFlags *guest_flags; 206 GError *gerr = NULL; 207 wchar_t *w_path = NULL; 208 209 if (!has_mode) { 210 mode = "r"; 211 } 212 slog("guest-file-open called, filepath: %s, mode: %s", path, mode); 213 guest_flags = find_open_flag(mode); 214 if (guest_flags == NULL) { 215 error_setg(errp, "invalid file open mode"); 216 goto done; 217 } 218 219 w_path = g_utf8_to_utf16(path, -1, NULL, NULL, &gerr); 220 if (!w_path) { 221 goto done; 222 } 223 224 fh = CreateFileW(w_path, guest_flags->desired_access, share_mode, sa_attr, 225 guest_flags->creation_disposition, flags_and_attr, 226 templ_file); 227 if (fh == INVALID_HANDLE_VALUE) { 228 error_setg_win32(errp, GetLastError(), "failed to open file '%s'", 229 path); 230 goto done; 231 } 232 233 /* set fd non-blocking to avoid common use cases (like reading from a 234 * named pipe) from hanging the agent 235 */ 236 handle_set_nonblocking(fh); 237 238 fd = guest_file_handle_add(fh, errp); 239 if (fd < 0) { 240 CloseHandle(fh); 241 error_setg(errp, "failed to add handle to qmp handle table"); 242 goto done; 243 } 244 245 slog("guest-file-open, handle: % " PRId64, fd); 246 247 done: 248 if (gerr) { 249 error_setg(errp, QERR_QGA_COMMAND_FAILED, gerr->message); 250 g_error_free(gerr); 251 } 252 g_free(w_path); 253 return fd; 254 } 255 256 void qmp_guest_file_close(int64_t handle, Error **errp) 257 { 258 bool ret; 259 GuestFileHandle *gfh = guest_file_handle_find(handle, errp); 260 slog("guest-file-close called, handle: %" PRId64, handle); 261 if (gfh == NULL) { 262 return; 263 } 264 ret = CloseHandle(gfh->fh); 265 if (!ret) { 266 error_setg_win32(errp, GetLastError(), "failed close handle"); 267 return; 268 } 269 270 QTAILQ_REMOVE(&guest_file_state.filehandles, gfh, next); 271 g_free(gfh); 272 } 273 274 static void acquire_privilege(const char *name, Error **errp) 275 { 276 HANDLE token = NULL; 277 TOKEN_PRIVILEGES priv; 278 279 if (OpenProcessToken(GetCurrentProcess(), 280 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token)) 281 { 282 if (!LookupPrivilegeValue(NULL, name, &priv.Privileges[0].Luid)) { 283 error_setg(errp, QERR_QGA_COMMAND_FAILED, 284 "no luid for requested privilege"); 285 goto out; 286 } 287 288 priv.PrivilegeCount = 1; 289 priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; 290 291 if (!AdjustTokenPrivileges(token, FALSE, &priv, 0, NULL, 0)) { 292 error_setg(errp, QERR_QGA_COMMAND_FAILED, 293 "unable to acquire requested privilege"); 294 goto out; 295 } 296 297 } else { 298 error_setg(errp, QERR_QGA_COMMAND_FAILED, 299 "failed to open privilege token"); 300 } 301 302 out: 303 if (token) { 304 CloseHandle(token); 305 } 306 } 307 308 static void execute_async(DWORD WINAPI (*func)(LPVOID), LPVOID opaque, 309 Error **errp) 310 { 311 HANDLE thread = CreateThread(NULL, 0, func, opaque, 0, NULL); 312 if (!thread) { 313 error_setg(errp, QERR_QGA_COMMAND_FAILED, 314 "failed to dispatch asynchronous command"); 315 } 316 } 317 318 void qmp_guest_shutdown(bool has_mode, const char *mode, Error **errp) 319 { 320 Error *local_err = NULL; 321 UINT shutdown_flag = EWX_FORCE; 322 323 slog("guest-shutdown called, mode: %s", mode); 324 325 if (!has_mode || strcmp(mode, "powerdown") == 0) { 326 shutdown_flag |= EWX_POWEROFF; 327 } else if (strcmp(mode, "halt") == 0) { 328 shutdown_flag |= EWX_SHUTDOWN; 329 } else if (strcmp(mode, "reboot") == 0) { 330 shutdown_flag |= EWX_REBOOT; 331 } else { 332 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "mode", 333 "'halt', 'powerdown', or 'reboot'"); 334 return; 335 } 336 337 /* Request a shutdown privilege, but try to shut down the system 338 anyway. */ 339 acquire_privilege(SE_SHUTDOWN_NAME, &local_err); 340 if (local_err) { 341 error_propagate(errp, local_err); 342 return; 343 } 344 345 if (!ExitWindowsEx(shutdown_flag, SHTDN_REASON_FLAG_PLANNED)) { 346 g_autofree gchar *emsg = g_win32_error_message(GetLastError()); 347 slog("guest-shutdown failed: %s", emsg); 348 error_setg_win32(errp, GetLastError(), "guest-shutdown failed"); 349 } 350 } 351 352 GuestFileRead *guest_file_read_unsafe(GuestFileHandle *gfh, 353 int64_t count, Error **errp) 354 { 355 GuestFileRead *read_data = NULL; 356 guchar *buf; 357 HANDLE fh = gfh->fh; 358 bool is_ok; 359 DWORD read_count; 360 361 buf = g_malloc0(count + 1); 362 is_ok = ReadFile(fh, buf, count, &read_count, NULL); 363 if (!is_ok) { 364 error_setg_win32(errp, GetLastError(), "failed to read file"); 365 } else { 366 buf[read_count] = 0; 367 read_data = g_new0(GuestFileRead, 1); 368 read_data->count = (size_t)read_count; 369 read_data->eof = read_count == 0; 370 371 if (read_count != 0) { 372 read_data->buf_b64 = g_base64_encode(buf, read_count); 373 } 374 } 375 g_free(buf); 376 377 return read_data; 378 } 379 380 GuestFileWrite *qmp_guest_file_write(int64_t handle, const char *buf_b64, 381 bool has_count, int64_t count, 382 Error **errp) 383 { 384 GuestFileWrite *write_data = NULL; 385 guchar *buf; 386 gsize buf_len; 387 bool is_ok; 388 DWORD write_count; 389 GuestFileHandle *gfh = guest_file_handle_find(handle, errp); 390 HANDLE fh; 391 392 if (!gfh) { 393 return NULL; 394 } 395 fh = gfh->fh; 396 buf = qbase64_decode(buf_b64, -1, &buf_len, errp); 397 if (!buf) { 398 return NULL; 399 } 400 401 if (!has_count) { 402 count = buf_len; 403 } else if (count < 0 || count > buf_len) { 404 error_setg(errp, "value '%" PRId64 405 "' is invalid for argument count", count); 406 goto done; 407 } 408 409 is_ok = WriteFile(fh, buf, count, &write_count, NULL); 410 if (!is_ok) { 411 error_setg_win32(errp, GetLastError(), "failed to write to file"); 412 slog("guest-file-write-failed, handle: %" PRId64, handle); 413 } else { 414 write_data = g_new0(GuestFileWrite, 1); 415 write_data->count = (size_t) write_count; 416 } 417 418 done: 419 g_free(buf); 420 return write_data; 421 } 422 423 GuestFileSeek *qmp_guest_file_seek(int64_t handle, int64_t offset, 424 GuestFileWhence *whence_code, 425 Error **errp) 426 { 427 GuestFileHandle *gfh; 428 GuestFileSeek *seek_data; 429 HANDLE fh; 430 LARGE_INTEGER new_pos, off_pos; 431 off_pos.QuadPart = offset; 432 BOOL res; 433 int whence; 434 Error *err = NULL; 435 436 gfh = guest_file_handle_find(handle, errp); 437 if (!gfh) { 438 return NULL; 439 } 440 441 /* We stupidly exposed 'whence':'int' in our qapi */ 442 whence = ga_parse_whence(whence_code, &err); 443 if (err) { 444 error_propagate(errp, err); 445 return NULL; 446 } 447 448 fh = gfh->fh; 449 res = SetFilePointerEx(fh, off_pos, &new_pos, whence); 450 if (!res) { 451 error_setg_win32(errp, GetLastError(), "failed to seek file"); 452 return NULL; 453 } 454 seek_data = g_new0(GuestFileSeek, 1); 455 seek_data->position = new_pos.QuadPart; 456 return seek_data; 457 } 458 459 void qmp_guest_file_flush(int64_t handle, Error **errp) 460 { 461 HANDLE fh; 462 GuestFileHandle *gfh = guest_file_handle_find(handle, errp); 463 if (!gfh) { 464 return; 465 } 466 467 fh = gfh->fh; 468 if (!FlushFileBuffers(fh)) { 469 error_setg_win32(errp, GetLastError(), "failed to flush file"); 470 } 471 } 472 473 static GuestDiskBusType win2qemu[] = { 474 [BusTypeUnknown] = GUEST_DISK_BUS_TYPE_UNKNOWN, 475 [BusTypeScsi] = GUEST_DISK_BUS_TYPE_SCSI, 476 [BusTypeAtapi] = GUEST_DISK_BUS_TYPE_IDE, 477 [BusTypeAta] = GUEST_DISK_BUS_TYPE_IDE, 478 [BusType1394] = GUEST_DISK_BUS_TYPE_IEEE1394, 479 [BusTypeSsa] = GUEST_DISK_BUS_TYPE_SSA, 480 [BusTypeFibre] = GUEST_DISK_BUS_TYPE_SSA, 481 [BusTypeUsb] = GUEST_DISK_BUS_TYPE_USB, 482 [BusTypeRAID] = GUEST_DISK_BUS_TYPE_RAID, 483 [BusTypeiScsi] = GUEST_DISK_BUS_TYPE_ISCSI, 484 [BusTypeSas] = GUEST_DISK_BUS_TYPE_SAS, 485 [BusTypeSata] = GUEST_DISK_BUS_TYPE_SATA, 486 [BusTypeSd] = GUEST_DISK_BUS_TYPE_SD, 487 [BusTypeMmc] = GUEST_DISK_BUS_TYPE_MMC, 488 #if (_WIN32_WINNT >= 0x0601) 489 [BusTypeVirtual] = GUEST_DISK_BUS_TYPE_VIRTUAL, 490 [BusTypeFileBackedVirtual] = GUEST_DISK_BUS_TYPE_FILE_BACKED_VIRTUAL, 491 /* 492 * BusTypeSpaces currently is not suported 493 */ 494 [BusTypeSpaces] = GUEST_DISK_BUS_TYPE_UNKNOWN, 495 [BusTypeNvme] = GUEST_DISK_BUS_TYPE_NVME, 496 #endif 497 }; 498 499 static GuestDiskBusType find_bus_type(STORAGE_BUS_TYPE bus) 500 { 501 if (bus >= ARRAY_SIZE(win2qemu) || (int)bus < 0) { 502 return GUEST_DISK_BUS_TYPE_UNKNOWN; 503 } 504 return win2qemu[(int)bus]; 505 } 506 507 DEFINE_GUID(GUID_DEVINTERFACE_DISK, 508 0x53f56307L, 0xb6bf, 0x11d0, 0x94, 0xf2, 509 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b); 510 DEFINE_GUID(GUID_DEVINTERFACE_STORAGEPORT, 511 0x2accfe60L, 0xc130, 0x11d2, 0xb0, 0x82, 512 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b); 513 514 static void get_pci_address_for_device(GuestPCIAddress *pci, 515 HDEVINFO dev_info) 516 { 517 SP_DEVINFO_DATA dev_info_data; 518 DWORD j; 519 DWORD size; 520 bool partial_pci = false; 521 522 dev_info_data.cbSize = sizeof(SP_DEVINFO_DATA); 523 524 for (j = 0; 525 SetupDiEnumDeviceInfo(dev_info, j, &dev_info_data); 526 j++) { 527 DWORD addr, bus, ui_slot, type; 528 int func, slot; 529 size = sizeof(DWORD); 530 531 /* 532 * There is no need to allocate buffer in the next functions. The 533 * size is known and ULONG according to 534 * https://msdn.microsoft.com/en-us/library/windows/hardware/ff543095(v=vs.85).aspx 535 */ 536 if (!SetupDiGetDeviceRegistryProperty( 537 dev_info, &dev_info_data, SPDRP_BUSNUMBER, 538 &type, (PBYTE)&bus, size, NULL)) { 539 debug_error("failed to get PCI bus"); 540 bus = -1; 541 partial_pci = true; 542 } 543 544 /* 545 * The function retrieves the device's address. This value will be 546 * transformed into device function and number 547 */ 548 if (!SetupDiGetDeviceRegistryProperty( 549 dev_info, &dev_info_data, SPDRP_ADDRESS, 550 &type, (PBYTE)&addr, size, NULL)) { 551 debug_error("failed to get PCI address"); 552 addr = -1; 553 partial_pci = true; 554 } 555 556 /* 557 * This call returns UINumber of DEVICE_CAPABILITIES structure. 558 * This number is typically a user-perceived slot number. 559 */ 560 if (!SetupDiGetDeviceRegistryProperty( 561 dev_info, &dev_info_data, SPDRP_UI_NUMBER, 562 &type, (PBYTE)&ui_slot, size, NULL)) { 563 debug_error("failed to get PCI slot"); 564 ui_slot = -1; 565 partial_pci = true; 566 } 567 568 /* 569 * SetupApi gives us the same information as driver with 570 * IoGetDeviceProperty. According to Microsoft: 571 * 572 * FunctionNumber = (USHORT)((propertyAddress) & 0x0000FFFF) 573 * DeviceNumber = (USHORT)(((propertyAddress) >> 16) & 0x0000FFFF) 574 * SPDRP_ADDRESS is propertyAddress, so we do the same. 575 * 576 * https://docs.microsoft.com/en-us/windows/desktop/api/setupapi/nf-setupapi-setupdigetdeviceregistrypropertya 577 */ 578 if (partial_pci) { 579 pci->domain = -1; 580 pci->slot = -1; 581 pci->function = -1; 582 pci->bus = -1; 583 continue; 584 } else { 585 func = ((int)addr == -1) ? -1 : addr & 0x0000FFFF; 586 slot = ((int)addr == -1) ? -1 : (addr >> 16) & 0x0000FFFF; 587 if ((int)ui_slot != slot) { 588 g_debug("mismatch with reported slot values: %d vs %d", 589 (int)ui_slot, slot); 590 } 591 pci->domain = 0; 592 pci->slot = (int)ui_slot; 593 pci->function = func; 594 pci->bus = (int)bus; 595 return; 596 } 597 } 598 } 599 600 static GuestPCIAddress *get_pci_info(int number, Error **errp) 601 { 602 HDEVINFO dev_info = INVALID_HANDLE_VALUE; 603 HDEVINFO parent_dev_info = INVALID_HANDLE_VALUE; 604 605 SP_DEVINFO_DATA dev_info_data; 606 SP_DEVICE_INTERFACE_DATA dev_iface_data; 607 HANDLE dev_file; 608 int i; 609 GuestPCIAddress *pci = NULL; 610 611 pci = g_malloc0(sizeof(*pci)); 612 pci->domain = -1; 613 pci->slot = -1; 614 pci->function = -1; 615 pci->bus = -1; 616 617 dev_info = SetupDiGetClassDevs(&GUID_DEVINTERFACE_DISK, 0, 0, 618 DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); 619 if (dev_info == INVALID_HANDLE_VALUE) { 620 error_setg_win32(errp, GetLastError(), "failed to get devices tree"); 621 goto end; 622 } 623 624 g_debug("enumerating devices"); 625 dev_info_data.cbSize = sizeof(SP_DEVINFO_DATA); 626 dev_iface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); 627 for (i = 0; SetupDiEnumDeviceInfo(dev_info, i, &dev_info_data); i++) { 628 g_autofree PSP_DEVICE_INTERFACE_DETAIL_DATA pdev_iface_detail_data = NULL; 629 STORAGE_DEVICE_NUMBER sdn; 630 g_autofree char *parent_dev_id = NULL; 631 SP_DEVINFO_DATA parent_dev_info_data; 632 DWORD size = 0; 633 634 g_debug("getting device path"); 635 if (SetupDiEnumDeviceInterfaces(dev_info, &dev_info_data, 636 &GUID_DEVINTERFACE_DISK, 0, 637 &dev_iface_data)) { 638 if (!SetupDiGetDeviceInterfaceDetail(dev_info, &dev_iface_data, 639 pdev_iface_detail_data, 640 size, &size, 641 &dev_info_data)) { 642 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { 643 pdev_iface_detail_data = g_malloc(size); 644 pdev_iface_detail_data->cbSize = 645 sizeof(*pdev_iface_detail_data); 646 } else { 647 error_setg_win32(errp, GetLastError(), 648 "failed to get device interfaces"); 649 goto end; 650 } 651 } 652 653 if (!SetupDiGetDeviceInterfaceDetail(dev_info, &dev_iface_data, 654 pdev_iface_detail_data, 655 size, &size, 656 &dev_info_data)) { 657 // pdev_iface_detail_data already is allocated 658 error_setg_win32(errp, GetLastError(), 659 "failed to get device interfaces"); 660 goto end; 661 } 662 663 dev_file = CreateFile(pdev_iface_detail_data->DevicePath, 0, 664 FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 665 NULL); 666 667 if (!DeviceIoControl(dev_file, IOCTL_STORAGE_GET_DEVICE_NUMBER, 668 NULL, 0, &sdn, sizeof(sdn), &size, NULL)) { 669 CloseHandle(dev_file); 670 error_setg_win32(errp, GetLastError(), 671 "failed to get device slot number"); 672 goto end; 673 } 674 675 CloseHandle(dev_file); 676 if (sdn.DeviceNumber != number) { 677 continue; 678 } 679 } else { 680 error_setg_win32(errp, GetLastError(), 681 "failed to get device interfaces"); 682 goto end; 683 } 684 685 g_debug("found device slot %d. Getting storage controller", number); 686 { 687 CONFIGRET cr; 688 DEVINST dev_inst, parent_dev_inst; 689 ULONG dev_id_size = 0; 690 691 size = 0; 692 if (!SetupDiGetDeviceInstanceId(dev_info, &dev_info_data, 693 parent_dev_id, size, &size)) { 694 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { 695 parent_dev_id = g_malloc(size); 696 } else { 697 error_setg_win32(errp, GetLastError(), 698 "failed to get device instance ID"); 699 goto end; 700 } 701 } 702 703 if (!SetupDiGetDeviceInstanceId(dev_info, &dev_info_data, 704 parent_dev_id, size, &size)) { 705 // parent_dev_id already is allocated 706 error_setg_win32(errp, GetLastError(), 707 "failed to get device instance ID"); 708 goto end; 709 } 710 711 /* 712 * CM API used here as opposed to 713 * SetupDiGetDeviceProperty(..., DEVPKEY_Device_Parent, ...) 714 * which exports are only available in mingw-w64 6+ 715 */ 716 cr = CM_Locate_DevInst(&dev_inst, parent_dev_id, 0); 717 if (cr != CR_SUCCESS) { 718 g_error("CM_Locate_DevInst failed with code %lx", cr); 719 error_setg_win32(errp, GetLastError(), 720 "failed to get device instance"); 721 goto end; 722 } 723 cr = CM_Get_Parent(&parent_dev_inst, dev_inst, 0); 724 if (cr != CR_SUCCESS) { 725 g_error("CM_Get_Parent failed with code %lx", cr); 726 error_setg_win32(errp, GetLastError(), 727 "failed to get parent device instance"); 728 goto end; 729 } 730 731 cr = CM_Get_Device_ID_Size(&dev_id_size, parent_dev_inst, 0); 732 if (cr != CR_SUCCESS) { 733 g_error("CM_Get_Device_ID_Size failed with code %lx", cr); 734 error_setg_win32(errp, GetLastError(), 735 "failed to get parent device ID length"); 736 goto end; 737 } 738 739 ++dev_id_size; 740 if (dev_id_size > size) { 741 g_free(parent_dev_id); 742 parent_dev_id = g_malloc(dev_id_size); 743 } 744 745 cr = CM_Get_Device_ID(parent_dev_inst, parent_dev_id, dev_id_size, 746 0); 747 if (cr != CR_SUCCESS) { 748 g_error("CM_Get_Device_ID failed with code %lx", cr); 749 error_setg_win32(errp, GetLastError(), 750 "failed to get parent device ID"); 751 goto end; 752 } 753 } 754 755 g_debug("querying storage controller %s for PCI information", 756 parent_dev_id); 757 parent_dev_info = 758 SetupDiGetClassDevs(&GUID_DEVINTERFACE_STORAGEPORT, parent_dev_id, 759 NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); 760 761 if (parent_dev_info == INVALID_HANDLE_VALUE) { 762 error_setg_win32(errp, GetLastError(), 763 "failed to get parent device"); 764 goto end; 765 } 766 767 parent_dev_info_data.cbSize = sizeof(SP_DEVINFO_DATA); 768 if (!SetupDiEnumDeviceInfo(parent_dev_info, 0, &parent_dev_info_data)) { 769 error_setg_win32(errp, GetLastError(), 770 "failed to get parent device data"); 771 goto end; 772 } 773 774 get_pci_address_for_device(pci, parent_dev_info); 775 776 break; 777 } 778 779 end: 780 if (parent_dev_info != INVALID_HANDLE_VALUE) { 781 SetupDiDestroyDeviceInfoList(parent_dev_info); 782 } 783 if (dev_info != INVALID_HANDLE_VALUE) { 784 SetupDiDestroyDeviceInfoList(dev_info); 785 } 786 return pci; 787 } 788 789 static void get_disk_properties(HANDLE vol_h, GuestDiskAddress *disk, 790 Error **errp) 791 { 792 STORAGE_PROPERTY_QUERY query; 793 STORAGE_DEVICE_DESCRIPTOR *dev_desc, buf; 794 DWORD received; 795 ULONG size = sizeof(buf); 796 797 dev_desc = &buf; 798 query.PropertyId = StorageDeviceProperty; 799 query.QueryType = PropertyStandardQuery; 800 801 if (!DeviceIoControl(vol_h, IOCTL_STORAGE_QUERY_PROPERTY, &query, 802 sizeof(STORAGE_PROPERTY_QUERY), dev_desc, 803 size, &received, NULL)) { 804 error_setg_win32(errp, GetLastError(), "failed to get bus type"); 805 return; 806 } 807 disk->bus_type = find_bus_type(dev_desc->BusType); 808 g_debug("bus type %d", disk->bus_type); 809 810 /* Query once more. Now with long enough buffer. */ 811 size = dev_desc->Size; 812 dev_desc = g_malloc0(size); 813 if (!DeviceIoControl(vol_h, IOCTL_STORAGE_QUERY_PROPERTY, &query, 814 sizeof(STORAGE_PROPERTY_QUERY), dev_desc, 815 size, &received, NULL)) { 816 error_setg_win32(errp, GetLastError(), "failed to get serial number"); 817 g_debug("failed to get serial number"); 818 goto out_free; 819 } 820 if (dev_desc->SerialNumberOffset > 0) { 821 const char *serial; 822 size_t len; 823 824 if (dev_desc->SerialNumberOffset >= received) { 825 error_setg(errp, "failed to get serial number: offset outside the buffer"); 826 g_debug("serial number offset outside the buffer"); 827 goto out_free; 828 } 829 serial = (char *)dev_desc + dev_desc->SerialNumberOffset; 830 len = received - dev_desc->SerialNumberOffset; 831 g_debug("serial number \"%s\"", serial); 832 if (*serial != 0) { 833 disk->serial = g_strndup(serial, len); 834 disk->has_serial = true; 835 } 836 } 837 out_free: 838 g_free(dev_desc); 839 840 return; 841 } 842 843 static void get_single_disk_info(int disk_number, 844 GuestDiskAddress *disk, Error **errp) 845 { 846 SCSI_ADDRESS addr, *scsi_ad; 847 DWORD len; 848 HANDLE disk_h; 849 Error *local_err = NULL; 850 851 scsi_ad = &addr; 852 853 g_debug("getting disk info for: %s", disk->dev); 854 disk_h = CreateFile(disk->dev, 0, FILE_SHARE_READ, NULL, OPEN_EXISTING, 855 0, NULL); 856 if (disk_h == INVALID_HANDLE_VALUE) { 857 error_setg_win32(errp, GetLastError(), "failed to open disk"); 858 return; 859 } 860 861 get_disk_properties(disk_h, disk, &local_err); 862 if (local_err) { 863 error_propagate(errp, local_err); 864 goto err_close; 865 } 866 867 g_debug("bus type %d", disk->bus_type); 868 /* always set pci_controller as required by schema. get_pci_info() should 869 * report -1 values for non-PCI buses rather than fail. fail the command 870 * if that doesn't hold since that suggests some other unexpected 871 * breakage 872 */ 873 disk->pci_controller = get_pci_info(disk_number, &local_err); 874 if (local_err) { 875 error_propagate(errp, local_err); 876 goto err_close; 877 } 878 if (disk->bus_type == GUEST_DISK_BUS_TYPE_SCSI 879 || disk->bus_type == GUEST_DISK_BUS_TYPE_IDE 880 || disk->bus_type == GUEST_DISK_BUS_TYPE_RAID 881 /* This bus type is not supported before Windows Server 2003 SP1 */ 882 || disk->bus_type == GUEST_DISK_BUS_TYPE_SAS 883 ) { 884 /* We are able to use the same ioctls for different bus types 885 * according to Microsoft docs 886 * https://technet.microsoft.com/en-us/library/ee851589(v=ws.10).aspx */ 887 g_debug("getting SCSI info"); 888 if (DeviceIoControl(disk_h, IOCTL_SCSI_GET_ADDRESS, NULL, 0, scsi_ad, 889 sizeof(SCSI_ADDRESS), &len, NULL)) { 890 disk->unit = addr.Lun; 891 disk->target = addr.TargetId; 892 disk->bus = addr.PathId; 893 } 894 /* We do not set error in this case, because we still have enough 895 * information about volume. */ 896 } 897 898 err_close: 899 CloseHandle(disk_h); 900 return; 901 } 902 903 /* VSS provider works with volumes, thus there is no difference if 904 * the volume consist of spanned disks. Info about the first disk in the 905 * volume is returned for the spanned disk group (LVM) */ 906 static GuestDiskAddressList *build_guest_disk_info(char *guid, Error **errp) 907 { 908 Error *local_err = NULL; 909 GuestDiskAddressList *list = NULL; 910 GuestDiskAddress *disk = NULL; 911 int i; 912 HANDLE vol_h; 913 DWORD size; 914 PVOLUME_DISK_EXTENTS extents = NULL; 915 916 /* strip final backslash */ 917 char *name = g_strdup(guid); 918 if (g_str_has_suffix(name, "\\")) { 919 name[strlen(name) - 1] = 0; 920 } 921 922 g_debug("opening %s", name); 923 vol_h = CreateFile(name, 0, FILE_SHARE_READ, NULL, OPEN_EXISTING, 924 0, NULL); 925 if (vol_h == INVALID_HANDLE_VALUE) { 926 error_setg_win32(errp, GetLastError(), "failed to open volume"); 927 goto out; 928 } 929 930 /* Get list of extents */ 931 g_debug("getting disk extents"); 932 size = sizeof(VOLUME_DISK_EXTENTS); 933 extents = g_malloc0(size); 934 if (!DeviceIoControl(vol_h, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, NULL, 935 0, extents, size, &size, NULL)) { 936 DWORD last_err = GetLastError(); 937 if (last_err == ERROR_MORE_DATA) { 938 /* Try once more with big enough buffer */ 939 g_free(extents); 940 extents = g_malloc0(size); 941 if (!DeviceIoControl( 942 vol_h, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, NULL, 943 0, extents, size, NULL, NULL)) { 944 error_setg_win32(errp, GetLastError(), 945 "failed to get disk extents"); 946 goto out; 947 } 948 } else if (last_err == ERROR_INVALID_FUNCTION) { 949 /* Possibly CD-ROM or a shared drive. Try to pass the volume */ 950 g_debug("volume not on disk"); 951 disk = g_new0(GuestDiskAddress, 1); 952 disk->has_dev = true; 953 disk->dev = g_strdup(name); 954 get_single_disk_info(0xffffffff, disk, &local_err); 955 if (local_err) { 956 g_debug("failed to get disk info, ignoring error: %s", 957 error_get_pretty(local_err)); 958 error_free(local_err); 959 goto out; 960 } 961 QAPI_LIST_PREPEND(list, disk); 962 disk = NULL; 963 goto out; 964 } else { 965 error_setg_win32(errp, GetLastError(), 966 "failed to get disk extents"); 967 goto out; 968 } 969 } 970 g_debug("Number of extents: %lu", extents->NumberOfDiskExtents); 971 972 /* Go through each extent */ 973 for (i = 0; i < extents->NumberOfDiskExtents; i++) { 974 disk = g_new0(GuestDiskAddress, 1); 975 976 /* Disk numbers directly correspond to numbers used in UNCs 977 * 978 * See documentation for DISK_EXTENT: 979 * https://docs.microsoft.com/en-us/windows/desktop/api/winioctl/ns-winioctl-_disk_extent 980 * 981 * See also Naming Files, Paths and Namespaces: 982 * https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#win32-device-namespaces 983 */ 984 disk->has_dev = true; 985 disk->dev = g_strdup_printf("\\\\.\\PhysicalDrive%lu", 986 extents->Extents[i].DiskNumber); 987 988 get_single_disk_info(extents->Extents[i].DiskNumber, disk, &local_err); 989 if (local_err) { 990 error_propagate(errp, local_err); 991 goto out; 992 } 993 QAPI_LIST_PREPEND(list, disk); 994 disk = NULL; 995 } 996 997 998 out: 999 if (vol_h != INVALID_HANDLE_VALUE) { 1000 CloseHandle(vol_h); 1001 } 1002 qapi_free_GuestDiskAddress(disk); 1003 g_free(extents); 1004 g_free(name); 1005 1006 return list; 1007 } 1008 1009 GuestDiskInfoList *qmp_guest_get_disks(Error **errp) 1010 { 1011 GuestDiskInfoList *ret = NULL; 1012 HDEVINFO dev_info; 1013 SP_DEVICE_INTERFACE_DATA dev_iface_data; 1014 int i; 1015 1016 dev_info = SetupDiGetClassDevs(&GUID_DEVINTERFACE_DISK, 0, 0, 1017 DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); 1018 if (dev_info == INVALID_HANDLE_VALUE) { 1019 error_setg_win32(errp, GetLastError(), "failed to get device tree"); 1020 return NULL; 1021 } 1022 1023 g_debug("enumerating devices"); 1024 dev_iface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); 1025 for (i = 0; 1026 SetupDiEnumDeviceInterfaces(dev_info, NULL, &GUID_DEVINTERFACE_DISK, 1027 i, &dev_iface_data); 1028 i++) { 1029 GuestDiskAddress *address = NULL; 1030 GuestDiskInfo *disk = NULL; 1031 Error *local_err = NULL; 1032 g_autofree PSP_DEVICE_INTERFACE_DETAIL_DATA 1033 pdev_iface_detail_data = NULL; 1034 STORAGE_DEVICE_NUMBER sdn; 1035 HANDLE dev_file; 1036 DWORD size = 0; 1037 BOOL result; 1038 int attempt; 1039 1040 g_debug(" getting device path"); 1041 for (attempt = 0, result = FALSE; attempt < 2 && !result; attempt++) { 1042 result = SetupDiGetDeviceInterfaceDetail(dev_info, 1043 &dev_iface_data, pdev_iface_detail_data, size, &size, NULL); 1044 if (result) { 1045 break; 1046 } 1047 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { 1048 pdev_iface_detail_data = g_realloc(pdev_iface_detail_data, 1049 size); 1050 pdev_iface_detail_data->cbSize = 1051 sizeof(*pdev_iface_detail_data); 1052 } else { 1053 g_debug("failed to get device interface details"); 1054 break; 1055 } 1056 } 1057 if (!result) { 1058 g_debug("skipping device"); 1059 continue; 1060 } 1061 1062 g_debug(" device: %s", pdev_iface_detail_data->DevicePath); 1063 dev_file = CreateFile(pdev_iface_detail_data->DevicePath, 0, 1064 FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); 1065 if (!DeviceIoControl(dev_file, IOCTL_STORAGE_GET_DEVICE_NUMBER, 1066 NULL, 0, &sdn, sizeof(sdn), &size, NULL)) { 1067 CloseHandle(dev_file); 1068 debug_error("failed to get storage device number"); 1069 continue; 1070 } 1071 CloseHandle(dev_file); 1072 1073 disk = g_new0(GuestDiskInfo, 1); 1074 disk->name = g_strdup_printf("\\\\.\\PhysicalDrive%lu", 1075 sdn.DeviceNumber); 1076 1077 g_debug(" number: %lu", sdn.DeviceNumber); 1078 address = g_new0(GuestDiskAddress, 1); 1079 address->has_dev = true; 1080 address->dev = g_strdup(disk->name); 1081 get_single_disk_info(sdn.DeviceNumber, address, &local_err); 1082 if (local_err) { 1083 g_debug("failed to get disk info: %s", 1084 error_get_pretty(local_err)); 1085 error_free(local_err); 1086 qapi_free_GuestDiskAddress(address); 1087 address = NULL; 1088 } else { 1089 disk->address = address; 1090 disk->has_address = true; 1091 } 1092 1093 QAPI_LIST_PREPEND(ret, disk); 1094 } 1095 1096 SetupDiDestroyDeviceInfoList(dev_info); 1097 return ret; 1098 } 1099 1100 static GuestFilesystemInfo *build_guest_fsinfo(char *guid, Error **errp) 1101 { 1102 DWORD info_size; 1103 char mnt, *mnt_point; 1104 wchar_t wfs_name[32]; 1105 char fs_name[32]; 1106 wchar_t vol_info[MAX_PATH + 1]; 1107 size_t len; 1108 uint64_t i64FreeBytesToCaller, i64TotalBytes, i64FreeBytes; 1109 GuestFilesystemInfo *fs = NULL; 1110 HANDLE hLocalDiskHandle = INVALID_HANDLE_VALUE; 1111 1112 GetVolumePathNamesForVolumeName(guid, (LPCH)&mnt, 0, &info_size); 1113 if (GetLastError() != ERROR_MORE_DATA) { 1114 error_setg_win32(errp, GetLastError(), "failed to get volume name"); 1115 return NULL; 1116 } 1117 1118 mnt_point = g_malloc(info_size + 1); 1119 if (!GetVolumePathNamesForVolumeName(guid, mnt_point, info_size, 1120 &info_size)) { 1121 error_setg_win32(errp, GetLastError(), "failed to get volume name"); 1122 goto free; 1123 } 1124 1125 hLocalDiskHandle = CreateFile(guid, 0 , 0, NULL, OPEN_EXISTING, 1126 FILE_ATTRIBUTE_NORMAL | 1127 FILE_FLAG_BACKUP_SEMANTICS, NULL); 1128 if (INVALID_HANDLE_VALUE == hLocalDiskHandle) { 1129 error_setg_win32(errp, GetLastError(), "failed to get handle for volume"); 1130 goto free; 1131 } 1132 1133 len = strlen(mnt_point); 1134 mnt_point[len] = '\\'; 1135 mnt_point[len + 1] = 0; 1136 1137 if (!GetVolumeInformationByHandleW(hLocalDiskHandle, vol_info, 1138 sizeof(vol_info), NULL, NULL, NULL, 1139 (LPWSTR) & wfs_name, sizeof(wfs_name))) { 1140 if (GetLastError() != ERROR_NOT_READY) { 1141 error_setg_win32(errp, GetLastError(), "failed to get volume info"); 1142 } 1143 goto free; 1144 } 1145 1146 fs = g_malloc(sizeof(*fs)); 1147 fs->name = g_strdup(guid); 1148 fs->has_total_bytes = false; 1149 fs->has_used_bytes = false; 1150 if (len == 0) { 1151 fs->mountpoint = g_strdup("System Reserved"); 1152 } else { 1153 fs->mountpoint = g_strndup(mnt_point, len); 1154 if (GetDiskFreeSpaceEx(fs->mountpoint, 1155 (PULARGE_INTEGER) & i64FreeBytesToCaller, 1156 (PULARGE_INTEGER) & i64TotalBytes, 1157 (PULARGE_INTEGER) & i64FreeBytes)) { 1158 fs->used_bytes = i64TotalBytes - i64FreeBytes; 1159 fs->total_bytes = i64TotalBytes; 1160 fs->has_total_bytes = true; 1161 fs->has_used_bytes = true; 1162 } 1163 } 1164 wcstombs(fs_name, wfs_name, sizeof(wfs_name)); 1165 fs->type = g_strdup(fs_name); 1166 fs->disk = build_guest_disk_info(guid, errp); 1167 free: 1168 if (hLocalDiskHandle != INVALID_HANDLE_VALUE) { 1169 CloseHandle(hLocalDiskHandle); 1170 } 1171 g_free(mnt_point); 1172 return fs; 1173 } 1174 1175 GuestFilesystemInfoList *qmp_guest_get_fsinfo(Error **errp) 1176 { 1177 HANDLE vol_h; 1178 GuestFilesystemInfoList *ret = NULL; 1179 char guid[256]; 1180 1181 vol_h = FindFirstVolume(guid, sizeof(guid)); 1182 if (vol_h == INVALID_HANDLE_VALUE) { 1183 error_setg_win32(errp, GetLastError(), "failed to find any volume"); 1184 return NULL; 1185 } 1186 1187 do { 1188 Error *local_err = NULL; 1189 GuestFilesystemInfo *info = build_guest_fsinfo(guid, &local_err); 1190 if (local_err) { 1191 g_debug("failed to get filesystem info, ignoring error: %s", 1192 error_get_pretty(local_err)); 1193 error_free(local_err); 1194 continue; 1195 } 1196 QAPI_LIST_PREPEND(ret, info); 1197 } while (FindNextVolume(vol_h, guid, sizeof(guid))); 1198 1199 if (GetLastError() != ERROR_NO_MORE_FILES) { 1200 error_setg_win32(errp, GetLastError(), "failed to find next volume"); 1201 } 1202 1203 FindVolumeClose(vol_h); 1204 return ret; 1205 } 1206 1207 /* 1208 * Return status of freeze/thaw 1209 */ 1210 GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **errp) 1211 { 1212 if (!vss_initialized()) { 1213 error_setg(errp, QERR_UNSUPPORTED); 1214 return 0; 1215 } 1216 1217 if (ga_is_frozen(ga_state)) { 1218 return GUEST_FSFREEZE_STATUS_FROZEN; 1219 } 1220 1221 return GUEST_FSFREEZE_STATUS_THAWED; 1222 } 1223 1224 /* 1225 * Freeze local file systems using Volume Shadow-copy Service. 1226 * The frozen state is limited for up to 10 seconds by VSS. 1227 */ 1228 int64_t qmp_guest_fsfreeze_freeze(Error **errp) 1229 { 1230 return qmp_guest_fsfreeze_freeze_list(false, NULL, errp); 1231 } 1232 1233 int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints, 1234 strList *mountpoints, 1235 Error **errp) 1236 { 1237 int i; 1238 Error *local_err = NULL; 1239 1240 if (!vss_initialized()) { 1241 error_setg(errp, QERR_UNSUPPORTED); 1242 return 0; 1243 } 1244 1245 slog("guest-fsfreeze called"); 1246 1247 /* cannot risk guest agent blocking itself on a write in this state */ 1248 ga_set_frozen(ga_state); 1249 1250 qga_vss_fsfreeze(&i, true, mountpoints, &local_err); 1251 if (local_err) { 1252 error_propagate(errp, local_err); 1253 goto error; 1254 } 1255 1256 return i; 1257 1258 error: 1259 local_err = NULL; 1260 qmp_guest_fsfreeze_thaw(&local_err); 1261 if (local_err) { 1262 g_debug("cleanup thaw: %s", error_get_pretty(local_err)); 1263 error_free(local_err); 1264 } 1265 return 0; 1266 } 1267 1268 /* 1269 * Thaw local file systems using Volume Shadow-copy Service. 1270 */ 1271 int64_t qmp_guest_fsfreeze_thaw(Error **errp) 1272 { 1273 int i; 1274 1275 if (!vss_initialized()) { 1276 error_setg(errp, QERR_UNSUPPORTED); 1277 return 0; 1278 } 1279 1280 qga_vss_fsfreeze(&i, false, NULL, errp); 1281 1282 ga_unset_frozen(ga_state); 1283 return i; 1284 } 1285 1286 static void guest_fsfreeze_cleanup(void) 1287 { 1288 Error *err = NULL; 1289 1290 if (!vss_initialized()) { 1291 return; 1292 } 1293 1294 if (ga_is_frozen(ga_state) == GUEST_FSFREEZE_STATUS_FROZEN) { 1295 qmp_guest_fsfreeze_thaw(&err); 1296 if (err) { 1297 slog("failed to clean up frozen filesystems: %s", 1298 error_get_pretty(err)); 1299 error_free(err); 1300 } 1301 } 1302 1303 vss_deinit(true); 1304 } 1305 1306 /* 1307 * Walk list of mounted file systems in the guest, and discard unused 1308 * areas. 1309 */ 1310 GuestFilesystemTrimResponse * 1311 qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **errp) 1312 { 1313 GuestFilesystemTrimResponse *resp; 1314 HANDLE handle; 1315 WCHAR guid[MAX_PATH] = L""; 1316 OSVERSIONINFO osvi; 1317 BOOL win8_or_later; 1318 1319 ZeroMemory(&osvi, sizeof(OSVERSIONINFO)); 1320 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); 1321 GetVersionEx(&osvi); 1322 win8_or_later = (osvi.dwMajorVersion > 6 || 1323 ((osvi.dwMajorVersion == 6) && 1324 (osvi.dwMinorVersion >= 2))); 1325 if (!win8_or_later) { 1326 error_setg(errp, "fstrim is only supported for Win8+"); 1327 return NULL; 1328 } 1329 1330 handle = FindFirstVolumeW(guid, ARRAYSIZE(guid)); 1331 if (handle == INVALID_HANDLE_VALUE) { 1332 error_setg_win32(errp, GetLastError(), "failed to find any volume"); 1333 return NULL; 1334 } 1335 1336 resp = g_new0(GuestFilesystemTrimResponse, 1); 1337 1338 do { 1339 GuestFilesystemTrimResult *res; 1340 PWCHAR uc_path; 1341 DWORD char_count = 0; 1342 char *path, *out; 1343 GError *gerr = NULL; 1344 gchar *argv[4]; 1345 1346 GetVolumePathNamesForVolumeNameW(guid, NULL, 0, &char_count); 1347 1348 if (GetLastError() != ERROR_MORE_DATA) { 1349 continue; 1350 } 1351 if (GetDriveTypeW(guid) != DRIVE_FIXED) { 1352 continue; 1353 } 1354 1355 uc_path = g_new(WCHAR, char_count); 1356 if (!GetVolumePathNamesForVolumeNameW(guid, uc_path, char_count, 1357 &char_count) || !*uc_path) { 1358 /* strange, but this condition could be faced even with size == 2 */ 1359 g_free(uc_path); 1360 continue; 1361 } 1362 1363 res = g_new0(GuestFilesystemTrimResult, 1); 1364 1365 path = g_utf16_to_utf8(uc_path, char_count, NULL, NULL, &gerr); 1366 1367 g_free(uc_path); 1368 1369 if (!path) { 1370 res->has_error = true; 1371 res->error = g_strdup(gerr->message); 1372 g_error_free(gerr); 1373 break; 1374 } 1375 1376 res->path = path; 1377 1378 QAPI_LIST_PREPEND(resp->paths, res); 1379 1380 memset(argv, 0, sizeof(argv)); 1381 argv[0] = (gchar *)"defrag.exe"; 1382 argv[1] = (gchar *)"/L"; 1383 argv[2] = path; 1384 1385 if (!g_spawn_sync(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, 1386 &out /* stdout */, NULL /* stdin */, 1387 NULL, &gerr)) { 1388 res->has_error = true; 1389 res->error = g_strdup(gerr->message); 1390 g_error_free(gerr); 1391 } else { 1392 /* defrag.exe is UGLY. Exit code is ALWAYS zero. 1393 Error is reported in the output with something like 1394 (x89000020) etc code in the stdout */ 1395 1396 int i; 1397 gchar **lines = g_strsplit(out, "\r\n", 0); 1398 g_free(out); 1399 1400 for (i = 0; lines[i] != NULL; i++) { 1401 if (g_strstr_len(lines[i], -1, "(0x") == NULL) { 1402 continue; 1403 } 1404 res->has_error = true; 1405 res->error = g_strdup(lines[i]); 1406 break; 1407 } 1408 g_strfreev(lines); 1409 } 1410 } while (FindNextVolumeW(handle, guid, ARRAYSIZE(guid))); 1411 1412 FindVolumeClose(handle); 1413 return resp; 1414 } 1415 1416 typedef enum { 1417 GUEST_SUSPEND_MODE_DISK, 1418 GUEST_SUSPEND_MODE_RAM 1419 } GuestSuspendMode; 1420 1421 static void check_suspend_mode(GuestSuspendMode mode, Error **errp) 1422 { 1423 SYSTEM_POWER_CAPABILITIES sys_pwr_caps; 1424 1425 ZeroMemory(&sys_pwr_caps, sizeof(sys_pwr_caps)); 1426 if (!GetPwrCapabilities(&sys_pwr_caps)) { 1427 error_setg(errp, QERR_QGA_COMMAND_FAILED, 1428 "failed to determine guest suspend capabilities"); 1429 return; 1430 } 1431 1432 switch (mode) { 1433 case GUEST_SUSPEND_MODE_DISK: 1434 if (!sys_pwr_caps.SystemS4) { 1435 error_setg(errp, QERR_QGA_COMMAND_FAILED, 1436 "suspend-to-disk not supported by OS"); 1437 } 1438 break; 1439 case GUEST_SUSPEND_MODE_RAM: 1440 if (!sys_pwr_caps.SystemS3) { 1441 error_setg(errp, QERR_QGA_COMMAND_FAILED, 1442 "suspend-to-ram not supported by OS"); 1443 } 1444 break; 1445 default: 1446 abort(); 1447 } 1448 } 1449 1450 static DWORD WINAPI do_suspend(LPVOID opaque) 1451 { 1452 GuestSuspendMode *mode = opaque; 1453 DWORD ret = 0; 1454 1455 if (!SetSuspendState(*mode == GUEST_SUSPEND_MODE_DISK, TRUE, TRUE)) { 1456 g_autofree gchar *emsg = g_win32_error_message(GetLastError()); 1457 slog("failed to suspend guest: %s", emsg); 1458 ret = -1; 1459 } 1460 g_free(mode); 1461 return ret; 1462 } 1463 1464 void qmp_guest_suspend_disk(Error **errp) 1465 { 1466 Error *local_err = NULL; 1467 GuestSuspendMode *mode = g_new(GuestSuspendMode, 1); 1468 1469 *mode = GUEST_SUSPEND_MODE_DISK; 1470 check_suspend_mode(*mode, &local_err); 1471 if (local_err) { 1472 goto out; 1473 } 1474 acquire_privilege(SE_SHUTDOWN_NAME, &local_err); 1475 if (local_err) { 1476 goto out; 1477 } 1478 execute_async(do_suspend, mode, &local_err); 1479 1480 out: 1481 if (local_err) { 1482 error_propagate(errp, local_err); 1483 g_free(mode); 1484 } 1485 } 1486 1487 void qmp_guest_suspend_ram(Error **errp) 1488 { 1489 Error *local_err = NULL; 1490 GuestSuspendMode *mode = g_new(GuestSuspendMode, 1); 1491 1492 *mode = GUEST_SUSPEND_MODE_RAM; 1493 check_suspend_mode(*mode, &local_err); 1494 if (local_err) { 1495 goto out; 1496 } 1497 acquire_privilege(SE_SHUTDOWN_NAME, &local_err); 1498 if (local_err) { 1499 goto out; 1500 } 1501 execute_async(do_suspend, mode, &local_err); 1502 1503 out: 1504 if (local_err) { 1505 error_propagate(errp, local_err); 1506 g_free(mode); 1507 } 1508 } 1509 1510 void qmp_guest_suspend_hybrid(Error **errp) 1511 { 1512 error_setg(errp, QERR_UNSUPPORTED); 1513 } 1514 1515 static IP_ADAPTER_ADDRESSES *guest_get_adapters_addresses(Error **errp) 1516 { 1517 IP_ADAPTER_ADDRESSES *adptr_addrs = NULL; 1518 ULONG adptr_addrs_len = 0; 1519 DWORD ret; 1520 1521 /* Call the first time to get the adptr_addrs_len. */ 1522 GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, 1523 NULL, adptr_addrs, &adptr_addrs_len); 1524 1525 adptr_addrs = g_malloc(adptr_addrs_len); 1526 ret = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, 1527 NULL, adptr_addrs, &adptr_addrs_len); 1528 if (ret != ERROR_SUCCESS) { 1529 error_setg_win32(errp, ret, "failed to get adapters addresses"); 1530 g_free(adptr_addrs); 1531 adptr_addrs = NULL; 1532 } 1533 return adptr_addrs; 1534 } 1535 1536 static char *guest_wctomb_dup(WCHAR *wstr) 1537 { 1538 char *str; 1539 size_t str_size; 1540 1541 str_size = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL); 1542 /* add 1 to str_size for NULL terminator */ 1543 str = g_malloc(str_size + 1); 1544 WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, str_size, NULL, NULL); 1545 return str; 1546 } 1547 1548 static char *guest_addr_to_str(IP_ADAPTER_UNICAST_ADDRESS *ip_addr, 1549 Error **errp) 1550 { 1551 char addr_str[INET6_ADDRSTRLEN + INET_ADDRSTRLEN]; 1552 DWORD len; 1553 int ret; 1554 1555 if (ip_addr->Address.lpSockaddr->sa_family == AF_INET || 1556 ip_addr->Address.lpSockaddr->sa_family == AF_INET6) { 1557 len = sizeof(addr_str); 1558 ret = WSAAddressToString(ip_addr->Address.lpSockaddr, 1559 ip_addr->Address.iSockaddrLength, 1560 NULL, 1561 addr_str, 1562 &len); 1563 if (ret != 0) { 1564 error_setg_win32(errp, WSAGetLastError(), 1565 "failed address presentation form conversion"); 1566 return NULL; 1567 } 1568 return g_strdup(addr_str); 1569 } 1570 return NULL; 1571 } 1572 1573 static int64_t guest_ip_prefix(IP_ADAPTER_UNICAST_ADDRESS *ip_addr) 1574 { 1575 /* For Windows Vista/2008 and newer, use the OnLinkPrefixLength 1576 * field to obtain the prefix. 1577 */ 1578 return ip_addr->OnLinkPrefixLength; 1579 } 1580 1581 #define INTERFACE_PATH_BUF_SZ 512 1582 1583 static DWORD get_interface_index(const char *guid) 1584 { 1585 ULONG index; 1586 DWORD status; 1587 wchar_t wbuf[INTERFACE_PATH_BUF_SZ]; 1588 snwprintf(wbuf, INTERFACE_PATH_BUF_SZ, L"\\device\\tcpip_%s", guid); 1589 wbuf[INTERFACE_PATH_BUF_SZ - 1] = 0; 1590 status = GetAdapterIndex (wbuf, &index); 1591 if (status != NO_ERROR) { 1592 return (DWORD)~0; 1593 } else { 1594 return index; 1595 } 1596 } 1597 1598 typedef NETIOAPI_API (WINAPI *GetIfEntry2Func)(PMIB_IF_ROW2 Row); 1599 1600 static int guest_get_network_stats(const char *name, 1601 GuestNetworkInterfaceStat *stats) 1602 { 1603 OSVERSIONINFO os_ver; 1604 1605 os_ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); 1606 GetVersionEx(&os_ver); 1607 if (os_ver.dwMajorVersion >= 6) { 1608 MIB_IF_ROW2 a_mid_ifrow; 1609 GetIfEntry2Func getifentry2_ex; 1610 DWORD if_index = 0; 1611 HMODULE module = GetModuleHandle("iphlpapi"); 1612 PVOID func = GetProcAddress(module, "GetIfEntry2"); 1613 1614 if (func == NULL) { 1615 return -1; 1616 } 1617 1618 getifentry2_ex = (GetIfEntry2Func)func; 1619 if_index = get_interface_index(name); 1620 if (if_index == (DWORD)~0) { 1621 return -1; 1622 } 1623 1624 memset(&a_mid_ifrow, 0, sizeof(a_mid_ifrow)); 1625 a_mid_ifrow.InterfaceIndex = if_index; 1626 if (NO_ERROR == getifentry2_ex(&a_mid_ifrow)) { 1627 stats->rx_bytes = a_mid_ifrow.InOctets; 1628 stats->rx_packets = a_mid_ifrow.InUcastPkts; 1629 stats->rx_errs = a_mid_ifrow.InErrors; 1630 stats->rx_dropped = a_mid_ifrow.InDiscards; 1631 stats->tx_bytes = a_mid_ifrow.OutOctets; 1632 stats->tx_packets = a_mid_ifrow.OutUcastPkts; 1633 stats->tx_errs = a_mid_ifrow.OutErrors; 1634 stats->tx_dropped = a_mid_ifrow.OutDiscards; 1635 return 0; 1636 } 1637 } 1638 return -1; 1639 } 1640 1641 GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp) 1642 { 1643 IP_ADAPTER_ADDRESSES *adptr_addrs, *addr; 1644 IP_ADAPTER_UNICAST_ADDRESS *ip_addr = NULL; 1645 GuestNetworkInterfaceList *head = NULL, **tail = &head; 1646 GuestIpAddressList *head_addr, **tail_addr; 1647 GuestNetworkInterface *info; 1648 GuestNetworkInterfaceStat *interface_stat = NULL; 1649 GuestIpAddress *address_item = NULL; 1650 unsigned char *mac_addr; 1651 char *addr_str; 1652 WORD wsa_version; 1653 WSADATA wsa_data; 1654 int ret; 1655 1656 adptr_addrs = guest_get_adapters_addresses(errp); 1657 if (adptr_addrs == NULL) { 1658 return NULL; 1659 } 1660 1661 /* Make WSA APIs available. */ 1662 wsa_version = MAKEWORD(2, 2); 1663 ret = WSAStartup(wsa_version, &wsa_data); 1664 if (ret != 0) { 1665 error_setg_win32(errp, ret, "failed socket startup"); 1666 goto out; 1667 } 1668 1669 for (addr = adptr_addrs; addr; addr = addr->Next) { 1670 info = g_malloc0(sizeof(*info)); 1671 1672 QAPI_LIST_APPEND(tail, info); 1673 1674 info->name = guest_wctomb_dup(addr->FriendlyName); 1675 1676 if (addr->PhysicalAddressLength != 0) { 1677 mac_addr = addr->PhysicalAddress; 1678 1679 info->hardware_address = 1680 g_strdup_printf("%02x:%02x:%02x:%02x:%02x:%02x", 1681 (int) mac_addr[0], (int) mac_addr[1], 1682 (int) mac_addr[2], (int) mac_addr[3], 1683 (int) mac_addr[4], (int) mac_addr[5]); 1684 1685 info->has_hardware_address = true; 1686 } 1687 1688 head_addr = NULL; 1689 tail_addr = &head_addr; 1690 for (ip_addr = addr->FirstUnicastAddress; 1691 ip_addr; 1692 ip_addr = ip_addr->Next) { 1693 addr_str = guest_addr_to_str(ip_addr, errp); 1694 if (addr_str == NULL) { 1695 continue; 1696 } 1697 1698 address_item = g_malloc0(sizeof(*address_item)); 1699 1700 QAPI_LIST_APPEND(tail_addr, address_item); 1701 1702 address_item->ip_address = addr_str; 1703 address_item->prefix = guest_ip_prefix(ip_addr); 1704 if (ip_addr->Address.lpSockaddr->sa_family == AF_INET) { 1705 address_item->ip_address_type = GUEST_IP_ADDRESS_TYPE_IPV4; 1706 } else if (ip_addr->Address.lpSockaddr->sa_family == AF_INET6) { 1707 address_item->ip_address_type = GUEST_IP_ADDRESS_TYPE_IPV6; 1708 } 1709 } 1710 if (head_addr) { 1711 info->has_ip_addresses = true; 1712 info->ip_addresses = head_addr; 1713 } 1714 if (!info->has_statistics) { 1715 interface_stat = g_malloc0(sizeof(*interface_stat)); 1716 if (guest_get_network_stats(addr->AdapterName, 1717 interface_stat) == -1) { 1718 info->has_statistics = false; 1719 g_free(interface_stat); 1720 } else { 1721 info->statistics = interface_stat; 1722 info->has_statistics = true; 1723 } 1724 } 1725 } 1726 WSACleanup(); 1727 out: 1728 g_free(adptr_addrs); 1729 return head; 1730 } 1731 1732 static int64_t filetime_to_ns(const FILETIME *tf) 1733 { 1734 return ((((int64_t)tf->dwHighDateTime << 32) | tf->dwLowDateTime) 1735 - W32_FT_OFFSET) * 100; 1736 } 1737 1738 void qmp_guest_set_time(bool has_time, int64_t time_ns, Error **errp) 1739 { 1740 Error *local_err = NULL; 1741 SYSTEMTIME ts; 1742 FILETIME tf; 1743 LONGLONG time; 1744 1745 if (!has_time) { 1746 /* Unfortunately, Windows libraries don't provide an easy way to access 1747 * RTC yet: 1748 * 1749 * https://msdn.microsoft.com/en-us/library/aa908981.aspx 1750 * 1751 * Instead, a workaround is to use the Windows win32tm command to 1752 * resync the time using the Windows Time service. 1753 */ 1754 LPVOID msg_buffer; 1755 DWORD ret_flags; 1756 1757 HRESULT hr = system("w32tm /resync /nowait"); 1758 1759 if (GetLastError() != 0) { 1760 strerror_s((LPTSTR) & msg_buffer, 0, errno); 1761 error_setg(errp, "system(...) failed: %s", (LPCTSTR)msg_buffer); 1762 } else if (hr != 0) { 1763 if (hr == HRESULT_FROM_WIN32(ERROR_SERVICE_NOT_ACTIVE)) { 1764 error_setg(errp, "Windows Time service not running on the " 1765 "guest"); 1766 } else { 1767 if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | 1768 FORMAT_MESSAGE_FROM_SYSTEM | 1769 FORMAT_MESSAGE_IGNORE_INSERTS, NULL, 1770 (DWORD)hr, MAKELANGID(LANG_NEUTRAL, 1771 SUBLANG_DEFAULT), (LPTSTR) & msg_buffer, 0, 1772 NULL)) { 1773 error_setg(errp, "w32tm failed with error (0x%lx), couldn'" 1774 "t retrieve error message", hr); 1775 } else { 1776 error_setg(errp, "w32tm failed with error (0x%lx): %s", hr, 1777 (LPCTSTR)msg_buffer); 1778 LocalFree(msg_buffer); 1779 } 1780 } 1781 } else if (!InternetGetConnectedState(&ret_flags, 0)) { 1782 error_setg(errp, "No internet connection on guest, sync not " 1783 "accurate"); 1784 } 1785 return; 1786 } 1787 1788 /* Validate time passed by user. */ 1789 if (time_ns < 0 || time_ns / 100 > INT64_MAX - W32_FT_OFFSET) { 1790 error_setg(errp, "Time %" PRId64 "is invalid", time_ns); 1791 return; 1792 } 1793 1794 time = time_ns / 100 + W32_FT_OFFSET; 1795 1796 tf.dwLowDateTime = (DWORD) time; 1797 tf.dwHighDateTime = (DWORD) (time >> 32); 1798 1799 if (!FileTimeToSystemTime(&tf, &ts)) { 1800 error_setg(errp, "Failed to convert system time %d", 1801 (int)GetLastError()); 1802 return; 1803 } 1804 1805 acquire_privilege(SE_SYSTEMTIME_NAME, &local_err); 1806 if (local_err) { 1807 error_propagate(errp, local_err); 1808 return; 1809 } 1810 1811 if (!SetSystemTime(&ts)) { 1812 error_setg(errp, "Failed to set time to guest: %d", (int)GetLastError()); 1813 return; 1814 } 1815 } 1816 1817 GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp) 1818 { 1819 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION pslpi, ptr; 1820 DWORD length; 1821 GuestLogicalProcessorList *head, **tail; 1822 Error *local_err = NULL; 1823 int64_t current; 1824 1825 ptr = pslpi = NULL; 1826 length = 0; 1827 current = 0; 1828 head = NULL; 1829 tail = &head; 1830 1831 if ((GetLogicalProcessorInformation(pslpi, &length) == FALSE) && 1832 (GetLastError() == ERROR_INSUFFICIENT_BUFFER) && 1833 (length > sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION))) { 1834 ptr = pslpi = g_malloc0(length); 1835 if (GetLogicalProcessorInformation(pslpi, &length) == FALSE) { 1836 error_setg(&local_err, "Failed to get processor information: %d", 1837 (int)GetLastError()); 1838 } 1839 } else { 1840 error_setg(&local_err, 1841 "Failed to get processor information buffer length: %d", 1842 (int)GetLastError()); 1843 } 1844 1845 while ((local_err == NULL) && (length > 0)) { 1846 if (pslpi->Relationship == RelationProcessorCore) { 1847 ULONG_PTR cpu_bits = pslpi->ProcessorMask; 1848 1849 while (cpu_bits > 0) { 1850 if (!!(cpu_bits & 1)) { 1851 GuestLogicalProcessor *vcpu; 1852 1853 vcpu = g_malloc0(sizeof *vcpu); 1854 vcpu->logical_id = current++; 1855 vcpu->online = true; 1856 vcpu->has_can_offline = true; 1857 1858 QAPI_LIST_APPEND(tail, vcpu); 1859 } 1860 cpu_bits >>= 1; 1861 } 1862 } 1863 length -= sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); 1864 pslpi++; /* next entry */ 1865 } 1866 1867 g_free(ptr); 1868 1869 if (local_err == NULL) { 1870 if (head != NULL) { 1871 return head; 1872 } 1873 /* there's no guest with zero VCPUs */ 1874 error_setg(&local_err, "Guest reported zero VCPUs"); 1875 } 1876 1877 qapi_free_GuestLogicalProcessorList(head); 1878 error_propagate(errp, local_err); 1879 return NULL; 1880 } 1881 1882 int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp) 1883 { 1884 error_setg(errp, QERR_UNSUPPORTED); 1885 return -1; 1886 } 1887 1888 static gchar * 1889 get_net_error_message(gint error) 1890 { 1891 HMODULE module = NULL; 1892 gchar *retval = NULL; 1893 wchar_t *msg = NULL; 1894 int flags; 1895 size_t nchars; 1896 1897 flags = FORMAT_MESSAGE_ALLOCATE_BUFFER | 1898 FORMAT_MESSAGE_IGNORE_INSERTS | 1899 FORMAT_MESSAGE_FROM_SYSTEM; 1900 1901 if (error >= NERR_BASE && error <= MAX_NERR) { 1902 module = LoadLibraryExW(L"netmsg.dll", NULL, LOAD_LIBRARY_AS_DATAFILE); 1903 1904 if (module != NULL) { 1905 flags |= FORMAT_MESSAGE_FROM_HMODULE; 1906 } 1907 } 1908 1909 FormatMessageW(flags, module, error, 0, (LPWSTR)&msg, 0, NULL); 1910 1911 if (msg != NULL) { 1912 nchars = wcslen(msg); 1913 1914 if (nchars >= 2 && 1915 msg[nchars - 1] == L'\n' && 1916 msg[nchars - 2] == L'\r') { 1917 msg[nchars - 2] = L'\0'; 1918 } 1919 1920 retval = g_utf16_to_utf8(msg, -1, NULL, NULL, NULL); 1921 1922 LocalFree(msg); 1923 } 1924 1925 if (module != NULL) { 1926 FreeLibrary(module); 1927 } 1928 1929 return retval; 1930 } 1931 1932 void qmp_guest_set_user_password(const char *username, 1933 const char *password, 1934 bool crypted, 1935 Error **errp) 1936 { 1937 NET_API_STATUS nas; 1938 char *rawpasswddata = NULL; 1939 size_t rawpasswdlen; 1940 wchar_t *user = NULL, *wpass = NULL; 1941 USER_INFO_1003 pi1003 = { 0, }; 1942 GError *gerr = NULL; 1943 1944 if (crypted) { 1945 error_setg(errp, QERR_UNSUPPORTED); 1946 return; 1947 } 1948 1949 rawpasswddata = (char *)qbase64_decode(password, -1, &rawpasswdlen, errp); 1950 if (!rawpasswddata) { 1951 return; 1952 } 1953 rawpasswddata = g_renew(char, rawpasswddata, rawpasswdlen + 1); 1954 rawpasswddata[rawpasswdlen] = '\0'; 1955 1956 user = g_utf8_to_utf16(username, -1, NULL, NULL, &gerr); 1957 if (!user) { 1958 goto done; 1959 } 1960 1961 wpass = g_utf8_to_utf16(rawpasswddata, -1, NULL, NULL, &gerr); 1962 if (!wpass) { 1963 goto done; 1964 } 1965 1966 pi1003.usri1003_password = wpass; 1967 nas = NetUserSetInfo(NULL, user, 1968 1003, (LPBYTE)&pi1003, 1969 NULL); 1970 1971 if (nas != NERR_Success) { 1972 gchar *msg = get_net_error_message(nas); 1973 error_setg(errp, "failed to set password: %s", msg); 1974 g_free(msg); 1975 } 1976 1977 done: 1978 if (gerr) { 1979 error_setg(errp, QERR_QGA_COMMAND_FAILED, gerr->message); 1980 g_error_free(gerr); 1981 } 1982 g_free(user); 1983 g_free(wpass); 1984 g_free(rawpasswddata); 1985 } 1986 1987 GuestMemoryBlockList *qmp_guest_get_memory_blocks(Error **errp) 1988 { 1989 error_setg(errp, QERR_UNSUPPORTED); 1990 return NULL; 1991 } 1992 1993 GuestMemoryBlockResponseList * 1994 qmp_guest_set_memory_blocks(GuestMemoryBlockList *mem_blks, Error **errp) 1995 { 1996 error_setg(errp, QERR_UNSUPPORTED); 1997 return NULL; 1998 } 1999 2000 GuestMemoryBlockInfo *qmp_guest_get_memory_block_info(Error **errp) 2001 { 2002 error_setg(errp, QERR_UNSUPPORTED); 2003 return NULL; 2004 } 2005 2006 /* add unsupported commands to the list of blocked RPCs */ 2007 GList *ga_command_init_blockedrpcs(GList *blockedrpcs) 2008 { 2009 const char *list_unsupported[] = { 2010 "guest-suspend-hybrid", 2011 "guest-set-vcpus", 2012 "guest-get-memory-blocks", "guest-set-memory-blocks", 2013 "guest-get-memory-block-size", "guest-get-memory-block-info", 2014 NULL}; 2015 char **p = (char **)list_unsupported; 2016 2017 while (*p) { 2018 blockedrpcs = g_list_append(blockedrpcs, g_strdup(*p++)); 2019 } 2020 2021 if (!vss_init(true)) { 2022 g_debug("vss_init failed, vss commands are going to be disabled"); 2023 const char *list[] = { 2024 "guest-get-fsinfo", "guest-fsfreeze-status", 2025 "guest-fsfreeze-freeze", "guest-fsfreeze-thaw", NULL}; 2026 p = (char **)list; 2027 2028 while (*p) { 2029 blockedrpcs = g_list_append(blockedrpcs, g_strdup(*p++)); 2030 } 2031 } 2032 2033 return blockedrpcs; 2034 } 2035 2036 /* register init/cleanup routines for stateful command groups */ 2037 void ga_command_state_init(GAState *s, GACommandState *cs) 2038 { 2039 if (!vss_initialized()) { 2040 ga_command_state_add(cs, NULL, guest_fsfreeze_cleanup); 2041 } 2042 } 2043 2044 /* MINGW is missing two fields: IncomingFrames & OutgoingFrames */ 2045 typedef struct _GA_WTSINFOA { 2046 WTS_CONNECTSTATE_CLASS State; 2047 DWORD SessionId; 2048 DWORD IncomingBytes; 2049 DWORD OutgoingBytes; 2050 DWORD IncomingFrames; 2051 DWORD OutgoingFrames; 2052 DWORD IncomingCompressedBytes; 2053 DWORD OutgoingCompressedBy; 2054 CHAR WinStationName[WINSTATIONNAME_LENGTH]; 2055 CHAR Domain[DOMAIN_LENGTH]; 2056 CHAR UserName[USERNAME_LENGTH + 1]; 2057 LARGE_INTEGER ConnectTime; 2058 LARGE_INTEGER DisconnectTime; 2059 LARGE_INTEGER LastInputTime; 2060 LARGE_INTEGER LogonTime; 2061 LARGE_INTEGER CurrentTime; 2062 2063 } GA_WTSINFOA; 2064 2065 GuestUserList *qmp_guest_get_users(Error **errp) 2066 { 2067 #define QGA_NANOSECONDS 10000000 2068 2069 GHashTable *cache = NULL; 2070 GuestUserList *head = NULL, **tail = &head; 2071 2072 DWORD buffer_size = 0, count = 0, i = 0; 2073 GA_WTSINFOA *info = NULL; 2074 WTS_SESSION_INFOA *entries = NULL; 2075 GuestUser *user = NULL; 2076 gpointer value = NULL; 2077 INT64 login = 0; 2078 double login_time = 0; 2079 2080 cache = g_hash_table_new(g_str_hash, g_str_equal); 2081 2082 if (WTSEnumerateSessionsA(NULL, 0, 1, &entries, &count)) { 2083 for (i = 0; i < count; ++i) { 2084 buffer_size = 0; 2085 info = NULL; 2086 if (WTSQuerySessionInformationA( 2087 NULL, 2088 entries[i].SessionId, 2089 WTSSessionInfo, 2090 (LPSTR *)&info, 2091 &buffer_size 2092 )) { 2093 2094 if (strlen(info->UserName) == 0) { 2095 WTSFreeMemory(info); 2096 continue; 2097 } 2098 2099 login = info->LogonTime.QuadPart; 2100 login -= W32_FT_OFFSET; 2101 login_time = ((double)login) / QGA_NANOSECONDS; 2102 2103 if (g_hash_table_contains(cache, info->UserName)) { 2104 value = g_hash_table_lookup(cache, info->UserName); 2105 user = (GuestUser *)value; 2106 if (user->login_time > login_time) { 2107 user->login_time = login_time; 2108 } 2109 } else { 2110 user = g_new0(GuestUser, 1); 2111 2112 user->user = g_strdup(info->UserName); 2113 user->domain = g_strdup(info->Domain); 2114 user->has_domain = true; 2115 2116 user->login_time = login_time; 2117 2118 g_hash_table_add(cache, user->user); 2119 2120 QAPI_LIST_APPEND(tail, user); 2121 } 2122 } 2123 WTSFreeMemory(info); 2124 } 2125 WTSFreeMemory(entries); 2126 } 2127 g_hash_table_destroy(cache); 2128 return head; 2129 } 2130 2131 typedef struct _ga_matrix_lookup_t { 2132 int major; 2133 int minor; 2134 char const *version; 2135 char const *version_id; 2136 } ga_matrix_lookup_t; 2137 2138 static ga_matrix_lookup_t const WIN_VERSION_MATRIX[2][7] = { 2139 { 2140 /* Desktop editions */ 2141 { 5, 0, "Microsoft Windows 2000", "2000"}, 2142 { 5, 1, "Microsoft Windows XP", "xp"}, 2143 { 6, 0, "Microsoft Windows Vista", "vista"}, 2144 { 6, 1, "Microsoft Windows 7" "7"}, 2145 { 6, 2, "Microsoft Windows 8", "8"}, 2146 { 6, 3, "Microsoft Windows 8.1", "8.1"}, 2147 { 0, 0, 0} 2148 },{ 2149 /* Server editions */ 2150 { 5, 2, "Microsoft Windows Server 2003", "2003"}, 2151 { 6, 0, "Microsoft Windows Server 2008", "2008"}, 2152 { 6, 1, "Microsoft Windows Server 2008 R2", "2008r2"}, 2153 { 6, 2, "Microsoft Windows Server 2012", "2012"}, 2154 { 6, 3, "Microsoft Windows Server 2012 R2", "2012r2"}, 2155 { 0, 0, 0}, 2156 { 0, 0, 0} 2157 } 2158 }; 2159 2160 typedef struct _ga_win_10_0_t { 2161 int first_build; 2162 char const *version; 2163 char const *version_id; 2164 } ga_win_10_0_t; 2165 2166 static ga_win_10_0_t const WIN_10_0_SERVER_VERSION_MATRIX[4] = { 2167 {14393, "Microsoft Windows Server 2016", "2016"}, 2168 {17763, "Microsoft Windows Server 2019", "2019"}, 2169 {20344, "Microsoft Windows Server 2022", "2022"}, 2170 {0, 0} 2171 }; 2172 2173 static ga_win_10_0_t const WIN_10_0_CLIENT_VERSION_MATRIX[3] = { 2174 {10240, "Microsoft Windows 10", "10"}, 2175 {22000, "Microsoft Windows 11", "11"}, 2176 {0, 0} 2177 }; 2178 2179 static void ga_get_win_version(RTL_OSVERSIONINFOEXW *info, Error **errp) 2180 { 2181 typedef NTSTATUS(WINAPI *rtl_get_version_t)( 2182 RTL_OSVERSIONINFOEXW *os_version_info_ex); 2183 2184 info->dwOSVersionInfoSize = sizeof(RTL_OSVERSIONINFOEXW); 2185 2186 HMODULE module = GetModuleHandle("ntdll"); 2187 PVOID fun = GetProcAddress(module, "RtlGetVersion"); 2188 if (fun == NULL) { 2189 error_setg(errp, QERR_QGA_COMMAND_FAILED, 2190 "Failed to get address of RtlGetVersion"); 2191 return; 2192 } 2193 2194 rtl_get_version_t rtl_get_version = (rtl_get_version_t)fun; 2195 rtl_get_version(info); 2196 return; 2197 } 2198 2199 static char *ga_get_win_name(OSVERSIONINFOEXW const *os_version, bool id) 2200 { 2201 DWORD major = os_version->dwMajorVersion; 2202 DWORD minor = os_version->dwMinorVersion; 2203 DWORD build = os_version->dwBuildNumber; 2204 int tbl_idx = (os_version->wProductType != VER_NT_WORKSTATION); 2205 ga_matrix_lookup_t const *table = WIN_VERSION_MATRIX[tbl_idx]; 2206 ga_win_10_0_t const *win_10_0_table = tbl_idx ? 2207 WIN_10_0_SERVER_VERSION_MATRIX : WIN_10_0_CLIENT_VERSION_MATRIX; 2208 ga_win_10_0_t const *win_10_0_version = NULL; 2209 while (table->version != NULL) { 2210 if (major == 10 && minor == 0) { 2211 while (win_10_0_table->version != NULL) { 2212 if (build >= win_10_0_table->first_build) { 2213 win_10_0_version = win_10_0_table; 2214 } 2215 win_10_0_table++; 2216 } 2217 if (win_10_0_table) { 2218 if (id) { 2219 return g_strdup(win_10_0_version->version_id); 2220 } else { 2221 return g_strdup(win_10_0_version->version); 2222 } 2223 } 2224 } else if (major == table->major && minor == table->minor) { 2225 if (id) { 2226 return g_strdup(table->version_id); 2227 } else { 2228 return g_strdup(table->version); 2229 } 2230 } 2231 ++table; 2232 } 2233 slog("failed to lookup Windows version: major=%lu, minor=%lu", 2234 major, minor); 2235 return g_strdup("N/A"); 2236 } 2237 2238 static char *ga_get_win_product_name(Error **errp) 2239 { 2240 HKEY key = INVALID_HANDLE_VALUE; 2241 DWORD size = 128; 2242 char *result = g_malloc0(size); 2243 LONG err = ERROR_SUCCESS; 2244 2245 err = RegOpenKeyA(HKEY_LOCAL_MACHINE, 2246 "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", 2247 &key); 2248 if (err != ERROR_SUCCESS) { 2249 error_setg_win32(errp, err, "failed to open registry key"); 2250 g_free(result); 2251 return NULL; 2252 } 2253 2254 err = RegQueryValueExA(key, "ProductName", NULL, NULL, 2255 (LPBYTE)result, &size); 2256 if (err == ERROR_MORE_DATA) { 2257 slog("ProductName longer than expected (%lu bytes), retrying", 2258 size); 2259 g_free(result); 2260 result = NULL; 2261 if (size > 0) { 2262 result = g_malloc0(size); 2263 err = RegQueryValueExA(key, "ProductName", NULL, NULL, 2264 (LPBYTE)result, &size); 2265 } 2266 } 2267 if (err != ERROR_SUCCESS) { 2268 error_setg_win32(errp, err, "failed to retrive ProductName"); 2269 goto fail; 2270 } 2271 2272 RegCloseKey(key); 2273 return result; 2274 2275 fail: 2276 if (key != INVALID_HANDLE_VALUE) { 2277 RegCloseKey(key); 2278 } 2279 g_free(result); 2280 return NULL; 2281 } 2282 2283 static char *ga_get_current_arch(void) 2284 { 2285 SYSTEM_INFO info; 2286 GetNativeSystemInfo(&info); 2287 char *result = NULL; 2288 switch (info.wProcessorArchitecture) { 2289 case PROCESSOR_ARCHITECTURE_AMD64: 2290 result = g_strdup("x86_64"); 2291 break; 2292 case PROCESSOR_ARCHITECTURE_ARM: 2293 result = g_strdup("arm"); 2294 break; 2295 case PROCESSOR_ARCHITECTURE_IA64: 2296 result = g_strdup("ia64"); 2297 break; 2298 case PROCESSOR_ARCHITECTURE_INTEL: 2299 result = g_strdup("x86"); 2300 break; 2301 case PROCESSOR_ARCHITECTURE_UNKNOWN: 2302 default: 2303 slog("unknown processor architecture 0x%0x", 2304 info.wProcessorArchitecture); 2305 result = g_strdup("unknown"); 2306 break; 2307 } 2308 return result; 2309 } 2310 2311 GuestOSInfo *qmp_guest_get_osinfo(Error **errp) 2312 { 2313 Error *local_err = NULL; 2314 OSVERSIONINFOEXW os_version = {0}; 2315 bool server; 2316 char *product_name; 2317 GuestOSInfo *info; 2318 2319 ga_get_win_version(&os_version, &local_err); 2320 if (local_err) { 2321 error_propagate(errp, local_err); 2322 return NULL; 2323 } 2324 2325 server = os_version.wProductType != VER_NT_WORKSTATION; 2326 product_name = ga_get_win_product_name(errp); 2327 if (product_name == NULL) { 2328 return NULL; 2329 } 2330 2331 info = g_new0(GuestOSInfo, 1); 2332 2333 info->has_kernel_version = true; 2334 info->kernel_version = g_strdup_printf("%lu.%lu", 2335 os_version.dwMajorVersion, 2336 os_version.dwMinorVersion); 2337 info->has_kernel_release = true; 2338 info->kernel_release = g_strdup_printf("%lu", 2339 os_version.dwBuildNumber); 2340 info->has_machine = true; 2341 info->machine = ga_get_current_arch(); 2342 2343 info->has_id = true; 2344 info->id = g_strdup("mswindows"); 2345 info->has_name = true; 2346 info->name = g_strdup("Microsoft Windows"); 2347 info->has_pretty_name = true; 2348 info->pretty_name = product_name; 2349 info->has_version = true; 2350 info->version = ga_get_win_name(&os_version, false); 2351 info->has_version_id = true; 2352 info->version_id = ga_get_win_name(&os_version, true); 2353 info->has_variant = true; 2354 info->variant = g_strdup(server ? "server" : "client"); 2355 info->has_variant_id = true; 2356 info->variant_id = g_strdup(server ? "server" : "client"); 2357 2358 return info; 2359 } 2360 2361 /* 2362 * Safely get device property. Returned strings are using wide characters. 2363 * Caller is responsible for freeing the buffer. 2364 */ 2365 static LPBYTE cm_get_property(DEVINST devInst, const DEVPROPKEY *propName, 2366 PDEVPROPTYPE propType) 2367 { 2368 CONFIGRET cr; 2369 g_autofree LPBYTE buffer = NULL; 2370 ULONG buffer_len = 0; 2371 2372 /* First query for needed space */ 2373 cr = CM_Get_DevNode_PropertyW(devInst, propName, propType, 2374 buffer, &buffer_len, 0); 2375 if (cr != CR_SUCCESS && cr != CR_BUFFER_SMALL) { 2376 2377 slog("failed to get property size, error=0x%lx", cr); 2378 return NULL; 2379 } 2380 buffer = g_new0(BYTE, buffer_len + 1); 2381 cr = CM_Get_DevNode_PropertyW(devInst, propName, propType, 2382 buffer, &buffer_len, 0); 2383 if (cr != CR_SUCCESS) { 2384 slog("failed to get device property, error=0x%lx", cr); 2385 return NULL; 2386 } 2387 return g_steal_pointer(&buffer); 2388 } 2389 2390 static GStrv ga_get_hardware_ids(DEVINST devInstance) 2391 { 2392 GArray *values = NULL; 2393 DEVPROPTYPE cm_type; 2394 LPWSTR id; 2395 g_autofree LPWSTR property = (LPWSTR)cm_get_property(devInstance, 2396 &qga_DEVPKEY_Device_HardwareIds, &cm_type); 2397 if (property == NULL) { 2398 slog("failed to get hardware IDs"); 2399 return NULL; 2400 } 2401 if (*property == '\0') { 2402 /* empty list */ 2403 return NULL; 2404 } 2405 values = g_array_new(TRUE, TRUE, sizeof(gchar *)); 2406 for (id = property; '\0' != *id; id += lstrlenW(id) + 1) { 2407 gchar *id8 = g_utf16_to_utf8(id, -1, NULL, NULL, NULL); 2408 g_array_append_val(values, id8); 2409 } 2410 return (GStrv)g_array_free(values, FALSE); 2411 } 2412 2413 /* 2414 * https://docs.microsoft.com/en-us/windows-hardware/drivers/install/identifiers-for-pci-devices 2415 */ 2416 #define DEVICE_PCI_RE "PCI\\\\VEN_(1AF4|1B36)&DEV_([0-9A-B]{4})(&|$)" 2417 2418 GuestDeviceInfoList *qmp_guest_get_devices(Error **errp) 2419 { 2420 GuestDeviceInfoList *head = NULL, **tail = &head; 2421 HDEVINFO dev_info = INVALID_HANDLE_VALUE; 2422 SP_DEVINFO_DATA dev_info_data; 2423 int i, j; 2424 GError *gerr = NULL; 2425 g_autoptr(GRegex) device_pci_re = NULL; 2426 DEVPROPTYPE cm_type; 2427 2428 device_pci_re = g_regex_new(DEVICE_PCI_RE, 2429 G_REGEX_ANCHORED | G_REGEX_OPTIMIZE, 0, 2430 &gerr); 2431 g_assert(device_pci_re != NULL); 2432 2433 dev_info_data.cbSize = sizeof(SP_DEVINFO_DATA); 2434 dev_info = SetupDiGetClassDevs(0, 0, 0, DIGCF_PRESENT | DIGCF_ALLCLASSES); 2435 if (dev_info == INVALID_HANDLE_VALUE) { 2436 error_setg(errp, "failed to get device tree"); 2437 return NULL; 2438 } 2439 2440 slog("enumerating devices"); 2441 for (i = 0; SetupDiEnumDeviceInfo(dev_info, i, &dev_info_data); i++) { 2442 bool skip = true; 2443 g_autofree LPWSTR name = NULL; 2444 g_autofree LPFILETIME date = NULL; 2445 g_autofree LPWSTR version = NULL; 2446 g_auto(GStrv) hw_ids = NULL; 2447 g_autoptr(GuestDeviceInfo) device = g_new0(GuestDeviceInfo, 1); 2448 g_autofree char *vendor_id = NULL; 2449 g_autofree char *device_id = NULL; 2450 2451 name = (LPWSTR)cm_get_property(dev_info_data.DevInst, 2452 &qga_DEVPKEY_NAME, &cm_type); 2453 if (name == NULL) { 2454 slog("failed to get device description"); 2455 continue; 2456 } 2457 device->driver_name = g_utf16_to_utf8(name, -1, NULL, NULL, NULL); 2458 if (device->driver_name == NULL) { 2459 error_setg(errp, "conversion to utf8 failed (driver name)"); 2460 return NULL; 2461 } 2462 slog("querying device: %s", device->driver_name); 2463 hw_ids = ga_get_hardware_ids(dev_info_data.DevInst); 2464 if (hw_ids == NULL) { 2465 continue; 2466 } 2467 for (j = 0; hw_ids[j] != NULL; j++) { 2468 g_autoptr(GMatchInfo) match_info; 2469 GuestDeviceIdPCI *id; 2470 if (!g_regex_match(device_pci_re, hw_ids[j], 0, &match_info)) { 2471 continue; 2472 } 2473 skip = false; 2474 2475 vendor_id = g_match_info_fetch(match_info, 1); 2476 device_id = g_match_info_fetch(match_info, 2); 2477 2478 device->id = g_new0(GuestDeviceId, 1); 2479 device->has_id = true; 2480 device->id->type = GUEST_DEVICE_TYPE_PCI; 2481 id = &device->id->u.pci; 2482 id->vendor_id = g_ascii_strtoull(vendor_id, NULL, 16); 2483 id->device_id = g_ascii_strtoull(device_id, NULL, 16); 2484 2485 break; 2486 } 2487 if (skip) { 2488 continue; 2489 } 2490 2491 version = (LPWSTR)cm_get_property(dev_info_data.DevInst, 2492 &qga_DEVPKEY_Device_DriverVersion, &cm_type); 2493 if (version == NULL) { 2494 slog("failed to get driver version"); 2495 continue; 2496 } 2497 device->driver_version = g_utf16_to_utf8(version, -1, NULL, 2498 NULL, NULL); 2499 if (device->driver_version == NULL) { 2500 error_setg(errp, "conversion to utf8 failed (driver version)"); 2501 return NULL; 2502 } 2503 device->has_driver_version = true; 2504 2505 date = (LPFILETIME)cm_get_property(dev_info_data.DevInst, 2506 &qga_DEVPKEY_Device_DriverDate, &cm_type); 2507 if (date == NULL) { 2508 slog("failed to get driver date"); 2509 continue; 2510 } 2511 device->driver_date = filetime_to_ns(date); 2512 device->has_driver_date = true; 2513 2514 slog("driver: %s\ndriver version: %" PRId64 ",%s\n", 2515 device->driver_name, device->driver_date, 2516 device->driver_version); 2517 QAPI_LIST_APPEND(tail, g_steal_pointer(&device)); 2518 } 2519 2520 if (dev_info != INVALID_HANDLE_VALUE) { 2521 SetupDiDestroyDeviceInfoList(dev_info); 2522 } 2523 return head; 2524 } 2525 2526 char *qga_get_host_name(Error **errp) 2527 { 2528 wchar_t tmp[MAX_COMPUTERNAME_LENGTH + 1]; 2529 DWORD size = G_N_ELEMENTS(tmp); 2530 2531 if (GetComputerNameW(tmp, &size) == 0) { 2532 error_setg_win32(errp, GetLastError(), "failed close handle"); 2533 return NULL; 2534 } 2535 2536 return g_utf16_to_utf8(tmp, size, NULL, NULL, NULL); 2537 } 2538 2539 GuestDiskStatsInfoList *qmp_guest_get_diskstats(Error **errp) 2540 { 2541 error_setg(errp, QERR_UNSUPPORTED); 2542 return NULL; 2543 } 2544 2545 GuestCpuStatsList *qmp_guest_get_cpustats(Error **errp) 2546 { 2547 error_setg(errp, QERR_UNSUPPORTED); 2548 return NULL; 2549 } 2550