2015-10-17 23:35:36 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package libcontainer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/vishvananda/netlink/nl"
|
|
|
|
)
|
|
|
|
|
|
|
|
// list of known message types we want to send to bootstrap program
|
|
|
|
// The number is randomly chosen to not conflict with known netlink types
|
|
|
|
const (
|
2016-06-03 23:29:34 +08:00
|
|
|
InitMsg uint16 = 62000
|
|
|
|
CloneFlagsAttr uint16 = 27281
|
|
|
|
NsPathsAttr uint16 = 27282
|
|
|
|
UidmapAttr uint16 = 27283
|
|
|
|
GidmapAttr uint16 = 27284
|
|
|
|
SetgroupAttr uint16 = 27285
|
2015-12-18 00:46:38 +08:00
|
|
|
// When syscall.NLA_HDRLEN is in gccgo, take this out.
|
|
|
|
syscall_NLA_HDRLEN = (syscall.SizeofNlAttr + syscall.NLA_ALIGNTO - 1) & ^(syscall.NLA_ALIGNTO - 1)
|
2015-10-17 23:35:36 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Int32msg struct {
|
|
|
|
Type uint16
|
|
|
|
Value uint32
|
|
|
|
}
|
|
|
|
|
2016-04-12 16:12:23 +08:00
|
|
|
// Serialize serializes the message.
|
|
|
|
// Int32msg has the following representation
|
2015-10-17 23:35:36 +08:00
|
|
|
// | nlattr len | nlattr type |
|
|
|
|
// | uint32 value |
|
|
|
|
func (msg *Int32msg) Serialize() []byte {
|
|
|
|
buf := make([]byte, msg.Len())
|
|
|
|
native := nl.NativeEndian()
|
|
|
|
native.PutUint16(buf[0:2], uint16(msg.Len()))
|
|
|
|
native.PutUint16(buf[2:4], msg.Type)
|
|
|
|
native.PutUint32(buf[4:8], msg.Value)
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msg *Int32msg) Len() int {
|
2015-12-18 00:46:38 +08:00
|
|
|
return syscall_NLA_HDRLEN + 4
|
2015-10-17 23:35:36 +08:00
|
|
|
}
|
|
|
|
|
2016-04-12 16:12:23 +08:00
|
|
|
// Bytemsg has the following representation
|
2015-10-17 23:35:36 +08:00
|
|
|
// | nlattr len | nlattr type |
|
|
|
|
// | value | pad |
|
|
|
|
type Bytemsg struct {
|
|
|
|
Type uint16
|
|
|
|
Value []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msg *Bytemsg) Serialize() []byte {
|
|
|
|
l := msg.Len()
|
|
|
|
buf := make([]byte, (l+syscall.NLA_ALIGNTO-1) & ^(syscall.NLA_ALIGNTO-1))
|
|
|
|
native := nl.NativeEndian()
|
|
|
|
native.PutUint16(buf[0:2], uint16(l))
|
|
|
|
native.PutUint16(buf[2:4], msg.Type)
|
|
|
|
copy(buf[4:], msg.Value)
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msg *Bytemsg) Len() int {
|
2015-12-18 00:46:38 +08:00
|
|
|
return syscall_NLA_HDRLEN + len(msg.Value) + 1 // null-terminated
|
2015-10-17 23:35:36 +08:00
|
|
|
}
|
2015-09-14 08:40:43 +08:00
|
|
|
|
|
|
|
type Boolmsg struct {
|
|
|
|
Type uint16
|
|
|
|
Value bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msg *Boolmsg) Serialize() []byte {
|
|
|
|
buf := make([]byte, msg.Len())
|
|
|
|
native := nl.NativeEndian()
|
|
|
|
native.PutUint16(buf[0:2], uint16(msg.Len()))
|
|
|
|
native.PutUint16(buf[2:4], msg.Type)
|
|
|
|
if msg.Value {
|
|
|
|
buf[4] = 1
|
|
|
|
} else {
|
|
|
|
buf[4] = 0
|
|
|
|
}
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msg *Boolmsg) Len() int {
|
|
|
|
return syscall_NLA_HDRLEN + 1
|
|
|
|
}
|