diff options
Diffstat (limited to 'net-scripts/net.modules.d/dhclient.sh')
-rw-r--r-- | net-scripts/net.modules.d/dhclient.sh | 150 |
1 files changed, 150 insertions, 0 deletions
diff --git a/net-scripts/net.modules.d/dhclient.sh b/net-scripts/net.modules.d/dhclient.sh new file mode 100644 index 0000000..b8d3312 --- /dev/null +++ b/net-scripts/net.modules.d/dhclient.sh @@ -0,0 +1,150 @@ +# Copyright (c) 2004-2006 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 +# Contributed by Roy Marples (uberlord@gentoo.org) + +# Fix any potential localisation problems +# Note that LC_ALL trumps LC_anything_else according to locale(7) +dhclient() { + LC_ALL=C /sbin/dhclient "$@" +} + +# void dhclient_depend(void) +# +# Sets up the dependancies for the module +dhclient_depend() { + after interface + provide dhcp + functions interface_exists interface_get_address +} + +# void dhclient_expose(void) +# +# Expose variables that can be configured +dhclient_expose() { + variables dhclient dhcp +} + +# bool dhclient_check_installed(void) +# +# Returns 1 if dhclient is installed, otherwise 0 +dhclient_check_installed() { + [[ -x /sbin/dhclient ]] && return 0 + ${1:-false} && eerror "For DHCP (dhclient) support, emerge net-misc/dhcp" + return 1 +} + +# bool dhclient_stop(char *iface) +# +# Stop dhclient on an interface +# Always returns 0 +dhclient_stop() { + local iface="$1" pidfile="/var/run/dhclient-$1.pid" + + [[ ! -f ${pidfile} ]] && return 0 + + ebegin "Stopping dhclient on ${iface}" + + local ifvar="$(bash_variable "${iface}")" + local d="dhcp_${ifvar}" + d=" ${!d} " + [[ ${d} == " " ]] && d=" ${dhcp} " + + if [[ ${d} == *" release "* ]] ; then + local dhconf="interface \"${iface}\" {\n \ + script \"${MODULES_DIR}/helpers.d/dhclient-wrapper\";\n \ + }\n" + local r="$(echo -e "${dhconf}" \ + | dhclient -q -r -pf "${pidfile}" "${iface}" )" + [[ ${r} == "deconfig" ]] + eend $? "dhclient returned ${r}" + else + start-stop-daemon --stop --exec /sbin/dhclient --pidfile "${pidfile}" + eend $? + fi +} + +# bool dhclient_start(char *iface) +# +# Start DHCP on an interface by calling dhclient $iface $options +# +# Returns 0 (true) when a DHCP address is obtained, otherwise 1 +dhclient_start() { + local iface="$1" ifvar="$(bash_variable "$1")" + local pidfile="/var/run/dhclient-${iface}.pid" + local cffile="/etc/dhcp/dhclient.conf" + + interface_exists "${iface}" true || return 1 + + # Load any dhclient.conf instructions specified by the user + local opts="dhclient_conf_${ifvar}" + local dhconf="${!opts}" + + # Load our options + opts="dhclient_${ifvar}" + opts="${!opts}" + + # Work out our cffile + local x="${opts##* -cf }" + if [[ ${x} != "${opts}" ]]; then + x="${x%% *}" + if [[ -n ${x} ]]; then + cffile="${x}" + opts="${opts//-cf ${cffile}/}" + fi + fi + + # Warn that we're going to override parts of their cffile + if [[ -e ${cffile} ]] ; then + opts="${opts} -cf ${cffile}" + if grep -q "^[ \t]*script[ \t]" "${cffile}" ; then + ewarn "The script specified in ${cffile} will not be used" + fi + fi + + local d="dhcp_${ifvar}" + d=" ${!d} " + [[ ${d} == " " ]] && d=" ${dhcp} " + + local ah="" + # Send our hostname by editing cffile + if [[ ${d} != *" nosendhost "* ]] ; then + local hname="$(hostname)" + if [[ ${hname} != "(none)" && ${hname} != "localhost" ]]; then + ah="send host-name \"${hname}\"\n;" + # Warn that we're going to override parts of their cffile + if [[ -e ${cffile} ]] ; then + if grep -q "^[ \t]*send[ \t]*host-name[ \t]" "${cffile}" ; then + ewarn "The host-name \"${hname}\" will be sent instead of" + ewarn "the one specified in ${cffile}" + vewarn "Set dhcp_${ifvar}=\"nosendhost\" in /etc/conf.d/net" + vewarn "to stop this from happening" + fi + fi + fi + fi + dhconf="${dhconf} interface \"${iface}\" {\n \ + script \"/${MODULES_DIR}/helpers.d/dhclient-wrapper\";\n \ + ${ah} + }" + + # Bring up DHCP for this interface (or alias) + ebegin "Running dhclient" + x="$(echo -e "${dhconf}" | dhclient ${opts} -q -1 -pf "${pidfile}")" + # We just check the last 5 letters + [[ ${x:${#x} - 5:5} == "bound" ]] + if [[ $? != "0" ]]; then + # Kill the offending daemon as it likes to hang around + start-stop-daemon --stop --exec /sbin/dhclient --pidfile "${pidfile}" + eend 1 "${x}" + return 1 + fi + eend 0 + + # DHCP succeeded, show address retrieved + local addr="$(interface_get_address "${iface}")" + einfo "${iface} received address ${addr}" + + return 0 +} + +# vim: set ft=sh ts=4 : |