summaryrefslogtreecommitdiff
path: root/extra/dhcp/dhcpd.initd
blob: f69ae71dee213e12d7fa2228428eccbc146263a4 (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
#!/sbin/openrc-run
# Copyright 1999-2011 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/net-misc/dhcp/files/dhcpd.init5,v 1.1 2011/12/04 22:45:07 vapier Exp $

extra_commands="configtest"

: ${DHCPD_CONF:=/etc/dhcp/${SVCNAME}.conf}

depend() {
	need net
	after firewall
	use logger dns
}

get_var() {
	local var="$(sed -n 's/^[[:blank:]]\?'"$1"' "*\([^#";]\+\).*/\1/p' "${chroot}${DHCPD_CONF}")"
	echo ${var:-$2}
}

checkconfig() {
	set -- ${DHCPD_OPTS} ${chroot:+-chroot} ${chroot} -t

	dhcpd "$@" 1>/dev/null 2>&1
	local ret=$?
	if [ ${ret} -ne 0 ] ; then
		eerror "${SVCNAME} has detected a syntax error in your configuration files:"
		dhcpd "$@"
	fi

	return ${ret}
}

configtest() {
	local chroot=${DHCPD_CHROOT%/}

	ebegin "Checking ${SVCNAME} configuration"
	checkconfig
	eend $?
}

start() {
	local chroot=${DHCPD_CHROOT%/}

	# Work out our cffile if it's in our DHCPD_OPTS
	case " ${DHCPD_OPTS} " in
		*" -cf "*)
			DHCPD_CONF=" ${DHCPD_OPTS} "
			DHCPD_CONF="${DHCPD_CONF##* -cf }"
			DHCPD_CONF="${DHCPD_CONF%% *}"
			;;
		*)	DHCPD_OPTS="${DHCPD_OPTS} -cf ${DHCPD_CONF}"
			;;
	esac

	if [ -n "${chroot}" ] ; then
		# the config test want's these to exist
		mkdir -p \
			"${chroot}"/run/dhcp \
			"${chroot}"/var/lib/dhcp \
			"${chroot}"/etc/dhcp
	fi

	# see comment in get_var() above
	if [ ! -f "${chroot}${DHCPD_CONF}" ] ; then
		eerror "${chroot}${DHCPD_CONF} does not exist"
		return 1
	fi

	checkconfig || return 1

	checkpath -d -o dhcp:dhcp "${chroot}"/run/dhcp "${chroot}"/var/lib/dhcp

	local leasefile="$(get_var lease-file-name /var/lib/dhcp/${SVCNAME}.leases)"
	checkpath -f -o dhcp:dhcp "${chroot}${leasefile}"

	# Setup LD_PRELOAD so name resolution works in our chroot.
	if [ -n "${chroot}" ] ; then
		checkpath -d -o root:root -m 755 "${chroot}"/dev "${chroot}"/etc "${chroot}"/proc
		cp -pP /etc/localtime /etc/resolv.conf "${chroot}"/etc/
		export LD_PRELOAD="${LD_PRELOAD} libresolv.so libnss_dns.so"
		if ! mountinfo -q "${chroot}/proc" ; then
			mount --bind /proc "${chroot}/proc"
		fi
	fi

	local pidfile="$(get_var pid-file-name /run/dhcp/${SVCNAME}.pid)"

	ebegin "Starting ${chroot:+chrooted }${SVCNAME}"
	start-stop-daemon --start --exec /usr/sbin/dhcpd \
		--pidfile "${chroot}/${pidfile}" \
		-- ${DHCPD_OPTS} -q -pf "${pidfile}" -lf "${leasefile}" \
		-user dhcp -group dhcp \
		${chroot:+-chroot} ${chroot} ${DHCPD_IFACE}
	eend $? \
		&& save_options chroot "${chroot}" \
		&& save_options pidfile "${pidfile}"
}

stop() {
	local chroot="$(get_options chroot)"

	ebegin "Stopping ${chroot:+chrooted }${SVCNAME}"
	start-stop-daemon --stop --exec /usr/sbin/dhcpd \
		--pidfile "${chroot}/$(get_options pidfile)"
	res=$?

	if [ ${res} -eq 0 ] && [ -n "${chroot}" ] ; then
		if mountinfo -q "${chroot}/proc" ; then
			umount "${chroot}/proc"
		fi
	fi

	eend $res
}