do_ntp_conf()
{
- local cf=/etc/ntp.conf."${interface}" x= m1= m2=
- local sig="# Generated by dhcpcd for interface"
- local sig_end="# End of dhcpcd content for interface"
+ local cleaned= added=1 conf= x=
- if [ -f /etc/ntp.conf ]; then
- # Remove our old entry
- m1="^${sig} ${interface}$"
- m2="^${sig_end} ${interface}$"
- sed "/${m1}/,/${m2}/d" /etc/ntp.conf > "${cf}"
- # Remove stale entries
- m1="^${sig} "
- for x in $(sed -n "s/${m1}//p" "${cf}"); do
- if [ ! -s /var/run/dhcpcd-${x}.pid ]; then
- m1="^${sig} ${x}$"
- m2="^${sig_end} ${x}$"
- sed "/${m1}/,/${m2}/d" "${cf}" >"${cf}".tmp
- mv -f "${cf}".tmp "${cf}"
- fi
- done
- else
- rm -f "${cf}"
- fi
+ clean_conf /etc/ntp.conf
+ cleaned=$?
if [ "$1" = "add" -a -n "${new_ntp_servers}" ]; then
- echo "${sig} ${interface}" >> "${cf}"
for x in ${new_ntp_servers}; do
- echo "server ${x}" >> "${cf}"
+ conf="${conf:+\n}server ${x}"
done
- echo "${sig_end} ${interface}" >> "${cf}"
+ append_conf /etc/ntp.conf "${conf}"
+ added=0
fi
- if [ -f "${cf}" ]; then
- mv -f "${cf}" /etc/ntp.conf
+ if [ ${cleaned} -eq 0 -o ${added} -eq 0 ]; then
[ -n "${ntpd_restart_cmd}" ] && ${ntpd_restart_cmd}
fi
}
case "${reason}" in
-BOUND|INFORM|REBIND|REBOOT|RENEW|TIMEOUT) do_ntp_conf add;;
+BOUND|INFORM|REBIND|REBOOT|RENEW|TIMEOUT) do_ntp_conf add;;
EXPIRE|FAIL|IPV4LL|RELEASE|STOP) do_ntp_conf del;;
esac
#!/bin/sh
# dhcpcd client configuration script
-# Handy functions for our hooks to use
+# Handy variables functions for our hooks to use
+signature_base="# Generated by dhcpcd for interface "
+signature="${signature_base}${interface}"
+signature_base_end="# End of dhcpcd content for interface "
+signature_end="${signature_base_end}${interface}"
+
+# Clean a configuration file of our current signature and stale ones
+clean_conf()
+{
+ 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"
+}
+
+# Prepend our config to the start of a file, surrouned by our signature
+prepend_conf()
+{
+ 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"
+}
+
+# Save a config file
save_conf()
{
if [ -f "$1" ]; then
mv -f "$1" "$1"-pre."${interface}"
fi
}
+
+# Restore a config file
restore_conf()
{
[ -f "$1"-pre."${interface}" ] || return 1
mv -f "$1"-pre."${interface}" "$1"
}
+
# We source each script into this one so that scripts run earlier can
# remove variables from the environment so later scripts don't see them.
# Thus, the user can create their dhcpcd.hook script to configure