sig passthrough and tmpfiles
Signed-off-by: fbt <fbt@fleshless.org>
This commit is contained in:
parent
7cc969de2b
commit
e980a7e1dc
68
sm
68
sm
|
@ -26,23 +26,17 @@ svc() {
|
||||||
svc::cleanup() {
|
svc::cleanup() {
|
||||||
kill -n "$service_stop_signal" "$job_pid"
|
kill -n "$service_stop_signal" "$job_pid"
|
||||||
|
|
||||||
pid_wait "$service_pid" && {
|
pid_wait "$job_pid" && {
|
||||||
rm -f $svc_pidfile $service_ready_flag
|
rm -f $svc_pidfile $service_ready_flag
|
||||||
}
|
}
|
||||||
}; trap 'svc::cleanup' TERM
|
}; trap 'svc::cleanup' TERM
|
||||||
|
|
||||||
svc::reload() {
|
|
||||||
kill -n "$service_reload_signal" "$job_pid"
|
|
||||||
}; trap 'svc::reload' HUP
|
|
||||||
|
|
||||||
"$@" & job_pid=$!
|
"$@" & job_pid=$!
|
||||||
|
|
||||||
while nullexec kill -n 0 "$job_pid"; do
|
printf '%s' "$job_pid" > "$svc_pidfile"
|
||||||
wait
|
wait "$job_pid"
|
||||||
done
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#cat >/dev/null << EOF
|
|
||||||
## Respawn
|
## Respawn
|
||||||
respawn() {
|
respawn() {
|
||||||
declare jobs job_pid
|
declare jobs job_pid
|
||||||
|
@ -58,12 +52,25 @@ respawn() {
|
||||||
exit 0
|
exit 0
|
||||||
}; trap 'respawn::cleanup' TERM
|
}; trap 'respawn::cleanup' TERM
|
||||||
|
|
||||||
|
respawn::sigpass() {
|
||||||
|
declare sig=$1 pid=$2
|
||||||
|
kill -n "$sig" "$pid"
|
||||||
|
}
|
||||||
|
|
||||||
|
respawn::set_traps() {
|
||||||
|
for s in "${service_signals[@]}"; do
|
||||||
|
trap "respawn::sigpass $s \$job_pid" "$s"
|
||||||
|
done
|
||||||
|
}; respawn::set_traps
|
||||||
|
|
||||||
while true; do
|
while true; do
|
||||||
exec "$@" & job_pid=$!
|
exec "$@" & job_pid=$!
|
||||||
wait "$job_pid"
|
|
||||||
|
while nullexec kill -n 0 "$job_pid"; do
|
||||||
|
wait "$job_pid"
|
||||||
|
done
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
#EOF
|
|
||||||
|
|
||||||
## Run a command with its output discarded
|
## Run a command with its output discarded
|
||||||
nullexec() {
|
nullexec() {
|
||||||
|
@ -132,6 +139,32 @@ depend() {
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
## Create tmpfiles
|
||||||
|
mktmpfiles() {
|
||||||
|
declare f
|
||||||
|
|
||||||
|
for f in "${service_tmpfiles[@]}"; do
|
||||||
|
IFS=':' read -r f_path f_type f_args <<< "$f"
|
||||||
|
|
||||||
|
if ! [[ -e $f_path ]]; then
|
||||||
|
case "$f_type" in
|
||||||
|
symlink) ln -s "$f_args" "$f_path";;
|
||||||
|
file|dir) IFS=':' read -r f_perms f_owner f_group <<< "$f_args"
|
||||||
|
if [[ $f_type == 'file' ]]; then
|
||||||
|
> "$f_path"
|
||||||
|
chmod "${f_perms:-644}" "$f_path"
|
||||||
|
elif [[ "$f_type" == 'dir' ]]; then
|
||||||
|
mkdir -p -m "${f_perms:-755}" "$f_path"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "$f_owner" || "$f_group" ]]; then
|
||||||
|
chown "${f_owner:-root}:${f_group:-root}" "$f_path"
|
||||||
|
fi;;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
## Depend on other services to be ready
|
## Depend on other services to be ready
|
||||||
depend_ready() {
|
depend_ready() {
|
||||||
declare s
|
declare s
|
||||||
|
@ -156,6 +189,8 @@ super_start() {
|
||||||
depend "${service_depends[@]}" || return 7
|
depend "${service_depends[@]}" || return 7
|
||||||
depend_ready "${service_depends_ready[@]}" || return 7
|
depend_ready "${service_depends_ready[@]}" || return 7
|
||||||
|
|
||||||
|
mktmpfiles || return 13
|
||||||
|
|
||||||
if (( service_managed )); then
|
if (( service_managed )); then
|
||||||
if (( service_respawn )); then
|
if (( service_respawn )); then
|
||||||
svc respawn "${service_command[@]}" &>"$service_logfile" &
|
svc respawn "${service_command[@]}" &>"$service_logfile" &
|
||||||
|
@ -163,10 +198,6 @@ super_start() {
|
||||||
svc "${service_command[@]}" &>"$service_logfile" &
|
svc "${service_command[@]}" &>"$service_logfile" &
|
||||||
fi
|
fi
|
||||||
|
|
||||||
svc_pid=$!
|
|
||||||
|
|
||||||
printf '%s' "$svc_pid" > "$svc_pidfile"
|
|
||||||
|
|
||||||
if timer "$service_ready_timeout" ready; then
|
if timer "$service_ready_timeout" ready; then
|
||||||
printf '1' > "$service_ready_flag"
|
printf '1' > "$service_ready_flag"
|
||||||
else
|
else
|
||||||
|
@ -246,6 +277,9 @@ ready() { :; }
|
||||||
|
|
||||||
# Code
|
# Code
|
||||||
main() {
|
main() {
|
||||||
|
# Needs to be global
|
||||||
|
declare -g service_pid
|
||||||
|
|
||||||
# Let's set some defaults
|
# Let's set some defaults
|
||||||
service_managed=1
|
service_managed=1
|
||||||
|
|
||||||
|
@ -308,6 +342,9 @@ main() {
|
||||||
default service_ready_flag "$rundir/$service_name.ready"
|
default service_ready_flag "$rundir/$service_name.ready"
|
||||||
default service_enabled_flag "$rundir/$service_name.enabled"
|
default service_enabled_flag "$rundir/$service_name.enabled"
|
||||||
|
|
||||||
|
# default does not support arrays
|
||||||
|
[[ "$service_signals" ]] || service_signals=( 1 10 12 )
|
||||||
|
|
||||||
# Let's see if there's a PID
|
# Let's see if there's a PID
|
||||||
if [[ -f "$service_pidfile" ]]; then
|
if [[ -f "$service_pidfile" ]]; then
|
||||||
service_pid=$(<$service_pidfile)
|
service_pid=$(<$service_pidfile)
|
||||||
|
@ -361,6 +398,7 @@ main() {
|
||||||
5) printf 'readyness check timed out.\n';;
|
5) printf 'readyness check timed out.\n';;
|
||||||
7) printf 'dependencies failed: %s.\n' "${failed_deps[@]}";;
|
7) printf 'dependencies failed: %s.\n' "${failed_deps[@]}";;
|
||||||
9) printf 'service_command does not exist: %s.\n' "${service_command[0]}";;
|
9) printf 'service_command does not exist: %s.\n' "${service_command[0]}";;
|
||||||
|
13) printf '%s: failed to create temporary files.\n';;
|
||||||
*) printf 'fail.\n';;
|
*) printf 'fail.\n';;
|
||||||
esac
|
esac
|
||||||
;;
|
;;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user