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
**errp
)
35 TOKEN_PRIVILEGES priv
;
36 Error
*local_err
= NULL
;
38 if (OpenProcessToken(GetCurrentProcess(),
39 TOKEN_ADJUST_PRIVILEGES
|TOKEN_QUERY
, &token
))
41 if (!LookupPrivilegeValue(NULL
, name
, &priv
.Privileges
[0].Luid
)) {
42 error_set(&local_err
, QERR_QGA_COMMAND_FAILED
,
43 "no luid for requested privilege");
47 priv
.PrivilegeCount
= 1;
48 priv
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
50 if (!AdjustTokenPrivileges(token
, FALSE
, &priv
, 0, NULL
, 0)) {
51 error_set(&local_err
, QERR_QGA_COMMAND_FAILED
,
52 "unable to acquire requested privilege");
58 error_set(&local_err
, QERR_QGA_COMMAND_FAILED
,
59 "failed to open privilege token");
64 error_propagate(errp
, local_err
);
68 static void execute_async(DWORD
WINAPI (*func
)(LPVOID
), LPVOID opaque
,
71 Error
*local_err
= NULL
;
73 HANDLE thread
= CreateThread(NULL
, 0, func
, opaque
, 0, NULL
);
75 error_set(&local_err
, QERR_QGA_COMMAND_FAILED
,
76 "failed to dispatch asynchronous command");
77 error_propagate(errp
, local_err
);
81 void qmp_guest_shutdown(bool has_mode
, const char *mode
, Error
**errp
)
83 Error
*local_err
= NULL
;
84 UINT shutdown_flag
= EWX_FORCE
;
86 slog("guest-shutdown called, mode: %s", mode
);
88 if (!has_mode
|| strcmp(mode
, "powerdown") == 0) {
89 shutdown_flag
|= EWX_POWEROFF
;
90 } else if (strcmp(mode
, "halt") == 0) {
91 shutdown_flag
|= EWX_SHUTDOWN
;
92 } else if (strcmp(mode
, "reboot") == 0) {
93 shutdown_flag
|= EWX_REBOOT
;
95 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "mode",
96 "halt|powerdown|reboot");
100 /* Request a shutdown privilege, but try to shut down the system
102 acquire_privilege(SE_SHUTDOWN_NAME
, &local_err
);
104 error_propagate(errp
, local_err
);
108 if (!ExitWindowsEx(shutdown_flag
, SHTDN_REASON_FLAG_PLANNED
)) {
109 slog("guest-shutdown failed: %lu", GetLastError());
110 error_set(errp
, QERR_UNDEFINED_ERROR
);
114 int64_t qmp_guest_file_open(const char *path
, bool has_mode
, const char *mode
,
117 error_set(errp
, QERR_UNSUPPORTED
);
121 void qmp_guest_file_close(int64_t handle
, Error
**errp
)
123 error_set(errp
, QERR_UNSUPPORTED
);
126 GuestFileRead
*qmp_guest_file_read(int64_t handle
, bool has_count
,
127 int64_t count
, Error
**errp
)
129 error_set(errp
, QERR_UNSUPPORTED
);
133 GuestFileWrite
*qmp_guest_file_write(int64_t handle
, const char *buf_b64
,
134 bool has_count
, int64_t count
,
137 error_set(errp
, QERR_UNSUPPORTED
);
141 GuestFileSeek
*qmp_guest_file_seek(int64_t handle
, int64_t offset
,
142 int64_t whence
, Error
**errp
)
144 error_set(errp
, QERR_UNSUPPORTED
);
148 void qmp_guest_file_flush(int64_t handle
, Error
**errp
)
150 error_set(errp
, QERR_UNSUPPORTED
);
154 * Return status of freeze/thaw
156 GuestFsfreezeStatus
qmp_guest_fsfreeze_status(Error
**errp
)
158 if (!vss_initialized()) {
159 error_set(errp
, QERR_UNSUPPORTED
);
163 if (ga_is_frozen(ga_state
)) {
164 return GUEST_FSFREEZE_STATUS_FROZEN
;
167 return GUEST_FSFREEZE_STATUS_THAWED
;
171 * Freeze local file systems using Volume Shadow-copy Service.
172 * The frozen state is limited for up to 10 seconds by VSS.
174 int64_t qmp_guest_fsfreeze_freeze(Error
**errp
)
177 Error
*local_err
= NULL
;
179 if (!vss_initialized()) {
180 error_set(errp
, QERR_UNSUPPORTED
);
184 slog("guest-fsfreeze called");
186 /* cannot risk guest agent blocking itself on a write in this state */
187 ga_set_frozen(ga_state
);
189 qga_vss_fsfreeze(&i
, &local_err
, true);
191 error_propagate(errp
, local_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
**errp
)
214 if (!vss_initialized()) {
215 error_set(errp
, QERR_UNSUPPORTED
);
219 qga_vss_fsfreeze(&i
, errp
, 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
**errp
)
251 error_set(errp
, QERR_UNSUPPORTED
);
255 GUEST_SUSPEND_MODE_DISK
,
256 GUEST_SUSPEND_MODE_RAM
259 static void check_suspend_mode(GuestSuspendMode mode
, Error
**errp
)
261 SYSTEM_POWER_CAPABILITIES sys_pwr_caps
;
262 Error
*local_err
= NULL
;
264 ZeroMemory(&sys_pwr_caps
, sizeof(sys_pwr_caps
));
265 if (!GetPwrCapabilities(&sys_pwr_caps
)) {
266 error_set(&local_err
, QERR_QGA_COMMAND_FAILED
,
267 "failed to determine guest suspend capabilities");
272 case GUEST_SUSPEND_MODE_DISK
:
273 if (!sys_pwr_caps
.SystemS4
) {
274 error_set(&local_err
, QERR_QGA_COMMAND_FAILED
,
275 "suspend-to-disk not supported by OS");
278 case GUEST_SUSPEND_MODE_RAM
:
279 if (!sys_pwr_caps
.SystemS3
) {
280 error_set(&local_err
, QERR_QGA_COMMAND_FAILED
,
281 "suspend-to-ram not supported by OS");
285 error_set(&local_err
, QERR_INVALID_PARAMETER_VALUE
, "mode",
291 error_propagate(errp
, local_err
);
295 static DWORD WINAPI
do_suspend(LPVOID opaque
)
297 GuestSuspendMode
*mode
= opaque
;
300 if (!SetSuspendState(*mode
== GUEST_SUSPEND_MODE_DISK
, TRUE
, TRUE
)) {
301 slog("failed to suspend guest, %lu", GetLastError());
308 void qmp_guest_suspend_disk(Error
**errp
)
310 Error
*local_err
= NULL
;
311 GuestSuspendMode
*mode
= g_malloc(sizeof(GuestSuspendMode
));
313 *mode
= GUEST_SUSPEND_MODE_DISK
;
314 check_suspend_mode(*mode
, &local_err
);
315 acquire_privilege(SE_SHUTDOWN_NAME
, &local_err
);
316 execute_async(do_suspend
, mode
, &local_err
);
319 error_propagate(errp
, local_err
);
324 void qmp_guest_suspend_ram(Error
**errp
)
326 Error
*local_err
= NULL
;
327 GuestSuspendMode
*mode
= g_malloc(sizeof(GuestSuspendMode
));
329 *mode
= GUEST_SUSPEND_MODE_RAM
;
330 check_suspend_mode(*mode
, &local_err
);
331 acquire_privilege(SE_SHUTDOWN_NAME
, &local_err
);
332 execute_async(do_suspend
, mode
, &local_err
);
335 error_propagate(errp
, local_err
);
340 void qmp_guest_suspend_hybrid(Error
**errp
)
342 error_set(errp
, QERR_UNSUPPORTED
);
345 GuestNetworkInterfaceList
*qmp_guest_network_get_interfaces(Error
**errp
)
347 error_set(errp
, QERR_UNSUPPORTED
);
351 int64_t qmp_guest_get_time(Error
**errp
)
358 if (ts
.wYear
< 1601 || ts
.wYear
> 30827) {
359 error_setg(errp
, "Failed to get time");
363 if (!SystemTimeToFileTime(&ts
, &tf
)) {
364 error_setg(errp
, "Failed to convert system time: %d", (int)GetLastError());
368 time_ns
= ((((int64_t)tf
.dwHighDateTime
<< 32) | tf
.dwLowDateTime
)
369 - W32_FT_OFFSET
) * 100;
374 void qmp_guest_set_time(bool has_time
, int64_t time_ns
, Error
**errp
)
376 Error
*local_err
= NULL
;
382 /* Okay, user passed a time to set. Validate it. */
383 if (time_ns
< 0 || time_ns
/ 100 > INT64_MAX
- W32_FT_OFFSET
) {
384 error_setg(errp
, "Time %" PRId64
"is invalid", time_ns
);
388 time
= time_ns
/ 100 + W32_FT_OFFSET
;
390 tf
.dwLowDateTime
= (DWORD
) time
;
391 tf
.dwHighDateTime
= (DWORD
) (time
>> 32);
393 if (!FileTimeToSystemTime(&tf
, &ts
)) {
394 error_setg(errp
, "Failed to convert system time %d",
395 (int)GetLastError());
399 /* Otherwise read the time from RTC which contains the correct value.
402 if (ts
.wYear
< 1601 || ts
.wYear
> 30827) {
403 error_setg(errp
, "Failed to get time");
408 acquire_privilege(SE_SYSTEMTIME_NAME
, &local_err
);
410 error_propagate(errp
, local_err
);
414 if (!SetSystemTime(&ts
)) {
415 error_setg(errp
, "Failed to set time to guest: %d", (int)GetLastError());
420 GuestLogicalProcessorList
*qmp_guest_get_vcpus(Error
**errp
)
422 error_set(errp
, QERR_UNSUPPORTED
);
426 int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList
*vcpus
, Error
**errp
)
428 error_set(errp
, QERR_UNSUPPORTED
);
432 /* register init/cleanup routines for stateful command groups */
433 void ga_command_state_init(GAState
*s
, GACommandState
*cs
)
435 if (vss_init(true)) {
436 ga_command_state_add(cs
, NULL
, guest_fsfreeze_cleanup
);