exec_windows.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Fork, exec, wait, etc.
  5. package windows
  6. import (
  7. errorspkg "errors"
  8. "unsafe"
  9. )
  10. // EscapeArg rewrites command line argument s as prescribed
  11. // in http://msdn.microsoft.com/en-us/library/ms880421.
  12. // This function returns "" (2 double quotes) if s is empty.
  13. // Alternatively, these transformations are done:
  14. // - every back slash (\) is doubled, but only if immediately
  15. // followed by double quote (");
  16. // - every double quote (") is escaped by back slash (\);
  17. // - finally, s is wrapped with double quotes (arg -> "arg"),
  18. // but only if there is space or tab inside s.
  19. func EscapeArg(s string) string {
  20. if len(s) == 0 {
  21. return `""`
  22. }
  23. n := len(s)
  24. hasSpace := false
  25. for i := 0; i < len(s); i++ {
  26. switch s[i] {
  27. case '"', '\\':
  28. n++
  29. case ' ', '\t':
  30. hasSpace = true
  31. }
  32. }
  33. if hasSpace {
  34. n += 2 // Reserve space for quotes.
  35. }
  36. if n == len(s) {
  37. return s
  38. }
  39. qs := make([]byte, n)
  40. j := 0
  41. if hasSpace {
  42. qs[j] = '"'
  43. j++
  44. }
  45. slashes := 0
  46. for i := 0; i < len(s); i++ {
  47. switch s[i] {
  48. default:
  49. slashes = 0
  50. qs[j] = s[i]
  51. case '\\':
  52. slashes++
  53. qs[j] = s[i]
  54. case '"':
  55. for ; slashes > 0; slashes-- {
  56. qs[j] = '\\'
  57. j++
  58. }
  59. qs[j] = '\\'
  60. j++
  61. qs[j] = s[i]
  62. }
  63. j++
  64. }
  65. if hasSpace {
  66. for ; slashes > 0; slashes-- {
  67. qs[j] = '\\'
  68. j++
  69. }
  70. qs[j] = '"'
  71. j++
  72. }
  73. return string(qs[:j])
  74. }
  75. // ComposeCommandLine escapes and joins the given arguments suitable for use as a Windows command line,
  76. // in CreateProcess's CommandLine argument, CreateService/ChangeServiceConfig's BinaryPathName argument,
  77. // or any program that uses CommandLineToArgv.
  78. func ComposeCommandLine(args []string) string {
  79. if len(args) == 0 {
  80. return ""
  81. }
  82. // Per https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-commandlinetoargvw:
  83. // “This function accepts command lines that contain a program name; the
  84. // program name can be enclosed in quotation marks or not.”
  85. //
  86. // Unfortunately, it provides no means of escaping interior quotation marks
  87. // within that program name, and we have no way to report them here.
  88. prog := args[0]
  89. mustQuote := len(prog) == 0
  90. for i := 0; i < len(prog); i++ {
  91. c := prog[i]
  92. if c <= ' ' || (c == '"' && i == 0) {
  93. // Force quotes for not only the ASCII space and tab as described in the
  94. // MSDN article, but also ASCII control characters.
  95. // The documentation for CommandLineToArgvW doesn't say what happens when
  96. // the first argument is not a valid program name, but it empirically
  97. // seems to drop unquoted control characters.
  98. mustQuote = true
  99. break
  100. }
  101. }
  102. var commandLine []byte
  103. if mustQuote {
  104. commandLine = make([]byte, 0, len(prog)+2)
  105. commandLine = append(commandLine, '"')
  106. for i := 0; i < len(prog); i++ {
  107. c := prog[i]
  108. if c == '"' {
  109. // This quote would interfere with our surrounding quotes.
  110. // We have no way to report an error, so just strip out
  111. // the offending character instead.
  112. continue
  113. }
  114. commandLine = append(commandLine, c)
  115. }
  116. commandLine = append(commandLine, '"')
  117. } else {
  118. if len(args) == 1 {
  119. // args[0] is a valid command line representing itself.
  120. // No need to allocate a new slice or string for it.
  121. return prog
  122. }
  123. commandLine = []byte(prog)
  124. }
  125. for _, arg := range args[1:] {
  126. commandLine = append(commandLine, ' ')
  127. // TODO(bcmills): since we're already appending to a slice, it would be nice
  128. // to avoid the intermediate allocations of EscapeArg.
  129. // Perhaps we can factor out an appendEscapedArg function.
  130. commandLine = append(commandLine, EscapeArg(arg)...)
  131. }
  132. return string(commandLine)
  133. }
  134. // DecomposeCommandLine breaks apart its argument command line into unescaped parts using CommandLineToArgv,
  135. // as gathered from GetCommandLine, QUERY_SERVICE_CONFIG's BinaryPathName argument, or elsewhere that
  136. // command lines are passed around.
  137. // DecomposeCommandLine returns an error if commandLine contains NUL.
  138. func DecomposeCommandLine(commandLine string) ([]string, error) {
  139. if len(commandLine) == 0 {
  140. return []string{}, nil
  141. }
  142. utf16CommandLine, err := UTF16FromString(commandLine)
  143. if err != nil {
  144. return nil, errorspkg.New("string with NUL passed to DecomposeCommandLine")
  145. }
  146. var argc int32
  147. argv, err := commandLineToArgv(&utf16CommandLine[0], &argc)
  148. if err != nil {
  149. return nil, err
  150. }
  151. defer LocalFree(Handle(unsafe.Pointer(argv)))
  152. var args []string
  153. for _, p := range unsafe.Slice(argv, argc) {
  154. args = append(args, UTF16PtrToString(p))
  155. }
  156. return args, nil
  157. }
  158. // CommandLineToArgv parses a Unicode command line string and sets
  159. // argc to the number of parsed arguments.
  160. //
  161. // The returned memory should be freed using a single call to LocalFree.
  162. //
  163. // Note that although the return type of CommandLineToArgv indicates 8192
  164. // entries of up to 8192 characters each, the actual count of parsed arguments
  165. // may exceed 8192, and the documentation for CommandLineToArgvW does not mention
  166. // any bound on the lengths of the individual argument strings.
  167. // (See https://go.dev/issue/63236.)
  168. func CommandLineToArgv(cmd *uint16, argc *int32) (argv *[8192]*[8192]uint16, err error) {
  169. argp, err := commandLineToArgv(cmd, argc)
  170. argv = (*[8192]*[8192]uint16)(unsafe.Pointer(argp))
  171. return argv, err
  172. }
  173. func CloseOnExec(fd Handle) {
  174. SetHandleInformation(Handle(fd), HANDLE_FLAG_INHERIT, 0)
  175. }
  176. // FullPath retrieves the full path of the specified file.
  177. func FullPath(name string) (path string, err error) {
  178. p, err := UTF16PtrFromString(name)
  179. if err != nil {
  180. return "", err
  181. }
  182. n := uint32(100)
  183. for {
  184. buf := make([]uint16, n)
  185. n, err = GetFullPathName(p, uint32(len(buf)), &buf[0], nil)
  186. if err != nil {
  187. return "", err
  188. }
  189. if n <= uint32(len(buf)) {
  190. return UTF16ToString(buf[:n]), nil
  191. }
  192. }
  193. }
  194. // NewProcThreadAttributeList allocates a new ProcThreadAttributeListContainer, with the requested maximum number of attributes.
  195. func NewProcThreadAttributeList(maxAttrCount uint32) (*ProcThreadAttributeListContainer, error) {
  196. var size uintptr
  197. err := initializeProcThreadAttributeList(nil, maxAttrCount, 0, &size)
  198. if err != ERROR_INSUFFICIENT_BUFFER {
  199. if err == nil {
  200. return nil, errorspkg.New("unable to query buffer size from InitializeProcThreadAttributeList")
  201. }
  202. return nil, err
  203. }
  204. alloc, err := LocalAlloc(LMEM_FIXED, uint32(size))
  205. if err != nil {
  206. return nil, err
  207. }
  208. // size is guaranteed to be ≥1 by InitializeProcThreadAttributeList.
  209. al := &ProcThreadAttributeListContainer{data: (*ProcThreadAttributeList)(unsafe.Pointer(alloc))}
  210. err = initializeProcThreadAttributeList(al.data, maxAttrCount, 0, &size)
  211. if err != nil {
  212. return nil, err
  213. }
  214. return al, err
  215. }
  216. // Update modifies the ProcThreadAttributeList using UpdateProcThreadAttribute.
  217. func (al *ProcThreadAttributeListContainer) Update(attribute uintptr, value unsafe.Pointer, size uintptr) error {
  218. al.pointers = append(al.pointers, value)
  219. return updateProcThreadAttribute(al.data, 0, attribute, value, size, nil, nil)
  220. }
  221. // Delete frees ProcThreadAttributeList's resources.
  222. func (al *ProcThreadAttributeListContainer) Delete() {
  223. deleteProcThreadAttributeList(al.data)
  224. LocalFree(Handle(unsafe.Pointer(al.data)))
  225. al.data = nil
  226. al.pointers = nil
  227. }
  228. // List returns the actual ProcThreadAttributeList to be passed to StartupInfoEx.
  229. func (al *ProcThreadAttributeListContainer) List() *ProcThreadAttributeList {
  230. return al.data
  231. }