]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - etc/rc.d/ntpd
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r305145, and update
[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         ntpd_init_leapfile
33
34         if [ ! -f $ntp_db_leapfile ]; then
35                 ntpd_fetch_leapfile
36         fi
37
38         if [ -z "$ntpd_chrootdir" ]; then
39                 return 0;
40         fi
41
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
44         # from outside.
45         #
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
49         # is used).
50         #
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" )
54         fi
55         ln -fs "${ntpd_chrootdir}/var/db/ntp.drift" /var/db/ntp.drift
56         ln -fs "${ntpd_chrootdir}${ntp_tmp_leapfile}" ${ntp_tmp_leapfile}
57
58         #       Change run_rc_commands()'s internal copy of $ntpd_flags
59         #
60         rc_flags="-u ntpd:ntpd -i ${ntpd_chrootdir} $rc_flags"
61 }
62
63 current_ntp_ts() {
64         # Seconds between 1900-01-01 and 1970-01-01
65         # echo $(((70*365+17)*86400))
66         ntp_to_unix=2208988800
67
68         echo $(($(date -u +%s)+$ntp_to_unix))
69 }
70         
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
75 }
76
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
81 }
82
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
87         # the source.
88         if [ ! -f $ntp_db_leapfile ]; then
89                 cp -p $ntp_src_leapfile $ntp_db_leapfile
90         fi
91 }
92
93 ntpd_fetch_leapfile() {
94         local ntp_tmp_leapfile rc verbose
95         
96         if checkyesno ntp_leapfile_fetch_verbose; then
97                 verbose=echo
98         else
99                 verbose=:
100         fi
101
102         ntp_tmp_leapfile="/var/run/ntpd.leap-seconds.list"
103
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
110
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
117         else
118                 $verbose not replacing $ntp_db_leapfile with $ntp_src_leapfile 
119         fi
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
128                 done
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
136                 else
137                         $verbose using existing $ntp_db_leapfile
138                 fi
139         fi
140 }
141
142 run_rc_command "$1"