Skip to contentSkip to conversation

Add Windows support for time resync by qemu-ga

PATCH Applied Lei Li 3 participants 5 messages Mar 15, 2013 – Mar 25, 2013

lore
Share & tools
lore

Lei Li

author
#0 lore raw

v7 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 v6:
- Fix other error handel that I missed.

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

author
#1 lore raw

PATCH v7 1/2 qga: add windows implementation for guest-get-time

---
 qga/commands-win32.c | 27 +++++++++++++++++++++++++--
 1 file changed, 25 insertions(+), 2 deletions(-)
qga/commands-win32.c +25 -2
index b19be9d..d98e3ee 100644
@@ -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;
@@ -280,8 +286,25 @@ GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **err)
 
 int64_t qmp_guest_get_time(Error **errp)
 {
-    error_set(errp, QERR_UNSUPPORTED);
-    return -1;
+    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(errp, "Failed to convert system time: %d", (int)GetLastError());
+        return -1;
+    }
+
+    time_ns = ((((int64_t)tf.dwHighDateTime << 32) | tf.dwLowDateTime)
+                - W32_FT_OFFSET) * 100;
+
+    return time_ns;
 }
 
 void qmp_guest_set_time(int64_t time_ns, Error **errp)
Signature · 1 line
1.7.11.7
author
#2 lore raw

PATCH v7 2/2 qga: add windows implementation for guest-set-time

In reply to Lei Li
---
 qga/commands-win32.c | 29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)
qga/commands-win32.c +28 -1
index d98e3ee..24e4ad0 100644
@@ -309,7 +309,34 @@ int64_t qmp_guest_get_time(Error **errp)
 
 void qmp_guest_set_time(int64_t time_ns, Error **errp)
 {
-    error_set(errp, QERR_UNSUPPORTED);
+    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 %d", (int)GetLastError());
+        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;
+    }
 }
 
 GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp)
Signature · 1 line
1.7.11.7
#3 lore raw
In reply to Lei Li
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: Reviewed-by: Eric Blake <[email protected]>

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Applied
#4 lore raw
In reply to Lei Li
Quoted 2 lines · Lei Li

This patch series attempts to add Windows implementation for qemu-ga commands guest-get-time and guest-set-time.

Thanks, applied to qga branch.

Quoted 41 lines

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 v6:
- Fix other error handel that I missed.

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