Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/20250807104110.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:bug: `proc` Make sure that invalid PIDs are ignored when stopping processes
10 changes: 6 additions & 4 deletions utils/proc/find/find_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import (
)

const (
procFS = "/proc"
procDataFile = "cmdline"
procFS = "/proc"
procDataFile = "cmdline"
invalidPIDErr = "could not parse PID from proc path"
)

func checkProcessMatch(ctx context.Context, fs filesystem.FS, re *regexp.Regexp, procEntry string) (ok bool, err error) {
Expand Down Expand Up @@ -51,7 +52,7 @@ func parseProcess(ctx context.Context, entry string) (p proc.IProcess, err error

pid, err := strconv.Atoi(strings.Trim(strings.TrimSuffix(strings.TrimPrefix(entry, procFS), fmt.Sprintf("%v", procDataFile)), "/"))
if err != nil {
err = commonerrors.WrapErrorf(commonerrors.ErrUnexpected, err, "could not parse PID from proc path '%v'", entry)
err = commonerrors.WrapErrorf(commonerrors.ErrUnexpected, err, "%v '%v'", invalidPIDErr, entry)
return
}

Expand All @@ -75,7 +76,7 @@ func FindProcessByRegexForFS(ctx context.Context, fs filesystem.FS, re *regexp.R
return
}

searchGlobTerm := fmt.Sprintf("%v/*/%v", procFS, procDataFile)
searchGlobTerm := fmt.Sprintf("%v/[0-9]*/%v", procFS, procDataFile)
procEntries, err := fs.Glob(searchGlobTerm)
if err != nil {
err = commonerrors.WrapErrorf(commonerrors.ErrUnexpected, err, "an error occurred when searching for processes using the following glob '%v'", searchGlobTerm)
Expand All @@ -90,6 +91,7 @@ func FindProcessByRegexForFS(ctx context.Context, fs filesystem.FS, re *regexp.R

p, err = parseProcess(ctx, entry)
if err != nil {
err = commonerrors.IgnoreCorrespondTo(err, invalidPIDErr)
return
}

Expand Down
9 changes: 9 additions & 0 deletions utils/proc/find/find_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/ARM-software/golang-utils/utils/commonerrors"
"github.com/ARM-software/golang-utils/utils/commonerrors/errortest"
"github.com/ARM-software/golang-utils/utils/filesystem"
"github.com/ARM-software/golang-utils/utils/logs"
"github.com/ARM-software/golang-utils/utils/logs/logstest"
"github.com/ARM-software/golang-utils/utils/subprocess"
Expand Down Expand Up @@ -87,4 +88,12 @@ func TestFind(t *testing.T) {
assert.Empty(t, processes)
})

t.Run("Exclude invalid", func(t *testing.T) {
ctx := context.Background()
allProcesses, err := filesystem.Glob(fmt.Sprintf("%v/[0-9]*/%v", procFS, procDataFile))
require.NoError(t, err)
processes, err := FindProcessByName(ctx, "") // empty name will return all processes that were valid according to the logic (e.g. no invalid names or empty cmdline files)
assert.NoError(t, err)
assert.LessOrEqual(t, len(processes), len(allProcesses))
})
}
Loading