errors.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. package hcs
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "syscall"
  7. "github.com/Microsoft/hcsshim/internal/interop"
  8. "github.com/Microsoft/hcsshim/internal/logfields"
  9. "github.com/sirupsen/logrus"
  10. )
  11. var (
  12. // ErrComputeSystemDoesNotExist is an error encountered when the container being operated on no longer exists
  13. ErrComputeSystemDoesNotExist = syscall.Errno(0xc037010e)
  14. // ErrElementNotFound is an error encountered when the object being referenced does not exist
  15. ErrElementNotFound = syscall.Errno(0x490)
  16. // ErrElementNotFound is an error encountered when the object being referenced does not exist
  17. ErrNotSupported = syscall.Errno(0x32)
  18. // ErrInvalidData is an error encountered when the request being sent to hcs is invalid/unsupported
  19. // decimal -2147024883 / hex 0x8007000d
  20. ErrInvalidData = syscall.Errno(0xd)
  21. // ErrHandleClose is an error encountered when the handle generating the notification being waited on has been closed
  22. ErrHandleClose = errors.New("hcsshim: the handle generating this notification has been closed")
  23. // ErrAlreadyClosed is an error encountered when using a handle that has been closed by the Close method
  24. ErrAlreadyClosed = errors.New("hcsshim: the handle has already been closed")
  25. // ErrInvalidNotificationType is an error encountered when an invalid notification type is used
  26. ErrInvalidNotificationType = errors.New("hcsshim: invalid notification type")
  27. // ErrInvalidProcessState is an error encountered when the process is not in a valid state for the requested operation
  28. ErrInvalidProcessState = errors.New("the process is in an invalid state for the attempted operation")
  29. // ErrTimeout is an error encountered when waiting on a notification times out
  30. ErrTimeout = errors.New("hcsshim: timeout waiting for notification")
  31. // ErrUnexpectedContainerExit is the error encountered when a container exits while waiting for
  32. // a different expected notification
  33. ErrUnexpectedContainerExit = errors.New("unexpected container exit")
  34. // ErrUnexpectedProcessAbort is the error encountered when communication with the compute service
  35. // is lost while waiting for a notification
  36. ErrUnexpectedProcessAbort = errors.New("lost communication with compute service")
  37. // ErrUnexpectedValue is an error encountered when hcs returns an invalid value
  38. ErrUnexpectedValue = errors.New("unexpected value returned from hcs")
  39. // ErrVmcomputeAlreadyStopped is an error encountered when a shutdown or terminate request is made on a stopped container
  40. ErrVmcomputeAlreadyStopped = syscall.Errno(0xc0370110)
  41. // ErrVmcomputeOperationPending is an error encountered when the operation is being completed asynchronously
  42. ErrVmcomputeOperationPending = syscall.Errno(0xC0370103)
  43. // ErrVmcomputeOperationInvalidState is an error encountered when the compute system is not in a valid state for the requested operation
  44. ErrVmcomputeOperationInvalidState = syscall.Errno(0xc0370105)
  45. // ErrProcNotFound is an error encountered when the the process cannot be found
  46. ErrProcNotFound = syscall.Errno(0x7f)
  47. // ErrVmcomputeOperationAccessIsDenied is an error which can be encountered when enumerating compute systems in RS1/RS2
  48. // builds when the underlying silo might be in the process of terminating. HCS was fixed in RS3.
  49. ErrVmcomputeOperationAccessIsDenied = syscall.Errno(0x5)
  50. // ErrVmcomputeInvalidJSON is an error encountered when the compute system does not support/understand the messages sent by management
  51. ErrVmcomputeInvalidJSON = syscall.Errno(0xc037010d)
  52. // ErrVmcomputeUnknownMessage is an error encountered guest compute system doesn't support the message
  53. ErrVmcomputeUnknownMessage = syscall.Errno(0xc037010b)
  54. // ErrVmcomputeUnexpectedExit is an error encountered when the compute system terminates unexpectedly
  55. ErrVmcomputeUnexpectedExit = syscall.Errno(0xC0370106)
  56. // ErrNotSupported is an error encountered when hcs doesn't support the request
  57. ErrPlatformNotSupported = errors.New("unsupported platform request")
  58. )
  59. type ErrorEvent struct {
  60. Message string `json:"Message,omitempty"` // Fully formated error message
  61. StackTrace string `json:"StackTrace,omitempty"` // Stack trace in string form
  62. Provider string `json:"Provider,omitempty"`
  63. EventID uint16 `json:"EventId,omitempty"`
  64. Flags uint32 `json:"Flags,omitempty"`
  65. Source string `json:"Source,omitempty"`
  66. //Data []EventData `json:"Data,omitempty"` // Omit this as HCS doesn't encode this well. It's more confusing to include. It is however logged in debug mode (see processHcsResult function)
  67. }
  68. type hcsResult struct {
  69. Error int32
  70. ErrorMessage string
  71. ErrorEvents []ErrorEvent `json:"ErrorEvents,omitempty"`
  72. }
  73. func (ev *ErrorEvent) String() string {
  74. evs := "[Event Detail: " + ev.Message
  75. if ev.StackTrace != "" {
  76. evs += " Stack Trace: " + ev.StackTrace
  77. }
  78. if ev.Provider != "" {
  79. evs += " Provider: " + ev.Provider
  80. }
  81. if ev.EventID != 0 {
  82. evs = fmt.Sprintf("%s EventID: %d", evs, ev.EventID)
  83. }
  84. if ev.Flags != 0 {
  85. evs = fmt.Sprintf("%s flags: %d", evs, ev.Flags)
  86. }
  87. if ev.Source != "" {
  88. evs += " Source: " + ev.Source
  89. }
  90. evs += "]"
  91. return evs
  92. }
  93. func processHcsResult(resultp *uint16) []ErrorEvent {
  94. if resultp != nil {
  95. resultj := interop.ConvertAndFreeCoTaskMemString(resultp)
  96. logrus.WithField(logfields.JSON, resultj).
  97. Debug("HCS Result")
  98. result := &hcsResult{}
  99. if err := json.Unmarshal([]byte(resultj), result); err != nil {
  100. logrus.WithFields(logrus.Fields{
  101. logfields.JSON: resultj,
  102. logrus.ErrorKey: err,
  103. }).Warning("Could not unmarshal HCS result")
  104. return nil
  105. }
  106. return result.ErrorEvents
  107. }
  108. return nil
  109. }
  110. type HcsError struct {
  111. Op string
  112. Err error
  113. Events []ErrorEvent
  114. }
  115. func (e *HcsError) Error() string {
  116. s := e.Op + ": " + e.Err.Error()
  117. for _, ev := range e.Events {
  118. s += "\n" + ev.String()
  119. }
  120. return s
  121. }
  122. // ProcessError is an error encountered in HCS during an operation on a Process object
  123. type ProcessError struct {
  124. SystemID string
  125. Pid int
  126. Op string
  127. Err error
  128. Events []ErrorEvent
  129. }
  130. // SystemError is an error encountered in HCS during an operation on a Container object
  131. type SystemError struct {
  132. ID string
  133. Op string
  134. Err error
  135. Extra string
  136. Events []ErrorEvent
  137. }
  138. func (e *SystemError) Error() string {
  139. s := e.Op + " " + e.ID + ": " + e.Err.Error()
  140. for _, ev := range e.Events {
  141. s += "\n" + ev.String()
  142. }
  143. if e.Extra != "" {
  144. s += "\n(extra info: " + e.Extra + ")"
  145. }
  146. return s
  147. }
  148. func makeSystemError(system *System, op string, extra string, err error, events []ErrorEvent) error {
  149. // Don't double wrap errors
  150. if _, ok := err.(*SystemError); ok {
  151. return err
  152. }
  153. return &SystemError{
  154. ID: system.ID(),
  155. Op: op,
  156. Extra: extra,
  157. Err: err,
  158. Events: events,
  159. }
  160. }
  161. func (e *ProcessError) Error() string {
  162. s := fmt.Sprintf("%s %s:%d: %s", e.Op, e.SystemID, e.Pid, e.Err.Error())
  163. for _, ev := range e.Events {
  164. s += "\n" + ev.String()
  165. }
  166. return s
  167. }
  168. func makeProcessError(process *Process, op string, err error, events []ErrorEvent) error {
  169. // Don't double wrap errors
  170. if _, ok := err.(*ProcessError); ok {
  171. return err
  172. }
  173. return &ProcessError{
  174. Pid: process.Pid(),
  175. SystemID: process.SystemID(),
  176. Op: op,
  177. Err: err,
  178. Events: events,
  179. }
  180. }
  181. // IsNotExist checks if an error is caused by the Container or Process not existing.
  182. // Note: Currently, ErrElementNotFound can mean that a Process has either
  183. // already exited, or does not exist. Both IsAlreadyStopped and IsNotExist
  184. // will currently return true when the error is ErrElementNotFound or ErrProcNotFound.
  185. func IsNotExist(err error) bool {
  186. err = getInnerError(err)
  187. return err == ErrComputeSystemDoesNotExist ||
  188. err == ErrElementNotFound ||
  189. err == ErrProcNotFound
  190. }
  191. // IsAlreadyClosed checks if an error is caused by the Container or Process having been
  192. // already closed by a call to the Close() method.
  193. func IsAlreadyClosed(err error) bool {
  194. err = getInnerError(err)
  195. return err == ErrAlreadyClosed
  196. }
  197. // IsPending returns a boolean indicating whether the error is that
  198. // the requested operation is being completed in the background.
  199. func IsPending(err error) bool {
  200. err = getInnerError(err)
  201. return err == ErrVmcomputeOperationPending
  202. }
  203. // IsTimeout returns a boolean indicating whether the error is caused by
  204. // a timeout waiting for the operation to complete.
  205. func IsTimeout(err error) bool {
  206. err = getInnerError(err)
  207. return err == ErrTimeout
  208. }
  209. // IsAlreadyStopped returns a boolean indicating whether the error is caused by
  210. // a Container or Process being already stopped.
  211. // Note: Currently, ErrElementNotFound can mean that a Process has either
  212. // already exited, or does not exist. Both IsAlreadyStopped and IsNotExist
  213. // will currently return true when the error is ErrElementNotFound or ErrProcNotFound.
  214. func IsAlreadyStopped(err error) bool {
  215. err = getInnerError(err)
  216. return err == ErrVmcomputeAlreadyStopped ||
  217. err == ErrElementNotFound ||
  218. err == ErrProcNotFound
  219. }
  220. // IsNotSupported returns a boolean indicating whether the error is caused by
  221. // unsupported platform requests
  222. // Note: Currently Unsupported platform requests can be mean either
  223. // ErrVmcomputeInvalidJSON, ErrInvalidData, ErrNotSupported or ErrVmcomputeUnknownMessage
  224. // is thrown from the Platform
  225. func IsNotSupported(err error) bool {
  226. err = getInnerError(err)
  227. // If Platform doesn't recognize or support the request sent, below errors are seen
  228. return err == ErrVmcomputeInvalidJSON ||
  229. err == ErrInvalidData ||
  230. err == ErrNotSupported ||
  231. err == ErrVmcomputeUnknownMessage
  232. }
  233. func getInnerError(err error) error {
  234. switch pe := err.(type) {
  235. case nil:
  236. return nil
  237. case *HcsError:
  238. err = pe.Err
  239. case *SystemError:
  240. err = pe.Err
  241. case *ProcessError:
  242. err = pe.Err
  243. }
  244. return err
  245. }