Retro68/gcc/libgo/go/net/sock_cloexec.go

88 lines
2.9 KiB
Go
Raw Normal View History

2014-09-21 17:33:12 +00: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.
2015-08-28 15:33:40 +00:00
// +build freebsd linux
2014-09-21 17:33:12 +00:00
package net
2017-04-10 11:32:00 +00:00
import (
"os"
"syscall"
)
2014-09-21 17:33:12 +00:00
// Wrapper around the socket system call that marks the returned file
// descriptor as nonblocking and close-on-exec.
2015-08-28 15:33:40 +00:00
func sysSocket(family, sotype, proto int) (int, error) {
2017-04-10 11:32:00 +00:00
s, err := socketFunc(family, sotype|syscall.SOCK_NONBLOCK|syscall.SOCK_CLOEXEC, proto)
2015-08-28 15:33:40 +00: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.
2017-04-10 11:32:00 +00:00
switch err {
case nil:
return s, nil
default:
return -1, os.NewSyscallError("socket", err)
case syscall.EPROTONOSUPPORT, syscall.EINVAL:
2014-09-21 17:33:12 +00:00
}
// See ../syscall/exec_unix.go for description of ForkLock.
syscall.ForkLock.RLock()
2017-04-10 11:32:00 +00:00
s, err = socketFunc(family, sotype, proto)
2014-09-21 17:33:12 +00:00
if err == nil {
syscall.CloseOnExec(s)
}
syscall.ForkLock.RUnlock()
if err != nil {
2017-04-10 11:32:00 +00:00
return -1, os.NewSyscallError("socket", err)
2014-09-21 17:33:12 +00:00
}
if err = syscall.SetNonblock(s, true); err != nil {
2017-04-10 11:32:00 +00:00
closeFunc(s)
return -1, os.NewSyscallError("setnonblock", err)
2014-09-21 17:33:12 +00:00
}
return s, nil
}
// Wrapper around the accept system call that marks the returned file
// descriptor as nonblocking and close-on-exec.
2015-08-28 15:33:40 +00:00
func accept(s int) (int, syscall.Sockaddr, error) {
2017-04-10 11:32:00 +00:00
ns, sa, err := accept4Func(s, syscall.SOCK_NONBLOCK|syscall.SOCK_CLOEXEC)
2015-08-28 15:33:40 +00:00
// 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 {
2017-04-10 11:32:00 +00:00
case nil:
return ns, sa, nil
default: // errors other than the ones listed
return -1, sa, os.NewSyscallError("accept4", err)
2015-08-28 15:33:40 +00:00
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
2014-09-21 17:33:12 +00:00
}
// 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.
2017-04-10 11:32:00 +00:00
ns, sa, err = acceptFunc(s)
2014-09-21 17:33:12 +00:00
if err == nil {
2015-08-28 15:33:40 +00:00
syscall.CloseOnExec(ns)
2014-09-21 17:33:12 +00:00
}
if err != nil {
2017-04-10 11:32:00 +00:00
return -1, nil, os.NewSyscallError("accept", err)
2014-09-21 17:33:12 +00:00
}
2015-08-28 15:33:40 +00:00
if err = syscall.SetNonblock(ns, true); err != nil {
2017-04-10 11:32:00 +00:00
closeFunc(ns)
return -1, nil, os.NewSyscallError("setnonblock", err)
2014-09-21 17:33:12 +00:00
}
2015-08-28 15:33:40 +00:00
return ns, sa, nil
2014-09-21 17:33:12 +00:00
}