From e4e91bc8d5f3c5d9bba7150f27b5e2d97fef476a Mon Sep 17 00:00:00 2001 From: Daisuke Maki Date: Mon, 5 Jan 2015 08:35:39 +0900 Subject: [PATCH] Add status file support, rework signals --- starter.go | 81 +++++++++++++++++++++++++--------------------- starter_any.go | 30 +++++++++++++++++ starter_windows.go | 12 +++++++ 3 files changed, 86 insertions(+), 37 deletions(-) create mode 100644 starter_any.go create mode 100644 starter_windows.go diff --git a/starter.go b/starter.go index 1bd5f09..9761544 100644 --- a/starter.go +++ b/starter.go @@ -14,43 +14,36 @@ import ( var niceSigNames map[syscall.Signal]string var niceNameToSigs map[string]syscall.Signal +var successStatus syscall.WaitStatus +var failureStatus syscall.WaitStatus -func init() { - niceSigNames = map[syscall.Signal]string{ +func makeNiceSigNamesCommon() map[syscall.Signal]string { + return map[syscall.Signal]string{ syscall.SIGABRT: "ABRT", syscall.SIGALRM: "ALRM", syscall.SIGBUS: "BUS", - syscall.SIGCHLD: "CHLD", - syscall.SIGCONT: "CONT", - syscall.SIGEMT: "EMT", - syscall.SIGFPE: "FPE", - syscall.SIGHUP: "HUP", - syscall.SIGILL: "ILL", - syscall.SIGINFO: "INFO", - syscall.SIGINT: "INT", - syscall.SIGIO: "IO", - // syscall.SIGIOT: "IOT", + // syscall.SIGEMT: "EMT", + syscall.SIGFPE: "FPE", + syscall.SIGHUP: "HUP", + syscall.SIGILL: "ILL", + // syscall.SIGINFO: "INFO", + syscall.SIGINT: "INT", + // syscall.SIGIOT: "IOT", syscall.SIGKILL: "KILL", syscall.SIGPIPE: "PIPE", - syscall.SIGPROF: "PROF", syscall.SIGQUIT: "QUIT", syscall.SIGSEGV: "SEGV", - syscall.SIGSTOP: "STOP", - syscall.SIGSYS: "SYS", syscall.SIGTERM: "TERM", syscall.SIGTRAP: "TRAP", - syscall.SIGTSTP: "TSTP", - syscall.SIGTTIN: "TTIN", - syscall.SIGTTOU: "TTOU", - syscall.SIGURG: "URG", - syscall.SIGUSR1: "USR1", - syscall.SIGUSR2: "USR2", - syscall.SIGVTALRM: "VTALRM", - syscall.SIGWINCH: "WINCH", - syscall.SIGXCPU: "XCPU", - syscall.SIGXFSZ: "GXFSZ", } +} + +func makeNiceSigNames() map[syscall.Signal]string { + return addPlatformDependentNiceSigNames(makeNiceSigNamesCommon()) +} +func init() { + niceSigNames = makeNiceSigNames() niceNameToSigs := make(map[string]syscall.Signal) for sig, name := range niceSigNames { niceNameToSigs[name] = sig @@ -122,16 +115,6 @@ func NewStarter(c Config) (*Starter, error) { return s, nil } -func (s *Starter) Close() { - if s.statusFile != "" { - os.Remove(s.statusFile) - } - - if s.pidFile != "" { - os.Remove(s.pidFile) - } -} - func (s Starter) Stop() { p, _ := os.FindProcess(os.Getpid()) p.Signal(syscall.SIGTERM) @@ -143,7 +126,7 @@ func grabExitStatus(st processState) syscall.WaitStatus { exitSt, ok := st.Sys().(syscall.WaitStatus) if !ok { fmt.Fprintf(os.Stderr, "Oh no, you are running on a platform where ProcessState.Sys().(syscall.WaitStatus) doesn't work! We're doomed! Temporarily setting status to 255. Please contact the author about this\n") - exitSt = syscall.WaitStatus(255) + exitSt = failureStatus } return exitSt } @@ -245,6 +228,26 @@ func (s *Starter) Run() error { var sigReceived os.Signal var sigToSend os.Signal + statusCh := make(chan map[int]int) + go func(fn string, ch chan map[int]int) { + for wmap := range ch { + if fn == "" { + continue + } + + f, err := os.OpenFile(fn, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644) + if err != nil { + continue + } + + for gen, pid := range wmap { + fmt.Fprintf(f, "%d:%d\n", gen, pid) + } + + f.Close() + } + }(s.statusFile, statusCh) + defer func() { if p != nil { oldWorkers[p.Pid] = s.generation @@ -467,7 +470,7 @@ func (s *Starter) StartWorker(sigCh chan os.Signal, ch chan processState) *os.Pr if err != nil { ch <- err.(*exec.ExitError).ProcessState } else { - ch <- &dummyProcessState{pid: pid, status: 0} + ch <- &dummyProcessState{pid: pid, status: successStatus} } }() // Bail out @@ -494,6 +497,10 @@ func (s *Starter) Teardown() error { os.Remove(s.pidFile) } + if s.statusFile != "" { + os.Remove(s.statusFile) + } + for _, l := range s.listeners { if l == nil { continue diff --git a/starter_any.go b/starter_any.go new file mode 100644 index 0000000..1b2288a --- /dev/null +++ b/starter_any.go @@ -0,0 +1,30 @@ +// +build !windows + +package starter + +import "syscall" + +func init() { + failureStatus = syscall.WaitStatus(255) + successStatus = syscall.WaitStatus(0) +} + +func addPlatformDependentNiceSigNames(v map[syscall.Signal]string) map[syscall.Signal]string { + v[syscall.SIGCHLD] = "CHLD" + v[syscall.SIGCONT] = "CONT" + v[syscall.SIGIO] = "IO" + v[syscall.SIGPROF] = "PROF" + v[syscall.SIGSTOP] = "STOP" + v[syscall.SIGSYS] = "SYS" + v[syscall.SIGTSTP] = "TSTP" + v[syscall.SIGTTIN] = "TTIN" + v[syscall.SIGTTOU] = "TTOU" + v[syscall.SIGURG] = "URG" + v[syscall.SIGUSR1] = "USR1" + v[syscall.SIGUSR2] = "USR2" + v[syscall.SIGVTALRM] = "VTALRM" + v[syscall.SIGWINCH] = "WINCH" + v[syscall.SIGXCPU] = "XCPU" + v[syscall.SIGXFSZ] = "GXFSZ" + return v +} \ No newline at end of file diff --git a/starter_windows.go b/starter_windows.go new file mode 100644 index 0000000..49c6ef7 --- /dev/null +++ b/starter_windows.go @@ -0,0 +1,12 @@ +package starter + +import "syscall" + +func init() { + failureStatus = syscall.WaitStatus{ExitCode: 255} + successStatus = syscall.WaitStatus{ExitCode: 0} +} + +func addPlatformDependentNiceSigNames(v map[syscall.Signal]string) map[syscall.Signal]string { + return v +}