Skip to content

Commit 5d04e7f

Browse files
authored
Merge pull request #4823 from kolyshkin/unix-conn
Simplify getting net.UnixConn
2 parents b64bb16 + 314dd81 commit 5d04e7f

File tree

3 files changed

+12
-34
lines changed

3 files changed

+12
-34
lines changed

tests/cmd/pidfd-kill/pidfd-kill.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,7 @@ func recvPidfd(socketFile string) (*os.File, error) {
9999
}
100100
defer conn.Close()
101101

102-
unixconn, ok := conn.(*net.UnixConn)
103-
if !ok {
104-
return nil, errors.New("failed to cast to unixconn")
105-
}
106-
107-
socket, err := unixconn.File()
102+
socket, err := conn.(*net.UnixConn).File()
108103
if err != nil {
109104
return nil, err
110105
}

tests/cmd/recvtty/recvtty.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,7 @@ func handleSingle(path string, noStdin bool) error {
8585
// Close ln, to allow for other instances to take over.
8686
ln.Close()
8787

88-
// Get the fd of the connection.
89-
unixconn, ok := conn.(*net.UnixConn)
90-
if !ok {
91-
return errors.New("failed to cast to unixconn")
92-
}
93-
94-
socket, err := unixconn.File()
88+
socket, err := conn.(*net.UnixConn).File()
9589
if err != nil {
9690
return err
9791
}
@@ -158,13 +152,7 @@ func handleNull(path string) error {
158152
// Don't leave references lying around.
159153
defer conn.Close()
160154

161-
// Get the fd of the connection.
162-
unixconn, ok := conn.(*net.UnixConn)
163-
if !ok {
164-
return
165-
}
166-
167-
socket, err := unixconn.File()
155+
socket, err := conn.(*net.UnixConn).File()
168156
if err != nil {
169157
return
170158
}

utils_linux.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func newProcess(p *specs.Process) (*libcontainer.Process, error) {
9595
}
9696

9797
// setupIO modifies the given process config according to the options.
98-
func setupIO(process *libcontainer.Process, container *libcontainer.Container, createTTY, detach bool, sockpath string) (*tty, error) {
98+
func setupIO(process *libcontainer.Process, container *libcontainer.Container, createTTY, detach bool, sockpath string) (_ *tty, Err error) {
9999
if createTTY {
100100
process.Stdin = nil
101101
process.Stdout = nil
@@ -121,12 +121,13 @@ func setupIO(process *libcontainer.Process, container *libcontainer.Container, c
121121
if err != nil {
122122
return nil, err
123123
}
124-
uc, ok := conn.(*net.UnixConn)
125-
if !ok {
126-
return nil, errors.New("casting to UnixConn failed")
127-
}
128-
t.postStart = append(t.postStart, uc)
129-
socket, err := uc.File()
124+
defer func() {
125+
if Err != nil {
126+
conn.Close()
127+
}
128+
}()
129+
t.postStart = append(t.postStart, conn)
130+
socket, err := conn.(*net.UnixConn).File()
130131
if err != nil {
131132
return nil, err
132133
}
@@ -432,13 +433,7 @@ func setupPidfdSocket(process *libcontainer.Process, sockpath string) (_clean fu
432433
return nil, fmt.Errorf("failed to dail %s: %w", sockpath, err)
433434
}
434435

435-
uc, ok := conn.(*net.UnixConn)
436-
if !ok {
437-
conn.Close()
438-
return nil, errors.New("failed to cast to UnixConn")
439-
}
440-
441-
socket, err := uc.File()
436+
socket, err := conn.(*net.UnixConn).File()
442437
if err != nil {
443438
conn.Close()
444439
return nil, fmt.Errorf("failed to dup socket: %w", err)

0 commit comments

Comments
 (0)