7 # REQUIRE: DAEMON ntpdate FILESYSTEMS devfs
9 # KEYWORD: nojail shutdown
15 command="/usr/sbin/${name}"
16 pidfile="/var/run/${name}.pid"
17 extra_commands="fetch"
18 fetch_cmd="ntpd_fetch_leapfile"
19 start_precmd="ntpd_precmd"
25 rc_flags="-c ${ntpd_config} ${ntpd_flags}"
27 if checkyesno ntpd_sync_on_start; then
28 rc_flags="-g $rc_flags"
31 if [ ! -f $ntp_db_leapfile ]; then
35 if [ -z "$ntpd_chrootdir" ]; then
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
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
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" )
52 ln -fs "${ntpd_chrootdir}/var/db/ntp.drift" /var/db/ntp.drift
53 ln -fs "${ntpd_chrootdir}${ntp_tmp_leapfile}" ${ntp_tmp_leapfile}
55 # Change run_rc_commands()'s internal copy of $ntpd_flags
57 rc_flags="-u ntpd:ntpd -i ${ntpd_chrootdir} $rc_flags"
61 # Seconds between 1900-01-01 and 1970-01-01
62 # echo $(((70*365+17)*86400))
63 ntp_to_unix=2208988800
65 echo $(($(date -u +%s)+$ntp_to_unix))
68 get_ntp_leapfile_ver() {
69 expr "$(awk '$1 == "#$" { print $2 }' "$1" 2>/dev/null)" : \
70 '^\([1-9][0-9]*\)$' \| 0
73 get_ntp_leapfile_expiry() {
74 expr "$(awk '$1 == "#@" { print $2 }' "$1" 2>/dev/null)" : \
75 '^\([1-9][0-9]*\)$' \| 0
78 ntpd_fetch_leapfile() {
79 local ntp_tmp_leapfile rc verbose
81 if checkyesno ntp_leapfile_fetch_verbose; then
87 ntp_tmp_leapfile="/var/run/ntpd.leap-seconds.list"
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
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
99 $verbose not replacing $ntp_db_leapfile with $ntp_src_leapfile
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
110 ntp_ver_no_tmp=$(get_ntp_leapfile_ver $ntp_tmp_leapfile)
111 if [ "$ntp_ver_no_tmp" -gt "$ntp_ver_no_db" ]; then
112 $verbose using $url as $ntp_db_leapfile
113 mv $ntp_tmp_leapfile $ntp_db_leapfile
115 $verbose using existing $ntp_db_leapfile