No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / dhcpcd / dist / dhcpcd-hooks / 50-ntp.conf
blob8fad6df55b334ded07539380d1d4f20940cbd354
1 # Sample dhcpcd hook script for ntp
2 # Like our resolv.conf hook script, we store a database of ntp.conf files
3 # and merge into /etc/ntp.conf
5 # You can set the env var NTP_CONF to another file like this
6 #   dhcpcd -e NTP_CONF=/usr/pkg/etc/ntpd.conf
7 # or by adding this to /etc/dhcpcd.enter-hook
8 #   NTP_CONF=/usr/pkg/etc/ntpd.conf
9 # to use openntpd from pkgsrc instead of the system provided ntp.
11 # Detect OpenRC or BSD rc
12 # Distributions may want to just have their command here instead of this
13 if type rc-service >/dev/null 2>&1 && rc-service --exists ntpd; then
14         ntpd_restart_cmd="rc-service ntpd -- -Ds restart"
15 elif [ -x /etc/rc.d/ntpd ]; then
16         ntpd_restart_cmd="/etc/rc.d/ntpd status >/dev/null 2>&1 && /etc/rc.d/ntpd restart"
17 elif [ -x /usr/local/etc/rc.d/ntpd ]; then
18         ntpd_restart_cmd="/usr/local/etc/rc.d/ntpd status >/dev/null 2>&1 && /usr/local/etc/rc.d/ntpd restart"
21 ntp_conf_dir="$state_dir/ntp.conf"
22 ntp_conf=${NTP_CONF:-/etc/ntp.conf}
24 build_ntp_conf()
26         local cf="$state_dir/ntp.conf.$interface"
27         local interfaces= header= srvs= servers= x=
29         # Build a list of interfaces
30         interfaces=$(list_interfaces "$ntp_conf_dir")
32         if [ -n "$interfaces" ]; then
33                 # Build the header
34                 for x in ${interfaces}; do
35                         header="$header${header:+, }$x"
36                 done
38                 # Build a server list
39                 srvs=$(cd "$ntp_conf_dir";
40                         key_get_value "server " $interfaces)
41                 if [ -n "$srvs" ]; then
42                         for x in $(uniqify $srvs); do
43                                 servers="${servers}server $x\n"
44                         done
45                 fi
46         fi
48         # Merge our config into ntp.conf
49         [ -e "$cf" ] && rm -f "$cf"
50         [ -d "$ntp_conf_dir" ] || mkdir -p "$ntp_conf_dir"
51         if [ -e "$ntp_conf" ]; then
52                 remove_markers "$signature_base" "$signature_base_end" \
53                         "$ntp_conf" > "$cf"
54         fi
55         if [ -n "$servers" ]; then
56                 echo "$signature_base${header:+ $from }$header" >> "$cf"
57                 printf "$search$servers" >> "$cf"
58                 echo "$signature_base_end${header:+ $from }$header" >> "$cf"
59         fi
61         # If we changed anything, restart ntpd
62         if change_file "$ntp_conf" "$cf"; then
63                 [ -n "$ntpd_restart_cmd" ] && eval $ntpd_restart_cmd
64         fi
67 add_ntp_conf()
69         local cf="$ntp_conf_dir/$interface" x=
71         [ -e "$cf" ] && rm "$cf"
72         [ -d "$ntp_conf_dir" ] || mkdir -p "$ntp_conf_dir"
73         if [ -n "$new_ntp_servers" ]; then
74                 for x in $new_ntp_servers; do
75                         echo "server $x" >> "$cf"
76                 done
77         fi
78         build_ntp_conf
81 remove_ntp_conf()
83         if [ -e "$ntp_conf_dir/$interface" ]; then
84                 rm "$ntp_conf_dir/$interface"
85         fi
86         build_ntp_conf
89 case "$reason" in
90 BOUND|INFORM|REBIND|REBOOT|RENEW|TIMEOUT|STATIC)        add_ntp_conf add;;
91 PREINIT|EXPIRE|FAIL|IPV4LL|NAK|NOCARRIER|RELEASE|STOP)  remove_ntp_conf del;;
92 esac