7 # REQUIRE: DAEMON ntpdate FILESYSTEMS devfs
9 # KEYWORD: nojail shutdown
14 desc="Network Time Protocol daemon"
16 command="/usr/sbin/${name}"
17 pidfile="/var/run/${name}.pid"
18 extra_commands="fetch"
19 fetch_cmd="ntpd_fetch_leapfile"
20 start_precmd="ntpd_precmd"
26 rc_flags="-c ${ntpd_config} ${ntpd_flags}"
28 if checkyesno ntpd_sync_on_start; then
29 rc_flags="-g $rc_flags"
32 if [ ! -f $ntp_db_leapfile ]; then
36 if [ -z "$ntpd_chrootdir" ]; then
40 # If running in a chroot cage, ensure that the appropriate files
41 # exist inside the cage, as well as helper symlinks into the cage
44 # As this is called after the is_running and required_dir checks
45 # are made in run_rc_command(), we can safely assume ${ntpd_chrootdir}
46 # exists and ntpd isn't running at this point (unless forcestart
49 if [ ! -c "${ntpd_chrootdir}/dev/clockctl" ]; then
50 rm -f "${ntpd_chrootdir}/dev/clockctl"
51 ( cd /dev ; /bin/pax -rw -pe clockctl "${ntpd_chrootdir}/dev" )
53 ln -fs "${ntpd_chrootdir}/var/db/ntp.drift" /var/db/ntp.drift
54 ln -fs "${ntpd_chrootdir}${ntp_tmp_leapfile}" ${ntp_tmp_leapfile}
56 # Change run_rc_commands()'s internal copy of $ntpd_flags
58 rc_flags="-u ntpd:ntpd -i ${ntpd_chrootdir} $rc_flags"
62 # Seconds between 1900-01-01 and 1970-01-01
63 # echo $(((70*365+17)*86400))
64 ntp_to_unix=2208988800
66 echo $(($(date -u +%s)+$ntp_to_unix))
69 get_ntp_leapfile_ver() {
70 expr "$(awk '$1 == "#$" { print $2 }' "$1" 2>/dev/null)" : \
71 '^\([1-9][0-9]*\)$' \| 0
74 get_ntp_leapfile_expiry() {
75 expr "$(awk '$1 == "#@" { print $2 }' "$1" 2>/dev/null)" : \
76 '^\([1-9][0-9]*\)$' \| 0
79 ntpd_fetch_leapfile() {
80 local ntp_tmp_leapfile rc verbose
82 if checkyesno ntp_leapfile_fetch_verbose; then
88 ntp_tmp_leapfile="/var/run/ntpd.leap-seconds.list"
90 ntp_ver_no_src=$(get_ntp_leapfile_ver $ntp_src_leapfile)
91 ntp_ver_no_db=$(get_ntp_leapfile_ver $ntp_db_leapfile)
92 $verbose ntp_src_leapfile version is $ntp_ver_no_src
93 $verbose ntp_db_leapfile version is $ntp_ver_no_db
95 if [ "$ntp_ver_no_src" -gt "$ntp_ver_no_db" ]; then
96 $verbose replacing $ntp_db_leapfile with $ntp_src_leapfile
97 cp -p $ntp_src_leapfile $ntp_db_leapfile
98 ntp_ver_no_db=$ntp_ver_no_src
100 $verbose not replacing $ntp_db_leapfile with $ntp_src_leapfile
102 ntp_leap_expiry=$(get_ntp_leapfile_expiry $ntp_db_leapfile)
103 ntp_leapfile_expiry_seconds=$((ntp_leapfile_expiry_days*86400))
104 ntp_leap_fetch_date=$((ntp_leap_expiry-ntp_leapfile_expiry_seconds))
105 if [ $(current_ntp_ts) -ge $ntp_leap_fetch_date ]; then
106 $verbose Within ntp leapfile expiry limit, initiating fetch
107 for url in $ntp_leapfile_sources ; do
108 $verbose fetching $url
109 fetch $ntp_leapfile_fetch_opts -o $ntp_tmp_leapfile $url && break
111 ntp_expiry_tmp=$(get_ntp_leapfile_expiry $ntp_tmp_leapfile)
112 if [ "$ntp_expiry_tmp" -gt "$ntp_leap_expiry" ]; then
113 $verbose using $url as $ntp_db_leapfile
114 mv $ntp_tmp_leapfile $ntp_db_leapfile
116 $verbose using existing $ntp_db_leapfile