2001-06-16 07:16:14 +00:00
|
|
|
#!/bin/sh
|
|
|
|
#
|
2002-06-13 22:14:37 +00:00
|
|
|
# $FreeBSD$
|
2001-06-16 07:16:14 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
# PROVIDE: ntpd
|
2012-09-11 05:04:59 +00:00
|
|
|
# REQUIRE: DAEMON ntpdate FILESYSTEMS devfs
|
2002-06-13 22:14:37 +00:00
|
|
|
# BEFORE: LOGIN
|
2008-07-16 19:50:29 +00:00
|
|
|
# KEYWORD: nojail shutdown
|
2001-06-16 07:16:14 +00:00
|
|
|
|
|
|
|
. /etc/rc.subr
|
|
|
|
|
2009-05-30 21:51:38 +00:00
|
|
|
name="ntpd"
|
2012-01-14 02:18:41 +00:00
|
|
|
rcvar="ntpd_enable"
|
2002-08-14 05:44:32 +00:00
|
|
|
command="/usr/sbin/${name}"
|
|
|
|
pidfile="/var/run/${name}.pid"
|
2016-01-26 07:06:44 +00:00
|
|
|
extra_commands="fetch"
|
|
|
|
fetch_cmd="ntpd_fetch_leapfile"
|
2004-09-14 03:01:38 +00:00
|
|
|
start_precmd="ntpd_precmd"
|
2006-04-18 15:02:24 +00:00
|
|
|
|
|
|
|
load_rc_config $name
|
|
|
|
|
2002-06-13 22:14:37 +00:00
|
|
|
ntpd_precmd()
|
|
|
|
{
|
2006-04-18 15:02:24 +00:00
|
|
|
rc_flags="-c ${ntpd_config} ${ntpd_flags}"
|
|
|
|
|
2004-09-14 03:01:38 +00:00
|
|
|
if checkyesno ntpd_sync_on_start; then
|
2009-01-29 06:43:29 +00:00
|
|
|
rc_flags="-g $rc_flags"
|
2004-09-14 03:01:38 +00:00
|
|
|
fi
|
|
|
|
|
2002-06-13 22:14:37 +00:00
|
|
|
if [ -z "$ntpd_chrootdir" ]; then
|
|
|
|
return 0;
|
|
|
|
fi
|
|
|
|
|
2016-01-26 07:06:44 +00:00
|
|
|
if [ ! -f $ntp_db_leapfile ]; then
|
|
|
|
ntpd_fetch_leapfile
|
|
|
|
fi
|
|
|
|
|
2002-06-13 22:14:37 +00:00
|
|
|
# If running in a chroot cage, ensure that the appropriate files
|
2002-10-12 10:31:31 +00:00
|
|
|
# exist inside the cage, as well as helper symlinks into the cage
|
2002-06-13 22:14:37 +00:00
|
|
|
# from outside.
|
|
|
|
#
|
|
|
|
# As this is called after the is_running and required_dir checks
|
|
|
|
# are made in run_rc_command(), we can safely assume ${ntpd_chrootdir}
|
|
|
|
# exists and ntpd isn't running at this point (unless forcestart
|
|
|
|
# is used).
|
|
|
|
#
|
|
|
|
if [ ! -c "${ntpd_chrootdir}/dev/clockctl" ]; then
|
|
|
|
rm -f "${ntpd_chrootdir}/dev/clockctl"
|
|
|
|
( cd /dev ; /bin/pax -rw -pe clockctl "${ntpd_chrootdir}/dev" )
|
|
|
|
fi
|
|
|
|
ln -fs "${ntpd_chrootdir}/var/db/ntp.drift" /var/db/ntp.drift
|
2016-01-26 07:06:44 +00:00
|
|
|
ln -fs "${ntpd_chrootdir}${ntp_tmp_leapfile}" ${ntp_tmp_leapfile}
|
2002-06-13 22:14:37 +00:00
|
|
|
|
|
|
|
# Change run_rc_commands()'s internal copy of $ntpd_flags
|
|
|
|
#
|
|
|
|
rc_flags="-u ntpd:ntpd -i ${ntpd_chrootdir} $rc_flags"
|
|
|
|
}
|
|
|
|
|
2016-01-26 07:06:44 +00:00
|
|
|
current_ntp_ts() {
|
|
|
|
# Seconds between 1900-01-01 and 1970-01-01
|
|
|
|
# echo $(((70*365+17)*86400))
|
|
|
|
ntp_to_unix=2208988800
|
|
|
|
|
|
|
|
echo $(($(date -u +%s)+$ntp_to_unix))
|
|
|
|
}
|
|
|
|
|
|
|
|
get_ntp_leapfile_ver() {
|
|
|
|
expr "$(awk '$1 == "#$" { print $2 }' "$1" 2>/dev/null)" : \
|
|
|
|
'^\([1-9][0-9]*\)$' \| 0
|
|
|
|
}
|
|
|
|
|
|
|
|
get_ntp_leapfile_expiry() {
|
|
|
|
expr "$(awk '$1 == "#@" { print $2 }' "$1" 2>/dev/null)" : \
|
|
|
|
'^\([1-9][0-9]*\)$' \| 0
|
|
|
|
}
|
|
|
|
|
|
|
|
ntpd_fetch_leapfile() {
|
|
|
|
local ntp_tmp_leapfile rc verbose
|
|
|
|
|
|
|
|
if checkyesno ntp_leapfile_fetch_verbose; then
|
|
|
|
verbose=echo
|
|
|
|
else
|
|
|
|
verbose=:
|
|
|
|
fi
|
|
|
|
|
|
|
|
ntp_tmp_leapfile="/var/run/ntpd.leap-seconds.list"
|
|
|
|
|
|
|
|
ntp_ver_no_src=$(get_ntp_leapfile_ver $ntp_src_leapfile)
|
|
|
|
ntp_ver_no_db=$(get_ntp_leapfile_ver $ntp_db_leapfile)
|
|
|
|
$verbose ntp_src_leapfile version is $ntp_ver_no_src
|
|
|
|
$verbose ntp_db_leapfile version is $ntp_ver_no_db
|
|
|
|
|
|
|
|
if [ "$ntp_ver_no_src" -gt "$ntp_ver_no_db" ]; then
|
|
|
|
$verbose replacing $ntp_db_leapfile with $ntp_src_leapfile
|
|
|
|
cp -p $ntp_src_leapfile $ntp_db_leapfile
|
|
|
|
ntp_ver_no_db=$ntp_ver_no_src
|
|
|
|
else
|
|
|
|
$verbose not replacing $ntp_db_leapfile with $ntp_src_leapfile
|
|
|
|
fi
|
|
|
|
ntp_leap_expiry=$(get_ntp_leapfile_expiry $ntp_db_leapfile)
|
|
|
|
ntp_leapfile_expiry_seconds=$((ntp_leapfile_expiry_days*86400))
|
|
|
|
ntp_leap_fetch_date=$((ntp_leap_expiry-ntp_leapfile_expiry_seconds))
|
|
|
|
if [ $(current_ntp_ts) -ge $ntp_leap_fetch_date ]; then
|
|
|
|
$verbose Within ntp leapfile expiry limit, initiating fetch
|
|
|
|
for url in $ntp_leapfile_sources ; do
|
|
|
|
$verbose fetching $url
|
|
|
|
fetch -mqo $ntp_tmp_leapfile $url && break
|
|
|
|
done
|
|
|
|
ntp_ver_no_tmp=$(get_ntp_leapfile_ver $ntp_tmp_leapfile)
|
|
|
|
if [ "$ntp_ver_no_tmp" -gt "$ntp_ver_no_db" ]; then
|
|
|
|
$verbose using $url as $ntp_db_leapfile
|
|
|
|
mv $ntp_tmp_leapfile $ntp_db_leapfile
|
|
|
|
else
|
|
|
|
$verbose using existing $ntp_db_leapfile
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2001-06-16 07:16:14 +00:00
|
|
|
run_rc_command "$1"
|