Skip to content

Commit badd60d

Browse files
committed
sys: un-export runningPrivileged(), remove runningUnprivileged()
These are currently only used inside this package, so we might as well un-export them until we need them elsewhere. Also updated SetOOMScore() to first check for privileged; check for privileged looks to be the "faster" path, and checking it first could (in case of non- privileged) save having to read and parse /proc/self/uid_map. Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 8b00eaf commit badd60d

4 files changed

Lines changed: 11 additions & 37 deletions

File tree

sys/env.go

Lines changed: 0 additions & 33 deletions
This file was deleted.

sys/mount_linux_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import (
3232
type fMountatCaseFunc func(t *testing.T, root string)
3333

3434
func TestFMountat(t *testing.T) {
35-
if RunningUnprivileged() {
35+
if !runningPrivileged() {
3636
t.Skip("Needs to be run as root")
3737
return
3838
}

sys/oom_linux.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"strings"
2525

2626
"github.com/containerd/containerd/pkg/userns"
27+
"golang.org/x/sys/unix"
2728
)
2829

2930
const (
@@ -42,7 +43,7 @@ func SetOOMScore(pid, score int) error {
4243
}
4344
defer f.Close()
4445
if _, err = f.WriteString(strconv.Itoa(score)); err != nil {
45-
if os.IsPermission(err) && (userns.RunningInUserNS() || RunningUnprivileged()) {
46+
if os.IsPermission(err) && (!runningPrivileged() || userns.RunningInUserNS()) {
4647
return nil
4748
}
4849
return err
@@ -59,3 +60,9 @@ func GetOOMScoreAdj(pid int) (int, error) {
5960
}
6061
return strconv.Atoi(strings.TrimSpace(string(data)))
6162
}
63+
64+
// runningPrivileged returns true if the effective user ID of the
65+
// calling process is 0
66+
func runningPrivileged() bool {
67+
return unix.Geteuid() == 0
68+
}

sys/oom_linux_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func TestSetPositiveOomScoreAdjustment(t *testing.T) {
3737
}
3838

3939
func TestSetNegativeOomScoreAdjustmentWhenPrivileged(t *testing.T) {
40-
if RunningUnprivileged() {
40+
if !runningPrivileged() {
4141
t.Skip("Needs to be run as root")
4242
return
4343
}
@@ -51,7 +51,7 @@ func TestSetNegativeOomScoreAdjustmentWhenPrivileged(t *testing.T) {
5151
}
5252

5353
func TestSetNegativeOomScoreAdjustmentWhenUnprivilegedHasNoEffect(t *testing.T) {
54-
if RunningPrivileged() {
54+
if runningPrivileged() {
5555
t.Skip("Needs to be run as non-root")
5656
return
5757
}

0 commit comments

Comments
 (0)