Skip to content

Commit

Permalink
Refine domain socket permission
Browse files Browse the repository at this point in the history
  • Loading branch information
hmol233 authored and yuhan6665 committed Feb 19, 2022
1 parent b6f77e4 commit b3ab94e
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions transport/internet/system_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package internet

import (
"context"
"os"
"runtime"
"strconv"
"strings"
"syscall"

"github.com/pires/go-proxyproto"
Expand Down Expand Up @@ -39,11 +42,10 @@ func getControlFunc(ctx context.Context, sockopt *SocketConfig, controllers []co
}
}

func (dl *DefaultListener) Listen(ctx context.Context, addr net.Addr, sockopt *SocketConfig) (net.Listener, error) {
func (dl *DefaultListener) Listen(ctx context.Context, addr net.Addr, sockopt *SocketConfig) (l net.Listener, err error) {
var lc net.ListenConfig
var l net.Listener
var err error
var network, address string

switch addr := addr.(type) {
case *net.TCPAddr:
network = addr.Network()
Expand All @@ -53,6 +55,24 @@ func (dl *DefaultListener) Listen(ctx context.Context, addr net.Addr, sockopt *S
lc.Control = nil
network = addr.Network()
address = addr.Name

if s := strings.Split(address, ","); len(s) == 2 {
address = s[0]
perm, perr := strconv.ParseUint(s[1], 8, 32)
if perr != nil {
return nil, newError("failed to parse permission: " + s[1]).Base(perr)
}

defer func(file string, permission os.FileMode) {
if err == nil {
cerr := os.Chmod(address, permission)
if cerr != nil {
err = newError("failed to set permission for " + file).Base(cerr)
}
}
}(address, os.FileMode(perm))
}

if (runtime.GOOS == "linux" || runtime.GOOS == "android") && address[0] == '@' {
// linux abstract unix domain socket is lockfree
if len(address) > 1 && address[1] == '@' {
Expand Down

0 comments on commit b3ab94e

Please sign in to comment.