Skip to content

Commit 4a1fbfe

Browse files
authored
Merge pull request #152 from kolyshkin/last-cap
capability: add LastCap stub for non-Linux
2 parents 8c5f336 + f8fb9c5 commit 4a1fbfe

4 files changed

Lines changed: 31 additions & 13 deletions

File tree

capability/capability.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,9 @@ func NewFile(path string) (Capabilities, error) {
134134
func NewFile2(path string) (Capabilities, error) {
135135
return newFile(path)
136136
}
137+
138+
// LastCap returns highest valid capability of the running kernel,
139+
// or an error if it can not be obtained.
140+
func LastCap() (Cap, error) {
141+
return lastCap()
142+
}

capability/capability_linux.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ const (
2525
linuxCapVer3 = 0x20080522
2626
)
2727

28-
// LastCap returns highest valid capability of the running kernel.
29-
func LastCap() (Cap, error) {
30-
return lastCap()
31-
}
32-
3328
var lastCap = sync.OnceValues(func() (Cap, error) {
3429
f, err := os.Open("/proc/sys/kernel/cap_last_cap")
3530
if err != nil {

capability/capability_noop.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@ package capability
1111

1212
import "errors"
1313

14-
func newPid(pid int) (Capabilities, error) {
15-
return nil, errors.New("not supported")
14+
var errNotSup = errors.New("not supported")
15+
16+
func newPid(_ int) (Capabilities, error) {
17+
return nil, errNotSup
18+
}
19+
20+
func newFile(_ string) (Capabilities, error) {
21+
return nil, errNotSup
1622
}
1723

18-
func newFile(path string) (Capabilities, error) {
19-
return nil, errors.New("not supported")
24+
func lastCap() (Cap, error) {
25+
return -1, errNotSup
2026
}
Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,26 @@
44

55
package capability
66

7-
import "testing"
7+
import (
8+
"runtime"
9+
"testing"
10+
)
811

912
func TestLastCap(t *testing.T) {
1013
last, err := LastCap()
11-
if err != nil {
12-
t.Fatal(err)
14+
switch runtime.GOOS {
15+
case "linux":
16+
if err != nil {
17+
t.Fatal(err)
18+
}
19+
default:
20+
if err == nil {
21+
t.Fatal(runtime.GOOS, ": want error, got nil")
22+
}
23+
return
1324
}
1425

15-
// Sanity checks.
26+
// Sanity checks (Linux only).
1627
//
1728
// Based on the fact Go 1.18+ supports Linux >= 2.6.32, and
1829
// - CAP_MAC_ADMIN (33) was added in 2.6.25;

0 commit comments

Comments
 (0)