]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Add a framework for top an tailing config files with DHCP data and a method for clean...
authorRoy Marples <roy@marples.name>
Wed, 13 Aug 2008 12:07:27 +0000 (12:07 +0000)
committerRoy Marples <roy@marples.name>
Wed, 13 Aug 2008 12:07:27 +0000 (12:07 +0000)
dhcpcd-hooks/20-resolv.conf
dhcpcd-hooks/50-ntp.conf
dhcpcd-hooks/50-yp.conf
dhcpcd-run-hooks.in

index b377a67bf07c01f0332cc494b59929c7522192df..437c116778c4f2a861e2b6a3a7f7d08306758cc3 100644 (file)
@@ -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
index 818b4dd4c141da0d35ec4a04b76b418fbc6b962b..78c5147cde1e5b7c27f8fe065ba13d07eb68c677 100644 (file)
@@ -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
index a82bd5d7c2407302a08529c3e0c22803a32bbf09..a2296ebfe4d957ae91bd56f84991052e3361f93c 100644 (file)
@@ -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
index 94d9dfc48e80baeb75e4327ee5eebb81ead6c331..ecd9aa98d87aae0e2ac73cdb694a88ee1f8064c5 100644 (file)
@@ -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