From 11228033119df6a25da0602de996bb0426966f78 Mon Sep 17 00:00:00 2001 From: Noah Pederson Date: Mon, 22 Jun 2026 10:23:18 -0500 Subject: [PATCH] Avoid shell wrapper for signal forwarding --- diag => disag | 94 +++++++++++++++++++++++++++++---------------------- 1 file changed, 53 insertions(+), 41 deletions(-) rename diag => disag (77%) diff --git a/diag b/disag similarity index 77% rename from diag rename to disag index c574579..072b752 100755 --- a/diag +++ b/disag @@ -33,7 +33,7 @@ Notes: Display prefixes and ANSI colors are never written to log files. Colors are used only when stdout is a terminal. diag uses anonymous rc pipes internally and only writes paths named with -f. - Signal forwarding uses sh and setsid so child command groups receive signals. + Signals are forwarded to child processes with kill. Examples: diag 'echo hello' @@ -64,9 +64,51 @@ fn runone { if(! ~ $file '') > $file + fn stoprunone { + sig=$1 + for(pid in $runpids) + kill -^$sig -^$pid >[2]/dev/null + for(pid in $runpids) + kill -^$sig $pid >[2]/dev/null + wait + exit sig^$sig + } + + fn sigint { + stoprunone INT + } + + fn sigterm { + stoprunone TERM + } + + fn sigquit { + stoprunone QUIT + } + + fn sighup { + stoprunone HUP + } + { - rc -c $cmd >[2=3] | - awk -v logfile=$file -v name=$name -v color=$color ' + { + rc -c $cmd >[2=3] | + awk -v logfile=$file -v name=$name -v color=$color ' + { + if(logfile != "") { + print $0 >> logfile + fflush(logfile) + } + line = $0 + if(name != "") + line = "[" name "] " line + if(color != "") + line = sprintf("%c[%sm%s%c[0m", 27, color, line, 27) + print line + fflush() + } + ' + } |[3] awk -v logfile=$file -v name=$name -v color=$color ' { if(logfile != "") { print $0 >> logfile @@ -77,25 +119,13 @@ fn runone { line = "[" name "] " line if(color != "") line = sprintf("%c[%sm%s%c[0m", 27, color, line, 27) - print line - fflush() + print line > "/dev/stderr" + fflush("/dev/stderr") } ' - } |[3] awk -v logfile=$file -v name=$name -v color=$color ' - { - if(logfile != "") { - print $0 >> logfile - fflush(logfile) - } - line = $0 - if(name != "") - line = "[" name "] " line - if(color != "") - line = sprintf("%c[%sm%s%c[0m", 27, color, line, 27) - print line > "/dev/stderr" - fflush("/dev/stderr") - } - ' + } & + runpids=$apid + wait $runpids } if(! ~ $#* 0 && ~ $1 --diag-run-one) { @@ -106,6 +136,8 @@ if(! ~ $#* 0 && ~ $1 --diag-run-one) { fn stopall { sig=$1 + for(pid in $apids) + kill -^$sig -^$pid >[2]/dev/null for(pid in $apids) kill -^$sig $pid >[2]/dev/null wait @@ -156,27 +188,7 @@ fn launch { color=$colors($color_index) } - sh -c ' - child= - forward() { - sig=$1 - code=$2 - if test -n "$child" - then - kill -$sig -$child 2>/dev/null - wait "$child" - fi - exit "$code" - } - trap "forward INT 130" INT - trap "forward TERM 143" TERM - trap "forward QUIT 131" QUIT - trap "forward HUP 129" HUP - setsid -w "$@" & - child=$! - wait "$child" - exit $? - ' sh $prog --diag-run-one $cmd $file $name $color & + $prog --diag-run-one $cmd $file $name $color & writers=($writers $apid) } -- 2.51.2