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 13if type rc-service >/dev/null 2>&1 && rc-service --exists ntpd; then 14 ntpd_restart_cmd="rc-service ntpd -- --ifstarted --quiet restart" 15elif [ -x /etc/rc.d/ntpd ]; then 16 ntpd_restart_cmd="/etc/rc.d/ntpd status && /etc/rc.d/ntpd restart" 17elif [ -x /usr/local/etc/rc.d/ntpd ]; then 18 ntpd_restart_cmd="/usr/local/etc/rc.d/ntpd status && /usr/local/etc/rc.d/ntpd restart" 19fi 20 21ntp_conf_dir="${state_dir}/ntp.conf" 22ntp_conf=${NTP_CONF:-/etc/ntp.conf} 23 24build_ntp_conf() 25{ 26 local cf="${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 remove_markers "${signature_base}" "${signature_base_end}" \ 51 /etc/ntp.conf > "${cf}" 52 if [ -n "${servers}" ]; then 53 echo "${signature_base}${header:+ ${from} }${header}" >> "${cf}" 54 printf "${search}${servers}" >> "${cf}" 55 echo "${signature_base_end}${header:+ ${from} }${header}" >> "${cf}" 56 fi 57 58 # If we changed anything, restart ntpd 59 if change_file "${ntp_conf}" "${cf}"; then 60 [ -n "${ntpd_restart_cmd}" ] && eval ${ntpd_restart_cmd} 61 fi 62} 63 64add_ntp_conf() 65{ 66 local cf="${ntp_conf_dir}/${interface}" x= 67 68 [ -e "${cf}" ] && rm "${cf}" 69 [ -d "${ntp_conf_dir}" ] || mkdir -p "${ntp_conf_dir}" 70 if [ -n "${new_ntp_servers}" ]; then 71 for x in ${new_ntp_servers}; do 72 echo "server ${x}" >> "${cf}" 73 done 74 fi 75 build_ntp_conf 76} 77 78remove_ntp_conf() 79{ 80 if [ -e "${ntp_conf_dir}/${interface}" ]; then 81 rm "${ntp_conf_dir}/${interface}" 82 fi 83 build_ntp_conf 84} 85 86case "${reason}" in 87BOUND|INFORM|REBIND|REBOOT|RENEW|TIMEOUT) add_ntp_conf add;; 88PREINIT|EXPIRE|FAIL|IPV4LL|RELEASE|STOP) remove_ntp_conf del;; 89esac 90