/[baselayout]/trunk/sh/net.sh
Gentoo

Contents of /trunk/sh/net.sh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2574 - (hide annotations) (download) (as text)
Wed Apr 11 10:40:53 2007 UTC (11 years, 4 months ago) by uberlord
File MIME type: text/x-sh
File size: 13063 byte(s)
Move carrier detection back into net.sh. Reserve a new hook numbers
1 uberlord 2547 #!/sbin/runscript
2     # Copyright 1999-2007 Gentoo Foundation
3     # Distributed under the terms of the GNU General Public License v2
4    
5     MODULESDIR="${RC_LIBDIR}/net"
6     MODULESLIST="${RC_SVCDIR}/nettree"
7 uberlord 2565 _config_vars="config routes"
8 uberlord 2547
9     [ -z "${IN_BACKGROUND}" ] && IN_BACKGROUND=false
10    
11     depend() {
12     local IFACE=${SVCNAME#*.}
13     local IFVAR=$(echo -n "${IFACE}" | sed -e 's/[^[:alnum:]]/_/g')
14    
15     need localmount
16     after bootmisc
17     provide net
18     case "${IFACE}" in
19     lo|lo0) ;;
20     *)
21     after net.lo net.lo0
22     local prov=
23     eval prov=\$RC_NEED_${IFVAR}
24     [ -n "${prov}" ] && need ${prov}
25     eval prov=\$RC_USE_${IFVAR}
26     [ -n "${prov}" ] && use ${prov}
27     eval prov=\$RC_BEFORE_${IFVAR}
28     [ -n "${prov}" ] && before ${prov}
29     eval prov=\$RC_AFTER_${IFVAR}
30     [ -n "${prov}" ] && after ${prov}
31     eval prov=\$RC_PROVIDE_${IFVAR}
32     [ -n "${prov}" ] && provide ${prov}
33     ;;
34     esac
35     }
36    
37     _shell_var() {
38     echo -n "$1" | sed -e 's/[^[:alnum:]]/_/g'
39     }
40    
41     # Credit to David Leverton for this function which handily maps a bash array
42     # structure to positional parameters so existing configs work :)
43     # We'll deprecate arrays at some point though.
44     _get_array() {
45     if [ -n "${BASH}" ] ; then
46     case "$(declare -p "$1" 2>/dev/null)" in
47     "declare -a "*)
48     echo "set -- \"\${$1[@]}\""
49     return
50     ;;
51     esac
52     fi
53    
54     echo "eval set -- \"\$$1\""
55     }
56    
57     _wait_for_carrier() {
58     local timeout= efunc=einfon
59    
60     _has_carrier && return 0
61    
62     eval timeout=\$carrier_timeout_${IF_VAR}
63     timeout=${timeout:-5}
64    
65     [ -n "${RC_EBUFFER}" ] && efunc=einfo
66     ${efunc} "Waiting for carrier (${timeout} seconds) "
67     while [ ${timeout} -gt 0 ] ; do
68     sleep 1
69     if _has_carrier ; then
70     [ -z "${RC_EBUFFER}" ] && echo
71 uberlord 2574 eend 0
72 uberlord 2547 return 0
73     fi
74     timeout=$((${timeout} - 1))
75 uberlord 2574 [ -z "${RC_EBUFFER}" ] && printf "."
76 uberlord 2547 done
77    
78 uberlord 2574 [ -z "${RC_EBUFFER}" ] && echo
79     eend 1
80 uberlord 2547 return 1
81     }
82    
83     _netmask2cidr() {
84     local i= len=0
85    
86     local IFS=.
87     for i in $1; do
88     while [ ${i} != "0" ] ; do
89     len=$((${len} + 1))
90     i=$((${i} >> 1))
91     done
92     done
93    
94     echo "${len}"
95     }
96    
97     _configure_variables() {
98     local var= v= t=
99    
100     for var in ${_config_vars} ; do
101     local v=
102     for t in "$@" ; do
103     eval v=\$${var}_${t}
104     if [ -n "${v}" ] ; then
105     eval ${var}_${IFVAR}=\$${var}_${t}
106     continue 2
107     fi
108     done
109     done
110     }
111    
112     _show_address() {
113     einfo "received address $(_get_inet_address "${IFACE}")"
114     }
115    
116     # Basically sorts our modules into order and saves the list
117     _gen_module_list() {
118     local x= f=
119     if [ -s "${MODULESLIST}" -a "${MODULESLIST}" -nt "${MODULESDIR}" ] ; then
120     local update=false
121     for x in "${MODULESDIR}"/* ; do
122     [ -e "${x}" ] || continue
123     if [ "${x}" -nt "${MODULESLIST}" ] ; then
124     update=true
125     break
126     fi
127     done
128     ${update} || return 0
129     fi
130    
131     einfo "Caching network module dependencies"
132     # Run in a subshell to protect the main script
133     (
134     after() {
135     eval ${MODULE}_after="\"\${${MODULE}_after}\${${MODULE}_after:+ }$*\""
136     }
137    
138     before() {
139     local mod=${MODULE}
140     local MODULE=
141     for MODULE in "$@" ; do
142     after "${mod}"
143     done
144     }
145    
146     program() {
147     if [ "$1" = "start" -o "$1" = "stop" ] ; then
148     local s="$1"
149     shift
150     eval ${MODULE}_program_${s}="\"\${${MODULE}_program_${s}}\${${MODULE}_program_${s}:+ }$*\""
151     else
152     eval ${MODULE}_program="\"\${${MODULE}_program}\${${MODULE}_program:+ }$*\""
153     fi
154     }
155    
156     provide() {
157     eval ${MODULE}_provide="\"\${${MODULE}_provide}\${${MODULE}_provide:+ }$*\""
158     local x
159     for x in $* ; do
160     eval ${x}_providedby="\"\${${MODULE}_providedby}\${${MODULE}_providedby:+ }${MODULE}\""
161     done
162     }
163    
164     for MODULE in "${MODULESDIR}"/* ; do
165     sh -n "${MODULE}" || continue
166     . "${MODULE}" || continue
167     MODULE=${MODULE#${MODULESDIR}/}
168     MODULE=${MODULE%.sh}
169     eval ${MODULE}_depend
170     MODULES="${MODULES} ${MODULE}"
171     done
172    
173     VISITED=
174     SORTED=
175     visit() {
176     case " ${VISITED} " in
177     *" $1 "*) return ;;
178     esac
179     VISITED="${VISITED} $1"
180    
181     eval AFTER=\$${1}_after
182     for MODULE in ${AFTER} ; do
183     eval PROVIDEDBY=\$${MODULE}_providedby
184     if [ -n "${PROVIDEDBY}" ] ; then
185     for MODULE in ${PROVIDEDBY} ; do
186     visit "${MODULE}"
187     done
188     else
189     visit "${MODULE}"
190     fi
191     done
192    
193     eval PROVIDE=\$${1}_provide
194     for MODULE in ${PROVIDE} ; do
195     visit "${MODULE}"
196     done
197    
198     eval PROVIDEDBY=\$${1}_providedby
199     [ -z "${PROVIDEDBY}" ] && SORTED="${SORTED} $1"
200     }
201    
202     for MODULE in ${MODULES} ; do
203     visit "${MODULE}"
204     done
205    
206     > "${MODULESLIST}"
207     i=0
208     for MODULE in ${SORTED} ; do
209     eval PROGRAM=\$${MODULE}_program
210     eval PROGRAM_START=\$${MODULE}_program_start
211     eval PROGRAM_STOP=\$${MODULE}_program_stop
212     #for x in ${PROGRAM} ; do
213     # [ -x "${x}" ] || continue 2
214     #done
215     eval PROVIDE=\$${MODULE}_provide
216     echo "module_${i}='${MODULE}'" >> "${MODULESLIST}"
217     echo "module_${i}_program='${PROGRAM}'" >> "${MODULESLIST}"
218     echo "module_${i}_program_start='${PROGRAM_START}'" >> "${MODULESLIST}"
219     echo "module_${i}_program_stop='${PROGRAM_STOP}'" >> "${MODULESLIST}"
220     echo "module_${i}_provide='${PROVIDE}'" >> "${MODULESLIST}"
221     i=$((${i} + 1))
222     done
223     echo "module_${i}=" >> "${MODULESLIST}"
224     )
225    
226     return 0
227     }
228    
229     _load_modules() {
230     # Ensure our list is up to date
231     _gen_module_list
232    
233     local starting=$1 mymods=
234    
235     MODULES=
236     if [ "${IFACE}" != "lo" -a "${IFACE}" != "lo0" ] ; then
237     eval mymods=\$modules_${IFVAR}
238     [ -z "${mymods}" ] && mymods=${modules}
239     fi
240    
241     . "${MODULESLIST}"
242     local i=-1 x= mod= f= provides=
243     while true ; do
244     i=$((${i} + 1))
245     eval mod=\$module_${i}
246     [ -z "${mod}" ] && break
247     [ -e "${MODULESDIR}/${mod}.sh" ] || continue
248    
249     eval set -- \$module_${i}_program
250     if [ -n "$1" ] ; then
251     x=
252     for x in "$@" ; do
253     [ -x "${x}" ] && break
254     done
255     [ -x "${x}" ] || continue
256     fi
257     if ${starting} ; then
258     eval set -- \$module_${i}_program_start
259     else
260     eval set -- \$module_${i}_program_stop
261     fi
262     if [ -n "$1" ] ; then
263     x=
264     for x in "$@" ; do
265     [ -x "${x}" ] && break
266     done
267     [ -x "${x}" ] || continue
268     fi
269    
270     eval provides=\$module_${i}_provide
271     if ${starting} ; then
272     case " ${mymods} " in
273     *" !${mod} "*) continue ;;
274     *" !${provides} "*) [ -n "${provides}" ] && continue ;;
275     esac
276     fi
277     MODULES="${MODULES}${MODULES:+ }${mod}"
278    
279     # Now load and wrap our functions
280     if ! . "${MODULESDIR}/${mod}.sh" ; then
281     eend 1 "${SVCNAME}: error loading module \`${mod}'"
282     exit 1
283     fi
284    
285     [ -z "${provides}" ] && continue
286    
287     # Wrap our provides
288     local f=
289     for f in pre_start start post_start ; do
290     eval "${provides}_${f}() { type ${mod}_${f} >/dev/null 2>/dev/null || return 0; ${mod}_${f} \"\$@\"; }"
291     done
292    
293     eval module_${mod}_provides="${provides}"
294     eval module_${provides}_providedby="${mod}"
295     done
296    
297     # Wrap our preferred modules
298     for mod in ${mymods} ; do
299     case " ${MODULES} " in
300     *" ${mod} "*)
301     eval x=\$module_${mod}_provides
302     [ -z "${x}" ] && continue
303     for f in pre_start start post_start ; do
304     eval "${x}_${f}() { type ${mod}_${f} >/dev/null 2>/dev/null || return 0; ${mod}_${f} \"\$@\"; }"
305     done
306     eval module_${x}_providedby="${mod}"
307     ;;
308     esac
309     done
310    
311     # Finally remove any duplicated provides from our list if we're starting
312     # Otherwise reverse the list
313     local LIST="${MODULES}" p=
314     MODULES=
315     if ${starting} ; then
316     for mod in ${LIST} ; do
317     eval x=\$module_${mod}_provides
318     if [ -n "${x}" ] ; then
319     eval p=\$module_${x}_providedby
320     [ "${mod}" != "${p}" ] && continue
321     fi
322     MODULES="${MODULES}${MODULES:+ }${mod}"
323     done
324     else
325     for mod in ${LIST} ; do
326     MODULES="${mod}${MODULES:+ }${MODULES}"
327     done
328     fi
329    
330     veinfo "Loaded modules: ${MODULES}"
331     }
332    
333     _load_config() {
334     eval "$(_get_array "config_${IFVAR}")"
335     if [ "${IFACE}" = "lo" -o "${IFACE}" = "lo0" ] ; then
336     set -- "127.0.0.1/8" "$@"
337     else
338     if [ $# -eq 0 ] ; then
339     ewarn "No configuration specified; defaulting to DHCP"
340     set -- "dhcp"
341     fi
342     fi
343    
344     # We store our config in an array like vars
345     # so modules can influence it
346     config_index=0
347     for cmd in "$@" ; do
348     eval config_${config_index}="'${cmd}'"
349     config_index=$((${config_index} + 1))
350     done
351     # Terminate the list
352     eval config_${config_index}=
353    
354     config_index=0
355     eval $(_get_array fallback_${IFVAR})
356     for cmd in "$@" ; do
357     eval fallback_${config_index}="'${cmd}'"
358     config_index=$((${config_index} + 1))
359     done
360     # Terminate the list
361     eval fallback_${config_index}=
362    
363     # Don't set to zero, so any net modules don't have to do anything extra
364     config_index=-1
365     }
366    
367     start() {
368     local IFACE=${SVCNAME#*.} oneworked=false module=
369     local IFVAR=$(_shell_var "${IFACE}") cmd= metric=0 our_metric=$metric
370    
371     einfo "Bringing up interface ${IFACE}"
372     eindent
373    
374     if [ -z "${MODULES}" ] ; then
375     local MODULES=
376     _load_modules true
377     fi
378    
379     _up 2>/dev/null
380    
381     if type preup >/dev/null 2>/dev/null ; then
382     ebegin "Running preup"
383     eindent
384     preup || return 1
385     eoutdent
386     fi
387    
388     for module in ${MODULES} ; do
389     if type "${module}_pre_start" >/dev/null 2>/dev/null ; then
390     if ! ${module}_pre_start ; then
391     eend 1
392     exit 1
393     fi
394     fi
395     done
396    
397 uberlord 2574 if ! _wait_for_carrier ; then
398     if service_started devd ; then
399     ewarn "no carrier, but devd will start us when we have one"
400     mark_service_inactive "${SVCNAME}"
401     else
402     eerror "no carrier"
403     fi
404     return 1
405     fi
406    
407 uberlord 2547 local config= config_index=
408     _load_config
409     config_index=0
410    
411     if [ -n "${our_metric}" ] ; then
412     metric=${our_metric}
413     elif [ "${IFACE}" != "lo" -a "${IFACE}" != "lo0" ] ; then
414     metric=$((${metric} + $(_ifindex)))
415     fi
416    
417     while true ; do
418     eval config=\$config_${config_index}
419     [ -z "${config}" ] && break
420    
421     set -- "${config}"
422     ebegin "$1"
423     eindent
424     case "$1" in
425     noop)
426     if [ -n "$(_get_inet_address)" ] ; then
427     oneworked=true
428     break
429     fi
430     ;;
431     null) : ;;
432     [0-9]*|*:*) _add_address ${config} ;;
433     *)
434     if type "${config}_start" >/dev/null 2>/dev/null ; then
435     "${config}"_start
436     else
437     eerror "nothing provides \`${config}'"
438     fi
439     ;;
440     esac
441     if eend $? ; then
442     oneworked=true
443     else
444     eval config=\$fallback_${IFVAR}
445     if [ -n "${config}" ] ; then
446     einfo "Trying fallback configuration"
447     eval config_${config_index}=\$fallback_${IFVAR}
448     eval fallback_${config_index}=
449     config_index=$((${config_index} - 1))
450     fi
451     fi
452     eoutdent
453     config_index=$((${config_index} + 1))
454     done
455    
456     if ! ${oneworked} ; then
457     if type failup >/dev/null 2>/dev/null ; then
458     ebegin "Running failup"
459     eindent
460     failup
461     eoutdent
462     fi
463     return 1
464     fi
465    
466     local hidefirstroute=false first=true routes=
467     eval "$(_get_array "routes_${IFVAR}")"
468     if [ "${IFACE}" = "lo" -o "${IFACE}" = "lo0" ] ; then
469     set -- "127.0.0.0/8 via 127.0.0.1" "$@"
470     hidefirstroute=true
471     fi
472     for cmd in "$@" ; do
473     if ${first} ; then
474     first=false
475     einfo "Adding routes"
476     fi
477     eindent
478     ebegin "${cmd}"
479     # Work out if we're a host or a net if not told
480     case "${cmd}" in
481     *" -net "*|*" -host "*) ;;
482     *" netmask "*) cmd="-net ${cmd}" ;;
483     *)
484     case "${cmd%% *}" in
485     *.*.*.*/32) cmd="-host ${cmd}" ;;
486     *.*.*.*/*|0.0.0.0|default) cmd="-net ${cmd}" ;;
487     *) cmd="-host ${cmd}" ;;
488     esac
489     ;;
490     esac
491     if ${hidefirstroute} ; then
492     _add_route ${cmd} >/dev/null 2>/dev/null
493     hidefirstroute=false
494     else
495     _add_route ${cmd} >/dev/null
496     fi
497     eend $?
498     eoutdent
499     done
500    
501     for module in ${MODULES} ; do
502     if type "${module}_post_start" >/dev/null 2>/dev/null ; then
503     if ! ${module}_post_start ; then
504     eend 1
505     exit 1
506     fi
507     fi
508     done
509    
510     if type postup >/dev/null 2>/dev/null ; then
511     ebegin "Running postup"
512     eindent
513     postup
514     eoutdent
515     fi
516    
517     return 0
518     }
519    
520     stop() {
521     local IFACE=${SVCNAME#*.} module=
522     local IFVAR=$(_shell_var "${IFACE}") opts=
523    
524     einfo "Bringing down interface ${IFACE}"
525     eindent
526    
527     if [ -z "${MODULES}" ] ; then
528     local MODULES=
529     _load_modules false
530     fi
531    
532     if type predown >/dev/null 2>/dev/null ; then
533     ebegin "Running predown"
534     eindent
535     predown || return 1
536     eoutdent
537     fi
538    
539     for module in ${MODULES} ; do
540     if type "${module}_pre_stop" >/dev/null 2>/dev/null ; then
541     if ! ${module}_pre_stop ; then
542     eend 1
543     exit 1
544     fi
545     fi
546     done
547    
548     for module in ${MODULES} ; do
549     if type "${module}_stop" >/dev/null 2>/dev/null ; then
550     ${module}_stop
551     fi
552     done
553    
554     _delete_addresses "${IFACE}"
555    
556     for module in ${MODULES} ; do
557     if type "${module}_post_stop" >/dev/null 2>/dev/null ; then
558     ${module}_post_stop
559     fi
560     done
561    
562     [ "${IN_BACKGROUND}" != "true" ] && \
563     [ "${IFACE}" != "lo" -a "${IFACE}" != "lo0" ] && \
564     _down 2>/dev/null
565    
566     [ -x /sbin/resolvconf ] && resolvconf -d "${IFACE}"
567    
568 uberlord 2567 if type postdown >/dev/null 2>/dev/null ; then
569 uberlord 2547 ebegin "Running postdown"
570     eindent
571     postdown
572     eoutdent
573     fi
574    
575     return 0
576     }
577    
578     # vim: set ts=4 :

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.20