Improve signal handling.

This commit is contained in:
Qi Xiao 2016-02-09 18:38:26 +01:00
parent 307f7fe6f0
commit 9bb9afb251
2 changed files with 27 additions and 16 deletions

View File

@ -4,7 +4,6 @@ package edit
import (
"fmt"
"os"
"os/signal"
"strings"
"syscall"
@ -376,22 +375,10 @@ MainLoop:
goto MainLoop
case syscall.SIGWINCH:
continue MainLoop
case syscall.SIGQUIT:
// Emulate default behavior
print(sys.DumpStack())
os.Exit(1)
case syscall.SIGCHLD:
// ignore
default:
// Other signals: turn off signal catching, and resend
signal.Stop(ed.sigs)
p, err := os.FindProcess(os.Getpid())
if err != nil {
panic(err)
}
err = p.Signal(sig)
if err != nil {
panic(err)
}
signal.Notify(ed.sigs)
ed.pushTip(fmt.Sprintf("ignored signal %s", sig))
}
case or := <-ones:
// Alert about error

24
main.go
View File

@ -14,6 +14,7 @@ import (
"github.com/elves/elvish/edit"
"github.com/elves/elvish/errutil"
"github.com/elves/elvish/eval"
"github.com/elves/elvish/logutil"
"github.com/elves/elvish/osutil"
"github.com/elves/elvish/parse"
"github.com/elves/elvish/store"
@ -26,6 +27,8 @@ const (
outChanLeader = "▶ "
)
var Logger = logutil.Discard
func usage() {
fmt.Println("usage: elvish [flags] [script]")
fmt.Println("flags:")
@ -55,10 +58,13 @@ func main() {
}
defer f.Close()
Logger = log.New(f, "[main]", log.LstdFlags)
eval.Logger = log.New(f, "[eval] ", log.LstdFlags)
edit.Logger = log.New(f, "[edit] ", log.LstdFlags)
}
go logSignals()
args := flag.Args()
switch len(args) {
case 0:
@ -106,6 +112,8 @@ func interact() {
}
rpromptStr := username + "@" + hostname
go dumpstackOnQuit()
sigch := make(chan os.Signal, sigchSize)
signal.Notify(sigch)
@ -142,6 +150,22 @@ func interact() {
}
}
func logSignals() {
sigs := make(chan os.Signal)
signal.Notify(sigs)
for sig := range sigs {
Logger.Println("signal", sig)
}
}
func dumpstackOnQuit() {
quitSigs := make(chan os.Signal)
signal.Notify(quitSigs, syscall.SIGQUIT)
for range quitSigs {
fmt.Print(sys.DumpStack())
}
}
func script(fname string) {
ev, _ := newEvalerAndStore()
err := ev.Source(fname)