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