From: NeilBrown Date: Sun, 8 Jul 2012 09:47:31 +0000 (+1000) Subject: wakealarmd: cope with delta between system time and RTC time. X-Git-Url: http://git.neil.brown.name/?a=commitdiff_plain;ds=inline;p=susman.git wakealarmd: cope with delta between system time and RTC time. If system time is different from RTC time, we need to allow for that difference when setting an alarm for a wakeup call. Signed-off-by: NeilBrown --- diff --git a/wakealarmd.c b/wakealarmd.c index dfd6adb..51d79cd 100644 --- a/wakealarmd.c +++ b/wakealarmd.c @@ -182,12 +182,24 @@ static int do_suspend(void *data) return 1; if (state->conns->stamp > now + 4) { - int fd = open("/sys/class/rtc/rtc0/wakealarm", O_WRONLY); + int fd = open("/sys/class/rtc/rtc0/since_epoch", O_RDONLY); + time_t rtc_now = now; + if (fd) { + char buf[20]; + int n = read(fd, buf, 20); + close(fd); + if (n > 1 && n < 20) { + buf[n] = 0; + rtc_now = strtoul(buf, NULL, 10); + } + } + fd = open("/sys/class/rtc/rtc0/wakealarm", O_WRONLY); if (fd >= 0) { char buf[20]; write(fd, "0\n", 2); sprintf(buf, "%lld\n", - (long long)state->conns->stamp - 2); + (long long)state->conns->stamp + - now + rtc_now - 2); write(fd, buf, strlen(buf)); close(fd); }