From 46c7d448f27e8fc213c50cd241914b2d94e2e308 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Mon, 10 Aug 2020 20:47:11 +0300 Subject: Fix bash coprocess usage races --- libbutl/utility.bash.in | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'libbutl/utility.bash.in') diff --git a/libbutl/utility.bash.in b/libbutl/utility.bash.in index 56bd3ab..5f51a2c 100644 --- a/libbutl/utility.bash.in +++ b/libbutl/utility.bash.in @@ -23,3 +23,28 @@ function butl_path () # dirname "${BASH_SOURCE[0]}" } + +# Resume a stopped process execution by sending it the SIGCONT signal. +# +# Note that to avoid races the function waits until the process enters the +# stopped state. +# +function butl_resume_process () # +{ + local pid="$1" + + while true; do + + # Note that while the ps's -o option 'state' value is not specified by + # POSIX, it is supported by all the major implementations with the leading + # 'T' character indicating the 'stopped' process run state. + # + local s + s="$(ps -p "$pid" -o state=)" + + if [ "${s:0:1}" == "T" ]; then + kill -SIGCONT "$pid" + break + fi + done +} -- cgit v1.1