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

Contents of /trunk/net-scripts/net.modules.d/dhclient

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1753 - (show annotations) (download)
Fri Dec 9 15:08:06 2005 UTC (8 years, 7 months ago) by uberlord
File size: 4201 byte(s)
Remove nasty undocumented backgrounding as we now support
ifplugd, netplug and wpa_supplicant-0.4.x which do much better jobs.
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 dhclient() {
10 LC_ALL=C /sbin/dhclient "$@"
11 }
12
13 # void dhclient_depend(void)
14 #
15 # Sets up the dependancies for the module
16 dhclient_depend() {
17 after interface
18 provide dhcp
19 functions interface_exists interface_get_address
20 variables dhclient dhcp
21 }
22
23 # bool dhclient_check_installed(void)
24 #
25 # Returns 1 if dhclient is installed, otherwise 0
26 dhclient_check_installed() {
27 [[ -x /sbin/dhclient ]] && return 0
28 ${1:-false} && eerror "For DHCP (dhclient) support, emerge net-misc/dhcp"
29 return 1
30 }
31
32 # bool dhclient_stop(char *iface)
33 #
34 # Stop dhclient on an interface
35 # Always returns 0
36 dhclient_stop() {
37 local iface="$1" d
38 local pidfile="/var/run/dhclient-${iface}.pid"
39
40 [[ ! -f ${pidfile} ]] && return 0
41
42 # We check for a dhclient process first as if we attempt to release
43 # an interface for which dhclient has obtained an IP in the past
44 # it causes a "RELEASE" event anyway.
45 local pid=$( < "${pidfile}" )
46
47 local ifvar=$( bash_variable "${iface}" )
48 d="dhcp_${ifvar}"
49 d=" ${!d} "
50 [[ ${d} == " " ]] && d=" ${dhcp} "
51
52 ebegin "Stopping dhclient on ${iface}"
53 if [[ ${d} == *" release "* ]]; then
54 local r=$( dhclient -q -r -pf "${pidfile}" \
55 -sf "${MODULES_DIR}/helpers.d/dhclient-wrapper" "${iface}" )
56 [[ ${r} == "deconfig" ]]
57 eend $? "dhclient returned a ${r}"
58 [[ -f "/var/cache/dhcp-${iface}.lease" ]] \
59 && rm -f "/var/cache/dhcp-${iface}.lease"
60 else
61 kill -s TERM "${pid}" 2>/dev/null
62 clean_pidfile "${pidfile}"
63 eend 0
64 fi
65
66 return 0
67 }
68
69 # bool dhclient_start(char *iface)
70 #
71 # Start DHCP on an interface by calling dhclient $iface $options
72 #
73 # Returns 0 (true) when a DHCP address is obtained, otherwise 1
74 dhclient_start() {
75 local iface="$1" opts ifvar=$( bash_variable "$1" ) d
76 local pidfile="/var/run/dhclient-${iface}.pid" edit=""
77 local cffile="/etc/dhcp/dhclient.conf"
78
79 interface_exists "${iface}" true || return 1
80
81 edit="dhclient_edit_config_${ifvar}"
82 [[ -z ${!edit} ]] && edit="dhclient_edit_config"
83 if [[ ${!edit} == "yes" || ${!edit} == "true" ]]; then
84 edit=true
85 else
86 edit=false
87 fi
88
89 # Load our options
90 opts="dhclient_${ifvar}"
91 opts="${!opts}"
92
93 # Work out our cffile
94 x="${opts##* -cf }"
95 if [[ ${x} != "${opts}" ]]; then
96 x="${x%% *}"
97 if [[ -n ${x} ]]; then
98 cffile="${x}"
99 opts="${opts//-cf ${cffile}/}"
100 fi
101 fi
102 opts="${opts} -cf ${cffile}"
103
104 # Ensure that the cffile does not contain any script lines
105 # as that will stop our helpers from running
106 if [[ -e ${cffile} ]] ; then
107 if grep -q "^[ \t]*script " "${cffile}" 2>/dev/null ; then
108 if ${edit} ; then
109 sed -i '/^[ \t]*script /d' "${cffile}" || return 1
110 else
111 eerror "You have to remove the script parameter from ${cffile}"
112 return 1
113 fi
114 fi
115 else
116 ${edit} && touch "${cffile}" 2>/dev/null
117 fi
118
119 d="dhcp_${ifvar}"
120 d=" ${!d} "
121 [[ ${d} == " " ]] && d=" ${dhcp} "
122
123 # Send our hostname by editing cffile
124 if ${edit} && [[ -e ${cffile} && ${d} != *" nosendhost "* ]] ; then
125 local hname=$( hostname )
126 if [[ ${hname} != "(none)" && ${hname} != "localhost" ]]; then
127 sed -i '/^[ \t]*send[ \t]*host-name[ \t]*/d' "${cffile}"
128 if [[ -s ${cffile} ]]; then
129 sed -i '1 isend host-name "'"${hname}"'";' "${cffile}"
130 else
131 echo "send host-name \"${hname}\";" > "${cffile}"
132 fi
133 fi
134 fi
135
136 # Bring up DHCP for this interface (or alias)
137 ebegin "Running dhclient"
138
139 # Stop dhclient if it's already running
140 dhclient_stop "${iface}"
141
142 local x=$( eval dhclient "${opts}" -1 -pf "${pidfile}" \
143 -sf "${MODULES_DIR}/helpers.d/dhclient-wrapper" -q "${iface}" 2>&1 )
144 # We just check the last 5 letters
145 [[ ${x:${#x} - 5:5} == "bound" ]]
146 if [[ $? != "0" ]]; then
147 echo "${x}"
148 # We need to kill the process if we fail
149 [[ -e ${pidfile} ]] && kill -s TERM $( < "${pidfile}" ) 2>/dev/null
150 eend 1
151 return 1
152 fi
153 eend 0
154
155 # DHCP succeeded, show address retrieved
156 local addr=$( interface_get_address "${iface}" )
157 einfo "${iface} received address ${addr}"
158
159 return 0
160 }
161
162 # vim:ts=4

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.20