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