species

package
v0.10.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 30, 2020 License: Apache-2.0 Imports: 2 Imported by: 5

Documentation

Overview

Package species defines the type constants and type names of the 7 Linux kernel namespace types ("species"). In addition, this package converts between the type names and their corresponding (Linux kernel) constants.

While Golang's x/sys/unix package finally defines even the formerly missing CLONE_NEWCGROUP constant, this package redefines the namespace-related CLONE_NEWxxx identifiers to be type-safe, so they cannot accidentally be mixed with other CLONE_xxx constants.

Index

Examples

Constants

View Source
const (
	CLONE_NEWNS     = NamespaceType(unix.CLONE_NEWNS)
	CLONE_NEWCGROUP = NamespaceType(unix.CLONE_NEWCGROUP)
	CLONE_NEWUTS    = NamespaceType(unix.CLONE_NEWUTS)
	CLONE_NEWIPC    = NamespaceType(unix.CLONE_NEWIPC)
	CLONE_NEWUSER   = NamespaceType(unix.CLONE_NEWUSER)
	CLONE_NEWPID    = NamespaceType(unix.CLONE_NEWPID)
	CLONE_NEWNET    = NamespaceType(unix.CLONE_NEWNET)
)

The 7 type of Linux namespaces defined at this time (sic!). Well, the 8th namespace for time is already ticking along...

Variables

This section is empty.

Functions

func IDwithType

func IDwithType(s string) (id NamespaceID, t NamespaceType)

IDwithType takes a string representation of a namespace instance, such as "net:[1234]", and returns the ID together with the type of the namespace. In case the string is malformed or contains an unknown namespace type, IDwithType returns (NoneID, NaNS).

Example
id, t := IDwithType("mnt:[12345678]")
fmt.Printf("%q %d\n", t.Name(), id)
// "nonsense" namespace textual representations return an identifier of
// NoneID and a type of NaNS (not a namespace).
id, t = IDwithType("foo:[-1]")
fmt.Printf("%v %v\n", t, id)
Output:

"mnt" 12345678
NaNS NoneID

Types

type NamespaceID

type NamespaceID uint64

NamespaceID represents a Linux kernel namespace identifier. While namespace identifiers currently use only 32bit values, we're playing safe here and keep with the 64bit-ness of inode numbers, as which they originally appear.

const NoneID NamespaceID = 0

NoneID is a convenience constant for signalling an invalid or non-existing namespace identifier.

func (NamespaceID) String

func (nsid NamespaceID) String() string

String returns the namespace identifier as text, or "NoneID", if it is invalid.

type NamespaceType

type NamespaceType uint64

NamespaceType mirrors the data type used in the Linux kernel for the namespace type constants. These constants are actually part of the clone() syscall options parameter.

const NaNS NamespaceType = 0

NaNS identifies an invalid namespace type.

func NameToType

func NameToType(name string) NamespaceType

NameToType returns the namespace type value (constant CLONE_NEWNS, ...) corresponding to the specified namespace type name (such as "mnt", "net", et cetera).

Example
for _, name := range []string{
	"mnt", "cgroup", "uts", "ipc", "user", "pid", "net", "spam",
} {
	fmt.Printf("0x%08x\n", uint64(NameToType(name)))
}
Output:

0x00020000
0x02000000
0x04000000
0x08000000
0x10000000
0x20000000
0x40000000
0x00000000

func (NamespaceType) Name

func (nstype NamespaceType) Name() string

Name returns the type name string (such as "mnt", "net", ...) of a namespace type value.

Example
fmt.Println(CLONE_NEWCGROUP.Name())
Output:

cgroup

func (NamespaceType) String

func (nstype NamespaceType) String() string

String returns the Linux kernel namespace constant name for a given namespace type value.

Example
for _, t := range []NamespaceType{
	CLONE_NEWNS, CLONE_NEWCGROUP, CLONE_NEWUTS, CLONE_NEWIPC,
	CLONE_NEWUSER, CLONE_NEWPID, CLONE_NEWNET,
} {
	fmt.Println(t.String())
}
Output:

CLONE_NEWNS
CLONE_NEWCGROUP
CLONE_NEWUTS
CLONE_NEWIPC
CLONE_NEWUSER
CLONE_NEWPID
CLONE_NEWNET

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL