Files
ssm/ssm
T

952 lines
22 KiB
Bash
Raw Normal View History

2016-11-13 11:11:47 +03:00
#!/usr/bin/env bash
shopt -s nullglob
# Utility functions
2018-03-06 19:47:42 +03:00
is_function() [[ "$(type -t "$1" 2>/dev/null)" == 'function' ]]
2018-01-22 20:08:48 +03:00
readonly -f is_function
2017-12-08 14:39:50 +03:00
2018-03-04 16:55:53 +03:00
usage() {
cat <<- EOF
Usage: ssm <service> <function>
EOF
2018-03-05 00:16:45 +03:00
}; readonly usage;
2018-03-04 16:55:53 +03:00
2018-01-08 04:38:24 +03:00
var() {
2018-03-04 19:26:40 +03:00
declare var_function=$1; shift
declare var_name
2018-01-08 04:38:24 +03:00
2018-01-10 22:00:22 +03:00
# This enforces bash's grammar against things like
# var 'cat /etc/shadow; foo' ...
2018-03-04 19:26:40 +03:00
[[ $var_function =~ ^[a-zA-Z_][a-zA-Z0-9_]+?$ ]] || {
die 73 "On line $LINENO, in $FUNCNAME: Invalid identifier: '$var_function'"
2018-01-10 22:00:22 +03:00
}
2018-03-04 19:26:40 +03:00
if [[ "$1" == '-v' ]]; then
var_name=$2
shift 2
else
var_name=$var_function
fi
if ! is_function "$var_function"; then
2018-01-08 04:38:24 +03:00
eval "
2018-03-04 19:26:40 +03:00
${var_function}() {
2018-01-08 04:38:24 +03:00
declare mode=set
2018-03-04 19:26:40 +03:00
declare -n _var=\"${var_name}\"
2018-01-08 04:38:24 +03:00
if (( \$# )); then
case \"\$1\" in
('=') mode=set;;
('+=') mode=append;;
('_=') mode=prepend;;
(':=') mode=default;;
('==') mode=compare;;
('=~') mode=regex;;
2018-01-09 23:47:50 +03:00
('is') mode=\"is_\$2\";;
2018-01-08 10:33:06 +03:00
('u') mode=includes;;
2018-03-04 19:26:40 +03:00
(*) die 71 \"Syntax error in ${var_function}!\";;
2018-01-08 04:38:24 +03:00
esac
shift
else
mode='bool'
fi
case \"\$mode\" in
(set) _var=( \"\$@\" );;
(append) _var+=( \"\$@\" );;
(prepend) _var=( \"\$@\" \"\${var[@]}\" );;
(default) [[ \"\$_var\" ]] || _var=( \"\$@\" );;
(compare) [[ \"\$_var\" == \"\$*\" ]];;
(regex) [[ \"\$_var\" =~ \$@ ]];;
(bool)
case \"\${_var,,}\" in
(''|'false'|'0') return 1;;
(*) return 0;;
esac
;;
2018-01-08 10:33:06 +03:00
(includes)
for i in \"\${_var[@]}\"; do
[[ \"\$i\" == \"\$*\" ]] && return 0
done
return 1
;;
2018-01-09 23:47:50 +03:00
(is_fs_object) [[ -e \"\$_var\" ]];;
(is_file) [[ -f \"\$_var\" ]];;
(is_dir|is_directory) [[ -d \"\$_var\" ]];;
2018-03-05 01:36:40 +03:00
(is_empty) [[ -z \"\${_var[*]}\" ]];;
2018-01-09 23:47:50 +03:00
2018-03-04 19:26:40 +03:00
(*) die 71 \"Syntax error in ${var_function}!\";;
2018-01-08 04:38:24 +03:00
esac
2018-03-04 19:26:40 +03:00
}; readonly -f \"${var_function}\"
2018-01-08 04:38:24 +03:00
2018-03-04 19:26:40 +03:00
${var_function}++() {
declare -n _var=\"${var_name}\"
(( _var++ ))
2018-01-08 04:38:24 +03:00
}
2018-03-04 19:26:40 +03:00
${var_function}--() {
declare -n _var=\"${var_name}\"
(( _var-- ))
2018-01-08 04:38:24 +03:00
}
"
fi
if (( $# )); then
case "$1" in
('='|'=='|'=~'|'+='|'_='|':=')
2018-03-04 19:26:40 +03:00
"$var_function" "$@"
2018-01-08 04:38:24 +03:00
;;
(*)
for v in "$@"; do
var "$v"
done
;;
esac
fi
2018-01-22 20:08:48 +03:00
}; readonly -f var
2016-11-13 11:11:47 +03:00
## Die. Why not?
die() {
declare code=${1:-0}
[[ "$2" ]] && printf '%s\n' "$2"
exit "$code"
2017-12-14 18:41:03 +03:00
}; readonly -f die
2016-11-13 11:11:47 +03:00
if_service_action() {
2018-03-06 19:42:31 +03:00
for f in "service::$1" "$1"; do
is_function "$f" && return 0
done
2018-03-06 19:47:42 +03:00
return 1
}; readonly -f if_service_action
run_service_action() {
2018-03-06 19:42:31 +03:00
for f in "service::$1" "$1"; do
is_function "$f" && {
"$f"; return $?
}
done
return 0
}; readonly -f run_service_action
2018-03-04 16:55:53 +03:00
spawn() {
if [[ $service_logfile_out == "$service_logfile_err" ]]; then
exec "$@" >"$service_logfile_out" 2>&1
else
exec "$@" >"$service_logfile_out" 2>"$service_logfile_err"
fi
2018-03-05 00:16:45 +03:00
}; readonly spawn;
2018-03-04 16:55:53 +03:00
2018-03-05 01:36:40 +03:00
cgroup_get_procs() {
if service_cgroup_path is dir; then
while read -r line; do
service_cgroup_procs += "$line"
done < "$cgroup_home/$service_cgroup_name/cgroup.procs"
fi
}; readonly -f cgroup_get_procs
2016-11-13 11:11:47 +03:00
## Run the command and wait for it to die
svc() {
2018-03-05 01:13:55 +03:00
declare job_pid job_exit job_success last_respawn fail_counter date counter p
var job_pid job_exit job_success last_respawn fail_counter date counter p
2018-01-08 08:07:18 +03:00
2016-11-13 11:11:47 +03:00
svc::cleanup() {
2018-01-08 05:48:40 +03:00
nullexec kill -n "$service_stop_signal" "$job_pid"
2016-11-13 11:11:47 +03:00
2018-03-04 19:26:40 +03:00
anywait "$job_pid" "$service_stop_timeout"
2018-03-04 16:55:53 +03:00
2018-03-05 01:13:55 +03:00
# Cgroup stuff
if cgroups; then
if service_cgroup_cleanup; then
2018-03-05 01:36:40 +03:00
cgroup_get_procs
service_cgroup_procs is empty || {
for p in "${service_cgroup_procs[@]}"; do
p == "$BASHPID" || {
nullexec kill -n "$service_cgroup_kill_signal" "$p"
anywait "$p" "$service_stop_timeout" &
}
done
2018-03-05 01:13:55 +03:00
2018-03-05 01:36:40 +03:00
wait || die $?
}
2018-03-05 01:13:55 +03:00
fi
fi
2018-03-06 18:21:21 +03:00
run_service_action 'cleanup'
rm -f "$svc_pidfile" "$service_pidfile" "$service_ready_flag"
2018-03-09 02:31:14 +03:00
die "$job_exit"
2017-07-13 15:37:53 +03:00
}; trap 'svc::cleanup' TERM
2016-11-13 11:11:47 +03:00
2018-03-04 19:26:40 +03:00
svc::reload() {
nullexec kill -n "$service_reload_signal" "$job_pid"
}; 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
2018-03-04 16:55:53 +03:00
printf '%s' $BASHPID > "$svc_pidfile"
2018-03-05 00:35:29 +03:00
# Cgroups
if cgroups; then
mkdir -p "$cgroup_home/$service_cgroup_name"
echo "$BASHPID" > "$cgroup_home/$service_cgroup_name/cgroup.procs"
fi
2018-03-04 16:55:53 +03:00
while true; do
2018-03-04 19:26:40 +03:00
job_success = 0 # Needs to be reset
2018-03-04 16:55:53 +03:00
2018-03-05 02:52:55 +03:00
# Remove stale pidfiles some services may leave behind
# If a pidfile exists at this point in the code, it should be stale.
if service_pidfile_remove_stale; then
if ! svc_pidfile == "$service_pidfile"; then
service_pidfile is file && rm -f "$service_pidfile"
fi
fi
2018-03-06 18:21:21 +03:00
# Some setup might be required on each loop
run_service_action 'setup'
2018-03-04 16:55:53 +03:00
# Spawn the process and record the PID
spawn "$@" & job_pid = "$!"
# Wait for the process to exit and record the exit code
2018-03-04 19:26:40 +03:00
# This depends on a few things
2018-03-09 02:31:14 +03:00
if service_own_pidfile; then
2018-03-04 19:26:40 +03:00
# We need to wait for the service to write down its pidfile
until service_pidfile is file; do
(( counter >= service_pidfile_timeout*10 )) && {
2018-03-05 00:16:45 +03:00
printf '127' > "$service_exit_file"
2018-03-04 19:26:40 +03:00
break
}
counter++
sleep 0.1
done
read -r job_pid < "$service_pidfile"
2018-03-09 02:31:14 +03:00
# We consider any termination of a service with its own pidfile
# to be a failure
2018-03-04 19:26:40 +03:00
anywait "$job_pid"; job_exit=127
2018-03-09 02:31:14 +03:00
else
printf '%s' "$job_pid" > "$service_pidfile"
wait "$job_pid"; job_exit=$?
2018-03-04 19:26:40 +03:00
fi
2018-03-04 16:55:53 +03:00
2018-03-09 02:31:14 +03:00
# One service failure, two service failures...
2018-03-04 16:55:53 +03:00
if service_success_exit u "$job_exit"; then
job_success = 1
(( fail_counter )) && fail_counter--
else
job_success = 0
fail_counter++
fi
2018-03-09 02:31:14 +03:00
# Record the exit code
2018-03-05 00:16:45 +03:00
printf '%s' "$job_exit" > "$service_exit_file"
2018-03-04 16:55:53 +03:00
# Back off if the service exits too much AND too quickly.
2018-03-09 02:31:14 +03:00
if ! service_respawn_force; then
2018-03-04 19:26:40 +03:00
if (( fail_counter >= 3 )); then
printf -v date '%(%s)T'
2018-03-04 16:55:53 +03:00
2018-03-04 19:26:40 +03:00
(( (date - last_respawn) <= 5 )) && break
fi
2018-03-09 02:31:14 +03:00
fi
2018-03-04 16:55:53 +03:00
# Respawn, if necessary
service_respawn_flag || break
case $service_respawn in
(on-success) job_success || break;;
(on-failure) job_success && break;;
esac
2018-01-07 09:12:12 +03:00
2018-03-04 16:55:53 +03:00
# Record the time every time we restart the loop
printf -v last_respawn '%(%s)T'
done
2017-03-11 17:36:07 +03:00
svc::cleanup
2017-12-14 18:41:03 +03:00
}; readonly -f svc
2016-11-13 11:11:47 +03:00
## Run a command with its output discarded
2017-12-14 18:41:03 +03:00
nullexec() { "$@" &>/dev/null; }
readonly -f nullexec
2016-11-13 11:11:47 +03:00
2018-03-04 19:26:40 +03:00
## Wait for a pid, indefinitely
anywait() {
declare counter timeout
var counter = 0
var timeout = "$2"
2016-11-13 11:11:47 +03:00
while nullexec kill -0 "$1"; do
2018-03-04 19:26:40 +03:00
timeout && {
(( counter >= timeout )) && return 1
counter++
}
2016-11-13 14:47:20 +03:00
sleep 0.1
2016-11-13 11:11:47 +03:00
done
return 0
2018-03-04 19:26:40 +03:00
}; readonly -f anywait
2016-11-13 11:11:47 +03:00
2016-11-13 14:47:20 +03:00
## Simple timer
timer() {
2018-01-08 04:38:24 +03:00
declare cnt timeout
var cnt = 0
var timeout = "$1"
2016-11-13 14:47:20 +03:00
shift
while ! "$@"; do
(( cnt >= (timeout*10) )) && return 1
sleep 0.1
2018-01-08 04:38:24 +03:00
cnt++
2016-11-13 14:47:20 +03:00
done
return 0
2017-12-14 18:41:03 +03:00
}; readonly -f timer
2016-11-13 14:47:20 +03:00
2018-03-05 00:16:45 +03:00
## Set a service's ready flag.
set_ready() { printf '1' > "$service_ready_flag"; }
readonly -f set_ready
2016-11-13 14:47:20 +03:00
## Is a service ready?
2018-01-09 23:47:50 +03:00
is_ready() { service_ready_flag is file; }
2017-12-14 18:41:03 +03:00
readonly -f is_ready
2016-11-13 14:47:20 +03:00
## Wait for this service to get ready
2017-12-14 18:41:03 +03:00
wait_ready() { timer "$service_ready_timeout" is_ready; }
readonly -f wait_ready
2016-11-13 14:47:20 +03:00
## Depend on other services to be started
depend() {
declare s
for s in "$@"; do
2017-12-14 07:07:48 +03:00
if ! "$_self" "$s" qstatus; then
nullexec "$_self" "$s" start || {
2018-01-08 04:38:24 +03:00
failed_deps += "$s"
2016-11-13 14:47:20 +03:00
return 1
}
fi
done
2017-12-14 18:41:03 +03:00
}; readonly -f depend
2016-11-13 14:47:20 +03:00
2016-11-13 17:33:58 +03:00
## 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
2017-12-14 18:41:03 +03:00
}; readonly -f mktmpfiles
2016-11-13 17:33:58 +03:00
2016-11-13 14:47:20 +03:00
## Depend on other services to be ready
depend_ready() {
declare s
depend "$@" || return 1
for s in "$@"; do
2017-12-14 07:07:48 +03:00
"$_self" "$s" wait_ready || {
2018-01-08 04:38:24 +03:00
failed_deps += "$s"
2016-11-13 14:47:20 +03:00
return 1
}
done
2017-12-14 18:41:03 +03:00
}; readonly -f depend_ready
result() {
2018-01-08 04:38:24 +03:00
declare rc; var rc = "$1"; shift
2017-12-14 18:41:03 +03:00
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
2016-11-13 11:11:47 +03:00
2017-12-14 18:41:03 +03:00
# Overloadable functions
2016-11-13 11:11:47 +03:00
## Start the service, write down the svc pid
2017-12-14 18:41:03 +03:00
start() {
2018-01-08 04:38:24 +03:00
service_running && return 3
2018-01-09 23:47:50 +03:00
service_command is file || return 9
2016-11-13 14:47:20 +03:00
2018-03-05 02:52:55 +03:00
# Preform cgroup checks
2018-01-08 10:33:06 +03:00
if cgroups; then
if service_cgroup_exclusive; then
2018-03-05 01:36:40 +03:00
service_cgroup_procs is empty || return 15
2018-01-08 10:33:06 +03:00
fi
fi
2016-11-13 14:47:20 +03:00
depend "${service_depends[@]}" || return 7
depend_ready "${service_depends_ready[@]}" || return 7
2018-03-09 02:31:14 +03:00
rm -f "$service_stopped_flag"
2016-11-13 17:33:58 +03:00
mktmpfiles || return 13
2018-03-09 02:31:14 +03:00
svc "${service_command[@]}" & job=$!
2018-01-08 10:33:06 +03:00
2018-03-04 19:26:40 +03:00
if service_oneshot; then
2018-03-09 02:31:14 +03:00
wait "$job"; res=$?
2018-03-04 19:26:40 +03:00
(( res )) && {
2018-03-05 00:16:45 +03:00
printf '%s' "$res" > "$service_exit_file"
2018-03-09 02:31:14 +03:00
return 17
2018-03-04 19:26:40 +03:00
}
2018-03-09 02:31:14 +03:00
fi
2016-11-13 11:11:47 +03:00
2018-03-09 02:31:14 +03:00
if timer "$service_ready_timeout" run_service_action 'ready'; then
set_ready
else
return 5
2016-11-13 14:47:20 +03:00
fi
2016-11-13 11:11:47 +03:00
return 0
}
## Reload the service
## Usually just sends HUP
2017-12-14 18:41:03 +03:00
reload() {
2018-01-08 04:38:24 +03:00
service_running || return 3
2018-01-07 09:12:12 +03:00
kill -n 1 "$svc_pid"
2016-11-13 11:11:47 +03:00
}
## Stop the service
## Returns:
## 3: Service is not running.
2017-12-14 18:41:03 +03:00
stop() {
2018-01-08 04:38:24 +03:00
if service_oneshot; then
service_enabled || return 3
2018-03-09 02:31:14 +03:00
return 7
2016-11-13 16:31:17 +03:00
else
2018-01-08 04:38:24 +03:00
service_running || return 3
2016-11-13 16:31:17 +03:00
kill -n 15 "$svc_pid" || return 1
2018-01-07 08:36:39 +03:00
anywait "$svc_pid" "$service_stop_timeout" || return 5
2017-11-09 04:19:26 +03:00
> "$service_stopped_flag"
2016-11-13 16:31:17 +03:00
2018-01-08 10:33:06 +03:00
# Cgroup stuff
if cgroups; then
if service_cgroup_kill; then
service_cgroup_wait = 1
for p in "${service_cgroup_procs[@]}"; do
nullexec kill -n "$service_cgroup_kill_signal" "$p"
done
fi
2018-01-08 10:33:06 +03:00
if service_cgroup_wait; then
for p in "${service_cgroup_procs[@]}"; do
2018-03-04 19:26:40 +03:00
anywait "$p" "$service_stop_timeout" &
2018-01-08 10:33:06 +03:00
done
wait || return 5
2018-01-08 10:33:06 +03:00
fi
fi
2016-11-13 16:31:17 +03:00
return 0
fi
2016-11-13 11:11:47 +03:00
}
2017-03-11 17:36:07 +03:00
info() {
2018-03-09 03:25:49 +03:00
declare -a _info_items
declare _status_code _status
var _info_items _status_code _status
_info_items = 'Name' "$service_name"
2018-01-08 04:38:24 +03:00
2018-03-09 02:31:14 +03:00
if service_oneshot; then
2018-03-09 03:25:49 +03:00
_info_items += 'Oneshot' 'yes'
2018-03-09 02:31:14 +03:00
fi
2017-03-11 17:57:16 +03:00
2018-03-09 03:25:49 +03:00
status; _status_code = "$?"
case $_status_code in
(0)
_status = 'running'
_info_items += 'PID' "$service_pid"
_info_items += 'PIDFile' "$service_pidfile"
;;
(2) _status = 'success';;
(7) _status = 'stopped';;
(9) _status = "failed: $service_exit_last";;
(11) _status = "exited: $service_exit_last";;
(*) _status = 'unknown';;
esac
_info_items += 'Status' "$_status"
_info_items += \
2018-01-08 14:22:13 +03:00
"Exec" "${service_command[*]}" \
2018-03-04 19:26:40 +03:00
"Respawn" "$service_respawn" \
2018-01-08 05:48:40 +03:00
"Config path" "$service_config" \
2017-03-11 17:57:16 +03:00
2018-03-09 03:25:49 +03:00
# Logs
_info_items += 'Output log' "$service_logfile_out"
service_logfile_out == "$service_logfile_err" || \
_info_items += "Error log" "$service_logfile_err"
2017-03-11 17:57:16 +03:00
2018-01-08 10:33:06 +03:00
# Show the cgroup
if cgroups; then
_info_items += "Cgroup" "$cgroup_home/$service_cgroup_name" \
"Cgroup procs" "${#service_cgroup_procs[@]}"
fi
printf "%16s: %s\n" "${_info_items[@]}"
2017-03-11 17:36:07 +03:00
}
2018-01-08 04:47:42 +03:00
## Restart just calls the script twice by default
2016-11-13 11:11:47 +03:00
restart() {
2017-12-14 07:07:48 +03:00
"$_self" "$service_name" stop
"$_self" "$service_name" start
2016-11-13 11:11:47 +03:00
}
2018-01-08 04:38:24 +03:00
edit() { $EDITOR "$service_config"; }
2018-03-04 16:55:53 +03:00
logs() {
if service_logfile_out == "$service_logfile_err"; then
$PAGER "$service_logfile_out"
else
printf '%s\n' "$service_logfile_out" "$service_logfile_err"
fi
}
2016-11-13 14:47:20 +03:00
## Status is a bit of a special case. It's talkative.
status() {
2018-01-08 04:47:42 +03:00
service_running && return 0
2018-03-05 03:42:30 +03:00
service_enabled && return 2
2018-03-09 03:25:49 +03:00
2018-01-08 04:47:42 +03:00
service_stopped && return 7
2018-03-09 03:25:49 +03:00
2018-03-04 16:55:53 +03:00
service_failed && return 9
2018-03-05 00:16:45 +03:00
service_exit_last is empty || return 11
2017-11-09 04:19:26 +03:00
2018-03-09 03:25:49 +03:00
if service_oneshot; then
return 13
else
return 1
fi
2016-11-13 14:47:20 +03:00
}
## For use in scripts
qstatus() { nullexec status; }
## By default there is no ready check
ready() { :; }
2018-03-04 19:26:40 +03:00
## Reset failes
2018-03-05 04:28:35 +03:00
reset-exit() { rm -f "$service_exit_file"; }
2018-03-04 19:26:40 +03:00
2017-12-14 18:41:03 +03:00
# Main code
2018-01-08 04:47:42 +03:00
## Empty declarations
var service_pid \
service_pidfile \
service_type \
service_depends_ready \
service_command \
service_config \
service_path \
service_name \
service_args \
2018-01-08 05:48:40 +03:00
service_logfile_out \
service_logfile_err \
2018-01-08 04:47:42 +03:00
service_ready_flag \
service_stopped_flag \
2018-03-05 00:16:45 +03:00
service_exit_file \
service_exit_last \
2018-01-08 10:33:06 +03:00
service_cgroup_name \
service_cgroup_procs \
2018-01-09 23:47:50 +03:00
service_cgroup_path \
service_signals_passthru \
2018-01-08 08:07:18 +03:00
cgroup_home \
2018-01-08 04:47:42 +03:00
failed_deps \
svc_pidfile \
svc_pid \
2018-01-08 04:47:42 +03:00
cfg_path \
2018-01-08 14:12:34 +03:00
cfg_file \
2018-01-08 04:47:42 +03:00
cfg_dir \
rundir \
logdir \
_self
2018-03-05 03:42:30 +03:00
## Internal defaults
var flag_list_services = 0
var flag_edit_service = 0
2018-03-05 04:28:35 +03:00
var flag_reset_exit = 0
2018-03-05 03:42:30 +03:00
2018-01-08 04:47:42 +03:00
## check for some environment stuff
2018-01-08 04:38:24 +03:00
var EDITOR := 'vim'
var PAGER := 'less'
2018-01-08 04:47:42 +03:00
var XDG_CONFIG_HOME := "$HOME/.config"
var XDG_RUNTIME_DIR := "/run/user/$UID"
2018-01-08 04:38:24 +03:00
2018-01-08 04:47:42 +03:00
## Let's set some defaults
2018-01-08 10:33:06 +03:00
# These are meaningful to reconfigure.
2018-03-04 16:55:53 +03:00
var service_respawn = 'no' # Respawn the service if it exits
2018-01-08 10:33:06 +03:00
var service_workdir = '/'
var service_stop_timeout = 30
var service_ready_timeout = 15
var service_signals = 1 10 12
var service_reload_signal = 1
var service_stop_signal = 15
var service_cgroup_exclusive = 0 # Refuse to start the service if its cgroup is not empty
var service_cgroup_wait = 0 # Wait on all the members of the cgroup to exit when stopping the service.
2018-03-05 00:35:29 +03:00
var service_cgroup_strict = 1 # Enable checking if the main service PID is in the correct cgroup before doing anythin with the service
var service_cgroup_kill = 0 # Kill the entire cgroup when stopping the service.
var service_cgroup_kill_signal = 15 # The signal to send to the stray cgroup members.
2018-03-05 01:13:55 +03:00
var service_cgroup_cleanup = 0 # Clean up the cgroup when the main PID exits. Uses service_cgroup_kill_signal.
2018-03-04 16:55:53 +03:00
var service_success_exit = 0 # Array, takes exit codes that are to be treated as successful termination.
2018-03-04 19:26:40 +03:00
var service_pidfile_timeout = 15 # How long to wait for unmanaged services to create their pidfiles.
2018-03-05 02:52:55 +03:00
var service_pidfile_remove_stale = 1 # Remove stale pidfiles from unmanaged services.
# Global config
2018-01-08 10:33:06 +03:00
var cgroups = 0 # Enable cgroup-related functions
var usrdir = '/usr/share/ssm'
# These are not
2018-03-09 02:31:14 +03:00
var service_own_pidfile = 0
2018-01-08 04:38:24 +03:00
var service_oneshot = 0
var service_running = 0
var service_enabled = 0
var service_stopped = 0
2018-03-04 16:55:53 +03:00
var service_failed = 0
2018-01-08 05:48:40 +03:00
var service_nologs = 0
2018-03-04 16:55:53 +03:00
var service_respawn_flag = 0
2018-03-04 19:26:40 +03:00
var service_respawn_force = 0
2018-01-08 14:12:34 +03:00
# These depend on who we are
if (( $UID )); then
rundir = "$XDG_RUNTIME_DIR/ssm"
logdir = "$HOME/log/ssm"
2018-01-08 14:17:02 +03:00
cgroup_home = "/sys/fs/cgroup/user/$UID/ssm"
2018-01-08 14:12:34 +03:00
cfg_file = "$XDG_CONFIG_HOME/ssm/ssm.conf"
else
rundir = '/run/ssm'
logdir = '/var/log/ssm'
cgroup_home = "/sys/fs/cgroup/ssm"
cfg_file = '/etc/ssm/ssm.conf'
fi
2018-01-08 04:38:24 +03:00
2018-01-08 04:47:42 +03:00
## Figure out our full path
case "$0" in
(/*) _self = "$0";;
(*) _self = "$PWD/$0";;
esac
2018-01-08 04:38:24 +03:00
# Warn the user of deprecated stuff.
for p in "/etc/ssm/init.d" "$XDG_CONFIG_HOME/ssm/init.d"; do
if [[ -d "$p" ]]; then
printf 'WARNING: `%s` was renamed to `%s`! Please move your scripts accordingly!\n' "$p" "${p%init.d}services" >&2
service_path += "$p"
2016-11-13 11:11:47 +03:00
fi
2018-01-08 04:38:24 +03:00
done
2016-11-13 11:11:47 +03:00
2018-01-08 14:12:34 +03:00
# Source the config
2018-01-09 23:47:50 +03:00
if cfg_file is file; then
2018-01-08 14:12:34 +03:00
source "$cfg_file" || die 37 "Failed to load config: $cfg_file"
fi
2018-01-08 04:38:24 +03:00
# Common config path
cfg_path = "$XDG_CONFIG_HOME/ssm" '/etc/ssm'
2017-03-12 04:30:36 +03:00
2018-01-08 04:38:24 +03:00
# Load custom config and functions, reversing the PATH order
for (( idx=${#cfg_path[@]}-1; idx>=0; idx-- )); do
cfg_dir = "${cfg_path[idx]}"
2017-12-08 16:49:34 +03:00
2018-01-08 04:38:24 +03:00
for f in "$cfg_dir/functions"/*; do
source "$f" || die 9 "Failed to source functions from $f"
2016-11-13 11:11:47 +03:00
done
2018-01-08 04:38:24 +03:00
done
2018-03-04 16:55:53 +03:00
# Parse arguments
while (( $# )); do
case $1 in
2018-03-05 03:42:30 +03:00
(-h|--help) # Show help
usage; exit 0;;
(-L|--list-services) # List all services
flag_list_services = 1;;
2018-03-05 04:28:35 +03:00
(--reset-exit) # Reset last exit status
flag_reset_exit = 1;;
2018-03-05 03:42:30 +03:00
(-e|--edit-service) # Edit the service file.
flag_edit_service = 1;;
2018-03-04 16:55:53 +03:00
(--) shift; break;;
(-*) printf 'Unknown key: %s\n' "$1" >&2; exit 1;;
(*) break;;
esac
shift
done
2018-01-08 04:38:24 +03:00
# Now create the needed runtime stuff
for d in "$rundir" "$logdir"; do
mkdir -p "$d" || die 3 "Failed to create runtime dir: $d"
done
2018-01-08 08:07:18 +03:00
# Common service path
service_path += "$XDG_CONFIG_HOME/ssm/services" '/etc/ssm/services' "$rundir/services" "$usrdir/services"
2018-03-05 03:42:30 +03:00
# Special actions
if flag_list_services; then
var known_services
for i in "$rundir"/*.{pid,exit,stopped,enabled}; do
i_fname="${i##*/}"
i_sname="${i_fname%.*}"
known_services u "$i_sname" || known_services += "$i_sname"
done
for s in "${known_services[@]}"; do
printf '%s: ' "$s"
ssm "$s" status
done
die 0
fi
# This script requires at least one argument
(( $# >= 1 )) || { usage; exit 2; }
2018-03-04 16:55:53 +03:00
2018-01-08 04:38:24 +03:00
# If $1 is a full path, source it.
# If not, search for it in the service path.
if [[ $1 == /* ]]; then
service_config = "$1"
else
for i in "${service_path[@]/%//$1}"; do
[[ -f "$i" ]] && {
service_config = "$i"
break
}
2016-11-13 11:11:47 +03:00
done
2018-01-08 04:38:24 +03:00
fi
# Die if there is no service config file
service_config || die 19 "Service not found: $1"
2018-03-05 03:42:30 +03:00
# Edit the service config
2018-03-05 04:28:35 +03:00
flag_edit_service && { edit; die $?; }
2018-03-05 03:42:30 +03:00
2018-01-10 11:30:02 +03:00
# Service name is the basename
service_name = "${1##*/}"
readonly service_name
2018-01-08 04:38:24 +03:00
2018-03-05 04:28:35 +03:00
# These depend on the service_name and make little sense to reconfigure.
service_ready_flag := "$rundir/$service_name.ready"
service_stopped_flag := "$rundir/$service_name.stopped"
service_exit_file := "$rundir/$service_name.exit"
service_cgroup_name := "$service_name"
service_cgroup_path := "$cgroup_home/$service_name"
2018-01-10 11:30:02 +03:00
# Get the service defaults
for p in "${cfg_path[@]/%//$service_name}"; do
[[ -f "$p" ]] && {
source "$p" || die 5 "Failed to read service defaults: $p"
}
done
2016-11-14 06:31:53 +03:00
2018-03-05 04:28:35 +03:00
# Reset the exit status
flag_reset_exit && { reset-exit; die $?; }
2018-01-10 11:30:02 +03:00
# Get the service config
source -- "$service_config" "${@:3}" || die 7 "Failed to read the service config: $service_config"
2018-01-07 08:36:39 +03:00
2018-03-05 03:42:30 +03:00
# Legacy
service_args && service_command += "${service_args[@]}"
service_type == 'oneshot' && service_oneshot = 1
service_respawn == 1 && service_respawn = always
# Set respawn flag
2018-03-04 16:55:53 +03:00
if ! service_respawn == 'no'; then
case $service_respawn in
(on-failure|on-success|always) service_respawn_flag = 1;;
(*) die 88 "Wrong value for service_respawn";;
esac
fi
2018-03-09 02:31:14 +03:00
if service_respawn_flag && service_oneshot; then
die 89 'Cowardly refusing to respawn a oneshot service'
fi
# Catch services with their own pidfile, set the appropriate flag.
service_pidfile && service_own_pidfile = 1
2017-03-12 04:30:36 +03:00
2018-01-08 04:38:24 +03:00
# Semi-hardcoded stuff
svc_pidfile = "$rundir/$service_name.svc_pid"
2016-11-13 11:11:47 +03:00
2018-01-08 04:38:24 +03:00
# Service-level defaults
service_pidfile := "$rundir/$service_name.pid"
2018-03-04 16:55:53 +03:00
service_logfile_out := "$logdir/${service_name}.log"
service_logfile_err := "$service_logfile_out"
service_success_exit := 0
2016-11-13 11:11:47 +03:00
2018-01-08 05:48:40 +03:00
# A shortcut for disabling logging
if service_nologs; then
service_logfile_out = '/dev/null'
service_logfile_err = '/dev/null'
fi
# Get the last recorded mainpid
2018-01-09 23:47:50 +03:00
if service_pidfile is file; then
2018-01-08 11:52:40 +03:00
read -r service_pid < "$service_pidfile"
fi
# Let's see if there's an svc running
if svc_pidfile is file; then
read -r svc_pid < "$svc_pidfile"
2016-11-13 16:31:17 +03:00
2018-01-08 04:38:24 +03:00
# Let's see if it's running
if nullexec kill -0 "$svc_pid"; then
2018-01-08 04:38:24 +03:00
service_running = 1
# If it's running, we know its PID probably:
if service_pid; then
if ! nullexec kill -0 "$service_pid"; then
printf 'WARNING: The recorded service main PID (%s) is not running.\n' "$service_pid" >&2
fi
else
printf 'WARNING: No service pidfile found; service PID unknown.\n' "$service_pidfile" >&2
fi
else
if nullexec kill -0 "$service_pid"; then
die 75 "ERROR: No svc active for $service_name, but its last recorded PID ($service_pidfile) is currenlty running: ${service_pid}."
fi
# Remove the stale svc pidfile
printf 'WARNING: Removing a stale svc pidfile: %s\n' "$svc_pidfile"
rm -f "$svc_pidfile"
2017-11-09 04:19:26 +03:00
fi
2018-01-08 04:38:24 +03:00
fi
# Let's see if the service was deliberately stopped
2018-01-09 23:47:50 +03:00
if service_stopped_flag is file; then
2018-01-08 04:38:24 +03:00
# Ooh, it was.
service_stopped = 1
fi
2018-03-05 00:16:45 +03:00
# Check if the service has failed
if service_exit_file is file; then
read -r service_exit_last < "$service_exit_file"
2018-03-09 02:31:14 +03:00
if service_success_exit u "$service_exit_last"; then
service_oneshot && service_enabled = 1
else
2018-03-05 00:16:45 +03:00
# :(
service_failed = 1
fi
2018-03-04 16:55:53 +03:00
fi
2018-01-08 10:33:06 +03:00
# Check cgroups, if enabled
if cgroups; then
2018-03-05 01:36:40 +03:00
cgroup_get_procs
2018-01-08 10:33:06 +03:00
# If there's a service PID, check if it's in the service's cgroup
2018-03-05 00:35:29 +03:00
if service_cgroup_strict; then
if service_running; then
service_cgroup_procs u "$service_pid" || die 29 "Recorded service PID is not in the service's cgroup, bailing!"
fi
2018-01-08 10:33:06 +03:00
fi
fi
# Do we have such a function?
if_service_action "$2" || die 17 "Function $2 is not defined for $service_name."
2018-01-08 04:38:24 +03:00
# cd into the workdir, if defined.
service_workdir && {
cd "$service_workdir" || die $?
}
2017-11-09 04:19:26 +03:00
2018-01-08 04:38:24 +03:00
# Run pre_$action function
run_service_action "pre_$2" || die 13 "pre_$2 failed!"
2018-01-08 04:38:24 +03:00
# Run the main action
run_service_action "$2"; res=$?
2018-01-08 04:38:24 +03:00
case "$2" in
stop)
result "$res" \
0 "Stopped $service_name" \
3 "$service_name is not running" \
2018-03-09 02:31:14 +03:00
5 "Operation timed out" \
7 "Can't “stop” a oneshot service. Use reset-exit if you want to “start” the service again"
2018-01-08 04:38:24 +03:00
;;
start)
result "$res" \
0 "Started $service_name" \
3 "$service_name is already running" \
5 "Readyness check for $service_name timed out" \
7 "Failed to start dependencies for $service_name: ${failed_deps[@]}" \
9 "service_command does not exist: ${service_command[0]}" \
2018-01-08 10:33:06 +03:00
13 "Failed to create temporary files for $service_name" \
2018-03-09 02:31:14 +03:00
15 "Refusing to start $service_name: the service cgroup is not empty and \$service_cgroup_exclusive is set" \
17 "$service_name failed"
2018-01-08 04:38:24 +03:00
;;
reload)
result "$res" \
0 "Reloading $service_name"
2018-01-08 04:38:24 +03:00
;;
status)
2018-03-05 03:42:30 +03:00
result "$res" \
0 "$service_name is running" \
2 "$service_name was successful" \
1 "$service_name is not running" \
7 "$service_name was stopped" \
9 "$service_name has failed with code: $service_exit_last" \
2018-03-09 03:25:49 +03:00
11 "$service_name has exited with code: $service_exit_last" \
13 "$service_name is not enabled"
2018-01-08 04:38:24 +03:00
;;
esac
(( res )) && exit "$res"
2016-11-13 11:11:47 +03:00
2018-01-08 04:38:24 +03:00
# Run post_$action function
run_service_action "post_$2" || die 15 "post_$2 has failed!"