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