2012-01-20 08:19:27 +04:00
|
|
|
/*
|
|
|
|
* QEMU Guest Agent common/cross-platform command implementations
|
|
|
|
*
|
|
|
|
* Copyright IBM Corp. 2012
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Michael Roth <mdroth@linux.vnet.ibm.com>
|
|
|
|
*
|
|
|
|
* 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 20:49:58 +03:00
|
|
|
#include "qemu/osdep.h"
|
2020-04-14 16:30:44 +03:00
|
|
|
#include "qemu/units.h"
|
2018-05-03 22:50:57 +03:00
|
|
|
#include "guest-agent-core.h"
|
2018-02-11 12:36:05 +03:00
|
|
|
#include "qga-qapi-commands.h"
|
2018-02-01 14:18:31 +03:00
|
|
|
#include "qapi/error.h"
|
2015-11-23 18:37:07 +03:00
|
|
|
#include "qemu/base64.h"
|
2016-03-20 20:16:19 +03:00
|
|
|
#include "qemu/cutils.h"
|
2020-04-14 16:30:43 +03:00
|
|
|
#include "commands-common.h"
|
2012-01-20 08:19:27 +04:00
|
|
|
|
2015-10-13 18:41:23 +03:00
|
|
|
/* Maximum captured guest-exec out_data/err_data - 16MB */
|
2020-10-26 12:05:38 +03:00
|
|
|
#define GUEST_EXEC_MAX_OUTPUT (16 * 1024 * 1024)
|
2015-10-13 18:41:23 +03:00
|
|
|
/* Allocation and I/O buffer for reading guest-exec out_data/err_data - 4KB */
|
2020-10-26 12:05:38 +03:00
|
|
|
#define GUEST_EXEC_IO_SIZE (4 * 1024)
|
2020-04-14 16:30:44 +03:00
|
|
|
/*
|
|
|
|
* Maximum file size to read - 48MB
|
|
|
|
*
|
|
|
|
* (48MB + Base64 3:4 overhead = JSON parser 64 MB limit)
|
|
|
|
*/
|
|
|
|
#define GUEST_FILE_READ_COUNT_MAX (48 * MiB)
|
2015-10-13 18:41:23 +03:00
|
|
|
|
2012-01-20 08:19:27 +04:00
|
|
|
/* Note: in some situations, like with the fsfreeze, logging may be
|
2023-02-07 10:51:11 +03:00
|
|
|
* temporarily disabled. if it is necessary that a command be able
|
|
|
|
* to log for accounting purposes, check ga_logging_enabled() beforehand.
|
2012-01-20 08:19:27 +04:00
|
|
|
*/
|
|
|
|
void slog(const gchar *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
g_logv("syslog", G_LOG_LEVEL_INFO, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2012-02-07 23:56:48 +04:00
|
|
|
int64_t qmp_guest_sync_delimited(int64_t id, Error **errp)
|
|
|
|
{
|
|
|
|
ga_set_response_delimited(ga_state);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2012-01-20 08:19:27 +04:00
|
|
|
int64_t qmp_guest_sync(int64_t id, Error **errp)
|
|
|
|
{
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2014-05-02 15:26:30 +04:00
|
|
|
void qmp_guest_ping(Error **errp)
|
2012-01-20 08:19:27 +04:00
|
|
|
{
|
|
|
|
slog("guest-ping called");
|
|
|
|
}
|
|
|
|
|
2020-03-16 20:18:24 +03:00
|
|
|
static void qmp_command_info(const QmpCommand *cmd, void *opaque)
|
2012-01-20 08:19:27 +04:00
|
|
|
{
|
2013-10-09 07:25:07 +04:00
|
|
|
GuestAgentInfo *info = opaque;
|
2012-01-20 08:19:27 +04:00
|
|
|
GuestAgentCommandInfo *cmd_info;
|
|
|
|
|
2015-09-14 14:50:44 +03:00
|
|
|
cmd_info = g_new0(GuestAgentCommandInfo, 1);
|
2013-10-09 07:25:07 +04:00
|
|
|
cmd_info->name = g_strdup(qmp_command_name(cmd));
|
|
|
|
cmd_info->enabled = qmp_command_is_enabled(cmd);
|
2013-10-09 06:37:26 +04:00
|
|
|
cmd_info->success_response = qmp_has_success_response(cmd);
|
2012-01-20 08:19:27 +04:00
|
|
|
|
2020-11-13 04:13:37 +03:00
|
|
|
QAPI_LIST_PREPEND(info->supported_commands, cmd_info);
|
2013-10-09 07:25:07 +04:00
|
|
|
}
|
2012-01-20 08:19:27 +04:00
|
|
|
|
2014-05-02 15:26:30 +04:00
|
|
|
struct GuestAgentInfo *qmp_guest_info(Error **errp)
|
2013-10-09 07:25:07 +04:00
|
|
|
{
|
2015-09-14 14:50:44 +03:00
|
|
|
GuestAgentInfo *info = g_new0(GuestAgentInfo, 1);
|
2012-01-20 08:19:27 +04:00
|
|
|
|
2013-10-09 07:25:07 +04:00
|
|
|
info->version = g_strdup(QEMU_VERSION);
|
2017-03-03 15:32:25 +03:00
|
|
|
qmp_for_each_command(&ga_commands, qmp_command_info, info);
|
2012-01-20 08:19:27 +04:00
|
|
|
return info;
|
|
|
|
}
|
2015-10-13 18:41:20 +03:00
|
|
|
|
2015-10-13 18:41:23 +03:00
|
|
|
struct GuestExecIOData {
|
|
|
|
guchar *data;
|
|
|
|
gsize size;
|
|
|
|
gsize length;
|
2016-10-01 00:31:02 +03:00
|
|
|
bool closed;
|
2015-10-13 18:41:23 +03:00
|
|
|
bool truncated;
|
|
|
|
const char *name;
|
|
|
|
};
|
|
|
|
typedef struct GuestExecIOData GuestExecIOData;
|
|
|
|
|
2015-10-13 18:41:20 +03:00
|
|
|
struct GuestExecInfo {
|
|
|
|
GPid pid;
|
|
|
|
int64_t pid_numeric;
|
|
|
|
gint status;
|
2015-10-13 18:41:23 +03:00
|
|
|
bool has_output;
|
2016-10-01 00:31:02 +03:00
|
|
|
bool finished;
|
2015-10-13 18:41:23 +03:00
|
|
|
GuestExecIOData in;
|
|
|
|
GuestExecIOData out;
|
|
|
|
GuestExecIOData err;
|
2015-10-13 18:41:20 +03:00
|
|
|
QTAILQ_ENTRY(GuestExecInfo) next;
|
|
|
|
};
|
|
|
|
typedef struct GuestExecInfo GuestExecInfo;
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
QTAILQ_HEAD(, GuestExecInfo) processes;
|
|
|
|
} guest_exec_state = {
|
|
|
|
.processes = QTAILQ_HEAD_INITIALIZER(guest_exec_state.processes),
|
|
|
|
};
|
|
|
|
|
|
|
|
static int64_t gpid_to_int64(GPid pid)
|
|
|
|
{
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
return GetProcessId(pid);
|
|
|
|
#else
|
|
|
|
return (int64_t)pid;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static GuestExecInfo *guest_exec_info_add(GPid pid)
|
|
|
|
{
|
|
|
|
GuestExecInfo *gei;
|
|
|
|
|
|
|
|
gei = g_new0(GuestExecInfo, 1);
|
|
|
|
gei->pid = pid;
|
|
|
|
gei->pid_numeric = gpid_to_int64(pid);
|
|
|
|
QTAILQ_INSERT_TAIL(&guest_exec_state.processes, gei, next);
|
|
|
|
|
|
|
|
return gei;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GuestExecInfo *guest_exec_info_find(int64_t pid_numeric)
|
|
|
|
{
|
|
|
|
GuestExecInfo *gei;
|
|
|
|
|
|
|
|
QTAILQ_FOREACH(gei, &guest_exec_state.processes, next) {
|
|
|
|
if (gei->pid_numeric == pid_numeric) {
|
|
|
|
return gei;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-12-05 20:46:26 +03:00
|
|
|
GuestExecStatus *qmp_guest_exec_status(int64_t pid, Error **errp)
|
2015-10-13 18:41:20 +03:00
|
|
|
{
|
|
|
|
GuestExecInfo *gei;
|
|
|
|
GuestExecStatus *ges;
|
|
|
|
|
|
|
|
slog("guest-exec-status called, pid: %u", (uint32_t)pid);
|
|
|
|
|
|
|
|
gei = guest_exec_info_find(pid);
|
|
|
|
if (gei == NULL) {
|
2023-10-31 14:10:55 +03:00
|
|
|
error_setg(errp, "PID " PRId64 " does not exist");
|
2015-10-13 18:41:20 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ges = g_new0(GuestExecStatus, 1);
|
|
|
|
|
2022-04-20 16:26:22 +03:00
|
|
|
bool finished = gei->finished;
|
2015-10-13 18:41:23 +03:00
|
|
|
|
|
|
|
/* need to wait till output channels are closed
|
|
|
|
* to be sure we captured all output at this point */
|
|
|
|
if (gei->has_output) {
|
2022-04-20 16:26:22 +03:00
|
|
|
finished &= gei->out.closed && gei->err.closed;
|
2015-10-13 18:41:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ges->exited = finished;
|
|
|
|
if (finished) {
|
2015-10-13 18:41:20 +03:00
|
|
|
/* Glib has no portable way to parse exit status.
|
|
|
|
* On UNIX, we can get either exit code from normal termination
|
|
|
|
* or signal number.
|
|
|
|
* On Windows, it is either the same exit code or the exception
|
|
|
|
* value for an unhandled exception that caused the process
|
|
|
|
* to terminate.
|
|
|
|
* See MSDN for GetExitCodeProcess() and ntstatus.h for possible
|
|
|
|
* well-known codes, e.g. C0000005 ACCESS_DENIED - analog of SIGSEGV
|
|
|
|
* References:
|
|
|
|
* https://msdn.microsoft.com/en-us/library/windows/desktop/ms683189(v=vs.85).aspx
|
|
|
|
* https://msdn.microsoft.com/en-us/library/aa260331(v=vs.60).aspx
|
|
|
|
*/
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
/* Additionally WIN32 does not provide any additional information
|
2016-03-23 17:59:57 +03:00
|
|
|
* on whether the child exited or terminated via signal.
|
|
|
|
* We use this simple range check to distinguish application exit code
|
2015-10-13 18:41:20 +03:00
|
|
|
* (usually value less then 256) and unhandled exception code with
|
|
|
|
* ntstatus (always value greater then 0xC0000005). */
|
|
|
|
if ((uint32_t)gei->status < 0xC0000000U) {
|
|
|
|
ges->has_exitcode = true;
|
|
|
|
ges->exitcode = gei->status;
|
|
|
|
} else {
|
|
|
|
ges->has_signal = true;
|
|
|
|
ges->signal = gei->status;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (WIFEXITED(gei->status)) {
|
|
|
|
ges->has_exitcode = true;
|
|
|
|
ges->exitcode = WEXITSTATUS(gei->status);
|
|
|
|
} else if (WIFSIGNALED(gei->status)) {
|
|
|
|
ges->has_signal = true;
|
|
|
|
ges->signal = WTERMSIG(gei->status);
|
|
|
|
}
|
|
|
|
#endif
|
2015-10-13 18:41:23 +03:00
|
|
|
if (gei->out.length > 0) {
|
|
|
|
ges->out_data = g_base64_encode(gei->out.data, gei->out.length);
|
|
|
|
ges->has_out_truncated = gei->out.truncated;
|
|
|
|
}
|
2023-10-01 21:38:25 +03:00
|
|
|
g_free(gei->out.data);
|
2015-10-13 18:41:23 +03:00
|
|
|
|
|
|
|
if (gei->err.length > 0) {
|
|
|
|
ges->err_data = g_base64_encode(gei->err.data, gei->err.length);
|
|
|
|
ges->has_err_truncated = gei->err.truncated;
|
|
|
|
}
|
2023-10-01 21:38:25 +03:00
|
|
|
g_free(gei->err.data);
|
2015-10-13 18:41:23 +03:00
|
|
|
|
2015-10-13 18:41:20 +03:00
|
|
|
QTAILQ_REMOVE(&guest_exec_state.processes, gei, next);
|
|
|
|
g_free(gei);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ges;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get environment variables or arguments array for execve(). */
|
|
|
|
static char **guest_exec_get_args(const strList *entry, bool log)
|
|
|
|
{
|
|
|
|
const strList *it;
|
|
|
|
int count = 1, i = 0; /* reserve for NULL terminator */
|
|
|
|
char **args;
|
|
|
|
char *str; /* for logging array of arguments */
|
|
|
|
size_t str_size = 1;
|
|
|
|
|
|
|
|
for (it = entry; it != NULL; it = it->next) {
|
|
|
|
count++;
|
|
|
|
str_size += 1 + strlen(it->value);
|
|
|
|
}
|
|
|
|
|
|
|
|
str = g_malloc(str_size);
|
|
|
|
*str = 0;
|
2022-03-15 17:41:56 +03:00
|
|
|
args = g_new(char *, count);
|
2015-10-13 18:41:20 +03:00
|
|
|
for (it = entry; it != NULL; it = it->next) {
|
|
|
|
args[i++] = it->value;
|
|
|
|
pstrcat(str, str_size, it->value);
|
|
|
|
if (it->next) {
|
|
|
|
pstrcat(str, str_size, " ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
args[i] = NULL;
|
|
|
|
|
|
|
|
if (log) {
|
|
|
|
slog("guest-exec called: \"%s\"", str);
|
|
|
|
}
|
|
|
|
g_free(str);
|
|
|
|
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void guest_exec_child_watch(GPid pid, gint status, gpointer data)
|
|
|
|
{
|
|
|
|
GuestExecInfo *gei = (GuestExecInfo *)data;
|
|
|
|
|
|
|
|
g_debug("guest_exec_child_watch called, pid: %d, status: %u",
|
|
|
|
(int32_t)gpid_to_int64(pid), (uint32_t)status);
|
|
|
|
|
|
|
|
gei->status = status;
|
2022-04-20 16:26:22 +03:00
|
|
|
gei->finished = true;
|
2015-10-13 18:41:20 +03:00
|
|
|
|
|
|
|
g_spawn_close_pid(pid);
|
|
|
|
}
|
|
|
|
|
2015-10-13 18:41:21 +03:00
|
|
|
static void guest_exec_task_setup(gpointer data)
|
|
|
|
{
|
|
|
|
#if !defined(G_OS_WIN32)
|
2023-03-23 03:19:27 +03:00
|
|
|
bool has_merge = *(bool *)data;
|
2015-10-13 18:41:21 +03:00
|
|
|
struct sigaction sigact;
|
|
|
|
|
2023-03-23 03:19:27 +03:00
|
|
|
if (has_merge) {
|
|
|
|
/*
|
|
|
|
* FIXME: When `GLIB_VERSION_MIN_REQUIRED` is bumped to 2.58+, use
|
|
|
|
* g_spawn_async_with_fds() to be portable on windows. The current
|
|
|
|
* logic does not work on windows b/c `GSpawnChildSetupFunc` is run
|
|
|
|
* inside the parent, not the child.
|
|
|
|
*/
|
|
|
|
if (dup2(STDOUT_FILENO, STDERR_FILENO) != 0) {
|
|
|
|
slog("dup2() failed to merge stderr into stdout: %s",
|
|
|
|
strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset ignored signals back to default. */
|
2015-10-13 18:41:21 +03:00
|
|
|
memset(&sigact, 0, sizeof(struct sigaction));
|
|
|
|
sigact.sa_handler = SIG_DFL;
|
|
|
|
|
|
|
|
if (sigaction(SIGPIPE, &sigact, NULL) != 0) {
|
|
|
|
slog("sigaction() failed to reset child process's SIGPIPE: %s",
|
|
|
|
strerror(errno));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-10-13 18:41:23 +03:00
|
|
|
static gboolean guest_exec_input_watch(GIOChannel *ch,
|
|
|
|
GIOCondition cond, gpointer p_)
|
|
|
|
{
|
|
|
|
GuestExecIOData *p = (GuestExecIOData *)p_;
|
|
|
|
gsize bytes_written = 0;
|
|
|
|
GIOStatus status;
|
|
|
|
GError *gerr = NULL;
|
|
|
|
|
|
|
|
/* nothing left to write */
|
|
|
|
if (p->size == p->length) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = g_io_channel_write_chars(ch, (gchar *)p->data + p->length,
|
|
|
|
p->size - p->length, &bytes_written, &gerr);
|
|
|
|
|
|
|
|
/* can be not 0 even if not G_IO_STATUS_NORMAL */
|
|
|
|
if (bytes_written != 0) {
|
|
|
|
p->length += bytes_written;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* continue write, our callback will be called again */
|
|
|
|
if (status == G_IO_STATUS_NORMAL || status == G_IO_STATUS_AGAIN) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gerr) {
|
|
|
|
g_warning("qga: i/o error writing to input_data channel: %s",
|
|
|
|
gerr->message);
|
|
|
|
g_error_free(gerr);
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
g_io_channel_shutdown(ch, true, NULL);
|
|
|
|
g_io_channel_unref(ch);
|
2022-04-20 16:26:22 +03:00
|
|
|
p->closed = true;
|
2015-10-13 18:41:23 +03:00
|
|
|
g_free(p->data);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean guest_exec_output_watch(GIOChannel *ch,
|
|
|
|
GIOCondition cond, gpointer p_)
|
|
|
|
{
|
|
|
|
GuestExecIOData *p = (GuestExecIOData *)p_;
|
|
|
|
gsize bytes_read;
|
|
|
|
GIOStatus gstatus;
|
|
|
|
|
|
|
|
if (cond == G_IO_HUP || cond == G_IO_ERR) {
|
|
|
|
goto close;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p->size == p->length) {
|
|
|
|
gpointer t = NULL;
|
|
|
|
if (!p->truncated && p->size < GUEST_EXEC_MAX_OUTPUT) {
|
|
|
|
t = g_try_realloc(p->data, p->size + GUEST_EXEC_IO_SIZE);
|
|
|
|
}
|
|
|
|
if (t == NULL) {
|
|
|
|
/* ignore truncated output */
|
|
|
|
gchar buf[GUEST_EXEC_IO_SIZE];
|
|
|
|
|
|
|
|
p->truncated = true;
|
|
|
|
gstatus = g_io_channel_read_chars(ch, buf, sizeof(buf),
|
|
|
|
&bytes_read, NULL);
|
|
|
|
if (gstatus == G_IO_STATUS_EOF || gstatus == G_IO_STATUS_ERROR) {
|
|
|
|
goto close;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
p->size += GUEST_EXEC_IO_SIZE;
|
|
|
|
p->data = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calling read API once.
|
|
|
|
* On next available data our callback will be called again */
|
|
|
|
gstatus = g_io_channel_read_chars(ch, (gchar *)p->data + p->length,
|
|
|
|
p->size - p->length, &bytes_read, NULL);
|
|
|
|
if (gstatus == G_IO_STATUS_EOF || gstatus == G_IO_STATUS_ERROR) {
|
|
|
|
goto close;
|
|
|
|
}
|
|
|
|
|
|
|
|
p->length += bytes_read;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
close:
|
2016-04-06 08:43:30 +03:00
|
|
|
g_io_channel_shutdown(ch, true, NULL);
|
2015-10-13 18:41:23 +03:00
|
|
|
g_io_channel_unref(ch);
|
2022-04-20 16:26:22 +03:00
|
|
|
p->closed = true;
|
2015-10-13 18:41:23 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-03-23 03:19:26 +03:00
|
|
|
static GuestExecCaptureOutputMode ga_parse_capture_output(
|
|
|
|
GuestExecCaptureOutput *capture_output)
|
|
|
|
{
|
|
|
|
if (!capture_output)
|
|
|
|
return GUEST_EXEC_CAPTURE_OUTPUT_MODE_NONE;
|
|
|
|
else if (capture_output->type == QTYPE_QBOOL)
|
|
|
|
return capture_output->u.flag ? GUEST_EXEC_CAPTURE_OUTPUT_MODE_SEPARATED
|
|
|
|
: GUEST_EXEC_CAPTURE_OUTPUT_MODE_NONE;
|
|
|
|
else
|
|
|
|
return capture_output->u.mode;
|
|
|
|
}
|
|
|
|
|
2015-10-13 18:41:20 +03:00
|
|
|
GuestExec *qmp_guest_exec(const char *path,
|
|
|
|
bool has_arg, strList *arg,
|
|
|
|
bool has_env, strList *env,
|
2022-11-04 19:07:11 +03:00
|
|
|
const char *input_data,
|
2023-03-23 03:19:26 +03:00
|
|
|
GuestExecCaptureOutput *capture_output,
|
2019-12-05 20:46:26 +03:00
|
|
|
Error **errp)
|
2015-10-13 18:41:20 +03:00
|
|
|
{
|
|
|
|
GPid pid;
|
|
|
|
GuestExec *ge = NULL;
|
|
|
|
GuestExecInfo *gei;
|
|
|
|
char **argv, **envp;
|
|
|
|
strList arglist;
|
|
|
|
gboolean ret;
|
|
|
|
GError *gerr = NULL;
|
2015-10-13 18:41:23 +03:00
|
|
|
gint in_fd, out_fd, err_fd;
|
|
|
|
GIOChannel *in_ch, *out_ch, *err_ch;
|
|
|
|
GSpawnFlags flags;
|
2023-03-23 03:19:26 +03:00
|
|
|
bool has_output = false;
|
2023-03-23 03:19:27 +03:00
|
|
|
bool has_merge = false;
|
2023-03-23 03:19:26 +03:00
|
|
|
GuestExecCaptureOutputMode output_mode;
|
2021-08-09 16:10:29 +03:00
|
|
|
g_autofree uint8_t *input = NULL;
|
2015-11-23 18:37:07 +03:00
|
|
|
size_t ninput = 0;
|
2015-10-13 18:41:20 +03:00
|
|
|
|
|
|
|
arglist.value = (char *)path;
|
|
|
|
arglist.next = has_arg ? arg : NULL;
|
|
|
|
|
2022-11-04 19:07:11 +03:00
|
|
|
if (input_data) {
|
2019-12-05 20:46:26 +03:00
|
|
|
input = qbase64_decode(input_data, -1, &ninput, errp);
|
2015-11-23 18:37:07 +03:00
|
|
|
if (!input) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-13 18:41:20 +03:00
|
|
|
argv = guest_exec_get_args(&arglist, true);
|
2015-11-12 16:36:20 +03:00
|
|
|
envp = has_env ? guest_exec_get_args(env, false) : NULL;
|
2015-10-13 18:41:20 +03:00
|
|
|
|
2018-05-04 17:34:46 +03:00
|
|
|
flags = G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD |
|
|
|
|
G_SPAWN_SEARCH_PATH_FROM_ENVP;
|
2023-03-23 03:19:26 +03:00
|
|
|
|
|
|
|
output_mode = ga_parse_capture_output(capture_output);
|
|
|
|
switch (output_mode) {
|
|
|
|
case GUEST_EXEC_CAPTURE_OUTPUT_MODE_NONE:
|
2015-10-13 18:41:23 +03:00
|
|
|
flags |= G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL;
|
2023-03-23 03:19:26 +03:00
|
|
|
break;
|
|
|
|
case GUEST_EXEC_CAPTURE_OUTPUT_MODE_STDOUT:
|
|
|
|
has_output = true;
|
|
|
|
flags |= G_SPAWN_STDERR_TO_DEV_NULL;
|
|
|
|
break;
|
|
|
|
case GUEST_EXEC_CAPTURE_OUTPUT_MODE_STDERR:
|
|
|
|
has_output = true;
|
|
|
|
flags |= G_SPAWN_STDOUT_TO_DEV_NULL;
|
|
|
|
break;
|
|
|
|
case GUEST_EXEC_CAPTURE_OUTPUT_MODE_SEPARATED:
|
|
|
|
has_output = true;
|
|
|
|
break;
|
2023-03-23 03:19:27 +03:00
|
|
|
#if !defined(G_OS_WIN32)
|
|
|
|
case GUEST_EXEC_CAPTURE_OUTPUT_MODE_MERGED:
|
|
|
|
has_output = true;
|
|
|
|
has_merge = true;
|
|
|
|
break;
|
|
|
|
#endif
|
2023-03-23 03:19:26 +03:00
|
|
|
case GUEST_EXEC_CAPTURE_OUTPUT_MODE__MAX:
|
|
|
|
/* Silence warning; impossible branch */
|
|
|
|
break;
|
2015-10-13 18:41:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = g_spawn_async_with_pipes(NULL, argv, envp, flags,
|
2023-03-23 03:19:27 +03:00
|
|
|
guest_exec_task_setup, &has_merge, &pid, input_data ? &in_fd : NULL,
|
2015-10-13 18:41:23 +03:00
|
|
|
has_output ? &out_fd : NULL, has_output ? &err_fd : NULL, &gerr);
|
2015-10-13 18:41:20 +03:00
|
|
|
if (!ret) {
|
2024-05-14 13:58:27 +03:00
|
|
|
error_setg(errp, "%s", gerr->message);
|
2015-10-13 18:41:20 +03:00
|
|
|
g_error_free(gerr);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
ge = g_new0(GuestExec, 1);
|
|
|
|
ge->pid = gpid_to_int64(pid);
|
|
|
|
|
|
|
|
gei = guest_exec_info_add(pid);
|
2015-10-13 18:41:23 +03:00
|
|
|
gei->has_output = has_output;
|
2015-10-13 18:41:20 +03:00
|
|
|
g_child_watch_add(pid, guest_exec_child_watch, gei);
|
|
|
|
|
2022-11-04 19:07:11 +03:00
|
|
|
if (input_data) {
|
2021-08-09 16:10:29 +03:00
|
|
|
gei->in.data = g_steal_pointer(&input);
|
2015-11-23 18:37:07 +03:00
|
|
|
gei->in.size = ninput;
|
2015-10-13 18:41:23 +03:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
in_ch = g_io_channel_win32_new_fd(in_fd);
|
|
|
|
#else
|
|
|
|
in_ch = g_io_channel_unix_new(in_fd);
|
|
|
|
#endif
|
|
|
|
g_io_channel_set_encoding(in_ch, NULL, NULL);
|
|
|
|
g_io_channel_set_buffered(in_ch, false);
|
|
|
|
g_io_channel_set_flags(in_ch, G_IO_FLAG_NONBLOCK, NULL);
|
2016-04-06 08:43:30 +03:00
|
|
|
g_io_channel_set_close_on_unref(in_ch, true);
|
2015-10-13 18:41:23 +03:00
|
|
|
g_io_add_watch(in_ch, G_IO_OUT, guest_exec_input_watch, &gei->in);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (has_output) {
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
out_ch = g_io_channel_win32_new_fd(out_fd);
|
|
|
|
err_ch = g_io_channel_win32_new_fd(err_fd);
|
|
|
|
#else
|
|
|
|
out_ch = g_io_channel_unix_new(out_fd);
|
|
|
|
err_ch = g_io_channel_unix_new(err_fd);
|
|
|
|
#endif
|
|
|
|
g_io_channel_set_encoding(out_ch, NULL, NULL);
|
|
|
|
g_io_channel_set_encoding(err_ch, NULL, NULL);
|
|
|
|
g_io_channel_set_buffered(out_ch, false);
|
|
|
|
g_io_channel_set_buffered(err_ch, false);
|
2016-04-06 08:43:30 +03:00
|
|
|
g_io_channel_set_close_on_unref(out_ch, true);
|
|
|
|
g_io_channel_set_close_on_unref(err_ch, true);
|
2015-10-13 18:41:23 +03:00
|
|
|
g_io_add_watch(out_ch, G_IO_IN | G_IO_HUP,
|
|
|
|
guest_exec_output_watch, &gei->out);
|
|
|
|
g_io_add_watch(err_ch, G_IO_IN | G_IO_HUP,
|
|
|
|
guest_exec_output_watch, &gei->err);
|
|
|
|
}
|
|
|
|
|
2015-10-13 18:41:20 +03:00
|
|
|
done:
|
|
|
|
g_free(argv);
|
|
|
|
g_free(envp);
|
|
|
|
|
|
|
|
return ge;
|
|
|
|
}
|
2016-02-10 00:27:16 +03:00
|
|
|
|
|
|
|
/* Convert GuestFileWhence (either a raw integer or an enum value) into
|
|
|
|
* the guest's SEEK_ constants. */
|
|
|
|
int ga_parse_whence(GuestFileWhence *whence, Error **errp)
|
|
|
|
{
|
2020-03-20 18:05:07 +03:00
|
|
|
/*
|
|
|
|
* Exploit the fact that we picked values to match QGA_SEEK_*;
|
|
|
|
* however, we have to use a temporary variable since the union
|
|
|
|
* members may have different size.
|
|
|
|
*/
|
2016-02-10 00:27:16 +03:00
|
|
|
if (whence->type == QTYPE_QSTRING) {
|
2020-03-20 18:05:07 +03:00
|
|
|
int value = whence->u.name;
|
2017-06-07 19:35:58 +03:00
|
|
|
whence->type = QTYPE_QNUM;
|
2020-03-20 18:05:07 +03:00
|
|
|
whence->u.value = value;
|
2016-02-10 00:27:16 +03:00
|
|
|
}
|
|
|
|
switch (whence->u.value) {
|
|
|
|
case QGA_SEEK_SET:
|
|
|
|
return SEEK_SET;
|
|
|
|
case QGA_SEEK_CUR:
|
|
|
|
return SEEK_CUR;
|
|
|
|
case QGA_SEEK_END:
|
|
|
|
return SEEK_END;
|
|
|
|
}
|
|
|
|
error_setg(errp, "invalid whence code %"PRId64, whence->u.value);
|
|
|
|
return -1;
|
|
|
|
}
|
2017-04-04 09:46:31 +03:00
|
|
|
|
2019-12-05 20:46:26 +03:00
|
|
|
GuestHostName *qmp_guest_get_host_name(Error **errp)
|
2017-04-04 09:46:31 +03:00
|
|
|
{
|
|
|
|
GuestHostName *result = NULL;
|
2022-04-20 16:26:14 +03:00
|
|
|
g_autofree char *hostname = qga_get_host_name(errp);
|
2020-06-22 21:19:36 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We want to avoid using g_get_host_name() because that
|
|
|
|
* caches the result and we wouldn't reflect changes in the
|
|
|
|
* host name.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!hostname) {
|
|
|
|
hostname = g_strdup("localhost");
|
2017-04-04 09:46:31 +03:00
|
|
|
}
|
2020-06-22 21:19:36 +03:00
|
|
|
|
|
|
|
result = g_new0(GuestHostName, 1);
|
|
|
|
result->host_name = g_steal_pointer(&hostname);
|
2017-04-04 09:46:31 +03:00
|
|
|
return result;
|
|
|
|
}
|
2017-04-19 13:52:58 +03:00
|
|
|
|
|
|
|
GuestTimezone *qmp_guest_get_timezone(Error **errp)
|
|
|
|
{
|
|
|
|
GuestTimezone *info = NULL;
|
|
|
|
GTimeZone *tz = NULL;
|
|
|
|
gint64 now = 0;
|
|
|
|
gint32 intv = 0;
|
|
|
|
gchar const *name = NULL;
|
|
|
|
|
|
|
|
info = g_new0(GuestTimezone, 1);
|
|
|
|
tz = g_time_zone_new_local();
|
|
|
|
if (tz == NULL) {
|
2024-05-14 13:58:27 +03:00
|
|
|
error_setg(errp, "Couldn't retrieve local timezone");
|
2017-04-19 13:52:58 +03:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
now = g_get_real_time() / G_USEC_PER_SEC;
|
|
|
|
intv = g_time_zone_find_interval(tz, G_TIME_TYPE_UNIVERSAL, now);
|
|
|
|
info->offset = g_time_zone_get_offset(tz, intv);
|
|
|
|
name = g_time_zone_get_abbreviation(tz, intv);
|
|
|
|
if (name != NULL) {
|
|
|
|
info->zone = g_strdup(name);
|
|
|
|
}
|
|
|
|
g_time_zone_unref(tz);
|
|
|
|
|
|
|
|
return info;
|
|
|
|
|
|
|
|
error:
|
|
|
|
g_free(info);
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-04-14 16:30:43 +03:00
|
|
|
|
|
|
|
GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
|
|
|
|
int64_t count, Error **errp)
|
|
|
|
{
|
|
|
|
GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
|
|
|
|
GuestFileRead *read_data;
|
|
|
|
|
|
|
|
if (!gfh) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (!has_count) {
|
|
|
|
count = QGA_READ_COUNT_DEFAULT;
|
2020-04-14 16:30:44 +03:00
|
|
|
} else if (count < 0 || count > GUEST_FILE_READ_COUNT_MAX) {
|
2020-04-14 16:30:43 +03:00
|
|
|
error_setg(errp, "value '%" PRId64 "' is invalid for argument count",
|
|
|
|
count);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
read_data = guest_file_read_unsafe(gfh, count, errp);
|
|
|
|
if (!read_data) {
|
|
|
|
slog("guest-file-write failed, handle: %" PRId64, handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
return read_data;
|
|
|
|
}
|
2022-03-07 10:03:59 +03:00
|
|
|
|
|
|
|
int64_t qmp_guest_get_time(Error **errp)
|
|
|
|
{
|
|
|
|
return g_get_real_time() * 1000;
|
|
|
|
}
|