1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // This file implements sysSocket and accept for platforms that
6 // provide a fast path for setting SetNonblock and CloseOnExec.
8 // +build dragonfly freebsd linux netbsd openbsd
18 // Wrapper around the socket system call that marks the returned file
19 // descriptor as nonblocking and close-on-exec.
20 func sysSocket(family
, sotype
, proto
int) (int, error
) {
21 s
, err
:= socketFunc(family
, sotype|syscall
.SOCK_NONBLOCK|syscall
.SOCK_CLOEXEC
, proto
)
22 // On Linux the SOCK_NONBLOCK and SOCK_CLOEXEC flags were
23 // introduced in 2.6.27 kernel and on FreeBSD both flags were
24 // introduced in 10 kernel. If we get an EINVAL error on Linux
25 // or EPROTONOSUPPORT error on FreeBSD, fall back to using
26 // socket without them.
31 return -1, os
.NewSyscallError("socket", err
)
32 case syscall
.EPROTONOSUPPORT
, syscall
.EINVAL
:
35 // See ../syscall/exec_unix.go for description of ForkLock.
36 syscall
.ForkLock
.RLock()
37 s
, err
= socketFunc(family
, sotype
, proto
)
39 syscall
.CloseOnExec(s
)
41 syscall
.ForkLock
.RUnlock()
43 return -1, os
.NewSyscallError("socket", err
)
45 if err
= syscall
.SetNonblock(s
, true); err
!= nil {
47 return -1, os
.NewSyscallError("setnonblock", err
)