Signal passthru and support for $service_name::function style namespacing

Signed-off-by: fbt <fbt@fleshless.org>
This commit is contained in:
Jack L. Frost 2018-03-06 17:42:10 +03:00
parent 075d94e58c
commit 88e05630ae
1 changed files with 33 additions and 17 deletions

50
ssm
View File

@ -123,6 +123,24 @@ die() {
exit "$code" exit "$code"
}; readonly -f die }; readonly -f die
if_service_action() {
for f in "$1" "$service_name::$1"; do
is_function "$f" && return 0
done
return 0
}; readonly -f if_service_action
run_service_action() {
for f in "$1" "$service_name::$1"; do
is_function "$f" && {
"$f"; return $?
}
done
return 0
}; readonly -f run_service_action
spawn() { spawn() {
if [[ $service_logfile_out == "$service_logfile_err" ]]; then if [[ $service_logfile_out == "$service_logfile_err" ]]; then
exec "$@" >"$service_logfile_out" 2>&1 exec "$@" >"$service_logfile_out" 2>&1
@ -176,6 +194,12 @@ svc() {
nullexec kill -n "$service_reload_signal" "$job_pid" nullexec kill -n "$service_reload_signal" "$job_pid"
}; trap 'svc::reload' HUP }; trap 'svc::reload' HUP
# Signals to pass through to the mainpid.
svc::passthru() { kill -n "$1" "$2"; }
for s in "${service_signals_passthru[@]}"; do
trap "svc::passthru $s \$job_pid" "$s"
done
printf '%s' $BASHPID > "$svc_pidfile" printf '%s' $BASHPID > "$svc_pidfile"
# Cgroups # Cgroups
@ -562,6 +586,7 @@ var service_pid \
service_cgroup_name \ service_cgroup_name \
service_cgroup_procs \ service_cgroup_procs \
service_cgroup_path \ service_cgroup_path \
service_signals_passthru \
cgroup_home \ cgroup_home \
failed_deps \ failed_deps \
svc_pidfile \ svc_pidfile \
@ -735,6 +760,7 @@ flag_edit_service && { edit; die $?; }
# Service name is the basename # Service name is the basename
service_name = "${1##*/}" service_name = "${1##*/}"
readonly service_name
# These depend on the service_name and make little sense to reconfigure. # These depend on the service_name and make little sense to reconfigure.
service_ready_flag := "$rundir/$service_name.ready" service_ready_flag := "$rundir/$service_name.ready"
@ -854,8 +880,8 @@ if cgroups; then
fi fi
fi fi
# Check if action is even defined # Do we have such a function?
is_function "$2" || die 17 "Function $2 is not defined for $service_name." if_service_action "$2" || die 17 "Function $2 is not defined for $service_name."
# cd into the workdir, if defined. # cd into the workdir, if defined.
service_workdir && { service_workdir && {
@ -863,15 +889,10 @@ service_workdir && {
} }
# Run pre_$action function # Run pre_$action function
if is_function "pre_$2"; then run_service_action "pre_$2" || die 13 "pre_$2 failed!"
"pre_$2" || {
printf 'pre_%s failed!\n' "$2"
die 13
}
fi
# Run the function # Run the main action
"$2"; res=$? run_service_action "$2"; res=$?
case "$2" in case "$2" in
stop) stop)
@ -894,7 +915,7 @@ case "$2" in
reload) reload)
result "$res" \ result "$res" \
0 "Reloaded $service_name" 0 "Reloading $service_name"
;; ;;
status) status)
@ -911,9 +932,4 @@ esac
(( res )) && exit "$res" (( res )) && exit "$res"
# Run post_$action function # Run post_$action function
if is_function "post_$2"; then run_service_action "post_$2" || die 15 "post_$2 has failed!"
"post_$2" || {
printf 'post_%s failed!\n' "$2"
die 15
}
fi