Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / ntp / dist / ntpd / refclock_local.c
blob3be5274b5b2af22c236a557073f3efb18f51f1d0
1 /* $NetBSD$ */
4 /*
5 * refclock_local - local pseudo-clock driver
7 * wjm 17-aug-1995: add a hook for special treatment of VMS_LOCALUNIT
8 */
9 #ifdef HAVE_CONFIG_H
10 #include <config.h>
11 #endif
13 #ifdef REFCLOCK
15 #include "ntpd.h"
16 #include "ntp_refclock.h"
17 #include "ntp_stdlib.h"
19 #include <stdio.h>
20 #include <ctype.h>
22 #ifdef KERNEL_PLL
23 #include "ntp_syscall.h"
24 #endif
27 * This is a hack to allow a machine to use its own system clock as a
28 * reference clock, i.e., to free-run using no outside clock discipline
29 * source. Note that the clock selection algorithm will not select this
30 * driver unless all other sources of synchronization have been lost.
31 * This is useful if you want to use NTP in an isolated environment
32 * with no radio clock or NIST modem available. Pick a machine that you
33 * figure has a good clock oscillator and configure it with this
34 * driver. Set the clock using the best means available, like
35 * eyeball-and-wristwatch. Then, point all the other machines at this
36 * one or use broadcast (not multicast) mode to distribute time.
38 * Another application for this driver is if you want to use a
39 * particular server's clock as the clock of last resort when all other
40 * normal synchronization sources have gone away. This is especially
41 * useful if that server has an ovenized oscillator. However, the
42 * preferred was to do this is using orphan mode. See the documentation.
44 * A third application for this driver is when an external discipline
45 * source is available, such as the NIST "lockclock" program, which
46 * synchronizes the local clock via a telephone modem and the NIST
47 * Automated Computer Time Service (ACTS), or the Digital Time
48 * Synchronization Service (DTSS), which runs on DCE machines. In this
49 * case the stratum should be set at zero, indicating a bona fide
50 * stratum-1 source. Exercise some caution with this, since there is no
51 * easy way to telegraph via NTP that something might be wrong in the
52 * discipline source itself. In the case of DTSS, the local clock can
53 * have a rather large jitter, depending on the interval between
54 * corrections and the intrinsic frequency error of the clock
55 * oscillator. In extreme cases, this can cause clients to exceed the
56 * 128-ms slew window and drop off the NTP subnet.
58 * Fudge Factors
60 * If fudge flag1 is lit, the leap second bit is set in the peer
61 * status word. It should be set early in the day of a leap second
62 * event and set dark on the day after the event.
64 * Note the fudge time1 and time2 have been deprecated. The fudge time1
65 * was intended to apply a bias offset. This can be done using the Unix
66 * date command. The fudge time2 was intended to apply a bias frequency.
67 * This can be done using the frequency file and/or the freq
68 * configuration command.
71 * Local interface definitions
73 #define PRECISION (-7) /* about 10 ms precision */
74 #define DESCRIPTION "Undisciplined local clock" /* WRU */
75 #define STRATUM 5 /* default stratum */
76 #define DISPERSION .01 /* default dispersion (10 ms) */
79 * Imported from the timer module
81 extern u_long current_time;
84 * Imported from ntp_proto
86 extern s_char sys_precision;
89 * Function prototypes
91 static int local_start (int, struct peer *);
92 static void local_poll (int, struct peer *);
95 * Local variables
97 static u_long poll_time; /* last time polled */
100 * Transfer vector
102 struct refclock refclock_local = {
103 local_start, /* start up driver */
104 noentry, /* shut down driver (not used) */
105 local_poll, /* transmit poll message */
106 noentry, /* not used (old lcl_control) */
107 noentry, /* initialize driver (not used) */
108 noentry, /* not used (old lcl_buginfo) */
109 NOFLAGS /* not used */
114 * local_start - start up the clock
116 static int
117 local_start(
118 int unit,
119 struct peer *peer
122 struct refclockproc *pp;
124 pp = peer->procptr;
127 * Initialize miscellaneous variables
129 peer->precision = sys_precision;
130 pp->leap = LEAP_NOTINSYNC;
131 peer->stratum = STRATUM;
132 pp->stratum = STRATUM;
133 pp->clockdesc = DESCRIPTION;
134 memcpy(&pp->refid, "LOCL", 4);
135 poll_time = current_time;
136 return (1);
141 * local_poll - called by the transmit procedure
143 * LOCKCLOCK: If the kernel supports the nanokernel or microkernel
144 * system calls, the leap bits are extracted from the kernel. If there
145 * is a kernel error or the kernel leap bits are set to 11, the NTP leap
146 * bits are set to 11 and the stratum is set to infinity. Otherwise, the
147 * NTP leap bits are set to the kernel leap bits and the stratum is set
148 * as fudged. This behavior does not faithfully follow the
149 * specification, but is probably more appropriate in a multiple-server
150 * national laboratory network.
152 static void
153 local_poll(
154 int unit,
155 struct peer *peer
158 #if defined(KERNEL_PLL) && defined(LOCKCLOCK)
159 struct timex ntv;
160 #endif /* KERNEL_PLL LOCKCLOCK */
161 struct refclockproc *pp;
164 * Do no evil unless the house is dark or lit with our own lamp.
166 if (!(sys_peer == NULL || sys_peer == peer))
167 return;
169 #if defined(VMS) && defined(VMS_LOCALUNIT)
170 if (unit == VMS_LOCALUNIT) {
171 extern void vms_local_poll(struct peer *);
173 vms_local_poll(peer);
174 return;
176 #endif /* VMS && VMS_LOCALUNIT */
178 pp = peer->procptr;
179 pp->polls++;
182 * Ramble through the usual filtering and grooming code, which
183 * is essentially a no-op and included mostly for pretty
184 * billboards. We allow a one-time time adjustment using fudge
185 * time1 (s) and a continuous frequency adjustment using fudge
186 * time 2 (ppm).
188 poll_time = current_time;
189 refclock_process_offset(pp, pp->lastrec, pp->lastrec, 0);
192 * If another process is disciplining the system clock, we set
193 * the leap bits and quality indicators from the kernel.
195 #if defined(KERNEL_PLL) && defined(LOCKCLOCK)
196 memset(&ntv, 0, sizeof ntv);
197 switch (ntp_adjtime(&ntv)) {
198 case TIME_OK:
199 pp->leap = LEAP_NOWARNING;
200 peer->stratum = pp->stratum;
201 break;
203 case TIME_INS:
204 pp->leap = LEAP_ADDSECOND;
205 peer->stratum = pp->stratum;
206 break;
208 case TIME_DEL:
209 pp->leap = LEAP_DELSECOND;
210 peer->stratum = pp->stratum;
211 break;
213 default:
214 pp->leap = LEAP_NOTINSYNC;
215 peer->stratum = STRATUM_UNSPEC;
217 pp->disp = 0;
218 pp->jitter = 0;
219 #else /* KERNEL_PLL LOCKCLOCK */
220 if (pp->sloppyclockflag & CLK_FLAG1)
221 pp->leap = LEAP_ADDSECOND;
222 else
223 pp->leap = LEAP_NOWARNING;
224 pp->disp = DISPERSION;
225 pp->jitter = 0;
226 #endif /* KERNEL_PLL LOCKCLOCK */
227 pp->lastref = pp->lastrec;
228 refclock_receive(peer);
230 #else
231 int refclock_local_bs;
232 #endif /* REFCLOCK */