Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

184 lignes
4.8 KiB

  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
  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. var commandLine string
  80. for i := range args {
  81. if i > 0 {
  82. commandLine += " "
  83. }
  84. commandLine += EscapeArg(args[i])
  85. }
  86. return commandLine
  87. }
  88. // DecomposeCommandLine breaks apart its argument command line into unescaped parts using CommandLineToArgv,
  89. // as gathered from GetCommandLine, QUERY_SERVICE_CONFIG's BinaryPathName argument, or elsewhere that
  90. // command lines are passed around.
  91. // DecomposeCommandLine returns error if commandLine contains NUL.
  92. func DecomposeCommandLine(commandLine string) ([]string, error) {
  93. if len(commandLine) == 0 {
  94. return []string{}, nil
  95. }
  96. utf16CommandLine, err := UTF16FromString(commandLine)
  97. if err != nil {
  98. return nil, errorspkg.New("string with NUL passed to DecomposeCommandLine")
  99. }
  100. var argc int32
  101. argv, err := CommandLineToArgv(&utf16CommandLine[0], &argc)
  102. if err != nil {
  103. return nil, err
  104. }
  105. defer LocalFree(Handle(unsafe.Pointer(argv)))
  106. var args []string
  107. for _, v := range (*argv)[:argc] {
  108. args = append(args, UTF16ToString((*v)[:]))
  109. }
  110. return args, nil
  111. }
  112. func CloseOnExec(fd Handle) {
  113. SetHandleInformation(Handle(fd), HANDLE_FLAG_INHERIT, 0)
  114. }
  115. // FullPath retrieves the full path of the specified file.
  116. func FullPath(name string) (path string, err error) {
  117. p, err := UTF16PtrFromString(name)
  118. if err != nil {
  119. return "", err
  120. }
  121. n := uint32(100)
  122. for {
  123. buf := make([]uint16, n)
  124. n, err = GetFullPathName(p, uint32(len(buf)), &buf[0], nil)
  125. if err != nil {
  126. return "", err
  127. }
  128. if n <= uint32(len(buf)) {
  129. return UTF16ToString(buf[:n]), nil
  130. }
  131. }
  132. }
  133. // NewProcThreadAttributeList allocates a new ProcThreadAttributeListContainer, with the requested maximum number of attributes.
  134. func NewProcThreadAttributeList(maxAttrCount uint32) (*ProcThreadAttributeListContainer, error) {
  135. var size uintptr
  136. err := initializeProcThreadAttributeList(nil, maxAttrCount, 0, &size)
  137. if err != ERROR_INSUFFICIENT_BUFFER {
  138. if err == nil {
  139. return nil, errorspkg.New("unable to query buffer size from InitializeProcThreadAttributeList")
  140. }
  141. return nil, err
  142. }
  143. alloc, err := LocalAlloc(LMEM_FIXED, uint32(size))
  144. if err != nil {
  145. return nil, err
  146. }
  147. // size is guaranteed to be ≥1 by InitializeProcThreadAttributeList.
  148. al := &ProcThreadAttributeListContainer{data: (*ProcThreadAttributeList)(unsafe.Pointer(alloc))}
  149. err = initializeProcThreadAttributeList(al.data, maxAttrCount, 0, &size)
  150. if err != nil {
  151. return nil, err
  152. }
  153. return al, err
  154. }
  155. // Update modifies the ProcThreadAttributeList using UpdateProcThreadAttribute.
  156. func (al *ProcThreadAttributeListContainer) Update(attribute uintptr, value unsafe.Pointer, size uintptr) error {
  157. al.pointers = append(al.pointers, value)
  158. return updateProcThreadAttribute(al.data, 0, attribute, value, size, nil, nil)
  159. }
  160. // Delete frees ProcThreadAttributeList's resources.
  161. func (al *ProcThreadAttributeListContainer) Delete() {
  162. deleteProcThreadAttributeList(al.data)
  163. LocalFree(Handle(unsafe.Pointer(al.data)))
  164. al.data = nil
  165. al.pointers = nil
  166. }
  167. // List returns the actual ProcThreadAttributeList to be passed to StartupInfoEx.
  168. func (al *ProcThreadAttributeListContainer) List() *ProcThreadAttributeList {
  169. return al.data
  170. }