linux.go 3.1 KB
Newer Older
S
stormgbs 已提交
1 2 3 4 5 6 7 8 9
// +build linux

package system

import (
	"os"
	"os/exec"
	"unsafe"

10 11 12
	"github.com/opencontainers/runc/libcontainer/user"
	"golang.org/x/sys/unix"
)
S
stormgbs 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

type ParentDeathSignal int

func (p ParentDeathSignal) Restore() error {
	if p == 0 {
		return nil
	}
	current, err := GetParentDeathSignal()
	if err != nil {
		return err
	}
	if p == current {
		return nil
	}
	return p.Set()
}

func (p ParentDeathSignal) Set() error {
	return SetParentDeathSignal(uintptr(p))
}

func Execv(cmd string, args []string, env []string) error {
	name, err := exec.LookPath(cmd)
	if err != nil {
		return err
	}

40
	return unix.Exec(name, args, env)
S
stormgbs 已提交
41 42
}

43 44
func Prlimit(pid, resource int, limit unix.Rlimit) error {
	_, _, err := unix.RawSyscall6(unix.SYS_PRLIMIT64, uintptr(pid), uintptr(resource), uintptr(unsafe.Pointer(&limit)), uintptr(unsafe.Pointer(&limit)), 0, 0)
S
stormgbs 已提交
45 46 47 48 49 50 51
	if err != 0 {
		return err
	}
	return nil
}

func SetParentDeathSignal(sig uintptr) error {
52
	if err := unix.Prctl(unix.PR_SET_PDEATHSIG, sig, 0, 0, 0); err != nil {
S
stormgbs 已提交
53 54 55 56 57 58 59
		return err
	}
	return nil
}

func GetParentDeathSignal() (ParentDeathSignal, error) {
	var sig int
60
	if err := unix.Prctl(unix.PR_GET_PDEATHSIG, uintptr(unsafe.Pointer(&sig)), 0, 0, 0); err != nil {
S
stormgbs 已提交
61 62 63 64 65 66
		return -1, err
	}
	return ParentDeathSignal(sig), nil
}

func SetKeepCaps() error {
67
	if err := unix.Prctl(unix.PR_SET_KEEPCAPS, 1, 0, 0, 0); err != nil {
S
stormgbs 已提交
68 69 70 71 72 73 74
		return err
	}

	return nil
}

func ClearKeepCaps() error {
75
	if err := unix.Prctl(unix.PR_SET_KEEPCAPS, 0, 0, 0, 0); err != nil {
S
stormgbs 已提交
76 77 78 79 80 81 82
		return err
	}

	return nil
}

func Setctty() error {
83
	if err := unix.IoctlSetInt(0, unix.TIOCSCTTY, 0); err != nil {
S
stormgbs 已提交
84 85 86 87 88
		return err
	}
	return nil
}

89 90
// RunningInUserNS detects whether we are currently running in a user namespace.
// Originally copied from github.com/lxc/lxd/shared/util.go
S
stormgbs 已提交
91
func RunningInUserNS() bool {
92
	uidmap, err := user.CurrentProcessUIDMap()
S
stormgbs 已提交
93
	if err != nil {
94
		// This kernel-provided file only exists if user namespaces are supported
S
stormgbs 已提交
95 96
		return false
	}
97 98
	return UIDMapInUserNS(uidmap)
}
S
stormgbs 已提交
99

100
func UIDMapInUserNS(uidmap []user.IDMap) bool {
S
stormgbs 已提交
101 102 103 104
	/*
	 * We assume we are in the initial user namespace if we have a full
	 * range - 4294967295 uids starting at uid 0.
	 */
105
	if len(uidmap) == 1 && uidmap[0].ID == 0 && uidmap[0].ParentID == 0 && uidmap[0].Count == 4294967295 {
S
stormgbs 已提交
106 107 108 109 110
		return false
	}
	return true
}

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
// GetParentNSeuid returns the euid within the parent user namespace
func GetParentNSeuid() int64 {
	euid := int64(os.Geteuid())
	uidmap, err := user.CurrentProcessUIDMap()
	if err != nil {
		// This kernel-provided file only exists if user namespaces are supported
		return euid
	}
	for _, um := range uidmap {
		if um.ID <= euid && euid <= um.ID+um.Count-1 {
			return um.ParentID + euid - um.ID
		}
	}
	return euid
}

S
stormgbs 已提交
127 128
// SetSubreaper sets the value i as the subreaper setting for the calling process
func SetSubreaper(i int) error {
129
	return unix.Prctl(unix.PR_SET_CHILD_SUBREAPER, uintptr(i), 0, 0, 0)
S
stormgbs 已提交
130 131
}

132 133 134 135 136 137
// GetSubreaper returns the subreaper setting for the calling process
func GetSubreaper() (int, error) {
	var i uintptr

	if err := unix.Prctl(unix.PR_GET_CHILD_SUBREAPER, uintptr(unsafe.Pointer(&i)), 0, 0, 0); err != nil {
		return -1, err
S
stormgbs 已提交
138
	}
139 140

	return int(i), nil
S
stormgbs 已提交
141
}