/[baselayout]/trunk/net-scripts/init.d/net.lo
Gentoo

Contents of /trunk/net-scripts/init.d/net.lo

Parent Directory Parent Directory | Revision Log Revision Log


Revision 545 - (hide annotations) (download)
Fri Sep 10 15:53:24 2004 UTC (8 years, 8 months ago) by agriffis
File size: 13738 byte(s)
update to net-scripts-0.6.2

1 agriffis 532 #!/sbin/runscript
2     # net-scripts main code
3 agriffis 545 # Version 0.6.2
4     # Copyright (c) 2004 Gentoo Foundation
5     # Distributed under the terms of the GNU General Public License V2
6     # Contributed by Roy "UberLord" Marples (uberlord@rsm.demon.co.uk)
7 agriffis 532
8     #NB: Config is in /etc/conf.d/net
9    
10     if [[ -n $NET_DEBUG ]]; then
11     set -x
12     devnull=/dev/stderr
13     else
14     devnull=/dev/null
15     fi
16    
17     # For pcmcia users. note that pcmcia must be added to the same
18     # runlevel as the net.* script that needs it.
19     depend() {
20     use hotplug pcmcia usb bridge
21     }
22    
23 agriffis 545 # Make some wrappers to fudge after/before/need/use depend flags
24     after() {
25     eval "${mods[i]}_after() { echo \"$@\"; }"
26 agriffis 535 }
27 agriffis 545 before() {
28     eval "${mods[i]}_before() { echo \"$@\"; }"
29     }
30     need() {
31     eval "${mods[i]}_need() { echo \"$@\"; }"
32     }
33     use() {
34     eval "${mods[i]}_user() { echo \"$@\"; }"
35     }
36 agriffis 535
37 agriffis 545 sort() {
38     LC_ALL=C /bin/sort "$@"
39 agriffis 535 }
40    
41 agriffis 532 # bool modules_load(char *iface, bool all)
42     #
43     # Loads the defined handler and modules for the interface
44     # Returns 0 on success, otherwise 1
45     modules_load() {
46 agriffis 545 local iface=${1} all=${2} i j k p um e before after diffs mod
47     local -a mods=( $( find /etc/net.modules.d/ -maxdepth 1 -type f | sort ) ) umods
48 agriffis 532
49 agriffis 545 all=${all:-false}
50 agriffis 532 [[ lo != ${iface} ]] && ebegin "Loading networking scripts"
51    
52 agriffis 534 # Each of these sources into the global namespace, so it's
53     # important that module functions and variables are prefixed with
54     # the module name, for example iproute2_
55 agriffis 535 before=$( typeset -f )
56 agriffis 545 j=${#mods[@]}
57     k=false
58     for ((i = 0; i < j; i++ )); do
59 agriffis 532 source ${mods[i]} || {
60 agriffis 545 eerror " ${mods[i]##*/} failed a sanity check - will not load"
61     unset mods[i]
62     continue
63 agriffis 532 }
64     mods[i]=${mods[i]##*/}
65 agriffis 545 if [[ interface == ${mods[i]} ]]; then
66     eerror " interface is a reserved name - cannot load a module called interface"
67     return 1
68     fi
69 agriffis 535
70 agriffis 545 e=false
71     for p in check_installed provides check_depends depend; do
72     [[ function == $(type -t ${mods[i]}_${p}) ]] && continue
73     eerror " ${mods[i]} does not support the required function ${p}\n"
74     e=true
75 agriffis 534 done
76 agriffis 545 [[ true == ${e} ]] && return 1
77 agriffis 535
78 agriffis 545 # Wrap our before/after/need/use functions
79     ${mods[i]}_depend
80    
81 agriffis 534 # Double check that we've only *added* to the function list, not
82     # replaced. This is as much protection for devs as for users ;-)
83 agriffis 535 after=$( typeset -f )
84 agriffis 545
85     # Though diffutils is in the profiles, don't depend that it will exist
86     # Currently we cannot tell which function got modified as bash can do nested functions .... :/
87     diffs=$( diff -up <(echo "${before}") <(echo "${after}") 2>/dev/null | awk '{if ($1 == "-") print "changed"}' | xargs )
88     if [[ -n ${diffs} ]]; then
89     eerror " ${mods[i]} modified at least one existing function"
90     return 1
91 agriffis 532 fi
92 agriffis 534 before="${after}"
93 agriffis 545
94     if ! ${mods[i]}_check_installed; then
95     unset mods[i]
96     continue
97     fi
98    
99     [[ -n ${mods[i]} && interface == $( ${mods[i]}_provides ) ]] && k=true
100 agriffis 532 done
101    
102 agriffis 545 if [[ false == k ]]; then
103     eerror " no interface module has been loaded"
104     return 1
105     fi
106    
107 agriffis 532 mods=( "${mods[@]}" )
108    
109 agriffis 545 if [[ true != ${all} ]]; then
110 agriffis 532 # Has the interface got any specific modules?
111     eval umods=( \"\$\{modules_${iface}\[@\]\}\" )
112    
113     # If not then has a global setting been made?
114     [[ -z ${umods} ]] && eval umods=( \"\$\{modules\[@\]\}\" )
115    
116 agriffis 545 # We add our preferred modules here
117 agriffis 535 umods=( "${umods[@]}" "ifconfig" "dhcpcd" )
118    
119 agriffis 532 # First we strip any modules that conflict from user settings
120     # So if the user specifies pump then we don't use dhcpcd
121 agriffis 545 for ((i = 0; i < ${#umods[@]}; i++)); do
122     if [[ function != $( type -t ${umods[i]}_provides ) ]]; then
123 agriffis 535 eerror "${umods[i]} is not a valid module (missing provides)"
124 agriffis 532 return 1
125 agriffis 545 fi
126    
127     #Ensure that the user specified module has it's package installed
128     if [[ ${e} -lt ${#umods[@]}-2 ]]; then
129     ${umods[i]}_check_installed true || return 1
130     fi
131    
132     k=${#mods[@]}
133     for ((j = 0; j < k; j++)); do
134 agriffis 535 if [[ $( ${mods[j]}_provides ) == $( ${umods[i]}_provides ) && ${umods[i]} != ${mods[j]} ]]; then
135 agriffis 545 # We don't have a match - now ensure that we still provide an alternative
136     # This is to handle our preferred modules
137     for ((e=0; e < k; e++)); do
138     [[ ${e} -eq ${j} || -z ${mods[e]} ]] && continue
139     if [[ $( ${mods[e]}_provides ) == $( ${umods[i]}_provides ) ]]; then
140     unset mods[j]
141     break
142     fi
143     done
144 agriffis 532 fi
145     done
146 agriffis 545 mods=( "${mods[@]}" )
147 agriffis 535 done
148    
149     # Then we strip conflicting modules
150 agriffis 545 # We only need todo this for 3rd party modules that conflict with
151     # our own modules and the preferred list AND the user modules
152     # list doesn't specify a preferrence
153     k=${#mods[@]}
154     for ((i = 0; i < k - 1; i++)); do
155     [[ -z ${mods[i]} ]] && continue
156     for ((j = i + 1; j < k; j++ )); do
157     [[ -z ${mods[j]} ]] && continue
158 agriffis 535 [[ $( ${mods[i]}_provides ) == $( ${mods[j]}_provides ) ]] && unset mods[j]
159     done
160 agriffis 532 done
161 agriffis 545 mods=( "${mods[@]}" )
162 agriffis 532 fi
163    
164 agriffis 545 # Sort our modules
165     # We do this by assigning numbers to each module
166     umods=()
167     for ((i=0; i < ${#mods[@]}; i++)); do
168     umods=( "${umods[@]}" "${i}" )
169     done
170    
171     # Then we swap numbers based on and after/before flags
172     # until we don't swap anymore. Yes, there's the potential for
173     # an infinite loop if module developers are clumsy
174     while [[ true ]]; do
175     um=false
176     for ((i=0; i < ${#mods[@]}; i++)); do
177     if [[ function == $( type -t ${mods[i]}_after ) ]]; then
178     for p in $( ${mods[i]}_after | sort ); do
179     for ((j=0; j < ${#mods[@]}; j++)); do
180     [[ ${p} != ${mods[j]} && ${p} != $( ${mods[j]}_provides ) ]] && continue
181     if [[ ${umods[i]} -lt ${umods[j]} ]]; then
182     k=${umods[i]}
183     umods[i]=${umods[j]}
184     umods[j]=${k}
185     um=true
186     fi
187     done
188     done
189     fi
190     if [[ function == $( type -t ${mods[i]}_before ) ]]; then
191     for p in $( ${mods[i]}_before | sort ); do
192     for ((j=0; j < ${#mods[@]}; j++)); do
193     [[ ${p} != ${mods[j]} && ${p} != $( ${mods[j]}_provides ) ]] && continue
194     if [[ ${umods[i]} -gt ${umods[j]} ]]; then
195     k=${umods[i]}
196     umods[i]=${umods[j]}
197     umods[j]=${k}
198     um=true;
199     fi
200     done
201     done
202     fi
203     done
204     [[ false == ${um} ]] && break
205     done
206    
207     # Finally we sort our modules in number order and volia :)
208     um=""
209     for ((i=0; i < ${#mods[@]}; i++)); do
210     um="${um}${umods[i]} ${mods[i]}\n"
211     done
212     MODULES=( $( echo -e ${um} | sort | awk '{print $2}' ) )
213    
214     [[ lo != ${iface} ]] && einfo " modules: ${MODULES[@]}"
215    
216 agriffis 534 # Setup class wrappers: interface_up -> iproute2_up, for example
217 agriffis 545 for i in ${MODULES[@]}; do
218 agriffis 535 p=$( ${i}_provides )
219     [[ ${p} == ${i} ]] && continue;
220     for j in $(typeset -f | grep -o ^${i}_'[^ ]*' ); do
221     eval "${p}${j#${i}}() { ${j} \"\$@\"; }"
222 agriffis 534 done
223     done
224    
225     # Ensure that all modules have what they need
226 agriffis 535 # from either other modules or the system
227 agriffis 532 for i in ${MODULES[@]}; do
228 agriffis 545 if [[ function == $( type -t ${i}_need ) ]]; then
229     for m in $( ${i}_need ); do
230     um=false
231     for j in ${MODULES[@]}; do
232     p=$( ${j}_provides )
233     if [[ ${m} == ${j} || ${m} == $( ${j}_provides ) ]]; then
234     um=true;
235     break;
236     fi
237     done
238     if [[ false == ${um} ]]; then
239     eerror "${i} needs ${m} (dependency failure)"
240     return 1
241     fi
242     done
243     fi
244 agriffis 532 ${i}_check_depends || return 1
245     e=$( ${i}_provides )
246 agriffis 535 if [[ true != ${all} && lo != ${iface} && ${e} != ${i} ]]; then
247     einfo " ${i} provides ${e}"
248     fi
249 agriffis 532 done
250     }
251    
252     # char* get_device(char *iface)
253     #
254     # Gets the base device of the interface
255     # Can handle eth0:1 and eth0.1
256     # Which returns eth0 in this case
257     get_device() {
258     local iface=${1}
259     local dev=${iface%%.*}
260     [[ ${dev} == ${iface} ]] && dev=${iface%%:*}
261     echo ${dev}
262     }
263    
264     # bool iface_start(char *interface)
265     #
266     # iface_start is called from start. It's expected to start the base
267     # interface (for example "eth0"), aliases (for example "eth0:1") and to start
268     # VLAN interfaces (for example eth0.0, eth0.1). VLAN setup is accomplished by
269     # calling itself recursively.
270     iface_start() {
271     local iface=${1} mod i label x conf
272    
273     # Localise and initialise variables
274     for mod in ${MODULES[@]}; do
275     x="${mod}_local_vars"
276     if [[ function == $( type -t ${x} ) ]]; then
277     x=$( ${x} )
278     ${x}
279     fi
280     x="${mod}_setup_vars"
281     if [[ function == $( type -t ${x} ) ]]; then
282     ${x} ${iface} || return 1
283     fi
284     done
285    
286     if [[ -z ${config_IFACE} ]]; then
287     eerror "configuration has not been set!"
288     return 1
289     fi
290    
291     ebegin "Bringing ${iface} up"
292     for ((i = 0; i < ${#config_IFACE[@]}; i++)); do
293     # Set a label for the alias if $i > 0
294     [[ ${i} -eq 0 ]] && label=${iface} || label=${iface}:${i}
295    
296     # We convert it to an array - this has the added
297     # bonus of trimming spaces!
298     conf=( ${config_IFACE[i]} )
299     einfo " ${label} ${conf[0]}"
300    
301     # Do we have a function for our config?
302     if [[ function == $( type -t ${conf[0]}_start ) ]]; then
303     ${conf[0]}_start ${label} && continue
304     if [[ -n ${config_fallback_IFACE[i]} ]]; then
305     einfo " Trying fallback configuration"
306     config_IFACE[i]=${config_fallback_IFACE[i]}
307     config_fallback_IFACE[i]=''
308     (( i = i - 1 )) # since the loop will increment it
309     else
310     # Only return failure if it was the first address for the interface
311     [[ ${i} -eq 0 ]] && return 1
312     continue
313     fi
314     fi
315    
316     # We need to test to see if it's an IP address or a function
317     # We do this by testing if there is only 1 parameter
318     if [[ ${#conf[@]} -eq 1 ]]; then
319     eerror "No loaded modules provide \"${conf[0]}\" (${conf[0]}_start)"
320     # Only return failure if it was the first address for the interface
321     [[ ${i} -eq 0 ]] && return 1
322     continue
323     fi
324    
325 agriffis 535 interface_configure ${label} ${conf[@]} && continue
326 agriffis 532 # Only return failure if it was the first address for the interface
327     [[ ${i} -eq 0 ]] && return 1
328     done
329    
330     # Enabling rp_filter causes wacky packets to be auto-dropped by
331     # the kernel. Note that we only do this if it is not set via
332     # /etc/sysctl.conf ...
333     if [[ -e /proc/sys/net/ipv4/conf/${iface}/rp_filter && \
334     -z "$(grep -s '^[^#]*rp_filter' /etc/sysctl.conf)" ]]; then
335     echo -n 1 > /proc/sys/net/ipv4/conf/${iface}/rp_filter
336     fi
337    
338     # Start any modules with _post_start
339     # We do this here as the variables were localised in this function
340     for mod in ${MODULES[@]}; do
341     if [[ function == $( type -t ${mod}_post_start ) ]]; then
342     ${mod}_post_start ${iface} || return 1
343     fi
344     done
345    
346     return 0
347     }
348    
349     # bool iface_stop(char *interface)
350     #
351     # iface_stop: bring down an interface. Don't trust information in
352     # /etc/conf.d/net since the configuration might have changed since
353     # iface_start ran. Instead query for current configuration and bring
354     # down the interface.
355     #
356     # However, we are currently reliant on handler and modules specified
357     # in /etc/conf.d/net
358     iface_stop() {
359     local iface=${1} i aliases need_begin=false mod
360    
361     ebegin "Bringing ${iface} down"
362    
363     # Collect list of aliases for this interface.
364     # List will be in reverse order.
365 agriffis 535 aliases=$( interface_get_aliases_rev ${iface} )
366 agriffis 532 [[ -n ${aliases} ]] && need_begin=true
367    
368     # Stop aliases before primary interface.
369     # Note this must be done in reverse order, since ifconfig eth0:1
370     # will remove eth0:2, etc. It might be sufficient to simply remove
371     # the base interface but we're being safe here.
372     for i in ${aliases} ${iface}; do
373     # Stop all our modules
374     for mod in ${MODULES[@]}; do
375     [[ function == $( type -t ${mod}_stop ) ]] && ${mod}_stop ${i} && need_begin=true
376     done
377    
378     # Delete all the addresses for this alias
379 agriffis 545 interface_del_addresses ${i} && need_begin=true
380 agriffis 532
381     # Do final shut down of this alias
382     ${need_begin} && einfo " Stopping ${i}"
383 agriffis 535 interface_iface_stop ${i}
384 agriffis 532 eend $?
385     done
386    
387     return 0
388     }
389    
390     # bool start(void)
391     #
392     # Brings up ${IFACE}. Calls preup, iface_start, then postup.
393     # Returns 0 (success) unless preup or iface_start returns 1 (failure).
394     # Ignores the return value from postup.
395     start() {
396 agriffis 535 local mod
397 agriffis 532 local -a MODULES
398    
399     modules_load ${IFACE}
400     eend $?
401     [[ 0 != $? ]] && return 1
402    
403     if [[ lo == ${IFACE} ]]; then
404     ebegin "Bringing ${IFACE} up"
405 agriffis 545 interface_loopback_create
406 agriffis 532 eend $?
407     return $?
408     fi
409    
410     # Call user-defined preup function if it exists
411     if [[ function == $( type -t preup ) ]]; then
412     einfo "Running preup function"
413     preup ${IFACE} || {
414     eerror "preup ${IFACE} failed"
415     return 1
416     }
417     fi
418    
419     # pre Start any modules with
420     for mod in ${MODULES[@]}; do
421     if [[ function == $( type -t ${mod}_pre_start ) ]]; then
422     ${mod}_pre_start ${IFACE} || return 1
423     fi
424     done
425    
426     # Start the interface
427 agriffis 545 if ! iface_start ${IFACE} ; then
428     interface_down ${IFACE}
429     return 1
430     fi
431 agriffis 532
432     # post Start modules is called in iface_start
433     # as that's where the functions get localised
434     # and they may have to call iface_start recursively
435    
436     # Call user-defined postup function if it exists
437     if [[ function == $( type -t postup ) ]]; then
438     einfo "Running postup function"
439     postup ${IFACE}
440     fi
441    
442     return 0
443     }
444    
445     # bool stop(void)
446     #
447     # Brings down ${IFACE}. If predown call returns non-zero, then
448     # stop returns non-zero to indicate failure bringing down device.
449     # In all other cases stop returns 0 to indicate success.
450     stop() {
451 agriffis 535 local mod
452 agriffis 532 local -a MODULES
453    
454 agriffis 535 modules_load ${IFACE} true || return 1
455 agriffis 532
456     # Call user-defined predown function if it exists
457     if [[ lo != ${IFACE} && function == $(type -t predown) ]]; then
458     einfo "Running predown function"
459     predown ${IFACE} || {
460     eerror "predown ${IFACE} failed"
461     return 1
462     }
463     fi
464    
465     # pre Stop any modules
466     for mod in ${MODULES[@]}; do
467     [[ function == $( type -t ${mod}_pre_stop ) ]] && ${mod}_pre_stop ${IFACE}
468     done
469    
470     iface_stop ${IFACE} || return 1 # always succeeds, btw
471    
472     # post Stop any modules
473     for mod in ${MODULES[@]}; do
474     # We have already taken down the interface, so no need to error
475     [[ function == $( type -t ${mod}_post_stop ) ]] && ${mod}_post_stop ${IFACE}
476     done
477    
478     # Call user-defined postdown function if it exists
479     if [[ lo != ${IFACE} && $(type -t postdown) == function ]]; then
480     einfo "Running postdown function"
481     postdown ${IFACE}
482     fi
483    
484     return 0
485     }
486    
487     # vim:ts=4

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.13