sample: eliminate clever code to determine which proc file is accessed
[madwifi/.git] / scripts / madwifi-unload
1 #!/bin/sh
2
3 : ${PATTERN='\(ath_.*\|wlan_.*\|wlan\)$'}
4 : ${MAX_TRIES=10}
5
6 test "$(id -u)" = 0 || {
7         echo "ERROR: You must be root to run this script" >&2
8         exit 1
9 }
10
11 test -r /proc/modules || {
12         echo "ERROR: Cannot read /proc/modules" >&2
13         exit 1
14 }
15
16 tries="$MAX_TRIES"
17 while test "$tries" != "0"; do
18         skipped=0
19         IFS='
20 '
21         for line in $(cat /proc/modules); do
22                 IFS='   '
23                 set x $line
24                 name="$2"
25                 size="$3"
26                 use_count="$4"
27                 use_name="$5"
28                 state="$6"
29                 expr "$name" : "$PATTERN" >/dev/null || continue
30
31                 if test "$state" != "Live" || test "$use_count" != "0" || \
32                    test "$use_name" != "-"; then
33                         # Don't skip unload in the last run
34                         if test "$tries" != "1"; then
35                                 skipped=1
36                                 continue
37                         fi
38                 fi
39
40                 echo "Unloading \"$name\""
41                 sync    # to be safe
42                 /sbin/rmmod "$name" || {
43                         echo "ERROR: cannot unload module \"$name\"" >&2
44                         exit 1
45                 }
46                 sync    # to be even safer
47         done
48         test "$skipped" = "0" && break
49         tries=$(($tries - 1))
50 done
51
52 exit 0