summaryrefslogtreecommitdiff
blob: c467f2a5144217334666af24c02c12c5782720b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/bash
# 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)
dhcpcd() {
	LC_ALL=C /sbin/dhcpcd "$@"
}

# void dhcpcd_depend(void)
#
# Sets up the dependancies for the module
dhcpcd_depend() {
	after interface
	provide dhcp
	functions interface_exists interface_get_address
	variables dhcpcd dhcp
}

# bool dhcpcd_check_installed(void)
#
# Returns 1 if dhcpcd is installed, otherwise 0
dhcpcd_check_installed() {
	if [[ -x /sbin/dhcpcd ]]; then
		if dhcpcd -h 2>&1 | grep -q "etcDir" ; then
			return 0
		else
			${1:-false} && eerror "We require dhcpcd-2.0.0 or newer"
			return 1
		fi
	fi

	${1:-false} && eerror "For DHCP (dhcpcd) support, emerge net-misc/dhcpcd"
	return 1
}

# bool dhcpcd_stop(char *iface)
#
# Stop DHCP on an interface by calling dhcpcd -z $iface
#
# Returns 0 (true) when a DHCP address dropped
# otherwise return 1
dhcpcd_stop() {
	local iface=$1 count signal pidfile="/var/run/dhcpcd-$1.pid" d

	[[ ! -f ${pidfile} ]] && return 0

	ebegin "Stopping dhcpcd on ${iface}"
	local pid=$( < "${pidfile}" )

	local ifvar=$( bash_variable "${iface}" )
	d="dhcp_${ifvar}"
	d=" ${!d} "
	[[ ${d} == "  " ]] && d=" ${dhcp} "

	if [[ ${d} == *" release "* ]]; then
		signal="HUP"
	else
		signal="TERM"
	fi

	kill -s "${signal}" "${pid}" &>/dev/null 
	process_finished "${pid}" dhcpcd
	eend $? "timed out"
	return $?
}

# bool dhcpcd_start(char *iface)
#
# Start DHCP on an interface by calling dhcpcd $iface $options
#
# Returns 0 (true) when a DHCP address is obtained, otherwise 1
dhcpcd_start() {
	local iface="$1" opts pidfile="/var/run/dhcpcd-$1.pid"
	local ifvar=$( bash_variable "${iface}" ) metric d

	interface_exists "${iface}" true || return 1

	# Get our options
	opts="dhcpcd_${ifvar}"
	opts="${!opts}"

	# Map some generic options to dhcpcd
	d="dhcp_${ifvar}"
	d=" ${!d} "
	[[ ${d} == "  " ]] && d=" ${dhcp} "
	[[ ${d} == *" nodns "* ]] && opts="${opts} -R"
	[[ ${d} == *" nontp "* ]] && opts="${opts} -N"
	[[ ${d} == *" nonis "* ]] && opts="${opts} -Y"
	[[ ${d} == *" nogateway "* ]] && opts="${opts} -G"

	# We transmit the hostname by default:q
	if [[ " ${d} " != *" nosendhost "* && " ${opts} " != *" -h "* ]]; then
		local hname=$( hostname )
		[[ -n ${hname} && ${hname} != "(none)" && ${hname} != "localhost" ]] \
			&& opts="-h \"${hname}\" ${opts}"
	fi

	# Stop dhcpcd from bringing the interface down when we exit
	opts="${opts} -o"

	# Add our route metric
	metric="metric_${ifvar}"
	[[ -n ${!metric} && ${!metric} != "0" ]] && opts="${opts} -m ${!metric}"

	# Instruct dhcpcd to use our wrapper 
	opts="${opts} -c \"${svclib}/net.modules.d/helpers.d/dhcpcd-wrapper\""

	# Instruct dhcpcd to create it's files in our state dir
	opts="${opts} -e \"${statedir}/${iface}\""

	# Bring up DHCP for this interface (or alias)
	ebegin "Running dhcpcd"

	[[ ! -d "${statedir}/${iface}" ]] && mkdir -m 0755 -p "${statedir}/${iface}"
	eval dhcpcd "${opts}" "${iface}"
	eend $? || return 1

	# DHCP succeeded, show address retrieved
	local addr=$( interface_get_address "${iface}" )
	einfo "${iface} received address ${addr}"

	return 0
}

# vim:ts=4