Clean sockets at exit.

This commit is contained in:
Blallo 2019-05-03 10:32:18 +02:00
parent 3d463823e3
commit 9728b236ed
No known key found for this signature in database
GPG key ID: 0CBE577C9B72DC3F

View file

@ -17,12 +17,18 @@ import (
syslog "gopkg.in/mcuadros/go-syslog.v2"
)
var socketsToRemove []string
func cleanSocket(socket string) {
if err := os.Remove(socket); err != nil {
fmt.Fprintln(os.Stderr, "Error cleaning", socket, ":", err)
}
}
func removeAtExit(socket string) {
socketsToRemove = append(socketsToRemove, socket)
}
func main() {
var err error
var logFmt formatter.SyslogRFC
@ -77,7 +83,7 @@ func main() {
fmt.Fprintln(os.Stderr, "Error binding HTTP unix domain socket", err)
return
}
defer cleanSocket(*querySocket)
removeAtExit(*querySocket)
go func() {
if err := httpQueryServer.Serve(unixListener); err != nil && err != http.ErrServerClosed {
fmt.Fprintln(os.Stderr, "error binding", *querySocket, ":", err)
@ -102,7 +108,7 @@ func main() {
fmt.Fprintln(os.Stderr, "Error binding HTTP unix domain socket", err)
return
}
defer cleanSocket(*ctlSocket)
removeAtExit(*ctlSocket)
go func() {
if err := httpCtlServer.Serve(unixListener); err != nil && err != http.ErrServerClosed {
fmt.Fprintln(os.Stderr, "error binding:", err)
@ -152,6 +158,9 @@ func main() {
if err := httpCtlServer.Shutdown(nil); err != nil {
fmt.Fprintln(os.Stderr, "Error closing control server:", err)
}
for _, socket := range socketsToRemove {
cleanSocket(socket)
}
return
}
}