Statistics
| Branch: | Revision:

root / qemu-ga.c @ 60b46aa2

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