]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Split dhcpcd.sh into smaller hook scripts, so that dhcpcd.sh just provide some handy...
authorRoy Marples <roy@marples.name>
Tue, 20 May 2008 10:55:25 +0000 (10:55 +0000)
committerRoy Marples <roy@marples.name>
Tue, 20 May 2008 10:55:25 +0000 (10:55 +0000)
16 files changed:
Makefile
dhcpcd.8.in
dhcpcd.sh.8.in
dhcpcd.sh.in
hook.d/01-test.sh [new file with mode: 0644]
hook.d/10-resolv.conf.sh [new file with mode: 0644]
hook.d/14-lookup-hostname.sh [moved from hook-samples/lookup_hostname.sh with 52% similarity]
hook.d/15-hostname.sh [new file with mode: 0644]
hook.d/50-dhcpcd-compat.sh [moved from hook-samples/dhcpcd-compat.sh with 100% similarity]
hook.d/50-ntp.sh [moved from hook-samples/ntp.sh with 100% similarity]
hook.d/50-ypbind.sh [moved from hook-samples/ypbind.sh with 100% similarity]
hook.d/Makefile [new file with mode: 0644]
mk/files.mk
mk/prog.mk
mk/scripts.mk
mk/sys.mk [new file with mode: 0644]

index bbf1d14922256e460183d75c9a7ff3bef2ed986a..0a46c3b8163e385272042dbdc20417806c101430 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -24,6 +24,8 @@ CPPFLAGS+=    -DSYSCONFDIR=\"${SYSCONFDIR}\"
 CPPFLAGS+=     -DDBDIR=\"${DBDIR}\"
 LDADD+=                ${LIBRT}
 
+SUBDIRS=       hook.d
+
 .SUFFIXES:     .in .sh.in
 
 .in:
index 4e1b97f7fc0b7f67a0031e1d5480ba429ccd3b81..d3a30fa1756a15e309f2e36fa71e9464b8262d44 100644 (file)
@@ -22,7 +22,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd May 12, 2008
+.Dd May 20, 2008
 .Dt DHCPCD 8 SMM
 .Sh NAME
 .Nm dhcpcd
@@ -108,11 +108,11 @@ option. This script will configure
 .Pa /etc/resolv.conf
 and the
 .Xr hostname 3
-if possible. It will also run custom hook scripts if defined. See
+if possible. You can customize its behaviour through hook scripts. See
 .Xr dhcpcd.sh 8
 for more details.
 .Nm
-ignores the exit code of the script.
+currently ignores the exit code of the script.
 .Ss Fine tuning
 You can fine tune the behaviour of
 .Nm
index ce8a575dcd8f849b0b86c56429e5b3394ae24051..3568366741bf49ceb5206a77143b9d9ab815e81d 100644 (file)
@@ -22,7 +22,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd May 02, 2008
+.Dd May 20, 2008
 .Dt DHCPCD.SH 8 SMM
 .Sh NAME
 .Nm dhcpcd.sh
@@ -31,9 +31,9 @@
 .Nm
 is used by
 .Xr dhcpcd 8
-to configure
+to run any system or user defined hook scripts, which include configuring
 .Pa /etc/resolv.conf
-and run any system or user defined hook scripts.
+and the hostname.
 Each time
 .Nm
 is invoked,
@@ -78,18 +78,13 @@ interface. This is primarily used to test the variables are filled correctly
 for the script to process them.
 .El
 .Sh FILES 
-Before
+When
 .Nm
 runs, it loads
-.Pa @SYSCONFDIR@/dhcpcd-enter-hook
+.Pa @SYSCONFDIR@/dhcpcd-hook
 and any scripts found in
-.Pa @SYSCONFDIR@/dhcpcd-enter-hook.d .
-Then it runs itself, configuring
-.Pa /etc/resolv.conf .
-Finally it loads
-.Pa @SYSCONFDIR@/dhcpcd-exit-hook
-and any scripts found in
-.Pa @SYSCONFDIR@/dhcpcd-exit-hook.d .
+.Pa @SYSCONFDIR@/dhcpcd-hook.d
+in a lexical order.
 .Sh SEE ALSO
 .Xr dhcpcd 8
 .Sh AUTHORS
index 7e080254351f1d71a609caf36bc1a3349f0c5d03..4715101a8512df4f177e879735e7b51953bb00d2 100644 (file)
 #!/bin/sh
-# dhcpcd - DHCP client daemon
-# Copyright 2006-2008 Roy Marples <roy@marples.name>
-# All rights reserved
-
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions
-# are met:
-# 1. Redistributions of source code must retain the above copyright
-#    notice, this list of conditions and the following disclaimer.
-# 2. Redistributions in binary form must reproduce the above copyright
-#    notice, this list of conditions and the following disclaimer in the
-#    documentation and/or other materials provided with the distribution.
-#
-# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
-# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
-# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
-# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
-# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-# SUCH DAMAGE.
-
-do_hooks()
-{
-       local x= r=0
-       for x in @SYSCONFDIR@/dhcpcd-"$1"-hook.d/* \
-               @SYSCONFDIR@/dhcpcd-"$1"-hook \
-       ; do
-               if [ -e "${x}" ]; then
-                       . "${x}"
-                       r=$((${r} + $?))
-               fi
-       done
-       return ${r}
-}
+# dhcpcd client configuration script 
 
+# Handy functions for our hooks to use
+signature="# Generated by dhcpcd for ${interface}"
 save_conf()
 {
-       if [ -e "$1" ]; then
+       if [ -f "$1" ]; then
                rm -f "$1"-pre."${interface}"
                mv -f "$1" "$1"-pre."${interface}"
        fi
 }
-
 restore_conf()
 {
-       [ -e "$1"-pre."${interface}" ] || return 1
+       [ -f "$1"-pre."${interface}" ] || return 1
        rm -f "$1"
        mv -f "$1"-pre."${interface}" "$1"
 }
 
-make_resolv_conf()
-{
-       if [ -z "${new_domain_name_servers}" -a \
-               -z "${new_domain_name}" -a \
-               -z "${new_domain_search}" ]; then
-               return 0
-       fi
-       local x= conf="${signature}\n"
-       if [ -n "${new_domain_search}" ]; then
-               conf="${conf}search ${new_domain_search}\n"
-       elif [ -n "${new_domain_name}" ]; then
-               conf="${conf}search ${new_domain_name}\n"
-       fi
-       for x in ${new_domain_name_servers}; do
-               conf="${conf}nameserver ${x}\n"
-       done
-       if type resolvconf >/dev/null 2>&1; then
-               printf "${conf}" | resolvconf -a "${interface}"
-       else
-               save_conf /etc/resolv.conf
-               printf "${conf}" > /etc/resolv.conf
-       fi
-}
-
-restore_resolv_conf()
-{
-       if type resolvconf >/dev/null 2>&1; then
-               resolvconf -d "${interface}" -f
-       else
-               restore_conf /etc/resolv.conf || return 0
+for hook in \
+       @SYSCONFDIR@/dhcpcd-hook \
+       @SYSCONFDIR@/dhcpcd-hook.d/* \
+do
+       if [ -f "${x}" ]; then
+               . "${x}"
        fi
-}
-
-need_hostname()
-{
-       case "$(hostname)" in
-               ""|"(none)"|localhost)  [ -n "${new_host_name}" ];;
-               "${old_host_name}")     true;;
-               *)                      false;;
-       esac
-}
-
-set_hostname()
-{
-       if need_hostname; then
-               hostname "${new_host_name}"
-       fi
-}
-
-run_test()
-{
-       env | grep "^\(interface\|pid\|reason\)="
-       env | grep "^\(new_\|old_\)" | sort
-}
-
-signature="# Generated by dhcpcd for ${interface}"
-
-# We should do something with exit codes really
-do_hooks enter
-case "${reason}" in
-       TEST)
-               run_test
-               ;;
-       BOUND|INFORM|REBIND|REBOOT|RENEW|TIMEOUT)
-               make_resolv_conf
-               set_hostname
-               ;;
-       EXPIRE|FAIL|IPV4LL|RELEASE|STOP)
-               restore_resolv_conf
-               ;;
-       *)
-               echo "unsupported reason ${reason}" >&2
-               false
-               ;;
-esac
-do_hooks exit
+done
diff --git a/hook.d/01-test.sh b/hook.d/01-test.sh
new file mode 100644 (file)
index 0000000..3306cab
--- /dev/null
@@ -0,0 +1,6 @@
+# Just echo our DHCP options we have
+
+if [ "${reason}" = "TEST" ]; then
+       env | grep "^\(interface\|pid\|reason\)="
+       env | grep "^\(new_\|old_\)" | sort
+fi
diff --git a/hook.d/10-resolv.conf.sh b/hook.d/10-resolv.conf.sh
new file mode 100644 (file)
index 0000000..e91916c
--- /dev/null
@@ -0,0 +1,40 @@
+# Generate /etc/resolv.conf
+# Support resolvconf(8) if available
+
+make_resolv_conf()
+{
+       if [ -z "${new_domain_name_servers}" -a \
+               -z "${new_domain_name}" -a \
+               -z "${new_domain_search}" ]; then
+               return 0
+       fi
+       local x= conf="${signature}\n"
+       if [ -n "${new_domain_search}" ]; then
+               conf="${conf}search ${new_domain_search}\n"
+       elif [ -n "${new_domain_name}" ]; then
+               conf="${conf}search ${new_domain_name}\n"
+       fi
+       for x in ${new_domain_name_servers}; do
+               conf="${conf}nameserver ${x}\n"
+       done
+       if type resolvconf >/dev/null 2>&1; then
+               printf "${conf}" | resolvconf -a "${interface}"
+       else
+               save_conf /etc/resolv.conf
+               printf "${conf}" > /etc/resolv.conf
+       fi
+}
+
+restore_resolv_conf()
+{
+       if type resolvconf >/dev/null 2>&1; then
+               resolvconf -d "${interface}" -f
+       else
+               restore_conf /etc/resolv.conf || return 0
+       fi
+}
+
+case "${reason}" in
+       BOUND|INFORM|REBIND|REBOOT|RENEW|TIMEOUT)       make_resolv_conf;;
+       EXPIRE|FAIL|IPV4LL|RELEASE|STOP)                restore_resolv_conf;;
+esac
similarity index 52%
rename from hook-samples/lookup_hostname.sh
rename to hook.d/14-lookup-hostname.sh
index c829b2d62ae7bef90310ed9c96a73ca453ad014a..6818c39223ac5c80f58a11f0bbf97bc29c8a5285 100644 (file)
@@ -1,4 +1,4 @@
-# Sample exit hook to lookup the hostname in DNS if not set
+# Lookup the hostname in DNS if not set
 
 lookup_hostname()
 {
@@ -21,26 +21,13 @@ lookup_hostname()
        return 1
 }
 
-do_hostname()
+set_hostname()
 {
-       if [ -z "${new_host_name}" ] && need_hostname; then
-               local hname="$(lookup_hostname)"
-               if [ -n "${hname}" ]; then
-                       hostname "${hname}"
-               fi
+       if [ -z "${new_host_name}" ]
+               export new_host_name="$(lookup_hostname)"
        fi
 }
 
 case "${reason}" in
-       TEST)
-               ;;
-       BOUND|INFORM|REBIND|REBOOT|RENEW|TIMEOUT)
-               do_hostname
-               ;;
-       EXPIRE|FAIL|IPV4LL|RELEASE|STOP)
-               ;;
-       *)
-               echo "lookup_hostname: unsupported reason ${reason}" >&2
-               false
-               ;;
+       BOUND|INFORM|REBIND|REBOOT|RENEW|TIMEOUT)       set_hostname;;
 esac
diff --git a/hook.d/15-hostname.sh b/hook.d/15-hostname.sh
new file mode 100644 (file)
index 0000000..2ba42d5
--- /dev/null
@@ -0,0 +1,21 @@
+# Set the hostname from DHCP data if required
+
+need_hostname()
+{
+       case "$(hostname)" in
+               ""|"(none)"|localhost)  [ -n "${new_host_name}" ];;
+               "${old_host_name}")     true;;
+               *)                      false;;
+       esac
+}
+
+set_hostname()
+{
+       if need_hostname; then
+               hostname "${new_host_name}"
+       fi
+}
+
+case "${reason}" in
+       BOUND|INFORM|REBIND|REBOOT|RENEW|TIMEOUT)       set_hostname;;
+esac
similarity index 100%
rename from hook-samples/ntp.sh
rename to hook.d/50-ntp.sh
similarity index 100%
rename from hook-samples/ypbind.sh
rename to hook.d/50-ypbind.sh
diff --git a/hook.d/Makefile b/hook.d/Makefile
new file mode 100644 (file)
index 0000000..a0fd00c
--- /dev/null
@@ -0,0 +1,9 @@
+SYSCONFDIR?=   /etc/dhcpcd
+
+SYSTEMSCRIPTS= 01-test.sh 10-resolv.conf.sh 15-hostname.sh
+SCRIPTS=       ${SYSTEMSCRIPTS} ${HOOKSCRIPTS}
+SCRIPTSDIR=    ${SYSCONFDIR}/hook.d
+
+MK=            ../mk
+include ${MK}/scripts.mk
+install: _scriptsinstall
index a2f9038e4ba595ac0cf01567a8edbfe50b3edc05..0682b0346598c134e2c8187ecddd0e27538dbf2d 100644 (file)
@@ -5,5 +5,5 @@ FILESDIR?=      ${BINDIR}
 FILESMODE?=    ${NONBINMODE}
 
 _filesinstall:
-       ${INSTALL} -d ${DESTDIR}${FILESIR}
+       ${INSTALL} -d ${DESTDIR}${FILESDIR}
        ${INSTALL} -m ${FILESMODE} ${FILES} ${DESTDIR}${FILESDIR}
index 684bf79e6f91cda4f1b185c7aba9589bd34b1f6e..55896021fca9791ac3eb7444e2d079ba31f17100 100644 (file)
@@ -5,17 +5,10 @@
 
 include ${MK}/cc.mk
 include ${MK}/os.mk
+include ${MK}/sys.mk
 
-BINDIR?=       ${PREFIX}/usr/bin
-BINMODE?=      0755
-NONBINMODE?=   0644
 OBJS+=         ${SRCS:.c=.o}
 
-SYSCONFDIR?=   ${PREFIX}/etc
-
-INSTALL?=      install
-SED?=          sed
-
 all: ${PROG} ${SCRIPTS} _man
 
 .c.o:
@@ -44,6 +37,7 @@ include ${MK}/man.mk
 include ${MK}/dist.mk
 
 install: _proginstall _scriptsinstall _filesinstall _maninstall
+       for x in ${SUBDIRS}; do cd $$x; ${MAKE} $@; cd ..; done
 
 clean:
        rm -f ${OBJS} ${PROG} _${PROG}.c _${PROG}.o ${CLEANFILES}
index c8fa67908e64082d2572ca1ffd477cb2fae298b1..ed2dca93e83a01c555b4e5720fbd9691d955ec5b 100644 (file)
@@ -1,9 +1,11 @@
 # Quick and dirty scripts
 # Copyright 2008 Roy Marples <roy@marples.name>
 
+include ${MK}/sys.mk
+
 SCRIPTSDIR?=   ${BINDIR}
 SCRIPTSMODE?=  ${BINMODE}
 
 _scriptsinstall:
        ${INSTALL} -d ${DESTDIR}${SCRIPTSDIR}
-       ${INSTALL} -m ${SCRIPTSMODE} ${SCRIPTS} ${DESTDIR}${FILESDIR}
+       ${INSTALL} -m ${SCRIPTSMODE} ${SCRIPTS} ${DESTDIR}${SCRIPTSDIR}
diff --git a/mk/sys.mk b/mk/sys.mk
new file mode 100644 (file)
index 0000000..359a1fd
--- /dev/null
+++ b/mk/sys.mk
@@ -0,0 +1,10 @@
+# Simple defaults
+
+BINDIR?=       ${PREFIX}/usr/bin
+BINMODE?=      0755
+NONBINMODE?=   0644
+
+SYSCONFDIR?=   ${PREFIX}/etc
+
+INSTALL?=      install
+SED?=          sed