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> |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 17 | #include <getopt.h> |
Michael Roth | d8ca685 | 2012-01-19 22:04:34 -0600 | [diff] [blame^] | 18 | #ifndef _WIN32 |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 19 | #include <syslog.h> |
Michael Roth | d8ca685 | 2012-01-19 22:04:34 -0600 | [diff] [blame^] | 20 | #endif |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 21 | #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 Roth | abd6cf6 | 2011-12-06 22:03:42 -0600 | [diff] [blame] | 30 | #include "qapi/qmp-core.h" |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 31 | #include "qga/channel.h" |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 32 | |
| 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 Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 35 | |
| 36 | struct GAState { |
| 37 | JSONMessageParser parser; |
| 38 | GMainLoop *main_loop; |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 39 | GAChannel *channel; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 40 | 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 | |
| 47 | static struct GAState *ga_state; |
| 48 | |
Michael Roth | d8ca685 | 2012-01-19 22:04:34 -0600 | [diff] [blame^] | 49 | #ifndef _WIN32 |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 50 | static void quit_handler(int sig) |
| 51 | { |
Stefan Weil | 2542bfd | 2011-08-28 21:45:40 +0200 | [diff] [blame] | 52 | g_debug("received signal num %d, quitting", sig); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 53 | |
| 54 | if (g_main_loop_is_running(ga_state->main_loop)) { |
| 55 | g_main_loop_quit(ga_state->main_loop); |
| 56 | } |
| 57 | } |
| 58 | |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 59 | static gboolean register_signal_handlers(void) |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 60 | { |
| 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 Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 70 | return false; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 71 | } |
| 72 | ret = sigaction(SIGTERM, &sigact, NULL); |
| 73 | if (ret == -1) { |
| 74 | g_error("error configuring signal handler: %s", strerror(errno)); |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 75 | return false; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 76 | } |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 77 | return true; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 78 | } |
Michael Roth | d8ca685 | 2012-01-19 22:04:34 -0600 | [diff] [blame^] | 79 | #endif |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 80 | |
| 81 | static 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 Roth | d8ca685 | 2012-01-19 22:04:34 -0600 | [diff] [blame^] | 94 | #ifndef _WIN32 |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 95 | " -d, --daemonize become a daemon\n" |
Michael Roth | d8ca685 | 2012-01-19 22:04:34 -0600 | [diff] [blame^] | 96 | #endif |
Stefan Weil | dabdf39 | 2012-01-08 19:35:09 +0100 | [diff] [blame] | 97 | " -b, --blacklist comma-separated list of RPCs to disable (no spaces, \"?\"" |
Michael Roth | abd6cf6 | 2011-12-06 22:03:42 -0600 | [diff] [blame] | 98 | " to list available RPCs)\n" |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 99 | " -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 Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 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; |
Michael Roth | d8ca685 | 2012-01-19 22:04:34 -0600 | [diff] [blame^] | 152 | #ifndef _WIN32 |
Michael Roth | 8f47747 | 2011-08-11 15:38:11 -0500 | [diff] [blame] | 153 | if (domain && strcmp(domain, "syslog") == 0) { |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 154 | syslog(LOG_INFO, "%s: %s", level_str, msg); |
| 155 | } else if (level & s->log_level) { |
Michael Roth | d8ca685 | 2012-01-19 22:04:34 -0600 | [diff] [blame^] | 156 | #else |
| 157 | if (level & s->log_level) { |
| 158 | #endif |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 159 | 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 Roth | d8ca685 | 2012-01-19 22:04:34 -0600 | [diff] [blame^] | 166 | #ifndef _WIN32 |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 167 | static 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 | |
| 212 | fail: |
| 213 | unlink(pidfile); |
| 214 | g_critical("failed to daemonize"); |
| 215 | exit(EXIT_FAILURE); |
| 216 | } |
Michael Roth | d8ca685 | 2012-01-19 22:04:34 -0600 | [diff] [blame^] | 217 | #endif |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 218 | |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 219 | static int send_response(GAState *s, QObject *payload) |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 220 | { |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 221 | const char *buf; |
| 222 | QString *payload_qstr; |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 223 | GIOStatus status; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 224 | |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 225 | g_assert(payload && s->channel); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 226 | |
| 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 Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 234 | status = ga_channel_write_all(s->channel, buf, strlen(buf)); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 235 | QDECREF(payload_qstr); |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 236 | if (status != G_IO_STATUS_NORMAL) { |
| 237 | return -EIO; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 238 | } |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 239 | |
| 240 | return 0; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | static 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 Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 252 | ret = send_response(s, rsp); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 253 | if (ret) { |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 254 | g_warning("error sending response: %s", strerror(ret)); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 255 | } |
| 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 */ |
| 263 | static 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 Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 304 | ret = send_response(s, QOBJECT(qdict)); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 305 | if (ret) { |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 306 | g_warning("error sending error response: %s", strerror(ret)); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
| 310 | QDECREF(qdict); |
| 311 | } |
| 312 | |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 313 | /* false return signals GAChannel to close the current client connection */ |
| 314 | static gboolean channel_event_cb(GIOCondition condition, gpointer data) |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 315 | { |
| 316 | GAState *s = data; |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 317 | gchar buf[QGA_READ_COUNT_DEFAULT+1]; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 318 | gsize count; |
| 319 | GError *err = NULL; |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 320 | GIOStatus status = ga_channel_read(s->channel, buf, QGA_READ_COUNT_DEFAULT, &count); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 321 | if (err != NULL) { |
| 322 | g_warning("error reading channel: %s", err->message); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 323 | g_error_free(err); |
| 324 | return false; |
| 325 | } |
| 326 | switch (status) { |
| 327 | case G_IO_STATUS_ERROR: |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 328 | g_warning("error reading channel"); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 329 | return false; |
| 330 | case G_IO_STATUS_NORMAL: |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 331 | buf[count] = 0; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 332 | g_debug("read data, count: %d, data: %s", (int)count, buf); |
| 333 | json_message_parser_feed(&s->parser, (char *)buf, (int)count); |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 334 | break; |
| 335 | case G_IO_STATUS_EOF: |
| 336 | g_debug("received EOF"); |
| 337 | if (!s->virtio) { |
| 338 | return false; |
| 339 | } |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 340 | 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 Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 348 | default: |
| 349 | g_warning("unknown channel read status, closing"); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 350 | return false; |
| 351 | } |
| 352 | return true; |
| 353 | } |
| 354 | |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 355 | static gboolean channel_init(GAState *s, const gchar *method, const gchar *path) |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 356 | { |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 357 | GAChannelMethod channel_method; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 358 | |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 359 | if (method == NULL) { |
| 360 | method = "virtio-serial"; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 361 | } |
| 362 | |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 363 | if (path == NULL) { |
| 364 | if (strcmp(method, "virtio-serial") != 0) { |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 365 | g_critical("must specify a path for this channel"); |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 366 | return false; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 367 | } |
| 368 | /* try the default path for the virtio-serial port */ |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 369 | path = QGA_VIRTIO_PATH_DEFAULT; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 370 | } |
| 371 | |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 372 | 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 Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 379 | } else { |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 380 | g_critical("unsupported channel method/type: %s", method); |
| 381 | return false; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 382 | } |
| 383 | |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 384 | 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 Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | int main(int argc, char **argv) |
| 394 | { |
Michael Roth | abd6cf6 | 2011-12-06 22:03:42 -0600 | [diff] [blame] | 395 | const char *sopt = "hVvdm:p:l:f:b:"; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 396 | 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 Roth | abd6cf6 | 2011-12-06 22:03:42 -0600 | [diff] [blame] | 406 | { "blacklist", 0, NULL, 'b' }, |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 407 | { NULL, 0, NULL, 0 } |
| 408 | }; |
Michael Roth | abd6cf6 | 2011-12-06 22:03:42 -0600 | [diff] [blame] | 409 | int opt_ind = 0, ch, daemonize = 0, i, j, len; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 410 | GLogLevelFlags log_level = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL; |
| 411 | FILE *log_file = stderr; |
| 412 | GAState *s; |
| 413 | |
Michael Roth | abd6cf6 | 2011-12-06 22:03:42 -0600 | [diff] [blame] | 414 | module_call_init(MODULE_INIT_QAPI); |
| 415 | |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 416 | 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 Roth | abd6cf6 | 2011-12-06 22:03:42 -0600 | [diff] [blame] | 445 | 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 Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 471 | 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 Roth | d8ca685 | 2012-01-19 22:04:34 -0600 | [diff] [blame^] | 481 | #ifndef _WIN32 |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 482 | if (daemonize) { |
| 483 | g_debug("starting daemon"); |
| 484 | become_daemon(pidfile); |
| 485 | } |
Michael Roth | d8ca685 | 2012-01-19 22:04:34 -0600 | [diff] [blame^] | 486 | #endif |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 487 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 488 | s = g_malloc0(sizeof(GAState)); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 489 | 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 Roth | e3d4d25 | 2011-07-19 15:41:55 -0500 | [diff] [blame] | 494 | 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 Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 497 | json_message_parser_init(&s->parser, process_event); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 498 | ga_state = s; |
Michael Roth | d8ca685 | 2012-01-19 22:04:34 -0600 | [diff] [blame^] | 499 | #ifndef _WIN32 |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 500 | if (!register_signal_handlers()) { |
| 501 | g_critical("failed to register signal handlers"); |
| 502 | goto out_bad; |
| 503 | } |
Michael Roth | d8ca685 | 2012-01-19 22:04:34 -0600 | [diff] [blame^] | 504 | #endif |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 505 | |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 506 | 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 Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 511 | g_main_loop_run(ga_state->main_loop); |
| 512 | |
Michael Roth | e3d4d25 | 2011-07-19 15:41:55 -0500 | [diff] [blame] | 513 | ga_command_state_cleanup_all(ga_state->command_state); |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 514 | ga_channel_free(ga_state->channel); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 515 | |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 516 | if (daemonize) { |
| 517 | unlink(pidfile); |
| 518 | } |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 519 | return 0; |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 520 | |
| 521 | out_bad: |
| 522 | if (daemonize) { |
| 523 | unlink(pidfile); |
| 524 | } |
| 525 | return EXIT_FAILURE; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 526 | } |