blob: 93ebc3e4718fd5831802a3de71c4593dc915f193 [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>
Michael Rothd8ca6852012-01-19 22:04:34 -060020#endif
Michael Roth48ff7a62011-07-20 15:19:37 -050021#include "json-streamer.h"
22#include "json-parser.h"
23#include "qint.h"
24#include "qjson.h"
25#include "qga/guest-agent-core.h"
26#include "module.h"
27#include "signal.h"
28#include "qerror.h"
29#include "error_int.h"
Michael Rothabd6cf62011-12-06 22:03:42 -060030#include "qapi/qmp-core.h"
Michael Roth125b3102012-01-19 00:18:20 -060031#include "qga/channel.h"
Michael Roth48ff7a62011-07-20 15:19:37 -050032
33#define QGA_VIRTIO_PATH_DEFAULT "/dev/virtio-ports/org.qemu.guest_agent.0"
34#define QGA_PIDFILE_DEFAULT "/var/run/qemu-ga.pid"
Michael Roth48ff7a62011-07-20 15:19:37 -050035
36struct GAState {
37 JSONMessageParser parser;
38 GMainLoop *main_loop;
Michael Roth125b3102012-01-19 00:18:20 -060039 GAChannel *channel;
Michael Roth48ff7a62011-07-20 15:19:37 -050040 bool virtio; /* fastpath to check for virtio to deal with poll() quirks */
41 GACommandState *command_state;
42 GLogLevelFlags log_level;
43 FILE *log_file;
44 bool logging_enabled;
45};
46
47static struct GAState *ga_state;
48
Michael Rothd8ca6852012-01-19 22:04:34 -060049#ifndef _WIN32
Michael Roth48ff7a62011-07-20 15:19:37 -050050static void quit_handler(int sig)
51{
Stefan Weil2542bfd2011-08-28 21:45:40 +020052 g_debug("received signal num %d, quitting", sig);
Michael Roth48ff7a62011-07-20 15:19:37 -050053
54 if (g_main_loop_is_running(ga_state->main_loop)) {
55 g_main_loop_quit(ga_state->main_loop);
56 }
57}
58
Michael Roth125b3102012-01-19 00:18:20 -060059static gboolean register_signal_handlers(void)
Michael Roth48ff7a62011-07-20 15:19:37 -050060{
61 struct sigaction sigact;
62 int ret;
63
64 memset(&sigact, 0, sizeof(struct sigaction));
65 sigact.sa_handler = quit_handler;
66
67 ret = sigaction(SIGINT, &sigact, NULL);
68 if (ret == -1) {
69 g_error("error configuring signal handler: %s", strerror(errno));
Michael Roth125b3102012-01-19 00:18:20 -060070 return false;
Michael Roth48ff7a62011-07-20 15:19:37 -050071 }
72 ret = sigaction(SIGTERM, &sigact, NULL);
73 if (ret == -1) {
74 g_error("error configuring signal handler: %s", strerror(errno));
Michael Roth125b3102012-01-19 00:18:20 -060075 return false;
Michael Roth48ff7a62011-07-20 15:19:37 -050076 }
Michael Roth125b3102012-01-19 00:18:20 -060077 return true;
Michael Roth48ff7a62011-07-20 15:19:37 -050078}
Michael Rothd8ca6852012-01-19 22:04:34 -060079#endif
Michael Roth48ff7a62011-07-20 15:19:37 -050080
81static void usage(const char *cmd)
82{
83 printf(
84"Usage: %s -c <channel_opts>\n"
85"QEMU Guest Agent %s\n"
86"\n"
87" -m, --method transport method: one of unix-listen, virtio-serial, or\n"
88" isa-serial (virtio-serial is the default)\n"
89" -p, --path device/socket path (%s is the default for virtio-serial)\n"
90" -l, --logfile set logfile path, logs to stderr by default\n"
91" -f, --pidfile specify pidfile (default is %s)\n"
92" -v, --verbose log extra debugging information\n"
93" -V, --version print version information and exit\n"
Michael Rothd8ca6852012-01-19 22:04:34 -060094#ifndef _WIN32
Michael Roth48ff7a62011-07-20 15:19:37 -050095" -d, --daemonize become a daemon\n"
Michael Rothd8ca6852012-01-19 22:04:34 -060096#endif
Stefan Weildabdf392012-01-08 19:35:09 +010097" -b, --blacklist comma-separated list of RPCs to disable (no spaces, \"?\""
Michael Rothabd6cf62011-12-06 22:03:42 -060098" to list available RPCs)\n"
Michael Roth48ff7a62011-07-20 15:19:37 -050099" -h, --help display this help and exit\n"
100"\n"
101"Report bugs to <mdroth@linux.vnet.ibm.com>\n"
102 , cmd, QGA_VERSION, QGA_VIRTIO_PATH_DEFAULT, QGA_PIDFILE_DEFAULT);
103}
104
Michael Roth48ff7a62011-07-20 15:19:37 -0500105static const char *ga_log_level_str(GLogLevelFlags level)
106{
107 switch (level & G_LOG_LEVEL_MASK) {
108 case G_LOG_LEVEL_ERROR:
109 return "error";
110 case G_LOG_LEVEL_CRITICAL:
111 return "critical";
112 case G_LOG_LEVEL_WARNING:
113 return "warning";
114 case G_LOG_LEVEL_MESSAGE:
115 return "message";
116 case G_LOG_LEVEL_INFO:
117 return "info";
118 case G_LOG_LEVEL_DEBUG:
119 return "debug";
120 default:
121 return "user";
122 }
123}
124
125bool ga_logging_enabled(GAState *s)
126{
127 return s->logging_enabled;
128}
129
130void ga_disable_logging(GAState *s)
131{
132 s->logging_enabled = false;
133}
134
135void ga_enable_logging(GAState *s)
136{
137 s->logging_enabled = true;
138}
139
140static void ga_log(const gchar *domain, GLogLevelFlags level,
141 const gchar *msg, gpointer opaque)
142{
143 GAState *s = opaque;
144 GTimeVal time;
145 const char *level_str = ga_log_level_str(level);
146
147 if (!ga_logging_enabled(s)) {
148 return;
149 }
150
151 level &= G_LOG_LEVEL_MASK;
Michael Rothd8ca6852012-01-19 22:04:34 -0600152#ifndef _WIN32
Michael Roth8f477472011-08-11 15:38:11 -0500153 if (domain && strcmp(domain, "syslog") == 0) {
Michael Roth48ff7a62011-07-20 15:19:37 -0500154 syslog(LOG_INFO, "%s: %s", level_str, msg);
155 } else if (level & s->log_level) {
Michael Rothd8ca6852012-01-19 22:04:34 -0600156#else
157 if (level & s->log_level) {
158#endif
Michael Roth48ff7a62011-07-20 15:19:37 -0500159 g_get_current_time(&time);
160 fprintf(s->log_file,
161 "%lu.%lu: %s: %s\n", time.tv_sec, time.tv_usec, level_str, msg);
162 fflush(s->log_file);
163 }
164}
165
Michael Rothd8ca6852012-01-19 22:04:34 -0600166#ifndef _WIN32
Michael Roth48ff7a62011-07-20 15:19:37 -0500167static void become_daemon(const char *pidfile)
168{
169 pid_t pid, sid;
170 int pidfd;
171 char *pidstr = NULL;
172
173 pid = fork();
174 if (pid < 0) {
175 exit(EXIT_FAILURE);
176 }
177 if (pid > 0) {
178 exit(EXIT_SUCCESS);
179 }
180
181 pidfd = open(pidfile, O_CREAT|O_WRONLY|O_EXCL, S_IRUSR|S_IWUSR);
182 if (pidfd == -1) {
183 g_critical("Cannot create pid file, %s", strerror(errno));
184 exit(EXIT_FAILURE);
185 }
186
187 if (asprintf(&pidstr, "%d", getpid()) == -1) {
188 g_critical("Cannot allocate memory");
189 goto fail;
190 }
191 if (write(pidfd, pidstr, strlen(pidstr)) != strlen(pidstr)) {
192 free(pidstr);
193 g_critical("Failed to write pid file");
194 goto fail;
195 }
196
197 umask(0);
198 sid = setsid();
199 if (sid < 0) {
200 goto fail;
201 }
202 if ((chdir("/")) < 0) {
203 goto fail;
204 }
205
206 close(STDIN_FILENO);
207 close(STDOUT_FILENO);
208 close(STDERR_FILENO);
209 free(pidstr);
210 return;
211
212fail:
213 unlink(pidfile);
214 g_critical("failed to daemonize");
215 exit(EXIT_FAILURE);
216}
Michael Rothd8ca6852012-01-19 22:04:34 -0600217#endif
Michael Roth48ff7a62011-07-20 15:19:37 -0500218
Michael Roth125b3102012-01-19 00:18:20 -0600219static int send_response(GAState *s, QObject *payload)
Michael Roth48ff7a62011-07-20 15:19:37 -0500220{
Michael Roth48ff7a62011-07-20 15:19:37 -0500221 const char *buf;
222 QString *payload_qstr;
Michael Roth125b3102012-01-19 00:18:20 -0600223 GIOStatus status;
Michael Roth48ff7a62011-07-20 15:19:37 -0500224
Michael Roth125b3102012-01-19 00:18:20 -0600225 g_assert(payload && s->channel);
Michael Roth48ff7a62011-07-20 15:19:37 -0500226
227 payload_qstr = qobject_to_json(payload);
228 if (!payload_qstr) {
229 return -EINVAL;
230 }
231
232 qstring_append_chr(payload_qstr, '\n');
233 buf = qstring_get_str(payload_qstr);
Michael Roth125b3102012-01-19 00:18:20 -0600234 status = ga_channel_write_all(s->channel, buf, strlen(buf));
Michael Roth48ff7a62011-07-20 15:19:37 -0500235 QDECREF(payload_qstr);
Michael Roth125b3102012-01-19 00:18:20 -0600236 if (status != G_IO_STATUS_NORMAL) {
237 return -EIO;
Michael Roth48ff7a62011-07-20 15:19:37 -0500238 }
Michael Roth125b3102012-01-19 00:18:20 -0600239
240 return 0;
Michael Roth48ff7a62011-07-20 15:19:37 -0500241}
242
243static void process_command(GAState *s, QDict *req)
244{
245 QObject *rsp = NULL;
246 int ret;
247
248 g_assert(req);
249 g_debug("processing command");
250 rsp = qmp_dispatch(QOBJECT(req));
251 if (rsp) {
Michael Roth125b3102012-01-19 00:18:20 -0600252 ret = send_response(s, rsp);
Michael Roth48ff7a62011-07-20 15:19:37 -0500253 if (ret) {
Michael Roth125b3102012-01-19 00:18:20 -0600254 g_warning("error sending response: %s", strerror(ret));
Michael Roth48ff7a62011-07-20 15:19:37 -0500255 }
256 qobject_decref(rsp);
257 } else {
258 g_warning("error getting response");
259 }
260}
261
262/* handle requests/control events coming in over the channel */
263static void process_event(JSONMessageParser *parser, QList *tokens)
264{
265 GAState *s = container_of(parser, GAState, parser);
266 QObject *obj;
267 QDict *qdict;
268 Error *err = NULL;
269 int ret;
270
271 g_assert(s && parser);
272
273 g_debug("process_event: called");
274 obj = json_parser_parse_err(tokens, NULL, &err);
275 if (err || !obj || qobject_type(obj) != QTYPE_QDICT) {
276 qobject_decref(obj);
277 qdict = qdict_new();
278 if (!err) {
279 g_warning("failed to parse event: unknown error");
280 error_set(&err, QERR_JSON_PARSING);
281 } else {
282 g_warning("failed to parse event: %s", error_get_pretty(err));
283 }
284 qdict_put_obj(qdict, "error", error_get_qobject(err));
285 error_free(err);
286 } else {
287 qdict = qobject_to_qdict(obj);
288 }
289
290 g_assert(qdict);
291
292 /* handle host->guest commands */
293 if (qdict_haskey(qdict, "execute")) {
294 process_command(s, qdict);
295 } else {
296 if (!qdict_haskey(qdict, "error")) {
297 QDECREF(qdict);
298 qdict = qdict_new();
299 g_warning("unrecognized payload format");
300 error_set(&err, QERR_UNSUPPORTED);
301 qdict_put_obj(qdict, "error", error_get_qobject(err));
302 error_free(err);
303 }
Michael Roth125b3102012-01-19 00:18:20 -0600304 ret = send_response(s, QOBJECT(qdict));
Michael Roth48ff7a62011-07-20 15:19:37 -0500305 if (ret) {
Michael Roth125b3102012-01-19 00:18:20 -0600306 g_warning("error sending error response: %s", strerror(ret));
Michael Roth48ff7a62011-07-20 15:19:37 -0500307 }
308 }
309
310 QDECREF(qdict);
311}
312
Michael Roth125b3102012-01-19 00:18:20 -0600313/* false return signals GAChannel to close the current client connection */
314static gboolean channel_event_cb(GIOCondition condition, gpointer data)
Michael Roth48ff7a62011-07-20 15:19:37 -0500315{
316 GAState *s = data;
Michael Roth125b3102012-01-19 00:18:20 -0600317 gchar buf[QGA_READ_COUNT_DEFAULT+1];
Michael Roth48ff7a62011-07-20 15:19:37 -0500318 gsize count;
319 GError *err = NULL;
Michael Roth125b3102012-01-19 00:18:20 -0600320 GIOStatus status = ga_channel_read(s->channel, buf, QGA_READ_COUNT_DEFAULT, &count);
Michael Roth48ff7a62011-07-20 15:19:37 -0500321 if (err != NULL) {
322 g_warning("error reading channel: %s", err->message);
Michael Roth48ff7a62011-07-20 15:19:37 -0500323 g_error_free(err);
324 return false;
325 }
326 switch (status) {
327 case G_IO_STATUS_ERROR:
Michael Roth125b3102012-01-19 00:18:20 -0600328 g_warning("error reading channel");
Michael Roth48ff7a62011-07-20 15:19:37 -0500329 return false;
330 case G_IO_STATUS_NORMAL:
Michael Roth125b3102012-01-19 00:18:20 -0600331 buf[count] = 0;
Michael Roth48ff7a62011-07-20 15:19:37 -0500332 g_debug("read data, count: %d, data: %s", (int)count, buf);
333 json_message_parser_feed(&s->parser, (char *)buf, (int)count);
Michael Roth125b3102012-01-19 00:18:20 -0600334 break;
335 case G_IO_STATUS_EOF:
336 g_debug("received EOF");
337 if (!s->virtio) {
338 return false;
339 }
Michael Roth48ff7a62011-07-20 15:19:37 -0500340 case G_IO_STATUS_AGAIN:
341 /* virtio causes us to spin here when no process is attached to
342 * host-side chardev. sleep a bit to mitigate this
343 */
344 if (s->virtio) {
345 usleep(100*1000);
346 }
347 return true;
Michael Roth48ff7a62011-07-20 15:19:37 -0500348 default:
349 g_warning("unknown channel read status, closing");
Michael Roth48ff7a62011-07-20 15:19:37 -0500350 return false;
351 }
352 return true;
353}
354
Michael Roth125b3102012-01-19 00:18:20 -0600355static gboolean channel_init(GAState *s, const gchar *method, const gchar *path)
Michael Roth48ff7a62011-07-20 15:19:37 -0500356{
Michael Roth125b3102012-01-19 00:18:20 -0600357 GAChannelMethod channel_method;
Michael Roth48ff7a62011-07-20 15:19:37 -0500358
Michael Roth125b3102012-01-19 00:18:20 -0600359 if (method == NULL) {
360 method = "virtio-serial";
Michael Roth48ff7a62011-07-20 15:19:37 -0500361 }
362
Michael Roth125b3102012-01-19 00:18:20 -0600363 if (path == NULL) {
364 if (strcmp(method, "virtio-serial") != 0) {
Michael Roth48ff7a62011-07-20 15:19:37 -0500365 g_critical("must specify a path for this channel");
Michael Roth125b3102012-01-19 00:18:20 -0600366 return false;
Michael Roth48ff7a62011-07-20 15:19:37 -0500367 }
368 /* try the default path for the virtio-serial port */
Michael Roth125b3102012-01-19 00:18:20 -0600369 path = QGA_VIRTIO_PATH_DEFAULT;
Michael Roth48ff7a62011-07-20 15:19:37 -0500370 }
371
Michael Roth125b3102012-01-19 00:18:20 -0600372 if (strcmp(method, "virtio-serial") == 0) {
373 s->virtio = true; /* virtio requires special handling in some cases */
374 channel_method = GA_CHANNEL_VIRTIO_SERIAL;
375 } else if (strcmp(method, "isa-serial") == 0) {
376 channel_method = GA_CHANNEL_ISA_SERIAL;
377 } else if (strcmp(method, "unix-listen") == 0) {
378 channel_method = GA_CHANNEL_UNIX_LISTEN;
Michael Roth48ff7a62011-07-20 15:19:37 -0500379 } else {
Michael Roth125b3102012-01-19 00:18:20 -0600380 g_critical("unsupported channel method/type: %s", method);
381 return false;
Michael Roth48ff7a62011-07-20 15:19:37 -0500382 }
383
Michael Roth125b3102012-01-19 00:18:20 -0600384 s->channel = ga_channel_new(channel_method, path, channel_event_cb, s);
385 if (!s->channel) {
386 g_critical("failed to create guest agent channel");
387 return false;
388 }
389
390 return true;
Michael Roth48ff7a62011-07-20 15:19:37 -0500391}
392
393int main(int argc, char **argv)
394{
Michael Rothabd6cf62011-12-06 22:03:42 -0600395 const char *sopt = "hVvdm:p:l:f:b:";
Michael Roth48ff7a62011-07-20 15:19:37 -0500396 const char *method = NULL, *path = NULL, *pidfile = QGA_PIDFILE_DEFAULT;
397 const struct option lopt[] = {
398 { "help", 0, NULL, 'h' },
399 { "version", 0, NULL, 'V' },
400 { "logfile", 0, NULL, 'l' },
401 { "pidfile", 0, NULL, 'f' },
402 { "verbose", 0, NULL, 'v' },
403 { "method", 0, NULL, 'm' },
404 { "path", 0, NULL, 'p' },
405 { "daemonize", 0, NULL, 'd' },
Michael Rothabd6cf62011-12-06 22:03:42 -0600406 { "blacklist", 0, NULL, 'b' },
Michael Roth48ff7a62011-07-20 15:19:37 -0500407 { NULL, 0, NULL, 0 }
408 };
Michael Rothabd6cf62011-12-06 22:03:42 -0600409 int opt_ind = 0, ch, daemonize = 0, i, j, len;
Michael Roth48ff7a62011-07-20 15:19:37 -0500410 GLogLevelFlags log_level = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL;
411 FILE *log_file = stderr;
412 GAState *s;
413
Michael Rothabd6cf62011-12-06 22:03:42 -0600414 module_call_init(MODULE_INIT_QAPI);
415
Michael Roth48ff7a62011-07-20 15:19:37 -0500416 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
417 switch (ch) {
418 case 'm':
419 method = optarg;
420 break;
421 case 'p':
422 path = optarg;
423 break;
424 case 'l':
425 log_file = fopen(optarg, "a");
426 if (!log_file) {
427 g_critical("unable to open specified log file: %s",
428 strerror(errno));
429 return EXIT_FAILURE;
430 }
431 break;
432 case 'f':
433 pidfile = optarg;
434 break;
435 case 'v':
436 /* enable all log levels */
437 log_level = G_LOG_LEVEL_MASK;
438 break;
439 case 'V':
440 printf("QEMU Guest Agent %s\n", QGA_VERSION);
441 return 0;
442 case 'd':
443 daemonize = 1;
444 break;
Michael Rothabd6cf62011-12-06 22:03:42 -0600445 case 'b': {
446 char **list_head, **list;
447 if (*optarg == '?') {
448 list_head = list = qmp_get_command_list();
449 while (*list != NULL) {
450 printf("%s\n", *list);
451 g_free(*list);
452 list++;
453 }
454 g_free(list_head);
455 return 0;
456 }
457 for (j = 0, i = 0, len = strlen(optarg); i < len; i++) {
458 if (optarg[i] == ',') {
459 optarg[i] = 0;
460 qmp_disable_command(&optarg[j]);
461 g_debug("disabling command: %s", &optarg[j]);
462 j = i + 1;
463 }
464 }
465 if (j < i) {
466 qmp_disable_command(&optarg[j]);
467 g_debug("disabling command: %s", &optarg[j]);
468 }
469 break;
470 }
Michael Roth48ff7a62011-07-20 15:19:37 -0500471 case 'h':
472 usage(argv[0]);
473 return 0;
474 case '?':
475 g_print("Unknown option, try '%s --help' for more information.\n",
476 argv[0]);
477 return EXIT_FAILURE;
478 }
479 }
480
Michael Rothd8ca6852012-01-19 22:04:34 -0600481#ifndef _WIN32
Michael Roth48ff7a62011-07-20 15:19:37 -0500482 if (daemonize) {
483 g_debug("starting daemon");
484 become_daemon(pidfile);
485 }
Michael Rothd8ca6852012-01-19 22:04:34 -0600486#endif
Michael Roth48ff7a62011-07-20 15:19:37 -0500487
Anthony Liguori7267c092011-08-20 22:09:37 -0500488 s = g_malloc0(sizeof(GAState));
Michael Roth48ff7a62011-07-20 15:19:37 -0500489 s->log_file = log_file;
490 s->log_level = log_level;
491 g_log_set_default_handler(ga_log, s);
492 g_log_set_fatal_mask(NULL, G_LOG_LEVEL_ERROR);
493 s->logging_enabled = true;
Michael Rothe3d4d252011-07-19 15:41:55 -0500494 s->command_state = ga_command_state_new();
495 ga_command_state_init(s, s->command_state);
496 ga_command_state_init_all(s->command_state);
Michael Roth125b3102012-01-19 00:18:20 -0600497 json_message_parser_init(&s->parser, process_event);
Michael Roth48ff7a62011-07-20 15:19:37 -0500498 ga_state = s;
Michael Rothd8ca6852012-01-19 22:04:34 -0600499#ifndef _WIN32
Michael Roth125b3102012-01-19 00:18:20 -0600500 if (!register_signal_handlers()) {
501 g_critical("failed to register signal handlers");
502 goto out_bad;
503 }
Michael Rothd8ca6852012-01-19 22:04:34 -0600504#endif
Michael Roth48ff7a62011-07-20 15:19:37 -0500505
Michael Roth125b3102012-01-19 00:18:20 -0600506 s->main_loop = g_main_loop_new(NULL, false);
507 if (!channel_init(ga_state, method, path)) {
508 g_critical("failed to initialize guest agent channel");
509 goto out_bad;
510 }
Michael Roth48ff7a62011-07-20 15:19:37 -0500511 g_main_loop_run(ga_state->main_loop);
512
Michael Rothe3d4d252011-07-19 15:41:55 -0500513 ga_command_state_cleanup_all(ga_state->command_state);
Michael Roth125b3102012-01-19 00:18:20 -0600514 ga_channel_free(ga_state->channel);
Michael Roth48ff7a62011-07-20 15:19:37 -0500515
Michael Roth125b3102012-01-19 00:18:20 -0600516 if (daemonize) {
517 unlink(pidfile);
518 }
Michael Roth48ff7a62011-07-20 15:19:37 -0500519 return 0;
Michael Roth125b3102012-01-19 00:18:20 -0600520
521out_bad:
522 if (daemonize) {
523 unlink(pidfile);
524 }
525 return EXIT_FAILURE;
Michael Roth48ff7a62011-07-20 15:19:37 -0500526}