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