2 namespace eval subproc {
4 #----- general purpose subprocess handling -----
6 # One useful procedure:
7 # subprocess::fork ONDEATH INCHILD
8 # forks, evaluates INCHILD in the calling context but in the child
9 # and when the child dies evaluates [concat [list ONDEATH] W2 W3]
10 # where W2 and W3 are the 2nd and 3rd elements of the list returned
13 # INCHILD should not return; if it does or if it gets an error, the
14 # result is that the child gets a SIGKILL.
18 proc fork {ondeath inchild} {
20 global errorCode errorInfo
21 foreach f {stdout stderr} {
22 if {[catch { flush $f } emsg]} {
23 catch { bgerror $emsg }
31 puts stderr "CHILD ERROR $emsg\n$errorCode\n$errorInfo\n"
33 kill KILL [id process]
35 set children($pid) $ondeath
39 proc chld-handler {} {
42 if {[catch { set got [wait -nohang] }]} break
43 if {![llength $got]} break
44 manyset $got pid how how2
45 if {[info exists children($pid)]} {
49 uplevel #0 [concat [list $l] $how $how2]
51 catch { bgerror $emsg }
57 signal -restart trap CHLD { after idle subproc::chld-handler }