Skip to content

Commit

Permalink
Merge pull request #877 from giuseppe/add-function-to-check-for-CAP_S…
Browse files Browse the repository at this point in the history
…YS_ADMIN

unshare: new function HasCapSysAdmin
  • Loading branch information
rhatdan authored Apr 20, 2021
2 parents 29560b3 + edf765f commit 8f31414
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pkg/unshare/unshare.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}

0 comments on commit 8f31414

Please sign in to comment.