diff options
-rw-r--r-- | src/os/exec/exec_test.go | 10 | ||||
-rw-r--r-- | src/os/exec/exec_windows_test.go | 43 | ||||
-rw-r--r-- | src/os/file_windows.go | 5 |
3 files changed, 56 insertions, 2 deletions
diff --git a/src/os/exec/exec_test.go b/src/os/exec/exec_test.go index 8b0c93f382..57591a38ab 100644 --- a/src/os/exec/exec_test.go +++ b/src/os/exec/exec_test.go @@ -915,6 +915,16 @@ func TestHelperProcess(*testing.T) { case "sleep": time.Sleep(3 * time.Second) os.Exit(0) + case "pipehandle": + handle, _ := strconv.ParseUint(args[0], 16, 64) + pipe := os.NewFile(uintptr(handle), "") + _, err := fmt.Fprint(pipe, args[1]) + if err != nil { + fmt.Fprintf(os.Stderr, "writing to pipe failed: %v\n", err) + os.Exit(1) + } + pipe.Close() + os.Exit(0) default: fmt.Fprintf(os.Stderr, "Unknown command %q\n", cmd) os.Exit(2) diff --git a/src/os/exec/exec_windows_test.go b/src/os/exec/exec_windows_test.go new file mode 100644 index 0000000000..fbccffec0e --- /dev/null +++ b/src/os/exec/exec_windows_test.go @@ -0,0 +1,43 @@ +// Copyright 2021 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build windows +// +build windows + +package exec_test + +import ( + "io" + "os" + "strconv" + "syscall" + "testing" +) + +func TestPipePassing(t *testing.T) { + r, w, err := os.Pipe() + if err != nil { + t.Error(err) + } + const marker = "arrakis, dune, desert planet" + childProc := helperCommand(t, "pipehandle", strconv.FormatUint(uint64(w.Fd()), 16), marker) + childProc.SysProcAttr = &syscall.SysProcAttr{AdditionalInheritedHandles: []syscall.Handle{syscall.Handle(w.Fd())}} + err = childProc.Start() + if err != nil { + t.Error(err) + } + w.Close() + response, err := io.ReadAll(r) + if err != nil { + t.Error(err) + } + r.Close() + if string(response) != marker { + t.Errorf("got %q; want %q", string(response), marker) + } + err = childProc.Wait() + if err != nil { + t.Error(err) + } +} diff --git a/src/os/file_windows.go b/src/os/file_windows.go index dfc5fc6ce6..0d3c048a75 100644 --- a/src/os/file_windows.go +++ b/src/os/file_windows.go @@ -279,10 +279,11 @@ func rename(oldname, newname string) error { } // Pipe returns a connected pair of Files; reads from r return bytes written to w. -// It returns the files and an error, if any. +// It returns the files and an error, if any. The Windows handles underlying +// the returned files are marked as inheritable by child processes. func Pipe() (r *File, w *File, err error) { var p [2]syscall.Handle - e := syscall.CreatePipe(&p[0], &p[1], nil, 0) + e := syscall.Pipe(p[:]) if e != nil { return nil, nil, NewSyscallError("pipe", e) } |