diff mbox

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

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

Commit Message

Lei Li March 14, 2013, 7:07 a.m. UTC
Signed-off-by: Lei Li <lilei@linux.vnet.ibm.com>
---
 qga/commands-win32.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

Comments

Michael Roth March 14, 2013, 12:38 p.m. UTC | #1
On Thu, Mar 14, 2013 at 03:07:51PM +0800, Lei Li wrote:
> Signed-off-by: Lei Li <lilei@linux.vnet.ibm.com>
> ---
>  qga/commands-win32.c | 33 +++++++++++++++++++++++++++++++++
>  1 file changed, 33 insertions(+)
> 
> diff --git a/qga/commands-win32.c b/qga/commands-win32.c
> index e24fb4a..8064c3a 100644
> --- a/qga/commands-win32.c
> +++ b/qga/commands-win32.c
> @@ -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");

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.

> +        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);
> -- 
> 1.7.11.7
>
diff mbox

Patch

diff --git a/qga/commands-win32.c b/qga/commands-win32.c
index e24fb4a..8064c3a 100644
--- a/qga/commands-win32.c
+++ b/qga/commands-win32.c
@@ -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);