From f66e07e8738f5371db8eb2935412accb4c3839a6 Mon Sep 17 00:00:00 2001 From: Blallo Date: Sat, 10 Nov 2018 16:32:12 +0100 Subject: [PATCH] Fix syslogd-socket handling at interrupt --- cmd/circologd/main.go | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/cmd/circologd/main.go b/cmd/circologd/main.go index 2364fe5..768cfbe 100644 --- a/cmd/circologd/main.go +++ b/cmd/circologd/main.go @@ -6,11 +6,18 @@ import ( "net" "net/http" "os" + "os/signal" "git.lattuga.net/boyska/circolog" syslog "gopkg.in/mcuadros/go-syslog.v2" ) +func cleanSocket(socket string) { + if err := os.Remove(socket); err != nil { + fmt.Fprintln(os.Stderr, socket, ":", err) + } +} + func main() { var err error syslogSocketPath := flag.String("syslogd-socket", "", "The socket to listen to syslog addresses") @@ -21,6 +28,9 @@ func main() { querySocket := flag.String("query-socket", "", "Path to a unix domain socket for the HTTP server; recommended for security reasons!") flag.Parse() + interrupt := make(chan os.Signal, 1) + signal.Notify(interrupt, os.Interrupt) + hub := circolog.NewHub(*bufsize) handler := syslog.NewChannelHandler(hub.LogMessages) @@ -32,6 +42,7 @@ func main() { fmt.Fprintln(os.Stderr, "argh", err) os.Exit(1) } + defer cleanSocket(*syslogSocketPath) fmt.Printf("Binding socket `%s` [syslog]\n", *syslogSocketPath) } else { fmt.Printf("Binding address `%s` [syslog]\n", *syslogAddr) @@ -53,11 +64,22 @@ func main() { if err != nil { fmt.Fprintln(os.Stderr, "Error binding HTTP unix domain socket", err) } - defer os.Remove(*querySocket) - http.Serve(unixListener, nil) + go http.Serve(unixListener, nil) } else { fmt.Printf("Binding address `%s` [http]\n", *queryAddr) - http.ListenAndServe(*queryAddr, nil) + go http.ListenAndServe(*queryAddr, nil) + } + + for { + select { + case <-interrupt: + server.Kill() + //server.Wait() + if *syslogSocketPath != "" { + + } + return + default: + } } - server.Wait() }