summaryrefslogtreecommitdiffstats
path: root/main/dhcp/dhcpd.initd
blob: 447578c90729a9ce36ea803a317b5bcc20634322 (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
#!/sbin/runscript
# Copyright 1999-2009 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/net-misc/dhcp/files/dhcpd.init2,v 1.1 2009/07/09 14:45:22 chainsaw Exp $

opts="configtest"

DHCPD_CONF=${DHCPD_CONF:-/etc/dhcp/dhcpd.conf}

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

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

checkconfig() {
	/usr/sbin/dhcpd -cf ${DHCPD_CHROOT}/${DHCPD_CONF} -t 1>/dev/null 2>&1
	ret=$?
	if [ $ret -ne 0 ]; then
		eerror "${SVCNAME} has detected a syntax error in your configuration files:"
		/usr/sbin/dhcpd -cf ${DHCPD_CHROOT}/${DHCPD_CONF} -t
	fi

	return $ret
}

configtest() {
	ebegin "Checking ${SVCNAME} configuration"
	checkconfig
	eend $?
}

start() {
	# Work out our cffile if it's on 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 [ ! -f "${DHCPD_CHROOT}/${DHCPD_CONF}" ] ; then
		eerror "${DHCPD_CHROOT}/${DHCPD_CONF} does not exist"
		return 1
	fi

	checkconfig || return 1

	local leasefile="$(get_var lease-file-name)"
	leasefile="${DHCPD_CHROOT}/${leasefile:-/var/lib/dhcp/dhcpd.leases}"
	if [ ! -f "${leasefile}" ] ; then
		ebegin "Creating ${leasefile}"
		touch "${leasefile}"
		chown dhcp:dhcp "${leasefile}"
		eend $? || return 1
	fi

	# Setup LD_PRELOAD so name resolution works in our chroot.
	if [ -n "${DHCPD_CHROOT}" ] ; then
	    LD_PRELOAD="${LD_PRELOAD} /usr/lib/libresolv.so"
	    export LD_PRELOAD="${LD_PRELOAD} /usr/lib/libnss_dns.so"
	fi

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

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

stop() {
	local chroot="$(get_options chroot)"
	
	checkconfig || return 1

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