mirror of
https://github.com/golang/go
synced 2024-10-04 17:21:20 -06:00
29d1f3b85c
This change adds socket system call hooks to existing test cases for simulating a bit complicated network conditions to help making timeout and dual IP stack test cases work more properly in followup changes. Also test cases print debugging information in non-short mode like the following: Leaked goroutines: net.TestWriteTimeout.func2(0xc20802a5a0, 0xc20801d000, 0x1000, 0x1000, 0xc2081d2ae0) /go/src/net/timeout_test.go:170 +0x98 created by net.TestWriteTimeout /go/src/net/timeout_test.go:173 +0x745 net.runDatagramPacketConnServer(0xc2080730e0, 0x2bd270, 0x3, 0x2c1770, 0xb, 0xc2081d2ba0, 0xc2081d2c00) /go/src/net/server_test.go:398 +0x667 created by net.TestTimeoutUDP /go/src/net/timeout_test.go:247 +0xc9 (snip) Leaked sockets: 3: {Cookie:615726511685632 Err:<nil> SocketErr:0} 5: {Cookie:7934075906097152 Err:<nil> SocketErr:0} Socket statistical information: {Family:1 Type:805306370 Protocol:0 Opened:17 Accepted:0 Connected:5 Closed:17} {Family:2 Type:805306369 Protocol:0 Opened:450 Accepted:234 Connected:279 Closed:636} {Family:1 Type:805306369 Protocol:0 Opened:11 Accepted:5 Connected:5 Closed:16} {Family:28 Type:805306369 Protocol:0 Opened:95 Accepted:22 Connected:16 Closed:116} {Family:2 Type:805306370 Protocol:0 Opened:84 Accepted:0 Connected:34 Closed:83} {Family:28 Type:805306370 Protocol:0 Opened:52 Accepted:0 Connected:4 Closed:52} Change-Id: I0e84be59a0699bc31245c78e2249423459b8cdda Reviewed-on: https://go-review.googlesource.com/6390 Reviewed-by: Ian Lance Taylor <iant@golang.org>
79 lines
2.6 KiB
Go
79 lines
2.6 KiB
Go
// Copyright 2013 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
// This file implements sysSocket and accept for platforms that
|
|
// provide a fast path for setting SetNonblock and CloseOnExec.
|
|
|
|
// +build freebsd linux
|
|
|
|
package net
|
|
|
|
import "syscall"
|
|
|
|
// Wrapper around the socket system call that marks the returned file
|
|
// descriptor as nonblocking and close-on-exec.
|
|
func sysSocket(family, sotype, proto int) (int, error) {
|
|
s, err := socketFunc(family, sotype|syscall.SOCK_NONBLOCK|syscall.SOCK_CLOEXEC, proto)
|
|
// On Linux the SOCK_NONBLOCK and SOCK_CLOEXEC flags were
|
|
// introduced in 2.6.27 kernel and on FreeBSD both flags were
|
|
// introduced in 10 kernel. If we get an EINVAL error on Linux
|
|
// or EPROTONOSUPPORT error on FreeBSD, fall back to using
|
|
// socket without them.
|
|
if err == nil || (err != syscall.EPROTONOSUPPORT && err != syscall.EINVAL) {
|
|
return s, err
|
|
}
|
|
|
|
// See ../syscall/exec_unix.go for description of ForkLock.
|
|
syscall.ForkLock.RLock()
|
|
s, err = socketFunc(family, sotype, proto)
|
|
if err == nil {
|
|
syscall.CloseOnExec(s)
|
|
}
|
|
syscall.ForkLock.RUnlock()
|
|
if err != nil {
|
|
return -1, err
|
|
}
|
|
if err = syscall.SetNonblock(s, true); err != nil {
|
|
closeFunc(s)
|
|
return -1, err
|
|
}
|
|
return s, nil
|
|
}
|
|
|
|
// Wrapper around the accept system call that marks the returned file
|
|
// descriptor as nonblocking and close-on-exec.
|
|
func accept(s int) (int, syscall.Sockaddr, error) {
|
|
ns, sa, err := accept4Func(s, syscall.SOCK_NONBLOCK|syscall.SOCK_CLOEXEC)
|
|
// On Linux the accept4 system call was introduced in 2.6.28
|
|
// kernel and on FreeBSD it was introduced in 10 kernel. If we
|
|
// get an ENOSYS error on both Linux and FreeBSD, or EINVAL
|
|
// error on Linux, fall back to using accept.
|
|
switch err {
|
|
default: // nil and errors other than the ones listed
|
|
return ns, sa, err
|
|
case syscall.ENOSYS: // syscall missing
|
|
case syscall.EINVAL: // some Linux use this instead of ENOSYS
|
|
case syscall.EACCES: // some Linux use this instead of ENOSYS
|
|
case syscall.EFAULT: // some Linux use this instead of ENOSYS
|
|
}
|
|
|
|
// See ../syscall/exec_unix.go for description of ForkLock.
|
|
// It is probably okay to hold the lock across syscall.Accept
|
|
// because we have put fd.sysfd into non-blocking mode.
|
|
// However, a call to the File method will put it back into
|
|
// blocking mode. We can't take that risk, so no use of ForkLock here.
|
|
ns, sa, err = acceptFunc(s)
|
|
if err == nil {
|
|
syscall.CloseOnExec(ns)
|
|
}
|
|
if err != nil {
|
|
return -1, nil, err
|
|
}
|
|
if err = syscall.SetNonblock(ns, true); err != nil {
|
|
closeFunc(ns)
|
|
return -1, nil, err
|
|
}
|
|
return ns, sa, nil
|
|
}
|