From 9bb9afb251f24f2d1ba94cbf4100e4cc198beed6 Mon Sep 17 00:00:00 2001 From: Qi Xiao Date: Tue, 9 Feb 2016 18:38:26 +0100 Subject: [PATCH] Improve signal handling. --- edit/editor.go | 19 +++---------------- main.go | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/edit/editor.go b/edit/editor.go index 7e75be1d..373b5f23 100644 --- a/edit/editor.go +++ b/edit/editor.go @@ -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 diff --git a/main.go b/main.go index dbc3300d..cab7262d 100644 --- a/main.go +++ b/main.go @@ -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)