]>
Commit | Line | Data |
---|---|---|
d8ca685a MR |
1 | /* |
2 | * QEMU Guest Agent win32-specific command implementations | |
3 | * | |
4 | * Copyright IBM Corp. 2012 | |
5 | * | |
6 | * Authors: | |
7 | * Michael Roth <[email protected]> | |
aa59637e | 8 | * Gal Hammer <[email protected]> |
d8ca685a MR |
9 | * |
10 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
11 | * See the COPYING file in the top-level directory. | |
12 | */ | |
13 | ||
161a56a9 VF |
14 | #ifndef _WIN32_WINNT |
15 | # define _WIN32_WINNT 0x0600 | |
16 | #endif | |
e688df6b | 17 | |
4459bf38 | 18 | #include "qemu/osdep.h" |
aa59637e GH |
19 | #include <wtypes.h> |
20 | #include <powrprof.h> | |
d6c5528b KA |
21 | #include <winsock2.h> |
22 | #include <ws2tcpip.h> | |
23 | #include <iptypes.h> | |
24 | #include <iphlpapi.h> | |
a3ef3b22 OK |
25 | #ifdef CONFIG_QGA_NTDDSCSI |
26 | #include <winioctl.h> | |
27 | #include <ntddscsi.h> | |
c54e1eb4 MR |
28 | #include <setupapi.h> |
29 | #include <initguid.h> | |
a3ef3b22 | 30 | #endif |
259434b8 | 31 | #include <lm.h> |
161a56a9 | 32 | #include <wtsapi32.h> |
105fad6b | 33 | #include <wininet.h> |
259434b8 | 34 | |
dc03272d MT |
35 | #include "guest-agent-core.h" |
36 | #include "vss-win32.h" | |
eb815e24 | 37 | #include "qga-qapi-commands.h" |
e688df6b | 38 | #include "qapi/error.h" |
7b1b5d19 | 39 | #include "qapi/qmp/qerror.h" |
fa193594 | 40 | #include "qemu/queue.h" |
d6c5528b | 41 | #include "qemu/host-utils.h" |
920639ca | 42 | #include "qemu/base64.h" |
d8ca685a | 43 | |
546b60d0 MR |
44 | #ifndef SHTDN_REASON_FLAG_PLANNED |
45 | #define SHTDN_REASON_FLAG_PLANNED 0x80000000 | |
46 | #endif | |
47 | ||
3f2a6087 LL |
48 | /* multiple of 100 nanoseconds elapsed between windows baseline |
49 | * (1/1/1601) and Unix Epoch (1/1/1970), accounting for leap years */ | |
50 | #define W32_FT_OFFSET (10000000ULL * 60 * 60 * 24 * \ | |
51 | (365 * (1970 - 1601) + \ | |
52 | (1970 - 1601) / 4 - 3)) | |
53 | ||
fa193594 OK |
54 | #define INVALID_SET_FILE_POINTER ((DWORD)-1) |
55 | ||
56 | typedef struct GuestFileHandle { | |
57 | int64_t id; | |
58 | HANDLE fh; | |
59 | QTAILQ_ENTRY(GuestFileHandle) next; | |
60 | } GuestFileHandle; | |
61 | ||
62 | static struct { | |
63 | QTAILQ_HEAD(, GuestFileHandle) filehandles; | |
b4fe97c8 DL |
64 | } guest_file_state = { |
65 | .filehandles = QTAILQ_HEAD_INITIALIZER(guest_file_state.filehandles), | |
66 | }; | |
fa193594 | 67 | |
52074d0f | 68 | #define FILE_GENERIC_APPEND (FILE_GENERIC_WRITE & ~FILE_WRITE_DATA) |
fa193594 OK |
69 | |
70 | typedef struct OpenFlags { | |
71 | const char *forms; | |
72 | DWORD desired_access; | |
73 | DWORD creation_disposition; | |
74 | } OpenFlags; | |
75 | static OpenFlags guest_file_open_modes[] = { | |
52074d0f KA |
76 | {"r", GENERIC_READ, OPEN_EXISTING}, |
77 | {"rb", GENERIC_READ, OPEN_EXISTING}, | |
78 | {"w", GENERIC_WRITE, CREATE_ALWAYS}, | |
79 | {"wb", GENERIC_WRITE, CREATE_ALWAYS}, | |
80 | {"a", FILE_GENERIC_APPEND, OPEN_ALWAYS }, | |
81 | {"r+", GENERIC_WRITE|GENERIC_READ, OPEN_EXISTING}, | |
82 | {"rb+", GENERIC_WRITE|GENERIC_READ, OPEN_EXISTING}, | |
83 | {"r+b", GENERIC_WRITE|GENERIC_READ, OPEN_EXISTING}, | |
84 | {"w+", GENERIC_WRITE|GENERIC_READ, CREATE_ALWAYS}, | |
85 | {"wb+", GENERIC_WRITE|GENERIC_READ, CREATE_ALWAYS}, | |
86 | {"w+b", GENERIC_WRITE|GENERIC_READ, CREATE_ALWAYS}, | |
87 | {"a+", FILE_GENERIC_APPEND|GENERIC_READ, OPEN_ALWAYS }, | |
88 | {"ab+", FILE_GENERIC_APPEND|GENERIC_READ, OPEN_ALWAYS }, | |
89 | {"a+b", FILE_GENERIC_APPEND|GENERIC_READ, OPEN_ALWAYS } | |
fa193594 OK |
90 | }; |
91 | ||
92 | static OpenFlags *find_open_flag(const char *mode_str) | |
93 | { | |
94 | int mode; | |
95 | Error **errp = NULL; | |
96 | ||
97 | for (mode = 0; mode < ARRAY_SIZE(guest_file_open_modes); ++mode) { | |
98 | OpenFlags *flags = guest_file_open_modes + mode; | |
99 | ||
100 | if (strcmp(flags->forms, mode_str) == 0) { | |
101 | return flags; | |
102 | } | |
103 | } | |
104 | ||
105 | error_setg(errp, "invalid file open mode '%s'", mode_str); | |
106 | return NULL; | |
107 | } | |
108 | ||
109 | static int64_t guest_file_handle_add(HANDLE fh, Error **errp) | |
110 | { | |
111 | GuestFileHandle *gfh; | |
112 | int64_t handle; | |
113 | ||
114 | handle = ga_get_fd_handle(ga_state, errp); | |
115 | if (handle < 0) { | |
116 | return -1; | |
117 | } | |
f3a06403 | 118 | gfh = g_new0(GuestFileHandle, 1); |
fa193594 OK |
119 | gfh->id = handle; |
120 | gfh->fh = fh; | |
121 | QTAILQ_INSERT_TAIL(&guest_file_state.filehandles, gfh, next); | |
122 | ||
123 | return handle; | |
124 | } | |
125 | ||
126 | static GuestFileHandle *guest_file_handle_find(int64_t id, Error **errp) | |
127 | { | |
128 | GuestFileHandle *gfh; | |
129 | QTAILQ_FOREACH(gfh, &guest_file_state.filehandles, next) { | |
130 | if (gfh->id == id) { | |
131 | return gfh; | |
132 | } | |
133 | } | |
134 | error_setg(errp, "handle '%" PRId64 "' has not been found", id); | |
135 | return NULL; | |
136 | } | |
137 | ||
fb687773 OK |
138 | static void handle_set_nonblocking(HANDLE fh) |
139 | { | |
140 | DWORD file_type, pipe_state; | |
141 | file_type = GetFileType(fh); | |
142 | if (file_type != FILE_TYPE_PIPE) { | |
143 | return; | |
144 | } | |
145 | /* If file_type == FILE_TYPE_PIPE, according to MSDN | |
146 | * the specified file is socket or named pipe */ | |
147 | if (!GetNamedPipeHandleState(fh, &pipe_state, NULL, | |
148 | NULL, NULL, NULL, 0)) { | |
149 | return; | |
150 | } | |
151 | /* The fd is named pipe fd */ | |
152 | if (pipe_state & PIPE_NOWAIT) { | |
153 | return; | |
154 | } | |
155 | ||
156 | pipe_state |= PIPE_NOWAIT; | |
157 | SetNamedPipeHandleState(fh, &pipe_state, NULL, NULL); | |
158 | } | |
159 | ||
fa193594 OK |
160 | int64_t qmp_guest_file_open(const char *path, bool has_mode, |
161 | const char *mode, Error **errp) | |
162 | { | |
163 | int64_t fd; | |
164 | HANDLE fh; | |
165 | HANDLE templ_file = NULL; | |
166 | DWORD share_mode = FILE_SHARE_READ; | |
167 | DWORD flags_and_attr = FILE_ATTRIBUTE_NORMAL; | |
168 | LPSECURITY_ATTRIBUTES sa_attr = NULL; | |
169 | OpenFlags *guest_flags; | |
170 | ||
171 | if (!has_mode) { | |
172 | mode = "r"; | |
173 | } | |
174 | slog("guest-file-open called, filepath: %s, mode: %s", path, mode); | |
175 | guest_flags = find_open_flag(mode); | |
176 | if (guest_flags == NULL) { | |
177 | error_setg(errp, "invalid file open mode"); | |
178 | return -1; | |
179 | } | |
180 | ||
181 | fh = CreateFile(path, guest_flags->desired_access, share_mode, sa_attr, | |
182 | guest_flags->creation_disposition, flags_and_attr, | |
183 | templ_file); | |
184 | if (fh == INVALID_HANDLE_VALUE) { | |
185 | error_setg_win32(errp, GetLastError(), "failed to open file '%s'", | |
186 | path); | |
187 | return -1; | |
188 | } | |
189 | ||
fb687773 OK |
190 | /* set fd non-blocking to avoid common use cases (like reading from a |
191 | * named pipe) from hanging the agent | |
192 | */ | |
193 | handle_set_nonblocking(fh); | |
194 | ||
fa193594 OK |
195 | fd = guest_file_handle_add(fh, errp); |
196 | if (fd < 0) { | |
c87d0964 | 197 | CloseHandle(fh); |
fa193594 OK |
198 | error_setg(errp, "failed to add handle to qmp handle table"); |
199 | return -1; | |
200 | } | |
201 | ||
202 | slog("guest-file-open, handle: % " PRId64, fd); | |
203 | return fd; | |
204 | } | |
205 | ||
206 | void qmp_guest_file_close(int64_t handle, Error **errp) | |
207 | { | |
208 | bool ret; | |
209 | GuestFileHandle *gfh = guest_file_handle_find(handle, errp); | |
210 | slog("guest-file-close called, handle: %" PRId64, handle); | |
211 | if (gfh == NULL) { | |
212 | return; | |
213 | } | |
214 | ret = CloseHandle(gfh->fh); | |
215 | if (!ret) { | |
216 | error_setg_win32(errp, GetLastError(), "failed close handle"); | |
217 | return; | |
218 | } | |
219 | ||
220 | QTAILQ_REMOVE(&guest_file_state.filehandles, gfh, next); | |
221 | g_free(gfh); | |
222 | } | |
223 | ||
77dbc81b | 224 | static void acquire_privilege(const char *name, Error **errp) |
d8ca685a | 225 | { |
374044f0 | 226 | HANDLE token = NULL; |
546b60d0 | 227 | TOKEN_PRIVILEGES priv; |
aa59637e GH |
228 | Error *local_err = NULL; |
229 | ||
aa59637e GH |
230 | if (OpenProcessToken(GetCurrentProcess(), |
231 | TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &token)) | |
232 | { | |
233 | if (!LookupPrivilegeValue(NULL, name, &priv.Privileges[0].Luid)) { | |
c6bd8c70 MA |
234 | error_setg(&local_err, QERR_QGA_COMMAND_FAILED, |
235 | "no luid for requested privilege"); | |
aa59637e GH |
236 | goto out; |
237 | } | |
238 | ||
239 | priv.PrivilegeCount = 1; | |
240 | priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; | |
241 | ||
242 | if (!AdjustTokenPrivileges(token, FALSE, &priv, 0, NULL, 0)) { | |
c6bd8c70 MA |
243 | error_setg(&local_err, QERR_QGA_COMMAND_FAILED, |
244 | "unable to acquire requested privilege"); | |
aa59637e GH |
245 | goto out; |
246 | } | |
247 | ||
aa59637e | 248 | } else { |
c6bd8c70 MA |
249 | error_setg(&local_err, QERR_QGA_COMMAND_FAILED, |
250 | "failed to open privilege token"); | |
aa59637e GH |
251 | } |
252 | ||
253 | out: | |
374044f0 GA |
254 | if (token) { |
255 | CloseHandle(token); | |
256 | } | |
621ff94d | 257 | error_propagate(errp, local_err); |
aa59637e GH |
258 | } |
259 | ||
77dbc81b MA |
260 | static void execute_async(DWORD WINAPI (*func)(LPVOID), LPVOID opaque, |
261 | Error **errp) | |
aa59637e GH |
262 | { |
263 | Error *local_err = NULL; | |
264 | ||
aa59637e GH |
265 | HANDLE thread = CreateThread(NULL, 0, func, opaque, 0, NULL); |
266 | if (!thread) { | |
c6bd8c70 MA |
267 | error_setg(&local_err, QERR_QGA_COMMAND_FAILED, |
268 | "failed to dispatch asynchronous command"); | |
77dbc81b | 269 | error_propagate(errp, local_err); |
aa59637e GH |
270 | } |
271 | } | |
272 | ||
77dbc81b | 273 | void qmp_guest_shutdown(bool has_mode, const char *mode, Error **errp) |
aa59637e | 274 | { |
0f230bf7 | 275 | Error *local_err = NULL; |
546b60d0 MR |
276 | UINT shutdown_flag = EWX_FORCE; |
277 | ||
278 | slog("guest-shutdown called, mode: %s", mode); | |
279 | ||
280 | if (!has_mode || strcmp(mode, "powerdown") == 0) { | |
281 | shutdown_flag |= EWX_POWEROFF; | |
282 | } else if (strcmp(mode, "halt") == 0) { | |
283 | shutdown_flag |= EWX_SHUTDOWN; | |
284 | } else if (strcmp(mode, "reboot") == 0) { | |
285 | shutdown_flag |= EWX_REBOOT; | |
286 | } else { | |
c6bd8c70 MA |
287 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "mode", |
288 | "halt|powerdown|reboot"); | |
546b60d0 MR |
289 | return; |
290 | } | |
291 | ||
292 | /* Request a shutdown privilege, but try to shut down the system | |
293 | anyway. */ | |
0f230bf7 MA |
294 | acquire_privilege(SE_SHUTDOWN_NAME, &local_err); |
295 | if (local_err) { | |
296 | error_propagate(errp, local_err); | |
aa59637e | 297 | return; |
546b60d0 MR |
298 | } |
299 | ||
300 | if (!ExitWindowsEx(shutdown_flag, SHTDN_REASON_FLAG_PLANNED)) { | |
16f4e8fa | 301 | slog("guest-shutdown failed: %lu", GetLastError()); |
c6bd8c70 | 302 | error_setg(errp, QERR_UNDEFINED_ERROR); |
546b60d0 | 303 | } |
d8ca685a MR |
304 | } |
305 | ||
d8ca685a | 306 | GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count, |
77dbc81b | 307 | int64_t count, Error **errp) |
d8ca685a | 308 | { |
fa193594 OK |
309 | GuestFileRead *read_data = NULL; |
310 | guchar *buf; | |
311 | HANDLE fh; | |
312 | bool is_ok; | |
313 | DWORD read_count; | |
314 | GuestFileHandle *gfh = guest_file_handle_find(handle, errp); | |
315 | ||
316 | if (!gfh) { | |
317 | return NULL; | |
318 | } | |
319 | if (!has_count) { | |
320 | count = QGA_READ_COUNT_DEFAULT; | |
141b1974 | 321 | } else if (count < 0 || count >= UINT32_MAX) { |
fa193594 OK |
322 | error_setg(errp, "value '%" PRId64 |
323 | "' is invalid for argument count", count); | |
324 | return NULL; | |
325 | } | |
326 | ||
327 | fh = gfh->fh; | |
328 | buf = g_malloc0(count+1); | |
329 | is_ok = ReadFile(fh, buf, count, &read_count, NULL); | |
330 | if (!is_ok) { | |
331 | error_setg_win32(errp, GetLastError(), "failed to read file"); | |
332 | slog("guest-file-read failed, handle %" PRId64, handle); | |
333 | } else { | |
334 | buf[read_count] = 0; | |
f3a06403 | 335 | read_data = g_new0(GuestFileRead, 1); |
fa193594 OK |
336 | read_data->count = (size_t)read_count; |
337 | read_data->eof = read_count == 0; | |
338 | ||
339 | if (read_count != 0) { | |
340 | read_data->buf_b64 = g_base64_encode(buf, read_count); | |
341 | } | |
342 | } | |
343 | g_free(buf); | |
344 | ||
345 | return read_data; | |
d8ca685a MR |
346 | } |
347 | ||
348 | GuestFileWrite *qmp_guest_file_write(int64_t handle, const char *buf_b64, | |
77dbc81b MA |
349 | bool has_count, int64_t count, |
350 | Error **errp) | |
d8ca685a | 351 | { |
fa193594 OK |
352 | GuestFileWrite *write_data = NULL; |
353 | guchar *buf; | |
354 | gsize buf_len; | |
355 | bool is_ok; | |
356 | DWORD write_count; | |
357 | GuestFileHandle *gfh = guest_file_handle_find(handle, errp); | |
358 | HANDLE fh; | |
359 | ||
360 | if (!gfh) { | |
361 | return NULL; | |
362 | } | |
363 | fh = gfh->fh; | |
920639ca DB |
364 | buf = qbase64_decode(buf_b64, -1, &buf_len, errp); |
365 | if (!buf) { | |
366 | return NULL; | |
367 | } | |
fa193594 OK |
368 | |
369 | if (!has_count) { | |
370 | count = buf_len; | |
371 | } else if (count < 0 || count > buf_len) { | |
372 | error_setg(errp, "value '%" PRId64 | |
373 | "' is invalid for argument count", count); | |
374 | goto done; | |
375 | } | |
376 | ||
377 | is_ok = WriteFile(fh, buf, count, &write_count, NULL); | |
378 | if (!is_ok) { | |
379 | error_setg_win32(errp, GetLastError(), "failed to write to file"); | |
380 | slog("guest-file-write-failed, handle: %" PRId64, handle); | |
381 | } else { | |
f3a06403 | 382 | write_data = g_new0(GuestFileWrite, 1); |
fa193594 OK |
383 | write_data->count = (size_t) write_count; |
384 | } | |
385 | ||
386 | done: | |
387 | g_free(buf); | |
388 | return write_data; | |
d8ca685a MR |
389 | } |
390 | ||
391 | GuestFileSeek *qmp_guest_file_seek(int64_t handle, int64_t offset, | |
0b4b4938 EB |
392 | GuestFileWhence *whence_code, |
393 | Error **errp) | |
d8ca685a | 394 | { |
fa193594 OK |
395 | GuestFileHandle *gfh; |
396 | GuestFileSeek *seek_data; | |
397 | HANDLE fh; | |
398 | LARGE_INTEGER new_pos, off_pos; | |
399 | off_pos.QuadPart = offset; | |
400 | BOOL res; | |
0a982b1b | 401 | int whence; |
0b4b4938 | 402 | Error *err = NULL; |
0a982b1b | 403 | |
fa193594 OK |
404 | gfh = guest_file_handle_find(handle, errp); |
405 | if (!gfh) { | |
406 | return NULL; | |
407 | } | |
408 | ||
0a982b1b | 409 | /* We stupidly exposed 'whence':'int' in our qapi */ |
0b4b4938 EB |
410 | whence = ga_parse_whence(whence_code, &err); |
411 | if (err) { | |
412 | error_propagate(errp, err); | |
0a982b1b EB |
413 | return NULL; |
414 | } | |
415 | ||
fa193594 OK |
416 | fh = gfh->fh; |
417 | res = SetFilePointerEx(fh, off_pos, &new_pos, whence); | |
418 | if (!res) { | |
419 | error_setg_win32(errp, GetLastError(), "failed to seek file"); | |
420 | return NULL; | |
421 | } | |
422 | seek_data = g_new0(GuestFileSeek, 1); | |
423 | seek_data->position = new_pos.QuadPart; | |
424 | return seek_data; | |
d8ca685a MR |
425 | } |
426 | ||
77dbc81b | 427 | void qmp_guest_file_flush(int64_t handle, Error **errp) |
d8ca685a | 428 | { |
fa193594 OK |
429 | HANDLE fh; |
430 | GuestFileHandle *gfh = guest_file_handle_find(handle, errp); | |
431 | if (!gfh) { | |
432 | return; | |
433 | } | |
434 | ||
435 | fh = gfh->fh; | |
436 | if (!FlushFileBuffers(fh)) { | |
437 | error_setg_win32(errp, GetLastError(), "failed to flush file"); | |
438 | } | |
439 | } | |
440 | ||
a3ef3b22 OK |
441 | #ifdef CONFIG_QGA_NTDDSCSI |
442 | ||
443 | static STORAGE_BUS_TYPE win2qemu[] = { | |
444 | [BusTypeUnknown] = GUEST_DISK_BUS_TYPE_UNKNOWN, | |
445 | [BusTypeScsi] = GUEST_DISK_BUS_TYPE_SCSI, | |
446 | [BusTypeAtapi] = GUEST_DISK_BUS_TYPE_IDE, | |
447 | [BusTypeAta] = GUEST_DISK_BUS_TYPE_IDE, | |
448 | [BusType1394] = GUEST_DISK_BUS_TYPE_IEEE1394, | |
449 | [BusTypeSsa] = GUEST_DISK_BUS_TYPE_SSA, | |
450 | [BusTypeFibre] = GUEST_DISK_BUS_TYPE_SSA, | |
451 | [BusTypeUsb] = GUEST_DISK_BUS_TYPE_USB, | |
452 | [BusTypeRAID] = GUEST_DISK_BUS_TYPE_RAID, | |
453 | #if (_WIN32_WINNT >= 0x0600) | |
454 | [BusTypeiScsi] = GUEST_DISK_BUS_TYPE_ISCSI, | |
455 | [BusTypeSas] = GUEST_DISK_BUS_TYPE_SAS, | |
456 | [BusTypeSata] = GUEST_DISK_BUS_TYPE_SATA, | |
457 | [BusTypeSd] = GUEST_DISK_BUS_TYPE_SD, | |
458 | [BusTypeMmc] = GUEST_DISK_BUS_TYPE_MMC, | |
459 | #endif | |
460 | #if (_WIN32_WINNT >= 0x0601) | |
461 | [BusTypeVirtual] = GUEST_DISK_BUS_TYPE_VIRTUAL, | |
462 | [BusTypeFileBackedVirtual] = GUEST_DISK_BUS_TYPE_FILE_BACKED_VIRTUAL, | |
463 | #endif | |
464 | }; | |
465 | ||
466 | static GuestDiskBusType find_bus_type(STORAGE_BUS_TYPE bus) | |
467 | { | |
468 | if (bus > ARRAY_SIZE(win2qemu) || (int)bus < 0) { | |
469 | return GUEST_DISK_BUS_TYPE_UNKNOWN; | |
470 | } | |
471 | return win2qemu[(int)bus]; | |
472 | } | |
473 | ||
c54e1eb4 MR |
474 | DEFINE_GUID(GUID_DEVINTERFACE_VOLUME, |
475 | 0x53f5630dL, 0xb6bf, 0x11d0, 0x94, 0xf2, | |
476 | 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b); | |
477 | ||
a3ef3b22 OK |
478 | static GuestPCIAddress *get_pci_info(char *guid, Error **errp) |
479 | { | |
c54e1eb4 MR |
480 | HDEVINFO dev_info; |
481 | SP_DEVINFO_DATA dev_info_data; | |
482 | DWORD size = 0; | |
483 | int i; | |
484 | char dev_name[MAX_PATH]; | |
485 | char *buffer = NULL; | |
486 | GuestPCIAddress *pci = NULL; | |
487 | char *name = g_strdup(&guid[4]); | |
488 | ||
489 | if (!QueryDosDevice(name, dev_name, ARRAY_SIZE(dev_name))) { | |
490 | error_setg_win32(errp, GetLastError(), "failed to get dos device name"); | |
491 | goto out; | |
492 | } | |
493 | ||
494 | dev_info = SetupDiGetClassDevs(&GUID_DEVINTERFACE_VOLUME, 0, 0, | |
495 | DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); | |
496 | if (dev_info == INVALID_HANDLE_VALUE) { | |
497 | error_setg_win32(errp, GetLastError(), "failed to get devices tree"); | |
498 | goto out; | |
499 | } | |
500 | ||
501 | dev_info_data.cbSize = sizeof(SP_DEVINFO_DATA); | |
502 | for (i = 0; SetupDiEnumDeviceInfo(dev_info, i, &dev_info_data); i++) { | |
503 | DWORD addr, bus, slot, func, dev, data, size2; | |
504 | while (!SetupDiGetDeviceRegistryProperty(dev_info, &dev_info_data, | |
505 | SPDRP_PHYSICAL_DEVICE_OBJECT_NAME, | |
506 | &data, (PBYTE)buffer, size, | |
507 | &size2)) { | |
508 | size = MAX(size, size2); | |
509 | if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { | |
510 | g_free(buffer); | |
511 | /* Double the size to avoid problems on | |
512 | * W2k MBCS systems per KB 888609. | |
513 | * https://support.microsoft.com/en-us/kb/259695 */ | |
514 | buffer = g_malloc(size * 2); | |
515 | } else { | |
516 | error_setg_win32(errp, GetLastError(), | |
517 | "failed to get device name"); | |
9bd8e933 | 518 | goto free_dev_info; |
c54e1eb4 MR |
519 | } |
520 | } | |
521 | ||
522 | if (g_strcmp0(buffer, dev_name)) { | |
523 | continue; | |
524 | } | |
525 | ||
526 | /* There is no need to allocate buffer in the next functions. The size | |
527 | * is known and ULONG according to | |
528 | * https://support.microsoft.com/en-us/kb/253232 | |
529 | * https://msdn.microsoft.com/en-us/library/windows/hardware/ff543095(v=vs.85).aspx | |
530 | */ | |
531 | if (!SetupDiGetDeviceRegistryProperty(dev_info, &dev_info_data, | |
532 | SPDRP_BUSNUMBER, &data, (PBYTE)&bus, size, NULL)) { | |
533 | break; | |
534 | } | |
535 | ||
536 | /* The function retrieves the device's address. This value will be | |
537 | * transformed into device function and number */ | |
538 | if (!SetupDiGetDeviceRegistryProperty(dev_info, &dev_info_data, | |
539 | SPDRP_ADDRESS, &data, (PBYTE)&addr, size, NULL)) { | |
540 | break; | |
541 | } | |
542 | ||
543 | /* This call returns UINumber of DEVICE_CAPABILITIES structure. | |
544 | * This number is typically a user-perceived slot number. */ | |
545 | if (!SetupDiGetDeviceRegistryProperty(dev_info, &dev_info_data, | |
546 | SPDRP_UI_NUMBER, &data, (PBYTE)&slot, size, NULL)) { | |
547 | break; | |
548 | } | |
549 | ||
550 | /* SetupApi gives us the same information as driver with | |
551 | * IoGetDeviceProperty. According to Microsoft | |
552 | * https://support.microsoft.com/en-us/kb/253232 | |
553 | * FunctionNumber = (USHORT)((propertyAddress) & 0x0000FFFF); | |
554 | * DeviceNumber = (USHORT)(((propertyAddress) >> 16) & 0x0000FFFF); | |
555 | * SPDRP_ADDRESS is propertyAddress, so we do the same.*/ | |
556 | ||
557 | func = addr & 0x0000FFFF; | |
558 | dev = (addr >> 16) & 0x0000FFFF; | |
559 | pci = g_malloc0(sizeof(*pci)); | |
560 | pci->domain = dev; | |
561 | pci->slot = slot; | |
562 | pci->function = func; | |
563 | pci->bus = bus; | |
564 | break; | |
565 | } | |
9bd8e933 LP |
566 | |
567 | free_dev_info: | |
568 | SetupDiDestroyDeviceInfoList(dev_info); | |
c54e1eb4 MR |
569 | out: |
570 | g_free(buffer); | |
571 | g_free(name); | |
572 | return pci; | |
a3ef3b22 OK |
573 | } |
574 | ||
575 | static int get_disk_bus_type(HANDLE vol_h, Error **errp) | |
576 | { | |
577 | STORAGE_PROPERTY_QUERY query; | |
578 | STORAGE_DEVICE_DESCRIPTOR *dev_desc, buf; | |
579 | DWORD received; | |
580 | ||
581 | dev_desc = &buf; | |
582 | dev_desc->Size = sizeof(buf); | |
583 | query.PropertyId = StorageDeviceProperty; | |
584 | query.QueryType = PropertyStandardQuery; | |
585 | ||
586 | if (!DeviceIoControl(vol_h, IOCTL_STORAGE_QUERY_PROPERTY, &query, | |
587 | sizeof(STORAGE_PROPERTY_QUERY), dev_desc, | |
588 | dev_desc->Size, &received, NULL)) { | |
589 | error_setg_win32(errp, GetLastError(), "failed to get bus type"); | |
590 | return -1; | |
591 | } | |
592 | ||
593 | return dev_desc->BusType; | |
594 | } | |
595 | ||
596 | /* VSS provider works with volumes, thus there is no difference if | |
597 | * the volume consist of spanned disks. Info about the first disk in the | |
598 | * volume is returned for the spanned disk group (LVM) */ | |
599 | static GuestDiskAddressList *build_guest_disk_info(char *guid, Error **errp) | |
600 | { | |
601 | GuestDiskAddressList *list = NULL; | |
602 | GuestDiskAddress *disk; | |
603 | SCSI_ADDRESS addr, *scsi_ad; | |
604 | DWORD len; | |
605 | int bus; | |
606 | HANDLE vol_h; | |
607 | ||
608 | scsi_ad = &addr; | |
609 | char *name = g_strndup(guid, strlen(guid)-1); | |
610 | ||
611 | vol_h = CreateFile(name, 0, FILE_SHARE_READ, NULL, OPEN_EXISTING, | |
612 | 0, NULL); | |
613 | if (vol_h == INVALID_HANDLE_VALUE) { | |
614 | error_setg_win32(errp, GetLastError(), "failed to open volume"); | |
615 | goto out_free; | |
616 | } | |
617 | ||
618 | bus = get_disk_bus_type(vol_h, errp); | |
619 | if (bus < 0) { | |
620 | goto out_close; | |
621 | } | |
622 | ||
623 | disk = g_malloc0(sizeof(*disk)); | |
624 | disk->bus_type = find_bus_type(bus); | |
625 | if (bus == BusTypeScsi || bus == BusTypeAta || bus == BusTypeRAID | |
626 | #if (_WIN32_WINNT >= 0x0600) | |
627 | /* This bus type is not supported before Windows Server 2003 SP1 */ | |
628 | || bus == BusTypeSas | |
629 | #endif | |
630 | ) { | |
631 | /* We are able to use the same ioctls for different bus types | |
632 | * according to Microsoft docs | |
633 | * https://technet.microsoft.com/en-us/library/ee851589(v=ws.10).aspx */ | |
634 | if (DeviceIoControl(vol_h, IOCTL_SCSI_GET_ADDRESS, NULL, 0, scsi_ad, | |
635 | sizeof(SCSI_ADDRESS), &len, NULL)) { | |
636 | disk->unit = addr.Lun; | |
637 | disk->target = addr.TargetId; | |
638 | disk->bus = addr.PathId; | |
639 | disk->pci_controller = get_pci_info(name, errp); | |
640 | } | |
641 | /* We do not set error in this case, because we still have enough | |
642 | * information about volume. */ | |
643 | } else { | |
644 | disk->pci_controller = NULL; | |
645 | } | |
646 | ||
647 | list = g_malloc0(sizeof(*list)); | |
648 | list->value = disk; | |
649 | list->next = NULL; | |
650 | out_close: | |
651 | CloseHandle(vol_h); | |
652 | out_free: | |
653 | g_free(name); | |
654 | return list; | |
655 | } | |
656 | ||
657 | #else | |
658 | ||
659 | static GuestDiskAddressList *build_guest_disk_info(char *guid, Error **errp) | |
660 | { | |
661 | return NULL; | |
662 | } | |
663 | ||
664 | #endif /* CONFIG_QGA_NTDDSCSI */ | |
665 | ||
d2b3f390 OK |
666 | static GuestFilesystemInfo *build_guest_fsinfo(char *guid, Error **errp) |
667 | { | |
668 | DWORD info_size; | |
669 | char mnt, *mnt_point; | |
670 | char fs_name[32]; | |
671 | char vol_info[MAX_PATH+1]; | |
672 | size_t len; | |
c07e5e6e | 673 | uint64_t i64FreeBytesToCaller, i64TotalBytes, i64FreeBytes; |
d2b3f390 OK |
674 | GuestFilesystemInfo *fs = NULL; |
675 | ||
676 | GetVolumePathNamesForVolumeName(guid, (LPCH)&mnt, 0, &info_size); | |
677 | if (GetLastError() != ERROR_MORE_DATA) { | |
678 | error_setg_win32(errp, GetLastError(), "failed to get volume name"); | |
679 | return NULL; | |
680 | } | |
681 | ||
682 | mnt_point = g_malloc(info_size + 1); | |
683 | if (!GetVolumePathNamesForVolumeName(guid, mnt_point, info_size, | |
684 | &info_size)) { | |
685 | error_setg_win32(errp, GetLastError(), "failed to get volume name"); | |
686 | goto free; | |
687 | } | |
688 | ||
689 | len = strlen(mnt_point); | |
690 | mnt_point[len] = '\\'; | |
691 | mnt_point[len+1] = 0; | |
692 | if (!GetVolumeInformation(mnt_point, vol_info, sizeof(vol_info), NULL, NULL, | |
693 | NULL, (LPSTR)&fs_name, sizeof(fs_name))) { | |
694 | if (GetLastError() != ERROR_NOT_READY) { | |
695 | error_setg_win32(errp, GetLastError(), "failed to get volume info"); | |
696 | } | |
697 | goto free; | |
698 | } | |
699 | ||
700 | fs_name[sizeof(fs_name) - 1] = 0; | |
701 | fs = g_malloc(sizeof(*fs)); | |
702 | fs->name = g_strdup(guid); | |
c07e5e6e CH |
703 | fs->has_total_bytes = false; |
704 | fs->has_used_bytes = false; | |
d2b3f390 OK |
705 | if (len == 0) { |
706 | fs->mountpoint = g_strdup("System Reserved"); | |
707 | } else { | |
708 | fs->mountpoint = g_strndup(mnt_point, len); | |
c07e5e6e CH |
709 | if (GetDiskFreeSpaceEx(fs->mountpoint, |
710 | (PULARGE_INTEGER) & i64FreeBytesToCaller, | |
711 | (PULARGE_INTEGER) & i64TotalBytes, | |
712 | (PULARGE_INTEGER) & i64FreeBytes)) { | |
713 | fs->used_bytes = i64TotalBytes - i64FreeBytes; | |
714 | fs->total_bytes = i64TotalBytes; | |
715 | fs->has_total_bytes = true; | |
716 | fs->has_used_bytes = true; | |
717 | } | |
d2b3f390 OK |
718 | } |
719 | fs->type = g_strdup(fs_name); | |
a8f15a27 | 720 | fs->disk = build_guest_disk_info(guid, errp); |
d2b3f390 OK |
721 | free: |
722 | g_free(mnt_point); | |
723 | return fs; | |
724 | } | |
725 | ||
46d4c572 TS |
726 | GuestFilesystemInfoList *qmp_guest_get_fsinfo(Error **errp) |
727 | { | |
ef0a03f2 OK |
728 | HANDLE vol_h; |
729 | GuestFilesystemInfoList *new, *ret = NULL; | |
730 | char guid[256]; | |
731 | ||
732 | vol_h = FindFirstVolume(guid, sizeof(guid)); | |
733 | if (vol_h == INVALID_HANDLE_VALUE) { | |
734 | error_setg_win32(errp, GetLastError(), "failed to find any volume"); | |
735 | return NULL; | |
736 | } | |
737 | ||
738 | do { | |
d2b3f390 OK |
739 | GuestFilesystemInfo *info = build_guest_fsinfo(guid, errp); |
740 | if (info == NULL) { | |
741 | continue; | |
742 | } | |
ef0a03f2 | 743 | new = g_malloc(sizeof(*ret)); |
d2b3f390 | 744 | new->value = info; |
ef0a03f2 OK |
745 | new->next = ret; |
746 | ret = new; | |
747 | } while (FindNextVolume(vol_h, guid, sizeof(guid))); | |
748 | ||
749 | if (GetLastError() != ERROR_NO_MORE_FILES) { | |
750 | error_setg_win32(errp, GetLastError(), "failed to find next volume"); | |
751 | } | |
752 | ||
753 | FindVolumeClose(vol_h); | |
754 | return ret; | |
46d4c572 TS |
755 | } |
756 | ||
d8ca685a MR |
757 | /* |
758 | * Return status of freeze/thaw | |
759 | */ | |
77dbc81b | 760 | GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **errp) |
d8ca685a | 761 | { |
64c00317 | 762 | if (!vss_initialized()) { |
c6bd8c70 | 763 | error_setg(errp, QERR_UNSUPPORTED); |
64c00317 TS |
764 | return 0; |
765 | } | |
766 | ||
767 | if (ga_is_frozen(ga_state)) { | |
768 | return GUEST_FSFREEZE_STATUS_FROZEN; | |
769 | } | |
770 | ||
771 | return GUEST_FSFREEZE_STATUS_THAWED; | |
d8ca685a MR |
772 | } |
773 | ||
774 | /* | |
64c00317 TS |
775 | * Freeze local file systems using Volume Shadow-copy Service. |
776 | * The frozen state is limited for up to 10 seconds by VSS. | |
d8ca685a | 777 | */ |
77dbc81b | 778 | int64_t qmp_guest_fsfreeze_freeze(Error **errp) |
d8ca685a | 779 | { |
64c00317 TS |
780 | int i; |
781 | Error *local_err = NULL; | |
782 | ||
783 | if (!vss_initialized()) { | |
c6bd8c70 | 784 | error_setg(errp, QERR_UNSUPPORTED); |
64c00317 TS |
785 | return 0; |
786 | } | |
787 | ||
788 | slog("guest-fsfreeze called"); | |
789 | ||
790 | /* cannot risk guest agent blocking itself on a write in this state */ | |
791 | ga_set_frozen(ga_state); | |
792 | ||
4462a530 | 793 | qga_vss_fsfreeze(&i, true, &local_err); |
0f230bf7 MA |
794 | if (local_err) { |
795 | error_propagate(errp, local_err); | |
64c00317 TS |
796 | goto error; |
797 | } | |
798 | ||
799 | return i; | |
800 | ||
801 | error: | |
0f230bf7 | 802 | local_err = NULL; |
64c00317 | 803 | qmp_guest_fsfreeze_thaw(&local_err); |
84d18f06 | 804 | if (local_err) { |
64c00317 TS |
805 | g_debug("cleanup thaw: %s", error_get_pretty(local_err)); |
806 | error_free(local_err); | |
807 | } | |
d8ca685a MR |
808 | return 0; |
809 | } | |
810 | ||
e99bce20 TS |
811 | int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints, |
812 | strList *mountpoints, | |
813 | Error **errp) | |
814 | { | |
c6bd8c70 | 815 | error_setg(errp, QERR_UNSUPPORTED); |
e99bce20 TS |
816 | |
817 | return 0; | |
818 | } | |
819 | ||
d8ca685a | 820 | /* |
64c00317 | 821 | * Thaw local file systems using Volume Shadow-copy Service. |
d8ca685a | 822 | */ |
77dbc81b | 823 | int64_t qmp_guest_fsfreeze_thaw(Error **errp) |
d8ca685a | 824 | { |
64c00317 TS |
825 | int i; |
826 | ||
827 | if (!vss_initialized()) { | |
c6bd8c70 | 828 | error_setg(errp, QERR_UNSUPPORTED); |
64c00317 TS |
829 | return 0; |
830 | } | |
831 | ||
4462a530 | 832 | qga_vss_fsfreeze(&i, false, errp); |
64c00317 TS |
833 | |
834 | ga_unset_frozen(ga_state); | |
835 | return i; | |
836 | } | |
837 | ||
838 | static void guest_fsfreeze_cleanup(void) | |
839 | { | |
840 | Error *err = NULL; | |
841 | ||
842 | if (!vss_initialized()) { | |
843 | return; | |
844 | } | |
845 | ||
846 | if (ga_is_frozen(ga_state) == GUEST_FSFREEZE_STATUS_FROZEN) { | |
847 | qmp_guest_fsfreeze_thaw(&err); | |
848 | if (err) { | |
849 | slog("failed to clean up frozen filesystems: %s", | |
850 | error_get_pretty(err)); | |
851 | error_free(err); | |
852 | } | |
853 | } | |
854 | ||
855 | vss_deinit(true); | |
d8ca685a MR |
856 | } |
857 | ||
eab5fd59 PB |
858 | /* |
859 | * Walk list of mounted file systems in the guest, and discard unused | |
860 | * areas. | |
861 | */ | |
e82855d9 JO |
862 | GuestFilesystemTrimResponse * |
863 | qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **errp) | |
eab5fd59 | 864 | { |
91274487 DL |
865 | GuestFilesystemTrimResponse *resp; |
866 | HANDLE handle; | |
867 | WCHAR guid[MAX_PATH] = L""; | |
c5840b90 SJ |
868 | OSVERSIONINFO osvi; |
869 | BOOL win8_or_later; | |
870 | ||
871 | ZeroMemory(&osvi, sizeof(OSVERSIONINFO)); | |
872 | osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); | |
873 | GetVersionEx(&osvi); | |
874 | win8_or_later = (osvi.dwMajorVersion > 6 || | |
875 | ((osvi.dwMajorVersion == 6) && | |
876 | (osvi.dwMinorVersion >= 2))); | |
877 | if (!win8_or_later) { | |
878 | error_setg(errp, "fstrim is only supported for Win8+"); | |
879 | return NULL; | |
880 | } | |
91274487 DL |
881 | |
882 | handle = FindFirstVolumeW(guid, ARRAYSIZE(guid)); | |
883 | if (handle == INVALID_HANDLE_VALUE) { | |
884 | error_setg_win32(errp, GetLastError(), "failed to find any volume"); | |
885 | return NULL; | |
886 | } | |
887 | ||
888 | resp = g_new0(GuestFilesystemTrimResponse, 1); | |
889 | ||
890 | do { | |
891 | GuestFilesystemTrimResult *res; | |
892 | GuestFilesystemTrimResultList *list; | |
893 | PWCHAR uc_path; | |
894 | DWORD char_count = 0; | |
895 | char *path, *out; | |
896 | GError *gerr = NULL; | |
897 | gchar * argv[4]; | |
898 | ||
899 | GetVolumePathNamesForVolumeNameW(guid, NULL, 0, &char_count); | |
900 | ||
901 | if (GetLastError() != ERROR_MORE_DATA) { | |
902 | continue; | |
903 | } | |
904 | if (GetDriveTypeW(guid) != DRIVE_FIXED) { | |
905 | continue; | |
906 | } | |
907 | ||
908 | uc_path = g_malloc(sizeof(WCHAR) * char_count); | |
909 | if (!GetVolumePathNamesForVolumeNameW(guid, uc_path, char_count, | |
910 | &char_count) || !*uc_path) { | |
911 | /* strange, but this condition could be faced even with size == 2 */ | |
912 | g_free(uc_path); | |
913 | continue; | |
914 | } | |
915 | ||
916 | res = g_new0(GuestFilesystemTrimResult, 1); | |
917 | ||
918 | path = g_utf16_to_utf8(uc_path, char_count, NULL, NULL, &gerr); | |
919 | ||
920 | g_free(uc_path); | |
921 | ||
922 | if (!path) { | |
923 | res->has_error = true; | |
924 | res->error = g_strdup(gerr->message); | |
925 | g_error_free(gerr); | |
926 | break; | |
927 | } | |
928 | ||
929 | res->path = path; | |
930 | ||
931 | list = g_new0(GuestFilesystemTrimResultList, 1); | |
932 | list->value = res; | |
933 | list->next = resp->paths; | |
934 | ||
935 | resp->paths = list; | |
936 | ||
937 | memset(argv, 0, sizeof(argv)); | |
938 | argv[0] = (gchar *)"defrag.exe"; | |
939 | argv[1] = (gchar *)"/L"; | |
940 | argv[2] = path; | |
941 | ||
942 | if (!g_spawn_sync(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, | |
943 | &out /* stdout */, NULL /* stdin */, | |
944 | NULL, &gerr)) { | |
945 | res->has_error = true; | |
946 | res->error = g_strdup(gerr->message); | |
947 | g_error_free(gerr); | |
948 | } else { | |
949 | /* defrag.exe is UGLY. Exit code is ALWAYS zero. | |
950 | Error is reported in the output with something like | |
951 | (x89000020) etc code in the stdout */ | |
952 | ||
953 | int i; | |
954 | gchar **lines = g_strsplit(out, "\r\n", 0); | |
955 | g_free(out); | |
956 | ||
957 | for (i = 0; lines[i] != NULL; i++) { | |
958 | if (g_strstr_len(lines[i], -1, "(0x") == NULL) { | |
959 | continue; | |
960 | } | |
961 | res->has_error = true; | |
962 | res->error = g_strdup(lines[i]); | |
963 | break; | |
964 | } | |
965 | g_strfreev(lines); | |
966 | } | |
967 | } while (FindNextVolumeW(handle, guid, ARRAYSIZE(guid))); | |
968 | ||
969 | FindVolumeClose(handle); | |
970 | return resp; | |
eab5fd59 PB |
971 | } |
972 | ||
aa59637e | 973 | typedef enum { |
f54603b6 MR |
974 | GUEST_SUSPEND_MODE_DISK, |
975 | GUEST_SUSPEND_MODE_RAM | |
aa59637e GH |
976 | } GuestSuspendMode; |
977 | ||
77dbc81b | 978 | static void check_suspend_mode(GuestSuspendMode mode, Error **errp) |
aa59637e GH |
979 | { |
980 | SYSTEM_POWER_CAPABILITIES sys_pwr_caps; | |
981 | Error *local_err = NULL; | |
982 | ||
aa59637e GH |
983 | ZeroMemory(&sys_pwr_caps, sizeof(sys_pwr_caps)); |
984 | if (!GetPwrCapabilities(&sys_pwr_caps)) { | |
c6bd8c70 MA |
985 | error_setg(&local_err, QERR_QGA_COMMAND_FAILED, |
986 | "failed to determine guest suspend capabilities"); | |
aa59637e GH |
987 | goto out; |
988 | } | |
989 | ||
f54603b6 MR |
990 | switch (mode) { |
991 | case GUEST_SUSPEND_MODE_DISK: | |
992 | if (!sys_pwr_caps.SystemS4) { | |
c6bd8c70 MA |
993 | error_setg(&local_err, QERR_QGA_COMMAND_FAILED, |
994 | "suspend-to-disk not supported by OS"); | |
aa59637e | 995 | } |
f54603b6 MR |
996 | break; |
997 | case GUEST_SUSPEND_MODE_RAM: | |
998 | if (!sys_pwr_caps.SystemS3) { | |
c6bd8c70 MA |
999 | error_setg(&local_err, QERR_QGA_COMMAND_FAILED, |
1000 | "suspend-to-ram not supported by OS"); | |
f54603b6 MR |
1001 | } |
1002 | break; | |
1003 | default: | |
c6bd8c70 MA |
1004 | error_setg(&local_err, QERR_INVALID_PARAMETER_VALUE, "mode", |
1005 | "GuestSuspendMode"); | |
aa59637e GH |
1006 | } |
1007 | ||
aa59637e | 1008 | out: |
621ff94d | 1009 | error_propagate(errp, local_err); |
aa59637e GH |
1010 | } |
1011 | ||
1012 | static DWORD WINAPI do_suspend(LPVOID opaque) | |
1013 | { | |
1014 | GuestSuspendMode *mode = opaque; | |
1015 | DWORD ret = 0; | |
1016 | ||
1017 | if (!SetSuspendState(*mode == GUEST_SUSPEND_MODE_DISK, TRUE, TRUE)) { | |
16f4e8fa | 1018 | slog("failed to suspend guest, %lu", GetLastError()); |
aa59637e GH |
1019 | ret = -1; |
1020 | } | |
1021 | g_free(mode); | |
1022 | return ret; | |
1023 | } | |
1024 | ||
77dbc81b | 1025 | void qmp_guest_suspend_disk(Error **errp) |
11d0f125 | 1026 | { |
0f230bf7 | 1027 | Error *local_err = NULL; |
f3a06403 | 1028 | GuestSuspendMode *mode = g_new(GuestSuspendMode, 1); |
aa59637e GH |
1029 | |
1030 | *mode = GUEST_SUSPEND_MODE_DISK; | |
0f230bf7 MA |
1031 | check_suspend_mode(*mode, &local_err); |
1032 | acquire_privilege(SE_SHUTDOWN_NAME, &local_err); | |
1033 | execute_async(do_suspend, mode, &local_err); | |
aa59637e | 1034 | |
0f230bf7 MA |
1035 | if (local_err) { |
1036 | error_propagate(errp, local_err); | |
aa59637e GH |
1037 | g_free(mode); |
1038 | } | |
11d0f125 LC |
1039 | } |
1040 | ||
77dbc81b | 1041 | void qmp_guest_suspend_ram(Error **errp) |
fbf42210 | 1042 | { |
0f230bf7 | 1043 | Error *local_err = NULL; |
f3a06403 | 1044 | GuestSuspendMode *mode = g_new(GuestSuspendMode, 1); |
f54603b6 MR |
1045 | |
1046 | *mode = GUEST_SUSPEND_MODE_RAM; | |
0f230bf7 MA |
1047 | check_suspend_mode(*mode, &local_err); |
1048 | acquire_privilege(SE_SHUTDOWN_NAME, &local_err); | |
1049 | execute_async(do_suspend, mode, &local_err); | |
f54603b6 | 1050 | |
0f230bf7 MA |
1051 | if (local_err) { |
1052 | error_propagate(errp, local_err); | |
f54603b6 MR |
1053 | g_free(mode); |
1054 | } | |
fbf42210 LC |
1055 | } |
1056 | ||
77dbc81b | 1057 | void qmp_guest_suspend_hybrid(Error **errp) |
95f4f404 | 1058 | { |
c6bd8c70 | 1059 | error_setg(errp, QERR_UNSUPPORTED); |
95f4f404 LC |
1060 | } |
1061 | ||
d6c5528b | 1062 | static IP_ADAPTER_ADDRESSES *guest_get_adapters_addresses(Error **errp) |
3424fc9f | 1063 | { |
d6c5528b KA |
1064 | IP_ADAPTER_ADDRESSES *adptr_addrs = NULL; |
1065 | ULONG adptr_addrs_len = 0; | |
1066 | DWORD ret; | |
1067 | ||
1068 | /* Call the first time to get the adptr_addrs_len. */ | |
1069 | GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, | |
1070 | NULL, adptr_addrs, &adptr_addrs_len); | |
1071 | ||
1072 | adptr_addrs = g_malloc(adptr_addrs_len); | |
1073 | ret = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, | |
1074 | NULL, adptr_addrs, &adptr_addrs_len); | |
1075 | if (ret != ERROR_SUCCESS) { | |
1076 | error_setg_win32(errp, ret, "failed to get adapters addresses"); | |
1077 | g_free(adptr_addrs); | |
1078 | adptr_addrs = NULL; | |
1079 | } | |
1080 | return adptr_addrs; | |
1081 | } | |
1082 | ||
1083 | static char *guest_wctomb_dup(WCHAR *wstr) | |
1084 | { | |
1085 | char *str; | |
1086 | size_t i; | |
1087 | ||
1088 | i = wcslen(wstr) + 1; | |
1089 | str = g_malloc(i); | |
1090 | WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, | |
1091 | wstr, -1, str, i, NULL, NULL); | |
1092 | return str; | |
1093 | } | |
1094 | ||
1095 | static char *guest_addr_to_str(IP_ADAPTER_UNICAST_ADDRESS *ip_addr, | |
1096 | Error **errp) | |
1097 | { | |
1098 | char addr_str[INET6_ADDRSTRLEN + INET_ADDRSTRLEN]; | |
1099 | DWORD len; | |
1100 | int ret; | |
1101 | ||
1102 | if (ip_addr->Address.lpSockaddr->sa_family == AF_INET || | |
1103 | ip_addr->Address.lpSockaddr->sa_family == AF_INET6) { | |
1104 | len = sizeof(addr_str); | |
1105 | ret = WSAAddressToString(ip_addr->Address.lpSockaddr, | |
1106 | ip_addr->Address.iSockaddrLength, | |
1107 | NULL, | |
1108 | addr_str, | |
1109 | &len); | |
1110 | if (ret != 0) { | |
1111 | error_setg_win32(errp, WSAGetLastError(), | |
1112 | "failed address presentation form conversion"); | |
1113 | return NULL; | |
1114 | } | |
1115 | return g_strdup(addr_str); | |
1116 | } | |
3424fc9f MP |
1117 | return NULL; |
1118 | } | |
1119 | ||
d6c5528b KA |
1120 | #if (_WIN32_WINNT >= 0x0600) |
1121 | static int64_t guest_ip_prefix(IP_ADAPTER_UNICAST_ADDRESS *ip_addr) | |
1122 | { | |
1123 | /* For Windows Vista/2008 and newer, use the OnLinkPrefixLength | |
1124 | * field to obtain the prefix. | |
1125 | */ | |
1126 | return ip_addr->OnLinkPrefixLength; | |
1127 | } | |
1128 | #else | |
1129 | /* When using the Windows XP and 2003 build environment, do the best we can to | |
1130 | * figure out the prefix. | |
1131 | */ | |
1132 | static IP_ADAPTER_INFO *guest_get_adapters_info(void) | |
1133 | { | |
1134 | IP_ADAPTER_INFO *adptr_info = NULL; | |
1135 | ULONG adptr_info_len = 0; | |
1136 | DWORD ret; | |
1137 | ||
1138 | /* Call the first time to get the adptr_info_len. */ | |
1139 | GetAdaptersInfo(adptr_info, &adptr_info_len); | |
1140 | ||
1141 | adptr_info = g_malloc(adptr_info_len); | |
1142 | ret = GetAdaptersInfo(adptr_info, &adptr_info_len); | |
1143 | if (ret != ERROR_SUCCESS) { | |
1144 | g_free(adptr_info); | |
1145 | adptr_info = NULL; | |
1146 | } | |
1147 | return adptr_info; | |
1148 | } | |
1149 | ||
1150 | static int64_t guest_ip_prefix(IP_ADAPTER_UNICAST_ADDRESS *ip_addr) | |
1151 | { | |
1152 | int64_t prefix = -1; /* Use for AF_INET6 and unknown/undetermined values. */ | |
1153 | IP_ADAPTER_INFO *adptr_info, *info; | |
1154 | IP_ADDR_STRING *ip; | |
1155 | struct in_addr *p; | |
1156 | ||
1157 | if (ip_addr->Address.lpSockaddr->sa_family != AF_INET) { | |
1158 | return prefix; | |
1159 | } | |
1160 | adptr_info = guest_get_adapters_info(); | |
1161 | if (adptr_info == NULL) { | |
1162 | return prefix; | |
1163 | } | |
1164 | ||
1165 | /* Match up the passed in ip_addr with one found in adaptr_info. | |
1166 | * The matching one in adptr_info will have the netmask. | |
1167 | */ | |
1168 | p = &((struct sockaddr_in *)ip_addr->Address.lpSockaddr)->sin_addr; | |
1169 | for (info = adptr_info; info; info = info->Next) { | |
1170 | for (ip = &info->IpAddressList; ip; ip = ip->Next) { | |
1171 | if (p->S_un.S_addr == inet_addr(ip->IpAddress.String)) { | |
1172 | prefix = ctpop32(inet_addr(ip->IpMask.String)); | |
1173 | goto out; | |
1174 | } | |
1175 | } | |
1176 | } | |
1177 | out: | |
1178 | g_free(adptr_info); | |
1179 | return prefix; | |
1180 | } | |
1181 | #endif | |
1182 | ||
53f9fcb2 ZL |
1183 | #define INTERFACE_PATH_BUF_SZ 512 |
1184 | ||
1185 | static DWORD get_interface_index(const char *guid) | |
1186 | { | |
1187 | ULONG index; | |
1188 | DWORD status; | |
1189 | wchar_t wbuf[INTERFACE_PATH_BUF_SZ]; | |
1190 | snwprintf(wbuf, INTERFACE_PATH_BUF_SZ, L"\\device\\tcpip_%s", guid); | |
1191 | wbuf[INTERFACE_PATH_BUF_SZ - 1] = 0; | |
1192 | status = GetAdapterIndex (wbuf, &index); | |
1193 | if (status != NO_ERROR) { | |
1194 | return (DWORD)~0; | |
1195 | } else { | |
1196 | return index; | |
1197 | } | |
1198 | } | |
df83eabd ZL |
1199 | |
1200 | typedef NETIOAPI_API (WINAPI *GetIfEntry2Func)(PMIB_IF_ROW2 Row); | |
1201 | ||
53f9fcb2 | 1202 | static int guest_get_network_stats(const char *name, |
df83eabd | 1203 | GuestNetworkInterfaceStat *stats) |
53f9fcb2 | 1204 | { |
df83eabd ZL |
1205 | OSVERSIONINFO os_ver; |
1206 | ||
1207 | os_ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); | |
1208 | GetVersionEx(&os_ver); | |
1209 | if (os_ver.dwMajorVersion >= 6) { | |
1210 | MIB_IF_ROW2 a_mid_ifrow; | |
1211 | GetIfEntry2Func getifentry2_ex; | |
1212 | DWORD if_index = 0; | |
1213 | HMODULE module = GetModuleHandle("iphlpapi"); | |
1214 | PVOID func = GetProcAddress(module, "GetIfEntry2"); | |
1215 | ||
1216 | if (func == NULL) { | |
1217 | return -1; | |
1218 | } | |
1219 | ||
1220 | getifentry2_ex = (GetIfEntry2Func)func; | |
1221 | if_index = get_interface_index(name); | |
1222 | if (if_index == (DWORD)~0) { | |
1223 | return -1; | |
1224 | } | |
1225 | ||
1226 | memset(&a_mid_ifrow, 0, sizeof(a_mid_ifrow)); | |
1227 | a_mid_ifrow.InterfaceIndex = if_index; | |
1228 | if (NO_ERROR == getifentry2_ex(&a_mid_ifrow)) { | |
1229 | stats->rx_bytes = a_mid_ifrow.InOctets; | |
1230 | stats->rx_packets = a_mid_ifrow.InUcastPkts; | |
1231 | stats->rx_errs = a_mid_ifrow.InErrors; | |
1232 | stats->rx_dropped = a_mid_ifrow.InDiscards; | |
1233 | stats->tx_bytes = a_mid_ifrow.OutOctets; | |
1234 | stats->tx_packets = a_mid_ifrow.OutUcastPkts; | |
1235 | stats->tx_errs = a_mid_ifrow.OutErrors; | |
1236 | stats->tx_dropped = a_mid_ifrow.OutDiscards; | |
1237 | return 0; | |
1238 | } | |
53f9fcb2 ZL |
1239 | } |
1240 | return -1; | |
1241 | } | |
1242 | ||
d6c5528b KA |
1243 | GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp) |
1244 | { | |
1245 | IP_ADAPTER_ADDRESSES *adptr_addrs, *addr; | |
1246 | IP_ADAPTER_UNICAST_ADDRESS *ip_addr = NULL; | |
1247 | GuestNetworkInterfaceList *head = NULL, *cur_item = NULL; | |
1248 | GuestIpAddressList *head_addr, *cur_addr; | |
1249 | GuestNetworkInterfaceList *info; | |
53f9fcb2 | 1250 | GuestNetworkInterfaceStat *interface_stat = NULL; |
d6c5528b KA |
1251 | GuestIpAddressList *address_item = NULL; |
1252 | unsigned char *mac_addr; | |
1253 | char *addr_str; | |
1254 | WORD wsa_version; | |
1255 | WSADATA wsa_data; | |
1256 | int ret; | |
1257 | ||
1258 | adptr_addrs = guest_get_adapters_addresses(errp); | |
1259 | if (adptr_addrs == NULL) { | |
1260 | return NULL; | |
1261 | } | |
1262 | ||
1263 | /* Make WSA APIs available. */ | |
1264 | wsa_version = MAKEWORD(2, 2); | |
1265 | ret = WSAStartup(wsa_version, &wsa_data); | |
1266 | if (ret != 0) { | |
1267 | error_setg_win32(errp, ret, "failed socket startup"); | |
1268 | goto out; | |
1269 | } | |
1270 | ||
1271 | for (addr = adptr_addrs; addr; addr = addr->Next) { | |
1272 | info = g_malloc0(sizeof(*info)); | |
1273 | ||
1274 | if (cur_item == NULL) { | |
1275 | head = cur_item = info; | |
1276 | } else { | |
1277 | cur_item->next = info; | |
1278 | cur_item = info; | |
1279 | } | |
1280 | ||
1281 | info->value = g_malloc0(sizeof(*info->value)); | |
1282 | info->value->name = guest_wctomb_dup(addr->FriendlyName); | |
1283 | ||
1284 | if (addr->PhysicalAddressLength != 0) { | |
1285 | mac_addr = addr->PhysicalAddress; | |
1286 | ||
1287 | info->value->hardware_address = | |
1288 | g_strdup_printf("%02x:%02x:%02x:%02x:%02x:%02x", | |
1289 | (int) mac_addr[0], (int) mac_addr[1], | |
1290 | (int) mac_addr[2], (int) mac_addr[3], | |
1291 | (int) mac_addr[4], (int) mac_addr[5]); | |
1292 | ||
1293 | info->value->has_hardware_address = true; | |
1294 | } | |
1295 | ||
1296 | head_addr = NULL; | |
1297 | cur_addr = NULL; | |
1298 | for (ip_addr = addr->FirstUnicastAddress; | |
1299 | ip_addr; | |
1300 | ip_addr = ip_addr->Next) { | |
1301 | addr_str = guest_addr_to_str(ip_addr, errp); | |
1302 | if (addr_str == NULL) { | |
1303 | continue; | |
1304 | } | |
1305 | ||
1306 | address_item = g_malloc0(sizeof(*address_item)); | |
1307 | ||
1308 | if (!cur_addr) { | |
1309 | head_addr = cur_addr = address_item; | |
1310 | } else { | |
1311 | cur_addr->next = address_item; | |
1312 | cur_addr = address_item; | |
1313 | } | |
1314 | ||
1315 | address_item->value = g_malloc0(sizeof(*address_item->value)); | |
1316 | address_item->value->ip_address = addr_str; | |
1317 | address_item->value->prefix = guest_ip_prefix(ip_addr); | |
1318 | if (ip_addr->Address.lpSockaddr->sa_family == AF_INET) { | |
1319 | address_item->value->ip_address_type = | |
1320 | GUEST_IP_ADDRESS_TYPE_IPV4; | |
1321 | } else if (ip_addr->Address.lpSockaddr->sa_family == AF_INET6) { | |
1322 | address_item->value->ip_address_type = | |
1323 | GUEST_IP_ADDRESS_TYPE_IPV6; | |
1324 | } | |
1325 | } | |
1326 | if (head_addr) { | |
1327 | info->value->has_ip_addresses = true; | |
1328 | info->value->ip_addresses = head_addr; | |
1329 | } | |
53f9fcb2 ZL |
1330 | if (!info->value->has_statistics) { |
1331 | interface_stat = g_malloc0(sizeof(*interface_stat)); | |
1332 | if (guest_get_network_stats(addr->AdapterName, | |
1333 | interface_stat) == -1) { | |
1334 | info->value->has_statistics = false; | |
1335 | g_free(interface_stat); | |
1336 | } else { | |
1337 | info->value->statistics = interface_stat; | |
1338 | info->value->has_statistics = true; | |
1339 | } | |
1340 | } | |
d6c5528b KA |
1341 | } |
1342 | WSACleanup(); | |
1343 | out: | |
1344 | g_free(adptr_addrs); | |
1345 | return head; | |
1346 | } | |
1347 | ||
6912e6a9 LL |
1348 | int64_t qmp_guest_get_time(Error **errp) |
1349 | { | |
3f2a6087 | 1350 | SYSTEMTIME ts = {0}; |
3f2a6087 LL |
1351 | FILETIME tf; |
1352 | ||
1353 | GetSystemTime(&ts); | |
1354 | if (ts.wYear < 1601 || ts.wYear > 30827) { | |
1355 | error_setg(errp, "Failed to get time"); | |
1356 | return -1; | |
1357 | } | |
1358 | ||
1359 | if (!SystemTimeToFileTime(&ts, &tf)) { | |
1360 | error_setg(errp, "Failed to convert system time: %d", (int)GetLastError()); | |
1361 | return -1; | |
1362 | } | |
1363 | ||
9be38598 | 1364 | return ((((int64_t)tf.dwHighDateTime << 32) | tf.dwLowDateTime) |
3f2a6087 | 1365 | - W32_FT_OFFSET) * 100; |
6912e6a9 LL |
1366 | } |
1367 | ||
2c958923 | 1368 | void qmp_guest_set_time(bool has_time, int64_t time_ns, Error **errp) |
a1bca57f | 1369 | { |
0f230bf7 | 1370 | Error *local_err = NULL; |
b8f954fe LL |
1371 | SYSTEMTIME ts; |
1372 | FILETIME tf; | |
1373 | LONGLONG time; | |
1374 | ||
ee17cbdc MP |
1375 | if (!has_time) { |
1376 | /* Unfortunately, Windows libraries don't provide an easy way to access | |
1377 | * RTC yet: | |
1378 | * | |
1379 | * https://msdn.microsoft.com/en-us/library/aa908981.aspx | |
105fad6b BA |
1380 | * |
1381 | * Instead, a workaround is to use the Windows win32tm command to | |
1382 | * resync the time using the Windows Time service. | |
ee17cbdc | 1383 | */ |
105fad6b BA |
1384 | LPVOID msg_buffer; |
1385 | DWORD ret_flags; | |
1386 | ||
1387 | HRESULT hr = system("w32tm /resync /nowait"); | |
1388 | ||
1389 | if (GetLastError() != 0) { | |
1390 | strerror_s((LPTSTR) & msg_buffer, 0, errno); | |
1391 | error_setg(errp, "system(...) failed: %s", (LPCTSTR)msg_buffer); | |
1392 | } else if (hr != 0) { | |
1393 | if (hr == HRESULT_FROM_WIN32(ERROR_SERVICE_NOT_ACTIVE)) { | |
1394 | error_setg(errp, "Windows Time service not running on the " | |
1395 | "guest"); | |
1396 | } else { | |
1397 | if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | | |
1398 | FORMAT_MESSAGE_FROM_SYSTEM | | |
1399 | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, | |
1400 | (DWORD)hr, MAKELANGID(LANG_NEUTRAL, | |
1401 | SUBLANG_DEFAULT), (LPTSTR) & msg_buffer, 0, | |
1402 | NULL)) { | |
1403 | error_setg(errp, "w32tm failed with error (0x%lx), couldn'" | |
1404 | "t retrieve error message", hr); | |
1405 | } else { | |
1406 | error_setg(errp, "w32tm failed with error (0x%lx): %s", hr, | |
1407 | (LPCTSTR)msg_buffer); | |
1408 | LocalFree(msg_buffer); | |
1409 | } | |
1410 | } | |
1411 | } else if (!InternetGetConnectedState(&ret_flags, 0)) { | |
1412 | error_setg(errp, "No internet connection on guest, sync not " | |
1413 | "accurate"); | |
1414 | } | |
ee17cbdc MP |
1415 | return; |
1416 | } | |
1417 | ||
1418 | /* Validate time passed by user. */ | |
1419 | if (time_ns < 0 || time_ns / 100 > INT64_MAX - W32_FT_OFFSET) { | |
1420 | error_setg(errp, "Time %" PRId64 "is invalid", time_ns); | |
1421 | return; | |
1422 | } | |
b8f954fe | 1423 | |
ee17cbdc | 1424 | time = time_ns / 100 + W32_FT_OFFSET; |
b8f954fe | 1425 | |
ee17cbdc MP |
1426 | tf.dwLowDateTime = (DWORD) time; |
1427 | tf.dwHighDateTime = (DWORD) (time >> 32); | |
b8f954fe | 1428 | |
ee17cbdc MP |
1429 | if (!FileTimeToSystemTime(&tf, &ts)) { |
1430 | error_setg(errp, "Failed to convert system time %d", | |
1431 | (int)GetLastError()); | |
1432 | return; | |
b8f954fe LL |
1433 | } |
1434 | ||
0f230bf7 MA |
1435 | acquire_privilege(SE_SYSTEMTIME_NAME, &local_err); |
1436 | if (local_err) { | |
1437 | error_propagate(errp, local_err); | |
b8f954fe LL |
1438 | return; |
1439 | } | |
1440 | ||
1441 | if (!SetSystemTime(&ts)) { | |
1442 | error_setg(errp, "Failed to set time to guest: %d", (int)GetLastError()); | |
1443 | return; | |
1444 | } | |
a1bca57f LL |
1445 | } |
1446 | ||
70e133a7 LE |
1447 | GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp) |
1448 | { | |
a7a17362 GH |
1449 | PSYSTEM_LOGICAL_PROCESSOR_INFORMATION pslpi, ptr; |
1450 | DWORD length; | |
1451 | GuestLogicalProcessorList *head, **link; | |
1452 | Error *local_err = NULL; | |
1453 | int64_t current; | |
1454 | ||
1455 | ptr = pslpi = NULL; | |
1456 | length = 0; | |
1457 | current = 0; | |
1458 | head = NULL; | |
1459 | link = &head; | |
1460 | ||
1461 | if ((GetLogicalProcessorInformation(pslpi, &length) == FALSE) && | |
1462 | (GetLastError() == ERROR_INSUFFICIENT_BUFFER) && | |
1463 | (length > sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION))) { | |
1464 | ptr = pslpi = g_malloc0(length); | |
1465 | if (GetLogicalProcessorInformation(pslpi, &length) == FALSE) { | |
1466 | error_setg(&local_err, "Failed to get processor information: %d", | |
1467 | (int)GetLastError()); | |
1468 | } | |
1469 | } else { | |
1470 | error_setg(&local_err, | |
1471 | "Failed to get processor information buffer length: %d", | |
1472 | (int)GetLastError()); | |
1473 | } | |
1474 | ||
1475 | while ((local_err == NULL) && (length > 0)) { | |
1476 | if (pslpi->Relationship == RelationProcessorCore) { | |
1477 | ULONG_PTR cpu_bits = pslpi->ProcessorMask; | |
1478 | ||
1479 | while (cpu_bits > 0) { | |
1480 | if (!!(cpu_bits & 1)) { | |
1481 | GuestLogicalProcessor *vcpu; | |
1482 | GuestLogicalProcessorList *entry; | |
1483 | ||
1484 | vcpu = g_malloc0(sizeof *vcpu); | |
1485 | vcpu->logical_id = current++; | |
1486 | vcpu->online = true; | |
54858553 | 1487 | vcpu->has_can_offline = true; |
a7a17362 GH |
1488 | |
1489 | entry = g_malloc0(sizeof *entry); | |
1490 | entry->value = vcpu; | |
1491 | ||
1492 | *link = entry; | |
1493 | link = &entry->next; | |
1494 | } | |
1495 | cpu_bits >>= 1; | |
1496 | } | |
1497 | } | |
1498 | length -= sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); | |
1499 | pslpi++; /* next entry */ | |
1500 | } | |
1501 | ||
1502 | g_free(ptr); | |
1503 | ||
1504 | if (local_err == NULL) { | |
1505 | if (head != NULL) { | |
1506 | return head; | |
1507 | } | |
1508 | /* there's no guest with zero VCPUs */ | |
1509 | error_setg(&local_err, "Guest reported zero VCPUs"); | |
1510 | } | |
1511 | ||
1512 | qapi_free_GuestLogicalProcessorList(head); | |
1513 | error_propagate(errp, local_err); | |
70e133a7 LE |
1514 | return NULL; |
1515 | } | |
1516 | ||
1517 | int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp) | |
1518 | { | |
c6bd8c70 | 1519 | error_setg(errp, QERR_UNSUPPORTED); |
70e133a7 LE |
1520 | return -1; |
1521 | } | |
1522 | ||
259434b8 MAL |
1523 | static gchar * |
1524 | get_net_error_message(gint error) | |
1525 | { | |
1526 | HMODULE module = NULL; | |
1527 | gchar *retval = NULL; | |
1528 | wchar_t *msg = NULL; | |
6771197d MAL |
1529 | int flags; |
1530 | size_t nchars; | |
259434b8 | 1531 | |
02506e2d MAL |
1532 | flags = FORMAT_MESSAGE_ALLOCATE_BUFFER | |
1533 | FORMAT_MESSAGE_IGNORE_INSERTS | | |
1534 | FORMAT_MESSAGE_FROM_SYSTEM; | |
259434b8 MAL |
1535 | |
1536 | if (error >= NERR_BASE && error <= MAX_NERR) { | |
1537 | module = LoadLibraryExW(L"netmsg.dll", NULL, LOAD_LIBRARY_AS_DATAFILE); | |
1538 | ||
1539 | if (module != NULL) { | |
1540 | flags |= FORMAT_MESSAGE_FROM_HMODULE; | |
1541 | } | |
1542 | } | |
1543 | ||
1544 | FormatMessageW(flags, module, error, 0, (LPWSTR)&msg, 0, NULL); | |
1545 | ||
1546 | if (msg != NULL) { | |
1547 | nchars = wcslen(msg); | |
1548 | ||
25d943b9 | 1549 | if (nchars >= 2 && |
6c6916da MAL |
1550 | msg[nchars - 1] == L'\n' && |
1551 | msg[nchars - 2] == L'\r') { | |
1552 | msg[nchars - 2] = L'\0'; | |
259434b8 MAL |
1553 | } |
1554 | ||
1555 | retval = g_utf16_to_utf8(msg, -1, NULL, NULL, NULL); | |
1556 | ||
1557 | LocalFree(msg); | |
1558 | } | |
1559 | ||
1560 | if (module != NULL) { | |
1561 | FreeLibrary(module); | |
1562 | } | |
1563 | ||
1564 | return retval; | |
1565 | } | |
1566 | ||
215a2771 DB |
1567 | void qmp_guest_set_user_password(const char *username, |
1568 | const char *password, | |
1569 | bool crypted, | |
1570 | Error **errp) | |
1571 | { | |
259434b8 MAL |
1572 | NET_API_STATUS nas; |
1573 | char *rawpasswddata = NULL; | |
1574 | size_t rawpasswdlen; | |
8021de10 | 1575 | wchar_t *user = NULL, *wpass = NULL; |
259434b8 | 1576 | USER_INFO_1003 pi1003 = { 0, }; |
8021de10 | 1577 | GError *gerr = NULL; |
259434b8 MAL |
1578 | |
1579 | if (crypted) { | |
1580 | error_setg(errp, QERR_UNSUPPORTED); | |
1581 | return; | |
1582 | } | |
1583 | ||
920639ca DB |
1584 | rawpasswddata = (char *)qbase64_decode(password, -1, &rawpasswdlen, errp); |
1585 | if (!rawpasswddata) { | |
1586 | return; | |
1587 | } | |
259434b8 MAL |
1588 | rawpasswddata = g_renew(char, rawpasswddata, rawpasswdlen + 1); |
1589 | rawpasswddata[rawpasswdlen] = '\0'; | |
1590 | ||
8021de10 MAL |
1591 | user = g_utf8_to_utf16(username, -1, NULL, NULL, &gerr); |
1592 | if (!user) { | |
1593 | goto done; | |
1594 | } | |
1595 | ||
1596 | wpass = g_utf8_to_utf16(rawpasswddata, -1, NULL, NULL, &gerr); | |
1597 | if (!wpass) { | |
1598 | goto done; | |
1599 | } | |
259434b8 MAL |
1600 | |
1601 | pi1003.usri1003_password = wpass; | |
1602 | nas = NetUserSetInfo(NULL, user, | |
1603 | 1003, (LPBYTE)&pi1003, | |
1604 | NULL); | |
1605 | ||
1606 | if (nas != NERR_Success) { | |
1607 | gchar *msg = get_net_error_message(nas); | |
1608 | error_setg(errp, "failed to set password: %s", msg); | |
1609 | g_free(msg); | |
1610 | } | |
1611 | ||
8021de10 MAL |
1612 | done: |
1613 | if (gerr) { | |
1614 | error_setg(errp, QERR_QGA_COMMAND_FAILED, gerr->message); | |
1615 | g_error_free(gerr); | |
1616 | } | |
259434b8 MAL |
1617 | g_free(user); |
1618 | g_free(wpass); | |
1619 | g_free(rawpasswddata); | |
215a2771 DB |
1620 | } |
1621 | ||
a065aaa9 HZ |
1622 | GuestMemoryBlockList *qmp_guest_get_memory_blocks(Error **errp) |
1623 | { | |
c6bd8c70 | 1624 | error_setg(errp, QERR_UNSUPPORTED); |
a065aaa9 HZ |
1625 | return NULL; |
1626 | } | |
1627 | ||
1628 | GuestMemoryBlockResponseList * | |
1629 | qmp_guest_set_memory_blocks(GuestMemoryBlockList *mem_blks, Error **errp) | |
1630 | { | |
c6bd8c70 | 1631 | error_setg(errp, QERR_UNSUPPORTED); |
a065aaa9 HZ |
1632 | return NULL; |
1633 | } | |
1634 | ||
1635 | GuestMemoryBlockInfo *qmp_guest_get_memory_block_info(Error **errp) | |
1636 | { | |
c6bd8c70 | 1637 | error_setg(errp, QERR_UNSUPPORTED); |
a065aaa9 HZ |
1638 | return NULL; |
1639 | } | |
1640 | ||
1281c08a TS |
1641 | /* add unsupported commands to the blacklist */ |
1642 | GList *ga_command_blacklist_init(GList *blacklist) | |
1643 | { | |
1644 | const char *list_unsupported[] = { | |
d6c5528b | 1645 | "guest-suspend-hybrid", |
a7a17362 | 1646 | "guest-set-vcpus", |
0dd38a03 HZ |
1647 | "guest-get-memory-blocks", "guest-set-memory-blocks", |
1648 | "guest-get-memory-block-size", | |
ef0a03f2 | 1649 | "guest-fsfreeze-freeze-list", |
91274487 | 1650 | NULL}; |
1281c08a TS |
1651 | char **p = (char **)list_unsupported; |
1652 | ||
1653 | while (*p) { | |
4bca81ce | 1654 | blacklist = g_list_append(blacklist, g_strdup(*p++)); |
1281c08a TS |
1655 | } |
1656 | ||
1657 | if (!vss_init(true)) { | |
c69403fc | 1658 | g_debug("vss_init failed, vss commands are going to be disabled"); |
1281c08a TS |
1659 | const char *list[] = { |
1660 | "guest-get-fsinfo", "guest-fsfreeze-status", | |
1661 | "guest-fsfreeze-freeze", "guest-fsfreeze-thaw", NULL}; | |
1662 | p = (char **)list; | |
1663 | ||
1664 | while (*p) { | |
4bca81ce | 1665 | blacklist = g_list_append(blacklist, g_strdup(*p++)); |
1281c08a TS |
1666 | } |
1667 | } | |
1668 | ||
1669 | return blacklist; | |
1670 | } | |
1671 | ||
d8ca685a MR |
1672 | /* register init/cleanup routines for stateful command groups */ |
1673 | void ga_command_state_init(GAState *s, GACommandState *cs) | |
1674 | { | |
1281c08a | 1675 | if (!vss_initialized()) { |
64c00317 TS |
1676 | ga_command_state_add(cs, NULL, guest_fsfreeze_cleanup); |
1677 | } | |
d8ca685a | 1678 | } |
161a56a9 VF |
1679 | |
1680 | /* MINGW is missing two fields: IncomingFrames & OutgoingFrames */ | |
1681 | typedef struct _GA_WTSINFOA { | |
1682 | WTS_CONNECTSTATE_CLASS State; | |
1683 | DWORD SessionId; | |
1684 | DWORD IncomingBytes; | |
1685 | DWORD OutgoingBytes; | |
1686 | DWORD IncomingFrames; | |
1687 | DWORD OutgoingFrames; | |
1688 | DWORD IncomingCompressedBytes; | |
1689 | DWORD OutgoingCompressedBy; | |
1690 | CHAR WinStationName[WINSTATIONNAME_LENGTH]; | |
1691 | CHAR Domain[DOMAIN_LENGTH]; | |
1692 | CHAR UserName[USERNAME_LENGTH + 1]; | |
1693 | LARGE_INTEGER ConnectTime; | |
1694 | LARGE_INTEGER DisconnectTime; | |
1695 | LARGE_INTEGER LastInputTime; | |
1696 | LARGE_INTEGER LogonTime; | |
1697 | LARGE_INTEGER CurrentTime; | |
1698 | ||
1699 | } GA_WTSINFOA; | |
1700 | ||
1701 | GuestUserList *qmp_guest_get_users(Error **err) | |
1702 | { | |
1703 | #if (_WIN32_WINNT >= 0x0600) | |
1704 | #define QGA_NANOSECONDS 10000000 | |
1705 | ||
1706 | GHashTable *cache = NULL; | |
1707 | GuestUserList *head = NULL, *cur_item = NULL; | |
1708 | ||
1709 | DWORD buffer_size = 0, count = 0, i = 0; | |
1710 | GA_WTSINFOA *info = NULL; | |
1711 | WTS_SESSION_INFOA *entries = NULL; | |
1712 | GuestUserList *item = NULL; | |
1713 | GuestUser *user = NULL; | |
1714 | gpointer value = NULL; | |
1715 | INT64 login = 0; | |
1716 | double login_time = 0; | |
1717 | ||
1718 | cache = g_hash_table_new(g_str_hash, g_str_equal); | |
1719 | ||
1720 | if (WTSEnumerateSessionsA(NULL, 0, 1, &entries, &count)) { | |
1721 | for (i = 0; i < count; ++i) { | |
1722 | buffer_size = 0; | |
1723 | info = NULL; | |
1724 | if (WTSQuerySessionInformationA( | |
1725 | NULL, | |
1726 | entries[i].SessionId, | |
1727 | WTSSessionInfo, | |
1728 | (LPSTR *)&info, | |
1729 | &buffer_size | |
1730 | )) { | |
1731 | ||
1732 | if (strlen(info->UserName) == 0) { | |
1733 | WTSFreeMemory(info); | |
1734 | continue; | |
1735 | } | |
1736 | ||
1737 | login = info->LogonTime.QuadPart; | |
1738 | login -= W32_FT_OFFSET; | |
1739 | login_time = ((double)login) / QGA_NANOSECONDS; | |
1740 | ||
1741 | if (g_hash_table_contains(cache, info->UserName)) { | |
1742 | value = g_hash_table_lookup(cache, info->UserName); | |
1743 | user = (GuestUser *)value; | |
1744 | if (user->login_time > login_time) { | |
1745 | user->login_time = login_time; | |
1746 | } | |
1747 | } else { | |
1748 | item = g_new0(GuestUserList, 1); | |
1749 | item->value = g_new0(GuestUser, 1); | |
1750 | ||
1751 | item->value->user = g_strdup(info->UserName); | |
1752 | item->value->domain = g_strdup(info->Domain); | |
1753 | item->value->has_domain = true; | |
1754 | ||
1755 | item->value->login_time = login_time; | |
1756 | ||
1757 | g_hash_table_add(cache, item->value->user); | |
1758 | ||
1759 | if (!cur_item) { | |
1760 | head = cur_item = item; | |
1761 | } else { | |
1762 | cur_item->next = item; | |
1763 | cur_item = item; | |
1764 | } | |
1765 | } | |
1766 | } | |
1767 | WTSFreeMemory(info); | |
1768 | } | |
1769 | WTSFreeMemory(entries); | |
1770 | } | |
1771 | g_hash_table_destroy(cache); | |
1772 | return head; | |
1773 | #else | |
1774 | error_setg(err, QERR_UNSUPPORTED); | |
1775 | return NULL; | |
1776 | #endif | |
1777 | } | |
9848f797 TG |
1778 | |
1779 | typedef struct _ga_matrix_lookup_t { | |
1780 | int major; | |
1781 | int minor; | |
1782 | char const *version; | |
1783 | char const *version_id; | |
1784 | } ga_matrix_lookup_t; | |
1785 | ||
1786 | static ga_matrix_lookup_t const WIN_VERSION_MATRIX[2][8] = { | |
1787 | { | |
1788 | /* Desktop editions */ | |
1789 | { 5, 0, "Microsoft Windows 2000", "2000"}, | |
1790 | { 5, 1, "Microsoft Windows XP", "xp"}, | |
1791 | { 6, 0, "Microsoft Windows Vista", "vista"}, | |
1792 | { 6, 1, "Microsoft Windows 7" "7"}, | |
1793 | { 6, 2, "Microsoft Windows 8", "8"}, | |
1794 | { 6, 3, "Microsoft Windows 8.1", "8.1"}, | |
1795 | {10, 0, "Microsoft Windows 10", "10"}, | |
1796 | { 0, 0, 0} | |
1797 | },{ | |
1798 | /* Server editions */ | |
1799 | { 5, 2, "Microsoft Windows Server 2003", "2003"}, | |
1800 | { 6, 0, "Microsoft Windows Server 2008", "2008"}, | |
1801 | { 6, 1, "Microsoft Windows Server 2008 R2", "2008r2"}, | |
1802 | { 6, 2, "Microsoft Windows Server 2012", "2012"}, | |
1803 | { 6, 3, "Microsoft Windows Server 2012 R2", "2012r2"}, | |
1804 | {10, 0, "Microsoft Windows Server 2016", "2016"}, | |
1805 | { 0, 0, 0}, | |
1806 | { 0, 0, 0} | |
1807 | } | |
1808 | }; | |
1809 | ||
1810 | static void ga_get_win_version(RTL_OSVERSIONINFOEXW *info, Error **errp) | |
1811 | { | |
1812 | typedef NTSTATUS(WINAPI * rtl_get_version_t)( | |
1813 | RTL_OSVERSIONINFOEXW *os_version_info_ex); | |
1814 | ||
1815 | info->dwOSVersionInfoSize = sizeof(RTL_OSVERSIONINFOEXW); | |
1816 | ||
1817 | HMODULE module = GetModuleHandle("ntdll"); | |
1818 | PVOID fun = GetProcAddress(module, "RtlGetVersion"); | |
1819 | if (fun == NULL) { | |
1820 | error_setg(errp, QERR_QGA_COMMAND_FAILED, | |
1821 | "Failed to get address of RtlGetVersion"); | |
1822 | return; | |
1823 | } | |
1824 | ||
1825 | rtl_get_version_t rtl_get_version = (rtl_get_version_t)fun; | |
1826 | rtl_get_version(info); | |
1827 | return; | |
1828 | } | |
1829 | ||
1830 | static char *ga_get_win_name(OSVERSIONINFOEXW const *os_version, bool id) | |
1831 | { | |
1832 | DWORD major = os_version->dwMajorVersion; | |
1833 | DWORD minor = os_version->dwMinorVersion; | |
1834 | int tbl_idx = (os_version->wProductType != VER_NT_WORKSTATION); | |
1835 | ga_matrix_lookup_t const *table = WIN_VERSION_MATRIX[tbl_idx]; | |
1836 | while (table->version != NULL) { | |
1837 | if (major == table->major && minor == table->minor) { | |
1838 | if (id) { | |
1839 | return g_strdup(table->version_id); | |
1840 | } else { | |
1841 | return g_strdup(table->version); | |
1842 | } | |
1843 | } | |
1844 | ++table; | |
1845 | } | |
1846 | slog("failed to lookup Windows version: major=%lu, minor=%lu", | |
1847 | major, minor); | |
1848 | return g_strdup("N/A"); | |
1849 | } | |
1850 | ||
1851 | static char *ga_get_win_product_name(Error **errp) | |
1852 | { | |
1853 | HKEY key = NULL; | |
1854 | DWORD size = 128; | |
1855 | char *result = g_malloc0(size); | |
1856 | LONG err = ERROR_SUCCESS; | |
1857 | ||
1858 | err = RegOpenKeyA(HKEY_LOCAL_MACHINE, | |
1859 | "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", | |
1860 | &key); | |
1861 | if (err != ERROR_SUCCESS) { | |
1862 | error_setg_win32(errp, err, "failed to open registry key"); | |
1863 | goto fail; | |
1864 | } | |
1865 | ||
1866 | err = RegQueryValueExA(key, "ProductName", NULL, NULL, | |
1867 | (LPBYTE)result, &size); | |
1868 | if (err == ERROR_MORE_DATA) { | |
1869 | slog("ProductName longer than expected (%lu bytes), retrying", | |
1870 | size); | |
1871 | g_free(result); | |
1872 | result = NULL; | |
1873 | if (size > 0) { | |
1874 | result = g_malloc0(size); | |
1875 | err = RegQueryValueExA(key, "ProductName", NULL, NULL, | |
1876 | (LPBYTE)result, &size); | |
1877 | } | |
1878 | } | |
1879 | if (err != ERROR_SUCCESS) { | |
1880 | error_setg_win32(errp, err, "failed to retrive ProductName"); | |
1881 | goto fail; | |
1882 | } | |
1883 | ||
1884 | return result; | |
1885 | ||
1886 | fail: | |
1887 | g_free(result); | |
1888 | return NULL; | |
1889 | } | |
1890 | ||
1891 | static char *ga_get_current_arch(void) | |
1892 | { | |
1893 | SYSTEM_INFO info; | |
1894 | GetNativeSystemInfo(&info); | |
1895 | char *result = NULL; | |
1896 | switch (info.wProcessorArchitecture) { | |
1897 | case PROCESSOR_ARCHITECTURE_AMD64: | |
1898 | result = g_strdup("x86_64"); | |
1899 | break; | |
1900 | case PROCESSOR_ARCHITECTURE_ARM: | |
1901 | result = g_strdup("arm"); | |
1902 | break; | |
1903 | case PROCESSOR_ARCHITECTURE_IA64: | |
1904 | result = g_strdup("ia64"); | |
1905 | break; | |
1906 | case PROCESSOR_ARCHITECTURE_INTEL: | |
1907 | result = g_strdup("x86"); | |
1908 | break; | |
1909 | case PROCESSOR_ARCHITECTURE_UNKNOWN: | |
1910 | default: | |
1911 | slog("unknown processor architecture 0x%0x", | |
1912 | info.wProcessorArchitecture); | |
1913 | result = g_strdup("unknown"); | |
1914 | break; | |
1915 | } | |
1916 | return result; | |
1917 | } | |
1918 | ||
1919 | GuestOSInfo *qmp_guest_get_osinfo(Error **errp) | |
1920 | { | |
1921 | Error *local_err = NULL; | |
1922 | OSVERSIONINFOEXW os_version = {0}; | |
1923 | bool server; | |
1924 | char *product_name; | |
1925 | GuestOSInfo *info; | |
1926 | ||
1927 | ga_get_win_version(&os_version, &local_err); | |
1928 | if (local_err) { | |
1929 | error_propagate(errp, local_err); | |
1930 | return NULL; | |
1931 | } | |
1932 | ||
1933 | server = os_version.wProductType != VER_NT_WORKSTATION; | |
1934 | product_name = ga_get_win_product_name(&local_err); | |
1935 | if (product_name == NULL) { | |
1936 | error_propagate(errp, local_err); | |
1937 | return NULL; | |
1938 | } | |
1939 | ||
1940 | info = g_new0(GuestOSInfo, 1); | |
1941 | ||
1942 | info->has_kernel_version = true; | |
1943 | info->kernel_version = g_strdup_printf("%lu.%lu", | |
1944 | os_version.dwMajorVersion, | |
1945 | os_version.dwMinorVersion); | |
1946 | info->has_kernel_release = true; | |
1947 | info->kernel_release = g_strdup_printf("%lu", | |
1948 | os_version.dwBuildNumber); | |
1949 | info->has_machine = true; | |
1950 | info->machine = ga_get_current_arch(); | |
1951 | ||
1952 | info->has_id = true; | |
1953 | info->id = g_strdup("mswindows"); | |
1954 | info->has_name = true; | |
1955 | info->name = g_strdup("Microsoft Windows"); | |
1956 | info->has_pretty_name = true; | |
1957 | info->pretty_name = product_name; | |
1958 | info->has_version = true; | |
1959 | info->version = ga_get_win_name(&os_version, false); | |
1960 | info->has_version_id = true; | |
1961 | info->version_id = ga_get_win_name(&os_version, true); | |
1962 | info->has_variant = true; | |
1963 | info->variant = g_strdup(server ? "server" : "client"); | |
1964 | info->has_variant_id = true; | |
1965 | info->variant_id = g_strdup(server ? "server" : "client"); | |
1966 | ||
1967 | return info; | |
1968 | } |