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> |
Luiz Capitulino | 11d0f12 | 2012-02-28 11:03:03 -0300 | [diff] [blame] | 20 | #include <sys/wait.h> |
Michael Roth | f789aa7 | 2012-04-18 16:28:01 -0500 | [diff] [blame] | 21 | #include <sys/stat.h> |
Michael Roth | d8ca685 | 2012-01-19 22:04:34 -0600 | [diff] [blame] | 22 | #endif |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 23 | #include "json-streamer.h" |
| 24 | #include "json-parser.h" |
| 25 | #include "qint.h" |
| 26 | #include "qjson.h" |
| 27 | #include "qga/guest-agent-core.h" |
| 28 | #include "module.h" |
| 29 | #include "signal.h" |
| 30 | #include "qerror.h" |
Michael Roth | abd6cf6 | 2011-12-06 22:03:42 -0600 | [diff] [blame] | 31 | #include "qapi/qmp-core.h" |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 32 | #include "qga/channel.h" |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 33 | #ifdef _WIN32 |
| 34 | #include "qga/service-win32.h" |
| 35 | #include <windows.h> |
| 36 | #endif |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 37 | |
Michael Roth | 7868e26 | 2012-01-20 19:01:30 -0600 | [diff] [blame] | 38 | #ifndef _WIN32 |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 39 | #define QGA_VIRTIO_PATH_DEFAULT "/dev/virtio-ports/org.qemu.guest_agent.0" |
Michael Roth | 7868e26 | 2012-01-20 19:01:30 -0600 | [diff] [blame] | 40 | #else |
| 41 | #define QGA_VIRTIO_PATH_DEFAULT "\\\\.\\Global\\org.qemu.guest_agent.0" |
| 42 | #endif |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 43 | #define QGA_PIDFILE_DEFAULT "/var/run/qemu-ga.pid" |
Michael Roth | f789aa7 | 2012-04-18 16:28:01 -0500 | [diff] [blame] | 44 | #define QGA_STATEDIR_DEFAULT "/tmp" |
Michael Roth | 3cf0bed | 2012-02-07 13:56:48 -0600 | [diff] [blame] | 45 | #define QGA_SENTINEL_BYTE 0xFF |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 46 | |
| 47 | struct GAState { |
| 48 | JSONMessageParser parser; |
| 49 | GMainLoop *main_loop; |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 50 | GAChannel *channel; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 51 | bool virtio; /* fastpath to check for virtio to deal with poll() quirks */ |
| 52 | GACommandState *command_state; |
| 53 | GLogLevelFlags log_level; |
| 54 | FILE *log_file; |
| 55 | bool logging_enabled; |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 56 | #ifdef _WIN32 |
| 57 | GAService service; |
| 58 | #endif |
Michael Roth | 3cf0bed | 2012-02-07 13:56:48 -0600 | [diff] [blame] | 59 | bool delimit_response; |
Michael Roth | f22d85e | 2012-04-17 19:01:45 -0500 | [diff] [blame] | 60 | bool frozen; |
| 61 | GList *blacklist; |
Michael Roth | f789aa7 | 2012-04-18 16:28:01 -0500 | [diff] [blame] | 62 | const char *state_filepath_isfrozen; |
| 63 | struct { |
| 64 | const char *log_filepath; |
| 65 | const char *pid_filepath; |
| 66 | } deferred_options; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 67 | }; |
| 68 | |
Michael Roth | 3cf0bed | 2012-02-07 13:56:48 -0600 | [diff] [blame] | 69 | struct GAState *ga_state; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 70 | |
Michael Roth | f22d85e | 2012-04-17 19:01:45 -0500 | [diff] [blame] | 71 | /* commands that are safe to issue while filesystems are frozen */ |
| 72 | static const char *ga_freeze_whitelist[] = { |
| 73 | "guest-ping", |
| 74 | "guest-info", |
| 75 | "guest-sync", |
| 76 | "guest-fsfreeze-status", |
| 77 | "guest-fsfreeze-thaw", |
| 78 | NULL |
| 79 | }; |
| 80 | |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 81 | #ifdef _WIN32 |
| 82 | DWORD WINAPI service_ctrl_handler(DWORD ctrl, DWORD type, LPVOID data, |
| 83 | LPVOID ctx); |
| 84 | VOID WINAPI service_main(DWORD argc, TCHAR *argv[]); |
| 85 | #endif |
| 86 | |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 87 | static void quit_handler(int sig) |
| 88 | { |
Michael Roth | f22d85e | 2012-04-17 19:01:45 -0500 | [diff] [blame] | 89 | /* if we're frozen, don't exit unless we're absolutely forced to, |
| 90 | * because it's basically impossible for graceful exit to complete |
| 91 | * unless all log/pid files are on unfreezable filesystems. there's |
| 92 | * also a very likely chance killing the agent before unfreezing |
| 93 | * the filesystems is a mistake (or will be viewed as one later). |
| 94 | */ |
| 95 | if (ga_is_frozen(ga_state)) { |
| 96 | return; |
| 97 | } |
Stefan Weil | 2542bfd | 2011-08-28 21:45:40 +0200 | [diff] [blame] | 98 | g_debug("received signal num %d, quitting", sig); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 99 | |
| 100 | if (g_main_loop_is_running(ga_state->main_loop)) { |
| 101 | g_main_loop_quit(ga_state->main_loop); |
| 102 | } |
| 103 | } |
| 104 | |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 105 | #ifndef _WIN32 |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 106 | static gboolean register_signal_handlers(void) |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 107 | { |
Luiz Capitulino | dc8764f0 | 2012-05-11 16:19:46 -0300 | [diff] [blame] | 108 | struct sigaction sigact; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 109 | int ret; |
| 110 | |
| 111 | memset(&sigact, 0, sizeof(struct sigaction)); |
| 112 | sigact.sa_handler = quit_handler; |
| 113 | |
| 114 | ret = sigaction(SIGINT, &sigact, NULL); |
| 115 | if (ret == -1) { |
| 116 | g_error("error configuring signal handler: %s", strerror(errno)); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 117 | } |
| 118 | ret = sigaction(SIGTERM, &sigact, NULL); |
| 119 | if (ret == -1) { |
| 120 | g_error("error configuring signal handler: %s", strerror(errno)); |
| 121 | } |
Luiz Capitulino | 11d0f12 | 2012-02-28 11:03:03 -0300 | [diff] [blame] | 122 | |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 123 | return true; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 124 | } |
Luiz Capitulino | 04b4e75 | 2012-05-10 16:50:41 -0300 | [diff] [blame] | 125 | |
| 126 | /* TODO: use this in place of all post-fork() fclose(std*) callers */ |
| 127 | void reopen_fd_to_null(int fd) |
| 128 | { |
| 129 | int nullfd; |
| 130 | |
| 131 | nullfd = open("/dev/null", O_RDWR); |
| 132 | if (nullfd < 0) { |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | dup2(nullfd, fd); |
| 137 | |
| 138 | if (nullfd != fd) { |
| 139 | close(nullfd); |
| 140 | } |
| 141 | } |
Michael Roth | d8ca685 | 2012-01-19 22:04:34 -0600 | [diff] [blame] | 142 | #endif |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 143 | |
| 144 | static void usage(const char *cmd) |
| 145 | { |
| 146 | printf( |
Michael Roth | 4bdd041 | 2012-04-17 11:28:27 -0500 | [diff] [blame] | 147 | "Usage: %s [-m <method> -p <path>] [<options>]\n" |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 148 | "QEMU Guest Agent %s\n" |
| 149 | "\n" |
| 150 | " -m, --method transport method: one of unix-listen, virtio-serial, or\n" |
| 151 | " isa-serial (virtio-serial is the default)\n" |
Michael Roth | 4bdd041 | 2012-04-17 11:28:27 -0500 | [diff] [blame] | 152 | " -p, --path device/socket path (the default for virtio-serial is:\n" |
| 153 | " %s)\n" |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 154 | " -l, --logfile set logfile path, logs to stderr by default\n" |
| 155 | " -f, --pidfile specify pidfile (default is %s)\n" |
Michael Roth | f789aa7 | 2012-04-18 16:28:01 -0500 | [diff] [blame] | 156 | " -t, --statedir specify dir to store state information (absolute paths\n" |
| 157 | " only, default is %s)\n" |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 158 | " -v, --verbose log extra debugging information\n" |
| 159 | " -V, --version print version information and exit\n" |
| 160 | " -d, --daemonize become a daemon\n" |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 161 | #ifdef _WIN32 |
| 162 | " -s, --service service commands: install, uninstall\n" |
Michael Roth | d8ca685 | 2012-01-19 22:04:34 -0600 | [diff] [blame] | 163 | #endif |
Michael Roth | 4bdd041 | 2012-04-17 11:28:27 -0500 | [diff] [blame] | 164 | " -b, --blacklist comma-separated list of RPCs to disable (no spaces, \"?\"\n" |
Michael Roth | abd6cf6 | 2011-12-06 22:03:42 -0600 | [diff] [blame] | 165 | " to list available RPCs)\n" |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 166 | " -h, --help display this help and exit\n" |
| 167 | "\n" |
| 168 | "Report bugs to <mdroth@linux.vnet.ibm.com>\n" |
Michael Roth | 8efacc4 | 2012-05-14 09:33:48 -0500 | [diff] [blame] | 169 | , cmd, QEMU_VERSION, QGA_VIRTIO_PATH_DEFAULT, QGA_PIDFILE_DEFAULT, |
Michael Roth | f789aa7 | 2012-04-18 16:28:01 -0500 | [diff] [blame] | 170 | QGA_STATEDIR_DEFAULT); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 171 | } |
| 172 | |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 173 | static const char *ga_log_level_str(GLogLevelFlags level) |
| 174 | { |
| 175 | switch (level & G_LOG_LEVEL_MASK) { |
| 176 | case G_LOG_LEVEL_ERROR: |
| 177 | return "error"; |
| 178 | case G_LOG_LEVEL_CRITICAL: |
| 179 | return "critical"; |
| 180 | case G_LOG_LEVEL_WARNING: |
| 181 | return "warning"; |
| 182 | case G_LOG_LEVEL_MESSAGE: |
| 183 | return "message"; |
| 184 | case G_LOG_LEVEL_INFO: |
| 185 | return "info"; |
| 186 | case G_LOG_LEVEL_DEBUG: |
| 187 | return "debug"; |
| 188 | default: |
| 189 | return "user"; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | bool ga_logging_enabled(GAState *s) |
| 194 | { |
| 195 | return s->logging_enabled; |
| 196 | } |
| 197 | |
| 198 | void ga_disable_logging(GAState *s) |
| 199 | { |
| 200 | s->logging_enabled = false; |
| 201 | } |
| 202 | |
| 203 | void ga_enable_logging(GAState *s) |
| 204 | { |
| 205 | s->logging_enabled = true; |
| 206 | } |
| 207 | |
| 208 | static void ga_log(const gchar *domain, GLogLevelFlags level, |
| 209 | const gchar *msg, gpointer opaque) |
| 210 | { |
| 211 | GAState *s = opaque; |
| 212 | GTimeVal time; |
| 213 | const char *level_str = ga_log_level_str(level); |
| 214 | |
| 215 | if (!ga_logging_enabled(s)) { |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | level &= G_LOG_LEVEL_MASK; |
Michael Roth | d8ca685 | 2012-01-19 22:04:34 -0600 | [diff] [blame] | 220 | #ifndef _WIN32 |
Michael Roth | 8f47747 | 2011-08-11 15:38:11 -0500 | [diff] [blame] | 221 | if (domain && strcmp(domain, "syslog") == 0) { |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 222 | syslog(LOG_INFO, "%s: %s", level_str, msg); |
| 223 | } else if (level & s->log_level) { |
Michael Roth | d8ca685 | 2012-01-19 22:04:34 -0600 | [diff] [blame] | 224 | #else |
| 225 | if (level & s->log_level) { |
| 226 | #endif |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 227 | g_get_current_time(&time); |
| 228 | fprintf(s->log_file, |
| 229 | "%lu.%lu: %s: %s\n", time.tv_sec, time.tv_usec, level_str, msg); |
| 230 | fflush(s->log_file); |
| 231 | } |
| 232 | } |
| 233 | |
Michael Roth | 3cf0bed | 2012-02-07 13:56:48 -0600 | [diff] [blame] | 234 | void ga_set_response_delimited(GAState *s) |
| 235 | { |
| 236 | s->delimit_response = true; |
| 237 | } |
| 238 | |
Michael Roth | f789aa7 | 2012-04-18 16:28:01 -0500 | [diff] [blame] | 239 | #ifndef _WIN32 |
| 240 | static bool ga_open_pidfile(const char *pidfile) |
| 241 | { |
| 242 | int pidfd; |
| 243 | char pidstr[32]; |
| 244 | |
| 245 | pidfd = open(pidfile, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR); |
| 246 | if (pidfd == -1 || lockf(pidfd, F_TLOCK, 0)) { |
| 247 | g_critical("Cannot lock pid file, %s", strerror(errno)); |
Jim Meyering | 4144f12 | 2012-08-22 13:55:52 +0200 | [diff] [blame] | 248 | if (pidfd != -1) { |
| 249 | close(pidfd); |
| 250 | } |
Michael Roth | f789aa7 | 2012-04-18 16:28:01 -0500 | [diff] [blame] | 251 | return false; |
| 252 | } |
| 253 | |
| 254 | if (ftruncate(pidfd, 0) || lseek(pidfd, 0, SEEK_SET)) { |
| 255 | g_critical("Failed to truncate pid file"); |
| 256 | goto fail; |
| 257 | } |
| 258 | sprintf(pidstr, "%d", getpid()); |
| 259 | if (write(pidfd, pidstr, strlen(pidstr)) != strlen(pidstr)) { |
| 260 | g_critical("Failed to write pid file"); |
| 261 | goto fail; |
| 262 | } |
| 263 | |
| 264 | return true; |
| 265 | |
| 266 | fail: |
| 267 | unlink(pidfile); |
| 268 | return false; |
| 269 | } |
| 270 | #else /* _WIN32 */ |
| 271 | static bool ga_open_pidfile(const char *pidfile) |
| 272 | { |
| 273 | return true; |
| 274 | } |
| 275 | #endif |
| 276 | |
Michael Roth | f22d85e | 2012-04-17 19:01:45 -0500 | [diff] [blame] | 277 | static gint ga_strcmp(gconstpointer str1, gconstpointer str2) |
| 278 | { |
| 279 | return strcmp(str1, str2); |
| 280 | } |
| 281 | |
| 282 | /* disable commands that aren't safe for fsfreeze */ |
| 283 | static void ga_disable_non_whitelisted(void) |
| 284 | { |
| 285 | char **list_head, **list; |
| 286 | bool whitelisted; |
| 287 | int i; |
| 288 | |
| 289 | list_head = list = qmp_get_command_list(); |
| 290 | while (*list != NULL) { |
| 291 | whitelisted = false; |
| 292 | i = 0; |
| 293 | while (ga_freeze_whitelist[i] != NULL) { |
| 294 | if (strcmp(*list, ga_freeze_whitelist[i]) == 0) { |
| 295 | whitelisted = true; |
| 296 | } |
| 297 | i++; |
| 298 | } |
| 299 | if (!whitelisted) { |
| 300 | g_debug("disabling command: %s", *list); |
| 301 | qmp_disable_command(*list); |
| 302 | } |
| 303 | g_free(*list); |
| 304 | list++; |
| 305 | } |
| 306 | g_free(list_head); |
| 307 | } |
| 308 | |
Jim Meyering | a31f053 | 2012-05-09 05:12:04 +0000 | [diff] [blame] | 309 | /* [re-]enable all commands, except those explicitly blacklisted by user */ |
Michael Roth | f22d85e | 2012-04-17 19:01:45 -0500 | [diff] [blame] | 310 | static void ga_enable_non_blacklisted(GList *blacklist) |
| 311 | { |
| 312 | char **list_head, **list; |
| 313 | |
| 314 | list_head = list = qmp_get_command_list(); |
| 315 | while (*list != NULL) { |
| 316 | if (g_list_find_custom(blacklist, *list, ga_strcmp) == NULL && |
| 317 | !qmp_command_is_enabled(*list)) { |
| 318 | g_debug("enabling command: %s", *list); |
| 319 | qmp_enable_command(*list); |
| 320 | } |
| 321 | g_free(*list); |
| 322 | list++; |
| 323 | } |
| 324 | g_free(list_head); |
| 325 | } |
| 326 | |
Michael Roth | f789aa7 | 2012-04-18 16:28:01 -0500 | [diff] [blame] | 327 | static bool ga_create_file(const char *path) |
| 328 | { |
| 329 | int fd = open(path, O_CREAT | O_WRONLY, S_IWUSR | S_IRUSR); |
| 330 | if (fd == -1) { |
| 331 | g_warning("unable to open/create file %s: %s", path, strerror(errno)); |
| 332 | return false; |
| 333 | } |
| 334 | close(fd); |
| 335 | return true; |
| 336 | } |
| 337 | |
| 338 | static bool ga_delete_file(const char *path) |
| 339 | { |
| 340 | int ret = unlink(path); |
| 341 | if (ret == -1) { |
| 342 | g_warning("unable to delete file: %s: %s", path, strerror(errno)); |
| 343 | return false; |
| 344 | } |
| 345 | |
| 346 | return true; |
| 347 | } |
| 348 | |
Michael Roth | f22d85e | 2012-04-17 19:01:45 -0500 | [diff] [blame] | 349 | bool ga_is_frozen(GAState *s) |
| 350 | { |
| 351 | return s->frozen; |
| 352 | } |
| 353 | |
| 354 | void ga_set_frozen(GAState *s) |
| 355 | { |
| 356 | if (ga_is_frozen(s)) { |
| 357 | return; |
| 358 | } |
| 359 | /* disable all non-whitelisted (for frozen state) commands */ |
| 360 | ga_disable_non_whitelisted(); |
| 361 | g_warning("disabling logging due to filesystem freeze"); |
| 362 | ga_disable_logging(s); |
| 363 | s->frozen = true; |
Michael Roth | f789aa7 | 2012-04-18 16:28:01 -0500 | [diff] [blame] | 364 | if (!ga_create_file(s->state_filepath_isfrozen)) { |
| 365 | g_warning("unable to create %s, fsfreeze may not function properly", |
| 366 | s->state_filepath_isfrozen); |
| 367 | } |
Michael Roth | f22d85e | 2012-04-17 19:01:45 -0500 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | void ga_unset_frozen(GAState *s) |
| 371 | { |
| 372 | if (!ga_is_frozen(s)) { |
| 373 | return; |
| 374 | } |
| 375 | |
Michael Roth | f789aa7 | 2012-04-18 16:28:01 -0500 | [diff] [blame] | 376 | /* if we delayed creation/opening of pid/log files due to being |
| 377 | * in a frozen state at start up, do it now |
| 378 | */ |
| 379 | if (s->deferred_options.log_filepath) { |
| 380 | s->log_file = fopen(s->deferred_options.log_filepath, "a"); |
| 381 | if (!s->log_file) { |
| 382 | s->log_file = stderr; |
| 383 | } |
| 384 | s->deferred_options.log_filepath = NULL; |
| 385 | } |
Michael Roth | f22d85e | 2012-04-17 19:01:45 -0500 | [diff] [blame] | 386 | ga_enable_logging(s); |
Michael Roth | f789aa7 | 2012-04-18 16:28:01 -0500 | [diff] [blame] | 387 | g_warning("logging re-enabled due to filesystem unfreeze"); |
| 388 | if (s->deferred_options.pid_filepath) { |
| 389 | if (!ga_open_pidfile(s->deferred_options.pid_filepath)) { |
| 390 | g_warning("failed to create/open pid file"); |
| 391 | } |
| 392 | s->deferred_options.pid_filepath = NULL; |
| 393 | } |
Michael Roth | f22d85e | 2012-04-17 19:01:45 -0500 | [diff] [blame] | 394 | |
| 395 | /* enable all disabled, non-blacklisted commands */ |
| 396 | ga_enable_non_blacklisted(s->blacklist); |
| 397 | s->frozen = false; |
Michael Roth | f789aa7 | 2012-04-18 16:28:01 -0500 | [diff] [blame] | 398 | if (!ga_delete_file(s->state_filepath_isfrozen)) { |
| 399 | g_warning("unable to delete %s, fsfreeze may not function properly", |
| 400 | s->state_filepath_isfrozen); |
| 401 | } |
Michael Roth | f22d85e | 2012-04-17 19:01:45 -0500 | [diff] [blame] | 402 | } |
| 403 | |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 404 | static void become_daemon(const char *pidfile) |
| 405 | { |
Michael Roth | f789aa7 | 2012-04-18 16:28:01 -0500 | [diff] [blame] | 406 | #ifndef _WIN32 |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 407 | pid_t pid, sid; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 408 | |
| 409 | pid = fork(); |
| 410 | if (pid < 0) { |
| 411 | exit(EXIT_FAILURE); |
| 412 | } |
| 413 | if (pid > 0) { |
| 414 | exit(EXIT_SUCCESS); |
| 415 | } |
| 416 | |
Michael Roth | f789aa7 | 2012-04-18 16:28:01 -0500 | [diff] [blame] | 417 | if (pidfile) { |
| 418 | if (!ga_open_pidfile(pidfile)) { |
| 419 | g_critical("failed to create pidfile"); |
| 420 | exit(EXIT_FAILURE); |
| 421 | } |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | umask(0); |
| 425 | sid = setsid(); |
| 426 | if (sid < 0) { |
| 427 | goto fail; |
| 428 | } |
| 429 | if ((chdir("/")) < 0) { |
| 430 | goto fail; |
| 431 | } |
| 432 | |
Luiz Capitulino | 226a489 | 2012-05-10 16:50:42 -0300 | [diff] [blame] | 433 | reopen_fd_to_null(STDIN_FILENO); |
| 434 | reopen_fd_to_null(STDOUT_FILENO); |
| 435 | reopen_fd_to_null(STDERR_FILENO); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 436 | return; |
| 437 | |
| 438 | fail: |
Stefan Weil | 4bdb1a30 | 2012-08-24 07:03:03 +0200 | [diff] [blame] | 439 | if (pidfile) { |
| 440 | unlink(pidfile); |
| 441 | } |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 442 | g_critical("failed to daemonize"); |
| 443 | exit(EXIT_FAILURE); |
Michael Roth | d8ca685 | 2012-01-19 22:04:34 -0600 | [diff] [blame] | 444 | #endif |
Michael Roth | f789aa7 | 2012-04-18 16:28:01 -0500 | [diff] [blame] | 445 | } |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 446 | |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 447 | static int send_response(GAState *s, QObject *payload) |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 448 | { |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 449 | const char *buf; |
Michael Roth | 3cf0bed | 2012-02-07 13:56:48 -0600 | [diff] [blame] | 450 | QString *payload_qstr, *response_qstr; |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 451 | GIOStatus status; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 452 | |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 453 | g_assert(payload && s->channel); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 454 | |
| 455 | payload_qstr = qobject_to_json(payload); |
| 456 | if (!payload_qstr) { |
| 457 | return -EINVAL; |
| 458 | } |
| 459 | |
Michael Roth | 3cf0bed | 2012-02-07 13:56:48 -0600 | [diff] [blame] | 460 | if (s->delimit_response) { |
| 461 | s->delimit_response = false; |
| 462 | response_qstr = qstring_new(); |
| 463 | qstring_append_chr(response_qstr, QGA_SENTINEL_BYTE); |
| 464 | qstring_append(response_qstr, qstring_get_str(payload_qstr)); |
| 465 | QDECREF(payload_qstr); |
| 466 | } else { |
| 467 | response_qstr = payload_qstr; |
| 468 | } |
| 469 | |
| 470 | qstring_append_chr(response_qstr, '\n'); |
| 471 | buf = qstring_get_str(response_qstr); |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 472 | status = ga_channel_write_all(s->channel, buf, strlen(buf)); |
Michael Roth | 3cf0bed | 2012-02-07 13:56:48 -0600 | [diff] [blame] | 473 | QDECREF(response_qstr); |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 474 | if (status != G_IO_STATUS_NORMAL) { |
| 475 | return -EIO; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 476 | } |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 477 | |
| 478 | return 0; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | static void process_command(GAState *s, QDict *req) |
| 482 | { |
| 483 | QObject *rsp = NULL; |
| 484 | int ret; |
| 485 | |
| 486 | g_assert(req); |
| 487 | g_debug("processing command"); |
| 488 | rsp = qmp_dispatch(QOBJECT(req)); |
| 489 | if (rsp) { |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 490 | ret = send_response(s, rsp); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 491 | if (ret) { |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 492 | g_warning("error sending response: %s", strerror(ret)); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 493 | } |
| 494 | qobject_decref(rsp); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | |
| 498 | /* handle requests/control events coming in over the channel */ |
| 499 | static void process_event(JSONMessageParser *parser, QList *tokens) |
| 500 | { |
| 501 | GAState *s = container_of(parser, GAState, parser); |
| 502 | QObject *obj; |
| 503 | QDict *qdict; |
| 504 | Error *err = NULL; |
| 505 | int ret; |
| 506 | |
| 507 | g_assert(s && parser); |
| 508 | |
| 509 | g_debug("process_event: called"); |
| 510 | obj = json_parser_parse_err(tokens, NULL, &err); |
| 511 | if (err || !obj || qobject_type(obj) != QTYPE_QDICT) { |
| 512 | qobject_decref(obj); |
| 513 | qdict = qdict_new(); |
| 514 | if (!err) { |
| 515 | g_warning("failed to parse event: unknown error"); |
| 516 | error_set(&err, QERR_JSON_PARSING); |
| 517 | } else { |
| 518 | g_warning("failed to parse event: %s", error_get_pretty(err)); |
| 519 | } |
Luiz Capitulino | 93b91c5 | 2012-08-01 16:30:13 -0300 | [diff] [blame] | 520 | qdict_put_obj(qdict, "error", qmp_build_error_object(err)); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 521 | error_free(err); |
| 522 | } else { |
| 523 | qdict = qobject_to_qdict(obj); |
| 524 | } |
| 525 | |
| 526 | g_assert(qdict); |
| 527 | |
| 528 | /* handle host->guest commands */ |
| 529 | if (qdict_haskey(qdict, "execute")) { |
| 530 | process_command(s, qdict); |
| 531 | } else { |
| 532 | if (!qdict_haskey(qdict, "error")) { |
| 533 | QDECREF(qdict); |
| 534 | qdict = qdict_new(); |
| 535 | g_warning("unrecognized payload format"); |
| 536 | error_set(&err, QERR_UNSUPPORTED); |
Luiz Capitulino | 93b91c5 | 2012-08-01 16:30:13 -0300 | [diff] [blame] | 537 | qdict_put_obj(qdict, "error", qmp_build_error_object(err)); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 538 | error_free(err); |
| 539 | } |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 540 | ret = send_response(s, QOBJECT(qdict)); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 541 | if (ret) { |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 542 | g_warning("error sending error response: %s", strerror(ret)); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | |
| 546 | QDECREF(qdict); |
| 547 | } |
| 548 | |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 549 | /* false return signals GAChannel to close the current client connection */ |
| 550 | static gboolean channel_event_cb(GIOCondition condition, gpointer data) |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 551 | { |
| 552 | GAState *s = data; |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 553 | gchar buf[QGA_READ_COUNT_DEFAULT+1]; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 554 | gsize count; |
| 555 | GError *err = NULL; |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 556 | 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] | 557 | if (err != NULL) { |
| 558 | g_warning("error reading channel: %s", err->message); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 559 | g_error_free(err); |
| 560 | return false; |
| 561 | } |
| 562 | switch (status) { |
| 563 | case G_IO_STATUS_ERROR: |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 564 | g_warning("error reading channel"); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 565 | return false; |
| 566 | case G_IO_STATUS_NORMAL: |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 567 | buf[count] = 0; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 568 | g_debug("read data, count: %d, data: %s", (int)count, buf); |
| 569 | json_message_parser_feed(&s->parser, (char *)buf, (int)count); |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 570 | break; |
| 571 | case G_IO_STATUS_EOF: |
| 572 | g_debug("received EOF"); |
| 573 | if (!s->virtio) { |
| 574 | return false; |
| 575 | } |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 576 | case G_IO_STATUS_AGAIN: |
| 577 | /* virtio causes us to spin here when no process is attached to |
| 578 | * host-side chardev. sleep a bit to mitigate this |
| 579 | */ |
| 580 | if (s->virtio) { |
| 581 | usleep(100*1000); |
| 582 | } |
| 583 | return true; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 584 | default: |
| 585 | g_warning("unknown channel read status, closing"); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 586 | return false; |
| 587 | } |
| 588 | return true; |
| 589 | } |
| 590 | |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 591 | static gboolean channel_init(GAState *s, const gchar *method, const gchar *path) |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 592 | { |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 593 | GAChannelMethod channel_method; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 594 | |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 595 | if (method == NULL) { |
| 596 | method = "virtio-serial"; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 597 | } |
| 598 | |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 599 | if (path == NULL) { |
| 600 | if (strcmp(method, "virtio-serial") != 0) { |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 601 | g_critical("must specify a path for this channel"); |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 602 | return false; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 603 | } |
| 604 | /* try the default path for the virtio-serial port */ |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 605 | path = QGA_VIRTIO_PATH_DEFAULT; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 606 | } |
| 607 | |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 608 | if (strcmp(method, "virtio-serial") == 0) { |
| 609 | s->virtio = true; /* virtio requires special handling in some cases */ |
| 610 | channel_method = GA_CHANNEL_VIRTIO_SERIAL; |
| 611 | } else if (strcmp(method, "isa-serial") == 0) { |
| 612 | channel_method = GA_CHANNEL_ISA_SERIAL; |
| 613 | } else if (strcmp(method, "unix-listen") == 0) { |
| 614 | channel_method = GA_CHANNEL_UNIX_LISTEN; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 615 | } else { |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 616 | g_critical("unsupported channel method/type: %s", method); |
| 617 | return false; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 618 | } |
| 619 | |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 620 | s->channel = ga_channel_new(channel_method, path, channel_event_cb, s); |
| 621 | if (!s->channel) { |
| 622 | g_critical("failed to create guest agent channel"); |
| 623 | return false; |
| 624 | } |
| 625 | |
| 626 | return true; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 627 | } |
| 628 | |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 629 | #ifdef _WIN32 |
| 630 | DWORD WINAPI service_ctrl_handler(DWORD ctrl, DWORD type, LPVOID data, |
| 631 | LPVOID ctx) |
| 632 | { |
| 633 | DWORD ret = NO_ERROR; |
| 634 | GAService *service = &ga_state->service; |
| 635 | |
| 636 | switch (ctrl) |
| 637 | { |
| 638 | case SERVICE_CONTROL_STOP: |
| 639 | case SERVICE_CONTROL_SHUTDOWN: |
| 640 | quit_handler(SIGTERM); |
| 641 | service->status.dwCurrentState = SERVICE_STOP_PENDING; |
| 642 | SetServiceStatus(service->status_handle, &service->status); |
| 643 | break; |
| 644 | |
| 645 | default: |
| 646 | ret = ERROR_CALL_NOT_IMPLEMENTED; |
| 647 | } |
| 648 | return ret; |
| 649 | } |
| 650 | |
| 651 | VOID WINAPI service_main(DWORD argc, TCHAR *argv[]) |
| 652 | { |
| 653 | GAService *service = &ga_state->service; |
| 654 | |
| 655 | service->status_handle = RegisterServiceCtrlHandlerEx(QGA_SERVICE_NAME, |
| 656 | service_ctrl_handler, NULL); |
| 657 | |
| 658 | if (service->status_handle == 0) { |
| 659 | g_critical("Failed to register extended requests function!\n"); |
| 660 | return; |
| 661 | } |
| 662 | |
| 663 | service->status.dwServiceType = SERVICE_WIN32; |
| 664 | service->status.dwCurrentState = SERVICE_RUNNING; |
| 665 | service->status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN; |
| 666 | service->status.dwWin32ExitCode = NO_ERROR; |
| 667 | service->status.dwServiceSpecificExitCode = NO_ERROR; |
| 668 | service->status.dwCheckPoint = 0; |
| 669 | service->status.dwWaitHint = 0; |
| 670 | SetServiceStatus(service->status_handle, &service->status); |
| 671 | |
| 672 | g_main_loop_run(ga_state->main_loop); |
| 673 | |
| 674 | service->status.dwCurrentState = SERVICE_STOPPED; |
| 675 | SetServiceStatus(service->status_handle, &service->status); |
| 676 | } |
| 677 | #endif |
| 678 | |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 679 | int main(int argc, char **argv) |
| 680 | { |
Michael Roth | f789aa7 | 2012-04-18 16:28:01 -0500 | [diff] [blame] | 681 | const char *sopt = "hVvdm:p:l:f:b:s:t:"; |
| 682 | const char *method = NULL, *path = NULL; |
| 683 | const char *log_filepath = NULL; |
| 684 | const char *pid_filepath = QGA_PIDFILE_DEFAULT; |
| 685 | const char *state_dir = QGA_STATEDIR_DEFAULT; |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 686 | #ifdef _WIN32 |
| 687 | const char *service = NULL; |
| 688 | #endif |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 689 | const struct option lopt[] = { |
| 690 | { "help", 0, NULL, 'h' }, |
| 691 | { "version", 0, NULL, 'V' }, |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 692 | { "logfile", 1, NULL, 'l' }, |
| 693 | { "pidfile", 1, NULL, 'f' }, |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 694 | { "verbose", 0, NULL, 'v' }, |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 695 | { "method", 1, NULL, 'm' }, |
| 696 | { "path", 1, NULL, 'p' }, |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 697 | { "daemonize", 0, NULL, 'd' }, |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 698 | { "blacklist", 1, NULL, 'b' }, |
| 699 | #ifdef _WIN32 |
| 700 | { "service", 1, NULL, 's' }, |
Michael Roth | f22d85e | 2012-04-17 19:01:45 -0500 | [diff] [blame] | 701 | #endif |
Michael Roth | f789aa7 | 2012-04-18 16:28:01 -0500 | [diff] [blame] | 702 | { "statedir", 1, NULL, 't' }, |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 703 | { NULL, 0, NULL, 0 } |
| 704 | }; |
Michael Roth | abd6cf6 | 2011-12-06 22:03:42 -0600 | [diff] [blame] | 705 | int opt_ind = 0, ch, daemonize = 0, i, j, len; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 706 | GLogLevelFlags log_level = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL; |
Michael Roth | f22d85e | 2012-04-17 19:01:45 -0500 | [diff] [blame] | 707 | GList *blacklist = NULL; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 708 | GAState *s; |
| 709 | |
Michael Roth | abd6cf6 | 2011-12-06 22:03:42 -0600 | [diff] [blame] | 710 | module_call_init(MODULE_INIT_QAPI); |
| 711 | |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 712 | while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) { |
| 713 | switch (ch) { |
| 714 | case 'm': |
| 715 | method = optarg; |
| 716 | break; |
| 717 | case 'p': |
| 718 | path = optarg; |
| 719 | break; |
| 720 | case 'l': |
Michael Roth | f789aa7 | 2012-04-18 16:28:01 -0500 | [diff] [blame] | 721 | log_filepath = optarg; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 722 | break; |
| 723 | case 'f': |
Michael Roth | f789aa7 | 2012-04-18 16:28:01 -0500 | [diff] [blame] | 724 | pid_filepath = optarg; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 725 | break; |
Michael Roth | f789aa7 | 2012-04-18 16:28:01 -0500 | [diff] [blame] | 726 | case 't': |
| 727 | state_dir = optarg; |
| 728 | break; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 729 | case 'v': |
| 730 | /* enable all log levels */ |
| 731 | log_level = G_LOG_LEVEL_MASK; |
| 732 | break; |
| 733 | case 'V': |
Michael Roth | 8efacc4 | 2012-05-14 09:33:48 -0500 | [diff] [blame] | 734 | printf("QEMU Guest Agent %s\n", QEMU_VERSION); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 735 | return 0; |
| 736 | case 'd': |
| 737 | daemonize = 1; |
| 738 | break; |
Michael Roth | abd6cf6 | 2011-12-06 22:03:42 -0600 | [diff] [blame] | 739 | case 'b': { |
| 740 | char **list_head, **list; |
Peter Maydell | c8057f9 | 2012-08-02 13:45:54 +0100 | [diff] [blame] | 741 | if (is_help_option(optarg)) { |
Michael Roth | abd6cf6 | 2011-12-06 22:03:42 -0600 | [diff] [blame] | 742 | list_head = list = qmp_get_command_list(); |
| 743 | while (*list != NULL) { |
| 744 | printf("%s\n", *list); |
| 745 | g_free(*list); |
| 746 | list++; |
| 747 | } |
| 748 | g_free(list_head); |
| 749 | return 0; |
| 750 | } |
| 751 | for (j = 0, i = 0, len = strlen(optarg); i < len; i++) { |
| 752 | if (optarg[i] == ',') { |
| 753 | optarg[i] = 0; |
Michael Roth | f22d85e | 2012-04-17 19:01:45 -0500 | [diff] [blame] | 754 | blacklist = g_list_append(blacklist, &optarg[j]); |
Michael Roth | abd6cf6 | 2011-12-06 22:03:42 -0600 | [diff] [blame] | 755 | j = i + 1; |
| 756 | } |
| 757 | } |
| 758 | if (j < i) { |
Michael Roth | f22d85e | 2012-04-17 19:01:45 -0500 | [diff] [blame] | 759 | blacklist = g_list_append(blacklist, &optarg[j]); |
Michael Roth | abd6cf6 | 2011-12-06 22:03:42 -0600 | [diff] [blame] | 760 | } |
| 761 | break; |
| 762 | } |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 763 | #ifdef _WIN32 |
| 764 | case 's': |
| 765 | service = optarg; |
| 766 | if (strcmp(service, "install") == 0) { |
Michael Roth | f789aa7 | 2012-04-18 16:28:01 -0500 | [diff] [blame] | 767 | return ga_install_service(path, log_filepath); |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 768 | } else if (strcmp(service, "uninstall") == 0) { |
| 769 | return ga_uninstall_service(); |
| 770 | } else { |
| 771 | printf("Unknown service command.\n"); |
| 772 | return EXIT_FAILURE; |
| 773 | } |
| 774 | break; |
| 775 | #endif |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 776 | case 'h': |
| 777 | usage(argv[0]); |
| 778 | return 0; |
| 779 | case '?': |
| 780 | g_print("Unknown option, try '%s --help' for more information.\n", |
| 781 | argv[0]); |
| 782 | return EXIT_FAILURE; |
| 783 | } |
| 784 | } |
| 785 | |
Michael Roth | f789aa7 | 2012-04-18 16:28:01 -0500 | [diff] [blame] | 786 | s = g_malloc0(sizeof(GAState)); |
| 787 | s->log_level = log_level; |
| 788 | s->log_file = stderr; |
| 789 | g_log_set_default_handler(ga_log, s); |
| 790 | g_log_set_fatal_mask(NULL, G_LOG_LEVEL_ERROR); |
| 791 | ga_enable_logging(s); |
| 792 | s->state_filepath_isfrozen = g_strdup_printf("%s/qga.state.isfrozen", |
| 793 | state_dir); |
| 794 | s->frozen = false; |
Michael Roth | d8ca685 | 2012-01-19 22:04:34 -0600 | [diff] [blame] | 795 | #ifndef _WIN32 |
Michael Roth | f789aa7 | 2012-04-18 16:28:01 -0500 | [diff] [blame] | 796 | /* check if a previous instance of qemu-ga exited with filesystems' state |
| 797 | * marked as frozen. this could be a stale value (a non-qemu-ga process |
| 798 | * or reboot may have since unfrozen them), but better to require an |
| 799 | * uneeded unfreeze than to risk hanging on start-up |
| 800 | */ |
| 801 | struct stat st; |
| 802 | if (stat(s->state_filepath_isfrozen, &st) == -1) { |
| 803 | /* it's okay if the file doesn't exist, but if we can't access for |
| 804 | * some other reason, such as permissions, there's a configuration |
| 805 | * that needs to be addressed. so just bail now before we get into |
| 806 | * more trouble later |
| 807 | */ |
| 808 | if (errno != ENOENT) { |
| 809 | g_critical("unable to access state file at path %s: %s", |
| 810 | s->state_filepath_isfrozen, strerror(errno)); |
| 811 | return EXIT_FAILURE; |
| 812 | } |
| 813 | } else { |
| 814 | g_warning("previous instance appears to have exited with frozen" |
| 815 | " filesystems. deferring logging/pidfile creation and" |
| 816 | " disabling non-fsfreeze-safe commands until" |
| 817 | " guest-fsfreeze-thaw is issued, or filesystems are" |
| 818 | " manually unfrozen and the file %s is removed", |
| 819 | s->state_filepath_isfrozen); |
| 820 | s->frozen = true; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 821 | } |
Michael Roth | d8ca685 | 2012-01-19 22:04:34 -0600 | [diff] [blame] | 822 | #endif |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 823 | |
Michael Roth | f789aa7 | 2012-04-18 16:28:01 -0500 | [diff] [blame] | 824 | if (ga_is_frozen(s)) { |
| 825 | if (daemonize) { |
| 826 | /* delay opening/locking of pidfile till filesystem are unfrozen */ |
| 827 | s->deferred_options.pid_filepath = pid_filepath; |
| 828 | become_daemon(NULL); |
| 829 | } |
| 830 | if (log_filepath) { |
| 831 | /* delay opening the log file till filesystems are unfrozen */ |
| 832 | s->deferred_options.log_filepath = log_filepath; |
| 833 | } |
| 834 | ga_disable_logging(s); |
| 835 | ga_disable_non_whitelisted(); |
| 836 | } else { |
| 837 | if (daemonize) { |
| 838 | become_daemon(pid_filepath); |
| 839 | } |
| 840 | if (log_filepath) { |
Michael Roth | 6c615ec | 2012-05-14 16:42:35 -0500 | [diff] [blame] | 841 | FILE *log_file = fopen(log_filepath, "a"); |
| 842 | if (!log_file) { |
Michael Roth | f789aa7 | 2012-04-18 16:28:01 -0500 | [diff] [blame] | 843 | g_critical("unable to open specified log file: %s", |
| 844 | strerror(errno)); |
| 845 | goto out_bad; |
| 846 | } |
Michael Roth | 6c615ec | 2012-05-14 16:42:35 -0500 | [diff] [blame] | 847 | s->log_file = log_file; |
Michael Roth | f789aa7 | 2012-04-18 16:28:01 -0500 | [diff] [blame] | 848 | } |
| 849 | } |
| 850 | |
Michael Roth | f22d85e | 2012-04-17 19:01:45 -0500 | [diff] [blame] | 851 | if (blacklist) { |
| 852 | s->blacklist = blacklist; |
| 853 | do { |
| 854 | g_debug("disabling command: %s", (char *)blacklist->data); |
| 855 | qmp_disable_command(blacklist->data); |
| 856 | blacklist = g_list_next(blacklist); |
| 857 | } while (blacklist); |
| 858 | } |
Michael Roth | e3d4d25 | 2011-07-19 15:41:55 -0500 | [diff] [blame] | 859 | s->command_state = ga_command_state_new(); |
| 860 | ga_command_state_init(s, s->command_state); |
| 861 | ga_command_state_init_all(s->command_state); |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 862 | json_message_parser_init(&s->parser, process_event); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 863 | ga_state = s; |
Michael Roth | d8ca685 | 2012-01-19 22:04:34 -0600 | [diff] [blame] | 864 | #ifndef _WIN32 |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 865 | if (!register_signal_handlers()) { |
| 866 | g_critical("failed to register signal handlers"); |
| 867 | goto out_bad; |
| 868 | } |
Michael Roth | d8ca685 | 2012-01-19 22:04:34 -0600 | [diff] [blame] | 869 | #endif |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 870 | |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 871 | s->main_loop = g_main_loop_new(NULL, false); |
| 872 | if (!channel_init(ga_state, method, path)) { |
| 873 | g_critical("failed to initialize guest agent channel"); |
| 874 | goto out_bad; |
| 875 | } |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 876 | #ifndef _WIN32 |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 877 | g_main_loop_run(ga_state->main_loop); |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 878 | #else |
| 879 | if (daemonize) { |
| 880 | SERVICE_TABLE_ENTRY service_table[] = { |
| 881 | { (char *)QGA_SERVICE_NAME, service_main }, { NULL, NULL } }; |
| 882 | StartServiceCtrlDispatcher(service_table); |
| 883 | } else { |
| 884 | g_main_loop_run(ga_state->main_loop); |
| 885 | } |
| 886 | #endif |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 887 | |
Michael Roth | e3d4d25 | 2011-07-19 15:41:55 -0500 | [diff] [blame] | 888 | ga_command_state_cleanup_all(ga_state->command_state); |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 889 | ga_channel_free(ga_state->channel); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 890 | |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 891 | if (daemonize) { |
Michael Roth | f789aa7 | 2012-04-18 16:28:01 -0500 | [diff] [blame] | 892 | unlink(pid_filepath); |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 893 | } |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 894 | return 0; |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 895 | |
| 896 | out_bad: |
| 897 | if (daemonize) { |
Michael Roth | f789aa7 | 2012-04-18 16:28:01 -0500 | [diff] [blame] | 898 | unlink(pid_filepath); |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 899 | } |
| 900 | return EXIT_FAILURE; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 901 | } |