From: Roy Marples Date: Wed, 13 Aug 2008 12:07:27 +0000 (+0000) Subject: Add a framework for top an tailing config files with DHCP data and a method for clean... X-Git-Tag: v4.0.2~54 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=91309576bd255aadb15e88d8fb3a67ffe1c3854d;p=thirdparty%2Fdhcpcd.git Add a framework for top an tailing config files with DHCP data and a method for cleaning them. --- diff --git a/dhcpcd-hooks/20-resolv.conf b/dhcpcd-hooks/20-resolv.conf index b377a67b..437c1167 100644 --- a/dhcpcd-hooks/20-resolv.conf +++ b/dhcpcd-hooks/20-resolv.conf @@ -8,7 +8,7 @@ make_resolv_conf() -z "${new_domain_search}" ]; then return 0 fi - local x= conf="# Generated by dhcpcd for interface ${interface}\n" + local x= conf="${signature}\n" if [ -n "${new_domain_search}" ]; then conf="${conf}search ${new_domain_search}\n" elif [ -n "${new_domain_name}" ]; then diff --git a/dhcpcd-hooks/50-ntp.conf b/dhcpcd-hooks/50-ntp.conf index 818b4dd4..78c5147c 100644 --- a/dhcpcd-hooks/50-ntp.conf +++ b/dhcpcd-hooks/50-ntp.conf @@ -12,42 +12,23 @@ fi 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 diff --git a/dhcpcd-hooks/50-yp.conf b/dhcpcd-hooks/50-yp.conf index a82bd5d7..a2296ebf 100644 --- a/dhcpcd-hooks/50-yp.conf +++ b/dhcpcd-hooks/50-yp.conf @@ -10,7 +10,8 @@ make_yp_conf() { [ -z "${new_nis_domain}" -a -z "${new_nis_servers}" ] && return 0 local cf=/etc/yp.conf."${interface}" prefix= x= pid= - echo "# Generated by dhcpcd for interface ${interface}" > "${cf}" + rm -f "${cf}" + echo "${signature}" > "${cf}" if [ -n "${new_nis_domain}" ]; then domainname "${new_nis_domain}" if [ -n "${new_nis_servers}" ]; then diff --git a/dhcpcd-run-hooks.in b/dhcpcd-run-hooks.in index 94d9dfc4..ecd9aa98 100644 --- a/dhcpcd-run-hooks.in +++ b/dhcpcd-run-hooks.in @@ -1,7 +1,71 @@ #!/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 @@ -9,6 +73,8 @@ save_conf() mv -f "$1" "$1"-pre."${interface}" fi } + +# Restore a config file restore_conf() { [ -f "$1"-pre."${interface}" ] || return 1 @@ -16,6 +82,7 @@ restore_conf() 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