]>
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) |
d8ca685a | 793 | { |
64c00317 TS |
794 | int i; |
795 | Error *local_err = NULL; | |
796 | ||
797 | if (!vss_initialized()) { | |
c6bd8c70 | 798 | error_setg(errp, QERR_UNSUPPORTED); |
64c00317 TS |
799 | return 0; |
800 | } | |
801 | ||
802 | slog("guest-fsfreeze called"); | |
803 | ||
804 | /* cannot risk guest agent blocking itself on a write in this state */ | |
805 | ga_set_frozen(ga_state); | |
806 | ||
4462a530 | 807 | qga_vss_fsfreeze(&i, true, &local_err); |
0f230bf7 MA |
808 | if (local_err) { |
809 | error_propagate(errp, local_err); | |
64c00317 TS |
810 | goto error; |
811 | } | |
812 | ||
813 | return i; | |
814 | ||
815 | error: | |
0f230bf7 | 816 | local_err = NULL; |
64c00317 | 817 | qmp_guest_fsfreeze_thaw(&local_err); |
84d18f06 | 818 | if (local_err) { |
64c00317 TS |
819 | g_debug("cleanup thaw: %s", error_get_pretty(local_err)); |
820 | error_free(local_err); | |
821 | } | |
d8ca685a MR |
822 | return 0; |
823 | } | |
824 | ||
e99bce20 TS |
825 | int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints, |
826 | strList *mountpoints, | |
827 | Error **errp) | |
828 | { | |
c6bd8c70 | 829 | error_setg(errp, QERR_UNSUPPORTED); |
e99bce20 TS |
830 | |
831 | return 0; | |
832 | } | |
833 | ||
d8ca685a | 834 | /* |
64c00317 | 835 | * Thaw local file systems using Volume Shadow-copy Service. |
d8ca685a | 836 | */ |
77dbc81b | 837 | int64_t qmp_guest_fsfreeze_thaw(Error **errp) |
d8ca685a | 838 | { |
64c00317 TS |
839 | int i; |
840 | ||
841 | if (!vss_initialized()) { | |
c6bd8c70 | 842 | error_setg(errp, QERR_UNSUPPORTED); |
64c00317 TS |
843 | return 0; |
844 | } | |
845 | ||
4462a530 | 846 | qga_vss_fsfreeze(&i, false, errp); |
64c00317 TS |
847 | |
848 | ga_unset_frozen(ga_state); | |
849 | return i; | |
850 | } | |
851 | ||
852 | static void guest_fsfreeze_cleanup(void) | |
853 | { | |
854 | Error *err = NULL; | |
855 | ||
856 | if (!vss_initialized()) { | |
857 | return; | |
858 | } | |
859 | ||
860 | if (ga_is_frozen(ga_state) == GUEST_FSFREEZE_STATUS_FROZEN) { | |
861 | qmp_guest_fsfreeze_thaw(&err); | |
862 | if (err) { | |
863 | slog("failed to clean up frozen filesystems: %s", | |
864 | error_get_pretty(err)); | |
865 | error_free(err); | |
866 | } | |
867 | } | |
868 | ||
869 | vss_deinit(true); | |
d8ca685a MR |
870 | } |
871 | ||
eab5fd59 PB |
872 | /* |
873 | * Walk list of mounted file systems in the guest, and discard unused | |
874 | * areas. | |
875 | */ | |
e82855d9 JO |
876 | GuestFilesystemTrimResponse * |
877 | qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **errp) | |
eab5fd59 | 878 | { |
91274487 DL |
879 | GuestFilesystemTrimResponse *resp; |
880 | HANDLE handle; | |
881 | WCHAR guid[MAX_PATH] = L""; | |
c5840b90 SJ |
882 | OSVERSIONINFO osvi; |
883 | BOOL win8_or_later; | |
884 | ||
885 | ZeroMemory(&osvi, sizeof(OSVERSIONINFO)); | |
886 | osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); | |
887 | GetVersionEx(&osvi); | |
888 | win8_or_later = (osvi.dwMajorVersion > 6 || | |
889 | ((osvi.dwMajorVersion == 6) && | |
890 | (osvi.dwMinorVersion >= 2))); | |
891 | if (!win8_or_later) { | |
892 | error_setg(errp, "fstrim is only supported for Win8+"); | |
893 | return NULL; | |
894 | } | |
91274487 DL |
895 | |
896 | handle = FindFirstVolumeW(guid, ARRAYSIZE(guid)); | |
897 | if (handle == INVALID_HANDLE_VALUE) { | |
898 | error_setg_win32(errp, GetLastError(), "failed to find any volume"); | |
899 | return NULL; | |
900 | } | |
901 | ||
902 | resp = g_new0(GuestFilesystemTrimResponse, 1); | |
903 | ||
904 | do { | |
905 | GuestFilesystemTrimResult *res; | |
906 | GuestFilesystemTrimResultList *list; | |
907 | PWCHAR uc_path; | |
908 | DWORD char_count = 0; | |
909 | char *path, *out; | |
910 | GError *gerr = NULL; | |
911 | gchar * argv[4]; | |
912 | ||
913 | GetVolumePathNamesForVolumeNameW(guid, NULL, 0, &char_count); | |
914 | ||
915 | if (GetLastError() != ERROR_MORE_DATA) { | |
916 | continue; | |
917 | } | |
918 | if (GetDriveTypeW(guid) != DRIVE_FIXED) { | |
919 | continue; | |
920 | } | |
921 | ||
922 | uc_path = g_malloc(sizeof(WCHAR) * char_count); | |
923 | if (!GetVolumePathNamesForVolumeNameW(guid, uc_path, char_count, | |
924 | &char_count) || !*uc_path) { | |
925 | /* strange, but this condition could be faced even with size == 2 */ | |
926 | g_free(uc_path); | |
927 | continue; | |
928 | } | |
929 | ||
930 | res = g_new0(GuestFilesystemTrimResult, 1); | |
931 | ||
932 | path = g_utf16_to_utf8(uc_path, char_count, NULL, NULL, &gerr); | |
933 | ||
934 | g_free(uc_path); | |
935 | ||
936 | if (!path) { | |
937 | res->has_error = true; | |
938 | res->error = g_strdup(gerr->message); | |
939 | g_error_free(gerr); | |
940 | break; | |
941 | } | |
942 | ||
943 | res->path = path; | |
944 | ||
945 | list = g_new0(GuestFilesystemTrimResultList, 1); | |
946 | list->value = res; | |
947 | list->next = resp->paths; | |
948 | ||
949 | resp->paths = list; | |
950 | ||
951 | memset(argv, 0, sizeof(argv)); | |
952 | argv[0] = (gchar *)"defrag.exe"; | |
953 | argv[1] = (gchar *)"/L"; | |
954 | argv[2] = path; | |
955 | ||
956 | if (!g_spawn_sync(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, | |
957 | &out /* stdout */, NULL /* stdin */, | |
958 | NULL, &gerr)) { | |
959 | res->has_error = true; | |
960 | res->error = g_strdup(gerr->message); | |
961 | g_error_free(gerr); | |
962 | } else { | |
963 | /* defrag.exe is UGLY. Exit code is ALWAYS zero. | |
964 | Error is reported in the output with something like | |
965 | (x89000020) etc code in the stdout */ | |
966 | ||
967 | int i; | |
968 | gchar **lines = g_strsplit(out, "\r\n", 0); | |
969 | g_free(out); | |
970 | ||
971 | for (i = 0; lines[i] != NULL; i++) { | |
972 | if (g_strstr_len(lines[i], -1, "(0x") == NULL) { | |
973 | continue; | |
974 | } | |
975 | res->has_error = true; | |
976 | res->error = g_strdup(lines[i]); | |
977 | break; | |
978 | } | |
979 | g_strfreev(lines); | |
980 | } | |
981 | } while (FindNextVolumeW(handle, guid, ARRAYSIZE(guid))); | |
982 | ||
983 | FindVolumeClose(handle); | |
984 | return resp; | |
eab5fd59 PB |
985 | } |
986 | ||
aa59637e | 987 | typedef enum { |
f54603b6 MR |
988 | GUEST_SUSPEND_MODE_DISK, |
989 | GUEST_SUSPEND_MODE_RAM | |
aa59637e GH |
990 | } GuestSuspendMode; |
991 | ||
77dbc81b | 992 | static void check_suspend_mode(GuestSuspendMode mode, Error **errp) |
aa59637e GH |
993 | { |
994 | SYSTEM_POWER_CAPABILITIES sys_pwr_caps; | |
995 | Error *local_err = NULL; | |
996 | ||
aa59637e GH |
997 | ZeroMemory(&sys_pwr_caps, sizeof(sys_pwr_caps)); |
998 | if (!GetPwrCapabilities(&sys_pwr_caps)) { | |
c6bd8c70 MA |
999 | error_setg(&local_err, QERR_QGA_COMMAND_FAILED, |
1000 | "failed to determine guest suspend capabilities"); | |
aa59637e GH |
1001 | goto out; |
1002 | } | |
1003 | ||
f54603b6 MR |
1004 | switch (mode) { |
1005 | case GUEST_SUSPEND_MODE_DISK: | |
1006 | if (!sys_pwr_caps.SystemS4) { | |
c6bd8c70 MA |
1007 | error_setg(&local_err, QERR_QGA_COMMAND_FAILED, |
1008 | "suspend-to-disk not supported by OS"); | |
aa59637e | 1009 | } |
f54603b6 MR |
1010 | break; |
1011 | case GUEST_SUSPEND_MODE_RAM: | |
1012 | if (!sys_pwr_caps.SystemS3) { | |
c6bd8c70 MA |
1013 | error_setg(&local_err, QERR_QGA_COMMAND_FAILED, |
1014 | "suspend-to-ram not supported by OS"); | |
f54603b6 MR |
1015 | } |
1016 | break; | |
1017 | default: | |
c6bd8c70 MA |
1018 | error_setg(&local_err, QERR_INVALID_PARAMETER_VALUE, "mode", |
1019 | "GuestSuspendMode"); | |
aa59637e GH |
1020 | } |
1021 | ||
aa59637e | 1022 | out: |
621ff94d | 1023 | error_propagate(errp, local_err); |
aa59637e GH |
1024 | } |
1025 | ||
1026 | static DWORD WINAPI do_suspend(LPVOID opaque) | |
1027 | { | |
1028 | GuestSuspendMode *mode = opaque; | |
1029 | DWORD ret = 0; | |
1030 | ||
1031 | if (!SetSuspendState(*mode == GUEST_SUSPEND_MODE_DISK, TRUE, TRUE)) { | |
16f4e8fa | 1032 | slog("failed to suspend guest, %lu", GetLastError()); |
aa59637e GH |
1033 | ret = -1; |
1034 | } | |
1035 | g_free(mode); | |
1036 | return ret; | |
1037 | } | |
1038 | ||
77dbc81b | 1039 | void qmp_guest_suspend_disk(Error **errp) |
11d0f125 | 1040 | { |
0f230bf7 | 1041 | Error *local_err = NULL; |
f3a06403 | 1042 | GuestSuspendMode *mode = g_new(GuestSuspendMode, 1); |
aa59637e GH |
1043 | |
1044 | *mode = GUEST_SUSPEND_MODE_DISK; | |
0f230bf7 MA |
1045 | check_suspend_mode(*mode, &local_err); |
1046 | acquire_privilege(SE_SHUTDOWN_NAME, &local_err); | |
1047 | execute_async(do_suspend, mode, &local_err); | |
aa59637e | 1048 | |
0f230bf7 MA |
1049 | if (local_err) { |
1050 | error_propagate(errp, local_err); | |
aa59637e GH |
1051 | g_free(mode); |
1052 | } | |
11d0f125 LC |
1053 | } |
1054 | ||
77dbc81b | 1055 | void qmp_guest_suspend_ram(Error **errp) |
fbf42210 | 1056 | { |
0f230bf7 | 1057 | Error *local_err = NULL; |
f3a06403 | 1058 | GuestSuspendMode *mode = g_new(GuestSuspendMode, 1); |
f54603b6 MR |
1059 | |
1060 | *mode = GUEST_SUSPEND_MODE_RAM; | |
0f230bf7 MA |
1061 | check_suspend_mode(*mode, &local_err); |
1062 | acquire_privilege(SE_SHUTDOWN_NAME, &local_err); | |
1063 | execute_async(do_suspend, mode, &local_err); | |
f54603b6 | 1064 | |
0f230bf7 MA |
1065 | if (local_err) { |
1066 | error_propagate(errp, local_err); | |
f54603b6 MR |
1067 | g_free(mode); |
1068 | } | |
fbf42210 LC |
1069 | } |
1070 | ||
77dbc81b | 1071 | void qmp_guest_suspend_hybrid(Error **errp) |
95f4f404 | 1072 | { |
c6bd8c70 | 1073 | error_setg(errp, QERR_UNSUPPORTED); |
95f4f404 LC |
1074 | } |
1075 | ||
d6c5528b | 1076 | static IP_ADAPTER_ADDRESSES *guest_get_adapters_addresses(Error **errp) |
3424fc9f | 1077 | { |
d6c5528b KA |
1078 | IP_ADAPTER_ADDRESSES *adptr_addrs = NULL; |
1079 | ULONG adptr_addrs_len = 0; | |
1080 | DWORD ret; | |
1081 | ||
1082 | /* Call the first time to get the adptr_addrs_len. */ | |
1083 | GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, | |
1084 | NULL, adptr_addrs, &adptr_addrs_len); | |
1085 | ||
1086 | adptr_addrs = g_malloc(adptr_addrs_len); | |
1087 | ret = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, | |
1088 | NULL, adptr_addrs, &adptr_addrs_len); | |
1089 | if (ret != ERROR_SUCCESS) { | |
1090 | error_setg_win32(errp, ret, "failed to get adapters addresses"); | |
1091 | g_free(adptr_addrs); | |
1092 | adptr_addrs = NULL; | |
1093 | } | |
1094 | return adptr_addrs; | |
1095 | } | |
1096 | ||
1097 | static char *guest_wctomb_dup(WCHAR *wstr) | |
1098 | { | |
1099 | char *str; | |
1100 | size_t i; | |
1101 | ||
1102 | i = wcslen(wstr) + 1; | |
1103 | str = g_malloc(i); | |
1104 | WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, | |
1105 | wstr, -1, str, i, NULL, NULL); | |
1106 | return str; | |
1107 | } | |
1108 | ||
1109 | static char *guest_addr_to_str(IP_ADAPTER_UNICAST_ADDRESS *ip_addr, | |
1110 | Error **errp) | |
1111 | { | |
1112 | char addr_str[INET6_ADDRSTRLEN + INET_ADDRSTRLEN]; | |
1113 | DWORD len; | |
1114 | int ret; | |
1115 | ||
1116 | if (ip_addr->Address.lpSockaddr->sa_family == AF_INET || | |
1117 | ip_addr->Address.lpSockaddr->sa_family == AF_INET6) { | |
1118 | len = sizeof(addr_str); | |
1119 | ret = WSAAddressToString(ip_addr->Address.lpSockaddr, | |
1120 | ip_addr->Address.iSockaddrLength, | |
1121 | NULL, | |
1122 | addr_str, | |
1123 | &len); | |
1124 | if (ret != 0) { | |
1125 | error_setg_win32(errp, WSAGetLastError(), | |
1126 | "failed address presentation form conversion"); | |
1127 | return NULL; | |
1128 | } | |
1129 | return g_strdup(addr_str); | |
1130 | } | |
3424fc9f MP |
1131 | return NULL; |
1132 | } | |
1133 | ||
d6c5528b KA |
1134 | #if (_WIN32_WINNT >= 0x0600) |
1135 | static int64_t guest_ip_prefix(IP_ADAPTER_UNICAST_ADDRESS *ip_addr) | |
1136 | { | |
1137 | /* For Windows Vista/2008 and newer, use the OnLinkPrefixLength | |
1138 | * field to obtain the prefix. | |
1139 | */ | |
1140 | return ip_addr->OnLinkPrefixLength; | |
1141 | } | |
1142 | #else | |
1143 | /* When using the Windows XP and 2003 build environment, do the best we can to | |
1144 | * figure out the prefix. | |
1145 | */ | |
1146 | static IP_ADAPTER_INFO *guest_get_adapters_info(void) | |
1147 | { | |
1148 | IP_ADAPTER_INFO *adptr_info = NULL; | |
1149 | ULONG adptr_info_len = 0; | |
1150 | DWORD ret; | |
1151 | ||
1152 | /* Call the first time to get the adptr_info_len. */ | |
1153 | GetAdaptersInfo(adptr_info, &adptr_info_len); | |
1154 | ||
1155 | adptr_info = g_malloc(adptr_info_len); | |
1156 | ret = GetAdaptersInfo(adptr_info, &adptr_info_len); | |
1157 | if (ret != ERROR_SUCCESS) { | |
1158 | g_free(adptr_info); | |
1159 | adptr_info = NULL; | |
1160 | } | |
1161 | return adptr_info; | |
1162 | } | |
1163 | ||
1164 | static int64_t guest_ip_prefix(IP_ADAPTER_UNICAST_ADDRESS *ip_addr) | |
1165 | { | |
1166 | int64_t prefix = -1; /* Use for AF_INET6 and unknown/undetermined values. */ | |
1167 | IP_ADAPTER_INFO *adptr_info, *info; | |
1168 | IP_ADDR_STRING *ip; | |
1169 | struct in_addr *p; | |
1170 | ||
1171 | if (ip_addr->Address.lpSockaddr->sa_family != AF_INET) { | |
1172 | return prefix; | |
1173 | } | |
1174 | adptr_info = guest_get_adapters_info(); | |
1175 | if (adptr_info == NULL) { | |
1176 | return prefix; | |
1177 | } | |
1178 | ||
1179 | /* Match up the passed in ip_addr with one found in adaptr_info. | |
1180 | * The matching one in adptr_info will have the netmask. | |
1181 | */ | |
1182 | p = &((struct sockaddr_in *)ip_addr->Address.lpSockaddr)->sin_addr; | |
1183 | for (info = adptr_info; info; info = info->Next) { | |
1184 | for (ip = &info->IpAddressList; ip; ip = ip->Next) { | |
1185 | if (p->S_un.S_addr == inet_addr(ip->IpAddress.String)) { | |
1186 | prefix = ctpop32(inet_addr(ip->IpMask.String)); | |
1187 | goto out; | |
1188 | } | |
1189 | } | |
1190 | } | |
1191 | out: | |
1192 | g_free(adptr_info); | |
1193 | return prefix; | |
1194 | } | |
1195 | #endif | |
1196 | ||
53f9fcb2 ZL |
1197 | #define INTERFACE_PATH_BUF_SZ 512 |
1198 | ||
1199 | static DWORD get_interface_index(const char *guid) | |
1200 | { | |
1201 | ULONG index; | |
1202 | DWORD status; | |
1203 | wchar_t wbuf[INTERFACE_PATH_BUF_SZ]; | |
1204 | snwprintf(wbuf, INTERFACE_PATH_BUF_SZ, L"\\device\\tcpip_%s", guid); | |
1205 | wbuf[INTERFACE_PATH_BUF_SZ - 1] = 0; | |
1206 | status = GetAdapterIndex (wbuf, &index); | |
1207 | if (status != NO_ERROR) { | |
1208 | return (DWORD)~0; | |
1209 | } else { | |
1210 | return index; | |
1211 | } | |
1212 | } | |
df83eabd ZL |
1213 | |
1214 | typedef NETIOAPI_API (WINAPI *GetIfEntry2Func)(PMIB_IF_ROW2 Row); | |
1215 | ||
53f9fcb2 | 1216 | static int guest_get_network_stats(const char *name, |
df83eabd | 1217 | GuestNetworkInterfaceStat *stats) |
53f9fcb2 | 1218 | { |
df83eabd ZL |
1219 | OSVERSIONINFO os_ver; |
1220 | ||
1221 | os_ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); | |
1222 | GetVersionEx(&os_ver); | |
1223 | if (os_ver.dwMajorVersion >= 6) { | |
1224 | MIB_IF_ROW2 a_mid_ifrow; | |
1225 | GetIfEntry2Func getifentry2_ex; | |
1226 | DWORD if_index = 0; | |
1227 | HMODULE module = GetModuleHandle("iphlpapi"); | |
1228 | PVOID func = GetProcAddress(module, "GetIfEntry2"); | |
1229 | ||
1230 | if (func == NULL) { | |
1231 | return -1; | |
1232 | } | |
1233 | ||
1234 | getifentry2_ex = (GetIfEntry2Func)func; | |
1235 | if_index = get_interface_index(name); | |
1236 | if (if_index == (DWORD)~0) { | |
1237 | return -1; | |
1238 | } | |
1239 | ||
1240 | memset(&a_mid_ifrow, 0, sizeof(a_mid_ifrow)); | |
1241 | a_mid_ifrow.InterfaceIndex = if_index; | |
1242 | if (NO_ERROR == getifentry2_ex(&a_mid_ifrow)) { | |
1243 | stats->rx_bytes = a_mid_ifrow.InOctets; | |
1244 | stats->rx_packets = a_mid_ifrow.InUcastPkts; | |
1245 | stats->rx_errs = a_mid_ifrow.InErrors; | |
1246 | stats->rx_dropped = a_mid_ifrow.InDiscards; | |
1247 | stats->tx_bytes = a_mid_ifrow.OutOctets; | |
1248 | stats->tx_packets = a_mid_ifrow.OutUcastPkts; | |
1249 | stats->tx_errs = a_mid_ifrow.OutErrors; | |
1250 | stats->tx_dropped = a_mid_ifrow.OutDiscards; | |
1251 | return 0; | |
1252 | } | |
53f9fcb2 ZL |
1253 | } |
1254 | return -1; | |
1255 | } | |
1256 | ||
d6c5528b KA |
1257 | GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp) |
1258 | { | |
1259 | IP_ADAPTER_ADDRESSES *adptr_addrs, *addr; | |
1260 | IP_ADAPTER_UNICAST_ADDRESS *ip_addr = NULL; | |
1261 | GuestNetworkInterfaceList *head = NULL, *cur_item = NULL; | |
1262 | GuestIpAddressList *head_addr, *cur_addr; | |
1263 | GuestNetworkInterfaceList *info; | |
53f9fcb2 | 1264 | GuestNetworkInterfaceStat *interface_stat = NULL; |
d6c5528b KA |
1265 | GuestIpAddressList *address_item = NULL; |
1266 | unsigned char *mac_addr; | |
1267 | char *addr_str; | |
1268 | WORD wsa_version; | |
1269 | WSADATA wsa_data; | |
1270 | int ret; | |
1271 | ||
1272 | adptr_addrs = guest_get_adapters_addresses(errp); | |
1273 | if (adptr_addrs == NULL) { | |
1274 | return NULL; | |
1275 | } | |
1276 | ||
1277 | /* Make WSA APIs available. */ | |
1278 | wsa_version = MAKEWORD(2, 2); | |
1279 | ret = WSAStartup(wsa_version, &wsa_data); | |
1280 | if (ret != 0) { | |
1281 | error_setg_win32(errp, ret, "failed socket startup"); | |
1282 | goto out; | |
1283 | } | |
1284 | ||
1285 | for (addr = adptr_addrs; addr; addr = addr->Next) { | |
1286 | info = g_malloc0(sizeof(*info)); | |
1287 | ||
1288 | if (cur_item == NULL) { | |
1289 | head = cur_item = info; | |
1290 | } else { | |
1291 | cur_item->next = info; | |
1292 | cur_item = info; | |
1293 | } | |
1294 | ||
1295 | info->value = g_malloc0(sizeof(*info->value)); | |
1296 | info->value->name = guest_wctomb_dup(addr->FriendlyName); | |
1297 | ||
1298 | if (addr->PhysicalAddressLength != 0) { | |
1299 | mac_addr = addr->PhysicalAddress; | |
1300 | ||
1301 | info->value->hardware_address = | |
1302 | g_strdup_printf("%02x:%02x:%02x:%02x:%02x:%02x", | |
1303 | (int) mac_addr[0], (int) mac_addr[1], | |
1304 | (int) mac_addr[2], (int) mac_addr[3], | |
1305 | (int) mac_addr[4], (int) mac_addr[5]); | |
1306 | ||
1307 | info->value->has_hardware_address = true; | |
1308 | } | |
1309 | ||
1310 | head_addr = NULL; | |
1311 | cur_addr = NULL; | |
1312 | for (ip_addr = addr->FirstUnicastAddress; | |
1313 | ip_addr; | |
1314 | ip_addr = ip_addr->Next) { | |
1315 | addr_str = guest_addr_to_str(ip_addr, errp); | |
1316 | if (addr_str == NULL) { | |
1317 | continue; | |
1318 | } | |
1319 | ||
1320 | address_item = g_malloc0(sizeof(*address_item)); | |
1321 | ||
1322 | if (!cur_addr) { | |
1323 | head_addr = cur_addr = address_item; | |
1324 | } else { | |
1325 | cur_addr->next = address_item; | |
1326 | cur_addr = address_item; | |
1327 | } | |
1328 | ||
1329 | address_item->value = g_malloc0(sizeof(*address_item->value)); | |
1330 | address_item->value->ip_address = addr_str; | |
1331 | address_item->value->prefix = guest_ip_prefix(ip_addr); | |
1332 | if (ip_addr->Address.lpSockaddr->sa_family == AF_INET) { | |
1333 | address_item->value->ip_address_type = | |
1334 | GUEST_IP_ADDRESS_TYPE_IPV4; | |
1335 | } else if (ip_addr->Address.lpSockaddr->sa_family == AF_INET6) { | |
1336 | address_item->value->ip_address_type = | |
1337 | GUEST_IP_ADDRESS_TYPE_IPV6; | |
1338 | } | |
1339 | } | |
1340 | if (head_addr) { | |
1341 | info->value->has_ip_addresses = true; | |
1342 | info->value->ip_addresses = head_addr; | |
1343 | } | |
53f9fcb2 ZL |
1344 | if (!info->value->has_statistics) { |
1345 | interface_stat = g_malloc0(sizeof(*interface_stat)); | |
1346 | if (guest_get_network_stats(addr->AdapterName, | |
1347 | interface_stat) == -1) { | |
1348 | info->value->has_statistics = false; | |
1349 | g_free(interface_stat); | |
1350 | } else { | |
1351 | info->value->statistics = interface_stat; | |
1352 | info->value->has_statistics = true; | |
1353 | } | |
1354 | } | |
d6c5528b KA |
1355 | } |
1356 | WSACleanup(); | |
1357 | out: | |
1358 | g_free(adptr_addrs); | |
1359 | return head; | |
1360 | } | |
1361 | ||
6912e6a9 LL |
1362 | int64_t qmp_guest_get_time(Error **errp) |
1363 | { | |
3f2a6087 | 1364 | SYSTEMTIME ts = {0}; |
3f2a6087 LL |
1365 | FILETIME tf; |
1366 | ||
1367 | GetSystemTime(&ts); | |
1368 | if (ts.wYear < 1601 || ts.wYear > 30827) { | |
1369 | error_setg(errp, "Failed to get time"); | |
1370 | return -1; | |
1371 | } | |
1372 | ||
1373 | if (!SystemTimeToFileTime(&ts, &tf)) { | |
1374 | error_setg(errp, "Failed to convert system time: %d", (int)GetLastError()); | |
1375 | return -1; | |
1376 | } | |
1377 | ||
9be38598 | 1378 | return ((((int64_t)tf.dwHighDateTime << 32) | tf.dwLowDateTime) |
3f2a6087 | 1379 | - W32_FT_OFFSET) * 100; |
6912e6a9 LL |
1380 | } |
1381 | ||
2c958923 | 1382 | void qmp_guest_set_time(bool has_time, int64_t time_ns, Error **errp) |
a1bca57f | 1383 | { |
0f230bf7 | 1384 | Error *local_err = NULL; |
b8f954fe LL |
1385 | SYSTEMTIME ts; |
1386 | FILETIME tf; | |
1387 | LONGLONG time; | |
1388 | ||
ee17cbdc MP |
1389 | if (!has_time) { |
1390 | /* Unfortunately, Windows libraries don't provide an easy way to access | |
1391 | * RTC yet: | |
1392 | * | |
1393 | * https://msdn.microsoft.com/en-us/library/aa908981.aspx | |
105fad6b BA |
1394 | * |
1395 | * Instead, a workaround is to use the Windows win32tm command to | |
1396 | * resync the time using the Windows Time service. | |
ee17cbdc | 1397 | */ |
105fad6b BA |
1398 | LPVOID msg_buffer; |
1399 | DWORD ret_flags; | |
1400 | ||
1401 | HRESULT hr = system("w32tm /resync /nowait"); | |
1402 | ||
1403 | if (GetLastError() != 0) { | |
1404 | strerror_s((LPTSTR) & msg_buffer, 0, errno); | |
1405 | error_setg(errp, "system(...) failed: %s", (LPCTSTR)msg_buffer); | |
1406 | } else if (hr != 0) { | |
1407 | if (hr == HRESULT_FROM_WIN32(ERROR_SERVICE_NOT_ACTIVE)) { | |
1408 | error_setg(errp, "Windows Time service not running on the " | |
1409 | "guest"); | |
1410 | } else { | |
1411 | if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | | |
1412 | FORMAT_MESSAGE_FROM_SYSTEM | | |
1413 | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, | |
1414 | (DWORD)hr, MAKELANGID(LANG_NEUTRAL, | |
1415 | SUBLANG_DEFAULT), (LPTSTR) & msg_buffer, 0, | |
1416 | NULL)) { | |
1417 | error_setg(errp, "w32tm failed with error (0x%lx), couldn'" | |
1418 | "t retrieve error message", hr); | |
1419 | } else { | |
1420 | error_setg(errp, "w32tm failed with error (0x%lx): %s", hr, | |
1421 | (LPCTSTR)msg_buffer); | |
1422 | LocalFree(msg_buffer); | |
1423 | } | |
1424 | } | |
1425 | } else if (!InternetGetConnectedState(&ret_flags, 0)) { | |
1426 | error_setg(errp, "No internet connection on guest, sync not " | |
1427 | "accurate"); | |
1428 | } | |
ee17cbdc MP |
1429 | return; |
1430 | } | |
1431 | ||
1432 | /* Validate time passed by user. */ | |
1433 | if (time_ns < 0 || time_ns / 100 > INT64_MAX - W32_FT_OFFSET) { | |
1434 | error_setg(errp, "Time %" PRId64 "is invalid", time_ns); | |
1435 | return; | |
1436 | } | |
b8f954fe | 1437 | |
ee17cbdc | 1438 | time = time_ns / 100 + W32_FT_OFFSET; |
b8f954fe | 1439 | |
ee17cbdc MP |
1440 | tf.dwLowDateTime = (DWORD) time; |
1441 | tf.dwHighDateTime = (DWORD) (time >> 32); | |
b8f954fe | 1442 | |
ee17cbdc MP |
1443 | if (!FileTimeToSystemTime(&tf, &ts)) { |
1444 | error_setg(errp, "Failed to convert system time %d", | |
1445 | (int)GetLastError()); | |
1446 | return; | |
b8f954fe LL |
1447 | } |
1448 | ||
0f230bf7 MA |
1449 | acquire_privilege(SE_SYSTEMTIME_NAME, &local_err); |
1450 | if (local_err) { | |
1451 | error_propagate(errp, local_err); | |
b8f954fe LL |
1452 | return; |
1453 | } | |
1454 | ||
1455 | if (!SetSystemTime(&ts)) { | |
1456 | error_setg(errp, "Failed to set time to guest: %d", (int)GetLastError()); | |
1457 | return; | |
1458 | } | |
a1bca57f LL |
1459 | } |
1460 | ||
70e133a7 LE |
1461 | GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp) |
1462 | { | |
a7a17362 GH |
1463 | PSYSTEM_LOGICAL_PROCESSOR_INFORMATION pslpi, ptr; |
1464 | DWORD length; | |
1465 | GuestLogicalProcessorList *head, **link; | |
1466 | Error *local_err = NULL; | |
1467 | int64_t current; | |
1468 | ||
1469 | ptr = pslpi = NULL; | |
1470 | length = 0; | |
1471 | current = 0; | |
1472 | head = NULL; | |
1473 | link = &head; | |
1474 | ||
1475 | if ((GetLogicalProcessorInformation(pslpi, &length) == FALSE) && | |
1476 | (GetLastError() == ERROR_INSUFFICIENT_BUFFER) && | |
1477 | (length > sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION))) { | |
1478 | ptr = pslpi = g_malloc0(length); | |
1479 | if (GetLogicalProcessorInformation(pslpi, &length) == FALSE) { | |
1480 | error_setg(&local_err, "Failed to get processor information: %d", | |
1481 | (int)GetLastError()); | |
1482 | } | |
1483 | } else { | |
1484 | error_setg(&local_err, | |
1485 | "Failed to get processor information buffer length: %d", | |
1486 | (int)GetLastError()); | |
1487 | } | |
1488 | ||
1489 | while ((local_err == NULL) && (length > 0)) { | |
1490 | if (pslpi->Relationship == RelationProcessorCore) { | |
1491 | ULONG_PTR cpu_bits = pslpi->ProcessorMask; | |
1492 | ||
1493 | while (cpu_bits > 0) { | |
1494 | if (!!(cpu_bits & 1)) { | |
1495 | GuestLogicalProcessor *vcpu; | |
1496 | GuestLogicalProcessorList *entry; | |
1497 | ||
1498 | vcpu = g_malloc0(sizeof *vcpu); | |
1499 | vcpu->logical_id = current++; | |
1500 | vcpu->online = true; | |
54858553 | 1501 | vcpu->has_can_offline = true; |
a7a17362 GH |
1502 | |
1503 | entry = g_malloc0(sizeof *entry); | |
1504 | entry->value = vcpu; | |
1505 | ||
1506 | *link = entry; | |
1507 | link = &entry->next; | |
1508 | } | |
1509 | cpu_bits >>= 1; | |
1510 | } | |
1511 | } | |
1512 | length -= sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); | |
1513 | pslpi++; /* next entry */ | |
1514 | } | |
1515 | ||
1516 | g_free(ptr); | |
1517 | ||
1518 | if (local_err == NULL) { | |
1519 | if (head != NULL) { | |
1520 | return head; | |
1521 | } | |
1522 | /* there's no guest with zero VCPUs */ | |
1523 | error_setg(&local_err, "Guest reported zero VCPUs"); | |
1524 | } | |
1525 | ||
1526 | qapi_free_GuestLogicalProcessorList(head); | |
1527 | error_propagate(errp, local_err); | |
70e133a7 LE |
1528 | return NULL; |
1529 | } | |
1530 | ||
1531 | int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp) | |
1532 | { | |
c6bd8c70 | 1533 | error_setg(errp, QERR_UNSUPPORTED); |
70e133a7 LE |
1534 | return -1; |
1535 | } | |
1536 | ||
259434b8 MAL |
1537 | static gchar * |
1538 | get_net_error_message(gint error) | |
1539 | { | |
1540 | HMODULE module = NULL; | |
1541 | gchar *retval = NULL; | |
1542 | wchar_t *msg = NULL; | |
6771197d MAL |
1543 | int flags; |
1544 | size_t nchars; | |
259434b8 | 1545 | |
02506e2d MAL |
1546 | flags = FORMAT_MESSAGE_ALLOCATE_BUFFER | |
1547 | FORMAT_MESSAGE_IGNORE_INSERTS | | |
1548 | FORMAT_MESSAGE_FROM_SYSTEM; | |
259434b8 MAL |
1549 | |
1550 | if (error >= NERR_BASE && error <= MAX_NERR) { | |
1551 | module = LoadLibraryExW(L"netmsg.dll", NULL, LOAD_LIBRARY_AS_DATAFILE); | |
1552 | ||
1553 | if (module != NULL) { | |
1554 | flags |= FORMAT_MESSAGE_FROM_HMODULE; | |
1555 | } | |
1556 | } | |
1557 | ||
1558 | FormatMessageW(flags, module, error, 0, (LPWSTR)&msg, 0, NULL); | |
1559 | ||
1560 | if (msg != NULL) { | |
1561 | nchars = wcslen(msg); | |
1562 | ||
25d943b9 | 1563 | if (nchars >= 2 && |
6c6916da MAL |
1564 | msg[nchars - 1] == L'\n' && |
1565 | msg[nchars - 2] == L'\r') { | |
1566 | msg[nchars - 2] = L'\0'; | |
259434b8 MAL |
1567 | } |
1568 | ||
1569 | retval = g_utf16_to_utf8(msg, -1, NULL, NULL, NULL); | |
1570 | ||
1571 | LocalFree(msg); | |
1572 | } | |
1573 | ||
1574 | if (module != NULL) { | |
1575 | FreeLibrary(module); | |
1576 | } | |
1577 | ||
1578 | return retval; | |
1579 | } | |
1580 | ||
215a2771 DB |
1581 | void qmp_guest_set_user_password(const char *username, |
1582 | const char *password, | |
1583 | bool crypted, | |
1584 | Error **errp) | |
1585 | { | |
259434b8 MAL |
1586 | NET_API_STATUS nas; |
1587 | char *rawpasswddata = NULL; | |
1588 | size_t rawpasswdlen; | |
8021de10 | 1589 | wchar_t *user = NULL, *wpass = NULL; |
259434b8 | 1590 | USER_INFO_1003 pi1003 = { 0, }; |
8021de10 | 1591 | GError *gerr = NULL; |
259434b8 MAL |
1592 | |
1593 | if (crypted) { | |
1594 | error_setg(errp, QERR_UNSUPPORTED); | |
1595 | return; | |
1596 | } | |
1597 | ||
920639ca DB |
1598 | rawpasswddata = (char *)qbase64_decode(password, -1, &rawpasswdlen, errp); |
1599 | if (!rawpasswddata) { | |
1600 | return; | |
1601 | } | |
259434b8 MAL |
1602 | rawpasswddata = g_renew(char, rawpasswddata, rawpasswdlen + 1); |
1603 | rawpasswddata[rawpasswdlen] = '\0'; | |
1604 | ||
8021de10 MAL |
1605 | user = g_utf8_to_utf16(username, -1, NULL, NULL, &gerr); |
1606 | if (!user) { | |
1607 | goto done; | |
1608 | } | |
1609 | ||
1610 | wpass = g_utf8_to_utf16(rawpasswddata, -1, NULL, NULL, &gerr); | |
1611 | if (!wpass) { | |
1612 | goto done; | |
1613 | } | |
259434b8 MAL |
1614 | |
1615 | pi1003.usri1003_password = wpass; | |
1616 | nas = NetUserSetInfo(NULL, user, | |
1617 | 1003, (LPBYTE)&pi1003, | |
1618 | NULL); | |
1619 | ||
1620 | if (nas != NERR_Success) { | |
1621 | gchar *msg = get_net_error_message(nas); | |
1622 | error_setg(errp, "failed to set password: %s", msg); | |
1623 | g_free(msg); | |
1624 | } | |
1625 | ||
8021de10 MAL |
1626 | done: |
1627 | if (gerr) { | |
1628 | error_setg(errp, QERR_QGA_COMMAND_FAILED, gerr->message); | |
1629 | g_error_free(gerr); | |
1630 | } | |
259434b8 MAL |
1631 | g_free(user); |
1632 | g_free(wpass); | |
1633 | g_free(rawpasswddata); | |
215a2771 DB |
1634 | } |
1635 | ||
a065aaa9 HZ |
1636 | GuestMemoryBlockList *qmp_guest_get_memory_blocks(Error **errp) |
1637 | { | |
c6bd8c70 | 1638 | error_setg(errp, QERR_UNSUPPORTED); |
a065aaa9 HZ |
1639 | return NULL; |
1640 | } | |
1641 | ||
1642 | GuestMemoryBlockResponseList * | |
1643 | qmp_guest_set_memory_blocks(GuestMemoryBlockList *mem_blks, Error **errp) | |
1644 | { | |
c6bd8c70 | 1645 | error_setg(errp, QERR_UNSUPPORTED); |
a065aaa9 HZ |
1646 | return NULL; |
1647 | } | |
1648 | ||
1649 | GuestMemoryBlockInfo *qmp_guest_get_memory_block_info(Error **errp) | |
1650 | { | |
c6bd8c70 | 1651 | error_setg(errp, QERR_UNSUPPORTED); |
a065aaa9 HZ |
1652 | return NULL; |
1653 | } | |
1654 | ||
1281c08a TS |
1655 | /* add unsupported commands to the blacklist */ |
1656 | GList *ga_command_blacklist_init(GList *blacklist) | |
1657 | { | |
1658 | const char *list_unsupported[] = { | |
d6c5528b | 1659 | "guest-suspend-hybrid", |
a7a17362 | 1660 | "guest-set-vcpus", |
0dd38a03 HZ |
1661 | "guest-get-memory-blocks", "guest-set-memory-blocks", |
1662 | "guest-get-memory-block-size", | |
ef0a03f2 | 1663 | "guest-fsfreeze-freeze-list", |
91274487 | 1664 | NULL}; |
1281c08a TS |
1665 | char **p = (char **)list_unsupported; |
1666 | ||
1667 | while (*p) { | |
4bca81ce | 1668 | blacklist = g_list_append(blacklist, g_strdup(*p++)); |
1281c08a TS |
1669 | } |
1670 | ||
1671 | if (!vss_init(true)) { | |
c69403fc | 1672 | g_debug("vss_init failed, vss commands are going to be disabled"); |
1281c08a TS |
1673 | const char *list[] = { |
1674 | "guest-get-fsinfo", "guest-fsfreeze-status", | |
1675 | "guest-fsfreeze-freeze", "guest-fsfreeze-thaw", NULL}; | |
1676 | p = (char **)list; | |
1677 | ||
1678 | while (*p) { | |
4bca81ce | 1679 | blacklist = g_list_append(blacklist, g_strdup(*p++)); |
1281c08a TS |
1680 | } |
1681 | } | |
1682 | ||
1683 | return blacklist; | |
1684 | } | |
1685 | ||
d8ca685a MR |
1686 | /* register init/cleanup routines for stateful command groups */ |
1687 | void ga_command_state_init(GAState *s, GACommandState *cs) | |
1688 | { | |
1281c08a | 1689 | if (!vss_initialized()) { |
64c00317 TS |
1690 | ga_command_state_add(cs, NULL, guest_fsfreeze_cleanup); |
1691 | } | |
d8ca685a | 1692 | } |
161a56a9 VF |
1693 | |
1694 | /* MINGW is missing two fields: IncomingFrames & OutgoingFrames */ | |
1695 | typedef struct _GA_WTSINFOA { | |
1696 | WTS_CONNECTSTATE_CLASS State; | |
1697 | DWORD SessionId; | |
1698 | DWORD IncomingBytes; | |
1699 | DWORD OutgoingBytes; | |
1700 | DWORD IncomingFrames; | |
1701 | DWORD OutgoingFrames; | |
1702 | DWORD IncomingCompressedBytes; | |
1703 | DWORD OutgoingCompressedBy; | |
1704 | CHAR WinStationName[WINSTATIONNAME_LENGTH]; | |
1705 | CHAR Domain[DOMAIN_LENGTH]; | |
1706 | CHAR UserName[USERNAME_LENGTH + 1]; | |
1707 | LARGE_INTEGER ConnectTime; | |
1708 | LARGE_INTEGER DisconnectTime; | |
1709 | LARGE_INTEGER LastInputTime; | |
1710 | LARGE_INTEGER LogonTime; | |
1711 | LARGE_INTEGER CurrentTime; | |
1712 | ||
1713 | } GA_WTSINFOA; | |
1714 | ||
1715 | GuestUserList *qmp_guest_get_users(Error **err) | |
1716 | { | |
1717 | #if (_WIN32_WINNT >= 0x0600) | |
1718 | #define QGA_NANOSECONDS 10000000 | |
1719 | ||
1720 | GHashTable *cache = NULL; | |
1721 | GuestUserList *head = NULL, *cur_item = NULL; | |
1722 | ||
1723 | DWORD buffer_size = 0, count = 0, i = 0; | |
1724 | GA_WTSINFOA *info = NULL; | |
1725 | WTS_SESSION_INFOA *entries = NULL; | |
1726 | GuestUserList *item = NULL; | |
1727 | GuestUser *user = NULL; | |
1728 | gpointer value = NULL; | |
1729 | INT64 login = 0; | |
1730 | double login_time = 0; | |
1731 | ||
1732 | cache = g_hash_table_new(g_str_hash, g_str_equal); | |
1733 | ||
1734 | if (WTSEnumerateSessionsA(NULL, 0, 1, &entries, &count)) { | |
1735 | for (i = 0; i < count; ++i) { | |
1736 | buffer_size = 0; | |
1737 | info = NULL; | |
1738 | if (WTSQuerySessionInformationA( | |
1739 | NULL, | |
1740 | entries[i].SessionId, | |
1741 | WTSSessionInfo, | |
1742 | (LPSTR *)&info, | |
1743 | &buffer_size | |
1744 | )) { | |
1745 | ||
1746 | if (strlen(info->UserName) == 0) { | |
1747 | WTSFreeMemory(info); | |
1748 | continue; | |
1749 | } | |
1750 | ||
1751 | login = info->LogonTime.QuadPart; | |
1752 | login -= W32_FT_OFFSET; | |
1753 | login_time = ((double)login) / QGA_NANOSECONDS; | |
1754 | ||
1755 | if (g_hash_table_contains(cache, info->UserName)) { | |
1756 | value = g_hash_table_lookup(cache, info->UserName); | |
1757 | user = (GuestUser *)value; | |
1758 | if (user->login_time > login_time) { | |
1759 | user->login_time = login_time; | |
1760 | } | |
1761 | } else { | |
1762 | item = g_new0(GuestUserList, 1); | |
1763 | item->value = g_new0(GuestUser, 1); | |
1764 | ||
1765 | item->value->user = g_strdup(info->UserName); | |
1766 | item->value->domain = g_strdup(info->Domain); | |
1767 | item->value->has_domain = true; | |
1768 | ||
1769 | item->value->login_time = login_time; | |
1770 | ||
1771 | g_hash_table_add(cache, item->value->user); | |
1772 | ||
1773 | if (!cur_item) { | |
1774 | head = cur_item = item; | |
1775 | } else { | |
1776 | cur_item->next = item; | |
1777 | cur_item = item; | |
1778 | } | |
1779 | } | |
1780 | } | |
1781 | WTSFreeMemory(info); | |
1782 | } | |
1783 | WTSFreeMemory(entries); | |
1784 | } | |
1785 | g_hash_table_destroy(cache); | |
1786 | return head; | |
1787 | #else | |
1788 | error_setg(err, QERR_UNSUPPORTED); | |
1789 | return NULL; | |
1790 | #endif | |
1791 | } | |
9848f797 TG |
1792 | |
1793 | typedef struct _ga_matrix_lookup_t { | |
1794 | int major; | |
1795 | int minor; | |
1796 | char const *version; | |
1797 | char const *version_id; | |
1798 | } ga_matrix_lookup_t; | |
1799 | ||
1800 | static ga_matrix_lookup_t const WIN_VERSION_MATRIX[2][8] = { | |
1801 | { | |
1802 | /* Desktop editions */ | |
1803 | { 5, 0, "Microsoft Windows 2000", "2000"}, | |
1804 | { 5, 1, "Microsoft Windows XP", "xp"}, | |
1805 | { 6, 0, "Microsoft Windows Vista", "vista"}, | |
1806 | { 6, 1, "Microsoft Windows 7" "7"}, | |
1807 | { 6, 2, "Microsoft Windows 8", "8"}, | |
1808 | { 6, 3, "Microsoft Windows 8.1", "8.1"}, | |
1809 | {10, 0, "Microsoft Windows 10", "10"}, | |
1810 | { 0, 0, 0} | |
1811 | },{ | |
1812 | /* Server editions */ | |
1813 | { 5, 2, "Microsoft Windows Server 2003", "2003"}, | |
1814 | { 6, 0, "Microsoft Windows Server 2008", "2008"}, | |
1815 | { 6, 1, "Microsoft Windows Server 2008 R2", "2008r2"}, | |
1816 | { 6, 2, "Microsoft Windows Server 2012", "2012"}, | |
1817 | { 6, 3, "Microsoft Windows Server 2012 R2", "2012r2"}, | |
1818 | {10, 0, "Microsoft Windows Server 2016", "2016"}, | |
1819 | { 0, 0, 0}, | |
1820 | { 0, 0, 0} | |
1821 | } | |
1822 | }; | |
1823 | ||
1824 | static void ga_get_win_version(RTL_OSVERSIONINFOEXW *info, Error **errp) | |
1825 | { | |
1826 | typedef NTSTATUS(WINAPI * rtl_get_version_t)( | |
1827 | RTL_OSVERSIONINFOEXW *os_version_info_ex); | |
1828 | ||
1829 | info->dwOSVersionInfoSize = sizeof(RTL_OSVERSIONINFOEXW); | |
1830 | ||
1831 | HMODULE module = GetModuleHandle("ntdll"); | |
1832 | PVOID fun = GetProcAddress(module, "RtlGetVersion"); | |
1833 | if (fun == NULL) { | |
1834 | error_setg(errp, QERR_QGA_COMMAND_FAILED, | |
1835 | "Failed to get address of RtlGetVersion"); | |
1836 | return; | |
1837 | } | |
1838 | ||
1839 | rtl_get_version_t rtl_get_version = (rtl_get_version_t)fun; | |
1840 | rtl_get_version(info); | |
1841 | return; | |
1842 | } | |
1843 | ||
1844 | static char *ga_get_win_name(OSVERSIONINFOEXW const *os_version, bool id) | |
1845 | { | |
1846 | DWORD major = os_version->dwMajorVersion; | |
1847 | DWORD minor = os_version->dwMinorVersion; | |
1848 | int tbl_idx = (os_version->wProductType != VER_NT_WORKSTATION); | |
1849 | ga_matrix_lookup_t const *table = WIN_VERSION_MATRIX[tbl_idx]; | |
1850 | while (table->version != NULL) { | |
1851 | if (major == table->major && minor == table->minor) { | |
1852 | if (id) { | |
1853 | return g_strdup(table->version_id); | |
1854 | } else { | |
1855 | return g_strdup(table->version); | |
1856 | } | |
1857 | } | |
1858 | ++table; | |
1859 | } | |
1860 | slog("failed to lookup Windows version: major=%lu, minor=%lu", | |
1861 | major, minor); | |
1862 | return g_strdup("N/A"); | |
1863 | } | |
1864 | ||
1865 | static char *ga_get_win_product_name(Error **errp) | |
1866 | { | |
1867 | HKEY key = NULL; | |
1868 | DWORD size = 128; | |
1869 | char *result = g_malloc0(size); | |
1870 | LONG err = ERROR_SUCCESS; | |
1871 | ||
1872 | err = RegOpenKeyA(HKEY_LOCAL_MACHINE, | |
1873 | "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", | |
1874 | &key); | |
1875 | if (err != ERROR_SUCCESS) { | |
1876 | error_setg_win32(errp, err, "failed to open registry key"); | |
1877 | goto fail; | |
1878 | } | |
1879 | ||
1880 | err = RegQueryValueExA(key, "ProductName", NULL, NULL, | |
1881 | (LPBYTE)result, &size); | |
1882 | if (err == ERROR_MORE_DATA) { | |
1883 | slog("ProductName longer than expected (%lu bytes), retrying", | |
1884 | size); | |
1885 | g_free(result); | |
1886 | result = NULL; | |
1887 | if (size > 0) { | |
1888 | result = g_malloc0(size); | |
1889 | err = RegQueryValueExA(key, "ProductName", NULL, NULL, | |
1890 | (LPBYTE)result, &size); | |
1891 | } | |
1892 | } | |
1893 | if (err != ERROR_SUCCESS) { | |
1894 | error_setg_win32(errp, err, "failed to retrive ProductName"); | |
1895 | goto fail; | |
1896 | } | |
1897 | ||
1898 | return result; | |
1899 | ||
1900 | fail: | |
1901 | g_free(result); | |
1902 | return NULL; | |
1903 | } | |
1904 | ||
1905 | static char *ga_get_current_arch(void) | |
1906 | { | |
1907 | SYSTEM_INFO info; | |
1908 | GetNativeSystemInfo(&info); | |
1909 | char *result = NULL; | |
1910 | switch (info.wProcessorArchitecture) { | |
1911 | case PROCESSOR_ARCHITECTURE_AMD64: | |
1912 | result = g_strdup("x86_64"); | |
1913 | break; | |
1914 | case PROCESSOR_ARCHITECTURE_ARM: | |
1915 | result = g_strdup("arm"); | |
1916 | break; | |
1917 | case PROCESSOR_ARCHITECTURE_IA64: | |
1918 | result = g_strdup("ia64"); | |
1919 | break; | |
1920 | case PROCESSOR_ARCHITECTURE_INTEL: | |
1921 | result = g_strdup("x86"); | |
1922 | break; | |
1923 | case PROCESSOR_ARCHITECTURE_UNKNOWN: | |
1924 | default: | |
1925 | slog("unknown processor architecture 0x%0x", | |
1926 | info.wProcessorArchitecture); | |
1927 | result = g_strdup("unknown"); | |
1928 | break; | |
1929 | } | |
1930 | return result; | |
1931 | } | |
1932 | ||
1933 | GuestOSInfo *qmp_guest_get_osinfo(Error **errp) | |
1934 | { | |
1935 | Error *local_err = NULL; | |
1936 | OSVERSIONINFOEXW os_version = {0}; | |
1937 | bool server; | |
1938 | char *product_name; | |
1939 | GuestOSInfo *info; | |
1940 | ||
1941 | ga_get_win_version(&os_version, &local_err); | |
1942 | if (local_err) { | |
1943 | error_propagate(errp, local_err); | |
1944 | return NULL; | |
1945 | } | |
1946 | ||
1947 | server = os_version.wProductType != VER_NT_WORKSTATION; | |
1948 | product_name = ga_get_win_product_name(&local_err); | |
1949 | if (product_name == NULL) { | |
1950 | error_propagate(errp, local_err); | |
1951 | return NULL; | |
1952 | } | |
1953 | ||
1954 | info = g_new0(GuestOSInfo, 1); | |
1955 | ||
1956 | info->has_kernel_version = true; | |
1957 | info->kernel_version = g_strdup_printf("%lu.%lu", | |
1958 | os_version.dwMajorVersion, | |
1959 | os_version.dwMinorVersion); | |
1960 | info->has_kernel_release = true; | |
1961 | info->kernel_release = g_strdup_printf("%lu", | |
1962 | os_version.dwBuildNumber); | |
1963 | info->has_machine = true; | |
1964 | info->machine = ga_get_current_arch(); | |
1965 | ||
1966 | info->has_id = true; | |
1967 | info->id = g_strdup("mswindows"); | |
1968 | info->has_name = true; | |
1969 | info->name = g_strdup("Microsoft Windows"); | |
1970 | info->has_pretty_name = true; | |
1971 | info->pretty_name = product_name; | |
1972 | info->has_version = true; | |
1973 | info->version = ga_get_win_name(&os_version, false); | |
1974 | info->has_version_id = true; | |
1975 | info->version_id = ga_get_win_name(&os_version, true); | |
1976 | info->has_variant = true; | |
1977 | info->variant = g_strdup(server ? "server" : "client"); | |
1978 | info->has_variant_id = true; | |
1979 | info->variant_id = g_strdup(server ? "server" : "client"); | |
1980 | ||
1981 | return info; | |
1982 | } |