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