Import dhcpcd-8.0.4 to vendor branch.
[dragonfly.git] / contrib / dhcpcd / hooks / 50-ntp.conf
blob4b68f7776d0e11eb0f96bccee565b7bdd2739953
1 # Sample dhcpcd hook script for NTP
2 # It will configure either one of NTP, OpenNTP or Chrony (in that order)
3 # and will default to NTP if no default config is found.
5 # Like our resolv.conf hook script, we store a database of ntp.conf files
6 # and merge into /etc/ntp.conf
8 # You can set the env var NTP_CONF to override the derived default on
9 # systems with >1 NTP client installed.
10 # Here is an example for OpenNTP
11 #   dhcpcd -e NTP_CONF=/usr/pkg/etc/ntpd.conf
12 # or by adding this to /etc/dhcpcd.conf
13 #   env NTP_CONF=/usr/pkg/etc/ntpd.conf
14 # or by adding this to /etc/dhcpcd.enter-hook
15 #   NTP_CONF=/usr/pkg/etc/ntpd.conf
16 # To use Chrony instead, simply change ntpd.conf to chrony.conf in the
17 # above examples.
19 : ${ntp_confs:=ntp.conf ntpd.conf chrony.conf}
20 : ${ntp_conf_dirs=/etc /usr/pkg/etc /usr/local/etc}
21 ntp_conf_dir="$state_dir/ntp.conf"
23 # If NTP_CONF is not set, work out a good default
24 if [ -z "$NTP_CONF" ]; then
25         for d in ${ntp_conf_dirs}; do
26                 for f in ${ntp_confs}; do
27                         if [ -e "$d/$f" ]; then
28                                 NTP_CONF="$d/$f"
29                                 break 2
30                         fi
31                 done
32         done
33         [ -e "$NTP_CONF" ] || NTP_CONF=/etc/ntp.conf
36 # Derive service name from configuration
37 if [ -z "$ntp_service" ]; then
38         case "$NTP_CONF" in
39         *chrony.conf)           ntp_service=chronyd;;
40         *)                      ntp_service=ntpd;;
41         esac
44 # Debian has a seperate file for DHCP config to avoid stamping on
45 # the master.
46 if [ "$ntp_service" = ntpd ] && type invoke-rc.d >/dev/null 2>&1; then
47         [ -e /var/lib/ntp ] || mkdir /var/lib/ntp
48         : ${ntp_service:=ntp}
49         : ${NTP_DHCP_CONF:=/var/lib/ntp/ntp.conf.dhcp}
52 : ${ntp_restart_cmd:=service_condcommand $ntp_service restart}
54 ntp_conf=${NTP_CONF}
55 NL="
58 build_ntp_conf()
60         cf="$state_dir/ntp.conf.$ifname"
62         # Build a list of interfaces
63         interfaces=$(list_interfaces "$ntp_conf_dir")
65         header=
66         servers=
67         if [ -n "$interfaces" ]; then
68                 # Build the header
69                 for x in ${interfaces}; do
70                         header="$header${header:+, }$x"
71                 done
73                 # Build a server list
74                 srvs=$(cd "$ntp_conf_dir";
75                         key_get_value "server " $interfaces)
76                 if [ -n "$srvs" ]; then
77                         for x in $(uniqify $srvs); do
78                                 servers="${servers}server $x$NL"
79                         done
80                 fi
81         fi
83         # Merge our config into ntp.conf
84         [ -e "$cf" ] && rm -f "$cf"
85         [ -d "$ntp_conf_dir" ] || mkdir -p "$ntp_conf_dir"
87         if [ -n "$NTP_DHCP_CONF" ]; then
88                 [ -e "$ntp_conf" ] && cp "$ntp_conf" "$cf"
89                 ntp_conf="$NTP_DHCP_CONF"
90         elif [ -e "$ntp_conf" ]; then
91                 remove_markers "$signature_base" "$signature_base_end" \
92                         "$ntp_conf" > "$cf"
93         fi
95         if [ -n "$servers" ]; then
96                 echo "$signature_base${header:+ $from }$header" >> "$cf"
97                 printf %s "$servers" >> "$cf"
98                 echo "$signature_base_end${header:+ $from }$header" >> "$cf"
99         else
100                 [ -e "$ntp_conf" ] && [ -e "$cf" ] || return
101         fi
103         # If we changed anything, restart ntpd
104         if change_file "$ntp_conf" "$cf"; then
105                 [ -n "$ntp_restart_cmd" ] && eval $ntp_restart_cmd
106         fi
109 add_ntp_conf()
111         cf="$ntp_conf_dir/$ifname"
113         [ -e "$cf" ] && rm "$cf"
114         [ -d "$ntp_conf_dir" ] || mkdir -p "$ntp_conf_dir"
115         if [ -n "$new_ntp_servers" ]; then
116                 for x in $new_ntp_servers; do
117                         echo "server $x" >> "$cf"
118                 done
119         fi
120         build_ntp_conf
123 remove_ntp_conf()
125         if [ -e "$ntp_conf_dir/$ifname" ]; then
126                 rm "$ntp_conf_dir/$ifname"
127         fi
128         build_ntp_conf
131 # For ease of use, map DHCP6 names onto our DHCP4 names
132 case "$reason" in
133 BOUND6|RENEW6|REBIND6|REBOOT6|INFORM6)
134         new_ntp_servers="$new_dhcp6_sntp_servers"
136 esac
138 if $if_up; then
139         add_ntp_conf
140 elif $if_down; then
141         remove_ntp_conf