diff options
Diffstat (limited to 'dhcpcd-run-hooks.in')
-rw-r--r-- | dhcpcd-run-hooks.in | 167 |
1 files changed, 113 insertions, 54 deletions
diff --git a/dhcpcd-run-hooks.in b/dhcpcd-run-hooks.in index a848260..cb897b4 100644 --- a/dhcpcd-run-hooks.in +++ b/dhcpcd-run-hooks.in @@ -2,38 +2,54 @@ # dhcpcd client configuration script # Handy variables and functions for our hooks to use -from="from" +from=from signature_base="# Generated by dhcpcd" -signature="${signature_base} ${from} ${interface}" +signature="$signature_base $from $interface" signature_base_end="# End of dhcpcd" -signature_end="${signature_base_end} ${from} ${interface}" -state_dir="/var/run/dhcpcd" +signature_end="$signature_base_end $from $interface" +state_dir=/var/run/dhcpcd + +if_up=false +if_down=false +case "$reason" in +BOUND|INFORM|REBIND|REBOOT|RENEW|TIMEOUT|STATIC) if_up=true;; +PREINIT|EXPIRE|FAIL|IPV4LL|NAK|NOCARRIER|RELEASE|STOP) if_down=true;; +esac # Ensure that all arguments are unique uniqify() { - local result= - - while [ -n "$1" ]; do - case " ${result} " in - *" $1 "*);; - *) result="${result}${result:+ }$1";; + local result= i= + for i; do + case " $result " in + *" $i "*);; + *) result="$result $i";; esac - shift done - echo "${result}" + echo "${result# *}" } -# List interface config files in a dir -# We may wish to control the order at some point rather than just lexical +# List interface config files in a directory. +# If dhcpcd is running as a single instance then it will have a list of +# interfaces in the preferred order. +# Otherwise we just use what we have. list_interfaces() { - local x= interfaces= + local i= x= ifaces= + for i in $interface_order; do + [ -e "$1/$i" ] && ifaces="$ifaces${ifaces:+ }$i" + done for x in "$1"/*; do - [ -e "${x}" ] || continue - interfaces="${interfaces}${interfaces:+ }${x##*/}" + [ -e "$x" ] || continue + for i in $interface_order; do + if [ $i = "${x##*/}" ]; then + unset x + break + fi + done + [ -n "$x" ] && ifaces="$ifaces${ifaces:+ }${x##*/}" done - echo "${interfaces}" + echo "$ifaces" } # We normally use sed to extract values using a key from a list of files @@ -44,14 +60,14 @@ key_get_value() shift if type sed >/dev/null 2>&1; then - sed -n "s/^${key}//p" $@ + sed -n "s/^$key//p" $@ else for x; do while read line; do - case "${line}" in - "${key}"*) echo "${line##${key}}";; + case "$line" in + "$key"*) echo "${line##$key}";; esac - done < "${x}" + done < "$x" done fi } @@ -64,37 +80,40 @@ remove_markers() shift; shift if type sed >/dev/null 2>&1; then - sed "/^${m1}/,/^${m2}/d" $@ + sed "/^$m1/,/^$m2/d" $@ else for x; do while read line; do - case "${line}" in - "${m1}"*) in_marker=1;; - "${m2}"*) in_marker=0;; - *) [ ${in_marker} = 0 ] && echo "${line}";; + case "$line" in + "$m1"*) in_marker=1;; + "$m2"*) in_marker=0;; + *) [ $in_marker = 0 ] && echo "$line";; esac - done < "${x}" + done < "$x" done fi } -# Compare two files -# If different, replace first with second otherwise remove second +# Compare two files. +# If different, replace first with second otherwise remove second. change_file() { - 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 - # Hopefully we're only working on small text files ... - [ "$(cat "$1")" = "$(cat "$2")" ] - fi - if [ $? -eq 0 ]; then - rm -f "$2" - return 1 + if [ -e "$1" ]; then + 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 + # Hopefully we're only working on small text files ... + [ "$(cat "$1")" = "$(cat "$2")" ] + fi + if [ $? -eq 0 ]; then + rm -f "$2" + return 1 + fi fi - mv -f "$2" "$1" + cat "$2" > "$1" + rm -f "$2" return 0 } @@ -102,19 +121,59 @@ change_file() save_conf() { if [ -f "$1" ]; then - rm -f "$1"-pre."${interface}" - mv -f "$1" "$1"-pre."${interface}" + rm -f "$1-pre.$interface" + cat "$1" > "$1-pre.$interface" fi } # Restore a config file restore_conf() { - [ -f "$1"-pre."${interface}" ] || return 1 - rm -f "$1" - mv -f "$1"-pre."${interface}" "$1" + [ -f "$1-pre.$interface" ] || return 1 + cat "$1-pre.$interface" > "$1" + rm -f "$1-pre.$interface" +} + +# Write a syslog entry +syslog() +{ + local lvl="$1" + + [ -n "$lvl" ] && shift + if [ -n "$*" ]; then + if type logger >/dev/null 2>&1; then + logger -t dhcpcd -p daemon."$lvl" -s "$*" + fi + fi +} + +# Check a system service exists +service_exists() +{ + @SERVICEEXISTS@ +} + +# Send a command to a system service +service_cmd() +{ + @SERVICECMD@ +} + +# Send a command to a system service if it is running +service_status() +{ + @SERVICESTATUS@ } +# Handy macros for our hooks +service_command() +{ + service_exists $1 && service_cmd $1 $2 +} +service_condcommand() +{ + service_exists $1 && service_status $1 && service_cmd $1 $2 +} # 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. @@ -125,14 +184,14 @@ for hook in \ @HOOKDIR@/* \ @SYSCONFDIR@/dhcpcd.exit-hook do - for skip in ${skip_hooks}; do - case "${hook}" in - */"${skip}") continue 2;; - */[0-9][0-9]"-${skip}") continue 2;; - */[0-9][0-9]"-${skip}.sh") continue 2;; + for skip in $skip_hooks; do + case "$hook" in + */"$skip") continue 2;; + */[0-9][0-9]"-$skip") continue 2;; + */[0-9][0-9]"-$skip.sh") continue 2;; esac done - if [ -f "${hook}" ]; then - . "${hook}" + if [ -f "$hook" ]; then + . "$hook" fi done |