diff options
| author | Roy Marples <roy@marples.name> | 2008-08-13 12:07:27 +0000 |
|---|---|---|
| committer | Roy Marples <roy@marples.name> | 2008-08-13 12:07:27 +0000 |
| commit | 91309576bd255aadb15e88d8fb3a67ffe1c3854d (patch) | |
| tree | 179fc7f9b873d5f29529f8103d0d8af8c2420d0d /dhcpcd-run-hooks.in | |
| parent | 16ceedbb2da1112848b71b58f617d1c3bb94e0e7 (diff) | |
| download | dhcpcd-91309576bd255aadb15e88d8fb3a67ffe1c3854d.tar.xz | |
Add a framework for top an tailing config files with DHCP data and a method for cleaning them.
Diffstat (limited to 'dhcpcd-run-hooks.in')
| -rw-r--r-- | dhcpcd-run-hooks.in | 69 |
1 files changed, 68 insertions, 1 deletions
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 |
