Skip to contentSkip to conversation

Add Windows support for time resync by qemu-ga

PATCH New Lei Li 1 participant 4 messages Mar 6, 2013

lore
Share & tools
lore

Lei Li

author
#0 lore raw

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:
For now, It was just tested on Windows XP SP3. I will test it on Windows 7 later. As I am new to windows development, so there may be a lot of flaws. Please let me know your suggestions, and comments are very welcome!

Thanks.

Lei Lii (2):
qga: add windows implementation for guest-get-time
qga: add windows implementation for guest-set-time

author
#1 lore raw

PATCH 3/4 qga: add windows implementation for guest-get-time

---
 qga/commands-win32.c |   28 ++++++++++++++++++++++++++++
 1 files changed, 28 insertions(+), 0 deletions(-)
qga/commands-win32.c +28 -0
index 7e8ecb3..4febec7 100644
@@ -22,6 +22,8 @@
 #define SHTDN_REASON_FLAG_PLANNED 0x80000000
 #endif
 
+#define _W32_FT_OFFSET (116444736000000000ULL)
+
 static void acquire_privilege(const char *name, Error **err)
 {
     HANDLE token;
@@ -108,6 +110,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.7.6
author
#2 lore raw

Re: [Qemu-devel] [PATCH 3/4] qga: add windows implementation for guest-get-time

Ops, wrong patch set... will resend it. Sorry for the missending. :-(

Quoted 51 lines · Lei Li
---
  qga/commands-win32.c |   28 ++++++++++++++++++++++++++++
  1 files changed, 28 insertions(+), 0 deletions(-)
qga/commands-win32.c +28 -0
index 7e8ecb3..4febec7 100644
@@ -22,6 +22,8 @@
  #define SHTDN_REASON_FLAG_PLANNED 0x80000000
  #endif
 
+#define _W32_FT_OFFSET (116444736000000000ULL)
+
  static void acquire_privilege(const char *name, Error **err)
  {
      HANDLE token;
@@ -108,6 +110,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
Lei
author
#3 lore raw

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

In reply to Lei Li
---
 qga/commands-win32.c |   35 +++++++++++++++++++++++++++++++++++
 1 files changed, 35 insertions(+), 0 deletions(-)
qga/commands-win32.c +35 -0
index 4febec7..1a90aa7 100644
@@ -136,6 +136,41 @@ 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;
+
+    /* year-2038 will overflow in case time_t is 32bit */
+    if (time_ns / 1000000000 != (time_t)(time_ns / 1000000000)) {
+        error_setg(errp, "Time %" PRId64 " is too large", time_ns);
+        return;
+    }
+
+    acquire_privilege(SE_SYSTEMTIME_NAME, errp);
+    if (error_is_set(errp)) {
+        error_setg(errp, "Failed to acquire privilege");
+        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.7.6