]> git.pld-linux.org Git - packages/dhcp.git/blobdiff - dhcp.init
- skip if appropriate network family is disabled
[packages/dhcp.git] / dhcp.init
index 6ac0f8a8288955a71d9cc8d2e764c6818410997e..cf5b726f88b7078b5cf7f5df657f729a4230c031 100644 (file)
--- a/dhcp.init
+++ b/dhcp.init
@@ -14,7 +14,7 @@
 [ -f /etc/sysconfig/dhcpd ] && . /etc/sysconfig/dhcpd
 
 # Check that networking is up.
-if is_yes "${NETWORKING}"; then
+if is_yes "${IPV4_NETWORKING}"; then
        if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then
                msg_network_down "DHCP Server"
                exit 1
@@ -23,78 +23,130 @@ else
        exit 0
 fi
 
-checkconfig() {
-       show "Checking %s configuration" "DHCP Server"
-       out=`/sbin/dhcpd -t 2>&1`; rc=$?
-       if [ $rc -gt 0 ]; then
-               fail
-               echo >&2 "$out"
+check_device_up()
+{
+       local DEVICE=$1
+       if LC_ALL=C ip addr show dev $DEVICE | grep -q inet; then
+               return 0
        else
-               ok
+               return 1
        fi
+}
+
+# configtest itself
+configtest() {
+       local rc=0
+       /sbin/dhcpd -4 -q -t -T || rc=$?
+
+       # check if interfaces specified exist and have addresses
+       for i in $DHCPD_INTERFACES; do
+               if ! check_device_up $i; then
+                       echo >&2 "Device '$i' does not exist or has no address configured"
+                       rc=1
+               fi
+       done
+
        return $rc
 }
 
+# wrapper for configtest
+checkconfig=-1
+checkconfig() {
+       local details=${1:-0}
+
+       # run checkconfig only once
+       if [ "$checkconfig" != -1 ]; then
+               return $checkconfig
+       fi
+
+       if [ $details = 1 ]; then
+               # run config test and display report (status action)
+               show "Checking %s configuration" "DHCP Server"; busy
+               local out
+               out=$(configtest 2>&1)
+               checkconfig=$?
+               if [ $checkconfig = 0 ]; then
+                       ok
+               else
+                       fail
+               fi
+               [ "$out" ] && echo >&2 "$out"
+               RETVAL=$checkconfig
+       else
+               # run config test and abort with nice message if failed
+               # (for actions checking status before action).
+               configtest >/dev/null 2>&1
+               checkconfig=$?
+               if [ $checkconfig != 0 ]; then
+                       show "Checking %s configuration" "DHCP Server"; fail
+                       nls 'Configuration test failed. See details with %s "checkconfig"' $0
+                       exit $checkconfig
+               fi
+       fi
+}
+
 start() {
        # Check if the service is already running?
-       if [ ! -f /var/lock/subsys/dhcpd ]; then
-               msg_starting "DHCP Server"
-               daemon /sbin/dhcpd $DHCPD_INTERFACES
-               RETVAL=$?
-               [ $RETVAL -eq 0 ] && touch /var/lock/subsys/dhcpd
-       else
+       if [ -f /var/lock/subsys/dhcpd ]; then
                msg_already_running "DHCP Server"
+               return
        fi
+
+       checkconfig
+       msg_starting "DHCP Server"
+       daemon /sbin/dhcpd -4 -q $DHCPD_INTERFACES
+       RETVAL=$?
+       [ $RETVAL -eq 0 ] && touch /var/lock/subsys/dhcpd
 }
 
 stop() {
-       if [ -f /var/lock/subsys/dhcpd ]; then
-               msg_stopping "DHCP Server"
-               killproc dhcpd
-               rm -f /var/run/dhcpd.pid /var/lock/subsys/dhcpd >/dev/null 2>&1
-       else
+       if [ ! -f /var/lock/subsys/dhcpd ]; then
                msg_not_running "DHCP Server"
+               return
        fi
+
+       msg_stopping "DHCP Server"
+       killproc --pidfile /var/run/dhcpd.pid dhcpd
+       rm -f /var/run/dhcpd.pid /var/lock/subsys/dhcpd >/dev/null 2>&1
 }
 
 condrestart() {
-       if [ -f /var/lock/subsys/dhcpd ]; then
-               stop
-               start
-       else
-               msg_not_running dhcpd
+       if [ ! -f /var/lock/subsys/dhcpd ]; then
+               msg_not_running "DHCP Server"
                RETVAL=$1
+               return
        fi
+
+       checkconfig
+       stop
+       start
 }
 
 RETVAL=0
 # See how we were called.
 case "$1" in
   start)
-       checkconfig || exit 1
        start
        ;;
   stop)
        stop
        ;;
   restart)
-       checkconfig || exit 1
+       checkconfig
        stop
        start
        ;;
   try-restart)
-       checkconfig || exit 1
        condrestart 0
        ;;
   force-reload)
-       checkconfig || exit 1
        condrestart 7
        ;;
-  checkconfig)
-       checkconfig
+  checkconfig|configtest)
+       checkconfig 1
        ;;
   status)
-       status dhcpd
+       status --pidfile /var/run/dhcpd.pid dhcpd
        exit $?
        ;;
   *)
This page took 0.074426 seconds and 4 git commands to generate.