Skip to contentSkip to conversation

Add Windows support for time resync by qemu-ga

PATCH Superseded Lei Li 2 participants 6 messages Mar 14, 2013

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Lei Li

author
#0 lore raw

v5 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 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 v5 1/2 qga: add windows implementation for guest-get-time

---
 qga/commands-win32.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
qga/commands-win32.c +33 -0
index 7e8ecb3..e24fb4a 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,33 @@ 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;
+    FILETIME tf;
+
+    GetSystemTime(ts);
+    if (!ts) {
+        error_setg(errp, "Failed to get time");
+        goto out;
+    }
+
+    if (!SystemTimeToFileTime(ts, &tf)) {
+        error_setg_errno(errp, errno, "Failed to convert system time");
+        goto out;
+    }
+
+    time_ns = ((((int64_t)tf.dwHighDateTime << 32) | tf.dwLowDateTime)
+                - W32_FT_OFFSET) * 100;
+
+    return time_ns;
+
+out:
+    g_free(ts);
+    return -1;
+}
+
 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
#2 lore raw

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

Quoted 23 lines · Lei Li
---
 qga/commands-win32.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
qga/commands-win32.c +6 -0
index 7e8ecb3..e24fb4a 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,33 @@ 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));

I still don't understand why we do just do:

SYSTEM ts = {0};

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

Quoted 4 lines
qga/commands-win32.c +4 -0
+    int64_t time_ns;
+    FILETIME tf;
+
+    GetSystemTime(ts);

followed by:

GetSystemTime(&ts);

(and same for SystemTimeToFileTime() below)

This would avoid the need to add common cleanup code for all the return paths.

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

Quoted 1 line
qga/commands-win32.c +1 -0
+    if (!ts) {

this is gonna always be false since we initialize it at the start of this function.

also, GetSystemTime() does seem to provide any error indication whatsoever, which is strange. But it also doesn't seem to have any guarantee this it will always succeed...

I think the best we could do is probably just some kind of sanity check, like making sure ts.wYear != 0, or maybe that 1601 <= ts.wYear <= 30827

Quoted 25 lines
qga/commands-win32.c +19 -0
+        error_setg(errp, "Failed to get time");
+        goto out;
+    }
+
+    if (!SystemTimeToFileTime(ts, &tf)) {
+        error_setg_errno(errp, errno, "Failed to convert system time");
+        goto out;
+    }
+
+    time_ns = ((((int64_t)tf.dwHighDateTime << 32) | tf.dwLowDateTime)
+                - W32_FT_OFFSET) * 100;
+
+    return time_ns;
+
+out:
+    g_free(ts);
+    return -1;
+}
+
 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
#3 lore raw

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

Quoted 51 lines · mdroth
Quoted 23 lines · Lei Li
---
  qga/commands-win32.c | 33 +++++++++++++++++++++++++++++++++
  1 file changed, 33 insertions(+)
qga/commands-win32.c +6 -0
index 7e8ecb3..e24fb4a 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,33 @@ 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));

I still don't understand why we do just do:

SYSTEM ts = {0};

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

Quoted 4 lines
qga/commands-win32.c +4 -0
+    int64_t time_ns;
+    FILETIME tf;
+
+    GetSystemTime(ts);

followed by:

GetSystemTime(&ts);

(and same for SystemTimeToFileTime() below)

This would avoid the need to add common cleanup code for all the return paths.

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

Quoted 1 line
qga/commands-win32.c +1 -0
+    if (!ts) {

this is gonna always be false since we initialize it at the start of this function.

Quoted 7 lines

also, GetSystemTime() does seem to provide any error indication whatsoever, which is strange. But it also doesn't seem to have any guarantee this it will always succeed...

I think the best we could do is probably just some kind of sanity check, like making sure ts.wYear != 0, or maybe that 1601 <= ts.wYear <= 30827

I was trying to check it by ts.wYear != 0 this afternoon... OK, I will send update later as you suggested.

Thanks!

Quoted 26 lines
Quoted 26 lines
qga/commands-win32.c +19 -0
+        error_setg(errp, "Failed to get time");
+        goto out;
+    }
+
+    if (!SystemTimeToFileTime(ts, &tf)) {
+        error_setg_errno(errp, errno, "Failed to convert system time");
+        goto out;
+    }
+
+    time_ns = ((((int64_t)tf.dwHighDateTime << 32) | tf.dwLowDateTime)
+                - W32_FT_OFFSET) * 100;
+
+    return time_ns;
+
+out:
+    g_free(ts);
+    return -1;
+}
+
  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
Signature · 1 line
Lei
author
#4 lore raw

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

In reply to Lei Li
---
 qga/commands-win32.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
qga/commands-win32.c +33 -0
index e24fb4a..8064c3a 100644
@@ -141,6 +141,39 @@ out:
     return -1;
 }
 
+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)) {
+        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
#5 lore raw

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

Quoted 27 lines · Lei Li
---
 qga/commands-win32.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
qga/commands-win32.c +14 -0
index e24fb4a..8064c3a 100644
@@ -141,6 +141,39 @@ out:
     return -1;
 }
 
+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;

qga/commands-win32.c out: · line 167

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

Sorry, missed this one on the last review:

Overriding a previously set error will cause an assert. We should just return and pass back the error from acquire_privilege.

Quoted 15 lines
qga/commands-win32.c +9 -0
+        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