From patchwork Mon Apr 18 15:02:31 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 91784 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 86CADB6FF3 for ; Tue, 19 Apr 2011 01:08:43 +1000 (EST) Received: from localhost ([::1]:43207 helo=lists2.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QBq48-0007zV-O7 for incoming@patchwork.ozlabs.org; Mon, 18 Apr 2011 11:08:40 -0400 Received: from eggs.gnu.org ([140.186.70.92]:39553) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QBpz8-00077M-WC for qemu-devel@nongnu.org; Mon, 18 Apr 2011 11:03:36 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QBpz6-0000r3-Rz for qemu-devel@nongnu.org; Mon, 18 Apr 2011 11:03:30 -0400 Received: from e32.co.us.ibm.com ([32.97.110.150]:56042) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QBpz6-0000qx-ER for qemu-devel@nongnu.org; Mon, 18 Apr 2011 11:03:28 -0400 Received: from d03relay02.boulder.ibm.com (d03relay02.boulder.ibm.com [9.17.195.227]) by e32.co.us.ibm.com (8.14.4/8.13.1) with ESMTP id p3IEqOsN012703 for ; Mon, 18 Apr 2011 08:52:24 -0600 Received: from d03av04.boulder.ibm.com (d03av04.boulder.ibm.com [9.17.195.170]) by d03relay02.boulder.ibm.com (8.13.8/8.13.8/NCO v9.1) with ESMTP id p3IF3OGc107712 for ; Mon, 18 Apr 2011 09:03:25 -0600 Received: from d03av04.boulder.ibm.com (loopback [127.0.0.1]) by d03av04.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id p3IF3NvF023891 for ; Mon, 18 Apr 2011 09:03:23 -0600 Received: from localhost.localdomain (sig-9-65-250-146.mts.ibm.com [9.65.250.146]) by d03av04.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id p3IF2eZs019079; Mon, 18 Apr 2011 09:03:21 -0600 From: Michael Roth To: qemu-devel@nongnu.org Date: Mon, 18 Apr 2011 10:02:31 -0500 Message-Id: <1303138953-1334-16-git-send-email-mdroth@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.0.4 In-Reply-To: <1303138953-1334-1-git-send-email-mdroth@linux.vnet.ibm.com> References: <1303138953-1334-1-git-send-email-mdroth@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) X-Received-From: 32.97.110.150 Cc: aliguori@linux.vnet.ibm.com, agl@linux.vnet.ibm.com, mdroth@linux.vnet.ibm.com, Jes.Sorensen@redhat.com Subject: [Qemu-devel] [RFC][PATCH v2 15/17] guest agent: qemu-ga daemon X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This is the actual guest daemon, it listens for requests over a virtio-serial/isa-serial/unix socket channel and routes them through to dispatch routines, and writes the results back to the channel in a manner similar to QMP. A shorthand invocation: qemu-ga -d Is equivalent to: qemu-ga -c virtio-serial -p /dev/virtio-ports/org.qemu.guest_agent \ -p /var/run/qemu-guest-agent.pid -d Signed-off-by: Michael Roth --- qemu-ga.c | 711 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 711 insertions(+), 0 deletions(-) create mode 100644 qemu-ga.c diff --git a/qemu-ga.c b/qemu-ga.c new file mode 100644 index 0000000..800d16b --- /dev/null +++ b/qemu-ga.c @@ -0,0 +1,711 @@ +/* + * QEMU Guest Agent + * + * Copyright IBM Corp. 2011 + * + * Authors: + * Adam Litke + * Michael Roth + * + * 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 +#include +#include +#include +#include +#include +#include +#include +#include "qemu_socket.h" +#include "json-streamer.h" +#include "json-parser.h" +#include "qga/guest-agent.h" + +#define QGA_VIRTIO_PATH_DEFAULT "/dev/virtio-ports/org.qemu.guest_agent" +#define QGA_PIDFILE_DEFAULT "/var/run/qemu-va.pid" +#define QGA_BAUDRATE_DEFAULT B38400 /* for isa-serial channels */ +#define QGA_TIMEOUT_DEFAULT 30*1000 /* ms */ + +struct GAState { + bool active; + int session_id; + const char *proxy_path; + JSONMessageParser parser; + GMainLoop *main_loop; + guint conn_id; + GSocket *conn_sock; + GIOChannel *conn_channel; + guint listen_id; + GSocket *listen_sock; + GIOChannel *listen_channel; + const char *path; + const char *method; + bool virtio; /* fastpath to check for virtio to deal with poll() quirks */ + GACommandState *command_state; + GAWorker *worker; + int timeout_ms; + GLogLevelFlags log_level; + FILE *log_file; +}; + +static void usage(const char *cmd) +{ + printf( +"Usage: %s -c \n" +"QEMU Guest Agent %s\n" +"\n" +" -c, --channel channel method: one of unix-connect, virtio-serial, or\n" +" isa-serial (virtio-serial is the default)\n" +" -p, --path channel 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" +" -h, --help display this help and exit\n" +"\n" +"Report bugs to \n" + , cmd, QGA_VERSION, QGA_VIRTIO_PATH_DEFAULT, QGA_PIDFILE_DEFAULT); +} + +static void conn_channel_close(GAState *s); + +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"; + } +} + +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); + + level &= G_LOG_LEVEL_MASK; + if (g_strcmp0(domain, "syslog") == 0) { + syslog(LOG_INFO, "%s: %s", level_str, msg); + } else if (level & s->log_level) { + 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); + } +} + +int ga_get_timeout(GAState *s) +{ + return s->timeout_ms; +} + +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_RDWR, S_IRUSR|S_IWUSR); + if (!pidfd || lockf(pidfd, F_TLOCK, 0)) { + g_error("Cannot lock pid file"); + } + + if (ftruncate(pidfd, 0) || lseek(pidfd, 0, SEEK_SET)) { + g_critical("Cannot truncate pid file"); + goto fail; + } + if (asprintf(&pidstr, "%d", getpid()) == -1) { + g_critical("Cannot allocate memory"); + goto fail; + } + if (write(pidfd, pidstr, strlen(pidstr)) != strlen(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); + return; + +fail: + if (pidstr) { + free(pidstr); + } + unlink(pidfile); + g_error("failed to daemonize"); +} + +static int conn_channel_send_payload(GIOChannel *channel, QObject *payload) +{ + gsize count, written = 0; + int ret; + const char *buf; + QString *payload_qstr; + GIOStatus status; + GError *err = NULL; + + if (!payload || !channel) { + ret = -EINVAL; + goto out; + } + + payload_qstr = qobject_to_json(payload); + if (!payload_qstr) { + ret = -EINVAL; + goto out; + } + + buf = qstring_get_str(payload_qstr); + count = strlen(buf); + + while (count) { + g_debug("sending data, count: %d", (int)count); + status = g_io_channel_write_chars(channel, buf, count, &written, &err); + if (err != NULL) { + g_warning("error sending payload: %s", err->message); + ret = err->code; + goto out_free; + } + if (status == G_IO_STATUS_NORMAL) { + count -= written; + } else if (status == G_IO_STATUS_ERROR || status == G_IO_STATUS_EOF) { + ret = -EPIPE; + goto out_free; + } + } + + status = g_io_channel_write_chars(channel, (char *)"\n", 1, &written, &err); + if (err != NULL) { + g_warning("error sending newline: %s", err->message); + ret = err->code; + goto out_free; + } else if (status == G_IO_STATUS_ERROR || status == G_IO_STATUS_EOF) { + ret = -EPIPE; + goto out_free; + } + + g_io_channel_flush(channel, &err); + if (err != NULL) { + g_warning("error flushing payload: %s", err->message); + ret = err->code; + goto out_free; + } + + ret = 0; + +out_free: + QDECREF(payload_qstr); +out: + return ret; +} + +/* when inactive, we keep reading channel, but ignore all non-control messages + * until channel is active again (i.e. negotiation is complete) + */ +static void set_channel_active(GAState *s, bool active) +{ + g_debug("channel active state: %d", active); + s->active = active; +} + +static void send_guest_up(GAState *s) +{ + QDict *payload = qdict_new(); + int ret; + + qdict_put_obj(payload, "_control_event", + QOBJECT(qstring_from_str("guest_up"))); + + ret = conn_channel_send_payload(s->conn_channel, QOBJECT(payload)); + if (ret) { + g_warning("failed to send guest init, resetting connection"); + conn_channel_close(s); + } +} + +/* host started channel negotiation, process/acknowledge it */ +static void send_guest_ack(GAState *s, int host_sid) +{ + QDict *payload = qdict_new(); + int ret; + + g_debug("[re]negotiating connection"); + + g_assert(s && s->conn_channel); + set_channel_active(s, false); + s->session_id = g_random_int_range(1, G_MAXINT32); + + qdict_put_obj(payload, "_control_event", + QOBJECT(qstring_from_str("guest_ack"))); + qdict_put_obj(payload, "_control_arg_guest_sid", + QOBJECT(qint_from_int(s->session_id))); + qdict_put_obj(payload, "_control_arg_host_sid", + QOBJECT(qint_from_int(host_sid))); + + ret = conn_channel_send_payload(s->conn_channel, QOBJECT(payload)); + if (ret) { + g_warning("failed to send guest init, resetting connection"); + conn_channel_close(s); + } +} + +/* process transport-level events */ +static void process_control_event(GAState *s, const QDict *qdict) +{ + const char *cmd; + int sid; + + g_assert(s && qdict); + cmd = qdict_get_try_str(qdict, "_control_event"); + if (!cmd) { + g_warning("received NULL transport event"); + } else if (strcmp(cmd, "host_init") == 0) { + sid = qdict_get_try_int(qdict, "_control_arg_host_sid", 0); + /* host started negotiation, acknowledge it */ + send_guest_ack(s, sid); + set_channel_active(s, false); + } else if (strcmp(cmd, "host_ack") == 0) { + /* host attempting to complete negotiation, process it */ + sid = qdict_get_try_int(qdict, "_control_arg_guest_sid", 0); + if (sid != s->session_id) { + g_warning("received host ack for incorrect session id"); + } else { + set_channel_active(s, true); + } + } else { + g_debug("recieved unknown control event: %s", cmd); + } +} + +static void process_command_worker(void *input, void *output, Error **errp) +{ + QDict *req = input; + QObject **rsp = output; + + g_assert(req && rsp); + *rsp = qga_dispatch(QOBJECT(req), errp); +} + +static void process_command(GAState *s, QDict *req) +{ + QObject *rsp = NULL; + Error *err = NULL; + bool timeout; + int ret; + + g_assert(req); + g_debug("processing command"); + timeout = ga_worker_dispatch(s->worker, req, &rsp, s->timeout_ms, &err); + if (timeout) { + g_warning("command timed out"); + } else if (rsp) { + if (err) { + g_warning("command failed: %s", error_get_pretty(err)); + } + g_debug("response: %s", qstring_get_str(qobject_to_json(rsp))); + ret = conn_channel_send_payload(s->conn_channel, rsp); + if (ret) { + g_warning("error sending payload: %s", strerror(ret)); + } + 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; + + g_assert(s && parser); + + g_debug("process_event: called"); + obj = json_parser_parse_err(tokens, NULL, &err); + if (!obj || qobject_type(obj) != QTYPE_QDICT) { + g_warning("failed to parse event"); + return; + } else { + g_debug("parse successful"); + qdict = qobject_to_qdict(obj); + g_assert(qdict); + } + + /* check for transport-only commands/events */ + if (qdict_haskey(qdict, "_control_event")) { + process_control_event(s, qdict); + goto out_free; + } + + /* ignore any non-control-related events/objects */ + if (!s->active) { + g_debug("ignoring non-control event, still awaiting host ack"); + goto out_free; + } + + /* handle host->guest commands */ + if (qdict_haskey(qdict, "execute")) { + process_command(s, qdict); + } else { + g_warning("unrecognized payload format, ignoring"); + } + +out_free: + QDECREF(qdict); +} + +static gboolean conn_channel_read(GIOChannel *channel, GIOCondition condition, + gpointer data) +{ + GAState *s = data; + gchar buf[1024]; + gsize count; + GError *err = NULL; + memset(buf, 0, 1024); + GIOStatus status = g_io_channel_read_chars(channel, buf, 1024, + &count, &err); + if (err != NULL) { + g_warning("error reading channel: %s", err->message); + conn_channel_close(s); + return false; + } + switch (status) { + case G_IO_STATUS_ERROR: + g_warning("problem"); + return false; + case G_IO_STATUS_NORMAL: + g_debug("read data, count: %d, data: %s", (int)count, buf); + json_message_parser_feed(&s->parser, (char *)buf, (int)count); + 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; + case G_IO_STATUS_EOF: + g_debug("received EOF"); + conn_channel_close(s); + if (s->virtio) { + return true; + } + return false; + default: + g_warning("unknown channel read status, closing"); + conn_channel_close(s); + return false; + } + return true; +} + +static int conn_channel_add(GAState *s, int fd) +{ + GIOChannel *conn_channel; + guint conn_id; + GError *err = NULL; + + g_assert(s && !s->conn_channel); + conn_channel = g_io_channel_unix_new(fd); + g_assert(conn_channel); + g_io_channel_set_encoding(conn_channel, NULL, &err); + if (err != NULL) { + g_warning("error setting channel encoding to binary"); + return -1; + } + conn_id = g_io_add_watch(conn_channel, G_IO_IN | G_IO_HUP, + conn_channel_read, s); + if (err != NULL) { + g_warning("error adding io watch: %s", err->message); + return -1; + } + s->conn_channel = conn_channel; + s->conn_id = conn_id; + return 0; +} + +static gboolean listen_channel_accept(GIOChannel *channel, + GIOCondition condition, gpointer data) +{ + GAState *s = data; + GError *err = NULL; + g_assert(channel != NULL); + int ret; + bool accepted = false; + + s->conn_sock = g_socket_accept(s->listen_sock, NULL, &err); + if (err != NULL) { + g_warning("error converting fd to gsocket: %s", err->message); + goto out; + } + ret = conn_channel_add(s, g_socket_get_fd(s->conn_sock)); + if (ret) { + g_warning("error setting up connection"); + goto out; + } + accepted = true; + +out: + /* only accept 1 connection at a time */ + return !accepted; +} + +/* start polling for readable events on listen fd, listen_fd=0 + * indicates we should use the existing s->listen_channel + */ +static int listen_channel_add(GAState *s, int listen_fd) +{ + GError *err = NULL; + guint listen_id; + + if (listen_fd) { + s->listen_channel = g_io_channel_unix_new(listen_fd); + if (s->listen_sock) { + g_object_unref(s->listen_sock); + } + s->listen_sock = g_socket_new_from_fd(listen_fd, &err); + if (err != NULL) { + g_warning("error converting fd to gsocket: %s", err->message); + return -1; + } + } + listen_id = g_io_add_watch(s->listen_channel, G_IO_IN, + listen_channel_accept, s); + if (err != NULL) { + g_warning("error adding io watch: %s", err->message); + return -1; + } + return 0; +} + +/* cleanup state for closed connection/session, start accepting new + * connections if we're in listening mode + */ +static void conn_channel_close(GAState *s) +{ + if (strcmp(s->method, "unix-listen") == 0) { + g_io_channel_shutdown(s->conn_channel, true, NULL); + g_object_unref(s->conn_sock); + s->conn_sock = NULL; + listen_channel_add(s, 0); + } else if (strcmp(s->method, "virtio-serial") == 0) { + /* we spin on EOF for virtio-serial, so back off a bit. also, + * dont close the connection in this case, it'll resume normal + * operation when another process connects to host chardev + */ + usleep(100*1000); + goto out_noclose; + } + g_io_channel_unref(s->conn_channel); + s->conn_channel = NULL; + s->conn_id = 0; +out_noclose: + /* reset negotiated state. we'll either send another init upon reconnect + * to our socket, or the host will send an init to tell us it has + * re-opened the chardev, at which point we'll send another init + */ + set_channel_active(s, false); +} + +static void init_guest_agent(GAState *s) +{ + struct termios tio; + int ret, fd; + + if (s->method == NULL) { + /* try virtio-serial as our default */ + s->method = "virtio-serial"; + } + + if (s->path == NULL) { + if (strcmp(s->method, "virtio-serial") != 0) { + g_error("must specify a path for this channel"); + } + /* try the default path for the virtio-serial port */ + s->path = QGA_VIRTIO_PATH_DEFAULT; + } + + if (strcmp(s->method, "virtio-serial") == 0) { + s->virtio = true; + fd = qemu_open(s->path, O_RDWR); + if (fd == -1) { + g_error("error opening channel: %s", strerror(errno)); + } + ret = fcntl(fd, F_GETFL); + if (ret < 0) { + g_error("error getting channel flags: %s", strerror(errno)); + } + ret = fcntl(fd, F_SETFL, ret | O_NONBLOCK | O_ASYNC); + if (ret < 0) { + g_error("error setting channel flags: %s", strerror(errno)); + } + ret = conn_channel_add(s, fd); + if (ret) { + g_error("error adding channel to main loop"); + } + send_guest_up(s); + } else if (strcmp(s->method, "isa-serial") == 0) { + fd = qemu_open(s->path, O_RDWR | O_NOCTTY); + if (fd == -1) { + g_error("error opening channel: %s", strerror(errno)); + } + tcgetattr(fd, &tio); + /* set up serial port for non-canonical, dumb byte streaming */ + tio.c_iflag &= ~(IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK | ISTRIP | + INLCR | IGNCR | ICRNL | IXON | IXOFF | IXANY | + IMAXBEL); + tio.c_oflag = 0; + tio.c_lflag = 0; + tio.c_cflag |= QGA_BAUDRATE_DEFAULT; + /* 1 available byte min or reads will block (we'll set non-blocking + * elsewhere, else we have to deal with read()=0 instead) + */ + tio.c_cc[VMIN] = 1; + tio.c_cc[VTIME] = 0; + /* flush everything waiting for read/xmit, it's garbage at this point */ + tcflush(fd, TCIFLUSH); + tcsetattr(fd, TCSANOW, &tio); + ret = conn_channel_add(s, fd); + if (ret) { + g_error("error adding channel to main loop"); + } + } else if (strcmp(s->method, "unix-listen") == 0) { + fd = unix_listen(s->path, NULL, strlen(s->path)); + if (fd == -1) { + g_error("error opening path: %s", strerror(errno)); + } + ret = listen_channel_add(s, fd); + if (ret) { + g_error("error binding/listening to specified socket"); + } + } else { + g_error("unsupported channel method/type: %s", s->method); + } + + json_message_parser_init(&s->parser, process_event); + s->main_loop = g_main_loop_new(NULL, false); +} + +int main(int argc, char **argv) +{ + const char *sopt = "hVvdc:p:l:f:"; + const char *method = NULL, *path = NULL, *pidfile = QGA_PIDFILE_DEFAULT; + struct option lopt[] = { + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'V' }, + { "logfile", 0, NULL, 'l' }, + { "pidfile", 0, NULL, 'f' }, + { "verbose", 0, NULL, 'v' }, + { "channel", 0, NULL, 'c' }, + { "path", 0, NULL, 'p' }, + { "daemonize", 0, NULL, 'd' }, + { NULL, 0, NULL, 0 } + }; + int opt_ind = 0, ch, daemonize = 0; + GLogLevelFlags log_level = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL; + FILE *log_file = stderr; + GAState *s; + + g_type_init(); + g_thread_init(NULL); + + while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) { + switch (ch) { + case 'c': + method = optarg; + break; + case 'p': + path = optarg; + break; + case 'l': + log_file = fopen(optarg, "a"); + if (!log_file) { + g_error("unable to open specified log file: %s", + strerror(errno)); + } + 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; + case 'h': + usage(argv[0]); + return 0; + case '?': + g_error("Unknown option, try '%s --help' for more information.", + argv[0]); + } + } + + if (daemonize) { + g_debug("starting daemon"); + become_daemon(pidfile); + } + + qga_init_marshal(); + + s = g_malloc0(sizeof(GAState)); + s->session_id = 0; + s->conn_id = 0; + s->conn_channel = NULL; + s->path = path; + s->method = method; + s->command_state = ga_command_state_new(); + ga_command_state_init(s, s->command_state); + ga_command_state_init_all(s->command_state); + s->worker = ga_worker_new(process_command_worker); + s->timeout_ms = QGA_TIMEOUT_DEFAULT; + 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); + + set_channel_active(s, false); + init_guest_agent(s); + + g_main_loop_run(s->main_loop); + + ga_command_state_cleanup_all(s->command_state); + ga_worker_cleanup(s->worker); + + return 0; +}