forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/golang.org/x/sys/windows/exec_windows.go

459 lines
7.5 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
// Copyright 2009 The Go Authors. All rights reserved.
2024-02-18 10:42:21 +00:00
// Use of this source code is governed by a BSD-style
2024-02-18 10:42:21 +00:00
// license that can be found in the LICENSE file.
// Fork, exec, wait, etc.
package windows
import (
errorspkg "errors"
"unsafe"
)
// EscapeArg rewrites command line argument s as prescribed
2024-02-18 10:42:21 +00:00
// in http://msdn.microsoft.com/en-us/library/ms880421.
2024-02-18 10:42:21 +00:00
// This function returns "" (2 double quotes) if s is empty.
2024-02-18 10:42:21 +00:00
// Alternatively, these transformations are done:
2024-02-18 10:42:21 +00:00
// - every back slash (\) is doubled, but only if immediately
2024-02-18 10:42:21 +00:00
// followed by double quote (");
2024-02-18 10:42:21 +00:00
// - every double quote (") is escaped by back slash (\);
2024-02-18 10:42:21 +00:00
// - finally, s is wrapped with double quotes (arg -> "arg"),
2024-02-18 10:42:21 +00:00
// but only if there is space or tab inside s.
2024-02-18 10:42:21 +00:00
func EscapeArg(s string) string {
2024-02-18 10:42:21 +00:00
if len(s) == 0 {
2024-02-18 10:42:21 +00:00
return `""`
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
n := len(s)
2024-02-18 10:42:21 +00:00
hasSpace := false
2024-02-18 10:42:21 +00:00
for i := 0; i < len(s); i++ {
2024-02-18 10:42:21 +00:00
switch s[i] {
2024-02-18 10:42:21 +00:00
case '"', '\\':
2024-02-18 10:42:21 +00:00
n++
2024-02-18 10:42:21 +00:00
case ' ', '\t':
2024-02-18 10:42:21 +00:00
hasSpace = true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if hasSpace {
2024-02-18 10:42:21 +00:00
n += 2 // Reserve space for quotes.
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if n == len(s) {
2024-02-18 10:42:21 +00:00
return s
2024-02-18 10:42:21 +00:00
}
qs := make([]byte, n)
2024-02-18 10:42:21 +00:00
j := 0
2024-02-18 10:42:21 +00:00
if hasSpace {
2024-02-18 10:42:21 +00:00
qs[j] = '"'
2024-02-18 10:42:21 +00:00
j++
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
slashes := 0
2024-02-18 10:42:21 +00:00
for i := 0; i < len(s); i++ {
2024-02-18 10:42:21 +00:00
switch s[i] {
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
slashes = 0
2024-02-18 10:42:21 +00:00
qs[j] = s[i]
2024-02-18 10:42:21 +00:00
case '\\':
2024-02-18 10:42:21 +00:00
slashes++
2024-02-18 10:42:21 +00:00
qs[j] = s[i]
2024-02-18 10:42:21 +00:00
case '"':
2024-02-18 10:42:21 +00:00
for ; slashes > 0; slashes-- {
2024-02-18 10:42:21 +00:00
qs[j] = '\\'
2024-02-18 10:42:21 +00:00
j++
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
qs[j] = '\\'
2024-02-18 10:42:21 +00:00
j++
2024-02-18 10:42:21 +00:00
qs[j] = s[i]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
j++
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if hasSpace {
2024-02-18 10:42:21 +00:00
for ; slashes > 0; slashes-- {
2024-02-18 10:42:21 +00:00
qs[j] = '\\'
2024-02-18 10:42:21 +00:00
j++
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
qs[j] = '"'
2024-02-18 10:42:21 +00:00
j++
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return string(qs[:j])
2024-02-18 10:42:21 +00:00
}
// ComposeCommandLine escapes and joins the given arguments suitable for use as a Windows command line,
2024-02-18 10:42:21 +00:00
// in CreateProcess's CommandLine argument, CreateService/ChangeServiceConfig's BinaryPathName argument,
2024-02-18 10:42:21 +00:00
// or any program that uses CommandLineToArgv.
2024-02-18 10:42:21 +00:00
func ComposeCommandLine(args []string) string {
2024-02-18 10:42:21 +00:00
if len(args) == 0 {
2024-02-18 10:42:21 +00:00
return ""
2024-02-18 10:42:21 +00:00
}
// Per https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-commandlinetoargvw:
2024-02-18 10:42:21 +00:00
// “This function accepts command lines that contain a program name; the
2024-02-18 10:42:21 +00:00
// program name can be enclosed in quotation marks or not.”
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Unfortunately, it provides no means of escaping interior quotation marks
2024-02-18 10:42:21 +00:00
// within that program name, and we have no way to report them here.
2024-02-18 10:42:21 +00:00
prog := args[0]
2024-02-18 10:42:21 +00:00
mustQuote := len(prog) == 0
2024-02-18 10:42:21 +00:00
for i := 0; i < len(prog); i++ {
2024-02-18 10:42:21 +00:00
c := prog[i]
2024-02-18 10:42:21 +00:00
if c <= ' ' || (c == '"' && i == 0) {
2024-02-18 10:42:21 +00:00
// Force quotes for not only the ASCII space and tab as described in the
2024-02-18 10:42:21 +00:00
// MSDN article, but also ASCII control characters.
2024-02-18 10:42:21 +00:00
// The documentation for CommandLineToArgvW doesn't say what happens when
2024-02-18 10:42:21 +00:00
// the first argument is not a valid program name, but it empirically
2024-02-18 10:42:21 +00:00
// seems to drop unquoted control characters.
2024-02-18 10:42:21 +00:00
mustQuote = true
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
var commandLine []byte
2024-02-18 10:42:21 +00:00
if mustQuote {
2024-02-18 10:42:21 +00:00
commandLine = make([]byte, 0, len(prog)+2)
2024-02-18 10:42:21 +00:00
commandLine = append(commandLine, '"')
2024-02-18 10:42:21 +00:00
for i := 0; i < len(prog); i++ {
2024-02-18 10:42:21 +00:00
c := prog[i]
2024-02-18 10:42:21 +00:00
if c == '"' {
2024-02-18 10:42:21 +00:00
// This quote would interfere with our surrounding quotes.
2024-02-18 10:42:21 +00:00
// We have no way to report an error, so just strip out
2024-02-18 10:42:21 +00:00
// the offending character instead.
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
commandLine = append(commandLine, c)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
commandLine = append(commandLine, '"')
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
if len(args) == 1 {
2024-02-18 10:42:21 +00:00
// args[0] is a valid command line representing itself.
2024-02-18 10:42:21 +00:00
// No need to allocate a new slice or string for it.
2024-02-18 10:42:21 +00:00
return prog
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
commandLine = []byte(prog)
2024-02-18 10:42:21 +00:00
}
for _, arg := range args[1:] {
2024-02-18 10:42:21 +00:00
commandLine = append(commandLine, ' ')
2024-02-18 10:42:21 +00:00
// TODO(bcmills): since we're already appending to a slice, it would be nice
2024-02-18 10:42:21 +00:00
// to avoid the intermediate allocations of EscapeArg.
2024-02-18 10:42:21 +00:00
// Perhaps we can factor out an appendEscapedArg function.
2024-02-18 10:42:21 +00:00
commandLine = append(commandLine, EscapeArg(arg)...)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return string(commandLine)
2024-02-18 10:42:21 +00:00
}
// DecomposeCommandLine breaks apart its argument command line into unescaped parts using CommandLineToArgv,
2024-02-18 10:42:21 +00:00
// as gathered from GetCommandLine, QUERY_SERVICE_CONFIG's BinaryPathName argument, or elsewhere that
2024-02-18 10:42:21 +00:00
// command lines are passed around.
2024-02-18 10:42:21 +00:00
// DecomposeCommandLine returns an error if commandLine contains NUL.
2024-02-18 10:42:21 +00:00
func DecomposeCommandLine(commandLine string) ([]string, error) {
2024-02-18 10:42:21 +00:00
if len(commandLine) == 0 {
2024-02-18 10:42:21 +00:00
return []string{}, nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
utf16CommandLine, err := UTF16FromString(commandLine)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, errorspkg.New("string with NUL passed to DecomposeCommandLine")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
var argc int32
2024-02-18 10:42:21 +00:00
argv, err := commandLineToArgv(&utf16CommandLine[0], &argc)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
defer LocalFree(Handle(unsafe.Pointer(argv)))
var args []string
2024-02-18 10:42:21 +00:00
for _, p := range unsafe.Slice(argv, argc) {
2024-02-18 10:42:21 +00:00
args = append(args, UTF16PtrToString(p))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return args, nil
2024-02-18 10:42:21 +00:00
}
// CommandLineToArgv parses a Unicode command line string and sets
2024-02-18 10:42:21 +00:00
// argc to the number of parsed arguments.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The returned memory should be freed using a single call to LocalFree.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Note that although the return type of CommandLineToArgv indicates 8192
2024-02-18 10:42:21 +00:00
// entries of up to 8192 characters each, the actual count of parsed arguments
2024-02-18 10:42:21 +00:00
// may exceed 8192, and the documentation for CommandLineToArgvW does not mention
2024-02-18 10:42:21 +00:00
// any bound on the lengths of the individual argument strings.
2024-02-18 10:42:21 +00:00
// (See https://go.dev/issue/63236.)
2024-02-18 10:42:21 +00:00
func CommandLineToArgv(cmd *uint16, argc *int32) (argv *[8192]*[8192]uint16, err error) {
2024-02-18 10:42:21 +00:00
argp, err := commandLineToArgv(cmd, argc)
2024-02-18 10:42:21 +00:00
argv = (*[8192]*[8192]uint16)(unsafe.Pointer(argp))
2024-02-18 10:42:21 +00:00
return argv, err
2024-02-18 10:42:21 +00:00
}
func CloseOnExec(fd Handle) {
2024-02-18 10:42:21 +00:00
SetHandleInformation(Handle(fd), HANDLE_FLAG_INHERIT, 0)
2024-02-18 10:42:21 +00:00
}
// FullPath retrieves the full path of the specified file.
2024-02-18 10:42:21 +00:00
func FullPath(name string) (path string, err error) {
2024-02-18 10:42:21 +00:00
p, err := UTF16PtrFromString(name)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return "", err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
n := uint32(100)
2024-02-18 10:42:21 +00:00
for {
2024-02-18 10:42:21 +00:00
buf := make([]uint16, n)
2024-02-18 10:42:21 +00:00
n, err = GetFullPathName(p, uint32(len(buf)), &buf[0], nil)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return "", err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if n <= uint32(len(buf)) {
2024-02-18 10:42:21 +00:00
return UTF16ToString(buf[:n]), nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// NewProcThreadAttributeList allocates a new ProcThreadAttributeListContainer, with the requested maximum number of attributes.
2024-02-18 10:42:21 +00:00
func NewProcThreadAttributeList(maxAttrCount uint32) (*ProcThreadAttributeListContainer, error) {
2024-02-18 10:42:21 +00:00
var size uintptr
2024-02-18 10:42:21 +00:00
err := initializeProcThreadAttributeList(nil, maxAttrCount, 0, &size)
2024-02-18 10:42:21 +00:00
if err != ERROR_INSUFFICIENT_BUFFER {
2024-02-18 10:42:21 +00:00
if err == nil {
2024-02-18 10:42:21 +00:00
return nil, errorspkg.New("unable to query buffer size from InitializeProcThreadAttributeList")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
alloc, err := LocalAlloc(LMEM_FIXED, uint32(size))
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// size is guaranteed to be ≥1 by InitializeProcThreadAttributeList.
2024-02-18 10:42:21 +00:00
al := &ProcThreadAttributeListContainer{data: (*ProcThreadAttributeList)(unsafe.Pointer(alloc))}
2024-02-18 10:42:21 +00:00
err = initializeProcThreadAttributeList(al.data, maxAttrCount, 0, &size)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return al, err
2024-02-18 10:42:21 +00:00
}
// Update modifies the ProcThreadAttributeList using UpdateProcThreadAttribute.
2024-02-18 10:42:21 +00:00
func (al *ProcThreadAttributeListContainer) Update(attribute uintptr, value unsafe.Pointer, size uintptr) error {
2024-02-18 10:42:21 +00:00
al.pointers = append(al.pointers, value)
2024-02-18 10:42:21 +00:00
return updateProcThreadAttribute(al.data, 0, attribute, value, size, nil, nil)
2024-02-18 10:42:21 +00:00
}
// Delete frees ProcThreadAttributeList's resources.
2024-02-18 10:42:21 +00:00
func (al *ProcThreadAttributeListContainer) Delete() {
2024-02-18 10:42:21 +00:00
deleteProcThreadAttributeList(al.data)
2024-02-18 10:42:21 +00:00
LocalFree(Handle(unsafe.Pointer(al.data)))
2024-02-18 10:42:21 +00:00
al.data = nil
2024-02-18 10:42:21 +00:00
al.pointers = nil
2024-02-18 10:42:21 +00:00
}
// List returns the actual ProcThreadAttributeList to be passed to StartupInfoEx.
2024-02-18 10:42:21 +00:00
func (al *ProcThreadAttributeListContainer) List() *ProcThreadAttributeList {
2024-02-18 10:42:21 +00:00
return al.data
2024-02-18 10:42:21 +00:00
}