pipe_linux.go 990 B

12345678910111213141516171819202122232425262728293031323334
  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. package os
  5. import "syscall"
  6. // Pipe returns a connected pair of Files; reads from r return bytes written to w.
  7. // It returns the files and an error, if any.
  8. func Pipe() (r *File, w *File, err error) {
  9. var p [2]int
  10. e := syscall.Pipe2(p[0:], syscall.O_CLOEXEC)
  11. // pipe2 was added in 2.6.27 and our minimum requirement is 2.6.23, so it
  12. // might not be implemented.
  13. if e == syscall.ENOSYS {
  14. // See ../syscall/exec.go for description of lock.
  15. syscall.ForkLock.RLock()
  16. e = syscall.Pipe(p[0:])
  17. if e != nil {
  18. syscall.ForkLock.RUnlock()
  19. return nil, nil, NewSyscallError("pipe", e)
  20. }
  21. syscall.CloseOnExec(p[0])
  22. syscall.CloseOnExec(p[1])
  23. syscall.ForkLock.RUnlock()
  24. } else if e != nil {
  25. return nil, nil, NewSyscallError("pipe2", e)
  26. }
  27. return NewFile(uintptr(p[0]), "|0"), NewFile(uintptr(p[1]), "|1"), nil
  28. }