1
0
Fork 0
forked from boyska/circolog

Fix syslogd-socket handling at interrupt

This commit is contained in:
Blallo 2018-11-10 16:32:12 +01:00
parent 36f531a58a
commit f66e07e873
No known key found for this signature in database
GPG key ID: 0CBE577C9B72DC3F

View file

@ -6,11 +6,18 @@ import (
"net" "net"
"net/http" "net/http"
"os" "os"
"os/signal"
"git.lattuga.net/boyska/circolog" "git.lattuga.net/boyska/circolog"
syslog "gopkg.in/mcuadros/go-syslog.v2" 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() { func main() {
var err error var err error
syslogSocketPath := flag.String("syslogd-socket", "", "The socket to listen to syslog addresses") 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!") querySocket := flag.String("query-socket", "", "Path to a unix domain socket for the HTTP server; recommended for security reasons!")
flag.Parse() flag.Parse()
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
hub := circolog.NewHub(*bufsize) hub := circolog.NewHub(*bufsize)
handler := syslog.NewChannelHandler(hub.LogMessages) handler := syslog.NewChannelHandler(hub.LogMessages)
@ -32,6 +42,7 @@ func main() {
fmt.Fprintln(os.Stderr, "argh", err) fmt.Fprintln(os.Stderr, "argh", err)
os.Exit(1) os.Exit(1)
} }
defer cleanSocket(*syslogSocketPath)
fmt.Printf("Binding socket `%s` [syslog]\n", *syslogSocketPath) fmt.Printf("Binding socket `%s` [syslog]\n", *syslogSocketPath)
} else { } else {
fmt.Printf("Binding address `%s` [syslog]\n", *syslogAddr) fmt.Printf("Binding address `%s` [syslog]\n", *syslogAddr)
@ -53,11 +64,22 @@ func main() {
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, "Error binding HTTP unix domain socket", err) fmt.Fprintln(os.Stderr, "Error binding HTTP unix domain socket", err)
} }
defer os.Remove(*querySocket) go http.Serve(unixListener, nil)
http.Serve(unixListener, nil)
} else { } else {
fmt.Printf("Binding address `%s` [http]\n", *queryAddr) 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()
} }