github.com/karrick/go@v0.0.0-20170817181416-d5b0ec858b37/src/os/pipe_freebsd.go (about)

     1  // Copyright 2017 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  package os
     6  
     7  import "syscall"
     8  
     9  // Pipe returns a connected pair of Files; reads from r return bytes written to w.
    10  // It returns the files and an error, if any.
    11  func Pipe() (r *File, w *File, err error) {
    12  	var p [2]int
    13  
    14  	e := syscall.Pipe2(p[0:], syscall.O_CLOEXEC)
    15  	if e != nil {
    16  		// Fallback support for FreeBSD 9, which lacks Pipe2.
    17  		//
    18  		// TODO: remove this for Go 1.10 when FreeBSD 9
    19  		// is removed (Issue 19072).
    20  
    21  		// See ../syscall/exec.go for description of lock.
    22  		syscall.ForkLock.RLock()
    23  		e := syscall.Pipe(p[0:])
    24  		if e != nil {
    25  			syscall.ForkLock.RUnlock()
    26  			return nil, nil, NewSyscallError("pipe", e)
    27  		}
    28  		syscall.CloseOnExec(p[0])
    29  		syscall.CloseOnExec(p[1])
    30  		syscall.ForkLock.RUnlock()
    31  	}
    32  
    33  	return newFile(uintptr(p[0]), "|0", true), newFile(uintptr(p[1]), "|1", true), nil
    34  }