ntpd revision 304879
1#!/bin/sh
2#
3# $FreeBSD: stable/10/etc/rc.d/ntpd 304879 2016-08-27 02:53:21Z cy $
4#
5
6# PROVIDE: ntpd
7# REQUIRE: DAEMON ntpdate FILESYSTEMS devfs
8# BEFORE:  LOGIN
9# KEYWORD: nojail shutdown
10
11. /etc/rc.subr
12
13name="ntpd"
14rcvar="ntpd_enable"
15command="/usr/sbin/${name}"
16pidfile="/var/run/${name}.pid"
17extra_commands="fetch"
18fetch_cmd="ntpd_fetch_leapfile"
19start_precmd="ntpd_precmd"
20
21load_rc_config $name
22
23ntpd_precmd()
24{
25	rc_flags="-c ${ntpd_config} ${ntpd_flags}"
26
27	if checkyesno ntpd_sync_on_start; then
28		rc_flags="-g $rc_flags"
29	fi
30
31	ntpd_init_leapfile
32
33	if [ ! -f $ntp_db_leapfile ]; then
34		ntpd_fetch_leapfile
35	fi
36
37	if [ -z "$ntpd_chrootdir" ]; then
38		return 0;
39	fi
40
41	# If running in a chroot cage, ensure that the appropriate files
42	# exist inside the cage, as well as helper symlinks into the cage
43	# from outside.
44	#
45	# As this is called after the is_running and required_dir checks
46	# are made in run_rc_command(), we can safely assume ${ntpd_chrootdir}
47	# exists and ntpd isn't running at this point (unless forcestart
48	# is used).
49	#
50	if [ ! -c "${ntpd_chrootdir}/dev/clockctl" ]; then
51		rm -f "${ntpd_chrootdir}/dev/clockctl"
52		( cd /dev ; /bin/pax -rw -pe clockctl "${ntpd_chrootdir}/dev" )
53	fi
54	ln -fs "${ntpd_chrootdir}/var/db/ntp.drift" /var/db/ntp.drift
55	ln -fs "${ntpd_chrootdir}${ntp_tmp_leapfile}" ${ntp_tmp_leapfile}
56
57	#	Change run_rc_commands()'s internal copy of $ntpd_flags
58	#
59	rc_flags="-u ntpd:ntpd -i ${ntpd_chrootdir} $rc_flags"
60}
61
62current_ntp_ts() {
63	# Seconds between 1900-01-01 and 1970-01-01
64	# echo $(((70*365+17)*86400))
65	ntp_to_unix=2208988800
66
67	echo $(($(date -u +%s)+$ntp_to_unix))
68}
69	
70get_ntp_leapfile_ver() {
71	# Leapfile update date (version number).
72	expr "$(awk '$1 == "#$" { print $2 }' "$1" 2>/dev/null)" : \
73		'^\([1-9][0-9]*\)$' \| 0
74}
75
76get_ntp_leapfile_expiry() {
77	# Leapfile expiry date.
78	expr "$(awk '$1 == "#@" { print $2 }' "$1" 2>/dev/null)" : \
79		'^\([1-9][0-9]*\)$' \| 0
80}
81
82ntpd_init_leapfile() {
83	# Refresh working leapfile with an invalid hash due to
84	# FreeBSD id header. Ntpd will ignore leapfiles with a
85	# mismatch hash. The file must be the virgin file from
86	# the source.
87	if [ ! -f $ntp_db_leapfile ]; then
88		cp -p $ntp_src_leapfile $ntp_db_leapfile
89	fi
90}
91
92ntpd_fetch_leapfile() {
93	local ntp_tmp_leapfile rc verbose
94	
95	if checkyesno ntp_leapfile_fetch_verbose; then
96		verbose=echo
97	else
98		verbose=:
99	fi
100
101	ntp_tmp_leapfile="/var/run/ntpd.leap-seconds.list"
102
103	ntp_ver_no_src=$(get_ntp_leapfile_ver $ntp_src_leapfile)
104	ntp_expiry_src=$(get_ntp_leapfile_expiry $ntp_src_leapfile)
105	ntp_ver_no_db=$(get_ntp_leapfile_ver $ntp_db_leapfile)
106	ntp_expiry_db=$(get_ntp_leapfile_expiry $ntp_db_leapfile)
107	$verbose ntp_src_leapfile version is $ntp_ver_no_src
108	$verbose ntp_db_leapfile version is $ntp_ver_no_db
109
110	if [ "$ntp_ver_no_src" -gt "$ntp_ver_no_db" -o \
111	     "$ntp_ver_no_src" -eq "$ntp_ver_no_db" -a \
112	     "$ntp_expiry_src" -gt "$ntp_expiry_db" ]; then
113		$verbose replacing $ntp_db_leapfile with $ntp_src_leapfile 
114		cp -p $ntp_src_leapfile $ntp_db_leapfile
115		ntp_ver_no_db=$ntp_ver_no_src
116	else
117		$verbose not replacing $ntp_db_leapfile with $ntp_src_leapfile 
118	fi
119	ntp_leapfile_expiry_seconds=$((ntp_leapfile_expiry_days*86400))
120	ntp_leap_expiry=$(get_ntp_leapfile_expiry $ntp_db_leapfile)
121	ntp_leap_fetch_date=$((ntp_leap_expiry-ntp_leapfile_expiry_seconds))
122	if [ $(current_ntp_ts) -ge $ntp_leap_fetch_date ]; then
123		$verbose Within ntp leapfile expiry limit, initiating fetch
124		for url in $ntp_leapfile_sources ; do
125			$verbose fetching $url
126			fetch $ntp_leapfile_fetch_opts -o $ntp_tmp_leapfile $url && break
127		done
128		ntp_ver_no_tmp=$(get_ntp_leapfile_ver $ntp_tmp_leapfile)
129		ntp_expiry_tmp=$(get_ntp_leapfile_expiry $ntp_tmp_leapfile)
130		if [ "$ntp_ver_no_tmp" -gt "$ntp_ver_no_db" -o \
131		     "$ntp_ver_no_tmp" -eq "$ntp_ver_no_db" -a \
132		     "$ntp_expiry_tmp" -gt "$ntp_expiry_db" ]; then
133			$verbose using $url as $ntp_db_leapfile
134			mv $ntp_tmp_leapfile $ntp_db_leapfile
135		else
136			$verbose using existing $ntp_db_leapfile
137		fi
138	fi
139}
140
141run_rc_command "$1"
142