Skip to content

Commit a969a60

Browse files
authored
fix pidexists to use procfs (#43754)
### What does this PR do? Fixes `os.PidExists` to handle PID namespaces. ### Motivation There is no guarantee an agent container is in the same PID namespace as the host, so we need to use `procfs` to determine PID aliveness. ### Describe how you validated your changes Existing tests. ### Additional Notes Co-authored-by: bryce.kahle <bryce.kahle@datadoghq.com>
1 parent 9ac90bd commit a969a60

File tree

2 files changed

+7
-12
lines changed

2 files changed

+7
-12
lines changed
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,21 @@
33
// This product includes software developed at Datadog (https://www.datadoghq.com/).
44
// Copyright 2025-present Datadog, Inc.
55

6-
//go:build linux || freebsd || openbsd || darwin
7-
86
// Package os provides additional OS utilities
97
package os
108

119
import (
12-
"errors"
13-
"syscall"
10+
"os"
11+
"strconv"
12+
13+
"github.com/DataDog/datadog-agent/pkg/util/kernel"
1414
)
1515

1616
// PidExists returns true if the pid is still alive
1717
func PidExists(pid int) bool {
18-
// the kill syscall will check for the existence of a process
19-
// if the signal is 0. See
20-
// https://man7.org/linux/man-pages/man2/kill.2.html
21-
if err := syscall.Kill(pid, 0); errors.Is(err, syscall.ESRCH) {
18+
_, err := os.Stat(kernel.HostProc(strconv.Itoa(pid)))
19+
if os.IsNotExist(err) {
2220
return false
2321
}
24-
25-
return true
22+
return err == nil
2623
}
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
// This product includes software developed at Datadog (https://www.datadoghq.com/).
44
// Copyright 2025-present Datadog, Inc.
55

6-
//go:build linux || freebsd || openbsd || darwin
7-
86
package os
97

108
import (

0 commit comments

Comments
 (0)