]> git.neil.brown.name Git - susman.git/commitdiff
wakealarmd: cope with delta between system time and RTC time. master
authorNeilBrown <neilb@suse.de>
Sun, 8 Jul 2012 09:47:31 +0000 (19:47 +1000)
committerNeilBrown <neilb@suse.de>
Sun, 8 Jul 2012 09:47:31 +0000 (19:47 +1000)
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 <neilb@suse.de>
wakealarmd.c

index dfd6adb61d587d2d2537da84c9f3fa33cad127bf..51d79cd58dfe23861a2bc9965b4b2c5fbdba27d8 100644 (file)
@@ -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);
                }