| ---
common.sh (3212B)
---
1 #!/bin/sh
2
3 . /etc/conn/config.sh
4
5 [ $DEBUG -eq 1 ] && set -x
6
7 ## Common dirs
8 WIFIDIR="${ETCDIR}/wifi"
9 WWANDIR="${ETCDIR}/wwan"
10 ETHDIR="${ETCDIR}/eth"
11
12 mkpaths() {
13 for i in $RUNDIR $STATEDIR;
14 do
15 [ ! -e $i ] && mkdir -p $i
16 done
17 }
18
19 ## Locking in bash
20 lock() {
21 LOCKF="$1.lock"
22 lock=0
23
24 [ -e $LOCKF ] && lock=1
25
26 i=0
27 while [ $lock -eq 1 ]
28 do
29 [ ! -e $LOCKF ] && lock=0
30 sleep 0.5;
31
32 i=`expr $i + 1`
33 if [ $i -gt 100 ];
34 then
35 echo "It took too long to aquire lock $1"
36 exit 1
37 fi
38 done
39
40 touch $LOCKF
41 }
42
43 unlock() {
44 LOCKF="$1.lock"
45 [ -e $LOCKF ] && rm $LOCKF
46 }
47
48 ## Global conn lock, when there is database access.
49 CONNLOCKF="$STATEDIR/conn"
50 lockconn() {
51 lock $CONNLOCKF
52 }
53
54 unlockconn() {
55 unlock $CONNLOCKF
56 }
57
58 ## State handling
59 getstate() {
60 [ ! -e $STATEDIR/$1 ] && return 1
61 [ `stat -c%s $STATEDIR/$1` -eq 0 ] && return 1
62 cat $STATEDIR/$1
63 return 0
64 }
65
66 getstates() {
67 getstate $1 | egrep "$2|$3"
68 return 0
69 }
70
71 isset() {
72 isin=`getstate $1 | grep "$2|$3"`
73 [ -z "$isin" ] && return 1
74 return 0
75 }
76
77 addstate() {
78 isset $1 $2 $3 && return 1
79 echo "$2|$3" >> $STATEDIR/$1
80 }
81
82 delstate() {
83 [ ! -e $STATEDIR/$1 ] && return 1
84
85 if ! isset $1 $2 $3;
86 then
87 return 1
88 else
89 cat $STATEDIR/$1 | grep -v "$2|$3" \
90 > $STATEDIR/$1.bak
91 mv $STATEDIR/$1.bak $STATEDIR/$1
92 fi
93 }
94
95 getdefaultstate() {
96 case "$1" in
97 open)
98 echo "$DEFAULTCONNECTION $DEFAULTPROFILE"
99 ;;
100 *)
101 ;;
102 esac
103 }
104
105 ## WPA handling
106
107 startwpa() {
108 PID=`pgrep -f "$WPACMD -i $1"`
109 [ -z "$PID" ] && $WPACMD -i $1
110 PID=`pgrep -f "$WPACLICMD -i $1 -a $WIFIDIR/$1-action.sh -B"`
111 [ -z "$PID" ] && $WPACLICMD -i $1 -a $WIFIDIR/$1-action.sh -B \
112 2>&1 >/dev/null &
113 }
114
115 hupwpa() {
116 PID=`pgrep -f "$WPACMD -i $1"`
117 [ ! -z "$PID" ] && kill -HUP $PID
118 }
119
120 stopwpa() {
121 pkill -KILL -f "$WPACMD -i $1"
122 pkill -KILL -f "$WPACLICMD -i $1 -a $WIFIDIR/$1-action.sh -B"
123 }
124
125 iswparunning() {
126 if [ -n "$(pgrep -f "$WPACMD -i $1")" ];
127 then
128 return 1
129 else
130 return 0
131 fi
132 }
133
134 getssid() {
135 $WPACLIREQ -i $1 list_networks \
136 | awk -F'\t' "/\[CURRENT\]$/ {print \$2}"
137 }
138
139 ## DHCP handling
140 startdhcp() {
141 $DHCPRELEASECMD $1 2>&1 >/dev/null
142 $DHCPCMD $1 2>&1 >/dev/null
143 }
144
145 stopdhcp() {
146 $DHCPKILLCMD $1 2>&1 >/dev/null
147 }
148
149 isdhcprunning() {
150 if [ -n "$(pgrep -f "$DHCPCMD $1")" ];
151 then
152 return 1
153 else
154 return 0
155 fi
156 }
157
158 ## Connection handling
159 runconnection() {
160 SRVDIR="${ETCDIR}/$1"
161 SRVRUN="${ETCDIR}/$1/run.sh"
162 shift 1
163 if [ -e $SRVRUN ];
164 then
165 cd $SRVDIR
166 sh $SRVRUN $*
167 fi
168 }
169
170 ## Interface handling
171 PINGPID="${RUNDIR}/pingd.pid"
172
173 startpingd() {
174 ping -q $PINGHOST 2>&1 >/dev/null &
175 echo $! > $PINGPID.$1
176 }
177
178 stoppingd() {
179 if [ -e $PINGPID.$1 ];
180 then
181 /bin/kill -KILL `cat $PINGPID.$1`
182 rm $PINGPID.$1
183 fi
184 }
185
186 getinterface() {
187 basename $1 | awk -F'-' '{print $1}'
188 }
189
190 ## Link handling
191 setlinkup() {
192 ip link set $1 up
193 }
194
195 setlinkdown() {
196 ip link set $1 down
197 }
198
199 islinkup() {
200 state=`ip link show $1 2>/dev/null | grep ',UP'`
201 if [ -z "$state" ];
202 then
203 return 1
204 else
205 return 0
206 fi
207 }
208
209 ## Rfkill handling
210 isblocked() {
211 res=`rfkill list $1 | grep ' yes'`
212 if [ -z "$res" ];
213 then
214 return 1
215 else
216 return 0
217 fi
218 }
219
220 doblock() {
221 if ! isblocked $1;
222 then
223 rfkill block $1
224 fi
225 }
226
227 dounblock() {
228 if isblocked $1;
229 then
230 rfkill unblock $1
231 fi
232 }
233 |