From d19016802710e3eb513a1f0ab1b2a193fa8112d2 Mon Sep 17 00:00:00 2001 From: Vincent Chang Date: Fri, 28 Sep 2012 14:38:50 +0800 Subject: [PATCH] Bug 793558 - Time API: changes does not persist after a restart. r=mwu --- hal/gonk/GonkHal.cpp | 43 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/hal/gonk/GonkHal.cpp b/hal/gonk/GonkHal.cpp index 8e231f102e8..f0ac51b11fb 100644 --- a/hal/gonk/GonkHal.cpp +++ b/hal/gonk/GonkHal.cpp @@ -56,7 +56,7 @@ #include "UeventPoller.h" #define LOG(args...) __android_log_print(ANDROID_LOG_INFO, "Gonk", args) -#define NsecPerMsec 1000000 +#define NsecPerMsec 1000000LL #define NsecPerSec 1000000000 // The header linux/oom.h is not available in bionic libc. We @@ -603,45 +603,42 @@ GetLight(hal::LightType light, hal::LightConfiguration* aConfig) return true; } -/** - * clock_settime() is not exposed through bionic. - * we define the new function to set system time. - * The result is the same as using clock_settime() system call. - */ -static int -sys_clock_settime(clockid_t clk_id, const struct timespec *tp) -{ - return syscall(__NR_clock_settime, clk_id, tp); -} - void AdjustSystemClock(int64_t aDeltaMilliseconds) { + int fd; + struct timespec now; + if (aDeltaMilliseconds == 0) { return; } - struct timespec now; - // Preventing context switch before setting system clock sched_yield(); clock_gettime(CLOCK_REALTIME, &now); - now.tv_sec += aDeltaMilliseconds / 1000; - now.tv_nsec += (aDeltaMilliseconds % 1000) * NsecPerMsec; - if (now.tv_nsec >= NsecPerSec) - { + now.tv_sec += (time_t)(aDeltaMilliseconds / 1000LL); + now.tv_nsec += (long)((aDeltaMilliseconds % 1000LL) * NsecPerMsec); + if (now.tv_nsec >= NsecPerSec) { now.tv_sec += 1; now.tv_nsec -= NsecPerSec; } - if (now.tv_nsec < 0) - { + if (now.tv_nsec < 0) { now.tv_nsec += NsecPerSec; now.tv_sec -= 1; } - // we need to have root privilege. - if (sys_clock_settime(CLOCK_REALTIME, &now) != 0) { - NS_ERROR("sys_clock_settime failed"); + + do { + fd = open("/dev/alarm", O_RDWR); + } while (fd == -1 && errno == EINTR); + ScopedClose autoClose(fd); + if (fd < 0) { + HAL_LOG(("Failed to open /dev/alarm: %s", strerror(errno))); + return; + } + + if (ioctl(fd, ANDROID_ALARM_SET_RTC, &now) < 0) { + HAL_LOG(("ANDROID_ALARM_SET_RTC failed: %s", strerror(errno))); return; }