From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:40916) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Trn8c-0004t6-HE for qemu-devel@nongnu.org; Sun, 06 Jan 2013 05:07:31 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Trn8b-00082u-5C for qemu-devel@nongnu.org; Sun, 06 Jan 2013 05:07:30 -0500 Received: from e28smtp02.in.ibm.com ([122.248.162.2]:34655) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Trn8a-00082i-I2 for qemu-devel@nongnu.org; Sun, 06 Jan 2013 05:07:29 -0500 Received: from /spool/local by e28smtp02.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Sun, 6 Jan 2013 15:35:59 +0530 Received: from d28relay04.in.ibm.com (d28relay04.in.ibm.com [9.184.220.61]) by d28dlp01.in.ibm.com (Postfix) with ESMTP id 62797E004C for ; Sun, 6 Jan 2013 15:37:32 +0530 (IST) Received: from d28av01.in.ibm.com (d28av01.in.ibm.com [9.184.220.63]) by d28relay04.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id r06A7KFL7733630 for ; Sun, 6 Jan 2013 15:37:20 +0530 Received: from d28av01.in.ibm.com (loopback [127.0.0.1]) by d28av01.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id r06A7Lgw001860 for ; Sun, 6 Jan 2013 10:07:22 GMT From: Lei Li Date: Sun, 6 Jan 2013 18:07:00 +0800 Message-Id: <1357466820-12860-4-git-send-email-lilei@linux.vnet.ibm.com> In-Reply-To: <1357466820-12860-1-git-send-email-lilei@linux.vnet.ibm.com> References: <1357466820-12860-1-git-send-email-lilei@linux.vnet.ibm.com> Subject: [Qemu-devel] [PATCH 3/3] qga: add guest-set-time command List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: aliguori@us.ibm.com, mdroth@linux.vnet.ibm.com, Lei Li Signed-off-by: Lei Li --- qga/commands-posix.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ qga/qapi-schema.json | 32 ++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 0 deletions(-) diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 190199d..7fff49a 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -121,6 +121,63 @@ struct HostTimeInfo *qmp_guest_get_time(Error **errp) return host_time; } +void qmp_guest_set_time(bool has_seconds, int64_t seconds, + bool has_microseconds, int64_t microseconds, + bool has_utc_offset, int64_t utc_offset, Error **errp) +{ + int ret; + int status; + pid_t pid, rpid; + struct timeval tv; + HostTimeInfo *host_time; + + if ((!has_seconds) && (!has_microseconds) && (!has_utc_offset)) { + host_time = get_host_time(); + if (!host_time) { + error_set(errp, QERR_QGA_COMMAND_FAILED, "Failed to set guest time"); + return; + } + tv.tv_sec = host_time->seconds; + tv.tv_usec = host_time->microseconds; + } else if (has_seconds && has_microseconds && has_utc_offset) { + tv.tv_sec = (time_t) seconds + utc_offset; + tv.tv_usec = (time_t) microseconds; + } else { + error_set(errp, QERR_INVALID_PARAMETER, "parameter missing"); + return; + } + + ret = settimeofday(&tv, NULL); + if (ret < 0) { + error_set(errp, QERR_QGA_COMMAND_FAILED, strerror(errno)); + return; + } + + /* Set the Hardware Clock to the current System Time. */ + pid = fork(); + if (pid == 0) { + setsid(); + reopen_fd_to_null(0); + reopen_fd_to_null(1); + reopen_fd_to_null(2); + + execle("/sbin/hwclock", "hwclock", "-w", NULL, environ); + _exit(EXIT_FAILURE); + } else if (pid < 0) { + goto exit_err; + } + + do { + rpid = waitpid(pid, &status, 0); + } while (rpid == -1 && errno == EINTR); + if (rpid == pid && WIFEXITED(status) && !WEXITSTATUS(status)) { + return; + } + +exit_err: + error_set(errp, QERR_UNDEFINED_ERROR); +} + typedef struct GuestFileHandle { uint64_t id; FILE *fh; diff --git a/qga/qapi-schema.json b/qga/qapi-schema.json index 4a8b93c..4649b55 100644 --- a/qga/qapi-schema.json +++ b/qga/qapi-schema.json @@ -117,6 +117,38 @@ 'returns': 'HostTimeInfo' } ## +# @guest-set-time: +# +# Set guest time. If none arguments were given, will set +# host time to guest. +# +# Right now, when a guest is paused or migrated to a file +# then loaded from that file, the guest OS has no idea that +# there was a big gap in the time. Depending on how long +# the gap was, NTP might not be able to resynchronize the +# guest. +# +# This command tries to set guest time based on the information +# from host or an absolute value given by management app, and +# set the Hardware Clock to the current System Time. This +# will make it easier for a guest to resynchronize without +# waiting for NTP. +# +# @seconds: #optional "seconds" time. +# +# @microseconds: #optional "microseconds" time. +# +# @utc-offset: #optional utc offset. +# +# Returns: Nothing on success. +# +# Since: 1.4 +## +{ 'command': 'guest-set-time', + 'data': { '*seconds': 'int', '*microseconds': 'int', + '*utc-offset': 'int' } } + +## # @GuestAgentCommandInfo: # # Information about guest agent commands. -- 1.7.7.6