Skip to contentSkip to conversation

Add Windows support for time resync by qemu-ga

PATCH Superseded [email protected] 3 participants 9 messages Mar 13, 2013 – Mar 14, 2013

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v4 cover Add Windows support for time resync by qemu-ga

From: Lei Li <[email protected]>

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

#1 lore raw

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

From: Lei Li <[email protected]>

---
 qga/commands-win32.c |   32 ++++++++++++++++++++++++++++++++
 1 files changed, 32 insertions(+), 0 deletions(-)
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.7.6
#2 lore raw

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

Quoted 25 lines · On Wed, Mar 13, 2013 at 06:10:30PM +0800, [email protected] wrote:

From: Lei Li <[email protected]>

---
 qga/commands-win32.c |   32 ++++++++++++++++++++++++++++++++
 1 files changed, 32 insertions(+), 0 deletions(-)
qga/commands-win32.c +6 -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;

qga/commands-win32.c void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err) · line 119

Quoted 6 lines
qga/commands-win32.c +3 -0
@@ -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));

Don't we need to free this at some point?

qga/commands-win32.c void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err) · line 128

Quoted 9 lines
qga/commands-win32.c +9 -0
+   int64_t time_ns;
+   union {
+       UINT64 ns100;
+       FILETIME tf;
+   } time;
+
+   GetSystemTime(ts);
+   if (!ts) {
+       slog("guest-get-time failed: %d", GetLastError());

GetSystemTime() does not seem to set an error that can be retrieved by GetLastError().

qga/commands-win32.c void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err) · line 138

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

I'm not sure how safe this union stuff is. The documentation suggests that in some circumstances the low/high fields in FILETIME might be padded for 64-bit alignment and that doing this type of cast could generate an alignment fault:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms724284(v=vs.85).aspx

or it might, perhaps even worse, just silently report the wrong time.

I think we should just do the math explicitly:

(((tf.dwHighDateTime << 32) | tf.dwLowDateTime) - W32_FT_OFFSET) * 100)

(or something along that line)

Quoted 10 lines
qga/commands-win32.c +4 -0
+
+   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
#3 lore raw

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

Quoted 29 lines · mdroth
Quoted 28 lines · On Wed, Mar 13, 2013 at 06:10:30PM +0800, [email protected] wrote:

From: Lei Li <[email protected]>

---
 qga/commands-win32.c |   32 ++++++++++++++++++++++++++++++++
 1 files changed, 32 insertions(+), 0 deletions(-)
qga/commands-win32.c +6 -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)
     }
 }
 
Quoted 5 lines

qga/commands-win32.c void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err) · line 119

Quoted 3 lines
qga/commands-win32.c +3 -0
+int64_t qmp_guest_get_time(Error **errp)
+{
+   SYSTEMTIME *ts = g_malloc0(sizeof(SYSTEMTIME));

Don't we need to free this at some point?

Actually, do we even need malloc? Can't be just allocate SYSTEMTIME struct on the stack and pass a pointer to GetSystemTime?

Quoted 50 lines

qga/commands-win32.c void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err) · line 128

Quoted 10 lines
qga/commands-win32.c +9 -0
+   int64_t time_ns;
+   union {
+       UINT64 ns100;
+       FILETIME tf;
+   } time;
+
+   GetSystemTime(ts);
+   if (!ts) {
+       slog("guest-get-time failed: %d", GetLastError());

GetSystemTime() does not seem to set an error that can be retrieved by GetLastError().

qga/commands-win32.c void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err) · line 138

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

I'm not sure how safe this union stuff is. The documentation suggests that in some circumstances the low/high fields in FILETIME might be padded for 64-bit alignment and that doing this type of cast could generate an alignment fault:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms724284(v=vs.85).aspx

or it might, perhaps even worse, just silently report the wrong time.

I think we should just do the math explicitly:

(((tf.dwHighDateTime << 32) | tf.dwLowDateTime) - W32_FT_OFFSET) * 100)

(or something along that line)

Quoted 10 lines
qga/commands-win32.c +4 -0
+
+   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
#4 lore raw

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

In reply to mdroth
Quoted 29 lines · mdroth
Quoted 28 lines · On Wed, Mar 13, 2013 at 06:10:30PM +0800, [email protected] wrote:

From: Lei Li <[email protected]>

---
  qga/commands-win32.c |   32 ++++++++++++++++++++++++++++++++
  1 files changed, 32 insertions(+), 0 deletions(-)
qga/commands-win32.c +6 -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)
      }
  }
 
Quoted 5 lines

qga/commands-win32.c void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err) · line 119

Quoted 3 lines
qga/commands-win32.c +3 -0
+int64_t qmp_guest_get_time(Error **errp)
+{
+   SYSTEMTIME *ts = g_malloc0(sizeof(SYSTEMTIME));

Don't we need to free this at some point?

Sorry, my fault here...

Quoted 11 lines

qga/commands-win32.c void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err) · line 128

Quoted 9 lines
qga/commands-win32.c +9 -0
+   int64_t time_ns;
+   union {
+       UINT64 ns100;
+       FILETIME tf;
+   } time;
+
+   GetSystemTime(ts);
+   if (!ts) {
+       slog("guest-get-time failed: %d", GetLastError());

GetSystemTime() does not seem to set an error that can be retrieved by GetLastError().

Yes, you are right.

Quoted 17 lines

qga/commands-win32.c void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err) · line 138

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

I'm not sure how safe this union stuff is. The documentation suggests that in some circumstances the low/high fields in FILETIME might be padded for 64-bit alignment and that doing this type of cast could generate an alignment fault:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms724284(v=vs.85).aspx

Quoted 7 lines

or it might, perhaps even worse, just silently report the wrong time.

I think we should just do the math explicitly:

(((tf.dwHighDateTime << 32) | tf.dwLowDateTime) - W32_FT_OFFSET) * 100)

(or something along that line)

OK, thanks.

Quoted 10 lines
Quoted 10 lines
qga/commands-win32.c +4 -0
+
+   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
Signature · 1 line
Lei
#5 lore raw

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

In reply to [email protected]

From: Lei Li <[email protected]>

---
 qga/commands-win32.c |   34 ++++++++++++++++++++++++++++++++++
 1 files changed, 34 insertions(+), 0 deletions(-)
qga/commands-win32.c +34 -0
index 0a2bb34..a0c8d43 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;
+
+    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)) {
+        error_setg(errp, "Failed to acquire privilege");
+        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
#6 lore raw

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

Quoted 35 lines · On Wed, Mar 13, 2013 at 06:10:31PM +0800, [email protected] wrote:

From: Lei Li <[email protected]>

---
 qga/commands-win32.c |   34 ++++++++++++++++++++++++++++++++++
 1 files changed, 34 insertions(+), 0 deletions(-)
qga/commands-win32.c +20 -0
index 0a2bb34..a0c8d43 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;
+
+    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;
+    }

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

Quoted 10 lines
qga/commands-win32.c +10 -0
+
+    acquire_privilege(SE_SYSTEMTIME_NAME, errp);
+    if (error_is_set(errp)) {
+        error_setg(errp, "Failed to acquire privilege");
+        return;
+    }
+
+    if (!SetSystemTime(&ts)) {
+        slog("guest-set-time failed: %d", GetLastError());
+        error_setg_errno(errp, errno, "Failed to set time to guest");

I think we want to pass back GetLastError(), SetSystemTime doesn't set errno (and if it did something in the slog() callchain probably would've clobbered it)

Looks good otherwise.

Quoted 10 lines
qga/commands-win32.c +4 -0
+        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
author
#7 lore raw

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

Quoted 41 lines · mdroth
Quoted 40 lines · On Wed, Mar 13, 2013 at 06:10:31PM +0800, [email protected] wrote:

From: Lei Li <[email protected]>

---
  qga/commands-win32.c |   34 ++++++++++++++++++++++++++++++++++
  1 files changed, 34 insertions(+), 0 deletions(-)
qga/commands-win32.c +25 -0
index 0a2bb34..a0c8d43 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;
+
+    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)) {
+        error_setg(errp, "Failed to acquire privilege");
+        return;
Quoted 8 lines

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

Quoted 5 lines
qga/commands-win32.c +5 -0
+    }
+
+    if (!SetSystemTime(&ts)) {
+        slog("guest-set-time failed: %d", GetLastError());
+        error_setg_errno(errp, errno, "Failed to set time to guest");

I think we want to pass back GetLastError(), SetSystemTime doesn't set errno (and if it did something in the slog() callchain probably would've clobbered it)

OK.

Quoted 12 lines

Looks good otherwise.

Quoted 10 lines
qga/commands-win32.c +4 -0
+        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
Signature · 1 line
Lei
#8 lore raw
In reply to [email protected]
Quoted 8 lines · On 03/13/2013 04:10 AM, [email protected] wrote:

From: Lei Li <[email protected]>

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:

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