github.com/spotify/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/src/pkg/net/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 dragonfly freebsd netbsd openbsd
     9  
    10  package net
    11  
    12  import "syscall"
    13  
    14  // Wrapper around the socket system call that marks the returned file
    15  // descriptor as nonblocking and close-on-exec.
    16  func sysSocket(f, t, p int) (int, error) {
    17  	// See ../syscall/exec_unix.go for description of ForkLock.
    18  	syscall.ForkLock.RLock()
    19  	s, err := syscall.Socket(f, t, p)
    20  	if err == nil {
    21  		syscall.CloseOnExec(s)
    22  	}
    23  	syscall.ForkLock.RUnlock()
    24  	if err != nil {
    25  		return -1, err
    26  	}
    27  	if err = syscall.SetNonblock(s, true); err != nil {
    28  		syscall.Close(s)
    29  		return -1, err
    30  	}
    31  	return s, nil
    32  }
    33  
    34  // Wrapper around the accept system call that marks the returned file
    35  // descriptor as nonblocking and close-on-exec.
    36  func accept(fd int) (int, syscall.Sockaddr, error) {
    37  	// See ../syscall/exec_unix.go for description of ForkLock.
    38  	// It is probably okay to hold the lock across syscall.Accept
    39  	// because we have put fd.sysfd into non-blocking mode.
    40  	// However, a call to the File method will put it back into
    41  	// blocking mode. We can't take that risk, so no use of ForkLock here.
    42  	nfd, sa, err := syscall.Accept(fd)
    43  	if err == nil {
    44  		syscall.CloseOnExec(nfd)
    45  	}
    46  	if err != nil {
    47  		return -1, nil, err
    48  	}
    49  	if err = syscall.SetNonblock(nfd, true); err != nil {
    50  		syscall.Close(nfd)
    51  		return -1, nil, err
    52  	}
    53  	return nfd, sa, nil
    54  }