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