sys_cloexec.mx raw

   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  
   5  // This file implements sysSocket for platforms that do not provide a fast path
   6  // for setting SetNonblock and CloseOnExec.
   7  
   8  //go:build aix || darwin
   9  
  10  package net
  11  
  12  import (
  13  	"internal/poll"
  14  	"os"
  15  	"syscall"
  16  )
  17  
  18  // Wrapper around the socket system call that marks the returned file
  19  // descriptor as nonblocking and close-on-exec.
  20  func sysSocket(family, sotype, proto int) (int, error) {
  21  	// See ../syscall/exec_unix.go for description of ForkLock.
  22  	syscall.ForkLock.RLock()
  23  	s, err := socketFunc(family, sotype, proto)
  24  	if err == nil {
  25  		syscall.CloseOnExec(s)
  26  	}
  27  	syscall.ForkLock.RUnlock()
  28  	if err != nil {
  29  		return -1, os.NewSyscallError("socket", err)
  30  	}
  31  	if err = syscall.SetNonblock(s, true); err != nil {
  32  		poll.CloseFunc(s)
  33  		return -1, os.NewSyscallError("setnonblock", err)
  34  	}
  35  	return s, nil
  36  }
  37