Ticket #2247: wpa-service

File wpa-service, 4.3 KB (added by Eloi Primaux, 18 years ago)
Line 
1#!/bin/bash
2# Begin $network-devices/services/wpa-service
3# wpa-service 0.21
4# Based on dhcpcd script adapted for wpa networks with or without dhcp support
5# Rewritten by Eloi Primaux - eloi AT bliscat DOT org
6# 2006-02-20 First script
7# 2006-03-31 wpa-service is functional
8# 2006-08-21 version by Maarten Lankhorst - m.b.lankhorst.(@).gmail.com
9# 2006-10-15 0.2 release by Eloi Primaux
10# merging Maarten work into configure_network function
11# can use both wpa_cli -a function and ifplugd.
12# 2007-02-14 0.21 ifplugd support removed
13# wait_for_up removed
14# status* and available_network* functions removed
15
16IFACE=$1
17BRING=$2
18
19. /etc/sysconfig/rc
20. $rc_functions
21. $IFCONFIG
22. /etc/sysconfig/wpa_supplicant/wpa_service.conf
23
24function get_real_pid {
25 if [ -e $1 ]; then
26 RET=`fuser $1` &> /dev/null
27 else
28 return 1
29 fi
30}
31
32function wpa_is_up {
33 get_real_pid $WPA_GLOBAL_FILE
34 if [ $? != 0 ]; then
35 return 2
36 fi
37}
38
39function wpa {
40 wpa_is_up
41 if [ $? != 0 ]; then
42 verbose '' "$WPA_DAEMON_NAME isn't running, please check why"
43 if [ $BRING != "UP" ]; then
44 return 1
45 else
46 exit 1
47 fi
48 fi
49}
50
51function is_iface_not_managed {
52 [ -n "`get_real_pid $WPA_ACCESS_DIR/$IFACE`" ] && return 2
53}
54
55function verbose {
56 [ "$VERBOSE" == "YES" ] && boot_mesg $1 "$2"
57}
58
59function test_fail {
60 [ "$1" != 0 ] && RET="FAIL:$1"
61 verbose '' " $RET"
62 if [[ $RET = FAIL* ]]; then
63 echo_failure ; return 2
64 else
65 return $1
66 fi
67}
68
69function add_iface {
70 verbose -n "$WPA_CLIENT_NAME -g$WPA_GLOBAL_FILE interface_add $IFACE '' $WPA_DRIVER $WPA_ACCESS_DIR :"
71 RET=`$WPA_CLIENT_NAME -g$WPA_GLOBAL_FILE interface_add $IFACE '' $WPA_DRIVER $WPA_ACCESS_DIR` &> /dev/null
72 test_fail $?
73}
74
75function ctrl_iface {
76 verbose -n "$WPA_CLIENT_NAME -i$IFACE $@ :"
77 RET=`$WPA_CLIENT_NAME -i$IFACE $@` &> /dev/null
78 test_fail $?
79}
80
81function remove_iface {
82 verbose -n "$WPA_CLIENT_NAME -g$WPA_GLOBAL_FILE interface_remove $IFACE :"
83 RET=`$WPA_CLIENT_NAME -g$WPA_GLOBAL_FILE interface_remove $IFACE` &> /dev/null
84 test_fail $?
85}
86
87function configure_network {
88 #Read the wpa config file
89 LINENUMBER=0
90 CONFIGFILE="$WPA_CONFIG_DIR/$WPA_CONFIG_FILE"
91 LINES="$(wc -l $CONFIGFILE | sed 's/ .*//')"
92 while [ "$LINENUMBER" -lt "$LINES" ]
93 do
94 verbose '' "NETWORK=$NETWORK"
95 # Increase line number
96 let ++LINENUMBER
97 verbose '' "Parsing line ${LINENUMBER}"
98 # Fetch a line
99 preline="$(head -n $LINENUMBER $CONFIGFILE | tail -n 1)"
100 # Remove everything after a '#' (comment)
101 line="$(echo $preline | sed -e 's/#.*//')"
102
103 #echo "Parsing $LINENUMBER: '$preline' => '$line'"
104
105 if [ -z "`echo $line`" ]; then
106 # ignore now empty lines
107 continue
108 elif [ "$line" = "network={" ]; then
109 # creating a new network configuration,
110 # saving network number to NETWORK
111 ctrl_iface add_network
112 NETWORK="$RET"
113 elif [ "$line" = "}" ]; then
114 # now the network is configured, enabling it
115 ctrl_iface "enable_network $NETWORK"
116 unset NETWORK
117 elif [ -n "$( echo "$line" | grep -v "ctrl_interface")" ]; then
118 # all others lines should be network parameters...
119 # i need to replace the first '=' character by a pace
120 line="`echo "$line" | sed 's,=, ,'`"
121 verbose '' "ctrl_iface set_network $NETWORK $line"
122 RET="`$WPA_CLIENT_NAME -i $IFACE set_network $NETWORK $line`"
123 #ctrl_iface set_network $NETWORK $line
124 fi
125 if [[ $RET = FAIL* ]]; then
126 boot_mesg "Parse error on line ${LINENUMBER}"
127 remove_iface
128 echo_failure
129 return 2
130 fi
131 done
132}
133
134function wait_for_events {
135# This is a new wpa_supplicant function: really usefull
136# but requires an additional action file to handle CONNECTED/DISCONNECTED events
137 ctrl_iface -a$WPA_ACTION_FILE &
138}
139
140
141function real_fail {
142 [ "$1" != 0 ] && RET="FAIL:$1"
143 verbose '' " $RET"
144 if [[ $RET = FAIL* ]]; then
145 exit $1
146 else
147 return $1
148 fi
149}
150
151function wait_for_up {
152 wait_for_up_loop
153 real_fail $?
154 $WPA_ACTION_FILE $IFACE CONNECTED
155}
156
157
158function iface_up {
159 wpa
160 is_iface_not_managed
161 if [ $? = 0 ]; then
162 verbose '' "Interface already managed, continuing"
163 else
164 add_iface
165 real_fail $?
166 fi
167 configure_network
168 real_fail $?
169 wait_for_events
170 }
171
172
173function iface_down {
174 wpa
175 ctrl_iface disconnect
176 test_fail $?
177 verbose '' "wpa_cli -a will exit when interface will be removed"
178 $WPA_ACTION_FILE $IFACE DISCONNECTED
179 test_fail $?
180 wpa_cli -g$WPA_GLOBAL_FILE interface_remove $IFACE
181 test_fail $?
182}
183
184case "$2" in
185 up)
186 iface_up
187 ;;
188 down)
189 iface_down
190 ;;
191esac
192