Skip to content

Commit

Permalink
Change to x/sys/windows for /internal/exec package
Browse files Browse the repository at this point in the history
x/sys/windows has the Proc thread attribute work we need already, so swap
to this instead of our own definitions.

Signed-off-by: Daniel Canter <dcanter@microsoft.com>
  • Loading branch information
dcantah committed Dec 16, 2021
1 parent 6861cd6 commit 49f270a
Show file tree
Hide file tree
Showing 345 changed files with 25,032 additions and 16,944 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ require (
go.opencensus.io v0.22.3
golang.org/x/net v0.0.0-20210825183410-e898025ed96a // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/sys v0.0.0-20210510120138-977fb7262007
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e
google.golang.org/grpc v1.40.0
)

Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -814,8 +814,9 @@ golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210426230700-d19ff857e887/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
12 changes: 4 additions & 8 deletions internal/conpty/conpty.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,22 @@ func New(width, height int16, flags uint32) (*ConPTY, error) {

// UpdateProcThreadAttribute updates the passed in attribute list to contain the entry necessary for use with
// CreateProcess.
func (c *ConPTY) UpdateProcThreadAttribute(attributeList *winapi.ProcThreadAttributeList) error {
func (c *ConPTY) UpdateProcThreadAttribute(attrList *windows.ProcThreadAttributeListContainer) error {
c.handleLock.RLock()
defer c.handleLock.RUnlock()

if c.hpc == 0 {
return errClosedConPty
}

err := winapi.UpdateProcThreadAttribute(
attributeList,
0,
if err := attrList.Update(
winapi.PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE,
unsafe.Pointer(c.hpc),
unsafe.Sizeof(c.hpc),
nil,
nil,
)
if err != nil {
); err != nil {
return fmt.Errorf("failed to update proc thread attributes for pseudo console: %w", err)
}

return nil
}

Expand Down
23 changes: 9 additions & 14 deletions internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"unicode/utf16"
"unsafe"

"github.com/Microsoft/hcsshim/internal/winapi"
"golang.org/x/sys/windows"
)

Expand All @@ -27,7 +26,7 @@ type Exec struct {
waitCalled bool
stdioOurEnd [3]*os.File
stdioProcEnd [3]*os.File
attrList *winapi.ProcThreadAttributeList
attrList *windows.ProcThreadAttributeListContainer
*execConfig
}

Expand Down Expand Up @@ -94,7 +93,7 @@ func (e *Exec) Start() error {
}
}

siEx := new(winapi.StartupInfoEx)
siEx := new(windows.StartupInfoEx)
siEx.Flags = windows.STARTF_USESTDHANDLES
pi := new(windows.ProcessInformation)

Expand All @@ -106,11 +105,10 @@ func (e *Exec) Start() error {
// 2. Pseudo console setup if one was requested.
// 3. Inherit only stdio handles if ones were requested.
// Therefore we need a list of size 3.
e.attrList, err = winapi.NewProcThreadAttributeList(3)
e.attrList, err = windows.NewProcThreadAttributeList(3)
if err != nil {
return fmt.Errorf("failed to initialize process thread attribute list: %w", err)
}
siEx.ProcThreadAttributeList = e.attrList

// Need to know whether the process needs to inherit stdio handles. The below setup is so that we only inherit the
// stdio pipes and nothing else into the new process.
Expand All @@ -124,14 +122,10 @@ func (e *Exec) Start() error {
}

// Set up the process to only inherit stdio handles and nothing else.
err = winapi.UpdateProcThreadAttribute(
siEx.ProcThreadAttributeList,
0,
err := e.attrList.Update(
windows.PROC_THREAD_ATTRIBUTE_HANDLE_LIST,
unsafe.Pointer(&handles[0]),
uintptr(len(handles))*unsafe.Sizeof(handles[0]),
nil,
nil,
)
if err != nil {
return err
Expand All @@ -150,13 +144,13 @@ func (e *Exec) Start() error {
}

if e.job != nil {
if err := e.job.UpdateProcThreadAttribute(siEx.ProcThreadAttributeList); err != nil {
if err := e.job.UpdateProcThreadAttribute(e.attrList); err != nil {
return err
}
}

if e.cpty != nil {
if err := e.cpty.UpdateProcThreadAttribute(siEx.ProcThreadAttributeList); err != nil {
if err := e.cpty.UpdateProcThreadAttribute(e.attrList); err != nil {
return err
}
}
Expand All @@ -165,9 +159,10 @@ func (e *Exec) Start() error {
pSec := &windows.SecurityAttributes{Length: uint32(unsafe.Sizeof(zeroSec)), InheritHandle: 1}
tSec := &windows.SecurityAttributes{Length: uint32(unsafe.Sizeof(zeroSec)), InheritHandle: 1}

siEx.ProcThreadAttributeList = e.attrList.List()
siEx.Cb = uint32(unsafe.Sizeof(*siEx))
if e.execConfig.token != 0 {
err = winapi.CreateProcessAsUser(
err = windows.CreateProcessAsUser(
e.execConfig.token,
argv0p,
argvp,
Expand Down Expand Up @@ -227,7 +222,7 @@ func (e *Exec) Close() error {
if e.procState == nil {
return errProcNotFinished
}
winapi.DeleteProcThreadAttributeList(e.attrList)
e.attrList.Delete()
e.closeStdio()
return nil
}
Expand Down
12 changes: 4 additions & 8 deletions internal/jobobject/jobobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,26 +241,22 @@ func (job *JobObject) PollNotification() (interface{}, error) {
// UpdateProcThreadAttribute updates the passed in ProcThreadAttributeList to contain what is necessary to
// launch a process in a job at creation time. This can be used to avoid having to call Assign() after a process
// has already started running.
func (job *JobObject) UpdateProcThreadAttribute(attrList *winapi.ProcThreadAttributeList) error {
func (job *JobObject) UpdateProcThreadAttribute(attrList *windows.ProcThreadAttributeListContainer) error {
job.handleLock.RLock()
defer job.handleLock.RUnlock()

if job.handle == 0 {
return ErrAlreadyClosed
}

err := winapi.UpdateProcThreadAttribute(
attrList,
0,
if err := attrList.Update(
winapi.PROC_THREAD_ATTRIBUTE_JOB_LIST,
unsafe.Pointer(&job.handle),
unsafe.Sizeof(job.handle),
nil,
nil,
)
if err != nil {
); err != nil {
return fmt.Errorf("failed to update proc thread attributes for job object: %w", err)
}

return nil
}

Expand Down
75 changes: 0 additions & 75 deletions internal/winapi/process.go
Original file line number Diff line number Diff line change
@@ -1,83 +1,8 @@
package winapi

import (
"errors"
"unsafe"

"golang.org/x/sys/windows"
)

const PROCESS_ALL_ACCESS uint32 = 2097151

const (
PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE = 0x20016
PROC_THREAD_ATTRIBUTE_JOB_LIST = 0x2000D
)

type ProcThreadAttributeList struct {
_ [1]byte
}

// typedef struct _STARTUPINFOEXW {
// STARTUPINFOW StartupInfo;
// LPPROC_THREAD_ATTRIBUTE_LIST lpAttributeList;
// } STARTUPINFOEXW, *LPSTARTUPINFOEXW;
type StartupInfoEx struct {
// This is a recreation of the same binding from the stdlib. The x/sys/windows variant for whatever reason
// doesn't work when updating the list for the pseudo console attribute. It has the process immediately exit
// with exit code 0xc0000142 shortly after start.
//
// TODO (dcantah): Swap to the x/sys/windows definitions after https://go-review.googlesource.com/c/sys/+/371276/1/windows/exec_windows.go#153
// gets in.
windows.StartupInfo
ProcThreadAttributeList *ProcThreadAttributeList
}

// NewProcThreadAttributeList allocates a new ProcThreadAttributeList, with
// the requested maximum number of attributes. This must be cleaned up by calling
// DeleteProcThreadAttributeList.
func NewProcThreadAttributeList(maxAttrCount uint32) (*ProcThreadAttributeList, error) {
var size uintptr
err := initializeProcThreadAttributeList(nil, maxAttrCount, 0, &size)
if err != windows.ERROR_INSUFFICIENT_BUFFER {
if err == nil {
return nil, errors.New("unable to query buffer size from InitializeProcThreadAttributeList")
}
return nil, err
}
al := (*ProcThreadAttributeList)(unsafe.Pointer(&make([]byte, size)[0]))
err = initializeProcThreadAttributeList(al, maxAttrCount, 0, &size)
if err != nil {
return nil, err
}
return al, nil
}

// BOOL InitializeProcThreadAttributeList(
// [out, optional] LPPROC_THREAD_ATTRIBUTE_LIST lpAttributeList,
// [in] DWORD dwAttributeCount,
// DWORD dwFlags,
// [in, out] PSIZE_T lpSize
// );
//
//sys initializeProcThreadAttributeList(lpAttributeList *ProcThreadAttributeList, dwAttributeCount uint32, dwFlags uint32, lpSize *uintptr) (err error) = kernel32.InitializeProcThreadAttributeList

// void DeleteProcThreadAttributeList(
// [in, out] LPPROC_THREAD_ATTRIBUTE_LIST lpAttributeList
// );
//
//sys DeleteProcThreadAttributeList(lpAttributeList *ProcThreadAttributeList) = kernel32.DeleteProcThreadAttributeList

// BOOL UpdateProcThreadAttribute(
// [in, out] LPPROC_THREAD_ATTRIBUTE_LIST lpAttributeList,
// [in] DWORD dwFlags,
// [in] DWORD_PTR Attribute,
// [in] PVOID lpValue,
// [in] SIZE_T cbSize,
// [out, optional] PVOID lpPreviousValue,
// [in, optional] PSIZE_T lpReturnSize
// );
//
//sys UpdateProcThreadAttribute(lpAttributeList *ProcThreadAttributeList, dwFlags uint32, attribute uintptr, lpValue unsafe.Pointer, cbSize uintptr, lpPreviousValue unsafe.Pointer, lpReturnSize *uintptr) (err error) = kernel32.UpdateProcThreadAttribute

//sys CreateProcessAsUser(token windows.Token, appName *uint16, commandLine *uint16, procSecurity *windows.SecurityAttributes, threadSecurity *windows.SecurityAttributes, inheritHandles bool, creationFlags uint32, env *uint16, currentDir *uint16, startupInfo *windows.StartupInfo, outProcInfo *windows.ProcessInformation) (err error) = advapi32.CreateProcessAsUserW
51 changes: 0 additions & 51 deletions internal/winapi/zsyscall_windows.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.8.1
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/sys v0.0.0-20210510120138-977fb7262007
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e
google.golang.org/grpc v1.40.0
k8s.io/cri-api v0.20.6
)
Expand Down
3 changes: 2 additions & 1 deletion test/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -776,8 +776,9 @@ golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210426230700-d19ff857e887/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
2 changes: 1 addition & 1 deletion test/vendor/github.com/Microsoft/hcsshim/go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion test/vendor/github.com/Microsoft/hcsshim/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 49f270a

Please sign in to comment.