[time-nuts] NTP API on Linux 2.6.26

Magnus Danielson magnus at rubidium.dyndns.org
Wed Jan 7 05:15:10 UTC 2009


M. Warner Losh skrev:
> In message: <4964340D.8030909 at rubidium.dyndns.org>
>             Magnus Danielson <magnus at rubidium.dyndns.org> writes:
> : Hi!
> : 
> : As I have been investigating the ways of NTPD to fiddle with time in the 
> : LINUX kernel I discovered that /usr/include/linux/timex.h (as supplied 
> : by the kernel) is not in sync with /usr/include/sys/timex.h (as supplied 
> : by glibc 2.7). Since it is the sys/timex.h which is the interface to 
> : NTPD and anyone else (it is actually a neat little interface if 
> : correctly supported).
> : 
> : The fluke is that glibc duplicates the timex.h but has not been updated 
> : since oh... Linux 2.2.0. The linux/timex.h is up to date with the NTP 
> : API as far as I can see (have not checked the details).
> : 
> : There are some links that may be handy:
> : http://bugs.gentoo.org/attachment.cgi?id=165913&action=view
> : http://sources.redhat.com/bugzilla/show_bug.cgi?id=9690
> : http://sourceware.org/ml/libc-alpha/2008-03/msg00076.html
> : 
> : However a small test-program:
> : #include <stdio.h>
> : //#include <sys/timex.h>
> : #include "timex.h"
> : 
> : int main()
> : {
> : 	struct timex foo;
> : 	adjtimex(&foo);
> : 	printf("TAI Offset %i\n", foo.tai);
> : 	return 0;
> : }
> : 
> : (Notice my quick and dirty hack to use a hacked variant of timex.h as if 
> : the patch was being applied, also notice that the .c part does not apply 
> : to the adjtimex() call but to the ntp_gettime() call which I am not 
> : using, so I do not require that patch for this purpose.)
> : 
> : This should be the kernels feeling of the TAI-UTC difference. I do not 
> : think it reflects that:
> : magnus at heaven:~/gcc/ntptest$ ./tai
> : TAI Offset -1553771440
> : magnus at heaven:~/gcc/ntptest$ ./tai
> : TAI Offset -263060400
> : magnus at heaven:~/gcc/ntptest$ ./tai
> : TAI Offset 238212176
> : magnus at heaven:~/gcc/ntptest$ ./tai
> : TAI Offset 658158672
> : magnus at heaven:~/gcc/ntptest$ ./tai
> : TAI Offset 1551639632
> : 
> : So I guess there is more to it than that patch alone.
> : 
> : If someone could run the above test-program on some *BSD box or whatever 
> : implementing the NTP API version 4 I would be interested in seeing what 
> : the result would be. It surely isn't the definitive test on the API, but 
> : seems to detect one (of possible several) flaws.
> 
> That didn't work.
> 
> % cc -o y y.c -Wall
> y.c: In function 'main':
> y.c:9: warning: implicit declaration of function 'adjtimex'
> y.c:10: error: 'struct timex' has no member named 'tai'

Oh, I just recalled, adjtimex is the Linux variant for ntp_adjtime, this 
achieves the same thing using the same interface (but a more portable 
name as ntp_adjtime exists in Linux also):

#include <stdio.h>
//#include <sys/timex.h>
#include "timex.h"

int main()
{
	struct timex foo;
	//adjtimex(&foo);
	ntp_adjtime(&foo);
	printf("TAI Offset %i\n", foo.tai);
	return 0;
}

and it fails misserably in the same fashion.

> But this does:
> 
> #include <stdio.h>
> #include <sys/time.h>
> #include <sys/timex.h>
> 
> int
> main(int argc, char **argv)
> {
>     int rv;
>     struct ntptimeval ntv;
>     struct timeval tv1;
>     struct timeval tv2;
>     
>     gettimeofday(&tv1, NULL);
>     rv = ntp_gettime(&ntv);
>     gettimeofday(&tv2, NULL);
>     printf("System: %ld.%06ld\nntp:    %ld.%09ld (err %ld)\nntp*:   %ld.%09ld\nsystem: %ld.%06ld\n",
>       (long)tv1.tv_sec, (long)tv1.tv_usec,
>       (long)ntv.time.tv_sec, (long)ntv.time.tv_nsec, ntv.esterror * 1000,
>       (long)ntv.time.tv_sec, (long)ntv.time.tv_nsec + ntv.esterror * 1000,
>       (long)tv2.tv_sec, (long)tv2.tv_usec);
>     printf("TAI Offset is %ld\n", ntv.tai);
> }

Patching it to use my patched timex.h and also patching out tv_nsec 
since it was also not supported (should be another bug as NANO is 
supported with this kernel) I got this:

magnus at heaven:~/gcc/ntptest$ ./taiwarner
System: 1231305074.211898
ntp:    1231305074.000000000 (err 491000)
ntp*:   1231305074.000491000
system: 1231305074.211906
TAI Offset is 0

TAI Warner is that big media company you know. :)

Either this is just a mockupfield of some sort or it just shows that my 
box does not have any propper TAI-UTS offset given, regardless of which, 
it should be fixed.

Cheers,
Magnus



More information about the time-nuts mailing list