From: Roy Marples Date: Tue, 20 May 2008 10:55:25 +0000 (+0000) Subject: Split dhcpcd.sh into smaller hook scripts, so that dhcpcd.sh just provide some handy... X-Git-Tag: v4.0.2~371 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c61c4118d8a688f26a9bd53b061090aca5e45f66;p=thirdparty%2Fdhcpcd.git Split dhcpcd.sh into smaller hook scripts, so that dhcpcd.sh just provide some handy functions and runs the hooks. We now only have one hook, instead of enter/exit. --- diff --git a/Makefile b/Makefile index bbf1d149..0a46c3b8 100644 --- a/Makefile +++ b/Makefile @@ -24,6 +24,8 @@ CPPFLAGS+= -DSYSCONFDIR=\"${SYSCONFDIR}\" CPPFLAGS+= -DDBDIR=\"${DBDIR}\" LDADD+= ${LIBRT} +SUBDIRS= hook.d + .SUFFIXES: .in .sh.in .in: diff --git a/dhcpcd.8.in b/dhcpcd.8.in index 4e1b97f7..d3a30fa1 100644 --- a/dhcpcd.8.in +++ b/dhcpcd.8.in @@ -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 diff --git a/dhcpcd.sh.8.in b/dhcpcd.sh.8.in index ce8a575d..35683667 100644 --- a/dhcpcd.sh.8.in +++ b/dhcpcd.sh.8.in @@ -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 diff --git a/dhcpcd.sh.in b/dhcpcd.sh.in index 7e080254..4715101a 100644 --- a/dhcpcd.sh.in +++ b/dhcpcd.sh.in @@ -1,131 +1,27 @@ #!/bin/sh -# dhcpcd - DHCP client daemon -# Copyright 2006-2008 Roy Marples -# 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 index 00000000..3306cab1 --- /dev/null +++ b/hook.d/01-test.sh @@ -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 index 00000000..e91916c4 --- /dev/null +++ b/hook.d/10-resolv.conf.sh @@ -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 diff --git a/hook-samples/lookup_hostname.sh b/hook.d/14-lookup-hostname.sh similarity index 52% rename from hook-samples/lookup_hostname.sh rename to hook.d/14-lookup-hostname.sh index c829b2d6..6818c392 100644 --- a/hook-samples/lookup_hostname.sh +++ b/hook.d/14-lookup-hostname.sh @@ -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 index 00000000..2ba42d51 --- /dev/null +++ b/hook.d/15-hostname.sh @@ -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 diff --git a/hook-samples/dhcpcd-compat.sh b/hook.d/50-dhcpcd-compat.sh similarity index 100% rename from hook-samples/dhcpcd-compat.sh rename to hook.d/50-dhcpcd-compat.sh diff --git a/hook-samples/ntp.sh b/hook.d/50-ntp.sh similarity index 100% rename from hook-samples/ntp.sh rename to hook.d/50-ntp.sh diff --git a/hook-samples/ypbind.sh b/hook.d/50-ypbind.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 index 00000000..a0fd00c7 --- /dev/null +++ b/hook.d/Makefile @@ -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 diff --git a/mk/files.mk b/mk/files.mk index a2f9038e..0682b034 100644 --- a/mk/files.mk +++ b/mk/files.mk @@ -5,5 +5,5 @@ FILESDIR?= ${BINDIR} FILESMODE?= ${NONBINMODE} _filesinstall: - ${INSTALL} -d ${DESTDIR}${FILESIR} + ${INSTALL} -d ${DESTDIR}${FILESDIR} ${INSTALL} -m ${FILESMODE} ${FILES} ${DESTDIR}${FILESDIR} diff --git a/mk/prog.mk b/mk/prog.mk index 684bf79e..55896021 100644 --- a/mk/prog.mk +++ b/mk/prog.mk @@ -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} diff --git a/mk/scripts.mk b/mk/scripts.mk index c8fa6790..ed2dca93 100644 --- a/mk/scripts.mk +++ b/mk/scripts.mk @@ -1,9 +1,11 @@ # Quick and dirty scripts # Copyright 2008 Roy Marples +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 index 00000000..359a1fdc --- /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