approach. It's more work, but the end result looks cleaner.
# Also, resolvconf can configure local nameservers such as bind
# or dnsmasq. This is important as the libc resolver isn't that powerful.
-# We store our resolvconfs in /var/run so they are cleaned at boot.
-resolvconf_dir="${state_dir}/resolv.conf"
+resolv_conf_dir="${state_dir}/resolv.conf"
build_resolv_conf()
{
local cf="/etc/resolv.conf.${interface}"
- local header="${signature_base}"
- local interfaces= search= srvs= servers= x=
+ local interfaces= header= search= srvs= servers= x=
# Build a list of interfaces
- for x in "${resolvconf_dir}"/*; do
- [ -e "${x}" ] || continue
- [ -n "${interfaces}" ] && header="${header}, "
- header="${header}${x##*/}"
- interfaces="${interfaces}${interfaces:+ }${x##*/}"
- done
+ interfaces=$(list_interfaces "${resolv_conf_dir}")
+ # Build the resolv.conf
if [ -n "${interfaces}" ]; then
- # Build a search list
- search=$(cd "${resolvconf_dir}";
+ # Build the header
+ for x in ${interfaces}; do
+ header="${header}${header:+, }${x}"
+ done
+
+ # Build the search list
+ search=$(cd "${resolv_conf_dir}";
sed -n 's/^search //p' ${interfaces})
[ -n "${search}" ] && search="search $(uniqify ${search})\n"
- # Build a nameserver list
- srvs=$(cd "${resolvconf_dir}";
+ # Build the nameserver list
+ srvs=$(cd "${resolv_conf_dir}";
sed -n 's/^nameserver //p' ${interfaces})
if [ -n "${srvs}" ]; then
for x in $(uniqify ${srvs}); do
done
fi
fi
+ header="${signature_base}${header:+ ${from} }${header}"
- # Build our final resolv.conf
+ # Assemble resolv.conf using our head and tail files
[ -f "${cf}" ] && rm -f "${cf}"
echo "${header}" > "${cf}"
if [ -f /etc/resolv.conf.head ]; then
mv -f "${cf}" /etc/resolv.conf
}
-make_resolv_conf()
+add_resolv_conf()
{
+ local x= conf="${signature}\n"
+
+ # If we don't have any configuration, remove it
if [ -z "${new_domain_name_servers}" -a \
-z "${new_domain_name}" -a \
-z "${new_domain_search}" ]; then
- return 0
+ remove_resolv_conf
+ return $?
fi
- local x= conf="${signature}\n"
+
if [ -n "${new_domain_search}" ]; then
conf="${conf}search ${new_domain_search}\n"
elif [ -n "${new_domain_name}" ]; then
return $?
fi
- if [ ! -d "${resolvconf_dir}" ]; then
- mkdir -p "${resolvconf_dir}"
+ if [ -e "${resolv_conf_dir}/${interface}" ]; then
+ rm -f "${resolv_conf_dir}/${interface}"
fi
- if [ -e "${resolvconf_dir}/${interface}" ]; then
- rm -f "${resolvconf_dir}/${interface}"
+ if [ ! -d "${resolv_conf_dir}" ]; then
+ mkdir -p "${resolv_conf_dir}"
fi
- printf "${conf}" > "${resolvconf_dir}/${interface}"
+ printf "${conf}" > "${resolv_conf_dir}/${interface}"
build_resolv_conf
}
-restore_resolv_conf()
+remove_resolv_conf()
{
if type resolvconf >/dev/null 2>&1; then
resolvconf -d "${interface}" -f
else
- if [ -e "${resolvconf_dir}/${interface}" ]; then
- rm -f "${resolvconf_dir}/${interface}"
+ if [ -e "${resolv_conf_dir}/${interface}" ]; then
+ rm -f "${resolv_conf_dir}/${interface}"
fi
build_resolv_conf
fi
}
case "${reason}" in
-BOUND|INFORM|REBIND|REBOOT|RENEW|TIMEOUT) make_resolv_conf;;
-PREINIT|EXPIRE|FAIL|IPV4LL|RELEASE|STOP) restore_resolv_conf;;
+BOUND|INFORM|REBIND|REBOOT|RENEW|TIMEOUT) add_resolv_conf;;
+PREINIT|EXPIRE|FAIL|IPV4LL|RELEASE|STOP) remove_resolv_conf;;
esac
# Sample dhcpcd hook script for ntp
+# Like our resolv.conf hook script, we store a database of ntp.conf files
+# and merge into /etc/ntp.conf
# Detect OpenRC or BSD rc
# Distributions may want to just have their command here instead of this
ntpd_restart_cmd="/usr/local/etc/rc.d/ntpd restart"
fi
-do_ntp_conf()
+ntp_conf_dir="${state_dir}/ntp.conf"
+
+build_ntp_conf()
{
- local cleaned= added=1 conf= x=
+ local cf="/etc/ntp.conf.${interface}"
+ local interfaces= header= srvs= servers= x=
- clean_conf /etc/ntp.conf
- cleaned=$?
- if [ "$1" = "add" -a -n "${new_ntp_servers}" ]; then
- for x in ${new_ntp_servers}; do
- conf="${conf:+\n}server ${x}"
+ # Build a list of interfaces
+ interfaces=$(list_interfaces "${ntp_conf_dir}")
+
+ if [ -n "${interfaces}" ]; then
+ # Build the header
+ for x in ${interfaces}; do
+ header="${header}${header:+, }${x}"
done
- append_conf /etc/ntp.conf "${conf}"
- added=$?
+
+ # Build a server list
+ srvs=$(cd "${ntp_conf_dir}";
+ sed -n 's/^server //p' ${interfaces})
+ if [ -n "${srvs}" ]; then
+ for x in $(uniqify ${srvs}); do
+ servers="${servers}server ${x}\n"
+ done
+ fi
fi
- if [ ${cleaned} -eq 0 -o ${added} -eq 0 ]; then
+
+ # Merge our config into ntp.conf
+ [ -e "${cf}" ] && rm -f "${cf}"
+ sed "/^${signature_base}/,/^${signature_base_end}/d" \
+ /etc/ntp.conf > "${cf}"
+ if [ -n "${servers}" ]; then
+ echo "${signature_base}${header:+ ${from} }${header}" >> "${cf}"
+ printf "${search}${servers}" >> "${cf}"
+ echo "${signature_base_end}${header:+ ${from} }${header}" >> "${cf}"
+ fi
+
+ # If we changed anything, restart ntpd
+ if change_file /etc/ntp.conf "${cf}"; then
[ -n "${ntpd_restart_cmd}" ] && ${ntpd_restart_cmd}
fi
}
+add_ntp_conf()
+{
+ local cf="${ntp_conf_dir}/${interface}" x=
+
+ [ -e "${cf}" ] && rm "${cf}"
+ [ -d "${ntp_conf_dir}" ] || mkdir -p "${ntp_conf_dir}"
+ if [ -n "${new_ntp_servers}" ]; then
+ for x in ${new_ntp_servers}; do
+ echo "server ${x}" >> "${cf}"
+ done
+ fi
+ build_ntp_conf
+}
+
+remove_ntp_conf()
+{
+ if [ -e "${ntp_conf_dir}/${interface}" ]; then
+ rm "${ntp_conf_dir}/${interface}"
+ fi
+ build_ntp_conf
+}
+
case "${reason}" in
-PREINIT) clean_conf /etc/ntp.conf;;
-BOUND|INFORM|REBIND|REBOOT|RENEW|TIMEOUT) do_ntp_conf add;;
-EXPIRE|FAIL|IPV4LL|RELEASE|STOP) do_ntp_conf del;;
+BOUND|INFORM|REBIND|REBOOT|RENEW|TIMEOUT) add_ntp_conf add;;
+PREINIT|EXPIRE|FAIL|IPV4LL|RELEASE|STOP) remove_ntp_conf del;;
esac
# dhcpcd client configuration script
# Handy variables and functions for our hooks to use
-signature_base="# Generated by dhcpcd from "
-signature="${signature_base}${interface}"
-signature_base_end="# End of dhcpcd from "
-signature_end="${signature_base_end}${interface}"
+from="from"
+signature_base="# Generated by dhcpcd"
+signature="${signature_base} ${from} ${interface}"
+signature_base_end="# End of dhcpcd"
+signature_end="${signature_base_end} ${from} ${interface}"
state_dir="/var/run/dhcpcd"
# Ensure that all arguments are unique
echo "${result}"
}
-# Clean a configuration file of our current signature and stale ones
-clean_conf()
+# List interface config files in a dir
+# We may wish to control the order at some point rather than just lexical
+list_interfaces()
{
- local cf=$1 cft="$1.tmp" x= m1= m2=
-
- if [ -f "${cf}" ]; then
- # Remove our old entry
- m1="^${signature}$"
- m2="^${signature_end}$"
- rm -f "${cft}" "${cft}.tmp"
- sed "/${m1}/,/${m2}/d" "${cf}" > "${cft}"
- # Remove stale entries
- m1="^${siganture_base} "
- for x in $(sed -n "s/${m1}//p" "${cft}"); do
- if [ ! -s /var/run/dhcpcd-${x}.pid ]; then
- m1="^${signtaure_base}${x}$"
- m2="^${signature_base_end}${x}$"
- sed "/${m1}/,/${m2}/d" "${cft}" >"${cft}".tmp
- mv -f "${cft}".tmp "${cft}"
- fi
- done
- # If files are identical then don't replace and return 1
- # to show that no cleaning took place
- if type cmp >/dev/null 2>&1; then
- cmp -s "${cf}" "${cft}"
- elif type diff >/dev/null 2>&1; then
- diff -q "${cf}" "${cft}" >/dev/null
- else
- false
- fi
- if [ $? -eq 0 ]; then
- rm -f "${cft}"
- return 1
- fi
- mv -f "${cft}" "${cf}"
- return 0
- fi
-}
-
-# Append our config to the end of a file, surrouned by our signature
-append_conf()
-{
- echo "${signature}" >> "$1"
- echo "$2" >> "$1"
- echo "${signature_end}" >> "$1"
+ local x= interfaces=
+ for x in "$1"/*; do
+ [ -e "${x}" ] || continue
+ interfaces="${interfaces}${interfaces:+ }${x##*/}"
+ done
+ echo "${interfaces}"
}
-# Prepend our config to the start of a file, surrouned by our signature
-prepend_conf()
+# Compare two files
+# It different, replace first with second otherwise remove second
+change_file()
{
- rm -f "$1.${interface}"
- echo "${signature}" > "$1.${interface}"
- echo "$2" >> "$1.${interface}"
- echo "${signature_end}" >> "$1.${interface}"
- cat "$1" >> "$1.${interface}"
- mv -f "$1.${interface}" "$1"
+ if type cmp >/dev/null 2>&1; then
+ cmp -s "$1" "$2}"
+ elif type diff >/dev/null 2>&1; then
+ diff -q "$1" "$2" >/dev/null
+ else
+ false
+ fi
+ if [ $? -eq 0 ]; then
+ rm -f "$2"
+ return 1
+ fi
+ mv -f "$2" "$1"
+ return 0
}
# Save a config file