Skip to content

Commit

Permalink
capability: add LastCap stub for non-Linux
Browse files Browse the repository at this point in the history
So that the code which uses capability.LastCap can be compiled on
non-Linux platforms.

Amend the LastCap test to also run on non-Linux.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
  • Loading branch information
kolyshkin committed Sep 20, 2024
1 parent 7fafaed commit f8fb9c5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
6 changes: 6 additions & 0 deletions capability/capability.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,9 @@ func NewFile(path string) (Capabilities, error) {
func NewFile2(path string) (Capabilities, error) {
return newFile(path)
}

// LastCap returns highest valid capability of the running kernel,
// or an error if it can not be obtained.
func LastCap() (Cap, error) {
return lastCap()
}
5 changes: 0 additions & 5 deletions capability/capability_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ const (
linuxCapVer3 = 0x20080522
)

// LastCap returns highest valid capability of the running kernel.
func LastCap() (Cap, error) {
return lastCap()
}

var lastCap = sync.OnceValues(func() (Cap, error) {
f, err := os.Open("/proc/sys/kernel/cap_last_cap")
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions capability/capability_noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ func newPid(_ int) (Capabilities, error) {
func newFile(_ string) (Capabilities, error) {
return nil, errNotSup
}

func lastCap() (Cap, error) {
return -1, errNotSup
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,26 @@

package capability

import "testing"
import (
"runtime"
"testing"
)

func TestLastCap(t *testing.T) {
last, err := LastCap()
if err != nil {
t.Fatal(err)
switch runtime.GOOS {
case "linux":
if err != nil {
t.Fatal(err)
}
default:
if err == nil {
t.Fatal(runtime.GOOS, ": want error, got nil")
}
return
}

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

0 comments on commit f8fb9c5

Please sign in to comment.