message_linux.go 2.3 KB

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