blob: 1c90e6ef0de3f129530d785d6e2571b7adbd1df3 [file] [log] [blame]
Michael Roth48ff7a62011-07-20 15:19:37 -05001/*
2 * QEMU Guest Agent
3 *
4 * Copyright IBM Corp. 2011
5 *
6 * Authors:
7 * Adam Litke <aglitke@linux.vnet.ibm.com>
8 * Michael Roth <mdroth@linux.vnet.ibm.com>
9 *
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
12 */
13#include <stdlib.h>
14#include <stdio.h>
15#include <stdbool.h>
16#include <glib.h>
Michael Roth48ff7a62011-07-20 15:19:37 -050017#include <getopt.h>
Michael Rothd8ca6852012-01-19 22:04:34 -060018#ifndef _WIN32
Michael Roth48ff7a62011-07-20 15:19:37 -050019#include <syslog.h>
Luiz Capitulino11d0f122012-02-28 11:03:03 -030020#include <sys/wait.h>
Michael Rothd8ca6852012-01-19 22:04:34 -060021#endif
Michael Roth48ff7a62011-07-20 15:19:37 -050022#include "json-streamer.h"
23#include "json-parser.h"
24#include "qint.h"
25#include "qjson.h"
26#include "qga/guest-agent-core.h"
27#include "module.h"
28#include "signal.h"
29#include "qerror.h"
30#include "error_int.h"
Michael Rothabd6cf62011-12-06 22:03:42 -060031#include "qapi/qmp-core.h"
Michael Roth125b3102012-01-19 00:18:20 -060032#include "qga/channel.h"
Michael Rothbc62fa02012-01-21 16:42:27 -060033#ifdef _WIN32
34#include "qga/service-win32.h"
35#include <windows.h>
36#endif
Michael Roth48ff7a62011-07-20 15:19:37 -050037
Michael Roth7868e262012-01-20 19:01:30 -060038#ifndef _WIN32
Michael Roth48ff7a62011-07-20 15:19:37 -050039#define QGA_VIRTIO_PATH_DEFAULT "/dev/virtio-ports/org.qemu.guest_agent.0"
Michael Roth7868e262012-01-20 19:01:30 -060040#else
41#define QGA_VIRTIO_PATH_DEFAULT "\\\\.\\Global\\org.qemu.guest_agent.0"
42#endif
Michael Roth48ff7a62011-07-20 15:19:37 -050043#define QGA_PIDFILE_DEFAULT "/var/run/qemu-ga.pid"
Michael Roth48ff7a62011-07-20 15:19:37 -050044
45struct GAState {
46 JSONMessageParser parser;
47 GMainLoop *main_loop;
Michael Roth125b3102012-01-19 00:18:20 -060048 GAChannel *channel;
Michael Roth48ff7a62011-07-20 15:19:37 -050049 bool virtio; /* fastpath to check for virtio to deal with poll() quirks */
50 GACommandState *command_state;
51 GLogLevelFlags log_level;
52 FILE *log_file;
53 bool logging_enabled;
Michael Rothbc62fa02012-01-21 16:42:27 -060054#ifdef _WIN32
55 GAService service;
56#endif
Michael Roth48ff7a62011-07-20 15:19:37 -050057};
58
59static struct GAState *ga_state;
60
Michael Rothbc62fa02012-01-21 16:42:27 -060061#ifdef _WIN32
62DWORD WINAPI service_ctrl_handler(DWORD ctrl, DWORD type, LPVOID data,
63 LPVOID ctx);
64VOID WINAPI service_main(DWORD argc, TCHAR *argv[]);
65#endif
66
Michael Roth48ff7a62011-07-20 15:19:37 -050067static void quit_handler(int sig)
68{
Stefan Weil2542bfd2011-08-28 21:45:40 +020069 g_debug("received signal num %d, quitting", sig);
Michael Roth48ff7a62011-07-20 15:19:37 -050070
71 if (g_main_loop_is_running(ga_state->main_loop)) {
72 g_main_loop_quit(ga_state->main_loop);
73 }
74}
75
Michael Rothbc62fa02012-01-21 16:42:27 -060076#ifndef _WIN32
Luiz Capitulino11d0f122012-02-28 11:03:03 -030077/* reap _all_ terminated children */
78static void child_handler(int sig)
79{
80 int status;
81 while (waitpid(-1, &status, WNOHANG) > 0) /* NOTHING */;
82}
83
Michael Roth125b3102012-01-19 00:18:20 -060084static gboolean register_signal_handlers(void)
Michael Roth48ff7a62011-07-20 15:19:37 -050085{
Luiz Capitulino11d0f122012-02-28 11:03:03 -030086 struct sigaction sigact, sigact_chld;
Michael Roth48ff7a62011-07-20 15:19:37 -050087 int ret;
88
89 memset(&sigact, 0, sizeof(struct sigaction));
90 sigact.sa_handler = quit_handler;
91
92 ret = sigaction(SIGINT, &sigact, NULL);
93 if (ret == -1) {
94 g_error("error configuring signal handler: %s", strerror(errno));
Michael Roth125b3102012-01-19 00:18:20 -060095 return false;
Michael Roth48ff7a62011-07-20 15:19:37 -050096 }
97 ret = sigaction(SIGTERM, &sigact, NULL);
98 if (ret == -1) {
99 g_error("error configuring signal handler: %s", strerror(errno));
Michael Roth125b3102012-01-19 00:18:20 -0600100 return false;
Michael Roth48ff7a62011-07-20 15:19:37 -0500101 }
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300102
103 memset(&sigact_chld, 0, sizeof(struct sigaction));
104 sigact_chld.sa_handler = child_handler;
105 sigact_chld.sa_flags = SA_NOCLDSTOP;
106 ret = sigaction(SIGCHLD, &sigact_chld, NULL);
107 if (ret == -1) {
108 g_error("error configuring signal handler: %s", strerror(errno));
109 }
110
Michael Roth125b3102012-01-19 00:18:20 -0600111 return true;
Michael Roth48ff7a62011-07-20 15:19:37 -0500112}
Michael Rothd8ca6852012-01-19 22:04:34 -0600113#endif
Michael Roth48ff7a62011-07-20 15:19:37 -0500114
115static void usage(const char *cmd)
116{
117 printf(
118"Usage: %s -c <channel_opts>\n"
119"QEMU Guest Agent %s\n"
120"\n"
121" -m, --method transport method: one of unix-listen, virtio-serial, or\n"
122" isa-serial (virtio-serial is the default)\n"
123" -p, --path device/socket path (%s is the default for virtio-serial)\n"
124" -l, --logfile set logfile path, logs to stderr by default\n"
125" -f, --pidfile specify pidfile (default is %s)\n"
126" -v, --verbose log extra debugging information\n"
127" -V, --version print version information and exit\n"
128" -d, --daemonize become a daemon\n"
Michael Rothbc62fa02012-01-21 16:42:27 -0600129#ifdef _WIN32
130" -s, --service service commands: install, uninstall\n"
Michael Rothd8ca6852012-01-19 22:04:34 -0600131#endif
Stefan Weildabdf392012-01-08 19:35:09 +0100132" -b, --blacklist comma-separated list of RPCs to disable (no spaces, \"?\""
Michael Rothabd6cf62011-12-06 22:03:42 -0600133" to list available RPCs)\n"
Michael Roth48ff7a62011-07-20 15:19:37 -0500134" -h, --help display this help and exit\n"
135"\n"
136"Report bugs to <mdroth@linux.vnet.ibm.com>\n"
137 , cmd, QGA_VERSION, QGA_VIRTIO_PATH_DEFAULT, QGA_PIDFILE_DEFAULT);
138}
139
Michael Roth48ff7a62011-07-20 15:19:37 -0500140static const char *ga_log_level_str(GLogLevelFlags level)
141{
142 switch (level & G_LOG_LEVEL_MASK) {
143 case G_LOG_LEVEL_ERROR:
144 return "error";
145 case G_LOG_LEVEL_CRITICAL:
146 return "critical";
147 case G_LOG_LEVEL_WARNING:
148 return "warning";
149 case G_LOG_LEVEL_MESSAGE:
150 return "message";
151 case G_LOG_LEVEL_INFO:
152 return "info";
153 case G_LOG_LEVEL_DEBUG:
154 return "debug";
155 default:
156 return "user";
157 }
158}
159
160bool ga_logging_enabled(GAState *s)
161{
162 return s->logging_enabled;
163}
164
165void ga_disable_logging(GAState *s)
166{
167 s->logging_enabled = false;
168}
169
170void ga_enable_logging(GAState *s)
171{
172 s->logging_enabled = true;
173}
174
175static void ga_log(const gchar *domain, GLogLevelFlags level,
176 const gchar *msg, gpointer opaque)
177{
178 GAState *s = opaque;
179 GTimeVal time;
180 const char *level_str = ga_log_level_str(level);
181
182 if (!ga_logging_enabled(s)) {
183 return;
184 }
185
186 level &= G_LOG_LEVEL_MASK;
Michael Rothd8ca6852012-01-19 22:04:34 -0600187#ifndef _WIN32
Michael Roth8f477472011-08-11 15:38:11 -0500188 if (domain && strcmp(domain, "syslog") == 0) {
Michael Roth48ff7a62011-07-20 15:19:37 -0500189 syslog(LOG_INFO, "%s: %s", level_str, msg);
190 } else if (level & s->log_level) {
Michael Rothd8ca6852012-01-19 22:04:34 -0600191#else
192 if (level & s->log_level) {
193#endif
Michael Roth48ff7a62011-07-20 15:19:37 -0500194 g_get_current_time(&time);
195 fprintf(s->log_file,
196 "%lu.%lu: %s: %s\n", time.tv_sec, time.tv_usec, level_str, msg);
197 fflush(s->log_file);
198 }
199}
200
Michael Rothd8ca6852012-01-19 22:04:34 -0600201#ifndef _WIN32
Michael Roth48ff7a62011-07-20 15:19:37 -0500202static void become_daemon(const char *pidfile)
203{
204 pid_t pid, sid;
205 int pidfd;
206 char *pidstr = NULL;
207
208 pid = fork();
209 if (pid < 0) {
210 exit(EXIT_FAILURE);
211 }
212 if (pid > 0) {
213 exit(EXIT_SUCCESS);
214 }
215
216 pidfd = open(pidfile, O_CREAT|O_WRONLY|O_EXCL, S_IRUSR|S_IWUSR);
217 if (pidfd == -1) {
218 g_critical("Cannot create pid file, %s", strerror(errno));
219 exit(EXIT_FAILURE);
220 }
221
222 if (asprintf(&pidstr, "%d", getpid()) == -1) {
223 g_critical("Cannot allocate memory");
224 goto fail;
225 }
226 if (write(pidfd, pidstr, strlen(pidstr)) != strlen(pidstr)) {
227 free(pidstr);
228 g_critical("Failed to write pid file");
229 goto fail;
230 }
231
232 umask(0);
233 sid = setsid();
234 if (sid < 0) {
235 goto fail;
236 }
237 if ((chdir("/")) < 0) {
238 goto fail;
239 }
240
241 close(STDIN_FILENO);
242 close(STDOUT_FILENO);
243 close(STDERR_FILENO);
244 free(pidstr);
245 return;
246
247fail:
248 unlink(pidfile);
249 g_critical("failed to daemonize");
250 exit(EXIT_FAILURE);
251}
Michael Rothd8ca6852012-01-19 22:04:34 -0600252#endif
Michael Roth48ff7a62011-07-20 15:19:37 -0500253
Michael Roth125b3102012-01-19 00:18:20 -0600254static int send_response(GAState *s, QObject *payload)
Michael Roth48ff7a62011-07-20 15:19:37 -0500255{
Michael Roth48ff7a62011-07-20 15:19:37 -0500256 const char *buf;
257 QString *payload_qstr;
Michael Roth125b3102012-01-19 00:18:20 -0600258 GIOStatus status;
Michael Roth48ff7a62011-07-20 15:19:37 -0500259
Michael Roth125b3102012-01-19 00:18:20 -0600260 g_assert(payload && s->channel);
Michael Roth48ff7a62011-07-20 15:19:37 -0500261
262 payload_qstr = qobject_to_json(payload);
263 if (!payload_qstr) {
264 return -EINVAL;
265 }
266
267 qstring_append_chr(payload_qstr, '\n');
268 buf = qstring_get_str(payload_qstr);
Michael Roth125b3102012-01-19 00:18:20 -0600269 status = ga_channel_write_all(s->channel, buf, strlen(buf));
Michael Roth48ff7a62011-07-20 15:19:37 -0500270 QDECREF(payload_qstr);
Michael Roth125b3102012-01-19 00:18:20 -0600271 if (status != G_IO_STATUS_NORMAL) {
272 return -EIO;
Michael Roth48ff7a62011-07-20 15:19:37 -0500273 }
Michael Roth125b3102012-01-19 00:18:20 -0600274
275 return 0;
Michael Roth48ff7a62011-07-20 15:19:37 -0500276}
277
278static void process_command(GAState *s, QDict *req)
279{
280 QObject *rsp = NULL;
281 int ret;
282
283 g_assert(req);
284 g_debug("processing command");
285 rsp = qmp_dispatch(QOBJECT(req));
286 if (rsp) {
Michael Roth125b3102012-01-19 00:18:20 -0600287 ret = send_response(s, rsp);
Michael Roth48ff7a62011-07-20 15:19:37 -0500288 if (ret) {
Michael Roth125b3102012-01-19 00:18:20 -0600289 g_warning("error sending response: %s", strerror(ret));
Michael Roth48ff7a62011-07-20 15:19:37 -0500290 }
291 qobject_decref(rsp);
292 } else {
293 g_warning("error getting response");
294 }
295}
296
297/* handle requests/control events coming in over the channel */
298static void process_event(JSONMessageParser *parser, QList *tokens)
299{
300 GAState *s = container_of(parser, GAState, parser);
301 QObject *obj;
302 QDict *qdict;
303 Error *err = NULL;
304 int ret;
305
306 g_assert(s && parser);
307
308 g_debug("process_event: called");
309 obj = json_parser_parse_err(tokens, NULL, &err);
310 if (err || !obj || qobject_type(obj) != QTYPE_QDICT) {
311 qobject_decref(obj);
312 qdict = qdict_new();
313 if (!err) {
314 g_warning("failed to parse event: unknown error");
315 error_set(&err, QERR_JSON_PARSING);
316 } else {
317 g_warning("failed to parse event: %s", error_get_pretty(err));
318 }
319 qdict_put_obj(qdict, "error", error_get_qobject(err));
320 error_free(err);
321 } else {
322 qdict = qobject_to_qdict(obj);
323 }
324
325 g_assert(qdict);
326
327 /* handle host->guest commands */
328 if (qdict_haskey(qdict, "execute")) {
329 process_command(s, qdict);
330 } else {
331 if (!qdict_haskey(qdict, "error")) {
332 QDECREF(qdict);
333 qdict = qdict_new();
334 g_warning("unrecognized payload format");
335 error_set(&err, QERR_UNSUPPORTED);
336 qdict_put_obj(qdict, "error", error_get_qobject(err));
337 error_free(err);
338 }
Michael Roth125b3102012-01-19 00:18:20 -0600339 ret = send_response(s, QOBJECT(qdict));
Michael Roth48ff7a62011-07-20 15:19:37 -0500340 if (ret) {
Michael Roth125b3102012-01-19 00:18:20 -0600341 g_warning("error sending error response: %s", strerror(ret));
Michael Roth48ff7a62011-07-20 15:19:37 -0500342 }
343 }
344
345 QDECREF(qdict);
346}
347
Michael Roth125b3102012-01-19 00:18:20 -0600348/* false return signals GAChannel to close the current client connection */
349static gboolean channel_event_cb(GIOCondition condition, gpointer data)
Michael Roth48ff7a62011-07-20 15:19:37 -0500350{
351 GAState *s = data;
Michael Roth125b3102012-01-19 00:18:20 -0600352 gchar buf[QGA_READ_COUNT_DEFAULT+1];
Michael Roth48ff7a62011-07-20 15:19:37 -0500353 gsize count;
354 GError *err = NULL;
Michael Roth125b3102012-01-19 00:18:20 -0600355 GIOStatus status = ga_channel_read(s->channel, buf, QGA_READ_COUNT_DEFAULT, &count);
Michael Roth48ff7a62011-07-20 15:19:37 -0500356 if (err != NULL) {
357 g_warning("error reading channel: %s", err->message);
Michael Roth48ff7a62011-07-20 15:19:37 -0500358 g_error_free(err);
359 return false;
360 }
361 switch (status) {
362 case G_IO_STATUS_ERROR:
Michael Roth125b3102012-01-19 00:18:20 -0600363 g_warning("error reading channel");
Michael Roth48ff7a62011-07-20 15:19:37 -0500364 return false;
365 case G_IO_STATUS_NORMAL:
Michael Roth125b3102012-01-19 00:18:20 -0600366 buf[count] = 0;
Michael Roth48ff7a62011-07-20 15:19:37 -0500367 g_debug("read data, count: %d, data: %s", (int)count, buf);
368 json_message_parser_feed(&s->parser, (char *)buf, (int)count);
Michael Roth125b3102012-01-19 00:18:20 -0600369 break;
370 case G_IO_STATUS_EOF:
371 g_debug("received EOF");
372 if (!s->virtio) {
373 return false;
374 }
Michael Roth48ff7a62011-07-20 15:19:37 -0500375 case G_IO_STATUS_AGAIN:
376 /* virtio causes us to spin here when no process is attached to
377 * host-side chardev. sleep a bit to mitigate this
378 */
379 if (s->virtio) {
380 usleep(100*1000);
381 }
382 return true;
Michael Roth48ff7a62011-07-20 15:19:37 -0500383 default:
384 g_warning("unknown channel read status, closing");
Michael Roth48ff7a62011-07-20 15:19:37 -0500385 return false;
386 }
387 return true;
388}
389
Michael Roth125b3102012-01-19 00:18:20 -0600390static gboolean channel_init(GAState *s, const gchar *method, const gchar *path)
Michael Roth48ff7a62011-07-20 15:19:37 -0500391{
Michael Roth125b3102012-01-19 00:18:20 -0600392 GAChannelMethod channel_method;
Michael Roth48ff7a62011-07-20 15:19:37 -0500393
Michael Roth125b3102012-01-19 00:18:20 -0600394 if (method == NULL) {
395 method = "virtio-serial";
Michael Roth48ff7a62011-07-20 15:19:37 -0500396 }
397
Michael Roth125b3102012-01-19 00:18:20 -0600398 if (path == NULL) {
399 if (strcmp(method, "virtio-serial") != 0) {
Michael Roth48ff7a62011-07-20 15:19:37 -0500400 g_critical("must specify a path for this channel");
Michael Roth125b3102012-01-19 00:18:20 -0600401 return false;
Michael Roth48ff7a62011-07-20 15:19:37 -0500402 }
403 /* try the default path for the virtio-serial port */
Michael Roth125b3102012-01-19 00:18:20 -0600404 path = QGA_VIRTIO_PATH_DEFAULT;
Michael Roth48ff7a62011-07-20 15:19:37 -0500405 }
406
Michael Roth125b3102012-01-19 00:18:20 -0600407 if (strcmp(method, "virtio-serial") == 0) {
408 s->virtio = true; /* virtio requires special handling in some cases */
409 channel_method = GA_CHANNEL_VIRTIO_SERIAL;
410 } else if (strcmp(method, "isa-serial") == 0) {
411 channel_method = GA_CHANNEL_ISA_SERIAL;
412 } else if (strcmp(method, "unix-listen") == 0) {
413 channel_method = GA_CHANNEL_UNIX_LISTEN;
Michael Roth48ff7a62011-07-20 15:19:37 -0500414 } else {
Michael Roth125b3102012-01-19 00:18:20 -0600415 g_critical("unsupported channel method/type: %s", method);
416 return false;
Michael Roth48ff7a62011-07-20 15:19:37 -0500417 }
418
Michael Roth125b3102012-01-19 00:18:20 -0600419 s->channel = ga_channel_new(channel_method, path, channel_event_cb, s);
420 if (!s->channel) {
421 g_critical("failed to create guest agent channel");
422 return false;
423 }
424
425 return true;
Michael Roth48ff7a62011-07-20 15:19:37 -0500426}
427
Michael Rothbc62fa02012-01-21 16:42:27 -0600428#ifdef _WIN32
429DWORD WINAPI service_ctrl_handler(DWORD ctrl, DWORD type, LPVOID data,
430 LPVOID ctx)
431{
432 DWORD ret = NO_ERROR;
433 GAService *service = &ga_state->service;
434
435 switch (ctrl)
436 {
437 case SERVICE_CONTROL_STOP:
438 case SERVICE_CONTROL_SHUTDOWN:
439 quit_handler(SIGTERM);
440 service->status.dwCurrentState = SERVICE_STOP_PENDING;
441 SetServiceStatus(service->status_handle, &service->status);
442 break;
443
444 default:
445 ret = ERROR_CALL_NOT_IMPLEMENTED;
446 }
447 return ret;
448}
449
450VOID WINAPI service_main(DWORD argc, TCHAR *argv[])
451{
452 GAService *service = &ga_state->service;
453
454 service->status_handle = RegisterServiceCtrlHandlerEx(QGA_SERVICE_NAME,
455 service_ctrl_handler, NULL);
456
457 if (service->status_handle == 0) {
458 g_critical("Failed to register extended requests function!\n");
459 return;
460 }
461
462 service->status.dwServiceType = SERVICE_WIN32;
463 service->status.dwCurrentState = SERVICE_RUNNING;
464 service->status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
465 service->status.dwWin32ExitCode = NO_ERROR;
466 service->status.dwServiceSpecificExitCode = NO_ERROR;
467 service->status.dwCheckPoint = 0;
468 service->status.dwWaitHint = 0;
469 SetServiceStatus(service->status_handle, &service->status);
470
471 g_main_loop_run(ga_state->main_loop);
472
473 service->status.dwCurrentState = SERVICE_STOPPED;
474 SetServiceStatus(service->status_handle, &service->status);
475}
476#endif
477
Michael Roth48ff7a62011-07-20 15:19:37 -0500478int main(int argc, char **argv)
479{
Michael Rothbc62fa02012-01-21 16:42:27 -0600480 const char *sopt = "hVvdm:p:l:f:b:s:";
Michael Roth48ff7a62011-07-20 15:19:37 -0500481 const char *method = NULL, *path = NULL, *pidfile = QGA_PIDFILE_DEFAULT;
Michael Rothbc62fa02012-01-21 16:42:27 -0600482 const char *log_file_name = NULL;
483#ifdef _WIN32
484 const char *service = NULL;
485#endif
Michael Roth48ff7a62011-07-20 15:19:37 -0500486 const struct option lopt[] = {
487 { "help", 0, NULL, 'h' },
488 { "version", 0, NULL, 'V' },
Michael Rothbc62fa02012-01-21 16:42:27 -0600489 { "logfile", 1, NULL, 'l' },
490 { "pidfile", 1, NULL, 'f' },
Michael Roth48ff7a62011-07-20 15:19:37 -0500491 { "verbose", 0, NULL, 'v' },
Michael Rothbc62fa02012-01-21 16:42:27 -0600492 { "method", 1, NULL, 'm' },
493 { "path", 1, NULL, 'p' },
Michael Roth48ff7a62011-07-20 15:19:37 -0500494 { "daemonize", 0, NULL, 'd' },
Michael Rothbc62fa02012-01-21 16:42:27 -0600495 { "blacklist", 1, NULL, 'b' },
496#ifdef _WIN32
497 { "service", 1, NULL, 's' },
498#endif
Michael Roth48ff7a62011-07-20 15:19:37 -0500499 { NULL, 0, NULL, 0 }
500 };
Michael Rothabd6cf62011-12-06 22:03:42 -0600501 int opt_ind = 0, ch, daemonize = 0, i, j, len;
Michael Roth48ff7a62011-07-20 15:19:37 -0500502 GLogLevelFlags log_level = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL;
503 FILE *log_file = stderr;
504 GAState *s;
505
Michael Rothabd6cf62011-12-06 22:03:42 -0600506 module_call_init(MODULE_INIT_QAPI);
507
Michael Roth48ff7a62011-07-20 15:19:37 -0500508 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
509 switch (ch) {
510 case 'm':
511 method = optarg;
512 break;
513 case 'p':
514 path = optarg;
515 break;
516 case 'l':
Michael Rothbc62fa02012-01-21 16:42:27 -0600517 log_file_name = optarg;
518 log_file = fopen(log_file_name, "a");
Michael Roth48ff7a62011-07-20 15:19:37 -0500519 if (!log_file) {
520 g_critical("unable to open specified log file: %s",
521 strerror(errno));
522 return EXIT_FAILURE;
523 }
524 break;
525 case 'f':
526 pidfile = optarg;
527 break;
528 case 'v':
529 /* enable all log levels */
530 log_level = G_LOG_LEVEL_MASK;
531 break;
532 case 'V':
533 printf("QEMU Guest Agent %s\n", QGA_VERSION);
534 return 0;
535 case 'd':
536 daemonize = 1;
537 break;
Michael Rothabd6cf62011-12-06 22:03:42 -0600538 case 'b': {
539 char **list_head, **list;
540 if (*optarg == '?') {
541 list_head = list = qmp_get_command_list();
542 while (*list != NULL) {
543 printf("%s\n", *list);
544 g_free(*list);
545 list++;
546 }
547 g_free(list_head);
548 return 0;
549 }
550 for (j = 0, i = 0, len = strlen(optarg); i < len; i++) {
551 if (optarg[i] == ',') {
552 optarg[i] = 0;
553 qmp_disable_command(&optarg[j]);
554 g_debug("disabling command: %s", &optarg[j]);
555 j = i + 1;
556 }
557 }
558 if (j < i) {
559 qmp_disable_command(&optarg[j]);
560 g_debug("disabling command: %s", &optarg[j]);
561 }
562 break;
563 }
Michael Rothbc62fa02012-01-21 16:42:27 -0600564#ifdef _WIN32
565 case 's':
566 service = optarg;
567 if (strcmp(service, "install") == 0) {
568 return ga_install_service(path, log_file_name);
569 } else if (strcmp(service, "uninstall") == 0) {
570 return ga_uninstall_service();
571 } else {
572 printf("Unknown service command.\n");
573 return EXIT_FAILURE;
574 }
575 break;
576#endif
Michael Roth48ff7a62011-07-20 15:19:37 -0500577 case 'h':
578 usage(argv[0]);
579 return 0;
580 case '?':
581 g_print("Unknown option, try '%s --help' for more information.\n",
582 argv[0]);
583 return EXIT_FAILURE;
584 }
585 }
586
Michael Rothd8ca6852012-01-19 22:04:34 -0600587#ifndef _WIN32
Michael Roth48ff7a62011-07-20 15:19:37 -0500588 if (daemonize) {
589 g_debug("starting daemon");
590 become_daemon(pidfile);
591 }
Michael Rothd8ca6852012-01-19 22:04:34 -0600592#endif
Michael Roth48ff7a62011-07-20 15:19:37 -0500593
Anthony Liguori7267c092011-08-20 22:09:37 -0500594 s = g_malloc0(sizeof(GAState));
Michael Roth48ff7a62011-07-20 15:19:37 -0500595 s->log_file = log_file;
596 s->log_level = log_level;
597 g_log_set_default_handler(ga_log, s);
598 g_log_set_fatal_mask(NULL, G_LOG_LEVEL_ERROR);
599 s->logging_enabled = true;
Michael Rothe3d4d252011-07-19 15:41:55 -0500600 s->command_state = ga_command_state_new();
601 ga_command_state_init(s, s->command_state);
602 ga_command_state_init_all(s->command_state);
Michael Roth125b3102012-01-19 00:18:20 -0600603 json_message_parser_init(&s->parser, process_event);
Michael Roth48ff7a62011-07-20 15:19:37 -0500604 ga_state = s;
Michael Rothd8ca6852012-01-19 22:04:34 -0600605#ifndef _WIN32
Michael Roth125b3102012-01-19 00:18:20 -0600606 if (!register_signal_handlers()) {
607 g_critical("failed to register signal handlers");
608 goto out_bad;
609 }
Michael Rothd8ca6852012-01-19 22:04:34 -0600610#endif
Michael Roth48ff7a62011-07-20 15:19:37 -0500611
Michael Roth125b3102012-01-19 00:18:20 -0600612 s->main_loop = g_main_loop_new(NULL, false);
613 if (!channel_init(ga_state, method, path)) {
614 g_critical("failed to initialize guest agent channel");
615 goto out_bad;
616 }
Michael Rothbc62fa02012-01-21 16:42:27 -0600617#ifndef _WIN32
Michael Roth48ff7a62011-07-20 15:19:37 -0500618 g_main_loop_run(ga_state->main_loop);
Michael Rothbc62fa02012-01-21 16:42:27 -0600619#else
620 if (daemonize) {
621 SERVICE_TABLE_ENTRY service_table[] = {
622 { (char *)QGA_SERVICE_NAME, service_main }, { NULL, NULL } };
623 StartServiceCtrlDispatcher(service_table);
624 } else {
625 g_main_loop_run(ga_state->main_loop);
626 }
627#endif
Michael Roth48ff7a62011-07-20 15:19:37 -0500628
Michael Rothe3d4d252011-07-19 15:41:55 -0500629 ga_command_state_cleanup_all(ga_state->command_state);
Michael Roth125b3102012-01-19 00:18:20 -0600630 ga_channel_free(ga_state->channel);
Michael Roth48ff7a62011-07-20 15:19:37 -0500631
Michael Roth125b3102012-01-19 00:18:20 -0600632 if (daemonize) {
633 unlink(pidfile);
634 }
Michael Roth48ff7a62011-07-20 15:19:37 -0500635 return 0;
Michael Roth125b3102012-01-19 00:18:20 -0600636
637out_bad:
638 if (daemonize) {
639 unlink(pidfile);
640 }
641 return EXIT_FAILURE;
Michael Roth48ff7a62011-07-20 15:19:37 -0500642}