sys_cloexec.go 987 B

123456789101112131415161718192021222324252627282930313233343536
  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 do not provide a fast path
  5. // for setting SetNonblock and CloseOnExec.
  6. //go:build aix || darwin || (solaris && !illumos)
  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. // See ../syscall/exec_unix.go for description of ForkLock.
  17. syscall.ForkLock.RLock()
  18. s, err := socketFunc(family, sotype, proto)
  19. if err == nil {
  20. syscall.CloseOnExec(s)
  21. }
  22. syscall.ForkLock.RUnlock()
  23. if err != nil {
  24. return -1, os.NewSyscallError("socket", err)
  25. }
  26. if err = syscall.SetNonblock(s, true); err != nil {
  27. poll.CloseFunc(s)
  28. return -1, os.NewSyscallError("setnonblock", err)
  29. }
  30. return s, nil
  31. }