sock_cloexec_solaris.mx raw

   1  // Copyright 2024 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 accept for platforms that provide a fast path for
   6  // setting SetNonblock and CloseOnExec, but don't necessarily have accept4.
   7  // The accept4(3c) function was added to Oracle Solaris in the Solaris 11.4.0
   8  // release. Thus, on releases prior to 11.4, we fall back to the combination
   9  // of accept(3c) and fcntl(2).
  10  
  11  package poll
  12  
  13  import (
  14  	"internal/syscall/unix"
  15  	"syscall"
  16  )
  17  
  18  // Wrapper around the accept system call that marks the returned file
  19  // descriptor as nonblocking and close-on-exec.
  20  func accept(s int) (int, syscall.Sockaddr, []byte, error) {
  21  	// Perform a cheap test and try the fast path first.
  22  	if unix.SupportAccept4() {
  23  		ns, sa, err := Accept4Func(s, syscall.SOCK_NONBLOCK|syscall.SOCK_CLOEXEC)
  24  		if err != nil {
  25  			return -1, nil, "accept4", err
  26  		}
  27  		return ns, sa, "", nil
  28  	}
  29  
  30  	// See ../syscall/exec_unix.go for description of ForkLock.
  31  	// It is probably okay to hold the lock across syscall.Accept
  32  	// because we have put fd.sysfd into non-blocking mode.
  33  	// However, a call to the File method will put it back into
  34  	// blocking mode. We can't take that risk, so no use of ForkLock here.
  35  	ns, sa, err := AcceptFunc(s)
  36  	if err == nil {
  37  		syscall.CloseOnExec(ns)
  38  	}
  39  	if err != nil {
  40  		return -1, nil, "accept", err
  41  	}
  42  	if err = syscall.SetNonblock(ns, true); err != nil {
  43  		CloseFunc(ns)
  44  		return -1, nil, "setnonblock", err
  45  	}
  46  	return ns, sa, "", nil
  47  }
  48