message_linux.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // +build linux
  2. package libcontainer
  3. import (
  4. "syscall"
  5. "github.com/vishvananda/netlink/nl"
  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. ConsolePathAttr uint16 = 27282
  13. NsPathsAttr uint16 = 27283
  14. UidmapAttr uint16 = 27284
  15. GidmapAttr uint16 = 27285
  16. SetgroupAttr uint16 = 27286
  17. // When syscall.NLA_HDRLEN is in gccgo, take this out.
  18. syscall_NLA_HDRLEN = (syscall.SizeofNlAttr + syscall.NLA_ALIGNTO - 1) & ^(syscall.NLA_ALIGNTO - 1)
  19. )
  20. type Int32msg struct {
  21. Type uint16
  22. Value uint32
  23. }
  24. // int32msg has the following representation
  25. // | nlattr len | nlattr type |
  26. // | uint32 value |
  27. func (msg *Int32msg) Serialize() []byte {
  28. buf := make([]byte, msg.Len())
  29. native := nl.NativeEndian()
  30. native.PutUint16(buf[0:2], uint16(msg.Len()))
  31. native.PutUint16(buf[2:4], msg.Type)
  32. native.PutUint32(buf[4:8], msg.Value)
  33. return buf
  34. }
  35. func (msg *Int32msg) Len() int {
  36. return syscall_NLA_HDRLEN + 4
  37. }
  38. // bytemsg has the following representation
  39. // | nlattr len | nlattr type |
  40. // | value | pad |
  41. type Bytemsg struct {
  42. Type uint16
  43. Value []byte
  44. }
  45. func (msg *Bytemsg) Serialize() []byte {
  46. l := msg.Len()
  47. buf := make([]byte, (l+syscall.NLA_ALIGNTO-1) & ^(syscall.NLA_ALIGNTO-1))
  48. native := nl.NativeEndian()
  49. native.PutUint16(buf[0:2], uint16(l))
  50. native.PutUint16(buf[2:4], msg.Type)
  51. copy(buf[4:], msg.Value)
  52. return buf
  53. }
  54. func (msg *Bytemsg) Len() int {
  55. return syscall_NLA_HDRLEN + len(msg.Value) + 1 // null-terminated
  56. }
  57. type Boolmsg struct {
  58. Type uint16
  59. Value bool
  60. }
  61. func (msg *Boolmsg) Serialize() []byte {
  62. buf := make([]byte, msg.Len())
  63. native := nl.NativeEndian()
  64. native.PutUint16(buf[0:2], uint16(msg.Len()))
  65. native.PutUint16(buf[2:4], msg.Type)
  66. if msg.Value {
  67. buf[4] = 1
  68. } else {
  69. buf[4] = 0
  70. }
  71. return buf
  72. }
  73. func (msg *Boolmsg) Len() int {
  74. return syscall_NLA_HDRLEN + 1
  75. }