Statistics
| Branch: | Revision:

root / qemu-ga.c @ 31a32289

History | View | Annotate | Download (24.7 kB)

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