diff options
| author | Jason A. Donenfeld <Jason@zx2c4.com> | 2021-01-31 18:14:56 +0100 |
|---|---|---|
| committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2021-02-26 18:27:57 +0000 |
| commit | 19f96e73bf655764b57424cc9e00657f364ffb89 (patch) | |
| tree | d065c1d1667696bb65b6e7e9a7e6a401ea14ccf2 /src/syscall/exec_windows_test.go | |
| parent | 3146166baa8c420dfe20619e4aa9978b87927268 (diff) | |
| download | go-19f96e73bf655764b57424cc9e00657f364ffb89.tar.xz | |
syscall: introduce SysProcAttr.ParentProcess on Windows
This allows users to specify which process should be used as the parent
process when creating a new process.
Note that this doesn't just trivially pass the handle onward to
PROC_THREAD_ATTRIBUTE_PARENT_PROCESS, because inherited handles must be
valid in the parent process, so if we're changing the destination
process, then we must also change the origin of the parent handles. And,
the StartProcess function must clean up these handles successfully when
exiting, regardless of where the duplication happened. So, we take care
in this commit to use DuplicateHandle for both duplicating and for
closing the inherited handles.
The test was taken originally from CL 288272 and adjusted for use here.
Fixes #44011.
Change-Id: Ib3b132028dcab1aded3dc0e65126c8abebfa35eb
Reviewed-on: https://go-review.googlesource.com/c/go/+/288300
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: Alex Brainman <alex.brainman@gmail.com>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Diffstat (limited to 'src/syscall/exec_windows_test.go')
| -rw-r--r-- | src/syscall/exec_windows_test.go | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/syscall/exec_windows_test.go b/src/syscall/exec_windows_test.go index eda1d36877..8a1c2ceaae 100644 --- a/src/syscall/exec_windows_test.go +++ b/src/syscall/exec_windows_test.go @@ -5,8 +5,14 @@ package syscall_test import ( + "fmt" + "io/ioutil" + "os" + "os/exec" + "path/filepath" "syscall" "testing" + "time" ) func TestEscapeArg(t *testing.T) { @@ -41,3 +47,70 @@ func TestEscapeArg(t *testing.T) { } } } + +func TestChangingProcessParent(t *testing.T) { + if os.Getenv("GO_WANT_HELPER_PROCESS") == "parent" { + // in parent process + + // Parent does nothign. It is just used as a parent of a child process. + time.Sleep(time.Minute) + os.Exit(0) + } + + if os.Getenv("GO_WANT_HELPER_PROCESS") == "child" { + // in child process + dumpPath := os.Getenv("GO_WANT_HELPER_PROCESS_FILE") + if dumpPath == "" { + fmt.Fprintf(os.Stderr, "Dump file path cannot be blank.") + os.Exit(1) + } + err := os.WriteFile(dumpPath, []byte(fmt.Sprintf("%d", os.Getppid())), 0644) + if err != nil { + fmt.Fprintf(os.Stderr, "Error writing dump file: %v", err) + os.Exit(2) + } + os.Exit(0) + } + + // run parent process + + parent := exec.Command(os.Args[0], "-test.run=TestChangingProcessParent") + parent.Env = append(os.Environ(), "GO_WANT_HELPER_PROCESS=parent") + err := parent.Start() + if err != nil { + t.Fatal(err) + } + defer func() { + parent.Process.Kill() + parent.Wait() + }() + + // run child process + + const _PROCESS_CREATE_PROCESS = 0x0080 + const _PROCESS_DUP_HANDLE = 0x0040 + childDumpPath := filepath.Join(t.TempDir(), "ppid.txt") + ph, err := syscall.OpenProcess(_PROCESS_CREATE_PROCESS|_PROCESS_DUP_HANDLE|syscall.PROCESS_QUERY_INFORMATION, + false, uint32(parent.Process.Pid)) + if err != nil { + t.Fatal(err) + } + defer syscall.CloseHandle(ph) + + child := exec.Command(os.Args[0], "-test.run=TestChangingProcessParent") + child.Env = append(os.Environ(), + "GO_WANT_HELPER_PROCESS=child", + "GO_WANT_HELPER_PROCESS_FILE="+childDumpPath) + child.SysProcAttr = &syscall.SysProcAttr{ParentProcess: ph} + childOutput, err := child.CombinedOutput() + if err != nil { + t.Errorf("child failed: %v: %v", err, string(childOutput)) + } + childOutput, err = ioutil.ReadFile(childDumpPath) + if err != nil { + t.Fatalf("reading child ouput failed: %v", err) + } + if got, want := string(childOutput), fmt.Sprintf("%d", parent.Process.Pid); got != want { + t.Fatalf("child output: want %q, got %q", want, got) + } +} |
