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 for platforms that provide a fast path for
6 // setting SetNonblock and CloseOnExec.
8 //go:build dragonfly || freebsd || linux || netbsd || openbsd || solaris
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 // TODO: We can remove the fallback on Linux and *BSD,
23 // as currently supported versions all support accept4
24 // with SOCK_CLOEXEC, but Solaris does not. See issue #59359.
29 return -1, os.NewSyscallError("socket", err)
30 case syscall.EPROTONOSUPPORT, syscall.EINVAL:
33 // See ../syscall/exec_unix.go for description of ForkLock.
34 syscall.ForkLock.RLock()
35 s, err = socketFunc(family, sotype, proto)
37 syscall.CloseOnExec(s)
39 syscall.ForkLock.RUnlock()
41 return -1, os.NewSyscallError("socket", err)
43 if err = syscall.SetNonblock(s, true); err != nil {
45 return -1, os.NewSyscallError("setnonblock", err)