utils.go 906 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package runc
  2. import (
  3. "bytes"
  4. "io/ioutil"
  5. "strconv"
  6. "sync"
  7. "syscall"
  8. )
  9. // ReadPidFile reads the pid file at the provided path and returns
  10. // the pid or an error if the read and conversion is unsuccessful
  11. func ReadPidFile(path string) (int, error) {
  12. data, err := ioutil.ReadFile(path)
  13. if err != nil {
  14. return -1, err
  15. }
  16. return strconv.Atoi(string(data))
  17. }
  18. const exitSignalOffset = 128
  19. // exitStatus returns the correct exit status for a process based on if it
  20. // was signaled or exited cleanly
  21. func exitStatus(status syscall.WaitStatus) int {
  22. if status.Signaled() {
  23. return exitSignalOffset + int(status.Signal())
  24. }
  25. return status.ExitStatus()
  26. }
  27. var bytesBufferPool = sync.Pool{
  28. New: func() interface{} {
  29. return bytes.NewBuffer(nil)
  30. },
  31. }
  32. func getBuf() *bytes.Buffer {
  33. return bytesBufferPool.Get().(*bytes.Buffer)
  34. }
  35. func putBuf(b *bytes.Buffer) {
  36. b.Reset()
  37. bytesBufferPool.Put(b)
  38. }