Skip to content

Commit

Permalink
Progress has arrived, go 1.21 has a generic ErrUnsupported
Browse files Browse the repository at this point in the history
  • Loading branch information
haesbaert committed Jan 29, 2024
1 parent a008721 commit 1271f79
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 19 deletions.
11 changes: 3 additions & 8 deletions libbeat/common/capabilities/capabilities_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ import (
var (
// errInvalidCapability expresses an invalid capability ID: x < 0 || x >= 64.
errInvalidCapability = errors.New("invalid capability")

// ErrUnsupported is returned for all public functions on "not
// linux". There is a generic errors.ErrUnsupported present in
// golang 1.21, but we still support 1.20.
ErrUnsupported = errors.New("capabilities are only supported in linux")
)

// The capability set flag/vector, re-exported from
Expand All @@ -53,7 +48,7 @@ const (
// Fetch the capabilities of pid for a given flag/vector and convert
// it to the representation used in ECS. cap.GetPID() fetches it with
// SYS_CAPGET.
// Returns ErrUnsupported on "not linux".
// Returns errors.ErrUnsupported on "not linux".
func FromPid(flag Flag, pid int) ([]string, error) {
set, err := cap.GetPID(pid)
if err != nil {
Expand Down Expand Up @@ -89,7 +84,7 @@ func FromPid(flag Flag, pid int) ([]string, error) {
}

// Convert a uint64 to the capabilities representation used in ECS.
// Returns ErrUnsupported on "not linux".
// Returns errors.ErrUnsupported on "not linux".
func FromUint64(w uint64) ([]string, error) {
sl := make([]string, 0, bits.OnesCount64(w))
for i := 0; w != 0; i++ {
Expand All @@ -109,7 +104,7 @@ func FromUint64(w uint64) ([]string, error) {

// Convert a string to the capabilities representation used in
// ECS. Example input: "1ffffffffff", 16.
// Returns ErrUnsupported on "not linux".
// Returns errors.ErrUnsupported on "not linux".
func FromString(s string, base int) ([]string, error) {
w, err := strconv.ParseUint(s, 16, 64)
if err != nil {
Expand Down
15 changes: 6 additions & 9 deletions libbeat/common/capabilities/capabilities_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ package capabilities

import "errors"

// ErrUnsupported is returned for all public functions on "not linux".
var ErrUnsupported = errors.New("capabilities are only supported in linux")

// Dummy value on "not linux".
type Flag = uint

Expand All @@ -34,17 +31,17 @@ const (
Permitted = Flag(1)
)

// ErrUnsupported on "not linux".
// Returns errors.ErrUnsupported on "not linux".
func FromPid(flag Flag, pid int) ([]string, error) {
return nil, ErrUnsupported
return nil, errors.ErrUnsupported
}

// ErrUnsupported on "not linux".
// Returns errors.ErrUnsupported on "not linux".
func FromUint64(w uint64) ([]string, error) {
return nil, ErrUnsupported
return nil, errors.ErrUnsupported
}

// ErrUnsupported on "not linux".
// Returns errors.ErrUnsupported on "not linux".
func FromString(s string, base int) ([]string, error) {
return nil, ErrUnsupported
return nil, errors.ErrUnsupported
}
4 changes: 2 additions & 2 deletions x-pack/auditbeat/module/system/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,11 +500,11 @@ func (ms *MetricSet) getProcesses() ([]*Process, error) {
}

process.CapEffective, err = capabilities.FromPid(capabilities.Effective, pInfo.PID)
if err != nil && !errors.Is(err, capabilities.ErrUnsupported) && process.Error == nil {
if err != nil && !errors.Is(err, errors.ErrUnsupported) && process.Error == nil {
process.Error = err
}
process.CapPermitted, err = capabilities.FromPid(capabilities.Permitted, pInfo.PID)
if err != nil && !errors.Is(err, capabilities.ErrUnsupported) && process.Error == nil {
if err != nil && !errors.Is(err, errors.ErrUnsupported) && process.Error == nil {
process.Error = err
}
// Exclude Linux kernel processes, they are not very interesting.
Expand Down

0 comments on commit 1271f79

Please sign in to comment.