Merge remote-tracking branch 'remotes/mdroth/tags/qga-pull-2015-02-16-v2-tag' into staging

tag for qga-pull-2015-02-16-v2

v2:

* generalized QAPI function definition for guest-memory-block-size
  to guest-memory-block-info for future extensibility (Eric)

# gpg: Signature made Tue Feb 17 22:36:08 2015 GMT using RSA key ID F108B584
# gpg: Good signature from "Michael Roth <flukshun@gmail.com>"
# gpg:                 aka "Michael Roth <mdroth@utexas.edu>"
# gpg:                 aka "Michael Roth <mdroth@linux.vnet.ibm.com>"
# gpg: WARNING: This key is not certified with a trusted signature!
# gpg:          There is no indication that the signature belongs to the owner.
# Primary key fingerprint: CEAC C9E1 5534 EBAB B82D  3FA0 3353 C9CE F108 B584

* remotes/mdroth/tags/qga-pull-2015-02-16-v2-tag:
  qemu-ga-win: Fail loudly on bare 'set-time'
  qga: add memory block command that unsupported
  qga: implement qmp_guest_get_memory_block_info() for Linux with sysfs
  qga: implement qmp_guest_set_memory_blocks() for Linux with sysfs
  qga: implement qmp_guest_get_memory_blocks() for Linux with sysfs
  qga: introduce three guest memory block commmands with stubs
  qga: implement file commands for Windows guest
  guest agent: guest-file-open: refactoring
  utils: drop strtok_r from envlist_parse
  qga: add guest-set-user-password command

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
This commit is contained in:
Peter Maydell
2015-02-25 11:05:10 +00:00
5 changed files with 931 additions and 69 deletions
-1
View File
@@ -81,7 +81,6 @@ struct tm *gmtime_r(const time_t *timep, struct tm *result);
#undef localtime_r
struct tm *localtime_r(const time_t *timep, struct tm *result);
char *strtok_r(char *str, const char *delim, char **saveptr);
static inline void os_setup_signal_handling(void) {}
static inline void os_daemonize(void) {}
+460 -9
View File
File diff suppressed because it is too large Load Diff
+302 -43
View File
@@ -14,10 +14,13 @@
#include <glib.h>
#include <wtypes.h>
#include <powrprof.h>
#include <stdio.h>
#include <string.h>
#include "qga/guest-agent-core.h"
#include "qga/vss-win32.h"
#include "qga-qmp-commands.h"
#include "qapi/qmp/qerror.h"
#include "qemu/queue.h"
#ifndef SHTDN_REASON_FLAG_PLANNED
#define SHTDN_REASON_FLAG_PLANNED 0x80000000
@@ -29,6 +32,146 @@
(365 * (1970 - 1601) + \
(1970 - 1601) / 4 - 3))
#define INVALID_SET_FILE_POINTER ((DWORD)-1)
typedef struct GuestFileHandle {
int64_t id;
HANDLE fh;
QTAILQ_ENTRY(GuestFileHandle) next;
} GuestFileHandle;
static struct {
QTAILQ_HEAD(, GuestFileHandle) filehandles;
} guest_file_state;
typedef struct OpenFlags {
const char *forms;
DWORD desired_access;
DWORD creation_disposition;
} OpenFlags;
static OpenFlags guest_file_open_modes[] = {
{"r", GENERIC_READ, OPEN_EXISTING},
{"rb", GENERIC_READ, OPEN_EXISTING},
{"w", GENERIC_WRITE, CREATE_ALWAYS},
{"wb", GENERIC_WRITE, CREATE_ALWAYS},
{"a", GENERIC_WRITE, OPEN_ALWAYS },
{"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+", GENERIC_WRITE|GENERIC_READ, OPEN_ALWAYS },
{"ab+", GENERIC_WRITE|GENERIC_READ, OPEN_ALWAYS },
{"a+b", GENERIC_WRITE|GENERIC_READ, OPEN_ALWAYS }
};
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;
}
gfh = g_malloc0(sizeof(GuestFileHandle));
gfh->id = handle;
gfh->fh = fh;
QTAILQ_INSERT_TAIL(&guest_file_state.filehandles, gfh, next);
return handle;
}
static GuestFileHandle *guest_file_handle_find(int64_t id, Error **errp)
{
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;
}
int64_t qmp_guest_file_open(const char *path, bool has_mode,
const char *mode, Error **errp)
{
int64_t fd;
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;
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");
return -1;
}
fh = CreateFile(path, guest_flags->desired_access, share_mode, sa_attr,
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);
return -1;
}
fd = guest_file_handle_add(fh, errp);
if (fd < 0) {
CloseHandle(&fh);
error_setg(errp, "failed to add handle to qmp handle table");
return -1;
}
slog("guest-file-open, handle: % " PRId64, fd);
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);
}
static void acquire_privilege(const char *name, Error **errp)
{
HANDLE token = NULL;
@@ -113,43 +256,130 @@ void qmp_guest_shutdown(bool has_mode, const char *mode, Error **errp)
}
}
int64_t qmp_guest_file_open(const char *path, bool has_mode, const char *mode,
Error **errp)
{
error_set(errp, QERR_UNSUPPORTED);
return 0;
}
void qmp_guest_file_close(int64_t handle, Error **errp)
{
error_set(errp, QERR_UNSUPPORTED);
}
GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
int64_t count, Error **errp)
{
error_set(errp, QERR_UNSUPPORTED);
return 0;
GuestFileRead *read_data = NULL;
guchar *buf;
HANDLE fh;
bool is_ok;
DWORD read_count;
GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
if (!gfh) {
return NULL;
}
if (!has_count) {
count = QGA_READ_COUNT_DEFAULT;
} else if (count < 0) {
error_setg(errp, "value '%" PRId64
"' is invalid for argument count", count);
return NULL;
}
fh = gfh->fh;
buf = g_malloc0(count+1);
is_ok = ReadFile(fh, buf, count, &read_count, NULL);
if (!is_ok) {
error_setg_win32(errp, GetLastError(), "failed to read file");
slog("guest-file-read failed, handle %" PRId64, handle);
} else {
buf[read_count] = 0;
read_data = g_malloc0(sizeof(GuestFileRead));
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;
}
GuestFileWrite *qmp_guest_file_write(int64_t handle, const char *buf_b64,
bool has_count, int64_t count,
Error **errp)
{
error_set(errp, QERR_UNSUPPORTED);
return 0;
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;
buf = g_base64_decode(buf_b64, &buf_len);
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 {
write_data = g_malloc0(sizeof(GuestFileWrite));
write_data->count = (size_t) write_count;
}
done:
g_free(buf);
return write_data;
}
GuestFileSeek *qmp_guest_file_seek(int64_t handle, int64_t offset,
int64_t whence, Error **errp)
{
error_set(errp, QERR_UNSUPPORTED);
return 0;
GuestFileHandle *gfh;
GuestFileSeek *seek_data;
HANDLE fh;
LARGE_INTEGER new_pos, off_pos;
off_pos.QuadPart = offset;
BOOL res;
gfh = guest_file_handle_find(handle, errp);
if (!gfh) {
return NULL;
}
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;
}
void qmp_guest_file_flush(int64_t handle, Error **errp)
{
error_set(errp, QERR_UNSUPPORTED);
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");
}
}
static void guest_file_init(void)
{
QTAILQ_INIT(&guest_file_state.filehandles);
}
GuestFilesystemInfoList *qmp_guest_get_fsinfo(Error **errp)
@@ -395,31 +625,31 @@ void qmp_guest_set_time(bool has_time, int64_t time_ns, Error **errp)
FILETIME tf;
LONGLONG time;
if (has_time) {
/* Okay, user passed a time to set. Validate it. */
if (time_ns < 0 || time_ns / 100 > INT64_MAX - W32_FT_OFFSET) {
error_setg(errp, "Time %" PRId64 "is invalid", time_ns);
return;
}
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
*/
error_setg(errp, "Time argument is required on this platform");
return;
}
time = time_ns / 100 + W32_FT_OFFSET;
/* 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;
}
tf.dwLowDateTime = (DWORD) time;
tf.dwHighDateTime = (DWORD) (time >> 32);
time = time_ns / 100 + W32_FT_OFFSET;
if (!FileTimeToSystemTime(&tf, &ts)) {
error_setg(errp, "Failed to convert system time %d",
(int)GetLastError());
return;
}
} else {
/* Otherwise read the time from RTC which contains the correct value.
* Hopefully. */
GetSystemTime(&ts);
if (ts.wYear < 1601 || ts.wYear > 30827) {
error_setg(errp, "Failed to get time");
return;
}
tf.dwLowDateTime = (DWORD) time;
tf.dwHighDateTime = (DWORD) (time >> 32);
if (!FileTimeToSystemTime(&tf, &ts)) {
error_setg(errp, "Failed to convert system time %d",
(int)GetLastError());
return;
}
acquire_privilege(SE_SYSTEMTIME_NAME, &local_err);
@@ -446,14 +676,42 @@ int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp)
return -1;
}
void qmp_guest_set_user_password(const char *username,
const char *password,
bool crypted,
Error **errp)
{
error_set(errp, QERR_UNSUPPORTED);
}
GuestMemoryBlockList *qmp_guest_get_memory_blocks(Error **errp)
{
error_set(errp, QERR_UNSUPPORTED);
return NULL;
}
GuestMemoryBlockResponseList *
qmp_guest_set_memory_blocks(GuestMemoryBlockList *mem_blks, Error **errp)
{
error_set(errp, QERR_UNSUPPORTED);
return NULL;
}
GuestMemoryBlockInfo *qmp_guest_get_memory_block_info(Error **errp)
{
error_set(errp, QERR_UNSUPPORTED);
return NULL;
}
/* add unsupported commands to the blacklist */
GList *ga_command_blacklist_init(GList *blacklist)
{
const char *list_unsupported[] = {
"guest-file-open", "guest-file-close", "guest-file-read",
"guest-file-write", "guest-file-seek", "guest-file-flush",
"guest-suspend-hybrid", "guest-network-get-interfaces",
"guest-get-vcpus", "guest-set-vcpus",
"guest-set-user-password",
"guest-get-memory-blocks", "guest-set-memory-blocks",
"guest-get-memory-block-size",
"guest-fsfreeze-freeze-list", "guest-get-fsinfo",
"guest-fstrim", NULL};
char **p = (char **)list_unsupported;
@@ -482,4 +740,5 @@ void ga_command_state_init(GAState *s, GACommandState *cs)
if (!vss_initialized()) {
ga_command_state_add(cs, NULL, guest_fsfreeze_cleanup);
}
ga_command_state_add(cs, guest_file_init, NULL);
}
+154 -1
View File
@@ -121,7 +121,10 @@
# given value, then sets the Hardware Clock (RTC) to the
# current System Time. This will make it easier for a guest
# to resynchronize without waiting for NTP. If no @time is
# specified, then the time to set is read from RTC.
# specified, then the time to set is read from RTC. However,
# this may not be supported on all platforms (i.e. Windows).
# If that's the case users are advised to always pass a
# value.
#
# @time: #optional time of nanoseconds, relative to the Epoch
# of 1970-01-01 in UTC.
@@ -738,3 +741,153 @@
##
{ 'command': 'guest-get-fsinfo',
'returns': ['GuestFilesystemInfo'] }
##
# @guest-set-user-password
#
# @username: the user account whose password to change
# @password: the new password entry string, base64 encoded
# @crypted: true if password is already crypt()d, false if raw
#
# If the @crypted flag is true, it is the caller's responsibility
# to ensure the correct crypt() encryption scheme is used. This
# command does not attempt to interpret or report on the encryption
# scheme. Refer to the documentation of the guest operating system
# in question to determine what is supported.
#
# Note all guest operating systems will support use of the
# @crypted flag, as they may require the clear-text password
#
# The @password parameter must always be base64 encoded before
# transmission, even if already crypt()d, to ensure it is 8-bit
# safe when passed as JSON.
#
# Returns: Nothing on success.
#
# Since 2.3
##
{ 'command': 'guest-set-user-password',
'data': { 'username': 'str', 'password': 'str', 'crypted': 'bool' } }
# @GuestMemoryBlock:
#
# @phys-index: Arbitrary guest-specific unique identifier of the MEMORY BLOCK.
#
# @online: Whether the MEMORY BLOCK is enabled in guest.
#
# @can-offline: #optional Whether offlining the MEMORY BLOCK is possible.
# This member is always filled in by the guest agent when the
# structure is returned, and always ignored on input (hence it
# can be omitted then).
#
# Since: 2.3
##
{ 'type': 'GuestMemoryBlock',
'data': {'phys-index': 'uint64',
'online': 'bool',
'*can-offline': 'bool'} }
##
# @guest-get-memory-blocks:
#
# Retrieve the list of the guest's memory blocks.
#
# This is a read-only operation.
#
# Returns: The list of all memory blocks the guest knows about.
# Each memory block is put on the list exactly once, but their order
# is unspecified.
#
# Since: 2.3
##
{ 'command': 'guest-get-memory-blocks',
'returns': ['GuestMemoryBlock'] }
##
# @GuestMemoryBlockResponseType
#
# An enumeration of memory block operation result.
#
# @sucess: the operation of online/offline memory block is successful.
# @not-found: can't find the corresponding memoryXXX directory in sysfs.
# @operation-not-supported: for some old kernels, it does not support
# online or offline memory block.
# @operation-failed: the operation of online/offline memory block fails,
# because of some errors happen.
#
# Since: 2.3
##
{ 'enum': 'GuestMemoryBlockResponseType',
'data': ['success', 'not-found', 'operation-not-supported',
'operation-failed'] }
##
# @GuestMemoryBlockResponse:
#
# @phys-index: same with the 'phys-index' member of @GuestMemoryBlock.
#
# @response: the result of memory block operation.
#
# @error-code: #optional the error number.
# When memory block operation fails, we assign the value of
# 'errno' to this member, it indicates what goes wrong.
# When the operation succeeds, it will be omitted.
#
# Since: 2.3
##
{ 'type': 'GuestMemoryBlockResponse',
'data': { 'phys-index': 'uint64',
'response': 'GuestMemoryBlockResponseType',
'*error-code': 'int' }}
##
# @guest-set-memory-blocks:
#
# Attempt to reconfigure (currently: enable/disable) state of memory blocks
# inside the guest.
#
# The input list is processed node by node in order. In each node @phys-index
# is used to look up the guest MEMORY BLOCK, for which @online specifies the
# requested state. The set of distinct @phys-index's is only required to be a
# subset of the guest-supported identifiers. There's no restriction on list
# length or on repeating the same @phys-index (with possibly different @online
# field).
# Preferably the input list should describe a modified subset of
# @guest-get-memory-blocks' return value.
#
# Returns: The operation results, it is a list of @GuestMemoryBlockResponse,
# which is corresponding to the input list.
#
# Note: it will return NULL if the @mem-blks list was empty on input,
# or there is an error, and in this case, guest state will not be
# changed.
#
# Since: 2.3
##
{ 'command': 'guest-set-memory-blocks',
'data': {'mem-blks': ['GuestMemoryBlock'] },
'returns': ['GuestMemoryBlockResponse'] }
# @GuestMemoryBlockInfo:
#
# @size: the size (in bytes) of the guest memory blocks,
# which are the minimal units of memory block online/offline
# operations (also called Logical Memory Hotplug).
#
# Since: 2.3
##
{ 'type': 'GuestMemoryBlockInfo',
'data': {'size': 'uint64'} }
##
# @guest-get-memory-block-info:
#
# Get information relating to guest memory blocks.
#
# Returns: memory block size in bytes.
# Returns: @GuestMemoryBlockInfo
#
# Since 2.3
##
{ 'command': 'guest-get-memory-block-info',
'returns': 'GuestMemoryBlockInfo' }
+15 -15
View File
@@ -94,30 +94,30 @@ envlist_parse(envlist_t *envlist, const char *env,
{
char *tmpenv, *envvar;
char *envsave = NULL;
assert(callback != NULL);
int ret = 0;
assert(callback != NULL);
if ((envlist == NULL) || (env == NULL))
return (EINVAL);
/*
* We need to make temporary copy of the env string
* as strtok_r(3) modifies it while it tokenizes.
*/
if ((tmpenv = strdup(env)) == NULL)
return (errno);
envsave = tmpenv;
envvar = strtok_r(tmpenv, ",", &envsave);
while (envvar != NULL) {
if ((*callback)(envlist, envvar) != 0) {
free(tmpenv);
return (errno);
do {
envvar = strchr(tmpenv, ',');
if (envvar != NULL) {
*envvar = '\0';
}
if ((*callback)(envlist, tmpenv) != 0) {
ret = errno;
break;
}
envvar = strtok_r(NULL, ",", &envsave);
}
tmpenv = envvar + 1;
} while (envvar != NULL);
free(tmpenv);
return (0);
free(envsave);
return ret;
}
/*