Files
makronix/mobile/userland/kill/test.sh
T
lotherkandClaude Sonnet 5.5 73c9de51a6 licence: the CNCL line in every file Makronix wrote (tools, docs, www, mobile, top level)
consmirror.c: the BSD notice is replaced (Makronix's own file).  Files with
changes of other sessions pending are left for their commit.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-14 11:50:26 +00:00

95 lines
3.1 KiB
Bash

#!/bin/sh
# License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
#
# Test of the kill port. src/tests/bin/kill does not exist in our tree, so
# there is nothing from atf to translate; every case below is a sentence of
# kill(1) (src/bin/kill/kill.1) turned into a check.
#
# Usage: test.sh KILL DATADIR
# KILL the command that runs the port (a path, or "qemu-aarch64 -L
# <sysroot> <path>"; tools/userland-test.sh passes it)
# DATADIR unused: kill(1) has no test data (src/tests/bin/kill is absent)
# Prints one line per case and exits non-zero if one failed.
[ $# -eq 2 ] || { echo "usage: $0 KILL DATADIR" >&2; exit 2; }
kill=$1
fails=0
t=${TMPDIR:-/tmp}/kill-test.$$
mkdir -p "$t" || exit 1
trap 'rm -rf "$t"' EXIT
ok() { echo "PASS $1"; }
no() { echo "FAIL $1"; fails=$((fails + 1)); }
# is NAME EXPECTED OUTPUT: the output must be exactly one line, EXPECTED
is() {
if [ "$2" = "$3" ]; then ok "$1"; else no "$1"; fi
}
# "-l exit_status Display the name of the signal corresponding to
# exit_status." A signal number, and (same sentence) "the exit status of a
# command killed by a signal": 128 + the number.
is l-number KILL "$($kill -l 9 2>&1)"
is l-exit-status TERM "$($kill -l 143 2>&1)"
is l-exit-status-2 KILL "$($kill -l 137 2>&1)"
# "If no operand is given, display the names of all the signals." HUP, INT,
# KILL and TERM are among "the more commonly used signals" of the man page.
all=$($kill -l 2>&1)
if echo "$all" | grep -qw HUP && echo "$all" | grep -qw INT &&
echo "$all" | grep -qw KILL && echo "$all" | grep -qw TERM; then
ok l-all
else
no l-all
fi
# "0 (does not affect the process; can be used to test whether the process
# exists)" together with the default signal, TERM ("A symbolic signal name
# specifying the signal to be sent instead of the default TERM").
sleep 30 &
pid=$!
if $kill -0 "$pid" 2> "$t/err"; then ok zero-exists; else no zero-exists; fi
# "-signal_name A symbolic signal name specifying the signal to be sent";
# a process killed by SIGHUP (1) has the exit status 128+1 to its parent.
$kill -HUP "$pid" 2> "$t/err"
wait "$pid" 2>/dev/null
is signal-name 129 "$?"
# ... and the same through -s, with SIGTERM (15): 128+15.
sleep 30 &
pid=$!
$kill -s TERM "$pid" 2> "$t/err"
wait "$pid" 2>/dev/null
is s-term 143 "$?"
# The process is gone now: -0 must say so, with a message.
if $kill -0 "$pid" 2> "$t/err"; then
no zero-gone
elif [ -s "$t/err" ]; then
ok zero-gone
else
no zero-gone
fi
# "-signal_number A non-negative decimal integer, specifying the signal to
# be sent instead of the default TERM": 9 is KILL, so the exit status is
# 128+9. (Not SIGINT: an asynchronous command of a non-interactive shell
# starts with SIGINT ignored, so it would survive the signal.)
sleep 30 &
pid=$!
$kill -9 "$pid" 2> "$t/err"
wait "$pid" 2>/dev/null
is signal-number 137 "$?"
# A signal name that does not exist is an error, not a silent success.
if $kill -s NOSUCHSIGNAL "$$" 2> "$t/err"; then
no unknown-signal
elif [ -s "$t/err" ]; then
ok unknown-signal
else
no unknown-signal
fi
echo "kill: $fails failed"
[ "$fails" -eq 0 ]