mirror of
https://github.com/golang/go
synced 2024-11-11 23:20:24 -07:00
net: fix {FileConn, FileListener, FilePacketConn} fd leak to child process.
All of them call `newFileFD' which must properly restore close-on-exec on duplicated fds. R=golang-dev, bradfitz, mikioh.mikioh CC=golang-dev https://golang.org/cl/6445081
This commit is contained in:
parent
532dee3842
commit
2836c63459
@ -12,10 +12,14 @@ import (
|
||||
)
|
||||
|
||||
func newFileFD(f *os.File) (*netFD, error) {
|
||||
syscall.ForkLock.RLock()
|
||||
fd, err := syscall.Dup(int(f.Fd()))
|
||||
if err != nil {
|
||||
syscall.ForkLock.RUnlock()
|
||||
return nil, os.NewSyscallError("dup", err)
|
||||
}
|
||||
syscall.CloseOnExec(fd)
|
||||
syscall.ForkLock.RUnlock()
|
||||
|
||||
sotype, err := syscall.GetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_TYPE)
|
||||
if err != nil {
|
||||
|
@ -167,6 +167,18 @@ func TestExtraFiles(t *testing.T) {
|
||||
}
|
||||
defer ln.Close()
|
||||
|
||||
// Make sure duplicated fds don't leak to the child.
|
||||
f, err := ln.(*net.TCPListener).File()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer f.Close()
|
||||
ln2, err := net.FileListener(f)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer ln2.Close()
|
||||
|
||||
// Force TLS root certs to be loaded (which might involve
|
||||
// cgo), to make sure none of that potential C code leaks fds.
|
||||
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
|
Loading…
Reference in New Issue
Block a user