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# Detect OpenRC or BSD rc 6# Distributions may want to just have their command here instead of this 7if type rc-service >/dev/null 2>&1 && rc-service --exists ntpd; then 8 ntpd_restart_cmd="rc-service ntpd -- --ifstarted --quiet restart" 9elif [ -x /etc/rc.d/ntpd ]; then 10 ntpd_restart_cmd="/etc/rc.d/ntpd restart" 11elif [ -x /usr/local/etc/rc.d/ntpd ]; then 12 ntpd_restart_cmd="/usr/local/etc/rc.d/ntpd restart" 13fi 14 15ntp_conf_dir="${state_dir}/ntp.conf" 16 17build_ntp_conf() 18{ 19 local cf="/etc/ntp.conf.${interface}" 20 local interfaces= header= srvs= servers= x= 21 22 # Build a list of interfaces 23 interfaces=$(list_interfaces "${ntp_conf_dir}") 24 25 if [ -n "${interfaces}" ]; then 26 # Build the header 27 for x in ${interfaces}; do 28 header="${header}${header:+, }${x}" 29 done 30 31 # Build a server list 32 srvs=$(cd "${ntp_conf_dir}"; 33 key_get_value "server " ${interfaces}) 34 if [ -n "${srvs}" ]; then 35 for x in $(uniqify ${srvs}); do 36 servers="${servers}server ${x}\n" 37 done 38 fi 39 fi 40 41 # Merge our config into ntp.conf 42 [ -e "${cf}" ] && rm -f "${cf}" 43 remove_markers "${signature_base}" "${signature_base_end}" \ 44 /etc/ntp.conf > "${cf}" 45 if [ -n "${servers}" ]; then 46 echo "${signature_base}${header:+ ${from} }${header}" >> "${cf}" 47 printf "${search}${servers}" >> "${cf}" 48 echo "${signature_base_end}${header:+ ${from} }${header}" >> "${cf}" 49 fi 50 51 # If we changed anything, restart ntpd 52 if change_file /etc/ntp.conf "${cf}"; then 53 [ -n "${ntpd_restart_cmd}" ] && ${ntpd_restart_cmd} 54 fi 55} 56 57add_ntp_conf() 58{ 59 local cf="${ntp_conf_dir}/${interface}" x= 60 61 [ -e "${cf}" ] && rm "${cf}" 62 [ -d "${ntp_conf_dir}" ] || mkdir -p "${ntp_conf_dir}" 63 if [ -n "${new_ntp_servers}" ]; then 64 for x in ${new_ntp_servers}; do 65 echo "server ${x}" >> "${cf}" 66 done 67 fi 68 build_ntp_conf 69} 70 71remove_ntp_conf() 72{ 73 if [ -e "${ntp_conf_dir}/${interface}" ]; then 74 rm "${ntp_conf_dir}/${interface}" 75 fi 76 build_ntp_conf 77} 78 79case "${reason}" in 80BOUND|INFORM|REBIND|REBOOT|RENEW|TIMEOUT) add_ntp_conf add;; 81PREINIT|EXPIRE|FAIL|IPV4LL|RELEASE|STOP) remove_ntp_conf del;; 82esac 83