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