From e980a7e1dc616eed5ade9de500a9d2cd0f3df81c Mon Sep 17 00:00:00 2001 From: fbt Date: Sun, 13 Nov 2016 17:33:58 +0300 Subject: [PATCH] sig passthrough and tmpfiles Signed-off-by: fbt --- sm | 68 ++++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 53 insertions(+), 15 deletions(-) diff --git a/sm b/sm index 7adf970..bbe2841 100755 --- a/sm +++ b/sm @@ -26,23 +26,17 @@ svc() { svc::cleanup() { kill -n "$service_stop_signal" "$job_pid" - pid_wait "$service_pid" && { + pid_wait "$job_pid" && { rm -f $svc_pidfile $service_ready_flag } }; trap 'svc::cleanup' TERM - svc::reload() { - kill -n "$service_reload_signal" "$job_pid" - }; trap 'svc::reload' HUP - "$@" & job_pid=$! - while nullexec kill -n 0 "$job_pid"; do - wait - done + printf '%s' "$job_pid" > "$svc_pidfile" + wait "$job_pid" } -#cat >/dev/null << EOF ## Respawn respawn() { declare jobs job_pid @@ -58,12 +52,25 @@ respawn() { exit 0 }; 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 exec "$@" & job_pid=$! - wait "$job_pid" + + while nullexec kill -n 0 "$job_pid"; do + wait "$job_pid" + done done } -#EOF ## Run a command with its output discarded nullexec() { @@ -132,6 +139,32 @@ depend() { 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_ready() { declare s @@ -156,6 +189,8 @@ super_start() { depend "${service_depends[@]}" || return 7 depend_ready "${service_depends_ready[@]}" || return 7 + mktmpfiles || return 13 + if (( service_managed )); then if (( service_respawn )); then svc respawn "${service_command[@]}" &>"$service_logfile" & @@ -163,10 +198,6 @@ super_start() { svc "${service_command[@]}" &>"$service_logfile" & fi - svc_pid=$! - - printf '%s' "$svc_pid" > "$svc_pidfile" - if timer "$service_ready_timeout" ready; then printf '1' > "$service_ready_flag" else @@ -246,6 +277,9 @@ ready() { :; } # Code main() { + # Needs to be global + declare -g service_pid + # Let's set some defaults service_managed=1 @@ -308,6 +342,9 @@ main() { default service_ready_flag "$rundir/$service_name.ready" 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 if [[ -f "$service_pidfile" ]]; then service_pid=$(<$service_pidfile) @@ -361,6 +398,7 @@ main() { 5) printf 'readyness check timed out.\n';; 7) printf 'dependencies failed: %s.\n' "${failed_deps[@]}";; 9) printf 'service_command does not exist: %s.\n' "${service_command[0]}";; + 13) printf '%s: failed to create temporary files.\n';; *) printf 'fail.\n';; esac ;;