ntpd revision 300897
1#!/bin/sh
2#
3# $FreeBSD: stable/10/etc/rc.d/ntpd 300897 2016-05-28 03:33:06Z 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	if [ ! -f $ntp_db_leapfile ]; then
32		ntpd_fetch_leapfile
33	fi
34
35	if [ -z "$ntpd_chrootdir" ]; then
36		return 0;
37	fi
38
39	# If running in a chroot cage, ensure that the appropriate files
40	# exist inside the cage, as well as helper symlinks into the cage
41	# from outside.
42	#
43	# As this is called after the is_running and required_dir checks
44	# are made in run_rc_command(), we can safely assume ${ntpd_chrootdir}
45	# exists and ntpd isn't running at this point (unless forcestart
46	# is used).
47	#
48	if [ ! -c "${ntpd_chrootdir}/dev/clockctl" ]; then
49		rm -f "${ntpd_chrootdir}/dev/clockctl"
50		( cd /dev ; /bin/pax -rw -pe clockctl "${ntpd_chrootdir}/dev" )
51	fi
52	ln -fs "${ntpd_chrootdir}/var/db/ntp.drift" /var/db/ntp.drift
53	ln -fs "${ntpd_chrootdir}${ntp_tmp_leapfile}" ${ntp_tmp_leapfile}
54
55	#	Change run_rc_commands()'s internal copy of $ntpd_flags
56	#
57	rc_flags="-u ntpd:ntpd -i ${ntpd_chrootdir} $rc_flags"
58}
59
60current_ntp_ts() {
61	# Seconds between 1900-01-01 and 1970-01-01
62	# echo $(((70*365+17)*86400))
63	ntp_to_unix=2208988800
64
65	echo $(($(date -u +%s)+$ntp_to_unix))
66}
67	
68get_ntp_leapfile_ver() {
69	expr "$(awk '$1 == "#$" { print $2 }' "$1" 2>/dev/null)" : \
70		'^\([1-9][0-9]*\)$' \| 0
71}
72
73get_ntp_leapfile_expiry() {
74	expr "$(awk '$1 == "#@" { print $2 }' "$1" 2>/dev/null)" : \
75		'^\([1-9][0-9]*\)$' \| 0
76}
77
78ntpd_fetch_leapfile() {
79	local ntp_tmp_leapfile rc verbose
80	
81	if checkyesno ntp_leapfile_fetch_verbose; then
82		verbose=echo
83	else
84		verbose=:
85	fi
86
87	ntp_tmp_leapfile="/var/run/ntpd.leap-seconds.list"
88
89	ntp_ver_no_src=$(get_ntp_leapfile_ver $ntp_src_leapfile)
90	ntp_ver_no_db=$(get_ntp_leapfile_ver $ntp_db_leapfile)
91	$verbose ntp_src_leapfile version is $ntp_ver_no_src
92	$verbose ntp_db_leapfile version is $ntp_ver_no_db
93
94	if [ "$ntp_ver_no_src" -gt "$ntp_ver_no_db" ]; then
95		$verbose replacing $ntp_db_leapfile with $ntp_src_leapfile 
96		cp -p $ntp_src_leapfile $ntp_db_leapfile
97		ntp_ver_no_db=$ntp_ver_no_src
98	else
99		$verbose not replacing $ntp_db_leapfile with $ntp_src_leapfile 
100	fi
101	ntp_leap_expiry=$(get_ntp_leapfile_expiry $ntp_db_leapfile)
102	ntp_leapfile_expiry_seconds=$((ntp_leapfile_expiry_days*86400))
103	ntp_leap_fetch_date=$((ntp_leap_expiry-ntp_leapfile_expiry_seconds))
104	if [ $(current_ntp_ts) -ge $ntp_leap_fetch_date ]; then
105		$verbose Within ntp leapfile expiry limit, initiating fetch
106		for url in $ntp_leapfile_sources ; do
107			$verbose fetching $url
108			fetch $ntp_leapfile_fetch_opts -o $ntp_tmp_leapfile $url && break
109		done
110		ntp_expiry_tmp=$(get_ntp_leapfile_expiry $ntp_tmp_leapfile)
111		if [ "$ntp_expiry_tmp" -gt "$ntp_leap_expiry" ]; then
112			$verbose using $url as $ntp_db_leapfile
113			mv $ntp_tmp_leapfile $ntp_db_leapfile
114		else
115			$verbose using existing $ntp_db_leapfile
116		fi
117	fi
118}
119
120run_rc_command "$1"
121