root / qga / main.c @ c5dcb6ae
History | View | Annotate | Download (26.3 kB)
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> |
17 |
#include <getopt.h> |
18 |
#ifndef _WIN32
|
19 |
#include <syslog.h> |
20 |
#include <sys/wait.h> |
21 |
#include <sys/stat.h> |
22 |
#endif
|
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" |
27 |
#include "qga/guest-agent-core.h" |
28 |
#include "qemu/module.h" |
29 |
#include "signal.h" |
30 |
#include "qapi/qmp/qerror.h" |
31 |
#include "qapi/qmp/dispatch.h" |
32 |
#include "qga/channel.h" |
33 |
#ifdef _WIN32
|
34 |
#include "qga/service-win32.h" |
35 |
#include <windows.h> |
36 |
#endif
|
37 |
#ifdef __linux__
|
38 |
#include <linux/fs.h> |
39 |
#ifdef FIFREEZE
|
40 |
#define CONFIG_FSFREEZE
|
41 |
#endif
|
42 |
#endif
|
43 |
|
44 |
#ifndef _WIN32
|
45 |
#define QGA_VIRTIO_PATH_DEFAULT "/dev/virtio-ports/org.qemu.guest_agent.0" |
46 |
#else
|
47 |
#define QGA_VIRTIO_PATH_DEFAULT "\\\\.\\Global\\org.qemu.guest_agent.0" |
48 |
#endif
|
49 |
#define QGA_STATEDIR_DEFAULT CONFIG_QEMU_LOCALSTATEDIR "/run" |
50 |
#define QGA_PIDFILE_DEFAULT QGA_STATEDIR_DEFAULT "/qemu-ga.pid" |
51 |
#ifdef CONFIG_FSFREEZE
|
52 |
#define QGA_FSFREEZE_HOOK_DEFAULT CONFIG_QEMU_CONFDIR "/fsfreeze-hook" |
53 |
#endif
|
54 |
#define QGA_SENTINEL_BYTE 0xFF |
55 |
|
56 |
struct GAState {
|
57 |
JSONMessageParser parser; |
58 |
GMainLoop *main_loop; |
59 |
GAChannel *channel; |
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;
|
65 |
#ifdef _WIN32
|
66 |
GAService service; |
67 |
#endif
|
68 |
bool delimit_response;
|
69 |
bool frozen;
|
70 |
GList *blacklist; |
71 |
const char *state_filepath_isfrozen; |
72 |
struct {
|
73 |
const char *log_filepath; |
74 |
const char *pid_filepath; |
75 |
} deferred_options; |
76 |
#ifdef CONFIG_FSFREEZE
|
77 |
const char *fsfreeze_hook; |
78 |
#endif
|
79 |
}; |
80 |
|
81 |
struct GAState *ga_state;
|
82 |
|
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-sync-delimited",
|
89 |
"guest-fsfreeze-status",
|
90 |
"guest-fsfreeze-thaw",
|
91 |
NULL
|
92 |
}; |
93 |
|
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 |
|
100 |
static void quit_handler(int sig) |
101 |
{ |
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 |
} |
111 |
g_debug("received signal num %d, quitting", sig);
|
112 |
|
113 |
if (g_main_loop_is_running(ga_state->main_loop)) {
|
114 |
g_main_loop_quit(ga_state->main_loop); |
115 |
} |
116 |
} |
117 |
|
118 |
#ifndef _WIN32
|
119 |
static gboolean register_signal_handlers(void) |
120 |
{ |
121 |
struct sigaction sigact;
|
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));
|
130 |
} |
131 |
ret = sigaction(SIGTERM, &sigact, NULL);
|
132 |
if (ret == -1) { |
133 |
g_error("error configuring signal handler: %s", strerror(errno));
|
134 |
} |
135 |
|
136 |
return true; |
137 |
} |
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 |
} |
155 |
#endif
|
156 |
|
157 |
static void usage(const char *cmd) |
158 |
{ |
159 |
printf( |
160 |
"Usage: %s [-m <method> -p <path>] [<options>]\n"
|
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"
|
165 |
" -p, --path device/socket path (the default for virtio-serial is:\n"
|
166 |
" %s)\n"
|
167 |
" -l, --logfile set logfile path, logs to stderr by default\n"
|
168 |
" -f, --pidfile specify pidfile (default is %s)\n"
|
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
|
179 |
" -t, --statedir specify dir to store state information (absolute paths\n"
|
180 |
" only, default is %s)\n"
|
181 |
" -v, --verbose log extra debugging information\n"
|
182 |
" -V, --version print version information and exit\n"
|
183 |
" -d, --daemonize become a daemon\n"
|
184 |
#ifdef _WIN32
|
185 |
" -s, --service service commands: install, uninstall\n"
|
186 |
#endif
|
187 |
" -b, --blacklist comma-separated list of RPCs to disable (no spaces, \"?\"\n"
|
188 |
" to list available RPCs)\n"
|
189 |
" -h, --help display this help and exit\n"
|
190 |
"\n"
|
191 |
"Report bugs to <mdroth@linux.vnet.ibm.com>\n"
|
192 |
, cmd, QEMU_VERSION, QGA_VIRTIO_PATH_DEFAULT, QGA_PIDFILE_DEFAULT, |
193 |
#ifdef CONFIG_FSFREEZE
|
194 |
QGA_FSFREEZE_HOOK_DEFAULT, |
195 |
#endif
|
196 |
QGA_STATEDIR_DEFAULT); |
197 |
} |
198 |
|
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; |
246 |
#ifndef _WIN32
|
247 |
if (domain && strcmp(domain, "syslog") == 0) { |
248 |
syslog(LOG_INFO, "%s: %s", level_str, msg);
|
249 |
} else if (level & s->log_level) { |
250 |
#else
|
251 |
if (level & s->log_level) {
|
252 |
#endif
|
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 |
|
260 |
void ga_set_response_delimited(GAState *s)
|
261 |
{ |
262 |
s->delimit_response = true;
|
263 |
} |
264 |
|
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 |
|
278 |
#ifndef _WIN32
|
279 |
static bool ga_open_pidfile(const char *pidfile) |
280 |
{ |
281 |
int pidfd;
|
282 |
char pidstr[32]; |
283 |
|
284 |
pidfd = qemu_open(pidfile, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR); |
285 |
if (pidfd == -1 || lockf(pidfd, F_TLOCK, 0)) { |
286 |
g_critical("Cannot lock pid file, %s", strerror(errno));
|
287 |
if (pidfd != -1) { |
288 |
close(pidfd); |
289 |
} |
290 |
return false; |
291 |
} |
292 |
|
293 |
if (ftruncate(pidfd, 0)) { |
294 |
g_critical("Failed to truncate pid file");
|
295 |
goto fail;
|
296 |
} |
297 |
snprintf(pidstr, sizeof(pidstr), "%d\n", getpid()); |
298 |
if (write(pidfd, pidstr, strlen(pidstr)) != strlen(pidstr)) {
|
299 |
g_critical("Failed to write pid file");
|
300 |
goto fail;
|
301 |
} |
302 |
|
303 |
/* keep pidfile open & locked forever */
|
304 |
return true; |
305 |
|
306 |
fail:
|
307 |
unlink(pidfile); |
308 |
close(pidfd); |
309 |
return false; |
310 |
} |
311 |
#else /* _WIN32 */ |
312 |
static bool ga_open_pidfile(const char *pidfile) |
313 |
{ |
314 |
return true; |
315 |
} |
316 |
#endif
|
317 |
|
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 |
|
350 |
/* [re-]enable all commands, except those explicitly blacklisted by user */
|
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 |
|
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 |
|
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;
|
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 |
} |
409 |
} |
410 |
|
411 |
void ga_unset_frozen(GAState *s)
|
412 |
{ |
413 |
if (!ga_is_frozen(s)) {
|
414 |
return;
|
415 |
} |
416 |
|
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) {
|
421 |
s->log_file = ga_open_logfile(s->deferred_options.log_filepath); |
422 |
if (!s->log_file) {
|
423 |
s->log_file = stderr; |
424 |
} |
425 |
s->deferred_options.log_filepath = NULL;
|
426 |
} |
427 |
ga_enable_logging(s); |
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 |
} |
435 |
|
436 |
/* enable all disabled, non-blacklisted commands */
|
437 |
ga_enable_non_blacklisted(s->blacklist); |
438 |
s->frozen = false;
|
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 |
} |
443 |
} |
444 |
|
445 |
#ifdef CONFIG_FSFREEZE
|
446 |
const char *ga_fsfreeze_hook(GAState *s) |
447 |
{ |
448 |
return s->fsfreeze_hook;
|
449 |
} |
450 |
#endif
|
451 |
|
452 |
static void become_daemon(const char *pidfile) |
453 |
{ |
454 |
#ifndef _WIN32
|
455 |
pid_t pid, sid; |
456 |
|
457 |
pid = fork(); |
458 |
if (pid < 0) { |
459 |
exit(EXIT_FAILURE); |
460 |
} |
461 |
if (pid > 0) { |
462 |
exit(EXIT_SUCCESS); |
463 |
} |
464 |
|
465 |
if (pidfile) {
|
466 |
if (!ga_open_pidfile(pidfile)) {
|
467 |
g_critical("failed to create pidfile");
|
468 |
exit(EXIT_FAILURE); |
469 |
} |
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 |
|
481 |
reopen_fd_to_null(STDIN_FILENO); |
482 |
reopen_fd_to_null(STDOUT_FILENO); |
483 |
reopen_fd_to_null(STDERR_FILENO); |
484 |
return;
|
485 |
|
486 |
fail:
|
487 |
if (pidfile) {
|
488 |
unlink(pidfile); |
489 |
} |
490 |
g_critical("failed to daemonize");
|
491 |
exit(EXIT_FAILURE); |
492 |
#endif
|
493 |
} |
494 |
|
495 |
static int send_response(GAState *s, QObject *payload) |
496 |
{ |
497 |
const char *buf; |
498 |
QString *payload_qstr, *response_qstr; |
499 |
GIOStatus status; |
500 |
|
501 |
g_assert(payload && s->channel); |
502 |
|
503 |
payload_qstr = qobject_to_json(payload); |
504 |
if (!payload_qstr) {
|
505 |
return -EINVAL;
|
506 |
} |
507 |
|
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); |
520 |
status = ga_channel_write_all(s->channel, buf, strlen(buf)); |
521 |
QDECREF(response_qstr); |
522 |
if (status != G_IO_STATUS_NORMAL) {
|
523 |
return -EIO;
|
524 |
} |
525 |
|
526 |
return 0; |
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) {
|
538 |
ret = send_response(s, rsp); |
539 |
if (ret) {
|
540 |
g_warning("error sending response: %s", strerror(ret));
|
541 |
} |
542 |
qobject_decref(rsp); |
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 |
} |
568 |
qdict_put_obj(qdict, "error", qmp_build_error_object(err));
|
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); |
585 |
qdict_put_obj(qdict, "error", qmp_build_error_object(err));
|
586 |
error_free(err); |
587 |
} |
588 |
ret = send_response(s, QOBJECT(qdict)); |
589 |
if (ret) {
|
590 |
g_warning("error sending error response: %s", strerror(ret));
|
591 |
} |
592 |
} |
593 |
|
594 |
QDECREF(qdict); |
595 |
} |
596 |
|
597 |
/* false return signals GAChannel to close the current client connection */
|
598 |
static gboolean channel_event_cb(GIOCondition condition, gpointer data)
|
599 |
{ |
600 |
GAState *s = data; |
601 |
gchar buf[QGA_READ_COUNT_DEFAULT+1];
|
602 |
gsize count; |
603 |
GError *err = NULL;
|
604 |
GIOStatus status = ga_channel_read(s->channel, buf, QGA_READ_COUNT_DEFAULT, &count); |
605 |
if (err != NULL) { |
606 |
g_warning("error reading channel: %s", err->message);
|
607 |
g_error_free(err); |
608 |
return false; |
609 |
} |
610 |
switch (status) {
|
611 |
case G_IO_STATUS_ERROR:
|
612 |
g_warning("error reading channel");
|
613 |
return false; |
614 |
case G_IO_STATUS_NORMAL:
|
615 |
buf[count] = 0;
|
616 |
g_debug("read data, count: %d, data: %s", (int)count, buf); |
617 |
json_message_parser_feed(&s->parser, (char *)buf, (int)count); |
618 |
break;
|
619 |
case G_IO_STATUS_EOF:
|
620 |
g_debug("received EOF");
|
621 |
if (!s->virtio) {
|
622 |
return false; |
623 |
} |
624 |
/* fall through */
|
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; |
633 |
default:
|
634 |
g_warning("unknown channel read status, closing");
|
635 |
return false; |
636 |
} |
637 |
return true; |
638 |
} |
639 |
|
640 |
static gboolean channel_init(GAState *s, const gchar *method, const gchar *path) |
641 |
{ |
642 |
GAChannelMethod channel_method; |
643 |
|
644 |
if (method == NULL) { |
645 |
method = "virtio-serial";
|
646 |
} |
647 |
|
648 |
if (path == NULL) { |
649 |
if (strcmp(method, "virtio-serial") != 0) { |
650 |
g_critical("must specify a path for this channel");
|
651 |
return false; |
652 |
} |
653 |
/* try the default path for the virtio-serial port */
|
654 |
path = QGA_VIRTIO_PATH_DEFAULT; |
655 |
} |
656 |
|
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; |
664 |
} else {
|
665 |
g_critical("unsupported channel method/type: %s", method);
|
666 |
return false; |
667 |
} |
668 |
|
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; |
676 |
} |
677 |
|
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 |
|
728 |
int main(int argc, char **argv) |
729 |
{ |
730 |
const char *sopt = "hVvdm:p:l:f:F::b:s:t:"; |
731 |
const char *method = NULL, *path = NULL; |
732 |
const char *log_filepath = NULL; |
733 |
const char *pid_filepath = QGA_PIDFILE_DEFAULT; |
734 |
#ifdef CONFIG_FSFREEZE
|
735 |
const char *fsfreeze_hook = NULL; |
736 |
#endif
|
737 |
const char *state_dir = QGA_STATEDIR_DEFAULT; |
738 |
#ifdef _WIN32
|
739 |
const char *service = NULL; |
740 |
#endif
|
741 |
const struct option lopt[] = { |
742 |
{ "help", 0, NULL, 'h' }, |
743 |
{ "version", 0, NULL, 'V' }, |
744 |
{ "logfile", 1, NULL, 'l' }, |
745 |
{ "pidfile", 1, NULL, 'f' }, |
746 |
#ifdef CONFIG_FSFREEZE
|
747 |
{ "fsfreeze-hook", 2, NULL, 'F' }, |
748 |
#endif
|
749 |
{ "verbose", 0, NULL, 'v' }, |
750 |
{ "method", 1, NULL, 'm' }, |
751 |
{ "path", 1, NULL, 'p' }, |
752 |
{ "daemonize", 0, NULL, 'd' }, |
753 |
{ "blacklist", 1, NULL, 'b' }, |
754 |
#ifdef _WIN32
|
755 |
{ "service", 1, NULL, 's' }, |
756 |
#endif
|
757 |
{ "statedir", 1, NULL, 't' }, |
758 |
{ NULL, 0, NULL, 0 } |
759 |
}; |
760 |
int opt_ind = 0, ch, daemonize = 0, i, j, len; |
761 |
GLogLevelFlags log_level = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL; |
762 |
GList *blacklist = NULL;
|
763 |
GAState *s; |
764 |
|
765 |
module_call_init(MODULE_INIT_QAPI); |
766 |
|
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': |
776 |
log_filepath = optarg; |
777 |
break;
|
778 |
case 'f': |
779 |
pid_filepath = optarg; |
780 |
break;
|
781 |
#ifdef CONFIG_FSFREEZE
|
782 |
case 'F': |
783 |
fsfreeze_hook = optarg ? optarg : QGA_FSFREEZE_HOOK_DEFAULT; |
784 |
break;
|
785 |
#endif
|
786 |
case 't': |
787 |
state_dir = optarg; |
788 |
break;
|
789 |
case 'v': |
790 |
/* enable all log levels */
|
791 |
log_level = G_LOG_LEVEL_MASK; |
792 |
break;
|
793 |
case 'V': |
794 |
printf("QEMU Guest Agent %s\n", QEMU_VERSION);
|
795 |
return 0; |
796 |
case 'd': |
797 |
daemonize = 1;
|
798 |
break;
|
799 |
case 'b': { |
800 |
char **list_head, **list;
|
801 |
if (is_help_option(optarg)) {
|
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;
|
814 |
blacklist = g_list_append(blacklist, &optarg[j]); |
815 |
j = i + 1;
|
816 |
} |
817 |
} |
818 |
if (j < i) {
|
819 |
blacklist = g_list_append(blacklist, &optarg[j]); |
820 |
} |
821 |
break;
|
822 |
} |
823 |
#ifdef _WIN32
|
824 |
case 's': |
825 |
service = optarg; |
826 |
if (strcmp(service, "install") == 0) { |
827 |
return ga_install_service(path, log_filepath);
|
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
|
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 |
|
846 |
s = g_malloc0(sizeof(GAState));
|
847 |
s->log_level = log_level; |
848 |
s->log_file = stderr; |
849 |
#ifdef CONFIG_FSFREEZE
|
850 |
s->fsfreeze_hook = fsfreeze_hook; |
851 |
#endif
|
852 |
g_log_set_default_handler(ga_log, s); |
853 |
g_log_set_fatal_mask(NULL, G_LOG_LEVEL_ERROR);
|
854 |
ga_enable_logging(s); |
855 |
s->state_filepath_isfrozen = g_strdup_printf("%s/qga.state.isfrozen",
|
856 |
state_dir); |
857 |
s->frozen = false;
|
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) {
|
904 |
FILE *log_file = ga_open_logfile(log_filepath); |
905 |
if (!log_file) {
|
906 |
g_critical("unable to open specified log file: %s",
|
907 |
strerror(errno)); |
908 |
goto out_bad;
|
909 |
} |
910 |
s->log_file = log_file; |
911 |
} |
912 |
} |
913 |
|
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 |
} |
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); |
925 |
json_message_parser_init(&s->parser, process_event); |
926 |
ga_state = s; |
927 |
#ifndef _WIN32
|
928 |
if (!register_signal_handlers()) {
|
929 |
g_critical("failed to register signal handlers");
|
930 |
goto out_bad;
|
931 |
} |
932 |
#endif
|
933 |
|
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 |
} |
939 |
#ifndef _WIN32
|
940 |
g_main_loop_run(ga_state->main_loop); |
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
|
950 |
|
951 |
ga_command_state_cleanup_all(ga_state->command_state); |
952 |
ga_channel_free(ga_state->channel); |
953 |
|
954 |
if (daemonize) {
|
955 |
unlink(pid_filepath); |
956 |
} |
957 |
return 0; |
958 |
|
959 |
out_bad:
|
960 |
if (daemonize) {
|
961 |
unlink(pid_filepath); |
962 |
} |
963 |
return EXIT_FAILURE;
|
964 |
} |