2014-12-14 19:55:56 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-12-19 16:18:25 +00:00
|
|
|
"github.com/dballard/warren/lib/warren"
|
2014-12-14 19:55:56 +00:00
|
|
|
"log"
|
|
|
|
"net"
|
2014-12-19 16:18:25 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2015-01-27 16:19:38 +00:00
|
|
|
"strings"
|
2014-12-19 16:18:25 +00:00
|
|
|
"syscall"
|
2014-12-14 19:55:56 +00:00
|
|
|
)
|
|
|
|
|
2015-02-04 21:05:28 +00:00
|
|
|
var cmdSock net.Listener
|
|
|
|
|
2015-01-02 17:07:30 +00:00
|
|
|
type SockCommand struct {
|
|
|
|
warren.Command
|
|
|
|
Run func(cmd *SockCommand, c net.Conn, args ...string)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cmd *SockCommand) GetName() string {
|
|
|
|
return cmd.Name
|
|
|
|
}
|
|
|
|
|
2015-02-14 19:42:02 +00:00
|
|
|
func (cmd *SockCommand) GetSummary() string {
|
|
|
|
return cmd.Summary
|
|
|
|
}
|
|
|
|
|
2015-01-02 17:07:30 +00:00
|
|
|
func (cmd *SockCommand) Exec(c net.Conn, args []string) {
|
|
|
|
cmd.Flag.Usage = func() {
|
|
|
|
// helpFunc(c, c.Name)
|
|
|
|
}
|
|
|
|
cmd.Flag.Parse(args)
|
|
|
|
cmd.Run(cmd, c, cmd.Flag.Args()...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func sockCmdHandle(c net.Conn) {
|
2014-12-14 19:55:56 +00:00
|
|
|
defer c.Close()
|
2014-12-30 17:32:11 +00:00
|
|
|
fmt.Println("cmdHandle")
|
|
|
|
data, err := warren.ReadStringz(c)
|
2014-12-14 19:55:56 +00:00
|
|
|
if err != nil {
|
2014-12-30 17:32:11 +00:00
|
|
|
fmt.Println(err)
|
2015-01-28 06:20:39 +00:00
|
|
|
warren.WriteStringz(c, "read error:"+err.Error())
|
2014-12-14 19:55:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Println("cmdHandle got command:", string(data))
|
2015-01-02 17:07:30 +00:00
|
|
|
|
2015-01-27 16:19:38 +00:00
|
|
|
// BAD: TODO: handle quotes "
|
2015-01-28 06:20:39 +00:00
|
|
|
var args = strings.Split(data, " ")
|
2015-01-27 16:19:38 +00:00
|
|
|
|
2015-01-02 17:07:30 +00:00
|
|
|
// parse args
|
2015-01-27 16:19:38 +00:00
|
|
|
var icmd = warren.GetCommand(args[0])
|
2015-01-02 17:07:30 +00:00
|
|
|
// get and run cmd
|
2015-01-27 16:19:38 +00:00
|
|
|
if icmd == nil {
|
2015-01-28 06:20:39 +00:00
|
|
|
warren.WriteStringz(c, "Unknown command "+args[0])
|
2015-01-27 16:19:38 +00:00
|
|
|
// usage ?
|
2015-01-28 06:20:39 +00:00
|
|
|
|
2015-01-27 16:19:38 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
icmd.(*SockCommand).Exec(c, args[1:])
|
2014-12-14 19:55:56 +00:00
|
|
|
}
|
|
|
|
|
2015-02-04 21:05:28 +00:00
|
|
|
// determine if warrend is already running
|
|
|
|
func checkRunning() bool {
|
2015-02-05 18:58:17 +00:00
|
|
|
if pfile, err := os.Open(warren.WarrendPidFile); err == nil {
|
2015-02-04 21:05:28 +00:00
|
|
|
// 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
|
2014-12-19 16:18:25 +00:00
|
|
|
|
2015-02-04 21:05:28 +00:00
|
|
|
} else {
|
|
|
|
// pid file does not exist
|
|
|
|
return false
|
2015-01-28 06:20:39 +00:00
|
|
|
}
|
2015-02-04 21:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func initRunning() {
|
|
|
|
// rm old sock
|
2015-02-05 18:58:17 +00:00
|
|
|
os.Remove(warren.CmdSockAddr)
|
2015-02-04 21:05:28 +00:00
|
|
|
// drop down pid
|
2015-02-05 18:58:17 +00:00
|
|
|
pidfile, err := os.Create(warren.WarrendPidFile)
|
2014-12-14 19:55:56 +00:00
|
|
|
if err != nil {
|
2015-02-14 19:42:02 +00:00
|
|
|
log.Fatal("Could not create pidfile: ", warren.WarrendPidFile, " : ", err)
|
2015-02-04 21:05:28 +00:00
|
|
|
}
|
|
|
|
pid := os.Getpid()
|
|
|
|
fmt.Fprintf(pidfile, "%d", pid)
|
|
|
|
pidfile.Close()
|
|
|
|
|
2015-02-05 18:58:17 +00:00
|
|
|
cmdSock, err = net.Listen(warren.CmdSockType, warren.CmdSockAddr)
|
2015-02-04 21:05:28 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(cmdSock)
|
|
|
|
Shutdown()
|
2014-12-14 19:55:56 +00:00
|
|
|
log.Fatal("Listen on sock error:", err)
|
|
|
|
}
|
|
|
|
|
2015-02-04 21:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Shutdown() {
|
2015-02-05 18:58:17 +00:00
|
|
|
os.Remove(warren.WarrendPidFile)
|
2015-02-04 21:05:28 +00:00
|
|
|
if cmdSock != nil {
|
|
|
|
cmdSock.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if checkRunning() {
|
|
|
|
log.Fatal("warrend instance already running")
|
|
|
|
}
|
|
|
|
|
2015-02-14 19:42:02 +00:00
|
|
|
LoadConf()
|
|
|
|
if conf == nil {
|
|
|
|
log.Fatal("failed to load conf")
|
|
|
|
}
|
|
|
|
|
2015-02-04 21:05:28 +00:00
|
|
|
initRunning()
|
|
|
|
|
|
|
|
warren.RegisterCommand(statusCmd)
|
2015-02-14 19:42:02 +00:00
|
|
|
warren.RegisterCommand(regCmd)
|
2015-02-04 21:05:28 +00:00
|
|
|
|
2014-12-19 16:18:25 +00:00
|
|
|
// Handle signals
|
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(c,
|
|
|
|
syscall.SIGINT,
|
|
|
|
syscall.SIGTERM,
|
|
|
|
syscall.SIGQUIT,
|
|
|
|
syscall.SIGHUP,
|
|
|
|
)
|
|
|
|
go func() {
|
|
|
|
_ = <-c
|
2015-02-04 21:05:28 +00:00
|
|
|
Shutdown()
|
2014-12-19 16:18:25 +00:00
|
|
|
}()
|
|
|
|
|
2015-02-04 21:05:28 +00:00
|
|
|
// Listen
|
2014-12-14 19:55:56 +00:00
|
|
|
for {
|
|
|
|
fd, err := cmdSock.Accept()
|
|
|
|
fmt.Println("accept")
|
|
|
|
if err != nil {
|
2015-02-04 21:05:28 +00:00
|
|
|
Shutdown()
|
2014-12-14 19:55:56 +00:00
|
|
|
log.Fatal("accpet error:", err)
|
|
|
|
}
|
2015-01-02 17:07:30 +00:00
|
|
|
go sockCmdHandle(fd)
|
2014-12-14 19:55:56 +00:00
|
|
|
}
|
2015-02-04 21:05:28 +00:00
|
|
|
Shutdown()
|
2014-12-14 19:55:56 +00:00
|
|
|
}
|