OLD | NEW |
| (Empty) |
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 | |
4 | |
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. | |
10 | |
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/ntp
d 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 && /us
r/local/etc/rc.d/ntpd restart" | |
19 fi | |
20 | |
21 ntp_conf_dir="$state_dir/ntp.conf" | |
22 ntp_conf=${NTP_CONF:-/etc/ntp.conf} | |
23 | |
24 build_ntp_conf() | |
25 { | |
26 local cf="$state_dir/ntp.conf.$interface" | |
27 local interfaces= header= srvs= servers= x= | |
28 | |
29 # Build a list of interfaces | |
30 interfaces=$(list_interfaces "$ntp_conf_dir") | |
31 | |
32 if [ -n "$interfaces" ]; then | |
33 # Build the header | |
34 for x in ${interfaces}; do | |
35 header="$header${header:+, }$x" | |
36 done | |
37 | |
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 | |
47 | |
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 | |
60 | |
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 | |
65 } | |
66 | |
67 add_ntp_conf() | |
68 { | |
69 local cf="$ntp_conf_dir/$interface" x= | |
70 | |
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 | |
79 } | |
80 | |
81 remove_ntp_conf() | |
82 { | |
83 if [ -e "$ntp_conf_dir/$interface" ]; then | |
84 rm "$ntp_conf_dir/$interface" | |
85 fi | |
86 build_ntp_conf | |
87 } | |
88 | |
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 | |
OLD | NEW |