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