]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Move service detection and interaction logic from the hooks to the
authorRoy Marples <roy@marples.name>
Thu, 9 Sep 2010 14:40:27 +0000 (14:40 +0000)
committerRoy Marples <roy@marples.name>
Thu, 9 Sep 2010 14:40:27 +0000 (14:40 +0000)
configure script. This makes the hooks a lot less messy.

Makefile
README
configure
dhcpcd-hooks/50-ntp.conf
dhcpcd-hooks/50-ypbind
dhcpcd-run-hooks.in

index 2fb80f366d2f7c458720a4f12a567046a1d91459..ea6431f3f1db46072359462d8d5e51d4b47e02de 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -29,10 +29,13 @@ FILESDIR=   ${SYSCONFDIR}
 
 SUBDIRS=       dhcpcd-hooks
 
-SED_DBDIR=     -e 's:@DBDIR@:${DBDIR}:g'
-SED_HOOKDIR=   -e 's:@HOOKDIR@:${HOOKDIR}:g'
-SED_SCRIPT=    -e 's:@SCRIPT@:${SCRIPT}:g'
-SED_SYS=       -e 's:@SYSCONFDIR@:${SYSCONFDIR}:g'
+SED_DBDIR=             -e 's:@DBDIR@:${DBDIR}:g'
+SED_HOOKDIR=           -e 's:@HOOKDIR@:${HOOKDIR}:g'
+SED_SERVICEEXISTS=     -e 's:@SERVICEEXISTS@:${SERVICEEXISTS}:g'
+SED_SERVICECMD=                -e 's:@SERVICECMD@:${SERVICECMD}:g'
+SED_SERVICESTATUS=     -e 's:@SERVICESTATUS@:${SERVICESTATUS}:g'
+SED_SCRIPT=            -e 's:@SCRIPT@:${SCRIPT}:g'
+SED_SYS=               -e 's:@SYSCONFDIR@:${SYSCONFDIR}:g'
 
 _DEPEND_SH=    test -e .depend && echo ".depend" || echo ""
 _DEPEND!=      ${_DEPEND_SH}
@@ -53,7 +56,9 @@ CLEANFILES+=  *.tar.bz2
 .SUFFIXES:     .in
 
 .in:
-       ${SED} ${SED_DBDIR} ${SED_HOOKDIR} ${SED_SCRIPT} ${SED_SYS} $< > $@
+       ${SED} ${SED_DBDIR} ${SED_HOOKDIR} ${SED_SCRIPT} ${SED_SYS} \
+               ${SED_SERVICEEXISTS} ${SED_SERVICECMD} ${SED_SERVICESTATUS} \
+               $< > $@
 
 all: config.h ${PROG} ${SCRIPTS} ${MAN5} ${MAN8}
 
diff --git a/README b/README
index 60301617696c07189f0b2e428c9a68526a52b46a..cfbf3cdf0f364732571036eac30609e53c2a6ba5 100644 (file)
--- a/README
+++ b/README
@@ -38,6 +38,10 @@ routes. You can find discussion here:
 BSD systems where this has been fixed are:
     NetBSD-5.0
 
+We try and detect how dhcpcd should interact with system services during the
+configure stage. If we cannot auto-detect how do to this, or it is wrong then
+you can change this by passing shell commands to --service-exists,
+--servicecmd and optionally --servicestatus.
 
 To prepare dhcpcd for import into a platform source tree (like NetBSD)
 you can use the make import target to create /tmp/dhcpcd-$version and
index 6201ba8c4c2e224d377a2f813396be6f6fce05f2..b3783a01993aabd8817c79020c055600dc3a9490 100755 (executable)
--- a/configure
+++ b/configure
@@ -47,6 +47,9 @@ for x; do
        --without-closefrom) CLOSEFROM=no;;
        --without-getline) GETLINE=no;;
        --without-strlcpy) STRLCPY=no;;
+       --serviceexists) SERVICEEXISTS=$var;;
+       --servicecmd) SERVICECMD=$var;;
+       --servicestatus) SERVICESTATUS=$var;;
        --includedir) eval INCLUDEDIR="$INCLUDEDIR${INCLUDEDIR:+ }$var";;
        --datadir|--infodir) ;; # ignore autotools
        --disable-maintainer-mode|--disable-dependency-tracking) ;;
@@ -360,6 +363,83 @@ if [ "$STRLCPY" = no ]; then
        echo "#include \"compat/strlcpy.h\"" >>$CONFIG_H
 fi
 
+if [ -z "$SERVICECMD" ]; then
+       printf "Checking for OpenRC ... "
+       if [ -x /sbin/rc-service ]; then
+               SERVICEEXISTS="/sbin/rc-service -e \$1"
+               SERVICECMD="/sbin/rc-service \$1 -- -D \$2"
+               echo "yes"
+       else
+               echo "no"
+       fi
+fi
+if [ -z "$SERVICECMD"]; then
+       printf "Checking for invoke-rc.d ... "
+       if [ -x /usr/sbin/invoke-rc.d ]; then
+               SERVICEEXISTS="/usr/sbin/invoke-rc.d --query --quiet \$1 start >/dev/null 2>&1 || [ \$? = 104 ]"
+               SERVICECMD="/usr/sbin/invoke-rc.d \$1 \$2"
+               echo "yes"
+       else
+               echo "no"
+       fi
+fi
+if [ -z "$SERVICECMD" ]; then
+       printf "Checking for service ... "
+       if [ -x /sbin/service ]; then
+               SERVICEEXISTS="/sbin/service \$1 >/dev/null 2>&1"
+               SERVICECMD="/sbin/service \$1 \$2"
+               echo "yes"
+       else
+               echo "no"
+       fi
+fi
+if [ -z "$SERVICECMD" ]; then
+       for x in /etc/init.d/rc.d /etc/rc.d /etc/init.d; do
+               printf "Checking for $x ... "
+               if [ -d $x ]; then
+                       SERVICEEXISTS="[ -x $x/\$1 ]"
+                       SERVICECMD="$x/\$1 \$2"
+                       echo "yes"
+                       break
+               else
+                       echo "no"
+               fi
+       done
+fi
+if [ -e /etc/arch-release ]; then
+       echo "Overriding service status check for Arch Linux"
+       SERVICESTATUS="[ -e /var/run/daemons/$1 ]"
+       echo "yes"
+fi
+
+if [ -z "$SERVICEEXISTS" -o -z "$SERVICECMD" ]; then
+       echo "WARNING! No means of interacting with system services detected!"
+       SERVICEEXISTS="return 1"
+       SERVICECMD="return 1"
+fi
+if [ -z "$SERVICESTATUS" ]; then
+       SERVICESTATUS="service_command \$1 status >/dev/null 2>&1"
+fi
+# Transform for a make file
+SERVICEEXISTS=$(echo "$SERVICEEXISTS" | sed \
+       -e 's:\\:\\\\:g' \
+       -e 's:\&:\\\&:g' \
+       -e 's:\$:\$\$:g' \
+)
+echo "SERVICEEXISTS=   $SERVICEEXISTS" >>config.mk
+SERVICECMD=$(echo "$SERVICECMD" | sed \
+       -e 's:\\:\\\\:g' \
+       -e 's:\&:\\\&:g' \
+       -e 's:\$:\$\$:g' \
+)
+echo "SERVICECMD=      $SERVICECMD" >>config.mk
+SERVICESTATUS=$(echo "$SERVICESTATUS" | sed \
+       -e 's:\\:\\\\:g' \
+       -e 's:\&:\\\&:g' \
+       -e 's:\$:\$\$:g' \
+)
+echo "SERVICESTATUS=   $SERVICESTATUS" >>config.mk
+
 HOOKS=
 if ! $HOOKSET; then
        printf "Checking for ntpd ... "
index da8f83cc523a8905cc7eaa69ecec54458c387981..1c6ea8062d8dfca885eee40155e9a4a0ff961470 100644 (file)
@@ -8,22 +8,12 @@
 #   NTP_CONF=/usr/pkg/etc/ntpd.conf
 # to use openntpd from pkgsrc instead of the system provided ntp.
 
-# Detect OpenRC, Debian or BSD rc
-# Distributions may want to just have their command here instead of this
-if type rc-service >/dev/null 2>&1 && rc-service --exists ntpd; then
-       ntpd_restart_cmd="rc-service ntpd -- -Ds restart"
-elif type invoke-rc.d >/dev/null 2>&1; then
-       if [ -x /etc/init.d/ntp ]; then
-               ntpd_restart_cmd="invoke-rc.d ntp try-restart"
-       fi
-       # Debian as a seperate file for DHCP config to avoid stamping on
+: ${ntpd_restart_cmd:="service_condcommand ntpd restart || service_condcommand ntp restart"}
+if type invoke-rc.d >/dev/null 2>&1; then
+       # Debian has a seperate file for DHCP config to avoid stamping on
        # the master.
        [ -e /var/lib/ntp ] || mkdir /var/lib/ntp
        : ${NTP_DHCP_CONF:=/var/lib/ntp/ntp.conf.dhcp}
-elif [ -x /etc/rc.d/ntpd ]; then
-       ntpd_restart_cmd="/etc/rc.d/ntpd status >/dev/null 2>&1 && /etc/rc.d/ntpd restart"
-elif [ -x /usr/local/etc/rc.d/ntpd ]; then
-       ntpd_restart_cmd="/usr/local/etc/rc.d/ntpd status >/dev/null 2>&1 && /usr/local/etc/rc.d/ntpd restart"
 fi
 
 ntp_conf_dir="$state_dir/ntp.conf"
@@ -53,18 +43,18 @@ build_ntp_conf()
                fi
        fi
 
-       if [ -n "$NTP_DHCP_CONF" ]; then
-               cp "$ntp_conf" "$NTP_DHCP_CONF"
-               ntp_conf="$NTP_DHCP_CONF"
-       fi
-
        # Merge our config into ntp.conf
        [ -e "$cf" ] && rm -f "$cf"
        [ -d "$ntp_conf_dir" ] || mkdir -p "$ntp_conf_dir"
-       if [ -e "$ntp_conf" ]; then
+
+       if [ -n "$NTP_DHCP_CONF" ]; then
+               cp "$ntp_conf" "$cf"
+               ntp_conf="$NTP_DHCP_CONF"
+       elif [ -e "$ntp_conf" ]; then
                remove_markers "$signature_base" "$signature_base_end" \
                        "$ntp_conf" > "$cf"
        fi
+
        if [ -n "$servers" ]; then
                echo "$signature_base${header:+ $from }$header" >> "$cf"
                printf "$search$servers" >> "$cf"
index 3e569c73c6582e74034479492ca865c1a533fef6..780bbc1f0f944f68e01b4c5b83077b3df337d285 100644 (file)
@@ -1,15 +1,8 @@
 # Sample dhcpcd hook for ypbind
 # This script is only suitable for the Linux version.
 
-# Distributions may want to just have their command here instead of this
-if [ -x /etc/rc.d/ypbind ]; then
-       ypbind_restart_cmd="/etc/rc.d/ypbind restart"
-       ypbind_stop_cmd="/etc/rc.d/ypbind stop"
-elif [ -x /usr/local/etc/rc.d/ypbind ]; then
-       ypbind_restart_cmd="/usr/local/etc/rc.d/ypbind restart"
-       ypbind_stop_cmd="/usr/local/etc/rc.d/ypbind stop"
-fi
-
+: ${ypbind_restart_cmd:="service_command ypbind restart"}
+: ${ypbind_stop_cmd:="service_condcommand ypbind stop"}
 ypbind_dir="$state_dir/ypbind"
 
 best_domain()
index 46ed6d55835e67f44a09ed86e895023f1743df2c..926a2e11546b13bfad0e893397aeba2ffe66eb09 100644 (file)
@@ -147,6 +147,33 @@ syslog()
        fi
 }
 
+# Check a system service exists 
+service_exists()
+{
+       @SERVICEEXISTS@
+}
+
+# Send a command to a system service
+service_cmd()
+{
+       @SERVICECMD@
+}
+
+# Send a command to a system service if it is running
+service_status()
+{
+       @SERVICESTATUS@
+}
+
+# Handy macros for our hooks
+service_command()
+{
+       service_exists $1 && service_cmd $1 $2
+}
+service_condcommand()
+{
+       service_exists $1 && service_status $1 && service_cmd $1 $2
+}
 
 # We source each script into this one so that scripts run earlier can
 # remove variables from the environment so later scripts don't see them.