use lock to remove race conditions in bringing up ppp and dhcp that would lead to...
[librecmc/librecmc.git] / package / base-files / default / sbin / ifdown
1 #!/bin/sh
2 # Copyright (C) 2006 OpenWrt.org
3
4 [ $# = 0 ] && { echo "  $0 <group>"; exit; }
5 . /etc/functions.sh
6 include /lib/network
7 scan_interfaces
8
9 cfg=$1
10 debug "### ifdown $cfg ###"
11
12 config_get proto "$cfg" proto
13 [ -z "$proto" ] && { echo "interface not found."; exit; }
14
15 # make sure all locks are removed
16 for lock in "/var/lock/dhcp-$iface" "/var/lock/ppp-$iface"; do
17         [ -f "$lock" ] && {
18                 lock -u "$lock"
19                 sleep 1
20         }
21 done
22
23 # kill active ppp daemon and other processes
24 config_get ifname "$cfg" ifname
25 pids="$(cat /var/run/${ifname}.pid /var/run/ppp-${cfg}.pid 2>/dev/null)"
26 for pid in $pids; do 
27         [ -d "/proc/$pid" ] && {
28                 kill $pid
29                 [ -d "/proc/$pid" ] && {
30                         sleep 1
31                         kill -9 $pid 2>/dev/null >/dev/null
32                 }
33         }
34 done
35 rm -f /var/run/${ifname}.pid /var/run/ppp-${cfg}.pid
36
37 config_get ifname "$cfg" ifname
38 config_get device "$cfg" ifname
39
40 [ ."$device" != ."$ifname" ] || device=
41 for dev in $ifname $device; do
42         ifconfig "$ifname" 0.0.0.0 down >/dev/null 2>/dev/null
43 done
44
45 config_get iftype "$cfg" type
46 [ "$iftype" = "bridge" ] && brctl delbr "$ifname" >/dev/null 2>/dev/null
47