commit 1d92611cc4bccbd8b5c19a596ab950e31be44c91
parent bb6dd44d8638ccba315973f2c6c66262ef72f1d2
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Fri, 5 Oct 2018 23:55:40 +0200
sfeed_update: handle signals consistently in different shells
- Handle SIGTERM properly, don't leave stray processes. Kill them on both
SIGTERM and SIGINT.
- When a "batch" of feeds was interrupted, don't allow to wait again.
- Simplify and create sighandler function.
- Now on both SIGTERM and SIGINT the cleanup() handler is called to not leave
stray files.
Tested with ksh, dash, bash, zsh.
Diffstat:
1 file changed, 16 insertions(+), 11 deletions(-)
diff --git a/sfeed_update b/sfeed_update
@@ -77,8 +77,9 @@ fetchfeed() {
feed() {
# wait until ${maxjobs} are finished: throughput using this logic is
# non-optimal, but it is simple and portable.
+ [ ${signo} -ne 0 ] && return
[ $((curjobs % maxjobs)) -eq 0 ] && wait
- [ ${isinterrupted} -eq 1 ] && return
+ [ ${signo} -ne 0 ] && return
curjobs=$((curjobs + 1))
(name="$1"
@@ -123,8 +124,12 @@ cleanup() {
rm -rf "${sfeedtmpdir}"
}
-interrupted() {
- isinterrupted=1
+sighandler() {
+ signo="$1"
+ # ignore TERM signal for myself.
+ trap -- "" TERM
+ # kill all running childs >:D
+ kill -TERM -$$
}
feeds() {
@@ -134,12 +139,12 @@ feeds() {
# job counter.
curjobs=0
-# kill whole current process group on ^C (SIGINT).
-isinterrupted=0
+# signal number received for parent.
+signo=0
+# SIGINT: signal to interrupt parent.
+trap -- "sighandler 2" "INT"
# SIGTERM: signal to terminate parent.
-trap -- "interrupted" "TERM"
-# SIGINT: kill all running childs >:D
-trap -- "kill -TERM -$$" "INT"
+trap -- "sighandler 15" "TERM"
# load config file.
loadconfig "$1"
# fetch feeds and store in temporary file.
@@ -149,9 +154,9 @@ mkdir -p "${sfeedpath}"
# fetch feeds specified in config file.
feeds
# wait till all feeds are fetched (concurrently).
-wait
+[ ${signo} -eq 0 ] && wait
# cleanup temporary files etc.
cleanup
-# on SIGINT exit with 128 + signal (SIGINT = 2).
-[ ${isinterrupted} -eq 1 ] && exit 130
+# on signal SIGINT and SIGTERM exit with signal number + 128.
+[ ${signo} -ne 0 ] && exit $((signo+128))
exit 0