Skip to content

Commit

Permalink
[chore][windows][system/process]: ignore error in some cases (#184)
Browse files Browse the repository at this point in the history
- Enhancement

We can ignore the error in two cases:
- While reading the process executable name. 
- For pid 4, this call fails as we can't access the executable name via
the system call. Same for other kernel-level processes.
- While finding the owner for a particular process.
- We try to open the process token via `syscall.OpenProcessToken`and we
can't access the token for protected processes , even as an
administrator.
    
it's okay to ignore these errors and move forward as we can access few
other metrics (memory, cpu).
    
More context
[here](elastic/beats#40484 (comment))

Relates elastic/beats#40484
  • Loading branch information
VihasMakwana authored Oct 25, 2024
1 parent 9c2e691 commit 4b75d03
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions metric/system/process/process_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,12 @@ func FetchNumThreads(pid int) (int, error) {

// FillPidMetrics is the windows implementation
func FillPidMetrics(_ resolve.Resolver, pid int, state ProcState, _ func(string) bool) (ProcState, error) {
user, err := getProcCredName(pid)
if err != nil {
return state, fmt.Errorf("error fetching username: %w", err)
}
state.Username = user
user, _ := getProcCredName(pid)
state.Username = user // we cannot access process token for system-owned protected processes

ppid, _ := getParentPid(pid)
state.Ppid = opt.IntWith(ppid)
if ppid, err := getParentPid(pid); err == nil {
state.Ppid = opt.IntWith(ppid)
}

wss, size, err := procMem(pid)
if err != nil {
Expand Down Expand Up @@ -270,8 +268,15 @@ func getProcName(pid int) (string, error) {
}()

filename, err := windows.GetProcessImageFileName(handle)

//nolint:nilerr // safe to ignore this error
if err != nil {
return "", fmt.Errorf("GetProcessImageFileName failed for pid=%v: %w", pid, err)
if isNonFatal(err) {
// if we're able to open the handle but GetProcessImageFileName fails with access denied error
// that the process doesn't have any executable associated with it.
return "", nil
}
return "", err
}

return filepath.Base(filename), nil
Expand Down

0 comments on commit 4b75d03

Please sign in to comment.