[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: rtc and chrony on Ultra 1 and Ultra 5



Hello David,

I've run the new patch. See below.

FYI, I had already submitted a bug against chrony, mainly because it floods
the log files in an unacceptable way:
http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=301592
The last comment in that report is interesting. Does Sparc use genrtc?

Cheers,
Frans

P.S. No harm done to my system.

On Tuesday 19 April 2005 23:49, David S. Miller wrote:
> Or we can do something like this, provide the PC rtc ioctls
> in the Mostek RTC driver.

Yes, that sounds like a nice approach.

> @@ -164,6 +225,7 @@
>  		printk(KERN_ERR "rtc: unable to get misc minor for Mostek\n");
>  		return error;
>  	}
> +	printk("rtc_sun_init: Registered Mostek RTC driver.\n");
>
>  	return 0;
>  }

IMO this bit should go in anyway (though maybe without the closing period).
It makes at least clear that a driver for the RTC _is_ loaded. The message
appears a bit later in the kernel log than the "No PC RTC" message:

Apr 20 16:25:24 localhost kernel: rtc_init: no PC rtc found
Apr 20 16:25:24 localhost kernel: su0 at 0x000001fff13062f8 (irq = 4,7ea) is a 16550A
Apr 20 16:25:24 localhost kernel: su1 at 0x000001fff13083f8 (irq = 9,7e9) is a 16550A
Apr 20 16:25:24 localhost kernel: ttyS0 at MMIO 0x1fff1400000 (irq = 7052256) is a SAB82532 V3.2
Apr 20 16:25:24 localhost kernel: ttyS1 at MMIO 0x1fff1400040 (irq = 7052256) is a SAB82532 V3.2
Apr 20 16:25:24 localhost kernel: Using anticipatory io scheduler
Apr 20 16:25:24 localhost kernel: Floppy drive(s): fd0 is 1.44M
Apr 20 16:25:24 localhost kernel: FDC 0 is a National Semiconductor PC87306
Apr 20 16:25:24 localhost kernel: RAMDISK driver initialized: 16 RAM disks of 8192K size 1024 blocksize
Apr 20 16:25:24 localhost kernel: loop: loaded (max 8 devices)
Apr 20 16:25:24 localhost kernel: Uniform Multi-Platform E-IDE driver Revision: 7.00alpha2
Apr 20 16:25:24 localhost kernel: ide: Assuming 33MHz system bus speed for PIO modes; override with idebus=xx
Apr 20 16:25:24 localhost kernel: CMD646: IDE controller at PCI slot 0000:01:03.0
Apr 20 16:25:24 localhost kernel: CMD646: chipset revision 3
Apr 20 16:25:24 localhost kernel: CMD646: chipset revision 0x03, MultiWord DMA Force Limited
Apr 20 16:25:24 localhost kernel: CMD646: 100%% native mode on irq 4,7e0
Apr 20 16:25:24 localhost kernel:     ide0: BM-DMA at 0x1fe02c00020-0x1fe02c00027, BIOS settings: hda:pio, hdb:pio
Apr 20 16:25:24 localhost kernel:     ide1: BM-DMA at 0x1fe02c00028-0x1fe02c0002f, BIOS settings: hdc:pio, hdd:pio
Apr 20 16:25:24 localhost kernel: hda: ST34342A, ATA DISK drive
Apr 20 16:25:24 localhost kernel: ide0 at 0x1fe02c00000-0x1fe02c00007,0x1fe02c0000a on irq 4,7e0
Apr 20 16:25:24 localhost kernel: hdc: CD-ROM 56X/AKH, ATAPI CD/DVD-ROM drive
Apr 20 16:25:24 localhost kernel: ide1 at 0x1fe02c00010-0x1fe02c00017,0x1fe02c0001a on irq 4,7e0 (shared with ide0)
Apr 20 16:25:24 localhost kernel: hda: max request size: 128KiB
Apr 20 16:25:24 localhost kernel: hda: 8404830 sectors (4303 MB), CHS=8894/15/63, (U)DMA
Apr 20 16:25:24 localhost kernel:  hda: hda1 hda2 hda3 hda4
Apr 20 16:25:24 localhost kernel: rtc_sun_init: Registered Mostek RTC driver.


However, the patch does not yet solve the problem with chrony.
I've taken a look at chrony's source. The relevant files seem to be
rtc_linux.[hc] (attached).

The .c file defines the calls that are expected to be supported. Not all
seem to be actually used. AFAICT only the following calls are used:
RTC_UIE_ON, RTC_UIE_OFF
RTC_RD_TIME, RTC_SET_TIME

I've also attached an strace from starting the chrony daemon and marked the
relevant (AFAICT) lines.
From this I conclude that the following statements from rtc_linux.c are
causing the problems:
    status = ioctl(fd, RTC_UIE_ON, 0);
    status = read(fd, &data, sizeof(data));
    status = ioctl(fd, RTC_UIE_OFF, 0);
with fd refering to /dev/rtc.
   execve("/usr/sbin/chronyd", ["chronyd", "-d"], [/* 23 vars */]) = 0
   uname({sys="Linux", node="gimli", ...}) = 0
   brk(0)                                  = 0x3e000
   access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
   open("/etc/ld.so.preload", O_RDONLY)    = -1 ENOENT (No such file or directory)
   open("/etc/ld.so.cache", O_RDONLY)      = 3
   fstat64(3, {st_mode=S_IFREG|0644, st_size=24015, ...}) = 0
   mmap(NULL, 24015, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7001c000
   close(3)                                = 0
   access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
   open("/lib/libc.so.6", O_RDONLY)        = 3
   read(3, "\177ELF\1\2\1\0\0\0\0\0\0\0\0\0\0\3\0\2\0\0\0\1\0\1\316"..., 512) = 512
   fstat64(3, {st_mode=S_IFREG|0644, st_size=1291948, ...}) = 0
   mmap(NULL, 1361864, PROT_READ|PROT_EXEC, MAP_PRIVATE, 3, 0) = 0x7002c000
   mprotect(0x70160000, 100296, PROT_NONE) = 0
   mmap(0x7016c000, 49152, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_FIXED, 3, 0x130000) = 0x7016c000
   mmap(0x70178000, 1992, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x70178000
   close(3)                                = 0
   munmap(0x7001c000, 24015)               = 0
   getuid32()                              = 0
   brk(0)                                  = 0x3e000
   brk(0x60000)                            = 0x60000
   brk(0)                                  = 0x60000
   open("/var/run/chronyd.pid", O_RDONLY)  = -1 ENOENT (No such file or directory)
   open("/var/run/chronyd.pid", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3
   getpid()                                = 3906
   fstat64(3, {st_mode=S_IFREG|0644, st_size=0, ...}) = 0
   mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7001c000
   write(3, "3906\n", 5)                   = 5
   close(3)                                = 0
   munmap(0x7001c000, 8192)                = 0
   open("/etc/chrony/chrony.conf", O_RDONLY) = 3
   fstat64(3, {st_mode=S_IFREG|0644, st_size=3288, ...}) = 0
   mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7001c000
   read(3, "# This the default chrony.conf f"..., 8192) = 3288
   gettimeofday({1114011423, 285523}, NULL) = 0
   getpid()                                = 3906
   open("/etc/resolv.conf", O_RDONLY)      = 4
   fstat64(4, {st_mode=S_IFREG|0644, st_size=62, ...}) = 0
   mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7001e000
   read(4, "search fjphome.nl\nnameserver 10."..., 8192) = 62
   read(4, "", 8192)                       = 0
   close(4)                                = 0
   munmap(0x7001e000, 8192)                = 0
   read(3, "", 8192)                       = 0
   close(3)                                = 0
   munmap(0x7001c000, 8192)                = 0
   gettimeofday({1114011423, 292091}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 292554}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 293033}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 293495}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 293972}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 294434}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 294910}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 295372}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 295850}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 296310}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 296786}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 297249}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 297722}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 298189}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 298651}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 299132}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 299594}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 300072}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 300534}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 301013}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 301475}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 301953}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 302415}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 302890}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 303352}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 303899}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 304355}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 304820}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 305270}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 305730}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 306184}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 306633}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 307101}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 307551}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 308047}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 308496}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 308963}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 309413}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 309884}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 310334}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 310796}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 311247}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 311708}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 312161}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 312610}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 313080}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 313529}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 314000}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 314449}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 314916}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 315365}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 315830}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 316280}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 316744}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 317195}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 317644}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 318112}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 318562}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 319032}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 319481}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 319948}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 320397}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 320864}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 321314}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 321778}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 322229}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 322678}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 323144}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 323594}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 324063}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 324513}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 324981}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 325430}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 325897}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 326346}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 326811}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 327260}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 327721}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 328174}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 328623}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 329091}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 329541}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 330009}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 330463}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 330928}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 331377}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 331843}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 332293}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 332758}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 333210}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 333659}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 334126}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 334575}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 335041}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 335490}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 335960}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 336409}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 336876}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 337408}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 337876}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 338325}, {4294967176, 0}) = 0
   adjtimex({modes=0, offset=0, freq=1407230, maxerror=16384000, esterror=16384000, status=64, constant=2, precision=1, tolerance=33554432, time={1114011423, 338935}}) = 5
   time([1114011423])                      = 1114011423
   open("/etc/localtime", O_RDONLY)        = 3
   fstat64(3, {st_mode=S_IFREG|0644, st_size=1074, ...}) = 0
   mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7001c000
   read(3, "TZif\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\r\0\0\0\r\0"..., 8192) = 1074
   close(3)                                = 0
   munmap(0x7001c000, 8192)                = 0
   write(2, "sys_linux.c:649:(get_version_spe"..., 60sys_linux.c:649:(get_version_specific_details)[20-15:37:03] ) = 60
   write(2, "Initial txc.tick=9995 txc.freq=1"..., 87Initial txc.tick=9995 txc.freq=1407230 (21.47262573) txc.offset=0 => hz=100 shift_hz=7
   ) = 87
   time([1114011423])                      = 1114011423
   write(2, "sys_linux.c:665:(get_version_spe"..., 60sys_linux.c:665:(get_version_specific_details)[20-15:37:03] ) = 60
   write(2, "set_config_hz=0 hz=100 shift_hz="..., 120set_config_hz=0 hz=100 shift_hz=7 basic_freq_scale=1.28000000 nominal_tick=10000 slew_delta_tick=833 max_tick_bias=1000
   ) = 120
   uname({sys="Linux", node="gimli", ...}) = 0
   time([1114011423])                      = 1114011423
   write(2, "sys_linux.c:703:(get_version_spe"..., 60sys_linux.c:703:(get_version_specific_details)[20-15:37:03] ) = 60
   write(2, "Linux kernel major=2 minor=6 pat"..., 37Linux kernel major=2 minor=6 patch=8
   ) = 37
   time([1114011423])                      = 1114011423
   write(2, "sys_linux.c:787:(get_version_spe"..., 60sys_linux.c:787:(get_version_specific_details)[20-15:37:03] ) = 60
   write(2, "calculated_freq_scale=0.99902439"..., 55calculated_freq_scale=0.99902439 freq_scale=0.99902439
   ) = 55
   adjtimex({modes=0, offset=0, freq=1407230, maxerror=16384000, esterror=16384000, status=64, constant=2, precision=1, tolerance=33554432, time={1114011423, 355859}}) = 5
   open("/var/lib/chrony/chrony.drift", O_RDONLY) = 3
   fstat64(3, {st_mode=S_IFREG|0644, st_size=42, ...}) = 0
   mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7001c000
   read(3, "            478.5064            "..., 8192) = 42
   close(3)                                = 0
   munmap(0x7001c000, 8192)                = 0
   open("/var/lib/chrony/chrony.drift.tmp", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3
   fstat64(3, {st_mode=S_IFREG|0644, st_size=0, ...}) = 0
   mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7001c000
   write(3, "            478.5064            "..., 42) = 42
   close(3)                                = 0
   munmap(0x7001c000, 8192)                = 0
   stat64("/var/lib/chrony/chrony.drift", {st_mode=S_IFREG|0644, st_size=42, ...}) = 0
   chown32("/var/lib/chrony/chrony.drift.tmp", 0, 0) = 0
   chmod("/var/lib/chrony/chrony.drift.tmp", 0644) = 0
   rename("/var/lib/chrony/chrony.drift.tmp", "/var/lib/chrony/chrony.drift") = 0
   adjtimex({modes=16402, offset=0, freq=1407230, maxerror=16384000, esterror=16384000, status=64, constant=2, precision=1, tolerance=33554432, time={1114011423, 369597}}) = 5
   gettimeofday({1114011423, 371137}, {4294967176, 0}) = 0
   adjtimex({modes=32769, offset=0, freq=1407230, maxerror=16384000, esterror=16384000, status=64, constant=2, precision=1, tolerance=33554432, time={1114011423, 371597}}) = 5
   adjtimex({modes=32769, offset=0, freq=1407230, maxerror=16384000, esterror=16384000, status=64, constant=2, precision=1, tolerance=33554432, time={1114011423, 373139}}) = 5
   stat64("/var", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
   stat64("/var/log", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
   stat64("/var/log/chrony", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
   open("/var/log/chrony/tracking.log", O_WRONLY|O_APPEND|O_CREAT, 0666) = 3
   fstat64(3, {st_mode=S_IFREG|0644, st_size=6701, ...}) = 0
   mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7001c000
   fstat64(3, {st_mode=S_IFREG|0644, st_size=6701, ...}) = 0
   _llseek(3, 6701, [6701], SEEK_SET)      = 0
   stat64("/var", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
   stat64("/var/log", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
   stat64("/var/log/chrony", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
   open("/var/log/chrony/statistics.log", O_WRONLY|O_APPEND|O_CREAT, 0666) = 4
   fstat64(4, {st_mode=S_IFREG|0644, st_size=16983, ...}) = 0
   mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7001e000
   fstat64(4, {st_mode=S_IFREG|0644, st_size=16983, ...}) = 0
   _llseek(4, 16983, [16983], SEEK_SET)    = 0
   stat64("/var", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
   stat64("/var/log", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
   stat64("/var/log/chrony", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
   open("/var/log/chrony/measurements.log", O_WRONLY|O_APPEND|O_CREAT, 0666) = 5
   fstat64(5, {st_mode=S_IFREG|0644, st_size=21948, ...}) = 0
   mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x70020000
   fstat64(5, {st_mode=S_IFREG|0644, st_size=21948, ...}) = 0
   _llseek(5, 21948, [21948], SEEK_SET)    = 0
   gettimeofday({1114011423, 399878}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 400371}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 400843}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 401309}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 401779}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 402245}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 402713}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 403179}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 403633}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 404117}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 404570}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 405054}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 405507}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 405993}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 406446}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 406931}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 407385}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 407868}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 408322}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 408804}, {4294967176, 0}) = 0
   socket(PF_INET, SOCK_DGRAM, IPPROTO_IP) = 6
   rt_sigtimedwait(ptrace: umoven: Input/output error
   [?], ptrace: umoven: Input/output error
   0xffff, 0x4, -268437164) = 0
   rt_sigtimedwait(ptrace: umoven: Input/output error
   [?], ptrace: umoven: Input/output error
   0xffff, 0x20, -268437164) = 0
   rt_sigpending(ptrace: umoven: Input/output error
   [?])                      = 0
   open("/etc/chrony/chrony.keys", O_RDONLY) = 7
   fstat64(7, {st_mode=S_IFREG|0640, st_size=11, ...}) = 0
   mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x70022000
   read(7, "1 PASSWORD\n", 8192)           = 11
   read(7, "", 8192)                       = 0
   close(7)                                = 0
   munmap(0x70022000, 8192)                = 0
   time(NULL)                              = 1114011423
   socket(PF_INET, SOCK_DGRAM, IPPROTO_IP) = 7
   rt_sigtimedwait(ptrace: umoven: Input/output error
   [?], ptrace: umoven: Input/output error
   0xffff, 0x4, -268437164) = 0
   rt_sigpending(ptrace: umoven: Input/output error
   [?])                      = 0
!! open("/dev/rtc", O_RDWR)                = 8
   gettimeofday({1114011423, 417921}, {4294967176, 0}) = 0
   gettimeofday({1114011423, 418454}, {4294967176, 0}) = 0
!! ioctl(8, PRESTO_GETMOUNT or RTC_UIE_ON, 0) = -1 EINVAL (Invalid argument)
!! ioctl(8, PRESTO_GETMOUNT or RTC_UIE_ON, 0) = -1 EINVAL (Invalid argument)
   time([1114011423])                      = 1114011423
   write(2, "rtc_linux.c:734:(switch_interrup"..., 49rtc_linux.c:734:(switch_interrupts)[20-15:37:03] ) = 49
   write(2, "Could not start measurement : In"..., 47Could not start measurement : Invalid argument
   ) = 47
   rt_sigaction(SIGINT, {0x13cc8, [INT], SA_RESTART}, {SIG_DFL}, 0x7005f010, 4294967295) = 0
   rt_sigaction(SIGTERM, {0x13cc8, [TERM], SA_RESTART}, {SIG_DFL}, 0x7005f010, 4294967295) = 0
   rt_sigaction(SIGQUIT, {0x13cc8, [QUIT], SA_RESTART}, {SIG_DFL}, 0x7005f010, 4294967295) = 0
   rt_sigaction(SIGHUP, {0x13cc8, [HUP], SA_RESTART}, {SIG_DFL}, 0x7005f010, 4294967295) = 0
   
<repeats endlessly>
   gettimeofday({1114011423, 424851}, {4294967176, 0}) = 0
   select(9, [6 7 8], [], [], {1, 993070}) = 1 (in [8], left {1, 994000})
!! read(8, 0xeffff6a4, 4)                  = -1 EINVAL (Invalid argument)
   time([1114011423])                      = 1114011423
   write(2, "rtc_linux.c:925:(read_from_devic"..., 48rtc_linux.c:925:(read_from_device)[20-15:37:03] ) = 48
>  write(2, "Could not read flags /dev/rtc : "..., 49Could not read flags /dev/rtc : Invalid argument
   ) = 49
!! ioctl(8, PRESTO_SETPID or RTC_UIE_OFF, 0) = -1 EINVAL (Invalid argument)
   time([1114011423])                      = 1114011423
   write(2, "rtc_linux.c:740:(switch_interrup"..., 49rtc_linux.c:740:(switch_interrupts)[20-15:37:03] ) = 49
>  write(2, "Could not stop measurement : Inv"..., 46Could not stop measurement : Invalid argument
   ) = 46
   gettimeofday({1114011423, 434947}, {4294967176, 0}) = 0
</repeats endlessly>
/*
  $Header: /cvs/src/chrony/rtc_linux.c,v 1.32 2003/09/22 21:22:30 richard Exp $

  =======================================================================

  chronyd/chronyc - Programs for keeping computer clocks accurate.

 **********************************************************************
 * Copyright (C) Richard P. Curnow  1997-2003
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 * 
 **********************************************************************

  =======================================================================

  Real-time clock driver for linux.  This interfaces the program with
  the clock that keeps time when the machine is turned off.

  */

#if defined LINUX

#ifdef sparc
#define __KERNEL__
#endif

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>

/* This is a complete hack since the alpha sys/io.h needs these types
 * but does not arrange them to be defined.  This is almost certainly
 * not how one should do these things.  -- broonie
 */

#ifdef __alpha__
#include <asm/types.h> /* Alpha seems to need this now */
typedef __u8  u8;
typedef __u16 u16;
typedef __u32 u32;
typedef __u64 u64;
#endif

/* From linux/rtc.c */

/*
 * ioctl calls that are permitted to the /dev/rtc interface, if
 * any of the RTC drivers are enabled.
 */

#define RTC_AIE_ON      _IO('p', 0x01)  /* Alarm int. enable on         */
#define RTC_AIE_OFF     _IO('p', 0x02)  /* ... off                      */
#define RTC_UIE_ON      _IO('p', 0x03)  /* Update int. enable on        */
#define RTC_UIE_OFF     _IO('p', 0x04)  /* ... off                      */
#define RTC_PIE_ON      _IO('p', 0x05)  /* Periodic int. enable on      */
#define RTC_PIE_OFF     _IO('p', 0x06)  /* ... off                      */
#define RTC_WIE_ON      _IO('p', 0x0f)  /* Watchdog int. enable on      */
#define RTC_WIE_OFF     _IO('p', 0x10)  /* ... off                      */

#define RTC_ALM_SET     _IOW('p', 0x07, struct rtc_time) /* Set alarm time  */
#define RTC_ALM_READ    _IOR('p', 0x08, struct rtc_time) /* Read alarm time */
#define RTC_RD_TIME     _IOR('p', 0x09, struct rtc_time) /* Read RTC time   */
#define RTC_SET_TIME    _IOW('p', 0x0a, struct rtc_time) /* Set RTC time    */
#define RTC_IRQP_READ   _IOR('p', 0x0b, unsigned long)   /* Read IRQ rate   */
#define RTC_IRQP_SET    _IOW('p', 0x0c, unsigned long)   /* Set IRQ rate    */
#define RTC_EPOCH_READ  _IOR('p', 0x0d, unsigned long)   /* Read epoch      */
#define RTC_EPOCH_SET   _IOW('p', 0x0e, unsigned long)   /* Set epoch       */

#define RTC_WKALM_SET   _IOW('p', 0x0f, struct rtc_wkalrm)/* Set wakeup alarm*/
#define RTC_WKALM_RD    _IOR('p', 0x10, struct rtc_wkalrm)/* Get wakeup alarm*/

#define RTC_PLL_GET     _IOR('p', 0x11, struct rtc_pll_info)  /* Get PLL correction */
#define RTC_PLL_SET     _IOW('p', 0x12, struct rtc_pll_info)  /* Set PLL correction */

/*
 * The struct used to pass data via the following ioctl. Similar to the
 * struct tm in <time.h>, but it needs to be here so that the kernel
 * source is self contained, allowing cross-compiles, etc. etc.
 */

struct rtc_time {
        int tm_sec;
        int tm_min;
        int tm_hour;
        int tm_mday;
        int tm_mon;
        int tm_year;
        int tm_wday;
        int tm_yday;
        int tm_isdst;
};




#define RTC_UIE 0x10L		/* update-finished interrupt enable */

#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>
#include <string.h>

#include "logging.h"
#include "sched.h"
#include "local.h"
#include "util.h"
#include "sys_linux.h"
#include "regress.h"
#include "rtc.h"
#include "rtc_linux.h"
#include "conf.h"
#include "memory.h"
#include "mkdirpp.h"

/* ================================================== */
/* Forward prototypes */

static void measurement_timeout(void *any);

static void read_from_device(void *any);

/* ================================================== */

typedef enum {
  OM_NORMAL,
  OM_INITIAL,
  OM_AFTERTRIM
} OperatingMode;

static OperatingMode operating_mode = OM_NORMAL;

/* ================================================== */

static int fd = -1;

#define LOWEST_MEASUREMENT_PERIOD 15
#define HIGHEST_MEASUREMENT_PERIOD 480

/* Try to avoid doing regression after _every_ sample we accumulate */
#define N_SAMPLES_PER_REGRESSION 4

static int measurement_period = LOWEST_MEASUREMENT_PERIOD;

static int timeout_running = 0;
static SCH_TimeoutID timeout_id;

/* ================================================== */

/* Maximum number of samples held */
#define MAX_SAMPLES 64

/* Real time clock samples.  We store the seconds count as originally
   measured, together with a 'trim' that compensates these values for
   any steps made to the RTC to bring it back into line
   occasionally.  The trim is in seconds. */
static time_t rtc_sec[MAX_SAMPLES];
static double rtc_trim[MAX_SAMPLES];

/* Reference time, against which delta times on the RTC scale are measured */
static time_t rtc_ref;


/* System clock (gettimeofday) samples associated with the above
   samples. */
static struct timeval system_times[MAX_SAMPLES];

/* Number of samples currently stored. */
static int n_samples;   

/* Number of new samples since last regression */
static int n_samples_since_regression;

/* Number of runs of residuals in last regression (for logging) */
static int n_runs;

/* Coefficients */
/* Whether they are valid */
static int coefs_valid;

/* Reference time */
static time_t coef_ref_time;
/* Number of seconds by which RTC was fast of the system time at coef_ref_time */
static double coef_seconds_fast;

/* Estimated number of seconds that RTC gains relative to system time
   for each second of ITS OWN time */
static double coef_gain_rate;

/* Gain rate saved just before we step the RTC to correct it to the
   nearest second, so that we can write a useful set of coefs to the
   RTC data file once we have reacquired its offset after the step */
static double saved_coef_gain_rate;

/* Filename supplied by config file where RTC coefficients are
   stored. */
static char *coefs_file_name;

/* ================================================== */
/* Coefficients read from file at start of run. */

/* Whether we have tried to load the coefficients */
static int tried_to_load_coefs = 0;

/* Whether valid coefficients were read */
static int valid_coefs_from_file = 0;

/* Coefs read in */
static time_t file_ref_time;
static double file_ref_offset, file_rate_ppm;

/* ================================================== */

/* Flag to remember whether to assume the RTC is running on UTC */
static int rtc_on_utc = 0;

/* ================================================== */

static FILE *logfile=NULL;
static char *logfilename = NULL;
static unsigned long logwrites=0;

#define RTC_LOG "rtc.log"

/* ================================================== */

static void (*after_init_hook)(void *) = NULL;
static void *after_init_hook_arg = NULL;

/* ================================================== */

static void
discard_samples(int new_first)
{
  int n_to_save;

  if (!(new_first < n_samples)) {
    CROAK("new_first should be < n_samples");
  }
  if (!(new_first >= 0)) {
    CROAK("new_first should be non-negative");
  }

  n_to_save = n_samples - new_first;

  memmove(rtc_sec, rtc_sec + new_first, n_to_save * sizeof(time_t));
  memmove(rtc_trim, rtc_trim + new_first, n_to_save * sizeof(double));
  memmove(system_times, system_times + new_first, n_to_save * sizeof(struct timeval));

  n_samples = n_to_save;
  return;
}

/* ================================================== */

#define NEW_FIRST_WHEN_FULL 4

static void
accumulate_sample(time_t rtc, struct timeval *sys)
{

  if (n_samples == MAX_SAMPLES) {
    /* Discard oldest samples */
    discard_samples(NEW_FIRST_WHEN_FULL);
  }

  rtc_sec[n_samples] = rtc;

  /* Always use most recent sample as reference */
  rtc_ref = rtc;

  rtc_trim[n_samples] = 0.0;
  system_times[n_samples] = *sys;
  ++n_samples;
  ++n_samples_since_regression;
  return;
  
}

/* ================================================== */
/* The new_sample flag is to indicate whether to adjust the
   measurement period depending on the behaviour of the standard
   deviation. */

static void
run_regression(int new_sample,
               int *valid,
               time_t *ref,
               double *fast,
               double *slope)
{
  double rtc_rel[MAX_SAMPLES]; /* Relative times on RTC axis */
  double offsets[MAX_SAMPLES]; /* How much the RTC is fast of the system clock */
  int i, n;
  double est_intercept, est_slope;
  int best_new_start;

  if (n_samples > 0) {

    n = n_samples - 1;

    for (i=0; i<n_samples; i++) {
      rtc_rel[i] = rtc_trim[i] + (double)(rtc_sec[i] - rtc_ref);
      offsets[i] = ((double) (rtc_ref - system_times[i].tv_sec) -
                    (1.0e-6 * (double) system_times[i].tv_usec) +
                    rtc_rel[i]);

    }

    if (RGR_FindBestRobustRegression
        (rtc_rel, offsets,
         n_samples, 1.0e-9,
         &est_intercept, &est_slope,
         &n_runs,
         &best_new_start)) {

      /* Calculate and store coefficients.  We don't do any error
         bounds processing on any of these. */
      *valid = 1;
      *ref = rtc_ref;
      *fast = est_intercept;
      *slope = est_slope;

      if (best_new_start > 0) {
        discard_samples(best_new_start);
      }


    } else {
      /* Keep existing coefficients. */
    }
  } else {
    /* Keep existing coefficients. */
  }

}

/* ================================================== */

static void
slew_samples
(struct timeval *raw, struct timeval *cooked,
 double dfreq, double afreq_ppm,
 double doffset, int is_step_change,
 void *anything)
{
  int i;
  double elapsed;
  double new_freq;
  double old_freq;
  double delta_time;
  double old_seconds_fast, old_gain_rate;

  new_freq = 1.0e-6 * afreq_ppm;
  old_freq = (new_freq - dfreq) / (1.0 - dfreq);

  for (i=0; i<n_samples; i++) {
    UTI_DiffTimevalsToDouble(&elapsed, cooked, system_times + i);

    delta_time = -(elapsed * dfreq) - doffset;

    UTI_AddDoubleToTimeval(system_times + i, delta_time, system_times + i);

  }

  old_seconds_fast = coef_seconds_fast;
  old_gain_rate = coef_gain_rate;

  if (coefs_valid) {
    coef_seconds_fast += doffset;
    coef_gain_rate = 1.0 - ((1.0 + new_freq) / (1.0 + old_freq)) * (1.0 - coef_gain_rate);
  }

#if 0
  LOG(LOGS_INFO, LOGF_RtcLinux, "dfreq=%.8f doffset=%.6f new_freq=%.3f old_freq=%.3f old_fast=%.6f old_rate=%.3f new_fast=%.6f new_rate=%.3f",
      dfreq, doffset, 1.0e6*new_freq, 1.0e6*old_freq,
      old_seconds_fast, 1.0e6 * old_gain_rate,
      coef_seconds_fast, 1.0e6 * coef_gain_rate);
#endif

}

/* ================================================== */

/* Function to convert from a time_t value represenging UTC to the
   corresponding real time clock 'DMY HMS' form, taking account of
   whether the user runs his RTC on the local time zone or UTC */

static struct tm *
rtc_from_t(const time_t *t)
{
  if (rtc_on_utc) {
    return gmtime(t);
  } else {
    return localtime(t);
  }
}

/* ================================================== */

/* Inverse function to get back from RTC 'DMY HMS' form to time_t UTC
   form.  This essentially uses mktime(), but involves some awful
   complexity to cope with timezones.  The problem is that mktime's
   behaviour with regard to the daylight saving flag in the 'struct
   tm' does not seem to be reliable across all systems, unless that
   flag is set to zero. 

   tm_isdst = -1 does not seem to work with all libc's - it is treated
   as meaning there is DST, or fails completely.  (It is supposed to
   use the timezone info to work out whether summer time is active at
   the specified epoch).

   tm_isdst = 1 fails if the local timezone has no summer time defined.

   The approach taken is as follows.  Suppose the RTC is on localtime.
   We perform all mktime calls with the tm_isdst field set to zero.

   Let y be the RTC reading in 'DMY HMS' form.  Let M be the mktime
   function with tm_isdst=0 and L be the localtime function.

   We seek x such that y = L(x).  Now there will exist a value Z(t)
   such that M(L(t)) = t + Z(t) for all t, where Z(t) depends on
   whether daylight saving is active at time t.

   We want L(x) = y.  Therefore M(L(x)) = x + Z = M(y).  But
   M(L(M(y))) = M(y) + Z.  Therefore x = M(y) - Z = M(y) - (M(L(M(y)))
   - M(y)).

   The case for the RTC running on UTC is identical but without the
   potential complication that Z depends on t.
*/

static time_t
t_from_rtc(struct tm *stm) {
  struct tm temp1, temp2;
  long diff;
  time_t t1, t2;

  temp1 = *stm;
  temp1.tm_isdst = 0;
  
  t1 = mktime(&temp1);
  if (rtc_on_utc) {
    temp2 = *gmtime(&t1);
  } else {
    temp2 = *localtime(&t1);
  }
  
  temp2.tm_isdst = 0;
  t2 = mktime(&temp2);
  diff = t2 - t1;

  return t1 - diff;
}

/* ================================================== */

static void
setup_config(void)
{
  if (CNF_GetRTCOnUTC()) {
    rtc_on_utc = 1;
  } else {
    rtc_on_utc = 0;
  }
}

/* ================================================== */
/* Read the coefficients from the file where they were saved
   the last time the program was run. */

static void
read_coefs_from_file(void)
{
  FILE *in;
  char line[256];

  if (!tried_to_load_coefs) {

    valid_coefs_from_file = 0; /* only gets set true if we succeed */

    tried_to_load_coefs = 1;

    in = fopen(coefs_file_name, "r");
    if (in) {
      if (fgets(line, sizeof(line), in)) {
        if (sscanf(line, "%d%ld%lf%lf",
                   &valid_coefs_from_file,
                   &file_ref_time,
                   &file_ref_offset,
                   &file_rate_ppm) == 4) {
        } else {
          LOG(LOGS_WARN, LOGF_RtcLinux, "Could not parse coefficients line from RTC file %s",
              coefs_file_name);
        }
      } else {
        LOG(LOGS_WARN, LOGF_RtcLinux, "Could not read first line from RTC file %s",
            coefs_file_name);
      }
      fclose(in);
    } else {
      LOG(LOGS_WARN, LOGF_RtcLinux, "Could not open RTC file %s for reading",
          coefs_file_name);
    }
  }
}

/* ================================================== */
/* Write the coefficients to the file where they will be read
   the next time the program is run. */

static int
write_coefs_to_file(int valid,time_t ref_time,double offset,double rate)
{
  struct stat buf;
  char *temp_coefs_file_name;
  FILE *out;

  /* Create a temporary file with a '.tmp' extension. */

  temp_coefs_file_name = (char*) Malloc(strlen(coefs_file_name)+8);

  if(!temp_coefs_file_name) {
    return RTC_ST_BADFILE;
  }

  strcpy(temp_coefs_file_name,coefs_file_name);
  strcat(temp_coefs_file_name,".tmp");

  out = fopen(temp_coefs_file_name, "w");
  if (!out) {
    Free(temp_coefs_file_name);
    LOG(LOGS_WARN, LOGF_RtcLinux, "Could not open temporary RTC file %s.tmp for writing",
        coefs_file_name);
    return RTC_ST_BADFILE;
  }

  /* Gain rate is written out in ppm */
  fprintf(out, "%1d %ld %.6f %.3f\n",
          valid,ref_time, offset, 1.0e6 * rate);

  fclose(out);

  /* Clone the file attributes from the existing file if there is one. */

  if (!stat(coefs_file_name,&buf)) {
    chown(temp_coefs_file_name,buf.st_uid,buf.st_gid);
    chmod(temp_coefs_file_name,buf.st_mode&0777);
  }

  /* Rename the temporary file to the correct location (see rename(2) for details). */

  if (rename(temp_coefs_file_name,coefs_file_name)) {
    unlink(temp_coefs_file_name);
    Free(temp_coefs_file_name);
    LOG(LOGS_WARN, LOGF_RtcLinux, "Could not replace old RTC file %s.tmp with new one %s",
        coefs_file_name, coefs_file_name);
    return RTC_ST_BADFILE;
  }

  Free(temp_coefs_file_name);

  return RTC_ST_OK;
}


/* ================================================== */
/* file_name is the name of the file where we save the RTC params
   between executions.  Return status is whether we could initialise
   on this version of the system. */

int
RTC_Linux_Initialise(void)
{
  int major, minor, patch;
  char *direc;


  /* Check whether we can support the real time clock.

     Linux 1.2.x - haven't checked yet

     Linux 1.3.x - don't know, haven't got a system to look at

     Linux 2.0.x - For x<=31, using any variant of the adjtimex() call
     sets the kernel into a mode where the RTC was updated every 11
     minutes.  The only way to escape this is to use settimeofday().
     Since we need to have sole control over the RTC to be able to
     measure its drift rate, and there is no 'notify' callback to warn
     you that the kernel is going to do this, I can't see a way to
     support this.

     Linux 2.0.x - For x>=32 the adjtimex()/RTC behaviour was
     modified, so that as long as the STA_UNSYNC flag is set the RTC
     is left alone.  This is the mode we exploit here, so that the RTC
     continues to go its own sweet way, unless we make updates to it
     from this module.

     Linux 2.1.x - don't know, haven't got a system to look at.

     Linux 2.2.x, 2.3.x and 2.4.x are believed to be OK for all
     patch levels

     */

  SYS_Linux_GetKernelVersion(&major, &minor, &patch);

  /* Obviously this test can get more elaborate when we know about
     more system types. */
  if (major != 2) {
    return 0;
  } else {
    switch (minor) {
      case 0:
        if (patch <= 31) {
          return 0;
        }
        break;
      case 1:
        return 0;
        break;
      case 2:
      case 3:
      case 4:
      case 5:
      case 6:
      case 7:
      case 8:
        break; /* OK for all patch levels */
    } 
  }

  /* Setup details depending on configuration options */
  setup_config();

  /* In case it didn't get done by pre-init */
  coefs_file_name = CNF_GetRtcFile();

  /* Try to open device */

  fd = open (CNF_GetRtcDevice(), O_RDWR);
  if (fd < 0) {
    LOG(LOGS_ERR, LOGF_RtcLinux, "Could not open %s, %s", CNF_GetRtcDevice(), strerror(errno));
    return 0;
  }
  n_samples = 0;
  n_samples_since_regression = 0;
  n_runs = 0;
  coefs_valid = 0;

  measurement_period = LOWEST_MEASUREMENT_PERIOD;

  operating_mode = OM_NORMAL;

  /* Register file handler */
  SCH_AddInputFileHandler(fd, read_from_device, NULL);

  /* Register slew handler */
  LCL_AddParameterChangeHandler(slew_samples, NULL);

  if (CNF_GetLogRtc()) {
    direc = CNF_GetLogDir();
    if (!mkdir_and_parents(direc)) {
      LOG(LOGS_ERR, LOGF_RtcLinux, "Could not create directory %s", direc);
      logfile = NULL;
    } else {
      logfilename = MallocArray(char, 2 + strlen(direc) + strlen(RTC_LOG));
      strcpy(logfilename, direc);
      strcat(logfilename, "/");
      strcat(logfilename, RTC_LOG);
      logfile = fopen(logfilename, "a");
      if (!logfile) {
        LOG(LOGS_WARN, LOGF_RtcLinux, "Couldn't open logfile %s for update", logfilename);
      }
    }
  }

  return 1;
}

/* ================================================== */

void
RTC_Linux_Finalise(void)
{
  if (timeout_running) {
    SCH_RemoveTimeout(timeout_id);
    timeout_running = 0;
  }

  /* Remove input file handler */
  if (fd >= 0) {
    SCH_RemoveInputFileHandler(fd);
    close(fd);

    /* Save the RTC data */
    (void) RTC_Linux_WriteParameters();

  }

  if (logfile) {
    fclose(logfile);
  }

}

/* ================================================== */

static void
switch_interrupts(int onoff)
{
  int status;

  if (onoff) {
    status = ioctl(fd, RTC_UIE_ON, 0);
    status = ioctl(fd, RTC_UIE_ON, 0);
    if (status < 0) {
      LOG(LOGS_ERR, LOGF_RtcLinux, "Could not start measurement : %s", strerror(errno));
      return;
    }
  } else {
    status = ioctl(fd, RTC_UIE_OFF, 0);
    if (status < 0) {
      LOG(LOGS_ERR, LOGF_RtcLinux, "Could not stop measurement : %s", strerror(errno));
      return;
    }
  }
}    

/* ================================================== */

static void
measurement_timeout(void *any)
{
  timeout_running = 0;
  switch_interrupts(1);
}

/* ================================================== */

static void
set_rtc(time_t new_rtc_time)
{
  struct tm rtc_tm;
  struct rtc_time rtc_raw;
  int status;

  rtc_tm = *rtc_from_t(&new_rtc_time);

  rtc_raw.tm_sec = rtc_tm.tm_sec;
  rtc_raw.tm_min = rtc_tm.tm_min;
  rtc_raw.tm_hour = rtc_tm.tm_hour;
  rtc_raw.tm_mday = rtc_tm.tm_mday;
  rtc_raw.tm_mon = rtc_tm.tm_mon;
  rtc_raw.tm_year = rtc_tm.tm_year;

  status = ioctl(fd, RTC_SET_TIME, &rtc_raw);
  if (status < 0) {
    LOG(LOGS_ERR, LOGF_RtcLinux, "Could not set RTC time");
  }

}

/* ================================================== */

static void
handle_initial_trim(void)
{
  double rate;
  long delta_time;
  double rtc_error_now, sys_error_now;

    /* The idea is to accumulate some number of samples at 1 second
       intervals, then do a robust regression fit to this.  This
       should give a good fix on the intercept (=system clock error
       rel to RTC) at a particular time, removing risk of any
       particular sample being an outlier.  We can then look at the
       elapsed interval since the epoch recorded in the RTC file,
       and correct the system time accordingly. */
    
  run_regression(1, &coefs_valid, &coef_ref_time, &coef_seconds_fast, &coef_gain_rate);

  n_samples_since_regression = 0;
  n_samples = 0;

  read_coefs_from_file();

  if (valid_coefs_from_file) {
    /* Can process data */
    delta_time = coef_ref_time - file_ref_time;
    rate = 1.0e-6 * file_rate_ppm;
    rtc_error_now = file_ref_offset + rate * (double) delta_time;
          
    /* sys_error_now is positive if the system clock is fast */
    sys_error_now = rtc_error_now - coef_seconds_fast;
          
    LOG(LOGS_INFO, LOGF_RtcLinux, "System trim from RTC = %f", sys_error_now);
    LCL_AccumulateOffset(sys_error_now);
  } else {
    LOG(LOGS_WARN, LOGF_RtcLinux, "No valid file coefficients, cannot trim system time");
  }
  
  coefs_valid = 0;
  
  (after_init_hook)(after_init_hook_arg);
  
  operating_mode = OM_NORMAL;

  return;
}

/* ================================================== */

static void
handle_relock_after_trim(void)
{
  int valid;
  time_t ref;
  double fast, slope;

  run_regression(1, &valid, &ref, &fast, &slope);

  if (valid) {
    write_coefs_to_file(1,ref,fast,saved_coef_gain_rate);
  } else {
    LOG(LOGS_WARN, LOGF_RtcLinux, "Could not do regression after trim");
  }

  n_samples = 0;
  n_samples_since_regression = 0;
  operating_mode = OM_NORMAL;
  measurement_period = LOWEST_MEASUREMENT_PERIOD;
}

/* ================================================== */

/* Day number of 1 Jan 1970 */
#define MJD_1970 40587

static void
process_reading(time_t rtc_time, struct timeval *system_time)
{
  double rtc_fast;

  accumulate_sample(rtc_time, system_time);

  switch (operating_mode) {
    case OM_NORMAL:

      if (n_samples_since_regression >= /* 4 */ 1 ) {
        run_regression(1, &coefs_valid, &coef_ref_time, &coef_seconds_fast, &coef_gain_rate);
        n_samples_since_regression = 0;
      }
      
      break;
    case OM_INITIAL:
      if (n_samples_since_regression >= 8) {
        handle_initial_trim();
      }
      break;
    case OM_AFTERTRIM:
      if (n_samples_since_regression >= 8) {
        handle_relock_after_trim();
      }
      break;
    default:
      CROAK("Impossible");
      break;
  }  


  if (logfile) {
    rtc_fast = (double)(rtc_time - system_time->tv_sec) - 1.0e-6 * (double) system_time->tv_usec;

    if (((logwrites++) % 32) == 0) {
      fprintf(logfile,
              "===============================================================================\n"
              "   Date (UTC) Time   RTC fast (s) Val   Est fast (s)   Slope (ppm)  Ns  Nr Meas\n"
              "===============================================================================\n");
    }
    
    fprintf(logfile, "%s %14.6f %1d  %14.6f  %12.3f  %2d  %2d %4d\n",
            UTI_TimeToLogForm(system_time->tv_sec),
            rtc_fast,
            coefs_valid,
            coef_seconds_fast, coef_gain_rate * 1.0e6, n_samples, n_runs, measurement_period);

    fflush(logfile);
  }    

}

/* ================================================== */

static void
read_from_device(void *any)
{
  int status;
  unsigned long data;
  struct timeval sys_time;
  struct rtc_time rtc_raw;
  struct tm rtc_tm;
  time_t rtc_t;
  double read_err;
  int error = 0;

  status = read(fd, &data, sizeof(data));
  if (status < 0) {
    LOG(LOGS_ERR, LOGF_RtcLinux, "Could not read flags %s : %s", CNF_GetRtcDevice(), strerror(errno));
    error = 1;
    goto turn_off_interrupt;
  }

  if ((data & RTC_UIE) == RTC_UIE) {
    /* Update interrupt detected */
    
    /* Read RTC time, sandwiched between two polls of the system clock
       so we can bound any error. */

    LCL_ReadCookedTime(&sys_time, &read_err);

    status = ioctl(fd, RTC_RD_TIME, &rtc_raw);
    if (status < 0) {
      LOG(LOGS_ERR, LOGF_RtcLinux, "Could not read time from %s : %s", CNF_GetRtcDevice(), strerror(errno));
      error = 1;
      goto turn_off_interrupt;
    }


    /* Convert RTC time into a struct timeval */
    rtc_tm.tm_sec = rtc_raw.tm_sec;
    rtc_tm.tm_min = rtc_raw.tm_min;
    rtc_tm.tm_hour = rtc_raw.tm_hour;
    rtc_tm.tm_mday = rtc_raw.tm_mday;
    rtc_tm.tm_mon = rtc_raw.tm_mon;
    rtc_tm.tm_year = rtc_raw.tm_year;

    rtc_t = t_from_rtc(&rtc_tm);

    if (rtc_t == (time_t)(-1)) {
      LOG(LOGS_ERR, LOGF_RtcLinux, "Could not convert RTC time to timeval");
      error = 1;
      goto turn_off_interrupt;
    }      

    process_reading(rtc_t, &sys_time);

    if (n_samples < 4) {
      measurement_period = LOWEST_MEASUREMENT_PERIOD;
    } else if (n_samples < 6) {
      measurement_period = LOWEST_MEASUREMENT_PERIOD << 1;
    } else if (n_samples < 10) {
      measurement_period = LOWEST_MEASUREMENT_PERIOD << 2;
    } else if (n_samples < 14) {
      measurement_period = LOWEST_MEASUREMENT_PERIOD << 3;
    } else {
      measurement_period = LOWEST_MEASUREMENT_PERIOD << 4;
    }

  }

turn_off_interrupt:

  switch (operating_mode) {
    case OM_INITIAL:
      if (error) {
        LOG(LOGS_WARN, LOGF_RtcLinux, "Could not complete initial step due to errors");
        operating_mode = OM_NORMAL;
        (after_init_hook)(after_init_hook_arg);

        switch_interrupts(0);
    
        timeout_running = 1;
        timeout_id = SCH_AddTimeoutByDelay((double) measurement_period, measurement_timeout, NULL);
      }

      break;

    case OM_AFTERTRIM:
      if (error) {
        LOG(LOGS_WARN, LOGF_RtcLinux, "Could not complete after trim relock due to errors");
        operating_mode = OM_NORMAL;

        switch_interrupts(0);
    
        timeout_running = 1;
        timeout_id = SCH_AddTimeoutByDelay((double) measurement_period, measurement_timeout, NULL);
      }
      
      break;

    case OM_NORMAL:
      switch_interrupts(0);
    
      timeout_running = 1;
      timeout_id = SCH_AddTimeoutByDelay((double) measurement_period, measurement_timeout, NULL);

      break;
    default:
      CROAK("Impossible");
      break;
  }

}

/* ================================================== */

void
RTC_Linux_TimeInit(void (*after_hook)(void *), void *anything)
{
  after_init_hook = after_hook;
  after_init_hook_arg = anything;

  operating_mode = OM_INITIAL;
  timeout_running = 0;
  switch_interrupts(1);

}

/* ================================================== */

void
RTC_Linux_StartMeasurements(void)
{
  timeout_running = 0;
  measurement_timeout(NULL);
}

/* ================================================== */

int
RTC_Linux_WriteParameters(void)
{
  int retval;

  if (fd < 0) {
    return RTC_ST_NODRV;
  }
  
  if (coefs_valid) {
    retval = write_coefs_to_file(1,coef_ref_time, coef_seconds_fast, coef_gain_rate);
  } else {
   /* Don't change the existing file, it may not be 100% valid but is our
      current best guess. */
    retval = RTC_ST_OK; /*write_coefs_to_file(0,0,0.0,0.0); */
  }

  return(retval);
}

/* ================================================== */
/* Try to set the system clock from the RTC, in the same manner as
   /sbin/clock -s -u would do.  We're not as picky about OS version
   etc in this case, since we have fewer requirements regarding the
   RTC behaviour than we do for the rest of the module. */

void
RTC_Linux_TimePreInit(void)
{
  int fd, status;
  struct rtc_time rtc_raw;
  struct tm rtc_tm;
  time_t rtc_t, estimated_correct_rtc_t;
  long interval;
  double accumulated_error = 0.0;
  struct timeval new_sys_time;

  coefs_file_name = CNF_GetRtcFile();

  setup_config();
  read_coefs_from_file();
  fd = open(CNF_GetRtcDevice(), O_RDONLY);

  if (fd < 0) {
    return; /* Can't open it, and won't be able to later */
  }

  status = ioctl(fd, RTC_RD_TIME, &rtc_raw);

  if (status >= 0) {
    /* Convert to seconds since 1970 */
    rtc_tm.tm_sec = rtc_raw.tm_sec;
    rtc_tm.tm_min = rtc_raw.tm_min;
    rtc_tm.tm_hour = rtc_raw.tm_hour;
    rtc_tm.tm_mday = rtc_raw.tm_mday;
    rtc_tm.tm_mon = rtc_raw.tm_mon;
    rtc_tm.tm_year = rtc_raw.tm_year;
    
    rtc_t = t_from_rtc(&rtc_tm);

    if (rtc_t != (time_t)(-1)) {

      /* Work out approximatation to correct time (to about the
         nearest second) */
      if (valid_coefs_from_file) {
        interval = rtc_t - file_ref_time;
        accumulated_error = file_ref_offset + (double)(interval) * 1.0e-6 * file_rate_ppm;

        /* Correct time */
        LOG(LOGS_INFO, LOGF_RtcLinux, "Set system time, error in RTC = %f",
            accumulated_error);
        estimated_correct_rtc_t = rtc_t - (long)(0.5 + accumulated_error);
      } else {
        estimated_correct_rtc_t = rtc_t - (long)(0.5 + accumulated_error);
      }

      new_sys_time.tv_sec = estimated_correct_rtc_t;
      new_sys_time.tv_usec = 0;

      /* Tough luck if this fails */
      if (settimeofday(&new_sys_time, NULL) < 0) {
        LOG(LOGS_WARN, LOGF_RtcLinux, "Could not settimeofday");
      }
    } else {
      LOG(LOGS_WARN, LOGF_RtcLinux, "Could not convert RTC reading to seconds since 1/1/1970");
    }
  }

  close(fd);
}

/* ================================================== */

int
RTC_Linux_GetReport(RPT_RTC_Report *report)
{
  report->ref_time = (unsigned long) coef_ref_time;
  report->n_samples = n_samples;
  report->n_runs = n_runs;
  if (n_samples > 1) {
    report->span_seconds = ((rtc_sec[n_samples-1] - rtc_sec[0]) +
                            (long)(rtc_trim[n_samples-1] - rtc_trim[0]));
  } else {
    report->span_seconds = 0;
  }
  report->rtc_seconds_fast = coef_seconds_fast;
  report->rtc_gain_rate_ppm = 1.0e6 * coef_gain_rate;
  return 1;
}

/* ================================================== */

int
RTC_Linux_Trim(void)
{
  struct timeval now;
  double local_clock_err;


  /* Remember the slope coefficient - we won't be able to determine a
     good one in a few seconds when we determine the new offset! */
  saved_coef_gain_rate = coef_gain_rate;

  if (fabs(coef_seconds_fast) > 1.0) {

    LOG(LOGS_INFO, LOGF_RtcLinux, "Trimming RTC, error = %.3f seconds", coef_seconds_fast);

    /* Do processing to set clock.  Let R be the value we set the
       RTC to, then in 500ms the RTC ticks (R+1) (see comments in
       arch/i386/kernel/time.c about the behaviour of the real time
       clock chip).  If S is the system time now, the error at the
       next RTC tick is given by E = (R+1) - (S+0.5).  Ideally we
       want |E| <= 0.5, which implies R <= S <= R+1, i.e. R is just
       the rounded down part of S, i.e. the seconds part. */

    LCL_ReadCookedTime(&now, &local_clock_err);
    
    set_rtc(now.tv_sec);

    /* All old samples will now look bogus under the new
           regime. */
    n_samples = 0;
    operating_mode = OM_AFTERTRIM;

    /* And start rapid sampling, interrupts on now */
    if (timeout_running) {
      SCH_RemoveTimeout(timeout_id);
      timeout_running = 0;
    }
    switch_interrupts(1);
  }

  return 1;
  
}

/* ================================================== */

void
RTC_Linux_CycleLogFile(void)
{
  if (logfile && logfilename) {
    fclose(logfile);
    logfile = fopen(logfilename, "a");
    if (!logfile) {
      LOG(LOGS_WARN, LOGF_RtcLinux, "Could not reopen logfile %s", logfilename);
    }
    logwrites = 0;
  }
}

/* ================================================== */

#endif /* defined LINUX */
/*
  $Header: /cvs/src/chrony/rtc_linux.h,v 1.13 2002/02/28 23:27:13 richard Exp $

  ======================================================================

  chronyd/chronyc - Programs for keeping computer clocks accurate.

 **********************************************************************
 * Copyright (C) Richard P. Curnow  1997-2002
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 * 
 **********************************************************************

  ======================================================================

  */

#ifndef _GOT_RTC_LINUX_H
#define _GOT_RTC_LINUX_H

#include "reports.h"

#if defined LINUX

extern int RTC_Linux_Initialise(void);
extern void RTC_Linux_Finalise(void);
extern void RTC_Linux_TimePreInit(void);
extern void RTC_Linux_TimeInit(void (*after_hook)(void *), void *anything);
extern void RTC_Linux_StartMeasurements(void);

/* 0=success, 1=no driver, 2=can't write file */
extern int RTC_Linux_WriteParameters(void);

extern int RTC_Linux_GetReport(RPT_RTC_Report *report);
extern int RTC_Linux_Trim(void);

extern void RTC_Linux_CycleLogFile(void);

#endif /* defined LINUX */

#endif /* _GOT_RTC_LINUX_H */

Attachment: pgpjTHnQoMsi7.pgp
Description: PGP signature


Reply to: