Path: blob/main/vendor/golang.org/x/sys/windows/exec_windows.go
2880 views
// Copyright 2009 The Go Authors. All rights reserved.1// Use of this source code is governed by a BSD-style2// license that can be found in the LICENSE file.34// Fork, exec, wait, etc.56package windows78import (9errorspkg "errors"10"unsafe"11)1213// EscapeArg rewrites command line argument s as prescribed14// in http://msdn.microsoft.com/en-us/library/ms880421.15// This function returns "" (2 double quotes) if s is empty.16// Alternatively, these transformations are done:17// - every back slash (\) is doubled, but only if immediately18// followed by double quote (");19// - every double quote (") is escaped by back slash (\);20// - finally, s is wrapped with double quotes (arg -> "arg"),21// but only if there is space or tab inside s.22func EscapeArg(s string) string {23if len(s) == 0 {24return `""`25}26n := len(s)27hasSpace := false28for i := 0; i < len(s); i++ {29switch s[i] {30case '"', '\\':31n++32case ' ', '\t':33hasSpace = true34}35}36if hasSpace {37n += 2 // Reserve space for quotes.38}39if n == len(s) {40return s41}4243qs := make([]byte, n)44j := 045if hasSpace {46qs[j] = '"'47j++48}49slashes := 050for i := 0; i < len(s); i++ {51switch s[i] {52default:53slashes = 054qs[j] = s[i]55case '\\':56slashes++57qs[j] = s[i]58case '"':59for ; slashes > 0; slashes-- {60qs[j] = '\\'61j++62}63qs[j] = '\\'64j++65qs[j] = s[i]66}67j++68}69if hasSpace {70for ; slashes > 0; slashes-- {71qs[j] = '\\'72j++73}74qs[j] = '"'75j++76}77return string(qs[:j])78}7980// ComposeCommandLine escapes and joins the given arguments suitable for use as a Windows command line,81// in CreateProcess's CommandLine argument, CreateService/ChangeServiceConfig's BinaryPathName argument,82// or any program that uses CommandLineToArgv.83func ComposeCommandLine(args []string) string {84if len(args) == 0 {85return ""86}8788// Per https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-commandlinetoargvw:89// “This function accepts command lines that contain a program name; the90// program name can be enclosed in quotation marks or not.”91//92// Unfortunately, it provides no means of escaping interior quotation marks93// within that program name, and we have no way to report them here.94prog := args[0]95mustQuote := len(prog) == 096for i := 0; i < len(prog); i++ {97c := prog[i]98if c <= ' ' || (c == '"' && i == 0) {99// Force quotes for not only the ASCII space and tab as described in the100// MSDN article, but also ASCII control characters.101// The documentation for CommandLineToArgvW doesn't say what happens when102// the first argument is not a valid program name, but it empirically103// seems to drop unquoted control characters.104mustQuote = true105break106}107}108var commandLine []byte109if mustQuote {110commandLine = make([]byte, 0, len(prog)+2)111commandLine = append(commandLine, '"')112for i := 0; i < len(prog); i++ {113c := prog[i]114if c == '"' {115// This quote would interfere with our surrounding quotes.116// We have no way to report an error, so just strip out117// the offending character instead.118continue119}120commandLine = append(commandLine, c)121}122commandLine = append(commandLine, '"')123} else {124if len(args) == 1 {125// args[0] is a valid command line representing itself.126// No need to allocate a new slice or string for it.127return prog128}129commandLine = []byte(prog)130}131132for _, arg := range args[1:] {133commandLine = append(commandLine, ' ')134// TODO(bcmills): since we're already appending to a slice, it would be nice135// to avoid the intermediate allocations of EscapeArg.136// Perhaps we can factor out an appendEscapedArg function.137commandLine = append(commandLine, EscapeArg(arg)...)138}139return string(commandLine)140}141142// DecomposeCommandLine breaks apart its argument command line into unescaped parts using CommandLineToArgv,143// as gathered from GetCommandLine, QUERY_SERVICE_CONFIG's BinaryPathName argument, or elsewhere that144// command lines are passed around.145// DecomposeCommandLine returns an error if commandLine contains NUL.146func DecomposeCommandLine(commandLine string) ([]string, error) {147if len(commandLine) == 0 {148return []string{}, nil149}150utf16CommandLine, err := UTF16FromString(commandLine)151if err != nil {152return nil, errorspkg.New("string with NUL passed to DecomposeCommandLine")153}154var argc int32155argv, err := commandLineToArgv(&utf16CommandLine[0], &argc)156if err != nil {157return nil, err158}159defer LocalFree(Handle(unsafe.Pointer(argv)))160161var args []string162for _, p := range unsafe.Slice(argv, argc) {163args = append(args, UTF16PtrToString(p))164}165return args, nil166}167168// CommandLineToArgv parses a Unicode command line string and sets169// argc to the number of parsed arguments.170//171// The returned memory should be freed using a single call to LocalFree.172//173// Note that although the return type of CommandLineToArgv indicates 8192174// entries of up to 8192 characters each, the actual count of parsed arguments175// may exceed 8192, and the documentation for CommandLineToArgvW does not mention176// any bound on the lengths of the individual argument strings.177// (See https://go.dev/issue/63236.)178func CommandLineToArgv(cmd *uint16, argc *int32) (argv *[8192]*[8192]uint16, err error) {179argp, err := commandLineToArgv(cmd, argc)180argv = (*[8192]*[8192]uint16)(unsafe.Pointer(argp))181return argv, err182}183184func CloseOnExec(fd Handle) {185SetHandleInformation(Handle(fd), HANDLE_FLAG_INHERIT, 0)186}187188// FullPath retrieves the full path of the specified file.189func FullPath(name string) (path string, err error) {190p, err := UTF16PtrFromString(name)191if err != nil {192return "", err193}194n := uint32(100)195for {196buf := make([]uint16, n)197n, err = GetFullPathName(p, uint32(len(buf)), &buf[0], nil)198if err != nil {199return "", err200}201if n <= uint32(len(buf)) {202return UTF16ToString(buf[:n]), nil203}204}205}206207// NewProcThreadAttributeList allocates a new ProcThreadAttributeListContainer, with the requested maximum number of attributes.208func NewProcThreadAttributeList(maxAttrCount uint32) (*ProcThreadAttributeListContainer, error) {209var size uintptr210err := initializeProcThreadAttributeList(nil, maxAttrCount, 0, &size)211if err != ERROR_INSUFFICIENT_BUFFER {212if err == nil {213return nil, errorspkg.New("unable to query buffer size from InitializeProcThreadAttributeList")214}215return nil, err216}217alloc, err := LocalAlloc(LMEM_FIXED, uint32(size))218if err != nil {219return nil, err220}221// size is guaranteed to be ≥1 by InitializeProcThreadAttributeList.222al := &ProcThreadAttributeListContainer{data: (*ProcThreadAttributeList)(unsafe.Pointer(alloc))}223err = initializeProcThreadAttributeList(al.data, maxAttrCount, 0, &size)224if err != nil {225return nil, err226}227return al, err228}229230// Update modifies the ProcThreadAttributeList using UpdateProcThreadAttribute.231func (al *ProcThreadAttributeListContainer) Update(attribute uintptr, value unsafe.Pointer, size uintptr) error {232al.pointers = append(al.pointers, value)233return updateProcThreadAttribute(al.data, 0, attribute, value, size, nil, nil)234}235236// Delete frees ProcThreadAttributeList's resources.237func (al *ProcThreadAttributeListContainer) Delete() {238deleteProcThreadAttributeList(al.data)239LocalFree(Handle(unsafe.Pointer(al.data)))240al.data = nil241al.pointers = nil242}243244// List returns the actual ProcThreadAttributeList to be passed to StartupInfoEx.245func (al *ProcThreadAttributeListContainer) List() *ProcThreadAttributeList {246return al.data247}248249250