virtio: force VIRTIO_F_IOMMU_PLATFORM
[qemu.git] / qga / commands-win32.c
blob19d72b2411e3434de565ba1ef3de9553bc3275f3
1 /*
2 * QEMU Guest Agent win32-specific command implementations
4 * Copyright IBM Corp. 2012
6 * Authors:
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.
14 #include "qemu/osdep.h"
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 #ifdef CONFIG_QGA_NTDDSCSI
22 #include <winioctl.h>
23 #include <ntddscsi.h>
24 #include <setupapi.h>
25 #include <initguid.h>
26 #endif
27 #include <lm.h>
29 #include "qga/guest-agent-core.h"
30 #include "qga/vss-win32.h"
31 #include "qga-qmp-commands.h"
32 #include "qapi/qmp/qerror.h"
33 #include "qemu/queue.h"
34 #include "qemu/host-utils.h"
35 #include "qemu/base64.h"
37 #ifndef SHTDN_REASON_FLAG_PLANNED
38 #define SHTDN_REASON_FLAG_PLANNED 0x80000000
39 #endif
41 /* multiple of 100 nanoseconds elapsed between windows baseline
42 * (1/1/1601) and Unix Epoch (1/1/1970), accounting for leap years */
43 #define W32_FT_OFFSET (10000000ULL * 60 * 60 * 24 * \
44 (365 * (1970 - 1601) + \
45 (1970 - 1601) / 4 - 3))
47 #define INVALID_SET_FILE_POINTER ((DWORD)-1)
49 typedef struct GuestFileHandle {
50 int64_t id;
51 HANDLE fh;
52 QTAILQ_ENTRY(GuestFileHandle) next;
53 } GuestFileHandle;
55 static struct {
56 QTAILQ_HEAD(, GuestFileHandle) filehandles;
57 } guest_file_state = {
58 .filehandles = QTAILQ_HEAD_INITIALIZER(guest_file_state.filehandles),
61 #define FILE_GENERIC_APPEND (FILE_GENERIC_WRITE & ~FILE_WRITE_DATA)
63 typedef struct OpenFlags {
64 const char *forms;
65 DWORD desired_access;
66 DWORD creation_disposition;
67 } OpenFlags;
68 static OpenFlags guest_file_open_modes[] = {
69 {"r", GENERIC_READ, OPEN_EXISTING},
70 {"rb", GENERIC_READ, OPEN_EXISTING},
71 {"w", GENERIC_WRITE, CREATE_ALWAYS},
72 {"wb", GENERIC_WRITE, CREATE_ALWAYS},
73 {"a", FILE_GENERIC_APPEND, OPEN_ALWAYS },
74 {"r+", GENERIC_WRITE|GENERIC_READ, OPEN_EXISTING},
75 {"rb+", GENERIC_WRITE|GENERIC_READ, OPEN_EXISTING},
76 {"r+b", GENERIC_WRITE|GENERIC_READ, OPEN_EXISTING},
77 {"w+", GENERIC_WRITE|GENERIC_READ, CREATE_ALWAYS},
78 {"wb+", GENERIC_WRITE|GENERIC_READ, CREATE_ALWAYS},
79 {"w+b", GENERIC_WRITE|GENERIC_READ, CREATE_ALWAYS},
80 {"a+", FILE_GENERIC_APPEND|GENERIC_READ, OPEN_ALWAYS },
81 {"ab+", FILE_GENERIC_APPEND|GENERIC_READ, OPEN_ALWAYS },
82 {"a+b", FILE_GENERIC_APPEND|GENERIC_READ, OPEN_ALWAYS }
85 static OpenFlags *find_open_flag(const char *mode_str)
87 int mode;
88 Error **errp = NULL;
90 for (mode = 0; mode < ARRAY_SIZE(guest_file_open_modes); ++mode) {
91 OpenFlags *flags = guest_file_open_modes + mode;
93 if (strcmp(flags->forms, mode_str) == 0) {
94 return flags;
98 error_setg(errp, "invalid file open mode '%s'", mode_str);
99 return NULL;
102 static int64_t guest_file_handle_add(HANDLE fh, Error **errp)
104 GuestFileHandle *gfh;
105 int64_t handle;
107 handle = ga_get_fd_handle(ga_state, errp);
108 if (handle < 0) {
109 return -1;
111 gfh = g_new0(GuestFileHandle, 1);
112 gfh->id = handle;
113 gfh->fh = fh;
114 QTAILQ_INSERT_TAIL(&guest_file_state.filehandles, gfh, next);
116 return handle;
119 static GuestFileHandle *guest_file_handle_find(int64_t id, Error **errp)
121 GuestFileHandle *gfh;
122 QTAILQ_FOREACH(gfh, &guest_file_state.filehandles, next) {
123 if (gfh->id == id) {
124 return gfh;
127 error_setg(errp, "handle '%" PRId64 "' has not been found", id);
128 return NULL;
131 static void handle_set_nonblocking(HANDLE fh)
133 DWORD file_type, pipe_state;
134 file_type = GetFileType(fh);
135 if (file_type != FILE_TYPE_PIPE) {
136 return;
138 /* If file_type == FILE_TYPE_PIPE, according to MSDN
139 * the specified file is socket or named pipe */
140 if (!GetNamedPipeHandleState(fh, &pipe_state, NULL,
141 NULL, NULL, NULL, 0)) {
142 return;
144 /* The fd is named pipe fd */
145 if (pipe_state & PIPE_NOWAIT) {
146 return;
149 pipe_state |= PIPE_NOWAIT;
150 SetNamedPipeHandleState(fh, &pipe_state, NULL, NULL);
153 int64_t qmp_guest_file_open(const char *path, bool has_mode,
154 const char *mode, Error **errp)
156 int64_t fd;
157 HANDLE fh;
158 HANDLE templ_file = NULL;
159 DWORD share_mode = FILE_SHARE_READ;
160 DWORD flags_and_attr = FILE_ATTRIBUTE_NORMAL;
161 LPSECURITY_ATTRIBUTES sa_attr = NULL;
162 OpenFlags *guest_flags;
164 if (!has_mode) {
165 mode = "r";
167 slog("guest-file-open called, filepath: %s, mode: %s", path, mode);
168 guest_flags = find_open_flag(mode);
169 if (guest_flags == NULL) {
170 error_setg(errp, "invalid file open mode");
171 return -1;
174 fh = CreateFile(path, guest_flags->desired_access, share_mode, sa_attr,
175 guest_flags->creation_disposition, flags_and_attr,
176 templ_file);
177 if (fh == INVALID_HANDLE_VALUE) {
178 error_setg_win32(errp, GetLastError(), "failed to open file '%s'",
179 path);
180 return -1;
183 /* set fd non-blocking to avoid common use cases (like reading from a
184 * named pipe) from hanging the agent
186 handle_set_nonblocking(fh);
188 fd = guest_file_handle_add(fh, errp);
189 if (fd < 0) {
190 CloseHandle(fh);
191 error_setg(errp, "failed to add handle to qmp handle table");
192 return -1;
195 slog("guest-file-open, handle: % " PRId64, fd);
196 return fd;
199 void qmp_guest_file_close(int64_t handle, Error **errp)
201 bool ret;
202 GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
203 slog("guest-file-close called, handle: %" PRId64, handle);
204 if (gfh == NULL) {
205 return;
207 ret = CloseHandle(gfh->fh);
208 if (!ret) {
209 error_setg_win32(errp, GetLastError(), "failed close handle");
210 return;
213 QTAILQ_REMOVE(&guest_file_state.filehandles, gfh, next);
214 g_free(gfh);
217 static void acquire_privilege(const char *name, Error **errp)
219 HANDLE token = NULL;
220 TOKEN_PRIVILEGES priv;
221 Error *local_err = NULL;
223 if (OpenProcessToken(GetCurrentProcess(),
224 TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &token))
226 if (!LookupPrivilegeValue(NULL, name, &priv.Privileges[0].Luid)) {
227 error_setg(&local_err, QERR_QGA_COMMAND_FAILED,
228 "no luid for requested privilege");
229 goto out;
232 priv.PrivilegeCount = 1;
233 priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
235 if (!AdjustTokenPrivileges(token, FALSE, &priv, 0, NULL, 0)) {
236 error_setg(&local_err, QERR_QGA_COMMAND_FAILED,
237 "unable to acquire requested privilege");
238 goto out;
241 } else {
242 error_setg(&local_err, QERR_QGA_COMMAND_FAILED,
243 "failed to open privilege token");
246 out:
247 if (token) {
248 CloseHandle(token);
250 error_propagate(errp, local_err);
253 static void execute_async(DWORD WINAPI (*func)(LPVOID), LPVOID opaque,
254 Error **errp)
256 Error *local_err = NULL;
258 HANDLE thread = CreateThread(NULL, 0, func, opaque, 0, NULL);
259 if (!thread) {
260 error_setg(&local_err, QERR_QGA_COMMAND_FAILED,
261 "failed to dispatch asynchronous command");
262 error_propagate(errp, local_err);
266 void qmp_guest_shutdown(bool has_mode, const char *mode, Error **errp)
268 Error *local_err = NULL;
269 UINT shutdown_flag = EWX_FORCE;
271 slog("guest-shutdown called, mode: %s", mode);
273 if (!has_mode || strcmp(mode, "powerdown") == 0) {
274 shutdown_flag |= EWX_POWEROFF;
275 } else if (strcmp(mode, "halt") == 0) {
276 shutdown_flag |= EWX_SHUTDOWN;
277 } else if (strcmp(mode, "reboot") == 0) {
278 shutdown_flag |= EWX_REBOOT;
279 } else {
280 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "mode",
281 "halt|powerdown|reboot");
282 return;
285 /* Request a shutdown privilege, but try to shut down the system
286 anyway. */
287 acquire_privilege(SE_SHUTDOWN_NAME, &local_err);
288 if (local_err) {
289 error_propagate(errp, local_err);
290 return;
293 if (!ExitWindowsEx(shutdown_flag, SHTDN_REASON_FLAG_PLANNED)) {
294 slog("guest-shutdown failed: %lu", GetLastError());
295 error_setg(errp, QERR_UNDEFINED_ERROR);
299 GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
300 int64_t count, Error **errp)
302 GuestFileRead *read_data = NULL;
303 guchar *buf;
304 HANDLE fh;
305 bool is_ok;
306 DWORD read_count;
307 GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
309 if (!gfh) {
310 return NULL;
312 if (!has_count) {
313 count = QGA_READ_COUNT_DEFAULT;
314 } else if (count < 0) {
315 error_setg(errp, "value '%" PRId64
316 "' is invalid for argument count", count);
317 return NULL;
320 fh = gfh->fh;
321 buf = g_malloc0(count+1);
322 is_ok = ReadFile(fh, buf, count, &read_count, NULL);
323 if (!is_ok) {
324 error_setg_win32(errp, GetLastError(), "failed to read file");
325 slog("guest-file-read failed, handle %" PRId64, handle);
326 } else {
327 buf[read_count] = 0;
328 read_data = g_new0(GuestFileRead, 1);
329 read_data->count = (size_t)read_count;
330 read_data->eof = read_count == 0;
332 if (read_count != 0) {
333 read_data->buf_b64 = g_base64_encode(buf, read_count);
336 g_free(buf);
338 return read_data;
341 GuestFileWrite *qmp_guest_file_write(int64_t handle, const char *buf_b64,
342 bool has_count, int64_t count,
343 Error **errp)
345 GuestFileWrite *write_data = NULL;
346 guchar *buf;
347 gsize buf_len;
348 bool is_ok;
349 DWORD write_count;
350 GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
351 HANDLE fh;
353 if (!gfh) {
354 return NULL;
356 fh = gfh->fh;
357 buf = qbase64_decode(buf_b64, -1, &buf_len, errp);
358 if (!buf) {
359 return NULL;
362 if (!has_count) {
363 count = buf_len;
364 } else if (count < 0 || count > buf_len) {
365 error_setg(errp, "value '%" PRId64
366 "' is invalid for argument count", count);
367 goto done;
370 is_ok = WriteFile(fh, buf, count, &write_count, NULL);
371 if (!is_ok) {
372 error_setg_win32(errp, GetLastError(), "failed to write to file");
373 slog("guest-file-write-failed, handle: %" PRId64, handle);
374 } else {
375 write_data = g_new0(GuestFileWrite, 1);
376 write_data->count = (size_t) write_count;
379 done:
380 g_free(buf);
381 return write_data;
384 GuestFileSeek *qmp_guest_file_seek(int64_t handle, int64_t offset,
385 GuestFileWhence *whence_code,
386 Error **errp)
388 GuestFileHandle *gfh;
389 GuestFileSeek *seek_data;
390 HANDLE fh;
391 LARGE_INTEGER new_pos, off_pos;
392 off_pos.QuadPart = offset;
393 BOOL res;
394 int whence;
395 Error *err = NULL;
397 gfh = guest_file_handle_find(handle, errp);
398 if (!gfh) {
399 return NULL;
402 /* We stupidly exposed 'whence':'int' in our qapi */
403 whence = ga_parse_whence(whence_code, &err);
404 if (err) {
405 error_propagate(errp, err);
406 return NULL;
409 fh = gfh->fh;
410 res = SetFilePointerEx(fh, off_pos, &new_pos, whence);
411 if (!res) {
412 error_setg_win32(errp, GetLastError(), "failed to seek file");
413 return NULL;
415 seek_data = g_new0(GuestFileSeek, 1);
416 seek_data->position = new_pos.QuadPart;
417 return seek_data;
420 void qmp_guest_file_flush(int64_t handle, Error **errp)
422 HANDLE fh;
423 GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
424 if (!gfh) {
425 return;
428 fh = gfh->fh;
429 if (!FlushFileBuffers(fh)) {
430 error_setg_win32(errp, GetLastError(), "failed to flush file");
434 #ifdef CONFIG_QGA_NTDDSCSI
436 static STORAGE_BUS_TYPE win2qemu[] = {
437 [BusTypeUnknown] = GUEST_DISK_BUS_TYPE_UNKNOWN,
438 [BusTypeScsi] = GUEST_DISK_BUS_TYPE_SCSI,
439 [BusTypeAtapi] = GUEST_DISK_BUS_TYPE_IDE,
440 [BusTypeAta] = GUEST_DISK_BUS_TYPE_IDE,
441 [BusType1394] = GUEST_DISK_BUS_TYPE_IEEE1394,
442 [BusTypeSsa] = GUEST_DISK_BUS_TYPE_SSA,
443 [BusTypeFibre] = GUEST_DISK_BUS_TYPE_SSA,
444 [BusTypeUsb] = GUEST_DISK_BUS_TYPE_USB,
445 [BusTypeRAID] = GUEST_DISK_BUS_TYPE_RAID,
446 #if (_WIN32_WINNT >= 0x0600)
447 [BusTypeiScsi] = GUEST_DISK_BUS_TYPE_ISCSI,
448 [BusTypeSas] = GUEST_DISK_BUS_TYPE_SAS,
449 [BusTypeSata] = GUEST_DISK_BUS_TYPE_SATA,
450 [BusTypeSd] = GUEST_DISK_BUS_TYPE_SD,
451 [BusTypeMmc] = GUEST_DISK_BUS_TYPE_MMC,
452 #endif
453 #if (_WIN32_WINNT >= 0x0601)
454 [BusTypeVirtual] = GUEST_DISK_BUS_TYPE_VIRTUAL,
455 [BusTypeFileBackedVirtual] = GUEST_DISK_BUS_TYPE_FILE_BACKED_VIRTUAL,
456 #endif
459 static GuestDiskBusType find_bus_type(STORAGE_BUS_TYPE bus)
461 if (bus > ARRAY_SIZE(win2qemu) || (int)bus < 0) {
462 return GUEST_DISK_BUS_TYPE_UNKNOWN;
464 return win2qemu[(int)bus];
467 DEFINE_GUID(GUID_DEVINTERFACE_VOLUME,
468 0x53f5630dL, 0xb6bf, 0x11d0, 0x94, 0xf2,
469 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
471 static GuestPCIAddress *get_pci_info(char *guid, Error **errp)
473 HDEVINFO dev_info;
474 SP_DEVINFO_DATA dev_info_data;
475 DWORD size = 0;
476 int i;
477 char dev_name[MAX_PATH];
478 char *buffer = NULL;
479 GuestPCIAddress *pci = NULL;
480 char *name = g_strdup(&guid[4]);
482 if (!QueryDosDevice(name, dev_name, ARRAY_SIZE(dev_name))) {
483 error_setg_win32(errp, GetLastError(), "failed to get dos device name");
484 goto out;
487 dev_info = SetupDiGetClassDevs(&GUID_DEVINTERFACE_VOLUME, 0, 0,
488 DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
489 if (dev_info == INVALID_HANDLE_VALUE) {
490 error_setg_win32(errp, GetLastError(), "failed to get devices tree");
491 goto out;
494 dev_info_data.cbSize = sizeof(SP_DEVINFO_DATA);
495 for (i = 0; SetupDiEnumDeviceInfo(dev_info, i, &dev_info_data); i++) {
496 DWORD addr, bus, slot, func, dev, data, size2;
497 while (!SetupDiGetDeviceRegistryProperty(dev_info, &dev_info_data,
498 SPDRP_PHYSICAL_DEVICE_OBJECT_NAME,
499 &data, (PBYTE)buffer, size,
500 &size2)) {
501 size = MAX(size, size2);
502 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
503 g_free(buffer);
504 /* Double the size to avoid problems on
505 * W2k MBCS systems per KB 888609.
506 * https://support.microsoft.com/en-us/kb/259695 */
507 buffer = g_malloc(size * 2);
508 } else {
509 error_setg_win32(errp, GetLastError(),
510 "failed to get device name");
511 goto out;
515 if (g_strcmp0(buffer, dev_name)) {
516 continue;
519 /* There is no need to allocate buffer in the next functions. The size
520 * is known and ULONG according to
521 * https://support.microsoft.com/en-us/kb/253232
522 * https://msdn.microsoft.com/en-us/library/windows/hardware/ff543095(v=vs.85).aspx
524 if (!SetupDiGetDeviceRegistryProperty(dev_info, &dev_info_data,
525 SPDRP_BUSNUMBER, &data, (PBYTE)&bus, size, NULL)) {
526 break;
529 /* The function retrieves the device's address. This value will be
530 * transformed into device function and number */
531 if (!SetupDiGetDeviceRegistryProperty(dev_info, &dev_info_data,
532 SPDRP_ADDRESS, &data, (PBYTE)&addr, size, NULL)) {
533 break;
536 /* This call returns UINumber of DEVICE_CAPABILITIES structure.
537 * This number is typically a user-perceived slot number. */
538 if (!SetupDiGetDeviceRegistryProperty(dev_info, &dev_info_data,
539 SPDRP_UI_NUMBER, &data, (PBYTE)&slot, size, NULL)) {
540 break;
543 /* SetupApi gives us the same information as driver with
544 * IoGetDeviceProperty. According to Microsoft
545 * https://support.microsoft.com/en-us/kb/253232
546 * FunctionNumber = (USHORT)((propertyAddress) & 0x0000FFFF);
547 * DeviceNumber = (USHORT)(((propertyAddress) >> 16) & 0x0000FFFF);
548 * SPDRP_ADDRESS is propertyAddress, so we do the same.*/
550 func = addr & 0x0000FFFF;
551 dev = (addr >> 16) & 0x0000FFFF;
552 pci = g_malloc0(sizeof(*pci));
553 pci->domain = dev;
554 pci->slot = slot;
555 pci->function = func;
556 pci->bus = bus;
557 break;
559 out:
560 g_free(buffer);
561 g_free(name);
562 return pci;
565 static int get_disk_bus_type(HANDLE vol_h, Error **errp)
567 STORAGE_PROPERTY_QUERY query;
568 STORAGE_DEVICE_DESCRIPTOR *dev_desc, buf;
569 DWORD received;
571 dev_desc = &buf;
572 dev_desc->Size = sizeof(buf);
573 query.PropertyId = StorageDeviceProperty;
574 query.QueryType = PropertyStandardQuery;
576 if (!DeviceIoControl(vol_h, IOCTL_STORAGE_QUERY_PROPERTY, &query,
577 sizeof(STORAGE_PROPERTY_QUERY), dev_desc,
578 dev_desc->Size, &received, NULL)) {
579 error_setg_win32(errp, GetLastError(), "failed to get bus type");
580 return -1;
583 return dev_desc->BusType;
586 /* VSS provider works with volumes, thus there is no difference if
587 * the volume consist of spanned disks. Info about the first disk in the
588 * volume is returned for the spanned disk group (LVM) */
589 static GuestDiskAddressList *build_guest_disk_info(char *guid, Error **errp)
591 GuestDiskAddressList *list = NULL;
592 GuestDiskAddress *disk;
593 SCSI_ADDRESS addr, *scsi_ad;
594 DWORD len;
595 int bus;
596 HANDLE vol_h;
598 scsi_ad = &addr;
599 char *name = g_strndup(guid, strlen(guid)-1);
601 vol_h = CreateFile(name, 0, FILE_SHARE_READ, NULL, OPEN_EXISTING,
602 0, NULL);
603 if (vol_h == INVALID_HANDLE_VALUE) {
604 error_setg_win32(errp, GetLastError(), "failed to open volume");
605 goto out_free;
608 bus = get_disk_bus_type(vol_h, errp);
609 if (bus < 0) {
610 goto out_close;
613 disk = g_malloc0(sizeof(*disk));
614 disk->bus_type = find_bus_type(bus);
615 if (bus == BusTypeScsi || bus == BusTypeAta || bus == BusTypeRAID
616 #if (_WIN32_WINNT >= 0x0600)
617 /* This bus type is not supported before Windows Server 2003 SP1 */
618 || bus == BusTypeSas
619 #endif
621 /* We are able to use the same ioctls for different bus types
622 * according to Microsoft docs
623 * https://technet.microsoft.com/en-us/library/ee851589(v=ws.10).aspx */
624 if (DeviceIoControl(vol_h, IOCTL_SCSI_GET_ADDRESS, NULL, 0, scsi_ad,
625 sizeof(SCSI_ADDRESS), &len, NULL)) {
626 disk->unit = addr.Lun;
627 disk->target = addr.TargetId;
628 disk->bus = addr.PathId;
629 disk->pci_controller = get_pci_info(name, errp);
631 /* We do not set error in this case, because we still have enough
632 * information about volume. */
633 } else {
634 disk->pci_controller = NULL;
637 list = g_malloc0(sizeof(*list));
638 list->value = disk;
639 list->next = NULL;
640 out_close:
641 CloseHandle(vol_h);
642 out_free:
643 g_free(name);
644 return list;
647 #else
649 static GuestDiskAddressList *build_guest_disk_info(char *guid, Error **errp)
651 return NULL;
654 #endif /* CONFIG_QGA_NTDDSCSI */
656 static GuestFilesystemInfo *build_guest_fsinfo(char *guid, Error **errp)
658 DWORD info_size;
659 char mnt, *mnt_point;
660 char fs_name[32];
661 char vol_info[MAX_PATH+1];
662 size_t len;
663 GuestFilesystemInfo *fs = NULL;
665 GetVolumePathNamesForVolumeName(guid, (LPCH)&mnt, 0, &info_size);
666 if (GetLastError() != ERROR_MORE_DATA) {
667 error_setg_win32(errp, GetLastError(), "failed to get volume name");
668 return NULL;
671 mnt_point = g_malloc(info_size + 1);
672 if (!GetVolumePathNamesForVolumeName(guid, mnt_point, info_size,
673 &info_size)) {
674 error_setg_win32(errp, GetLastError(), "failed to get volume name");
675 goto free;
678 len = strlen(mnt_point);
679 mnt_point[len] = '\\';
680 mnt_point[len+1] = 0;
681 if (!GetVolumeInformation(mnt_point, vol_info, sizeof(vol_info), NULL, NULL,
682 NULL, (LPSTR)&fs_name, sizeof(fs_name))) {
683 if (GetLastError() != ERROR_NOT_READY) {
684 error_setg_win32(errp, GetLastError(), "failed to get volume info");
686 goto free;
689 fs_name[sizeof(fs_name) - 1] = 0;
690 fs = g_malloc(sizeof(*fs));
691 fs->name = g_strdup(guid);
692 if (len == 0) {
693 fs->mountpoint = g_strdup("System Reserved");
694 } else {
695 fs->mountpoint = g_strndup(mnt_point, len);
697 fs->type = g_strdup(fs_name);
698 fs->disk = build_guest_disk_info(guid, errp);
699 free:
700 g_free(mnt_point);
701 return fs;
704 GuestFilesystemInfoList *qmp_guest_get_fsinfo(Error **errp)
706 HANDLE vol_h;
707 GuestFilesystemInfoList *new, *ret = NULL;
708 char guid[256];
710 vol_h = FindFirstVolume(guid, sizeof(guid));
711 if (vol_h == INVALID_HANDLE_VALUE) {
712 error_setg_win32(errp, GetLastError(), "failed to find any volume");
713 return NULL;
716 do {
717 GuestFilesystemInfo *info = build_guest_fsinfo(guid, errp);
718 if (info == NULL) {
719 continue;
721 new = g_malloc(sizeof(*ret));
722 new->value = info;
723 new->next = ret;
724 ret = new;
725 } while (FindNextVolume(vol_h, guid, sizeof(guid)));
727 if (GetLastError() != ERROR_NO_MORE_FILES) {
728 error_setg_win32(errp, GetLastError(), "failed to find next volume");
731 FindVolumeClose(vol_h);
732 return ret;
736 * Return status of freeze/thaw
738 GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **errp)
740 if (!vss_initialized()) {
741 error_setg(errp, QERR_UNSUPPORTED);
742 return 0;
745 if (ga_is_frozen(ga_state)) {
746 return GUEST_FSFREEZE_STATUS_FROZEN;
749 return GUEST_FSFREEZE_STATUS_THAWED;
753 * Freeze local file systems using Volume Shadow-copy Service.
754 * The frozen state is limited for up to 10 seconds by VSS.
756 int64_t qmp_guest_fsfreeze_freeze(Error **errp)
758 int i;
759 Error *local_err = NULL;
761 if (!vss_initialized()) {
762 error_setg(errp, QERR_UNSUPPORTED);
763 return 0;
766 slog("guest-fsfreeze called");
768 /* cannot risk guest agent blocking itself on a write in this state */
769 ga_set_frozen(ga_state);
771 qga_vss_fsfreeze(&i, &local_err, true);
772 if (local_err) {
773 error_propagate(errp, local_err);
774 goto error;
777 return i;
779 error:
780 local_err = NULL;
781 qmp_guest_fsfreeze_thaw(&local_err);
782 if (local_err) {
783 g_debug("cleanup thaw: %s", error_get_pretty(local_err));
784 error_free(local_err);
786 return 0;
789 int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints,
790 strList *mountpoints,
791 Error **errp)
793 error_setg(errp, QERR_UNSUPPORTED);
795 return 0;
799 * Thaw local file systems using Volume Shadow-copy Service.
801 int64_t qmp_guest_fsfreeze_thaw(Error **errp)
803 int i;
805 if (!vss_initialized()) {
806 error_setg(errp, QERR_UNSUPPORTED);
807 return 0;
810 qga_vss_fsfreeze(&i, errp, false);
812 ga_unset_frozen(ga_state);
813 return i;
816 static void guest_fsfreeze_cleanup(void)
818 Error *err = NULL;
820 if (!vss_initialized()) {
821 return;
824 if (ga_is_frozen(ga_state) == GUEST_FSFREEZE_STATUS_FROZEN) {
825 qmp_guest_fsfreeze_thaw(&err);
826 if (err) {
827 slog("failed to clean up frozen filesystems: %s",
828 error_get_pretty(err));
829 error_free(err);
833 vss_deinit(true);
837 * Walk list of mounted file systems in the guest, and discard unused
838 * areas.
840 GuestFilesystemTrimResponse *
841 qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **errp)
843 GuestFilesystemTrimResponse *resp;
844 HANDLE handle;
845 WCHAR guid[MAX_PATH] = L"";
847 handle = FindFirstVolumeW(guid, ARRAYSIZE(guid));
848 if (handle == INVALID_HANDLE_VALUE) {
849 error_setg_win32(errp, GetLastError(), "failed to find any volume");
850 return NULL;
853 resp = g_new0(GuestFilesystemTrimResponse, 1);
855 do {
856 GuestFilesystemTrimResult *res;
857 GuestFilesystemTrimResultList *list;
858 PWCHAR uc_path;
859 DWORD char_count = 0;
860 char *path, *out;
861 GError *gerr = NULL;
862 gchar * argv[4];
864 GetVolumePathNamesForVolumeNameW(guid, NULL, 0, &char_count);
866 if (GetLastError() != ERROR_MORE_DATA) {
867 continue;
869 if (GetDriveTypeW(guid) != DRIVE_FIXED) {
870 continue;
873 uc_path = g_malloc(sizeof(WCHAR) * char_count);
874 if (!GetVolumePathNamesForVolumeNameW(guid, uc_path, char_count,
875 &char_count) || !*uc_path) {
876 /* strange, but this condition could be faced even with size == 2 */
877 g_free(uc_path);
878 continue;
881 res = g_new0(GuestFilesystemTrimResult, 1);
883 path = g_utf16_to_utf8(uc_path, char_count, NULL, NULL, &gerr);
885 g_free(uc_path);
887 if (!path) {
888 res->has_error = true;
889 res->error = g_strdup(gerr->message);
890 g_error_free(gerr);
891 break;
894 res->path = path;
896 list = g_new0(GuestFilesystemTrimResultList, 1);
897 list->value = res;
898 list->next = resp->paths;
900 resp->paths = list;
902 memset(argv, 0, sizeof(argv));
903 argv[0] = (gchar *)"defrag.exe";
904 argv[1] = (gchar *)"/L";
905 argv[2] = path;
907 if (!g_spawn_sync(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
908 &out /* stdout */, NULL /* stdin */,
909 NULL, &gerr)) {
910 res->has_error = true;
911 res->error = g_strdup(gerr->message);
912 g_error_free(gerr);
913 } else {
914 /* defrag.exe is UGLY. Exit code is ALWAYS zero.
915 Error is reported in the output with something like
916 (x89000020) etc code in the stdout */
918 int i;
919 gchar **lines = g_strsplit(out, "\r\n", 0);
920 g_free(out);
922 for (i = 0; lines[i] != NULL; i++) {
923 if (g_strstr_len(lines[i], -1, "(0x") == NULL) {
924 continue;
926 res->has_error = true;
927 res->error = g_strdup(lines[i]);
928 break;
930 g_strfreev(lines);
932 } while (FindNextVolumeW(handle, guid, ARRAYSIZE(guid)));
934 FindVolumeClose(handle);
935 return resp;
938 typedef enum {
939 GUEST_SUSPEND_MODE_DISK,
940 GUEST_SUSPEND_MODE_RAM
941 } GuestSuspendMode;
943 static void check_suspend_mode(GuestSuspendMode mode, Error **errp)
945 SYSTEM_POWER_CAPABILITIES sys_pwr_caps;
946 Error *local_err = NULL;
948 ZeroMemory(&sys_pwr_caps, sizeof(sys_pwr_caps));
949 if (!GetPwrCapabilities(&sys_pwr_caps)) {
950 error_setg(&local_err, QERR_QGA_COMMAND_FAILED,
951 "failed to determine guest suspend capabilities");
952 goto out;
955 switch (mode) {
956 case GUEST_SUSPEND_MODE_DISK:
957 if (!sys_pwr_caps.SystemS4) {
958 error_setg(&local_err, QERR_QGA_COMMAND_FAILED,
959 "suspend-to-disk not supported by OS");
961 break;
962 case GUEST_SUSPEND_MODE_RAM:
963 if (!sys_pwr_caps.SystemS3) {
964 error_setg(&local_err, QERR_QGA_COMMAND_FAILED,
965 "suspend-to-ram not supported by OS");
967 break;
968 default:
969 error_setg(&local_err, QERR_INVALID_PARAMETER_VALUE, "mode",
970 "GuestSuspendMode");
973 out:
974 error_propagate(errp, local_err);
977 static DWORD WINAPI do_suspend(LPVOID opaque)
979 GuestSuspendMode *mode = opaque;
980 DWORD ret = 0;
982 if (!SetSuspendState(*mode == GUEST_SUSPEND_MODE_DISK, TRUE, TRUE)) {
983 slog("failed to suspend guest, %lu", GetLastError());
984 ret = -1;
986 g_free(mode);
987 return ret;
990 void qmp_guest_suspend_disk(Error **errp)
992 Error *local_err = NULL;
993 GuestSuspendMode *mode = g_new(GuestSuspendMode, 1);
995 *mode = GUEST_SUSPEND_MODE_DISK;
996 check_suspend_mode(*mode, &local_err);
997 acquire_privilege(SE_SHUTDOWN_NAME, &local_err);
998 execute_async(do_suspend, mode, &local_err);
1000 if (local_err) {
1001 error_propagate(errp, local_err);
1002 g_free(mode);
1006 void qmp_guest_suspend_ram(Error **errp)
1008 Error *local_err = NULL;
1009 GuestSuspendMode *mode = g_new(GuestSuspendMode, 1);
1011 *mode = GUEST_SUSPEND_MODE_RAM;
1012 check_suspend_mode(*mode, &local_err);
1013 acquire_privilege(SE_SHUTDOWN_NAME, &local_err);
1014 execute_async(do_suspend, mode, &local_err);
1016 if (local_err) {
1017 error_propagate(errp, local_err);
1018 g_free(mode);
1022 void qmp_guest_suspend_hybrid(Error **errp)
1024 error_setg(errp, QERR_UNSUPPORTED);
1027 static IP_ADAPTER_ADDRESSES *guest_get_adapters_addresses(Error **errp)
1029 IP_ADAPTER_ADDRESSES *adptr_addrs = NULL;
1030 ULONG adptr_addrs_len = 0;
1031 DWORD ret;
1033 /* Call the first time to get the adptr_addrs_len. */
1034 GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX,
1035 NULL, adptr_addrs, &adptr_addrs_len);
1037 adptr_addrs = g_malloc(adptr_addrs_len);
1038 ret = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX,
1039 NULL, adptr_addrs, &adptr_addrs_len);
1040 if (ret != ERROR_SUCCESS) {
1041 error_setg_win32(errp, ret, "failed to get adapters addresses");
1042 g_free(adptr_addrs);
1043 adptr_addrs = NULL;
1045 return adptr_addrs;
1048 static char *guest_wctomb_dup(WCHAR *wstr)
1050 char *str;
1051 size_t i;
1053 i = wcslen(wstr) + 1;
1054 str = g_malloc(i);
1055 WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK,
1056 wstr, -1, str, i, NULL, NULL);
1057 return str;
1060 static char *guest_addr_to_str(IP_ADAPTER_UNICAST_ADDRESS *ip_addr,
1061 Error **errp)
1063 char addr_str[INET6_ADDRSTRLEN + INET_ADDRSTRLEN];
1064 DWORD len;
1065 int ret;
1067 if (ip_addr->Address.lpSockaddr->sa_family == AF_INET ||
1068 ip_addr->Address.lpSockaddr->sa_family == AF_INET6) {
1069 len = sizeof(addr_str);
1070 ret = WSAAddressToString(ip_addr->Address.lpSockaddr,
1071 ip_addr->Address.iSockaddrLength,
1072 NULL,
1073 addr_str,
1074 &len);
1075 if (ret != 0) {
1076 error_setg_win32(errp, WSAGetLastError(),
1077 "failed address presentation form conversion");
1078 return NULL;
1080 return g_strdup(addr_str);
1082 return NULL;
1085 #if (_WIN32_WINNT >= 0x0600)
1086 static int64_t guest_ip_prefix(IP_ADAPTER_UNICAST_ADDRESS *ip_addr)
1088 /* For Windows Vista/2008 and newer, use the OnLinkPrefixLength
1089 * field to obtain the prefix.
1091 return ip_addr->OnLinkPrefixLength;
1093 #else
1094 /* When using the Windows XP and 2003 build environment, do the best we can to
1095 * figure out the prefix.
1097 static IP_ADAPTER_INFO *guest_get_adapters_info(void)
1099 IP_ADAPTER_INFO *adptr_info = NULL;
1100 ULONG adptr_info_len = 0;
1101 DWORD ret;
1103 /* Call the first time to get the adptr_info_len. */
1104 GetAdaptersInfo(adptr_info, &adptr_info_len);
1106 adptr_info = g_malloc(adptr_info_len);
1107 ret = GetAdaptersInfo(adptr_info, &adptr_info_len);
1108 if (ret != ERROR_SUCCESS) {
1109 g_free(adptr_info);
1110 adptr_info = NULL;
1112 return adptr_info;
1115 static int64_t guest_ip_prefix(IP_ADAPTER_UNICAST_ADDRESS *ip_addr)
1117 int64_t prefix = -1; /* Use for AF_INET6 and unknown/undetermined values. */
1118 IP_ADAPTER_INFO *adptr_info, *info;
1119 IP_ADDR_STRING *ip;
1120 struct in_addr *p;
1122 if (ip_addr->Address.lpSockaddr->sa_family != AF_INET) {
1123 return prefix;
1125 adptr_info = guest_get_adapters_info();
1126 if (adptr_info == NULL) {
1127 return prefix;
1130 /* Match up the passed in ip_addr with one found in adaptr_info.
1131 * The matching one in adptr_info will have the netmask.
1133 p = &((struct sockaddr_in *)ip_addr->Address.lpSockaddr)->sin_addr;
1134 for (info = adptr_info; info; info = info->Next) {
1135 for (ip = &info->IpAddressList; ip; ip = ip->Next) {
1136 if (p->S_un.S_addr == inet_addr(ip->IpAddress.String)) {
1137 prefix = ctpop32(inet_addr(ip->IpMask.String));
1138 goto out;
1142 out:
1143 g_free(adptr_info);
1144 return prefix;
1146 #endif
1148 GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
1150 IP_ADAPTER_ADDRESSES *adptr_addrs, *addr;
1151 IP_ADAPTER_UNICAST_ADDRESS *ip_addr = NULL;
1152 GuestNetworkInterfaceList *head = NULL, *cur_item = NULL;
1153 GuestIpAddressList *head_addr, *cur_addr;
1154 GuestNetworkInterfaceList *info;
1155 GuestIpAddressList *address_item = NULL;
1156 unsigned char *mac_addr;
1157 char *addr_str;
1158 WORD wsa_version;
1159 WSADATA wsa_data;
1160 int ret;
1162 adptr_addrs = guest_get_adapters_addresses(errp);
1163 if (adptr_addrs == NULL) {
1164 return NULL;
1167 /* Make WSA APIs available. */
1168 wsa_version = MAKEWORD(2, 2);
1169 ret = WSAStartup(wsa_version, &wsa_data);
1170 if (ret != 0) {
1171 error_setg_win32(errp, ret, "failed socket startup");
1172 goto out;
1175 for (addr = adptr_addrs; addr; addr = addr->Next) {
1176 info = g_malloc0(sizeof(*info));
1178 if (cur_item == NULL) {
1179 head = cur_item = info;
1180 } else {
1181 cur_item->next = info;
1182 cur_item = info;
1185 info->value = g_malloc0(sizeof(*info->value));
1186 info->value->name = guest_wctomb_dup(addr->FriendlyName);
1188 if (addr->PhysicalAddressLength != 0) {
1189 mac_addr = addr->PhysicalAddress;
1191 info->value->hardware_address =
1192 g_strdup_printf("%02x:%02x:%02x:%02x:%02x:%02x",
1193 (int) mac_addr[0], (int) mac_addr[1],
1194 (int) mac_addr[2], (int) mac_addr[3],
1195 (int) mac_addr[4], (int) mac_addr[5]);
1197 info->value->has_hardware_address = true;
1200 head_addr = NULL;
1201 cur_addr = NULL;
1202 for (ip_addr = addr->FirstUnicastAddress;
1203 ip_addr;
1204 ip_addr = ip_addr->Next) {
1205 addr_str = guest_addr_to_str(ip_addr, errp);
1206 if (addr_str == NULL) {
1207 continue;
1210 address_item = g_malloc0(sizeof(*address_item));
1212 if (!cur_addr) {
1213 head_addr = cur_addr = address_item;
1214 } else {
1215 cur_addr->next = address_item;
1216 cur_addr = address_item;
1219 address_item->value = g_malloc0(sizeof(*address_item->value));
1220 address_item->value->ip_address = addr_str;
1221 address_item->value->prefix = guest_ip_prefix(ip_addr);
1222 if (ip_addr->Address.lpSockaddr->sa_family == AF_INET) {
1223 address_item->value->ip_address_type =
1224 GUEST_IP_ADDRESS_TYPE_IPV4;
1225 } else if (ip_addr->Address.lpSockaddr->sa_family == AF_INET6) {
1226 address_item->value->ip_address_type =
1227 GUEST_IP_ADDRESS_TYPE_IPV6;
1230 if (head_addr) {
1231 info->value->has_ip_addresses = true;
1232 info->value->ip_addresses = head_addr;
1235 WSACleanup();
1236 out:
1237 g_free(adptr_addrs);
1238 return head;
1241 int64_t qmp_guest_get_time(Error **errp)
1243 SYSTEMTIME ts = {0};
1244 FILETIME tf;
1246 GetSystemTime(&ts);
1247 if (ts.wYear < 1601 || ts.wYear > 30827) {
1248 error_setg(errp, "Failed to get time");
1249 return -1;
1252 if (!SystemTimeToFileTime(&ts, &tf)) {
1253 error_setg(errp, "Failed to convert system time: %d", (int)GetLastError());
1254 return -1;
1257 return ((((int64_t)tf.dwHighDateTime << 32) | tf.dwLowDateTime)
1258 - W32_FT_OFFSET) * 100;
1261 void qmp_guest_set_time(bool has_time, int64_t time_ns, Error **errp)
1263 Error *local_err = NULL;
1264 SYSTEMTIME ts;
1265 FILETIME tf;
1266 LONGLONG time;
1268 if (!has_time) {
1269 /* Unfortunately, Windows libraries don't provide an easy way to access
1270 * RTC yet:
1272 * https://msdn.microsoft.com/en-us/library/aa908981.aspx
1274 error_setg(errp, "Time argument is required on this platform");
1275 return;
1278 /* Validate time passed by user. */
1279 if (time_ns < 0 || time_ns / 100 > INT64_MAX - W32_FT_OFFSET) {
1280 error_setg(errp, "Time %" PRId64 "is invalid", time_ns);
1281 return;
1284 time = time_ns / 100 + W32_FT_OFFSET;
1286 tf.dwLowDateTime = (DWORD) time;
1287 tf.dwHighDateTime = (DWORD) (time >> 32);
1289 if (!FileTimeToSystemTime(&tf, &ts)) {
1290 error_setg(errp, "Failed to convert system time %d",
1291 (int)GetLastError());
1292 return;
1295 acquire_privilege(SE_SYSTEMTIME_NAME, &local_err);
1296 if (local_err) {
1297 error_propagate(errp, local_err);
1298 return;
1301 if (!SetSystemTime(&ts)) {
1302 error_setg(errp, "Failed to set time to guest: %d", (int)GetLastError());
1303 return;
1307 GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp)
1309 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION pslpi, ptr;
1310 DWORD length;
1311 GuestLogicalProcessorList *head, **link;
1312 Error *local_err = NULL;
1313 int64_t current;
1315 ptr = pslpi = NULL;
1316 length = 0;
1317 current = 0;
1318 head = NULL;
1319 link = &head;
1321 if ((GetLogicalProcessorInformation(pslpi, &length) == FALSE) &&
1322 (GetLastError() == ERROR_INSUFFICIENT_BUFFER) &&
1323 (length > sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION))) {
1324 ptr = pslpi = g_malloc0(length);
1325 if (GetLogicalProcessorInformation(pslpi, &length) == FALSE) {
1326 error_setg(&local_err, "Failed to get processor information: %d",
1327 (int)GetLastError());
1329 } else {
1330 error_setg(&local_err,
1331 "Failed to get processor information buffer length: %d",
1332 (int)GetLastError());
1335 while ((local_err == NULL) && (length > 0)) {
1336 if (pslpi->Relationship == RelationProcessorCore) {
1337 ULONG_PTR cpu_bits = pslpi->ProcessorMask;
1339 while (cpu_bits > 0) {
1340 if (!!(cpu_bits & 1)) {
1341 GuestLogicalProcessor *vcpu;
1342 GuestLogicalProcessorList *entry;
1344 vcpu = g_malloc0(sizeof *vcpu);
1345 vcpu->logical_id = current++;
1346 vcpu->online = true;
1347 vcpu->has_can_offline = false;
1349 entry = g_malloc0(sizeof *entry);
1350 entry->value = vcpu;
1352 *link = entry;
1353 link = &entry->next;
1355 cpu_bits >>= 1;
1358 length -= sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
1359 pslpi++; /* next entry */
1362 g_free(ptr);
1364 if (local_err == NULL) {
1365 if (head != NULL) {
1366 return head;
1368 /* there's no guest with zero VCPUs */
1369 error_setg(&local_err, "Guest reported zero VCPUs");
1372 qapi_free_GuestLogicalProcessorList(head);
1373 error_propagate(errp, local_err);
1374 return NULL;
1377 int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp)
1379 error_setg(errp, QERR_UNSUPPORTED);
1380 return -1;
1383 static gchar *
1384 get_net_error_message(gint error)
1386 HMODULE module = NULL;
1387 gchar *retval = NULL;
1388 wchar_t *msg = NULL;
1389 int flags;
1390 size_t nchars;
1392 flags = FORMAT_MESSAGE_ALLOCATE_BUFFER |
1393 FORMAT_MESSAGE_IGNORE_INSERTS |
1394 FORMAT_MESSAGE_FROM_SYSTEM;
1396 if (error >= NERR_BASE && error <= MAX_NERR) {
1397 module = LoadLibraryExW(L"netmsg.dll", NULL, LOAD_LIBRARY_AS_DATAFILE);
1399 if (module != NULL) {
1400 flags |= FORMAT_MESSAGE_FROM_HMODULE;
1404 FormatMessageW(flags, module, error, 0, (LPWSTR)&msg, 0, NULL);
1406 if (msg != NULL) {
1407 nchars = wcslen(msg);
1409 if (nchars >= 2 &&
1410 msg[nchars - 1] == L'\n' &&
1411 msg[nchars - 2] == L'\r') {
1412 msg[nchars - 2] = L'\0';
1415 retval = g_utf16_to_utf8(msg, -1, NULL, NULL, NULL);
1417 LocalFree(msg);
1420 if (module != NULL) {
1421 FreeLibrary(module);
1424 return retval;
1427 void qmp_guest_set_user_password(const char *username,
1428 const char *password,
1429 bool crypted,
1430 Error **errp)
1432 NET_API_STATUS nas;
1433 char *rawpasswddata = NULL;
1434 size_t rawpasswdlen;
1435 wchar_t *user = NULL, *wpass = NULL;
1436 USER_INFO_1003 pi1003 = { 0, };
1437 GError *gerr = NULL;
1439 if (crypted) {
1440 error_setg(errp, QERR_UNSUPPORTED);
1441 return;
1444 rawpasswddata = (char *)qbase64_decode(password, -1, &rawpasswdlen, errp);
1445 if (!rawpasswddata) {
1446 return;
1448 rawpasswddata = g_renew(char, rawpasswddata, rawpasswdlen + 1);
1449 rawpasswddata[rawpasswdlen] = '\0';
1451 user = g_utf8_to_utf16(username, -1, NULL, NULL, &gerr);
1452 if (!user) {
1453 goto done;
1456 wpass = g_utf8_to_utf16(rawpasswddata, -1, NULL, NULL, &gerr);
1457 if (!wpass) {
1458 goto done;
1461 pi1003.usri1003_password = wpass;
1462 nas = NetUserSetInfo(NULL, user,
1463 1003, (LPBYTE)&pi1003,
1464 NULL);
1466 if (nas != NERR_Success) {
1467 gchar *msg = get_net_error_message(nas);
1468 error_setg(errp, "failed to set password: %s", msg);
1469 g_free(msg);
1472 done:
1473 if (gerr) {
1474 error_setg(errp, QERR_QGA_COMMAND_FAILED, gerr->message);
1475 g_error_free(gerr);
1477 g_free(user);
1478 g_free(wpass);
1479 g_free(rawpasswddata);
1482 GuestMemoryBlockList *qmp_guest_get_memory_blocks(Error **errp)
1484 error_setg(errp, QERR_UNSUPPORTED);
1485 return NULL;
1488 GuestMemoryBlockResponseList *
1489 qmp_guest_set_memory_blocks(GuestMemoryBlockList *mem_blks, Error **errp)
1491 error_setg(errp, QERR_UNSUPPORTED);
1492 return NULL;
1495 GuestMemoryBlockInfo *qmp_guest_get_memory_block_info(Error **errp)
1497 error_setg(errp, QERR_UNSUPPORTED);
1498 return NULL;
1501 /* add unsupported commands to the blacklist */
1502 GList *ga_command_blacklist_init(GList *blacklist)
1504 const char *list_unsupported[] = {
1505 "guest-suspend-hybrid",
1506 "guest-set-vcpus",
1507 "guest-get-memory-blocks", "guest-set-memory-blocks",
1508 "guest-get-memory-block-size",
1509 "guest-fsfreeze-freeze-list",
1510 NULL};
1511 char **p = (char **)list_unsupported;
1513 while (*p) {
1514 blacklist = g_list_append(blacklist, g_strdup(*p++));
1517 if (!vss_init(true)) {
1518 g_debug("vss_init failed, vss commands are going to be disabled");
1519 const char *list[] = {
1520 "guest-get-fsinfo", "guest-fsfreeze-status",
1521 "guest-fsfreeze-freeze", "guest-fsfreeze-thaw", NULL};
1522 p = (char **)list;
1524 while (*p) {
1525 blacklist = g_list_append(blacklist, g_strdup(*p++));
1529 return blacklist;
1532 /* register init/cleanup routines for stateful command groups */
1533 void ga_command_state_init(GAState *s, GACommandState *cs)
1535 if (!vss_initialized()) {
1536 ga_command_state_add(cs, NULL, guest_fsfreeze_cleanup);