Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

capability: add LastCap stub for non-Linux #152

Merged
merged 2 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 10 additions & 4 deletions capability/capability_noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ package capability

import "errors"

func newPid(pid int) (Capabilities, error) {
return nil, errors.New("not supported")
var errNotSup = errors.New("not supported")

func newPid(_ int) (Capabilities, error) {
return nil, errNotSup
}

func newFile(_ string) (Capabilities, error) {
return nil, errNotSup
}

func newFile(path string) (Capabilities, error) {
return nil, errors.New("not supported")
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
Loading