]> git.ipfire.org Git - people/ms/network.git/commitdiff
Execute hooks faster by sourcing them.
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 10 Jun 2012 14:53:01 +0000 (14:53 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 10 Jun 2012 14:53:01 +0000 (14:53 +0000)
Also most of the hook_* functions were partly rewritten
for performance.

18 files changed:
functions.hook
functions.zone
header-config
header-port
header-zone
hooks/zones/6to4-tunnel
hooks/zones/aiccu
hooks/zones/bridge
hooks/zones/bridge.configs/ipv4-dhcp
hooks/zones/bridge.configs/ipv4-static
hooks/zones/bridge.configs/ipv6-static
hooks/zones/bridge.ports/ethernet
hooks/zones/isdn
hooks/zones/isdn-server
hooks/zones/modem
hooks/zones/pppoe
hooks/zones/teredo
hooks/zones/wireless

index 9a1f3523035da0cc391335a5a4689527fde34e14..792f2ba8e30e8df47f5dc19846acb477ad534d2a 100644 (file)
@@ -28,6 +28,7 @@ function hook_dir() {
 
        echo "${NETWORK_HOOKS_DIR}${type}"
 }
 
        echo "${NETWORK_HOOKS_DIR}${type}"
 }
+NETWORK_HOOKS_DIR_ZONES="$(hook_dir zone)"
 
 function hook_exists() {
        local type=${1}
 
 function hook_exists() {
        local type=${1}
@@ -36,27 +37,48 @@ function hook_exists() {
        assert isset type
        assert isset hook
 
        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}
 }
 
 function hook_exec() {
        local type=${1}
        local hook=${2}
-       shift 2
+       local cmd=${3}
+       shift 3
 
        assert isset type
        assert isset hook
 
        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
 
        fi
 
-       exec_cmd $(hook_dir ${type})/${hook} $@
+       return ${ret}
 }
 
 function config_get_hook() {
 }
 
 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 $@
 }
 function hook_zone_exists() {
        hook_exists zone $@
 }
@@ -81,34 +101,26 @@ function hook_zone_port_exists() {
        local hook_zone=${1}
        local hook_port=${2}
 
        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}
 
 }
 
 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}
 
 }
 
 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}
 
 }
 
 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() {
 }
 
 function hook_zone_exec() {
@@ -120,38 +132,15 @@ function hook_zone_port_exec() {
        local hook_port=${2}
        shift 2
 
        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}
 }
 
 function hook_zone_config_exec() {
        local hook_zone=${1}
-       local hook_config=${2}
+       local hook_port=${2}
        shift 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() {
 }
 
 function hook_zone_get_all() {
index 433275cd042dfc1e386f38380fe94fbc8eb2c72c..63cc37dfc5ad163f3362a114a13bb81b3bbd9f4e 100644 (file)
@@ -27,7 +27,6 @@ function zone_dir() {
 
 function zone_exists() {
        local zone=${1}
 
 function zone_exists() {
        local zone=${1}
-
        assert isset zone
 
        [ -d "$(zone_dir ${zone})" ]
        assert isset zone
 
        [ -d "$(zone_dir ${zone})" ]
@@ -46,7 +45,6 @@ function zone_match() {
 
 function zone_name_is_valid() {
        local zone=${1}
 
 function zone_name_is_valid() {
        local zone=${1}
-
        assert isset zone
 
        [[ ${zone} =~ $(zone_match) ]]
        assert isset zone
 
        [[ ${zone} =~ $(zone_match) ]]
@@ -66,7 +64,6 @@ function zone_is_nonlocal() {
 
 function zone_get_hook() {
        local zone=${1}
 
 function zone_get_hook() {
        local zone=${1}
-
        assert isset zone
 
        config_get_hook $(zone_dir ${zone})/settings
        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 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} $@
 }
 
        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
 
        local port
        for port in $(zone_get_ports ${zone}); do
-               #zone_port_cmd ${cmd} ${zone} ${port} $@
                hook_zone_exec ${hook} ${cmd} ${zone} ${port} $@
        done
 }
                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
 
        for config in $(zone_dir ${zone})/configs/*; do
                [ -e "${config}" ] || continue
 
-               echo $(basename ${config})
+               basename ${config}
        done
 }
 
        done
 }
 
index 47e9921785da779c3d7044f327c430027d1c880f..71cd5ad06454986951771beaf4cc67f9511307d3 100644 (file)
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
 #                                                                             #
 ###############################################################################
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
 #                                                                             #
 ###############################################################################
-
-. /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}
-}
index bb169174c6efcf829436bea74877999b78c32def..ae12047357bcd0daf6c67912319daeba19a38397 100644 (file)
 #                                                                             #
 ###############################################################################
 
 #                                                                             #
 ###############################################################################
 
-. /usr/lib/network/functions
-
-HOOK=$(basename ${0})
 INFO_SETTINGS="HOOK PORT_PARENTS PORT_CHILDREN"
 
 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
 # 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
index 664fc0ce045ef5016c20d6f920cfdfcb8c065e23..bbc2c0bcbb4afa9e063eb96370dc673cea57844b 100644 (file)
 #     conflict with any functions that were defined somewhere else.
 #
 
 #     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.
 #
 # _notimplemented
 #   Returns a soft error if a function was not implemented, yet.
 #
@@ -337,43 +317,3 @@ function _ppp-ipv6-down() {
 
        exit $?
 }
 
        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}
-}
index a45f2f999220354134223d9c8488dc0c87b97521..f032529556d24f489c4213f0672da5bdcaacd4a9 100755 (executable)
@@ -127,5 +127,3 @@ function _status() {
 
        exit ${EXIT_OK}
 }
 
        exit ${EXIT_OK}
 }
-
-run $@
index b49cb62d7dd407711b61ded045b60d9b687cdca7..f9ab0cd232b8ac8d5335027a59f9483bfbbd7ab1 100755 (executable)
@@ -19,7 +19,7 @@
 #                                                                             #
 ###############################################################################
 
 #                                                                             #
 ###############################################################################
 
-. /lib/network/header-zone
+. /usr/lib/network/header-zone
 
 HOOK_SETTINGS="HOOK PROTOCOL USER SECRET SERVER TUNNEL_ID"
 
 
 HOOK_SETTINGS="HOOK PROTOCOL USER SECRET SERVER TUNNEL_ID"
 
@@ -114,5 +114,3 @@ function _status() {
 
        exit ${EXIT_OK}
 }
 
        exit ${EXIT_OK}
 }
-
-run $@
index 074cfb338e95aade83536a8d8f345e2fe531f7ca..19959247a811eb327dfd4bf0058a2e2bb899bbbe 100755 (executable)
@@ -205,5 +205,3 @@ function _status() {
        cli_space
        exit ${EXIT_OK}
 }
        cli_space
        exit ${EXIT_OK}
 }
-
-run $@
index fd5f1c64bdd0284d7358cfaa1977f3657e52f354..eb9ad33ce97f68c1a4e1fea20cd04940a9c7b62c 100755 (executable)
@@ -19,7 +19,7 @@
 #                                                                             #
 ###############################################################################
 
 #                                                                             #
 ###############################################################################
 
-. /lib/network/header-config
+. /usr/lib/network/header-config
 
 HOOK_SETTINGS="HOOK DELAY"
 
 
 HOOK_SETTINGS="HOOK DELAY"
 
@@ -107,5 +107,3 @@ function _status() {
 
        exit ${EXIT_OK}
 }
 
        exit ${EXIT_OK}
 }
-
-run $@
index 44b854397c7b1c687051c51ae1bfa17c447d0424..fb72bcb311bd6f8bf19c6bf9d2a5789c6aad7d58 100755 (executable)
@@ -19,7 +19,7 @@
 #                                                                             #
 ###############################################################################
 
 #                                                                             #
 ###############################################################################
 
-. /lib/network/header-config
+. /usr/lib/network/header-config
 
 HOOK_SETTINGS="HOOK ADDRESS PREFIX GATEWAY"
 
 
 HOOK_SETTINGS="HOOK ADDRESS PREFIX GATEWAY"
 
@@ -156,5 +156,3 @@ function ipv4_mask_to_cidr() {
                echo ${cidr}
        fi
 }
                echo ${cidr}
        fi
 }
-
-run $@
index 691005db967167797a78cf50c8a715ab3826c58b..94f3bb6bd5465b475c60ef02232eaa261628b242 100755 (executable)
@@ -19,7 +19,7 @@
 #                                                                             #
 ###############################################################################
 
 #                                                                             #
 ###############################################################################
 
-. /lib/network/header-port
+. /usr/lib/network/header-port
 
 HOOK_SETTINGS="HOOK ADDRESS PREFIX GATEWAY"
 
 
 HOOK_SETTINGS="HOOK ADDRESS PREFIX GATEWAY"
 
@@ -135,5 +135,3 @@ function _status() {
 
        exit ${EXIT_OK}
 }
 
        exit ${EXIT_OK}
 }
-
-run $@
index 9fc1aa93856731823fe6aa381387dea72d6dc061..dcb6f84032804d515a49e4ebaaddec846e3e77ee 100755 (executable)
@@ -19,7 +19,7 @@
 #                                                                             #
 ###############################################################################
 
 #                                                                             #
 ###############################################################################
 
-. /lib/network/header-port
+. /usr/lib/network/header-port
 
 HOOK_SETTINGS="COST PRIORITY"
 
 
 HOOK_SETTINGS="COST PRIORITY"
 
@@ -155,5 +155,3 @@ function _status() {
 
        exit ${EXIT_OK}
 }
 
        exit ${EXIT_OK}
 }
-
-run $@
index df948e179537124d3f3b7b9a83ea16d92ec8a4c3..366a65c7fbf186bb3d6940e42495078d1752dc20 100755 (executable)
@@ -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"
 
 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}
 }
 
        exit ${EXIT_OK}
 }
-
-run $@
index f5d1c7ab776d61ed6183b74a0d83c065d6383d35..856e00a18eeb421166792466e681ae6fe526f3ec 100755 (executable)
@@ -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"
 
 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}
 }
        echo # Empty line
        exit ${EXIT_OK}
 }
-
-run $@
index 57a90e01171b10d418fb3ecc02dcefd3d0d19002..472988b68bf90b03639e99ffededfcc0a946c65a 100755 (executable)
@@ -227,5 +227,3 @@ function _status() {
        echo # Empty line
        exit ${EXIT_OK}
 }
        echo # Empty line
        exit ${EXIT_OK}
 }
-
-run $@
index df4bb46328923f2eca7dfeef3841404ef6630ed7..64d4781490e637a7910b47028cb5016ffb2edd19 100755 (executable)
@@ -19,7 +19,7 @@
 #                                                                             #
 ###############################################################################
 
 #                                                                             #
 ###############################################################################
 
-. /lib/network/header-zone
+. /usr/lib/network/header-zone
 
 # TODO XXX AC name, service name, sync?
 
 
 # TODO XXX AC name, service name, sync?
 
@@ -315,5 +315,3 @@ function _port_add() {
 
        exit ${EXIT_OK}
 }
 
        exit ${EXIT_OK}
 }
-
-run $@
index 1dd525e9af97adcd59edd79769457000fe67807e..32f48d451f93e29552109201f7bfa382a45843cb 100755 (executable)
@@ -88,5 +88,3 @@ function _status() {
 
        exit ${EXIT_OK}
 }
 
        exit ${EXIT_OK}
 }
-
-run $@
index 22f911f34bbae0870544714154e19f513d237a33..a150cfd489ddab29809ddfb644c68eb3b9bd4410 100755 (executable)
@@ -19,7 +19,7 @@
 #                                                                             #
 ###############################################################################
 
 #                                                                             #
 ###############################################################################
 
-. /lib/network/header-zone
+. /usr/lib/network/header-zone
 
 HOOK_SETTINGS="HOOK PHY MAC MTU SSID KEY ENCRYPTION"
 
 
 HOOK_SETTINGS="HOOK PHY MAC MTU SSID KEY ENCRYPTION"
 
@@ -154,5 +154,3 @@ function _status() {
        echo # Empty line
        exit ${EXIT_OK}
 }
        echo # Empty line
        exit ${EXIT_OK}
 }
-
-run $@