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}
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
34 for x in ${interfaces}; do
35 header="$header${header:+, }$x"
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"
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" \
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"
61 # If we changed anything, restart ntpd
62 if change_file "$ntp_conf" "$cf"; then
63 [ -n "$ntpd_restart_cmd" ] && eval $ntpd_restart_cmd
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"
83 if [ -e "$ntp_conf_dir/$interface" ]; then
84 rm "$ntp_conf_dir/$interface"
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;;