/[baselayout]/trunk/net-scripts/net.modules.d/helpers.d/functions
Gentoo

Contents of /trunk/net-scripts/net.modules.d/helpers.d/functions

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1103 - (show annotations) (download)
Fri Apr 22 14:42:57 2005 UTC (14 years, 1 month ago) by uberlord
File size: 6726 byte(s)
Address network routes now get metrics applied as well
(ie a route automatically added when an address is added)

net.lo no longer errors when exlcuding modules who are installed

1 # Copyright (c) 2004-2005 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Header$
4
5 # Contributed by Roy Marples (uberlord@gentoo.org)
6
7 # char* interface_device(char *iface)
8 #
9 # Gets the base device of the interface
10 # Can handle eth0:1 and eth0.1
11 # Which returns eth0 in this case
12 interface_device() {
13 local dev=${1%%.*}
14 [[ ${dev} == ${1} ]] && dev=${1%%:*}
15 echo ${dev}
16 }
17
18 # char* interface_type(char* iface)
19 #
20 # Returns the base type of the interface
21 # eth, ippp, etc
22 interface_type() {
23 echo ${1%%[0-9]*}
24 }
25
26 # void save_state(char *interface)
27 #
28 # Saves state information regarding the interface
29 save_state() {
30 local iface="$1"
31 local d="${svcdir}/state/${iface}"
32
33 [[ ! -d ${d} ]] && mkdir -p "${d}"
34 cp -a /etc/resolv.conf /etc/ntp.conf /etc/yp.conf "${d}" 2>/dev/null
35 }
36
37 # void remove_state(char *interface)
38 #
39 # Removes state information regarding the interface
40 remove_state() {
41 local d="${svcdir}/state/$1"
42
43 [[ -d "${d}" ]] && rm -Rf "${d}"
44 }
45
46 # void apply_state(char *interface)
47 #
48 # Apply's state information about the interface to the system
49 #
50 apply_state() {
51 local iface
52 if [[ -z $1 ]]; then
53 iface=$( select_best_interface )
54 [[ -z ${iface} ]] && return
55 else
56 iface="$1"
57 fi
58
59 local d="${svcdir}/state/${iface}"
60 [[ -d ${d} ]] && cp -aR ${d}/* /etc
61 }
62
63 # char* select_best_interface() {
64 #
65 # Selects the best interface to apply state information to
66 # This is currently based on routing metrics
67 select_best_interface() {
68 awk '$2=="00000000" { print $7, $1 }' /proc/net/route | sort -n | head -n 1 | cut -d' ' -f2
69 }
70
71 # int calculate_metric(char *interface)
72 #
73 # Calculates the best metric for the interface
74 # The Linux kernel does not use this at the moment, but we use it so that
75 # default routes remain and we can work out the "best" interface
76 calculate_metric() {
77 local iface="$1" exclude='$1!="Iface" && $1!="lo"'
78
79 # Have we already got a metric?
80 local m=$( awk '$1=="'${iface}'" && $2=="00000000" { print $7 }' /proc/net/route )
81 if [[ -n ${m} ]]; then
82 echo ${m}
83 return 0
84 fi
85
86 local itype=$( interface_type "${iface}" ) x i
87
88 # If we're not a wireless device then exclude wireless from the
89 # routing table so we stay < 1000
90 if [[ -e /proc/net/wireless ]]; then
91 if ! grep -q "${iface}: " /proc/net/wireless ; then
92 local i=$( sed -n -e 's/^ *\(.*\):.*/\1/p' /proc/net/wireless )
93 for x in ${i} ; do
94 exclude="${exclude} && "'$1'"!=\"${x}\""
95 done
96 fi
97 fi
98
99 # Exclude ppp and ippp as well
100 local ix="ppp|ippp"
101 [[ ${itype} == "ppp" ]] && ix="ippp"
102 [[ ${itype} == "ippp" ]] && ix="ppp"
103 i=$( sed -n -e 's/^[ ]*\('${ix}'[0-9]*\):.*$/\1/p' /proc/net/dev )
104 for x in ${i} ; do
105 exclude="${exclude} && "'$1'"!=\"${x}\""
106 done
107
108 local m=$( awk "${exclude} { print "'$7'" }" /proc/net/route | sort -rn | head -n 1 | cut -d' ' -f2 )
109 m=${m:--1}
110 (( m ++ ))
111
112 # If we're a wireless device then add 1000 so that wired interfaces take preference
113 if [[ -e /proc/net/wireless ]]; then
114 grep -q "${iface}:" /proc/net/wireless && (( m+= 1000 ))
115 fi
116
117 # If we're a ppp device then we add 2000 for ISDN, otherwise 3000
118 [[ ${itype} == "ippp" ]] && (( m+= 2000 ))
119 [[ ${itype} == "ppp" ]] && (( m+= 3000 ))
120
121 echo ${m}
122 }
123
124 # int netmask2cidr(char *netmask)
125 #
126 # Returns the CIDR of a given netmask
127 netmask2cidr() {
128 local binary="" i bin
129
130 for i in ${1//./ }; do
131 bin=""
132 while [[ ${i} != 0 ]]; do
133 bin=$[${i}%2]${bin}
134 (( i=i>>1 ))
135 done
136 binary="${binary}${bin}"
137 done
138 binary=${binary%%0*}
139 echo ${#binary}
140 }
141
142 # char* netmask2cidr(int cidr)
143 #
144 # Returns the netmask of a given CIDR
145 cidr2netmask() {
146 local cidr=${1} netmask="" done=0 i sum=0 cur=128
147 local octets frac
148
149 (( octets=cidr/8 ))
150 (( frac=cidr%8 ))
151 while [[ octets -gt 0 ]]; do
152 netmask="${netmask}.255"
153 (( octets-- ))
154 (( done++ ))
155 done
156
157 if [[ ${done} -lt 4 ]]; then
158 for (( i=0; i<${frac}; i++ )); do
159 (( sum+=cur ))
160 (( cur/=2 ))
161 done
162 netmask="${netmask}.${sum}"
163 (( done++ ))
164
165 while [[ ${done} -lt 4 ]]; do
166 netmask="${netmask}.0"
167 (( done++ ))
168 done
169 fi
170
171 echo ${netmask:1}
172 }
173
174 # char* ip_network(char *ip, char *netmask)
175 #
176 # Returns the network of the ip address
177 # ip can be 192.168.0.51/24
178 # or
179 # ip can be 192.168.0.51 and netmask is 255.255.255.0
180 ip_network() {
181 local ip=$1 mask=$2 i network x
182
183 # If we didn't get parameter 2 then assume we have a CIDR
184 if [[ -z ${mask} ]]; then
185 mask=${ip##*/}
186 [[ -z ${mask} || ${mask} == ${ip} ]] && return 1
187 mask=$( cidr2netmask ${mask} )
188 ip=${ip%%/*}
189 fi
190
191 ip=( ${ip//./ } )
192 mask=( ${mask//./ } )
193
194 for (( i=0; i<4; i++ )); do
195 (( x=ip[i] & mask[i] ))
196 network="${network}${x}"
197 [[ ${i} -lt 3 ]] && network="${network}."
198 done
199
200 echo "${network}"
201 }
202
203
204 # bool clean_pidfile(char *file)
205 #
206 # Removes the given pidfile if the process is not running
207 # Returns 1 if the process is still running otherwise 0
208 clean_pidfile() {
209 local pidfile=${1}
210
211 [[ ! -f ${pidfile} ]] && return 0
212 local pid=$( < ${pidfile} )
213
214 if [[ -n ${pid} ]]; then
215 local cmd=${pidfile##*/}
216 cmd=${cmd%%-*}
217 ps -p ${pid} 2>/dev/null | grep -q ${cmd} && return 1
218 fi
219
220 rm -f ${pidfile}
221 return 0
222 }
223
224 # bool process_finished(int pid, char* cmd)
225 #
226 # We wait for 10 seconds until the command ${cmd}
227 # stops running on the process ${pid}
228 process_finished() {
229 local i pid=$1 cmd=$2 secs=${3:-9}
230
231 for (( i=0; i<secs; i++ )); do
232 ps -p ${pid} 2>/dev/null | grep -q ${cmd} || return 0
233 sleep 1
234 done
235
236 return 1
237 }
238
239 # void function_wrap(char* source, char* target)
240 #
241 # wraps function calls - for example function_wrap(this, that)
242 # maps function names this_* to that_*
243 function_wrap() {
244 local i
245
246 [[ $( type -t ${2}_provides ) == function ]] && return
247
248 for i in $( typeset -f | grep -o ^${1}_'[^ ]*' ); do
249 eval "${2}${i#${1}}() { ${i} \"\$@\"; }"
250 done
251 }
252
253 # Currently sorucing rc-services.sh causes problems, so we replicate
254 # code here when needed
255 if [[ $( type -t mark_service_inactive ) != "function" ]]; then
256 svcdir=/var/lib/init.d
257 mark_service_inactive() {
258 [[ -z $1 ]] && return 1
259 ln -snf /etc/init.d/$1 ${svcdir}/inactive/$1
260 return $?
261 }
262 mark_service_stopped() {
263 [[ -z $1 ]] && return 1
264 [[ -f ${svcdir}/starting/$1 ]] && rm -f ${svcdir}/starting/$1
265 [[ -f ${svcdir}/started/$1 ]] && rm -f ${svcdir}/started/$1
266 [[ -f ${svcdir}/inactive/$1 ]] && rm -f ${svcdir}/inactive/$1
267 return $?
268 }
269 mark_service_started() {
270 [[ -z $1 ]] && return 1
271 ln -snf /etc/init.d/${1} ${svcdir}/started/$1
272 [[ -f ${svcdir}/starting/$1 ]] && rm -f ${svcdir}/starting/$1
273 [[ -f ${svcdir}/inactive/$1 ]] && rm -f ${svcdir}/inactive/$1
274 return $?
275 }
276 fi
277
278 if [[ $( type -t bash_variable ) != "function" ]]; then
279 bash_variable() {
280 local argv="$@"
281 LC_ALL=C echo "${argv//[![:word:]]/_}"
282 }
283 fi

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.20