monitor.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. Copyright The containerd Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package runc
  14. import (
  15. "os/exec"
  16. "runtime"
  17. "syscall"
  18. "time"
  19. )
  20. // Monitor is the default ProcessMonitor for handling runc process exit
  21. var Monitor ProcessMonitor = &defaultMonitor{}
  22. // Exit holds the exit information from a process
  23. type Exit struct {
  24. Timestamp time.Time
  25. Pid int
  26. Status int
  27. }
  28. // ProcessMonitor is an interface for process monitoring.
  29. //
  30. // It allows daemons using go-runc to have a SIGCHLD handler
  31. // to handle exits without introducing races between the handler
  32. // and go's exec.Cmd.
  33. //
  34. // ProcessMonitor also provides a StartLocked method which is similar to
  35. // Start, but locks the goroutine used to start the process to an OS thread
  36. // (for example: when Pdeathsig is set).
  37. type ProcessMonitor interface {
  38. Start(*exec.Cmd) (chan Exit, error)
  39. StartLocked(*exec.Cmd) (chan Exit, error)
  40. Wait(*exec.Cmd, chan Exit) (int, error)
  41. }
  42. type defaultMonitor struct{}
  43. func (m *defaultMonitor) Start(c *exec.Cmd) (chan Exit, error) {
  44. if err := c.Start(); err != nil {
  45. return nil, err
  46. }
  47. ec := make(chan Exit, 1)
  48. go func() {
  49. var status int
  50. if err := c.Wait(); err != nil {
  51. status = 255
  52. if exitErr, ok := err.(*exec.ExitError); ok {
  53. if ws, ok := exitErr.Sys().(syscall.WaitStatus); ok {
  54. status = ws.ExitStatus()
  55. }
  56. }
  57. }
  58. ec <- Exit{
  59. Timestamp: time.Now(),
  60. Pid: c.Process.Pid,
  61. Status: status,
  62. }
  63. close(ec)
  64. }()
  65. return ec, nil
  66. }
  67. // StartLocked is like Start, but locks the goroutine used to start the process to
  68. // the OS thread for use-cases where the parent thread matters to the child process
  69. // (for example: when Pdeathsig is set).
  70. func (m *defaultMonitor) StartLocked(c *exec.Cmd) (chan Exit, error) {
  71. started := make(chan error)
  72. ec := make(chan Exit, 1)
  73. go func() {
  74. runtime.LockOSThread()
  75. defer runtime.UnlockOSThread()
  76. if err := c.Start(); err != nil {
  77. started <- err
  78. return
  79. }
  80. close(started)
  81. var status int
  82. if err := c.Wait(); err != nil {
  83. status = 255
  84. if exitErr, ok := err.(*exec.ExitError); ok {
  85. if ws, ok := exitErr.Sys().(syscall.WaitStatus); ok {
  86. status = ws.ExitStatus()
  87. }
  88. }
  89. }
  90. ec <- Exit{
  91. Timestamp: time.Now(),
  92. Pid: c.Process.Pid,
  93. Status: status,
  94. }
  95. close(ec)
  96. }()
  97. if err := <-started; err != nil {
  98. return nil, err
  99. }
  100. return ec, nil
  101. }
  102. func (m *defaultMonitor) Wait(c *exec.Cmd, ec chan Exit) (int, error) {
  103. e := <-ec
  104. return e.Status, nil
  105. }