forked from syndtr/gocapability
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove init and CAP_LAST_CAP, add LastCap
One (minor) issue with this package is it has func init which reads a file in /proc, making the start of any program which imports the package a bit slower. Let's switch to lazy initialization, i.e. only read the file when needed. Unfortunately, this can not be done in a non-disruptive manner, since we have a public variable CAP_LAST_CAP. So, this is a disruptive change, and anyone who's using CAP_LAST_CAP should change their code to something like this: last, err := capability.LastCap() if err != nil { return err } Also, add a test case for LastCap. Reported-by: ningmingxiao <ning.mingxiao@zte.com.cn> Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
- Loading branch information
Showing
4 changed files
with
73 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package capability | ||
|
||
import "testing" | ||
|
||
func TestLastCap(t *testing.T) { | ||
last, err := LastCap() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Sanity checks. | ||
// | ||
// Based on the fact Go 1.18+ supports Linux >= 2.6.32, and | ||
// - CAP_MAC_ADMIN (33) was added in 2.6.25; | ||
// - CAP_SYSLOG (34) was added in 2.6.38; | ||
// - CAP_CHECKPOINT_RESTORE (40) was added in 5.9, and it is | ||
// the last added capability as of today (July 2024); | ||
// LastCap return value should be between minCap and maxCap. | ||
minCap := CAP_MAC_ADMIN | ||
maxCap := CAP_CHECKPOINT_RESTORE | ||
if last < minCap { | ||
t.Fatalf("LastCap returned %d (%s), expected >= %d (%s)", | ||
last, last, minCap, minCap) | ||
} | ||
if last > maxCap { | ||
t.Fatalf("LastCap returned %d, expected <= %d (%s). Package needs to be updated.", | ||
last, maxCap, maxCap) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters