| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
# This file is part of util-linux. |
| 4 |
# |
| 5 |
# This file is free software; you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation; either version 2 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# This file is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
|
| 15 |
TS_TOPDIR="${0%/*}/../.." |
| 16 |
TS_DESC="decode functions" |
| 17 |
|
| 18 |
. "$TS_TOPDIR/functions.sh" |
| 19 |
ts_init "$*" |
| 20 |
|
| 21 |
# make sure we do not use shell built-in command |
| 22 |
if [ "$TS_USE_SYSTEM_COMMANDS" == "yes" ]; then |
| 23 |
TS_CMD_KILL="$(which kill)" |
| 24 |
fi |
| 25 |
|
| 26 |
ts_skip_qemu_user |
| 27 |
|
| 28 |
ts_check_test_command "$TS_CMD_KILL" |
| 29 |
ts_check_test_command "$TS_HELPER_SIGSTATE" |
| 30 |
|
| 31 |
ts_check_prog "sed" |
| 32 |
ts_check_prog "sort" |
| 33 |
ts_check_prog "tr" |
| 34 |
|
| 35 |
. "$TS_SELF/kill_functions.sh" |
| 36 |
|
| 37 |
decode() |
| 38 |
{ |
| 39 |
"$TS_CMD_KILL" -l "$1" |
| 40 |
} |
| 41 |
|
| 42 |
signame2num() |
| 43 |
{ |
| 44 |
local name=$1 |
| 45 |
local n s |
| 46 |
|
| 47 |
"$TS_CMD_KILL" -L | while read n s; do |
| 48 |
if [[ "$s" == "$name" ]]; then |
| 49 |
echo "$n" |
| 50 |
return 0 |
| 51 |
fi |
| 52 |
done |
| 53 |
|
| 54 |
return 1 |
| 55 |
} |
| 56 |
|
| 57 |
encode() |
| 58 |
{ |
| 59 |
local s |
| 60 |
local n |
| 61 |
local -i bits=0 |
| 62 |
|
| 63 |
for s in "$@"; do |
| 64 |
n=$(signame2num "$s") |
| 65 |
if [[ -z "$n" ]]; then |
| 66 |
ts_log_both "failed to encode the signal name: $s" |
| 67 |
# The test may fail. |
| 68 |
continue |
| 69 |
fi |
| 70 |
|
| 71 |
(( bits |= 1 << (n - 1) )) |
| 72 |
done |
| 73 |
|
| 74 |
printf "0x%0.16x\n" "$bits" |
| 75 |
} |
| 76 |
|
| 77 |
sort_signals_by_name() |
| 78 |
{ |
| 79 |
local field |
| 80 |
local siglist |
| 81 |
|
| 82 |
while IFS=: read field siglist; do |
| 83 |
echo "${field}:" |
| 84 |
echo $siglist | tr ' ' '\n' | sort |
| 85 |
done |
| 86 |
} |
| 87 |
|
| 88 |
PID= |
| 89 |
ACK= |
| 90 |
{ |
| 91 |
for siglist in "INT ILL ABRT FPE SEGV TERM" \ |
| 92 |
"HUP QUIT TRAP PIPE ALRM" \ |
| 93 |
"USR1"; do |
| 94 |
printf 'encode-decode round-trip "%s":\n' "${siglist}" |
| 95 |
d=$(encode ${siglist}) |
| 96 |
decode "$d" |
| 97 |
done |
| 98 |
|
| 99 |
coproc SIGSTATE { "$TS_HELPER_SIGSTATE" ; } |
| 100 |
if read -u ${SIGSTATE[0]} PID; then |
| 101 |
"$TS_CMD_KILL" -USR1 "$PID" |
| 102 |
if read -u ${SIGSTATE[0]} ACK; then |
| 103 |
# The target process is in its signal handler for USR1. |
| 104 |
# Sending one more USR1 is for making the signal pending state. |
| 105 |
"$TS_CMD_KILL" -USR1 "$PID" |
| 106 |
"$TS_CMD_KILL" -d "$PID" | { |
| 107 |
SIGRTMIN=$("$TS_CMD_KILL" -L | grep -o '[0-9]\+ RTMIN' | cut -d " " -f 1) |
| 108 |
if [[ $("$TS_CMD_KILL" --list=$SIGRTMIN) == RT0 ]]; then |
| 109 |
# See man signal(7). |
| 110 |
# The Linux kernel supports a range of 33 different real-time signals, |
| 111 |
# numbered 32 to 64. However, the glibc POSIX threads implementation in‐ |
| 112 |
# ternally uses two (for NPTL) or three (for LinuxThreads) real-time sig‐ |
| 113 |
# nals (see pthreads(7)), and adjusts the value of SIGRTMIN suitably (to |
| 114 |
# 34 or 35). |
| 115 |
sed_cmd="sed" |
| 116 |
for ((i=32; i<=SIGRTMIN; i++)); do |
| 117 |
sed_cmd+=" -e s/' $i'//" |
| 118 |
done |
| 119 |
eval $sed_cmd |
| 120 |
else |
| 121 |
cat |
| 122 |
fi |
| 123 |
} | sort_signals_by_name |
| 124 |
fi |
| 125 |
echo DONE >&"${SIGSTATE[1]}" |
| 126 |
fi |
| 127 |
wait ${SIGSTATE_PID} |
| 128 |
} > "$TS_OUTPUT" 2>&1 |
| 129 |
|
| 130 |
ts_finalize |