diff --git a/pkg/unshare/unshare.go b/pkg/unshare/unshare.go index a9210b0bf1..53cfeb0ec4 100644 --- a/pkg/unshare/unshare.go +++ b/pkg/unshare/unshare.go @@ -7,12 +7,17 @@ import ( "sync" "github.com/pkg/errors" + "github.com/syndtr/gocapability/capability" ) var ( homeDirOnce sync.Once homeDirErr error homeDir string + + hasCapSysAdminOnce sync.Once + hasCapSysAdminRet bool + hasCapSysAdminErr error ) // HomeDir returns the home directory for the current user. @@ -32,3 +37,20 @@ func HomeDir() (string, error) { }) return homeDir, homeDirErr } + +// HasCapSysAdmin returns whether the current process has CAP_SYS_ADMIN. +func HasCapSysAdmin() (bool, error) { + hasCapSysAdminOnce.Do(func() { + currentCaps, err := capability.NewPid2(0) + if err != nil { + hasCapSysAdminErr = err + return + } + if err = currentCaps.Load(); err != nil { + hasCapSysAdminErr = err + return + } + hasCapSysAdminRet = currentCaps.Get(capability.EFFECTIVE, capability.CAP_SYS_ADMIN) + }) + return hasCapSysAdminRet, hasCapSysAdminErr +}