91 lines
3.3 KiB
Bash
91 lines
3.3 KiB
Bash
#!/usr/bin/env bash
|
|
# ------------------------------------------------------------------------------
|
|
# Copyright (c) 2013-2026 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
|
# Protected by the BSD3 license. Please read bellow for details.
|
|
#
|
|
# * Redistribution and use in source and binary forms,
|
|
# * with or without modification, are permitted provided
|
|
# * that the following conditions are met:
|
|
# *
|
|
# * Redistributions of source code must retain the above
|
|
# * copyright notice, this list of conditions and the
|
|
# * following disclaimer.
|
|
# *
|
|
# * Redistributions in binary form must reproduce the above
|
|
# * copyright notice, this list of conditions and the following
|
|
# * disclaimer in the documentation and/or other materials
|
|
# * provided with the distribution.
|
|
# *
|
|
# * Neither the name of the copyright holder nor the names
|
|
# * of any other contributors may be used to endorse or
|
|
# * promote products derived from this software without
|
|
# * specific prior written permission.
|
|
# *
|
|
# * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|
# * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
# * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
# * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
# * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
# * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
# * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
# * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
# * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
# * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
# * OF SUCH DAMAGE.
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helpers of profile.d/processes.sh, loaded by the module itself.
|
|
|
|
# Internal helper: take the kill-style signal arguments (-9, -KILL, -SIGHUP)
|
|
# out of an argument list. getopt rejects them as unknown options, so they must
|
|
# be removed before it runs. They are recognised anywhere before a "--"
|
|
# (digits, or upper-case names, which cannot clash with the lower-case options
|
|
# of the callers); everything after "--" is left untouched.
|
|
# Usage: _proc_split_signals <signals_array> <rest_array> [args...]
|
|
_proc_split_signals()
|
|
{
|
|
local -n _pss_sigs="$1" _pss_rest="$2"
|
|
shift 2
|
|
_pss_sigs=()
|
|
_pss_rest=()
|
|
local a
|
|
while [[ $# -gt 0 ]]; do
|
|
a="$1"
|
|
shift
|
|
if [[ $a == "--" ]]; then
|
|
_pss_rest+=(-- "$@")
|
|
break
|
|
fi
|
|
if [[ $a =~ ^-([0-9]+|SIG[A-Z0-9+-]+|[A-Z][A-Z0-9+-]*)$ ]]; then
|
|
_pss_sigs+=("$a")
|
|
else
|
|
_pss_rest+=("$a")
|
|
fi
|
|
done
|
|
}
|
|
export -f _proc_split_signals
|
|
|
|
# Internal helper of kt: send a signal to a process and, first, to all its
|
|
# descendants (depth first, so a parent never outlives the children it could
|
|
# respawn from). A failure does not stop the others; the status is non-zero if
|
|
# any kill failed.
|
|
# Usage: _kt_tree <dry_run 0|1> <pid> [kill_args...]
|
|
_kt_tree()
|
|
{
|
|
local dry="$1" pid="$2" child rc=0
|
|
shift 2
|
|
for child in $(pgrep -P "$pid" 2>/dev/null); do
|
|
_kt_tree "$dry" "$child" "$@" || rc=1
|
|
done
|
|
|
|
if (( dry )); then
|
|
printf "DRY-RUN: "
|
|
printf "%q " kill "$@" "$pid"
|
|
printf "\n"
|
|
else
|
|
kill "$@" "$pid" || rc=1
|
|
fi
|
|
return "$rc"
|
|
}
|
|
export -f _kt_tree
|