keep track of the network interface states (stored in uci format in /var/state/networ...
[openwrt-10.03/.git] / package / base-files / files / sbin / ifdown
1 #!/bin/sh
2 # Copyright (C) 2006 OpenWrt.org
3
4 . /etc/functions.sh
5 [ $# = 0 ] && { echo "  $0 <group>"; exit; }
6 [ "x$1" = "x-a" ] && {
7         config_cb() {
8                 [ interface != "$1" -o -z "$2" ] || eval "$0 $2"
9         }
10         config_load network
11         exit
12 }
13
14 # remove the interface's network state
15 FILE=/var/state/network.$$
16 grep -v "^config_set '$1' " /var/state/network > "$FILE"
17 mv "$FILE" /var/state/network
18
19 include /lib/network
20 scan_interfaces
21
22 cfg=$1
23 debug "### ifdown $cfg ###"
24
25 config_get proto "$cfg" proto
26 [ -z "$proto" ] && { echo "interface not found."; exit; }
27
28 config_get iface "$cfg" device
29 [ "static" = "$proto" ] && {
30         env -i ACTION="ifdown" INTERFACE="$cfg" DEVICE="$iface" PROTO=static /sbin/hotplug-call "iface" &
31 }
32
33 # call interface stop handler
34 ( type "stop_interface_$proto" ) >/dev/null 2>/dev/null && eval "stop_interface_$proto '$cfg'"
35
36 # make sure all locks are removed
37 for lock in "/var/lock/dhcp-$iface" "/var/lock/ppp-$iface"; do
38         [ -f "$lock" ] && {
39                 lock -u "$lock"
40                 sleep 1
41         }
42 done
43
44 # kill active ppp daemon and other processes
45 config_get ifname "$cfg" ifname
46 pids="$(head -n1 -q /var/run/${ifname}.pid /var/run/ppp-${cfg}.pid 2>/dev/null)"
47 for pid in $pids; do 
48         [ -d "/proc/$pid" ] && {
49                 kill $pid
50                 [ -d "/proc/$pid" ] && {
51                         sleep 1
52                         kill -9 $pid 2>/dev/null >/dev/null
53                 }
54         }
55 done
56 rm -f /var/run/${ifname}.pid /var/run/ppp-${cfg}.pid
57
58 config_get ifname "$cfg" ifname
59 config_get device "$cfg" device
60
61 [ ."$device" != ."$ifname" ] || device=
62 for dev in $ifname $device; do
63         ifconfig "$dev" 0.0.0.0 down >/dev/null 2>/dev/null
64 done
65
66 config_get iftype "$cfg" type
67 [ "bridge" = "$iftype" ] && brctl delbr "$ifname" >/dev/null 2>/dev/null
68