178344Sobrien#!/bin/sh
278344Sobrien#
398184Sgordon# $FreeBSD: releng/10.3/etc/rc.d/ntpd 295619 2016-02-14 23:04:52Z cy $
478344Sobrien#
578344Sobrien
678344Sobrien# PROVIDE: ntpd
7240336Sobrien# REQUIRE: DAEMON ntpdate FILESYSTEMS devfs
898184Sgordon# BEFORE:  LOGIN
9180564Sdougb# KEYWORD: nojail shutdown
1078344Sobrien
1178344Sobrien. /etc/rc.subr
1278344Sobrien
13193119Sdougbname="ntpd"
14230099Sdougbrcvar="ntpd_enable"
15101851Sgordoncommand="/usr/sbin/${name}"
16101851Sgordonpidfile="/var/run/${name}.pid"
17295461Scyextra_commands="fetch"
18295461Scyfetch_cmd="ntpd_fetch_leapfile"
19135194Sseancstart_precmd="ntpd_precmd"
20101851Sgordon
21157840Sflzload_rc_config $name
22157840Sflz
2398184Sgordonntpd_precmd()
2498184Sgordon{
25157840Sflz	rc_flags="-c ${ntpd_config} ${ntpd_flags}"
26157840Sflz
27135194Sseanc	if checkyesno ntpd_sync_on_start; then
28187879Skeramida		rc_flags="-g $rc_flags"
29135194Sseanc	fi
30135194Sseanc
31295619Scy	if [ ! -f $ntp_db_leapfile ]; then
32295619Scy		ntpd_fetch_leapfile
33295619Scy	fi
34295619Scy
3598184Sgordon	if [ -z "$ntpd_chrootdir" ]; then
3698184Sgordon		return 0;
3798184Sgordon	fi
3898184Sgordon
3998184Sgordon	# If running in a chroot cage, ensure that the appropriate files
40104980Sschweikh	# exist inside the cage, as well as helper symlinks into the cage
4198184Sgordon	# from outside.
4298184Sgordon	#
4398184Sgordon	# As this is called after the is_running and required_dir checks
4498184Sgordon	# are made in run_rc_command(), we can safely assume ${ntpd_chrootdir}
4598184Sgordon	# exists and ntpd isn't running at this point (unless forcestart
4698184Sgordon	# is used).
4798184Sgordon	#
4898184Sgordon	if [ ! -c "${ntpd_chrootdir}/dev/clockctl" ]; then
4998184Sgordon		rm -f "${ntpd_chrootdir}/dev/clockctl"
5098184Sgordon		( cd /dev ; /bin/pax -rw -pe clockctl "${ntpd_chrootdir}/dev" )
5198184Sgordon	fi
5298184Sgordon	ln -fs "${ntpd_chrootdir}/var/db/ntp.drift" /var/db/ntp.drift
53295461Scy	ln -fs "${ntpd_chrootdir}${ntp_tmp_leapfile}" ${ntp_tmp_leapfile}
5498184Sgordon
5598184Sgordon	#	Change run_rc_commands()'s internal copy of $ntpd_flags
5698184Sgordon	#
5798184Sgordon	rc_flags="-u ntpd:ntpd -i ${ntpd_chrootdir} $rc_flags"
5898184Sgordon}
5998184Sgordon
60295461Scycurrent_ntp_ts() {
61295461Scy	# Seconds between 1900-01-01 and 1970-01-01
62295461Scy	# echo $(((70*365+17)*86400))
63295461Scy	ntp_to_unix=2208988800
64295461Scy
65295461Scy	echo $(($(date -u +%s)+$ntp_to_unix))
66295461Scy}
67295461Scy	
68295461Scyget_ntp_leapfile_ver() {
69295461Scy	expr "$(awk '$1 == "#$" { print $2 }' "$1" 2>/dev/null)" : \
70295461Scy		'^\([1-9][0-9]*\)$' \| 0
71295461Scy}
72295461Scy
73295461Scyget_ntp_leapfile_expiry() {
74295461Scy	expr "$(awk '$1 == "#@" { print $2 }' "$1" 2>/dev/null)" : \
75295461Scy		'^\([1-9][0-9]*\)$' \| 0
76295461Scy}
77295461Scy
78295461Scyntpd_fetch_leapfile() {
79295461Scy	local ntp_tmp_leapfile rc verbose
80295461Scy	
81295461Scy	if checkyesno ntp_leapfile_fetch_verbose; then
82295461Scy		verbose=echo
83295461Scy	else
84295461Scy		verbose=:
85295461Scy	fi
86295461Scy
87295461Scy	ntp_tmp_leapfile="/var/run/ntpd.leap-seconds.list"
88295461Scy
89295461Scy	ntp_ver_no_src=$(get_ntp_leapfile_ver $ntp_src_leapfile)
90295461Scy	ntp_ver_no_db=$(get_ntp_leapfile_ver $ntp_db_leapfile)
91295461Scy	$verbose ntp_src_leapfile version is $ntp_ver_no_src
92295461Scy	$verbose ntp_db_leapfile version is $ntp_ver_no_db
93295461Scy
94295461Scy	if [ "$ntp_ver_no_src" -gt "$ntp_ver_no_db" ]; then
95295461Scy		$verbose replacing $ntp_db_leapfile with $ntp_src_leapfile 
96295461Scy		cp -p $ntp_src_leapfile $ntp_db_leapfile
97295461Scy		ntp_ver_no_db=$ntp_ver_no_src
98295461Scy	else
99295461Scy		$verbose not replacing $ntp_db_leapfile with $ntp_src_leapfile 
100295461Scy	fi
101295461Scy	ntp_leap_expiry=$(get_ntp_leapfile_expiry $ntp_db_leapfile)
102295461Scy	ntp_leapfile_expiry_seconds=$((ntp_leapfile_expiry_days*86400))
103295461Scy	ntp_leap_fetch_date=$((ntp_leap_expiry-ntp_leapfile_expiry_seconds))
104295461Scy	if [ $(current_ntp_ts) -ge $ntp_leap_fetch_date ]; then
105295461Scy		$verbose Within ntp leapfile expiry limit, initiating fetch
106295461Scy		for url in $ntp_leapfile_sources ; do
107295461Scy			$verbose fetching $url
108295461Scy			fetch $ntp_leapfile_fetch_opts -o $ntp_tmp_leapfile $url && break
109295461Scy		done
110295461Scy		ntp_ver_no_tmp=$(get_ntp_leapfile_ver $ntp_tmp_leapfile)
111295461Scy		if [ "$ntp_ver_no_tmp" -gt "$ntp_ver_no_db" ]; then
112295461Scy			$verbose using $url as $ntp_db_leapfile
113295461Scy			mv $ntp_tmp_leapfile $ntp_db_leapfile
114295461Scy		else
115295461Scy			$verbose using existing $ntp_db_leapfile
116295461Scy		fi
117295461Scy	fi
118295461Scy}
119295461Scy
12078344Sobrienrun_rc_command "$1"
121