Make internal functions readonly

Signed-off-by: fbt <fbt@fleshless.org>
This commit is contained in:
Jack L. Frost 2017-12-14 18:41:03 +03:00
parent cb6b3d61df
commit 492e2aa81b

89
ssm
View File

@ -11,7 +11,7 @@ default() {
_p+=( "$v" ) _p+=( "$v" )
done done
} }
} }; readonly -f default
## Die. Why not? ## Die. Why not?
die() { die() {
@ -19,7 +19,7 @@ die() {
[[ "$2" ]] && printf '%s\n' "$2" [[ "$2" ]] && printf '%s\n' "$2"
exit "$code" exit "$code"
} }; readonly -f die
## Run the command and wait for it to die ## Run the command and wait for it to die
svc() { svc() {
@ -39,7 +39,7 @@ svc() {
wait "$job_pid" wait "$job_pid"
svc::cleanup svc::cleanup
} }; readonly -f svc
## Respawn ## Respawn
respawn() { respawn() {
@ -76,12 +76,11 @@ respawn() {
wait "$job_pid" wait "$job_pid"
done done
done done
} }; readonly -f respawn
## Run a command with its output discarded ## Run a command with its output discarded
nullexec() { nullexec() { "$@" &>/dev/null; }
"$@" &>/dev/null readonly -f nullexec
}
## Wait for a pid to die ## Wait for a pid to die
pid_wait() { pid_wait() {
@ -94,7 +93,7 @@ pid_wait() {
done done
return 0 return 0
} }; readonly -f pid_wait
## See if NAME is a function ## See if NAME is a function
is_function() { is_function() {
@ -107,7 +106,7 @@ is_function() {
fi fi
return 1 return 1
} }; readonly -f is_function
## Simple timer ## Simple timer
timer() { timer() {
@ -121,15 +120,15 @@ timer() {
done done
return 0 return 0
} }; readonly -f timer
## Is a service ready? ## Is a service ready?
is_ready() [[ -f "$service_ready_flag" ]] is_ready() [[ -f "$service_ready_flag" ]]
readonly -f is_ready
## Wait for this service to get ready ## Wait for this service to get ready
wait_ready() { wait_ready() { timer "$service_ready_timeout" is_ready; }
timer "$service_ready_timeout" is_ready readonly -f wait_ready
}
## Depend on other services to be started ## Depend on other services to be started
depend() { depend() {
@ -143,7 +142,7 @@ depend() {
} }
fi fi
done done
} }; readonly -f depend
## Create tmpfiles ## Create tmpfiles
mktmpfiles() { mktmpfiles() {
@ -169,7 +168,7 @@ mktmpfiles() {
esac esac
fi fi
done done
} }; readonly -f mktmpfiles
## Depend on other services to be ready ## Depend on other services to be ready
depend_ready() { depend_ready() {
@ -183,11 +182,27 @@ depend_ready() {
return 1 return 1
} }
done done
} }; readonly -f depend_ready
# Super functions result() {
declare rc=$1; shift
declare -A msgs
while (( $# )); do
[[ "$2" ]] || return 1
msgs["$1"]="$2"
shift 2
done
[[ "${msgs[$rc]}" ]] || msgs["$rc"]="Failed!"
printf '%s\n' "${msgs[$rc]}"
}; readonly -f result
# Overloadable functions
## Start the service, write down the svc pid ## Start the service, write down the svc pid
super_start() { start() {
(( service_running )) && return 3 (( service_running )) && return 3
rm -f "$service_stopped_flag" rm -f "$service_stopped_flag"
@ -222,14 +237,9 @@ super_start() {
return 0 return 0
} }
# A separate function for oneshot services
super_oneshot() {
(( service_enabled )) && return 3
}
## Reload the service ## Reload the service
## Usually just sends HUP ## Usually just sends HUP
super_reload() { reload() {
(( service_running )) || return 3 (( service_running )) || return 3
if (( service_managed )); then if (( service_managed )); then
@ -242,7 +252,7 @@ super_reload() {
## Stop the service ## Stop the service
## Returns: ## Returns:
## 3: Service is not running. ## 3: Service is not running.
super_stop() { stop() {
if (( service_oneshot )); then if (( service_oneshot )); then
(( service_enabled )) || return 3 (( service_enabled )) || return 3
@ -293,26 +303,7 @@ info() {
printf "%12s: %s\n" "${_info_items[@]}" printf "%12s: %s\n" "${_info_items[@]}"
} }
result() { # Restart just calls the script twice by default
declare rc=$1; shift
declare -A msgs
while (( $# )); do
[[ "$2" ]] || return 1
msgs["$1"]="$2"
shift 2
done
[[ "${msgs[$rc]}" ]] || msgs["$rc"]="Failed!"
printf '%s\n' "${msgs[$rc]}"
}
# Overloadable functions
start() { super_start; }
stop() { super_stop; }
reload() { super_reload; }
restart() { restart() {
"$_self" "$service_name" stop "$_self" "$service_name" stop
"$_self" "$service_name" start "$_self" "$service_name" start
@ -335,7 +326,7 @@ qstatus() { nullexec status; }
## By default there is no ready check ## By default there is no ready check
ready() { :; } ready() { :; }
# Code # Main code
main() { main() {
# Figure out our full path # Figure out our full path
case "$0" in case "$0" in
@ -387,7 +378,7 @@ main() {
# Common config path # Common config path
cfg_path+=( '/etc/ssm' ) cfg_path+=( '/etc/ssm' )
# Load custom functions # Load custom functions, reversing the PATH order
for (( idx=${#cfg_path[@]}-1; idx>=0; idx-- )); do for (( idx=${#cfg_path[@]}-1; idx>=0; idx-- )); do
cfg_dir="${cfg_path[idx]}" cfg_dir="${cfg_path[idx]}"
@ -432,7 +423,7 @@ main() {
done done
# Get the service config # Get the service config
source -- "$service_config" "${@:3}" || die 7 "Failed to read the service config: $service_config" source "$service_config" "${@:3}" || die 7 "Failed to read the service config: $service_config"
# Legacy # Legacy
[[ "$service_args" ]] && service_command=( "${service_command[@]}" "${service_args[@]}" ) [[ "$service_args" ]] && service_command=( "${service_command[@]}" "${service_args[@]}" )
@ -547,6 +538,6 @@ main() {
die 15 die 15
} }
fi fi
} }; readonly -f main
main "$@" main "$@"