| 1 |
//go:build windows |
| 2 |
// +build windows |
| 3 |
|
| 4 |
package conpty |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"io" |
| 9 |
"sync" |
| 10 |
"syscall" |
| 11 |
"unsafe" |
| 12 |
|
| 13 |
"github.com/charmbracelet/x/errors" |
| 14 |
"golang.org/x/sys/windows" |
| 15 |
) |
| 16 |
|
| 17 |
// ConPty represents a Windows Console Pseudo-terminal. |
| 18 |
// https://learn.microsoft.com/en-us/windows/console/creating-a-pseudoconsole-session#preparing-the-communication-channels |
| 19 |
type ConPty struct { |
| 20 |
hpc *windows.Handle |
| 21 |
inPipeWrite, inPipeRead windows.Handle |
| 22 |
outPipeWrite, outPipeRead windows.Handle |
| 23 |
attrList *windows.ProcThreadAttributeListContainer |
| 24 |
size windows.Coord |
| 25 |
closeOnce sync.Once |
| 26 |
} |
| 27 |
|
| 28 |
var ( |
| 29 |
_ io.Writer = &ConPty{} |
| 30 |
_ io.Reader = &ConPty{} |
| 31 |
) |
| 32 |
|
| 33 |
// CreatePipes is a helper function to create connected input and output pipes. |
| 34 |
func CreatePipes() (inPipeRead, inPipeWrite, outPipeRead, outPipeWrite uintptr, err error) { |
| 35 |
var inPipeReadHandle, inPipeWriteHandle windows.Handle |
| 36 |
var outPipeReadHandle, outPipeWriteHandle windows.Handle |
| 37 |
pSec := &windows.SecurityAttributes{Length: uint32(unsafe.Sizeof(zeroSec)), InheritHandle: 1} |
| 38 |
|
| 39 |
if err := windows.CreatePipe(&inPipeReadHandle, &inPipeWriteHandle, pSec, 0); err != nil { |
| 40 |
return 0, 0, 0, 0, fmt.Errorf("failed to create input pipes for pseudo console: %w", err) |
| 41 |
} |
| 42 |
|
| 43 |
if err := windows.CreatePipe(&outPipeReadHandle, &outPipeWriteHandle, pSec, 0); err != nil { |
| 44 |
return 0, 0, 0, 0, fmt.Errorf("failed to create output pipes for pseudo console: %w", err) |
| 45 |
} |
| 46 |
|
| 47 |
return uintptr(inPipeReadHandle), uintptr(inPipeWriteHandle), |
| 48 |
uintptr(outPipeReadHandle), uintptr(outPipeWriteHandle), |
| 49 |
nil |
| 50 |
} |
| 51 |
|
| 52 |
// New creates a new ConPty device. |
| 53 |
// Accepts a custom width, height, and flags that will get passed to |
| 54 |
// windows.CreatePseudoConsole. |
| 55 |
func New(w int, h int, flags int) (*ConPty, error) { |
| 56 |
inPipeRead, inPipeWrite, outPipeRead, outPipeWrite, err := CreatePipes() |
| 57 |
if err != nil { |
| 58 |
return nil, fmt.Errorf("failed to create pipes for pseudo console: %w", err) |
| 59 |
} |
| 60 |
|
| 61 |
c, err := NewWithPipes(inPipeRead, inPipeWrite, outPipeRead, outPipeWrite, w, h, flags) |
| 62 |
if err != nil { |
| 63 |
return nil, err |
| 64 |
} |
| 65 |
|
| 66 |
return c, nil |
| 67 |
} |
| 68 |
|
| 69 |
// NewWithPipes creates a new ConPty device with the provided pipe handles. |
| 70 |
// This is useful for when you want to use existing pipes, such as when |
| 71 |
// using a ConPty with a process that has already been created, or when |
| 72 |
// you want to use a ConPty with a specific set of pipes for input and output. |
| 73 |
// |
| 74 |
// The PTY-slave end (input read and output write) of the pipes can be closed |
| 75 |
// after the ConPty is created, as the ConPty will take ownership of the handles |
| 76 |
// and dup them for the new process that will be spawned. The PTY-master end of |
| 77 |
// the pipes will be used to communicate with the pseudo console. |
| 78 |
func NewWithPipes(inPipeRead, inPipeWrite, outPipeRead, outPipeWrite uintptr, w int, h int, flags int) (c *ConPty, err error) { |
| 79 |
if w <= 0 { |
| 80 |
w = DefaultWidth |
| 81 |
} |
| 82 |
if h <= 0 { |
| 83 |
h = DefaultHeight |
| 84 |
} |
| 85 |
|
| 86 |
c = &ConPty{ |
| 87 |
hpc: new(windows.Handle), |
| 88 |
size: windows.Coord{ |
| 89 |
X: int16(w), Y: int16(h), |
| 90 |
}, |
| 91 |
inPipeWrite: windows.Handle(inPipeWrite), |
| 92 |
inPipeRead: windows.Handle(inPipeRead), |
| 93 |
outPipeWrite: windows.Handle(outPipeWrite), |
| 94 |
outPipeRead: windows.Handle(outPipeRead), |
| 95 |
} |
| 96 |
|
| 97 |
if err := windows.CreatePseudoConsole(c.size, windows.Handle(inPipeRead), windows.Handle(outPipeWrite), uint32(flags), c.hpc); err != nil { |
| 98 |
return nil, fmt.Errorf("failed to create pseudo console: %w", err) |
| 99 |
} |
| 100 |
|
| 101 |
// Allocate an attribute list that's large enough to do the operations we care about |
| 102 |
// 1. Pseudo console setup |
| 103 |
c.attrList, err = windows.NewProcThreadAttributeList(1) |
| 104 |
if err != nil { |
| 105 |
return nil, err |
| 106 |
} |
| 107 |
|
| 108 |
if err := c.attrList.Update( |
| 109 |
windows.PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, |
| 110 |
unsafe.Pointer(*c.hpc), |
| 111 |
unsafe.Sizeof(*c.hpc), |
| 112 |
); err != nil { |
| 113 |
return nil, fmt.Errorf("failed to update proc thread attributes for pseudo console: %w", err) |
| 114 |
} |
| 115 |
|
| 116 |
return c, err |
| 117 |
} |
| 118 |
|
| 119 |
// Fd returns the ConPty handle. |
| 120 |
func (p *ConPty) Fd() uintptr { |
| 121 |
return uintptr(*p.hpc) |
| 122 |
} |
| 123 |
|
| 124 |
// Close closes the ConPty device. |
| 125 |
func (p *ConPty) Close() error { |
| 126 |
var err error |
| 127 |
p.closeOnce.Do(func() { |
| 128 |
// Ensure that we have the PTY-end of the pipes closed. |
| 129 |
_ = windows.CloseHandle(p.inPipeRead) |
| 130 |
_ = windows.CloseHandle(p.outPipeWrite) |
| 131 |
if p.attrList != nil { |
| 132 |
p.attrList.Delete() |
| 133 |
} |
| 134 |
windows.ClosePseudoConsole(*p.hpc) |
| 135 |
err = errors.Join( |
| 136 |
windows.CloseHandle(p.inPipeWrite), |
| 137 |
windows.CloseHandle(p.outPipeRead), |
| 138 |
) |
| 139 |
}) |
| 140 |
return err |
| 141 |
} |
| 142 |
|
| 143 |
// InPipeReadFd returns the ConPty input pipe read file descriptor handle. |
| 144 |
func (p *ConPty) InPipeReadFd() uintptr { |
| 145 |
return uintptr(p.inPipeRead) |
| 146 |
} |
| 147 |
|
| 148 |
// InPipeWriteFd returns the ConPty input pipe write file descriptor handle. |
| 149 |
func (p *ConPty) InPipeWriteFd() uintptr { |
| 150 |
return uintptr(p.inPipeWrite) |
| 151 |
} |
| 152 |
|
| 153 |
// OutPipeReadFd returns the ConPty output pipe read file descriptor handle. |
| 154 |
func (p *ConPty) OutPipeReadFd() uintptr { |
| 155 |
return uintptr(p.outPipeRead) |
| 156 |
} |
| 157 |
|
| 158 |
// OutPipeWriteFd returns the ConPty output pipe write file descriptor handle. |
| 159 |
func (p *ConPty) OutPipeWriteFd() uintptr { |
| 160 |
return uintptr(p.outPipeWrite) |
| 161 |
} |
| 162 |
|
| 163 |
// Write safely writes bytes to master end of the ConPty. |
| 164 |
func (c *ConPty) Write(p []byte) (n int, err error) { |
| 165 |
var l uint32 |
| 166 |
err = windows.WriteFile(c.inPipeWrite, p, &l, nil) |
| 167 |
return int(l), err |
| 168 |
} |
| 169 |
|
| 170 |
// Read safely reads bytes from master end of the ConPty. |
| 171 |
func (c *ConPty) Read(p []byte) (n int, err error) { |
| 172 |
var l uint32 |
| 173 |
err = windows.ReadFile(c.outPipeRead, p, &l, nil) |
| 174 |
return int(l), err |
| 175 |
} |
| 176 |
|
| 177 |
// Resize resizes the pseudo-console. |
| 178 |
func (c *ConPty) Resize(w int, h int) error { |
| 179 |
size := windows.Coord{X: int16(w), Y: int16(h)} |
| 180 |
if err := windows.ResizePseudoConsole(*c.hpc, size); err != nil { |
| 181 |
return fmt.Errorf("failed to resize pseudo console: %w", err) |
| 182 |
} |
| 183 |
c.size = size |
| 184 |
return nil |
| 185 |
} |
| 186 |
|
| 187 |
// Size returns the current pseudo-console size. |
| 188 |
func (c *ConPty) Size() (w int, h int, err error) { |
| 189 |
w = int(c.size.X) |
| 190 |
h = int(c.size.Y) |
| 191 |
return w, h, err |
| 192 |
} |
| 193 |
|
| 194 |
var zeroAttr syscall.ProcAttr |
| 195 |
var zeroSec windows.SecurityAttributes |
| 196 |
|
| 197 |
// Spawn spawns a new process attached to the pseudo-console. |
| 198 |
func (c *ConPty) Spawn(name string, args []string, attr *syscall.ProcAttr) (pid int, handle uintptr, err error) { |
| 199 |
if attr == nil { |
| 200 |
attr = &zeroAttr |
| 201 |
} |
| 202 |
|
| 203 |
argv0, err := lookExtensions(name, attr.Dir) |
| 204 |
if err != nil { |
| 205 |
return 0, 0, err |
| 206 |
} |
| 207 |
if len(attr.Dir) != 0 { |
| 208 |
// Windows CreateProcess looks for argv0 relative to the current |
| 209 |
// directory, and, only once the new process is started, it does |
| 210 |
// Chdir(attr.Dir). We are adjusting for that difference here by |
| 211 |
// making argv0 absolute. |
| 212 |
var err error |
| 213 |
argv0, err = joinExeDirAndFName(attr.Dir, argv0) |
| 214 |
if err != nil { |
| 215 |
return 0, 0, err |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
argv0p, err := windows.UTF16PtrFromString(argv0) |
| 220 |
if err != nil { |
| 221 |
return 0, 0, err |
| 222 |
} |
| 223 |
|
| 224 |
var cmdline string |
| 225 |
if attr.Sys != nil && attr.Sys.CmdLine != "" { |
| 226 |
cmdline = attr.Sys.CmdLine |
| 227 |
} else { |
| 228 |
cmdline = windows.ComposeCommandLine(args) |
| 229 |
} |
| 230 |
argvp, err := windows.UTF16PtrFromString(cmdline) |
| 231 |
if err != nil { |
| 232 |
return 0, 0, err |
| 233 |
} |
| 234 |
|
| 235 |
var dirp *uint16 |
| 236 |
if len(attr.Dir) != 0 { |
| 237 |
dirp, err = windows.UTF16PtrFromString(attr.Dir) |
| 238 |
if err != nil { |
| 239 |
return 0, 0, err |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
if attr.Env == nil { |
| 244 |
attr.Env, err = execEnvDefault(attr.Sys) |
| 245 |
if err != nil { |
| 246 |
return 0, 0, err |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
siEx := new(windows.StartupInfoEx) |
| 251 |
siEx.Flags = windows.STARTF_USESTDHANDLES |
| 252 |
|
| 253 |
pi := new(windows.ProcessInformation) |
| 254 |
|
| 255 |
// Need EXTENDED_STARTUPINFO_PRESENT as we're making use of the attribute list field. |
| 256 |
flags := uint32(windows.CREATE_UNICODE_ENVIRONMENT) | windows.EXTENDED_STARTUPINFO_PRESENT |
| 257 |
if attr.Sys != nil && attr.Sys.CreationFlags != 0 { |
| 258 |
flags |= attr.Sys.CreationFlags |
| 259 |
} |
| 260 |
|
| 261 |
pSec := &windows.SecurityAttributes{Length: uint32(unsafe.Sizeof(zeroSec)), InheritHandle: 1} |
| 262 |
if attr.Sys != nil && attr.Sys.ProcessAttributes != nil { |
| 263 |
pSec = &windows.SecurityAttributes{ |
| 264 |
Length: attr.Sys.ProcessAttributes.Length, |
| 265 |
InheritHandle: attr.Sys.ProcessAttributes.InheritHandle, |
| 266 |
} |
| 267 |
} |
| 268 |
tSec := &windows.SecurityAttributes{Length: uint32(unsafe.Sizeof(zeroSec)), InheritHandle: 1} |
| 269 |
if attr.Sys != nil && attr.Sys.ThreadAttributes != nil { |
| 270 |
tSec = &windows.SecurityAttributes{ |
| 271 |
Length: attr.Sys.ThreadAttributes.Length, |
| 272 |
InheritHandle: attr.Sys.ThreadAttributes.InheritHandle, |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
siEx.ProcThreadAttributeList = c.attrList.List() //nolint:govet // unusedwrite: ProcThreadAttributeList will be read in syscall |
| 277 |
siEx.Cb = uint32(unsafe.Sizeof(*siEx)) |
| 278 |
if attr.Sys != nil && attr.Sys.Token != 0 { |
| 279 |
err = windows.CreateProcessAsUser( |
| 280 |
windows.Token(attr.Sys.Token), |
| 281 |
argv0p, |
| 282 |
argvp, |
| 283 |
pSec, |
| 284 |
tSec, |
| 285 |
false, |
| 286 |
flags, |
| 287 |
createEnvBlock(addCriticalEnv(dedupEnvCase(true, attr.Env))), |
| 288 |
dirp, |
| 289 |
&siEx.StartupInfo, |
| 290 |
pi, |
| 291 |
) |
| 292 |
} else { |
| 293 |
err = windows.CreateProcess( |
| 294 |
argv0p, |
| 295 |
argvp, |
| 296 |
pSec, |
| 297 |
tSec, |
| 298 |
false, |
| 299 |
flags, |
| 300 |
createEnvBlock(addCriticalEnv(dedupEnvCase(true, attr.Env))), |
| 301 |
dirp, |
| 302 |
&siEx.StartupInfo, |
| 303 |
pi, |
| 304 |
) |
| 305 |
} |
| 306 |
if err != nil { |
| 307 |
return 0, 0, fmt.Errorf("failed to create process: %w", err) |
| 308 |
} |
| 309 |
|
| 310 |
defer windows.CloseHandle(pi.Thread) |
| 311 |
|
| 312 |
return int(pi.ProcessId), uintptr(pi.Process), nil |
| 313 |
} |