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 needfetch"
19 fetch_cmd="ntpd_fetch_leapfile"
20 needfetch_cmd="ntpd_needfetch_leapfile"
21 start_precmd="ntpd_precmd"
23 ntp_tmp_leapfile="/var/run/ntpd.leap-seconds.list"
29 rc_flags="-c ${ntpd_config} ${ntpd_flags}"
31 if checkyesno ntpd_sync_on_start; then
32 rc_flags="-g $rc_flags"
37 if [ ! -f $ntp_db_leapfile ]; then
41 if [ -z "$ntpd_chrootdir" ]; then
45 # If running in a chroot cage, ensure that the appropriate files
46 # exist inside the cage, as well as helper symlinks into the cage
49 # As this is called after the is_running and required_dir checks
50 # are made in run_rc_command(), we can safely assume ${ntpd_chrootdir}
51 # exists and ntpd isn't running at this point (unless forcestart
54 if [ ! -c "${ntpd_chrootdir}/dev/clockctl" ]; then
55 rm -f "${ntpd_chrootdir}/dev/clockctl"
56 ( cd /dev ; /bin/pax -rw -pe clockctl "${ntpd_chrootdir}/dev" )
58 ln -fs "${ntpd_chrootdir}/var/db/ntp.drift" /var/db/ntp.drift
59 ln -fs "${ntpd_chrootdir}${ntp_tmp_leapfile}" ${ntp_tmp_leapfile}
61 # Change run_rc_commands()'s internal copy of $ntpd_flags
63 rc_flags="-u ntpd:ntpd -i ${ntpd_chrootdir} $rc_flags"
67 # Seconds between 1900-01-01 and 1970-01-01
68 # echo $(((70*365+17)*86400))
69 ntp_to_unix=2208988800
71 echo $(($(date -u +%s)+$ntp_to_unix))
74 get_ntp_leapfile_ver() {
75 # Leapfile update date (version number).
76 expr "$(awk '$1 == "#$" { print $2 }' "$1" 2>/dev/null)" : \
77 '^\([1-9][0-9]*\)$' \| 0
80 get_ntp_leapfile_expiry() {
81 # Leapfile expiry date.
82 expr "$(awk '$1 == "#@" { print $2 }' "$1" 2>/dev/null)" : \
83 '^\([1-9][0-9]*\)$' \| 0
86 ntpd_init_leapfile() {
87 # Refresh working leapfile with an invalid hash due to
88 # FreeBSD id header. Ntpd will ignore leapfiles with a
89 # mismatch hash. The file must be the virgin file from
91 if [ ! -f $ntp_db_leapfile ]; then
92 cp -p $ntp_src_leapfile $ntp_db_leapfile
96 ntpd_needfetch_leapfile() {
99 if checkyesno ntp_leapfile_fetch_verbose; then
105 ntp_ver_no_src=$(get_ntp_leapfile_ver $ntp_src_leapfile)
106 ntp_expiry_src=$(get_ntp_leapfile_expiry $ntp_src_leapfile)
107 ntp_ver_no_db=$(get_ntp_leapfile_ver $ntp_db_leapfile)
108 ntp_expiry_db=$(get_ntp_leapfile_expiry $ntp_db_leapfile)
109 $verbose ntp_src_leapfile version is $ntp_ver_no_src
110 $verbose ntp_db_leapfile version is $ntp_ver_no_db
112 if [ "$ntp_ver_no_src" -gt "$ntp_ver_no_db" -o \
113 "$ntp_ver_no_src" -eq "$ntp_ver_no_db" -a \
114 "$ntp_expiry_src" -gt "$ntp_expiry_db" ]; then
115 $verbose replacing $ntp_db_leapfile with $ntp_src_leapfile
116 cp -p $ntp_src_leapfile $ntp_db_leapfile
117 ntp_ver_no_db=$ntp_ver_no_src
119 $verbose not replacing $ntp_db_leapfile with $ntp_src_leapfile
121 ntp_leapfile_expiry_seconds=$((ntp_leapfile_expiry_days*86400))
122 ntp_leap_expiry=$(get_ntp_leapfile_expiry $ntp_db_leapfile)
123 ntp_leap_fetch_date=$((ntp_leap_expiry-ntp_leapfile_expiry_seconds))
124 if [ $(current_ntp_ts) -ge $ntp_leap_fetch_date ]; then
125 $verbose Within ntp leapfile expiry limit, initiating fetch
126 # Return code 0: ntp leapfile fetch needed
129 # Return code 1: ntp leapfile fetch not needed
133 ntpd_fetch_leapfile() {
134 if checkyesno ntp_leapfile_fetch_verbose; then
140 if ntpd_needfetch_leapfile ; then
141 for url in $ntp_leapfile_sources ; do
142 $verbose fetching $url
143 fetch $ntp_leapfile_fetch_opts -o $ntp_tmp_leapfile $url && break
145 ntp_ver_no_tmp=$(get_ntp_leapfile_ver $ntp_tmp_leapfile)
146 ntp_expiry_tmp=$(get_ntp_leapfile_expiry $ntp_tmp_leapfile)
147 if [ "$ntp_expiry_tmp" -gt "$ntp_expiry_db" -o \
148 "$ntp_expiry_tmp" -eq "$ntp_expiry_db" -a \
149 "$ntp_ver_no_tmp" -gt "$ntp_ver_no_db" ]; then
150 $verbose using $url as $ntp_db_leapfile
151 mv $ntp_tmp_leapfile $ntp_db_leapfile
153 $verbose using existing $ntp_db_leapfile