Source file src/internal/poll/sock_cloexec.go

     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 accept for platforms that provide a fast path for
     6  // setting SetNonblock and CloseOnExec.
     7  
     8  //go:build dragonfly || freebsd || (linux && !arm) || netbsd || openbsd || solaris
     9  
    10  package poll
    11  
    12  import "syscall"
    13  
    14  // Wrapper around the accept system call that marks the returned file
    15  // descriptor as nonblocking and close-on-exec.
    16  func accept(s int) (int, syscall.Sockaddr, string, error) {
    17  	ns, sa, err := Accept4Func(s, syscall.SOCK_NONBLOCK|syscall.SOCK_CLOEXEC)
    18  	// TODO: We can remove the fallback on Linux and *BSD,
    19  	// as currently supported versions all support accept4
    20  	// with SOCK_CLOEXEC, but Solaris does not. See issue #59359.
    21  	switch err {
    22  	case nil:
    23  		return ns, sa, "", nil
    24  	default: // errors other than the ones listed
    25  		return -1, sa, "accept4", err
    26  	case syscall.ENOSYS: // syscall missing
    27  	case syscall.EINVAL: // some Linux use this instead of ENOSYS
    28  	case syscall.EACCES: // some Linux use this instead of ENOSYS
    29  	case syscall.EFAULT: // some Linux use this instead of ENOSYS
    30  	}
    31  
    32  	// See ../syscall/exec_unix.go for description of ForkLock.
    33  	// It is probably okay to hold the lock across syscall.Accept
    34  	// because we have put fd.sysfd into non-blocking mode.
    35  	// However, a call to the File method will put it back into
    36  	// blocking mode. We can't take that risk, so no use of ForkLock here.
    37  	ns, sa, err = AcceptFunc(s)
    38  	if err == nil {
    39  		syscall.CloseOnExec(ns)
    40  	}
    41  	if err != nil {
    42  		return -1, nil, "accept", err
    43  	}
    44  	if err = syscall.SetNonblock(ns, true); err != nil {
    45  		CloseFunc(ns)
    46  		return -1, nil, "setnonblock", err
    47  	}
    48  	return ns, sa, "", nil
    49  }
    50  

View as plain text