Only use stub when it is alive.

This commit is contained in:
Qi Xiao 2016-02-23 12:32:05 +01:00
parent c67ad408ee
commit 6a80c3dcef
4 changed files with 16 additions and 2 deletions

View File

@ -180,7 +180,7 @@ func (ev *Evaler) EvalInteractive(text string, n *parse.Chunk) error {
signal.Ignore(syscall.SIGTTIN) signal.Ignore(syscall.SIGTTIN)
signal.Ignore(syscall.SIGTTOU) signal.Ignore(syscall.SIGTTOU)
// XXX Should use fd of /dev/terminal instead of 0. // XXX Should use fd of /dev/terminal instead of 0.
if ev.Stub != nil && sys.IsATTY(0) { if ev.Stub != nil && ev.Stub.Alive() && sys.IsATTY(0) {
ev.Stub.SetTitle(summarize(text)) ev.Stub.SetTitle(summarize(text))
dir, err := os.Getwd() dir, err := os.Getwd()
if err != nil { if err != nil {

View File

@ -59,7 +59,7 @@ func (e ExternalCmd) Call(ec *EvalCtx, argVals []Value) {
} }
sys := syscall.SysProcAttr{} sys := syscall.SysProcAttr{}
if ec.Stub != nil { if ec.Stub != nil && ec.Stub.Alive() {
sys.Setpgid = true sys.Setpgid = true
sys.Pgid = ec.Stub.Process().Pid sys.Pgid = ec.Stub.Process().Pid
} }

View File

@ -76,6 +76,10 @@ func Main() {
fmt.Println("failed to spawn stub:", err) fmt.Println("failed to spawn stub:", err)
} else { } else {
ev.Stub = stub ev.Stub = stub
go func() {
<-stub.State()
fmt.Println("stub has died")
}()
} }
if len(args) == 1 { if len(args) == 1 {

View File

@ -114,6 +114,16 @@ func (stub *Stub) State() <-chan struct{} {
return stub.statech return stub.statech
} }
// Alive reports whether the stub is alive.
func (stub *Stub) Alive() bool {
select {
case <-stub.statech:
return false
default:
return true
}
}
// relaySignals relays output of the stub to sigch, assuming that outputs // relaySignals relays output of the stub to sigch, assuming that outputs
// represent signal numbers. // represent signal numbers.
func relaySignals(reader io.Reader, sigch chan<- os.Signal) { func relaySignals(reader io.Reader, sigch chan<- os.Signal) {