2011-07-21 00:19:37 +04:00
|
|
|
/*
|
|
|
|
* QEMU Guest Agent
|
|
|
|
*
|
|
|
|
* Copyright IBM Corp. 2011
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Adam Litke <aglitke@linux.vnet.ibm.com>
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <glib.h>
|
|
|
|
#include <getopt.h>
|
2012-01-20 08:04:34 +04:00
|
|
|
#ifndef _WIN32
|
2011-07-21 00:19:37 +04:00
|
|
|
#include <syslog.h>
|
2012-01-20 08:04:34 +04:00
|
|
|
#endif
|
2011-07-21 00:19:37 +04:00
|
|
|
#include "json-streamer.h"
|
|
|
|
#include "json-parser.h"
|
|
|
|
#include "qint.h"
|
|
|
|
#include "qjson.h"
|
|
|
|
#include "qga/guest-agent-core.h"
|
|
|
|
#include "module.h"
|
|
|
|
#include "signal.h"
|
|
|
|
#include "qerror.h"
|
|
|
|
#include "error_int.h"
|
2011-12-07 08:03:42 +04:00
|
|
|
#include "qapi/qmp-core.h"
|
2012-01-19 10:18:20 +04:00
|
|
|
#include "qga/channel.h"
|
2012-01-22 02:42:27 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include "qga/service-win32.h"
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
2011-07-21 00:19:37 +04:00
|
|
|
|
qemu-ga: add initial win32 support
This adds a win32 channel implementation that makes qemu-ga functional
on Windows using virtio-serial (unix-listen/isa-serial not currently
implemented). Unlike with the posix implementation, we do not use
GIOChannel for the following reasons:
- glib calls stat() on an fd to check whether S_IFCHR is set, which is
the case for virtio-serial on win32. Because of that, a one-time
check to determine whether the channel is readable is done by making
a call to PeekConsoleInput(), which reports the underlying handle is
not a valid console handle, and thus we can never read from the
channel.
- if one goes as far as to "trick" glib into thinking it is a normal
file descripter, the buffering is done in such a way that data
written to the output stream will subsequently result in that same
data being read back as if it were input, causing an error loop.
furthermore, a forced flush of the channel only moves the data into a
secondary buffer managed by glib, so there's no way to prevent output
from getting read back as input.
The implementation here ties into the glib main loop by implementing a
custom GSource that continually submits asynchronous/overlapped I/O to
fill an GAChannel-managed read buffer, and tells glib to poll the
corresponding event handle for a completion whenever there is no
data/RPC in the read buffer to notify the main application about.
2012-01-21 05:01:30 +04:00
|
|
|
#ifndef _WIN32
|
2011-07-21 00:19:37 +04:00
|
|
|
#define QGA_VIRTIO_PATH_DEFAULT "/dev/virtio-ports/org.qemu.guest_agent.0"
|
qemu-ga: add initial win32 support
This adds a win32 channel implementation that makes qemu-ga functional
on Windows using virtio-serial (unix-listen/isa-serial not currently
implemented). Unlike with the posix implementation, we do not use
GIOChannel for the following reasons:
- glib calls stat() on an fd to check whether S_IFCHR is set, which is
the case for virtio-serial on win32. Because of that, a one-time
check to determine whether the channel is readable is done by making
a call to PeekConsoleInput(), which reports the underlying handle is
not a valid console handle, and thus we can never read from the
channel.
- if one goes as far as to "trick" glib into thinking it is a normal
file descripter, the buffering is done in such a way that data
written to the output stream will subsequently result in that same
data being read back as if it were input, causing an error loop.
furthermore, a forced flush of the channel only moves the data into a
secondary buffer managed by glib, so there's no way to prevent output
from getting read back as input.
The implementation here ties into the glib main loop by implementing a
custom GSource that continually submits asynchronous/overlapped I/O to
fill an GAChannel-managed read buffer, and tells glib to poll the
corresponding event handle for a completion whenever there is no
data/RPC in the read buffer to notify the main application about.
2012-01-21 05:01:30 +04:00
|
|
|
#else
|
|
|
|
#define QGA_VIRTIO_PATH_DEFAULT "\\\\.\\Global\\org.qemu.guest_agent.0"
|
|
|
|
#endif
|
2011-07-21 00:19:37 +04:00
|
|
|
#define QGA_PIDFILE_DEFAULT "/var/run/qemu-ga.pid"
|
|
|
|
|
|
|
|
struct GAState {
|
|
|
|
JSONMessageParser parser;
|
|
|
|
GMainLoop *main_loop;
|
2012-01-19 10:18:20 +04:00
|
|
|
GAChannel *channel;
|
2011-07-21 00:19:37 +04:00
|
|
|
bool virtio; /* fastpath to check for virtio to deal with poll() quirks */
|
|
|
|
GACommandState *command_state;
|
|
|
|
GLogLevelFlags log_level;
|
|
|
|
FILE *log_file;
|
|
|
|
bool logging_enabled;
|
2012-01-22 02:42:27 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
GAService service;
|
|
|
|
#endif
|
2011-07-21 00:19:37 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct GAState *ga_state;
|
|
|
|
|
2012-01-22 02:42:27 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
DWORD WINAPI service_ctrl_handler(DWORD ctrl, DWORD type, LPVOID data,
|
|
|
|
LPVOID ctx);
|
|
|
|
VOID WINAPI service_main(DWORD argc, TCHAR *argv[]);
|
|
|
|
#endif
|
|
|
|
|
2011-07-21 00:19:37 +04:00
|
|
|
static void quit_handler(int sig)
|
|
|
|
{
|
2011-08-28 23:45:40 +04:00
|
|
|
g_debug("received signal num %d, quitting", sig);
|
2011-07-21 00:19:37 +04:00
|
|
|
|
|
|
|
if (g_main_loop_is_running(ga_state->main_loop)) {
|
|
|
|
g_main_loop_quit(ga_state->main_loop);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-22 02:42:27 +04:00
|
|
|
#ifndef _WIN32
|
2012-01-19 10:18:20 +04:00
|
|
|
static gboolean register_signal_handlers(void)
|
2011-07-21 00:19:37 +04:00
|
|
|
{
|
|
|
|
struct sigaction sigact;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
memset(&sigact, 0, sizeof(struct sigaction));
|
|
|
|
sigact.sa_handler = quit_handler;
|
|
|
|
|
|
|
|
ret = sigaction(SIGINT, &sigact, NULL);
|
|
|
|
if (ret == -1) {
|
|
|
|
g_error("error configuring signal handler: %s", strerror(errno));
|
2012-01-19 10:18:20 +04:00
|
|
|
return false;
|
2011-07-21 00:19:37 +04:00
|
|
|
}
|
|
|
|
ret = sigaction(SIGTERM, &sigact, NULL);
|
|
|
|
if (ret == -1) {
|
|
|
|
g_error("error configuring signal handler: %s", strerror(errno));
|
2012-01-19 10:18:20 +04:00
|
|
|
return false;
|
2011-07-21 00:19:37 +04:00
|
|
|
}
|
2012-01-19 10:18:20 +04:00
|
|
|
return true;
|
2011-07-21 00:19:37 +04:00
|
|
|
}
|
2012-01-20 08:04:34 +04:00
|
|
|
#endif
|
2011-07-21 00:19:37 +04:00
|
|
|
|
|
|
|
static void usage(const char *cmd)
|
|
|
|
{
|
|
|
|
printf(
|
|
|
|
"Usage: %s -c <channel_opts>\n"
|
|
|
|
"QEMU Guest Agent %s\n"
|
|
|
|
"\n"
|
|
|
|
" -m, --method transport method: one of unix-listen, virtio-serial, or\n"
|
|
|
|
" isa-serial (virtio-serial is the default)\n"
|
|
|
|
" -p, --path device/socket path (%s is the default for virtio-serial)\n"
|
|
|
|
" -l, --logfile set logfile path, logs to stderr by default\n"
|
|
|
|
" -f, --pidfile specify pidfile (default is %s)\n"
|
|
|
|
" -v, --verbose log extra debugging information\n"
|
|
|
|
" -V, --version print version information and exit\n"
|
|
|
|
" -d, --daemonize become a daemon\n"
|
2012-01-22 02:42:27 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
" -s, --service service commands: install, uninstall\n"
|
2012-01-20 08:04:34 +04:00
|
|
|
#endif
|
2012-01-08 22:35:09 +04:00
|
|
|
" -b, --blacklist comma-separated list of RPCs to disable (no spaces, \"?\""
|
2011-12-07 08:03:42 +04:00
|
|
|
" to list available RPCs)\n"
|
2011-07-21 00:19:37 +04:00
|
|
|
" -h, --help display this help and exit\n"
|
|
|
|
"\n"
|
|
|
|
"Report bugs to <mdroth@linux.vnet.ibm.com>\n"
|
|
|
|
, cmd, QGA_VERSION, QGA_VIRTIO_PATH_DEFAULT, QGA_PIDFILE_DEFAULT);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *ga_log_level_str(GLogLevelFlags level)
|
|
|
|
{
|
|
|
|
switch (level & G_LOG_LEVEL_MASK) {
|
|
|
|
case G_LOG_LEVEL_ERROR:
|
|
|
|
return "error";
|
|
|
|
case G_LOG_LEVEL_CRITICAL:
|
|
|
|
return "critical";
|
|
|
|
case G_LOG_LEVEL_WARNING:
|
|
|
|
return "warning";
|
|
|
|
case G_LOG_LEVEL_MESSAGE:
|
|
|
|
return "message";
|
|
|
|
case G_LOG_LEVEL_INFO:
|
|
|
|
return "info";
|
|
|
|
case G_LOG_LEVEL_DEBUG:
|
|
|
|
return "debug";
|
|
|
|
default:
|
|
|
|
return "user";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ga_logging_enabled(GAState *s)
|
|
|
|
{
|
|
|
|
return s->logging_enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ga_disable_logging(GAState *s)
|
|
|
|
{
|
|
|
|
s->logging_enabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ga_enable_logging(GAState *s)
|
|
|
|
{
|
|
|
|
s->logging_enabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ga_log(const gchar *domain, GLogLevelFlags level,
|
|
|
|
const gchar *msg, gpointer opaque)
|
|
|
|
{
|
|
|
|
GAState *s = opaque;
|
|
|
|
GTimeVal time;
|
|
|
|
const char *level_str = ga_log_level_str(level);
|
|
|
|
|
|
|
|
if (!ga_logging_enabled(s)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
level &= G_LOG_LEVEL_MASK;
|
2012-01-20 08:04:34 +04:00
|
|
|
#ifndef _WIN32
|
2011-08-12 00:38:11 +04:00
|
|
|
if (domain && strcmp(domain, "syslog") == 0) {
|
2011-07-21 00:19:37 +04:00
|
|
|
syslog(LOG_INFO, "%s: %s", level_str, msg);
|
|
|
|
} else if (level & s->log_level) {
|
2012-01-20 08:04:34 +04:00
|
|
|
#else
|
|
|
|
if (level & s->log_level) {
|
|
|
|
#endif
|
2011-07-21 00:19:37 +04:00
|
|
|
g_get_current_time(&time);
|
|
|
|
fprintf(s->log_file,
|
|
|
|
"%lu.%lu: %s: %s\n", time.tv_sec, time.tv_usec, level_str, msg);
|
|
|
|
fflush(s->log_file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-20 08:04:34 +04:00
|
|
|
#ifndef _WIN32
|
2011-07-21 00:19:37 +04:00
|
|
|
static void become_daemon(const char *pidfile)
|
|
|
|
{
|
|
|
|
pid_t pid, sid;
|
|
|
|
int pidfd;
|
|
|
|
char *pidstr = NULL;
|
|
|
|
|
|
|
|
pid = fork();
|
|
|
|
if (pid < 0) {
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (pid > 0) {
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
pidfd = open(pidfile, O_CREAT|O_WRONLY|O_EXCL, S_IRUSR|S_IWUSR);
|
|
|
|
if (pidfd == -1) {
|
|
|
|
g_critical("Cannot create pid file, %s", strerror(errno));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (asprintf(&pidstr, "%d", getpid()) == -1) {
|
|
|
|
g_critical("Cannot allocate memory");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (write(pidfd, pidstr, strlen(pidstr)) != strlen(pidstr)) {
|
|
|
|
free(pidstr);
|
|
|
|
g_critical("Failed to write pid file");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
umask(0);
|
|
|
|
sid = setsid();
|
|
|
|
if (sid < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if ((chdir("/")) < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(STDIN_FILENO);
|
|
|
|
close(STDOUT_FILENO);
|
|
|
|
close(STDERR_FILENO);
|
|
|
|
free(pidstr);
|
|
|
|
return;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
unlink(pidfile);
|
|
|
|
g_critical("failed to daemonize");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2012-01-20 08:04:34 +04:00
|
|
|
#endif
|
2011-07-21 00:19:37 +04:00
|
|
|
|
2012-01-19 10:18:20 +04:00
|
|
|
static int send_response(GAState *s, QObject *payload)
|
2011-07-21 00:19:37 +04:00
|
|
|
{
|
|
|
|
const char *buf;
|
|
|
|
QString *payload_qstr;
|
2012-01-19 10:18:20 +04:00
|
|
|
GIOStatus status;
|
2011-07-21 00:19:37 +04:00
|
|
|
|
2012-01-19 10:18:20 +04:00
|
|
|
g_assert(payload && s->channel);
|
2011-07-21 00:19:37 +04:00
|
|
|
|
|
|
|
payload_qstr = qobject_to_json(payload);
|
|
|
|
if (!payload_qstr) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
qstring_append_chr(payload_qstr, '\n');
|
|
|
|
buf = qstring_get_str(payload_qstr);
|
2012-01-19 10:18:20 +04:00
|
|
|
status = ga_channel_write_all(s->channel, buf, strlen(buf));
|
2011-07-21 00:19:37 +04:00
|
|
|
QDECREF(payload_qstr);
|
2012-01-19 10:18:20 +04:00
|
|
|
if (status != G_IO_STATUS_NORMAL) {
|
|
|
|
return -EIO;
|
2011-07-21 00:19:37 +04:00
|
|
|
}
|
2012-01-19 10:18:20 +04:00
|
|
|
|
|
|
|
return 0;
|
2011-07-21 00:19:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void process_command(GAState *s, QDict *req)
|
|
|
|
{
|
|
|
|
QObject *rsp = NULL;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
g_assert(req);
|
|
|
|
g_debug("processing command");
|
|
|
|
rsp = qmp_dispatch(QOBJECT(req));
|
|
|
|
if (rsp) {
|
2012-01-19 10:18:20 +04:00
|
|
|
ret = send_response(s, rsp);
|
2011-07-21 00:19:37 +04:00
|
|
|
if (ret) {
|
2012-01-19 10:18:20 +04:00
|
|
|
g_warning("error sending response: %s", strerror(ret));
|
2011-07-21 00:19:37 +04:00
|
|
|
}
|
|
|
|
qobject_decref(rsp);
|
|
|
|
} else {
|
|
|
|
g_warning("error getting response");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handle requests/control events coming in over the channel */
|
|
|
|
static void process_event(JSONMessageParser *parser, QList *tokens)
|
|
|
|
{
|
|
|
|
GAState *s = container_of(parser, GAState, parser);
|
|
|
|
QObject *obj;
|
|
|
|
QDict *qdict;
|
|
|
|
Error *err = NULL;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
g_assert(s && parser);
|
|
|
|
|
|
|
|
g_debug("process_event: called");
|
|
|
|
obj = json_parser_parse_err(tokens, NULL, &err);
|
|
|
|
if (err || !obj || qobject_type(obj) != QTYPE_QDICT) {
|
|
|
|
qobject_decref(obj);
|
|
|
|
qdict = qdict_new();
|
|
|
|
if (!err) {
|
|
|
|
g_warning("failed to parse event: unknown error");
|
|
|
|
error_set(&err, QERR_JSON_PARSING);
|
|
|
|
} else {
|
|
|
|
g_warning("failed to parse event: %s", error_get_pretty(err));
|
|
|
|
}
|
|
|
|
qdict_put_obj(qdict, "error", error_get_qobject(err));
|
|
|
|
error_free(err);
|
|
|
|
} else {
|
|
|
|
qdict = qobject_to_qdict(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_assert(qdict);
|
|
|
|
|
|
|
|
/* handle host->guest commands */
|
|
|
|
if (qdict_haskey(qdict, "execute")) {
|
|
|
|
process_command(s, qdict);
|
|
|
|
} else {
|
|
|
|
if (!qdict_haskey(qdict, "error")) {
|
|
|
|
QDECREF(qdict);
|
|
|
|
qdict = qdict_new();
|
|
|
|
g_warning("unrecognized payload format");
|
|
|
|
error_set(&err, QERR_UNSUPPORTED);
|
|
|
|
qdict_put_obj(qdict, "error", error_get_qobject(err));
|
|
|
|
error_free(err);
|
|
|
|
}
|
2012-01-19 10:18:20 +04:00
|
|
|
ret = send_response(s, QOBJECT(qdict));
|
2011-07-21 00:19:37 +04:00
|
|
|
if (ret) {
|
2012-01-19 10:18:20 +04:00
|
|
|
g_warning("error sending error response: %s", strerror(ret));
|
2011-07-21 00:19:37 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QDECREF(qdict);
|
|
|
|
}
|
|
|
|
|
2012-01-19 10:18:20 +04:00
|
|
|
/* false return signals GAChannel to close the current client connection */
|
|
|
|
static gboolean channel_event_cb(GIOCondition condition, gpointer data)
|
2011-07-21 00:19:37 +04:00
|
|
|
{
|
|
|
|
GAState *s = data;
|
2012-01-19 10:18:20 +04:00
|
|
|
gchar buf[QGA_READ_COUNT_DEFAULT+1];
|
2011-07-21 00:19:37 +04:00
|
|
|
gsize count;
|
|
|
|
GError *err = NULL;
|
2012-01-19 10:18:20 +04:00
|
|
|
GIOStatus status = ga_channel_read(s->channel, buf, QGA_READ_COUNT_DEFAULT, &count);
|
2011-07-21 00:19:37 +04:00
|
|
|
if (err != NULL) {
|
|
|
|
g_warning("error reading channel: %s", err->message);
|
|
|
|
g_error_free(err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
switch (status) {
|
|
|
|
case G_IO_STATUS_ERROR:
|
2012-01-19 10:18:20 +04:00
|
|
|
g_warning("error reading channel");
|
2011-07-21 00:19:37 +04:00
|
|
|
return false;
|
|
|
|
case G_IO_STATUS_NORMAL:
|
2012-01-19 10:18:20 +04:00
|
|
|
buf[count] = 0;
|
2011-07-21 00:19:37 +04:00
|
|
|
g_debug("read data, count: %d, data: %s", (int)count, buf);
|
|
|
|
json_message_parser_feed(&s->parser, (char *)buf, (int)count);
|
2012-01-19 10:18:20 +04:00
|
|
|
break;
|
|
|
|
case G_IO_STATUS_EOF:
|
|
|
|
g_debug("received EOF");
|
|
|
|
if (!s->virtio) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-07-21 00:19:37 +04:00
|
|
|
case G_IO_STATUS_AGAIN:
|
|
|
|
/* virtio causes us to spin here when no process is attached to
|
|
|
|
* host-side chardev. sleep a bit to mitigate this
|
|
|
|
*/
|
|
|
|
if (s->virtio) {
|
|
|
|
usleep(100*1000);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
g_warning("unknown channel read status, closing");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-19 10:18:20 +04:00
|
|
|
static gboolean channel_init(GAState *s, const gchar *method, const gchar *path)
|
2011-07-21 00:19:37 +04:00
|
|
|
{
|
2012-01-19 10:18:20 +04:00
|
|
|
GAChannelMethod channel_method;
|
2011-07-21 00:19:37 +04:00
|
|
|
|
2012-01-19 10:18:20 +04:00
|
|
|
if (method == NULL) {
|
|
|
|
method = "virtio-serial";
|
2011-07-21 00:19:37 +04:00
|
|
|
}
|
|
|
|
|
2012-01-19 10:18:20 +04:00
|
|
|
if (path == NULL) {
|
|
|
|
if (strcmp(method, "virtio-serial") != 0) {
|
2011-07-21 00:19:37 +04:00
|
|
|
g_critical("must specify a path for this channel");
|
2012-01-19 10:18:20 +04:00
|
|
|
return false;
|
2011-07-21 00:19:37 +04:00
|
|
|
}
|
|
|
|
/* try the default path for the virtio-serial port */
|
2012-01-19 10:18:20 +04:00
|
|
|
path = QGA_VIRTIO_PATH_DEFAULT;
|
2011-07-21 00:19:37 +04:00
|
|
|
}
|
|
|
|
|
2012-01-19 10:18:20 +04:00
|
|
|
if (strcmp(method, "virtio-serial") == 0) {
|
|
|
|
s->virtio = true; /* virtio requires special handling in some cases */
|
|
|
|
channel_method = GA_CHANNEL_VIRTIO_SERIAL;
|
|
|
|
} else if (strcmp(method, "isa-serial") == 0) {
|
|
|
|
channel_method = GA_CHANNEL_ISA_SERIAL;
|
|
|
|
} else if (strcmp(method, "unix-listen") == 0) {
|
|
|
|
channel_method = GA_CHANNEL_UNIX_LISTEN;
|
2011-07-21 00:19:37 +04:00
|
|
|
} else {
|
2012-01-19 10:18:20 +04:00
|
|
|
g_critical("unsupported channel method/type: %s", method);
|
|
|
|
return false;
|
2011-07-21 00:19:37 +04:00
|
|
|
}
|
|
|
|
|
2012-01-19 10:18:20 +04:00
|
|
|
s->channel = ga_channel_new(channel_method, path, channel_event_cb, s);
|
|
|
|
if (!s->channel) {
|
|
|
|
g_critical("failed to create guest agent channel");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2011-07-21 00:19:37 +04:00
|
|
|
}
|
|
|
|
|
2012-01-22 02:42:27 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
DWORD WINAPI service_ctrl_handler(DWORD ctrl, DWORD type, LPVOID data,
|
|
|
|
LPVOID ctx)
|
|
|
|
{
|
|
|
|
DWORD ret = NO_ERROR;
|
|
|
|
GAService *service = &ga_state->service;
|
|
|
|
|
|
|
|
switch (ctrl)
|
|
|
|
{
|
|
|
|
case SERVICE_CONTROL_STOP:
|
|
|
|
case SERVICE_CONTROL_SHUTDOWN:
|
|
|
|
quit_handler(SIGTERM);
|
|
|
|
service->status.dwCurrentState = SERVICE_STOP_PENDING;
|
|
|
|
SetServiceStatus(service->status_handle, &service->status);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ret = ERROR_CALL_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID WINAPI service_main(DWORD argc, TCHAR *argv[])
|
|
|
|
{
|
|
|
|
GAService *service = &ga_state->service;
|
|
|
|
|
|
|
|
service->status_handle = RegisterServiceCtrlHandlerEx(QGA_SERVICE_NAME,
|
|
|
|
service_ctrl_handler, NULL);
|
|
|
|
|
|
|
|
if (service->status_handle == 0) {
|
|
|
|
g_critical("Failed to register extended requests function!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
service->status.dwServiceType = SERVICE_WIN32;
|
|
|
|
service->status.dwCurrentState = SERVICE_RUNNING;
|
|
|
|
service->status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
|
|
|
|
service->status.dwWin32ExitCode = NO_ERROR;
|
|
|
|
service->status.dwServiceSpecificExitCode = NO_ERROR;
|
|
|
|
service->status.dwCheckPoint = 0;
|
|
|
|
service->status.dwWaitHint = 0;
|
|
|
|
SetServiceStatus(service->status_handle, &service->status);
|
|
|
|
|
|
|
|
g_main_loop_run(ga_state->main_loop);
|
|
|
|
|
|
|
|
service->status.dwCurrentState = SERVICE_STOPPED;
|
|
|
|
SetServiceStatus(service->status_handle, &service->status);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-07-21 00:19:37 +04:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2012-01-22 02:42:27 +04:00
|
|
|
const char *sopt = "hVvdm:p:l:f:b:s:";
|
2011-07-21 00:19:37 +04:00
|
|
|
const char *method = NULL, *path = NULL, *pidfile = QGA_PIDFILE_DEFAULT;
|
2012-01-22 02:42:27 +04:00
|
|
|
const char *log_file_name = NULL;
|
|
|
|
#ifdef _WIN32
|
|
|
|
const char *service = NULL;
|
|
|
|
#endif
|
2011-07-21 00:19:37 +04:00
|
|
|
const struct option lopt[] = {
|
|
|
|
{ "help", 0, NULL, 'h' },
|
|
|
|
{ "version", 0, NULL, 'V' },
|
2012-01-22 02:42:27 +04:00
|
|
|
{ "logfile", 1, NULL, 'l' },
|
|
|
|
{ "pidfile", 1, NULL, 'f' },
|
2011-07-21 00:19:37 +04:00
|
|
|
{ "verbose", 0, NULL, 'v' },
|
2012-01-22 02:42:27 +04:00
|
|
|
{ "method", 1, NULL, 'm' },
|
|
|
|
{ "path", 1, NULL, 'p' },
|
2011-07-21 00:19:37 +04:00
|
|
|
{ "daemonize", 0, NULL, 'd' },
|
2012-01-22 02:42:27 +04:00
|
|
|
{ "blacklist", 1, NULL, 'b' },
|
|
|
|
#ifdef _WIN32
|
|
|
|
{ "service", 1, NULL, 's' },
|
|
|
|
#endif
|
2011-07-21 00:19:37 +04:00
|
|
|
{ NULL, 0, NULL, 0 }
|
|
|
|
};
|
2011-12-07 08:03:42 +04:00
|
|
|
int opt_ind = 0, ch, daemonize = 0, i, j, len;
|
2011-07-21 00:19:37 +04:00
|
|
|
GLogLevelFlags log_level = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL;
|
|
|
|
FILE *log_file = stderr;
|
|
|
|
GAState *s;
|
|
|
|
|
2011-12-07 08:03:42 +04:00
|
|
|
module_call_init(MODULE_INIT_QAPI);
|
|
|
|
|
2011-07-21 00:19:37 +04:00
|
|
|
while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
|
|
|
|
switch (ch) {
|
|
|
|
case 'm':
|
|
|
|
method = optarg;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
path = optarg;
|
|
|
|
break;
|
|
|
|
case 'l':
|
2012-01-22 02:42:27 +04:00
|
|
|
log_file_name = optarg;
|
|
|
|
log_file = fopen(log_file_name, "a");
|
2011-07-21 00:19:37 +04:00
|
|
|
if (!log_file) {
|
|
|
|
g_critical("unable to open specified log file: %s",
|
|
|
|
strerror(errno));
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
pidfile = optarg;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
/* enable all log levels */
|
|
|
|
log_level = G_LOG_LEVEL_MASK;
|
|
|
|
break;
|
|
|
|
case 'V':
|
|
|
|
printf("QEMU Guest Agent %s\n", QGA_VERSION);
|
|
|
|
return 0;
|
|
|
|
case 'd':
|
|
|
|
daemonize = 1;
|
|
|
|
break;
|
2011-12-07 08:03:42 +04:00
|
|
|
case 'b': {
|
|
|
|
char **list_head, **list;
|
|
|
|
if (*optarg == '?') {
|
|
|
|
list_head = list = qmp_get_command_list();
|
|
|
|
while (*list != NULL) {
|
|
|
|
printf("%s\n", *list);
|
|
|
|
g_free(*list);
|
|
|
|
list++;
|
|
|
|
}
|
|
|
|
g_free(list_head);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
for (j = 0, i = 0, len = strlen(optarg); i < len; i++) {
|
|
|
|
if (optarg[i] == ',') {
|
|
|
|
optarg[i] = 0;
|
|
|
|
qmp_disable_command(&optarg[j]);
|
|
|
|
g_debug("disabling command: %s", &optarg[j]);
|
|
|
|
j = i + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (j < i) {
|
|
|
|
qmp_disable_command(&optarg[j]);
|
|
|
|
g_debug("disabling command: %s", &optarg[j]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-01-22 02:42:27 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
case 's':
|
|
|
|
service = optarg;
|
|
|
|
if (strcmp(service, "install") == 0) {
|
|
|
|
return ga_install_service(path, log_file_name);
|
|
|
|
} else if (strcmp(service, "uninstall") == 0) {
|
|
|
|
return ga_uninstall_service();
|
|
|
|
} else {
|
|
|
|
printf("Unknown service command.\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
2011-07-21 00:19:37 +04:00
|
|
|
case 'h':
|
|
|
|
usage(argv[0]);
|
|
|
|
return 0;
|
|
|
|
case '?':
|
|
|
|
g_print("Unknown option, try '%s --help' for more information.\n",
|
|
|
|
argv[0]);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-20 08:04:34 +04:00
|
|
|
#ifndef _WIN32
|
2011-07-21 00:19:37 +04:00
|
|
|
if (daemonize) {
|
|
|
|
g_debug("starting daemon");
|
|
|
|
become_daemon(pidfile);
|
|
|
|
}
|
2012-01-20 08:04:34 +04:00
|
|
|
#endif
|
2011-07-21 00:19:37 +04:00
|
|
|
|
2011-08-21 07:09:37 +04:00
|
|
|
s = g_malloc0(sizeof(GAState));
|
2011-07-21 00:19:37 +04:00
|
|
|
s->log_file = log_file;
|
|
|
|
s->log_level = log_level;
|
|
|
|
g_log_set_default_handler(ga_log, s);
|
|
|
|
g_log_set_fatal_mask(NULL, G_LOG_LEVEL_ERROR);
|
|
|
|
s->logging_enabled = true;
|
2011-07-20 00:41:55 +04:00
|
|
|
s->command_state = ga_command_state_new();
|
|
|
|
ga_command_state_init(s, s->command_state);
|
|
|
|
ga_command_state_init_all(s->command_state);
|
2012-01-19 10:18:20 +04:00
|
|
|
json_message_parser_init(&s->parser, process_event);
|
2011-07-21 00:19:37 +04:00
|
|
|
ga_state = s;
|
2012-01-20 08:04:34 +04:00
|
|
|
#ifndef _WIN32
|
2012-01-19 10:18:20 +04:00
|
|
|
if (!register_signal_handlers()) {
|
|
|
|
g_critical("failed to register signal handlers");
|
|
|
|
goto out_bad;
|
|
|
|
}
|
2012-01-20 08:04:34 +04:00
|
|
|
#endif
|
2011-07-21 00:19:37 +04:00
|
|
|
|
2012-01-19 10:18:20 +04:00
|
|
|
s->main_loop = g_main_loop_new(NULL, false);
|
|
|
|
if (!channel_init(ga_state, method, path)) {
|
|
|
|
g_critical("failed to initialize guest agent channel");
|
|
|
|
goto out_bad;
|
|
|
|
}
|
2012-01-22 02:42:27 +04:00
|
|
|
#ifndef _WIN32
|
2011-07-21 00:19:37 +04:00
|
|
|
g_main_loop_run(ga_state->main_loop);
|
2012-01-22 02:42:27 +04:00
|
|
|
#else
|
|
|
|
if (daemonize) {
|
|
|
|
SERVICE_TABLE_ENTRY service_table[] = {
|
|
|
|
{ (char *)QGA_SERVICE_NAME, service_main }, { NULL, NULL } };
|
|
|
|
StartServiceCtrlDispatcher(service_table);
|
|
|
|
} else {
|
|
|
|
g_main_loop_run(ga_state->main_loop);
|
|
|
|
}
|
|
|
|
#endif
|
2011-07-21 00:19:37 +04:00
|
|
|
|
2011-07-20 00:41:55 +04:00
|
|
|
ga_command_state_cleanup_all(ga_state->command_state);
|
2012-01-19 10:18:20 +04:00
|
|
|
ga_channel_free(ga_state->channel);
|
2011-07-21 00:19:37 +04:00
|
|
|
|
2012-01-19 10:18:20 +04:00
|
|
|
if (daemonize) {
|
|
|
|
unlink(pidfile);
|
|
|
|
}
|
2011-07-21 00:19:37 +04:00
|
|
|
return 0;
|
2012-01-19 10:18:20 +04:00
|
|
|
|
|
|
|
out_bad:
|
|
|
|
if (daemonize) {
|
|
|
|
unlink(pidfile);
|
|
|
|
}
|
|
|
|
return EXIT_FAILURE;
|
2011-07-21 00:19:37 +04:00
|
|
|
}
|