Add Windows support for time resync by qemu-ga
v6 cover Add Windows support for time resync by qemu-ga
This patch series attempts to add Windows implementation for qemu-ga commands guest-get-time and guest-set-time.
The previous thread about the interfaces introduced and the POSIX-specific command implementation has already been accepted, the reference link:
http://article.gmane.org/gmane.comp.emulators.qemu/198472
Notes:
Now It was tested on Windows XP SP3 and Windows 7. Please comment!
Thanks.
Changes since v5:
- Fix the error check for GetSystemTime() from Michael.
- Other fixups from Michael.
Changes since v4:
- Error handel improvement from Michael.
- Do the math explicitly for the time convert of FILETIME suggested by Michael.
Changes since v3:
- Reorder the acquire_privilege to avoid a possible leak of privileges suggested by Eric.
Changes since v2:
- Overflow check improvement for time_ns from Eric.
Changes since v1:
- Make the macro for the offset between windows baseline and Unix Epoch more readable from Eric.
- Overflow check for filetime pointed by Eric.
Lei Li (2):
qga: add windows implementation for guest-get-time
qga: add windows implementation for guest-set-time
PATCH v6 1/2 qga: add windows implementation for guest-get-time
--- qga/commands-win32.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+)
qga/commands-win32.c +29 -0
@@ -22,6 +22,12 @@#define SHTDN_REASON_FLAG_PLANNED 0x80000000#endif +/* multiple of 100 nanoseconds elapsed between windows baseline +(1/1/1601) and Unix Epoch (1/1/1970), accounting for leap years */ +#define W32_FT_OFFSET (10000000ULL * 60 * 60 * 24 * \ + (365 * (1970 - 1601) + \ + (1970 - 1601) / 4 - 3)) +static void acquire_privilege (const char *name,Error **err) {HANDLE token;
@@ -108,6 +114,29 @@ void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err) } } +int64_t qmp_guest_get_time (Error **errp) +{ +SYSTEMTIME ts = {0 }; +int64_t time_ns; +FILETIME tf; + +GetSystemTime (&ts); +if (ts.wYear <1601 || ts.wYear >30827 ) { +error_setg (errp,"Failed to get time" ); +return -1 ; + } + +if (!SystemTimeToFileTime (&ts, &tf)) { +error_setg_errno (errp, errno,"Failed to convert system time" ); +return -1 ; + } + + time_ns = ((((int64_t )tf.dwHighDateTime <<32 ) | tf.dwLowDateTime) + -W32_FT_OFFSET ) *100 ; + +return time_ns; +} +int64_t qmp_guest_file_open (const char *path,bool has_mode,const char *mode,Error **err) {error_set (err,QERR_UNSUPPORTED );
Signature · 1 line
1.7.11.7
Re: [Qemu-devel] [PATCH 1/2] qga: add windows implementation for guest-get-time
Quoted 30 lines · Lei Li
Signed-off-by Lei Li <[email protected]>--- qga/commands-win32.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+)qga/commands-win32.c +10
-0@@ -22,6 +22,12 @@#define SHTDN_REASON_FLAG_PLANNED 0x80000000#endif +/* multiple of 100 nanoseconds elapsed between windows baseline +(1/1/1601) and Unix Epoch (1/1/1970), accounting for leap years */ +#define W32_FT_OFFSET (10000000ULL * 60 * 60 * 24 * \ + (365 * (1970 - 1601) + \ + (1970 - 1601) / 4 - 3)) +static void acquire_privilege (const char *name,Error **err) {HANDLE token;@@ -108,6 +114,29 @@ void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err) } } +int64_t qmp_guest_get_time (Error **errp) +{ +SYSTEMTIME ts = {0 }; +int64_t time_ns;
qga/commands-win32.c void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err) · line 130
Quoted 10 lines
qga/commands-win32.c +10
-0+FILETIME tf; + +GetSystemTime (&ts); +if (ts.wYear <1601 || ts.wYear >30827 ) { +error_setg (errp,"Failed to get time" ); +return -1 ; + } + +if (!SystemTimeToFileTime (&ts, &tf)) { +error_setg_errno (errp, errno,"Failed to convert system time" );
I missed this is well, SystemTimeToFileTime() doesn't set errno, but if this is the only change needed I can fix this up in my tree.
Quoted 15 lines
qga/commands-win32.c +9
-0+return -1 ; + } + + time_ns = ((((int64_t )tf.dwHighDateTime <<32 ) | tf.dwLowDateTime) + -W32_FT_OFFSET ) *100 ; + +return time_ns; +} +int64_t qmp_guest_file_open (const char *path,bool has_mode,const char *mode,Error **err) {error_set (err,QERR_UNSUPPORTED );Signature · 1 line
1.7.11.7
PATCH v6 2/2 qga: add windows implementation for guest-set-time
In reply to Lei Li--- qga/commands-win32.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+)
qga/commands-win32.c +32 -0
@@ -137,6 +137,38 @@ int64_t qmp_guest_get_time(Error **errp)return time_ns; } +void qmp_guest_set_time (int64_t time_ns,Error **errp) +{ +SYSTEMTIME ts; +FILETIME tf; +LONGLONG time; + +if (time_ns <0 || time_ns /100 >INT64_MAX -W32_FT_OFFSET ) { +error_setg (errp,"Time %" PRId64"is invalid" , time_ns); +return ; + } + + time = time_ns /100 +W32_FT_OFFSET ; + + tf.dwLowDateTime = (DWORD ) time; + tf.dwHighDateTime = (DWORD ) (time >>32 ); + +if (!FileTimeToSystemTime (&tf, &ts)) { +error_setg (errp,"Failed to convert system time" ); +return ; + } + +acquire_privilege (SE_SYSTEMTIME_NAME , errp); +if (error_is_set (errp)) { +return ; + } + +if (!SetSystemTime (&ts)) { +error_setg (errp,"Failed to set time to guest: %d" , (int )GetLastError ()); +return ; + } +} +int64_t qmp_guest_file_open (const char *path,bool has_mode,const char *mode,Error **err) {error_set (err,QERR_UNSUPPORTED );
Signature · 1 line
1.7.11.7
Quoted 7 lines · Lei Li
This patch series attempts to add Windows implementation for qemu-ga commands guest-get-time and guest-set-time.
The previous thread about the interfaces introduced and the POSIX-specific command implementation has already been accepted, the reference link:
Quoted 5 lines
http://article.gmane.org/gmane.comp.emulators.qemu/198472
Notes:
Now It was tested on Windows XP SP3 and Windows 7. Please comment!
Series looks good other than comment in patch 1. I can fix this in tree or you can send another version.
Quoted 28 lines
Thanks.
Changes since v5:
- Fix the error check forGetSystemTime()from Michael.
- Other fixups from Michael.Changes since v4:
- Error handel improvement from Michael.
- Do the math explicitly for the time convert of FILETIME suggested by Michael.Changes since v3:
- Reorder theacquire_privilegeto avoid a possible leak of privileges suggested by Eric.Changes since v2:
- Overflow check improvement fortime_nsfrom Eric.Changes since v1:
- Make the macro for the offset between windows baseline and Unix Epoch more readable from Eric.
- Overflow check for filetime pointed by Eric.Lei Li (2):
qga: add windows implementation for guest-get-time
qga: add windows implementation for guest-set-time
Quoted 8 lines · mdroth
Quoted 7 lines · Lei Li
This patch series attempts to add Windows implementation for qemu-ga commands guest-get-time and guest-set-time.
The previous thread about the interfaces introduced and the POSIX-specific command implementation has already been accepted, the reference link:
Quoted 7 lines
Quoted 5 lines
http://article.gmane.org/gmane.comp.emulators.qemu/198472
Notes:
Now It was tested on Windows XP SP3 and Windows 7. Please comment!Series looks good other than comment in patch 1. I can fix this in tree or you can send another version.
I am very sorry for this, I should check the rest of it... :-( Sure, I will submit new version with this fixed.
Thanks for your time!
Quoted 30 lines
Reviewed-by Michael Roth <[email protected]>Quoted 27 lines
Thanks.
Changes since v5:
- Fix the error check forGetSystemTime()from Michael.
- Other fixups from Michael.Changes since v4:
- Error handel improvement from Michael.
- Do the math explicitly for the time convert of FILETIME suggested by Michael.Changes since v3:
- Reorder theacquire_privilegeto avoid a possible leak of privileges suggested by Eric.Changes since v2:
- Overflow check improvement fortime_nsfrom Eric.Changes since v1:
- Make the macro for the offset between windows baseline and Unix Epoch more readable from Eric.
- Overflow check for filetime pointed by Eric.Lei Li (2):
qga: add windows implementation for guest-get-time
qga: add windows implementation for guest-set-time
Signature · 1 line
Lei