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");
57 error_set(&local_err
, QERR_QGA_COMMAND_FAILED
,
58 "failed to open privilege token");
66 error_propagate(errp
, local_err
);
70 static void execute_async(DWORD
WINAPI (*func
)(LPVOID
), LPVOID opaque
,
73 Error
*local_err
= NULL
;
75 HANDLE thread
= CreateThread(NULL
, 0, func
, opaque
, 0, NULL
);
77 error_set(&local_err
, QERR_QGA_COMMAND_FAILED
,
78 "failed to dispatch asynchronous command");
79 error_propagate(errp
, local_err
);
83 void qmp_guest_shutdown(bool has_mode
, const char *mode
, Error
**errp
)
85 Error
*local_err
= NULL
;
86 UINT shutdown_flag
= EWX_FORCE
;
88 slog("guest-shutdown called, mode: %s", mode
);
90 if (!has_mode
|| strcmp(mode
, "powerdown") == 0) {
91 shutdown_flag
|= EWX_POWEROFF
;
92 } else if (strcmp(mode
, "halt") == 0) {
93 shutdown_flag
|= EWX_SHUTDOWN
;
94 } else if (strcmp(mode
, "reboot") == 0) {
95 shutdown_flag
|= EWX_REBOOT
;
97 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "mode",
98 "halt|powerdown|reboot");
102 /* Request a shutdown privilege, but try to shut down the system
104 acquire_privilege(SE_SHUTDOWN_NAME
, &local_err
);
106 error_propagate(errp
, local_err
);
110 if (!ExitWindowsEx(shutdown_flag
, SHTDN_REASON_FLAG_PLANNED
)) {
111 slog("guest-shutdown failed: %lu", GetLastError());
112 error_set(errp
, QERR_UNDEFINED_ERROR
);
116 int64_t qmp_guest_file_open(const char *path
, bool has_mode
, const char *mode
,
119 error_set(errp
, QERR_UNSUPPORTED
);
123 void qmp_guest_file_close(int64_t handle
, Error
**errp
)
125 error_set(errp
, QERR_UNSUPPORTED
);
128 GuestFileRead
*qmp_guest_file_read(int64_t handle
, bool has_count
,
129 int64_t count
, Error
**errp
)
131 error_set(errp
, QERR_UNSUPPORTED
);
135 GuestFileWrite
*qmp_guest_file_write(int64_t handle
, const char *buf_b64
,
136 bool has_count
, int64_t count
,
139 error_set(errp
, QERR_UNSUPPORTED
);
143 GuestFileSeek
*qmp_guest_file_seek(int64_t handle
, int64_t offset
,
144 int64_t whence
, Error
**errp
)
146 error_set(errp
, QERR_UNSUPPORTED
);
150 void qmp_guest_file_flush(int64_t handle
, Error
**errp
)
152 error_set(errp
, QERR_UNSUPPORTED
);
155 GuestFilesystemInfoList
*qmp_guest_get_fsinfo(Error
**errp
)
157 error_set(errp
, QERR_UNSUPPORTED
);
162 * Return status of freeze/thaw
164 GuestFsfreezeStatus
qmp_guest_fsfreeze_status(Error
**errp
)
166 if (!vss_initialized()) {
167 error_set(errp
, QERR_UNSUPPORTED
);
171 if (ga_is_frozen(ga_state
)) {
172 return GUEST_FSFREEZE_STATUS_FROZEN
;
175 return GUEST_FSFREEZE_STATUS_THAWED
;
179 * Freeze local file systems using Volume Shadow-copy Service.
180 * The frozen state is limited for up to 10 seconds by VSS.
182 int64_t qmp_guest_fsfreeze_freeze(Error
**errp
)
185 Error
*local_err
= NULL
;
187 if (!vss_initialized()) {
188 error_set(errp
, QERR_UNSUPPORTED
);
192 slog("guest-fsfreeze called");
194 /* cannot risk guest agent blocking itself on a write in this state */
195 ga_set_frozen(ga_state
);
197 qga_vss_fsfreeze(&i
, &local_err
, true);
199 error_propagate(errp
, local_err
);
207 qmp_guest_fsfreeze_thaw(&local_err
);
209 g_debug("cleanup thaw: %s", error_get_pretty(local_err
));
210 error_free(local_err
);
215 int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints
,
216 strList
*mountpoints
,
219 error_set(errp
, QERR_UNSUPPORTED
);
225 * Thaw local file systems using Volume Shadow-copy Service.
227 int64_t qmp_guest_fsfreeze_thaw(Error
**errp
)
231 if (!vss_initialized()) {
232 error_set(errp
, QERR_UNSUPPORTED
);
236 qga_vss_fsfreeze(&i
, errp
, false);
238 ga_unset_frozen(ga_state
);
242 static void guest_fsfreeze_cleanup(void)
246 if (!vss_initialized()) {
250 if (ga_is_frozen(ga_state
) == GUEST_FSFREEZE_STATUS_FROZEN
) {
251 qmp_guest_fsfreeze_thaw(&err
);
253 slog("failed to clean up frozen filesystems: %s",
254 error_get_pretty(err
));
263 * Walk list of mounted file systems in the guest, and discard unused
266 void qmp_guest_fstrim(bool has_minimum
, int64_t minimum
, Error
**errp
)
268 error_set(errp
, QERR_UNSUPPORTED
);
272 GUEST_SUSPEND_MODE_DISK
,
273 GUEST_SUSPEND_MODE_RAM
276 static void check_suspend_mode(GuestSuspendMode mode
, Error
**errp
)
278 SYSTEM_POWER_CAPABILITIES sys_pwr_caps
;
279 Error
*local_err
= NULL
;
281 ZeroMemory(&sys_pwr_caps
, sizeof(sys_pwr_caps
));
282 if (!GetPwrCapabilities(&sys_pwr_caps
)) {
283 error_set(&local_err
, QERR_QGA_COMMAND_FAILED
,
284 "failed to determine guest suspend capabilities");
289 case GUEST_SUSPEND_MODE_DISK
:
290 if (!sys_pwr_caps
.SystemS4
) {
291 error_set(&local_err
, QERR_QGA_COMMAND_FAILED
,
292 "suspend-to-disk not supported by OS");
295 case GUEST_SUSPEND_MODE_RAM
:
296 if (!sys_pwr_caps
.SystemS3
) {
297 error_set(&local_err
, QERR_QGA_COMMAND_FAILED
,
298 "suspend-to-ram not supported by OS");
302 error_set(&local_err
, QERR_INVALID_PARAMETER_VALUE
, "mode",
308 error_propagate(errp
, local_err
);
312 static DWORD WINAPI
do_suspend(LPVOID opaque
)
314 GuestSuspendMode
*mode
= opaque
;
317 if (!SetSuspendState(*mode
== GUEST_SUSPEND_MODE_DISK
, TRUE
, TRUE
)) {
318 slog("failed to suspend guest, %lu", GetLastError());
325 void qmp_guest_suspend_disk(Error
**errp
)
327 Error
*local_err
= NULL
;
328 GuestSuspendMode
*mode
= g_malloc(sizeof(GuestSuspendMode
));
330 *mode
= GUEST_SUSPEND_MODE_DISK
;
331 check_suspend_mode(*mode
, &local_err
);
332 acquire_privilege(SE_SHUTDOWN_NAME
, &local_err
);
333 execute_async(do_suspend
, mode
, &local_err
);
336 error_propagate(errp
, local_err
);
341 void qmp_guest_suspend_ram(Error
**errp
)
343 Error
*local_err
= NULL
;
344 GuestSuspendMode
*mode
= g_malloc(sizeof(GuestSuspendMode
));
346 *mode
= GUEST_SUSPEND_MODE_RAM
;
347 check_suspend_mode(*mode
, &local_err
);
348 acquire_privilege(SE_SHUTDOWN_NAME
, &local_err
);
349 execute_async(do_suspend
, mode
, &local_err
);
352 error_propagate(errp
, local_err
);
357 void qmp_guest_suspend_hybrid(Error
**errp
)
359 error_set(errp
, QERR_UNSUPPORTED
);
362 GuestNetworkInterfaceList
*qmp_guest_network_get_interfaces(Error
**errp
)
364 error_set(errp
, QERR_UNSUPPORTED
);
368 int64_t qmp_guest_get_time(Error
**errp
)
375 if (ts
.wYear
< 1601 || ts
.wYear
> 30827) {
376 error_setg(errp
, "Failed to get time");
380 if (!SystemTimeToFileTime(&ts
, &tf
)) {
381 error_setg(errp
, "Failed to convert system time: %d", (int)GetLastError());
385 time_ns
= ((((int64_t)tf
.dwHighDateTime
<< 32) | tf
.dwLowDateTime
)
386 - W32_FT_OFFSET
) * 100;
391 void qmp_guest_set_time(bool has_time
, int64_t time_ns
, Error
**errp
)
393 Error
*local_err
= NULL
;
399 /* Okay, user passed a time to set. Validate it. */
400 if (time_ns
< 0 || time_ns
/ 100 > INT64_MAX
- W32_FT_OFFSET
) {
401 error_setg(errp
, "Time %" PRId64
"is invalid", time_ns
);
405 time
= time_ns
/ 100 + W32_FT_OFFSET
;
407 tf
.dwLowDateTime
= (DWORD
) time
;
408 tf
.dwHighDateTime
= (DWORD
) (time
>> 32);
410 if (!FileTimeToSystemTime(&tf
, &ts
)) {
411 error_setg(errp
, "Failed to convert system time %d",
412 (int)GetLastError());
416 /* Otherwise read the time from RTC which contains the correct value.
419 if (ts
.wYear
< 1601 || ts
.wYear
> 30827) {
420 error_setg(errp
, "Failed to get time");
425 acquire_privilege(SE_SYSTEMTIME_NAME
, &local_err
);
427 error_propagate(errp
, local_err
);
431 if (!SetSystemTime(&ts
)) {
432 error_setg(errp
, "Failed to set time to guest: %d", (int)GetLastError());
437 GuestLogicalProcessorList
*qmp_guest_get_vcpus(Error
**errp
)
439 error_set(errp
, QERR_UNSUPPORTED
);
443 int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList
*vcpus
, Error
**errp
)
445 error_set(errp
, QERR_UNSUPPORTED
);
449 /* add unsupported commands to the blacklist */
450 GList
*ga_command_blacklist_init(GList
*blacklist
)
452 const char *list_unsupported
[] = {
453 "guest-file-open", "guest-file-close", "guest-file-read",
454 "guest-file-write", "guest-file-seek", "guest-file-flush",
455 "guest-suspend-hybrid", "guest-network-get-interfaces",
456 "guest-get-vcpus", "guest-set-vcpus",
457 "guest-fsfreeze-freeze-list", "guest-get-fsinfo",
458 "guest-fstrim", NULL
};
459 char **p
= (char **)list_unsupported
;
462 blacklist
= g_list_append(blacklist
, *p
++);
465 if (!vss_init(true)) {
466 const char *list
[] = {
467 "guest-get-fsinfo", "guest-fsfreeze-status",
468 "guest-fsfreeze-freeze", "guest-fsfreeze-thaw", NULL
};
472 blacklist
= g_list_append(blacklist
, *p
++);
479 /* register init/cleanup routines for stateful command groups */
480 void ga_command_state_init(GAState
*s
, GACommandState
*cs
)
482 if (!vss_initialized()) {
483 ga_command_state_add(cs
, NULL
, guest_fsfreeze_cleanup
);