From 0b1d455bc59a09115590d87698b2c4c8de14300b Mon Sep 17 00:00:00 2001 From: Balakrishnan Balasubramanian Date: Wed, 29 Mar 2023 18:37:30 -0400 Subject: [PATCH] cleanup old unix socket and set write permissions --- main.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index bc6eff6..f7e0665 100644 --- a/main.go +++ b/main.go @@ -4,8 +4,10 @@ package main import ( "bytes" "encoding/json" + "errors" "fmt" "io" + "io/fs" "log" "net" "net/http" @@ -146,10 +148,20 @@ func main() { } }) if unixSocketPath != "" { + // Remove old one + if err := os.Remove(unixSocketPath); err != nil && !errors.Is(err, fs.ErrNotExist) { + log.Panicf("Failed to remove unix socket : %q err: %v\n", unixSocketPath, err) + } + l, err := net.Listen("unix", unixSocketPath) if err != nil { - log.Panicf("Unable to listen to unix socket in path: %q err:%v\n", unixSocketPath, err) + log.Panicf("Unable to listen to unix socket : %q err: %v\n", unixSocketPath, err) } + + if err = os.Chmod(unixSocketPath, 0666); err != nil { + log.Panicf("Failed to set permission of unix socket %q err: %v\n", unixSocketPath, err) + } + log.Panicln(http.Serve(l, nil)) } log.Panicln(http.ListenAndServe(fmt.Sprintf(":%v", port), nil))