11d0f1255b
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>
136 lines
3.3 KiB
C
136 lines
3.3 KiB
C
/*
|
|
* QEMU Guest Agent win32-specific command implementations
|
|
*
|
|
* Copyright IBM Corp. 2012
|
|
*
|
|
* Authors:
|
|
* 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.
|
|
*/
|
|
|
|
#include <glib.h>
|
|
#include "qga/guest-agent-core.h"
|
|
#include "qga-qmp-commands.h"
|
|
#include "qerror.h"
|
|
|
|
#ifndef SHTDN_REASON_FLAG_PLANNED
|
|
#define SHTDN_REASON_FLAG_PLANNED 0x80000000
|
|
#endif
|
|
|
|
void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err)
|
|
{
|
|
HANDLE token;
|
|
TOKEN_PRIVILEGES priv;
|
|
UINT shutdown_flag = EWX_FORCE;
|
|
|
|
slog("guest-shutdown called, mode: %s", mode);
|
|
|
|
if (!has_mode || strcmp(mode, "powerdown") == 0) {
|
|
shutdown_flag |= EWX_POWEROFF;
|
|
} else if (strcmp(mode, "halt") == 0) {
|
|
shutdown_flag |= EWX_SHUTDOWN;
|
|
} else if (strcmp(mode, "reboot") == 0) {
|
|
shutdown_flag |= EWX_REBOOT;
|
|
} else {
|
|
error_set(err, QERR_INVALID_PARAMETER_VALUE, "mode",
|
|
"halt|powerdown|reboot");
|
|
return;
|
|
}
|
|
|
|
/* Request a shutdown privilege, but try to shut down the system
|
|
anyway. */
|
|
if (OpenProcessToken(GetCurrentProcess(),
|
|
TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &token))
|
|
{
|
|
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
|
|
&priv.Privileges[0].Luid);
|
|
|
|
priv.PrivilegeCount = 1;
|
|
priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
|
|
|
|
AdjustTokenPrivileges(token, FALSE, &priv, 0, NULL, 0);
|
|
}
|
|
|
|
if (!ExitWindowsEx(shutdown_flag, SHTDN_REASON_FLAG_PLANNED)) {
|
|
slog("guest-shutdown failed: %d", GetLastError());
|
|
error_set(err, QERR_UNDEFINED_ERROR);
|
|
}
|
|
}
|
|
|
|
int64_t qmp_guest_file_open(const char *path, bool has_mode, const char *mode, Error **err)
|
|
{
|
|
error_set(err, QERR_UNSUPPORTED);
|
|
return 0;
|
|
}
|
|
|
|
void qmp_guest_file_close(int64_t handle, Error **err)
|
|
{
|
|
error_set(err, QERR_UNSUPPORTED);
|
|
}
|
|
|
|
GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
|
|
int64_t count, Error **err)
|
|
{
|
|
error_set(err, QERR_UNSUPPORTED);
|
|
return 0;
|
|
}
|
|
|
|
GuestFileWrite *qmp_guest_file_write(int64_t handle, const char *buf_b64,
|
|
bool has_count, int64_t count, Error **err)
|
|
{
|
|
error_set(err, QERR_UNSUPPORTED);
|
|
return 0;
|
|
}
|
|
|
|
GuestFileSeek *qmp_guest_file_seek(int64_t handle, int64_t offset,
|
|
int64_t whence, Error **err)
|
|
{
|
|
error_set(err, QERR_UNSUPPORTED);
|
|
return 0;
|
|
}
|
|
|
|
void qmp_guest_file_flush(int64_t handle, Error **err)
|
|
{
|
|
error_set(err, QERR_UNSUPPORTED);
|
|
}
|
|
|
|
/*
|
|
* Return status of freeze/thaw
|
|
*/
|
|
GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **err)
|
|
{
|
|
error_set(err, QERR_UNSUPPORTED);
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* Walk list of mounted file systems in the guest, and freeze the ones which
|
|
* are real local file systems.
|
|
*/
|
|
int64_t qmp_guest_fsfreeze_freeze(Error **err)
|
|
{
|
|
error_set(err, QERR_UNSUPPORTED);
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* Walk list of frozen file systems in the guest, and thaw them.
|
|
*/
|
|
int64_t qmp_guest_fsfreeze_thaw(Error **err)
|
|
{
|
|
error_set(err, QERR_UNSUPPORTED);
|
|
return 0;
|
|
}
|
|
|
|
void qmp_guest_suspend_disk(Error **err)
|
|
{
|
|
error_set(err, QERR_UNSUPPORTED);
|
|
}
|
|
|
|
/* register init/cleanup routines for stateful command groups */
|
|
void ga_command_state_init(GAState *s, GACommandState *cs)
|
|
{
|
|
}
|