xref: /openbsd/usr.sbin/rdate/ntpleaps.h (revision 7b9c60e5)
1 /*	$OpenBSD: ntpleaps.h,v 1.4 2007/11/25 16:40:04 jmc Exp $	*/
2 
3 /*
4  * Copyright (c) 2002 Thorsten Glaser. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  *    - Redistributions of source code must retain the above copyright
11  *      notice, this list of conditions and the following disclaimer.
12  *    - Redistributions in binary form must reproduce the above
13  *      copyright notice, this list of conditions and the following
14  *      disclaimer in the documentation and/or other materials provided
15  *      with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  */
31 
32 /* Leap second support for SNTP clients
33  * This header file and its corresponding C file provide generic
34  * ability for NTP or SNTP clients to correctly handle leap seconds
35  * by reading them from an always existing file and subtracting the
36  * leap seconds from the NTP return value before setting the posix
37  * clock. This is fairly portable between operating systems and may
38  * be used for patching other ntp clients, too. The tzfile used is:
39  * /usr/share/zoneinfo/right/UTC which is available on any unix-like
40  * platform with the Olson tz library, which is necessary to get real
41  * leap second zoneinfo files and userland support anyways.
42  */
43 
44 #ifndef _NTPLEAPS_H
45 #define _NTPLEAPS_H
46 
47 /* Offset between struct timeval.tv_sec and a tai64_t */
48 #define	NTPLEAPS_OFFSET	(4611686018427387914ULL)
49 
50 /* Hide this ugly value from programmes */
51 #define	SEC_TO_TAI64(s)	(NTPLEAPS_OFFSET + (u_int64_t)(s))
52 #define	TAI64_TO_SEC(t)	((t) - NTPLEAPS_OFFSET)
53 
54 /* Initializes the leap second table. Does not need to be called
55  * before usage of the subtract function, but calls ntpleaps_read.
56  * returns 0 on success, -1 on error (displays a warning on stderr)
57  */
58 int ntpleaps_init(void);
59 
60 /* Re-reads the leap second table, thus consuming quite much time.
61  * Ought to be called from within daemons at least once a month to
62  * ensure the in-memory table is always up-to-date.
63  * returns 0 on success, -1 on error (leap seconds will not be available)
64  */
65 int ntpleaps_read(void);
66 
67 /* Subtracts leap seconds from the given value (converts NTP time
68  * to posix clock tick time.
69  * returns 0 on success, -1 on error (time is unchanged), 1 on leap second
70  */
71 int ntpleaps_sub(u_int64_t *);
72 
73 #endif
74