Skip to content

Commit cb8dfa0

Browse files
committed
decorators for listener factory
1 parent 9993a2f commit cb8dfa0

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

main.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"flag"
1414
"fmt"
1515
"io"
16+
"io/fs"
1617
"log"
1718
"net"
1819
"net/http"
@@ -22,6 +23,7 @@ import (
2223
"os/signal"
2324
"path/filepath"
2425
"runtime"
26+
"strconv"
2527
"strings"
2628
"syscall"
2729
"time"
@@ -160,6 +162,25 @@ func (a *hexArg) Value() []byte {
160162
return a.value
161163
}
162164

165+
type modeArg fs.FileMode
166+
167+
func (a *modeArg) String() string {
168+
return fmt.Sprintf("%#o", uint32(*a))
169+
}
170+
171+
func (a *modeArg) Set(s string) error {
172+
p, err := strconv.ParseUint(s, 8, 32)
173+
if err != nil {
174+
return err
175+
}
176+
*a = modeArg(p)
177+
return nil
178+
}
179+
180+
func (a *modeArg) Value() fs.FileMode {
181+
return fs.FileMode(*a)
182+
}
183+
163184
type cacheKind int
164185

165186
const (
@@ -184,6 +205,8 @@ type CLIArgs struct {
184205
bind bindSpec
185206
bindReusePort bool
186207
bindPprof bindSpec
208+
unixSockUnlink bool
209+
unixSockMode modeArg
187210
auth string
188211
verbosity int
189212
cert, key, cafile string
@@ -275,6 +298,8 @@ func parse_args() CLIArgs {
275298
args.bindPprof.af = "unix"
276299
return nil
277300
})
301+
flag.BoolVar(&args.unixSockUnlink, "unix-sock-unlink", true, "delete file object located at Unix domain socket bind path before binding")
302+
flag.Var(&args.unixSockMode, "unix-sock-mode", "set file mode for bound unix socket")
278303
flag.StringVar(&args.auth, "auth", "none://", "auth parameters")
279304
flag.IntVar(&args.verbosity, "verbosity", 20, "logging verbosity "+
280305
"(10 - debug, 20 - info, 30 - warning, 40 - error, 50 - critical)")
@@ -551,6 +576,26 @@ func run() int {
551576
mainLogger.Warning("reuseport was requested but not available!")
552577
}
553578
}
579+
if args.unixSockUnlink {
580+
listenerFactory = func(orig func(string, string) (net.Listener, error)) func(string, string) (net.Listener, error) {
581+
return func(network, address string) (net.Listener, error) {
582+
if (network == "unix" || network == "unixdgram") && len(address) > 0 && address[0] != '@' {
583+
os.Remove(address)
584+
}
585+
return orig(network, address)
586+
}
587+
}(listenerFactory)
588+
}
589+
if args.unixSockMode != 0 {
590+
listenerFactory = func(orig func(string, string) (net.Listener, error)) func(string, string) (net.Listener, error) {
591+
return func(network, address string) (net.Listener, error) {
592+
if (network == "unix" || network == "unixdgram") && len(address) > 0 && address[0] != '@' {
593+
defer os.Chmod(address, args.unixSockMode.Value())
594+
}
595+
return orig(network, address)
596+
}
597+
}(listenerFactory)
598+
}
554599

555600
var listener net.Listener
556601
if args.bind.address == "" {

0 commit comments

Comments
 (0)