Skip to content

Commit

Permalink
syscall: add SysProcAttr.PseudoConsole on Windows
Browse files Browse the repository at this point in the history
This allows the user to pass a ConPty handle to run a process on a
ConPty session.

See: https://learn.microsoft.com/en-us/windows/console/creating-a-pseudoconsole-session
Fixes: golang#62708
  • Loading branch information
aymanbagabas committed Sep 18, 2023
1 parent c631297 commit 914b398
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/syscall/exec_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ type SysProcAttr struct {
NoInheritHandles bool // if set, no handles are inherited by the new process, not even the standard handles, contained in ProcAttr.Files, nor the ones contained in AdditionalInheritedHandles
AdditionalInheritedHandles []Handle // a list of additional handles, already marked as inheritable, that will be inherited by the new process
ParentProcess Handle // if non-zero, the new process regards the process given by this handle as its parent process, and AdditionalInheritedHandles, if set, should exist in this parent process
PseudoConsole Handle // if non-zero, the new process will be attached to the console represented by this handle, any AdditionalInheritedHandles will be ignored, this implies NoInheritHandles
}

var zeroProcAttr ProcAttr
Expand Down Expand Up @@ -371,9 +372,13 @@ func StartProcess(argv0 string, argv []string, attr *ProcAttr) (pid int, handle
return 0, 0, err
}
}
si.StdInput = fd[0]
si.StdOutput = fd[1]
si.StdErr = fd[2]

// If a PseudoConsole is specified, then there is nothing we need to do with the handles since the process will inherit the other end of the PseudoConsole.
if sys.PseudoConsole == 0 {
si.StdInput = fd[0]
si.StdOutput = fd[1]
si.StdErr = fd[2]
}

fd = append(fd, sys.AdditionalInheritedHandles...)

Expand All @@ -396,7 +401,7 @@ func StartProcess(argv0 string, argv []string, attr *ProcAttr) (pid int, handle
}
fd = fd[:j]

willInheritHandles := len(fd) > 0 && !sys.NoInheritHandles
willInheritHandles := len(fd) > 0 && !sys.NoInheritHandles && sys.PseudoConsole == 0

// Do not accidentally inherit more than these handles.
if willInheritHandles {
Expand All @@ -406,6 +411,13 @@ func StartProcess(argv0 string, argv []string, attr *ProcAttr) (pid int, handle
}
}

if sys.PseudoConsole != 0 {
err = updateProcThreadAttribute(si.ProcThreadAttributeList, 0, _PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, unsafe.Pointer(sys.PseudoConsole), unsafe.Sizeof(sys.PseudoConsole), nil, nil)
if err != nil {
return 0, 0, err
}
}

envBlock, err := createEnvBlock(attr.Env)
if err != nil {
return 0, 0, err
Expand Down
1 change: 1 addition & 0 deletions src/syscall/types_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ type _PROC_THREAD_ATTRIBUTE_LIST struct {
const (
_PROC_THREAD_ATTRIBUTE_PARENT_PROCESS = 0x00020000
_PROC_THREAD_ATTRIBUTE_HANDLE_LIST = 0x00020002
_PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE = 0x00020016
)

type _STARTUPINFOEXW struct {
Expand Down

0 comments on commit 914b398

Please sign in to comment.