errors.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. // ErrNotSupported is an error encountered when hcs doesn't support the request
  55. ErrPlatformNotSupported = errors.New("unsupported platform request")
  56. )
  57. type ErrorEvent struct {
  58. Message string `json:"Message,omitempty"` // Fully formated error message
  59. StackTrace string `json:"StackTrace,omitempty"` // Stack trace in string form
  60. Provider string `json:"Provider,omitempty"`
  61. EventID uint16 `json:"EventId,omitempty"`
  62. Flags uint32 `json:"Flags,omitempty"`
  63. Source string `json:"Source,omitempty"`
  64. //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)
  65. }
  66. type hcsResult struct {
  67. Error int32
  68. ErrorMessage string
  69. ErrorEvents []ErrorEvent `json:"ErrorEvents,omitempty"`
  70. }
  71. func (ev *ErrorEvent) String() string {
  72. evs := "[Event Detail: " + ev.Message
  73. if ev.StackTrace != "" {
  74. evs += " Stack Trace: " + ev.StackTrace
  75. }
  76. if ev.Provider != "" {
  77. evs += " Provider: " + ev.Provider
  78. }
  79. if ev.EventID != 0 {
  80. evs = fmt.Sprintf("%s EventID: %d", evs, ev.EventID)
  81. }
  82. if ev.Flags != 0 {
  83. evs = fmt.Sprintf("%s flags: %d", evs, ev.Flags)
  84. }
  85. if ev.Source != "" {
  86. evs += " Source: " + ev.Source
  87. }
  88. evs += "]"
  89. return evs
  90. }
  91. func processHcsResult(resultp *uint16) []ErrorEvent {
  92. if resultp != nil {
  93. resultj := interop.ConvertAndFreeCoTaskMemString(resultp)
  94. logrus.WithField(logfields.JSON, resultj).
  95. Debug("HCS Result")
  96. result := &hcsResult{}
  97. if err := json.Unmarshal([]byte(resultj), result); err != nil {
  98. logrus.WithFields(logrus.Fields{
  99. logfields.JSON: resultj,
  100. logrus.ErrorKey: err,
  101. }).Warning("Could not unmarshal HCS result")
  102. return nil
  103. }
  104. return result.ErrorEvents
  105. }
  106. return nil
  107. }
  108. type HcsError struct {
  109. Op string
  110. Err error
  111. Events []ErrorEvent
  112. }
  113. func (e *HcsError) Error() string {
  114. s := e.Op + ": " + e.Err.Error()
  115. for _, ev := range e.Events {
  116. s += "\n" + ev.String()
  117. }
  118. return s
  119. }
  120. // ProcessError is an error encountered in HCS during an operation on a Process object
  121. type ProcessError struct {
  122. SystemID string
  123. Pid int
  124. Op string
  125. Err error
  126. Events []ErrorEvent
  127. }
  128. // SystemError is an error encountered in HCS during an operation on a Container object
  129. type SystemError struct {
  130. ID string
  131. Op string
  132. Err error
  133. Extra string
  134. Events []ErrorEvent
  135. }
  136. func (e *SystemError) Error() string {
  137. s := e.Op + " " + e.ID + ": " + e.Err.Error()
  138. for _, ev := range e.Events {
  139. s += "\n" + ev.String()
  140. }
  141. if e.Extra != "" {
  142. s += "\n(extra info: " + e.Extra + ")"
  143. }
  144. return s
  145. }
  146. func makeSystemError(system *System, op string, extra string, err error, events []ErrorEvent) error {
  147. // Don't double wrap errors
  148. if _, ok := err.(*SystemError); ok {
  149. return err
  150. }
  151. return &SystemError{
  152. ID: system.ID(),
  153. Op: op,
  154. Extra: extra,
  155. Err: err,
  156. Events: events,
  157. }
  158. }
  159. func (e *ProcessError) Error() string {
  160. s := fmt.Sprintf("%s %s:%d: %s", e.Op, e.SystemID, e.Pid, e.Err.Error())
  161. for _, ev := range e.Events {
  162. s += "\n" + ev.String()
  163. }
  164. return s
  165. }
  166. func makeProcessError(process *Process, op string, err error, events []ErrorEvent) error {
  167. // Don't double wrap errors
  168. if _, ok := err.(*ProcessError); ok {
  169. return err
  170. }
  171. return &ProcessError{
  172. Pid: process.Pid(),
  173. SystemID: process.SystemID(),
  174. Op: op,
  175. Err: err,
  176. Events: events,
  177. }
  178. }
  179. // IsNotExist checks if an error is caused by the Container or Process not existing.
  180. // Note: Currently, ErrElementNotFound can mean that a Process has either
  181. // already exited, or does not exist. Both IsAlreadyStopped and IsNotExist
  182. // will currently return true when the error is ErrElementNotFound or ErrProcNotFound.
  183. func IsNotExist(err error) bool {
  184. err = getInnerError(err)
  185. return err == ErrComputeSystemDoesNotExist ||
  186. err == ErrElementNotFound ||
  187. err == ErrProcNotFound
  188. }
  189. // IsAlreadyClosed checks if an error is caused by the Container or Process having been
  190. // already closed by a call to the Close() method.
  191. func IsAlreadyClosed(err error) bool {
  192. err = getInnerError(err)
  193. return err == ErrAlreadyClosed
  194. }
  195. // IsPending returns a boolean indicating whether the error is that
  196. // the requested operation is being completed in the background.
  197. func IsPending(err error) bool {
  198. err = getInnerError(err)
  199. return err == ErrVmcomputeOperationPending
  200. }
  201. // IsTimeout returns a boolean indicating whether the error is caused by
  202. // a timeout waiting for the operation to complete.
  203. func IsTimeout(err error) bool {
  204. err = getInnerError(err)
  205. return err == ErrTimeout
  206. }
  207. // IsAlreadyStopped returns a boolean indicating whether the error is caused by
  208. // a Container or Process being already stopped.
  209. // Note: Currently, ErrElementNotFound can mean that a Process has either
  210. // already exited, or does not exist. Both IsAlreadyStopped and IsNotExist
  211. // will currently return true when the error is ErrElementNotFound or ErrProcNotFound.
  212. func IsAlreadyStopped(err error) bool {
  213. err = getInnerError(err)
  214. return err == ErrVmcomputeAlreadyStopped ||
  215. err == ErrElementNotFound ||
  216. err == ErrProcNotFound
  217. }
  218. // IsNotSupported returns a boolean indicating whether the error is caused by
  219. // unsupported platform requests
  220. // Note: Currently Unsupported platform requests can be mean either
  221. // ErrVmcomputeInvalidJSON, ErrInvalidData, ErrNotSupported or ErrVmcomputeUnknownMessage
  222. // is thrown from the Platform
  223. func IsNotSupported(err error) bool {
  224. err = getInnerError(err)
  225. // If Platform doesn't recognize or support the request sent, below errors are seen
  226. return err == ErrVmcomputeInvalidJSON ||
  227. err == ErrInvalidData ||
  228. err == ErrNotSupported ||
  229. err == ErrVmcomputeUnknownMessage
  230. }
  231. func getInnerError(err error) error {
  232. switch pe := err.(type) {
  233. case nil:
  234. return nil
  235. case *HcsError:
  236. err = pe.Err
  237. case *SystemError:
  238. err = pe.Err
  239. case *ProcessError:
  240. err = pe.Err
  241. }
  242. return err
  243. }