Skip to content

Commit aaea55a

Browse files
authored
Merge pull request #153 from kolyshkin/list-enh
capability: add ListKnown, ListSupported; deprecate List
2 parents 29394de + 166b98b commit aaea55a

File tree

5 files changed

+72
-19
lines changed

5 files changed

+72
-19
lines changed

capability/capability.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ func NewFile2(path string) (Capabilities, error) {
137137

138138
// LastCap returns highest valid capability of the running kernel,
139139
// or an error if it can not be obtained.
140+
//
141+
// See also: [ListSupported].
140142
func LastCap() (Cap, error) {
141143
return lastCap()
142144
}

capability/capability_test.go

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ import (
99
"testing"
1010
)
1111

12+
// Based on the fact Go 1.18+ supports Linux >= 2.6.32, and
13+
// - CAP_MAC_ADMIN (33) was added in 2.6.25;
14+
// - CAP_SYSLOG (34) was added in 2.6.38;
15+
// - CAP_CHECKPOINT_RESTORE (40) was added in 5.9, and it is
16+
// the last added capability as of today (July 2024);
17+
//
18+
// LastCap return value should be between minLastCap and maxLastCap.
19+
const (
20+
minLastCap = CAP_MAC_ADMIN
21+
maxLastCap = CAP_CHECKPOINT_RESTORE
22+
)
23+
1224
func TestLastCap(t *testing.T) {
1325
last, err := LastCap()
1426
switch runtime.GOOS {
@@ -24,21 +36,35 @@ func TestLastCap(t *testing.T) {
2436
}
2537

2638
// Sanity checks (Linux only).
27-
//
28-
// Based on the fact Go 1.18+ supports Linux >= 2.6.32, and
29-
// - CAP_MAC_ADMIN (33) was added in 2.6.25;
30-
// - CAP_SYSLOG (34) was added in 2.6.38;
31-
// - CAP_CHECKPOINT_RESTORE (40) was added in 5.9, and it is
32-
// the last added capability as of today (July 2024);
33-
// LastCap return value should be between minCap and maxCap.
34-
minCap := CAP_MAC_ADMIN
35-
maxCap := CAP_CHECKPOINT_RESTORE
36-
if last < minCap {
39+
if last < minLastCap {
3740
t.Fatalf("LastCap returned %d (%s), expected >= %d (%s)",
38-
last, last, minCap, minCap)
41+
last, last, minLastCap, minLastCap)
3942
}
40-
if last > maxCap {
43+
if last > maxLastCap {
4144
t.Fatalf("LastCap returned %d, expected <= %d (%s). Package needs to be updated.",
42-
last, maxCap, maxCap)
45+
last, maxLastCap, maxLastCap)
46+
}
47+
}
48+
49+
func TestListSupported(t *testing.T) {
50+
list, err := ListSupported()
51+
switch runtime.GOOS {
52+
case "linux":
53+
if err != nil {
54+
t.Fatal(err)
55+
}
56+
default:
57+
if err == nil {
58+
t.Fatal(runtime.GOOS, ": want error, got nil")
59+
}
60+
}
61+
if runtime.GOOS != "linux" {
62+
return
63+
}
64+
// Sanity check (Linux only).
65+
t.Logf("got +%v (len %d)", list, len(list))
66+
minLen := int(minLastCap) + 1
67+
if len(list) < minLen {
68+
t.Fatalf("result is too short (got %d, want %d): +%v", len(list), minLen, list)
4369
}
4470
}

capability/enum.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
package capability
99

10+
import "slices"
11+
1012
type CapType uint
1113

1214
func (c CapType) String() string {
@@ -301,3 +303,28 @@ const (
301303
// Introduced in kernel 5.9
302304
CAP_CHECKPOINT_RESTORE = Cap(40)
303305
)
306+
307+
// List returns the list of all capabilities known to the package.
308+
//
309+
// Deprecated: use [ListKnown] or [ListSupported] instead.
310+
func List() []Cap {
311+
return ListKnown()
312+
}
313+
314+
// ListKnown returns the list of all capabilities known to the package.
315+
func ListKnown() []Cap {
316+
return list()
317+
}
318+
319+
// ListSupported retuns the list of all capabilities known to the package,
320+
// except those that are not supported by the currently running Linux kernel.
321+
func ListSupported() ([]Cap, error) {
322+
last, err := LastCap()
323+
if err != nil {
324+
return nil, err
325+
}
326+
return slices.DeleteFunc(list(), func(c Cap) bool {
327+
// Remove caps not supported by the kernel.
328+
return c > last
329+
}), nil
330+
}

capability/enum_gen.go

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

capability/enumgen/gen.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type generator struct {
2323
}
2424

2525
func (g *generator) writeHeader() {
26-
g.buf.WriteString("// generated file; DO NOT EDIT - use go generate in directory with source\n")
26+
g.buf.WriteString("// Code generated by go generate; DO NOT EDIT.\n")
2727
g.buf.WriteString("\n")
2828
g.buf.WriteString("package capability")
2929
}
@@ -43,8 +43,7 @@ func (g *generator) writeStringFunc() {
4343

4444
func (g *generator) writeListFunc() {
4545
g.buf.WriteString("\n")
46-
g.buf.WriteString("// List returns list of all supported capabilities\n")
47-
g.buf.WriteString("func List() []Cap {\n")
46+
g.buf.WriteString("func list() []Cap {\n")
4847
g.buf.WriteString("return []Cap{\n")
4948
for _, cap := range g.caps {
5049
fmt.Fprintf(&g.buf, "%s,\n", cap)

0 commit comments

Comments
 (0)