Skip to content

Commit 0bc45bf

Browse files
🐛 proc Make sure that invalid PIDs are ignored when stopping processes (#668)
<!-- Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved. SPDX-License-Identifier: Apache-2.0 --> ### Description <!-- Please add any detail or context that would be useful to a reviewer. --> Make sure that invalid PIDs are ignored when stopping processes ### Test Coverage <!-- Please put an `x` in the correct box e.g. `[x]` to indicate the testing coverage of this change. --> - [x] This change is covered by existing or additional automated tests. - [ ] Manual testing has been performed (and evidence provided) as automated testing was not feasible. - [ ] Additional tests are not required for this change (e.g. documentation update).
1 parent c803b74 commit 0bc45bf

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

changes/20250807104110.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:bug: `proc` Make sure that invalid PIDs are ignored when stopping processes

utils/proc/find/find_linux.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ import (
1717
)
1818

1919
const (
20-
procFS = "/proc"
21-
procDataFile = "cmdline"
20+
procFS = "/proc"
21+
procDataFile = "cmdline"
22+
invalidPIDErr = "could not parse PID from proc path"
2223
)
2324

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

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

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

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

9192
p, err = parseProcess(ctx, entry)
9293
if err != nil {
94+
err = commonerrors.IgnoreCorrespondTo(err, invalidPIDErr)
9395
return
9496
}
9597

utils/proc/find/find_linux_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/ARM-software/golang-utils/utils/commonerrors"
1515
"github.com/ARM-software/golang-utils/utils/commonerrors/errortest"
16+
"github.com/ARM-software/golang-utils/utils/filesystem"
1617
"github.com/ARM-software/golang-utils/utils/logs"
1718
"github.com/ARM-software/golang-utils/utils/logs/logstest"
1819
"github.com/ARM-software/golang-utils/utils/subprocess"
@@ -87,4 +88,12 @@ func TestFind(t *testing.T) {
8788
assert.Empty(t, processes)
8889
})
8990

91+
t.Run("Exclude invalid", func(t *testing.T) {
92+
ctx := context.Background()
93+
allProcesses, err := filesystem.Glob(fmt.Sprintf("%v/[0-9]*/%v", procFS, procDataFile))
94+
require.NoError(t, err)
95+
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)
96+
assert.NoError(t, err)
97+
assert.LessOrEqual(t, len(processes), len(allProcesses))
98+
})
9099
}

0 commit comments

Comments
 (0)