4 * Copyright IBM Corp. 2011
7 * Adam Litke <aglitke@linux.vnet.ibm.com>
8 * Michael Roth <mdroth@linux.vnet.ibm.com>
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.
19 #include "json-streamer.h"
20 #include "json-parser.h"
23 #include "qga/guest-agent-core.h"
27 #include "error_int.h"
28 #include "qapi/qmp-core.h"
29 #include "qga/channel.h"
31 #define QGA_VIRTIO_PATH_DEFAULT "/dev/virtio-ports/org.qemu.guest_agent.0"
32 #define QGA_PIDFILE_DEFAULT "/var/run/qemu-ga.pid"
35 JSONMessageParser parser
;
38 bool virtio
; /* fastpath to check for virtio to deal with poll() quirks */
39 GACommandState
*command_state
;
40 GLogLevelFlags log_level
;
45 static struct GAState
*ga_state
;
47 static void quit_handler(int sig
)
49 g_debug("received signal num %d, quitting", sig
);
51 if (g_main_loop_is_running(ga_state
->main_loop
)) {
52 g_main_loop_quit(ga_state
->main_loop
);
56 static gboolean
register_signal_handlers(void)
58 struct sigaction sigact
;
61 memset(&sigact
, 0, sizeof(struct sigaction
));
62 sigact
.sa_handler
= quit_handler
;
64 ret
= sigaction(SIGINT
, &sigact
, NULL
);
66 g_error("error configuring signal handler: %s", strerror(errno
));
69 ret
= sigaction(SIGTERM
, &sigact
, NULL
);
71 g_error("error configuring signal handler: %s", strerror(errno
));
77 static void usage(const char *cmd
)
80 "Usage: %s -c <channel_opts>\n"
81 "QEMU Guest Agent %s\n"
83 " -m, --method transport method: one of unix-listen, virtio-serial, or\n"
84 " isa-serial (virtio-serial is the default)\n"
85 " -p, --path device/socket path (%s is the default for virtio-serial)\n"
86 " -l, --logfile set logfile path, logs to stderr by default\n"
87 " -f, --pidfile specify pidfile (default is %s)\n"
88 " -v, --verbose log extra debugging information\n"
89 " -V, --version print version information and exit\n"
90 " -d, --daemonize become a daemon\n"
91 " -b, --blacklist comma-separated list of RPCs to disable (no spaces, \"?\""
92 " to list available RPCs)\n"
93 " -h, --help display this help and exit\n"
95 "Report bugs to <mdroth@linux.vnet.ibm.com>\n"
96 , cmd
, QGA_VERSION
, QGA_VIRTIO_PATH_DEFAULT
, QGA_PIDFILE_DEFAULT
);
99 static const char *ga_log_level_str(GLogLevelFlags level
)
101 switch (level
& G_LOG_LEVEL_MASK
) {
102 case G_LOG_LEVEL_ERROR
:
104 case G_LOG_LEVEL_CRITICAL
:
106 case G_LOG_LEVEL_WARNING
:
108 case G_LOG_LEVEL_MESSAGE
:
110 case G_LOG_LEVEL_INFO
:
112 case G_LOG_LEVEL_DEBUG
:
119 bool ga_logging_enabled(GAState
*s
)
121 return s
->logging_enabled
;
124 void ga_disable_logging(GAState
*s
)
126 s
->logging_enabled
= false;
129 void ga_enable_logging(GAState
*s
)
131 s
->logging_enabled
= true;
134 static void ga_log(const gchar
*domain
, GLogLevelFlags level
,
135 const gchar
*msg
, gpointer opaque
)
139 const char *level_str
= ga_log_level_str(level
);
141 if (!ga_logging_enabled(s
)) {
145 level
&= G_LOG_LEVEL_MASK
;
146 if (domain
&& strcmp(domain
, "syslog") == 0) {
147 syslog(LOG_INFO
, "%s: %s", level_str
, msg
);
148 } else if (level
& s
->log_level
) {
149 g_get_current_time(&time
);
151 "%lu.%lu: %s: %s\n", time
.tv_sec
, time
.tv_usec
, level_str
, msg
);
156 static void become_daemon(const char *pidfile
)
170 pidfd
= open(pidfile
, O_CREAT
|O_WRONLY
|O_EXCL
, S_IRUSR
|S_IWUSR
);
172 g_critical("Cannot create pid file, %s", strerror(errno
));
176 if (asprintf(&pidstr
, "%d", getpid()) == -1) {
177 g_critical("Cannot allocate memory");
180 if (write(pidfd
, pidstr
, strlen(pidstr
)) != strlen(pidstr
)) {
182 g_critical("Failed to write pid file");
191 if ((chdir("/")) < 0) {
196 close(STDOUT_FILENO
);
197 close(STDERR_FILENO
);
203 g_critical("failed to daemonize");
207 static int send_response(GAState
*s
, QObject
*payload
)
210 QString
*payload_qstr
;
213 g_assert(payload
&& s
->channel
);
215 payload_qstr
= qobject_to_json(payload
);
220 qstring_append_chr(payload_qstr
, '\n');
221 buf
= qstring_get_str(payload_qstr
);
222 status
= ga_channel_write_all(s
->channel
, buf
, strlen(buf
));
223 QDECREF(payload_qstr
);
224 if (status
!= G_IO_STATUS_NORMAL
) {
231 static void process_command(GAState
*s
, QDict
*req
)
237 g_debug("processing command");
238 rsp
= qmp_dispatch(QOBJECT(req
));
240 ret
= send_response(s
, rsp
);
242 g_warning("error sending response: %s", strerror(ret
));
246 g_warning("error getting response");
250 /* handle requests/control events coming in over the channel */
251 static void process_event(JSONMessageParser
*parser
, QList
*tokens
)
253 GAState
*s
= container_of(parser
, GAState
, parser
);
259 g_assert(s
&& parser
);
261 g_debug("process_event: called");
262 obj
= json_parser_parse_err(tokens
, NULL
, &err
);
263 if (err
|| !obj
|| qobject_type(obj
) != QTYPE_QDICT
) {
267 g_warning("failed to parse event: unknown error");
268 error_set(&err
, QERR_JSON_PARSING
);
270 g_warning("failed to parse event: %s", error_get_pretty(err
));
272 qdict_put_obj(qdict
, "error", error_get_qobject(err
));
275 qdict
= qobject_to_qdict(obj
);
280 /* handle host->guest commands */
281 if (qdict_haskey(qdict
, "execute")) {
282 process_command(s
, qdict
);
284 if (!qdict_haskey(qdict
, "error")) {
287 g_warning("unrecognized payload format");
288 error_set(&err
, QERR_UNSUPPORTED
);
289 qdict_put_obj(qdict
, "error", error_get_qobject(err
));
292 ret
= send_response(s
, QOBJECT(qdict
));
294 g_warning("error sending error response: %s", strerror(ret
));
301 /* false return signals GAChannel to close the current client connection */
302 static gboolean
channel_event_cb(GIOCondition condition
, gpointer data
)
305 gchar buf
[QGA_READ_COUNT_DEFAULT
+1];
308 GIOStatus status
= ga_channel_read(s
->channel
, buf
, QGA_READ_COUNT_DEFAULT
, &count
);
310 g_warning("error reading channel: %s", err
->message
);
315 case G_IO_STATUS_ERROR
:
316 g_warning("error reading channel");
318 case G_IO_STATUS_NORMAL
:
320 g_debug("read data, count: %d, data: %s", (int)count
, buf
);
321 json_message_parser_feed(&s
->parser
, (char *)buf
, (int)count
);
323 case G_IO_STATUS_EOF
:
324 g_debug("received EOF");
328 case G_IO_STATUS_AGAIN
:
329 /* virtio causes us to spin here when no process is attached to
330 * host-side chardev. sleep a bit to mitigate this
337 g_warning("unknown channel read status, closing");
343 static gboolean
channel_init(GAState
*s
, const gchar
*method
, const gchar
*path
)
345 GAChannelMethod channel_method
;
347 if (method
== NULL
) {
348 method
= "virtio-serial";
352 if (strcmp(method
, "virtio-serial") != 0) {
353 g_critical("must specify a path for this channel");
356 /* try the default path for the virtio-serial port */
357 path
= QGA_VIRTIO_PATH_DEFAULT
;
360 if (strcmp(method
, "virtio-serial") == 0) {
361 s
->virtio
= true; /* virtio requires special handling in some cases */
362 channel_method
= GA_CHANNEL_VIRTIO_SERIAL
;
363 } else if (strcmp(method
, "isa-serial") == 0) {
364 channel_method
= GA_CHANNEL_ISA_SERIAL
;
365 } else if (strcmp(method
, "unix-listen") == 0) {
366 channel_method
= GA_CHANNEL_UNIX_LISTEN
;
368 g_critical("unsupported channel method/type: %s", method
);
372 s
->channel
= ga_channel_new(channel_method
, path
, channel_event_cb
, s
);
374 g_critical("failed to create guest agent channel");
381 int main(int argc
, char **argv
)
383 const char *sopt
= "hVvdm:p:l:f:b:";
384 const char *method
= NULL
, *path
= NULL
, *pidfile
= QGA_PIDFILE_DEFAULT
;
385 const struct option lopt
[] = {
386 { "help", 0, NULL
, 'h' },
387 { "version", 0, NULL
, 'V' },
388 { "logfile", 0, NULL
, 'l' },
389 { "pidfile", 0, NULL
, 'f' },
390 { "verbose", 0, NULL
, 'v' },
391 { "method", 0, NULL
, 'm' },
392 { "path", 0, NULL
, 'p' },
393 { "daemonize", 0, NULL
, 'd' },
394 { "blacklist", 0, NULL
, 'b' },
397 int opt_ind
= 0, ch
, daemonize
= 0, i
, j
, len
;
398 GLogLevelFlags log_level
= G_LOG_LEVEL_ERROR
| G_LOG_LEVEL_CRITICAL
;
399 FILE *log_file
= stderr
;
402 module_call_init(MODULE_INIT_QAPI
);
404 while ((ch
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_ind
)) != -1) {
413 log_file
= fopen(optarg
, "a");
415 g_critical("unable to open specified log file: %s",
424 /* enable all log levels */
425 log_level
= G_LOG_LEVEL_MASK
;
428 printf("QEMU Guest Agent %s\n", QGA_VERSION
);
434 char **list_head
, **list
;
435 if (*optarg
== '?') {
436 list_head
= list
= qmp_get_command_list();
437 while (*list
!= NULL
) {
438 printf("%s\n", *list
);
445 for (j
= 0, i
= 0, len
= strlen(optarg
); i
< len
; i
++) {
446 if (optarg
[i
] == ',') {
448 qmp_disable_command(&optarg
[j
]);
449 g_debug("disabling command: %s", &optarg
[j
]);
454 qmp_disable_command(&optarg
[j
]);
455 g_debug("disabling command: %s", &optarg
[j
]);
463 g_print("Unknown option, try '%s --help' for more information.\n",
470 g_debug("starting daemon");
471 become_daemon(pidfile
);
474 s
= g_malloc0(sizeof(GAState
));
475 s
->log_file
= log_file
;
476 s
->log_level
= log_level
;
477 g_log_set_default_handler(ga_log
, s
);
478 g_log_set_fatal_mask(NULL
, G_LOG_LEVEL_ERROR
);
479 s
->logging_enabled
= true;
480 s
->command_state
= ga_command_state_new();
481 ga_command_state_init(s
, s
->command_state
);
482 ga_command_state_init_all(s
->command_state
);
483 json_message_parser_init(&s
->parser
, process_event
);
485 if (!register_signal_handlers()) {
486 g_critical("failed to register signal handlers");
490 s
->main_loop
= g_main_loop_new(NULL
, false);
491 if (!channel_init(ga_state
, method
, path
)) {
492 g_critical("failed to initialize guest agent channel");
495 g_main_loop_run(ga_state
->main_loop
);
497 ga_command_state_cleanup_all(ga_state
->command_state
);
498 ga_channel_free(ga_state
->channel
);