2 * QEMU Guest Agent win32-specific command implementations
4 * Copyright IBM Corp. 2012
7 * Michael Roth <mdroth@linux.vnet.ibm.com>
8 * Gal Hammer <ghammer@redhat.com>
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
17 #include "qga/guest-agent-core.h"
18 #include "qga/vss-win32.h"
19 #include "qga-qmp-commands.h"
20 #include "qapi/qmp/qerror.h"
22 #ifndef SHTDN_REASON_FLAG_PLANNED
23 #define SHTDN_REASON_FLAG_PLANNED 0x80000000
26 /* multiple of 100 nanoseconds elapsed between windows baseline
27 * (1/1/1601) and Unix Epoch (1/1/1970), accounting for leap years */
28 #define W32_FT_OFFSET (10000000ULL * 60 * 60 * 24 * \
29 (365 * (1970 - 1601) + \
30 (1970 - 1601) / 4 - 3))
32 static void acquire_privilege(const char *name
, Error
**err
)
35 TOKEN_PRIVILEGES priv
;
36 Error
*local_err
= NULL
;
38 if (error_is_set(err
)) {
42 if (OpenProcessToken(GetCurrentProcess(),
43 TOKEN_ADJUST_PRIVILEGES
|TOKEN_QUERY
, &token
))
45 if (!LookupPrivilegeValue(NULL
, name
, &priv
.Privileges
[0].Luid
)) {
46 error_set(&local_err
, QERR_QGA_COMMAND_FAILED
,
47 "no luid for requested privilege");
51 priv
.PrivilegeCount
= 1;
52 priv
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
54 if (!AdjustTokenPrivileges(token
, FALSE
, &priv
, 0, NULL
, 0)) {
55 error_set(&local_err
, QERR_QGA_COMMAND_FAILED
,
56 "unable to acquire requested privilege");
62 error_set(&local_err
, QERR_QGA_COMMAND_FAILED
,
63 "failed to open privilege token");
68 error_propagate(err
, local_err
);
72 static void execute_async(DWORD
WINAPI (*func
)(LPVOID
), LPVOID opaque
, Error
**err
)
74 Error
*local_err
= NULL
;
76 if (error_is_set(err
)) {
79 HANDLE thread
= CreateThread(NULL
, 0, func
, opaque
, 0, NULL
);
81 error_set(&local_err
, QERR_QGA_COMMAND_FAILED
,
82 "failed to dispatch asynchronous command");
83 error_propagate(err
, local_err
);
87 void qmp_guest_shutdown(bool has_mode
, const char *mode
, Error
**err
)
89 UINT shutdown_flag
= EWX_FORCE
;
91 slog("guest-shutdown called, mode: %s", mode
);
93 if (!has_mode
|| strcmp(mode
, "powerdown") == 0) {
94 shutdown_flag
|= EWX_POWEROFF
;
95 } else if (strcmp(mode
, "halt") == 0) {
96 shutdown_flag
|= EWX_SHUTDOWN
;
97 } else if (strcmp(mode
, "reboot") == 0) {
98 shutdown_flag
|= EWX_REBOOT
;
100 error_set(err
, QERR_INVALID_PARAMETER_VALUE
, "mode",
101 "halt|powerdown|reboot");
105 /* Request a shutdown privilege, but try to shut down the system
107 acquire_privilege(SE_SHUTDOWN_NAME
, err
);
108 if (error_is_set(err
)) {
112 if (!ExitWindowsEx(shutdown_flag
, SHTDN_REASON_FLAG_PLANNED
)) {
113 slog("guest-shutdown failed: %lu", GetLastError());
114 error_set(err
, QERR_UNDEFINED_ERROR
);
118 int64_t qmp_guest_file_open(const char *path
, bool has_mode
, const char *mode
, Error
**err
)
120 error_set(err
, QERR_UNSUPPORTED
);
124 void qmp_guest_file_close(int64_t handle
, Error
**err
)
126 error_set(err
, QERR_UNSUPPORTED
);
129 GuestFileRead
*qmp_guest_file_read(int64_t handle
, bool has_count
,
130 int64_t count
, Error
**err
)
132 error_set(err
, QERR_UNSUPPORTED
);
136 GuestFileWrite
*qmp_guest_file_write(int64_t handle
, const char *buf_b64
,
137 bool has_count
, int64_t count
, Error
**err
)
139 error_set(err
, QERR_UNSUPPORTED
);
143 GuestFileSeek
*qmp_guest_file_seek(int64_t handle
, int64_t offset
,
144 int64_t whence
, Error
**err
)
146 error_set(err
, QERR_UNSUPPORTED
);
150 void qmp_guest_file_flush(int64_t handle
, Error
**err
)
152 error_set(err
, QERR_UNSUPPORTED
);
156 * Return status of freeze/thaw
158 GuestFsfreezeStatus
qmp_guest_fsfreeze_status(Error
**err
)
160 if (!vss_initialized()) {
161 error_set(err
, QERR_UNSUPPORTED
);
165 if (ga_is_frozen(ga_state
)) {
166 return GUEST_FSFREEZE_STATUS_FROZEN
;
169 return GUEST_FSFREEZE_STATUS_THAWED
;
173 * Freeze local file systems using Volume Shadow-copy Service.
174 * The frozen state is limited for up to 10 seconds by VSS.
176 int64_t qmp_guest_fsfreeze_freeze(Error
**err
)
179 Error
*local_err
= NULL
;
181 if (!vss_initialized()) {
182 error_set(err
, QERR_UNSUPPORTED
);
186 slog("guest-fsfreeze called");
188 /* cannot risk guest agent blocking itself on a write in this state */
189 ga_set_frozen(ga_state
);
191 qga_vss_fsfreeze(&i
, err
, true);
192 if (error_is_set(err
)) {
199 qmp_guest_fsfreeze_thaw(&local_err
);
201 g_debug("cleanup thaw: %s", error_get_pretty(local_err
));
202 error_free(local_err
);
208 * Thaw local file systems using Volume Shadow-copy Service.
210 int64_t qmp_guest_fsfreeze_thaw(Error
**err
)
214 if (!vss_initialized()) {
215 error_set(err
, QERR_UNSUPPORTED
);
219 qga_vss_fsfreeze(&i
, err
, false);
221 ga_unset_frozen(ga_state
);
225 static void guest_fsfreeze_cleanup(void)
229 if (!vss_initialized()) {
233 if (ga_is_frozen(ga_state
) == GUEST_FSFREEZE_STATUS_FROZEN
) {
234 qmp_guest_fsfreeze_thaw(&err
);
236 slog("failed to clean up frozen filesystems: %s",
237 error_get_pretty(err
));
246 * Walk list of mounted file systems in the guest, and discard unused
249 void qmp_guest_fstrim(bool has_minimum
, int64_t minimum
, Error
**err
)
251 error_set(err
, QERR_UNSUPPORTED
);
255 GUEST_SUSPEND_MODE_DISK
,
256 GUEST_SUSPEND_MODE_RAM
259 static void check_suspend_mode(GuestSuspendMode mode
, Error
**err
)
261 SYSTEM_POWER_CAPABILITIES sys_pwr_caps
;
262 Error
*local_err
= NULL
;
264 if (error_is_set(err
)) {
267 ZeroMemory(&sys_pwr_caps
, sizeof(sys_pwr_caps
));
268 if (!GetPwrCapabilities(&sys_pwr_caps
)) {
269 error_set(&local_err
, QERR_QGA_COMMAND_FAILED
,
270 "failed to determine guest suspend capabilities");
275 case GUEST_SUSPEND_MODE_DISK
:
276 if (!sys_pwr_caps
.SystemS4
) {
277 error_set(&local_err
, QERR_QGA_COMMAND_FAILED
,
278 "suspend-to-disk not supported by OS");
281 case GUEST_SUSPEND_MODE_RAM
:
282 if (!sys_pwr_caps
.SystemS3
) {
283 error_set(&local_err
, QERR_QGA_COMMAND_FAILED
,
284 "suspend-to-ram not supported by OS");
288 error_set(&local_err
, QERR_INVALID_PARAMETER_VALUE
, "mode",
294 error_propagate(err
, local_err
);
298 static DWORD WINAPI
do_suspend(LPVOID opaque
)
300 GuestSuspendMode
*mode
= opaque
;
303 if (!SetSuspendState(*mode
== GUEST_SUSPEND_MODE_DISK
, TRUE
, TRUE
)) {
304 slog("failed to suspend guest, %lu", GetLastError());
311 void qmp_guest_suspend_disk(Error
**err
)
313 GuestSuspendMode
*mode
= g_malloc(sizeof(GuestSuspendMode
));
315 *mode
= GUEST_SUSPEND_MODE_DISK
;
316 check_suspend_mode(*mode
, err
);
317 acquire_privilege(SE_SHUTDOWN_NAME
, err
);
318 execute_async(do_suspend
, mode
, err
);
320 if (error_is_set(err
)) {
325 void qmp_guest_suspend_ram(Error
**err
)
327 GuestSuspendMode
*mode
= g_malloc(sizeof(GuestSuspendMode
));
329 *mode
= GUEST_SUSPEND_MODE_RAM
;
330 check_suspend_mode(*mode
, err
);
331 acquire_privilege(SE_SHUTDOWN_NAME
, err
);
332 execute_async(do_suspend
, mode
, err
);
334 if (error_is_set(err
)) {
339 void qmp_guest_suspend_hybrid(Error
**err
)
341 error_set(err
, QERR_UNSUPPORTED
);
344 GuestNetworkInterfaceList
*qmp_guest_network_get_interfaces(Error
**err
)
346 error_set(err
, QERR_UNSUPPORTED
);
350 int64_t qmp_guest_get_time(Error
**errp
)
357 if (ts
.wYear
< 1601 || ts
.wYear
> 30827) {
358 error_setg(errp
, "Failed to get time");
362 if (!SystemTimeToFileTime(&ts
, &tf
)) {
363 error_setg(errp
, "Failed to convert system time: %d", (int)GetLastError());
367 time_ns
= ((((int64_t)tf
.dwHighDateTime
<< 32) | tf
.dwLowDateTime
)
368 - W32_FT_OFFSET
) * 100;
373 void qmp_guest_set_time(bool has_time
, int64_t time_ns
, Error
**errp
)
380 /* Okay, user passed a time to set. Validate it. */
381 if (time_ns
< 0 || time_ns
/ 100 > INT64_MAX
- W32_FT_OFFSET
) {
382 error_setg(errp
, "Time %" PRId64
"is invalid", time_ns
);
386 time
= time_ns
/ 100 + W32_FT_OFFSET
;
388 tf
.dwLowDateTime
= (DWORD
) time
;
389 tf
.dwHighDateTime
= (DWORD
) (time
>> 32);
391 if (!FileTimeToSystemTime(&tf
, &ts
)) {
392 error_setg(errp
, "Failed to convert system time %d",
393 (int)GetLastError());
397 /* Otherwise read the time from RTC which contains the correct value.
400 if (ts
.wYear
< 1601 || ts
.wYear
> 30827) {
401 error_setg(errp
, "Failed to get time");
406 acquire_privilege(SE_SYSTEMTIME_NAME
, errp
);
407 if (error_is_set(errp
)) {
411 if (!SetSystemTime(&ts
)) {
412 error_setg(errp
, "Failed to set time to guest: %d", (int)GetLastError());
417 GuestLogicalProcessorList
*qmp_guest_get_vcpus(Error
**errp
)
419 error_set(errp
, QERR_UNSUPPORTED
);
423 int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList
*vcpus
, Error
**errp
)
425 error_set(errp
, QERR_UNSUPPORTED
);
429 /* register init/cleanup routines for stateful command groups */
430 void ga_command_state_init(GAState
*s
, GACommandState
*cs
)
432 if (vss_init(true)) {
433 ga_command_state_add(cs
, NULL
, guest_fsfreeze_cleanup
);