Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 1 | /* |
| 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> |
| 17 | #include <gio/gio.h> |
| 18 | #include <getopt.h> |
| 19 | #include <termios.h> |
| 20 | #include <syslog.h> |
| 21 | #include "qemu_socket.h" |
| 22 | #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" |
| 31 | |
| 32 | #define QGA_VIRTIO_PATH_DEFAULT "/dev/virtio-ports/org.qemu.guest_agent.0" |
| 33 | #define QGA_PIDFILE_DEFAULT "/var/run/qemu-ga.pid" |
| 34 | #define QGA_BAUDRATE_DEFAULT B38400 /* for isa-serial channels */ |
| 35 | #define QGA_TIMEOUT_DEFAULT 30*1000 /* ms */ |
| 36 | |
| 37 | struct GAState { |
| 38 | JSONMessageParser parser; |
| 39 | GMainLoop *main_loop; |
| 40 | GSocket *conn_sock; |
| 41 | GIOChannel *conn_channel; |
| 42 | GSocket *listen_sock; |
| 43 | GIOChannel *listen_channel; |
| 44 | const char *path; |
| 45 | const char *method; |
| 46 | bool virtio; /* fastpath to check for virtio to deal with poll() quirks */ |
| 47 | GACommandState *command_state; |
| 48 | GLogLevelFlags log_level; |
| 49 | FILE *log_file; |
| 50 | bool logging_enabled; |
| 51 | }; |
| 52 | |
| 53 | static struct GAState *ga_state; |
| 54 | |
| 55 | static void quit_handler(int sig) |
| 56 | { |
| 57 | g_debug("recieved signal num %d, quitting", sig); |
| 58 | |
| 59 | if (g_main_loop_is_running(ga_state->main_loop)) { |
| 60 | g_main_loop_quit(ga_state->main_loop); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | static void register_signal_handlers(void) |
| 65 | { |
| 66 | struct sigaction sigact; |
| 67 | int ret; |
| 68 | |
| 69 | memset(&sigact, 0, sizeof(struct sigaction)); |
| 70 | sigact.sa_handler = quit_handler; |
| 71 | |
| 72 | ret = sigaction(SIGINT, &sigact, NULL); |
| 73 | if (ret == -1) { |
| 74 | g_error("error configuring signal handler: %s", strerror(errno)); |
| 75 | exit(EXIT_FAILURE); |
| 76 | } |
| 77 | ret = sigaction(SIGTERM, &sigact, NULL); |
| 78 | if (ret == -1) { |
| 79 | g_error("error configuring signal handler: %s", strerror(errno)); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | static void usage(const char *cmd) |
| 84 | { |
| 85 | printf( |
| 86 | "Usage: %s -c <channel_opts>\n" |
| 87 | "QEMU Guest Agent %s\n" |
| 88 | "\n" |
| 89 | " -m, --method transport method: one of unix-listen, virtio-serial, or\n" |
| 90 | " isa-serial (virtio-serial is the default)\n" |
| 91 | " -p, --path device/socket path (%s is the default for virtio-serial)\n" |
| 92 | " -l, --logfile set logfile path, logs to stderr by default\n" |
| 93 | " -f, --pidfile specify pidfile (default is %s)\n" |
| 94 | " -v, --verbose log extra debugging information\n" |
| 95 | " -V, --version print version information and exit\n" |
| 96 | " -d, --daemonize become a daemon\n" |
| 97 | " -h, --help display this help and exit\n" |
| 98 | "\n" |
| 99 | "Report bugs to <mdroth@linux.vnet.ibm.com>\n" |
| 100 | , cmd, QGA_VERSION, QGA_VIRTIO_PATH_DEFAULT, QGA_PIDFILE_DEFAULT); |
| 101 | } |
| 102 | |
| 103 | static void conn_channel_close(GAState *s); |
| 104 | |
| 105 | static 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 | |
| 125 | bool ga_logging_enabled(GAState *s) |
| 126 | { |
| 127 | return s->logging_enabled; |
| 128 | } |
| 129 | |
| 130 | void ga_disable_logging(GAState *s) |
| 131 | { |
| 132 | s->logging_enabled = false; |
| 133 | } |
| 134 | |
| 135 | void ga_enable_logging(GAState *s) |
| 136 | { |
| 137 | s->logging_enabled = true; |
| 138 | } |
| 139 | |
| 140 | static 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; |
| 152 | if (g_strcmp0(domain, "syslog") == 0) { |
| 153 | syslog(LOG_INFO, "%s: %s", level_str, msg); |
| 154 | } else if (level & s->log_level) { |
| 155 | g_get_current_time(&time); |
| 156 | fprintf(s->log_file, |
| 157 | "%lu.%lu: %s: %s\n", time.tv_sec, time.tv_usec, level_str, msg); |
| 158 | fflush(s->log_file); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | static void become_daemon(const char *pidfile) |
| 163 | { |
| 164 | pid_t pid, sid; |
| 165 | int pidfd; |
| 166 | char *pidstr = NULL; |
| 167 | |
| 168 | pid = fork(); |
| 169 | if (pid < 0) { |
| 170 | exit(EXIT_FAILURE); |
| 171 | } |
| 172 | if (pid > 0) { |
| 173 | exit(EXIT_SUCCESS); |
| 174 | } |
| 175 | |
| 176 | pidfd = open(pidfile, O_CREAT|O_WRONLY|O_EXCL, S_IRUSR|S_IWUSR); |
| 177 | if (pidfd == -1) { |
| 178 | g_critical("Cannot create pid file, %s", strerror(errno)); |
| 179 | exit(EXIT_FAILURE); |
| 180 | } |
| 181 | |
| 182 | if (asprintf(&pidstr, "%d", getpid()) == -1) { |
| 183 | g_critical("Cannot allocate memory"); |
| 184 | goto fail; |
| 185 | } |
| 186 | if (write(pidfd, pidstr, strlen(pidstr)) != strlen(pidstr)) { |
| 187 | free(pidstr); |
| 188 | g_critical("Failed to write pid file"); |
| 189 | goto fail; |
| 190 | } |
| 191 | |
| 192 | umask(0); |
| 193 | sid = setsid(); |
| 194 | if (sid < 0) { |
| 195 | goto fail; |
| 196 | } |
| 197 | if ((chdir("/")) < 0) { |
| 198 | goto fail; |
| 199 | } |
| 200 | |
| 201 | close(STDIN_FILENO); |
| 202 | close(STDOUT_FILENO); |
| 203 | close(STDERR_FILENO); |
| 204 | free(pidstr); |
| 205 | return; |
| 206 | |
| 207 | fail: |
| 208 | unlink(pidfile); |
| 209 | g_critical("failed to daemonize"); |
| 210 | exit(EXIT_FAILURE); |
| 211 | } |
| 212 | |
| 213 | static int conn_channel_send_buf(GIOChannel *channel, const char *buf, |
| 214 | gsize count) |
| 215 | { |
| 216 | GError *err = NULL; |
| 217 | gsize written = 0; |
| 218 | GIOStatus status; |
| 219 | |
| 220 | while (count) { |
| 221 | status = g_io_channel_write_chars(channel, buf, count, &written, &err); |
| 222 | g_debug("sending data, count: %d", (int)count); |
| 223 | if (err != NULL) { |
| 224 | g_warning("error sending newline: %s", err->message); |
| 225 | return err->code; |
| 226 | } |
| 227 | if (status == G_IO_STATUS_ERROR || status == G_IO_STATUS_EOF) { |
| 228 | return -EPIPE; |
| 229 | } |
| 230 | |
| 231 | if (status == G_IO_STATUS_NORMAL) { |
| 232 | count -= written; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | static int conn_channel_send_payload(GIOChannel *channel, QObject *payload) |
| 240 | { |
| 241 | int ret = 0; |
| 242 | const char *buf; |
| 243 | QString *payload_qstr; |
| 244 | GError *err = NULL; |
| 245 | |
| 246 | g_assert(payload && channel); |
| 247 | |
| 248 | payload_qstr = qobject_to_json(payload); |
| 249 | if (!payload_qstr) { |
| 250 | return -EINVAL; |
| 251 | } |
| 252 | |
| 253 | qstring_append_chr(payload_qstr, '\n'); |
| 254 | buf = qstring_get_str(payload_qstr); |
| 255 | ret = conn_channel_send_buf(channel, buf, strlen(buf)); |
| 256 | if (ret) { |
| 257 | goto out_free; |
| 258 | } |
| 259 | |
| 260 | g_io_channel_flush(channel, &err); |
| 261 | if (err != NULL) { |
| 262 | g_warning("error flushing payload: %s", err->message); |
| 263 | ret = err->code; |
| 264 | goto out_free; |
| 265 | } |
| 266 | |
| 267 | out_free: |
| 268 | QDECREF(payload_qstr); |
| 269 | if (err) { |
| 270 | g_error_free(err); |
| 271 | } |
| 272 | return ret; |
| 273 | } |
| 274 | |
| 275 | static void process_command(GAState *s, QDict *req) |
| 276 | { |
| 277 | QObject *rsp = NULL; |
| 278 | int ret; |
| 279 | |
| 280 | g_assert(req); |
| 281 | g_debug("processing command"); |
| 282 | rsp = qmp_dispatch(QOBJECT(req)); |
| 283 | if (rsp) { |
| 284 | ret = conn_channel_send_payload(s->conn_channel, rsp); |
| 285 | if (ret) { |
| 286 | g_warning("error sending payload: %s", strerror(ret)); |
| 287 | } |
| 288 | qobject_decref(rsp); |
| 289 | } else { |
| 290 | g_warning("error getting response"); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | /* handle requests/control events coming in over the channel */ |
| 295 | static void process_event(JSONMessageParser *parser, QList *tokens) |
| 296 | { |
| 297 | GAState *s = container_of(parser, GAState, parser); |
| 298 | QObject *obj; |
| 299 | QDict *qdict; |
| 300 | Error *err = NULL; |
| 301 | int ret; |
| 302 | |
| 303 | g_assert(s && parser); |
| 304 | |
| 305 | g_debug("process_event: called"); |
| 306 | obj = json_parser_parse_err(tokens, NULL, &err); |
| 307 | if (err || !obj || qobject_type(obj) != QTYPE_QDICT) { |
| 308 | qobject_decref(obj); |
| 309 | qdict = qdict_new(); |
| 310 | if (!err) { |
| 311 | g_warning("failed to parse event: unknown error"); |
| 312 | error_set(&err, QERR_JSON_PARSING); |
| 313 | } else { |
| 314 | g_warning("failed to parse event: %s", error_get_pretty(err)); |
| 315 | } |
| 316 | qdict_put_obj(qdict, "error", error_get_qobject(err)); |
| 317 | error_free(err); |
| 318 | } else { |
| 319 | qdict = qobject_to_qdict(obj); |
| 320 | } |
| 321 | |
| 322 | g_assert(qdict); |
| 323 | |
| 324 | /* handle host->guest commands */ |
| 325 | if (qdict_haskey(qdict, "execute")) { |
| 326 | process_command(s, qdict); |
| 327 | } else { |
| 328 | if (!qdict_haskey(qdict, "error")) { |
| 329 | QDECREF(qdict); |
| 330 | qdict = qdict_new(); |
| 331 | g_warning("unrecognized payload format"); |
| 332 | error_set(&err, QERR_UNSUPPORTED); |
| 333 | qdict_put_obj(qdict, "error", error_get_qobject(err)); |
| 334 | error_free(err); |
| 335 | } |
| 336 | ret = conn_channel_send_payload(s->conn_channel, QOBJECT(qdict)); |
| 337 | if (ret) { |
| 338 | g_warning("error sending payload: %s", strerror(ret)); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | QDECREF(qdict); |
| 343 | } |
| 344 | |
| 345 | static gboolean conn_channel_read(GIOChannel *channel, GIOCondition condition, |
| 346 | gpointer data) |
| 347 | { |
| 348 | GAState *s = data; |
| 349 | gchar buf[1024]; |
| 350 | gsize count; |
| 351 | GError *err = NULL; |
| 352 | memset(buf, 0, 1024); |
| 353 | GIOStatus status = g_io_channel_read_chars(channel, buf, 1024, |
| 354 | &count, &err); |
| 355 | if (err != NULL) { |
| 356 | g_warning("error reading channel: %s", err->message); |
| 357 | conn_channel_close(s); |
| 358 | g_error_free(err); |
| 359 | return false; |
| 360 | } |
| 361 | switch (status) { |
| 362 | case G_IO_STATUS_ERROR: |
| 363 | g_warning("problem"); |
| 364 | return false; |
| 365 | case G_IO_STATUS_NORMAL: |
| 366 | g_debug("read data, count: %d, data: %s", (int)count, buf); |
| 367 | json_message_parser_feed(&s->parser, (char *)buf, (int)count); |
| 368 | case G_IO_STATUS_AGAIN: |
| 369 | /* virtio causes us to spin here when no process is attached to |
| 370 | * host-side chardev. sleep a bit to mitigate this |
| 371 | */ |
| 372 | if (s->virtio) { |
| 373 | usleep(100*1000); |
| 374 | } |
| 375 | return true; |
| 376 | case G_IO_STATUS_EOF: |
| 377 | g_debug("received EOF"); |
| 378 | conn_channel_close(s); |
| 379 | if (s->virtio) { |
| 380 | return true; |
| 381 | } |
| 382 | return false; |
| 383 | default: |
| 384 | g_warning("unknown channel read status, closing"); |
| 385 | conn_channel_close(s); |
| 386 | return false; |
| 387 | } |
| 388 | return true; |
| 389 | } |
| 390 | |
| 391 | static int conn_channel_add(GAState *s, int fd) |
| 392 | { |
| 393 | GIOChannel *conn_channel; |
| 394 | GError *err = NULL; |
| 395 | |
| 396 | g_assert(s && !s->conn_channel); |
| 397 | conn_channel = g_io_channel_unix_new(fd); |
| 398 | g_assert(conn_channel); |
| 399 | g_io_channel_set_encoding(conn_channel, NULL, &err); |
| 400 | if (err != NULL) { |
| 401 | g_warning("error setting channel encoding to binary"); |
| 402 | g_error_free(err); |
| 403 | return -1; |
| 404 | } |
| 405 | g_io_add_watch(conn_channel, G_IO_IN | G_IO_HUP, |
| 406 | conn_channel_read, s); |
| 407 | s->conn_channel = conn_channel; |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | static gboolean listen_channel_accept(GIOChannel *channel, |
| 412 | GIOCondition condition, gpointer data) |
| 413 | { |
| 414 | GAState *s = data; |
| 415 | GError *err = NULL; |
| 416 | g_assert(channel != NULL); |
| 417 | int ret; |
| 418 | bool accepted = false; |
| 419 | |
| 420 | s->conn_sock = g_socket_accept(s->listen_sock, NULL, &err); |
| 421 | if (err != NULL) { |
| 422 | g_warning("error converting fd to gsocket: %s", err->message); |
| 423 | g_error_free(err); |
| 424 | goto out; |
| 425 | } |
| 426 | ret = conn_channel_add(s, g_socket_get_fd(s->conn_sock)); |
| 427 | if (ret) { |
| 428 | g_warning("error setting up connection"); |
| 429 | goto out; |
| 430 | } |
| 431 | accepted = true; |
| 432 | |
| 433 | out: |
| 434 | /* only accept 1 connection at a time */ |
| 435 | return !accepted; |
| 436 | } |
| 437 | |
| 438 | /* start polling for readable events on listen fd, new==true |
| 439 | * indicates we should use the existing s->listen_channel |
| 440 | */ |
| 441 | static int listen_channel_add(GAState *s, int listen_fd, bool new) |
| 442 | { |
| 443 | GError *err = NULL; |
| 444 | |
| 445 | if (new) { |
| 446 | s->listen_channel = g_io_channel_unix_new(listen_fd); |
| 447 | if (s->listen_sock) { |
| 448 | g_object_unref(s->listen_sock); |
| 449 | } |
| 450 | s->listen_sock = g_socket_new_from_fd(listen_fd, &err); |
| 451 | if (err != NULL) { |
| 452 | g_warning("error converting fd to gsocket: %s", err->message); |
| 453 | g_error_free(err); |
| 454 | return -1; |
| 455 | } |
| 456 | } |
| 457 | g_io_add_watch(s->listen_channel, G_IO_IN, |
| 458 | listen_channel_accept, s); |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | /* cleanup state for closed connection/session, start accepting new |
| 463 | * connections if we're in listening mode |
| 464 | */ |
| 465 | static void conn_channel_close(GAState *s) |
| 466 | { |
| 467 | if (strcmp(s->method, "unix-listen") == 0) { |
| 468 | g_io_channel_shutdown(s->conn_channel, true, NULL); |
| 469 | g_object_unref(s->conn_sock); |
| 470 | s->conn_sock = NULL; |
| 471 | listen_channel_add(s, 0, false); |
| 472 | } else if (strcmp(s->method, "virtio-serial") == 0) { |
| 473 | /* we spin on EOF for virtio-serial, so back off a bit. also, |
| 474 | * dont close the connection in this case, it'll resume normal |
| 475 | * operation when another process connects to host chardev |
| 476 | */ |
| 477 | usleep(100*1000); |
| 478 | goto out_noclose; |
| 479 | } |
| 480 | g_io_channel_unref(s->conn_channel); |
| 481 | s->conn_channel = NULL; |
| 482 | out_noclose: |
| 483 | return; |
| 484 | } |
| 485 | |
| 486 | static void init_guest_agent(GAState *s) |
| 487 | { |
| 488 | struct termios tio; |
| 489 | int ret, fd; |
| 490 | |
| 491 | if (s->method == NULL) { |
| 492 | /* try virtio-serial as our default */ |
| 493 | s->method = "virtio-serial"; |
| 494 | } |
| 495 | |
| 496 | if (s->path == NULL) { |
| 497 | if (strcmp(s->method, "virtio-serial") != 0) { |
| 498 | g_critical("must specify a path for this channel"); |
| 499 | exit(EXIT_FAILURE); |
| 500 | } |
| 501 | /* try the default path for the virtio-serial port */ |
| 502 | s->path = QGA_VIRTIO_PATH_DEFAULT; |
| 503 | } |
| 504 | |
| 505 | if (strcmp(s->method, "virtio-serial") == 0) { |
| 506 | s->virtio = true; |
| 507 | fd = qemu_open(s->path, O_RDWR | O_NONBLOCK | O_ASYNC); |
| 508 | if (fd == -1) { |
| 509 | g_critical("error opening channel: %s", strerror(errno)); |
| 510 | exit(EXIT_FAILURE); |
| 511 | } |
| 512 | ret = conn_channel_add(s, fd); |
| 513 | if (ret) { |
| 514 | g_critical("error adding channel to main loop"); |
| 515 | exit(EXIT_FAILURE); |
| 516 | } |
| 517 | } else if (strcmp(s->method, "isa-serial") == 0) { |
| 518 | fd = qemu_open(s->path, O_RDWR | O_NOCTTY); |
| 519 | if (fd == -1) { |
| 520 | g_critical("error opening channel: %s", strerror(errno)); |
| 521 | exit(EXIT_FAILURE); |
| 522 | } |
| 523 | tcgetattr(fd, &tio); |
| 524 | /* set up serial port for non-canonical, dumb byte streaming */ |
| 525 | tio.c_iflag &= ~(IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK | ISTRIP | |
| 526 | INLCR | IGNCR | ICRNL | IXON | IXOFF | IXANY | |
| 527 | IMAXBEL); |
| 528 | tio.c_oflag = 0; |
| 529 | tio.c_lflag = 0; |
| 530 | tio.c_cflag |= QGA_BAUDRATE_DEFAULT; |
| 531 | /* 1 available byte min or reads will block (we'll set non-blocking |
| 532 | * elsewhere, else we have to deal with read()=0 instead) |
| 533 | */ |
| 534 | tio.c_cc[VMIN] = 1; |
| 535 | tio.c_cc[VTIME] = 0; |
| 536 | /* flush everything waiting for read/xmit, it's garbage at this point */ |
| 537 | tcflush(fd, TCIFLUSH); |
| 538 | tcsetattr(fd, TCSANOW, &tio); |
| 539 | ret = conn_channel_add(s, fd); |
| 540 | if (ret) { |
| 541 | g_error("error adding channel to main loop"); |
| 542 | } |
| 543 | } else if (strcmp(s->method, "unix-listen") == 0) { |
| 544 | fd = unix_listen(s->path, NULL, strlen(s->path)); |
| 545 | if (fd == -1) { |
| 546 | g_critical("error opening path: %s", strerror(errno)); |
| 547 | exit(EXIT_FAILURE); |
| 548 | } |
| 549 | ret = listen_channel_add(s, fd, true); |
| 550 | if (ret) { |
| 551 | g_critical("error binding/listening to specified socket"); |
| 552 | exit(EXIT_FAILURE); |
| 553 | } |
| 554 | } else { |
| 555 | g_critical("unsupported channel method/type: %s", s->method); |
| 556 | exit(EXIT_FAILURE); |
| 557 | } |
| 558 | |
| 559 | json_message_parser_init(&s->parser, process_event); |
| 560 | s->main_loop = g_main_loop_new(NULL, false); |
| 561 | } |
| 562 | |
| 563 | int main(int argc, char **argv) |
| 564 | { |
| 565 | const char *sopt = "hVvdm:p:l:f:"; |
| 566 | const char *method = NULL, *path = NULL, *pidfile = QGA_PIDFILE_DEFAULT; |
| 567 | const struct option lopt[] = { |
| 568 | { "help", 0, NULL, 'h' }, |
| 569 | { "version", 0, NULL, 'V' }, |
| 570 | { "logfile", 0, NULL, 'l' }, |
| 571 | { "pidfile", 0, NULL, 'f' }, |
| 572 | { "verbose", 0, NULL, 'v' }, |
| 573 | { "method", 0, NULL, 'm' }, |
| 574 | { "path", 0, NULL, 'p' }, |
| 575 | { "daemonize", 0, NULL, 'd' }, |
| 576 | { NULL, 0, NULL, 0 } |
| 577 | }; |
| 578 | int opt_ind = 0, ch, daemonize = 0; |
| 579 | GLogLevelFlags log_level = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL; |
| 580 | FILE *log_file = stderr; |
| 581 | GAState *s; |
| 582 | |
| 583 | while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) { |
| 584 | switch (ch) { |
| 585 | case 'm': |
| 586 | method = optarg; |
| 587 | break; |
| 588 | case 'p': |
| 589 | path = optarg; |
| 590 | break; |
| 591 | case 'l': |
| 592 | log_file = fopen(optarg, "a"); |
| 593 | if (!log_file) { |
| 594 | g_critical("unable to open specified log file: %s", |
| 595 | strerror(errno)); |
| 596 | return EXIT_FAILURE; |
| 597 | } |
| 598 | break; |
| 599 | case 'f': |
| 600 | pidfile = optarg; |
| 601 | break; |
| 602 | case 'v': |
| 603 | /* enable all log levels */ |
| 604 | log_level = G_LOG_LEVEL_MASK; |
| 605 | break; |
| 606 | case 'V': |
| 607 | printf("QEMU Guest Agent %s\n", QGA_VERSION); |
| 608 | return 0; |
| 609 | case 'd': |
| 610 | daemonize = 1; |
| 611 | break; |
| 612 | case 'h': |
| 613 | usage(argv[0]); |
| 614 | return 0; |
| 615 | case '?': |
| 616 | g_print("Unknown option, try '%s --help' for more information.\n", |
| 617 | argv[0]); |
| 618 | return EXIT_FAILURE; |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | if (daemonize) { |
| 623 | g_debug("starting daemon"); |
| 624 | become_daemon(pidfile); |
| 625 | } |
| 626 | |
| 627 | g_type_init(); |
| 628 | g_thread_init(NULL); |
| 629 | |
| 630 | s = qemu_mallocz(sizeof(GAState)); |
| 631 | s->conn_channel = NULL; |
| 632 | s->path = path; |
| 633 | s->method = method; |
| 634 | s->log_file = log_file; |
| 635 | s->log_level = log_level; |
| 636 | g_log_set_default_handler(ga_log, s); |
| 637 | g_log_set_fatal_mask(NULL, G_LOG_LEVEL_ERROR); |
| 638 | s->logging_enabled = true; |
Michael Roth | e3d4d25 | 2011-07-19 15:41:55 -0500 | [diff] [blame^] | 639 | s->command_state = ga_command_state_new(); |
| 640 | ga_command_state_init(s, s->command_state); |
| 641 | ga_command_state_init_all(s->command_state); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 642 | ga_state = s; |
| 643 | |
| 644 | module_call_init(MODULE_INIT_QAPI); |
| 645 | init_guest_agent(ga_state); |
| 646 | register_signal_handlers(); |
| 647 | |
| 648 | g_main_loop_run(ga_state->main_loop); |
| 649 | |
Michael Roth | e3d4d25 | 2011-07-19 15:41:55 -0500 | [diff] [blame^] | 650 | ga_command_state_cleanup_all(ga_state->command_state); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 651 | unlink(pidfile); |
| 652 | |
| 653 | return 0; |
| 654 | } |