2 * QEMU Guest Agent win32-specific command implementations
4 * Copyright IBM Corp. 2012
7 * Michael Roth <mdroth@linux.vnet.ibm.com>
8 * Gal Hammer <ghammer@redhat.com>
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.
15 # define _WIN32_WINNT 0x0600
17 #include "qemu/osdep.h"
24 #ifdef CONFIG_QGA_NTDDSCSI
33 #include "qga/guest-agent-core.h"
34 #include "qga/vss-win32.h"
35 #include "qga-qmp-commands.h"
36 #include "qapi/qmp/qerror.h"
37 #include "qemu/queue.h"
38 #include "qemu/host-utils.h"
39 #include "qemu/base64.h"
41 #ifndef SHTDN_REASON_FLAG_PLANNED
42 #define SHTDN_REASON_FLAG_PLANNED 0x80000000
45 /* multiple of 100 nanoseconds elapsed between windows baseline
46 * (1/1/1601) and Unix Epoch (1/1/1970), accounting for leap years */
47 #define W32_FT_OFFSET (10000000ULL * 60 * 60 * 24 * \
48 (365 * (1970 - 1601) + \
49 (1970 - 1601) / 4 - 3))
51 #define INVALID_SET_FILE_POINTER ((DWORD)-1)
53 typedef struct GuestFileHandle
{
56 QTAILQ_ENTRY(GuestFileHandle
) next
;
60 QTAILQ_HEAD(, GuestFileHandle
) filehandles
;
61 } guest_file_state
= {
62 .filehandles
= QTAILQ_HEAD_INITIALIZER(guest_file_state
.filehandles
),
65 #define FILE_GENERIC_APPEND (FILE_GENERIC_WRITE & ~FILE_WRITE_DATA)
67 typedef struct OpenFlags
{
70 DWORD creation_disposition
;
72 static OpenFlags guest_file_open_modes
[] = {
73 {"r", GENERIC_READ
, OPEN_EXISTING
},
74 {"rb", GENERIC_READ
, OPEN_EXISTING
},
75 {"w", GENERIC_WRITE
, CREATE_ALWAYS
},
76 {"wb", GENERIC_WRITE
, CREATE_ALWAYS
},
77 {"a", FILE_GENERIC_APPEND
, OPEN_ALWAYS
},
78 {"r+", GENERIC_WRITE
|GENERIC_READ
, OPEN_EXISTING
},
79 {"rb+", GENERIC_WRITE
|GENERIC_READ
, OPEN_EXISTING
},
80 {"r+b", GENERIC_WRITE
|GENERIC_READ
, OPEN_EXISTING
},
81 {"w+", GENERIC_WRITE
|GENERIC_READ
, CREATE_ALWAYS
},
82 {"wb+", GENERIC_WRITE
|GENERIC_READ
, CREATE_ALWAYS
},
83 {"w+b", GENERIC_WRITE
|GENERIC_READ
, CREATE_ALWAYS
},
84 {"a+", FILE_GENERIC_APPEND
|GENERIC_READ
, OPEN_ALWAYS
},
85 {"ab+", FILE_GENERIC_APPEND
|GENERIC_READ
, OPEN_ALWAYS
},
86 {"a+b", FILE_GENERIC_APPEND
|GENERIC_READ
, OPEN_ALWAYS
}
89 static OpenFlags
*find_open_flag(const char *mode_str
)
94 for (mode
= 0; mode
< ARRAY_SIZE(guest_file_open_modes
); ++mode
) {
95 OpenFlags
*flags
= guest_file_open_modes
+ mode
;
97 if (strcmp(flags
->forms
, mode_str
) == 0) {
102 error_setg(errp
, "invalid file open mode '%s'", mode_str
);
106 static int64_t guest_file_handle_add(HANDLE fh
, Error
**errp
)
108 GuestFileHandle
*gfh
;
111 handle
= ga_get_fd_handle(ga_state
, errp
);
115 gfh
= g_new0(GuestFileHandle
, 1);
118 QTAILQ_INSERT_TAIL(&guest_file_state
.filehandles
, gfh
, next
);
123 static GuestFileHandle
*guest_file_handle_find(int64_t id
, Error
**errp
)
125 GuestFileHandle
*gfh
;
126 QTAILQ_FOREACH(gfh
, &guest_file_state
.filehandles
, next
) {
131 error_setg(errp
, "handle '%" PRId64
"' has not been found", id
);
135 static void handle_set_nonblocking(HANDLE fh
)
137 DWORD file_type
, pipe_state
;
138 file_type
= GetFileType(fh
);
139 if (file_type
!= FILE_TYPE_PIPE
) {
142 /* If file_type == FILE_TYPE_PIPE, according to MSDN
143 * the specified file is socket or named pipe */
144 if (!GetNamedPipeHandleState(fh
, &pipe_state
, NULL
,
145 NULL
, NULL
, NULL
, 0)) {
148 /* The fd is named pipe fd */
149 if (pipe_state
& PIPE_NOWAIT
) {
153 pipe_state
|= PIPE_NOWAIT
;
154 SetNamedPipeHandleState(fh
, &pipe_state
, NULL
, NULL
);
157 int64_t qmp_guest_file_open(const char *path
, bool has_mode
,
158 const char *mode
, Error
**errp
)
162 HANDLE templ_file
= NULL
;
163 DWORD share_mode
= FILE_SHARE_READ
;
164 DWORD flags_and_attr
= FILE_ATTRIBUTE_NORMAL
;
165 LPSECURITY_ATTRIBUTES sa_attr
= NULL
;
166 OpenFlags
*guest_flags
;
171 slog("guest-file-open called, filepath: %s, mode: %s", path
, mode
);
172 guest_flags
= find_open_flag(mode
);
173 if (guest_flags
== NULL
) {
174 error_setg(errp
, "invalid file open mode");
178 fh
= CreateFile(path
, guest_flags
->desired_access
, share_mode
, sa_attr
,
179 guest_flags
->creation_disposition
, flags_and_attr
,
181 if (fh
== INVALID_HANDLE_VALUE
) {
182 error_setg_win32(errp
, GetLastError(), "failed to open file '%s'",
187 /* set fd non-blocking to avoid common use cases (like reading from a
188 * named pipe) from hanging the agent
190 handle_set_nonblocking(fh
);
192 fd
= guest_file_handle_add(fh
, errp
);
195 error_setg(errp
, "failed to add handle to qmp handle table");
199 slog("guest-file-open, handle: % " PRId64
, fd
);
203 void qmp_guest_file_close(int64_t handle
, Error
**errp
)
206 GuestFileHandle
*gfh
= guest_file_handle_find(handle
, errp
);
207 slog("guest-file-close called, handle: %" PRId64
, handle
);
211 ret
= CloseHandle(gfh
->fh
);
213 error_setg_win32(errp
, GetLastError(), "failed close handle");
217 QTAILQ_REMOVE(&guest_file_state
.filehandles
, gfh
, next
);
221 static void acquire_privilege(const char *name
, Error
**errp
)
224 TOKEN_PRIVILEGES priv
;
225 Error
*local_err
= NULL
;
227 if (OpenProcessToken(GetCurrentProcess(),
228 TOKEN_ADJUST_PRIVILEGES
|TOKEN_QUERY
, &token
))
230 if (!LookupPrivilegeValue(NULL
, name
, &priv
.Privileges
[0].Luid
)) {
231 error_setg(&local_err
, QERR_QGA_COMMAND_FAILED
,
232 "no luid for requested privilege");
236 priv
.PrivilegeCount
= 1;
237 priv
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
239 if (!AdjustTokenPrivileges(token
, FALSE
, &priv
, 0, NULL
, 0)) {
240 error_setg(&local_err
, QERR_QGA_COMMAND_FAILED
,
241 "unable to acquire requested privilege");
246 error_setg(&local_err
, QERR_QGA_COMMAND_FAILED
,
247 "failed to open privilege token");
254 error_propagate(errp
, local_err
);
257 static void execute_async(DWORD
WINAPI (*func
)(LPVOID
), LPVOID opaque
,
260 Error
*local_err
= NULL
;
262 HANDLE thread
= CreateThread(NULL
, 0, func
, opaque
, 0, NULL
);
264 error_setg(&local_err
, QERR_QGA_COMMAND_FAILED
,
265 "failed to dispatch asynchronous command");
266 error_propagate(errp
, local_err
);
270 void qmp_guest_shutdown(bool has_mode
, const char *mode
, Error
**errp
)
272 Error
*local_err
= NULL
;
273 UINT shutdown_flag
= EWX_FORCE
;
275 slog("guest-shutdown called, mode: %s", mode
);
277 if (!has_mode
|| strcmp(mode
, "powerdown") == 0) {
278 shutdown_flag
|= EWX_POWEROFF
;
279 } else if (strcmp(mode
, "halt") == 0) {
280 shutdown_flag
|= EWX_SHUTDOWN
;
281 } else if (strcmp(mode
, "reboot") == 0) {
282 shutdown_flag
|= EWX_REBOOT
;
284 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "mode",
285 "halt|powerdown|reboot");
289 /* Request a shutdown privilege, but try to shut down the system
291 acquire_privilege(SE_SHUTDOWN_NAME
, &local_err
);
293 error_propagate(errp
, local_err
);
297 if (!ExitWindowsEx(shutdown_flag
, SHTDN_REASON_FLAG_PLANNED
)) {
298 slog("guest-shutdown failed: %lu", GetLastError());
299 error_setg(errp
, QERR_UNDEFINED_ERROR
);
303 GuestFileRead
*qmp_guest_file_read(int64_t handle
, bool has_count
,
304 int64_t count
, Error
**errp
)
306 GuestFileRead
*read_data
= NULL
;
311 GuestFileHandle
*gfh
= guest_file_handle_find(handle
, errp
);
317 count
= QGA_READ_COUNT_DEFAULT
;
318 } else if (count
< 0) {
319 error_setg(errp
, "value '%" PRId64
320 "' is invalid for argument count", count
);
325 buf
= g_malloc0(count
+1);
326 is_ok
= ReadFile(fh
, buf
, count
, &read_count
, NULL
);
328 error_setg_win32(errp
, GetLastError(), "failed to read file");
329 slog("guest-file-read failed, handle %" PRId64
, handle
);
332 read_data
= g_new0(GuestFileRead
, 1);
333 read_data
->count
= (size_t)read_count
;
334 read_data
->eof
= read_count
== 0;
336 if (read_count
!= 0) {
337 read_data
->buf_b64
= g_base64_encode(buf
, read_count
);
345 GuestFileWrite
*qmp_guest_file_write(int64_t handle
, const char *buf_b64
,
346 bool has_count
, int64_t count
,
349 GuestFileWrite
*write_data
= NULL
;
354 GuestFileHandle
*gfh
= guest_file_handle_find(handle
, errp
);
361 buf
= qbase64_decode(buf_b64
, -1, &buf_len
, errp
);
368 } else if (count
< 0 || count
> buf_len
) {
369 error_setg(errp
, "value '%" PRId64
370 "' is invalid for argument count", count
);
374 is_ok
= WriteFile(fh
, buf
, count
, &write_count
, NULL
);
376 error_setg_win32(errp
, GetLastError(), "failed to write to file");
377 slog("guest-file-write-failed, handle: %" PRId64
, handle
);
379 write_data
= g_new0(GuestFileWrite
, 1);
380 write_data
->count
= (size_t) write_count
;
388 GuestFileSeek
*qmp_guest_file_seek(int64_t handle
, int64_t offset
,
389 GuestFileWhence
*whence_code
,
392 GuestFileHandle
*gfh
;
393 GuestFileSeek
*seek_data
;
395 LARGE_INTEGER new_pos
, off_pos
;
396 off_pos
.QuadPart
= offset
;
401 gfh
= guest_file_handle_find(handle
, errp
);
406 /* We stupidly exposed 'whence':'int' in our qapi */
407 whence
= ga_parse_whence(whence_code
, &err
);
409 error_propagate(errp
, err
);
414 res
= SetFilePointerEx(fh
, off_pos
, &new_pos
, whence
);
416 error_setg_win32(errp
, GetLastError(), "failed to seek file");
419 seek_data
= g_new0(GuestFileSeek
, 1);
420 seek_data
->position
= new_pos
.QuadPart
;
424 void qmp_guest_file_flush(int64_t handle
, Error
**errp
)
427 GuestFileHandle
*gfh
= guest_file_handle_find(handle
, errp
);
433 if (!FlushFileBuffers(fh
)) {
434 error_setg_win32(errp
, GetLastError(), "failed to flush file");
438 #ifdef CONFIG_QGA_NTDDSCSI
440 static STORAGE_BUS_TYPE win2qemu
[] = {
441 [BusTypeUnknown
] = GUEST_DISK_BUS_TYPE_UNKNOWN
,
442 [BusTypeScsi
] = GUEST_DISK_BUS_TYPE_SCSI
,
443 [BusTypeAtapi
] = GUEST_DISK_BUS_TYPE_IDE
,
444 [BusTypeAta
] = GUEST_DISK_BUS_TYPE_IDE
,
445 [BusType1394
] = GUEST_DISK_BUS_TYPE_IEEE1394
,
446 [BusTypeSsa
] = GUEST_DISK_BUS_TYPE_SSA
,
447 [BusTypeFibre
] = GUEST_DISK_BUS_TYPE_SSA
,
448 [BusTypeUsb
] = GUEST_DISK_BUS_TYPE_USB
,
449 [BusTypeRAID
] = GUEST_DISK_BUS_TYPE_RAID
,
450 #if (_WIN32_WINNT >= 0x0600)
451 [BusTypeiScsi
] = GUEST_DISK_BUS_TYPE_ISCSI
,
452 [BusTypeSas
] = GUEST_DISK_BUS_TYPE_SAS
,
453 [BusTypeSata
] = GUEST_DISK_BUS_TYPE_SATA
,
454 [BusTypeSd
] = GUEST_DISK_BUS_TYPE_SD
,
455 [BusTypeMmc
] = GUEST_DISK_BUS_TYPE_MMC
,
457 #if (_WIN32_WINNT >= 0x0601)
458 [BusTypeVirtual
] = GUEST_DISK_BUS_TYPE_VIRTUAL
,
459 [BusTypeFileBackedVirtual
] = GUEST_DISK_BUS_TYPE_FILE_BACKED_VIRTUAL
,
463 static GuestDiskBusType
find_bus_type(STORAGE_BUS_TYPE bus
)
465 if (bus
> ARRAY_SIZE(win2qemu
) || (int)bus
< 0) {
466 return GUEST_DISK_BUS_TYPE_UNKNOWN
;
468 return win2qemu
[(int)bus
];
471 DEFINE_GUID(GUID_DEVINTERFACE_VOLUME
,
472 0x53f5630dL
, 0xb6bf, 0x11d0, 0x94, 0xf2,
473 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
475 static GuestPCIAddress
*get_pci_info(char *guid
, Error
**errp
)
478 SP_DEVINFO_DATA dev_info_data
;
481 char dev_name
[MAX_PATH
];
483 GuestPCIAddress
*pci
= NULL
;
484 char *name
= g_strdup(&guid
[4]);
486 if (!QueryDosDevice(name
, dev_name
, ARRAY_SIZE(dev_name
))) {
487 error_setg_win32(errp
, GetLastError(), "failed to get dos device name");
491 dev_info
= SetupDiGetClassDevs(&GUID_DEVINTERFACE_VOLUME
, 0, 0,
492 DIGCF_PRESENT
| DIGCF_DEVICEINTERFACE
);
493 if (dev_info
== INVALID_HANDLE_VALUE
) {
494 error_setg_win32(errp
, GetLastError(), "failed to get devices tree");
498 dev_info_data
.cbSize
= sizeof(SP_DEVINFO_DATA
);
499 for (i
= 0; SetupDiEnumDeviceInfo(dev_info
, i
, &dev_info_data
); i
++) {
500 DWORD addr
, bus
, slot
, func
, dev
, data
, size2
;
501 while (!SetupDiGetDeviceRegistryProperty(dev_info
, &dev_info_data
,
502 SPDRP_PHYSICAL_DEVICE_OBJECT_NAME
,
503 &data
, (PBYTE
)buffer
, size
,
505 size
= MAX(size
, size2
);
506 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
) {
508 /* Double the size to avoid problems on
509 * W2k MBCS systems per KB 888609.
510 * https://support.microsoft.com/en-us/kb/259695 */
511 buffer
= g_malloc(size
* 2);
513 error_setg_win32(errp
, GetLastError(),
514 "failed to get device name");
519 if (g_strcmp0(buffer
, dev_name
)) {
523 /* There is no need to allocate buffer in the next functions. The size
524 * is known and ULONG according to
525 * https://support.microsoft.com/en-us/kb/253232
526 * https://msdn.microsoft.com/en-us/library/windows/hardware/ff543095(v=vs.85).aspx
528 if (!SetupDiGetDeviceRegistryProperty(dev_info
, &dev_info_data
,
529 SPDRP_BUSNUMBER
, &data
, (PBYTE
)&bus
, size
, NULL
)) {
533 /* The function retrieves the device's address. This value will be
534 * transformed into device function and number */
535 if (!SetupDiGetDeviceRegistryProperty(dev_info
, &dev_info_data
,
536 SPDRP_ADDRESS
, &data
, (PBYTE
)&addr
, size
, NULL
)) {
540 /* This call returns UINumber of DEVICE_CAPABILITIES structure.
541 * This number is typically a user-perceived slot number. */
542 if (!SetupDiGetDeviceRegistryProperty(dev_info
, &dev_info_data
,
543 SPDRP_UI_NUMBER
, &data
, (PBYTE
)&slot
, size
, NULL
)) {
547 /* SetupApi gives us the same information as driver with
548 * IoGetDeviceProperty. According to Microsoft
549 * https://support.microsoft.com/en-us/kb/253232
550 * FunctionNumber = (USHORT)((propertyAddress) & 0x0000FFFF);
551 * DeviceNumber = (USHORT)(((propertyAddress) >> 16) & 0x0000FFFF);
552 * SPDRP_ADDRESS is propertyAddress, so we do the same.*/
554 func
= addr
& 0x0000FFFF;
555 dev
= (addr
>> 16) & 0x0000FFFF;
556 pci
= g_malloc0(sizeof(*pci
));
559 pci
->function
= func
;
569 static int get_disk_bus_type(HANDLE vol_h
, Error
**errp
)
571 STORAGE_PROPERTY_QUERY query
;
572 STORAGE_DEVICE_DESCRIPTOR
*dev_desc
, buf
;
576 dev_desc
->Size
= sizeof(buf
);
577 query
.PropertyId
= StorageDeviceProperty
;
578 query
.QueryType
= PropertyStandardQuery
;
580 if (!DeviceIoControl(vol_h
, IOCTL_STORAGE_QUERY_PROPERTY
, &query
,
581 sizeof(STORAGE_PROPERTY_QUERY
), dev_desc
,
582 dev_desc
->Size
, &received
, NULL
)) {
583 error_setg_win32(errp
, GetLastError(), "failed to get bus type");
587 return dev_desc
->BusType
;
590 /* VSS provider works with volumes, thus there is no difference if
591 * the volume consist of spanned disks. Info about the first disk in the
592 * volume is returned for the spanned disk group (LVM) */
593 static GuestDiskAddressList
*build_guest_disk_info(char *guid
, Error
**errp
)
595 GuestDiskAddressList
*list
= NULL
;
596 GuestDiskAddress
*disk
;
597 SCSI_ADDRESS addr
, *scsi_ad
;
603 char *name
= g_strndup(guid
, strlen(guid
)-1);
605 vol_h
= CreateFile(name
, 0, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
,
607 if (vol_h
== INVALID_HANDLE_VALUE
) {
608 error_setg_win32(errp
, GetLastError(), "failed to open volume");
612 bus
= get_disk_bus_type(vol_h
, errp
);
617 disk
= g_malloc0(sizeof(*disk
));
618 disk
->bus_type
= find_bus_type(bus
);
619 if (bus
== BusTypeScsi
|| bus
== BusTypeAta
|| bus
== BusTypeRAID
620 #if (_WIN32_WINNT >= 0x0600)
621 /* This bus type is not supported before Windows Server 2003 SP1 */
625 /* We are able to use the same ioctls for different bus types
626 * according to Microsoft docs
627 * https://technet.microsoft.com/en-us/library/ee851589(v=ws.10).aspx */
628 if (DeviceIoControl(vol_h
, IOCTL_SCSI_GET_ADDRESS
, NULL
, 0, scsi_ad
,
629 sizeof(SCSI_ADDRESS
), &len
, NULL
)) {
630 disk
->unit
= addr
.Lun
;
631 disk
->target
= addr
.TargetId
;
632 disk
->bus
= addr
.PathId
;
633 disk
->pci_controller
= get_pci_info(name
, errp
);
635 /* We do not set error in this case, because we still have enough
636 * information about volume. */
638 disk
->pci_controller
= NULL
;
641 list
= g_malloc0(sizeof(*list
));
653 static GuestDiskAddressList
*build_guest_disk_info(char *guid
, Error
**errp
)
658 #endif /* CONFIG_QGA_NTDDSCSI */
660 static GuestFilesystemInfo
*build_guest_fsinfo(char *guid
, Error
**errp
)
663 char mnt
, *mnt_point
;
665 char vol_info
[MAX_PATH
+1];
667 GuestFilesystemInfo
*fs
= NULL
;
669 GetVolumePathNamesForVolumeName(guid
, (LPCH
)&mnt
, 0, &info_size
);
670 if (GetLastError() != ERROR_MORE_DATA
) {
671 error_setg_win32(errp
, GetLastError(), "failed to get volume name");
675 mnt_point
= g_malloc(info_size
+ 1);
676 if (!GetVolumePathNamesForVolumeName(guid
, mnt_point
, info_size
,
678 error_setg_win32(errp
, GetLastError(), "failed to get volume name");
682 len
= strlen(mnt_point
);
683 mnt_point
[len
] = '\\';
684 mnt_point
[len
+1] = 0;
685 if (!GetVolumeInformation(mnt_point
, vol_info
, sizeof(vol_info
), NULL
, NULL
,
686 NULL
, (LPSTR
)&fs_name
, sizeof(fs_name
))) {
687 if (GetLastError() != ERROR_NOT_READY
) {
688 error_setg_win32(errp
, GetLastError(), "failed to get volume info");
693 fs_name
[sizeof(fs_name
) - 1] = 0;
694 fs
= g_malloc(sizeof(*fs
));
695 fs
->name
= g_strdup(guid
);
697 fs
->mountpoint
= g_strdup("System Reserved");
699 fs
->mountpoint
= g_strndup(mnt_point
, len
);
701 fs
->type
= g_strdup(fs_name
);
702 fs
->disk
= build_guest_disk_info(guid
, errp
);
708 GuestFilesystemInfoList
*qmp_guest_get_fsinfo(Error
**errp
)
711 GuestFilesystemInfoList
*new, *ret
= NULL
;
714 vol_h
= FindFirstVolume(guid
, sizeof(guid
));
715 if (vol_h
== INVALID_HANDLE_VALUE
) {
716 error_setg_win32(errp
, GetLastError(), "failed to find any volume");
721 GuestFilesystemInfo
*info
= build_guest_fsinfo(guid
, errp
);
725 new = g_malloc(sizeof(*ret
));
729 } while (FindNextVolume(vol_h
, guid
, sizeof(guid
)));
731 if (GetLastError() != ERROR_NO_MORE_FILES
) {
732 error_setg_win32(errp
, GetLastError(), "failed to find next volume");
735 FindVolumeClose(vol_h
);
740 * Return status of freeze/thaw
742 GuestFsfreezeStatus
qmp_guest_fsfreeze_status(Error
**errp
)
744 if (!vss_initialized()) {
745 error_setg(errp
, QERR_UNSUPPORTED
);
749 if (ga_is_frozen(ga_state
)) {
750 return GUEST_FSFREEZE_STATUS_FROZEN
;
753 return GUEST_FSFREEZE_STATUS_THAWED
;
757 * Freeze local file systems using Volume Shadow-copy Service.
758 * The frozen state is limited for up to 10 seconds by VSS.
760 int64_t qmp_guest_fsfreeze_freeze(Error
**errp
)
763 Error
*local_err
= NULL
;
765 if (!vss_initialized()) {
766 error_setg(errp
, QERR_UNSUPPORTED
);
770 slog("guest-fsfreeze called");
772 /* cannot risk guest agent blocking itself on a write in this state */
773 ga_set_frozen(ga_state
);
775 qga_vss_fsfreeze(&i
, true, &local_err
);
777 error_propagate(errp
, local_err
);
785 qmp_guest_fsfreeze_thaw(&local_err
);
787 g_debug("cleanup thaw: %s", error_get_pretty(local_err
));
788 error_free(local_err
);
793 int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints
,
794 strList
*mountpoints
,
797 error_setg(errp
, QERR_UNSUPPORTED
);
803 * Thaw local file systems using Volume Shadow-copy Service.
805 int64_t qmp_guest_fsfreeze_thaw(Error
**errp
)
809 if (!vss_initialized()) {
810 error_setg(errp
, QERR_UNSUPPORTED
);
814 qga_vss_fsfreeze(&i
, false, errp
);
816 ga_unset_frozen(ga_state
);
820 static void guest_fsfreeze_cleanup(void)
824 if (!vss_initialized()) {
828 if (ga_is_frozen(ga_state
) == GUEST_FSFREEZE_STATUS_FROZEN
) {
829 qmp_guest_fsfreeze_thaw(&err
);
831 slog("failed to clean up frozen filesystems: %s",
832 error_get_pretty(err
));
841 * Walk list of mounted file systems in the guest, and discard unused
844 GuestFilesystemTrimResponse
*
845 qmp_guest_fstrim(bool has_minimum
, int64_t minimum
, Error
**errp
)
847 GuestFilesystemTrimResponse
*resp
;
849 WCHAR guid
[MAX_PATH
] = L
"";
851 handle
= FindFirstVolumeW(guid
, ARRAYSIZE(guid
));
852 if (handle
== INVALID_HANDLE_VALUE
) {
853 error_setg_win32(errp
, GetLastError(), "failed to find any volume");
857 resp
= g_new0(GuestFilesystemTrimResponse
, 1);
860 GuestFilesystemTrimResult
*res
;
861 GuestFilesystemTrimResultList
*list
;
863 DWORD char_count
= 0;
868 GetVolumePathNamesForVolumeNameW(guid
, NULL
, 0, &char_count
);
870 if (GetLastError() != ERROR_MORE_DATA
) {
873 if (GetDriveTypeW(guid
) != DRIVE_FIXED
) {
877 uc_path
= g_malloc(sizeof(WCHAR
) * char_count
);
878 if (!GetVolumePathNamesForVolumeNameW(guid
, uc_path
, char_count
,
879 &char_count
) || !*uc_path
) {
880 /* strange, but this condition could be faced even with size == 2 */
885 res
= g_new0(GuestFilesystemTrimResult
, 1);
887 path
= g_utf16_to_utf8(uc_path
, char_count
, NULL
, NULL
, &gerr
);
892 res
->has_error
= true;
893 res
->error
= g_strdup(gerr
->message
);
900 list
= g_new0(GuestFilesystemTrimResultList
, 1);
902 list
->next
= resp
->paths
;
906 memset(argv
, 0, sizeof(argv
));
907 argv
[0] = (gchar
*)"defrag.exe";
908 argv
[1] = (gchar
*)"/L";
911 if (!g_spawn_sync(NULL
, argv
, NULL
, G_SPAWN_SEARCH_PATH
, NULL
, NULL
,
912 &out
/* stdout */, NULL
/* stdin */,
914 res
->has_error
= true;
915 res
->error
= g_strdup(gerr
->message
);
918 /* defrag.exe is UGLY. Exit code is ALWAYS zero.
919 Error is reported in the output with something like
920 (x89000020) etc code in the stdout */
923 gchar
**lines
= g_strsplit(out
, "\r\n", 0);
926 for (i
= 0; lines
[i
] != NULL
; i
++) {
927 if (g_strstr_len(lines
[i
], -1, "(0x") == NULL
) {
930 res
->has_error
= true;
931 res
->error
= g_strdup(lines
[i
]);
936 } while (FindNextVolumeW(handle
, guid
, ARRAYSIZE(guid
)));
938 FindVolumeClose(handle
);
943 GUEST_SUSPEND_MODE_DISK
,
944 GUEST_SUSPEND_MODE_RAM
947 static void check_suspend_mode(GuestSuspendMode mode
, Error
**errp
)
949 SYSTEM_POWER_CAPABILITIES sys_pwr_caps
;
950 Error
*local_err
= NULL
;
952 ZeroMemory(&sys_pwr_caps
, sizeof(sys_pwr_caps
));
953 if (!GetPwrCapabilities(&sys_pwr_caps
)) {
954 error_setg(&local_err
, QERR_QGA_COMMAND_FAILED
,
955 "failed to determine guest suspend capabilities");
960 case GUEST_SUSPEND_MODE_DISK
:
961 if (!sys_pwr_caps
.SystemS4
) {
962 error_setg(&local_err
, QERR_QGA_COMMAND_FAILED
,
963 "suspend-to-disk not supported by OS");
966 case GUEST_SUSPEND_MODE_RAM
:
967 if (!sys_pwr_caps
.SystemS3
) {
968 error_setg(&local_err
, QERR_QGA_COMMAND_FAILED
,
969 "suspend-to-ram not supported by OS");
973 error_setg(&local_err
, QERR_INVALID_PARAMETER_VALUE
, "mode",
978 error_propagate(errp
, local_err
);
981 static DWORD WINAPI
do_suspend(LPVOID opaque
)
983 GuestSuspendMode
*mode
= opaque
;
986 if (!SetSuspendState(*mode
== GUEST_SUSPEND_MODE_DISK
, TRUE
, TRUE
)) {
987 slog("failed to suspend guest, %lu", GetLastError());
994 void qmp_guest_suspend_disk(Error
**errp
)
996 Error
*local_err
= NULL
;
997 GuestSuspendMode
*mode
= g_new(GuestSuspendMode
, 1);
999 *mode
= GUEST_SUSPEND_MODE_DISK
;
1000 check_suspend_mode(*mode
, &local_err
);
1001 acquire_privilege(SE_SHUTDOWN_NAME
, &local_err
);
1002 execute_async(do_suspend
, mode
, &local_err
);
1005 error_propagate(errp
, local_err
);
1010 void qmp_guest_suspend_ram(Error
**errp
)
1012 Error
*local_err
= NULL
;
1013 GuestSuspendMode
*mode
= g_new(GuestSuspendMode
, 1);
1015 *mode
= GUEST_SUSPEND_MODE_RAM
;
1016 check_suspend_mode(*mode
, &local_err
);
1017 acquire_privilege(SE_SHUTDOWN_NAME
, &local_err
);
1018 execute_async(do_suspend
, mode
, &local_err
);
1021 error_propagate(errp
, local_err
);
1026 void qmp_guest_suspend_hybrid(Error
**errp
)
1028 error_setg(errp
, QERR_UNSUPPORTED
);
1031 static IP_ADAPTER_ADDRESSES
*guest_get_adapters_addresses(Error
**errp
)
1033 IP_ADAPTER_ADDRESSES
*adptr_addrs
= NULL
;
1034 ULONG adptr_addrs_len
= 0;
1037 /* Call the first time to get the adptr_addrs_len. */
1038 GetAdaptersAddresses(AF_UNSPEC
, GAA_FLAG_INCLUDE_PREFIX
,
1039 NULL
, adptr_addrs
, &adptr_addrs_len
);
1041 adptr_addrs
= g_malloc(adptr_addrs_len
);
1042 ret
= GetAdaptersAddresses(AF_UNSPEC
, GAA_FLAG_INCLUDE_PREFIX
,
1043 NULL
, adptr_addrs
, &adptr_addrs_len
);
1044 if (ret
!= ERROR_SUCCESS
) {
1045 error_setg_win32(errp
, ret
, "failed to get adapters addresses");
1046 g_free(adptr_addrs
);
1052 static char *guest_wctomb_dup(WCHAR
*wstr
)
1057 i
= wcslen(wstr
) + 1;
1059 WideCharToMultiByte(CP_ACP
, WC_COMPOSITECHECK
,
1060 wstr
, -1, str
, i
, NULL
, NULL
);
1064 static char *guest_addr_to_str(IP_ADAPTER_UNICAST_ADDRESS
*ip_addr
,
1067 char addr_str
[INET6_ADDRSTRLEN
+ INET_ADDRSTRLEN
];
1071 if (ip_addr
->Address
.lpSockaddr
->sa_family
== AF_INET
||
1072 ip_addr
->Address
.lpSockaddr
->sa_family
== AF_INET6
) {
1073 len
= sizeof(addr_str
);
1074 ret
= WSAAddressToString(ip_addr
->Address
.lpSockaddr
,
1075 ip_addr
->Address
.iSockaddrLength
,
1080 error_setg_win32(errp
, WSAGetLastError(),
1081 "failed address presentation form conversion");
1084 return g_strdup(addr_str
);
1089 #if (_WIN32_WINNT >= 0x0600)
1090 static int64_t guest_ip_prefix(IP_ADAPTER_UNICAST_ADDRESS
*ip_addr
)
1092 /* For Windows Vista/2008 and newer, use the OnLinkPrefixLength
1093 * field to obtain the prefix.
1095 return ip_addr
->OnLinkPrefixLength
;
1098 /* When using the Windows XP and 2003 build environment, do the best we can to
1099 * figure out the prefix.
1101 static IP_ADAPTER_INFO
*guest_get_adapters_info(void)
1103 IP_ADAPTER_INFO
*adptr_info
= NULL
;
1104 ULONG adptr_info_len
= 0;
1107 /* Call the first time to get the adptr_info_len. */
1108 GetAdaptersInfo(adptr_info
, &adptr_info_len
);
1110 adptr_info
= g_malloc(adptr_info_len
);
1111 ret
= GetAdaptersInfo(adptr_info
, &adptr_info_len
);
1112 if (ret
!= ERROR_SUCCESS
) {
1119 static int64_t guest_ip_prefix(IP_ADAPTER_UNICAST_ADDRESS
*ip_addr
)
1121 int64_t prefix
= -1; /* Use for AF_INET6 and unknown/undetermined values. */
1122 IP_ADAPTER_INFO
*adptr_info
, *info
;
1126 if (ip_addr
->Address
.lpSockaddr
->sa_family
!= AF_INET
) {
1129 adptr_info
= guest_get_adapters_info();
1130 if (adptr_info
== NULL
) {
1134 /* Match up the passed in ip_addr with one found in adaptr_info.
1135 * The matching one in adptr_info will have the netmask.
1137 p
= &((struct sockaddr_in
*)ip_addr
->Address
.lpSockaddr
)->sin_addr
;
1138 for (info
= adptr_info
; info
; info
= info
->Next
) {
1139 for (ip
= &info
->IpAddressList
; ip
; ip
= ip
->Next
) {
1140 if (p
->S_un
.S_addr
== inet_addr(ip
->IpAddress
.String
)) {
1141 prefix
= ctpop32(inet_addr(ip
->IpMask
.String
));
1152 GuestNetworkInterfaceList
*qmp_guest_network_get_interfaces(Error
**errp
)
1154 IP_ADAPTER_ADDRESSES
*adptr_addrs
, *addr
;
1155 IP_ADAPTER_UNICAST_ADDRESS
*ip_addr
= NULL
;
1156 GuestNetworkInterfaceList
*head
= NULL
, *cur_item
= NULL
;
1157 GuestIpAddressList
*head_addr
, *cur_addr
;
1158 GuestNetworkInterfaceList
*info
;
1159 GuestIpAddressList
*address_item
= NULL
;
1160 unsigned char *mac_addr
;
1166 adptr_addrs
= guest_get_adapters_addresses(errp
);
1167 if (adptr_addrs
== NULL
) {
1171 /* Make WSA APIs available. */
1172 wsa_version
= MAKEWORD(2, 2);
1173 ret
= WSAStartup(wsa_version
, &wsa_data
);
1175 error_setg_win32(errp
, ret
, "failed socket startup");
1179 for (addr
= adptr_addrs
; addr
; addr
= addr
->Next
) {
1180 info
= g_malloc0(sizeof(*info
));
1182 if (cur_item
== NULL
) {
1183 head
= cur_item
= info
;
1185 cur_item
->next
= info
;
1189 info
->value
= g_malloc0(sizeof(*info
->value
));
1190 info
->value
->name
= guest_wctomb_dup(addr
->FriendlyName
);
1192 if (addr
->PhysicalAddressLength
!= 0) {
1193 mac_addr
= addr
->PhysicalAddress
;
1195 info
->value
->hardware_address
=
1196 g_strdup_printf("%02x:%02x:%02x:%02x:%02x:%02x",
1197 (int) mac_addr
[0], (int) mac_addr
[1],
1198 (int) mac_addr
[2], (int) mac_addr
[3],
1199 (int) mac_addr
[4], (int) mac_addr
[5]);
1201 info
->value
->has_hardware_address
= true;
1206 for (ip_addr
= addr
->FirstUnicastAddress
;
1208 ip_addr
= ip_addr
->Next
) {
1209 addr_str
= guest_addr_to_str(ip_addr
, errp
);
1210 if (addr_str
== NULL
) {
1214 address_item
= g_malloc0(sizeof(*address_item
));
1217 head_addr
= cur_addr
= address_item
;
1219 cur_addr
->next
= address_item
;
1220 cur_addr
= address_item
;
1223 address_item
->value
= g_malloc0(sizeof(*address_item
->value
));
1224 address_item
->value
->ip_address
= addr_str
;
1225 address_item
->value
->prefix
= guest_ip_prefix(ip_addr
);
1226 if (ip_addr
->Address
.lpSockaddr
->sa_family
== AF_INET
) {
1227 address_item
->value
->ip_address_type
=
1228 GUEST_IP_ADDRESS_TYPE_IPV4
;
1229 } else if (ip_addr
->Address
.lpSockaddr
->sa_family
== AF_INET6
) {
1230 address_item
->value
->ip_address_type
=
1231 GUEST_IP_ADDRESS_TYPE_IPV6
;
1235 info
->value
->has_ip_addresses
= true;
1236 info
->value
->ip_addresses
= head_addr
;
1241 g_free(adptr_addrs
);
1245 int64_t qmp_guest_get_time(Error
**errp
)
1247 SYSTEMTIME ts
= {0};
1251 if (ts
.wYear
< 1601 || ts
.wYear
> 30827) {
1252 error_setg(errp
, "Failed to get time");
1256 if (!SystemTimeToFileTime(&ts
, &tf
)) {
1257 error_setg(errp
, "Failed to convert system time: %d", (int)GetLastError());
1261 return ((((int64_t)tf
.dwHighDateTime
<< 32) | tf
.dwLowDateTime
)
1262 - W32_FT_OFFSET
) * 100;
1265 void qmp_guest_set_time(bool has_time
, int64_t time_ns
, Error
**errp
)
1267 Error
*local_err
= NULL
;
1273 /* Unfortunately, Windows libraries don't provide an easy way to access
1276 * https://msdn.microsoft.com/en-us/library/aa908981.aspx
1278 error_setg(errp
, "Time argument is required on this platform");
1282 /* Validate time passed by user. */
1283 if (time_ns
< 0 || time_ns
/ 100 > INT64_MAX
- W32_FT_OFFSET
) {
1284 error_setg(errp
, "Time %" PRId64
"is invalid", time_ns
);
1288 time
= time_ns
/ 100 + W32_FT_OFFSET
;
1290 tf
.dwLowDateTime
= (DWORD
) time
;
1291 tf
.dwHighDateTime
= (DWORD
) (time
>> 32);
1293 if (!FileTimeToSystemTime(&tf
, &ts
)) {
1294 error_setg(errp
, "Failed to convert system time %d",
1295 (int)GetLastError());
1299 acquire_privilege(SE_SYSTEMTIME_NAME
, &local_err
);
1301 error_propagate(errp
, local_err
);
1305 if (!SetSystemTime(&ts
)) {
1306 error_setg(errp
, "Failed to set time to guest: %d", (int)GetLastError());
1311 GuestLogicalProcessorList
*qmp_guest_get_vcpus(Error
**errp
)
1313 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION pslpi
, ptr
;
1315 GuestLogicalProcessorList
*head
, **link
;
1316 Error
*local_err
= NULL
;
1325 if ((GetLogicalProcessorInformation(pslpi
, &length
) == FALSE
) &&
1326 (GetLastError() == ERROR_INSUFFICIENT_BUFFER
) &&
1327 (length
> sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION
))) {
1328 ptr
= pslpi
= g_malloc0(length
);
1329 if (GetLogicalProcessorInformation(pslpi
, &length
) == FALSE
) {
1330 error_setg(&local_err
, "Failed to get processor information: %d",
1331 (int)GetLastError());
1334 error_setg(&local_err
,
1335 "Failed to get processor information buffer length: %d",
1336 (int)GetLastError());
1339 while ((local_err
== NULL
) && (length
> 0)) {
1340 if (pslpi
->Relationship
== RelationProcessorCore
) {
1341 ULONG_PTR cpu_bits
= pslpi
->ProcessorMask
;
1343 while (cpu_bits
> 0) {
1344 if (!!(cpu_bits
& 1)) {
1345 GuestLogicalProcessor
*vcpu
;
1346 GuestLogicalProcessorList
*entry
;
1348 vcpu
= g_malloc0(sizeof *vcpu
);
1349 vcpu
->logical_id
= current
++;
1350 vcpu
->online
= true;
1351 vcpu
->has_can_offline
= true;
1353 entry
= g_malloc0(sizeof *entry
);
1354 entry
->value
= vcpu
;
1357 link
= &entry
->next
;
1362 length
-= sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION
);
1363 pslpi
++; /* next entry */
1368 if (local_err
== NULL
) {
1372 /* there's no guest with zero VCPUs */
1373 error_setg(&local_err
, "Guest reported zero VCPUs");
1376 qapi_free_GuestLogicalProcessorList(head
);
1377 error_propagate(errp
, local_err
);
1381 int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList
*vcpus
, Error
**errp
)
1383 error_setg(errp
, QERR_UNSUPPORTED
);
1388 get_net_error_message(gint error
)
1390 HMODULE module
= NULL
;
1391 gchar
*retval
= NULL
;
1392 wchar_t *msg
= NULL
;
1396 flags
= FORMAT_MESSAGE_ALLOCATE_BUFFER
|
1397 FORMAT_MESSAGE_IGNORE_INSERTS
|
1398 FORMAT_MESSAGE_FROM_SYSTEM
;
1400 if (error
>= NERR_BASE
&& error
<= MAX_NERR
) {
1401 module
= LoadLibraryExW(L
"netmsg.dll", NULL
, LOAD_LIBRARY_AS_DATAFILE
);
1403 if (module
!= NULL
) {
1404 flags
|= FORMAT_MESSAGE_FROM_HMODULE
;
1408 FormatMessageW(flags
, module
, error
, 0, (LPWSTR
)&msg
, 0, NULL
);
1411 nchars
= wcslen(msg
);
1414 msg
[nchars
- 1] == L
'\n' &&
1415 msg
[nchars
- 2] == L
'\r') {
1416 msg
[nchars
- 2] = L
'\0';
1419 retval
= g_utf16_to_utf8(msg
, -1, NULL
, NULL
, NULL
);
1424 if (module
!= NULL
) {
1425 FreeLibrary(module
);
1431 void qmp_guest_set_user_password(const char *username
,
1432 const char *password
,
1437 char *rawpasswddata
= NULL
;
1438 size_t rawpasswdlen
;
1439 wchar_t *user
= NULL
, *wpass
= NULL
;
1440 USER_INFO_1003 pi1003
= { 0, };
1441 GError
*gerr
= NULL
;
1444 error_setg(errp
, QERR_UNSUPPORTED
);
1448 rawpasswddata
= (char *)qbase64_decode(password
, -1, &rawpasswdlen
, errp
);
1449 if (!rawpasswddata
) {
1452 rawpasswddata
= g_renew(char, rawpasswddata
, rawpasswdlen
+ 1);
1453 rawpasswddata
[rawpasswdlen
] = '\0';
1455 user
= g_utf8_to_utf16(username
, -1, NULL
, NULL
, &gerr
);
1460 wpass
= g_utf8_to_utf16(rawpasswddata
, -1, NULL
, NULL
, &gerr
);
1465 pi1003
.usri1003_password
= wpass
;
1466 nas
= NetUserSetInfo(NULL
, user
,
1467 1003, (LPBYTE
)&pi1003
,
1470 if (nas
!= NERR_Success
) {
1471 gchar
*msg
= get_net_error_message(nas
);
1472 error_setg(errp
, "failed to set password: %s", msg
);
1478 error_setg(errp
, QERR_QGA_COMMAND_FAILED
, gerr
->message
);
1483 g_free(rawpasswddata
);
1486 GuestMemoryBlockList
*qmp_guest_get_memory_blocks(Error
**errp
)
1488 error_setg(errp
, QERR_UNSUPPORTED
);
1492 GuestMemoryBlockResponseList
*
1493 qmp_guest_set_memory_blocks(GuestMemoryBlockList
*mem_blks
, Error
**errp
)
1495 error_setg(errp
, QERR_UNSUPPORTED
);
1499 GuestMemoryBlockInfo
*qmp_guest_get_memory_block_info(Error
**errp
)
1501 error_setg(errp
, QERR_UNSUPPORTED
);
1505 /* add unsupported commands to the blacklist */
1506 GList
*ga_command_blacklist_init(GList
*blacklist
)
1508 const char *list_unsupported
[] = {
1509 "guest-suspend-hybrid",
1511 "guest-get-memory-blocks", "guest-set-memory-blocks",
1512 "guest-get-memory-block-size",
1513 "guest-fsfreeze-freeze-list",
1515 char **p
= (char **)list_unsupported
;
1518 blacklist
= g_list_append(blacklist
, g_strdup(*p
++));
1521 if (!vss_init(true)) {
1522 g_debug("vss_init failed, vss commands are going to be disabled");
1523 const char *list
[] = {
1524 "guest-get-fsinfo", "guest-fsfreeze-status",
1525 "guest-fsfreeze-freeze", "guest-fsfreeze-thaw", NULL
};
1529 blacklist
= g_list_append(blacklist
, g_strdup(*p
++));
1536 /* register init/cleanup routines for stateful command groups */
1537 void ga_command_state_init(GAState
*s
, GACommandState
*cs
)
1539 if (!vss_initialized()) {
1540 ga_command_state_add(cs
, NULL
, guest_fsfreeze_cleanup
);
1544 /* MINGW is missing two fields: IncomingFrames & OutgoingFrames */
1545 typedef struct _GA_WTSINFOA
{
1546 WTS_CONNECTSTATE_CLASS State
;
1548 DWORD IncomingBytes
;
1549 DWORD OutgoingBytes
;
1550 DWORD IncomingFrames
;
1551 DWORD OutgoingFrames
;
1552 DWORD IncomingCompressedBytes
;
1553 DWORD OutgoingCompressedBy
;
1554 CHAR WinStationName
[WINSTATIONNAME_LENGTH
];
1555 CHAR Domain
[DOMAIN_LENGTH
];
1556 CHAR UserName
[USERNAME_LENGTH
+ 1];
1557 LARGE_INTEGER ConnectTime
;
1558 LARGE_INTEGER DisconnectTime
;
1559 LARGE_INTEGER LastInputTime
;
1560 LARGE_INTEGER LogonTime
;
1561 LARGE_INTEGER CurrentTime
;
1565 GuestUserList
*qmp_guest_get_users(Error
**err
)
1567 #if (_WIN32_WINNT >= 0x0600)
1568 #define QGA_NANOSECONDS 10000000
1570 GHashTable
*cache
= NULL
;
1571 GuestUserList
*head
= NULL
, *cur_item
= NULL
;
1573 DWORD buffer_size
= 0, count
= 0, i
= 0;
1574 GA_WTSINFOA
*info
= NULL
;
1575 WTS_SESSION_INFOA
*entries
= NULL
;
1576 GuestUserList
*item
= NULL
;
1577 GuestUser
*user
= NULL
;
1578 gpointer value
= NULL
;
1580 double login_time
= 0;
1582 cache
= g_hash_table_new(g_str_hash
, g_str_equal
);
1584 if (WTSEnumerateSessionsA(NULL
, 0, 1, &entries
, &count
)) {
1585 for (i
= 0; i
< count
; ++i
) {
1588 if (WTSQuerySessionInformationA(
1590 entries
[i
].SessionId
,
1596 if (strlen(info
->UserName
) == 0) {
1597 WTSFreeMemory(info
);
1601 login
= info
->LogonTime
.QuadPart
;
1602 login
-= W32_FT_OFFSET
;
1603 login_time
= ((double)login
) / QGA_NANOSECONDS
;
1605 if (g_hash_table_contains(cache
, info
->UserName
)) {
1606 value
= g_hash_table_lookup(cache
, info
->UserName
);
1607 user
= (GuestUser
*)value
;
1608 if (user
->login_time
> login_time
) {
1609 user
->login_time
= login_time
;
1612 item
= g_new0(GuestUserList
, 1);
1613 item
->value
= g_new0(GuestUser
, 1);
1615 item
->value
->user
= g_strdup(info
->UserName
);
1616 item
->value
->domain
= g_strdup(info
->Domain
);
1617 item
->value
->has_domain
= true;
1619 item
->value
->login_time
= login_time
;
1621 g_hash_table_add(cache
, item
->value
->user
);
1624 head
= cur_item
= item
;
1626 cur_item
->next
= item
;
1631 WTSFreeMemory(info
);
1633 WTSFreeMemory(entries
);
1635 g_hash_table_destroy(cache
);
1638 error_setg(err
, QERR_UNSUPPORTED
);