2012-01-20 05:04:34 +01:00
|
|
|
/*
|
|
|
|
* QEMU Guest Agent win32-specific command implementations
|
|
|
|
*
|
|
|
|
* Copyright IBM Corp. 2012
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Michael Roth <mdroth@linux.vnet.ibm.com>
|
2012-01-29 10:53:31 +01:00
|
|
|
* Gal Hammer <ghammer@redhat.com>
|
2012-01-20 05:04:34 +01:00
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*/
|
2016-01-29 18:49:58 +01:00
|
|
|
#include "qemu/osdep.h"
|
2018-11-22 12:00:38 +01:00
|
|
|
|
2012-01-29 10:53:31 +01:00
|
|
|
#include <wtypes.h>
|
|
|
|
#include <powrprof.h>
|
2015-06-02 19:41:07 +02:00
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#include <iptypes.h>
|
|
|
|
#include <iphlpapi.h>
|
2015-06-30 12:25:22 +02:00
|
|
|
#ifdef CONFIG_QGA_NTDDSCSI
|
|
|
|
#include <winioctl.h>
|
|
|
|
#include <ntddscsi.h>
|
2020-07-21 17:40:41 +02:00
|
|
|
#endif
|
2015-07-08 02:12:18 +02:00
|
|
|
#include <setupapi.h>
|
2019-01-28 23:30:56 +01:00
|
|
|
#include <cfgmgr32.h>
|
2015-07-08 02:12:18 +02:00
|
|
|
#include <initguid.h>
|
2020-07-21 17:40:41 +02:00
|
|
|
#include <devpropdef.h>
|
2015-06-30 16:37:13 +02:00
|
|
|
#include <lm.h>
|
2017-04-19 11:26:15 +02:00
|
|
|
#include <wtsapi32.h>
|
2017-08-22 15:55:04 +02:00
|
|
|
#include <wininet.h>
|
2015-06-30 16:37:13 +02:00
|
|
|
|
2018-05-03 21:50:57 +02:00
|
|
|
#include "guest-agent-core.h"
|
|
|
|
#include "vss-win32.h"
|
2018-02-11 10:36:05 +01:00
|
|
|
#include "qga-qapi-commands.h"
|
2018-02-01 12:18:31 +01:00
|
|
|
#include "qapi/error.h"
|
2012-12-17 18:19:43 +01:00
|
|
|
#include "qapi/qmp/qerror.h"
|
2015-02-06 18:59:55 +01:00
|
|
|
#include "qemu/queue.h"
|
2015-06-02 19:41:07 +02:00
|
|
|
#include "qemu/host-utils.h"
|
2015-11-23 16:37:07 +01:00
|
|
|
#include "qemu/base64.h"
|
2020-04-14 15:30:42 +02:00
|
|
|
#include "commands-common.h"
|
2012-01-20 05:04:34 +01:00
|
|
|
|
2020-07-21 17:40:41 +02:00
|
|
|
/*
|
|
|
|
* The following should be in devpkey.h, but it isn't. The key names were
|
|
|
|
* prefixed to avoid (future) name clashes. Once the definitions get into
|
|
|
|
* mingw the following lines can be removed.
|
|
|
|
*/
|
|
|
|
DEFINE_DEVPROPKEY(qga_DEVPKEY_NAME, 0xb725f130, 0x47ef, 0x101a, 0xa5,
|
|
|
|
0xf1, 0x02, 0x60, 0x8c, 0x9e, 0xeb, 0xac, 10);
|
|
|
|
/* DEVPROP_TYPE_STRING */
|
|
|
|
DEFINE_DEVPROPKEY(qga_DEVPKEY_Device_HardwareIds, 0xa45c254e, 0xdf1c,
|
|
|
|
0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 3);
|
|
|
|
/* DEVPROP_TYPE_STRING_LIST */
|
|
|
|
DEFINE_DEVPROPKEY(qga_DEVPKEY_Device_DriverDate, 0xa8b865dd, 0x2e3d,
|
|
|
|
0x4094, 0xad, 0x97, 0xe5, 0x93, 0xa7, 0xc, 0x75, 0xd6, 2);
|
|
|
|
/* DEVPROP_TYPE_FILETIME */
|
|
|
|
DEFINE_DEVPROPKEY(qga_DEVPKEY_Device_DriverVersion, 0xa8b865dd, 0x2e3d,
|
|
|
|
0x4094, 0xad, 0x97, 0xe5, 0x93, 0xa7, 0xc, 0x75, 0xd6, 3);
|
|
|
|
/* DEVPROP_TYPE_STRING */
|
2020-09-15 12:18:03 +02:00
|
|
|
/* The CM_Get_DevNode_PropertyW prototype is only sometimes in cfgmgr32.h */
|
2020-07-21 17:40:41 +02:00
|
|
|
#ifndef CM_Get_DevNode_Property
|
2020-09-15 12:18:03 +02:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wredundant-decls"
|
2020-07-21 17:40:41 +02:00
|
|
|
CMAPI CONFIGRET WINAPI CM_Get_DevNode_PropertyW(
|
|
|
|
DEVINST dnDevInst,
|
|
|
|
CONST DEVPROPKEY * PropertyKey,
|
|
|
|
DEVPROPTYPE * PropertyType,
|
|
|
|
PBYTE PropertyBuffer,
|
|
|
|
PULONG PropertyBufferSize,
|
|
|
|
ULONG ulFlags
|
|
|
|
);
|
|
|
|
#define CM_Get_DevNode_Property CM_Get_DevNode_PropertyW
|
2020-09-15 12:18:03 +02:00
|
|
|
#pragma GCC diagnostic pop
|
2020-07-21 17:40:41 +02:00
|
|
|
#endif
|
|
|
|
|
2012-01-23 03:24:37 +01:00
|
|
|
#ifndef SHTDN_REASON_FLAG_PLANNED
|
|
|
|
#define SHTDN_REASON_FLAG_PLANNED 0x80000000
|
|
|
|
#endif
|
|
|
|
|
2013-03-15 10:29:04 +01:00
|
|
|
/* multiple of 100 nanoseconds elapsed between windows baseline
|
|
|
|
* (1/1/1601) and Unix Epoch (1/1/1970), accounting for leap years */
|
|
|
|
#define W32_FT_OFFSET (10000000ULL * 60 * 60 * 24 * \
|
|
|
|
(365 * (1970 - 1601) + \
|
|
|
|
(1970 - 1601) / 4 - 3))
|
|
|
|
|
2015-02-06 18:59:55 +01:00
|
|
|
#define INVALID_SET_FILE_POINTER ((DWORD)-1)
|
|
|
|
|
2020-04-14 15:30:42 +02:00
|
|
|
struct GuestFileHandle {
|
2015-02-06 18:59:55 +01:00
|
|
|
int64_t id;
|
|
|
|
HANDLE fh;
|
|
|
|
QTAILQ_ENTRY(GuestFileHandle) next;
|
2020-04-14 15:30:42 +02:00
|
|
|
};
|
2015-02-06 18:59:55 +01:00
|
|
|
|
|
|
|
static struct {
|
|
|
|
QTAILQ_HEAD(, GuestFileHandle) filehandles;
|
2015-10-13 17:41:19 +02:00
|
|
|
} guest_file_state = {
|
|
|
|
.filehandles = QTAILQ_HEAD_INITIALIZER(guest_file_state.filehandles),
|
|
|
|
};
|
2015-02-06 18:59:55 +01:00
|
|
|
|
2015-11-11 00:19:11 +01:00
|
|
|
#define FILE_GENERIC_APPEND (FILE_GENERIC_WRITE & ~FILE_WRITE_DATA)
|
2015-02-06 18:59:55 +01:00
|
|
|
|
|
|
|
typedef struct OpenFlags {
|
|
|
|
const char *forms;
|
|
|
|
DWORD desired_access;
|
|
|
|
DWORD creation_disposition;
|
|
|
|
} OpenFlags;
|
|
|
|
static OpenFlags guest_file_open_modes[] = {
|
2015-11-11 00:19:11 +01:00
|
|
|
{"r", GENERIC_READ, OPEN_EXISTING},
|
|
|
|
{"rb", GENERIC_READ, OPEN_EXISTING},
|
|
|
|
{"w", GENERIC_WRITE, CREATE_ALWAYS},
|
|
|
|
{"wb", GENERIC_WRITE, CREATE_ALWAYS},
|
|
|
|
{"a", FILE_GENERIC_APPEND, OPEN_ALWAYS },
|
2020-10-26 10:05:38 +01:00
|
|
|
{"r+", GENERIC_WRITE | GENERIC_READ, OPEN_EXISTING},
|
|
|
|
{"rb+", GENERIC_WRITE | GENERIC_READ, OPEN_EXISTING},
|
|
|
|
{"r+b", GENERIC_WRITE | GENERIC_READ, OPEN_EXISTING},
|
|
|
|
{"w+", GENERIC_WRITE | GENERIC_READ, CREATE_ALWAYS},
|
|
|
|
{"wb+", GENERIC_WRITE | GENERIC_READ, CREATE_ALWAYS},
|
|
|
|
{"w+b", GENERIC_WRITE | GENERIC_READ, CREATE_ALWAYS},
|
|
|
|
{"a+", FILE_GENERIC_APPEND | GENERIC_READ, OPEN_ALWAYS },
|
|
|
|
{"ab+", FILE_GENERIC_APPEND | GENERIC_READ, OPEN_ALWAYS },
|
|
|
|
{"a+b", FILE_GENERIC_APPEND | GENERIC_READ, OPEN_ALWAYS }
|
2015-02-06 18:59:55 +01:00
|
|
|
};
|
|
|
|
|
2018-10-30 00:42:19 +01:00
|
|
|
#define debug_error(msg) do { \
|
|
|
|
char *suffix = g_win32_error_message(GetLastError()); \
|
|
|
|
g_debug("%s: %s", (msg), suffix); \
|
|
|
|
g_free(suffix); \
|
|
|
|
} while (0)
|
|
|
|
|
2015-02-06 18:59:55 +01:00
|
|
|
static OpenFlags *find_open_flag(const char *mode_str)
|
|
|
|
{
|
|
|
|
int mode;
|
|
|
|
Error **errp = NULL;
|
|
|
|
|
|
|
|
for (mode = 0; mode < ARRAY_SIZE(guest_file_open_modes); ++mode) {
|
|
|
|
OpenFlags *flags = guest_file_open_modes + mode;
|
|
|
|
|
|
|
|
if (strcmp(flags->forms, mode_str) == 0) {
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
error_setg(errp, "invalid file open mode '%s'", mode_str);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int64_t guest_file_handle_add(HANDLE fh, Error **errp)
|
|
|
|
{
|
|
|
|
GuestFileHandle *gfh;
|
|
|
|
int64_t handle;
|
|
|
|
|
|
|
|
handle = ga_get_fd_handle(ga_state, errp);
|
|
|
|
if (handle < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-09-14 13:50:44 +02:00
|
|
|
gfh = g_new0(GuestFileHandle, 1);
|
2015-02-06 18:59:55 +01:00
|
|
|
gfh->id = handle;
|
|
|
|
gfh->fh = fh;
|
|
|
|
QTAILQ_INSERT_TAIL(&guest_file_state.filehandles, gfh, next);
|
|
|
|
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2020-04-14 15:30:42 +02:00
|
|
|
GuestFileHandle *guest_file_handle_find(int64_t id, Error **errp)
|
2015-02-06 18:59:55 +01:00
|
|
|
{
|
|
|
|
GuestFileHandle *gfh;
|
|
|
|
QTAILQ_FOREACH(gfh, &guest_file_state.filehandles, next) {
|
|
|
|
if (gfh->id == id) {
|
|
|
|
return gfh;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
error_setg(errp, "handle '%" PRId64 "' has not been found", id);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-10-28 16:13:57 +01:00
|
|
|
static void handle_set_nonblocking(HANDLE fh)
|
|
|
|
{
|
|
|
|
DWORD file_type, pipe_state;
|
|
|
|
file_type = GetFileType(fh);
|
|
|
|
if (file_type != FILE_TYPE_PIPE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* If file_type == FILE_TYPE_PIPE, according to MSDN
|
|
|
|
* the specified file is socket or named pipe */
|
|
|
|
if (!GetNamedPipeHandleState(fh, &pipe_state, NULL,
|
|
|
|
NULL, NULL, NULL, 0)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* The fd is named pipe fd */
|
|
|
|
if (pipe_state & PIPE_NOWAIT) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pipe_state |= PIPE_NOWAIT;
|
|
|
|
SetNamedPipeHandleState(fh, &pipe_state, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
2015-02-06 18:59:55 +01:00
|
|
|
int64_t qmp_guest_file_open(const char *path, bool has_mode,
|
|
|
|
const char *mode, Error **errp)
|
|
|
|
{
|
2018-08-02 01:10:59 +02:00
|
|
|
int64_t fd = -1;
|
2015-02-06 18:59:55 +01:00
|
|
|
HANDLE fh;
|
|
|
|
HANDLE templ_file = NULL;
|
|
|
|
DWORD share_mode = FILE_SHARE_READ;
|
|
|
|
DWORD flags_and_attr = FILE_ATTRIBUTE_NORMAL;
|
|
|
|
LPSECURITY_ATTRIBUTES sa_attr = NULL;
|
|
|
|
OpenFlags *guest_flags;
|
2018-08-02 01:10:59 +02:00
|
|
|
GError *gerr = NULL;
|
|
|
|
wchar_t *w_path = NULL;
|
2015-02-06 18:59:55 +01:00
|
|
|
|
|
|
|
if (!has_mode) {
|
|
|
|
mode = "r";
|
|
|
|
}
|
|
|
|
slog("guest-file-open called, filepath: %s, mode: %s", path, mode);
|
|
|
|
guest_flags = find_open_flag(mode);
|
|
|
|
if (guest_flags == NULL) {
|
|
|
|
error_setg(errp, "invalid file open mode");
|
2018-08-02 01:10:59 +02:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
w_path = g_utf8_to_utf16(path, -1, NULL, NULL, &gerr);
|
|
|
|
if (!w_path) {
|
|
|
|
goto done;
|
2015-02-06 18:59:55 +01:00
|
|
|
}
|
|
|
|
|
2018-08-02 01:10:59 +02:00
|
|
|
fh = CreateFileW(w_path, guest_flags->desired_access, share_mode, sa_attr,
|
2015-02-06 18:59:55 +01:00
|
|
|
guest_flags->creation_disposition, flags_and_attr,
|
|
|
|
templ_file);
|
|
|
|
if (fh == INVALID_HANDLE_VALUE) {
|
|
|
|
error_setg_win32(errp, GetLastError(), "failed to open file '%s'",
|
|
|
|
path);
|
2018-08-02 01:10:59 +02:00
|
|
|
goto done;
|
2015-02-06 18:59:55 +01:00
|
|
|
}
|
|
|
|
|
2015-10-28 16:13:57 +01:00
|
|
|
/* set fd non-blocking to avoid common use cases (like reading from a
|
|
|
|
* named pipe) from hanging the agent
|
|
|
|
*/
|
|
|
|
handle_set_nonblocking(fh);
|
|
|
|
|
2015-02-06 18:59:55 +01:00
|
|
|
fd = guest_file_handle_add(fh, errp);
|
|
|
|
if (fd < 0) {
|
2015-10-28 16:13:56 +01:00
|
|
|
CloseHandle(fh);
|
2015-02-06 18:59:55 +01:00
|
|
|
error_setg(errp, "failed to add handle to qmp handle table");
|
2018-08-02 01:10:59 +02:00
|
|
|
goto done;
|
2015-02-06 18:59:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
slog("guest-file-open, handle: % " PRId64, fd);
|
2018-08-02 01:10:59 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
if (gerr) {
|
|
|
|
error_setg(errp, QERR_QGA_COMMAND_FAILED, gerr->message);
|
|
|
|
g_error_free(gerr);
|
|
|
|
}
|
|
|
|
g_free(w_path);
|
2015-02-06 18:59:55 +01:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
void qmp_guest_file_close(int64_t handle, Error **errp)
|
|
|
|
{
|
|
|
|
bool ret;
|
|
|
|
GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
|
|
|
|
slog("guest-file-close called, handle: %" PRId64, handle);
|
|
|
|
if (gfh == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ret = CloseHandle(gfh->fh);
|
|
|
|
if (!ret) {
|
|
|
|
error_setg_win32(errp, GetLastError(), "failed close handle");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTAILQ_REMOVE(&guest_file_state.filehandles, gfh, next);
|
|
|
|
g_free(gfh);
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:26:30 +02:00
|
|
|
static void acquire_privilege(const char *name, Error **errp)
|
2012-01-20 05:04:34 +01:00
|
|
|
{
|
2014-05-19 09:26:03 +02:00
|
|
|
HANDLE token = NULL;
|
2012-01-23 03:24:37 +01:00
|
|
|
TOKEN_PRIVILEGES priv;
|
2012-01-29 10:53:31 +01:00
|
|
|
Error *local_err = NULL;
|
|
|
|
|
|
|
|
if (OpenProcessToken(GetCurrentProcess(),
|
2020-10-26 10:05:38 +01:00
|
|
|
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token))
|
2012-01-29 10:53:31 +01:00
|
|
|
{
|
|
|
|
if (!LookupPrivilegeValue(NULL, name, &priv.Privileges[0].Luid)) {
|
2015-03-17 11:54:50 +01:00
|
|
|
error_setg(&local_err, QERR_QGA_COMMAND_FAILED,
|
|
|
|
"no luid for requested privilege");
|
2012-01-29 10:53:31 +01:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv.PrivilegeCount = 1;
|
|
|
|
priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
|
|
|
|
|
|
|
|
if (!AdjustTokenPrivileges(token, FALSE, &priv, 0, NULL, 0)) {
|
2015-03-17 11:54:50 +01:00
|
|
|
error_setg(&local_err, QERR_QGA_COMMAND_FAILED,
|
|
|
|
"unable to acquire requested privilege");
|
2012-01-29 10:53:31 +01:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2015-03-17 11:54:50 +01:00
|
|
|
error_setg(&local_err, QERR_QGA_COMMAND_FAILED,
|
|
|
|
"failed to open privilege token");
|
2012-01-29 10:53:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2014-05-19 09:26:03 +02:00
|
|
|
if (token) {
|
|
|
|
CloseHandle(token);
|
|
|
|
}
|
2016-06-13 23:57:56 +02:00
|
|
|
error_propagate(errp, local_err);
|
2012-01-29 10:53:31 +01:00
|
|
|
}
|
|
|
|
|
2014-05-02 13:26:30 +02:00
|
|
|
static void execute_async(DWORD WINAPI (*func)(LPVOID), LPVOID opaque,
|
|
|
|
Error **errp)
|
2012-01-29 10:53:31 +01:00
|
|
|
{
|
|
|
|
HANDLE thread = CreateThread(NULL, 0, func, opaque, 0, NULL);
|
|
|
|
if (!thread) {
|
error: Avoid unnecessary error_propagate() after error_setg()
Replace
error_setg(&err, ...);
error_propagate(errp, err);
by
error_setg(errp, ...);
Related pattern:
if (...) {
error_setg(&err, ...);
goto out;
}
...
out:
error_propagate(errp, err);
return;
When all paths to label out are that way, replace by
if (...) {
error_setg(errp, ...);
return;
}
and delete the label along with the error_propagate().
When we have at most one other path that actually needs to propagate,
and maybe one at the end that where propagation is unnecessary, e.g.
foo(..., &err);
if (err) {
goto out;
}
...
bar(..., &err);
out:
error_propagate(errp, err);
return;
move the error_propagate() to where it's needed, like
if (...) {
foo(..., &err);
error_propagate(errp, err);
return;
}
...
bar(..., errp);
return;
and transform the error_setg() as above.
In some places, the transformation results in obviously unnecessary
error_propagate(). The next few commits will eliminate them.
Bonus: the elimination of gotos will make later patches in this series
easier to review.
Candidates for conversion tracked down with this Coccinelle script:
@@
identifier err, errp;
expression list args;
@@
- error_setg(&err, args);
+ error_setg(errp, args);
... when != err
error_propagate(errp, err);
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <20200707160613.848843-34-armbru@redhat.com>
2020-07-07 18:06:01 +02:00
|
|
|
error_setg(errp, QERR_QGA_COMMAND_FAILED,
|
2015-03-17 11:54:50 +01:00
|
|
|
"failed to dispatch asynchronous command");
|
2012-01-29 10:53:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:26:30 +02:00
|
|
|
void qmp_guest_shutdown(bool has_mode, const char *mode, Error **errp)
|
2012-01-29 10:53:31 +01:00
|
|
|
{
|
2014-05-02 13:26:38 +02:00
|
|
|
Error *local_err = NULL;
|
2012-01-23 03:24:37 +01:00
|
|
|
UINT shutdown_flag = EWX_FORCE;
|
|
|
|
|
|
|
|
slog("guest-shutdown called, mode: %s", mode);
|
|
|
|
|
|
|
|
if (!has_mode || strcmp(mode, "powerdown") == 0) {
|
|
|
|
shutdown_flag |= EWX_POWEROFF;
|
|
|
|
} else if (strcmp(mode, "halt") == 0) {
|
|
|
|
shutdown_flag |= EWX_SHUTDOWN;
|
|
|
|
} else if (strcmp(mode, "reboot") == 0) {
|
|
|
|
shutdown_flag |= EWX_REBOOT;
|
|
|
|
} else {
|
2015-03-17 11:54:50 +01:00
|
|
|
error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "mode",
|
2020-11-13 09:26:24 +01:00
|
|
|
"'halt', 'powerdown', or 'reboot'");
|
2012-01-23 03:24:37 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Request a shutdown privilege, but try to shut down the system
|
|
|
|
anyway. */
|
2014-05-02 13:26:38 +02:00
|
|
|
acquire_privilege(SE_SHUTDOWN_NAME, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
2012-01-29 10:53:31 +01:00
|
|
|
return;
|
2012-01-23 03:24:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!ExitWindowsEx(shutdown_flag, SHTDN_REASON_FLAG_PLANNED)) {
|
2020-02-28 11:07:25 +01:00
|
|
|
g_autofree gchar *emsg = g_win32_error_message(GetLastError());
|
|
|
|
slog("guest-shutdown failed: %s", emsg);
|
|
|
|
error_setg_win32(errp, GetLastError(), "guest-shutdown failed");
|
2012-01-23 03:24:37 +01:00
|
|
|
}
|
2012-01-20 05:04:34 +01:00
|
|
|
}
|
|
|
|
|
2020-04-14 15:30:43 +02:00
|
|
|
GuestFileRead *guest_file_read_unsafe(GuestFileHandle *gfh,
|
|
|
|
int64_t count, Error **errp)
|
2012-01-20 05:04:34 +01:00
|
|
|
{
|
2015-02-06 18:59:55 +01:00
|
|
|
GuestFileRead *read_data = NULL;
|
|
|
|
guchar *buf;
|
2020-04-14 15:30:43 +02:00
|
|
|
HANDLE fh = gfh->fh;
|
2015-02-06 18:59:55 +01:00
|
|
|
bool is_ok;
|
|
|
|
DWORD read_count;
|
|
|
|
|
Revert "prevent crash when executing guest-file-read with large count"
As noted by Daniel Berrangé in [*], the fix from commit 807e2b6fce
which replaced malloc() by try_malloc() is not enough, the process
can still run out of memory a few line later:
346 buf = g_try_malloc0(count + 1);
347 if (!buf) {
348 error_setg(errp,
349 "failed to allocate sufficient memory "
350 "to complete the requested service");
351 return NULL;
352 }
353 is_ok = ReadFile(fh, buf, count, &read_count, NULL);
354 if (!is_ok) {
355 error_setg_win32(errp, GetLastError(), "failed to read file");
356 slog("guest-file-read failed, handle %" PRId64, handle);
357 } else {
358 buf[read_count] = 0;
359 read_data = g_new0(GuestFileRead, 1);
^^^^^^
Instead we are going to put a low hard limit on 'count' in the next
commits. This reverts commit 807e2b6fce022707418bc8f61c069d91c613b3d2.
[*] https://lists.gnu.org/archive/html/qemu-devel/2018-06/msg03471.html
Suggested-by: Daniel P. Berrangé <berrange@redhat.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2020-04-14 15:30:41 +02:00
|
|
|
buf = g_malloc0(count + 1);
|
2015-02-06 18:59:55 +01:00
|
|
|
is_ok = ReadFile(fh, buf, count, &read_count, NULL);
|
|
|
|
if (!is_ok) {
|
|
|
|
error_setg_win32(errp, GetLastError(), "failed to read file");
|
|
|
|
} else {
|
|
|
|
buf[read_count] = 0;
|
2015-09-14 13:50:44 +02:00
|
|
|
read_data = g_new0(GuestFileRead, 1);
|
2015-02-06 18:59:55 +01:00
|
|
|
read_data->count = (size_t)read_count;
|
|
|
|
read_data->eof = read_count == 0;
|
|
|
|
|
|
|
|
if (read_count != 0) {
|
|
|
|
read_data->buf_b64 = g_base64_encode(buf, read_count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_free(buf);
|
|
|
|
|
|
|
|
return read_data;
|
2012-01-20 05:04:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
GuestFileWrite *qmp_guest_file_write(int64_t handle, const char *buf_b64,
|
2014-05-02 13:26:30 +02:00
|
|
|
bool has_count, int64_t count,
|
|
|
|
Error **errp)
|
2012-01-20 05:04:34 +01:00
|
|
|
{
|
2015-02-06 18:59:55 +01:00
|
|
|
GuestFileWrite *write_data = NULL;
|
|
|
|
guchar *buf;
|
|
|
|
gsize buf_len;
|
|
|
|
bool is_ok;
|
|
|
|
DWORD write_count;
|
|
|
|
GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
|
|
|
|
HANDLE fh;
|
|
|
|
|
|
|
|
if (!gfh) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
fh = gfh->fh;
|
2015-11-23 16:37:07 +01:00
|
|
|
buf = qbase64_decode(buf_b64, -1, &buf_len, errp);
|
|
|
|
if (!buf) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-02-06 18:59:55 +01:00
|
|
|
|
|
|
|
if (!has_count) {
|
|
|
|
count = buf_len;
|
|
|
|
} else if (count < 0 || count > buf_len) {
|
|
|
|
error_setg(errp, "value '%" PRId64
|
|
|
|
"' is invalid for argument count", count);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
is_ok = WriteFile(fh, buf, count, &write_count, NULL);
|
|
|
|
if (!is_ok) {
|
|
|
|
error_setg_win32(errp, GetLastError(), "failed to write to file");
|
|
|
|
slog("guest-file-write-failed, handle: %" PRId64, handle);
|
|
|
|
} else {
|
2015-09-14 13:50:44 +02:00
|
|
|
write_data = g_new0(GuestFileWrite, 1);
|
2015-02-06 18:59:55 +01:00
|
|
|
write_data->count = (size_t) write_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
g_free(buf);
|
|
|
|
return write_data;
|
2012-01-20 05:04:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
GuestFileSeek *qmp_guest_file_seek(int64_t handle, int64_t offset,
|
2016-02-09 22:27:16 +01:00
|
|
|
GuestFileWhence *whence_code,
|
|
|
|
Error **errp)
|
2012-01-20 05:04:34 +01:00
|
|
|
{
|
2015-02-06 18:59:55 +01:00
|
|
|
GuestFileHandle *gfh;
|
|
|
|
GuestFileSeek *seek_data;
|
|
|
|
HANDLE fh;
|
|
|
|
LARGE_INTEGER new_pos, off_pos;
|
|
|
|
off_pos.QuadPart = offset;
|
|
|
|
BOOL res;
|
2015-11-25 18:37:15 +01:00
|
|
|
int whence;
|
2016-02-09 22:27:16 +01:00
|
|
|
Error *err = NULL;
|
2015-11-25 18:37:15 +01:00
|
|
|
|
2015-02-06 18:59:55 +01:00
|
|
|
gfh = guest_file_handle_find(handle, errp);
|
|
|
|
if (!gfh) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-11-25 18:37:15 +01:00
|
|
|
/* We stupidly exposed 'whence':'int' in our qapi */
|
2016-02-09 22:27:16 +01:00
|
|
|
whence = ga_parse_whence(whence_code, &err);
|
|
|
|
if (err) {
|
|
|
|
error_propagate(errp, err);
|
2015-11-25 18:37:15 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-02-06 18:59:55 +01:00
|
|
|
fh = gfh->fh;
|
|
|
|
res = SetFilePointerEx(fh, off_pos, &new_pos, whence);
|
|
|
|
if (!res) {
|
|
|
|
error_setg_win32(errp, GetLastError(), "failed to seek file");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
seek_data = g_new0(GuestFileSeek, 1);
|
|
|
|
seek_data->position = new_pos.QuadPart;
|
|
|
|
return seek_data;
|
2012-01-20 05:04:34 +01:00
|
|
|
}
|
|
|
|
|
2014-05-02 13:26:30 +02:00
|
|
|
void qmp_guest_file_flush(int64_t handle, Error **errp)
|
2012-01-20 05:04:34 +01:00
|
|
|
{
|
2015-02-06 18:59:55 +01:00
|
|
|
HANDLE fh;
|
|
|
|
GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
|
|
|
|
if (!gfh) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fh = gfh->fh;
|
|
|
|
if (!FlushFileBuffers(fh)) {
|
|
|
|
error_setg_win32(errp, GetLastError(), "failed to flush file");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-30 12:25:22 +02:00
|
|
|
#ifdef CONFIG_QGA_NTDDSCSI
|
|
|
|
|
2019-05-07 13:55:03 +02:00
|
|
|
static GuestDiskBusType win2qemu[] = {
|
2015-06-30 12:25:22 +02:00
|
|
|
[BusTypeUnknown] = GUEST_DISK_BUS_TYPE_UNKNOWN,
|
|
|
|
[BusTypeScsi] = GUEST_DISK_BUS_TYPE_SCSI,
|
|
|
|
[BusTypeAtapi] = GUEST_DISK_BUS_TYPE_IDE,
|
|
|
|
[BusTypeAta] = GUEST_DISK_BUS_TYPE_IDE,
|
|
|
|
[BusType1394] = GUEST_DISK_BUS_TYPE_IEEE1394,
|
|
|
|
[BusTypeSsa] = GUEST_DISK_BUS_TYPE_SSA,
|
|
|
|
[BusTypeFibre] = GUEST_DISK_BUS_TYPE_SSA,
|
|
|
|
[BusTypeUsb] = GUEST_DISK_BUS_TYPE_USB,
|
|
|
|
[BusTypeRAID] = GUEST_DISK_BUS_TYPE_RAID,
|
|
|
|
[BusTypeiScsi] = GUEST_DISK_BUS_TYPE_ISCSI,
|
|
|
|
[BusTypeSas] = GUEST_DISK_BUS_TYPE_SAS,
|
|
|
|
[BusTypeSata] = GUEST_DISK_BUS_TYPE_SATA,
|
|
|
|
[BusTypeSd] = GUEST_DISK_BUS_TYPE_SD,
|
|
|
|
[BusTypeMmc] = GUEST_DISK_BUS_TYPE_MMC,
|
|
|
|
#if (_WIN32_WINNT >= 0x0601)
|
|
|
|
[BusTypeVirtual] = GUEST_DISK_BUS_TYPE_VIRTUAL,
|
|
|
|
[BusTypeFileBackedVirtual] = GUEST_DISK_BUS_TYPE_FILE_BACKED_VIRTUAL,
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
static GuestDiskBusType find_bus_type(STORAGE_BUS_TYPE bus)
|
|
|
|
{
|
2018-10-18 04:10:37 +02:00
|
|
|
if (bus >= ARRAY_SIZE(win2qemu) || (int)bus < 0) {
|
2015-06-30 12:25:22 +02:00
|
|
|
return GUEST_DISK_BUS_TYPE_UNKNOWN;
|
|
|
|
}
|
|
|
|
return win2qemu[(int)bus];
|
|
|
|
}
|
|
|
|
|
2018-10-23 13:23:21 +02:00
|
|
|
DEFINE_GUID(GUID_DEVINTERFACE_DISK,
|
|
|
|
0x53f56307L, 0xb6bf, 0x11d0, 0x94, 0xf2,
|
|
|
|
0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
|
2019-01-28 23:30:56 +01:00
|
|
|
DEFINE_GUID(GUID_DEVINTERFACE_STORAGEPORT,
|
|
|
|
0x2accfe60L, 0xc130, 0x11d2, 0xb0, 0x82,
|
|
|
|
0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
|
2018-10-23 13:23:21 +02:00
|
|
|
|
2021-12-14 13:15:27 +01:00
|
|
|
static void get_pci_address_for_device(GuestPCIAddress *pci,
|
|
|
|
HDEVINFO dev_info)
|
|
|
|
{
|
|
|
|
SP_DEVINFO_DATA dev_info_data;
|
|
|
|
DWORD j;
|
|
|
|
DWORD size;
|
|
|
|
bool partial_pci = false;
|
|
|
|
|
|
|
|
dev_info_data.cbSize = sizeof(SP_DEVINFO_DATA);
|
|
|
|
|
|
|
|
for (j = 0;
|
|
|
|
SetupDiEnumDeviceInfo(dev_info, j, &dev_info_data);
|
|
|
|
j++) {
|
|
|
|
DWORD addr, bus, ui_slot, type;
|
|
|
|
int func, slot;
|
|
|
|
size = sizeof(DWORD);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There is no need to allocate buffer in the next functions. The
|
|
|
|
* size is known and ULONG according to
|
|
|
|
* https://msdn.microsoft.com/en-us/library/windows/hardware/ff543095(v=vs.85).aspx
|
|
|
|
*/
|
|
|
|
if (!SetupDiGetDeviceRegistryProperty(
|
|
|
|
dev_info, &dev_info_data, SPDRP_BUSNUMBER,
|
|
|
|
&type, (PBYTE)&bus, size, NULL)) {
|
|
|
|
debug_error("failed to get PCI bus");
|
|
|
|
bus = -1;
|
|
|
|
partial_pci = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The function retrieves the device's address. This value will be
|
|
|
|
* transformed into device function and number
|
|
|
|
*/
|
|
|
|
if (!SetupDiGetDeviceRegistryProperty(
|
|
|
|
dev_info, &dev_info_data, SPDRP_ADDRESS,
|
|
|
|
&type, (PBYTE)&addr, size, NULL)) {
|
|
|
|
debug_error("failed to get PCI address");
|
|
|
|
addr = -1;
|
|
|
|
partial_pci = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This call returns UINumber of DEVICE_CAPABILITIES structure.
|
|
|
|
* This number is typically a user-perceived slot number.
|
|
|
|
*/
|
|
|
|
if (!SetupDiGetDeviceRegistryProperty(
|
|
|
|
dev_info, &dev_info_data, SPDRP_UI_NUMBER,
|
|
|
|
&type, (PBYTE)&ui_slot, size, NULL)) {
|
|
|
|
debug_error("failed to get PCI slot");
|
|
|
|
ui_slot = -1;
|
|
|
|
partial_pci = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SetupApi gives us the same information as driver with
|
|
|
|
* IoGetDeviceProperty. According to Microsoft:
|
|
|
|
*
|
|
|
|
* FunctionNumber = (USHORT)((propertyAddress) & 0x0000FFFF)
|
|
|
|
* DeviceNumber = (USHORT)(((propertyAddress) >> 16) & 0x0000FFFF)
|
|
|
|
* SPDRP_ADDRESS is propertyAddress, so we do the same.
|
|
|
|
*
|
|
|
|
* https://docs.microsoft.com/en-us/windows/desktop/api/setupapi/nf-setupapi-setupdigetdeviceregistrypropertya
|
|
|
|
*/
|
|
|
|
if (partial_pci) {
|
|
|
|
pci->domain = -1;
|
|
|
|
pci->slot = -1;
|
|
|
|
pci->function = -1;
|
|
|
|
pci->bus = -1;
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
func = ((int)addr == -1) ? -1 : addr & 0x0000FFFF;
|
|
|
|
slot = ((int)addr == -1) ? -1 : (addr >> 16) & 0x0000FFFF;
|
|
|
|
if ((int)ui_slot != slot) {
|
|
|
|
g_debug("mismatch with reported slot values: %d vs %d",
|
|
|
|
(int)ui_slot, slot);
|
|
|
|
}
|
|
|
|
pci->domain = 0;
|
|
|
|
pci->slot = (int)ui_slot;
|
|
|
|
pci->function = func;
|
|
|
|
pci->bus = (int)bus;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-28 23:30:56 +01:00
|
|
|
static GuestPCIAddress *get_pci_info(int number, Error **errp)
|
2015-06-30 12:25:22 +02:00
|
|
|
{
|
2021-08-03 11:38:39 +02:00
|
|
|
HDEVINFO dev_info = INVALID_HANDLE_VALUE;
|
2021-08-03 12:26:14 +02:00
|
|
|
HDEVINFO parent_dev_info = INVALID_HANDLE_VALUE;
|
|
|
|
|
2015-07-08 02:12:18 +02:00
|
|
|
SP_DEVINFO_DATA dev_info_data;
|
2019-01-28 23:30:56 +01:00
|
|
|
SP_DEVICE_INTERFACE_DATA dev_iface_data;
|
|
|
|
HANDLE dev_file;
|
2015-07-08 02:12:18 +02:00
|
|
|
int i;
|
|
|
|
GuestPCIAddress *pci = NULL;
|
2019-01-28 23:30:56 +01:00
|
|
|
|
2018-10-23 13:23:13 +02:00
|
|
|
pci = g_malloc0(sizeof(*pci));
|
|
|
|
pci->domain = -1;
|
|
|
|
pci->slot = -1;
|
|
|
|
pci->function = -1;
|
|
|
|
pci->bus = -1;
|
2015-07-08 02:12:18 +02:00
|
|
|
|
2018-10-23 13:23:21 +02:00
|
|
|
dev_info = SetupDiGetClassDevs(&GUID_DEVINTERFACE_DISK, 0, 0,
|
2015-07-08 02:12:18 +02:00
|
|
|
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
|
|
|
|
if (dev_info == INVALID_HANDLE_VALUE) {
|
|
|
|
error_setg_win32(errp, GetLastError(), "failed to get devices tree");
|
2021-08-03 12:11:48 +02:00
|
|
|
goto end;
|
2015-07-08 02:12:18 +02:00
|
|
|
}
|
|
|
|
|
2018-10-30 00:42:19 +01:00
|
|
|
g_debug("enumerating devices");
|
2015-07-08 02:12:18 +02:00
|
|
|
dev_info_data.cbSize = sizeof(SP_DEVINFO_DATA);
|
2019-01-28 23:30:56 +01:00
|
|
|
dev_iface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
|
2015-07-08 02:12:18 +02:00
|
|
|
for (i = 0; SetupDiEnumDeviceInfo(dev_info, i, &dev_info_data); i++) {
|
2019-01-28 23:30:56 +01:00
|
|
|
PSP_DEVICE_INTERFACE_DETAIL_DATA pdev_iface_detail_data = NULL;
|
|
|
|
STORAGE_DEVICE_NUMBER sdn;
|
|
|
|
char *parent_dev_id = NULL;
|
|
|
|
SP_DEVINFO_DATA parent_dev_info_data;
|
|
|
|
DWORD size = 0;
|
|
|
|
|
|
|
|
g_debug("getting device path");
|
|
|
|
if (SetupDiEnumDeviceInterfaces(dev_info, &dev_info_data,
|
|
|
|
&GUID_DEVINTERFACE_DISK, 0,
|
|
|
|
&dev_iface_data)) {
|
|
|
|
while (!SetupDiGetDeviceInterfaceDetail(dev_info, &dev_iface_data,
|
|
|
|
pdev_iface_detail_data,
|
|
|
|
size, &size,
|
|
|
|
&dev_info_data)) {
|
|
|
|
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
|
|
|
|
pdev_iface_detail_data = g_malloc(size);
|
|
|
|
pdev_iface_detail_data->cbSize =
|
|
|
|
sizeof(*pdev_iface_detail_data);
|
|
|
|
} else {
|
|
|
|
error_setg_win32(errp, GetLastError(),
|
|
|
|
"failed to get device interfaces");
|
2021-08-03 12:11:48 +02:00
|
|
|
goto end;
|
2019-01-28 23:30:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dev_file = CreateFile(pdev_iface_detail_data->DevicePath, 0,
|
|
|
|
FILE_SHARE_READ, NULL, OPEN_EXISTING, 0,
|
|
|
|
NULL);
|
|
|
|
g_free(pdev_iface_detail_data);
|
|
|
|
|
|
|
|
if (!DeviceIoControl(dev_file, IOCTL_STORAGE_GET_DEVICE_NUMBER,
|
|
|
|
NULL, 0, &sdn, sizeof(sdn), &size, NULL)) {
|
|
|
|
CloseHandle(dev_file);
|
2015-07-08 02:12:18 +02:00
|
|
|
error_setg_win32(errp, GetLastError(),
|
2019-01-28 23:30:56 +01:00
|
|
|
"failed to get device slot number");
|
2021-08-03 12:11:48 +02:00
|
|
|
goto end;
|
2015-07-08 02:12:18 +02:00
|
|
|
}
|
|
|
|
|
2019-01-28 23:30:56 +01:00
|
|
|
CloseHandle(dev_file);
|
|
|
|
if (sdn.DeviceNumber != number) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
error_setg_win32(errp, GetLastError(),
|
|
|
|
"failed to get device interfaces");
|
2021-08-03 12:11:48 +02:00
|
|
|
goto end;
|
2015-07-08 02:12:18 +02:00
|
|
|
}
|
|
|
|
|
2019-01-28 23:30:56 +01:00
|
|
|
g_debug("found device slot %d. Getting storage controller", number);
|
|
|
|
{
|
|
|
|
CONFIGRET cr;
|
|
|
|
DEVINST dev_inst, parent_dev_inst;
|
|
|
|
ULONG dev_id_size = 0;
|
|
|
|
|
|
|
|
size = 0;
|
|
|
|
while (!SetupDiGetDeviceInstanceId(dev_info, &dev_info_data,
|
|
|
|
parent_dev_id, size, &size)) {
|
|
|
|
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
|
|
|
|
parent_dev_id = g_malloc(size);
|
|
|
|
} else {
|
|
|
|
error_setg_win32(errp, GetLastError(),
|
|
|
|
"failed to get device instance ID");
|
2021-08-03 12:11:48 +02:00
|
|
|
goto end;
|
2019-01-28 23:30:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* CM API used here as opposed to
|
|
|
|
* SetupDiGetDeviceProperty(..., DEVPKEY_Device_Parent, ...)
|
|
|
|
* which exports are only available in mingw-w64 6+
|
|
|
|
*/
|
|
|
|
cr = CM_Locate_DevInst(&dev_inst, parent_dev_id, 0);
|
|
|
|
if (cr != CR_SUCCESS) {
|
|
|
|
g_error("CM_Locate_DevInst failed with code %lx", cr);
|
|
|
|
error_setg_win32(errp, GetLastError(),
|
|
|
|
"failed to get device instance");
|
2021-08-03 12:11:48 +02:00
|
|
|
goto end;
|
2019-01-28 23:30:56 +01:00
|
|
|
}
|
|
|
|
cr = CM_Get_Parent(&parent_dev_inst, dev_inst, 0);
|
|
|
|
if (cr != CR_SUCCESS) {
|
|
|
|
g_error("CM_Get_Parent failed with code %lx", cr);
|
|
|
|
error_setg_win32(errp, GetLastError(),
|
|
|
|
"failed to get parent device instance");
|
2021-08-03 12:11:48 +02:00
|
|
|
goto end;
|
2019-01-28 23:30:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cr = CM_Get_Device_ID_Size(&dev_id_size, parent_dev_inst, 0);
|
|
|
|
if (cr != CR_SUCCESS) {
|
|
|
|
g_error("CM_Get_Device_ID_Size failed with code %lx", cr);
|
|
|
|
error_setg_win32(errp, GetLastError(),
|
|
|
|
"failed to get parent device ID length");
|
2021-08-03 12:11:48 +02:00
|
|
|
goto end;
|
2019-01-28 23:30:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
++dev_id_size;
|
|
|
|
if (dev_id_size > size) {
|
|
|
|
g_free(parent_dev_id);
|
|
|
|
parent_dev_id = g_malloc(dev_id_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
cr = CM_Get_Device_ID(parent_dev_inst, parent_dev_id, dev_id_size,
|
|
|
|
0);
|
|
|
|
if (cr != CR_SUCCESS) {
|
|
|
|
g_error("CM_Get_Device_ID failed with code %lx", cr);
|
|
|
|
error_setg_win32(errp, GetLastError(),
|
|
|
|
"failed to get parent device ID");
|
2021-08-03 12:11:48 +02:00
|
|
|
goto end;
|
2019-01-28 23:30:56 +01:00
|
|
|
}
|
2015-07-08 02:12:18 +02:00
|
|
|
}
|
|
|
|
|
2019-01-28 23:30:56 +01:00
|
|
|
g_debug("querying storage controller %s for PCI information",
|
|
|
|
parent_dev_id);
|
|
|
|
parent_dev_info =
|
|
|
|
SetupDiGetClassDevs(&GUID_DEVINTERFACE_STORAGEPORT, parent_dev_id,
|
|
|
|
NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
|
|
|
|
g_free(parent_dev_id);
|
|
|
|
|
|
|
|
if (parent_dev_info == INVALID_HANDLE_VALUE) {
|
|
|
|
error_setg_win32(errp, GetLastError(),
|
|
|
|
"failed to get parent device");
|
2021-08-03 12:11:48 +02:00
|
|
|
goto end;
|
2015-07-08 02:12:18 +02:00
|
|
|
}
|
|
|
|
|
2019-01-28 23:30:56 +01:00
|
|
|
parent_dev_info_data.cbSize = sizeof(SP_DEVINFO_DATA);
|
|
|
|
if (!SetupDiEnumDeviceInfo(parent_dev_info, 0, &parent_dev_info_data)) {
|
|
|
|
error_setg_win32(errp, GetLastError(),
|
|
|
|
"failed to get parent device data");
|
2021-08-03 12:11:48 +02:00
|
|
|
goto end;
|
2015-07-08 02:12:18 +02:00
|
|
|
}
|
|
|
|
|
2021-12-14 13:15:27 +01:00
|
|
|
get_pci_address_for_device(pci, parent_dev_info);
|
2019-01-28 23:30:56 +01:00
|
|
|
|
2015-07-08 02:12:18 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-06-23 05:17:43 +02:00
|
|
|
|
2021-08-03 12:11:48 +02:00
|
|
|
end:
|
2021-08-03 12:26:14 +02:00
|
|
|
if (parent_dev_info != INVALID_HANDLE_VALUE) {
|
|
|
|
SetupDiDestroyDeviceInfoList(parent_dev_info);
|
|
|
|
}
|
2021-08-03 11:38:39 +02:00
|
|
|
if (dev_info != INVALID_HANDLE_VALUE) {
|
|
|
|
SetupDiDestroyDeviceInfoList(dev_info);
|
|
|
|
}
|
2015-07-08 02:12:18 +02:00
|
|
|
return pci;
|
2015-06-30 12:25:22 +02:00
|
|
|
}
|
|
|
|
|
2018-10-23 13:23:18 +02:00
|
|
|
static void get_disk_properties(HANDLE vol_h, GuestDiskAddress *disk,
|
|
|
|
Error **errp)
|
2015-06-30 12:25:22 +02:00
|
|
|
{
|
|
|
|
STORAGE_PROPERTY_QUERY query;
|
|
|
|
STORAGE_DEVICE_DESCRIPTOR *dev_desc, buf;
|
|
|
|
DWORD received;
|
2018-10-23 13:23:18 +02:00
|
|
|
ULONG size = sizeof(buf);
|
2015-06-30 12:25:22 +02:00
|
|
|
|
|
|
|
dev_desc = &buf;
|
|
|
|
query.PropertyId = StorageDeviceProperty;
|
|
|
|
query.QueryType = PropertyStandardQuery;
|
|
|
|
|
|
|
|
if (!DeviceIoControl(vol_h, IOCTL_STORAGE_QUERY_PROPERTY, &query,
|
|
|
|
sizeof(STORAGE_PROPERTY_QUERY), dev_desc,
|
2018-10-23 13:23:18 +02:00
|
|
|
size, &received, NULL)) {
|
2015-06-30 12:25:22 +02:00
|
|
|
error_setg_win32(errp, GetLastError(), "failed to get bus type");
|
2018-10-23 13:23:18 +02:00
|
|
|
return;
|
2015-06-30 12:25:22 +02:00
|
|
|
}
|
2018-10-23 13:23:18 +02:00
|
|
|
disk->bus_type = find_bus_type(dev_desc->BusType);
|
|
|
|
g_debug("bus type %d", disk->bus_type);
|
2015-06-30 12:25:22 +02:00
|
|
|
|
2018-10-23 13:23:19 +02:00
|
|
|
/* Query once more. Now with long enough buffer. */
|
|
|
|
size = dev_desc->Size;
|
|
|
|
dev_desc = g_malloc0(size);
|
|
|
|
if (!DeviceIoControl(vol_h, IOCTL_STORAGE_QUERY_PROPERTY, &query,
|
|
|
|
sizeof(STORAGE_PROPERTY_QUERY), dev_desc,
|
|
|
|
size, &received, NULL)) {
|
|
|
|
error_setg_win32(errp, GetLastError(), "failed to get serial number");
|
|
|
|
g_debug("failed to get serial number");
|
|
|
|
goto out_free;
|
|
|
|
}
|
|
|
|
if (dev_desc->SerialNumberOffset > 0) {
|
|
|
|
const char *serial;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if (dev_desc->SerialNumberOffset >= received) {
|
|
|
|
error_setg(errp, "failed to get serial number: offset outside the buffer");
|
|
|
|
g_debug("serial number offset outside the buffer");
|
|
|
|
goto out_free;
|
|
|
|
}
|
|
|
|
serial = (char *)dev_desc + dev_desc->SerialNumberOffset;
|
|
|
|
len = received - dev_desc->SerialNumberOffset;
|
|
|
|
g_debug("serial number \"%s\"", serial);
|
|
|
|
if (*serial != 0) {
|
|
|
|
disk->serial = g_strndup(serial, len);
|
|
|
|
disk->has_serial = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out_free:
|
|
|
|
g_free(dev_desc);
|
|
|
|
|
2018-10-23 13:23:18 +02:00
|
|
|
return;
|
2015-06-30 12:25:22 +02:00
|
|
|
}
|
|
|
|
|
2019-01-28 23:30:56 +01:00
|
|
|
static void get_single_disk_info(int disk_number,
|
|
|
|
GuestDiskAddress *disk, Error **errp)
|
2015-06-30 12:25:22 +02:00
|
|
|
{
|
|
|
|
SCSI_ADDRESS addr, *scsi_ad;
|
|
|
|
DWORD len;
|
2018-10-23 13:23:21 +02:00
|
|
|
HANDLE disk_h;
|
2018-10-23 13:23:14 +02:00
|
|
|
Error *local_err = NULL;
|
2015-06-30 12:25:22 +02:00
|
|
|
|
|
|
|
scsi_ad = &addr;
|
|
|
|
|
2018-10-23 13:23:22 +02:00
|
|
|
g_debug("getting disk info for: %s", disk->dev);
|
|
|
|
disk_h = CreateFile(disk->dev, 0, FILE_SHARE_READ, NULL, OPEN_EXISTING,
|
2015-06-30 12:25:22 +02:00
|
|
|
0, NULL);
|
2018-10-23 13:23:21 +02:00
|
|
|
if (disk_h == INVALID_HANDLE_VALUE) {
|
|
|
|
error_setg_win32(errp, GetLastError(), "failed to open disk");
|
|
|
|
return;
|
2015-06-30 12:25:22 +02:00
|
|
|
}
|
|
|
|
|
2018-10-23 13:23:21 +02:00
|
|
|
get_disk_properties(disk_h, disk, &local_err);
|
2018-10-23 13:23:18 +02:00
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
goto err_close;
|
2015-06-30 12:25:22 +02:00
|
|
|
}
|
|
|
|
|
2018-10-30 00:42:19 +01:00
|
|
|
g_debug("bus type %d", disk->bus_type);
|
2018-10-23 13:23:14 +02:00
|
|
|
/* always set pci_controller as required by schema. get_pci_info() should
|
|
|
|
* report -1 values for non-PCI buses rather than fail. fail the command
|
|
|
|
* if that doesn't hold since that suggests some other unexpected
|
|
|
|
* breakage
|
|
|
|
*/
|
2019-01-28 23:30:56 +01:00
|
|
|
disk->pci_controller = get_pci_info(disk_number, &local_err);
|
2018-10-23 13:23:14 +02:00
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
2018-10-23 13:23:18 +02:00
|
|
|
goto err_close;
|
2018-10-23 13:23:14 +02:00
|
|
|
}
|
2018-10-23 13:23:18 +02:00
|
|
|
if (disk->bus_type == GUEST_DISK_BUS_TYPE_SCSI
|
|
|
|
|| disk->bus_type == GUEST_DISK_BUS_TYPE_IDE
|
|
|
|
|| disk->bus_type == GUEST_DISK_BUS_TYPE_RAID
|
2015-06-30 12:25:22 +02:00
|
|
|
/* This bus type is not supported before Windows Server 2003 SP1 */
|
2018-10-23 13:23:18 +02:00
|
|
|
|| disk->bus_type == GUEST_DISK_BUS_TYPE_SAS
|
2015-06-30 12:25:22 +02:00
|
|
|
) {
|
|
|
|
/* We are able to use the same ioctls for different bus types
|
|
|
|
* according to Microsoft docs
|
|
|
|
* https://technet.microsoft.com/en-us/library/ee851589(v=ws.10).aspx */
|
2019-01-28 23:30:56 +01:00
|
|
|
g_debug("getting SCSI info");
|
2018-10-23 13:23:21 +02:00
|
|
|
if (DeviceIoControl(disk_h, IOCTL_SCSI_GET_ADDRESS, NULL, 0, scsi_ad,
|
2015-06-30 12:25:22 +02:00
|
|
|
sizeof(SCSI_ADDRESS), &len, NULL)) {
|
|
|
|
disk->unit = addr.Lun;
|
|
|
|
disk->target = addr.TargetId;
|
|
|
|
disk->bus = addr.PathId;
|
|
|
|
}
|
|
|
|
/* We do not set error in this case, because we still have enough
|
|
|
|
* information about volume. */
|
|
|
|
}
|
|
|
|
|
2018-10-23 13:23:18 +02:00
|
|
|
err_close:
|
2018-10-23 13:23:21 +02:00
|
|
|
CloseHandle(disk_h);
|
2018-10-23 13:23:20 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* VSS provider works with volumes, thus there is no difference if
|
|
|
|
* the volume consist of spanned disks. Info about the first disk in the
|
|
|
|
* volume is returned for the spanned disk group (LVM) */
|
|
|
|
static GuestDiskAddressList *build_guest_disk_info(char *guid, Error **errp)
|
|
|
|
{
|
|
|
|
Error *local_err = NULL;
|
2020-11-13 02:13:37 +01:00
|
|
|
GuestDiskAddressList *list = NULL;
|
2018-10-23 13:23:20 +02:00
|
|
|
GuestDiskAddress *disk = NULL;
|
2018-10-23 13:23:21 +02:00
|
|
|
int i;
|
|
|
|
HANDLE vol_h;
|
|
|
|
DWORD size;
|
|
|
|
PVOLUME_DISK_EXTENTS extents = NULL;
|
2018-10-23 13:23:20 +02:00
|
|
|
|
|
|
|
/* strip final backslash */
|
|
|
|
char *name = g_strdup(guid);
|
|
|
|
if (g_str_has_suffix(name, "\\")) {
|
|
|
|
name[strlen(name) - 1] = 0;
|
|
|
|
}
|
|
|
|
|
2018-10-23 13:23:21 +02:00
|
|
|
g_debug("opening %s", name);
|
|
|
|
vol_h = CreateFile(name, 0, FILE_SHARE_READ, NULL, OPEN_EXISTING,
|
|
|
|
0, NULL);
|
|
|
|
if (vol_h == INVALID_HANDLE_VALUE) {
|
|
|
|
error_setg_win32(errp, GetLastError(), "failed to open volume");
|
2018-10-23 13:23:20 +02:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2018-10-23 13:23:21 +02:00
|
|
|
/* Get list of extents */
|
|
|
|
g_debug("getting disk extents");
|
|
|
|
size = sizeof(VOLUME_DISK_EXTENTS);
|
|
|
|
extents = g_malloc0(size);
|
|
|
|
if (!DeviceIoControl(vol_h, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, NULL,
|
2019-01-28 23:30:56 +01:00
|
|
|
0, extents, size, &size, NULL)) {
|
2018-10-23 13:23:21 +02:00
|
|
|
DWORD last_err = GetLastError();
|
|
|
|
if (last_err == ERROR_MORE_DATA) {
|
|
|
|
/* Try once more with big enough buffer */
|
|
|
|
g_free(extents);
|
|
|
|
extents = g_malloc0(size);
|
|
|
|
if (!DeviceIoControl(
|
|
|
|
vol_h, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, NULL,
|
|
|
|
0, extents, size, NULL, NULL)) {
|
|
|
|
error_setg_win32(errp, GetLastError(),
|
|
|
|
"failed to get disk extents");
|
2018-11-03 14:01:43 +01:00
|
|
|
goto out;
|
2018-10-23 13:23:21 +02:00
|
|
|
}
|
|
|
|
} else if (last_err == ERROR_INVALID_FUNCTION) {
|
|
|
|
/* Possibly CD-ROM or a shared drive. Try to pass the volume */
|
|
|
|
g_debug("volume not on disk");
|
|
|
|
disk = g_malloc0(sizeof(GuestDiskAddress));
|
2018-10-23 13:23:22 +02:00
|
|
|
disk->has_dev = true;
|
|
|
|
disk->dev = g_strdup(name);
|
2019-01-28 23:30:56 +01:00
|
|
|
get_single_disk_info(0xffffffff, disk, &local_err);
|
2018-10-23 13:23:21 +02:00
|
|
|
if (local_err) {
|
|
|
|
g_debug("failed to get disk info, ignoring error: %s",
|
|
|
|
error_get_pretty(local_err));
|
|
|
|
error_free(local_err);
|
|
|
|
goto out;
|
|
|
|
}
|
2020-11-13 02:13:37 +01:00
|
|
|
QAPI_LIST_PREPEND(list, disk);
|
2018-10-23 13:23:21 +02:00
|
|
|
disk = NULL;
|
|
|
|
goto out;
|
|
|
|
} else {
|
|
|
|
error_setg_win32(errp, GetLastError(),
|
|
|
|
"failed to get disk extents");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_debug("Number of extents: %lu", extents->NumberOfDiskExtents);
|
|
|
|
|
|
|
|
/* Go through each extent */
|
|
|
|
for (i = 0; i < extents->NumberOfDiskExtents; i++) {
|
|
|
|
disk = g_malloc0(sizeof(GuestDiskAddress));
|
|
|
|
|
|
|
|
/* Disk numbers directly correspond to numbers used in UNCs
|
|
|
|
*
|
|
|
|
* See documentation for DISK_EXTENT:
|
|
|
|
* https://docs.microsoft.com/en-us/windows/desktop/api/winioctl/ns-winioctl-_disk_extent
|
|
|
|
*
|
|
|
|
* See also Naming Files, Paths and Namespaces:
|
|
|
|
* https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#win32-device-namespaces
|
|
|
|
*/
|
2018-10-23 13:23:22 +02:00
|
|
|
disk->has_dev = true;
|
|
|
|
disk->dev = g_strdup_printf("\\\\.\\PhysicalDrive%lu",
|
2019-01-28 23:30:56 +01:00
|
|
|
extents->Extents[i].DiskNumber);
|
2018-10-23 13:23:22 +02:00
|
|
|
|
2019-01-28 23:30:56 +01:00
|
|
|
get_single_disk_info(extents->Extents[i].DiskNumber, disk, &local_err);
|
2018-10-23 13:23:21 +02:00
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
goto out;
|
|
|
|
}
|
2020-11-13 02:13:37 +01:00
|
|
|
QAPI_LIST_PREPEND(list, disk);
|
2018-10-23 13:23:21 +02:00
|
|
|
disk = NULL;
|
|
|
|
}
|
|
|
|
|
2018-10-23 13:23:20 +02:00
|
|
|
|
|
|
|
out:
|
2018-11-03 14:01:43 +01:00
|
|
|
if (vol_h != INVALID_HANDLE_VALUE) {
|
|
|
|
CloseHandle(vol_h);
|
|
|
|
}
|
2018-10-23 13:23:20 +02:00
|
|
|
qapi_free_GuestDiskAddress(disk);
|
2018-10-23 13:23:21 +02:00
|
|
|
g_free(extents);
|
2018-10-23 13:23:18 +02:00
|
|
|
g_free(name);
|
|
|
|
|
2018-10-23 13:23:20 +02:00
|
|
|
return list;
|
2015-06-30 12:25:22 +02:00
|
|
|
}
|
|
|
|
|
2020-10-12 10:36:03 +02:00
|
|
|
GuestDiskInfoList *qmp_guest_get_disks(Error **errp)
|
|
|
|
{
|
2020-11-13 02:13:37 +01:00
|
|
|
GuestDiskInfoList *ret = NULL;
|
2020-10-12 10:36:03 +02:00
|
|
|
HDEVINFO dev_info;
|
|
|
|
SP_DEVICE_INTERFACE_DATA dev_iface_data;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
dev_info = SetupDiGetClassDevs(&GUID_DEVINTERFACE_DISK, 0, 0,
|
|
|
|
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
|
|
|
|
if (dev_info == INVALID_HANDLE_VALUE) {
|
|
|
|
error_setg_win32(errp, GetLastError(), "failed to get device tree");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_debug("enumerating devices");
|
|
|
|
dev_iface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
|
|
|
|
for (i = 0;
|
|
|
|
SetupDiEnumDeviceInterfaces(dev_info, NULL, &GUID_DEVINTERFACE_DISK,
|
|
|
|
i, &dev_iface_data);
|
|
|
|
i++) {
|
|
|
|
GuestDiskAddress *address = NULL;
|
|
|
|
GuestDiskInfo *disk = NULL;
|
|
|
|
Error *local_err = NULL;
|
|
|
|
g_autofree PSP_DEVICE_INTERFACE_DETAIL_DATA
|
|
|
|
pdev_iface_detail_data = NULL;
|
|
|
|
STORAGE_DEVICE_NUMBER sdn;
|
|
|
|
HANDLE dev_file;
|
|
|
|
DWORD size = 0;
|
|
|
|
BOOL result;
|
|
|
|
int attempt;
|
|
|
|
|
|
|
|
g_debug(" getting device path");
|
|
|
|
for (attempt = 0, result = FALSE; attempt < 2 && !result; attempt++) {
|
|
|
|
result = SetupDiGetDeviceInterfaceDetail(dev_info,
|
|
|
|
&dev_iface_data, pdev_iface_detail_data, size, &size, NULL);
|
|
|
|
if (result) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
|
|
|
|
pdev_iface_detail_data = g_realloc(pdev_iface_detail_data,
|
|
|
|
size);
|
|
|
|
pdev_iface_detail_data->cbSize =
|
|
|
|
sizeof(*pdev_iface_detail_data);
|
|
|
|
} else {
|
|
|
|
g_debug("failed to get device interface details");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!result) {
|
|
|
|
g_debug("skipping device");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_debug(" device: %s", pdev_iface_detail_data->DevicePath);
|
|
|
|
dev_file = CreateFile(pdev_iface_detail_data->DevicePath, 0,
|
|
|
|
FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
|
|
|
|
if (!DeviceIoControl(dev_file, IOCTL_STORAGE_GET_DEVICE_NUMBER,
|
|
|
|
NULL, 0, &sdn, sizeof(sdn), &size, NULL)) {
|
|
|
|
CloseHandle(dev_file);
|
|
|
|
debug_error("failed to get storage device number");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
CloseHandle(dev_file);
|
|
|
|
|
|
|
|
disk = g_new0(GuestDiskInfo, 1);
|
|
|
|
disk->name = g_strdup_printf("\\\\.\\PhysicalDrive%lu",
|
|
|
|
sdn.DeviceNumber);
|
|
|
|
|
|
|
|
g_debug(" number: %lu", sdn.DeviceNumber);
|
|
|
|
address = g_malloc0(sizeof(GuestDiskAddress));
|
|
|
|
address->has_dev = true;
|
|
|
|
address->dev = g_strdup(disk->name);
|
|
|
|
get_single_disk_info(sdn.DeviceNumber, address, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
g_debug("failed to get disk info: %s",
|
|
|
|
error_get_pretty(local_err));
|
|
|
|
error_free(local_err);
|
|
|
|
qapi_free_GuestDiskAddress(address);
|
|
|
|
address = NULL;
|
|
|
|
} else {
|
|
|
|
disk->address = address;
|
|
|
|
disk->has_address = true;
|
|
|
|
}
|
|
|
|
|
2020-11-13 02:13:37 +01:00
|
|
|
QAPI_LIST_PREPEND(ret, disk);
|
2020-10-12 10:36:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SetupDiDestroyDeviceInfoList(dev_info);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-06-30 12:25:22 +02:00
|
|
|
#else
|
|
|
|
|
|
|
|
static GuestDiskAddressList *build_guest_disk_info(char *guid, Error **errp)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-10-12 10:36:03 +02:00
|
|
|
GuestDiskInfoList *qmp_guest_get_disks(Error **errp)
|
|
|
|
{
|
|
|
|
error_setg(errp, QERR_UNSUPPORTED);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-06-30 12:25:22 +02:00
|
|
|
#endif /* CONFIG_QGA_NTDDSCSI */
|
|
|
|
|
2015-06-30 12:25:21 +02:00
|
|
|
static GuestFilesystemInfo *build_guest_fsinfo(char *guid, Error **errp)
|
|
|
|
{
|
|
|
|
DWORD info_size;
|
|
|
|
char mnt, *mnt_point;
|
2020-04-30 14:11:40 +02:00
|
|
|
wchar_t wfs_name[32];
|
2015-06-30 12:25:21 +02:00
|
|
|
char fs_name[32];
|
2020-04-30 14:11:40 +02:00
|
|
|
wchar_t vol_info[MAX_PATH + 1];
|
2015-06-30 12:25:21 +02:00
|
|
|
size_t len;
|
2018-06-14 10:06:07 +02:00
|
|
|
uint64_t i64FreeBytesToCaller, i64TotalBytes, i64FreeBytes;
|
2015-06-30 12:25:21 +02:00
|
|
|
GuestFilesystemInfo *fs = NULL;
|
2021-07-12 18:15:08 +02:00
|
|
|
HANDLE hLocalDiskHandle = INVALID_HANDLE_VALUE;
|
2015-06-30 12:25:21 +02:00
|
|
|
|
|
|
|
GetVolumePathNamesForVolumeName(guid, (LPCH)&mnt, 0, &info_size);
|
|
|
|
if (GetLastError() != ERROR_MORE_DATA) {
|
|
|
|
error_setg_win32(errp, GetLastError(), "failed to get volume name");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
mnt_point = g_malloc(info_size + 1);
|
|
|
|
if (!GetVolumePathNamesForVolumeName(guid, mnt_point, info_size,
|
|
|
|
&info_size)) {
|
|
|
|
error_setg_win32(errp, GetLastError(), "failed to get volume name");
|
|
|
|
goto free;
|
|
|
|
}
|
|
|
|
|
2020-04-30 14:11:40 +02:00
|
|
|
hLocalDiskHandle = CreateFile(guid, 0 , 0, NULL, OPEN_EXISTING,
|
|
|
|
FILE_ATTRIBUTE_NORMAL |
|
|
|
|
FILE_FLAG_BACKUP_SEMANTICS, NULL);
|
|
|
|
if (INVALID_HANDLE_VALUE == hLocalDiskHandle) {
|
|
|
|
error_setg_win32(errp, GetLastError(), "failed to get handle for volume");
|
|
|
|
goto free;
|
|
|
|
}
|
|
|
|
|
2015-06-30 12:25:21 +02:00
|
|
|
len = strlen(mnt_point);
|
|
|
|
mnt_point[len] = '\\';
|
2020-10-26 10:05:38 +01:00
|
|
|
mnt_point[len + 1] = 0;
|
2020-04-30 14:11:40 +02:00
|
|
|
|
|
|
|
if (!GetVolumeInformationByHandleW(hLocalDiskHandle, vol_info,
|
|
|
|
sizeof(vol_info), NULL, NULL, NULL,
|
|
|
|
(LPWSTR) & wfs_name, sizeof(wfs_name))) {
|
2015-06-30 12:25:21 +02:00
|
|
|
if (GetLastError() != ERROR_NOT_READY) {
|
|
|
|
error_setg_win32(errp, GetLastError(), "failed to get volume info");
|
|
|
|
}
|
|
|
|
goto free;
|
|
|
|
}
|
|
|
|
|
|
|
|
fs = g_malloc(sizeof(*fs));
|
|
|
|
fs->name = g_strdup(guid);
|
2018-06-14 10:06:07 +02:00
|
|
|
fs->has_total_bytes = false;
|
|
|
|
fs->has_used_bytes = false;
|
2015-06-30 12:25:21 +02:00
|
|
|
if (len == 0) {
|
|
|
|
fs->mountpoint = g_strdup("System Reserved");
|
|
|
|
} else {
|
|
|
|
fs->mountpoint = g_strndup(mnt_point, len);
|
2018-06-14 10:06:07 +02:00
|
|
|
if (GetDiskFreeSpaceEx(fs->mountpoint,
|
|
|
|
(PULARGE_INTEGER) & i64FreeBytesToCaller,
|
|
|
|
(PULARGE_INTEGER) & i64TotalBytes,
|
|
|
|
(PULARGE_INTEGER) & i64FreeBytes)) {
|
|
|
|
fs->used_bytes = i64TotalBytes - i64FreeBytes;
|
|
|
|
fs->total_bytes = i64TotalBytes;
|
|
|
|
fs->has_total_bytes = true;
|
|
|
|
fs->has_used_bytes = true;
|
|
|
|
}
|
2015-06-30 12:25:21 +02:00
|
|
|
}
|
2020-04-30 14:11:40 +02:00
|
|
|
wcstombs(fs_name, wfs_name, sizeof(wfs_name));
|
2015-06-30 12:25:21 +02:00
|
|
|
fs->type = g_strdup(fs_name);
|
2015-08-26 13:17:12 +02:00
|
|
|
fs->disk = build_guest_disk_info(guid, errp);
|
2015-06-30 12:25:21 +02:00
|
|
|
free:
|
2021-07-12 18:15:08 +02:00
|
|
|
if (hLocalDiskHandle != INVALID_HANDLE_VALUE) {
|
|
|
|
CloseHandle(hLocalDiskHandle);
|
|
|
|
}
|
2015-06-30 12:25:21 +02:00
|
|
|
g_free(mnt_point);
|
|
|
|
return fs;
|
|
|
|
}
|
|
|
|
|
qga: Add guest-get-fsinfo command
Add command to get mounted filesystems information in the guest.
The returned value contains a list of mountpoint paths and
corresponding disks info such as disk bus type, drive address,
and the disk controllers' PCI addresses, so that management layer
such as libvirt can resolve the disk backends.
For example, when `lsblk' result is:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 1G 0 disk
`-sdb1 8:17 0 1024M 0 part
`-vg0-lv0 253:1 0 1.4G 0 lvm /mnt/test
sdc 8:32 0 1G 0 disk
`-sdc1 8:33 0 512M 0 part
`-vg0-lv0 253:1 0 1.4G 0 lvm /mnt/test
vda 252:0 0 25G 0 disk
`-vda1 252:1 0 25G 0 part /
where sdb is a SCSI disk with PCI controller 0000:00:0a.0 and ID=1,
sdc is an IDE disk with PCI controller 0000:00:01.1, and
vda is a virtio-blk disk with PCI device 0000:00:06.0,
guest-get-fsinfo command will return the following result:
{"return":
[{"name":"dm-1",
"mountpoint":"/mnt/test",
"disk":[
{"bus-type":"scsi","bus":0,"unit":1,"target":0,
"pci-controller":{"bus":0,"slot":10,"domain":0,"function":0}},
{"bus-type":"ide","bus":0,"unit":0,"target":0,
"pci-controller":{"bus":0,"slot":1,"domain":0,"function":1}}],
"type":"xfs"},
{"name":"vda1", "mountpoint":"/",
"disk":[
{"bus-type":"virtio","bus":0,"unit":0,"target":0,
"pci-controller":{"bus":0,"slot":6,"domain":0,"function":0}}],
"type":"ext4"}]}
In Linux guest, the disk information is resolved from sysfs. So far,
it only supports virtio-blk, virtio-scsi, IDE, SATA, SCSI disks on x86
hosts, and "disk" parameter may be empty for unsupported disk types.
Signed-off-by: Tomoki Sekiyama <tomoki.sekiyama@hds.com>
*updated schema to report 2.2 as initial supported version
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2014-06-30 23:51:34 +02:00
|
|
|
GuestFilesystemInfoList *qmp_guest_get_fsinfo(Error **errp)
|
|
|
|
{
|
2015-06-30 12:25:20 +02:00
|
|
|
HANDLE vol_h;
|
2020-11-13 02:13:37 +01:00
|
|
|
GuestFilesystemInfoList *ret = NULL;
|
2015-06-30 12:25:20 +02:00
|
|
|
char guid[256];
|
|
|
|
|
|
|
|
vol_h = FindFirstVolume(guid, sizeof(guid));
|
|
|
|
if (vol_h == INVALID_HANDLE_VALUE) {
|
|
|
|
error_setg_win32(errp, GetLastError(), "failed to find any volume");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2020-04-30 14:11:40 +02:00
|
|
|
Error *local_err = NULL;
|
|
|
|
GuestFilesystemInfo *info = build_guest_fsinfo(guid, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
g_debug("failed to get filesystem info, ignoring error: %s",
|
|
|
|
error_get_pretty(local_err));
|
|
|
|
error_free(local_err);
|
2015-06-30 12:25:21 +02:00
|
|
|
continue;
|
|
|
|
}
|
2020-11-13 02:13:37 +01:00
|
|
|
QAPI_LIST_PREPEND(ret, info);
|
2015-06-30 12:25:20 +02:00
|
|
|
} while (FindNextVolume(vol_h, guid, sizeof(guid)));
|
|
|
|
|
|
|
|
if (GetLastError() != ERROR_NO_MORE_FILES) {
|
|
|
|
error_setg_win32(errp, GetLastError(), "failed to find next volume");
|
|
|
|
}
|
|
|
|
|
|
|
|
FindVolumeClose(vol_h);
|
|
|
|
return ret;
|
qga: Add guest-get-fsinfo command
Add command to get mounted filesystems information in the guest.
The returned value contains a list of mountpoint paths and
corresponding disks info such as disk bus type, drive address,
and the disk controllers' PCI addresses, so that management layer
such as libvirt can resolve the disk backends.
For example, when `lsblk' result is:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 1G 0 disk
`-sdb1 8:17 0 1024M 0 part
`-vg0-lv0 253:1 0 1.4G 0 lvm /mnt/test
sdc 8:32 0 1G 0 disk
`-sdc1 8:33 0 512M 0 part
`-vg0-lv0 253:1 0 1.4G 0 lvm /mnt/test
vda 252:0 0 25G 0 disk
`-vda1 252:1 0 25G 0 part /
where sdb is a SCSI disk with PCI controller 0000:00:0a.0 and ID=1,
sdc is an IDE disk with PCI controller 0000:00:01.1, and
vda is a virtio-blk disk with PCI device 0000:00:06.0,
guest-get-fsinfo command will return the following result:
{"return":
[{"name":"dm-1",
"mountpoint":"/mnt/test",
"disk":[
{"bus-type":"scsi","bus":0,"unit":1,"target":0,
"pci-controller":{"bus":0,"slot":10,"domain":0,"function":0}},
{"bus-type":"ide","bus":0,"unit":0,"target":0,
"pci-controller":{"bus":0,"slot":1,"domain":0,"function":1}}],
"type":"xfs"},
{"name":"vda1", "mountpoint":"/",
"disk":[
{"bus-type":"virtio","bus":0,"unit":0,"target":0,
"pci-controller":{"bus":0,"slot":6,"domain":0,"function":0}}],
"type":"ext4"}]}
In Linux guest, the disk information is resolved from sysfs. So far,
it only supports virtio-blk, virtio-scsi, IDE, SATA, SCSI disks on x86
hosts, and "disk" parameter may be empty for unsupported disk types.
Signed-off-by: Tomoki Sekiyama <tomoki.sekiyama@hds.com>
*updated schema to report 2.2 as initial supported version
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2014-06-30 23:51:34 +02:00
|
|
|
}
|
|
|
|
|
2012-01-20 05:04:34 +01:00
|
|
|
/*
|
|
|
|
* Return status of freeze/thaw
|
|
|
|
*/
|
2014-05-02 13:26:30 +02:00
|
|
|
GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **errp)
|
2012-01-20 05:04:34 +01:00
|
|
|
{
|
2013-08-07 17:40:25 +02:00
|
|
|
if (!vss_initialized()) {
|
2015-03-17 11:54:50 +01:00
|
|
|
error_setg(errp, QERR_UNSUPPORTED);
|
2013-08-07 17:40:25 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ga_is_frozen(ga_state)) {
|
|
|
|
return GUEST_FSFREEZE_STATUS_FROZEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GUEST_FSFREEZE_STATUS_THAWED;
|
2012-01-20 05:04:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-08-07 17:40:25 +02:00
|
|
|
* Freeze local file systems using Volume Shadow-copy Service.
|
|
|
|
* The frozen state is limited for up to 10 seconds by VSS.
|
2012-01-20 05:04:34 +01:00
|
|
|
*/
|
2014-05-02 13:26:30 +02:00
|
|
|
int64_t qmp_guest_fsfreeze_freeze(Error **errp)
|
2018-08-31 08:22:50 +02:00
|
|
|
{
|
|
|
|
return qmp_guest_fsfreeze_freeze_list(false, NULL, errp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints,
|
|
|
|
strList *mountpoints,
|
|
|
|
Error **errp)
|
2012-01-20 05:04:34 +01:00
|
|
|
{
|
2013-08-07 17:40:25 +02:00
|
|
|
int i;
|
|
|
|
Error *local_err = NULL;
|
|
|
|
|
|
|
|
if (!vss_initialized()) {
|
2015-03-17 11:54:50 +01:00
|
|
|
error_setg(errp, QERR_UNSUPPORTED);
|
2013-08-07 17:40:25 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
slog("guest-fsfreeze called");
|
|
|
|
|
|
|
|
/* cannot risk guest agent blocking itself on a write in this state */
|
|
|
|
ga_set_frozen(ga_state);
|
|
|
|
|
2018-08-31 08:22:50 +02:00
|
|
|
qga_vss_fsfreeze(&i, true, mountpoints, &local_err);
|
2014-05-02 13:26:38 +02:00
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
2013-08-07 17:40:25 +02:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
|
|
|
|
error:
|
2014-05-02 13:26:38 +02:00
|
|
|
local_err = NULL;
|
2013-08-07 17:40:25 +02:00
|
|
|
qmp_guest_fsfreeze_thaw(&local_err);
|
2014-01-30 15:07:28 +01:00
|
|
|
if (local_err) {
|
2013-08-07 17:40:25 +02:00
|
|
|
g_debug("cleanup thaw: %s", error_get_pretty(local_err));
|
|
|
|
error_free(local_err);
|
|
|
|
}
|
2012-01-20 05:04:34 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-08-07 17:40:25 +02:00
|
|
|
* Thaw local file systems using Volume Shadow-copy Service.
|
2012-01-20 05:04:34 +01:00
|
|
|
*/
|
2014-05-02 13:26:30 +02:00
|
|
|
int64_t qmp_guest_fsfreeze_thaw(Error **errp)
|
2012-01-20 05:04:34 +01:00
|
|
|
{
|
2013-08-07 17:40:25 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!vss_initialized()) {
|
2015-03-17 11:54:50 +01:00
|
|
|
error_setg(errp, QERR_UNSUPPORTED);
|
2013-08-07 17:40:25 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-08-31 08:22:50 +02:00
|
|
|
qga_vss_fsfreeze(&i, false, NULL, errp);
|
2013-08-07 17:40:25 +02:00
|
|
|
|
|
|
|
ga_unset_frozen(ga_state);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void guest_fsfreeze_cleanup(void)
|
|
|
|
{
|
|
|
|
Error *err = NULL;
|
|
|
|
|
|
|
|
if (!vss_initialized()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ga_is_frozen(ga_state) == GUEST_FSFREEZE_STATUS_FROZEN) {
|
|
|
|
qmp_guest_fsfreeze_thaw(&err);
|
|
|
|
if (err) {
|
|
|
|
slog("failed to clean up frozen filesystems: %s",
|
|
|
|
error_get_pretty(err));
|
|
|
|
error_free(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vss_deinit(true);
|
2012-01-20 05:04:34 +01:00
|
|
|
}
|
|
|
|
|
2012-06-13 07:41:28 +02:00
|
|
|
/*
|
|
|
|
* Walk list of mounted file systems in the guest, and discard unused
|
|
|
|
* areas.
|
|
|
|
*/
|
2015-05-11 08:58:45 +02:00
|
|
|
GuestFilesystemTrimResponse *
|
|
|
|
qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **errp)
|
2012-06-13 07:41:28 +02:00
|
|
|
{
|
2016-10-03 16:01:25 +02:00
|
|
|
GuestFilesystemTrimResponse *resp;
|
|
|
|
HANDLE handle;
|
|
|
|
WCHAR guid[MAX_PATH] = L"";
|
2018-06-24 14:45:40 +02:00
|
|
|
OSVERSIONINFO osvi;
|
|
|
|
BOOL win8_or_later;
|
|
|
|
|
|
|
|
ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
|
|
|
|
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
|
|
|
|
GetVersionEx(&osvi);
|
|
|
|
win8_or_later = (osvi.dwMajorVersion > 6 ||
|
|
|
|
((osvi.dwMajorVersion == 6) &&
|
|
|
|
(osvi.dwMinorVersion >= 2)));
|
|
|
|
if (!win8_or_later) {
|
|
|
|
error_setg(errp, "fstrim is only supported for Win8+");
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-10-03 16:01:25 +02:00
|
|
|
|
|
|
|
handle = FindFirstVolumeW(guid, ARRAYSIZE(guid));
|
|
|
|
if (handle == INVALID_HANDLE_VALUE) {
|
|
|
|
error_setg_win32(errp, GetLastError(), "failed to find any volume");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
resp = g_new0(GuestFilesystemTrimResponse, 1);
|
|
|
|
|
|
|
|
do {
|
|
|
|
GuestFilesystemTrimResult *res;
|
|
|
|
PWCHAR uc_path;
|
|
|
|
DWORD char_count = 0;
|
|
|
|
char *path, *out;
|
|
|
|
GError *gerr = NULL;
|
2020-10-26 10:05:41 +01:00
|
|
|
gchar *argv[4];
|
2016-10-03 16:01:25 +02:00
|
|
|
|
|
|
|
GetVolumePathNamesForVolumeNameW(guid, NULL, 0, &char_count);
|
|
|
|
|
|
|
|
if (GetLastError() != ERROR_MORE_DATA) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (GetDriveTypeW(guid) != DRIVE_FIXED) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
uc_path = g_malloc(sizeof(WCHAR) * char_count);
|
|
|
|
if (!GetVolumePathNamesForVolumeNameW(guid, uc_path, char_count,
|
|
|
|
&char_count) || !*uc_path) {
|
|
|
|
/* strange, but this condition could be faced even with size == 2 */
|
|
|
|
g_free(uc_path);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = g_new0(GuestFilesystemTrimResult, 1);
|
|
|
|
|
|
|
|
path = g_utf16_to_utf8(uc_path, char_count, NULL, NULL, &gerr);
|
|
|
|
|
|
|
|
g_free(uc_path);
|
|
|
|
|
|
|
|
if (!path) {
|
|
|
|
res->has_error = true;
|
|
|
|
res->error = g_strdup(gerr->message);
|
|
|
|
g_error_free(gerr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
res->path = path;
|
|
|
|
|
2020-11-13 02:13:37 +01:00
|
|
|
QAPI_LIST_PREPEND(resp->paths, res);
|
2016-10-03 16:01:25 +02:00
|
|
|
|
|
|
|
memset(argv, 0, sizeof(argv));
|
|
|
|
argv[0] = (gchar *)"defrag.exe";
|
|
|
|
argv[1] = (gchar *)"/L";
|
|
|
|
argv[2] = path;
|
|
|
|
|
|
|
|
if (!g_spawn_sync(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
|
|
|
|
&out /* stdout */, NULL /* stdin */,
|
|
|
|
NULL, &gerr)) {
|
|
|
|
res->has_error = true;
|
|
|
|
res->error = g_strdup(gerr->message);
|
|
|
|
g_error_free(gerr);
|
|
|
|
} else {
|
|
|
|
/* defrag.exe is UGLY. Exit code is ALWAYS zero.
|
|
|
|
Error is reported in the output with something like
|
|
|
|
(x89000020) etc code in the stdout */
|
|
|
|
|
|
|
|
int i;
|
|
|
|
gchar **lines = g_strsplit(out, "\r\n", 0);
|
|
|
|
g_free(out);
|
|
|
|
|
|
|
|
for (i = 0; lines[i] != NULL; i++) {
|
|
|
|
if (g_strstr_len(lines[i], -1, "(0x") == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
res->has_error = true;
|
|
|
|
res->error = g_strdup(lines[i]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
g_strfreev(lines);
|
|
|
|
}
|
|
|
|
} while (FindNextVolumeW(handle, guid, ARRAYSIZE(guid)));
|
|
|
|
|
|
|
|
FindVolumeClose(handle);
|
|
|
|
return resp;
|
2012-06-13 07:41:28 +02:00
|
|
|
}
|
|
|
|
|
2012-01-29 10:53:31 +01:00
|
|
|
typedef enum {
|
2012-03-12 18:50:02 +01:00
|
|
|
GUEST_SUSPEND_MODE_DISK,
|
|
|
|
GUEST_SUSPEND_MODE_RAM
|
2012-01-29 10:53:31 +01:00
|
|
|
} GuestSuspendMode;
|
|
|
|
|
2014-05-02 13:26:30 +02:00
|
|
|
static void check_suspend_mode(GuestSuspendMode mode, Error **errp)
|
2012-01-29 10:53:31 +01:00
|
|
|
{
|
|
|
|
SYSTEM_POWER_CAPABILITIES sys_pwr_caps;
|
|
|
|
|
|
|
|
ZeroMemory(&sys_pwr_caps, sizeof(sys_pwr_caps));
|
|
|
|
if (!GetPwrCapabilities(&sys_pwr_caps)) {
|
error: Avoid unnecessary error_propagate() after error_setg()
Replace
error_setg(&err, ...);
error_propagate(errp, err);
by
error_setg(errp, ...);
Related pattern:
if (...) {
error_setg(&err, ...);
goto out;
}
...
out:
error_propagate(errp, err);
return;
When all paths to label out are that way, replace by
if (...) {
error_setg(errp, ...);
return;
}
and delete the label along with the error_propagate().
When we have at most one other path that actually needs to propagate,
and maybe one at the end that where propagation is unnecessary, e.g.
foo(..., &err);
if (err) {
goto out;
}
...
bar(..., &err);
out:
error_propagate(errp, err);
return;
move the error_propagate() to where it's needed, like
if (...) {
foo(..., &err);
error_propagate(errp, err);
return;
}
...
bar(..., errp);
return;
and transform the error_setg() as above.
In some places, the transformation results in obviously unnecessary
error_propagate(). The next few commits will eliminate them.
Bonus: the elimination of gotos will make later patches in this series
easier to review.
Candidates for conversion tracked down with this Coccinelle script:
@@
identifier err, errp;
expression list args;
@@
- error_setg(&err, args);
+ error_setg(errp, args);
... when != err
error_propagate(errp, err);
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <20200707160613.848843-34-armbru@redhat.com>
2020-07-07 18:06:01 +02:00
|
|
|
error_setg(errp, QERR_QGA_COMMAND_FAILED,
|
2015-03-17 11:54:50 +01:00
|
|
|
"failed to determine guest suspend capabilities");
|
error: Avoid unnecessary error_propagate() after error_setg()
Replace
error_setg(&err, ...);
error_propagate(errp, err);
by
error_setg(errp, ...);
Related pattern:
if (...) {
error_setg(&err, ...);
goto out;
}
...
out:
error_propagate(errp, err);
return;
When all paths to label out are that way, replace by
if (...) {
error_setg(errp, ...);
return;
}
and delete the label along with the error_propagate().
When we have at most one other path that actually needs to propagate,
and maybe one at the end that where propagation is unnecessary, e.g.
foo(..., &err);
if (err) {
goto out;
}
...
bar(..., &err);
out:
error_propagate(errp, err);
return;
move the error_propagate() to where it's needed, like
if (...) {
foo(..., &err);
error_propagate(errp, err);
return;
}
...
bar(..., errp);
return;
and transform the error_setg() as above.
In some places, the transformation results in obviously unnecessary
error_propagate(). The next few commits will eliminate them.
Bonus: the elimination of gotos will make later patches in this series
easier to review.
Candidates for conversion tracked down with this Coccinelle script:
@@
identifier err, errp;
expression list args;
@@
- error_setg(&err, args);
+ error_setg(errp, args);
... when != err
error_propagate(errp, err);
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <20200707160613.848843-34-armbru@redhat.com>
2020-07-07 18:06:01 +02:00
|
|
|
return;
|
2012-01-29 10:53:31 +01:00
|
|
|
}
|
|
|
|
|
2012-03-12 18:50:02 +01:00
|
|
|
switch (mode) {
|
|
|
|
case GUEST_SUSPEND_MODE_DISK:
|
|
|
|
if (!sys_pwr_caps.SystemS4) {
|
error: Avoid unnecessary error_propagate() after error_setg()
Replace
error_setg(&err, ...);
error_propagate(errp, err);
by
error_setg(errp, ...);
Related pattern:
if (...) {
error_setg(&err, ...);
goto out;
}
...
out:
error_propagate(errp, err);
return;
When all paths to label out are that way, replace by
if (...) {
error_setg(errp, ...);
return;
}
and delete the label along with the error_propagate().
When we have at most one other path that actually needs to propagate,
and maybe one at the end that where propagation is unnecessary, e.g.
foo(..., &err);
if (err) {
goto out;
}
...
bar(..., &err);
out:
error_propagate(errp, err);
return;
move the error_propagate() to where it's needed, like
if (...) {
foo(..., &err);
error_propagate(errp, err);
return;
}
...
bar(..., errp);
return;
and transform the error_setg() as above.
In some places, the transformation results in obviously unnecessary
error_propagate(). The next few commits will eliminate them.
Bonus: the elimination of gotos will make later patches in this series
easier to review.
Candidates for conversion tracked down with this Coccinelle script:
@@
identifier err, errp;
expression list args;
@@
- error_setg(&err, args);
+ error_setg(errp, args);
... when != err
error_propagate(errp, err);
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <20200707160613.848843-34-armbru@redhat.com>
2020-07-07 18:06:01 +02:00
|
|
|
error_setg(errp, QERR_QGA_COMMAND_FAILED,
|
2015-03-17 11:54:50 +01:00
|
|
|
"suspend-to-disk not supported by OS");
|
2012-01-29 10:53:31 +01:00
|
|
|
}
|
2012-03-12 18:50:02 +01:00
|
|
|
break;
|
|
|
|
case GUEST_SUSPEND_MODE_RAM:
|
|
|
|
if (!sys_pwr_caps.SystemS3) {
|
error: Avoid unnecessary error_propagate() after error_setg()
Replace
error_setg(&err, ...);
error_propagate(errp, err);
by
error_setg(errp, ...);
Related pattern:
if (...) {
error_setg(&err, ...);
goto out;
}
...
out:
error_propagate(errp, err);
return;
When all paths to label out are that way, replace by
if (...) {
error_setg(errp, ...);
return;
}
and delete the label along with the error_propagate().
When we have at most one other path that actually needs to propagate,
and maybe one at the end that where propagation is unnecessary, e.g.
foo(..., &err);
if (err) {
goto out;
}
...
bar(..., &err);
out:
error_propagate(errp, err);
return;
move the error_propagate() to where it's needed, like
if (...) {
foo(..., &err);
error_propagate(errp, err);
return;
}
...
bar(..., errp);
return;
and transform the error_setg() as above.
In some places, the transformation results in obviously unnecessary
error_propagate(). The next few commits will eliminate them.
Bonus: the elimination of gotos will make later patches in this series
easier to review.
Candidates for conversion tracked down with this Coccinelle script:
@@
identifier err, errp;
expression list args;
@@
- error_setg(&err, args);
+ error_setg(errp, args);
... when != err
error_propagate(errp, err);
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <20200707160613.848843-34-armbru@redhat.com>
2020-07-07 18:06:01 +02:00
|
|
|
error_setg(errp, QERR_QGA_COMMAND_FAILED,
|
2015-03-17 11:54:50 +01:00
|
|
|
"suspend-to-ram not supported by OS");
|
2012-03-12 18:50:02 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2020-11-13 09:26:23 +01:00
|
|
|
abort();
|
2012-01-29 10:53:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static DWORD WINAPI do_suspend(LPVOID opaque)
|
|
|
|
{
|
|
|
|
GuestSuspendMode *mode = opaque;
|
|
|
|
DWORD ret = 0;
|
|
|
|
|
|
|
|
if (!SetSuspendState(*mode == GUEST_SUSPEND_MODE_DISK, TRUE, TRUE)) {
|
2020-02-28 11:07:25 +01:00
|
|
|
g_autofree gchar *emsg = g_win32_error_message(GetLastError());
|
|
|
|
slog("failed to suspend guest: %s", emsg);
|
2012-01-29 10:53:31 +01:00
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
g_free(mode);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:26:30 +02:00
|
|
|
void qmp_guest_suspend_disk(Error **errp)
|
qemu-ga: add guest-suspend-disk
As the command name implies, this command suspends the guest to disk.
The suspend operation is implemented by two functions: bios_supports_mode()
and guest_suspend(). Both functions are generic enough to be used by
other suspend modes (introduced by next commits).
Both functions will try to use the scripts provided by the pm-utils
package if it's available. If it's not available, a manual method,
which consists of directly writing to '/sys/power/state', will be used.
To reap terminated children, a new signal handler is installed in the
parent to catch SIGCHLD signals and a non-blocking call to waitpid()
is done to collect their exit statuses. The statuses, however, are
discarded.
The approach used to query the guest for suspend support deserves some
explanation. It's implemented by bios_supports_mode() and shown below:
qemu-ga
|
create pipe
|
fork()
-----------------
| |
| |
| fork()
| --------------------------
| | |
| | |
| | exec('pm-is-supported')
| |
| wait()
| write exit status to pipe
| exit
|
read pipe
This might look complex, but the resulting code is quite simple.
The purpose of that approach is to allow qemu-ga to reap its children
(semi-)automatically from its SIGCHLD handler.
Implementing this the obvious way, that's, doing the exec() call from
the first child process, would force us to introduce a more complex way
to reap qemu-ga's children. Like registering PIDs to be reaped and
having a way to wait for them when returning their exit status to
qemu-ga is necessary. The approach explained above avoids that complexity.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2012-02-28 15:03:03 +01:00
|
|
|
{
|
2014-05-02 13:26:38 +02:00
|
|
|
Error *local_err = NULL;
|
2015-09-14 13:50:44 +02:00
|
|
|
GuestSuspendMode *mode = g_new(GuestSuspendMode, 1);
|
2012-01-29 10:53:31 +01:00
|
|
|
|
|
|
|
*mode = GUEST_SUSPEND_MODE_DISK;
|
2014-05-02 13:26:38 +02:00
|
|
|
check_suspend_mode(*mode, &local_err);
|
2020-04-22 15:07:19 +02:00
|
|
|
if (local_err) {
|
|
|
|
goto out;
|
|
|
|
}
|
2014-05-02 13:26:38 +02:00
|
|
|
acquire_privilege(SE_SHUTDOWN_NAME, &local_err);
|
2020-04-22 15:07:19 +02:00
|
|
|
if (local_err) {
|
|
|
|
goto out;
|
|
|
|
}
|
2014-05-02 13:26:38 +02:00
|
|
|
execute_async(do_suspend, mode, &local_err);
|
2012-01-29 10:53:31 +01:00
|
|
|
|
2020-04-22 15:07:19 +02:00
|
|
|
out:
|
2014-05-02 13:26:38 +02:00
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
2012-01-29 10:53:31 +01:00
|
|
|
g_free(mode);
|
|
|
|
}
|
qemu-ga: add guest-suspend-disk
As the command name implies, this command suspends the guest to disk.
The suspend operation is implemented by two functions: bios_supports_mode()
and guest_suspend(). Both functions are generic enough to be used by
other suspend modes (introduced by next commits).
Both functions will try to use the scripts provided by the pm-utils
package if it's available. If it's not available, a manual method,
which consists of directly writing to '/sys/power/state', will be used.
To reap terminated children, a new signal handler is installed in the
parent to catch SIGCHLD signals and a non-blocking call to waitpid()
is done to collect their exit statuses. The statuses, however, are
discarded.
The approach used to query the guest for suspend support deserves some
explanation. It's implemented by bios_supports_mode() and shown below:
qemu-ga
|
create pipe
|
fork()
-----------------
| |
| |
| fork()
| --------------------------
| | |
| | |
| | exec('pm-is-supported')
| |
| wait()
| write exit status to pipe
| exit
|
read pipe
This might look complex, but the resulting code is quite simple.
The purpose of that approach is to allow qemu-ga to reap its children
(semi-)automatically from its SIGCHLD handler.
Implementing this the obvious way, that's, doing the exec() call from
the first child process, would force us to introduce a more complex way
to reap qemu-ga's children. Like registering PIDs to be reaped and
having a way to wait for them when returning their exit status to
qemu-ga is necessary. The approach explained above avoids that complexity.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2012-02-28 15:03:03 +01:00
|
|
|
}
|
|
|
|
|
2014-05-02 13:26:30 +02:00
|
|
|
void qmp_guest_suspend_ram(Error **errp)
|
2012-02-28 15:03:04 +01:00
|
|
|
{
|
2014-05-02 13:26:38 +02:00
|
|
|
Error *local_err = NULL;
|
2015-09-14 13:50:44 +02:00
|
|
|
GuestSuspendMode *mode = g_new(GuestSuspendMode, 1);
|
2012-03-12 18:50:02 +01:00
|
|
|
|
|
|
|
*mode = GUEST_SUSPEND_MODE_RAM;
|
2014-05-02 13:26:38 +02:00
|
|
|
check_suspend_mode(*mode, &local_err);
|
2020-04-22 15:07:19 +02:00
|
|
|
if (local_err) {
|
|
|
|
goto out;
|
|
|
|
}
|
2014-05-02 13:26:38 +02:00
|
|
|
acquire_privilege(SE_SHUTDOWN_NAME, &local_err);
|
2020-04-22 15:07:19 +02:00
|
|
|
if (local_err) {
|
|
|
|
goto out;
|
|
|
|
}
|
2014-05-02 13:26:38 +02:00
|
|
|
execute_async(do_suspend, mode, &local_err);
|
2012-03-12 18:50:02 +01:00
|
|
|
|
2020-04-22 15:07:19 +02:00
|
|
|
out:
|
2014-05-02 13:26:38 +02:00
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
2012-03-12 18:50:02 +01:00
|
|
|
g_free(mode);
|
|
|
|
}
|
2012-02-28 15:03:04 +01:00
|
|
|
}
|
|
|
|
|
2014-05-02 13:26:30 +02:00
|
|
|
void qmp_guest_suspend_hybrid(Error **errp)
|
2012-02-28 15:03:05 +01:00
|
|
|
{
|
2015-03-17 11:54:50 +01:00
|
|
|
error_setg(errp, QERR_UNSUPPORTED);
|
2012-02-28 15:03:05 +01:00
|
|
|
}
|
|
|
|
|
2015-06-02 19:41:07 +02:00
|
|
|
static IP_ADAPTER_ADDRESSES *guest_get_adapters_addresses(Error **errp)
|
2012-02-29 17:02:23 +01:00
|
|
|
{
|
2015-06-02 19:41:07 +02:00
|
|
|
IP_ADAPTER_ADDRESSES *adptr_addrs = NULL;
|
|
|
|
ULONG adptr_addrs_len = 0;
|
|
|
|
DWORD ret;
|
|
|
|
|
|
|
|
/* Call the first time to get the adptr_addrs_len. */
|
|
|
|
GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX,
|
|
|
|
NULL, adptr_addrs, &adptr_addrs_len);
|
|
|
|
|
|
|
|
adptr_addrs = g_malloc(adptr_addrs_len);
|
|
|
|
ret = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX,
|
|
|
|
NULL, adptr_addrs, &adptr_addrs_len);
|
|
|
|
if (ret != ERROR_SUCCESS) {
|
|
|
|
error_setg_win32(errp, ret, "failed to get adapters addresses");
|
|
|
|
g_free(adptr_addrs);
|
|
|
|
adptr_addrs = NULL;
|
|
|
|
}
|
|
|
|
return adptr_addrs;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *guest_wctomb_dup(WCHAR *wstr)
|
|
|
|
{
|
|
|
|
char *str;
|
2019-08-19 15:16:20 +02:00
|
|
|
size_t str_size;
|
2015-06-02 19:41:07 +02:00
|
|
|
|
2019-08-19 15:16:20 +02:00
|
|
|
str_size = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
|
|
|
|
/* add 1 to str_size for NULL terminator */
|
|
|
|
str = g_malloc(str_size + 1);
|
|
|
|
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, str_size, NULL, NULL);
|
2015-06-02 19:41:07 +02:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *guest_addr_to_str(IP_ADAPTER_UNICAST_ADDRESS *ip_addr,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
char addr_str[INET6_ADDRSTRLEN + INET_ADDRSTRLEN];
|
|
|
|
DWORD len;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (ip_addr->Address.lpSockaddr->sa_family == AF_INET ||
|
|
|
|
ip_addr->Address.lpSockaddr->sa_family == AF_INET6) {
|
|
|
|
len = sizeof(addr_str);
|
|
|
|
ret = WSAAddressToString(ip_addr->Address.lpSockaddr,
|
|
|
|
ip_addr->Address.iSockaddrLength,
|
|
|
|
NULL,
|
|
|
|
addr_str,
|
|
|
|
&len);
|
|
|
|
if (ret != 0) {
|
|
|
|
error_setg_win32(errp, WSAGetLastError(),
|
|
|
|
"failed address presentation form conversion");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return g_strdup(addr_str);
|
|
|
|
}
|
2012-02-29 17:02:23 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-06-02 19:41:07 +02:00
|
|
|
static int64_t guest_ip_prefix(IP_ADAPTER_UNICAST_ADDRESS *ip_addr)
|
|
|
|
{
|
|
|
|
/* For Windows Vista/2008 and newer, use the OnLinkPrefixLength
|
|
|
|
* field to obtain the prefix.
|
|
|
|
*/
|
|
|
|
return ip_addr->OnLinkPrefixLength;
|
|
|
|
}
|
|
|
|
|
2017-09-12 10:54:26 +02:00
|
|
|
#define INTERFACE_PATH_BUF_SZ 512
|
|
|
|
|
|
|
|
static DWORD get_interface_index(const char *guid)
|
|
|
|
{
|
|
|
|
ULONG index;
|
|
|
|
DWORD status;
|
|
|
|
wchar_t wbuf[INTERFACE_PATH_BUF_SZ];
|
|
|
|
snwprintf(wbuf, INTERFACE_PATH_BUF_SZ, L"\\device\\tcpip_%s", guid);
|
|
|
|
wbuf[INTERFACE_PATH_BUF_SZ - 1] = 0;
|
|
|
|
status = GetAdapterIndex (wbuf, &index);
|
|
|
|
if (status != NO_ERROR) {
|
|
|
|
return (DWORD)~0;
|
|
|
|
} else {
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
}
|
2017-11-03 15:54:20 +01:00
|
|
|
|
|
|
|
typedef NETIOAPI_API (WINAPI *GetIfEntry2Func)(PMIB_IF_ROW2 Row);
|
|
|
|
|
2017-09-12 10:54:26 +02:00
|
|
|
static int guest_get_network_stats(const char *name,
|
2017-11-03 15:54:20 +01:00
|
|
|
GuestNetworkInterfaceStat *stats)
|
2017-09-12 10:54:26 +02:00
|
|
|
{
|
2017-11-03 15:54:20 +01:00
|
|
|
OSVERSIONINFO os_ver;
|
|
|
|
|
|
|
|
os_ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
|
|
|
|
GetVersionEx(&os_ver);
|
|
|
|
if (os_ver.dwMajorVersion >= 6) {
|
|
|
|
MIB_IF_ROW2 a_mid_ifrow;
|
|
|
|
GetIfEntry2Func getifentry2_ex;
|
|
|
|
DWORD if_index = 0;
|
|
|
|
HMODULE module = GetModuleHandle("iphlpapi");
|
|
|
|
PVOID func = GetProcAddress(module, "GetIfEntry2");
|
|
|
|
|
|
|
|
if (func == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
getifentry2_ex = (GetIfEntry2Func)func;
|
|
|
|
if_index = get_interface_index(name);
|
|
|
|
if (if_index == (DWORD)~0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&a_mid_ifrow, 0, sizeof(a_mid_ifrow));
|
|
|
|
a_mid_ifrow.InterfaceIndex = if_index;
|
|
|
|
if (NO_ERROR == getifentry2_ex(&a_mid_ifrow)) {
|
|
|
|
stats->rx_bytes = a_mid_ifrow.InOctets;
|
|
|
|
stats->rx_packets = a_mid_ifrow.InUcastPkts;
|
|
|
|
stats->rx_errs = a_mid_ifrow.InErrors;
|
|
|
|
stats->rx_dropped = a_mid_ifrow.InDiscards;
|
|
|
|
stats->tx_bytes = a_mid_ifrow.OutOctets;
|
|
|
|
stats->tx_packets = a_mid_ifrow.OutUcastPkts;
|
|
|
|
stats->tx_errs = a_mid_ifrow.OutErrors;
|
|
|
|
stats->tx_dropped = a_mid_ifrow.OutDiscards;
|
|
|
|
return 0;
|
|
|
|
}
|
2017-09-12 10:54:26 +02:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-02 19:41:07 +02:00
|
|
|
GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
|
|
|
|
{
|
|
|
|
IP_ADAPTER_ADDRESSES *adptr_addrs, *addr;
|
|
|
|
IP_ADAPTER_UNICAST_ADDRESS *ip_addr = NULL;
|
2021-01-13 23:10:13 +01:00
|
|
|
GuestNetworkInterfaceList *head = NULL, **tail = &head;
|
|
|
|
GuestIpAddressList *head_addr, **tail_addr;
|
|
|
|
GuestNetworkInterface *info;
|
2017-09-12 10:54:26 +02:00
|
|
|
GuestNetworkInterfaceStat *interface_stat = NULL;
|
2021-01-13 23:10:13 +01:00
|
|
|
GuestIpAddress *address_item = NULL;
|
2015-06-02 19:41:07 +02:00
|
|
|
unsigned char *mac_addr;
|
|
|
|
char *addr_str;
|
|
|
|
WORD wsa_version;
|
|
|
|
WSADATA wsa_data;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
adptr_addrs = guest_get_adapters_addresses(errp);
|
|
|
|
if (adptr_addrs == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make WSA APIs available. */
|
|
|
|
wsa_version = MAKEWORD(2, 2);
|
|
|
|
ret = WSAStartup(wsa_version, &wsa_data);
|
|
|
|
if (ret != 0) {
|
|
|
|
error_setg_win32(errp, ret, "failed socket startup");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (addr = adptr_addrs; addr; addr = addr->Next) {
|
|
|
|
info = g_malloc0(sizeof(*info));
|
|
|
|
|
2021-01-13 23:10:13 +01:00
|
|
|
QAPI_LIST_APPEND(tail, info);
|
2015-06-02 19:41:07 +02:00
|
|
|
|
2021-01-13 23:10:13 +01:00
|
|
|
info->name = guest_wctomb_dup(addr->FriendlyName);
|
2015-06-02 19:41:07 +02:00
|
|
|
|
|
|
|
if (addr->PhysicalAddressLength != 0) {
|
|
|
|
mac_addr = addr->PhysicalAddress;
|
|
|
|
|
2021-01-13 23:10:13 +01:00
|
|
|
info->hardware_address =
|
2015-06-02 19:41:07 +02:00
|
|
|
g_strdup_printf("%02x:%02x:%02x:%02x:%02x:%02x",
|
|
|
|
(int) mac_addr[0], (int) mac_addr[1],
|
|
|
|
(int) mac_addr[2], (int) mac_addr[3],
|
|
|
|
(int) mac_addr[4], (int) mac_addr[5]);
|
|
|
|
|
2021-01-13 23:10:13 +01:00
|
|
|
info->has_hardware_address = true;
|
2015-06-02 19:41:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
head_addr = NULL;
|
2021-01-13 23:10:13 +01:00
|
|
|
tail_addr = &head_addr;
|
2015-06-02 19:41:07 +02:00
|
|
|
for (ip_addr = addr->FirstUnicastAddress;
|
|
|
|
ip_addr;
|
|
|
|
ip_addr = ip_addr->Next) {
|
|
|
|
addr_str = guest_addr_to_str(ip_addr, errp);
|
|
|
|
if (addr_str == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
address_item = g_malloc0(sizeof(*address_item));
|
|
|
|
|
2021-01-13 23:10:13 +01:00
|
|
|
QAPI_LIST_APPEND(tail_addr, address_item);
|
2015-06-02 19:41:07 +02:00
|
|
|
|
2021-01-13 23:10:13 +01:00
|
|
|
address_item->ip_address = addr_str;
|
|
|
|
address_item->prefix = guest_ip_prefix(ip_addr);
|
2015-06-02 19:41:07 +02:00
|
|
|
if (ip_addr->Address.lpSockaddr->sa_family == AF_INET) {
|
2021-01-13 23:10:13 +01:00
|
|
|
address_item->ip_address_type = GUEST_IP_ADDRESS_TYPE_IPV4;
|
2015-06-02 19:41:07 +02:00
|
|
|
} else if (ip_addr->Address.lpSockaddr->sa_family == AF_INET6) {
|
2021-01-13 23:10:13 +01:00
|
|
|
address_item->ip_address_type = GUEST_IP_ADDRESS_TYPE_IPV6;
|
2015-06-02 19:41:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (head_addr) {
|
2021-01-13 23:10:13 +01:00
|
|
|
info->has_ip_addresses = true;
|
|
|
|
info->ip_addresses = head_addr;
|
2015-06-02 19:41:07 +02:00
|
|
|
}
|
2021-01-13 23:10:13 +01:00
|
|
|
if (!info->has_statistics) {
|
2017-09-12 10:54:26 +02:00
|
|
|
interface_stat = g_malloc0(sizeof(*interface_stat));
|
|
|
|
if (guest_get_network_stats(addr->AdapterName,
|
|
|
|
interface_stat) == -1) {
|
2021-01-13 23:10:13 +01:00
|
|
|
info->has_statistics = false;
|
2017-09-12 10:54:26 +02:00
|
|
|
g_free(interface_stat);
|
|
|
|
} else {
|
2021-01-13 23:10:13 +01:00
|
|
|
info->statistics = interface_stat;
|
|
|
|
info->has_statistics = true;
|
2017-09-12 10:54:26 +02:00
|
|
|
}
|
|
|
|
}
|
2015-06-02 19:41:07 +02:00
|
|
|
}
|
|
|
|
WSACleanup();
|
|
|
|
out:
|
|
|
|
g_free(adptr_addrs);
|
|
|
|
return head;
|
|
|
|
}
|
|
|
|
|
2020-10-21 09:15:18 +02:00
|
|
|
static int64_t filetime_to_ns(const FILETIME *tf)
|
|
|
|
{
|
|
|
|
return ((((int64_t)tf->dwHighDateTime << 32) | tf->dwLowDateTime)
|
|
|
|
- W32_FT_OFFSET) * 100;
|
|
|
|
}
|
|
|
|
|
2013-03-05 10:39:11 +01:00
|
|
|
int64_t qmp_guest_get_time(Error **errp)
|
|
|
|
{
|
2013-03-15 10:29:04 +01:00
|
|
|
SYSTEMTIME ts = {0};
|
|
|
|
FILETIME tf;
|
|
|
|
|
|
|
|
GetSystemTime(&ts);
|
|
|
|
if (ts.wYear < 1601 || ts.wYear > 30827) {
|
|
|
|
error_setg(errp, "Failed to get time");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!SystemTimeToFileTime(&ts, &tf)) {
|
|
|
|
error_setg(errp, "Failed to convert system time: %d", (int)GetLastError());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-10-21 09:15:18 +02:00
|
|
|
return filetime_to_ns(&tf);
|
2013-03-05 10:39:11 +01:00
|
|
|
}
|
|
|
|
|
2014-01-31 11:29:51 +01:00
|
|
|
void qmp_guest_set_time(bool has_time, int64_t time_ns, Error **errp)
|
2013-03-05 10:39:12 +01:00
|
|
|
{
|
2014-05-02 13:26:38 +02:00
|
|
|
Error *local_err = NULL;
|
2013-03-15 10:29:05 +01:00
|
|
|
SYSTEMTIME ts;
|
|
|
|
FILETIME tf;
|
|
|
|
LONGLONG time;
|
|
|
|
|
2015-01-21 12:09:50 +01:00
|
|
|
if (!has_time) {
|
|
|
|
/* Unfortunately, Windows libraries don't provide an easy way to access
|
|
|
|
* RTC yet:
|
|
|
|
*
|
|
|
|
* https://msdn.microsoft.com/en-us/library/aa908981.aspx
|
2017-08-22 15:55:04 +02:00
|
|
|
*
|
|
|
|
* Instead, a workaround is to use the Windows win32tm command to
|
|
|
|
* resync the time using the Windows Time service.
|
2015-01-21 12:09:50 +01:00
|
|
|
*/
|
2017-08-22 15:55:04 +02:00
|
|
|
LPVOID msg_buffer;
|
|
|
|
DWORD ret_flags;
|
|
|
|
|
|
|
|
HRESULT hr = system("w32tm /resync /nowait");
|
|
|
|
|
|
|
|
if (GetLastError() != 0) {
|
|
|
|
strerror_s((LPTSTR) & msg_buffer, 0, errno);
|
|
|
|
error_setg(errp, "system(...) failed: %s", (LPCTSTR)msg_buffer);
|
|
|
|
} else if (hr != 0) {
|
|
|
|
if (hr == HRESULT_FROM_WIN32(ERROR_SERVICE_NOT_ACTIVE)) {
|
|
|
|
error_setg(errp, "Windows Time service not running on the "
|
|
|
|
"guest");
|
|
|
|
} else {
|
|
|
|
if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
|
|
|
|
(DWORD)hr, MAKELANGID(LANG_NEUTRAL,
|
|
|
|
SUBLANG_DEFAULT), (LPTSTR) & msg_buffer, 0,
|
|
|
|
NULL)) {
|
|
|
|
error_setg(errp, "w32tm failed with error (0x%lx), couldn'"
|
|
|
|
"t retrieve error message", hr);
|
|
|
|
} else {
|
|
|
|
error_setg(errp, "w32tm failed with error (0x%lx): %s", hr,
|
|
|
|
(LPCTSTR)msg_buffer);
|
|
|
|
LocalFree(msg_buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (!InternetGetConnectedState(&ret_flags, 0)) {
|
|
|
|
error_setg(errp, "No internet connection on guest, sync not "
|
|
|
|
"accurate");
|
|
|
|
}
|
2015-01-21 12:09:50 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Validate time passed by user. */
|
|
|
|
if (time_ns < 0 || time_ns / 100 > INT64_MAX - W32_FT_OFFSET) {
|
|
|
|
error_setg(errp, "Time %" PRId64 "is invalid", time_ns);
|
|
|
|
return;
|
|
|
|
}
|
2013-03-15 10:29:05 +01:00
|
|
|
|
2015-01-21 12:09:50 +01:00
|
|
|
time = time_ns / 100 + W32_FT_OFFSET;
|
2013-03-15 10:29:05 +01:00
|
|
|
|
2015-01-21 12:09:50 +01:00
|
|
|
tf.dwLowDateTime = (DWORD) time;
|
|
|
|
tf.dwHighDateTime = (DWORD) (time >> 32);
|
2013-03-15 10:29:05 +01:00
|
|
|
|
2015-01-21 12:09:50 +01:00
|
|
|
if (!FileTimeToSystemTime(&tf, &ts)) {
|
|
|
|
error_setg(errp, "Failed to convert system time %d",
|
|
|
|
(int)GetLastError());
|
|
|
|
return;
|
2013-03-15 10:29:05 +01:00
|
|
|
}
|
|
|
|
|
2014-05-02 13:26:38 +02:00
|
|
|
acquire_privilege(SE_SYSTEMTIME_NAME, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
2013-03-15 10:29:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!SetSystemTime(&ts)) {
|
|
|
|
error_setg(errp, "Failed to set time to guest: %d", (int)GetLastError());
|
|
|
|
return;
|
|
|
|
}
|
2013-03-05 10:39:12 +01:00
|
|
|
}
|
|
|
|
|
2013-03-06 22:59:29 +01:00
|
|
|
GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp)
|
|
|
|
{
|
2015-11-02 15:49:48 +01:00
|
|
|
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION pslpi, ptr;
|
|
|
|
DWORD length;
|
2021-01-13 23:10:12 +01:00
|
|
|
GuestLogicalProcessorList *head, **tail;
|
2015-11-02 15:49:48 +01:00
|
|
|
Error *local_err = NULL;
|
|
|
|
int64_t current;
|
|
|
|
|
|
|
|
ptr = pslpi = NULL;
|
|
|
|
length = 0;
|
|
|
|
current = 0;
|
|
|
|
head = NULL;
|
2021-01-13 23:10:12 +01:00
|
|
|
tail = &head;
|
2015-11-02 15:49:48 +01:00
|
|
|
|
|
|
|
if ((GetLogicalProcessorInformation(pslpi, &length) == FALSE) &&
|
|
|
|
(GetLastError() == ERROR_INSUFFICIENT_BUFFER) &&
|
|
|
|
(length > sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION))) {
|
|
|
|
ptr = pslpi = g_malloc0(length);
|
|
|
|
if (GetLogicalProcessorInformation(pslpi, &length) == FALSE) {
|
|
|
|
error_setg(&local_err, "Failed to get processor information: %d",
|
|
|
|
(int)GetLastError());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
error_setg(&local_err,
|
|
|
|
"Failed to get processor information buffer length: %d",
|
|
|
|
(int)GetLastError());
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((local_err == NULL) && (length > 0)) {
|
|
|
|
if (pslpi->Relationship == RelationProcessorCore) {
|
|
|
|
ULONG_PTR cpu_bits = pslpi->ProcessorMask;
|
|
|
|
|
|
|
|
while (cpu_bits > 0) {
|
|
|
|
if (!!(cpu_bits & 1)) {
|
|
|
|
GuestLogicalProcessor *vcpu;
|
|
|
|
|
|
|
|
vcpu = g_malloc0(sizeof *vcpu);
|
|
|
|
vcpu->logical_id = current++;
|
|
|
|
vcpu->online = true;
|
2017-04-11 14:52:05 +02:00
|
|
|
vcpu->has_can_offline = true;
|
2015-11-02 15:49:48 +01:00
|
|
|
|
2021-01-13 23:10:12 +01:00
|
|
|
QAPI_LIST_APPEND(tail, vcpu);
|
2015-11-02 15:49:48 +01:00
|
|
|
}
|
|
|
|
cpu_bits >>= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
length -= sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
|
|
|
|
pslpi++; /* next entry */
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(ptr);
|
|
|
|
|
|
|
|
if (local_err == NULL) {
|
|
|
|
if (head != NULL) {
|
|
|
|
return head;
|
|
|
|
}
|
|
|
|
/* there's no guest with zero VCPUs */
|
|
|
|
error_setg(&local_err, "Guest reported zero VCPUs");
|
|
|
|
}
|
|
|
|
|
|
|
|
qapi_free_GuestLogicalProcessorList(head);
|
|
|
|
error_propagate(errp, local_err);
|
2013-03-06 22:59:29 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp)
|
|
|
|
{
|
2015-03-17 11:54:50 +01:00
|
|
|
error_setg(errp, QERR_UNSUPPORTED);
|
2013-03-06 22:59:29 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-30 16:37:13 +02:00
|
|
|
static gchar *
|
|
|
|
get_net_error_message(gint error)
|
|
|
|
{
|
|
|
|
HMODULE module = NULL;
|
|
|
|
gchar *retval = NULL;
|
|
|
|
wchar_t *msg = NULL;
|
2016-02-17 17:47:52 +01:00
|
|
|
int flags;
|
|
|
|
size_t nchars;
|
2015-06-30 16:37:13 +02:00
|
|
|
|
2016-02-17 17:47:51 +01:00
|
|
|
flags = FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM;
|
2015-06-30 16:37:13 +02:00
|
|
|
|
|
|
|
if (error >= NERR_BASE && error <= MAX_NERR) {
|
|
|
|
module = LoadLibraryExW(L"netmsg.dll", NULL, LOAD_LIBRARY_AS_DATAFILE);
|
|
|
|
|
|
|
|
if (module != NULL) {
|
|
|
|
flags |= FORMAT_MESSAGE_FROM_HMODULE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FormatMessageW(flags, module, error, 0, (LPWSTR)&msg, 0, NULL);
|
|
|
|
|
|
|
|
if (msg != NULL) {
|
|
|
|
nchars = wcslen(msg);
|
|
|
|
|
2016-02-17 17:47:54 +01:00
|
|
|
if (nchars >= 2 &&
|
2016-02-17 17:47:53 +01:00
|
|
|
msg[nchars - 1] == L'\n' &&
|
|
|
|
msg[nchars - 2] == L'\r') {
|
|
|
|
msg[nchars - 2] = L'\0';
|
2015-06-30 16:37:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
retval = g_utf16_to_utf8(msg, -1, NULL, NULL, NULL);
|
|
|
|
|
|
|
|
LocalFree(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (module != NULL) {
|
|
|
|
FreeLibrary(module);
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
qga: add guest-set-user-password command
Add a new 'guest-set-user-password' command for changing the password
of guest OS user accounts. This command is needed to enable OpenStack
to support its API for changing the admin password of guests running
on KVM/QEMU. It is not practical to provide a command at the QEMU
level explicitly targetting administrator account password change
only, since different guest OS have different names for the admin
account. While UNIX systems use 'root', Windows systems typically
use 'Administrator' and even that can be renamed. Higher level apps
like OpenStack have the ability to figure out the correct admin
account name since they have info that QEMU/libvirt do not.
The command accepts either the clear text password string, encoded
in base64 to make it 8-bit safe in JSON:
$ echo -n "123456" | base64
MTIzNDU2
$ virsh -c qemu:///system qemu-agent-command f21x86_64 \
'{ "execute": "guest-set-user-password",
"arguments": { "crypted": false,
"username": "root",
"password": "MTIzNDU2" } }'
{"return":{}}
Or a password that has already been run though a crypt(3) like
algorithm appropriate for the guest, again then base64 encoded:
$ echo -n '$6$n01A2Tau$e...snip...DfMOP7of9AJ1I8q0' | base64
JDYkb...snip...YT2Ey
$ virsh -c qemu:///system qemu-agent-command f21x86_64 \
'{ "execute": "guest-set-user-password",
"arguments": { "crypted": true,
"username": "root",
"password": "JDYkb...snip...YT2Ey" } }'
NB windows support is desirable, but not implemented in this
patch.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2015-02-11 12:26:12 +01:00
|
|
|
void qmp_guest_set_user_password(const char *username,
|
|
|
|
const char *password,
|
|
|
|
bool crypted,
|
|
|
|
Error **errp)
|
|
|
|
{
|
2015-06-30 16:37:13 +02:00
|
|
|
NET_API_STATUS nas;
|
|
|
|
char *rawpasswddata = NULL;
|
|
|
|
size_t rawpasswdlen;
|
2016-02-17 17:47:55 +01:00
|
|
|
wchar_t *user = NULL, *wpass = NULL;
|
2015-06-30 16:37:13 +02:00
|
|
|
USER_INFO_1003 pi1003 = { 0, };
|
2016-02-17 17:47:55 +01:00
|
|
|
GError *gerr = NULL;
|
2015-06-30 16:37:13 +02:00
|
|
|
|
|
|
|
if (crypted) {
|
|
|
|
error_setg(errp, QERR_UNSUPPORTED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-23 16:37:07 +01:00
|
|
|
rawpasswddata = (char *)qbase64_decode(password, -1, &rawpasswdlen, errp);
|
|
|
|
if (!rawpasswddata) {
|
|
|
|
return;
|
|
|
|
}
|
2015-06-30 16:37:13 +02:00
|
|
|
rawpasswddata = g_renew(char, rawpasswddata, rawpasswdlen + 1);
|
|
|
|
rawpasswddata[rawpasswdlen] = '\0';
|
|
|
|
|
2016-02-17 17:47:55 +01:00
|
|
|
user = g_utf8_to_utf16(username, -1, NULL, NULL, &gerr);
|
|
|
|
if (!user) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
wpass = g_utf8_to_utf16(rawpasswddata, -1, NULL, NULL, &gerr);
|
|
|
|
if (!wpass) {
|
|
|
|
goto done;
|
|
|
|
}
|
2015-06-30 16:37:13 +02:00
|
|
|
|
|
|
|
pi1003.usri1003_password = wpass;
|
|
|
|
nas = NetUserSetInfo(NULL, user,
|
|
|
|
1003, (LPBYTE)&pi1003,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
if (nas != NERR_Success) {
|
|
|
|
gchar *msg = get_net_error_message(nas);
|
|
|
|
error_setg(errp, "failed to set password: %s", msg);
|
|
|
|
g_free(msg);
|
|
|
|
}
|
|
|
|
|
2016-02-17 17:47:55 +01:00
|
|
|
done:
|
|
|
|
if (gerr) {
|
|
|
|
error_setg(errp, QERR_QGA_COMMAND_FAILED, gerr->message);
|
|
|
|
g_error_free(gerr);
|
|
|
|
}
|
2015-06-30 16:37:13 +02:00
|
|
|
g_free(user);
|
|
|
|
g_free(wpass);
|
|
|
|
g_free(rawpasswddata);
|
qga: add guest-set-user-password command
Add a new 'guest-set-user-password' command for changing the password
of guest OS user accounts. This command is needed to enable OpenStack
to support its API for changing the admin password of guests running
on KVM/QEMU. It is not practical to provide a command at the QEMU
level explicitly targetting administrator account password change
only, since different guest OS have different names for the admin
account. While UNIX systems use 'root', Windows systems typically
use 'Administrator' and even that can be renamed. Higher level apps
like OpenStack have the ability to figure out the correct admin
account name since they have info that QEMU/libvirt do not.
The command accepts either the clear text password string, encoded
in base64 to make it 8-bit safe in JSON:
$ echo -n "123456" | base64
MTIzNDU2
$ virsh -c qemu:///system qemu-agent-command f21x86_64 \
'{ "execute": "guest-set-user-password",
"arguments": { "crypted": false,
"username": "root",
"password": "MTIzNDU2" } }'
{"return":{}}
Or a password that has already been run though a crypt(3) like
algorithm appropriate for the guest, again then base64 encoded:
$ echo -n '$6$n01A2Tau$e...snip...DfMOP7of9AJ1I8q0' | base64
JDYkb...snip...YT2Ey
$ virsh -c qemu:///system qemu-agent-command f21x86_64 \
'{ "execute": "guest-set-user-password",
"arguments": { "crypted": true,
"username": "root",
"password": "JDYkb...snip...YT2Ey" } }'
NB windows support is desirable, but not implemented in this
patch.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2015-02-11 12:26:12 +01:00
|
|
|
}
|
|
|
|
|
2015-01-22 03:40:02 +01:00
|
|
|
GuestMemoryBlockList *qmp_guest_get_memory_blocks(Error **errp)
|
|
|
|
{
|
2015-03-17 11:54:50 +01:00
|
|
|
error_setg(errp, QERR_UNSUPPORTED);
|
2015-01-22 03:40:02 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
GuestMemoryBlockResponseList *
|
|
|
|
qmp_guest_set_memory_blocks(GuestMemoryBlockList *mem_blks, Error **errp)
|
|
|
|
{
|
2015-03-17 11:54:50 +01:00
|
|
|
error_setg(errp, QERR_UNSUPPORTED);
|
2015-01-22 03:40:02 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
GuestMemoryBlockInfo *qmp_guest_get_memory_block_info(Error **errp)
|
|
|
|
{
|
2015-03-17 11:54:50 +01:00
|
|
|
error_setg(errp, QERR_UNSUPPORTED);
|
2015-01-22 03:40:02 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-06-30 23:51:40 +02:00
|
|
|
/* add unsupported commands to the blacklist */
|
|
|
|
GList *ga_command_blacklist_init(GList *blacklist)
|
|
|
|
{
|
|
|
|
const char *list_unsupported[] = {
|
2015-06-02 19:41:07 +02:00
|
|
|
"guest-suspend-hybrid",
|
2015-11-02 15:49:48 +01:00
|
|
|
"guest-set-vcpus",
|
2015-01-22 03:40:06 +01:00
|
|
|
"guest-get-memory-blocks", "guest-set-memory-blocks",
|
2019-10-17 14:34:38 +02:00
|
|
|
"guest-get-memory-block-size", "guest-get-memory-block-info",
|
2016-10-03 16:01:25 +02:00
|
|
|
NULL};
|
2014-06-30 23:51:40 +02:00
|
|
|
char **p = (char **)list_unsupported;
|
|
|
|
|
|
|
|
while (*p) {
|
2015-08-27 01:34:50 +02:00
|
|
|
blacklist = g_list_append(blacklist, g_strdup(*p++));
|
2014-06-30 23:51:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!vss_init(true)) {
|
2015-05-06 13:57:38 +02:00
|
|
|
g_debug("vss_init failed, vss commands are going to be disabled");
|
2014-06-30 23:51:40 +02:00
|
|
|
const char *list[] = {
|
|
|
|
"guest-get-fsinfo", "guest-fsfreeze-status",
|
|
|
|
"guest-fsfreeze-freeze", "guest-fsfreeze-thaw", NULL};
|
|
|
|
p = (char **)list;
|
|
|
|
|
|
|
|
while (*p) {
|
2015-08-27 01:34:50 +02:00
|
|
|
blacklist = g_list_append(blacklist, g_strdup(*p++));
|
2014-06-30 23:51:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return blacklist;
|
|
|
|
}
|
|
|
|
|
2012-01-20 05:04:34 +01:00
|
|
|
/* register init/cleanup routines for stateful command groups */
|
|
|
|
void ga_command_state_init(GAState *s, GACommandState *cs)
|
|
|
|
{
|
2014-06-30 23:51:40 +02:00
|
|
|
if (!vss_initialized()) {
|
2013-08-07 17:40:25 +02:00
|
|
|
ga_command_state_add(cs, NULL, guest_fsfreeze_cleanup);
|
|
|
|
}
|
2012-01-20 05:04:34 +01:00
|
|
|
}
|
2017-04-19 11:26:15 +02:00
|
|
|
|
|
|
|
/* MINGW is missing two fields: IncomingFrames & OutgoingFrames */
|
|
|
|
typedef struct _GA_WTSINFOA {
|
|
|
|
WTS_CONNECTSTATE_CLASS State;
|
|
|
|
DWORD SessionId;
|
|
|
|
DWORD IncomingBytes;
|
|
|
|
DWORD OutgoingBytes;
|
|
|
|
DWORD IncomingFrames;
|
|
|
|
DWORD OutgoingFrames;
|
|
|
|
DWORD IncomingCompressedBytes;
|
|
|
|
DWORD OutgoingCompressedBy;
|
|
|
|
CHAR WinStationName[WINSTATIONNAME_LENGTH];
|
|
|
|
CHAR Domain[DOMAIN_LENGTH];
|
|
|
|
CHAR UserName[USERNAME_LENGTH + 1];
|
|
|
|
LARGE_INTEGER ConnectTime;
|
|
|
|
LARGE_INTEGER DisconnectTime;
|
|
|
|
LARGE_INTEGER LastInputTime;
|
|
|
|
LARGE_INTEGER LogonTime;
|
|
|
|
LARGE_INTEGER CurrentTime;
|
|
|
|
|
|
|
|
} GA_WTSINFOA;
|
|
|
|
|
2019-12-05 18:46:26 +01:00
|
|
|
GuestUserList *qmp_guest_get_users(Error **errp)
|
2017-04-19 11:26:15 +02:00
|
|
|
{
|
|
|
|
#define QGA_NANOSECONDS 10000000
|
|
|
|
|
|
|
|
GHashTable *cache = NULL;
|
2021-01-13 23:10:13 +01:00
|
|
|
GuestUserList *head = NULL, **tail = &head;
|
2017-04-19 11:26:15 +02:00
|
|
|
|
|
|
|
DWORD buffer_size = 0, count = 0, i = 0;
|
|
|
|
GA_WTSINFOA *info = NULL;
|
|
|
|
WTS_SESSION_INFOA *entries = NULL;
|
|
|
|
GuestUser *user = NULL;
|
|
|
|
gpointer value = NULL;
|
|
|
|
INT64 login = 0;
|
|
|
|
double login_time = 0;
|
|
|
|
|
|
|
|
cache = g_hash_table_new(g_str_hash, g_str_equal);
|
|
|
|
|
|
|
|
if (WTSEnumerateSessionsA(NULL, 0, 1, &entries, &count)) {
|
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
buffer_size = 0;
|
|
|
|
info = NULL;
|
|
|
|
if (WTSQuerySessionInformationA(
|
|
|
|
NULL,
|
|
|
|
entries[i].SessionId,
|
|
|
|
WTSSessionInfo,
|
|
|
|
(LPSTR *)&info,
|
|
|
|
&buffer_size
|
|
|
|
)) {
|
|
|
|
|
|
|
|
if (strlen(info->UserName) == 0) {
|
|
|
|
WTSFreeMemory(info);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
login = info->LogonTime.QuadPart;
|
|
|
|
login -= W32_FT_OFFSET;
|
|
|
|
login_time = ((double)login) / QGA_NANOSECONDS;
|
|
|
|
|
|
|
|
if (g_hash_table_contains(cache, info->UserName)) {
|
|
|
|
value = g_hash_table_lookup(cache, info->UserName);
|
|
|
|
user = (GuestUser *)value;
|
|
|
|
if (user->login_time > login_time) {
|
|
|
|
user->login_time = login_time;
|
|
|
|
}
|
|
|
|
} else {
|
2021-01-13 23:10:13 +01:00
|
|
|
user = g_new0(GuestUser, 1);
|
2017-04-19 11:26:15 +02:00
|
|
|
|
2021-01-13 23:10:13 +01:00
|
|
|
user->user = g_strdup(info->UserName);
|
|
|
|
user->domain = g_strdup(info->Domain);
|
|
|
|
user->has_domain = true;
|
2017-04-19 11:26:15 +02:00
|
|
|
|
2021-01-13 23:10:13 +01:00
|
|
|
user->login_time = login_time;
|
2017-04-19 11:26:15 +02:00
|
|
|
|
2021-01-13 23:10:13 +01:00
|
|
|
g_hash_table_add(cache, user->user);
|
2017-04-19 11:26:15 +02:00
|
|
|
|
2021-01-13 23:10:13 +01:00
|
|
|
QAPI_LIST_APPEND(tail, user);
|
2017-04-19 11:26:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
WTSFreeMemory(info);
|
|
|
|
}
|
|
|
|
WTSFreeMemory(entries);
|
|
|
|
}
|
|
|
|
g_hash_table_destroy(cache);
|
|
|
|
return head;
|
|
|
|
}
|
2017-07-14 16:28:56 +02:00
|
|
|
|
|
|
|
typedef struct _ga_matrix_lookup_t {
|
|
|
|
int major;
|
|
|
|
int minor;
|
|
|
|
char const *version;
|
|
|
|
char const *version_id;
|
|
|
|
} ga_matrix_lookup_t;
|
|
|
|
|
|
|
|
static ga_matrix_lookup_t const WIN_VERSION_MATRIX[2][8] = {
|
|
|
|
{
|
|
|
|
/* Desktop editions */
|
|
|
|
{ 5, 0, "Microsoft Windows 2000", "2000"},
|
|
|
|
{ 5, 1, "Microsoft Windows XP", "xp"},
|
|
|
|
{ 6, 0, "Microsoft Windows Vista", "vista"},
|
|
|
|
{ 6, 1, "Microsoft Windows 7" "7"},
|
|
|
|
{ 6, 2, "Microsoft Windows 8", "8"},
|
|
|
|
{ 6, 3, "Microsoft Windows 8.1", "8.1"},
|
|
|
|
{10, 0, "Microsoft Windows 10", "10"},
|
|
|
|
{ 0, 0, 0}
|
|
|
|
},{
|
|
|
|
/* Server editions */
|
|
|
|
{ 5, 2, "Microsoft Windows Server 2003", "2003"},
|
|
|
|
{ 6, 0, "Microsoft Windows Server 2008", "2008"},
|
|
|
|
{ 6, 1, "Microsoft Windows Server 2008 R2", "2008r2"},
|
|
|
|
{ 6, 2, "Microsoft Windows Server 2012", "2012"},
|
|
|
|
{ 6, 3, "Microsoft Windows Server 2012 R2", "2012r2"},
|
2018-12-19 09:40:51 +01:00
|
|
|
{ 0, 0, 0},
|
2017-07-14 16:28:56 +02:00
|
|
|
{ 0, 0, 0},
|
|
|
|
{ 0, 0, 0}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-19 09:40:51 +01:00
|
|
|
typedef struct _ga_win_10_0_server_t {
|
|
|
|
int final_build;
|
|
|
|
char const *version;
|
|
|
|
char const *version_id;
|
|
|
|
} ga_win_10_0_server_t;
|
|
|
|
|
2021-06-21 14:50:17 +02:00
|
|
|
static ga_win_10_0_server_t const WIN_10_0_SERVER_VERSION_MATRIX[4] = {
|
2018-12-19 09:40:51 +01:00
|
|
|
{14393, "Microsoft Windows Server 2016", "2016"},
|
|
|
|
{17763, "Microsoft Windows Server 2019", "2019"},
|
2021-06-21 14:50:17 +02:00
|
|
|
{20344, "Microsoft Windows Server 2022", "2022"},
|
2018-12-19 09:40:51 +01:00
|
|
|
{0, 0}
|
|
|
|
};
|
|
|
|
|
2017-07-14 16:28:56 +02:00
|
|
|
static void ga_get_win_version(RTL_OSVERSIONINFOEXW *info, Error **errp)
|
|
|
|
{
|
2020-10-26 10:05:41 +01:00
|
|
|
typedef NTSTATUS(WINAPI *rtl_get_version_t)(
|
2017-07-14 16:28:56 +02:00
|
|
|
RTL_OSVERSIONINFOEXW *os_version_info_ex);
|
|
|
|
|
|
|
|
info->dwOSVersionInfoSize = sizeof(RTL_OSVERSIONINFOEXW);
|
|
|
|
|
|
|
|
HMODULE module = GetModuleHandle("ntdll");
|
|
|
|
PVOID fun = GetProcAddress(module, "RtlGetVersion");
|
|
|
|
if (fun == NULL) {
|
|
|
|
error_setg(errp, QERR_QGA_COMMAND_FAILED,
|
|
|
|
"Failed to get address of RtlGetVersion");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rtl_get_version_t rtl_get_version = (rtl_get_version_t)fun;
|
|
|
|
rtl_get_version(info);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *ga_get_win_name(OSVERSIONINFOEXW const *os_version, bool id)
|
|
|
|
{
|
|
|
|
DWORD major = os_version->dwMajorVersion;
|
|
|
|
DWORD minor = os_version->dwMinorVersion;
|
2018-12-19 09:40:51 +01:00
|
|
|
DWORD build = os_version->dwBuildNumber;
|
2017-07-14 16:28:56 +02:00
|
|
|
int tbl_idx = (os_version->wProductType != VER_NT_WORKSTATION);
|
|
|
|
ga_matrix_lookup_t const *table = WIN_VERSION_MATRIX[tbl_idx];
|
2018-12-19 09:40:51 +01:00
|
|
|
ga_win_10_0_server_t const *win_10_0_table = WIN_10_0_SERVER_VERSION_MATRIX;
|
2017-07-14 16:28:56 +02:00
|
|
|
while (table->version != NULL) {
|
2018-12-19 09:40:51 +01:00
|
|
|
if (major == 10 && minor == 0 && tbl_idx) {
|
|
|
|
while (win_10_0_table->version != NULL) {
|
|
|
|
if (build <= win_10_0_table->final_build) {
|
|
|
|
if (id) {
|
|
|
|
return g_strdup(win_10_0_table->version_id);
|
|
|
|
} else {
|
|
|
|
return g_strdup(win_10_0_table->version);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
win_10_0_table++;
|
|
|
|
}
|
|
|
|
} else if (major == table->major && minor == table->minor) {
|
2017-07-14 16:28:56 +02:00
|
|
|
if (id) {
|
|
|
|
return g_strdup(table->version_id);
|
|
|
|
} else {
|
|
|
|
return g_strdup(table->version);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++table;
|
|
|
|
}
|
|
|
|
slog("failed to lookup Windows version: major=%lu, minor=%lu",
|
|
|
|
major, minor);
|
|
|
|
return g_strdup("N/A");
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *ga_get_win_product_name(Error **errp)
|
|
|
|
{
|
2021-07-12 18:24:44 +02:00
|
|
|
HKEY key = INVALID_HANDLE_VALUE;
|
2017-07-14 16:28:56 +02:00
|
|
|
DWORD size = 128;
|
|
|
|
char *result = g_malloc0(size);
|
|
|
|
LONG err = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
err = RegOpenKeyA(HKEY_LOCAL_MACHINE,
|
|
|
|
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",
|
|
|
|
&key);
|
|
|
|
if (err != ERROR_SUCCESS) {
|
|
|
|
error_setg_win32(errp, err, "failed to open registry key");
|
2021-07-12 18:24:44 +02:00
|
|
|
g_free(result);
|
|
|
|
return NULL;
|
2017-07-14 16:28:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = RegQueryValueExA(key, "ProductName", NULL, NULL,
|
|
|
|
(LPBYTE)result, &size);
|
|
|
|
if (err == ERROR_MORE_DATA) {
|
|
|
|
slog("ProductName longer than expected (%lu bytes), retrying",
|
|
|
|
size);
|
|
|
|
g_free(result);
|
|
|
|
result = NULL;
|
|
|
|
if (size > 0) {
|
|
|
|
result = g_malloc0(size);
|
|
|
|
err = RegQueryValueExA(key, "ProductName", NULL, NULL,
|
|
|
|
(LPBYTE)result, &size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (err != ERROR_SUCCESS) {
|
|
|
|
error_setg_win32(errp, err, "failed to retrive ProductName");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2021-07-12 18:24:44 +02:00
|
|
|
RegCloseKey(key);
|
2017-07-14 16:28:56 +02:00
|
|
|
return result;
|
|
|
|
|
|
|
|
fail:
|
2021-07-12 18:24:44 +02:00
|
|
|
if (key != INVALID_HANDLE_VALUE) {
|
|
|
|
RegCloseKey(key);
|
|
|
|
}
|
2017-07-14 16:28:56 +02:00
|
|
|
g_free(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *ga_get_current_arch(void)
|
|
|
|
{
|
|
|
|
SYSTEM_INFO info;
|
|
|
|
GetNativeSystemInfo(&info);
|
|
|
|
char *result = NULL;
|
|
|
|
switch (info.wProcessorArchitecture) {
|
|
|
|
case PROCESSOR_ARCHITECTURE_AMD64:
|
|
|
|
result = g_strdup("x86_64");
|
|
|
|
break;
|
|
|
|
case PROCESSOR_ARCHITECTURE_ARM:
|
|
|
|
result = g_strdup("arm");
|
|
|
|
break;
|
|
|
|
case PROCESSOR_ARCHITECTURE_IA64:
|
|
|
|
result = g_strdup("ia64");
|
|
|
|
break;
|
|
|
|
case PROCESSOR_ARCHITECTURE_INTEL:
|
|
|
|
result = g_strdup("x86");
|
|
|
|
break;
|
|
|
|
case PROCESSOR_ARCHITECTURE_UNKNOWN:
|
|
|
|
default:
|
|
|
|
slog("unknown processor architecture 0x%0x",
|
|
|
|
info.wProcessorArchitecture);
|
|
|
|
result = g_strdup("unknown");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
GuestOSInfo *qmp_guest_get_osinfo(Error **errp)
|
|
|
|
{
|
|
|
|
Error *local_err = NULL;
|
|
|
|
OSVERSIONINFOEXW os_version = {0};
|
|
|
|
bool server;
|
|
|
|
char *product_name;
|
|
|
|
GuestOSInfo *info;
|
|
|
|
|
|
|
|
ga_get_win_version(&os_version, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
server = os_version.wProductType != VER_NT_WORKSTATION;
|
error: Eliminate error_propagate() with Coccinelle, part 1
When all we do with an Error we receive into a local variable is
propagating to somewhere else, we can just as well receive it there
right away. Convert
if (!foo(..., &err)) {
...
error_propagate(errp, err);
...
return ...
}
to
if (!foo(..., errp)) {
...
...
return ...
}
where nothing else needs @err. Coccinelle script:
@rule1 forall@
identifier fun, err, errp, lbl;
expression list args, args2;
binary operator op;
constant c1, c2;
symbol false;
@@
if (
(
- fun(args, &err, args2)
+ fun(args, errp, args2)
|
- !fun(args, &err, args2)
+ !fun(args, errp, args2)
|
- fun(args, &err, args2) op c1
+ fun(args, errp, args2) op c1
)
)
{
... when != err
when != lbl:
when strict
- error_propagate(errp, err);
... when != err
(
return;
|
return c2;
|
return false;
)
}
@rule2 forall@
identifier fun, err, errp, lbl;
expression list args, args2;
expression var;
binary operator op;
constant c1, c2;
symbol false;
@@
- var = fun(args, &err, args2);
+ var = fun(args, errp, args2);
... when != err
if (
(
var
|
!var
|
var op c1
)
)
{
... when != err
when != lbl:
when strict
- error_propagate(errp, err);
... when != err
(
return;
|
return c2;
|
return false;
|
return var;
)
}
@depends on rule1 || rule2@
identifier err;
@@
- Error *err = NULL;
... when != err
Not exactly elegant, I'm afraid.
The "when != lbl:" is necessary to avoid transforming
if (fun(args, &err)) {
goto out
}
...
out:
error_propagate(errp, err);
even though other paths to label out still need the error_propagate().
For an actual example, see sclp_realize().
Without the "when strict", Coccinelle transforms vfio_msix_setup(),
incorrectly. I don't know what exactly "when strict" does, only that
it helps here.
The match of return is narrower than what I want, but I can't figure
out how to express "return where the operand doesn't use @err". For
an example where it's too narrow, see vfio_intx_enable().
Silently fails to convert hw/arm/armsse.c, because Coccinelle gets
confused by ARMSSE being used both as typedef and function-like macro
there. Converted manually.
Line breaks tidied up manually. One nested declaration of @local_err
deleted manually. Preexisting unwanted blank line dropped in
hw/riscv/sifive_e.c.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <20200707160613.848843-35-armbru@redhat.com>
2020-07-07 18:06:02 +02:00
|
|
|
product_name = ga_get_win_product_name(errp);
|
2017-07-14 16:28:56 +02:00
|
|
|
if (product_name == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
info = g_new0(GuestOSInfo, 1);
|
|
|
|
|
|
|
|
info->has_kernel_version = true;
|
|
|
|
info->kernel_version = g_strdup_printf("%lu.%lu",
|
|
|
|
os_version.dwMajorVersion,
|
|
|
|
os_version.dwMinorVersion);
|
|
|
|
info->has_kernel_release = true;
|
|
|
|
info->kernel_release = g_strdup_printf("%lu",
|
|
|
|
os_version.dwBuildNumber);
|
|
|
|
info->has_machine = true;
|
|
|
|
info->machine = ga_get_current_arch();
|
|
|
|
|
|
|
|
info->has_id = true;
|
|
|
|
info->id = g_strdup("mswindows");
|
|
|
|
info->has_name = true;
|
|
|
|
info->name = g_strdup("Microsoft Windows");
|
|
|
|
info->has_pretty_name = true;
|
|
|
|
info->pretty_name = product_name;
|
|
|
|
info->has_version = true;
|
|
|
|
info->version = ga_get_win_name(&os_version, false);
|
|
|
|
info->has_version_id = true;
|
|
|
|
info->version_id = ga_get_win_name(&os_version, true);
|
|
|
|
info->has_variant = true;
|
|
|
|
info->variant = g_strdup(server ? "server" : "client");
|
|
|
|
info->has_variant_id = true;
|
|
|
|
info->variant_id = g_strdup(server ? "server" : "client");
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
2020-07-21 17:40:41 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Safely get device property. Returned strings are using wide characters.
|
|
|
|
* Caller is responsible for freeing the buffer.
|
|
|
|
*/
|
|
|
|
static LPBYTE cm_get_property(DEVINST devInst, const DEVPROPKEY *propName,
|
|
|
|
PDEVPROPTYPE propType)
|
|
|
|
{
|
|
|
|
CONFIGRET cr;
|
|
|
|
g_autofree LPBYTE buffer = NULL;
|
|
|
|
ULONG buffer_len = 0;
|
|
|
|
|
|
|
|
/* First query for needed space */
|
|
|
|
cr = CM_Get_DevNode_PropertyW(devInst, propName, propType,
|
|
|
|
buffer, &buffer_len, 0);
|
|
|
|
if (cr != CR_SUCCESS && cr != CR_BUFFER_SMALL) {
|
|
|
|
|
|
|
|
slog("failed to get property size, error=0x%lx", cr);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
buffer = g_new0(BYTE, buffer_len + 1);
|
|
|
|
cr = CM_Get_DevNode_PropertyW(devInst, propName, propType,
|
|
|
|
buffer, &buffer_len, 0);
|
|
|
|
if (cr != CR_SUCCESS) {
|
|
|
|
slog("failed to get device property, error=0x%lx", cr);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return g_steal_pointer(&buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GStrv ga_get_hardware_ids(DEVINST devInstance)
|
|
|
|
{
|
|
|
|
GArray *values = NULL;
|
|
|
|
DEVPROPTYPE cm_type;
|
|
|
|
LPWSTR id;
|
|
|
|
g_autofree LPWSTR property = (LPWSTR)cm_get_property(devInstance,
|
|
|
|
&qga_DEVPKEY_Device_HardwareIds, &cm_type);
|
|
|
|
if (property == NULL) {
|
|
|
|
slog("failed to get hardware IDs");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (*property == '\0') {
|
|
|
|
/* empty list */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
values = g_array_new(TRUE, TRUE, sizeof(gchar *));
|
|
|
|
for (id = property; '\0' != *id; id += lstrlenW(id) + 1) {
|
|
|
|
gchar *id8 = g_utf16_to_utf8(id, -1, NULL, NULL, NULL);
|
|
|
|
g_array_append_val(values, id8);
|
|
|
|
}
|
|
|
|
return (GStrv)g_array_free(values, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* https://docs.microsoft.com/en-us/windows-hardware/drivers/install/identifiers-for-pci-devices
|
|
|
|
*/
|
|
|
|
#define DEVICE_PCI_RE "PCI\\\\VEN_(1AF4|1B36)&DEV_([0-9A-B]{4})(&|$)"
|
|
|
|
|
|
|
|
GuestDeviceInfoList *qmp_guest_get_devices(Error **errp)
|
|
|
|
{
|
2021-01-13 23:10:13 +01:00
|
|
|
GuestDeviceInfoList *head = NULL, **tail = &head;
|
2020-07-21 17:40:41 +02:00
|
|
|
HDEVINFO dev_info = INVALID_HANDLE_VALUE;
|
|
|
|
SP_DEVINFO_DATA dev_info_data;
|
|
|
|
int i, j;
|
|
|
|
GError *gerr = NULL;
|
|
|
|
g_autoptr(GRegex) device_pci_re = NULL;
|
|
|
|
DEVPROPTYPE cm_type;
|
|
|
|
|
|
|
|
device_pci_re = g_regex_new(DEVICE_PCI_RE,
|
|
|
|
G_REGEX_ANCHORED | G_REGEX_OPTIMIZE, 0,
|
|
|
|
&gerr);
|
|
|
|
g_assert(device_pci_re != NULL);
|
|
|
|
|
|
|
|
dev_info_data.cbSize = sizeof(SP_DEVINFO_DATA);
|
|
|
|
dev_info = SetupDiGetClassDevs(0, 0, 0, DIGCF_PRESENT | DIGCF_ALLCLASSES);
|
|
|
|
if (dev_info == INVALID_HANDLE_VALUE) {
|
|
|
|
error_setg(errp, "failed to get device tree");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
slog("enumerating devices");
|
|
|
|
for (i = 0; SetupDiEnumDeviceInfo(dev_info, i, &dev_info_data); i++) {
|
|
|
|
bool skip = true;
|
|
|
|
g_autofree LPWSTR name = NULL;
|
|
|
|
g_autofree LPFILETIME date = NULL;
|
|
|
|
g_autofree LPWSTR version = NULL;
|
|
|
|
g_auto(GStrv) hw_ids = NULL;
|
|
|
|
g_autoptr(GuestDeviceInfo) device = g_new0(GuestDeviceInfo, 1);
|
|
|
|
g_autofree char *vendor_id = NULL;
|
|
|
|
g_autofree char *device_id = NULL;
|
|
|
|
|
|
|
|
name = (LPWSTR)cm_get_property(dev_info_data.DevInst,
|
|
|
|
&qga_DEVPKEY_NAME, &cm_type);
|
|
|
|
if (name == NULL) {
|
|
|
|
slog("failed to get device description");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
device->driver_name = g_utf16_to_utf8(name, -1, NULL, NULL, NULL);
|
|
|
|
if (device->driver_name == NULL) {
|
|
|
|
error_setg(errp, "conversion to utf8 failed (driver name)");
|
2020-10-21 09:15:19 +02:00
|
|
|
return NULL;
|
2020-07-21 17:40:41 +02:00
|
|
|
}
|
|
|
|
slog("querying device: %s", device->driver_name);
|
|
|
|
hw_ids = ga_get_hardware_ids(dev_info_data.DevInst);
|
|
|
|
if (hw_ids == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (j = 0; hw_ids[j] != NULL; j++) {
|
2021-06-10 17:58:11 +02:00
|
|
|
g_autoptr(GMatchInfo) match_info;
|
2020-10-21 09:15:17 +02:00
|
|
|
GuestDeviceIdPCI *id;
|
2020-07-21 17:40:41 +02:00
|
|
|
if (!g_regex_match(device_pci_re, hw_ids[j], 0, &match_info)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
skip = false;
|
|
|
|
|
|
|
|
vendor_id = g_match_info_fetch(match_info, 1);
|
|
|
|
device_id = g_match_info_fetch(match_info, 2);
|
|
|
|
|
2020-10-21 09:15:17 +02:00
|
|
|
device->id = g_new0(GuestDeviceId, 1);
|
|
|
|
device->has_id = true;
|
2020-10-21 09:15:20 +02:00
|
|
|
device->id->type = GUEST_DEVICE_TYPE_PCI;
|
|
|
|
id = &device->id->u.pci;
|
|
|
|
id->vendor_id = g_ascii_strtoull(vendor_id, NULL, 16);
|
|
|
|
id->device_id = g_ascii_strtoull(device_id, NULL, 16);
|
2020-07-21 17:40:41 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (skip) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
version = (LPWSTR)cm_get_property(dev_info_data.DevInst,
|
|
|
|
&qga_DEVPKEY_Device_DriverVersion, &cm_type);
|
|
|
|
if (version == NULL) {
|
|
|
|
slog("failed to get driver version");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
device->driver_version = g_utf16_to_utf8(version, -1, NULL,
|
|
|
|
NULL, NULL);
|
|
|
|
if (device->driver_version == NULL) {
|
|
|
|
error_setg(errp, "conversion to utf8 failed (driver version)");
|
2020-10-21 09:15:19 +02:00
|
|
|
return NULL;
|
2020-07-21 17:40:41 +02:00
|
|
|
}
|
|
|
|
device->has_driver_version = true;
|
|
|
|
|
|
|
|
date = (LPFILETIME)cm_get_property(dev_info_data.DevInst,
|
|
|
|
&qga_DEVPKEY_Device_DriverDate, &cm_type);
|
|
|
|
if (date == NULL) {
|
|
|
|
slog("failed to get driver date");
|
|
|
|
continue;
|
|
|
|
}
|
2020-10-21 09:15:18 +02:00
|
|
|
device->driver_date = filetime_to_ns(date);
|
2020-07-21 17:40:41 +02:00
|
|
|
device->has_driver_date = true;
|
|
|
|
|
2020-10-21 09:15:18 +02:00
|
|
|
slog("driver: %s\ndriver version: %" PRId64 ",%s\n",
|
|
|
|
device->driver_name, device->driver_date,
|
|
|
|
device->driver_version);
|
2021-01-13 23:10:13 +01:00
|
|
|
QAPI_LIST_APPEND(tail, g_steal_pointer(&device));
|
2020-07-21 17:40:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dev_info != INVALID_HANDLE_VALUE) {
|
|
|
|
SetupDiDestroyDeviceInfoList(dev_info);
|
|
|
|
}
|
|
|
|
return head;
|
|
|
|
}
|