warrend init statup properly in /var/run and close and respect only one
instance
This commit is contained in:
parent
e8db38d869
commit
cf6a9c0a97
2
Makefile
2
Makefile
|
@ -18,7 +18,9 @@ clean:
|
||||||
install:
|
install:
|
||||||
cp cmd/warren/warren /usr/bin
|
cp cmd/warren/warren /usr/bin
|
||||||
cp cmd/warrend/warrend /usr/bin
|
cp cmd/warrend/warrend /usr/bin
|
||||||
|
mkdir /var/run/warrend
|
||||||
|
|
||||||
uninstall:
|
uninstall:
|
||||||
rm /usr/bin/warren
|
rm /usr/bin/warren
|
||||||
rm /usr/bin/warrend
|
rm /usr/bin/warrend
|
||||||
|
rmdir /var/run/warrend
|
||||||
|
|
|
@ -49,5 +49,4 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
icmd.(*warren.Command).Exec(args[1:])
|
icmd.(*warren.Command).Exec(args[1:])
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,8 @@ import (
|
||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var cmdSock net.Listener
|
||||||
|
|
||||||
type SockCommand struct {
|
type SockCommand struct {
|
||||||
warren.Command
|
warren.Command
|
||||||
Run func(cmd *SockCommand, c net.Conn, args ...string)
|
Run func(cmd *SockCommand, c net.Conn, args ...string)
|
||||||
|
@ -52,19 +54,64 @@ func sockCmdHandle(c net.Conn) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
icmd.(*SockCommand).Exec(c, args[1:])
|
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() {
|
func main() {
|
||||||
warren.RegisterCommand(statusCmd)
|
if checkRunning() {
|
||||||
|
log.Fatal("warrend instance already running")
|
||||||
|
}
|
||||||
|
|
||||||
cmdSock, err := net.Listen(warren.CmdSockType(), warren.CmdSockAddr())
|
initRunning()
|
||||||
if cmdSock != nil {
|
|
||||||
defer cmdSock.Close()
|
warren.RegisterCommand(statusCmd)
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal("Listen on sock error:", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle signals
|
// Handle signals
|
||||||
c := make(chan os.Signal, 1)
|
c := make(chan os.Signal, 1)
|
||||||
|
@ -76,16 +123,18 @@ func main() {
|
||||||
)
|
)
|
||||||
go func() {
|
go func() {
|
||||||
_ = <-c
|
_ = <-c
|
||||||
cmdSock.Close()
|
Shutdown()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
// Listen
|
||||||
for {
|
for {
|
||||||
fd, err := cmdSock.Accept()
|
fd, err := cmdSock.Accept()
|
||||||
fmt.Println("accept")
|
fmt.Println("accept")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Shutdown()
|
||||||
log.Fatal("accpet error:", err)
|
log.Fatal("accpet error:", err)
|
||||||
}
|
}
|
||||||
go sockCmdHandle(fd)
|
go sockCmdHandle(fd)
|
||||||
}
|
}
|
||||||
|
Shutdown()
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var cmdSockType string = "unix"
|
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 {
|
func CmdSockType() string {
|
||||||
return cmdSockType
|
return cmdSockType
|
||||||
|
@ -21,6 +22,10 @@ func CmdSockAddr() string {
|
||||||
return cmdSockAddr
|
return cmdSockAddr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WarrendPidFile() string {
|
||||||
|
return warrendPidFile
|
||||||
|
}
|
||||||
|
|
||||||
func GitHash() string {
|
func GitHash() string {
|
||||||
hash, err := exec.Command("git", "rev-parse", "HEAD").Output()
|
hash, err := exec.Command("git", "rev-parse", "HEAD").Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue