Skip to content

Commit

Permalink
Add status file support, rework signals
Browse files Browse the repository at this point in the history
  • Loading branch information
lestrrat committed Jan 4, 2015
1 parent fd1b1d3 commit e4e91bc
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 37 deletions.
81 changes: 44 additions & 37 deletions starter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
30 changes: 30 additions & 0 deletions starter_any.go
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 12 additions & 0 deletions starter_windows.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit e4e91bc

Please sign in to comment.