Skip to content

Commit

Permalink
[system/process][darwin] return error as non-fatal if invalid env k-v…
Browse files Browse the repository at this point in the history
… pair is encountered (#186)

## What does this PR do?

- Implements one of the solutions for
elastic/beats#41461 (comment)

## Why is it important?

- See
elastic/beats#41461 (comment)

## Checklist

<!-- Mandatory
Add a checklist of things that are required to be reviewed in order to
have the PR approved

List here all the items you have verified BEFORE sending this PR. Please
DO NOT remove any item, striking through those that do not apply. (Just
in case, strikethrough uses two tildes. ~~Scratch this.~~)
-->

- [x] My code follows the style guidelines of this project
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] I have added an entry in `CHANGELOG.md`

## Related issues
- Closes elastic/beats#41461

## Steps to reproduce
- See
elastic/beats#41461 (comment)

---------

Co-authored-by: Tiago Queiroz <[email protected]>
  • Loading branch information
VihasMakwana and belimawr authored Oct 31, 2024
1 parent ab8f3d6 commit e71e01d
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions metric/system/process/process_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ func getProcArgs(pid int, filter func(string) bool) ([]string, string, mapstr.M,
delim := []byte{61} // "=" for key value pairs

envVars := mapstr.M{}
var envErr error
for {
line, err := bbuf.ReadBytes(0)
if err == io.EOF || line[0] == 0 {
Expand All @@ -230,7 +231,9 @@ func getProcArgs(pid int, filter func(string) bool) ([]string, string, mapstr.M,
pair := bytes.SplitN(stripNullByteRaw(line), delim, 2)

if len(pair) != 2 {
return argv, exeName, nil, fmt.Errorf("error reading process information from KERN_PROCARGS2: %w", err)
// invalid k-v pair encountered, return non-fatal error so that we can continue
err := fmt.Errorf("error reading process information from KERN_PROCARGS2: encountered invalid env pair for pid %d", pid)
envErr = errors.Join(envErr, NonFatalErr{Err: err})
}
eKey := string(pair[0])
if filter == nil || filter(eKey) {
Expand All @@ -239,7 +242,7 @@ func getProcArgs(pid int, filter func(string) bool) ([]string, string, mapstr.M,

}

return argv, exeName, envVars, nil
return argv, exeName, envVars, envErr
}

func sysctl(mib []C.int, old *byte, oldlen *uintptr,
Expand Down

0 comments on commit e71e01d

Please sign in to comment.