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.SIGTTOU)
// 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))
dir, err := os.Getwd()
if err != nil {

View File

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

View File

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

View File

@ -114,6 +114,16 @@ func (stub *Stub) State() <-chan struct{} {
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
// represent signal numbers.
func relaySignals(reader io.Reader, sigch chan<- os.Signal) {