From: Roy Marples Date: Thu, 9 Sep 2010 14:40:27 +0000 (+0000) Subject: Move service detection and interaction logic from the hooks to the X-Git-Tag: v5.2.9~25 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=aab70680828e2cf7211617683c6716091b72c5ca;p=thirdparty%2Fdhcpcd.git Move service detection and interaction logic from the hooks to the configure script. This makes the hooks a lot less messy. --- diff --git a/Makefile b/Makefile index 2fb80f36..ea6431f3 100644 --- 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 60301617..cfbf3cdf 100644 --- 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 diff --git a/configure b/configure index 6201ba8c..b3783a01 100755 --- 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 ... " diff --git a/dhcpcd-hooks/50-ntp.conf b/dhcpcd-hooks/50-ntp.conf index da8f83cc..1c6ea806 100644 --- a/dhcpcd-hooks/50-ntp.conf +++ b/dhcpcd-hooks/50-ntp.conf @@ -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" diff --git a/dhcpcd-hooks/50-ypbind b/dhcpcd-hooks/50-ypbind index 3e569c73..780bbc1f 100644 --- a/dhcpcd-hooks/50-ypbind +++ b/dhcpcd-hooks/50-ypbind @@ -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() diff --git a/dhcpcd-run-hooks.in b/dhcpcd-run-hooks.in index 46ed6d55..926a2e11 100644 --- a/dhcpcd-run-hooks.in +++ b/dhcpcd-run-hooks.in @@ -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.