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