diff mbox

[2/2] qga: add windows implementation for guest-set-time

Message ID 1362761807-32475-3-git-send-email-lilei@linux.vnet.ibm.com
State New
Headers show

Commit Message

Lei Li March 8, 2013, 4:56 p.m. UTC
Signed-off-by: Lei Li <lilei@linux.vnet.ibm.com>
---
 qga/commands-win32.c |   35 +++++++++++++++++++++++++++++++++++
 1 files changed, 35 insertions(+), 0 deletions(-)

Comments

Eric Blake March 8, 2013, 8:11 p.m. UTC | #1
On 03/08/2013 09:56 AM, Lei Li wrote:
> Signed-off-by: Lei Li <lilei@linux.vnet.ibm.com>
> ---
>  qga/commands-win32.c |   35 +++++++++++++++++++++++++++++++++++
>  1 files changed, 35 insertions(+), 0 deletions(-)
> 

> +
> +    time = time_ns / 100 + W32_FT_OFFSET;
> +    /* FILETIME values should be less than 0x8000000000000000
> +       for function FileTimeToSystemTime. */
> +    if (time & 0x8000000000000000) {
> +        error_setg(errp, "Time %" PRId64 "is too large", time);
> +        return;
> +    }

That doesn't cover all cases of overflow.  Better would be:

if (time_ns < 0 || time_ns / 100 > INT64_MAX - W32_FT_OFFSET) {
    error ...
}
time = time_ns / 100 + W32_FT_OFFSET;
diff mbox

Patch

diff --git a/qga/commands-win32.c b/qga/commands-win32.c
index 0a2bb34..b5d4bd3 100644
--- a/qga/commands-win32.c
+++ b/qga/commands-win32.c
@@ -140,6 +140,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;
+
+    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;
+    /* FILETIME values should be less than 0x8000000000000000
+       for function FileTimeToSystemTime. */
+    if (time & 0x8000000000000000) {
+        error_setg(errp, "Time %" PRId64 "is too large", time);
+        return;
+    }
+
+    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);