utils.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // +build !windows
  2. /*
  3. Copyright The containerd Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package process
  15. import (
  16. "context"
  17. "encoding/json"
  18. "fmt"
  19. "io"
  20. "os"
  21. "path/filepath"
  22. "strings"
  23. "sync"
  24. "sync/atomic"
  25. "time"
  26. "github.com/containerd/containerd/errdefs"
  27. runc "github.com/containerd/go-runc"
  28. "github.com/pkg/errors"
  29. "golang.org/x/sys/unix"
  30. )
  31. const (
  32. // RuncRoot is the path to the root runc state directory
  33. RuncRoot = "/run/containerd/runc"
  34. // InitPidFile name of the file that contains the init pid
  35. InitPidFile = "init.pid"
  36. )
  37. // safePid is a thread safe wrapper for pid.
  38. type safePid struct {
  39. sync.Mutex
  40. pid int
  41. }
  42. func (s *safePid) get() int {
  43. s.Lock()
  44. defer s.Unlock()
  45. return s.pid
  46. }
  47. type atomicBool int32
  48. func (ab *atomicBool) set(b bool) {
  49. if b {
  50. atomic.StoreInt32((*int32)(ab), 1)
  51. } else {
  52. atomic.StoreInt32((*int32)(ab), 0)
  53. }
  54. }
  55. func (ab *atomicBool) get() bool {
  56. return atomic.LoadInt32((*int32)(ab)) == 1
  57. }
  58. // TODO(mlaventure): move to runc package?
  59. func getLastRuntimeError(r *runc.Runc) (string, error) {
  60. if r.Log == "" {
  61. return "", nil
  62. }
  63. f, err := os.OpenFile(r.Log, os.O_RDONLY, 0400)
  64. if err != nil {
  65. return "", err
  66. }
  67. defer f.Close()
  68. var (
  69. errMsg string
  70. log struct {
  71. Level string
  72. Msg string
  73. Time time.Time
  74. }
  75. )
  76. dec := json.NewDecoder(f)
  77. for err = nil; err == nil; {
  78. if err = dec.Decode(&log); err != nil && err != io.EOF {
  79. return "", err
  80. }
  81. if log.Level == "error" {
  82. errMsg = strings.TrimSpace(log.Msg)
  83. }
  84. }
  85. return errMsg, nil
  86. }
  87. // criuError returns only the first line of the error message from criu
  88. // it tries to add an invalid dump log location when returning the message
  89. func criuError(err error) string {
  90. parts := strings.Split(err.Error(), "\n")
  91. return parts[0]
  92. }
  93. func copyFile(to, from string) error {
  94. ff, err := os.Open(from)
  95. if err != nil {
  96. return err
  97. }
  98. defer ff.Close()
  99. tt, err := os.Create(to)
  100. if err != nil {
  101. return err
  102. }
  103. defer tt.Close()
  104. p := bufPool.Get().(*[]byte)
  105. defer bufPool.Put(p)
  106. _, err = io.CopyBuffer(tt, ff, *p)
  107. return err
  108. }
  109. func checkKillError(err error) error {
  110. if err == nil {
  111. return nil
  112. }
  113. if strings.Contains(err.Error(), "os: process already finished") ||
  114. strings.Contains(err.Error(), "container not running") ||
  115. strings.Contains(strings.ToLower(err.Error()), "no such process") ||
  116. err == unix.ESRCH {
  117. return errors.Wrapf(errdefs.ErrNotFound, "process already finished")
  118. } else if strings.Contains(err.Error(), "does not exist") {
  119. return errors.Wrapf(errdefs.ErrNotFound, "no such container")
  120. }
  121. return errors.Wrapf(err, "unknown error after kill")
  122. }
  123. func newPidFile(bundle string) *pidFile {
  124. return &pidFile{
  125. path: filepath.Join(bundle, InitPidFile),
  126. }
  127. }
  128. func newExecPidFile(bundle, id string) *pidFile {
  129. return &pidFile{
  130. path: filepath.Join(bundle, fmt.Sprintf("%s.pid", id)),
  131. }
  132. }
  133. type pidFile struct {
  134. path string
  135. }
  136. func (p *pidFile) Path() string {
  137. return p.path
  138. }
  139. func (p *pidFile) Read() (int, error) {
  140. return runc.ReadPidFile(p.path)
  141. }
  142. // waitTimeout handles waiting on a waitgroup with a specified timeout.
  143. // this is commonly used for waiting on IO to finish after a process has exited
  144. func waitTimeout(ctx context.Context, wg *sync.WaitGroup, timeout time.Duration) error {
  145. ctx, cancel := context.WithTimeout(ctx, timeout)
  146. defer cancel()
  147. done := make(chan struct{})
  148. go func() {
  149. wg.Wait()
  150. close(done)
  151. }()
  152. select {
  153. case <-done:
  154. return nil
  155. case <-ctx.Done():
  156. return ctx.Err()
  157. }
  158. }
  159. func stateName(v interface{}) string {
  160. switch v.(type) {
  161. case *runningState, *execRunningState:
  162. return "running"
  163. case *createdState, *execCreatedState, *createdCheckpointState:
  164. return "created"
  165. case *pausedState:
  166. return "paused"
  167. case *deletedState:
  168. return "deleted"
  169. case *stoppedState:
  170. return "stopped"
  171. }
  172. panic(errors.Errorf("invalid state %v", v))
  173. }