fix hotplug events for static (#1051)
[openwrt/svn-archive/archive.git] / openwrt / package / base-files / default / etc / functions.sh
1 #!/bin/sh
2 # Copyright (C) 2006 OpenWrt.org
3 # Copyright (C) 2006 Fokus Fraunhofer <carsten.tittel@fokus.fraunhofer.de>
4
5 alias debug=${DEBUG:-:}
6
7 # valid interface?
8 if_valid () (
9 ifconfig "$1" >&- 2>&- ||
10 [ "${1%%[0-9]}" = "br" ] ||
11 {
12 [ "${1%%[0-9]*}" = "vlan" ] && (
13 i=${1#vlan}
14 hwname=$(nvram get vlan${i}hwname)
15 hwaddr=$(nvram get ${hwname}macaddr)
16 [ -z "$hwaddr" ] && return 1
17
18 vif=$(ifconfig -a | awk '/^eth.*'$hwaddr'/ {print $1; exit}' IGNORECASE=1)
19 debug "# vlan$i => $vif"
20
21 $DEBUG ifconfig $vif up
22 $DEBUG vconfig add $vif $i 2>&-
23 )
24 } ||
25 { debug "# missing interface '$1' ignored"; false; }
26 )
27
28 do_ifup() {
29 ifproto="$1"
30 iftype="$2"
31 if="$(nvram get ${iftype}_ifname)"
32 [ "${if%%[0-9]}" = "ppp" ] && if="$(nvram get ${iftype}_device)"
33
34 pidfile=/var/run/${if}.pid
35 [ -f $pidfile ] && $DEBUG kill $(cat $pidfile)
36
37 case "$ifproto" in
38 static)
39 ip=$(nvram get ${iftype}_ipaddr)
40 netmask=$(nvram get ${iftype}_netmask)
41 gateway=$(nvram get ${iftype}_gateway)
42 mtu=$(nvram get ${iftype}_mtu)
43 static_route=$(nvram get ${iftype}_static_route)
44
45 $DEBUG ifconfig $if $ip ${netmask:+netmask $netmask} ${mtu:+mtu $(($mtu))} broadcast + up
46 ${gateway:+$DEBUG route add default gw $gateway}
47
48 [ -f /tmp/resolv.conf.auto ] || {
49 debug "# --- creating /tmp/resolv.conf.auto ---"
50 for dns in $(nvram get ${iftype}_dns); do
51 echo "nameserver $dns" >> /tmp/resolv.conf.auto
52 done
53 }
54
55 [ -n "$static_route" ] && {
56 for route in $static_route; do {
57 eval "set $(echo $route | sed 's/:/ /g')"
58 if [ "$2" = "255.255.255.255" ]; then
59 opt="-host"
60 fi
61 $DEBUG route add ${opt:-"-net"} $1 netmask $2 gw $3 metric $4
62 } done
63 }
64
65 env -i ACTION="ifup" INTERFACE="${iftype}" PROTO=static /sbin/hotplug "iface" &
66 ;;
67 dhcp*)
68 DHCP_IP=$(nvram get ${iftype}_ipaddr)
69 DHCP_NETMASK=$(nvram get ${iftype}_netmask)
70 mtu=$(nvram get ${iftype}_mtu)
71 $DEBUG ifconfig $if $DHCP_IP ${DHCP_NETMASK:+netmask $DHCP_NETMASK} ${mtu:+mtu $(($mtu))} broadcast + up
72
73 DHCP_ARGS="-i $if ${DHCP_IP:+-r $DHCP_IP} -b -p $pidfile -t 0"
74 DHCP_HOSTNAME=$(nvram get ${iftype}_hostname)
75 DHCP_HOSTNAME=${DHCP_HOSTNAME%%.*}
76 [ -z $DHCP_HOSTNAME ] || DHCP_ARGS="$DHCP_ARGS -H $DHCP_HOSTNAME"
77 [ "$(nvram get ${iftype}_proto)" = "pptp" ] && DHCP_ARGS="$DHCP_ARGS -n -q" || DHCP_ARGS="$DHCP_ARGS -R &"
78 [ -r $pidfile ] && oldpid=$(cat $pidfile 2>&-)
79 ${DEBUG:-eval} "udhcpc $DHCP_ARGS"
80 [ -n "$oldpid" ] && pidof udhcpc | grep "$oldpid" >&- 2>&- && {
81 sleep 1
82 kill -9 $oldpid
83 }
84 # hotplug events are handled by /usr/share/udhcpc/default.script
85 ;;
86 none|"")
87 ;;
88 *)
89 [ -x "/sbin/ifup.$ifproto" ] && { $DEBUG /sbin/ifup.$ifproto ${iftype}; exit; }
90 echo "### ifup ${iftype}: ignored ${iftype}_proto=\"$ifproto\" (not supported)"
91 ;;
92 esac
93 }
94
95 # newline
96 N="
97 "
98
99 _C=0
100
101 hotplug_dev() {
102 env -i ACTION=$1 INTERFACE=$2 /sbin/hotplug net
103 }
104
105 append() {
106 local var="$1"
107 local value="$2"
108 local sep="${3:- }"
109
110 eval "export -n -- \"$var=\${$var:+\${$var}\${value:+\$sep}}\$value\""
111 }
112
113 reset_cb() {
114 config_cb() { return 0; }
115 option_cb() { return 0; }
116 }
117 reset_cb
118
119 config () {
120 local cfgtype="$1"
121 local name="$2"
122
123 CONFIG_NUM_SECTIONS=$(($CONFIG_NUM_SECTIONS + 1))
124 name="${name:-cfg$CONFIG_NUM_SECTIONS}"
125 append CONFIG_SECTIONS "$name"
126 config_cb "$cfgtype" "$name"
127 CONFIG_SECTION="$name"
128 export -n "CONFIG_${CONFIG_SECTION}_TYPE=$cfgtype"
129 }
130
131 option () {
132 local varname="$1"; shift
133 local value="$*"
134
135 export -n "CONFIG_${CONFIG_SECTION}_${varname}=$value"
136 option_cb "$varname" "$*"
137 }
138
139 config_rename() {
140 local OLD="$1"
141 local NEW="$2"
142 local oldvar
143 local newvar
144
145 [ "$OLD" -a "$NEW" ] || return
146 for oldvar in `set | grep ^CONFIG_${OLD}_ | \
147 sed -e 's/\(.*\)=.*$/\1/'` ; do
148 newvar="CONFIG_${NEW}_${oldvar##CONFIG_${OLD}_}"
149 eval "export -n \"$newvar=\${$oldvar}\""
150 unset "$oldvar"
151 done
152 CONFIG_SECTIONS="$(echo " $CONFIG_SECTIONS " | sed -e "s, $OLD , $NEW ,")"
153
154 [ "$CONFIG_SECTION" = "$OLD" ] && CONFIG_SECTION="$NEW"
155 }
156
157 config_unset() {
158 config_set "$1" "$2" ""
159 }
160
161 config_clear() {
162 local SECTION="$1"
163 local oldvar
164
165 CONFIG_SECTIONS="$(echo " $CONFIG_SECTIONS " | sed -e "s, $OLD , ,")"
166 CONFIG_SECTIONS="${SECTION:+$CONFIG_SECTIONS}"
167
168 for oldvar in `set | grep ^CONFIG_${SECTION:+$SECTION_} | \
169 sed -e 's/\(.*\)=.*$/\1/'` ; do
170 unset $oldvar
171 done
172 }
173
174 config_load() {
175 local file="/etc/config/$1"
176 _C=0
177 CONFIG_SECTIONS=
178 CONFIG_NUM_SECTIONS=0
179 CONFIG_SECTION=
180
181 [ -e "$file" ] && {
182 . $file
183 } || return 1
184
185 ${CONFIG_SECTION:+config_cb}
186 }
187
188 config_get() {
189 case "$3" in
190 "") eval "echo \"\${CONFIG_${1}_${2}}\"";;
191 *) eval "export -n -- \"$1=\${CONFIG_${2}_${3}}\"";;
192 esac
193 }
194
195 config_set() {
196 local section="$1"
197 local option="$2"
198 local value="$3"
199 export -n "CONFIG_${section}_${option}=$value"
200 }
201
202 config_foreach() {
203 local function="$1"
204 local section
205
206 [ -z "$CONFIG_SECTIONS" ] && return 0
207 for section in ${CONFIG_SECTIONS}; do
208 eval "$function \"\$section\""
209 done
210 }
211
212 load_modules() {
213 cd /etc/modules.d
214 sed 's/^[^#]/insmod &/' $* | ash 2>&- || :
215 }
216
217 include() {
218 local file
219
220 for file in $(ls $1/*.sh 2>/dev/null); do
221 . $file
222 done
223 }
224
225 find_mtd_part() {
226 local PART="$(grep "\"$1\"" /proc/mtd | awk -F: '{print $1}')"
227
228 PART="${PART##mtd}"
229 echo "${PART:+/dev/mtdblock/$PART}"
230 }
231
232 strtok() { # <string> { <variable> [<separator>] ... }
233 local tmp
234 local val="$1"
235 local count=0
236
237 shift
238
239 while [ $# -gt 1 ]; do
240 tmp="${val%%$2*}"
241
242 [ "$tmp" = "$val" ] && break
243
244 val="${val#$tmp$2}"
245
246 export -n "$1=$tmp"; count=$((count+1))
247 shift 2
248 done
249
250 if [ $# -gt 0 -a "$val" ]; then
251 export -n "$1=$val"; count=$((count+1))
252 fi
253
254 return $count
255 }
256
257 set_led() {
258 local led="$1"
259 local state="$2"
260 [ -f "/proc/diag/led/$1" ] && echo "$state" > "/proc/diag/led/$1"
261 }
262
263 set_state() {
264 case "$1" in
265 preinit)
266 set_led dmz 1
267 set_led diag 1
268 set_led power 0
269 ;;
270 failsafe)
271 set_led diag f
272 set_led power f
273 set_led dmz f
274 ;;
275 done)
276 set_led dmz 0
277 set_led diag 0
278 set_led power 1
279 ;;
280 esac
281 }