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