From cf6a9c0a97f1270598dd2e62443794946b3d5e3b Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Wed, 4 Feb 2015 13:05:28 -0800 Subject: [PATCH] warrend init statup properly in /var/run and close and respect only one instance --- Makefile | 2 ++ cmd/warren/main.go | 1 - cmd/warrend/main.go | 71 +++++++++++++++++++++++++++++++++++++------- lib/warren/warren.go | 7 ++++- 4 files changed, 68 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index 2ab2804..2cc5bc0 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,9 @@ clean: install: cp cmd/warren/warren /usr/bin cp cmd/warrend/warrend /usr/bin + mkdir /var/run/warrend uninstall: rm /usr/bin/warren rm /usr/bin/warrend + rmdir /var/run/warrend diff --git a/cmd/warren/main.go b/cmd/warren/main.go index a9166a9..0366221 100644 --- a/cmd/warren/main.go +++ b/cmd/warren/main.go @@ -49,5 +49,4 @@ func main() { } icmd.(*warren.Command).Exec(args[1:]) - } diff --git a/cmd/warrend/main.go b/cmd/warrend/main.go index 4018155..b3af5b9 100644 --- a/cmd/warrend/main.go +++ b/cmd/warrend/main.go @@ -11,6 +11,8 @@ import ( "syscall" ) +var cmdSock net.Listener + type SockCommand struct { warren.Command Run func(cmd *SockCommand, c net.Conn, args ...string) @@ -52,19 +54,64 @@ func sockCmdHandle(c net.Conn) { return } icmd.(*SockCommand).Exec(c, args[1:]) - warren.WriteStringz(c, "echo: "+data) +} + +// determine if warrend is already running +func checkRunning() bool { + if pfile, err := os.Open(warren.WarrendPidFile()); err == nil { + // pid file exists + var pid int + n, err := fmt.Fscanf(pfile, "%d", &pid) + if n == 0 || err != nil { + // read error + return false + } + // is there a runnign process with the pid? + err = syscall.Kill(pid, 0) + return err == nil + + } else { + // pid file does not exist + return false + } +} + +func initRunning() { + // rm old sock + os.Remove(warren.CmdSockAddr()) + // drop down pid + pidfile, err := os.Create(warren.WarrendPidFile()) + if err != nil { + log.Fatal("Could not create pidfile: ", warren.WarrendPidFile(), " : ", err) + } + pid := os.Getpid() + fmt.Fprintf(pidfile, "%d", pid) + pidfile.Close() + + cmdSock, err = net.Listen(warren.CmdSockType(), warren.CmdSockAddr()) + if err != nil { + fmt.Println(cmdSock) + Shutdown() + log.Fatal("Listen on sock error:", err) + } + +} + +func Shutdown() { + os.Remove(warren.WarrendPidFile()) + if cmdSock != nil { + cmdSock.Close() + } } func main() { - warren.RegisterCommand(statusCmd) + if checkRunning() { + log.Fatal("warrend instance already running") + } - cmdSock, err := net.Listen(warren.CmdSockType(), warren.CmdSockAddr()) - if cmdSock != nil { - defer cmdSock.Close() - } - if err != nil { - log.Fatal("Listen on sock error:", err) - } + initRunning() + + warren.RegisterCommand(statusCmd) // Handle signals c := make(chan os.Signal, 1) @@ -76,16 +123,18 @@ func main() { ) go func() { _ = <-c - cmdSock.Close() + Shutdown() }() + // Listen for { fd, err := cmdSock.Accept() fmt.Println("accept") if err != nil { + Shutdown() log.Fatal("accpet error:", err) } go sockCmdHandle(fd) } - + Shutdown() } diff --git a/lib/warren/warren.go b/lib/warren/warren.go index 88a7b2b..1730b5c 100644 --- a/lib/warren/warren.go +++ b/lib/warren/warren.go @@ -11,7 +11,8 @@ import ( ) var cmdSockType string = "unix" -var cmdSockAddr string = "/tmp/warrend.sock" +var cmdSockAddr string = "/var/run/warrend/warrend.sock" +var warrendPidFile string = "/var/run/warrend/warrend.pid" func CmdSockType() string { return cmdSockType @@ -21,6 +22,10 @@ func CmdSockAddr() string { return cmdSockAddr } +func WarrendPidFile() string { + return warrendPidFile +} + func GitHash() string { hash, err := exec.Command("git", "rev-parse", "HEAD").Output() if err != nil {