sock_cloexec.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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.
  4. // This file implements sysSocket for platforms that provide a fast path for
  5. // setting SetNonblock and CloseOnExec.
  6. //go:build dragonfly || freebsd || hurd || illumos || linux || netbsd || openbsd
  7. package net
  8. import (
  9. "internal/poll"
  10. "os"
  11. "syscall"
  12. )
  13. // Wrapper around the socket system call that marks the returned file
  14. // descriptor as nonblocking and close-on-exec.
  15. func sysSocket(family, sotype, proto int) (int, error) {
  16. s, err := socketFunc(family, sotype|syscall.SOCK_NONBLOCK|syscall.SOCK_CLOEXEC, proto)
  17. // On Linux the SOCK_NONBLOCK and SOCK_CLOEXEC flags were
  18. // introduced in 2.6.27 kernel and on FreeBSD both flags were
  19. // introduced in 10 kernel. If we get an EINVAL error on Linux
  20. // or EPROTONOSUPPORT error on FreeBSD, fall back to using
  21. // socket without them.
  22. switch err {
  23. case nil:
  24. return s, nil
  25. default:
  26. return -1, os.NewSyscallError("socket", err)
  27. case syscall.EPROTONOSUPPORT, syscall.EINVAL:
  28. }
  29. // See ../syscall/exec_unix.go for description of ForkLock.
  30. syscall.ForkLock.RLock()
  31. s, err = socketFunc(family, sotype, proto)
  32. if err == nil {
  33. syscall.CloseOnExec(s)
  34. }
  35. syscall.ForkLock.RUnlock()
  36. if err != nil {
  37. return -1, os.NewSyscallError("socket", err)
  38. }
  39. if err = syscall.SetNonblock(s, true); err != nil {
  40. poll.CloseFunc(s)
  41. return -1, os.NewSyscallError("setnonblock", err)
  42. }
  43. return s, nil
  44. }