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"
34 if [ ! -f $ntp_db_leapfile ]; then
38 if [ -z "$ntpd_chrootdir" ]; then
42 # If running in a chroot cage, ensure that the appropriate files
43 # exist inside the cage, as well as helper symlinks into the cage
46 # As this is called after the is_running and required_dir checks
47 # are made in run_rc_command(), we can safely assume ${ntpd_chrootdir}
48 # exists and ntpd isn't running at this point (unless forcestart
51 if [ ! -c "${ntpd_chrootdir}/dev/clockctl" ]; then
52 rm -f "${ntpd_chrootdir}/dev/clockctl"
53 ( cd /dev ; /bin/pax -rw -pe clockctl "${ntpd_chrootdir}/dev" )
55 ln -fs "${ntpd_chrootdir}/var/db/ntp.drift" /var/db/ntp.drift
56 ln -fs "${ntpd_chrootdir}${ntp_tmp_leapfile}" ${ntp_tmp_leapfile}
58 # Change run_rc_commands()'s internal copy of $ntpd_flags
60 rc_flags="-u ntpd:ntpd -i ${ntpd_chrootdir} $rc_flags"
64 # Seconds between 1900-01-01 and 1970-01-01
65 # echo $(((70*365+17)*86400))
66 ntp_to_unix=2208988800
68 echo $(($(date -u +%s)+$ntp_to_unix))
71 get_ntp_leapfile_ver() {
72 # Leapfile update date (version number).
73 expr "$(awk '$1 == "#$" { print $2 }' "$1" 2>/dev/null)" : \
74 '^\([1-9][0-9]*\)$' \| 0
77 get_ntp_leapfile_expiry() {
78 # Leapfile expiry date.
79 expr "$(awk '$1 == "#@" { print $2 }' "$1" 2>/dev/null)" : \
80 '^\([1-9][0-9]*\)$' \| 0
83 ntpd_init_leapfile() {
84 # Refresh working leapfile with an invalid hash due to
85 # FreeBSD id header. Ntpd will ignore leapfiles with a
86 # mismatch hash. The file must be the virgin file from
88 if [ ! -f $ntp_db_leapfile ]; then
89 cp -p $ntp_src_leapfile $ntp_db_leapfile
93 ntpd_fetch_leapfile() {
94 local ntp_tmp_leapfile rc verbose
96 if checkyesno ntp_leapfile_fetch_verbose; then
102 ntp_tmp_leapfile="/var/run/ntpd.leap-seconds.list"
104 ntp_ver_no_src=$(get_ntp_leapfile_ver $ntp_src_leapfile)
105 ntp_expiry_src=$(get_ntp_leapfile_expiry $ntp_src_leapfile)
106 ntp_ver_no_db=$(get_ntp_leapfile_ver $ntp_db_leapfile)
107 ntp_expiry_db=$(get_ntp_leapfile_expiry $ntp_db_leapfile)
108 $verbose ntp_src_leapfile version is $ntp_ver_no_src
109 $verbose ntp_db_leapfile version is $ntp_ver_no_db
111 if [ "$ntp_ver_no_src" -gt "$ntp_ver_no_db" -o \
112 "$ntp_ver_no_src" -eq "$ntp_ver_no_db" -a \
113 "$ntp_expiry_src" -gt "$ntp_expiry_db" ]; then
114 $verbose replacing $ntp_db_leapfile with $ntp_src_leapfile
115 cp -p $ntp_src_leapfile $ntp_db_leapfile
116 ntp_ver_no_db=$ntp_ver_no_src
118 $verbose not replacing $ntp_db_leapfile with $ntp_src_leapfile
120 ntp_leapfile_expiry_seconds=$((ntp_leapfile_expiry_days*86400))
121 ntp_leap_expiry=$(get_ntp_leapfile_expiry $ntp_db_leapfile)
122 ntp_leap_fetch_date=$((ntp_leap_expiry-ntp_leapfile_expiry_seconds))
123 if [ $(current_ntp_ts) -ge $ntp_leap_fetch_date ]; then
124 $verbose Within ntp leapfile expiry limit, initiating fetch
125 for url in $ntp_leapfile_sources ; do
126 $verbose fetching $url
127 fetch $ntp_leapfile_fetch_opts -o $ntp_tmp_leapfile $url && break
129 ntp_ver_no_tmp=$(get_ntp_leapfile_ver $ntp_tmp_leapfile)
130 ntp_expiry_tmp=$(get_ntp_leapfile_expiry $ntp_tmp_leapfile)
131 if [ "$ntp_ver_no_tmp" -gt "$ntp_ver_no_db" -o \
132 "$ntp_ver_no_tmp" -eq "$ntp_ver_no_db" -a \
133 "$ntp_expiry_tmp" -gt "$ntp_expiry_db" ]; then
134 $verbose using $url as $ntp_db_leapfile
135 mv $ntp_tmp_leapfile $ntp_db_leapfile
137 $verbose using existing $ntp_db_leapfile