From cfabb85924f35c636550609b332d088aaae9941f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20B=C3=A4chler?= Date: Tue, 25 Aug 2009 12:57:02 +0200 Subject: Implement a hook-system that allows to add custom code to the initscripts at certain places A function add_hook can be called from functions.d to register a hook function. The existing hooks are based on suggestions from Michael Towers (larch) and on the implementation of initscripts-extras-fbsplash which currently uses the strings passed to stat_busy and stat_done for this. More hooks can be added if requested. The implementation uses associative arrays and will thus only work with bash 4 or later --- functions | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) (limited to 'functions') diff --git a/functions b/functions index 8745a38..56b78be 100644 --- a/functions +++ b/functions @@ -229,8 +229,52 @@ ck_status() { fi } +############################### +# Custom hooks in initscripts # +############################### +# Hooks can be used to include custom code in various places in the rc.* scripts +# +# Define a hook function in a functions.d file using: +# function_name() { +# ... +# } +# add_hook hook_name function_name +# It's allowed to register several hook functions for the same hook +# +# The function takes the filename of the script that launched it as an argument +# +# Currently, the following hooks exist: +# start: at the beginning of rc.{multi,shutdown,single,sysinit} +# end: at the end of rc.{multi,single,sysinit} +# udevlaunched: after udev has been launched in rc.sysinit and rc.single +# udevsettled: after uevents have settled in rc.sysinit and rc.single +# premount: before local filesystems are mounted, but after root is mounted read-write in rc.sysinit +# prekillall: before all processes are being killed in rc.shutdown and rc.single +# postkillall: after all processes have been killed in rc.shutdown and rc.single +# poweroff: directly before powering off in rc.shutdown +# +# Make sure to never override the add_hook and run_hook functions via functions.d + +declare -A hook_funcs +for hook in start end udevlaunched udevsettled premount prekillall postkillall poweroff; do + hook_funcs["${hook}"]="" +done + +add_hook() { + [ -z "$1" -o -z "$2" ] && return 1 + hook_funcs["$1"]="${hook_funcs["$1"]} $2" +} + +run_hook() { + local func + + [ -z "$1" ] && return 1 + for func in ${hook_funcs["$1"]}; do + ${func} "$2" + done +} -#Source additional functions at the end to allow overrides +# Source additional functions at the end to allow overrides for f in /etc/rc.d/functions.d/*; do if [ -e $f ]; then . $f -- cgit v1.2.3