Skip to contentSkip to conversation

Add Windows support for time resync by qemu-ga

PATCH Superseded Lei Li 2 participants 5 messages Mar 12, 2013 – Mar 13, 2013

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
author
#1 lore raw

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

In reply to Lei Li
---
 qga/commands-win32.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
qga/commands-win32.c +32 -0
index 7e8ecb3..0a2bb34 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;
@@ -108,6 +114,32 @@ void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err)
     }
 }
 
+int64_t qmp_guest_get_time(Error **errp)
+{
+   SYSTEMTIME *ts = g_malloc0(sizeof(SYSTEMTIME));
+   int64_t time_ns;
+   union {
+       UINT64 ns100;
+       FILETIME tf;
+   } time;
+
+   GetSystemTime(ts);
+   if (!ts) {
+       slog("guest-get-time failed: %d", GetLastError());
+       error_setg_errno(errp, errno, "Failed to get time");
+       return -1;
+   }
+
+   if (!SystemTimeToFileTime(ts, &time.tf)) {
+       error_setg_errno(errp, errno, "Failed to convert system time");
+       return -1;
+   }
+
+   time_ns = (int64_t)((time.ns100 - 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
author
#2 lore raw

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

In reply to Lei Li
---
 qga/commands-win32.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
qga/commands-win32.c +34 -0
index 0a2bb34..e000324 100644
@@ -140,6 +140,40 @@ 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;
+
+    acquire_privilege(SE_SYSTEMTIME_NAME, errp);
+    if (error_is_set(errp)) {
+        error_setg(errp, "Failed to acquire privilege");
+        return;
+    }
+
+    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;
+    }
+
+    if (!SetSystemTime(&ts)) {
+        slog("guest-set-time failed: %d", GetLastError());
+        error_setg_errno(errp, errno, "Failed to set time to guest");
+        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

Lei Li

author
#0 lore raw

v3 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 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

#3 lore raw

Re: [Qemu-devel] [PATCH 2/2] qga: add windows implementation for guest-set-time

In reply to Lei Li
Quoted 7 lines · Lei Li
---
 qga/commands-win32.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
qga/commands-win32.c +1 -0
+void qmp_guest_set_time(int64_t time_ns, Error **errp)

qga/commands-win32.c int64_t qmp_guest_get_time(Error **errp) · line 153

Quoted 10 lines
qga/commands-win32.c +10 -0
+{
+    SYSTEMTIME ts;
+    FILETIME tf;
+    LONGLONG time;
+
+    acquire_privilege(SE_SYSTEMTIME_NAME, errp);
+    if (error_is_set(errp)) {
+        error_setg(errp, "Failed to acquire privilege");
+        return;
+    }

Earlier, you told me that acquire_privilege is auto-dropped after a successful SetSystemTime. But here, you acquire the privilege...

qga/commands-win32.c int64_t qmp_guest_get_time(Error **errp) · line 157

Quoted 4 lines
qga/commands-win32.c +4 -0
+
+    if (time_ns < 0 || time_ns / 100 > INT64_MAX - W32_FT_OFFSET) {
+        error_setg(errp, "Time %" PRId64 "is invalid", time_ns);
+        return;

...then return early without ever relinquishing it.

qga/commands-win32.c int64_t qmp_guest_get_time(Error **errp) · line 168

Quoted 11 lines
qga/commands-win32.c +11 -0
+    }
+
+    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;
+    }

I would reorder the acquire_privilege to here, to give us the best possible chance of avoiding a leak of privileges when the user passes bogus data.

Quoted 12 lines
qga/commands-win32.c +8 -0
+
+    if (!SetSystemTime(&ts)) {
+        slog("guest-set-time failed: %d", GetLastError());
+        error_setg_errno(errp, errno, "Failed to set time to guest");
+        return;
+    }
+}
+
 int64_t qmp_guest_file_open(const char *path, bool has_mode, const char *mode, Error **err)
 {
     error_set(err, QERR_UNSUPPORTED);
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#4 lore raw

Re: [Qemu-devel] [PATCH 2/2] qga: add windows implementation for guest-set-time

Quoted 26 lines · Eric Blake
Quoted 6 lines · Lei Li
---
  qga/commands-win32.c | 34 ++++++++++++++++++++++++++++++++++
  1 file changed, 34 insertions(+)
qga/commands-win32.c +1 -0
+void qmp_guest_set_time(int64_t time_ns, Error **errp)

qga/commands-win32.c int64_t qmp_guest_get_time(Error **errp) · line 153

Quoted 10 lines
qga/commands-win32.c +10 -0
+{
+    SYSTEMTIME ts;
+    FILETIME tf;
+    LONGLONG time;
+
+    acquire_privilege(SE_SYSTEMTIME_NAME, errp);
+    if (error_is_set(errp)) {
+        error_setg(errp, "Failed to acquire privilege");
+        return;
+    }

Earlier, you told me that acquire_privilege is auto-dropped after a successful SetSystemTime. But here, you acquire the privilege...

qga/commands-win32.c int64_t qmp_guest_get_time(Error **errp) · line 157

Quoted 4 lines
qga/commands-win32.c +4 -0
+
+    if (time_ns < 0 || time_ns / 100 > INT64_MAX - W32_FT_OFFSET) {
+        error_setg(errp, "Time %" PRId64 "is invalid", time_ns);
+        return;

...then return early without ever relinquishing it.

Quoted 14 lines

qga/commands-win32.c int64_t qmp_guest_get_time(Error **errp) · line 168

Quoted 11 lines
qga/commands-win32.c +11 -0
+    }
+
+    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;
+    }

I would reorder the acquire_privilege to here, to give us the best possible chance of avoiding a leak of privileges when the user passes bogus data.

It make sense, I should thought about this, thank you!

Quoted 12 lines
Quoted 12 lines
qga/commands-win32.c +8 -0
+
+    if (!SetSystemTime(&ts)) {
+        slog("guest-set-time failed: %d", GetLastError());
+        error_setg_errno(errp, errno, "Failed to set time to guest");
+        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
Lei