/[vps]/baselayout-vserver/branches/baselayout-1_12/net-scripts/net/udhcpc.sh
Gentoo

Contents of /baselayout-vserver/branches/baselayout-1_12/net-scripts/net/udhcpc.sh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 436 - (show annotations) (download) (as text)
Sun Aug 27 18:14:39 2006 UTC (12 years, 1 month ago) by phreak
File MIME type: text/x-sh
File size: 3876 byte(s)
Merging r2213
1 # Copyright (c) 2004-2006 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # Contributed by Roy Marples (uberlord@gentoo.org)
4
5 # Fix any potential localisation problems
6 # Note that LC_ALL trumps LC_anything_else according to locale(7)
7 udhcpc() {
8 LC_ALL=C /sbin/udhcpc "$@"
9 }
10
11 # void udhcpc_depend(void)
12 #
13 # Sets up the dependancies for the module
14 udhcpc_depend() {
15 after interface
16 provide dhcp
17 functions interface_exists interface_get_address
18 }
19
20 # void udhcpc_expose(void)
21 #
22 # Expose variables that can be configured
23 udhcpc_expose() {
24 variables udhcpc dhcp
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_stop(char *iface)
37 #
38 # Stops udhcpc running on an interface
39 # Return 1 if we fail to stop udhcpc (if it's running) otherwise 0
40 udhcpc_stop() {
41 local iface="$1" pidfile="/var/run/udhcpc-$1.pid" d=
42
43 [[ ! -f ${pidfile} ]] && return 0
44
45 ebegin "Stopping udhcpc on ${iface}"
46 local pid=$(<"${pidfile}") e=true
47
48 local ifvar=$(bash_variable "${iface}")
49 d="dhcp_${ifvar}"
50 [[ -z ${!d} ]] && d="dhcp"
51
52 if [[ " ${!d} " == *" release "* ]]; then
53 kill -s USR2 "${pid}" &>/dev/null
54 [[ -f "/var/cache/udhcpc-${iface}.lease" ]] \
55 && rm "/var/cache/udhcpc-${iface}.lease"
56 fi
57
58 start-stop-daemon --stop --exec /sbin/udhcpc --pidfile "${pidfile}"
59 eend $? || return 1
60
61 [[ -e /var/run/udhcpc-"${iface}".conf ]] \
62 && rm -f /var/run/udhcpc-"${iface}".conf
63 return 0
64 }
65
66 # bool udhcpc_start(char *iface)
67 #
68 # Start DHCP on an interface by calling udhcpc $iface $options
69 #
70 # Returns 0 (true) when a DHCP address is obtained, otherwise 1
71 udhcpc_start() {
72 local iface="$1" opts= pidfile="/var/run/udhcpc-$1.pid"
73 local cachefile="/var/cache/udhcpc-$1.lease" d
74
75 interface_exists "${iface}" true || return 1
76
77 local ifvar=$(bash_variable "${iface}" ) opts=
78 opts="udhcpc_${ifvar} ${udhcpc}"
79 opts="${!opts}"
80
81 d="dhcp_${ifvar}"
82 [[ -z ${!d} ]] && d="dhcp"
83
84 if [[ " ${!d} " != *" nosendhost "* ]]; then
85 if [[ ! " ${opts}" =~ " -([hH] |-hostname=)" ]]; then
86 local hname=$(hostname)
87 [[ -n ${hname} && ${hname} != "(none)" && ${hname} != "localhost" ]] \
88 && opts="${opts} --hostname=${hname}"
89 fi
90 fi
91
92 # Setup options for the udhcpc script
93 # This requires a specfic Gentoo patch to udhcp which will not be
94 # accepted upstream.
95 if [[ " ${!d} " == *" nogateway "* ]] ; then
96 opts="${opts} --env PEER_ROUTERS=no"
97 else
98 opts="${opts} --env PEER_ROUTERS=yes"
99 fi
100 if [[ " ${!d} " == *" nodns "* ]] ; then
101 opts="${opts} --env PEER_DNS=no"
102 else
103 opts="${opts} --env PEER_DNS=yes"
104 fi
105 if [[ " ${!d} " == *" nontp "* ]] ; then
106 opts="${opts} --env PEER_NTP=no"
107 else
108 opts="${opts} --env PEER_NTP=yes"
109 fi
110 local metric="metric_${ifvar}"
111 if [[ -n ${!metric} ]] ; then
112 opts="${opts} --env IF_METRIC=${!metric}"
113 fi
114
115 # Bring up DHCP for this interface (or alias)
116 ebegin "Running udhcpc"
117
118 # Try and load the cache if it exists
119 if [[ -f ${cachefile} ]]; then
120 if [[ " ${opts}" != *" --request="* && " ${opts} " != *" -r "* ]]; then
121 local x=$(<"${cachefile}")
122 # Check for a valid ip
123 [[ ${x} == *.*.*.* ]] && opts="${opts} --request=${x}"
124 fi
125 fi
126
127 # Don't use s-s-d if the user wants to quit on lease.
128 if [[ " ${opts} " == *" -q "* || " ${opts} " == *" --quit "* ]]; then
129 x="/sbin/udhcpc"
130 else
131 x="start-stop-daemon --start --exec /sbin/udhcpc \
132 --pidfile \"${pidfile}\" --"
133 fi
134
135 eval "${x}" "${opts}" --interface="${iface}" --now \
136 --script=/lib/rcscripts/sh/udhcpc.sh \
137 --pidfile="${pidfile}" >/dev/null
138 eend $? || return 1
139
140 # DHCP succeeded, show address retrieved
141 local addr=$(interface_get_address "${iface}")
142 einfo "${iface} received address ${addr}"
143
144 return 0
145 }
146
147 # vim: set ts=4 :

  ViewVC Help
Powered by ViewVC 1.1.20