exec_windows.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. "golang.org/x/sys/internal/unsafeheader"
  10. )
  11. // EscapeArg rewrites command line argument s as prescribed
  12. // in http://msdn.microsoft.com/en-us/library/ms880421.
  13. // This function returns "" (2 double quotes) if s is empty.
  14. // Alternatively, these transformations are done:
  15. // - every back slash (\) is doubled, but only if immediately
  16. // followed by double quote (");
  17. // - every double quote (") is escaped by back slash (\);
  18. // - finally, s is wrapped with double quotes (arg -> "arg"),
  19. // but only if there is space or tab inside s.
  20. func EscapeArg(s string) string {
  21. if len(s) == 0 {
  22. return "\"\""
  23. }
  24. n := len(s)
  25. hasSpace := false
  26. for i := 0; i < len(s); i++ {
  27. switch s[i] {
  28. case '"', '\\':
  29. n++
  30. case ' ', '\t':
  31. hasSpace = true
  32. }
  33. }
  34. if hasSpace {
  35. n += 2
  36. }
  37. if n == len(s) {
  38. return s
  39. }
  40. qs := make([]byte, n)
  41. j := 0
  42. if hasSpace {
  43. qs[j] = '"'
  44. j++
  45. }
  46. slashes := 0
  47. for i := 0; i < len(s); i++ {
  48. switch s[i] {
  49. default:
  50. slashes = 0
  51. qs[j] = s[i]
  52. case '\\':
  53. slashes++
  54. qs[j] = s[i]
  55. case '"':
  56. for ; slashes > 0; slashes-- {
  57. qs[j] = '\\'
  58. j++
  59. }
  60. qs[j] = '\\'
  61. j++
  62. qs[j] = s[i]
  63. }
  64. j++
  65. }
  66. if hasSpace {
  67. for ; slashes > 0; slashes-- {
  68. qs[j] = '\\'
  69. j++
  70. }
  71. qs[j] = '"'
  72. j++
  73. }
  74. return string(qs[:j])
  75. }
  76. // ComposeCommandLine escapes and joins the given arguments suitable for use as a Windows command line,
  77. // in CreateProcess's CommandLine argument, CreateService/ChangeServiceConfig's BinaryPathName argument,
  78. // or any program that uses CommandLineToArgv.
  79. func ComposeCommandLine(args []string) string {
  80. var commandLine string
  81. for i := range args {
  82. if i > 0 {
  83. commandLine += " "
  84. }
  85. commandLine += EscapeArg(args[i])
  86. }
  87. return commandLine
  88. }
  89. // DecomposeCommandLine breaks apart its argument command line into unescaped parts using CommandLineToArgv,
  90. // as gathered from GetCommandLine, QUERY_SERVICE_CONFIG's BinaryPathName argument, or elsewhere that
  91. // command lines are passed around.
  92. func DecomposeCommandLine(commandLine string) ([]string, error) {
  93. if len(commandLine) == 0 {
  94. return []string{}, nil
  95. }
  96. var argc int32
  97. argv, err := CommandLineToArgv(StringToUTF16Ptr(commandLine), &argc)
  98. if err != nil {
  99. return nil, err
  100. }
  101. defer LocalFree(Handle(unsafe.Pointer(argv)))
  102. var args []string
  103. for _, v := range (*argv)[:argc] {
  104. args = append(args, UTF16ToString((*v)[:]))
  105. }
  106. return args, nil
  107. }
  108. func CloseOnExec(fd Handle) {
  109. SetHandleInformation(Handle(fd), HANDLE_FLAG_INHERIT, 0)
  110. }
  111. // FullPath retrieves the full path of the specified file.
  112. func FullPath(name string) (path string, err error) {
  113. p, err := UTF16PtrFromString(name)
  114. if err != nil {
  115. return "", err
  116. }
  117. n := uint32(100)
  118. for {
  119. buf := make([]uint16, n)
  120. n, err = GetFullPathName(p, uint32(len(buf)), &buf[0], nil)
  121. if err != nil {
  122. return "", err
  123. }
  124. if n <= uint32(len(buf)) {
  125. return UTF16ToString(buf[:n]), nil
  126. }
  127. }
  128. }
  129. // NewProcThreadAttributeList allocates a new ProcThreadAttributeListContainer, with the requested maximum number of attributes.
  130. func NewProcThreadAttributeList(maxAttrCount uint32) (*ProcThreadAttributeListContainer, error) {
  131. var size uintptr
  132. err := initializeProcThreadAttributeList(nil, maxAttrCount, 0, &size)
  133. if err != ERROR_INSUFFICIENT_BUFFER {
  134. if err == nil {
  135. return nil, errorspkg.New("unable to query buffer size from InitializeProcThreadAttributeList")
  136. }
  137. return nil, err
  138. }
  139. // size is guaranteed to be ≥1 by InitializeProcThreadAttributeList.
  140. al := &ProcThreadAttributeListContainer{data: (*ProcThreadAttributeList)(unsafe.Pointer(&make([]byte, size)[0]))}
  141. err = initializeProcThreadAttributeList(al.data, maxAttrCount, 0, &size)
  142. if err != nil {
  143. return nil, err
  144. }
  145. return al, err
  146. }
  147. // Update modifies the ProcThreadAttributeList using UpdateProcThreadAttribute.
  148. // Note that the value passed to this function will be copied into memory
  149. // allocated by LocalAlloc, the contents of which should not contain any
  150. // Go-managed pointers, even if the passed value itself is a Go-managed
  151. // pointer.
  152. func (al *ProcThreadAttributeListContainer) Update(attribute uintptr, value unsafe.Pointer, size uintptr) error {
  153. alloc, err := LocalAlloc(LMEM_FIXED, uint32(size))
  154. if err != nil {
  155. return err
  156. }
  157. var src, dst []byte
  158. hdr := (*unsafeheader.Slice)(unsafe.Pointer(&src))
  159. hdr.Data = value
  160. hdr.Cap = int(size)
  161. hdr.Len = int(size)
  162. hdr = (*unsafeheader.Slice)(unsafe.Pointer(&dst))
  163. hdr.Data = unsafe.Pointer(alloc)
  164. hdr.Cap = int(size)
  165. hdr.Len = int(size)
  166. copy(dst, src)
  167. al.heapAllocations = append(al.heapAllocations, alloc)
  168. return updateProcThreadAttribute(al.data, 0, attribute, unsafe.Pointer(alloc), size, nil, nil)
  169. }
  170. // Delete frees ProcThreadAttributeList's resources.
  171. func (al *ProcThreadAttributeListContainer) Delete() {
  172. deleteProcThreadAttributeList(al.data)
  173. for i := range al.heapAllocations {
  174. LocalFree(Handle(al.heapAllocations[i]))
  175. }
  176. al.heapAllocations = nil
  177. }
  178. // List returns the actual ProcThreadAttributeList to be passed to StartupInfoEx.
  179. func (al *ProcThreadAttributeListContainer) List() *ProcThreadAttributeList {
  180. return al.data
  181. }