1#!/bin/sh
2# Note that there is no provision to prevent several copies of the script
3# to be run in quick succession. In fact, it happens rather often
4# if initial syncronization results in a step.
5# You will see "step" and then "stratum" script runs, sometimes
6# as close as only 0.002 seconds apart.
7#
8# Script should be ready to deal with this.
9
10# For other parts of the system which prefer to run only on the stable clock
11echo "$1" >rundir/sync_status
12
13dt=`date '+%Y-%m-%d %H:%M:%S'`
14
15echo "`tail -n 199 -- "$0.log" 2>/dev/null`" >"$0.log.$$"
16
17if test x"$1" = x"unsync" \
18; then
19	# No replies for our NTP requests were seen for some time.
20	#
21	# Among more mundate cases like network outages, this happens
22	# if we ran for a LONG time (days) and ntp server's IP has changed.
23	# ntpd has no code to re-resolve peers' addresses to IPs,
24	# we need to help it:
25	#
26	echo "$dt: $1"\
27		"syncronization lost, restarting ntpd"\
28		>>"$0.log.$$"
29	mv -- "$0.log.$$" "$0.log"
30	kill $PPID
31	exit
32fi
33
34if test x"$stratum" != x"" \
35&& test x"$poll_interval" != x"" \
36&& test 4 -ge "$stratum" \
37&& test 128 -le "$poll_interval" \
38; then
39	echo "$dt: $1"\
40		"freq_drift_ppm=$freq_drift_ppm"\
41		"offset=$offset"\
42		"stratum=$stratum"\
43		"poll_interval=$poll_interval,"\
44		"setting hardware clock"\
45		>>"$0.log.$$"
46	mv -- "$0.log.$$" "$0.log"
47	exec hwclock --systohc
48fi
49
50echo "$dt: $1"\
51	"freq_drift_ppm=$freq_drift_ppm"\
52	"offset=$offset"\
53	"stratum=$stratum"\
54	"poll_interval=$poll_interval"\
55	>>"$0.log.$$"
56mv -- "$0.log.$$" "$0.log"
57