github.com/slayercat/go@v0.0.0-20170428012452-c51559813f61/src/internal/poll/sys_cloexec.go (about)

     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 and accept for platforms that do not
     6  // provide a fast path for setting SetNonblock and CloseOnExec.
     7  
     8  // +build darwin nacl netbsd openbsd solaris
     9  
    10  package poll
    11  
    12  import (
    13  	"syscall"
    14  )
    15  
    16  // Wrapper around the accept system call that marks the returned file
    17  // descriptor as nonblocking and close-on-exec.
    18  func accept(s int) (int, syscall.Sockaddr, string, error) {
    19  	// See ../syscall/exec_unix.go for description of ForkLock.
    20  	// It is probably okay to hold the lock across syscall.Accept
    21  	// because we have put fd.sysfd into non-blocking mode.
    22  	// However, a call to the File method will put it back into
    23  	// blocking mode. We can't take that risk, so no use of ForkLock here.
    24  	ns, sa, err := AcceptFunc(s)
    25  	if err == nil {
    26  		syscall.CloseOnExec(ns)
    27  	}
    28  	if err != nil {
    29  		return -1, nil, "accept", err
    30  	}
    31  	if err = syscall.SetNonblock(ns, true); err != nil {
    32  		CloseFunc(ns)
    33  		return -1, nil, "setnonblock", err
    34  	}
    35  	return ns, sa, "", nil
    36  }