]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - etc/rc.d/ntpd
Use the expiry date to determine whether to replace the DB copy of
[FreeBSD/FreeBSD.git] / etc / rc.d / ntpd
1 #!/bin/sh
2 #
3 # $FreeBSD$
4 #
5
6 # PROVIDE: ntpd
7 # REQUIRE: DAEMON ntpdate FILESYSTEMS devfs
8 # BEFORE:  LOGIN
9 # KEYWORD: nojail shutdown
10
11 . /etc/rc.subr
12
13 name="ntpd"
14 desc="Network Time Protocol daemon"
15 rcvar="ntpd_enable"
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"
21
22 load_rc_config $name
23
24 ntpd_precmd()
25 {
26         rc_flags="-c ${ntpd_config} ${ntpd_flags}"
27
28         if checkyesno ntpd_sync_on_start; then
29                 rc_flags="-g $rc_flags"
30         fi
31
32         if [ ! -f $ntp_db_leapfile ]; then
33                 ntpd_fetch_leapfile
34         fi
35
36         if [ -z "$ntpd_chrootdir" ]; then
37                 return 0;
38         fi
39
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
42         # from outside.
43         #
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
47         # is used).
48         #
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" )
52         fi
53         ln -fs "${ntpd_chrootdir}/var/db/ntp.drift" /var/db/ntp.drift
54         ln -fs "${ntpd_chrootdir}${ntp_tmp_leapfile}" ${ntp_tmp_leapfile}
55
56         #       Change run_rc_commands()'s internal copy of $ntpd_flags
57         #
58         rc_flags="-u ntpd:ntpd -i ${ntpd_chrootdir} $rc_flags"
59 }
60
61 current_ntp_ts() {
62         # Seconds between 1900-01-01 and 1970-01-01
63         # echo $(((70*365+17)*86400))
64         ntp_to_unix=2208988800
65
66         echo $(($(date -u +%s)+$ntp_to_unix))
67 }
68         
69 get_ntp_leapfile_ver() {
70         expr "$(awk '$1 == "#$" { print $2 }' "$1" 2>/dev/null)" : \
71                 '^\([1-9][0-9]*\)$' \| 0
72 }
73
74 get_ntp_leapfile_expiry() {
75         expr "$(awk '$1 == "#@" { print $2 }' "$1" 2>/dev/null)" : \
76                 '^\([1-9][0-9]*\)$' \| 0
77 }
78
79 ntpd_fetch_leapfile() {
80         local ntp_tmp_leapfile rc verbose
81         
82         if checkyesno ntp_leapfile_fetch_verbose; then
83                 verbose=echo
84         else
85                 verbose=:
86         fi
87
88         ntp_tmp_leapfile="/var/run/ntpd.leap-seconds.list"
89
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
94
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
99         else
100                 $verbose not replacing $ntp_db_leapfile with $ntp_src_leapfile 
101         fi
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
110                 done
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
115                 else
116                         $verbose using existing $ntp_db_leapfile
117                 fi
118         fi
119 }
120
121 run_rc_command "$1"