2013-01-29 21:52:43 +01:00
|
|
|
// 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.
|
|
|
|
|
2019-02-01 22:57:36 +01:00
|
|
|
// +build dragonfly freebsd hurd linux netbsd openbsd
|
2013-01-29 21:52:43 +01:00
|
|
|
|
|
|
|
package net
|
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
import (
|
2017-09-14 19:11:35 +02:00
|
|
|
"internal/poll"
|
2015-10-31 01:59:47 +01:00
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
)
|
2013-01-29 21:52:43 +01:00
|
|
|
|
|
|
|
// Wrapper around the socket system call that marks the returned file
|
|
|
|
// descriptor as nonblocking and close-on-exec.
|
2014-07-19 10:53:52 +02:00
|
|
|
func sysSocket(family, sotype, proto int) (int, error) {
|
2015-10-31 01:59:47 +01:00
|
|
|
s, err := socketFunc(family, sotype|syscall.SOCK_NONBLOCK|syscall.SOCK_CLOEXEC, proto)
|
2014-07-19 10:53:52 +02:00
|
|
|
// 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.
|
2015-10-31 01:59:47 +01:00
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
return s, nil
|
|
|
|
default:
|
|
|
|
return -1, os.NewSyscallError("socket", err)
|
|
|
|
case syscall.EPROTONOSUPPORT, syscall.EINVAL:
|
2013-01-29 21:52:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// See ../syscall/exec_unix.go for description of ForkLock.
|
|
|
|
syscall.ForkLock.RLock()
|
2015-10-31 01:59:47 +01:00
|
|
|
s, err = socketFunc(family, sotype, proto)
|
2013-01-29 21:52:43 +01:00
|
|
|
if err == nil {
|
|
|
|
syscall.CloseOnExec(s)
|
|
|
|
}
|
|
|
|
syscall.ForkLock.RUnlock()
|
|
|
|
if err != nil {
|
2015-10-31 01:59:47 +01:00
|
|
|
return -1, os.NewSyscallError("socket", err)
|
2013-01-29 21:52:43 +01:00
|
|
|
}
|
|
|
|
if err = syscall.SetNonblock(s, true); err != nil {
|
2017-09-14 19:11:35 +02:00
|
|
|
poll.CloseFunc(s)
|
2015-10-31 01:59:47 +01:00
|
|
|
return -1, os.NewSyscallError("setnonblock", err)
|
2013-01-29 21:52:43 +01:00
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|