Source File
sock_cloexec.go
Belonging Package
net
// 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 dragonfly freebsd illumos linux netbsd openbsd
package net
import (
)
// Wrapper around the socket system call that marks the returned file
// descriptor as nonblocking and close-on-exec.
func (, , int) (int, error) {
, := socketFunc(, |syscall.SOCK_NONBLOCK|syscall.SOCK_CLOEXEC, )
// 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.
switch {
case nil:
return , nil
default:
return -1, os.NewSyscallError("socket", )
case syscall.EPROTONOSUPPORT, syscall.EINVAL:
}
// See ../syscall/exec_unix.go for description of ForkLock.
syscall.ForkLock.RLock()
, = socketFunc(, , )
if == nil {
syscall.CloseOnExec()
}
syscall.ForkLock.RUnlock()
if != nil {
return -1, os.NewSyscallError("socket", )
}
if = syscall.SetNonblock(, true); != nil {
poll.CloseFunc()
return -1, os.NewSyscallError("setnonblock", )
}
return , nil
}