|
|
|
@ -9,6 +9,8 @@ package windows
|
|
|
|
|
import (
|
|
|
|
|
errorspkg "errors"
|
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
|
|
"golang.org/x/sys/internal/unsafeheader"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// EscapeArg rewrites command line argument s as prescribed
|
|
|
|
@ -78,6 +80,40 @@ func EscapeArg(s string) string {
|
|
|
|
|
return string(qs[:j])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ComposeCommandLine escapes and joins the given arguments suitable for use as a Windows command line,
|
|
|
|
|
// in CreateProcess's CommandLine argument, CreateService/ChangeServiceConfig's BinaryPathName argument,
|
|
|
|
|
// or any program that uses CommandLineToArgv.
|
|
|
|
|
func ComposeCommandLine(args []string) string {
|
|
|
|
|
var commandLine string
|
|
|
|
|
for i := range args {
|
|
|
|
|
if i > 0 {
|
|
|
|
|
commandLine += " "
|
|
|
|
|
}
|
|
|
|
|
commandLine += EscapeArg(args[i])
|
|
|
|
|
}
|
|
|
|
|
return commandLine
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DecomposeCommandLine breaks apart its argument command line into unescaped parts using CommandLineToArgv,
|
|
|
|
|
// as gathered from GetCommandLine, QUERY_SERVICE_CONFIG's BinaryPathName argument, or elsewhere that
|
|
|
|
|
// command lines are passed around.
|
|
|
|
|
func DecomposeCommandLine(commandLine string) ([]string, error) {
|
|
|
|
|
if len(commandLine) == 0 {
|
|
|
|
|
return []string{}, nil
|
|
|
|
|
}
|
|
|
|
|
var argc int32
|
|
|
|
|
argv, err := CommandLineToArgv(StringToUTF16Ptr(commandLine), &argc)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer LocalFree(Handle(unsafe.Pointer(argv)))
|
|
|
|
|
var args []string
|
|
|
|
|
for _, v := range (*argv)[:argc] {
|
|
|
|
|
args = append(args, UTF16ToString((*v)[:]))
|
|
|
|
|
}
|
|
|
|
|
return args, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func CloseOnExec(fd Handle) {
|
|
|
|
|
SetHandleInformation(Handle(fd), HANDLE_FLAG_INHERIT, 0)
|
|
|
|
|
}
|
|
|
|
@ -101,8 +137,8 @@ func FullPath(name string) (path string, err error) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewProcThreadAttributeList allocates a new ProcThreadAttributeList, with the requested maximum number of attributes.
|
|
|
|
|
func NewProcThreadAttributeList(maxAttrCount uint32) (*ProcThreadAttributeList, error) {
|
|
|
|
|
// NewProcThreadAttributeList allocates a new ProcThreadAttributeListContainer, with the requested maximum number of attributes.
|
|
|
|
|
func NewProcThreadAttributeList(maxAttrCount uint32) (*ProcThreadAttributeListContainer, error) {
|
|
|
|
|
var size uintptr
|
|
|
|
|
err := initializeProcThreadAttributeList(nil, maxAttrCount, 0, &size)
|
|
|
|
|
if err != ERROR_INSUFFICIENT_BUFFER {
|
|
|
|
@ -111,10 +147,9 @@ func NewProcThreadAttributeList(maxAttrCount uint32) (*ProcThreadAttributeList,
|
|
|
|
|
}
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
const psize = unsafe.Sizeof(uintptr(0))
|
|
|
|
|
// size is guaranteed to be ≥1 by InitializeProcThreadAttributeList.
|
|
|
|
|
al := (*ProcThreadAttributeList)(unsafe.Pointer(&make([]unsafe.Pointer, (size+psize-1)/psize)[0]))
|
|
|
|
|
err = initializeProcThreadAttributeList(al, maxAttrCount, 0, &size)
|
|
|
|
|
al := &ProcThreadAttributeListContainer{data: (*ProcThreadAttributeList)(unsafe.Pointer(&make([]byte, size)[0]))}
|
|
|
|
|
err = initializeProcThreadAttributeList(al.data, maxAttrCount, 0, &size)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
@ -122,11 +157,39 @@ func NewProcThreadAttributeList(maxAttrCount uint32) (*ProcThreadAttributeList,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update modifies the ProcThreadAttributeList using UpdateProcThreadAttribute.
|
|
|
|
|
func (al *ProcThreadAttributeList) Update(attribute uintptr, flags uint32, value unsafe.Pointer, size uintptr, prevValue unsafe.Pointer, returnedSize *uintptr) error {
|
|
|
|
|
return updateProcThreadAttribute(al, flags, attribute, value, size, prevValue, returnedSize)
|
|
|
|
|
// Note that the value passed to this function will be copied into memory
|
|
|
|
|
// allocated by LocalAlloc, the contents of which should not contain any
|
|
|
|
|
// Go-managed pointers, even if the passed value itself is a Go-managed
|
|
|
|
|
// pointer.
|
|
|
|
|
func (al *ProcThreadAttributeListContainer) Update(attribute uintptr, value unsafe.Pointer, size uintptr) error {
|
|
|
|
|
alloc, err := LocalAlloc(LMEM_FIXED, uint32(size))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
var src, dst []byte
|
|
|
|
|
hdr := (*unsafeheader.Slice)(unsafe.Pointer(&src))
|
|
|
|
|
hdr.Data = value
|
|
|
|
|
hdr.Cap = int(size)
|
|
|
|
|
hdr.Len = int(size)
|
|
|
|
|
hdr = (*unsafeheader.Slice)(unsafe.Pointer(&dst))
|
|
|
|
|
hdr.Data = unsafe.Pointer(alloc)
|
|
|
|
|
hdr.Cap = int(size)
|
|
|
|
|
hdr.Len = int(size)
|
|
|
|
|
copy(dst, src)
|
|
|
|
|
al.heapAllocations = append(al.heapAllocations, alloc)
|
|
|
|
|
return updateProcThreadAttribute(al.data, 0, attribute, unsafe.Pointer(alloc), size, nil, nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Delete frees ProcThreadAttributeList's resources.
|
|
|
|
|
func (al *ProcThreadAttributeList) Delete() {
|
|
|
|
|
deleteProcThreadAttributeList(al)
|
|
|
|
|
func (al *ProcThreadAttributeListContainer) Delete() {
|
|
|
|
|
deleteProcThreadAttributeList(al.data)
|
|
|
|
|
for i := range al.heapAllocations {
|
|
|
|
|
LocalFree(Handle(al.heapAllocations[i]))
|
|
|
|
|
}
|
|
|
|
|
al.heapAllocations = nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// List returns the actual ProcThreadAttributeList to be passed to StartupInfoEx.
|
|
|
|
|
func (al *ProcThreadAttributeListContainer) List() *ProcThreadAttributeList {
|
|
|
|
|
return al.data
|
|
|
|
|
}
|
|
|
|
|