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.
21 #include "json-streamer.h"
22 #include "json-parser.h"
25 #include "qga/guest-agent-core.h"
29 #include "error_int.h"
30 #include "qapi/qmp-core.h"
31 #include "qga/channel.h"
34 #define QGA_VIRTIO_PATH_DEFAULT "/dev/virtio-ports/org.qemu.guest_agent.0"
36 #define QGA_VIRTIO_PATH_DEFAULT "\\\\.\\Global\\org.qemu.guest_agent.0"
38 #define QGA_PIDFILE_DEFAULT "/var/run/qemu-ga.pid"
41 JSONMessageParser parser
;
44 bool virtio
; /* fastpath to check for virtio to deal with poll() quirks */
45 GACommandState
*command_state
;
46 GLogLevelFlags log_level
;
51 static struct GAState
*ga_state
;
54 static void quit_handler(int sig
)
56 g_debug("received signal num %d, quitting", sig
);
58 if (g_main_loop_is_running(ga_state
->main_loop
)) {
59 g_main_loop_quit(ga_state
->main_loop
);
63 static gboolean
register_signal_handlers(void)
65 struct sigaction sigact
;
68 memset(&sigact
, 0, sizeof(struct sigaction
));
69 sigact
.sa_handler
= quit_handler
;
71 ret
= sigaction(SIGINT
, &sigact
, NULL
);
73 g_error("error configuring signal handler: %s", strerror(errno
));
76 ret
= sigaction(SIGTERM
, &sigact
, NULL
);
78 g_error("error configuring signal handler: %s", strerror(errno
));
85 static void usage(const char *cmd
)
88 "Usage: %s -c <channel_opts>\n"
89 "QEMU Guest Agent %s\n"
91 " -m, --method transport method: one of unix-listen, virtio-serial, or\n"
92 " isa-serial (virtio-serial is the default)\n"
93 " -p, --path device/socket path (%s is the default for virtio-serial)\n"
94 " -l, --logfile set logfile path, logs to stderr by default\n"
95 " -f, --pidfile specify pidfile (default is %s)\n"
96 " -v, --verbose log extra debugging information\n"
97 " -V, --version print version information and exit\n"
99 " -d, --daemonize become a daemon\n"
101 " -b, --blacklist comma-separated list of RPCs to disable (no spaces, \"?\""
102 " to list available RPCs)\n"
103 " -h, --help display this help and exit\n"
105 "Report bugs to <mdroth@linux.vnet.ibm.com>\n"
106 , cmd
, QGA_VERSION
, QGA_VIRTIO_PATH_DEFAULT
, QGA_PIDFILE_DEFAULT
);
109 static const char *ga_log_level_str(GLogLevelFlags level
)
111 switch (level
& G_LOG_LEVEL_MASK
) {
112 case G_LOG_LEVEL_ERROR
:
114 case G_LOG_LEVEL_CRITICAL
:
116 case G_LOG_LEVEL_WARNING
:
118 case G_LOG_LEVEL_MESSAGE
:
120 case G_LOG_LEVEL_INFO
:
122 case G_LOG_LEVEL_DEBUG
:
129 bool ga_logging_enabled(GAState
*s
)
131 return s
->logging_enabled
;
134 void ga_disable_logging(GAState
*s
)
136 s
->logging_enabled
= false;
139 void ga_enable_logging(GAState
*s
)
141 s
->logging_enabled
= true;
144 static void ga_log(const gchar
*domain
, GLogLevelFlags level
,
145 const gchar
*msg
, gpointer opaque
)
149 const char *level_str
= ga_log_level_str(level
);
151 if (!ga_logging_enabled(s
)) {
155 level
&= G_LOG_LEVEL_MASK
;
157 if (domain
&& strcmp(domain
, "syslog") == 0) {
158 syslog(LOG_INFO
, "%s: %s", level_str
, msg
);
159 } else if (level
& s
->log_level
) {
161 if (level
& s
->log_level
) {
163 g_get_current_time(&time
);
165 "%lu.%lu: %s: %s\n", time
.tv_sec
, time
.tv_usec
, level_str
, msg
);
171 static void become_daemon(const char *pidfile
)
185 pidfd
= open(pidfile
, O_CREAT
|O_WRONLY
|O_EXCL
, S_IRUSR
|S_IWUSR
);
187 g_critical("Cannot create pid file, %s", strerror(errno
));
191 if (asprintf(&pidstr
, "%d", getpid()) == -1) {
192 g_critical("Cannot allocate memory");
195 if (write(pidfd
, pidstr
, strlen(pidstr
)) != strlen(pidstr
)) {
197 g_critical("Failed to write pid file");
206 if ((chdir("/")) < 0) {
211 close(STDOUT_FILENO
);
212 close(STDERR_FILENO
);
218 g_critical("failed to daemonize");
223 static int send_response(GAState
*s
, QObject
*payload
)
226 QString
*payload_qstr
;
229 g_assert(payload
&& s
->channel
);
231 payload_qstr
= qobject_to_json(payload
);
236 qstring_append_chr(payload_qstr
, '\n');
237 buf
= qstring_get_str(payload_qstr
);
238 status
= ga_channel_write_all(s
->channel
, buf
, strlen(buf
));
239 QDECREF(payload_qstr
);
240 if (status
!= G_IO_STATUS_NORMAL
) {
247 static void process_command(GAState
*s
, QDict
*req
)
253 g_debug("processing command");
254 rsp
= qmp_dispatch(QOBJECT(req
));
256 ret
= send_response(s
, rsp
);
258 g_warning("error sending response: %s", strerror(ret
));
262 g_warning("error getting response");
266 /* handle requests/control events coming in over the channel */
267 static void process_event(JSONMessageParser
*parser
, QList
*tokens
)
269 GAState
*s
= container_of(parser
, GAState
, parser
);
275 g_assert(s
&& parser
);
277 g_debug("process_event: called");
278 obj
= json_parser_parse_err(tokens
, NULL
, &err
);
279 if (err
|| !obj
|| qobject_type(obj
) != QTYPE_QDICT
) {
283 g_warning("failed to parse event: unknown error");
284 error_set(&err
, QERR_JSON_PARSING
);
286 g_warning("failed to parse event: %s", error_get_pretty(err
));
288 qdict_put_obj(qdict
, "error", error_get_qobject(err
));
291 qdict
= qobject_to_qdict(obj
);
296 /* handle host->guest commands */
297 if (qdict_haskey(qdict
, "execute")) {
298 process_command(s
, qdict
);
300 if (!qdict_haskey(qdict
, "error")) {
303 g_warning("unrecognized payload format");
304 error_set(&err
, QERR_UNSUPPORTED
);
305 qdict_put_obj(qdict
, "error", error_get_qobject(err
));
308 ret
= send_response(s
, QOBJECT(qdict
));
310 g_warning("error sending error response: %s", strerror(ret
));
317 /* false return signals GAChannel to close the current client connection */
318 static gboolean
channel_event_cb(GIOCondition condition
, gpointer data
)
321 gchar buf
[QGA_READ_COUNT_DEFAULT
+1];
324 GIOStatus status
= ga_channel_read(s
->channel
, buf
, QGA_READ_COUNT_DEFAULT
, &count
);
326 g_warning("error reading channel: %s", err
->message
);
331 case G_IO_STATUS_ERROR
:
332 g_warning("error reading channel");
334 case G_IO_STATUS_NORMAL
:
336 g_debug("read data, count: %d, data: %s", (int)count
, buf
);
337 json_message_parser_feed(&s
->parser
, (char *)buf
, (int)count
);
339 case G_IO_STATUS_EOF
:
340 g_debug("received EOF");
344 case G_IO_STATUS_AGAIN
:
345 /* virtio causes us to spin here when no process is attached to
346 * host-side chardev. sleep a bit to mitigate this
353 g_warning("unknown channel read status, closing");
359 static gboolean
channel_init(GAState
*s
, const gchar
*method
, const gchar
*path
)
361 GAChannelMethod channel_method
;
363 if (method
== NULL
) {
364 method
= "virtio-serial";
368 if (strcmp(method
, "virtio-serial") != 0) {
369 g_critical("must specify a path for this channel");
372 /* try the default path for the virtio-serial port */
373 path
= QGA_VIRTIO_PATH_DEFAULT
;
376 if (strcmp(method
, "virtio-serial") == 0) {
377 s
->virtio
= true; /* virtio requires special handling in some cases */
378 channel_method
= GA_CHANNEL_VIRTIO_SERIAL
;
379 } else if (strcmp(method
, "isa-serial") == 0) {
380 channel_method
= GA_CHANNEL_ISA_SERIAL
;
381 } else if (strcmp(method
, "unix-listen") == 0) {
382 channel_method
= GA_CHANNEL_UNIX_LISTEN
;
384 g_critical("unsupported channel method/type: %s", method
);
388 s
->channel
= ga_channel_new(channel_method
, path
, channel_event_cb
, s
);
390 g_critical("failed to create guest agent channel");
397 int main(int argc
, char **argv
)
399 const char *sopt
= "hVvdm:p:l:f:b:";
400 const char *method
= NULL
, *path
= NULL
, *pidfile
= QGA_PIDFILE_DEFAULT
;
401 const struct option lopt
[] = {
402 { "help", 0, NULL
, 'h' },
403 { "version", 0, NULL
, 'V' },
404 { "logfile", 0, NULL
, 'l' },
405 { "pidfile", 0, NULL
, 'f' },
406 { "verbose", 0, NULL
, 'v' },
407 { "method", 0, NULL
, 'm' },
408 { "path", 0, NULL
, 'p' },
409 { "daemonize", 0, NULL
, 'd' },
410 { "blacklist", 0, NULL
, 'b' },
413 int opt_ind
= 0, ch
, daemonize
= 0, i
, j
, len
;
414 GLogLevelFlags log_level
= G_LOG_LEVEL_ERROR
| G_LOG_LEVEL_CRITICAL
;
415 FILE *log_file
= stderr
;
418 module_call_init(MODULE_INIT_QAPI
);
420 while ((ch
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_ind
)) != -1) {
429 log_file
= fopen(optarg
, "a");
431 g_critical("unable to open specified log file: %s",
440 /* enable all log levels */
441 log_level
= G_LOG_LEVEL_MASK
;
444 printf("QEMU Guest Agent %s\n", QGA_VERSION
);
450 char **list_head
, **list
;
451 if (*optarg
== '?') {
452 list_head
= list
= qmp_get_command_list();
453 while (*list
!= NULL
) {
454 printf("%s\n", *list
);
461 for (j
= 0, i
= 0, len
= strlen(optarg
); i
< len
; i
++) {
462 if (optarg
[i
] == ',') {
464 qmp_disable_command(&optarg
[j
]);
465 g_debug("disabling command: %s", &optarg
[j
]);
470 qmp_disable_command(&optarg
[j
]);
471 g_debug("disabling command: %s", &optarg
[j
]);
479 g_print("Unknown option, try '%s --help' for more information.\n",
487 g_debug("starting daemon");
488 become_daemon(pidfile
);
492 s
= g_malloc0(sizeof(GAState
));
493 s
->log_file
= log_file
;
494 s
->log_level
= log_level
;
495 g_log_set_default_handler(ga_log
, s
);
496 g_log_set_fatal_mask(NULL
, G_LOG_LEVEL_ERROR
);
497 s
->logging_enabled
= true;
498 s
->command_state
= ga_command_state_new();
499 ga_command_state_init(s
, s
->command_state
);
500 ga_command_state_init_all(s
->command_state
);
501 json_message_parser_init(&s
->parser
, process_event
);
504 if (!register_signal_handlers()) {
505 g_critical("failed to register signal handlers");
510 s
->main_loop
= g_main_loop_new(NULL
, false);
511 if (!channel_init(ga_state
, method
, path
)) {
512 g_critical("failed to initialize guest agent channel");
515 g_main_loop_run(ga_state
->main_loop
);
517 ga_command_state_cleanup_all(ga_state
->command_state
);
518 ga_channel_free(ga_state
->channel
);