process.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. //go:build windows
  2. package hcs
  3. import (
  4. "context"
  5. "encoding/json"
  6. "errors"
  7. "io"
  8. "os"
  9. "sync"
  10. "syscall"
  11. "time"
  12. "github.com/Microsoft/hcsshim/internal/cow"
  13. "github.com/Microsoft/hcsshim/internal/log"
  14. "github.com/Microsoft/hcsshim/internal/oc"
  15. "github.com/Microsoft/hcsshim/internal/vmcompute"
  16. "go.opencensus.io/trace"
  17. )
  18. // ContainerError is an error encountered in HCS
  19. type Process struct {
  20. handleLock sync.RWMutex
  21. handle vmcompute.HcsProcess
  22. processID int
  23. system *System
  24. hasCachedStdio bool
  25. stdioLock sync.Mutex
  26. stdin io.WriteCloser
  27. stdout io.ReadCloser
  28. stderr io.ReadCloser
  29. callbackNumber uintptr
  30. killSignalDelivered bool
  31. closedWaitOnce sync.Once
  32. waitBlock chan struct{}
  33. exitCode int
  34. waitError error
  35. }
  36. var _ cow.Process = &Process{}
  37. func newProcess(process vmcompute.HcsProcess, processID int, computeSystem *System) *Process {
  38. return &Process{
  39. handle: process,
  40. processID: processID,
  41. system: computeSystem,
  42. waitBlock: make(chan struct{}),
  43. }
  44. }
  45. type processModifyRequest struct {
  46. Operation string
  47. ConsoleSize *consoleSize `json:",omitempty"`
  48. CloseHandle *closeHandle `json:",omitempty"`
  49. }
  50. type consoleSize struct {
  51. Height uint16
  52. Width uint16
  53. }
  54. type closeHandle struct {
  55. Handle string
  56. }
  57. type processStatus struct {
  58. ProcessID uint32
  59. Exited bool
  60. ExitCode uint32
  61. LastWaitResult int32
  62. }
  63. const stdIn string = "StdIn"
  64. const (
  65. modifyConsoleSize string = "ConsoleSize"
  66. modifyCloseHandle string = "CloseHandle"
  67. )
  68. // Pid returns the process ID of the process within the container.
  69. func (process *Process) Pid() int {
  70. return process.processID
  71. }
  72. // SystemID returns the ID of the process's compute system.
  73. func (process *Process) SystemID() string {
  74. return process.system.ID()
  75. }
  76. func (process *Process) processSignalResult(ctx context.Context, err error) (bool, error) {
  77. switch err {
  78. case nil:
  79. return true, nil
  80. case ErrVmcomputeOperationInvalidState, ErrComputeSystemDoesNotExist, ErrElementNotFound:
  81. if !process.stopped() {
  82. // The process should be gone, but we have not received the notification.
  83. // After a second, force unblock the process wait to work around a possible
  84. // deadlock in the HCS.
  85. go func() {
  86. time.Sleep(time.Second)
  87. process.closedWaitOnce.Do(func() {
  88. log.G(ctx).WithError(err).Warn("force unblocking process waits")
  89. process.exitCode = -1
  90. process.waitError = err
  91. close(process.waitBlock)
  92. })
  93. }()
  94. }
  95. return false, nil
  96. default:
  97. return false, err
  98. }
  99. }
  100. // Signal signals the process with `options`.
  101. //
  102. // For LCOW `guestresource.SignalProcessOptionsLCOW`.
  103. //
  104. // For WCOW `guestresource.SignalProcessOptionsWCOW`.
  105. func (process *Process) Signal(ctx context.Context, options interface{}) (bool, error) {
  106. process.handleLock.RLock()
  107. defer process.handleLock.RUnlock()
  108. operation := "hcs::Process::Signal"
  109. if process.handle == 0 {
  110. return false, makeProcessError(process, operation, ErrAlreadyClosed, nil)
  111. }
  112. optionsb, err := json.Marshal(options)
  113. if err != nil {
  114. return false, err
  115. }
  116. resultJSON, err := vmcompute.HcsSignalProcess(ctx, process.handle, string(optionsb))
  117. events := processHcsResult(ctx, resultJSON)
  118. delivered, err := process.processSignalResult(ctx, err)
  119. if err != nil {
  120. err = makeProcessError(process, operation, err, events)
  121. }
  122. return delivered, err
  123. }
  124. // Kill signals the process to terminate but does not wait for it to finish terminating.
  125. func (process *Process) Kill(ctx context.Context) (bool, error) {
  126. process.handleLock.RLock()
  127. defer process.handleLock.RUnlock()
  128. operation := "hcs::Process::Kill"
  129. if process.handle == 0 {
  130. return false, makeProcessError(process, operation, ErrAlreadyClosed, nil)
  131. }
  132. if process.stopped() {
  133. return false, makeProcessError(process, operation, ErrProcessAlreadyStopped, nil)
  134. }
  135. if process.killSignalDelivered {
  136. // A kill signal has already been sent to this process. Sending a second
  137. // one offers no real benefit, as processes cannot stop themselves from
  138. // being terminated, once a TerminateProcess has been issued. Sending a
  139. // second kill may result in a number of errors (two of which detailed bellow)
  140. // and which we can avoid handling.
  141. return true, nil
  142. }
  143. // HCS serializes the signals sent to a target pid per compute system handle.
  144. // To avoid SIGKILL being serialized behind other signals, we open a new compute
  145. // system handle to deliver the kill signal.
  146. // If the calls to opening a new compute system handle fail, we forcefully
  147. // terminate the container itself so that no container is left behind
  148. hcsSystem, err := OpenComputeSystem(ctx, process.system.id)
  149. if err != nil {
  150. // log error and force termination of container
  151. log.G(ctx).WithField("err", err).Error("OpenComputeSystem() call failed")
  152. err = process.system.Terminate(ctx)
  153. // if the Terminate() call itself ever failed, log and return error
  154. if err != nil {
  155. log.G(ctx).WithField("err", err).Error("Terminate() call failed")
  156. return false, err
  157. }
  158. process.system.Close()
  159. return true, nil
  160. }
  161. defer hcsSystem.Close()
  162. newProcessHandle, err := hcsSystem.OpenProcess(ctx, process.Pid())
  163. if err != nil {
  164. // Return true only if the target process has either already
  165. // exited, or does not exist.
  166. if IsAlreadyStopped(err) {
  167. return true, nil
  168. } else {
  169. return false, err
  170. }
  171. }
  172. defer newProcessHandle.Close()
  173. resultJSON, err := vmcompute.HcsTerminateProcess(ctx, newProcessHandle.handle)
  174. if err != nil {
  175. // We still need to check these two cases, as processes may still be killed by an
  176. // external actor (human operator, OOM, random script etc).
  177. if errors.Is(err, os.ErrPermission) || IsAlreadyStopped(err) {
  178. // There are two cases where it should be safe to ignore an error returned
  179. // by HcsTerminateProcess. The first one is cause by the fact that
  180. // HcsTerminateProcess ends up calling TerminateProcess in the context
  181. // of a container. According to the TerminateProcess documentation:
  182. // https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-terminateprocess#remarks
  183. // After a process has terminated, call to TerminateProcess with open
  184. // handles to the process fails with ERROR_ACCESS_DENIED (5) error code.
  185. // It's safe to ignore this error here. HCS should always have permissions
  186. // to kill processes inside any container. So an ERROR_ACCESS_DENIED
  187. // is unlikely to be anything else than what the ending remarks in the
  188. // documentation states.
  189. //
  190. // The second case is generated by hcs itself, if for any reason HcsTerminateProcess
  191. // is called twice in a very short amount of time. In such cases, hcs may return
  192. // HCS_E_PROCESS_ALREADY_STOPPED.
  193. return true, nil
  194. }
  195. }
  196. events := processHcsResult(ctx, resultJSON)
  197. delivered, err := newProcessHandle.processSignalResult(ctx, err)
  198. if err != nil {
  199. err = makeProcessError(newProcessHandle, operation, err, events)
  200. }
  201. process.killSignalDelivered = delivered
  202. return delivered, err
  203. }
  204. // waitBackground waits for the process exit notification. Once received sets
  205. // `process.waitError` (if any) and unblocks all `Wait` calls.
  206. //
  207. // This MUST be called exactly once per `process.handle` but `Wait` is safe to
  208. // call multiple times.
  209. func (process *Process) waitBackground() {
  210. operation := "hcs::Process::waitBackground"
  211. ctx, span := oc.StartSpan(context.Background(), operation)
  212. defer span.End()
  213. span.AddAttributes(
  214. trace.StringAttribute("cid", process.SystemID()),
  215. trace.Int64Attribute("pid", int64(process.processID)))
  216. var (
  217. err error
  218. exitCode = -1
  219. propertiesJSON string
  220. resultJSON string
  221. )
  222. err = waitForNotification(ctx, process.callbackNumber, hcsNotificationProcessExited, nil)
  223. if err != nil {
  224. err = makeProcessError(process, operation, err, nil)
  225. log.G(ctx).WithError(err).Error("failed wait")
  226. } else {
  227. process.handleLock.RLock()
  228. defer process.handleLock.RUnlock()
  229. // Make sure we didnt race with Close() here
  230. if process.handle != 0 {
  231. propertiesJSON, resultJSON, err = vmcompute.HcsGetProcessProperties(ctx, process.handle)
  232. events := processHcsResult(ctx, resultJSON)
  233. if err != nil {
  234. err = makeProcessError(process, operation, err, events)
  235. } else {
  236. properties := &processStatus{}
  237. err = json.Unmarshal([]byte(propertiesJSON), properties)
  238. if err != nil {
  239. err = makeProcessError(process, operation, err, nil)
  240. } else {
  241. if properties.LastWaitResult != 0 {
  242. log.G(ctx).WithField("wait-result", properties.LastWaitResult).Warning("non-zero last wait result")
  243. } else {
  244. exitCode = int(properties.ExitCode)
  245. }
  246. }
  247. }
  248. }
  249. }
  250. log.G(ctx).WithField("exitCode", exitCode).Debug("process exited")
  251. process.closedWaitOnce.Do(func() {
  252. process.exitCode = exitCode
  253. process.waitError = err
  254. close(process.waitBlock)
  255. })
  256. oc.SetSpanStatus(span, err)
  257. }
  258. // Wait waits for the process to exit. If the process has already exited returns
  259. // the previous error (if any).
  260. func (process *Process) Wait() error {
  261. <-process.waitBlock
  262. return process.waitError
  263. }
  264. // Exited returns if the process has stopped
  265. func (process *Process) stopped() bool {
  266. select {
  267. case <-process.waitBlock:
  268. return true
  269. default:
  270. return false
  271. }
  272. }
  273. // ResizeConsole resizes the console of the process.
  274. func (process *Process) ResizeConsole(ctx context.Context, width, height uint16) error {
  275. process.handleLock.RLock()
  276. defer process.handleLock.RUnlock()
  277. operation := "hcs::Process::ResizeConsole"
  278. if process.handle == 0 {
  279. return makeProcessError(process, operation, ErrAlreadyClosed, nil)
  280. }
  281. modifyRequest := processModifyRequest{
  282. Operation: modifyConsoleSize,
  283. ConsoleSize: &consoleSize{
  284. Height: height,
  285. Width: width,
  286. },
  287. }
  288. modifyRequestb, err := json.Marshal(modifyRequest)
  289. if err != nil {
  290. return err
  291. }
  292. resultJSON, err := vmcompute.HcsModifyProcess(ctx, process.handle, string(modifyRequestb))
  293. events := processHcsResult(ctx, resultJSON)
  294. if err != nil {
  295. return makeProcessError(process, operation, err, events)
  296. }
  297. return nil
  298. }
  299. // ExitCode returns the exit code of the process. The process must have
  300. // already terminated.
  301. func (process *Process) ExitCode() (int, error) {
  302. if !process.stopped() {
  303. return -1, makeProcessError(process, "hcs::Process::ExitCode", ErrInvalidProcessState, nil)
  304. }
  305. if process.waitError != nil {
  306. return -1, process.waitError
  307. }
  308. return process.exitCode, nil
  309. }
  310. // StdioLegacy returns the stdin, stdout, and stderr pipes, respectively. Closing
  311. // these pipes does not close the underlying pipes. Once returned, these pipes
  312. // are the responsibility of the caller to close.
  313. func (process *Process) StdioLegacy() (_ io.WriteCloser, _ io.ReadCloser, _ io.ReadCloser, err error) {
  314. operation := "hcs::Process::StdioLegacy"
  315. ctx, span := oc.StartSpan(context.Background(), operation)
  316. defer span.End()
  317. defer func() { oc.SetSpanStatus(span, err) }()
  318. span.AddAttributes(
  319. trace.StringAttribute("cid", process.SystemID()),
  320. trace.Int64Attribute("pid", int64(process.processID)))
  321. process.handleLock.RLock()
  322. defer process.handleLock.RUnlock()
  323. if process.handle == 0 {
  324. return nil, nil, nil, makeProcessError(process, operation, ErrAlreadyClosed, nil)
  325. }
  326. process.stdioLock.Lock()
  327. defer process.stdioLock.Unlock()
  328. if process.hasCachedStdio {
  329. stdin, stdout, stderr := process.stdin, process.stdout, process.stderr
  330. process.stdin, process.stdout, process.stderr = nil, nil, nil
  331. process.hasCachedStdio = false
  332. return stdin, stdout, stderr, nil
  333. }
  334. processInfo, resultJSON, err := vmcompute.HcsGetProcessInfo(ctx, process.handle)
  335. events := processHcsResult(ctx, resultJSON)
  336. if err != nil {
  337. return nil, nil, nil, makeProcessError(process, operation, err, events)
  338. }
  339. pipes, err := makeOpenFiles([]syscall.Handle{processInfo.StdInput, processInfo.StdOutput, processInfo.StdError})
  340. if err != nil {
  341. return nil, nil, nil, makeProcessError(process, operation, err, nil)
  342. }
  343. return pipes[0], pipes[1], pipes[2], nil
  344. }
  345. // Stdio returns the stdin, stdout, and stderr pipes, respectively.
  346. // To close them, close the process handle, or use the `CloseStd*` functions.
  347. func (process *Process) Stdio() (stdin io.Writer, stdout, stderr io.Reader) {
  348. process.stdioLock.Lock()
  349. defer process.stdioLock.Unlock()
  350. return process.stdin, process.stdout, process.stderr
  351. }
  352. // CloseStdin closes the write side of the stdin pipe so that the process is
  353. // notified on the read side that there is no more data in stdin.
  354. func (process *Process) CloseStdin(ctx context.Context) (err error) {
  355. operation := "hcs::Process::CloseStdin"
  356. ctx, span := trace.StartSpan(ctx, operation)
  357. defer span.End()
  358. defer func() { oc.SetSpanStatus(span, err) }()
  359. span.AddAttributes(
  360. trace.StringAttribute("cid", process.SystemID()),
  361. trace.Int64Attribute("pid", int64(process.processID)))
  362. process.handleLock.RLock()
  363. defer process.handleLock.RUnlock()
  364. if process.handle == 0 {
  365. return makeProcessError(process, operation, ErrAlreadyClosed, nil)
  366. }
  367. //HcsModifyProcess request to close stdin will fail if the process has already exited
  368. if !process.stopped() {
  369. modifyRequest := processModifyRequest{
  370. Operation: modifyCloseHandle,
  371. CloseHandle: &closeHandle{
  372. Handle: stdIn,
  373. },
  374. }
  375. modifyRequestb, err := json.Marshal(modifyRequest)
  376. if err != nil {
  377. return err
  378. }
  379. resultJSON, err := vmcompute.HcsModifyProcess(ctx, process.handle, string(modifyRequestb))
  380. events := processHcsResult(ctx, resultJSON)
  381. if err != nil {
  382. return makeProcessError(process, operation, err, events)
  383. }
  384. }
  385. process.stdioLock.Lock()
  386. defer process.stdioLock.Unlock()
  387. if process.stdin != nil {
  388. process.stdin.Close()
  389. process.stdin = nil
  390. }
  391. return nil
  392. }
  393. func (process *Process) CloseStdout(ctx context.Context) (err error) {
  394. ctx, span := oc.StartSpan(ctx, "hcs::Process::CloseStdout") //nolint:ineffassign,staticcheck
  395. defer span.End()
  396. defer func() { oc.SetSpanStatus(span, err) }()
  397. span.AddAttributes(
  398. trace.StringAttribute("cid", process.SystemID()),
  399. trace.Int64Attribute("pid", int64(process.processID)))
  400. process.handleLock.Lock()
  401. defer process.handleLock.Unlock()
  402. if process.handle == 0 {
  403. return nil
  404. }
  405. process.stdioLock.Lock()
  406. defer process.stdioLock.Unlock()
  407. if process.stdout != nil {
  408. process.stdout.Close()
  409. process.stdout = nil
  410. }
  411. return nil
  412. }
  413. func (process *Process) CloseStderr(ctx context.Context) (err error) {
  414. ctx, span := oc.StartSpan(ctx, "hcs::Process::CloseStderr") //nolint:ineffassign,staticcheck
  415. defer span.End()
  416. defer func() { oc.SetSpanStatus(span, err) }()
  417. span.AddAttributes(
  418. trace.StringAttribute("cid", process.SystemID()),
  419. trace.Int64Attribute("pid", int64(process.processID)))
  420. process.handleLock.Lock()
  421. defer process.handleLock.Unlock()
  422. if process.handle == 0 {
  423. return nil
  424. }
  425. process.stdioLock.Lock()
  426. defer process.stdioLock.Unlock()
  427. if process.stderr != nil {
  428. process.stderr.Close()
  429. process.stderr = nil
  430. }
  431. return nil
  432. }
  433. // Close cleans up any state associated with the process but does not kill
  434. // or wait on it.
  435. func (process *Process) Close() (err error) {
  436. operation := "hcs::Process::Close"
  437. ctx, span := oc.StartSpan(context.Background(), operation)
  438. defer span.End()
  439. defer func() { oc.SetSpanStatus(span, err) }()
  440. span.AddAttributes(
  441. trace.StringAttribute("cid", process.SystemID()),
  442. trace.Int64Attribute("pid", int64(process.processID)))
  443. process.handleLock.Lock()
  444. defer process.handleLock.Unlock()
  445. // Don't double free this
  446. if process.handle == 0 {
  447. return nil
  448. }
  449. process.stdioLock.Lock()
  450. if process.stdin != nil {
  451. process.stdin.Close()
  452. process.stdin = nil
  453. }
  454. if process.stdout != nil {
  455. process.stdout.Close()
  456. process.stdout = nil
  457. }
  458. if process.stderr != nil {
  459. process.stderr.Close()
  460. process.stderr = nil
  461. }
  462. process.stdioLock.Unlock()
  463. if err = process.unregisterCallback(ctx); err != nil {
  464. return makeProcessError(process, operation, err, nil)
  465. }
  466. if err = vmcompute.HcsCloseProcess(ctx, process.handle); err != nil {
  467. return makeProcessError(process, operation, err, nil)
  468. }
  469. process.handle = 0
  470. process.closedWaitOnce.Do(func() {
  471. process.exitCode = -1
  472. process.waitError = ErrAlreadyClosed
  473. close(process.waitBlock)
  474. })
  475. return nil
  476. }
  477. func (process *Process) registerCallback(ctx context.Context) error {
  478. callbackContext := &notificationWatcherContext{
  479. channels: newProcessChannels(),
  480. systemID: process.SystemID(),
  481. processID: process.processID,
  482. }
  483. callbackMapLock.Lock()
  484. callbackNumber := nextCallback
  485. nextCallback++
  486. callbackMap[callbackNumber] = callbackContext
  487. callbackMapLock.Unlock()
  488. callbackHandle, err := vmcompute.HcsRegisterProcessCallback(ctx, process.handle, notificationWatcherCallback, callbackNumber)
  489. if err != nil {
  490. return err
  491. }
  492. callbackContext.handle = callbackHandle
  493. process.callbackNumber = callbackNumber
  494. return nil
  495. }
  496. func (process *Process) unregisterCallback(ctx context.Context) error {
  497. callbackNumber := process.callbackNumber
  498. callbackMapLock.RLock()
  499. callbackContext := callbackMap[callbackNumber]
  500. callbackMapLock.RUnlock()
  501. if callbackContext == nil {
  502. return nil
  503. }
  504. handle := callbackContext.handle
  505. if handle == 0 {
  506. return nil
  507. }
  508. // vmcompute.HcsUnregisterProcessCallback has its own synchronization to
  509. // wait for all callbacks to complete. We must NOT hold the callbackMapLock.
  510. err := vmcompute.HcsUnregisterProcessCallback(ctx, handle)
  511. if err != nil {
  512. return err
  513. }
  514. closeChannels(callbackContext.channels)
  515. callbackMapLock.Lock()
  516. delete(callbackMap, callbackNumber)
  517. callbackMapLock.Unlock()
  518. handle = 0 //nolint:ineffassign
  519. return nil
  520. }