utils_linux.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package libcontainerd
  2. import (
  3. "syscall"
  4. containerd "github.com/containerd/containerd/api/grpc/types"
  5. "github.com/opencontainers/runtime-spec/specs-go"
  6. )
  7. func getRootIDs(s specs.Spec) (int, int, error) {
  8. var hasUserns bool
  9. for _, ns := range s.Linux.Namespaces {
  10. if ns.Type == specs.UserNamespace {
  11. hasUserns = true
  12. break
  13. }
  14. }
  15. if !hasUserns {
  16. return 0, 0, nil
  17. }
  18. uid := hostIDFromMap(0, s.Linux.UIDMappings)
  19. gid := hostIDFromMap(0, s.Linux.GIDMappings)
  20. return uid, gid, nil
  21. }
  22. func hostIDFromMap(id uint32, mp []specs.LinuxIDMapping) int {
  23. for _, m := range mp {
  24. if id >= m.ContainerID && id <= m.ContainerID+m.Size-1 {
  25. return int(m.HostID + id - m.ContainerID)
  26. }
  27. }
  28. return 0
  29. }
  30. func systemPid(ctr *containerd.Container) uint32 {
  31. var pid uint32
  32. for _, p := range ctr.Processes {
  33. if p.Pid == InitFriendlyName {
  34. pid = p.SystemPid
  35. }
  36. }
  37. return pid
  38. }
  39. func convertRlimits(sr []specs.LinuxRlimit) (cr []*containerd.Rlimit) {
  40. for _, r := range sr {
  41. cr = append(cr, &containerd.Rlimit{
  42. Type: r.Type,
  43. Hard: r.Hard,
  44. Soft: r.Soft,
  45. })
  46. }
  47. return
  48. }
  49. // setPDeathSig sets the parent death signal to SIGKILL
  50. func setSysProcAttr(sid bool) *syscall.SysProcAttr {
  51. return &syscall.SysProcAttr{
  52. Setsid: sid,
  53. Pdeathsig: syscall.SIGKILL,
  54. }
  55. }