errors.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. package hcs
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "net"
  8. "syscall"
  9. "github.com/Microsoft/hcsshim/internal/log"
  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(ctx context.Context, resultJSON string) []ErrorEvent {
  94. if resultJSON != "" {
  95. result := &hcsResult{}
  96. if err := json.Unmarshal([]byte(resultJSON), result); err != nil {
  97. log.G(ctx).WithError(err).Warning("Could not unmarshal HCS result")
  98. return nil
  99. }
  100. return result.ErrorEvents
  101. }
  102. return nil
  103. }
  104. type HcsError struct {
  105. Op string
  106. Err error
  107. Events []ErrorEvent
  108. }
  109. var _ net.Error = &HcsError{}
  110. func (e *HcsError) Error() string {
  111. s := e.Op + ": " + e.Err.Error()
  112. for _, ev := range e.Events {
  113. s += "\n" + ev.String()
  114. }
  115. return s
  116. }
  117. func (e *HcsError) Temporary() bool {
  118. err, ok := e.Err.(net.Error)
  119. return ok && err.Temporary()
  120. }
  121. func (e *HcsError) Timeout() bool {
  122. err, ok := e.Err.(net.Error)
  123. return ok && err.Timeout()
  124. }
  125. // ProcessError is an error encountered in HCS during an operation on a Process object
  126. type ProcessError struct {
  127. SystemID string
  128. Pid int
  129. Op string
  130. Err error
  131. Events []ErrorEvent
  132. }
  133. var _ net.Error = &ProcessError{}
  134. // SystemError is an error encountered in HCS during an operation on a Container object
  135. type SystemError struct {
  136. ID string
  137. Op string
  138. Err error
  139. Events []ErrorEvent
  140. }
  141. var _ net.Error = &SystemError{}
  142. func (e *SystemError) Error() string {
  143. s := e.Op + " " + e.ID + ": " + e.Err.Error()
  144. for _, ev := range e.Events {
  145. s += "\n" + ev.String()
  146. }
  147. return s
  148. }
  149. func (e *SystemError) Temporary() bool {
  150. err, ok := e.Err.(net.Error)
  151. return ok && err.Temporary()
  152. }
  153. func (e *SystemError) Timeout() bool {
  154. err, ok := e.Err.(net.Error)
  155. return ok && err.Timeout()
  156. }
  157. func makeSystemError(system *System, op string, err error, events []ErrorEvent) error {
  158. // Don't double wrap errors
  159. if _, ok := err.(*SystemError); ok {
  160. return err
  161. }
  162. return &SystemError{
  163. ID: system.ID(),
  164. Op: op,
  165. Err: err,
  166. Events: events,
  167. }
  168. }
  169. func (e *ProcessError) Error() string {
  170. s := fmt.Sprintf("%s %s:%d: %s", e.Op, e.SystemID, e.Pid, e.Err.Error())
  171. for _, ev := range e.Events {
  172. s += "\n" + ev.String()
  173. }
  174. return s
  175. }
  176. func (e *ProcessError) Temporary() bool {
  177. err, ok := e.Err.(net.Error)
  178. return ok && err.Temporary()
  179. }
  180. func (e *ProcessError) Timeout() bool {
  181. err, ok := e.Err.(net.Error)
  182. return ok && err.Timeout()
  183. }
  184. func makeProcessError(process *Process, op string, err error, events []ErrorEvent) error {
  185. // Don't double wrap errors
  186. if _, ok := err.(*ProcessError); ok {
  187. return err
  188. }
  189. return &ProcessError{
  190. Pid: process.Pid(),
  191. SystemID: process.SystemID(),
  192. Op: op,
  193. Err: err,
  194. Events: events,
  195. }
  196. }
  197. // IsNotExist checks if an error is caused by the Container or Process not existing.
  198. // Note: Currently, ErrElementNotFound can mean that a Process has either
  199. // already exited, or does not exist. Both IsAlreadyStopped and IsNotExist
  200. // will currently return true when the error is ErrElementNotFound or ErrProcNotFound.
  201. func IsNotExist(err error) bool {
  202. err = getInnerError(err)
  203. return err == ErrComputeSystemDoesNotExist ||
  204. err == ErrElementNotFound ||
  205. err == ErrProcNotFound
  206. }
  207. // IsAlreadyClosed checks if an error is caused by the Container or Process having been
  208. // already closed by a call to the Close() method.
  209. func IsAlreadyClosed(err error) bool {
  210. err = getInnerError(err)
  211. return err == ErrAlreadyClosed
  212. }
  213. // IsPending returns a boolean indicating whether the error is that
  214. // the requested operation is being completed in the background.
  215. func IsPending(err error) bool {
  216. err = getInnerError(err)
  217. return err == ErrVmcomputeOperationPending
  218. }
  219. // IsTimeout returns a boolean indicating whether the error is caused by
  220. // a timeout waiting for the operation to complete.
  221. func IsTimeout(err error) bool {
  222. if err, ok := err.(net.Error); ok && err.Timeout() {
  223. return true
  224. }
  225. err = getInnerError(err)
  226. return err == ErrTimeout
  227. }
  228. // IsAlreadyStopped returns a boolean indicating whether the error is caused by
  229. // a Container or Process being already stopped.
  230. // Note: Currently, ErrElementNotFound can mean that a Process has either
  231. // already exited, or does not exist. Both IsAlreadyStopped and IsNotExist
  232. // will currently return true when the error is ErrElementNotFound or ErrProcNotFound.
  233. func IsAlreadyStopped(err error) bool {
  234. err = getInnerError(err)
  235. return err == ErrVmcomputeAlreadyStopped ||
  236. err == ErrElementNotFound ||
  237. err == ErrProcNotFound
  238. }
  239. // IsNotSupported returns a boolean indicating whether the error is caused by
  240. // unsupported platform requests
  241. // Note: Currently Unsupported platform requests can be mean either
  242. // ErrVmcomputeInvalidJSON, ErrInvalidData, ErrNotSupported or ErrVmcomputeUnknownMessage
  243. // is thrown from the Platform
  244. func IsNotSupported(err error) bool {
  245. err = getInnerError(err)
  246. // If Platform doesn't recognize or support the request sent, below errors are seen
  247. return err == ErrVmcomputeInvalidJSON ||
  248. err == ErrInvalidData ||
  249. err == ErrNotSupported ||
  250. err == ErrVmcomputeUnknownMessage
  251. }
  252. // IsOperationInvalidState returns true when err is caused by
  253. // `ErrVmcomputeOperationInvalidState`.
  254. func IsOperationInvalidState(err error) bool {
  255. err = getInnerError(err)
  256. return err == ErrVmcomputeOperationInvalidState
  257. }
  258. // IsAccessIsDenied returns true when err is caused by
  259. // `ErrVmcomputeOperationAccessIsDenied`.
  260. func IsAccessIsDenied(err error) bool {
  261. err = getInnerError(err)
  262. return err == ErrVmcomputeOperationAccessIsDenied
  263. }
  264. func getInnerError(err error) error {
  265. switch pe := err.(type) {
  266. case nil:
  267. return nil
  268. case *HcsError:
  269. err = pe.Err
  270. case *SystemError:
  271. err = pe.Err
  272. case *ProcessError:
  273. err = pe.Err
  274. }
  275. return err
  276. }