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

51 lines
1.5 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 for platforms that provide a fast path for
// setting SetNonblock and CloseOnExec.
2014-09-21 17:33:12 +00:00
//go:build dragonfly || freebsd || hurd || illumos || linux || netbsd || openbsd
2014-09-21 17:33:12 +00:00
package net
2017-04-10 11:32:00 +00:00
import (
2018-12-28 15:30:48 +00:00
"internal/poll"
2017-04-10 11:32:00 +00:00
"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 {
2018-12-28 15:30:48 +00:00
poll.CloseFunc(s)
2017-04-10 11:32:00 +00:00
return -1, os.NewSyscallError("setnonblock", err)
2014-09-21 17:33:12 +00:00
}
return s, nil
}