...

Source file src/github.com/opencontainers/runc/libcontainer/message_linux.go

Documentation: github.com/opencontainers/runc/libcontainer

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

View as plain text