From 080174df47652f66e68eb47e8ab46c5ade62708e Mon Sep 17 00:00:00 2001 From: David Dollar Date: Thu, 29 Aug 2013 19:29:18 -0400 Subject: [PATCH] work on darwin --- Makefile | 8 ++++---- darwin.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 darwin.go diff --git a/Makefile b/Makefile index 7189cf6..f883eb5 100644 --- a/Makefile +++ b/Makefile @@ -17,13 +17,13 @@ lint: $(SRC) release: forego-linux forego-osx forego-windows forego-linux: $(SRC) - env GOOS=linux GOARCH=386 go build -o $@ + env GOOS=linux GOARCH=386 go build -o forego-osx: $(SRC) - env GOOS=darwin GOARCH=386 go build -o $@ + env GOOS=darwin GOARCH=386 go build -o forego-windows: $(SRC) - env GOOS=windows GOARCH=386 go build -o $@ + env GOOS=windows GOARCH=386 go build -o $(BIN): $(SRC) - go build -o $@ $(SRC) + go build -o $@ diff --git a/darwin.go b/darwin.go new file mode 100644 index 0000000..f8d87df --- /dev/null +++ b/darwin.go @@ -0,0 +1,44 @@ +package main + +import ( + "fmt" + "os" + "os/exec" + "path/filepath" + "syscall" + "time" +) + +func (p *Process) Start() { + command := []string{"/bin/bash", "-c", fmt.Sprintf("source \"%s\" 2>/dev/null; %s", filepath.Join(p.Root, ".profile"), p.Command)} + p.cmd = exec.Command(command[0], command[1:]...) + p.cmd.Dir = p.Root + p.cmd.Env = p.envAsArray() + p.cmd.Stdin = p.Stdin + p.cmd.Stdout = p.Stdout + p.cmd.Stderr = p.Stderr + p.cmd.SysProcAttr = &syscall.SysProcAttr{} + p.cmd.SysProcAttr.Setsid = true + p.cmd.Start() +} + +func (p *Process) Signal(signal syscall.Signal) { + group, _ := os.FindProcess(-1 * p.cmd.Process.Pid) + group.Signal(signal) +} + +func ShutdownProcesses(of *OutletFactory) { + shutdown_mutex.Lock() + of.SystemOutput("shutting down") + for name, ps := range processes { + of.SystemOutput(fmt.Sprintf("sending SIGTERM to %s", name)) + ps.Signal(syscall.SIGTERM) + } + go func() { + time.Sleep(shutdownGraceTime) + for name, ps := range processes { + of.SystemOutput(fmt.Sprintf("sending SIGKILL to %s", name)) + ps.Signal(syscall.SIGKILL) + } + }() +}