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 : ${ntpd_restart_cmd:=service_condcommand ntpd restart} 12 if type invoke-rc.d >/dev/null 2>&1; then 13 # Debian has a seperate file for DHCP config to avoid stamping on 14 # the master. 15 [ -e /var/lib/ntp ] || mkdir /var/lib/ntp 16 : ${NTP_DHCP_CONF:=/var/lib/ntp/ntp.conf.dhcp} 17 fi 18 19 ntp_conf_dir="$state_dir/ntp.conf" 20 ntp_conf=${NTP_CONF:-/etc/ntp.conf} 21 22 build_ntp_conf() 23 { 24 local cf="$state_dir/ntp.conf.$interface" 25 local interfaces= header= srvs= servers= x= 26 27 # Build a list of interfaces 28 interfaces=$(list_interfaces "$ntp_conf_dir") 29 30 if [ -n "$interfaces" ]; then 31 # Build the header 32 for x in ${interfaces}; do 33 header="$header${header:+, }$x" 34 done 35 36 # Build a server list 37 srvs=$(cd "$ntp_conf_dir"; 38 key_get_value "server " $interfaces) 39 if [ -n "$srvs" ]; then 40 for x in $(uniqify $srvs); do 41 servers="${servers}server $x\n" 42 done 43 fi 44 fi 45 46 # Merge our config into ntp.conf 47 [ -e "$cf" ] && rm -f "$cf" 48 [ -d "$ntp_conf_dir" ] || mkdir -p "$ntp_conf_dir" 49 50 if [ -n "$NTP_DHCP_CONF" ]; then 51 cp "$ntp_conf" "$cf" 52 ntp_conf="$NTP_DHCP_CONF" 53 elif [ -e "$ntp_conf" ]; then 54 remove_markers "$signature_base" "$signature_base_end" \ 55 "$ntp_conf" > "$cf" 56 fi 57 58 if [ -n "$servers" ]; then 59 echo "$signature_base${header:+ $from }$header" >> "$cf" 60 printf "$search$servers" >> "$cf" 61 echo "$signature_base_end${header:+ $from }$header" >> "$cf" 62 else 63 [ -e "$ntp_conf" ] || return 64 fi 65 66 # If we changed anything, restart ntpd 67 if change_file "$ntp_conf" "$cf"; then 68 [ -n "$ntpd_restart_cmd" ] && eval $ntpd_restart_cmd 69 fi 70 } 71 72 add_ntp_conf() 73 { 74 local cf="$ntp_conf_dir/$interface" x= 75 76 [ -e "$cf" ] && rm "$cf" 77 [ -d "$ntp_conf_dir" ] || mkdir -p "$ntp_conf_dir" 78 if [ -n "$new_ntp_servers" ]; then 79 for x in $new_ntp_servers; do 80 echo "server $x" >> "$cf" 81 done 82 fi 83 build_ntp_conf 84 } 85 86 remove_ntp_conf() 87 { 88 if [ -e "$ntp_conf_dir/$interface" ]; then 89 rm "$ntp_conf_dir/$interface" 90 fi 91 build_ntp_conf 92 } 93 94 if $if_up; then 95 add_ntp_conf add 96 elif $if_down; then 97 remove_ntp_conf del 98 fi 99