Generalized signal handling

This commit is contained in:
2024-05-14 21:56:04 +02:00
parent 9a1f419d87
commit 5b4ff9f1c4
3 changed files with 38 additions and 20 deletions
+2 -3
View File
@@ -12,6 +12,8 @@ RUN apt-get install -y wget
RUN apt-get install -y git make gcc
RUN apt-get install -y icewm
RUN apt-get install -y x11-apps
RUN apt-get install -y bc
RUN apt-get install -y net-tools socat python3-numpy
FROM base as build
ADD https://github.com/TurboVNC/turbovnc/releases/download/3.1.1/turbovnc_3.1.1_amd64.deb /tmp/
@@ -24,9 +26,6 @@ RUN dpkg -i turbovnc_3.1.1_amd64.deb
WORKDIR "/tmp/websockify"
RUN make -j4
# develop only
RUN apt-get install -y net-tools socat python3-numpy
ENV PATH=$PATH:/opt/TurboVNC/bin
RUN mkdir ~/.vnc/
RUN echo $PASSWORD | vncpasswd -f > ~/.vnc/passwd
+21 -6
View File
@@ -1,18 +1,33 @@
#!/usr/bin/env bash
set -x
# Signal helper
source /app/signals.sh
signals_trap_with_arg func_trap TERM
PID=0
TERM_handler() {
SIGNAME="$1"
echo "Killing ${PID}"
echo "Signal ${SIGNAME}"
if [ ${PID} -ne 0 ]; then
ps -ef --forest
CHILDS=$(pgrep -P "${PID}")
kill -"${SIGNAME}" -"${PID}"
for i in $CHILDS; do
wait $i
done
fi
ps -ef --forest
ret=$(signal_return_val ${SIGNAME})
exit $ret
}
signals_trap_with_arg TERM_handler SIGTERM
/app/server.sh &
PID=-1
PID=$$
# wait forever
while true
do
tail -f /dev/null & wait ${!}
done
signals_wait_forever
# https://docs.docker.com/compose/faq/
# https://medium.com/@gchudnov/trapping-signals-in-docker-containers-7a57fdda7d86
+15 -11
View File
@@ -1,5 +1,3 @@
PID=0
signals_trap_with_arg() {
func="$1" ; shift
for sig ; do
@@ -8,14 +6,20 @@ signals_trap_with_arg() {
done
}
func_trap() {
echo "Trapped: $1"
echo "Killing ${PID}"
if [ ${PID} -ne 0 ]; then
kill -SIGTERM "${PID}"
wait "${PID}"
fi
ps -ef --forest
signals_wait_forever() {
while true
do
tail -f /dev/null & wait ${!}
done
}
exit 143
signal_name_to_number() {
SIGNAME="$1"
trap -l | sed -nr "s/.*\b([0-9]+)\) $SIGNAME.*/\1/p"
}
signal_return_val() {
SIGNAME="$1"
SIGNUM=$(signal_name_to_number ${SIGNAME})
echo "128 + $SIGNUM" | bc
}