Skip to content

Commit 86ca1a3

Browse files
committed
sockets: make NewUnixSocket, WithChown, WithChmod unix-only
The WithChown and WithChmod options are not supported on Windows. We could provide stubs in future, but let's start with not including them. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 6bb1d15 commit 86ca1a3

4 files changed

Lines changed: 42 additions & 41 deletions

File tree

sockets/unix_socket.go

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,6 @@ import (
5555
// SockOption sets up socket file's creating option
5656
type SockOption func(string) error
5757

58-
// WithChown modifies the socket file's uid and gid
59-
func WithChown(uid, gid int) SockOption {
60-
return func(path string) error {
61-
if err := os.Chown(path, uid, gid); err != nil {
62-
return err
63-
}
64-
return nil
65-
}
66-
}
67-
68-
// WithChmod modifies socket file's access mode.
69-
func WithChmod(mask os.FileMode) SockOption {
70-
return func(path string) error {
71-
if err := os.Chmod(path, mask); err != nil {
72-
return err
73-
}
74-
return nil
75-
}
76-
}
77-
7858
// NewUnixSocketWithOpts creates a unix socket with the specified options.
7959
// By default, socket permissions are 0000 (i.e.: no access for anyone); pass
8060
// WithChmod() and WithChown() to set the desired ownership and permissions.
@@ -102,8 +82,3 @@ func NewUnixSocketWithOpts(path string, opts ...SockOption) (net.Listener, error
10282

10383
return l, nil
10484
}
105-
106-
// NewUnixSocket creates a unix socket with the specified path and group.
107-
func NewUnixSocket(path string, gid int) (net.Listener, error) {
108-
return NewUnixSocketWithOpts(path, WithChown(0, gid), WithChmod(0o660))
109-
}

sockets/unix_socket_test.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,6 @@ func runTest(t *testing.T, path string, l net.Listener, echoStr string) {
3232
}
3333
}
3434

35-
// TestNewUnixSocket run under root user.
36-
func TestNewUnixSocket(t *testing.T) {
37-
if os.Getuid() != 0 {
38-
t.Skip("requires root")
39-
}
40-
gid := os.Getgid()
41-
path := "/tmp/test.sock"
42-
echoStr := "hello"
43-
l, err := NewUnixSocket(path, gid)
44-
if err != nil {
45-
t.Fatal(err)
46-
}
47-
defer func() { _ = l.Close() }()
48-
runTest(t, path, l, echoStr)
49-
}
50-
5135
func TestUnixSocketWithOpts(t *testing.T) {
5236
socketFile, err := os.CreateTemp("", "test*.sock")
5337
if err != nil {

sockets/unix_socket_unix.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,35 @@ package sockets
44

55
import (
66
"net"
7+
"os"
78
"syscall"
89
)
910

11+
// WithChown modifies the socket file's uid and gid
12+
func WithChown(uid, gid int) SockOption {
13+
return func(path string) error {
14+
if err := os.Chown(path, uid, gid); err != nil {
15+
return err
16+
}
17+
return nil
18+
}
19+
}
20+
21+
// WithChmod modifies socket file's access mode.
22+
func WithChmod(mask os.FileMode) SockOption {
23+
return func(path string) error {
24+
if err := os.Chmod(path, mask); err != nil {
25+
return err
26+
}
27+
return nil
28+
}
29+
}
30+
31+
// NewUnixSocket creates a unix socket with the specified path and group.
32+
func NewUnixSocket(path string, gid int) (net.Listener, error) {
33+
return NewUnixSocketWithOpts(path, WithChown(0, gid), WithChmod(0o660))
34+
}
35+
1036
func listenUnix(path string) (net.Listener, error) {
1137
// net.Listen does not allow for permissions to be set. As a result, when
1238
// specifying custom permissions ("WithChmod()"), there is a short time

sockets/unix_socket_unix_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,19 @@ func createTestUnixSocket(t *testing.T, path string) (listener net.Listener) {
3030
}
3131
return l
3232
}
33+
34+
// TestNewUnixSocket run under root user.
35+
func TestNewUnixSocket(t *testing.T) {
36+
if os.Getuid() != 0 {
37+
t.Skip("requires root")
38+
}
39+
gid := os.Getgid()
40+
path := "/tmp/test.sock"
41+
echoStr := "hello"
42+
l, err := NewUnixSocket(path, gid)
43+
if err != nil {
44+
t.Fatal(err)
45+
}
46+
defer func() { _ = l.Close() }()
47+
runTest(t, path, l, echoStr)
48+
}

0 commit comments

Comments
 (0)