Bug 793558 - Time API: changes does not persist after a restart. r=mwu

This commit is contained in:
Vincent Chang 2012-09-28 14:38:50 +08:00
parent 2c7deb441b
commit d190168027

View File

@ -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;
}