container.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package runhcs
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "os"
  9. "syscall"
  10. "time"
  11. "github.com/Microsoft/hcsshim/internal/guid"
  12. )
  13. // ContainerState represents the platform agnostic pieces relating to a
  14. // running container's status and state
  15. type ContainerState struct {
  16. // Version is the OCI version for the container
  17. Version string `json:"ociVersion"`
  18. // ID is the container ID
  19. ID string `json:"id"`
  20. // InitProcessPid is the init process id in the parent namespace
  21. InitProcessPid int `json:"pid"`
  22. // Status is the current status of the container, running, paused, ...
  23. Status string `json:"status"`
  24. // Bundle is the path on the filesystem to the bundle
  25. Bundle string `json:"bundle"`
  26. // Rootfs is a path to a directory containing the container's root filesystem.
  27. Rootfs string `json:"rootfs"`
  28. // Created is the unix timestamp for the creation time of the container in UTC
  29. Created time.Time `json:"created"`
  30. // Annotations is the user defined annotations added to the config.
  31. Annotations map[string]string `json:"annotations,omitempty"`
  32. // The owner of the state directory (the owner of the container).
  33. Owner string `json:"owner"`
  34. }
  35. // GetErrorFromPipe returns reads from `pipe` and verifies if the operation
  36. // returned success or error. If error converts that to an error and returns. If
  37. // `p` is not nill will issue a `Kill` and `Wait` for exit.
  38. func GetErrorFromPipe(pipe io.Reader, p *os.Process) error {
  39. serr, err := ioutil.ReadAll(pipe)
  40. if err != nil {
  41. return err
  42. }
  43. if bytes.Equal(serr, ShimSuccess) {
  44. return nil
  45. }
  46. extra := ""
  47. if p != nil {
  48. p.Kill()
  49. state, err := p.Wait()
  50. if err != nil {
  51. panic(err)
  52. }
  53. extra = fmt.Sprintf(", exit code %d", state.Sys().(syscall.WaitStatus).ExitCode)
  54. }
  55. if len(serr) == 0 {
  56. return fmt.Errorf("unknown shim failure%s", extra)
  57. }
  58. return errors.New(string(serr))
  59. }
  60. // VMPipePath returns the named pipe path for the vm shim.
  61. func VMPipePath(hostUniqueID guid.GUID) string {
  62. return SafePipePath("runhcs-vm-" + hostUniqueID.String())
  63. }