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-qmp-commands.h"
19 #include "qapi/qmp/qerror.h"
21 #ifndef SHTDN_REASON_FLAG_PLANNED
22 #define SHTDN_REASON_FLAG_PLANNED 0x80000000
25 /* multiple of 100 nanoseconds elapsed between windows baseline
26 * (1/1/1601) and Unix Epoch (1/1/1970), accounting for leap years */
27 #define W32_FT_OFFSET (10000000ULL * 60 * 60 * 24 * \
28 (365 * (1970 - 1601) + \
29 (1970 - 1601) / 4 - 3))
31 static void acquire_privilege(const char *name
, Error
**err
)
34 TOKEN_PRIVILEGES priv
;
35 Error
*local_err
= NULL
;
37 if (error_is_set(err
)) {
41 if (OpenProcessToken(GetCurrentProcess(),
42 TOKEN_ADJUST_PRIVILEGES
|TOKEN_QUERY
, &token
))
44 if (!LookupPrivilegeValue(NULL
, name
, &priv
.Privileges
[0].Luid
)) {
45 error_set(&local_err
, QERR_QGA_COMMAND_FAILED
,
46 "no luid for requested privilege");
50 priv
.PrivilegeCount
= 1;
51 priv
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
53 if (!AdjustTokenPrivileges(token
, FALSE
, &priv
, 0, NULL
, 0)) {
54 error_set(&local_err
, QERR_QGA_COMMAND_FAILED
,
55 "unable to acquire requested privilege");
61 error_set(&local_err
, QERR_QGA_COMMAND_FAILED
,
62 "failed to open privilege token");
67 error_propagate(err
, local_err
);
71 static void execute_async(DWORD
WINAPI (*func
)(LPVOID
), LPVOID opaque
, Error
**err
)
73 Error
*local_err
= NULL
;
75 if (error_is_set(err
)) {
78 HANDLE thread
= CreateThread(NULL
, 0, func
, opaque
, 0, NULL
);
80 error_set(&local_err
, QERR_QGA_COMMAND_FAILED
,
81 "failed to dispatch asynchronous command");
82 error_propagate(err
, local_err
);
86 void qmp_guest_shutdown(bool has_mode
, const char *mode
, Error
**err
)
88 UINT shutdown_flag
= EWX_FORCE
;
90 slog("guest-shutdown called, mode: %s", mode
);
92 if (!has_mode
|| strcmp(mode
, "powerdown") == 0) {
93 shutdown_flag
|= EWX_POWEROFF
;
94 } else if (strcmp(mode
, "halt") == 0) {
95 shutdown_flag
|= EWX_SHUTDOWN
;
96 } else if (strcmp(mode
, "reboot") == 0) {
97 shutdown_flag
|= EWX_REBOOT
;
99 error_set(err
, QERR_INVALID_PARAMETER_VALUE
, "mode",
100 "halt|powerdown|reboot");
104 /* Request a shutdown privilege, but try to shut down the system
106 acquire_privilege(SE_SHUTDOWN_NAME
, err
);
107 if (error_is_set(err
)) {
111 if (!ExitWindowsEx(shutdown_flag
, SHTDN_REASON_FLAG_PLANNED
)) {
112 slog("guest-shutdown failed: %d", GetLastError());
113 error_set(err
, QERR_UNDEFINED_ERROR
);
117 int64_t qmp_guest_file_open(const char *path
, bool has_mode
, const char *mode
, Error
**err
)
119 error_set(err
, QERR_UNSUPPORTED
);
123 void qmp_guest_file_close(int64_t handle
, Error
**err
)
125 error_set(err
, QERR_UNSUPPORTED
);
128 GuestFileRead
*qmp_guest_file_read(int64_t handle
, bool has_count
,
129 int64_t count
, Error
**err
)
131 error_set(err
, QERR_UNSUPPORTED
);
135 GuestFileWrite
*qmp_guest_file_write(int64_t handle
, const char *buf_b64
,
136 bool has_count
, int64_t count
, Error
**err
)
138 error_set(err
, QERR_UNSUPPORTED
);
142 GuestFileSeek
*qmp_guest_file_seek(int64_t handle
, int64_t offset
,
143 int64_t whence
, Error
**err
)
145 error_set(err
, QERR_UNSUPPORTED
);
149 void qmp_guest_file_flush(int64_t handle
, Error
**err
)
151 error_set(err
, QERR_UNSUPPORTED
);
155 * Return status of freeze/thaw
157 GuestFsfreezeStatus
qmp_guest_fsfreeze_status(Error
**err
)
159 error_set(err
, QERR_UNSUPPORTED
);
164 * Walk list of mounted file systems in the guest, and freeze the ones which
165 * are real local file systems.
167 int64_t qmp_guest_fsfreeze_freeze(Error
**err
)
169 error_set(err
, QERR_UNSUPPORTED
);
174 * Walk list of frozen file systems in the guest, and thaw them.
176 int64_t qmp_guest_fsfreeze_thaw(Error
**err
)
178 error_set(err
, QERR_UNSUPPORTED
);
183 * Walk list of mounted file systems in the guest, and discard unused
186 void qmp_guest_fstrim(bool has_minimum
, int64_t minimum
, Error
**err
)
188 error_set(err
, QERR_UNSUPPORTED
);
192 GUEST_SUSPEND_MODE_DISK
,
193 GUEST_SUSPEND_MODE_RAM
196 static void check_suspend_mode(GuestSuspendMode mode
, Error
**err
)
198 SYSTEM_POWER_CAPABILITIES sys_pwr_caps
;
199 Error
*local_err
= NULL
;
201 if (error_is_set(err
)) {
204 ZeroMemory(&sys_pwr_caps
, sizeof(sys_pwr_caps
));
205 if (!GetPwrCapabilities(&sys_pwr_caps
)) {
206 error_set(&local_err
, QERR_QGA_COMMAND_FAILED
,
207 "failed to determine guest suspend capabilities");
212 case GUEST_SUSPEND_MODE_DISK
:
213 if (!sys_pwr_caps
.SystemS4
) {
214 error_set(&local_err
, QERR_QGA_COMMAND_FAILED
,
215 "suspend-to-disk not supported by OS");
218 case GUEST_SUSPEND_MODE_RAM
:
219 if (!sys_pwr_caps
.SystemS3
) {
220 error_set(&local_err
, QERR_QGA_COMMAND_FAILED
,
221 "suspend-to-ram not supported by OS");
225 error_set(&local_err
, QERR_INVALID_PARAMETER_VALUE
, "mode",
231 error_propagate(err
, local_err
);
235 static DWORD WINAPI
do_suspend(LPVOID opaque
)
237 GuestSuspendMode
*mode
= opaque
;
240 if (!SetSuspendState(*mode
== GUEST_SUSPEND_MODE_DISK
, TRUE
, TRUE
)) {
241 slog("failed to suspend guest, %s", GetLastError());
248 void qmp_guest_suspend_disk(Error
**err
)
250 GuestSuspendMode
*mode
= g_malloc(sizeof(GuestSuspendMode
));
252 *mode
= GUEST_SUSPEND_MODE_DISK
;
253 check_suspend_mode(*mode
, err
);
254 acquire_privilege(SE_SHUTDOWN_NAME
, err
);
255 execute_async(do_suspend
, mode
, err
);
257 if (error_is_set(err
)) {
262 void qmp_guest_suspend_ram(Error
**err
)
264 GuestSuspendMode
*mode
= g_malloc(sizeof(GuestSuspendMode
));
266 *mode
= GUEST_SUSPEND_MODE_RAM
;
267 check_suspend_mode(*mode
, err
);
268 acquire_privilege(SE_SHUTDOWN_NAME
, err
);
269 execute_async(do_suspend
, mode
, err
);
271 if (error_is_set(err
)) {
276 void qmp_guest_suspend_hybrid(Error
**err
)
278 error_set(err
, QERR_UNSUPPORTED
);
281 GuestNetworkInterfaceList
*qmp_guest_network_get_interfaces(Error
**err
)
283 error_set(err
, QERR_UNSUPPORTED
);
287 int64_t qmp_guest_get_time(Error
**errp
)
294 if (ts
.wYear
< 1601 || ts
.wYear
> 30827) {
295 error_setg(errp
, "Failed to get time");
299 if (!SystemTimeToFileTime(&ts
, &tf
)) {
300 error_setg(errp
, "Failed to convert system time: %d", (int)GetLastError());
304 time_ns
= ((((int64_t)tf
.dwHighDateTime
<< 32) | tf
.dwLowDateTime
)
305 - W32_FT_OFFSET
) * 100;
310 void qmp_guest_set_time(int64_t time_ns
, Error
**errp
)
316 if (time_ns
< 0 || time_ns
/ 100 > INT64_MAX
- W32_FT_OFFSET
) {
317 error_setg(errp
, "Time %" PRId64
"is invalid", time_ns
);
321 time
= time_ns
/ 100 + W32_FT_OFFSET
;
323 tf
.dwLowDateTime
= (DWORD
) time
;
324 tf
.dwHighDateTime
= (DWORD
) (time
>> 32);
326 if (!FileTimeToSystemTime(&tf
, &ts
)) {
327 error_setg(errp
, "Failed to convert system time %d", (int)GetLastError());
331 acquire_privilege(SE_SYSTEMTIME_NAME
, errp
);
332 if (error_is_set(errp
)) {
336 if (!SetSystemTime(&ts
)) {
337 error_setg(errp
, "Failed to set time to guest: %d", (int)GetLastError());
342 GuestLogicalProcessorList
*qmp_guest_get_vcpus(Error
**errp
)
344 error_set(errp
, QERR_UNSUPPORTED
);
348 int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList
*vcpus
, Error
**errp
)
350 error_set(errp
, QERR_UNSUPPORTED
);
354 /* register init/cleanup routines for stateful command groups */
355 void ga_command_state_init(GAState
*s
, GACommandState
*cs
)