[Buildroot] [PATCH v2 8/8] package/dhcp: SysV init scripts: refactor using functions

Benoît Thébaudeau benoit at wsystem.com
Fri Aug 21 16:45:19 UTC 2015


Refactor these scripts using start()/stop() functions rather than having
these scripts invoke themselves.

By the way, clean up these scripts.

Signed-off-by: Benoît Thébaudeau <benoit at wsystem.com>
---
 package/dhcp/S80dhcp-relay  | 63 +++++++++++++++++++++-----------------
 package/dhcp/S80dhcp-server | 74 +++++++++++++++++++++++++--------------------
 2 files changed, 76 insertions(+), 61 deletions(-)

diff --git a/package/dhcp/S80dhcp-relay b/package/dhcp/S80dhcp-relay
index c6f7f0f..2f9ed50 100755
--- a/package/dhcp/S80dhcp-relay
+++ b/package/dhcp/S80dhcp-relay
@@ -1,61 +1,68 @@
 #!/bin/sh
-#
-# $Id: dhcp3-relay,v 1.1 2004/04/16 15:41:08 ml Exp $
-#
+
+NAME="dhcrelay"
+DESC="DHCP relay"
+DAEMON="/usr/sbin/${NAME}"
+
+# On what interfaces should the DHCP relay serve DHCP requests?
+INTERFACES=""
 
 # What servers should the DHCP relay forward requests to?
-# e.g: SERVERS="192.168.0.1"
+# E.g: SERVERS="192.168.0.1"
 SERVERS=""
 
-# On what interfaces should the DHCP relay (dhrelay) serve DHCP requests?
-INTERFACES=""
-
 # Additional options that are passed to the DHCP relay daemon?
 OPTIONS=""
 
 # Read configuration variable file if it is present
-CFG_FILE="/etc/default/dhcrelay"
+CFG_FILE="/etc/default/${NAME}"
 [ -r "${CFG_FILE}" ] && . "${CFG_FILE}"
 
 # PID files generated by the daemon
-PID_FILES="/var/run/dhcrelay.pid /var/run/dhcrelay6.pid"
+PID_FILES="/var/run/${NAME}.pid /var/run/${NAME}6.pid"
 
 # Sanity checks
-test -f /usr/sbin/dhcrelay || exit 0
+test -f "${DAEMON}" || exit 0
 test -n "$INTERFACES" || exit 0
 test -n "$SERVERS" || exit 0
 
-# Build command line for interfaces (will be passed to dhrelay below.)
+# Build command line for interfaces (will be passed to dhcrelay below).
 IFCMD=""
 for I in $INTERFACES; do
 	IFCMD=${IFCMD}"-i "${I}" "
 done
 
-DHCRELAYPID=/var/run/dhcrelay.pid
+start()
+{
+	echo -n "Starting ${DESC}: "
+	start-stop-daemon -S -q -x "${DAEMON}" -- -q $OPTIONS $IFCMD $SERVERS &&
+			echo "OK" || echo "FAIL"
+}
+
+stop()
+{
+	echo -n "Stopping ${DESC}: "
+	if start-stop-daemon -K -q -x "${DAEMON}"; then
+		# This daemon does not remove its PID file when it exits.
+		rm -f ${PID_FILES}
+		echo "OK"
+	else
+		echo "FAIL"
+	fi
+}
 
 case "$1" in
 	start)
-		echo -n "Starting DHCP relay: "
-		start-stop-daemon -S -q -x /usr/sbin/dhcrelay -- -q $OPTIONS $IFCMD $SERVERS
-		[ $? = 0 ] && echo "OK" || echo "FAIL"
+		start
 		;;
 	stop)
-		echo -n "Stopping DHCP relay: "
-		if start-stop-daemon -K -q -x /usr/sbin/dhcrelay; then
-			# This daemon does not remove its PID file when it exits.
-			rm -f ${PID_FILES}
-			echo "OK"
-		else
-			echo "FAIL"
-		fi
+		stop
 		;;
-	restart | force-reload)
-		$0 stop
-		$0 start
+	restart|force-reload)
+		stop
+		start
 		;;
 	*)
 		echo "Usage: $0 {start|stop|restart|force-reload}"
 		exit 1
 esac
-
-exit 0
diff --git a/package/dhcp/S80dhcp-server b/package/dhcp/S80dhcp-server
index d3652b4..5b6e861 100755
--- a/package/dhcp/S80dhcp-server
+++ b/package/dhcp/S80dhcp-server
@@ -1,57 +1,65 @@
 #!/bin/sh
-#
-# $Id: dhcp3-server.init.d,v 1.4 2003/07/13 19:12:41 mdz Exp $
-#
 
-# On what interfaces should the DHCP server (dhcpd) serve DHCP requests?
-#       Separate multiple interfaces with spaces, e.g. "eth0 eth1".
+NAME="dhcpd"
+DESC="DHCP server"
+DAEMON="/usr/sbin/${NAME}"
+DAEMON_CONF="/etc/dhcp/${NAME}.conf"
+DAEMON_LIB_DIR="/var/lib/dhcp"
+LEASE_FILES="${DAEMON_LIB_DIR}/${NAME}.leases ${DAEMON_LIB_DIR}/${NAME}6.leases"
+
+# On what interfaces should the DHCP server serve DHCP requests?
+# Separate multiple interfaces with spaces, e.g. "eth0 eth1".
 INTERFACES=""
 
 # Additional options that are passed to the DHCP server daemon?
 OPTIONS=""
 
 # Read configuration variable file if it is present
-CFG_FILE="/etc/default/dhcpd"
+CFG_FILE="/etc/default/${NAME}"
 [ -r "${CFG_FILE}" ] && . "${CFG_FILE}"
 
 # PID files generated by the daemon
-PID_FILES="/var/run/dhcpd.pid /var/run/dhcpd6.pid"
+PID_FILES="/var/run/${NAME}.pid /var/run/${NAME}6.pid"
 
 # Sanity checks
-test -f /usr/sbin/dhcpd || exit 0
-test -f /etc/dhcp/dhcpd.conf || exit 0
+test -f "${DAEMON}" || exit 0
+test -f "${DAEMON_CONF}" || exit 0
+
+start()
+{
+	echo -n "Starting ${DESC}: "
+	test -d "${DAEMON_LIB_DIR}" || mkdir -p "${DAEMON_LIB_DIR}"
+	for lease_file in ${LEASE_FILES}; do
+		test -f "${lease_file}" || touch "${lease_file}"
+	done
+	start-stop-daemon -S -q -x "${DAEMON}" -- -q $OPTIONS $INTERFACES &&
+			echo "OK" || echo "FAIL"
+}
+
+stop()
+{
+	echo -n "Stopping ${DESC}: "
+	if start-stop-daemon -K -q -x "${DAEMON}"; then
+		# This daemon does not remove its PID file when it exits.
+		rm -f ${PID_FILES}
+		echo "OK"
+	else
+		echo "FAIL"
+	fi
+}
 
 case "$1" in
 	start)
-		echo -n "Starting DHCP server: "
-		test -d /var/lib/dhcp/ || mkdir -p /var/lib/dhcp/
-		for lease_file in /var/lib/dhcp/dhcpd.leases \
-					/var/lib/dhcp/dhcpd6.leases; do
-			test -f "${lease_file}" || touch "${lease_file}"
-		done
-		start-stop-daemon -S -q -x /usr/sbin/dhcpd -- -q $OPTIONS $INTERFACES
-		[ $? = 0 ] && echo "OK" || echo "FAIL"
+		start
 		;;
 	stop)
-		echo -n "Stopping DHCP server: "
-		if start-stop-daemon -K -q -x /usr/sbin/dhcpd; then
-			# This daemon does not remove its PID file when it exits.
-			rm -f ${PID_FILES}
-			echo "OK"
-		else
-			echo "FAIL"
-		fi
+		stop
 		;;
-	restart | force-reload)
-		$0 stop
-		$0 start
-		if [ "$?" != "0" ]; then
-			exit 1
-		fi
+	restart|force-reload)
+		stop
+		start
 		;;
 	*)
 		echo "Usage: $0 {start|stop|restart|force-reload}"
 		exit 1
 esac
-
-exit 0
-- 
2.1.4



More information about the buildroot mailing list