From: Michael Tremer Date: Sun, 10 Jun 2012 14:53:01 +0000 (+0000) Subject: Execute hooks faster by sourcing them. X-Git-Tag: 004~39 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f41fa3d70a710f94caac673ccaf2b4e6c7e9a1e5;p=network.git Execute hooks faster by sourcing them. Also most of the hook_* functions were partly rewritten for performance. --- diff --git a/functions.hook b/functions.hook index 9a1f3523..792f2ba8 100644 --- a/functions.hook +++ b/functions.hook @@ -28,6 +28,7 @@ function hook_dir() { echo "${NETWORK_HOOKS_DIR}${type}" } +NETWORK_HOOKS_DIR_ZONES="$(hook_dir zone)" function hook_exists() { local type=${1} @@ -36,27 +37,48 @@ function hook_exists() { assert isset type assert isset hook - local hook_dir=$(hook_dir ${type}) + # Add the path prefix. + hook="$(hook_dir ${type})/${hook}" - [ -d "${hook_dir}/${hook}" ] && return ${EXIT_ERROR} - - [ -x "${hook_dir}/${hook}" ] + [ ! -d "${hook}" ] && [ -x "${hook}" ] } function hook_exec() { local type=${1} local hook=${2} - shift 2 + local cmd=${3} + shift 3 assert isset type assert isset hook + assert isset cmd - if ! hook_exists ${type} ${hook}; then - error "Hook '${hook}' does not exist." - return ${EXIT_ERROR} + assert hook_exists "${type}" "${hook}" + + # For performance reasons, all hooks are executed + # in a subshell and so will inherit the currently + # running environment. + ( + # Set the name of the hook. + HOOK=$(basename ${hook}) + + # Source the code of the hook. + source "$(hook_dir ${type})/${hook}" + + # Make sure HOOK is still properly set. + assert isset HOOK + + # Execute the requested command. + _${cmd} $@ + ) + local ret=$? + + if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then + log ERROR "Hook exited with an assertion error." + exit ${ret} fi - exec_cmd $(hook_dir ${type})/${hook} $@ + return ${ret} } function config_get_hook() { @@ -71,8 +93,6 @@ function config_get_hook() { ) } -## Wrappers around the hook functions for zones - function hook_zone_exists() { hook_exists zone $@ } @@ -81,34 +101,26 @@ function hook_zone_port_exists() { local hook_zone=${1} local hook_port=${2} - hook_zone_exists ${hook_zone} || return ${EXIT_ERROR} - - [ -x "$(hook_dir zone)/${hook_zone}.ports/${hook_port}" ] + hook_exists zone "${hook_zone}.ports/${hook_port}" } function hook_zone_config_exists() { local hook_zone=${1} local hook_config=${2} - hook_zone_exists ${hook_zone} || return ${EXIT_ERROR} - - [ -x "$(hook_dir zone)/${hook_zone}.configs/${hook_config}" ] + hook_exists zone "${hook_zone}.configs/${hook_config}" } function hook_zone_has_ports() { local hook=${1} - [ -d "$(hook_dir zone)/${hook}.ports" ] -} - -function hook_zone_port_exists() { - : # XXX WANTED + [ -d "${NETWORK_HOOKS_DIR_ZONES}/${hook}.ports" ] } function hook_zone_has_configs() { local hook=${1} - [ -d "$(hook_dir zone)/${hook}.configs" ] + [ -d "${NETWORK_HOOKS_DIR_ZONES}/${hook}.configs" ] } function hook_zone_exec() { @@ -120,38 +132,15 @@ function hook_zone_port_exec() { local hook_port=${2} shift 2 - if ! hook_exists zone ${hook_zone}; then - error "Hook '${hook_zone}' does not exist." - return ${EXIT_ERROR} - fi - - if ! hook_zone_port_exists ${hook_zone} ${hook_port}; then - error "Port hook '${hook_port}' does not exist." - return ${EXIT_ERROR} - fi - - exec_cmd $(hook_dir zone)/${hook_zone}.ports/${hook_port} $@ + hook_zone_exec "${hook_zone}.ports/${hook_port}" $@ } function hook_zone_config_exec() { local hook_zone=${1} - local hook_config=${2} + local hook_port=${2} shift 2 - assert isset hook_zone - assert isset hook_config - - if ! hook_zone_exists ${hook_zone}; then - error "Hook '${hook_zone}' does not exist." - return ${EXIT_ERROR} - fi - - if ! hook_zone_config_exists ${hook_zone} ${hook_config}; then - error "Config hook '${hook_config}' does not exist." - return ${EXIT_ERROR} - fi - - exec_cmd $(hook_dir zone)/${hook_zone}.configs/${hook_config} $@ + hook_zone_exec "${hook_zone}.configs/${hook_port}" $@ } function hook_zone_get_all() { diff --git a/functions.zone b/functions.zone index 433275cd..63cc37df 100644 --- a/functions.zone +++ b/functions.zone @@ -27,7 +27,6 @@ function zone_dir() { function zone_exists() { local zone=${1} - assert isset zone [ -d "$(zone_dir ${zone})" ] @@ -46,7 +45,6 @@ function zone_match() { function zone_name_is_valid() { local zone=${1} - assert isset zone [[ ${zone} =~ $(zone_match) ]] @@ -66,7 +64,6 @@ function zone_is_nonlocal() { function zone_get_hook() { local zone=${1} - assert isset zone config_get_hook $(zone_dir ${zone})/settings @@ -350,8 +347,6 @@ function zone_port_cmd() { assert isset hook_zone assert isset hook_port - assert hook_zone_port_exists ${hook_zone} ${hook_port} - hook_zone_port_exec ${hook_zone} ${hook_port} ${cmd} ${zone} ${port} $@ } @@ -589,7 +584,6 @@ function zone_ports_cmd() { local port for port in $(zone_get_ports ${zone}); do - #zone_port_cmd ${cmd} ${zone} ${port} $@ hook_zone_exec ${hook} ${cmd} ${zone} ${port} $@ done } @@ -613,7 +607,7 @@ function zone_configs_list() { for config in $(zone_dir ${zone})/configs/*; do [ -e "${config}" ] || continue - echo $(basename ${config}) + basename ${config} done } diff --git a/header-config b/header-config index 47e99217..71cd5ad0 100644 --- a/header-config +++ b/header-config @@ -18,32 +18,3 @@ # along with this program. If not, see . # # # ############################################################################### - -. /lib/network/functions - -HOOK=$(basename ${0}) - -while [ $# -gt 0 ]; do - case "${1}" in - -*) - error "Unrecognized option: ${1}" - exit ${EXIT_ERROR} - ;; - *) - action=${1} - ;; - esac - shift - [ -n "${action}" ] && break -done - -function run() { - case "${action}" in - edit|create|rem|up|down|status) - _${action} $@ - ;; - esac - - error "Config hook '${HOOK}' didn't exit properly." - exit ${EXIT_ERROR} -} diff --git a/header-port b/header-port index bb169174..ae120473 100644 --- a/header-port +++ b/header-port @@ -19,36 +19,8 @@ # # ############################################################################### -. /usr/lib/network/functions - -HOOK=$(basename ${0}) INFO_SETTINGS="HOOK PORT_PARENTS PORT_CHILDREN" -while [ $# -gt 0 ]; do - case "${1}" in - -*) - error "Unrecognized option: ${1}" - exit ${EXIT_ERROR} - ;; - *) - action=${1} - ;; - esac - shift - [ -n "${action}" ] && break -done - -function run() { - case "${action}" in - edit|add|create|rem|up|down|status|info|hotplug|hotplug_rename) - _${action} $@ - ;; - esac - - error "Port hook '${HOOK}' didn't exit properly." - exit ${EXIT_ERROR} -} - # This function is called after a device has been plugged # into the system and got its correct name. # The function is intended to create child ports and things diff --git a/header-zone b/header-zone index 664fc0ce..bbc2c0bc 100644 --- a/header-zone +++ b/header-zone @@ -24,26 +24,6 @@ # conflict with any functions that were defined somewhere else. # -. /usr/lib/network/functions - -HOOK=$(basename ${0}) - -while [ $# -gt 0 ]; do - case "${1}" in - -*) - error "Unrecognized option: ${1}" - exit ${EXIT_ERROR} - ;; - *) - action=${1} - ;; - esac - shift - - # If action argument was given, we will exit. - [ -n "${action}" ] && break -done - # _notimplemented # Returns a soft error if a function was not implemented, yet. # @@ -337,43 +317,3 @@ function _ppp-ipv6-down() { exit $? } - -function run() { - # Replace all dashes by an underscore - #action=${action//-/_} - - case "${action}" in - # Main functions - create|discover|down|edit|info|rem|status|up) - _${action} $@ - ;; - - # Port callbacks - port_add|port_rem|port_up|port_down|port_status) - _${action} $@ - ;; - - # Configuration callbacks - config_create) - _${action} $@ - ;; - - # ppp daemon callbacks - ppp-ip-pre-up|ppp-ip-up|ppp-ip-down|ppp-ipv6-up|ppp-ipv6-down) - _${action} $@ - ;; - - # Help requested by the user. - help) - _help $@ - exit ${EXIT_OK} - ;; - - *) - error "Unknown action: ${action}" - ;; - esac - - error "Hook did not exit properly." - exit ${EXIT_ERROR} -} diff --git a/hooks/zones/6to4-tunnel b/hooks/zones/6to4-tunnel index a45f2f99..f0325295 100755 --- a/hooks/zones/6to4-tunnel +++ b/hooks/zones/6to4-tunnel @@ -127,5 +127,3 @@ function _status() { exit ${EXIT_OK} } - -run $@ diff --git a/hooks/zones/aiccu b/hooks/zones/aiccu index b49cb62d..f9ab0cd2 100755 --- a/hooks/zones/aiccu +++ b/hooks/zones/aiccu @@ -19,7 +19,7 @@ # # ############################################################################### -. /lib/network/header-zone +. /usr/lib/network/header-zone HOOK_SETTINGS="HOOK PROTOCOL USER SECRET SERVER TUNNEL_ID" @@ -114,5 +114,3 @@ function _status() { exit ${EXIT_OK} } - -run $@ diff --git a/hooks/zones/bridge b/hooks/zones/bridge index 074cfb33..19959247 100755 --- a/hooks/zones/bridge +++ b/hooks/zones/bridge @@ -205,5 +205,3 @@ function _status() { cli_space exit ${EXIT_OK} } - -run $@ diff --git a/hooks/zones/bridge.configs/ipv4-dhcp b/hooks/zones/bridge.configs/ipv4-dhcp index fd5f1c64..eb9ad33c 100755 --- a/hooks/zones/bridge.configs/ipv4-dhcp +++ b/hooks/zones/bridge.configs/ipv4-dhcp @@ -19,7 +19,7 @@ # # ############################################################################### -. /lib/network/header-config +. /usr/lib/network/header-config HOOK_SETTINGS="HOOK DELAY" @@ -107,5 +107,3 @@ function _status() { exit ${EXIT_OK} } - -run $@ diff --git a/hooks/zones/bridge.configs/ipv4-static b/hooks/zones/bridge.configs/ipv4-static index 44b85439..fb72bcb3 100755 --- a/hooks/zones/bridge.configs/ipv4-static +++ b/hooks/zones/bridge.configs/ipv4-static @@ -19,7 +19,7 @@ # # ############################################################################### -. /lib/network/header-config +. /usr/lib/network/header-config HOOK_SETTINGS="HOOK ADDRESS PREFIX GATEWAY" @@ -156,5 +156,3 @@ function ipv4_mask_to_cidr() { echo ${cidr} fi } - -run $@ diff --git a/hooks/zones/bridge.configs/ipv6-static b/hooks/zones/bridge.configs/ipv6-static index 691005db..94f3bb6b 100755 --- a/hooks/zones/bridge.configs/ipv6-static +++ b/hooks/zones/bridge.configs/ipv6-static @@ -19,7 +19,7 @@ # # ############################################################################### -. /lib/network/header-port +. /usr/lib/network/header-port HOOK_SETTINGS="HOOK ADDRESS PREFIX GATEWAY" @@ -135,5 +135,3 @@ function _status() { exit ${EXIT_OK} } - -run $@ diff --git a/hooks/zones/bridge.ports/ethernet b/hooks/zones/bridge.ports/ethernet index 9fc1aa93..dcb6f840 100755 --- a/hooks/zones/bridge.ports/ethernet +++ b/hooks/zones/bridge.ports/ethernet @@ -19,7 +19,7 @@ # # ############################################################################### -. /lib/network/header-port +. /usr/lib/network/header-port HOOK_SETTINGS="COST PRIORITY" @@ -155,5 +155,3 @@ function _status() { exit ${EXIT_OK} } - -run $@ diff --git a/hooks/zones/isdn b/hooks/zones/isdn index df948e17..366a65c7 100755 --- a/hooks/zones/isdn +++ b/hooks/zones/isdn @@ -19,7 +19,7 @@ # # ############################################################################### -. /lib/network/header-zone +. /usr/lib/network/header-zone HOOK_SETTINGS="HOOK AUTH LINKNAME USER SECRET PEERDNS DEFAULTROUTE MSN MTU" HOOK_SETTINGS="${HOOK_SETTINGS} L2PROTO L3PROTO ENCAP PHONE" @@ -203,5 +203,3 @@ function _status() { exit ${EXIT_OK} } - -run $@ diff --git a/hooks/zones/isdn-server b/hooks/zones/isdn-server index f5d1c7ab..856e00a1 100755 --- a/hooks/zones/isdn-server +++ b/hooks/zones/isdn-server @@ -19,7 +19,7 @@ # # ############################################################################### -. /lib/network/header-zone +. /usr/lib/network/header-zone HOOK_SETTINGS="HOOK LOCAL_ADDRESS REMOTE_ADDRESS MSN MTU MRU" HOOK_SETTINGS="${HOOK_SETTINGS} L2PROTO L3PROTO ENCAP" @@ -229,5 +229,3 @@ function _status() { echo # Empty line exit ${EXIT_OK} } - -run $@ diff --git a/hooks/zones/modem b/hooks/zones/modem index 57a90e01..472988b6 100755 --- a/hooks/zones/modem +++ b/hooks/zones/modem @@ -227,5 +227,3 @@ function _status() { echo # Empty line exit ${EXIT_OK} } - -run $@ diff --git a/hooks/zones/pppoe b/hooks/zones/pppoe index df4bb463..64d47814 100755 --- a/hooks/zones/pppoe +++ b/hooks/zones/pppoe @@ -19,7 +19,7 @@ # # ############################################################################### -. /lib/network/header-zone +. /usr/lib/network/header-zone # TODO XXX AC name, service name, sync? @@ -315,5 +315,3 @@ function _port_add() { exit ${EXIT_OK} } - -run $@ diff --git a/hooks/zones/teredo b/hooks/zones/teredo index 1dd525e9..32f48d45 100755 --- a/hooks/zones/teredo +++ b/hooks/zones/teredo @@ -88,5 +88,3 @@ function _status() { exit ${EXIT_OK} } - -run $@ diff --git a/hooks/zones/wireless b/hooks/zones/wireless index 22f911f3..a150cfd4 100755 --- a/hooks/zones/wireless +++ b/hooks/zones/wireless @@ -19,7 +19,7 @@ # # ############################################################################### -. /lib/network/header-zone +. /usr/lib/network/header-zone HOOK_SETTINGS="HOOK PHY MAC MTU SSID KEY ENCRYPTION" @@ -154,5 +154,3 @@ function _status() { echo # Empty line exit ${EXIT_OK} } - -run $@