/[vps]/baselayout-vserver/trunk/lib/rcscripts/net.modules.d/udhcpc
Gentoo

Contents of /baselayout-vserver/trunk/lib/rcscripts/net.modules.d/udhcpc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3 - (show annotations) (download)
Sat Sep 3 16:10:27 2005 UTC (9 years, 3 months ago) by hollow
File size: 3825 byte(s)
import initial baselayout sources (1.12.0_pre8)
1 #!/bin/bash
2 # Copyright (c) 2004-2005 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
4
5 # Contributed by Roy Marples (uberlord@gentoo.org)
6
7 # Fix any potential localisation problems
8 # Note that LC_ALL trumps LC_anything_else according to locale(7)
9 udhcpc() {
10 LC_ALL=C /sbin/udhcpc "$@"
11 }
12
13 # char* udhcpc_provides(void)
14 #
15 # Returns a string to change module definition for starting up
16 udhcpc_provides() {
17 echo "dhcp"
18 }
19
20 # void udhcpc_depend(void)
21 #
22 # Sets up the dependancies for the module
23 udhcpc_depend() {
24 after interface
25 }
26
27 # bool udhcpc_check_installed(void)
28 #
29 # Returns 1 if udhcpc is installed, otherwise 0
30 udhcpc_check_installed() {
31 [[ -x /sbin/udhcpc ]] && return 0
32 ${1:-false} && eerror "For DHCP (udhcpc) support, emerge net-misc/udhcp"
33 return 1
34 }
35
36 # bool udhcpc_check_depends(void)
37 #
38 # Checks to see if we have the needed functions
39 udhcpc_check_depends() {
40 local f
41
42 for f in interface_exists interface_get_address; do
43 [[ $( type -t "${f}" ) == "function" ]] && continue
44 eerror "udhcpc: missing required function ${f}\n"
45 return 1
46 done
47
48 return 0
49 }
50
51 # char* udhcpc_get_vars(char *interface)
52 #
53 # Returns a string spaced with possible user set
54 # configuration variables
55 udhcpc_get_vars() {
56 echo "udhcpc_$1 dhcp_$1"
57 }
58
59 # bool udhcpc_stop(char *iface)
60 #
61 # Stops udhcpc running on an interface
62 # Return 1 if we fail to stop udhcpc (if it's running) otherwise 0
63 udhcpc_stop() {
64 local iface="$1" pidfile="/var/run/udhcpc-$1.pid" d
65
66 udhcpc_check_installed || return 0
67 [[ ! -f ${pidfile} ]] && return 0
68
69 ebegin "Stopping udhcpc on ${iface}"
70 local pid=$( < "${pidfile}" ) e=true
71
72 local ifvar=$( bash_variable "${iface}" )
73 eval d=\" \$\{dhcp_${ifvar}\} \"
74 [[ ${d} == " " ]] && d=" ${dhcp} "
75
76 if [[ ${d} == *" release "* ]]; then
77 kill -s USR2 "${pid}" &>/dev/null
78 [[ -f "/var/cache/dhcp-${iface}.lease" ]] \
79 && rm "/var/cache/dhcp-${iface}.lease"
80 fi
81
82 kill -s TERM "${pid}" &>/dev/null
83
84 [[ -f ${pidfile} ]] && rm -f "${pidfile}"
85
86 ${e}
87 eend "$?"
88 return "$?"
89 }
90
91 # bool udhcpc_start(char *iface)
92 #
93 # Start DHCP on an interface by calling udhcpc $iface $options
94 #
95 # Returns 0 (true) when a DHCP address is obtained, otherwise 1
96 udhcpc_start() {
97 local iface="$1" opts pidfile="/var/run/udhcpc-$1.pid"
98 local cachefile="/var/cache/dhcp-$1.lease" d
99
100 interface_exists "${iface}" true || return 1
101
102 local ifvar=$( bash_variable "${iface}" ) opts hostname
103 eval opts=\"\$\{udhcpc_${ifvar}\}\"
104
105 eval d=\" \$\{dhcp_${ifvar}\} \"
106 [[ ${d} == " " ]] && d=" ${dhcp} "
107
108 if [[ ${d} != *" nosendhost "* ]]; then
109 if [[ ! " ${opts}" =~ " -([hH] |-hostname=)" ]]; then
110 local hostname=$( hostname )
111 [[ -n ${hostname} && ${hostname} != "(none)" \
112 && ${hostname} != "localhost" ]] \
113 && opts="${opts} --hostname=${hostname}"
114 fi
115 fi
116
117 # Bring up DHCP for this interface (or alias)
118 ebegin "Running udhcpc"
119
120 # Stop any instance of udhcpc on this interface
121 udhcpc_stop "${interface}"
122
123 # Try and load the cache if it exists
124 if [[ -f ${cachefile} ]]; then
125 if [[ " ${opts}" != *" --request="* && " ${opts} " != *" -r "* ]]; then
126 local x=$( < "${cachefile}" )
127 [[ -n ${x} ]] && opts="${opts} --request=${x}"
128 fi
129 fi
130
131 if [[ ${background} == "yes" ]]; then
132 eval udhcpc ${opts} --script="${MODULES_DIR}/helpers.d/udhcpc-wrapper" \
133 --pidfile="${pidfile}" --interface="${iface}" &>/dev/null &
134 eend 0
135 go_background
136 fi
137
138 x=$( eval udhcpc ${opts} --now --pidfile="${pidfile}" \
139 --interface="${iface}" \
140 --script="${MODULES_DIR}/helpers.d/udhcpc-wrapper" 2>&1 \
141 | egrep -v '^info,' )
142 # We just check the last 5 letters
143 [[ ${x:((${#x} - 5)):5} == "bound" ]]
144 eend "$?" "${x}" || return 1
145
146 # DHCP succeeded, show address retrieved
147 local addr=$( interface_get_address "${iface}" )
148 einfo "${iface} received address ${addr}"
149
150 return 0
151 }
152
153 # vim:ts=4

  ViewVC Help
Powered by ViewVC 1.1.20