2016-11-13 08:11:47 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
shopt -s nullglob
|
|
|
|
|
|
|
|
# Utility functions
|
|
|
|
## Make setting default values a bit less awkward
|
|
|
|
default() {
|
|
|
|
declare -n _p=$1
|
|
|
|
|
|
|
|
if ! [[ "$_p" ]]; then
|
|
|
|
_p=$2
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
## Die. Why not?
|
|
|
|
die() {
|
|
|
|
declare code=${1:-0}
|
|
|
|
|
|
|
|
[[ "$2" ]] && printf '%s\n' "$2"
|
|
|
|
exit "$code"
|
|
|
|
}
|
|
|
|
|
|
|
|
## Run the command and wait for it to die
|
|
|
|
svc() {
|
2016-11-13 13:31:17 +00:00
|
|
|
declare job_pid
|
|
|
|
|
2016-11-13 08:11:47 +00:00
|
|
|
svc::cleanup() {
|
2016-11-13 13:31:17 +00:00
|
|
|
kill -n "$service_stop_signal" "$job_pid"
|
2016-11-13 08:11:47 +00:00
|
|
|
|
|
|
|
pid_wait "$service_pid" && {
|
2016-11-13 11:47:20 +00:00
|
|
|
rm -f $svc_pidfile $service_ready_flag
|
2016-11-13 08:11:47 +00:00
|
|
|
}
|
2016-11-13 13:31:17 +00:00
|
|
|
}; trap 'svc::cleanup' TERM
|
2016-11-13 08:11:47 +00:00
|
|
|
|
|
|
|
svc::reload() {
|
2016-11-13 13:31:17 +00:00
|
|
|
kill -n "$service_reload_signal" "$job_pid"
|
2016-11-13 08:11:47 +00:00
|
|
|
}; trap 'svc::reload' HUP
|
|
|
|
|
2016-11-13 13:31:17 +00:00
|
|
|
"$@" & job_pid=$!
|
2016-11-13 11:47:20 +00:00
|
|
|
|
2016-11-13 13:31:17 +00:00
|
|
|
while nullexec kill -n 0 "$job_pid"; do
|
2016-11-13 11:47:20 +00:00
|
|
|
wait
|
|
|
|
done
|
2016-11-13 08:11:47 +00:00
|
|
|
}
|
|
|
|
|
2016-11-13 11:47:20 +00:00
|
|
|
#cat >/dev/null << EOF
|
|
|
|
## Respawn
|
2016-11-13 08:11:47 +00:00
|
|
|
respawn() {
|
2016-11-13 13:31:17 +00:00
|
|
|
declare jobs job_pid
|
|
|
|
|
2016-11-13 08:11:47 +00:00
|
|
|
respawn::cleanup() {
|
2016-11-13 13:31:17 +00:00
|
|
|
jobs=( $(jobs -p) )
|
|
|
|
|
|
|
|
if [[ "$jobs" ]]; then
|
|
|
|
kill -n 15 "${jobs[@]}"
|
|
|
|
wait "${jobs[@]}"
|
|
|
|
fi
|
|
|
|
|
2016-11-13 08:11:47 +00:00
|
|
|
exit 0
|
2016-11-13 11:47:20 +00:00
|
|
|
}; trap 'respawn::cleanup' TERM
|
2016-11-13 08:11:47 +00:00
|
|
|
|
|
|
|
while true; do
|
2016-11-13 13:31:17 +00:00
|
|
|
exec "$@" & job_pid=$!
|
|
|
|
wait "$job_pid"
|
2016-11-13 08:11:47 +00:00
|
|
|
done
|
|
|
|
}
|
2016-11-13 11:47:20 +00:00
|
|
|
#EOF
|
2016-11-13 08:11:47 +00:00
|
|
|
|
|
|
|
## Run a command with its output discarded
|
|
|
|
nullexec() {
|
|
|
|
"$@" &>/dev/null
|
|
|
|
}
|
|
|
|
|
|
|
|
## Wait for a pid to die
|
|
|
|
pid_wait() {
|
|
|
|
declare cnt=0
|
|
|
|
|
|
|
|
while nullexec kill -0 "$1"; do
|
2016-11-13 11:47:20 +00:00
|
|
|
(( cnt >= (service_stop_timeout*10) )) && return 1
|
|
|
|
sleep 0.1
|
2016-11-13 08:11:47 +00:00
|
|
|
(( cnt++ ))
|
|
|
|
done
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
## See if NAME is a function
|
|
|
|
is_function() {
|
|
|
|
declare name=$1 name_tupe
|
|
|
|
|
|
|
|
name_type=$( type -t "$name" )
|
|
|
|
|
|
|
|
if [[ $name_type == 'function' ]]; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-11-13 11:47:20 +00:00
|
|
|
## Simple timer
|
|
|
|
timer() {
|
|
|
|
declare cnt timeout=$1
|
|
|
|
shift
|
|
|
|
|
|
|
|
while ! "$@"; do
|
|
|
|
(( cnt >= (timeout*10) )) && return 1
|
|
|
|
sleep 0.1
|
|
|
|
(( cnt++ ))
|
|
|
|
done
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
## Is a service ready?
|
|
|
|
is_ready() [[ -f "$service_ready_flag" ]]
|
|
|
|
|
|
|
|
## Wait for this service to get ready
|
|
|
|
wait_ready() {
|
|
|
|
timer "$service_ready_timeout" is_ready
|
|
|
|
}
|
|
|
|
|
|
|
|
## Depend on other services to be started
|
|
|
|
depend() {
|
|
|
|
declare s
|
|
|
|
|
|
|
|
for s in "$@"; do
|
|
|
|
if ! "$0" "$s" qstatus; then
|
|
|
|
nullexec "$0" "$s" start || {
|
|
|
|
failed_deps+=( "$s" )
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
## Depend on other services to be ready
|
|
|
|
depend_ready() {
|
|
|
|
declare s
|
|
|
|
|
|
|
|
depend "$@" || return 1
|
|
|
|
|
|
|
|
for s in "$@"; do
|
|
|
|
"$0" "$s" wait_ready || {
|
|
|
|
failed_deps+=( "$s" )
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
done
|
2016-11-13 08:11:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Super functions
|
|
|
|
## Start the service, write down the svc pid
|
|
|
|
super_start() {
|
|
|
|
(( service_running )) && return 3
|
|
|
|
|
2016-11-13 11:47:20 +00:00
|
|
|
[[ -f "${service_command[0]}" ]] || return 9
|
|
|
|
|
|
|
|
depend "${service_depends[@]}" || return 7
|
|
|
|
depend_ready "${service_depends_ready[@]}" || return 7
|
|
|
|
|
2016-11-13 08:11:47 +00:00
|
|
|
if (( service_managed )); then
|
|
|
|
if (( service_respawn )); then
|
|
|
|
svc respawn "${service_command[@]}" &>"$service_logfile" &
|
|
|
|
else
|
|
|
|
svc "${service_command[@]}" &>"$service_logfile" &
|
|
|
|
fi
|
|
|
|
|
|
|
|
svc_pid=$!
|
|
|
|
|
|
|
|
printf '%s' "$svc_pid" > "$svc_pidfile"
|
|
|
|
|
2016-11-13 13:31:17 +00:00
|
|
|
if timer "$service_ready_timeout" ready; then
|
|
|
|
printf '1' > "$service_ready_flag"
|
|
|
|
else
|
|
|
|
return 5
|
|
|
|
fi
|
|
|
|
elif (( service_oneshot )); then
|
|
|
|
"${service_command[@]}"; res=$?
|
|
|
|
(( $res )) && return "$res"
|
|
|
|
printf '1' > "$service_enabled_flag"
|
2016-11-13 11:47:20 +00:00
|
|
|
else
|
2016-11-13 13:31:17 +00:00
|
|
|
exec "${service_command[@]}" &
|
2016-11-13 11:47:20 +00:00
|
|
|
fi
|
|
|
|
|
2016-11-13 08:11:47 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2016-11-13 11:47:20 +00:00
|
|
|
# A separate function for oneshot services
|
|
|
|
super_oneshot() {
|
|
|
|
(( service_enabled )) && return 3
|
|
|
|
}
|
|
|
|
|
2016-11-13 08:11:47 +00:00
|
|
|
## Reload the service
|
|
|
|
## Usually just sends HUP
|
|
|
|
super_reload() {
|
|
|
|
(( service_running )) || return 3
|
|
|
|
|
2016-11-13 13:31:17 +00:00
|
|
|
if (( service_managed )); then
|
|
|
|
kill -n 1 "$service_pid"
|
|
|
|
else
|
|
|
|
kill -n "$service_reload_signal" "$service_pid"
|
|
|
|
fi
|
2016-11-13 08:11:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
## Stop the service
|
|
|
|
## Returns:
|
|
|
|
## 3: Service is not running.
|
|
|
|
super_stop() {
|
2016-11-13 13:31:17 +00:00
|
|
|
if (( service_oneshot )); then
|
|
|
|
(( service_enabled )) || return 3
|
2016-11-13 08:11:47 +00:00
|
|
|
|
2016-11-13 13:31:17 +00:00
|
|
|
rm -f "$service_enabled_flag"
|
2016-11-13 08:11:47 +00:00
|
|
|
|
2016-11-13 13:31:17 +00:00
|
|
|
return 0
|
|
|
|
else
|
|
|
|
(( service_running )) || return 3
|
|
|
|
|
|
|
|
nullexec kill -n "$service_stop_signal" "$service_pid" || return 1
|
|
|
|
pid_wait "$service_pid" || return 5
|
|
|
|
|
|
|
|
return 0
|
|
|
|
fi
|
2016-11-13 08:11:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Overloadable functions
|
|
|
|
start() { super_start; }
|
|
|
|
stop() { super_stop; }
|
2016-11-13 11:47:20 +00:00
|
|
|
reload() { super_reload; }
|
2016-11-13 08:11:47 +00:00
|
|
|
restart() {
|
|
|
|
"$0" "$service_name" stop
|
|
|
|
"$0" "$service_name" start
|
|
|
|
}
|
|
|
|
|
2016-11-13 11:47:20 +00:00
|
|
|
logs() { cat "$service_logfile"; }
|
|
|
|
|
|
|
|
## Status is a bit of a special case. It's talkative.
|
|
|
|
status() {
|
|
|
|
(( service_running )) && return 0
|
2016-11-13 13:31:17 +00:00
|
|
|
(( service_enabled )) && return 0
|
2016-11-13 11:47:20 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
## For use in scripts
|
|
|
|
qstatus() { nullexec status; }
|
|
|
|
|
|
|
|
## By default there is no ready check
|
|
|
|
ready() { :; }
|
|
|
|
|
2016-11-13 08:11:47 +00:00
|
|
|
# Code
|
|
|
|
main() {
|
|
|
|
# Let's set some defaults
|
|
|
|
service_managed=1
|
|
|
|
|
|
|
|
if (( $UID )); then
|
|
|
|
# XDG stuff
|
|
|
|
default XDG_CONFIG_HOME "$HOME/.config"
|
|
|
|
default XDG_RUNTIME_DIR "/run/user/$UID"
|
|
|
|
|
|
|
|
cfgdir="$XDG_CONFIG_HOME/sm"
|
|
|
|
rundir="$XDG_RUNTIME_DIR/sm"
|
|
|
|
logdir="$HOME/log/sm"
|
|
|
|
else
|
|
|
|
cfgdir='/etc/sm'
|
|
|
|
rundir='/run/sm'
|
|
|
|
logdir='/var/log/sm'
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Load custom functions
|
|
|
|
for f in "$cfgdir/functions"/*; do
|
|
|
|
source "$f" || die 9 "Failed to source functions from $f"
|
|
|
|
done
|
|
|
|
|
|
|
|
# Now create the needed runtime stuff
|
|
|
|
for d in "$rundir" "$logdir"; do
|
|
|
|
mkdir -p "$d" || die 3 "Failed to create runtime dir: $d"
|
|
|
|
done
|
|
|
|
|
|
|
|
# service_name is just $1
|
|
|
|
service_name=$1
|
|
|
|
|
|
|
|
# Semi-hardcoded stuff
|
|
|
|
svc_pidfile="$rundir/$service_name.pid"
|
|
|
|
|
|
|
|
# Get the service defaults
|
|
|
|
[[ -f "$cfgdir/conf.d/$1" ]] && {
|
|
|
|
source "$cfgdir/conf.d/$1" || die 5 "Failed to read service defaults: $cfgdir/conf.d/$1"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Get the service config
|
|
|
|
source "$cfgdir/init.d/$1" || die 7 "Failed to read the service config: $cfgdir/init.d/$1"
|
|
|
|
|
|
|
|
# Legacy
|
|
|
|
[[ "$service_args" ]] && service_command=( "${service_command[@]}" "${service_args[@]}" )
|
|
|
|
[[ "$service_respawn" == 'true' ]] && service_respawn=1
|
2016-11-13 13:31:17 +00:00
|
|
|
[[ "$service_type" == 'oneshot' ]] && { service_oneshot=1; service_managed=0; }
|
2016-11-13 08:11:47 +00:00
|
|
|
|
|
|
|
[[ "$service_pidfile" ]] && service_managed=0
|
|
|
|
|
|
|
|
if ! (( service_managed )); then
|
|
|
|
(( service_respawn )) && die 21 "Refusing to respawn a service that manages itself."
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Service-level defaults
|
|
|
|
default service_pidfile "$svc_pidfile"
|
|
|
|
default service_logfile "$logdir/$service_name.log"
|
|
|
|
default service_stop_timeout 30
|
2016-11-13 11:47:20 +00:00
|
|
|
default service_ready_timeout 15
|
2016-11-13 08:11:47 +00:00
|
|
|
default service_stop_signal 15
|
|
|
|
default service_reload_signal 1
|
2016-11-13 11:47:20 +00:00
|
|
|
default service_ready_flag "$rundir/$service_name.ready"
|
2016-11-13 13:31:17 +00:00
|
|
|
default service_enabled_flag "$rundir/$service_name.enabled"
|
2016-11-13 08:11:47 +00:00
|
|
|
|
|
|
|
# Let's see if there's a PID
|
|
|
|
if [[ -f "$service_pidfile" ]]; then
|
|
|
|
service_pid=$(<$service_pidfile)
|
|
|
|
|
|
|
|
# Let's see if it's running
|
|
|
|
if nullexec kill -0 "$service_pid"; then
|
|
|
|
service_running=1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2016-11-13 13:31:17 +00:00
|
|
|
# Maybe the service is enabled?
|
|
|
|
if [[ -f "$service_enabled_flag" ]]; then
|
|
|
|
# Yay, it is!
|
|
|
|
service_enabled=1
|
|
|
|
fi
|
|
|
|
|
2016-11-13 08:11:47 +00:00
|
|
|
# Check if action is even defined
|
|
|
|
is_function "$2" || die 17 "Function $2 is not defined for $service_name."
|
|
|
|
|
|
|
|
# Run pre_$action function
|
|
|
|
if is_function "pre_$2"; then
|
|
|
|
"pre_$2" || {
|
2016-11-13 11:47:20 +00:00
|
|
|
printf 'pre_%s failed!\n' "$2"
|
|
|
|
die 13
|
2016-11-13 08:11:47 +00:00
|
|
|
}
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Run the function
|
|
|
|
case "$2" in
|
|
|
|
stop)
|
|
|
|
printf 'Stopping %s... ' "$service_name"
|
|
|
|
|
2016-11-13 11:47:20 +00:00
|
|
|
stop; res=$?
|
2016-11-13 08:11:47 +00:00
|
|
|
|
2016-11-13 11:47:20 +00:00
|
|
|
case "$res" in
|
2016-11-13 08:11:47 +00:00
|
|
|
0) printf 'ok.\n';;
|
|
|
|
3) printf 'not running.\n' "$service_name";;
|
|
|
|
5) printf 'timed out.\n';;
|
|
|
|
*) printf 'fail.\n';;
|
|
|
|
esac
|
|
|
|
;;
|
|
|
|
|
|
|
|
start)
|
|
|
|
printf 'Starting %s... ' "$service_name"
|
|
|
|
|
2016-11-13 11:47:20 +00:00
|
|
|
start; res=$?
|
2016-11-13 08:11:47 +00:00
|
|
|
|
2016-11-13 11:47:20 +00:00
|
|
|
case "$res" in
|
2016-11-13 08:11:47 +00:00
|
|
|
0) printf 'ok.\n';;
|
|
|
|
3) printf 'already running.\n';;
|
2016-11-13 11:47:20 +00:00
|
|
|
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]}";;
|
2016-11-13 08:11:47 +00:00
|
|
|
*) printf 'fail.\n';;
|
|
|
|
esac
|
|
|
|
;;
|
|
|
|
|
2016-11-13 11:47:20 +00:00
|
|
|
reload)
|
|
|
|
printf 'Reloading %s... ' "$service_name"
|
|
|
|
|
|
|
|
reload; res=$?
|
|
|
|
|
|
|
|
case "$res" in
|
|
|
|
0) printf 'ok.\n';;
|
|
|
|
*) printf 'fail.\n';;
|
|
|
|
esac
|
|
|
|
;;
|
|
|
|
|
|
|
|
status)
|
|
|
|
status; res=$?
|
|
|
|
|
|
|
|
case "$res" in
|
2016-11-13 13:31:17 +00:00
|
|
|
0)
|
|
|
|
if (( service_oneshot )); then
|
|
|
|
printf '%s is enabled.\n' "$service_name"
|
|
|
|
else
|
|
|
|
printf '%s is running.\n' "$service_name"
|
|
|
|
fi;;
|
|
|
|
|
|
|
|
1)
|
|
|
|
if (( service_oneshot )); then
|
|
|
|
printf '%s is not enabled.\n' "$service_name"
|
|
|
|
else
|
|
|
|
printf '%s is not running.\n' "$service_name"
|
|
|
|
fi;;
|
|
|
|
|
2016-11-13 11:47:20 +00:00
|
|
|
*) printf '%s: status unknown.\n' "$service_name";;
|
|
|
|
esac
|
|
|
|
;;
|
|
|
|
|
|
|
|
logs) logs;;
|
|
|
|
|
|
|
|
*) "$2"; res=$?;;
|
2016-11-13 08:11:47 +00:00
|
|
|
esac
|
|
|
|
|
2016-11-13 11:47:20 +00:00
|
|
|
(( res )) && return "$res"
|
|
|
|
|
2016-11-13 08:11:47 +00:00
|
|
|
# Run post_$action function
|
|
|
|
if is_function "post_$2"; then
|
|
|
|
"post_$2" || {
|
2016-11-13 11:47:20 +00:00
|
|
|
printf 'post_%s failed!\n' "$2"
|
|
|
|
die 15
|
2016-11-13 08:11:47 +00:00
|
|
|
}
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
main "$@"
|