message_linux.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // +build linux
  2. package libcontainer
  3. import (
  4. "github.com/vishvananda/netlink/nl"
  5. "golang.org/x/sys/unix"
  6. )
  7. // list of known message types we want to send to bootstrap program
  8. // The number is randomly chosen to not conflict with known netlink types
  9. const (
  10. InitMsg uint16 = 62000
  11. CloneFlagsAttr uint16 = 27281
  12. NsPathsAttr uint16 = 27282
  13. UidmapAttr uint16 = 27283
  14. GidmapAttr uint16 = 27284
  15. SetgroupAttr uint16 = 27285
  16. OomScoreAdjAttr uint16 = 27286
  17. RootlessEUIDAttr uint16 = 27287
  18. UidmapPathAttr uint16 = 27288
  19. GidmapPathAttr uint16 = 27289
  20. )
  21. type Int32msg struct {
  22. Type uint16
  23. Value uint32
  24. }
  25. // Serialize serializes the message.
  26. // Int32msg has the following representation
  27. // | nlattr len | nlattr type |
  28. // | uint32 value |
  29. func (msg *Int32msg) Serialize() []byte {
  30. buf := make([]byte, msg.Len())
  31. native := nl.NativeEndian()
  32. native.PutUint16(buf[0:2], uint16(msg.Len()))
  33. native.PutUint16(buf[2:4], msg.Type)
  34. native.PutUint32(buf[4:8], msg.Value)
  35. return buf
  36. }
  37. func (msg *Int32msg) Len() int {
  38. return unix.NLA_HDRLEN + 4
  39. }
  40. // Bytemsg has the following representation
  41. // | nlattr len | nlattr type |
  42. // | value | pad |
  43. type Bytemsg struct {
  44. Type uint16
  45. Value []byte
  46. }
  47. func (msg *Bytemsg) Serialize() []byte {
  48. l := msg.Len()
  49. buf := make([]byte, (l+unix.NLA_ALIGNTO-1) & ^(unix.NLA_ALIGNTO-1))
  50. native := nl.NativeEndian()
  51. native.PutUint16(buf[0:2], uint16(l))
  52. native.PutUint16(buf[2:4], msg.Type)
  53. copy(buf[4:], msg.Value)
  54. return buf
  55. }
  56. func (msg *Bytemsg) Len() int {
  57. return unix.NLA_HDRLEN + len(msg.Value) + 1 // null-terminated
  58. }
  59. type Boolmsg struct {
  60. Type uint16
  61. Value bool
  62. }
  63. func (msg *Boolmsg) Serialize() []byte {
  64. buf := make([]byte, msg.Len())
  65. native := nl.NativeEndian()
  66. native.PutUint16(buf[0:2], uint16(msg.Len()))
  67. native.PutUint16(buf[2:4], msg.Type)
  68. if msg.Value {
  69. native.PutUint32(buf[4:8], uint32(1))
  70. } else {
  71. native.PutUint32(buf[4:8], uint32(0))
  72. }
  73. return buf
  74. }
  75. func (msg *Boolmsg) Len() int {
  76. return unix.NLA_HDRLEN + 4 // alignment
  77. }