2011-07-21 00:19:37 +04:00
|
|
|
/*
|
|
|
|
* QEMU Guest Agent
|
|
|
|
*
|
|
|
|
* Copyright IBM Corp. 2011
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Adam Litke <aglitke@linux.vnet.ibm.com>
|
|
|
|
* Michael Roth <mdroth@linux.vnet.ibm.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*/
|
2018-02-01 14:18:31 +03:00
|
|
|
|
2016-01-29 20:49:58 +03:00
|
|
|
#include "qemu/osdep.h"
|
2011-07-21 00:19:37 +04:00
|
|
|
#include <getopt.h>
|
2013-03-01 21:40:27 +04:00
|
|
|
#include <glib/gstdio.h>
|
2012-01-20 08:04:34 +04:00
|
|
|
#ifndef _WIN32
|
2011-07-21 00:19:37 +04:00
|
|
|
#include <syslog.h>
|
qemu-ga: add guest-suspend-disk
As the command name implies, this command suspends the guest to disk.
The suspend operation is implemented by two functions: bios_supports_mode()
and guest_suspend(). Both functions are generic enough to be used by
other suspend modes (introduced by next commits).
Both functions will try to use the scripts provided by the pm-utils
package if it's available. If it's not available, a manual method,
which consists of directly writing to '/sys/power/state', will be used.
To reap terminated children, a new signal handler is installed in the
parent to catch SIGCHLD signals and a non-blocking call to waitpid()
is done to collect their exit statuses. The statuses, however, are
discarded.
The approach used to query the guest for suspend support deserves some
explanation. It's implemented by bios_supports_mode() and shown below:
qemu-ga
|
create pipe
|
fork()
-----------------
| |
| |
| fork()
| --------------------------
| | |
| | |
| | exec('pm-is-supported')
| |
| wait()
| write exit status to pipe
| exit
|
read pipe
This might look complex, but the resulting code is quite simple.
The purpose of that approach is to allow qemu-ga to reap its children
(semi-)automatically from its SIGCHLD handler.
Implementing this the obvious way, that's, doing the exec() call from
the first child process, would force us to introduce a more complex way
to reap qemu-ga's children. Like registering PIDs to be reaped and
having a way to wait for them when returning their exit status to
qemu-ga is necessary. The approach explained above avoids that complexity.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2012-02-28 18:03:03 +04:00
|
|
|
#include <sys/wait.h>
|
2012-01-20 08:04:34 +04:00
|
|
|
#endif
|
2022-04-20 16:25:49 +03:00
|
|
|
#include "qemu/help-texts.h"
|
2018-08-23 19:40:20 +03:00
|
|
|
#include "qapi/qmp/json-parser.h"
|
2018-02-01 14:18:39 +03:00
|
|
|
#include "qapi/qmp/qdict.h"
|
2012-12-17 21:19:43 +04:00
|
|
|
#include "qapi/qmp/qjson.h"
|
2018-05-03 22:50:57 +03:00
|
|
|
#include "guest-agent-core.h"
|
2019-11-20 21:25:48 +03:00
|
|
|
#include "qga-qapi-init-commands.h"
|
2012-12-17 21:19:43 +04:00
|
|
|
#include "qapi/qmp/qerror.h"
|
2018-02-01 14:18:31 +03:00
|
|
|
#include "qapi/error.h"
|
2018-05-03 22:50:57 +03:00
|
|
|
#include "channel.h"
|
2020-08-18 12:59:00 +03:00
|
|
|
#include "qemu/cutils.h"
|
2016-03-20 20:16:19 +03:00
|
|
|
#include "qemu/help_option.h"
|
2017-01-06 18:29:30 +03:00
|
|
|
#include "qemu/sockets.h"
|
2017-03-16 18:29:45 +03:00
|
|
|
#include "qemu/systemd.h"
|
2017-08-03 19:33:52 +03:00
|
|
|
#include "qemu-version.h"
|
2012-01-22 02:42:27 +04:00
|
|
|
#ifdef _WIN32
|
2018-10-07 14:02:23 +03:00
|
|
|
#include <dbt.h>
|
2012-01-22 02:42:27 +04:00
|
|
|
#include "qga/service-win32.h"
|
2013-08-07 19:40:32 +04:00
|
|
|
#include "qga/vss-win32.h"
|
2012-01-22 02:42:27 +04:00
|
|
|
#endif
|
2012-12-12 07:55:55 +04:00
|
|
|
#ifdef __linux__
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#ifdef FIFREEZE
|
|
|
|
#define CONFIG_FSFREEZE
|
|
|
|
#endif
|
|
|
|
#endif
|
2011-07-21 00:19:37 +04:00
|
|
|
|
qemu-ga: add initial win32 support
This adds a win32 channel implementation that makes qemu-ga functional
on Windows using virtio-serial (unix-listen/isa-serial not currently
implemented). Unlike with the posix implementation, we do not use
GIOChannel for the following reasons:
- glib calls stat() on an fd to check whether S_IFCHR is set, which is
the case for virtio-serial on win32. Because of that, a one-time
check to determine whether the channel is readable is done by making
a call to PeekConsoleInput(), which reports the underlying handle is
not a valid console handle, and thus we can never read from the
channel.
- if one goes as far as to "trick" glib into thinking it is a normal
file descripter, the buffering is done in such a way that data
written to the output stream will subsequently result in that same
data being read back as if it were input, causing an error loop.
furthermore, a forced flush of the channel only moves the data into a
secondary buffer managed by glib, so there's no way to prevent output
from getting read back as input.
The implementation here ties into the glib main loop by implementing a
custom GSource that continually submits asynchronous/overlapped I/O to
fill an GAChannel-managed read buffer, and tells glib to poll the
corresponding event handle for a completion whenever there is no
data/RPC in the read buffer to notify the main application about.
2012-01-21 05:01:30 +04:00
|
|
|
#ifndef _WIN32
|
2011-07-21 00:19:37 +04:00
|
|
|
#define QGA_VIRTIO_PATH_DEFAULT "/dev/virtio-ports/org.qemu.guest_agent.0"
|
2013-05-18 08:31:49 +04:00
|
|
|
#define QGA_STATE_RELATIVE_DIR "run"
|
2014-01-15 13:33:44 +04:00
|
|
|
#define QGA_SERIAL_PATH_DEFAULT "/dev/ttyS0"
|
qemu-ga: add initial win32 support
This adds a win32 channel implementation that makes qemu-ga functional
on Windows using virtio-serial (unix-listen/isa-serial not currently
implemented). Unlike with the posix implementation, we do not use
GIOChannel for the following reasons:
- glib calls stat() on an fd to check whether S_IFCHR is set, which is
the case for virtio-serial on win32. Because of that, a one-time
check to determine whether the channel is readable is done by making
a call to PeekConsoleInput(), which reports the underlying handle is
not a valid console handle, and thus we can never read from the
channel.
- if one goes as far as to "trick" glib into thinking it is a normal
file descripter, the buffering is done in such a way that data
written to the output stream will subsequently result in that same
data being read back as if it were input, causing an error loop.
furthermore, a forced flush of the channel only moves the data into a
secondary buffer managed by glib, so there's no way to prevent output
from getting read back as input.
The implementation here ties into the glib main loop by implementing a
custom GSource that continually submits asynchronous/overlapped I/O to
fill an GAChannel-managed read buffer, and tells glib to poll the
corresponding event handle for a completion whenever there is no
data/RPC in the read buffer to notify the main application about.
2012-01-21 05:01:30 +04:00
|
|
|
#else
|
|
|
|
#define QGA_VIRTIO_PATH_DEFAULT "\\\\.\\Global\\org.qemu.guest_agent.0"
|
2013-05-18 08:31:49 +04:00
|
|
|
#define QGA_STATE_RELATIVE_DIR "qemu-ga"
|
2014-01-15 13:33:44 +04:00
|
|
|
#define QGA_SERIAL_PATH_DEFAULT "COM1"
|
qemu-ga: add initial win32 support
This adds a win32 channel implementation that makes qemu-ga functional
on Windows using virtio-serial (unix-listen/isa-serial not currently
implemented). Unlike with the posix implementation, we do not use
GIOChannel for the following reasons:
- glib calls stat() on an fd to check whether S_IFCHR is set, which is
the case for virtio-serial on win32. Because of that, a one-time
check to determine whether the channel is readable is done by making
a call to PeekConsoleInput(), which reports the underlying handle is
not a valid console handle, and thus we can never read from the
channel.
- if one goes as far as to "trick" glib into thinking it is a normal
file descripter, the buffering is done in such a way that data
written to the output stream will subsequently result in that same
data being read back as if it were input, causing an error loop.
furthermore, a forced flush of the channel only moves the data into a
secondary buffer managed by glib, so there's no way to prevent output
from getting read back as input.
The implementation here ties into the glib main loop by implementing a
custom GSource that continually submits asynchronous/overlapped I/O to
fill an GAChannel-managed read buffer, and tells glib to poll the
corresponding event handle for a completion whenever there is no
data/RPC in the read buffer to notify the main application about.
2012-01-21 05:01:30 +04:00
|
|
|
#endif
|
2012-12-12 07:55:55 +04:00
|
|
|
#ifdef CONFIG_FSFREEZE
|
|
|
|
#define QGA_FSFREEZE_HOOK_DEFAULT CONFIG_QEMU_CONFDIR "/fsfreeze-hook"
|
|
|
|
#endif
|
2012-02-07 23:56:48 +04:00
|
|
|
#define QGA_SENTINEL_BYTE 0xFF
|
2015-08-27 02:34:57 +03:00
|
|
|
#define QGA_CONF_DEFAULT CONFIG_QEMU_CONFDIR G_DIR_SEPARATOR_S "qemu-ga.conf"
|
2018-10-07 14:02:20 +03:00
|
|
|
#define QGA_RETRY_INTERVAL 5
|
2011-07-21 00:19:37 +04:00
|
|
|
|
2013-05-18 08:31:49 +04:00
|
|
|
static struct {
|
|
|
|
const char *state_dir;
|
|
|
|
const char *pidfile;
|
|
|
|
} dfl_pathnames;
|
|
|
|
|
2013-03-01 21:40:27 +04:00
|
|
|
typedef struct GAPersistentState {
|
|
|
|
#define QGA_PSTATE_DEFAULT_FD_COUNTER 1000
|
|
|
|
int64_t fd_counter;
|
|
|
|
} GAPersistentState;
|
|
|
|
|
2018-10-07 14:02:18 +03:00
|
|
|
typedef struct GAConfig GAConfig;
|
|
|
|
|
2011-07-21 00:19:37 +04:00
|
|
|
struct GAState {
|
|
|
|
JSONMessageParser parser;
|
|
|
|
GMainLoop *main_loop;
|
2012-01-19 10:18:20 +04:00
|
|
|
GAChannel *channel;
|
2011-07-21 00:19:37 +04:00
|
|
|
bool virtio; /* fastpath to check for virtio to deal with poll() quirks */
|
|
|
|
GACommandState *command_state;
|
|
|
|
GLogLevelFlags log_level;
|
|
|
|
FILE *log_file;
|
|
|
|
bool logging_enabled;
|
2012-01-22 02:42:27 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
GAService service;
|
2018-10-07 14:02:23 +03:00
|
|
|
HANDLE wakeup_event;
|
2012-01-22 02:42:27 +04:00
|
|
|
#endif
|
2012-02-07 23:56:48 +04:00
|
|
|
bool delimit_response;
|
2012-04-18 04:01:45 +04:00
|
|
|
bool frozen;
|
2022-07-27 12:21:34 +03:00
|
|
|
GList *blockedrpcs;
|
2015-08-27 02:34:56 +03:00
|
|
|
char *state_filepath_isfrozen;
|
qemu-ga: persist tracking of fsfreeze state via filesystem
Currently, qemu-ga may die/get killed/go away for whatever reason after
guest-fsfreeze-freeze has been issued, and before guest-fsfreeze-thaw
has been issued. This means the only way to unfreeze the guest is via
VNC/network/console access, but obtaining that access after-the-fact can
often be very difficult when filesystems are frozen. Logins will almost
always hang, for instance. In many cases the only recourse would be to
reboot the guest without any quiescing of volatile state, which makes
this a corner-case worth giving some attention to.
A likely failsafe for this situation would be to use a watchdog to
restart qemu-ga if it goes away. There are some precautions qemu-ga
needs to take in order to avoid immediately hanging itself on I/O,
however, namely, we must disable logging and defer to processing/creation
of user-specific logfiles, along with creation of the pid file if we're
running as a daemon. We also need to disable non-fsfreeze-safe commands,
as we normally would when processing the guest-fsfreeze-freeze command.
To track when we need to do this in a way that persists between multiple
invocations of qemu-ga, we create a file on the guest filesystem before
issuing the fsfreeze, and delete it when doing the thaw. On qemu-ga
startup, we check for the existance of this file to determine
the need to take the above precautions.
We're forced to do it this way since a more traditional approach such as
reading/writing state to a dedicated state file will cause
access/modification time updates, respectively, both of which will hang
if the file resides on a frozen filesystem. Both can occur even if
relatime is enabled. Checking for file existence will not update the
access time, however, so it's a safe way to check for fsfreeze state.
An actual watchdog-based restart of qemu-ga can itself cause an access
time update that would thus hang the invocation of qemu-ga, but the
logic to workaround that can be handled via the watchdog, so we don't
address that here (for relatime we'd periodically touch the qemu-ga
binary if the file $qga_statedir/qga.state.isfrozen is not present, this
avoids qemu-ga updates or the 1 day relatime threshold causing an
access-time update if we try to respawn qemu-ga shortly after it goes
away)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-04-19 01:28:01 +04:00
|
|
|
struct {
|
|
|
|
const char *log_filepath;
|
|
|
|
const char *pid_filepath;
|
|
|
|
} deferred_options;
|
2012-12-12 07:55:55 +04:00
|
|
|
#ifdef CONFIG_FSFREEZE
|
|
|
|
const char *fsfreeze_hook;
|
|
|
|
#endif
|
2015-08-27 02:34:56 +03:00
|
|
|
gchar *pstate_filepath;
|
2013-03-01 21:40:27 +04:00
|
|
|
GAPersistentState pstate;
|
2018-10-07 14:02:18 +03:00
|
|
|
GAConfig *config;
|
|
|
|
int socket_activation;
|
2018-10-07 14:02:20 +03:00
|
|
|
bool force_exit;
|
2011-07-21 00:19:37 +04:00
|
|
|
};
|
|
|
|
|
2012-02-07 23:56:48 +04:00
|
|
|
struct GAState *ga_state;
|
2017-03-03 15:32:25 +03:00
|
|
|
QmpCommandList ga_commands;
|
2011-07-21 00:19:37 +04:00
|
|
|
|
2012-04-18 04:01:45 +04:00
|
|
|
/* commands that are safe to issue while filesystems are frozen */
|
2022-07-27 12:21:34 +03:00
|
|
|
static const char *ga_freeze_allowlist[] = {
|
2012-04-18 04:01:45 +04:00
|
|
|
"guest-ping",
|
|
|
|
"guest-info",
|
|
|
|
"guest-sync",
|
2013-03-01 21:49:38 +04:00
|
|
|
"guest-sync-delimited",
|
2012-04-18 04:01:45 +04:00
|
|
|
"guest-fsfreeze-status",
|
|
|
|
"guest-fsfreeze-thaw",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2012-01-22 02:42:27 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
DWORD WINAPI service_ctrl_handler(DWORD ctrl, DWORD type, LPVOID data,
|
|
|
|
LPVOID ctx);
|
2018-10-07 14:02:23 +03:00
|
|
|
DWORD WINAPI handle_serial_device_events(DWORD type, LPVOID data);
|
2012-01-22 02:42:27 +04:00
|
|
|
VOID WINAPI service_main(DWORD argc, TCHAR *argv[]);
|
|
|
|
#endif
|
2018-10-07 14:02:19 +03:00
|
|
|
static int run_agent(GAState *s);
|
2018-10-07 14:02:20 +03:00
|
|
|
static void stop_agent(GAState *s, bool requested);
|
2012-01-22 02:42:27 +04:00
|
|
|
|
2013-05-18 08:31:49 +04:00
|
|
|
static void
|
|
|
|
init_dfl_pathnames(void)
|
|
|
|
{
|
2022-04-20 16:26:21 +03:00
|
|
|
g_autofree char *state = qemu_get_local_state_dir();
|
|
|
|
|
2013-05-18 08:31:49 +04:00
|
|
|
g_assert(dfl_pathnames.state_dir == NULL);
|
|
|
|
g_assert(dfl_pathnames.pidfile == NULL);
|
2022-04-20 16:26:21 +03:00
|
|
|
dfl_pathnames.state_dir = g_build_filename(state, QGA_STATE_RELATIVE_DIR, NULL);
|
|
|
|
dfl_pathnames.pidfile = g_build_filename(state, QGA_STATE_RELATIVE_DIR, "qemu-ga.pid", NULL);
|
2013-05-18 08:31:49 +04:00
|
|
|
}
|
|
|
|
|
2011-07-21 00:19:37 +04:00
|
|
|
static void quit_handler(int sig)
|
|
|
|
{
|
2012-04-18 04:01:45 +04:00
|
|
|
/* if we're frozen, don't exit unless we're absolutely forced to,
|
|
|
|
* because it's basically impossible for graceful exit to complete
|
|
|
|
* unless all log/pid files are on unfreezable filesystems. there's
|
|
|
|
* also a very likely chance killing the agent before unfreezing
|
|
|
|
* the filesystems is a mistake (or will be viewed as one later).
|
2017-04-11 10:50:36 +03:00
|
|
|
* On Windows the freeze interval is limited to 10 seconds, so
|
|
|
|
* we should quit, but first we should wait for the timeout, thaw
|
|
|
|
* the filesystem and quit.
|
2012-04-18 04:01:45 +04:00
|
|
|
*/
|
|
|
|
if (ga_is_frozen(ga_state)) {
|
2017-04-11 10:50:36 +03:00
|
|
|
#ifdef _WIN32
|
|
|
|
int i = 0;
|
|
|
|
Error *err = NULL;
|
|
|
|
HANDLE hEventTimeout;
|
|
|
|
|
|
|
|
g_debug("Thawing filesystems before exiting");
|
|
|
|
|
|
|
|
hEventTimeout = OpenEvent(EVENT_ALL_ACCESS, FALSE, EVENT_NAME_TIMEOUT);
|
|
|
|
if (hEventTimeout) {
|
|
|
|
WaitForSingleObject(hEventTimeout, 0);
|
|
|
|
CloseHandle(hEventTimeout);
|
|
|
|
}
|
2018-08-31 09:22:50 +03:00
|
|
|
qga_vss_fsfreeze(&i, false, NULL, &err);
|
2017-04-11 10:50:36 +03:00
|
|
|
if (err) {
|
|
|
|
g_debug("Error unfreezing filesystems prior to exiting: %s",
|
|
|
|
error_get_pretty(err));
|
|
|
|
error_free(err);
|
|
|
|
}
|
|
|
|
#else
|
2012-04-18 04:01:45 +04:00
|
|
|
return;
|
2017-04-11 10:50:36 +03:00
|
|
|
#endif
|
2012-04-18 04:01:45 +04:00
|
|
|
}
|
2011-08-28 23:45:40 +04:00
|
|
|
g_debug("received signal num %d, quitting", sig);
|
2011-07-21 00:19:37 +04:00
|
|
|
|
2018-10-07 14:02:20 +03:00
|
|
|
stop_agent(ga_state, true);
|
2011-07-21 00:19:37 +04:00
|
|
|
}
|
|
|
|
|
2012-01-22 02:42:27 +04:00
|
|
|
#ifndef _WIN32
|
2012-01-19 10:18:20 +04:00
|
|
|
static gboolean register_signal_handlers(void)
|
2011-07-21 00:19:37 +04:00
|
|
|
{
|
qemu-ga: guest-suspend: make the API synchronous
Currently, qemu-ga has a SIGCHLD handler that automatically reaps terminated
children processes. The idea is to avoid having qemu-ga commands blocked
waiting for children to terminate.
That approach has two problems:
1. qemu-ga is unable to detect errors in the child, meaning that qemu-ga
returns success even if the child fails to perform its task
2. if a command does depend on the child exit status, the command has to
play tricks to bypass the automatic reaper
Case 2 impacts the guest-suspend-* API, because it has to execute an external
program to check for suspend support. Today, to bypass the automatic reaper,
suspend code has to double fork and pass exit status information through a
pipe. Besides being complex, this is prone to race condition bugs. Indeed,
the current code does have such bugs.
Making the guest-suspend-* API synchronous (ie. by dropping the SIGCHLD
handler and calling waitpid() from commands) is a much simpler approach,
which fixes current race conditions bugs and enables commands to detect
errors in the child.
This commit does just that. There's a side effect though, guest-shutdown
will generate zombies if shutting down fails. This will be fixed by the
next commit.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-05-11 23:19:46 +04:00
|
|
|
struct sigaction sigact;
|
2011-07-21 00:19:37 +04:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
memset(&sigact, 0, sizeof(struct sigaction));
|
|
|
|
sigact.sa_handler = quit_handler;
|
|
|
|
|
|
|
|
ret = sigaction(SIGINT, &sigact, NULL);
|
|
|
|
if (ret == -1) {
|
|
|
|
g_error("error configuring signal handler: %s", strerror(errno));
|
|
|
|
}
|
|
|
|
ret = sigaction(SIGTERM, &sigact, NULL);
|
|
|
|
if (ret == -1) {
|
|
|
|
g_error("error configuring signal handler: %s", strerror(errno));
|
|
|
|
}
|
qemu-ga: add guest-suspend-disk
As the command name implies, this command suspends the guest to disk.
The suspend operation is implemented by two functions: bios_supports_mode()
and guest_suspend(). Both functions are generic enough to be used by
other suspend modes (introduced by next commits).
Both functions will try to use the scripts provided by the pm-utils
package if it's available. If it's not available, a manual method,
which consists of directly writing to '/sys/power/state', will be used.
To reap terminated children, a new signal handler is installed in the
parent to catch SIGCHLD signals and a non-blocking call to waitpid()
is done to collect their exit statuses. The statuses, however, are
discarded.
The approach used to query the guest for suspend support deserves some
explanation. It's implemented by bios_supports_mode() and shown below:
qemu-ga
|
create pipe
|
fork()
-----------------
| |
| |
| fork()
| --------------------------
| | |
| | |
| | exec('pm-is-supported')
| |
| wait()
| write exit status to pipe
| exit
|
read pipe
This might look complex, but the resulting code is quite simple.
The purpose of that approach is to allow qemu-ga to reap its children
(semi-)automatically from its SIGCHLD handler.
Implementing this the obvious way, that's, doing the exec() call from
the first child process, would force us to introduce a more complex way
to reap qemu-ga's children. Like registering PIDs to be reaped and
having a way to wait for them when returning their exit status to
qemu-ga is necessary. The approach explained above avoids that complexity.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2012-02-28 18:03:03 +04:00
|
|
|
|
2015-10-13 18:41:21 +03:00
|
|
|
sigact.sa_handler = SIG_IGN;
|
|
|
|
if (sigaction(SIGPIPE, &sigact, NULL) != 0) {
|
|
|
|
g_error("error configuring SIGPIPE signal handler: %s",
|
|
|
|
strerror(errno));
|
|
|
|
}
|
|
|
|
|
2012-01-19 10:18:20 +04:00
|
|
|
return true;
|
2011-07-21 00:19:37 +04:00
|
|
|
}
|
2012-05-10 23:50:41 +04:00
|
|
|
|
|
|
|
/* TODO: use this in place of all post-fork() fclose(std*) callers */
|
|
|
|
void reopen_fd_to_null(int fd)
|
|
|
|
{
|
|
|
|
int nullfd;
|
|
|
|
|
|
|
|
nullfd = open("/dev/null", O_RDWR);
|
|
|
|
if (nullfd < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dup2(nullfd, fd);
|
|
|
|
|
|
|
|
if (nullfd != fd) {
|
|
|
|
close(nullfd);
|
|
|
|
}
|
|
|
|
}
|
2012-01-20 08:04:34 +04:00
|
|
|
#endif
|
2011-07-21 00:19:37 +04:00
|
|
|
|
|
|
|
static void usage(const char *cmd)
|
|
|
|
{
|
2022-06-24 17:54:55 +03:00
|
|
|
#ifdef CONFIG_FSFREEZE
|
|
|
|
g_autofree char *fsfreeze_hook = get_relocated_path(QGA_FSFREEZE_HOOK_DEFAULT);
|
|
|
|
#endif
|
|
|
|
|
2011-07-21 00:19:37 +04:00
|
|
|
printf(
|
2012-04-17 20:28:27 +04:00
|
|
|
"Usage: %s [-m <method> -p <path>] [<options>]\n"
|
2018-02-15 14:06:47 +03:00
|
|
|
"QEMU Guest Agent " QEMU_FULL_VERSION "\n"
|
2017-08-03 19:33:52 +03:00
|
|
|
QEMU_COPYRIGHT "\n"
|
2011-07-21 00:19:37 +04:00
|
|
|
"\n"
|
2016-10-14 12:00:56 +03:00
|
|
|
" -m, --method transport method: one of unix-listen, virtio-serial,\n"
|
|
|
|
" isa-serial, or vsock-listen (virtio-serial is the default)\n"
|
2012-04-17 20:28:27 +04:00
|
|
|
" -p, --path device/socket path (the default for virtio-serial is:\n"
|
2014-01-15 13:33:44 +04:00
|
|
|
" %s,\n"
|
|
|
|
" the default for isa-serial is:\n"
|
2020-03-23 14:04:08 +03:00
|
|
|
" %s).\n"
|
|
|
|
" Socket addresses for vsock-listen are written as\n"
|
|
|
|
" <cid>:<port>.\n"
|
2011-07-21 00:19:37 +04:00
|
|
|
" -l, --logfile set logfile path, logs to stderr by default\n"
|
|
|
|
" -f, --pidfile specify pidfile (default is %s)\n"
|
2012-12-12 07:55:55 +04:00
|
|
|
#ifdef CONFIG_FSFREEZE
|
|
|
|
" -F, --fsfreeze-hook\n"
|
|
|
|
" enable fsfreeze hook. Accepts an optional argument that\n"
|
|
|
|
" specifies script to run on freeze/thaw. Script will be\n"
|
|
|
|
" called with 'freeze'/'thaw' arguments accordingly.\n"
|
|
|
|
" (default is %s)\n"
|
|
|
|
" If using -F with an argument, do not follow -F with a\n"
|
|
|
|
" space.\n"
|
|
|
|
" (for example: -F/var/run/fsfreezehook.sh)\n"
|
|
|
|
#endif
|
qemu-ga: persist tracking of fsfreeze state via filesystem
Currently, qemu-ga may die/get killed/go away for whatever reason after
guest-fsfreeze-freeze has been issued, and before guest-fsfreeze-thaw
has been issued. This means the only way to unfreeze the guest is via
VNC/network/console access, but obtaining that access after-the-fact can
often be very difficult when filesystems are frozen. Logins will almost
always hang, for instance. In many cases the only recourse would be to
reboot the guest without any quiescing of volatile state, which makes
this a corner-case worth giving some attention to.
A likely failsafe for this situation would be to use a watchdog to
restart qemu-ga if it goes away. There are some precautions qemu-ga
needs to take in order to avoid immediately hanging itself on I/O,
however, namely, we must disable logging and defer to processing/creation
of user-specific logfiles, along with creation of the pid file if we're
running as a daemon. We also need to disable non-fsfreeze-safe commands,
as we normally would when processing the guest-fsfreeze-freeze command.
To track when we need to do this in a way that persists between multiple
invocations of qemu-ga, we create a file on the guest filesystem before
issuing the fsfreeze, and delete it when doing the thaw. On qemu-ga
startup, we check for the existance of this file to determine
the need to take the above precautions.
We're forced to do it this way since a more traditional approach such as
reading/writing state to a dedicated state file will cause
access/modification time updates, respectively, both of which will hang
if the file resides on a frozen filesystem. Both can occur even if
relatime is enabled. Checking for file existence will not update the
access time, however, so it's a safe way to check for fsfreeze state.
An actual watchdog-based restart of qemu-ga can itself cause an access
time update that would thus hang the invocation of qemu-ga, but the
logic to workaround that can be handled via the watchdog, so we don't
address that here (for relatime we'd periodically touch the qemu-ga
binary if the file $qga_statedir/qga.state.isfrozen is not present, this
avoids qemu-ga updates or the 1 day relatime threshold causing an
access-time update if we try to respawn qemu-ga shortly after it goes
away)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-04-19 01:28:01 +04:00
|
|
|
" -t, --statedir specify dir to store state information (absolute paths\n"
|
|
|
|
" only, default is %s)\n"
|
2011-07-21 00:19:37 +04:00
|
|
|
" -v, --verbose log extra debugging information\n"
|
|
|
|
" -V, --version print version information and exit\n"
|
|
|
|
" -d, --daemonize become a daemon\n"
|
2012-01-22 02:42:27 +04:00
|
|
|
#ifdef _WIN32
|
2015-05-06 14:57:37 +03:00
|
|
|
" -s, --service service commands: install, uninstall, vss-install, vss-uninstall\n"
|
2012-01-20 08:04:34 +04:00
|
|
|
#endif
|
2022-07-27 12:21:33 +03:00
|
|
|
" -b, --block-rpcs comma-separated list of RPCs to disable (no spaces,\n"
|
|
|
|
" use \"help\" to list available RPCs)\n"
|
2015-08-27 02:34:58 +03:00
|
|
|
" -D, --dump-conf dump a qemu-ga config file based on current config\n"
|
|
|
|
" options / command-line parameters to stdout\n"
|
2018-10-07 14:02:20 +03:00
|
|
|
" -r, --retry-path attempt re-opening path if it's unavailable or closed\n"
|
|
|
|
" due to an error which may be recoverable in the future\n"
|
|
|
|
" (virtio-serial driver re-install, serial device hot\n"
|
|
|
|
" plug/unplug, etc.)\n"
|
2011-07-21 00:19:37 +04:00
|
|
|
" -h, --help display this help and exit\n"
|
|
|
|
"\n"
|
2017-08-03 19:33:53 +03:00
|
|
|
QEMU_HELP_BOTTOM "\n"
|
2017-08-03 19:33:52 +03:00
|
|
|
, cmd, QGA_VIRTIO_PATH_DEFAULT, QGA_SERIAL_PATH_DEFAULT,
|
2014-01-15 13:33:44 +04:00
|
|
|
dfl_pathnames.pidfile,
|
2012-12-12 07:55:55 +04:00
|
|
|
#ifdef CONFIG_FSFREEZE
|
2022-06-24 17:54:55 +03:00
|
|
|
fsfreeze_hook,
|
2012-12-12 07:55:55 +04:00
|
|
|
#endif
|
2013-05-18 08:31:49 +04:00
|
|
|
dfl_pathnames.state_dir);
|
2011-07-21 00:19:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *ga_log_level_str(GLogLevelFlags level)
|
|
|
|
{
|
|
|
|
switch (level & G_LOG_LEVEL_MASK) {
|
2020-10-26 12:05:49 +03:00
|
|
|
case G_LOG_LEVEL_ERROR:
|
|
|
|
return "error";
|
|
|
|
case G_LOG_LEVEL_CRITICAL:
|
|
|
|
return "critical";
|
|
|
|
case G_LOG_LEVEL_WARNING:
|
|
|
|
return "warning";
|
|
|
|
case G_LOG_LEVEL_MESSAGE:
|
|
|
|
return "message";
|
|
|
|
case G_LOG_LEVEL_INFO:
|
|
|
|
return "info";
|
|
|
|
case G_LOG_LEVEL_DEBUG:
|
|
|
|
return "debug";
|
|
|
|
default:
|
|
|
|
return "user";
|
2011-07-21 00:19:37 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ga_logging_enabled(GAState *s)
|
|
|
|
{
|
|
|
|
return s->logging_enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ga_disable_logging(GAState *s)
|
|
|
|
{
|
|
|
|
s->logging_enabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ga_enable_logging(GAState *s)
|
|
|
|
{
|
|
|
|
s->logging_enabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ga_log(const gchar *domain, GLogLevelFlags level,
|
|
|
|
const gchar *msg, gpointer opaque)
|
|
|
|
{
|
|
|
|
GAState *s = opaque;
|
|
|
|
const char *level_str = ga_log_level_str(level);
|
|
|
|
|
|
|
|
if (!ga_logging_enabled(s)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
level &= G_LOG_LEVEL_MASK;
|
2012-01-20 08:04:34 +04:00
|
|
|
#ifndef _WIN32
|
2015-05-27 20:53:49 +03:00
|
|
|
if (g_strcmp0(domain, "syslog") == 0) {
|
2011-07-21 00:19:37 +04:00
|
|
|
syslog(LOG_INFO, "%s: %s", level_str, msg);
|
|
|
|
} else if (level & s->log_level) {
|
2012-01-20 08:04:34 +04:00
|
|
|
#else
|
|
|
|
if (level & s->log_level) {
|
|
|
|
#endif
|
2022-04-07 14:21:41 +03:00
|
|
|
g_autoptr(GDateTime) now = g_date_time_new_now_utc();
|
|
|
|
g_autofree char *nowstr = g_date_time_format(now, "%s.%f");
|
|
|
|
fprintf(s->log_file, "%s: %s: %s\n", nowstr, level_str, msg);
|
2011-07-21 00:19:37 +04:00
|
|
|
fflush(s->log_file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-07 23:56:48 +04:00
|
|
|
void ga_set_response_delimited(GAState *s)
|
|
|
|
{
|
|
|
|
s->delimit_response = true;
|
|
|
|
}
|
|
|
|
|
2013-01-09 01:26:26 +04:00
|
|
|
static FILE *ga_open_logfile(const char *logfile)
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
f = fopen(logfile, "a");
|
|
|
|
if (!f) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
qemu_set_cloexec(fileno(f));
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2012-04-18 04:01:45 +04:00
|
|
|
static gint ga_strcmp(gconstpointer str1, gconstpointer str2)
|
|
|
|
{
|
|
|
|
return strcmp(str1, str2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* disable commands that aren't safe for fsfreeze */
|
2022-07-27 12:21:34 +03:00
|
|
|
static void ga_disable_not_allowed(const QmpCommand *cmd, void *opaque)
|
2012-04-18 04:01:45 +04:00
|
|
|
{
|
2022-07-27 12:21:34 +03:00
|
|
|
bool allowed = false;
|
2013-10-09 07:25:07 +04:00
|
|
|
int i = 0;
|
|
|
|
const char *name = qmp_command_name(cmd);
|
|
|
|
|
2022-07-27 12:21:34 +03:00
|
|
|
while (ga_freeze_allowlist[i] != NULL) {
|
|
|
|
if (strcmp(name, ga_freeze_allowlist[i]) == 0) {
|
|
|
|
allowed = true;
|
2012-04-18 04:01:45 +04:00
|
|
|
}
|
2013-10-09 07:25:07 +04:00
|
|
|
i++;
|
|
|
|
}
|
2022-07-27 12:21:34 +03:00
|
|
|
if (!allowed) {
|
2013-10-09 07:25:07 +04:00
|
|
|
g_debug("disabling command: %s", name);
|
2021-02-19 11:28:14 +03:00
|
|
|
qmp_disable_command(&ga_commands, name, "the agent is in frozen state");
|
2012-04-18 04:01:45 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-27 12:21:34 +03:00
|
|
|
/* [re-]enable all commands, except those explicitly blocked by user */
|
|
|
|
static void ga_enable_non_blocked(const QmpCommand *cmd, void *opaque)
|
2012-04-18 04:01:45 +04:00
|
|
|
{
|
2022-07-27 12:21:34 +03:00
|
|
|
GList *blockedrpcs = opaque;
|
2013-10-09 07:25:07 +04:00
|
|
|
const char *name = qmp_command_name(cmd);
|
|
|
|
|
2022-07-27 12:21:34 +03:00
|
|
|
if (g_list_find_custom(blockedrpcs, name, ga_strcmp) == NULL &&
|
2013-10-09 07:25:07 +04:00
|
|
|
!qmp_command_is_enabled(cmd)) {
|
|
|
|
g_debug("enabling command: %s", name);
|
2017-03-03 15:32:25 +03:00
|
|
|
qmp_enable_command(&ga_commands, name);
|
2012-04-18 04:01:45 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
qemu-ga: persist tracking of fsfreeze state via filesystem
Currently, qemu-ga may die/get killed/go away for whatever reason after
guest-fsfreeze-freeze has been issued, and before guest-fsfreeze-thaw
has been issued. This means the only way to unfreeze the guest is via
VNC/network/console access, but obtaining that access after-the-fact can
often be very difficult when filesystems are frozen. Logins will almost
always hang, for instance. In many cases the only recourse would be to
reboot the guest without any quiescing of volatile state, which makes
this a corner-case worth giving some attention to.
A likely failsafe for this situation would be to use a watchdog to
restart qemu-ga if it goes away. There are some precautions qemu-ga
needs to take in order to avoid immediately hanging itself on I/O,
however, namely, we must disable logging and defer to processing/creation
of user-specific logfiles, along with creation of the pid file if we're
running as a daemon. We also need to disable non-fsfreeze-safe commands,
as we normally would when processing the guest-fsfreeze-freeze command.
To track when we need to do this in a way that persists between multiple
invocations of qemu-ga, we create a file on the guest filesystem before
issuing the fsfreeze, and delete it when doing the thaw. On qemu-ga
startup, we check for the existance of this file to determine
the need to take the above precautions.
We're forced to do it this way since a more traditional approach such as
reading/writing state to a dedicated state file will cause
access/modification time updates, respectively, both of which will hang
if the file resides on a frozen filesystem. Both can occur even if
relatime is enabled. Checking for file existence will not update the
access time, however, so it's a safe way to check for fsfreeze state.
An actual watchdog-based restart of qemu-ga can itself cause an access
time update that would thus hang the invocation of qemu-ga, but the
logic to workaround that can be handled via the watchdog, so we don't
address that here (for relatime we'd periodically touch the qemu-ga
binary if the file $qga_statedir/qga.state.isfrozen is not present, this
avoids qemu-ga updates or the 1 day relatime threshold causing an
access-time update if we try to respawn qemu-ga shortly after it goes
away)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-04-19 01:28:01 +04:00
|
|
|
static bool ga_create_file(const char *path)
|
|
|
|
{
|
|
|
|
int fd = open(path, O_CREAT | O_WRONLY, S_IWUSR | S_IRUSR);
|
|
|
|
if (fd == -1) {
|
|
|
|
g_warning("unable to open/create file %s: %s", path, strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ga_delete_file(const char *path)
|
|
|
|
{
|
|
|
|
int ret = unlink(path);
|
|
|
|
if (ret == -1) {
|
|
|
|
g_warning("unable to delete file: %s: %s", path, strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-04-18 04:01:45 +04:00
|
|
|
bool ga_is_frozen(GAState *s)
|
|
|
|
{
|
|
|
|
return s->frozen;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ga_set_frozen(GAState *s)
|
|
|
|
{
|
|
|
|
if (ga_is_frozen(s)) {
|
|
|
|
return;
|
|
|
|
}
|
2022-07-27 12:21:34 +03:00
|
|
|
/* disable all forbidden (for frozen state) commands */
|
|
|
|
qmp_for_each_command(&ga_commands, ga_disable_not_allowed, NULL);
|
2012-04-18 04:01:45 +04:00
|
|
|
g_warning("disabling logging due to filesystem freeze");
|
|
|
|
ga_disable_logging(s);
|
|
|
|
s->frozen = true;
|
qemu-ga: persist tracking of fsfreeze state via filesystem
Currently, qemu-ga may die/get killed/go away for whatever reason after
guest-fsfreeze-freeze has been issued, and before guest-fsfreeze-thaw
has been issued. This means the only way to unfreeze the guest is via
VNC/network/console access, but obtaining that access after-the-fact can
often be very difficult when filesystems are frozen. Logins will almost
always hang, for instance. In many cases the only recourse would be to
reboot the guest without any quiescing of volatile state, which makes
this a corner-case worth giving some attention to.
A likely failsafe for this situation would be to use a watchdog to
restart qemu-ga if it goes away. There are some precautions qemu-ga
needs to take in order to avoid immediately hanging itself on I/O,
however, namely, we must disable logging and defer to processing/creation
of user-specific logfiles, along with creation of the pid file if we're
running as a daemon. We also need to disable non-fsfreeze-safe commands,
as we normally would when processing the guest-fsfreeze-freeze command.
To track when we need to do this in a way that persists between multiple
invocations of qemu-ga, we create a file on the guest filesystem before
issuing the fsfreeze, and delete it when doing the thaw. On qemu-ga
startup, we check for the existance of this file to determine
the need to take the above precautions.
We're forced to do it this way since a more traditional approach such as
reading/writing state to a dedicated state file will cause
access/modification time updates, respectively, both of which will hang
if the file resides on a frozen filesystem. Both can occur even if
relatime is enabled. Checking for file existence will not update the
access time, however, so it's a safe way to check for fsfreeze state.
An actual watchdog-based restart of qemu-ga can itself cause an access
time update that would thus hang the invocation of qemu-ga, but the
logic to workaround that can be handled via the watchdog, so we don't
address that here (for relatime we'd periodically touch the qemu-ga
binary if the file $qga_statedir/qga.state.isfrozen is not present, this
avoids qemu-ga updates or the 1 day relatime threshold causing an
access-time update if we try to respawn qemu-ga shortly after it goes
away)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-04-19 01:28:01 +04:00
|
|
|
if (!ga_create_file(s->state_filepath_isfrozen)) {
|
|
|
|
g_warning("unable to create %s, fsfreeze may not function properly",
|
|
|
|
s->state_filepath_isfrozen);
|
|
|
|
}
|
2012-04-18 04:01:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ga_unset_frozen(GAState *s)
|
|
|
|
{
|
|
|
|
if (!ga_is_frozen(s)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
qemu-ga: persist tracking of fsfreeze state via filesystem
Currently, qemu-ga may die/get killed/go away for whatever reason after
guest-fsfreeze-freeze has been issued, and before guest-fsfreeze-thaw
has been issued. This means the only way to unfreeze the guest is via
VNC/network/console access, but obtaining that access after-the-fact can
often be very difficult when filesystems are frozen. Logins will almost
always hang, for instance. In many cases the only recourse would be to
reboot the guest without any quiescing of volatile state, which makes
this a corner-case worth giving some attention to.
A likely failsafe for this situation would be to use a watchdog to
restart qemu-ga if it goes away. There are some precautions qemu-ga
needs to take in order to avoid immediately hanging itself on I/O,
however, namely, we must disable logging and defer to processing/creation
of user-specific logfiles, along with creation of the pid file if we're
running as a daemon. We also need to disable non-fsfreeze-safe commands,
as we normally would when processing the guest-fsfreeze-freeze command.
To track when we need to do this in a way that persists between multiple
invocations of qemu-ga, we create a file on the guest filesystem before
issuing the fsfreeze, and delete it when doing the thaw. On qemu-ga
startup, we check for the existance of this file to determine
the need to take the above precautions.
We're forced to do it this way since a more traditional approach such as
reading/writing state to a dedicated state file will cause
access/modification time updates, respectively, both of which will hang
if the file resides on a frozen filesystem. Both can occur even if
relatime is enabled. Checking for file existence will not update the
access time, however, so it's a safe way to check for fsfreeze state.
An actual watchdog-based restart of qemu-ga can itself cause an access
time update that would thus hang the invocation of qemu-ga, but the
logic to workaround that can be handled via the watchdog, so we don't
address that here (for relatime we'd periodically touch the qemu-ga
binary if the file $qga_statedir/qga.state.isfrozen is not present, this
avoids qemu-ga updates or the 1 day relatime threshold causing an
access-time update if we try to respawn qemu-ga shortly after it goes
away)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-04-19 01:28:01 +04:00
|
|
|
/* if we delayed creation/opening of pid/log files due to being
|
|
|
|
* in a frozen state at start up, do it now
|
|
|
|
*/
|
|
|
|
if (s->deferred_options.log_filepath) {
|
2013-01-09 01:26:26 +04:00
|
|
|
s->log_file = ga_open_logfile(s->deferred_options.log_filepath);
|
qemu-ga: persist tracking of fsfreeze state via filesystem
Currently, qemu-ga may die/get killed/go away for whatever reason after
guest-fsfreeze-freeze has been issued, and before guest-fsfreeze-thaw
has been issued. This means the only way to unfreeze the guest is via
VNC/network/console access, but obtaining that access after-the-fact can
often be very difficult when filesystems are frozen. Logins will almost
always hang, for instance. In many cases the only recourse would be to
reboot the guest without any quiescing of volatile state, which makes
this a corner-case worth giving some attention to.
A likely failsafe for this situation would be to use a watchdog to
restart qemu-ga if it goes away. There are some precautions qemu-ga
needs to take in order to avoid immediately hanging itself on I/O,
however, namely, we must disable logging and defer to processing/creation
of user-specific logfiles, along with creation of the pid file if we're
running as a daemon. We also need to disable non-fsfreeze-safe commands,
as we normally would when processing the guest-fsfreeze-freeze command.
To track when we need to do this in a way that persists between multiple
invocations of qemu-ga, we create a file on the guest filesystem before
issuing the fsfreeze, and delete it when doing the thaw. On qemu-ga
startup, we check for the existance of this file to determine
the need to take the above precautions.
We're forced to do it this way since a more traditional approach such as
reading/writing state to a dedicated state file will cause
access/modification time updates, respectively, both of which will hang
if the file resides on a frozen filesystem. Both can occur even if
relatime is enabled. Checking for file existence will not update the
access time, however, so it's a safe way to check for fsfreeze state.
An actual watchdog-based restart of qemu-ga can itself cause an access
time update that would thus hang the invocation of qemu-ga, but the
logic to workaround that can be handled via the watchdog, so we don't
address that here (for relatime we'd periodically touch the qemu-ga
binary if the file $qga_statedir/qga.state.isfrozen is not present, this
avoids qemu-ga updates or the 1 day relatime threshold causing an
access-time update if we try to respawn qemu-ga shortly after it goes
away)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-04-19 01:28:01 +04:00
|
|
|
if (!s->log_file) {
|
|
|
|
s->log_file = stderr;
|
|
|
|
}
|
|
|
|
s->deferred_options.log_filepath = NULL;
|
|
|
|
}
|
2012-04-18 04:01:45 +04:00
|
|
|
ga_enable_logging(s);
|
qemu-ga: persist tracking of fsfreeze state via filesystem
Currently, qemu-ga may die/get killed/go away for whatever reason after
guest-fsfreeze-freeze has been issued, and before guest-fsfreeze-thaw
has been issued. This means the only way to unfreeze the guest is via
VNC/network/console access, but obtaining that access after-the-fact can
often be very difficult when filesystems are frozen. Logins will almost
always hang, for instance. In many cases the only recourse would be to
reboot the guest without any quiescing of volatile state, which makes
this a corner-case worth giving some attention to.
A likely failsafe for this situation would be to use a watchdog to
restart qemu-ga if it goes away. There are some precautions qemu-ga
needs to take in order to avoid immediately hanging itself on I/O,
however, namely, we must disable logging and defer to processing/creation
of user-specific logfiles, along with creation of the pid file if we're
running as a daemon. We also need to disable non-fsfreeze-safe commands,
as we normally would when processing the guest-fsfreeze-freeze command.
To track when we need to do this in a way that persists between multiple
invocations of qemu-ga, we create a file on the guest filesystem before
issuing the fsfreeze, and delete it when doing the thaw. On qemu-ga
startup, we check for the existance of this file to determine
the need to take the above precautions.
We're forced to do it this way since a more traditional approach such as
reading/writing state to a dedicated state file will cause
access/modification time updates, respectively, both of which will hang
if the file resides on a frozen filesystem. Both can occur even if
relatime is enabled. Checking for file existence will not update the
access time, however, so it's a safe way to check for fsfreeze state.
An actual watchdog-based restart of qemu-ga can itself cause an access
time update that would thus hang the invocation of qemu-ga, but the
logic to workaround that can be handled via the watchdog, so we don't
address that here (for relatime we'd periodically touch the qemu-ga
binary if the file $qga_statedir/qga.state.isfrozen is not present, this
avoids qemu-ga updates or the 1 day relatime threshold causing an
access-time update if we try to respawn qemu-ga shortly after it goes
away)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-04-19 01:28:01 +04:00
|
|
|
g_warning("logging re-enabled due to filesystem unfreeze");
|
|
|
|
if (s->deferred_options.pid_filepath) {
|
util: add qemu_write_pidfile()
There are variants of qemu_create_pidfile() in qemu-pr-helper and
qemu-ga. Let's have a common implementation in libqemuutil.
The code is initially based from pr-helper write_pidfile(), with
various improvements and suggestions from Daniel Berrangé:
QEMU will leave the pidfile existing on disk when it exits which
initially made me think it avoids the deletion race. The app
managing QEMU, however, may well delete the pidfile after it has
seen QEMU exit, and even if the app locks the pidfile before
deleting it, there is still a race.
eg consider the following sequence
QEMU 1 libvirtd QEMU 2
1. lock(pidfile)
2. exit()
3. open(pidfile)
4. lock(pidfile)
5. open(pidfile)
6. unlink(pidfile)
7. close(pidfile)
8. lock(pidfile)
IOW, at step 8 the new QEMU has successfully acquired the lock, but
the pidfile no longer exists on disk because it was deleted after
the original QEMU exited.
While we could just say no external app should ever delete the
pidfile, I don't think that is satisfactory as people don't read
docs, and admins don't like stale pidfiles being left around on
disk.
To make this robust, I think we might want to copy libvirt's
approach to pidfile acquisition which runs in a loop and checks that
the file on disk /after/ acquiring the lock matches the file that
was locked. Then we could in fact safely let QEMU delete its own
pidfiles on clean exit..
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-Id: <20180831145314.14736-2-marcandre.lureau@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2018-08-31 17:53:12 +03:00
|
|
|
Error *err = NULL;
|
|
|
|
|
|
|
|
if (!qemu_write_pidfile(s->deferred_options.pid_filepath, &err)) {
|
|
|
|
g_warning("%s", error_get_pretty(err));
|
|
|
|
error_free(err);
|
qemu-ga: persist tracking of fsfreeze state via filesystem
Currently, qemu-ga may die/get killed/go away for whatever reason after
guest-fsfreeze-freeze has been issued, and before guest-fsfreeze-thaw
has been issued. This means the only way to unfreeze the guest is via
VNC/network/console access, but obtaining that access after-the-fact can
often be very difficult when filesystems are frozen. Logins will almost
always hang, for instance. In many cases the only recourse would be to
reboot the guest without any quiescing of volatile state, which makes
this a corner-case worth giving some attention to.
A likely failsafe for this situation would be to use a watchdog to
restart qemu-ga if it goes away. There are some precautions qemu-ga
needs to take in order to avoid immediately hanging itself on I/O,
however, namely, we must disable logging and defer to processing/creation
of user-specific logfiles, along with creation of the pid file if we're
running as a daemon. We also need to disable non-fsfreeze-safe commands,
as we normally would when processing the guest-fsfreeze-freeze command.
To track when we need to do this in a way that persists between multiple
invocations of qemu-ga, we create a file on the guest filesystem before
issuing the fsfreeze, and delete it when doing the thaw. On qemu-ga
startup, we check for the existance of this file to determine
the need to take the above precautions.
We're forced to do it this way since a more traditional approach such as
reading/writing state to a dedicated state file will cause
access/modification time updates, respectively, both of which will hang
if the file resides on a frozen filesystem. Both can occur even if
relatime is enabled. Checking for file existence will not update the
access time, however, so it's a safe way to check for fsfreeze state.
An actual watchdog-based restart of qemu-ga can itself cause an access
time update that would thus hang the invocation of qemu-ga, but the
logic to workaround that can be handled via the watchdog, so we don't
address that here (for relatime we'd periodically touch the qemu-ga
binary if the file $qga_statedir/qga.state.isfrozen is not present, this
avoids qemu-ga updates or the 1 day relatime threshold causing an
access-time update if we try to respawn qemu-ga shortly after it goes
away)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-04-19 01:28:01 +04:00
|
|
|
}
|
|
|
|
s->deferred_options.pid_filepath = NULL;
|
|
|
|
}
|
2012-04-18 04:01:45 +04:00
|
|
|
|
2022-07-27 12:21:34 +03:00
|
|
|
/* enable all disabled, non-blocked commands */
|
|
|
|
qmp_for_each_command(&ga_commands, ga_enable_non_blocked, s->blockedrpcs);
|
2012-04-18 04:01:45 +04:00
|
|
|
s->frozen = false;
|
qemu-ga: persist tracking of fsfreeze state via filesystem
Currently, qemu-ga may die/get killed/go away for whatever reason after
guest-fsfreeze-freeze has been issued, and before guest-fsfreeze-thaw
has been issued. This means the only way to unfreeze the guest is via
VNC/network/console access, but obtaining that access after-the-fact can
often be very difficult when filesystems are frozen. Logins will almost
always hang, for instance. In many cases the only recourse would be to
reboot the guest without any quiescing of volatile state, which makes
this a corner-case worth giving some attention to.
A likely failsafe for this situation would be to use a watchdog to
restart qemu-ga if it goes away. There are some precautions qemu-ga
needs to take in order to avoid immediately hanging itself on I/O,
however, namely, we must disable logging and defer to processing/creation
of user-specific logfiles, along with creation of the pid file if we're
running as a daemon. We also need to disable non-fsfreeze-safe commands,
as we normally would when processing the guest-fsfreeze-freeze command.
To track when we need to do this in a way that persists between multiple
invocations of qemu-ga, we create a file on the guest filesystem before
issuing the fsfreeze, and delete it when doing the thaw. On qemu-ga
startup, we check for the existance of this file to determine
the need to take the above precautions.
We're forced to do it this way since a more traditional approach such as
reading/writing state to a dedicated state file will cause
access/modification time updates, respectively, both of which will hang
if the file resides on a frozen filesystem. Both can occur even if
relatime is enabled. Checking for file existence will not update the
access time, however, so it's a safe way to check for fsfreeze state.
An actual watchdog-based restart of qemu-ga can itself cause an access
time update that would thus hang the invocation of qemu-ga, but the
logic to workaround that can be handled via the watchdog, so we don't
address that here (for relatime we'd periodically touch the qemu-ga
binary if the file $qga_statedir/qga.state.isfrozen is not present, this
avoids qemu-ga updates or the 1 day relatime threshold causing an
access-time update if we try to respawn qemu-ga shortly after it goes
away)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-04-19 01:28:01 +04:00
|
|
|
if (!ga_delete_file(s->state_filepath_isfrozen)) {
|
|
|
|
g_warning("unable to delete %s, fsfreeze may not function properly",
|
|
|
|
s->state_filepath_isfrozen);
|
|
|
|
}
|
2012-04-18 04:01:45 +04:00
|
|
|
}
|
|
|
|
|
2012-12-12 07:55:55 +04:00
|
|
|
#ifdef CONFIG_FSFREEZE
|
|
|
|
const char *ga_fsfreeze_hook(GAState *s)
|
|
|
|
{
|
|
|
|
return s->fsfreeze_hook;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-07-21 00:19:37 +04:00
|
|
|
static void become_daemon(const char *pidfile)
|
|
|
|
{
|
qemu-ga: persist tracking of fsfreeze state via filesystem
Currently, qemu-ga may die/get killed/go away for whatever reason after
guest-fsfreeze-freeze has been issued, and before guest-fsfreeze-thaw
has been issued. This means the only way to unfreeze the guest is via
VNC/network/console access, but obtaining that access after-the-fact can
often be very difficult when filesystems are frozen. Logins will almost
always hang, for instance. In many cases the only recourse would be to
reboot the guest without any quiescing of volatile state, which makes
this a corner-case worth giving some attention to.
A likely failsafe for this situation would be to use a watchdog to
restart qemu-ga if it goes away. There are some precautions qemu-ga
needs to take in order to avoid immediately hanging itself on I/O,
however, namely, we must disable logging and defer to processing/creation
of user-specific logfiles, along with creation of the pid file if we're
running as a daemon. We also need to disable non-fsfreeze-safe commands,
as we normally would when processing the guest-fsfreeze-freeze command.
To track when we need to do this in a way that persists between multiple
invocations of qemu-ga, we create a file on the guest filesystem before
issuing the fsfreeze, and delete it when doing the thaw. On qemu-ga
startup, we check for the existance of this file to determine
the need to take the above precautions.
We're forced to do it this way since a more traditional approach such as
reading/writing state to a dedicated state file will cause
access/modification time updates, respectively, both of which will hang
if the file resides on a frozen filesystem. Both can occur even if
relatime is enabled. Checking for file existence will not update the
access time, however, so it's a safe way to check for fsfreeze state.
An actual watchdog-based restart of qemu-ga can itself cause an access
time update that would thus hang the invocation of qemu-ga, but the
logic to workaround that can be handled via the watchdog, so we don't
address that here (for relatime we'd periodically touch the qemu-ga
binary if the file $qga_statedir/qga.state.isfrozen is not present, this
avoids qemu-ga updates or the 1 day relatime threshold causing an
access-time update if we try to respawn qemu-ga shortly after it goes
away)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-04-19 01:28:01 +04:00
|
|
|
#ifndef _WIN32
|
2011-07-21 00:19:37 +04:00
|
|
|
pid_t pid, sid;
|
|
|
|
|
|
|
|
pid = fork();
|
|
|
|
if (pid < 0) {
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (pid > 0) {
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
qemu-ga: persist tracking of fsfreeze state via filesystem
Currently, qemu-ga may die/get killed/go away for whatever reason after
guest-fsfreeze-freeze has been issued, and before guest-fsfreeze-thaw
has been issued. This means the only way to unfreeze the guest is via
VNC/network/console access, but obtaining that access after-the-fact can
often be very difficult when filesystems are frozen. Logins will almost
always hang, for instance. In many cases the only recourse would be to
reboot the guest without any quiescing of volatile state, which makes
this a corner-case worth giving some attention to.
A likely failsafe for this situation would be to use a watchdog to
restart qemu-ga if it goes away. There are some precautions qemu-ga
needs to take in order to avoid immediately hanging itself on I/O,
however, namely, we must disable logging and defer to processing/creation
of user-specific logfiles, along with creation of the pid file if we're
running as a daemon. We also need to disable non-fsfreeze-safe commands,
as we normally would when processing the guest-fsfreeze-freeze command.
To track when we need to do this in a way that persists between multiple
invocations of qemu-ga, we create a file on the guest filesystem before
issuing the fsfreeze, and delete it when doing the thaw. On qemu-ga
startup, we check for the existance of this file to determine
the need to take the above precautions.
We're forced to do it this way since a more traditional approach such as
reading/writing state to a dedicated state file will cause
access/modification time updates, respectively, both of which will hang
if the file resides on a frozen filesystem. Both can occur even if
relatime is enabled. Checking for file existence will not update the
access time, however, so it's a safe way to check for fsfreeze state.
An actual watchdog-based restart of qemu-ga can itself cause an access
time update that would thus hang the invocation of qemu-ga, but the
logic to workaround that can be handled via the watchdog, so we don't
address that here (for relatime we'd periodically touch the qemu-ga
binary if the file $qga_statedir/qga.state.isfrozen is not present, this
avoids qemu-ga updates or the 1 day relatime threshold causing an
access-time update if we try to respawn qemu-ga shortly after it goes
away)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-04-19 01:28:01 +04:00
|
|
|
if (pidfile) {
|
util: add qemu_write_pidfile()
There are variants of qemu_create_pidfile() in qemu-pr-helper and
qemu-ga. Let's have a common implementation in libqemuutil.
The code is initially based from pr-helper write_pidfile(), with
various improvements and suggestions from Daniel Berrangé:
QEMU will leave the pidfile existing on disk when it exits which
initially made me think it avoids the deletion race. The app
managing QEMU, however, may well delete the pidfile after it has
seen QEMU exit, and even if the app locks the pidfile before
deleting it, there is still a race.
eg consider the following sequence
QEMU 1 libvirtd QEMU 2
1. lock(pidfile)
2. exit()
3. open(pidfile)
4. lock(pidfile)
5. open(pidfile)
6. unlink(pidfile)
7. close(pidfile)
8. lock(pidfile)
IOW, at step 8 the new QEMU has successfully acquired the lock, but
the pidfile no longer exists on disk because it was deleted after
the original QEMU exited.
While we could just say no external app should ever delete the
pidfile, I don't think that is satisfactory as people don't read
docs, and admins don't like stale pidfiles being left around on
disk.
To make this robust, I think we might want to copy libvirt's
approach to pidfile acquisition which runs in a loop and checks that
the file on disk /after/ acquiring the lock matches the file that
was locked. Then we could in fact safely let QEMU delete its own
pidfiles on clean exit..
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-Id: <20180831145314.14736-2-marcandre.lureau@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2018-08-31 17:53:12 +03:00
|
|
|
Error *err = NULL;
|
|
|
|
|
|
|
|
if (!qemu_write_pidfile(pidfile, &err)) {
|
|
|
|
g_critical("%s", error_get_pretty(err));
|
|
|
|
error_free(err);
|
qemu-ga: persist tracking of fsfreeze state via filesystem
Currently, qemu-ga may die/get killed/go away for whatever reason after
guest-fsfreeze-freeze has been issued, and before guest-fsfreeze-thaw
has been issued. This means the only way to unfreeze the guest is via
VNC/network/console access, but obtaining that access after-the-fact can
often be very difficult when filesystems are frozen. Logins will almost
always hang, for instance. In many cases the only recourse would be to
reboot the guest without any quiescing of volatile state, which makes
this a corner-case worth giving some attention to.
A likely failsafe for this situation would be to use a watchdog to
restart qemu-ga if it goes away. There are some precautions qemu-ga
needs to take in order to avoid immediately hanging itself on I/O,
however, namely, we must disable logging and defer to processing/creation
of user-specific logfiles, along with creation of the pid file if we're
running as a daemon. We also need to disable non-fsfreeze-safe commands,
as we normally would when processing the guest-fsfreeze-freeze command.
To track when we need to do this in a way that persists between multiple
invocations of qemu-ga, we create a file on the guest filesystem before
issuing the fsfreeze, and delete it when doing the thaw. On qemu-ga
startup, we check for the existance of this file to determine
the need to take the above precautions.
We're forced to do it this way since a more traditional approach such as
reading/writing state to a dedicated state file will cause
access/modification time updates, respectively, both of which will hang
if the file resides on a frozen filesystem. Both can occur even if
relatime is enabled. Checking for file existence will not update the
access time, however, so it's a safe way to check for fsfreeze state.
An actual watchdog-based restart of qemu-ga can itself cause an access
time update that would thus hang the invocation of qemu-ga, but the
logic to workaround that can be handled via the watchdog, so we don't
address that here (for relatime we'd periodically touch the qemu-ga
binary if the file $qga_statedir/qga.state.isfrozen is not present, this
avoids qemu-ga updates or the 1 day relatime threshold causing an
access-time update if we try to respawn qemu-ga shortly after it goes
away)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-04-19 01:28:01 +04:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2011-07-21 00:19:37 +04:00
|
|
|
}
|
|
|
|
|
2013-04-24 15:13:18 +04:00
|
|
|
umask(S_IRWXG | S_IRWXO);
|
2011-07-21 00:19:37 +04:00
|
|
|
sid = setsid();
|
|
|
|
if (sid < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if ((chdir("/")) < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2012-05-10 23:50:42 +04:00
|
|
|
reopen_fd_to_null(STDIN_FILENO);
|
|
|
|
reopen_fd_to_null(STDOUT_FILENO);
|
|
|
|
reopen_fd_to_null(STDERR_FILENO);
|
2011-07-21 00:19:37 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
fail:
|
2012-08-24 09:03:03 +04:00
|
|
|
if (pidfile) {
|
|
|
|
unlink(pidfile);
|
|
|
|
}
|
2011-07-21 00:19:37 +04:00
|
|
|
g_critical("failed to daemonize");
|
|
|
|
exit(EXIT_FAILURE);
|
2012-01-20 08:04:34 +04:00
|
|
|
#endif
|
qemu-ga: persist tracking of fsfreeze state via filesystem
Currently, qemu-ga may die/get killed/go away for whatever reason after
guest-fsfreeze-freeze has been issued, and before guest-fsfreeze-thaw
has been issued. This means the only way to unfreeze the guest is via
VNC/network/console access, but obtaining that access after-the-fact can
often be very difficult when filesystems are frozen. Logins will almost
always hang, for instance. In many cases the only recourse would be to
reboot the guest without any quiescing of volatile state, which makes
this a corner-case worth giving some attention to.
A likely failsafe for this situation would be to use a watchdog to
restart qemu-ga if it goes away. There are some precautions qemu-ga
needs to take in order to avoid immediately hanging itself on I/O,
however, namely, we must disable logging and defer to processing/creation
of user-specific logfiles, along with creation of the pid file if we're
running as a daemon. We also need to disable non-fsfreeze-safe commands,
as we normally would when processing the guest-fsfreeze-freeze command.
To track when we need to do this in a way that persists between multiple
invocations of qemu-ga, we create a file on the guest filesystem before
issuing the fsfreeze, and delete it when doing the thaw. On qemu-ga
startup, we check for the existance of this file to determine
the need to take the above precautions.
We're forced to do it this way since a more traditional approach such as
reading/writing state to a dedicated state file will cause
access/modification time updates, respectively, both of which will hang
if the file resides on a frozen filesystem. Both can occur even if
relatime is enabled. Checking for file existence will not update the
access time, however, so it's a safe way to check for fsfreeze state.
An actual watchdog-based restart of qemu-ga can itself cause an access
time update that would thus hang the invocation of qemu-ga, but the
logic to workaround that can be handled via the watchdog, so we don't
address that here (for relatime we'd periodically touch the qemu-ga
binary if the file $qga_statedir/qga.state.isfrozen is not present, this
avoids qemu-ga updates or the 1 day relatime threshold causing an
access-time update if we try to respawn qemu-ga shortly after it goes
away)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-04-19 01:28:01 +04:00
|
|
|
}
|
2011-07-21 00:19:37 +04:00
|
|
|
|
2019-02-20 18:42:52 +03:00
|
|
|
static int send_response(GAState *s, const QDict *rsp)
|
2011-07-21 00:19:37 +04:00
|
|
|
{
|
2020-12-11 20:11:37 +03:00
|
|
|
GString *response;
|
2012-01-19 10:18:20 +04:00
|
|
|
GIOStatus status;
|
2011-07-21 00:19:37 +04:00
|
|
|
|
2020-06-04 12:44:25 +03:00
|
|
|
g_assert(s->channel);
|
|
|
|
|
|
|
|
if (!rsp) {
|
|
|
|
return 0;
|
|
|
|
}
|
2011-07-21 00:19:37 +04:00
|
|
|
|
2020-12-11 20:11:37 +03:00
|
|
|
response = qobject_to_json(QOBJECT(rsp));
|
|
|
|
if (!response) {
|
2011-07-21 00:19:37 +04:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2012-02-07 23:56:48 +04:00
|
|
|
if (s->delimit_response) {
|
|
|
|
s->delimit_response = false;
|
2020-12-11 20:11:37 +03:00
|
|
|
g_string_prepend_c(response, QGA_SENTINEL_BYTE);
|
2012-02-07 23:56:48 +04:00
|
|
|
}
|
|
|
|
|
2020-12-11 20:11:37 +03:00
|
|
|
g_string_append_c(response, '\n');
|
|
|
|
status = ga_channel_write_all(s->channel, response->str, response->len);
|
|
|
|
g_string_free(response, true);
|
2012-01-19 10:18:20 +04:00
|
|
|
if (status != G_IO_STATUS_NORMAL) {
|
|
|
|
return -EIO;
|
2011-07-21 00:19:37 +04:00
|
|
|
}
|
2012-01-19 10:18:20 +04:00
|
|
|
|
|
|
|
return 0;
|
2011-07-21 00:19:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* handle requests/control events coming in over the channel */
|
2018-08-23 19:40:01 +03:00
|
|
|
static void process_event(void *opaque, QObject *obj, Error *err)
|
2011-07-21 00:19:37 +04:00
|
|
|
{
|
2018-08-23 19:40:01 +03:00
|
|
|
GAState *s = opaque;
|
2019-02-20 18:42:52 +03:00
|
|
|
QDict *rsp;
|
2011-07-21 00:19:37 +04:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
g_debug("process_event: called");
|
2018-08-23 19:40:06 +03:00
|
|
|
assert(!obj != !err);
|
2018-07-19 21:40:59 +03:00
|
|
|
if (err) {
|
2019-02-20 18:42:52 +03:00
|
|
|
rsp = qmp_error_response(err);
|
|
|
|
goto end;
|
2011-07-21 00:19:37 +04:00
|
|
|
}
|
|
|
|
|
2019-02-20 18:42:52 +03:00
|
|
|
g_debug("processing command");
|
2020-10-05 18:58:47 +03:00
|
|
|
rsp = qmp_dispatch(&ga_commands, obj, false, NULL);
|
2018-07-19 21:40:59 +03:00
|
|
|
|
2019-02-20 18:42:52 +03:00
|
|
|
end:
|
2018-07-19 21:40:59 +03:00
|
|
|
ret = send_response(s, rsp);
|
|
|
|
if (ret < 0) {
|
|
|
|
g_warning("error sending error response: %s", strerror(-ret));
|
|
|
|
}
|
|
|
|
qobject_unref(rsp);
|
|
|
|
qobject_unref(obj);
|
2011-07-21 00:19:37 +04:00
|
|
|
}
|
|
|
|
|
2012-01-19 10:18:20 +04:00
|
|
|
/* false return signals GAChannel to close the current client connection */
|
|
|
|
static gboolean channel_event_cb(GIOCondition condition, gpointer data)
|
2011-07-21 00:19:37 +04:00
|
|
|
{
|
|
|
|
GAState *s = data;
|
2020-10-26 12:05:38 +03:00
|
|
|
gchar buf[QGA_READ_COUNT_DEFAULT + 1];
|
2011-07-21 00:19:37 +04:00
|
|
|
gsize count;
|
2012-01-19 10:18:20 +04:00
|
|
|
GIOStatus status = ga_channel_read(s->channel, buf, QGA_READ_COUNT_DEFAULT, &count);
|
2011-07-21 00:19:37 +04:00
|
|
|
switch (status) {
|
|
|
|
case G_IO_STATUS_ERROR:
|
2012-01-19 10:18:20 +04:00
|
|
|
g_warning("error reading channel");
|
2018-10-07 14:02:20 +03:00
|
|
|
stop_agent(s, false);
|
2011-07-21 00:19:37 +04:00
|
|
|
return false;
|
|
|
|
case G_IO_STATUS_NORMAL:
|
2012-01-19 10:18:20 +04:00
|
|
|
buf[count] = 0;
|
2011-07-21 00:19:37 +04:00
|
|
|
g_debug("read data, count: %d, data: %s", (int)count, buf);
|
|
|
|
json_message_parser_feed(&s->parser, (char *)buf, (int)count);
|
2012-01-19 10:18:20 +04:00
|
|
|
break;
|
|
|
|
case G_IO_STATUS_EOF:
|
|
|
|
g_debug("received EOF");
|
|
|
|
if (!s->virtio) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-11 14:24:57 +04:00
|
|
|
/* fall through */
|
2011-07-21 00:19:37 +04:00
|
|
|
case G_IO_STATUS_AGAIN:
|
|
|
|
/* virtio causes us to spin here when no process is attached to
|
|
|
|
* host-side chardev. sleep a bit to mitigate this
|
|
|
|
*/
|
|
|
|
if (s->virtio) {
|
2022-04-20 16:25:54 +03:00
|
|
|
g_usleep(G_USEC_PER_SEC / 10);
|
2011-07-21 00:19:37 +04:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
g_warning("unknown channel read status, closing");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-06 18:29:30 +03:00
|
|
|
static gboolean channel_init(GAState *s, const gchar *method, const gchar *path,
|
|
|
|
int listen_fd)
|
2011-07-21 00:19:37 +04:00
|
|
|
{
|
2012-01-19 10:18:20 +04:00
|
|
|
GAChannelMethod channel_method;
|
2011-07-21 00:19:37 +04:00
|
|
|
|
2012-01-19 10:18:20 +04:00
|
|
|
if (strcmp(method, "virtio-serial") == 0) {
|
|
|
|
s->virtio = true; /* virtio requires special handling in some cases */
|
|
|
|
channel_method = GA_CHANNEL_VIRTIO_SERIAL;
|
|
|
|
} else if (strcmp(method, "isa-serial") == 0) {
|
|
|
|
channel_method = GA_CHANNEL_ISA_SERIAL;
|
|
|
|
} else if (strcmp(method, "unix-listen") == 0) {
|
|
|
|
channel_method = GA_CHANNEL_UNIX_LISTEN;
|
2016-10-14 12:00:56 +03:00
|
|
|
} else if (strcmp(method, "vsock-listen") == 0) {
|
|
|
|
channel_method = GA_CHANNEL_VSOCK_LISTEN;
|
2011-07-21 00:19:37 +04:00
|
|
|
} else {
|
2012-01-19 10:18:20 +04:00
|
|
|
g_critical("unsupported channel method/type: %s", method);
|
|
|
|
return false;
|
2011-07-21 00:19:37 +04:00
|
|
|
}
|
|
|
|
|
2017-01-06 18:29:30 +03:00
|
|
|
s->channel = ga_channel_new(channel_method, path, listen_fd,
|
|
|
|
channel_event_cb, s);
|
2012-01-19 10:18:20 +04:00
|
|
|
if (!s->channel) {
|
|
|
|
g_critical("failed to create guest agent channel");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2011-07-21 00:19:37 +04:00
|
|
|
}
|
|
|
|
|
2012-01-22 02:42:27 +04:00
|
|
|
#ifdef _WIN32
|
2018-10-07 14:02:23 +03:00
|
|
|
DWORD WINAPI handle_serial_device_events(DWORD type, LPVOID data)
|
|
|
|
{
|
|
|
|
DWORD ret = NO_ERROR;
|
|
|
|
PDEV_BROADCAST_HDR broadcast_header = (PDEV_BROADCAST_HDR)data;
|
|
|
|
|
|
|
|
if (broadcast_header->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) {
|
|
|
|
switch (type) {
|
|
|
|
/* Device inserted */
|
|
|
|
case DBT_DEVICEARRIVAL:
|
|
|
|
/* Start QEMU-ga's service */
|
|
|
|
if (!SetEvent(ga_state->wakeup_event)) {
|
|
|
|
ret = GetLastError();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
/* Device removed */
|
|
|
|
case DBT_DEVICEQUERYREMOVE:
|
|
|
|
case DBT_DEVICEREMOVEPENDING:
|
|
|
|
case DBT_DEVICEREMOVECOMPLETE:
|
|
|
|
/* Stop QEMU-ga's service */
|
|
|
|
if (!ResetEvent(ga_state->wakeup_event)) {
|
|
|
|
ret = GetLastError();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = ERROR_CALL_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-01-22 02:42:27 +04:00
|
|
|
DWORD WINAPI service_ctrl_handler(DWORD ctrl, DWORD type, LPVOID data,
|
|
|
|
LPVOID ctx)
|
|
|
|
{
|
|
|
|
DWORD ret = NO_ERROR;
|
|
|
|
GAService *service = &ga_state->service;
|
|
|
|
|
2020-10-26 12:05:46 +03:00
|
|
|
switch (ctrl) {
|
2020-10-26 12:05:49 +03:00
|
|
|
case SERVICE_CONTROL_STOP:
|
|
|
|
case SERVICE_CONTROL_SHUTDOWN:
|
|
|
|
quit_handler(SIGTERM);
|
|
|
|
SetEvent(ga_state->wakeup_event);
|
|
|
|
service->status.dwCurrentState = SERVICE_STOP_PENDING;
|
|
|
|
SetServiceStatus(service->status_handle, &service->status);
|
|
|
|
break;
|
|
|
|
case SERVICE_CONTROL_DEVICEEVENT:
|
|
|
|
handle_serial_device_events(type, data);
|
|
|
|
break;
|
2012-01-22 02:42:27 +04:00
|
|
|
|
2020-10-26 12:05:49 +03:00
|
|
|
default:
|
|
|
|
ret = ERROR_CALL_NOT_IMPLEMENTED;
|
2012-01-22 02:42:27 +04:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID WINAPI service_main(DWORD argc, TCHAR *argv[])
|
|
|
|
{
|
|
|
|
GAService *service = &ga_state->service;
|
|
|
|
|
|
|
|
service->status_handle = RegisterServiceCtrlHandlerEx(QGA_SERVICE_NAME,
|
|
|
|
service_ctrl_handler, NULL);
|
|
|
|
|
|
|
|
if (service->status_handle == 0) {
|
|
|
|
g_critical("Failed to register extended requests function!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
service->status.dwServiceType = SERVICE_WIN32;
|
|
|
|
service->status.dwCurrentState = SERVICE_RUNNING;
|
|
|
|
service->status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
|
|
|
|
service->status.dwWin32ExitCode = NO_ERROR;
|
|
|
|
service->status.dwServiceSpecificExitCode = NO_ERROR;
|
|
|
|
service->status.dwCheckPoint = 0;
|
|
|
|
service->status.dwWaitHint = 0;
|
2018-10-07 14:02:23 +03:00
|
|
|
DEV_BROADCAST_DEVICEINTERFACE notification_filter;
|
|
|
|
ZeroMemory(¬ification_filter, sizeof(notification_filter));
|
|
|
|
notification_filter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
|
|
|
|
notification_filter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
|
|
|
|
notification_filter.dbcc_classguid = GUID_VIOSERIAL_PORT;
|
|
|
|
|
|
|
|
service->device_notification_handle =
|
|
|
|
RegisterDeviceNotification(service->status_handle,
|
|
|
|
¬ification_filter, DEVICE_NOTIFY_SERVICE_HANDLE);
|
|
|
|
if (!service->device_notification_handle) {
|
|
|
|
g_critical("Failed to register device notification handle!\n");
|
|
|
|
return;
|
|
|
|
}
|
2012-01-22 02:42:27 +04:00
|
|
|
SetServiceStatus(service->status_handle, &service->status);
|
|
|
|
|
2018-10-07 14:02:19 +03:00
|
|
|
run_agent(ga_state);
|
2012-01-22 02:42:27 +04:00
|
|
|
|
2018-10-07 14:02:23 +03:00
|
|
|
UnregisterDeviceNotification(service->device_notification_handle);
|
2012-01-22 02:42:27 +04:00
|
|
|
service->status.dwCurrentState = SERVICE_STOPPED;
|
|
|
|
SetServiceStatus(service->status_handle, &service->status);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-03-01 21:40:27 +04:00
|
|
|
static void set_persistent_state_defaults(GAPersistentState *pstate)
|
|
|
|
{
|
|
|
|
g_assert(pstate);
|
|
|
|
pstate->fd_counter = QGA_PSTATE_DEFAULT_FD_COUNTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void persistent_state_from_keyfile(GAPersistentState *pstate,
|
|
|
|
GKeyFile *keyfile)
|
|
|
|
{
|
|
|
|
g_assert(pstate);
|
|
|
|
g_assert(keyfile);
|
|
|
|
/* if any fields are missing, either because the file was tampered with
|
|
|
|
* by agents of chaos, or because the field wasn't present at the time the
|
|
|
|
* file was created, the best we can ever do is start over with the default
|
|
|
|
* values. so load them now, and ignore any errors in accessing key-value
|
|
|
|
* pairs
|
|
|
|
*/
|
|
|
|
set_persistent_state_defaults(pstate);
|
|
|
|
|
|
|
|
if (g_key_file_has_key(keyfile, "global", "fd_counter", NULL)) {
|
|
|
|
pstate->fd_counter =
|
2013-03-15 09:04:39 +04:00
|
|
|
g_key_file_get_integer(keyfile, "global", "fd_counter", NULL);
|
2013-03-01 21:40:27 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void persistent_state_to_keyfile(const GAPersistentState *pstate,
|
|
|
|
GKeyFile *keyfile)
|
|
|
|
{
|
|
|
|
g_assert(pstate);
|
|
|
|
g_assert(keyfile);
|
|
|
|
|
2013-03-15 09:04:39 +04:00
|
|
|
g_key_file_set_integer(keyfile, "global", "fd_counter", pstate->fd_counter);
|
2013-03-01 21:40:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean write_persistent_state(const GAPersistentState *pstate,
|
|
|
|
const gchar *path)
|
|
|
|
{
|
|
|
|
GKeyFile *keyfile = g_key_file_new();
|
|
|
|
GError *gerr = NULL;
|
|
|
|
gboolean ret = true;
|
|
|
|
gchar *data = NULL;
|
|
|
|
gsize data_len;
|
|
|
|
|
|
|
|
g_assert(pstate);
|
|
|
|
|
|
|
|
persistent_state_to_keyfile(pstate, keyfile);
|
|
|
|
data = g_key_file_to_data(keyfile, &data_len, &gerr);
|
|
|
|
if (gerr) {
|
|
|
|
g_critical("failed to convert persistent state to string: %s",
|
|
|
|
gerr->message);
|
|
|
|
ret = false;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_file_set_contents(path, data, data_len, &gerr);
|
|
|
|
if (gerr) {
|
|
|
|
g_critical("failed to write persistent state to %s: %s",
|
|
|
|
path, gerr->message);
|
|
|
|
ret = false;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (gerr) {
|
|
|
|
g_error_free(gerr);
|
|
|
|
}
|
|
|
|
if (keyfile) {
|
|
|
|
g_key_file_free(keyfile);
|
|
|
|
}
|
|
|
|
g_free(data);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean read_persistent_state(GAPersistentState *pstate,
|
|
|
|
const gchar *path, gboolean frozen)
|
|
|
|
{
|
|
|
|
GKeyFile *keyfile = NULL;
|
|
|
|
GError *gerr = NULL;
|
|
|
|
struct stat st;
|
|
|
|
gboolean ret = true;
|
|
|
|
|
|
|
|
g_assert(pstate);
|
|
|
|
|
|
|
|
if (stat(path, &st) == -1) {
|
|
|
|
/* it's okay if state file doesn't exist, but any other error
|
|
|
|
* indicates a permissions issue or some other misconfiguration
|
|
|
|
* that we likely won't be able to recover from.
|
|
|
|
*/
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
g_critical("unable to access state file at path %s: %s",
|
|
|
|
path, strerror(errno));
|
|
|
|
ret = false;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* file doesn't exist. initialize state to default values and
|
|
|
|
* attempt to save now. (we could wait till later when we have
|
|
|
|
* modified state we need to commit, but if there's a problem,
|
|
|
|
* such as a missing parent directory, we want to catch it now)
|
|
|
|
*
|
|
|
|
* there is a potential scenario where someone either managed to
|
|
|
|
* update the agent from a version that didn't use a key store
|
|
|
|
* while qemu-ga thought the filesystem was frozen, or
|
|
|
|
* deleted the key store prior to issuing a fsfreeze, prior
|
|
|
|
* to restarting the agent. in this case we go ahead and defer
|
|
|
|
* initial creation till we actually have modified state to
|
|
|
|
* write, otherwise fail to recover from freeze.
|
|
|
|
*/
|
|
|
|
set_persistent_state_defaults(pstate);
|
|
|
|
if (!frozen) {
|
|
|
|
ret = write_persistent_state(pstate, path);
|
|
|
|
if (!ret) {
|
|
|
|
g_critical("unable to create state file at path %s", path);
|
|
|
|
ret = false;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = true;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
keyfile = g_key_file_new();
|
|
|
|
g_key_file_load_from_file(keyfile, path, 0, &gerr);
|
|
|
|
if (gerr) {
|
|
|
|
g_critical("error loading persistent state from path: %s, %s",
|
|
|
|
path, gerr->message);
|
|
|
|
ret = false;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
persistent_state_from_keyfile(pstate, keyfile);
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (keyfile) {
|
|
|
|
g_key_file_free(keyfile);
|
|
|
|
}
|
|
|
|
if (gerr) {
|
|
|
|
g_error_free(gerr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t ga_get_fd_handle(GAState *s, Error **errp)
|
|
|
|
{
|
|
|
|
int64_t handle;
|
|
|
|
|
|
|
|
g_assert(s->pstate_filepath);
|
2022-07-27 12:21:34 +03:00
|
|
|
/*
|
|
|
|
* We block commands and avoid operations that potentially require
|
2013-03-01 21:40:27 +04:00
|
|
|
* writing to disk when we're in a frozen state. this includes opening
|
|
|
|
* new files, so we should never get here in that situation
|
|
|
|
*/
|
|
|
|
g_assert(!ga_is_frozen(s));
|
|
|
|
|
|
|
|
handle = s->pstate.fd_counter++;
|
2013-03-23 00:31:07 +04:00
|
|
|
|
|
|
|
/* This should never happen on a reasonable timeframe, as guest-file-open
|
|
|
|
* would have to be issued 2^63 times */
|
|
|
|
if (s->pstate.fd_counter == INT64_MAX) {
|
|
|
|
abort();
|
2013-03-01 21:40:27 +04:00
|
|
|
}
|
2013-03-23 00:31:07 +04:00
|
|
|
|
2013-03-01 21:40:27 +04:00
|
|
|
if (!write_persistent_state(&s->pstate, s->pstate_filepath)) {
|
|
|
|
error_setg(errp, "failed to commit persistent state to disk");
|
2014-05-02 15:26:33 +04:00
|
|
|
return -1;
|
2013-03-01 21:40:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2020-03-16 20:18:24 +03:00
|
|
|
static void ga_print_cmd(const QmpCommand *cmd, void *opaque)
|
2013-10-09 07:25:07 +04:00
|
|
|
{
|
|
|
|
printf("%s\n", qmp_command_name(cmd));
|
|
|
|
}
|
|
|
|
|
2015-08-27 02:34:50 +03:00
|
|
|
static GList *split_list(const gchar *str, const gchar *delim)
|
2015-08-27 02:34:49 +03:00
|
|
|
{
|
|
|
|
GList *list = NULL;
|
2015-08-27 02:34:50 +03:00
|
|
|
int i;
|
|
|
|
gchar **strv;
|
2015-08-27 02:34:49 +03:00
|
|
|
|
2015-08-27 02:34:50 +03:00
|
|
|
strv = g_strsplit(str, delim, -1);
|
|
|
|
for (i = 0; strv[i]; i++) {
|
|
|
|
list = g_list_prepend(list, strv[i]);
|
2015-08-27 02:34:49 +03:00
|
|
|
}
|
2015-08-27 02:34:50 +03:00
|
|
|
g_free(strv);
|
2015-08-27 02:34:49 +03:00
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2018-10-07 14:02:18 +03:00
|
|
|
struct GAConfig {
|
2015-08-27 02:34:53 +03:00
|
|
|
char *channel_path;
|
|
|
|
char *method;
|
|
|
|
char *log_filepath;
|
|
|
|
char *pid_filepath;
|
2012-12-12 07:55:55 +04:00
|
|
|
#ifdef CONFIG_FSFREEZE
|
2015-08-27 02:34:53 +03:00
|
|
|
char *fsfreeze_hook;
|
2012-12-12 07:55:55 +04:00
|
|
|
#endif
|
2015-08-27 02:34:53 +03:00
|
|
|
char *state_dir;
|
2012-01-22 02:42:27 +04:00
|
|
|
#ifdef _WIN32
|
2015-08-27 02:34:53 +03:00
|
|
|
const char *service;
|
2012-01-22 02:42:27 +04:00
|
|
|
#endif
|
2022-07-27 12:21:34 +03:00
|
|
|
gchar *bliststr; /* blockedrpcs may point to this string */
|
|
|
|
GList *blockedrpcs;
|
2015-08-27 02:34:53 +03:00
|
|
|
int daemonize;
|
|
|
|
GLogLevelFlags log_level;
|
2015-08-27 02:34:58 +03:00
|
|
|
int dumpconf;
|
2018-10-07 14:02:20 +03:00
|
|
|
bool retry_path;
|
2018-10-07 14:02:18 +03:00
|
|
|
};
|
2015-08-27 02:34:53 +03:00
|
|
|
|
2015-08-27 02:34:57 +03:00
|
|
|
static void config_load(GAConfig *config)
|
|
|
|
{
|
|
|
|
GError *gerr = NULL;
|
|
|
|
GKeyFile *keyfile;
|
2020-08-18 12:59:00 +03:00
|
|
|
g_autofree char *conf = g_strdup(g_getenv("QGA_CONF")) ?: get_relocated_path(QGA_CONF_DEFAULT);
|
2022-07-27 12:21:33 +03:00
|
|
|
const gchar *blockrpcs_key = "block-rpcs";
|
2015-08-27 02:34:57 +03:00
|
|
|
|
|
|
|
/* read system config */
|
|
|
|
keyfile = g_key_file_new();
|
2015-10-02 15:58:14 +03:00
|
|
|
if (!g_key_file_load_from_file(keyfile, conf, 0, &gerr)) {
|
2015-08-27 02:34:57 +03:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
if (g_key_file_has_key(keyfile, "general", "daemon", NULL)) {
|
|
|
|
config->daemonize =
|
|
|
|
g_key_file_get_boolean(keyfile, "general", "daemon", &gerr);
|
|
|
|
}
|
|
|
|
if (g_key_file_has_key(keyfile, "general", "method", NULL)) {
|
|
|
|
config->method =
|
|
|
|
g_key_file_get_string(keyfile, "general", "method", &gerr);
|
|
|
|
}
|
|
|
|
if (g_key_file_has_key(keyfile, "general", "path", NULL)) {
|
|
|
|
config->channel_path =
|
|
|
|
g_key_file_get_string(keyfile, "general", "path", &gerr);
|
|
|
|
}
|
|
|
|
if (g_key_file_has_key(keyfile, "general", "logfile", NULL)) {
|
|
|
|
config->log_filepath =
|
|
|
|
g_key_file_get_string(keyfile, "general", "logfile", &gerr);
|
|
|
|
}
|
|
|
|
if (g_key_file_has_key(keyfile, "general", "pidfile", NULL)) {
|
|
|
|
config->pid_filepath =
|
|
|
|
g_key_file_get_string(keyfile, "general", "pidfile", &gerr);
|
|
|
|
}
|
|
|
|
#ifdef CONFIG_FSFREEZE
|
|
|
|
if (g_key_file_has_key(keyfile, "general", "fsfreeze-hook", NULL)) {
|
|
|
|
config->fsfreeze_hook =
|
|
|
|
g_key_file_get_string(keyfile,
|
|
|
|
"general", "fsfreeze-hook", &gerr);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (g_key_file_has_key(keyfile, "general", "statedir", NULL)) {
|
|
|
|
config->state_dir =
|
|
|
|
g_key_file_get_string(keyfile, "general", "statedir", &gerr);
|
|
|
|
}
|
|
|
|
if (g_key_file_has_key(keyfile, "general", "verbose", NULL) &&
|
|
|
|
g_key_file_get_boolean(keyfile, "general", "verbose", &gerr)) {
|
|
|
|
/* enable all log levels */
|
|
|
|
config->log_level = G_LOG_LEVEL_MASK;
|
|
|
|
}
|
2018-10-07 14:02:20 +03:00
|
|
|
if (g_key_file_has_key(keyfile, "general", "retry-path", NULL)) {
|
|
|
|
config->retry_path =
|
|
|
|
g_key_file_get_boolean(keyfile, "general", "retry-path", &gerr);
|
|
|
|
}
|
2022-07-27 12:21:33 +03:00
|
|
|
|
2015-08-27 02:34:57 +03:00
|
|
|
if (g_key_file_has_key(keyfile, "general", "blacklist", NULL)) {
|
2022-07-27 12:21:33 +03:00
|
|
|
g_warning("config using deprecated 'blacklist' key, should be replaced"
|
|
|
|
" with the 'block-rpcs' key.");
|
|
|
|
blockrpcs_key = "blacklist";
|
|
|
|
}
|
|
|
|
if (g_key_file_has_key(keyfile, "general", blockrpcs_key, NULL)) {
|
2015-08-27 02:34:57 +03:00
|
|
|
config->bliststr =
|
2022-07-27 12:21:33 +03:00
|
|
|
g_key_file_get_string(keyfile, "general", blockrpcs_key, &gerr);
|
2022-07-27 12:21:34 +03:00
|
|
|
config->blockedrpcs = g_list_concat(config->blockedrpcs,
|
2015-08-27 02:34:57 +03:00
|
|
|
split_list(config->bliststr, ","));
|
|
|
|
}
|
|
|
|
|
|
|
|
end:
|
|
|
|
g_key_file_free(keyfile);
|
|
|
|
if (gerr &&
|
|
|
|
!(gerr->domain == G_FILE_ERROR && gerr->code == G_FILE_ERROR_NOENT)) {
|
|
|
|
g_critical("error loading configuration from path: %s, %s",
|
2020-08-18 12:59:00 +03:00
|
|
|
conf, gerr->message);
|
2015-08-27 02:34:57 +03:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
g_clear_error(&gerr);
|
|
|
|
}
|
|
|
|
|
2015-08-27 02:34:58 +03:00
|
|
|
static gchar *list_join(GList *list, const gchar separator)
|
|
|
|
{
|
|
|
|
GString *str = g_string_new("");
|
|
|
|
|
|
|
|
while (list) {
|
|
|
|
str = g_string_append(str, (gchar *)list->data);
|
|
|
|
list = g_list_next(list);
|
|
|
|
if (list) {
|
|
|
|
str = g_string_append_c(str, separator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return g_string_free(str, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void config_dump(GAConfig *config)
|
|
|
|
{
|
|
|
|
GError *error = NULL;
|
|
|
|
GKeyFile *keyfile;
|
|
|
|
gchar *tmp;
|
|
|
|
|
|
|
|
keyfile = g_key_file_new();
|
|
|
|
g_assert(keyfile);
|
|
|
|
|
|
|
|
g_key_file_set_boolean(keyfile, "general", "daemon", config->daemonize);
|
|
|
|
g_key_file_set_string(keyfile, "general", "method", config->method);
|
2017-01-06 18:29:30 +03:00
|
|
|
if (config->channel_path) {
|
|
|
|
g_key_file_set_string(keyfile, "general", "path", config->channel_path);
|
|
|
|
}
|
2015-08-27 02:34:58 +03:00
|
|
|
if (config->log_filepath) {
|
|
|
|
g_key_file_set_string(keyfile, "general", "logfile",
|
|
|
|
config->log_filepath);
|
|
|
|
}
|
|
|
|
g_key_file_set_string(keyfile, "general", "pidfile", config->pid_filepath);
|
|
|
|
#ifdef CONFIG_FSFREEZE
|
|
|
|
if (config->fsfreeze_hook) {
|
|
|
|
g_key_file_set_string(keyfile, "general", "fsfreeze-hook",
|
|
|
|
config->fsfreeze_hook);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
g_key_file_set_string(keyfile, "general", "statedir", config->state_dir);
|
|
|
|
g_key_file_set_boolean(keyfile, "general", "verbose",
|
|
|
|
config->log_level == G_LOG_LEVEL_MASK);
|
2018-10-07 14:02:20 +03:00
|
|
|
g_key_file_set_boolean(keyfile, "general", "retry-path",
|
|
|
|
config->retry_path);
|
2022-07-27 12:21:34 +03:00
|
|
|
tmp = list_join(config->blockedrpcs, ',');
|
2022-07-27 12:21:33 +03:00
|
|
|
g_key_file_set_string(keyfile, "general", "block-rpcs", tmp);
|
2015-08-27 02:34:58 +03:00
|
|
|
g_free(tmp);
|
|
|
|
|
|
|
|
tmp = g_key_file_to_data(keyfile, NULL, &error);
|
2017-06-01 15:38:47 +03:00
|
|
|
if (error) {
|
|
|
|
g_critical("Failed to dump keyfile: %s", error->message);
|
|
|
|
g_clear_error(&error);
|
|
|
|
} else {
|
|
|
|
printf("%s", tmp);
|
|
|
|
}
|
2015-08-27 02:34:58 +03:00
|
|
|
|
|
|
|
g_free(tmp);
|
|
|
|
g_key_file_free(keyfile);
|
|
|
|
}
|
|
|
|
|
2015-08-27 02:34:57 +03:00
|
|
|
static void config_parse(GAConfig *config, int argc, char **argv)
|
2015-08-27 02:34:53 +03:00
|
|
|
{
|
2018-10-07 14:02:20 +03:00
|
|
|
const char *sopt = "hVvdm:p:l:f:F::b:s:t:Dr";
|
2015-08-27 02:34:53 +03:00
|
|
|
int opt_ind = 0, ch;
|
2011-07-21 00:19:37 +04:00
|
|
|
const struct option lopt[] = {
|
|
|
|
{ "help", 0, NULL, 'h' },
|
|
|
|
{ "version", 0, NULL, 'V' },
|
2015-08-27 02:34:58 +03:00
|
|
|
{ "dump-conf", 0, NULL, 'D' },
|
2012-01-22 02:42:27 +04:00
|
|
|
{ "logfile", 1, NULL, 'l' },
|
|
|
|
{ "pidfile", 1, NULL, 'f' },
|
2012-12-12 07:55:55 +04:00
|
|
|
#ifdef CONFIG_FSFREEZE
|
|
|
|
{ "fsfreeze-hook", 2, NULL, 'F' },
|
|
|
|
#endif
|
2011-07-21 00:19:37 +04:00
|
|
|
{ "verbose", 0, NULL, 'v' },
|
2012-01-22 02:42:27 +04:00
|
|
|
{ "method", 1, NULL, 'm' },
|
|
|
|
{ "path", 1, NULL, 'p' },
|
2011-07-21 00:19:37 +04:00
|
|
|
{ "daemonize", 0, NULL, 'd' },
|
2022-07-27 12:21:33 +03:00
|
|
|
{ "block-rpcs", 1, NULL, 'b' },
|
|
|
|
{ "blacklist", 1, NULL, 'b' }, /* deprecated alias for 'block-rpcs' */
|
2012-01-22 02:42:27 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
{ "service", 1, NULL, 's' },
|
2012-04-18 04:01:45 +04:00
|
|
|
#endif
|
qemu-ga: persist tracking of fsfreeze state via filesystem
Currently, qemu-ga may die/get killed/go away for whatever reason after
guest-fsfreeze-freeze has been issued, and before guest-fsfreeze-thaw
has been issued. This means the only way to unfreeze the guest is via
VNC/network/console access, but obtaining that access after-the-fact can
often be very difficult when filesystems are frozen. Logins will almost
always hang, for instance. In many cases the only recourse would be to
reboot the guest without any quiescing of volatile state, which makes
this a corner-case worth giving some attention to.
A likely failsafe for this situation would be to use a watchdog to
restart qemu-ga if it goes away. There are some precautions qemu-ga
needs to take in order to avoid immediately hanging itself on I/O,
however, namely, we must disable logging and defer to processing/creation
of user-specific logfiles, along with creation of the pid file if we're
running as a daemon. We also need to disable non-fsfreeze-safe commands,
as we normally would when processing the guest-fsfreeze-freeze command.
To track when we need to do this in a way that persists between multiple
invocations of qemu-ga, we create a file on the guest filesystem before
issuing the fsfreeze, and delete it when doing the thaw. On qemu-ga
startup, we check for the existance of this file to determine
the need to take the above precautions.
We're forced to do it this way since a more traditional approach such as
reading/writing state to a dedicated state file will cause
access/modification time updates, respectively, both of which will hang
if the file resides on a frozen filesystem. Both can occur even if
relatime is enabled. Checking for file existence will not update the
access time, however, so it's a safe way to check for fsfreeze state.
An actual watchdog-based restart of qemu-ga can itself cause an access
time update that would thus hang the invocation of qemu-ga, but the
logic to workaround that can be handled via the watchdog, so we don't
address that here (for relatime we'd periodically touch the qemu-ga
binary if the file $qga_statedir/qga.state.isfrozen is not present, this
avoids qemu-ga updates or the 1 day relatime threshold causing an
access-time update if we try to respawn qemu-ga shortly after it goes
away)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-04-19 01:28:01 +04:00
|
|
|
{ "statedir", 1, NULL, 't' },
|
2018-10-07 14:02:20 +03:00
|
|
|
{ "retry-path", 0, NULL, 'r' },
|
2011-07-21 00:19:37 +04:00
|
|
|
{ NULL, 0, NULL, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
|
|
|
|
switch (ch) {
|
|
|
|
case 'm':
|
2015-08-27 02:34:57 +03:00
|
|
|
g_free(config->method);
|
2015-08-27 02:34:53 +03:00
|
|
|
config->method = g_strdup(optarg);
|
2011-07-21 00:19:37 +04:00
|
|
|
break;
|
|
|
|
case 'p':
|
2015-08-27 02:34:57 +03:00
|
|
|
g_free(config->channel_path);
|
2015-08-27 02:34:53 +03:00
|
|
|
config->channel_path = g_strdup(optarg);
|
2011-07-21 00:19:37 +04:00
|
|
|
break;
|
|
|
|
case 'l':
|
2015-08-27 02:34:57 +03:00
|
|
|
g_free(config->log_filepath);
|
2015-08-27 02:34:53 +03:00
|
|
|
config->log_filepath = g_strdup(optarg);
|
2011-07-21 00:19:37 +04:00
|
|
|
break;
|
|
|
|
case 'f':
|
2015-08-27 02:34:57 +03:00
|
|
|
g_free(config->pid_filepath);
|
2015-08-27 02:34:53 +03:00
|
|
|
config->pid_filepath = g_strdup(optarg);
|
2011-07-21 00:19:37 +04:00
|
|
|
break;
|
2012-12-12 07:55:55 +04:00
|
|
|
#ifdef CONFIG_FSFREEZE
|
|
|
|
case 'F':
|
2015-08-27 02:34:57 +03:00
|
|
|
g_free(config->fsfreeze_hook);
|
2020-08-18 12:59:00 +03:00
|
|
|
config->fsfreeze_hook = optarg ? g_strdup(optarg) : get_relocated_path(QGA_FSFREEZE_HOOK_DEFAULT);
|
2012-12-12 07:55:55 +04:00
|
|
|
break;
|
|
|
|
#endif
|
qemu-ga: persist tracking of fsfreeze state via filesystem
Currently, qemu-ga may die/get killed/go away for whatever reason after
guest-fsfreeze-freeze has been issued, and before guest-fsfreeze-thaw
has been issued. This means the only way to unfreeze the guest is via
VNC/network/console access, but obtaining that access after-the-fact can
often be very difficult when filesystems are frozen. Logins will almost
always hang, for instance. In many cases the only recourse would be to
reboot the guest without any quiescing of volatile state, which makes
this a corner-case worth giving some attention to.
A likely failsafe for this situation would be to use a watchdog to
restart qemu-ga if it goes away. There are some precautions qemu-ga
needs to take in order to avoid immediately hanging itself on I/O,
however, namely, we must disable logging and defer to processing/creation
of user-specific logfiles, along with creation of the pid file if we're
running as a daemon. We also need to disable non-fsfreeze-safe commands,
as we normally would when processing the guest-fsfreeze-freeze command.
To track when we need to do this in a way that persists between multiple
invocations of qemu-ga, we create a file on the guest filesystem before
issuing the fsfreeze, and delete it when doing the thaw. On qemu-ga
startup, we check for the existance of this file to determine
the need to take the above precautions.
We're forced to do it this way since a more traditional approach such as
reading/writing state to a dedicated state file will cause
access/modification time updates, respectively, both of which will hang
if the file resides on a frozen filesystem. Both can occur even if
relatime is enabled. Checking for file existence will not update the
access time, however, so it's a safe way to check for fsfreeze state.
An actual watchdog-based restart of qemu-ga can itself cause an access
time update that would thus hang the invocation of qemu-ga, but the
logic to workaround that can be handled via the watchdog, so we don't
address that here (for relatime we'd periodically touch the qemu-ga
binary if the file $qga_statedir/qga.state.isfrozen is not present, this
avoids qemu-ga updates or the 1 day relatime threshold causing an
access-time update if we try to respawn qemu-ga shortly after it goes
away)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-04-19 01:28:01 +04:00
|
|
|
case 't':
|
2015-08-27 02:34:57 +03:00
|
|
|
g_free(config->state_dir);
|
2015-08-27 02:34:53 +03:00
|
|
|
config->state_dir = g_strdup(optarg);
|
2015-08-27 02:34:52 +03:00
|
|
|
break;
|
2011-07-21 00:19:37 +04:00
|
|
|
case 'v':
|
|
|
|
/* enable all log levels */
|
2015-08-27 02:34:53 +03:00
|
|
|
config->log_level = G_LOG_LEVEL_MASK;
|
2011-07-21 00:19:37 +04:00
|
|
|
break;
|
|
|
|
case 'V':
|
2012-05-14 18:33:48 +04:00
|
|
|
printf("QEMU Guest Agent %s\n", QEMU_VERSION);
|
2015-08-27 02:34:48 +03:00
|
|
|
exit(EXIT_SUCCESS);
|
2011-07-21 00:19:37 +04:00
|
|
|
case 'd':
|
2015-08-27 02:34:53 +03:00
|
|
|
config->daemonize = 1;
|
2011-07-21 00:19:37 +04:00
|
|
|
break;
|
2015-08-27 02:34:58 +03:00
|
|
|
case 'D':
|
|
|
|
config->dumpconf = 1;
|
|
|
|
break;
|
2018-10-07 14:02:20 +03:00
|
|
|
case 'r':
|
|
|
|
config->retry_path = true;
|
|
|
|
break;
|
2011-12-07 08:03:42 +04:00
|
|
|
case 'b': {
|
2012-08-02 16:45:54 +04:00
|
|
|
if (is_help_option(optarg)) {
|
2017-03-03 15:32:25 +03:00
|
|
|
qmp_for_each_command(&ga_commands, ga_print_cmd, NULL);
|
2015-08-27 02:34:48 +03:00
|
|
|
exit(EXIT_SUCCESS);
|
2011-12-07 08:03:42 +04:00
|
|
|
}
|
2022-07-27 12:21:34 +03:00
|
|
|
config->blockedrpcs = g_list_concat(config->blockedrpcs,
|
|
|
|
split_list(optarg, ","));
|
2011-12-07 08:03:42 +04:00
|
|
|
break;
|
|
|
|
}
|
2012-01-22 02:42:27 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
case 's':
|
2015-08-27 02:34:53 +03:00
|
|
|
config->service = optarg;
|
|
|
|
if (strcmp(config->service, "install") == 0) {
|
2013-08-07 19:40:32 +04:00
|
|
|
if (ga_install_vss_provider()) {
|
2015-08-27 02:34:48 +03:00
|
|
|
exit(EXIT_FAILURE);
|
2013-08-07 19:40:32 +04:00
|
|
|
}
|
2015-08-27 02:34:53 +03:00
|
|
|
if (ga_install_service(config->channel_path,
|
|
|
|
config->log_filepath, config->state_dir)) {
|
2015-08-27 02:34:48 +03:00
|
|
|
exit(EXIT_FAILURE);
|
2013-08-07 19:40:32 +04:00
|
|
|
}
|
2015-08-27 02:34:48 +03:00
|
|
|
exit(EXIT_SUCCESS);
|
2015-08-27 02:34:53 +03:00
|
|
|
} else if (strcmp(config->service, "uninstall") == 0) {
|
2013-08-07 19:40:32 +04:00
|
|
|
ga_uninstall_vss_provider();
|
2015-08-27 02:34:48 +03:00
|
|
|
exit(ga_uninstall_service());
|
2015-08-27 02:34:53 +03:00
|
|
|
} else if (strcmp(config->service, "vss-install") == 0) {
|
2015-05-06 14:57:37 +03:00
|
|
|
if (ga_install_vss_provider()) {
|
2015-08-27 02:34:48 +03:00
|
|
|
exit(EXIT_FAILURE);
|
2015-05-06 14:57:37 +03:00
|
|
|
}
|
2015-08-27 02:34:48 +03:00
|
|
|
exit(EXIT_SUCCESS);
|
2015-08-27 02:34:53 +03:00
|
|
|
} else if (strcmp(config->service, "vss-uninstall") == 0) {
|
2015-05-06 14:57:37 +03:00
|
|
|
ga_uninstall_vss_provider();
|
2015-08-27 02:34:48 +03:00
|
|
|
exit(EXIT_SUCCESS);
|
2012-01-22 02:42:27 +04:00
|
|
|
} else {
|
|
|
|
printf("Unknown service command.\n");
|
2015-08-27 02:34:48 +03:00
|
|
|
exit(EXIT_FAILURE);
|
2012-01-22 02:42:27 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
2011-07-21 00:19:37 +04:00
|
|
|
case 'h':
|
|
|
|
usage(argv[0]);
|
2015-08-27 02:34:48 +03:00
|
|
|
exit(EXIT_SUCCESS);
|
2011-07-21 00:19:37 +04:00
|
|
|
case '?':
|
|
|
|
g_print("Unknown option, try '%s --help' for more information.\n",
|
|
|
|
argv[0]);
|
2015-08-27 02:34:48 +03:00
|
|
|
exit(EXIT_FAILURE);
|
2011-07-21 00:19:37 +04:00
|
|
|
}
|
|
|
|
}
|
2015-08-27 02:34:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void config_free(GAConfig *config)
|
|
|
|
{
|
|
|
|
g_free(config->method);
|
|
|
|
g_free(config->log_filepath);
|
|
|
|
g_free(config->pid_filepath);
|
|
|
|
g_free(config->state_dir);
|
|
|
|
g_free(config->channel_path);
|
2015-08-27 02:34:57 +03:00
|
|
|
g_free(config->bliststr);
|
2015-08-27 02:34:53 +03:00
|
|
|
#ifdef CONFIG_FSFREEZE
|
|
|
|
g_free(config->fsfreeze_hook);
|
|
|
|
#endif
|
2022-07-27 12:21:34 +03:00
|
|
|
g_list_free_full(config->blockedrpcs, g_free);
|
2015-08-27 02:34:53 +03:00
|
|
|
g_free(config);
|
|
|
|
}
|
|
|
|
|
2015-08-27 02:34:55 +03:00
|
|
|
static bool check_is_frozen(GAState *s)
|
2015-08-27 02:34:53 +03:00
|
|
|
{
|
qemu-ga: persist tracking of fsfreeze state via filesystem
Currently, qemu-ga may die/get killed/go away for whatever reason after
guest-fsfreeze-freeze has been issued, and before guest-fsfreeze-thaw
has been issued. This means the only way to unfreeze the guest is via
VNC/network/console access, but obtaining that access after-the-fact can
often be very difficult when filesystems are frozen. Logins will almost
always hang, for instance. In many cases the only recourse would be to
reboot the guest without any quiescing of volatile state, which makes
this a corner-case worth giving some attention to.
A likely failsafe for this situation would be to use a watchdog to
restart qemu-ga if it goes away. There are some precautions qemu-ga
needs to take in order to avoid immediately hanging itself on I/O,
however, namely, we must disable logging and defer to processing/creation
of user-specific logfiles, along with creation of the pid file if we're
running as a daemon. We also need to disable non-fsfreeze-safe commands,
as we normally would when processing the guest-fsfreeze-freeze command.
To track when we need to do this in a way that persists between multiple
invocations of qemu-ga, we create a file on the guest filesystem before
issuing the fsfreeze, and delete it when doing the thaw. On qemu-ga
startup, we check for the existance of this file to determine
the need to take the above precautions.
We're forced to do it this way since a more traditional approach such as
reading/writing state to a dedicated state file will cause
access/modification time updates, respectively, both of which will hang
if the file resides on a frozen filesystem. Both can occur even if
relatime is enabled. Checking for file existence will not update the
access time, however, so it's a safe way to check for fsfreeze state.
An actual watchdog-based restart of qemu-ga can itself cause an access
time update that would thus hang the invocation of qemu-ga, but the
logic to workaround that can be handled via the watchdog, so we don't
address that here (for relatime we'd periodically touch the qemu-ga
binary if the file $qga_statedir/qga.state.isfrozen is not present, this
avoids qemu-ga updates or the 1 day relatime threshold causing an
access-time update if we try to respawn qemu-ga shortly after it goes
away)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-04-19 01:28:01 +04:00
|
|
|
#ifndef _WIN32
|
|
|
|
/* check if a previous instance of qemu-ga exited with filesystems' state
|
|
|
|
* marked as frozen. this could be a stale value (a non-qemu-ga process
|
|
|
|
* or reboot may have since unfrozen them), but better to require an
|
|
|
|
* uneeded unfreeze than to risk hanging on start-up
|
|
|
|
*/
|
|
|
|
struct stat st;
|
|
|
|
if (stat(s->state_filepath_isfrozen, &st) == -1) {
|
|
|
|
/* it's okay if the file doesn't exist, but if we can't access for
|
|
|
|
* some other reason, such as permissions, there's a configuration
|
|
|
|
* that needs to be addressed. so just bail now before we get into
|
|
|
|
* more trouble later
|
|
|
|
*/
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
g_critical("unable to access state file at path %s: %s",
|
|
|
|
s->state_filepath_isfrozen, strerror(errno));
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
g_warning("previous instance appears to have exited with frozen"
|
|
|
|
" filesystems. deferring logging/pidfile creation and"
|
|
|
|
" disabling non-fsfreeze-safe commands until"
|
|
|
|
" guest-fsfreeze-thaw is issued, or filesystems are"
|
|
|
|
" manually unfrozen and the file %s is removed",
|
|
|
|
s->state_filepath_isfrozen);
|
2015-08-27 02:34:55 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-07 14:02:18 +03:00
|
|
|
static GAState *initialize_agent(GAConfig *config, int socket_activation)
|
2015-08-27 02:34:55 +03:00
|
|
|
{
|
2018-10-07 14:02:17 +03:00
|
|
|
GAState *s = g_new0(GAState, 1);
|
|
|
|
|
|
|
|
g_assert(ga_state == NULL);
|
|
|
|
|
|
|
|
s->log_level = config->log_level;
|
|
|
|
s->log_file = stderr;
|
|
|
|
#ifdef CONFIG_FSFREEZE
|
|
|
|
s->fsfreeze_hook = config->fsfreeze_hook;
|
|
|
|
#endif
|
|
|
|
s->pstate_filepath = g_strdup_printf("%s/qga.state", config->state_dir);
|
|
|
|
s->state_filepath_isfrozen = g_strdup_printf("%s/qga.state.isfrozen",
|
|
|
|
config->state_dir);
|
|
|
|
s->frozen = check_is_frozen(s);
|
2015-08-27 02:34:55 +03:00
|
|
|
|
|
|
|
g_log_set_default_handler(ga_log, s);
|
|
|
|
g_log_set_fatal_mask(NULL, G_LOG_LEVEL_ERROR);
|
|
|
|
ga_enable_logging(s);
|
|
|
|
|
2022-05-23 22:16:44 +03:00
|
|
|
g_debug("Guest agent version %s started", QEMU_FULL_VERSION);
|
|
|
|
|
2015-08-27 02:34:55 +03:00
|
|
|
#ifdef _WIN32
|
|
|
|
/* On win32 the state directory is application specific (be it the default
|
|
|
|
* or a user override). We got past the command line parsing; let's create
|
|
|
|
* the directory (with any intermediate directories). If we run into an
|
|
|
|
* error later on, we won't try to clean up the directory, it is considered
|
|
|
|
* persistent.
|
|
|
|
*/
|
|
|
|
if (g_mkdir_with_parents(config->state_dir, S_IRWXU) == -1) {
|
|
|
|
g_critical("unable to create (an ancestor of) the state directory"
|
|
|
|
" '%s': %s", config->state_dir, strerror(errno));
|
2018-10-07 14:02:17 +03:00
|
|
|
return NULL;
|
qemu-ga: persist tracking of fsfreeze state via filesystem
Currently, qemu-ga may die/get killed/go away for whatever reason after
guest-fsfreeze-freeze has been issued, and before guest-fsfreeze-thaw
has been issued. This means the only way to unfreeze the guest is via
VNC/network/console access, but obtaining that access after-the-fact can
often be very difficult when filesystems are frozen. Logins will almost
always hang, for instance. In many cases the only recourse would be to
reboot the guest without any quiescing of volatile state, which makes
this a corner-case worth giving some attention to.
A likely failsafe for this situation would be to use a watchdog to
restart qemu-ga if it goes away. There are some precautions qemu-ga
needs to take in order to avoid immediately hanging itself on I/O,
however, namely, we must disable logging and defer to processing/creation
of user-specific logfiles, along with creation of the pid file if we're
running as a daemon. We also need to disable non-fsfreeze-safe commands,
as we normally would when processing the guest-fsfreeze-freeze command.
To track when we need to do this in a way that persists between multiple
invocations of qemu-ga, we create a file on the guest filesystem before
issuing the fsfreeze, and delete it when doing the thaw. On qemu-ga
startup, we check for the existance of this file to determine
the need to take the above precautions.
We're forced to do it this way since a more traditional approach such as
reading/writing state to a dedicated state file will cause
access/modification time updates, respectively, both of which will hang
if the file resides on a frozen filesystem. Both can occur even if
relatime is enabled. Checking for file existence will not update the
access time, however, so it's a safe way to check for fsfreeze state.
An actual watchdog-based restart of qemu-ga can itself cause an access
time update that would thus hang the invocation of qemu-ga, but the
logic to workaround that can be handled via the watchdog, so we don't
address that here (for relatime we'd periodically touch the qemu-ga
binary if the file $qga_statedir/qga.state.isfrozen is not present, this
avoids qemu-ga updates or the 1 day relatime threshold causing an
access-time update if we try to respawn qemu-ga shortly after it goes
away)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-04-19 01:28:01 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (ga_is_frozen(s)) {
|
2015-08-27 02:34:53 +03:00
|
|
|
if (config->daemonize) {
|
2014-04-29 10:24:34 +04:00
|
|
|
/* delay opening/locking of pidfile till filesystems are unfrozen */
|
2015-08-27 02:34:53 +03:00
|
|
|
s->deferred_options.pid_filepath = config->pid_filepath;
|
qemu-ga: persist tracking of fsfreeze state via filesystem
Currently, qemu-ga may die/get killed/go away for whatever reason after
guest-fsfreeze-freeze has been issued, and before guest-fsfreeze-thaw
has been issued. This means the only way to unfreeze the guest is via
VNC/network/console access, but obtaining that access after-the-fact can
often be very difficult when filesystems are frozen. Logins will almost
always hang, for instance. In many cases the only recourse would be to
reboot the guest without any quiescing of volatile state, which makes
this a corner-case worth giving some attention to.
A likely failsafe for this situation would be to use a watchdog to
restart qemu-ga if it goes away. There are some precautions qemu-ga
needs to take in order to avoid immediately hanging itself on I/O,
however, namely, we must disable logging and defer to processing/creation
of user-specific logfiles, along with creation of the pid file if we're
running as a daemon. We also need to disable non-fsfreeze-safe commands,
as we normally would when processing the guest-fsfreeze-freeze command.
To track when we need to do this in a way that persists between multiple
invocations of qemu-ga, we create a file on the guest filesystem before
issuing the fsfreeze, and delete it when doing the thaw. On qemu-ga
startup, we check for the existance of this file to determine
the need to take the above precautions.
We're forced to do it this way since a more traditional approach such as
reading/writing state to a dedicated state file will cause
access/modification time updates, respectively, both of which will hang
if the file resides on a frozen filesystem. Both can occur even if
relatime is enabled. Checking for file existence will not update the
access time, however, so it's a safe way to check for fsfreeze state.
An actual watchdog-based restart of qemu-ga can itself cause an access
time update that would thus hang the invocation of qemu-ga, but the
logic to workaround that can be handled via the watchdog, so we don't
address that here (for relatime we'd periodically touch the qemu-ga
binary if the file $qga_statedir/qga.state.isfrozen is not present, this
avoids qemu-ga updates or the 1 day relatime threshold causing an
access-time update if we try to respawn qemu-ga shortly after it goes
away)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-04-19 01:28:01 +04:00
|
|
|
become_daemon(NULL);
|
|
|
|
}
|
2015-08-27 02:34:53 +03:00
|
|
|
if (config->log_filepath) {
|
qemu-ga: persist tracking of fsfreeze state via filesystem
Currently, qemu-ga may die/get killed/go away for whatever reason after
guest-fsfreeze-freeze has been issued, and before guest-fsfreeze-thaw
has been issued. This means the only way to unfreeze the guest is via
VNC/network/console access, but obtaining that access after-the-fact can
often be very difficult when filesystems are frozen. Logins will almost
always hang, for instance. In many cases the only recourse would be to
reboot the guest without any quiescing of volatile state, which makes
this a corner-case worth giving some attention to.
A likely failsafe for this situation would be to use a watchdog to
restart qemu-ga if it goes away. There are some precautions qemu-ga
needs to take in order to avoid immediately hanging itself on I/O,
however, namely, we must disable logging and defer to processing/creation
of user-specific logfiles, along with creation of the pid file if we're
running as a daemon. We also need to disable non-fsfreeze-safe commands,
as we normally would when processing the guest-fsfreeze-freeze command.
To track when we need to do this in a way that persists between multiple
invocations of qemu-ga, we create a file on the guest filesystem before
issuing the fsfreeze, and delete it when doing the thaw. On qemu-ga
startup, we check for the existance of this file to determine
the need to take the above precautions.
We're forced to do it this way since a more traditional approach such as
reading/writing state to a dedicated state file will cause
access/modification time updates, respectively, both of which will hang
if the file resides on a frozen filesystem. Both can occur even if
relatime is enabled. Checking for file existence will not update the
access time, however, so it's a safe way to check for fsfreeze state.
An actual watchdog-based restart of qemu-ga can itself cause an access
time update that would thus hang the invocation of qemu-ga, but the
logic to workaround that can be handled via the watchdog, so we don't
address that here (for relatime we'd periodically touch the qemu-ga
binary if the file $qga_statedir/qga.state.isfrozen is not present, this
avoids qemu-ga updates or the 1 day relatime threshold causing an
access-time update if we try to respawn qemu-ga shortly after it goes
away)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-04-19 01:28:01 +04:00
|
|
|
/* delay opening the log file till filesystems are unfrozen */
|
2015-08-27 02:34:53 +03:00
|
|
|
s->deferred_options.log_filepath = config->log_filepath;
|
qemu-ga: persist tracking of fsfreeze state via filesystem
Currently, qemu-ga may die/get killed/go away for whatever reason after
guest-fsfreeze-freeze has been issued, and before guest-fsfreeze-thaw
has been issued. This means the only way to unfreeze the guest is via
VNC/network/console access, but obtaining that access after-the-fact can
often be very difficult when filesystems are frozen. Logins will almost
always hang, for instance. In many cases the only recourse would be to
reboot the guest without any quiescing of volatile state, which makes
this a corner-case worth giving some attention to.
A likely failsafe for this situation would be to use a watchdog to
restart qemu-ga if it goes away. There are some precautions qemu-ga
needs to take in order to avoid immediately hanging itself on I/O,
however, namely, we must disable logging and defer to processing/creation
of user-specific logfiles, along with creation of the pid file if we're
running as a daemon. We also need to disable non-fsfreeze-safe commands,
as we normally would when processing the guest-fsfreeze-freeze command.
To track when we need to do this in a way that persists between multiple
invocations of qemu-ga, we create a file on the guest filesystem before
issuing the fsfreeze, and delete it when doing the thaw. On qemu-ga
startup, we check for the existance of this file to determine
the need to take the above precautions.
We're forced to do it this way since a more traditional approach such as
reading/writing state to a dedicated state file will cause
access/modification time updates, respectively, both of which will hang
if the file resides on a frozen filesystem. Both can occur even if
relatime is enabled. Checking for file existence will not update the
access time, however, so it's a safe way to check for fsfreeze state.
An actual watchdog-based restart of qemu-ga can itself cause an access
time update that would thus hang the invocation of qemu-ga, but the
logic to workaround that can be handled via the watchdog, so we don't
address that here (for relatime we'd periodically touch the qemu-ga
binary if the file $qga_statedir/qga.state.isfrozen is not present, this
avoids qemu-ga updates or the 1 day relatime threshold causing an
access-time update if we try to respawn qemu-ga shortly after it goes
away)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-04-19 01:28:01 +04:00
|
|
|
}
|
|
|
|
ga_disable_logging(s);
|
2022-07-27 12:21:34 +03:00
|
|
|
qmp_for_each_command(&ga_commands, ga_disable_not_allowed, NULL);
|
qemu-ga: persist tracking of fsfreeze state via filesystem
Currently, qemu-ga may die/get killed/go away for whatever reason after
guest-fsfreeze-freeze has been issued, and before guest-fsfreeze-thaw
has been issued. This means the only way to unfreeze the guest is via
VNC/network/console access, but obtaining that access after-the-fact can
often be very difficult when filesystems are frozen. Logins will almost
always hang, for instance. In many cases the only recourse would be to
reboot the guest without any quiescing of volatile state, which makes
this a corner-case worth giving some attention to.
A likely failsafe for this situation would be to use a watchdog to
restart qemu-ga if it goes away. There are some precautions qemu-ga
needs to take in order to avoid immediately hanging itself on I/O,
however, namely, we must disable logging and defer to processing/creation
of user-specific logfiles, along with creation of the pid file if we're
running as a daemon. We also need to disable non-fsfreeze-safe commands,
as we normally would when processing the guest-fsfreeze-freeze command.
To track when we need to do this in a way that persists between multiple
invocations of qemu-ga, we create a file on the guest filesystem before
issuing the fsfreeze, and delete it when doing the thaw. On qemu-ga
startup, we check for the existance of this file to determine
the need to take the above precautions.
We're forced to do it this way since a more traditional approach such as
reading/writing state to a dedicated state file will cause
access/modification time updates, respectively, both of which will hang
if the file resides on a frozen filesystem. Both can occur even if
relatime is enabled. Checking for file existence will not update the
access time, however, so it's a safe way to check for fsfreeze state.
An actual watchdog-based restart of qemu-ga can itself cause an access
time update that would thus hang the invocation of qemu-ga, but the
logic to workaround that can be handled via the watchdog, so we don't
address that here (for relatime we'd periodically touch the qemu-ga
binary if the file $qga_statedir/qga.state.isfrozen is not present, this
avoids qemu-ga updates or the 1 day relatime threshold causing an
access-time update if we try to respawn qemu-ga shortly after it goes
away)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-04-19 01:28:01 +04:00
|
|
|
} else {
|
2015-08-27 02:34:53 +03:00
|
|
|
if (config->daemonize) {
|
|
|
|
become_daemon(config->pid_filepath);
|
qemu-ga: persist tracking of fsfreeze state via filesystem
Currently, qemu-ga may die/get killed/go away for whatever reason after
guest-fsfreeze-freeze has been issued, and before guest-fsfreeze-thaw
has been issued. This means the only way to unfreeze the guest is via
VNC/network/console access, but obtaining that access after-the-fact can
often be very difficult when filesystems are frozen. Logins will almost
always hang, for instance. In many cases the only recourse would be to
reboot the guest without any quiescing of volatile state, which makes
this a corner-case worth giving some attention to.
A likely failsafe for this situation would be to use a watchdog to
restart qemu-ga if it goes away. There are some precautions qemu-ga
needs to take in order to avoid immediately hanging itself on I/O,
however, namely, we must disable logging and defer to processing/creation
of user-specific logfiles, along with creation of the pid file if we're
running as a daemon. We also need to disable non-fsfreeze-safe commands,
as we normally would when processing the guest-fsfreeze-freeze command.
To track when we need to do this in a way that persists between multiple
invocations of qemu-ga, we create a file on the guest filesystem before
issuing the fsfreeze, and delete it when doing the thaw. On qemu-ga
startup, we check for the existance of this file to determine
the need to take the above precautions.
We're forced to do it this way since a more traditional approach such as
reading/writing state to a dedicated state file will cause
access/modification time updates, respectively, both of which will hang
if the file resides on a frozen filesystem. Both can occur even if
relatime is enabled. Checking for file existence will not update the
access time, however, so it's a safe way to check for fsfreeze state.
An actual watchdog-based restart of qemu-ga can itself cause an access
time update that would thus hang the invocation of qemu-ga, but the
logic to workaround that can be handled via the watchdog, so we don't
address that here (for relatime we'd periodically touch the qemu-ga
binary if the file $qga_statedir/qga.state.isfrozen is not present, this
avoids qemu-ga updates or the 1 day relatime threshold causing an
access-time update if we try to respawn qemu-ga shortly after it goes
away)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-04-19 01:28:01 +04:00
|
|
|
}
|
2015-08-27 02:34:53 +03:00
|
|
|
if (config->log_filepath) {
|
|
|
|
FILE *log_file = ga_open_logfile(config->log_filepath);
|
2012-05-15 01:42:35 +04:00
|
|
|
if (!log_file) {
|
qemu-ga: persist tracking of fsfreeze state via filesystem
Currently, qemu-ga may die/get killed/go away for whatever reason after
guest-fsfreeze-freeze has been issued, and before guest-fsfreeze-thaw
has been issued. This means the only way to unfreeze the guest is via
VNC/network/console access, but obtaining that access after-the-fact can
often be very difficult when filesystems are frozen. Logins will almost
always hang, for instance. In many cases the only recourse would be to
reboot the guest without any quiescing of volatile state, which makes
this a corner-case worth giving some attention to.
A likely failsafe for this situation would be to use a watchdog to
restart qemu-ga if it goes away. There are some precautions qemu-ga
needs to take in order to avoid immediately hanging itself on I/O,
however, namely, we must disable logging and defer to processing/creation
of user-specific logfiles, along with creation of the pid file if we're
running as a daemon. We also need to disable non-fsfreeze-safe commands,
as we normally would when processing the guest-fsfreeze-freeze command.
To track when we need to do this in a way that persists between multiple
invocations of qemu-ga, we create a file on the guest filesystem before
issuing the fsfreeze, and delete it when doing the thaw. On qemu-ga
startup, we check for the existance of this file to determine
the need to take the above precautions.
We're forced to do it this way since a more traditional approach such as
reading/writing state to a dedicated state file will cause
access/modification time updates, respectively, both of which will hang
if the file resides on a frozen filesystem. Both can occur even if
relatime is enabled. Checking for file existence will not update the
access time, however, so it's a safe way to check for fsfreeze state.
An actual watchdog-based restart of qemu-ga can itself cause an access
time update that would thus hang the invocation of qemu-ga, but the
logic to workaround that can be handled via the watchdog, so we don't
address that here (for relatime we'd periodically touch the qemu-ga
binary if the file $qga_statedir/qga.state.isfrozen is not present, this
avoids qemu-ga updates or the 1 day relatime threshold causing an
access-time update if we try to respawn qemu-ga shortly after it goes
away)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-04-19 01:28:01 +04:00
|
|
|
g_critical("unable to open specified log file: %s",
|
|
|
|
strerror(errno));
|
2018-10-07 14:02:17 +03:00
|
|
|
return NULL;
|
qemu-ga: persist tracking of fsfreeze state via filesystem
Currently, qemu-ga may die/get killed/go away for whatever reason after
guest-fsfreeze-freeze has been issued, and before guest-fsfreeze-thaw
has been issued. This means the only way to unfreeze the guest is via
VNC/network/console access, but obtaining that access after-the-fact can
often be very difficult when filesystems are frozen. Logins will almost
always hang, for instance. In many cases the only recourse would be to
reboot the guest without any quiescing of volatile state, which makes
this a corner-case worth giving some attention to.
A likely failsafe for this situation would be to use a watchdog to
restart qemu-ga if it goes away. There are some precautions qemu-ga
needs to take in order to avoid immediately hanging itself on I/O,
however, namely, we must disable logging and defer to processing/creation
of user-specific logfiles, along with creation of the pid file if we're
running as a daemon. We also need to disable non-fsfreeze-safe commands,
as we normally would when processing the guest-fsfreeze-freeze command.
To track when we need to do this in a way that persists between multiple
invocations of qemu-ga, we create a file on the guest filesystem before
issuing the fsfreeze, and delete it when doing the thaw. On qemu-ga
startup, we check for the existance of this file to determine
the need to take the above precautions.
We're forced to do it this way since a more traditional approach such as
reading/writing state to a dedicated state file will cause
access/modification time updates, respectively, both of which will hang
if the file resides on a frozen filesystem. Both can occur even if
relatime is enabled. Checking for file existence will not update the
access time, however, so it's a safe way to check for fsfreeze state.
An actual watchdog-based restart of qemu-ga can itself cause an access
time update that would thus hang the invocation of qemu-ga, but the
logic to workaround that can be handled via the watchdog, so we don't
address that here (for relatime we'd periodically touch the qemu-ga
binary if the file $qga_statedir/qga.state.isfrozen is not present, this
avoids qemu-ga updates or the 1 day relatime threshold causing an
access-time update if we try to respawn qemu-ga shortly after it goes
away)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-04-19 01:28:01 +04:00
|
|
|
}
|
2012-05-15 01:42:35 +04:00
|
|
|
s->log_file = log_file;
|
qemu-ga: persist tracking of fsfreeze state via filesystem
Currently, qemu-ga may die/get killed/go away for whatever reason after
guest-fsfreeze-freeze has been issued, and before guest-fsfreeze-thaw
has been issued. This means the only way to unfreeze the guest is via
VNC/network/console access, but obtaining that access after-the-fact can
often be very difficult when filesystems are frozen. Logins will almost
always hang, for instance. In many cases the only recourse would be to
reboot the guest without any quiescing of volatile state, which makes
this a corner-case worth giving some attention to.
A likely failsafe for this situation would be to use a watchdog to
restart qemu-ga if it goes away. There are some precautions qemu-ga
needs to take in order to avoid immediately hanging itself on I/O,
however, namely, we must disable logging and defer to processing/creation
of user-specific logfiles, along with creation of the pid file if we're
running as a daemon. We also need to disable non-fsfreeze-safe commands,
as we normally would when processing the guest-fsfreeze-freeze command.
To track when we need to do this in a way that persists between multiple
invocations of qemu-ga, we create a file on the guest filesystem before
issuing the fsfreeze, and delete it when doing the thaw. On qemu-ga
startup, we check for the existance of this file to determine
the need to take the above precautions.
We're forced to do it this way since a more traditional approach such as
reading/writing state to a dedicated state file will cause
access/modification time updates, respectively, both of which will hang
if the file resides on a frozen filesystem. Both can occur even if
relatime is enabled. Checking for file existence will not update the
access time, however, so it's a safe way to check for fsfreeze state.
An actual watchdog-based restart of qemu-ga can itself cause an access
time update that would thus hang the invocation of qemu-ga, but the
logic to workaround that can be handled via the watchdog, so we don't
address that here (for relatime we'd periodically touch the qemu-ga
binary if the file $qga_statedir/qga.state.isfrozen is not present, this
avoids qemu-ga updates or the 1 day relatime threshold causing an
access-time update if we try to respawn qemu-ga shortly after it goes
away)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-04-19 01:28:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-01 21:40:27 +04:00
|
|
|
/* load persistent state from disk */
|
|
|
|
if (!read_persistent_state(&s->pstate,
|
|
|
|
s->pstate_filepath,
|
|
|
|
ga_is_frozen(s))) {
|
|
|
|
g_critical("failed to load persistent state");
|
2018-10-07 14:02:17 +03:00
|
|
|
return NULL;
|
2013-03-01 21:40:27 +04:00
|
|
|
}
|
|
|
|
|
2022-07-27 12:21:34 +03:00
|
|
|
config->blockedrpcs = ga_command_init_blockedrpcs(config->blockedrpcs);
|
|
|
|
if (config->blockedrpcs) {
|
|
|
|
GList *l = config->blockedrpcs;
|
|
|
|
s->blockedrpcs = config->blockedrpcs;
|
2012-04-18 04:01:45 +04:00
|
|
|
do {
|
2015-08-27 02:34:53 +03:00
|
|
|
g_debug("disabling command: %s", (char *)l->data);
|
2021-02-19 11:28:14 +03:00
|
|
|
qmp_disable_command(&ga_commands, l->data, NULL);
|
2015-08-27 02:34:53 +03:00
|
|
|
l = g_list_next(l);
|
|
|
|
} while (l);
|
2012-04-18 04:01:45 +04:00
|
|
|
}
|
2011-07-20 00:41:55 +04:00
|
|
|
s->command_state = ga_command_state_new();
|
|
|
|
ga_command_state_init(s, s->command_state);
|
|
|
|
ga_command_state_init_all(s->command_state);
|
2018-08-23 19:40:01 +03:00
|
|
|
json_message_parser_init(&s->parser, process_event, s, NULL);
|
2017-07-16 01:44:01 +03:00
|
|
|
|
2012-01-20 08:04:34 +04:00
|
|
|
#ifndef _WIN32
|
2012-01-19 10:18:20 +04:00
|
|
|
if (!register_signal_handlers()) {
|
|
|
|
g_critical("failed to register signal handlers");
|
2018-10-07 14:02:17 +03:00
|
|
|
return NULL;
|
2012-01-19 10:18:20 +04:00
|
|
|
}
|
2012-01-20 08:04:34 +04:00
|
|
|
#endif
|
2011-07-21 00:19:37 +04:00
|
|
|
|
2012-01-19 10:18:20 +04:00
|
|
|
s->main_loop = g_main_loop_new(NULL, false);
|
2017-01-06 18:29:30 +03:00
|
|
|
|
2018-10-07 14:02:18 +03:00
|
|
|
s->config = config;
|
|
|
|
s->socket_activation = socket_activation;
|
2018-10-07 14:02:23 +03:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
s->wakeup_event = CreateEvent(NULL, TRUE, FALSE, TEXT("WakeUp"));
|
|
|
|
if (s->wakeup_event == NULL) {
|
|
|
|
g_critical("CreateEvent failed");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-10-07 14:02:17 +03:00
|
|
|
ga_state = s;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cleanup_agent(GAState *s)
|
|
|
|
{
|
2018-10-07 14:02:23 +03:00
|
|
|
#ifdef _WIN32
|
|
|
|
CloseHandle(s->wakeup_event);
|
|
|
|
#endif
|
2018-10-07 14:02:17 +03:00
|
|
|
if (s->command_state) {
|
|
|
|
ga_command_state_cleanup_all(s->command_state);
|
|
|
|
ga_command_state_free(s->command_state);
|
|
|
|
json_message_parser_destroy(&s->parser);
|
|
|
|
}
|
|
|
|
g_free(s->pstate_filepath);
|
|
|
|
g_free(s->state_filepath_isfrozen);
|
|
|
|
if (s->main_loop) {
|
|
|
|
g_main_loop_unref(s->main_loop);
|
|
|
|
}
|
|
|
|
g_free(s);
|
|
|
|
ga_state = NULL;
|
|
|
|
}
|
|
|
|
|
2018-10-07 14:02:20 +03:00
|
|
|
static int run_agent_once(GAState *s)
|
2018-10-07 14:02:17 +03:00
|
|
|
{
|
2018-10-07 14:02:18 +03:00
|
|
|
if (!channel_init(s, s->config->method, s->config->channel_path,
|
|
|
|
s->socket_activation ? FIRST_SOCKET_ACTIVATION_FD : -1)) {
|
2012-01-19 10:18:20 +04:00
|
|
|
g_critical("failed to initialize guest agent channel");
|
2015-08-27 02:34:55 +03:00
|
|
|
return EXIT_FAILURE;
|
2012-01-19 10:18:20 +04:00
|
|
|
}
|
2018-10-07 14:02:19 +03:00
|
|
|
|
2011-07-21 00:19:37 +04:00
|
|
|
g_main_loop_run(ga_state->main_loop);
|
|
|
|
|
2018-10-07 14:02:20 +03:00
|
|
|
if (s->channel) {
|
|
|
|
ga_channel_free(s->channel);
|
|
|
|
}
|
|
|
|
|
2015-08-27 02:34:55 +03:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2011-07-21 00:19:37 +04:00
|
|
|
|
2018-10-07 14:02:23 +03:00
|
|
|
static void wait_for_channel_availability(GAState *s)
|
|
|
|
{
|
|
|
|
g_warning("waiting for channel path...");
|
|
|
|
#ifndef _WIN32
|
|
|
|
sleep(QGA_RETRY_INTERVAL);
|
|
|
|
#else
|
|
|
|
DWORD dwWaitResult;
|
|
|
|
|
|
|
|
dwWaitResult = WaitForSingleObject(s->wakeup_event, INFINITE);
|
|
|
|
|
|
|
|
switch (dwWaitResult) {
|
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
break;
|
|
|
|
case WAIT_TIMEOUT:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_critical("WaitForSingleObject failed");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-10-07 14:02:20 +03:00
|
|
|
static int run_agent(GAState *s)
|
|
|
|
{
|
|
|
|
int ret = EXIT_SUCCESS;
|
|
|
|
|
|
|
|
s->force_exit = false;
|
|
|
|
|
|
|
|
do {
|
|
|
|
ret = run_agent_once(s);
|
|
|
|
if (s->config->retry_path && !s->force_exit) {
|
|
|
|
g_warning("agent stopped unexpectedly, restarting...");
|
2018-10-07 14:02:23 +03:00
|
|
|
wait_for_channel_availability(s);
|
2018-10-07 14:02:20 +03:00
|
|
|
}
|
|
|
|
} while (s->config->retry_path && !s->force_exit);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void stop_agent(GAState *s, bool requested)
|
|
|
|
{
|
|
|
|
if (!s->force_exit) {
|
|
|
|
s->force_exit = requested;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_main_loop_is_running(s->main_loop)) {
|
|
|
|
g_main_loop_quit(s->main_loop);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-27 02:34:55 +03:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int ret = EXIT_SUCCESS;
|
2018-10-07 14:02:17 +03:00
|
|
|
GAState *s;
|
2015-08-27 02:34:57 +03:00
|
|
|
GAConfig *config = g_new0(GAConfig, 1);
|
2017-03-16 18:29:45 +03:00
|
|
|
int socket_activation;
|
2015-08-27 02:34:55 +03:00
|
|
|
|
2015-10-02 15:58:15 +03:00
|
|
|
config->log_level = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL;
|
|
|
|
|
2020-08-18 12:59:00 +03:00
|
|
|
qemu_init_exec_dir(argv[0]);
|
2017-03-03 15:32:25 +03:00
|
|
|
qga_qmp_init_marshal(&ga_commands);
|
2015-08-27 02:34:55 +03:00
|
|
|
|
|
|
|
init_dfl_pathnames();
|
2015-08-27 02:34:57 +03:00
|
|
|
config_load(config);
|
|
|
|
config_parse(config, argc, argv);
|
2015-08-27 02:34:55 +03:00
|
|
|
|
|
|
|
if (config->pid_filepath == NULL) {
|
|
|
|
config->pid_filepath = g_strdup(dfl_pathnames.pidfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config->state_dir == NULL) {
|
|
|
|
config->state_dir = g_strdup(dfl_pathnames.state_dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config->method == NULL) {
|
|
|
|
config->method = g_strdup("virtio-serial");
|
2012-01-19 10:18:20 +04:00
|
|
|
}
|
|
|
|
|
2017-03-16 18:29:45 +03:00
|
|
|
socket_activation = check_socket_activation();
|
|
|
|
if (socket_activation > 1) {
|
|
|
|
g_critical("qemu-ga only supports listening on one socket");
|
|
|
|
ret = EXIT_FAILURE;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
if (socket_activation) {
|
2017-04-26 10:36:41 +03:00
|
|
|
SocketAddress *addr;
|
2017-01-06 18:29:30 +03:00
|
|
|
|
|
|
|
g_free(config->method);
|
|
|
|
g_free(config->channel_path);
|
|
|
|
config->method = NULL;
|
|
|
|
config->channel_path = NULL;
|
|
|
|
|
2017-03-16 18:29:45 +03:00
|
|
|
addr = socket_local_address(FIRST_SOCKET_ACTIVATION_FD, NULL);
|
2017-01-06 18:29:30 +03:00
|
|
|
if (addr) {
|
2017-04-26 10:36:41 +03:00
|
|
|
if (addr->type == SOCKET_ADDRESS_TYPE_UNIX) {
|
2017-01-06 18:29:30 +03:00
|
|
|
config->method = g_strdup("unix-listen");
|
2017-04-26 10:36:41 +03:00
|
|
|
} else if (addr->type == SOCKET_ADDRESS_TYPE_VSOCK) {
|
2017-01-06 18:29:30 +03:00
|
|
|
config->method = g_strdup("vsock-listen");
|
|
|
|
}
|
|
|
|
|
2017-04-26 10:36:41 +03:00
|
|
|
qapi_free_SocketAddress(addr);
|
2017-01-06 18:29:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!config->method) {
|
|
|
|
g_critical("unsupported listen fd type");
|
|
|
|
ret = EXIT_FAILURE;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
} else if (config->channel_path == NULL) {
|
2015-08-27 02:34:55 +03:00
|
|
|
if (strcmp(config->method, "virtio-serial") == 0) {
|
|
|
|
/* try the default path for the virtio-serial port */
|
|
|
|
config->channel_path = g_strdup(QGA_VIRTIO_PATH_DEFAULT);
|
|
|
|
} else if (strcmp(config->method, "isa-serial") == 0) {
|
|
|
|
/* try the default path for the serial port - COM1 */
|
|
|
|
config->channel_path = g_strdup(QGA_SERIAL_PATH_DEFAULT);
|
|
|
|
} else {
|
|
|
|
g_critical("must specify a path for this channel");
|
|
|
|
ret = EXIT_FAILURE;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-27 02:34:58 +03:00
|
|
|
if (config->dumpconf) {
|
|
|
|
config_dump(config);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2018-10-07 14:02:18 +03:00
|
|
|
s = initialize_agent(config, socket_activation);
|
2018-10-07 14:02:17 +03:00
|
|
|
if (!s) {
|
|
|
|
g_critical("error initializing guest agent");
|
|
|
|
goto end;
|
|
|
|
}
|
2018-10-07 14:02:19 +03:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (config->daemonize) {
|
|
|
|
SERVICE_TABLE_ENTRY service_table[] = {
|
|
|
|
{ (char *)QGA_SERVICE_NAME, service_main }, { NULL, NULL } };
|
|
|
|
StartServiceCtrlDispatcher(service_table);
|
|
|
|
} else {
|
|
|
|
ret = run_agent(s);
|
|
|
|
}
|
|
|
|
#else
|
2018-10-07 14:02:18 +03:00
|
|
|
ret = run_agent(s);
|
2018-10-07 14:02:19 +03:00
|
|
|
#endif
|
|
|
|
|
2018-10-07 14:02:17 +03:00
|
|
|
cleanup_agent(s);
|
2015-08-27 02:34:55 +03:00
|
|
|
|
|
|
|
end:
|
2015-08-27 02:34:53 +03:00
|
|
|
if (config->daemonize) {
|
|
|
|
unlink(config->pid_filepath);
|
2012-01-19 10:18:20 +04:00
|
|
|
}
|
2015-08-27 02:34:52 +03:00
|
|
|
|
2015-08-27 02:34:53 +03:00
|
|
|
config_free(config);
|
2015-08-27 02:34:52 +03:00
|
|
|
|
2015-08-27 02:34:55 +03:00
|
|
|
return ret;
|
2011-07-21 00:19:37 +04:00
|
|
|
}
|