| OLD | NEW |
| (Empty) |
| 1 #!/bin/sh | |
| 2 # dhcpcd client configuration script | |
| 3 | |
| 4 # Handy variables and functions for our hooks to use | |
| 5 from=from | |
| 6 signature_base="# Generated by dhcpcd" | |
| 7 signature="$signature_base $from $interface" | |
| 8 signature_base_end="# End of dhcpcd" | |
| 9 signature_end="$signature_base_end $from $interface" | |
| 10 state_dir=/var/run/dhcpcd | |
| 11 | |
| 12 # Ensure that all arguments are unique | |
| 13 uniqify() | |
| 14 { | |
| 15 local result= i= | |
| 16 for i; do | |
| 17 case " $result " in | |
| 18 *" $i "*);; | |
| 19 *) result="$result $i";; | |
| 20 esac | |
| 21 done | |
| 22 echo "${result# *}" | |
| 23 } | |
| 24 | |
| 25 # List interface config files in a dir | |
| 26 # If dhcpcd is running as a single instance then it will have a list of | |
| 27 # interfaces in the preferred order. | |
| 28 # Otherwise we just use what we have. | |
| 29 list_interfaces() | |
| 30 { | |
| 31 local i= x= ifaces= | |
| 32 for i in $interface_order; do | |
| 33 [ -e "$1/$i" ] && ifaces="$ifaces${ifaces:+ }$i" | |
| 34 done | |
| 35 for x in "$1"/*; do | |
| 36 [ -e "$x" ] || continue | |
| 37 for i in $interface_order; do | |
| 38 if [ $i = "${x##*/}" ]; then | |
| 39 unset x | |
| 40 break | |
| 41 fi | |
| 42 done | |
| 43 [ -n "$x" ] && ifaces="$ifaces${ifaces:+ }${x##*/}" | |
| 44 done | |
| 45 echo "$ifaces" | |
| 46 } | |
| 47 | |
| 48 # We normally use sed to extract values using a key from a list of files | |
| 49 # but sed may not always be available at the time. | |
| 50 key_get_value() | |
| 51 { | |
| 52 local key="$1" value= x= line= | |
| 53 | |
| 54 shift | |
| 55 if type sed >/dev/null 2>&1; then | |
| 56 sed -n "s/^$key//p" $@ | |
| 57 else | |
| 58 for x; do | |
| 59 while read line; do | |
| 60 case "$line" in | |
| 61 "$key"*) echo "${line##$key}";; | |
| 62 esac | |
| 63 done < "$x" | |
| 64 done | |
| 65 fi | |
| 66 } | |
| 67 | |
| 68 # We normally use sed to remove markers from a configuration file | |
| 69 # but sed may not always be available at the time. | |
| 70 remove_markers() | |
| 71 { | |
| 72 local m1="$1" m2="$2" x= line= in_marker=0 | |
| 73 | |
| 74 shift; shift | |
| 75 if type sed >/dev/null 2>&1; then | |
| 76 sed "/^$m1/,/^$m2/d" $@ | |
| 77 else | |
| 78 for x; do | |
| 79 while read line; do | |
| 80 case "$line" in | |
| 81 "$m1"*) in_marker=1;; | |
| 82 "$m2"*) in_marker=0;; | |
| 83 *) [ $in_marker = 0 ] && echo "$line";; | |
| 84 esac | |
| 85 done < "$x" | |
| 86 done | |
| 87 fi | |
| 88 } | |
| 89 | |
| 90 # Compare two files | |
| 91 # If different, replace first with second otherwise remove second | |
| 92 change_file() | |
| 93 { | |
| 94 if type cmp >/dev/null 2>&1; then | |
| 95 cmp -s "$1" "$2" | |
| 96 elif type diff >/dev/null 2>&1; then | |
| 97 diff -q "$1" "$2" >/dev/null | |
| 98 else | |
| 99 # Hopefully we're only working on small text files ... | |
| 100 [ "$(cat "$1")" = "$(cat "$2")" ] | |
| 101 fi | |
| 102 if [ $? -eq 0 ]; then | |
| 103 rm -f "$2" | |
| 104 return 1 | |
| 105 fi | |
| 106 cat "$2" > "$1" | |
| 107 rm -f "$2" | |
| 108 return 0 | |
| 109 } | |
| 110 | |
| 111 # Save a config file | |
| 112 save_conf() | |
| 113 { | |
| 114 if [ -f "$1" ]; then | |
| 115 rm -f "$1-pre.$interface" | |
| 116 cat "$1" > "$1-pre.$interface" | |
| 117 fi | |
| 118 } | |
| 119 | |
| 120 # Restore a config file | |
| 121 restore_conf() | |
| 122 { | |
| 123 [ -f "$1-pre.$interface" ] || return 1 | |
| 124 cat "$1-pre.$interface" > "$1" | |
| 125 rm -f "$1-pre.$interface" | |
| 126 } | |
| 127 | |
| 128 # Write a syslog entry | |
| 129 syslog() | |
| 130 { | |
| 131 local lvl="$1" | |
| 132 | |
| 133 [ -n "$lvl" ] && shift | |
| 134 if [ -n "$@" ]; then | |
| 135 if type logger >/dev/null 2>&1; then | |
| 136 logger -t dhcpcd -p daemon."$lvl" -s "$@" | |
| 137 fi | |
| 138 fi | |
| 139 } | |
| 140 | |
| 141 | |
| 142 # We source each script into this one so that scripts run earlier can | |
| 143 # remove variables from the environment so later scripts don't see them. | |
| 144 # Thus, the user can create their dhcpcd.enter/exit-hook script to configure | |
| 145 # /etc/resolv.conf how they want and stop the system scripts ever updating it. | |
| 146 for hook in \ | |
| 147 @SYSCONFDIR@/dhcpcd.enter-hook \ | |
| 148 @HOOKDIR@/* \ | |
| 149 @SYSCONFDIR@/dhcpcd.exit-hook | |
| 150 do | |
| 151 for skip in $skip_hooks; do | |
| 152 case "$hook" in | |
| 153 */"$skip") continue 2;; | |
| 154 */[0-9][0-9]"-$skip") continue 2;; | |
| 155 */[0-9][0-9]"-$skip.sh") continue 2;; | |
| 156 esac | |
| 157 done | |
| 158 if [ -f "$hook" ]; then | |
| 159 . "$hook" | |
| 160 fi | |
| 161 done | |
| OLD | NEW |