mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-09-19 17:50:12 +00:00
test(agentcompat): add process supervision harness
Co-authored-by: naiba/CloudCode <hi+cloudcode@nai.ba>
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
//go:build linux
|
||||
|
||||
package process
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func ProcessHasOpenPath(pid int, path string) (bool, error) {
|
||||
if pid < 1 || !filepath.IsAbs(path) {
|
||||
return false, errors.New("invalid process path query")
|
||||
}
|
||||
directory := filepath.Join("/proc", strconv.Itoa(pid), "fd")
|
||||
entries, err := os.ReadDir(directory)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
wanted := filepath.Clean(path)
|
||||
for _, entry := range entries {
|
||||
target, err := os.Readlink(filepath.Join(directory, entry.Name()))
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
continue
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
target = strings.TrimSuffix(target, " (deleted)")
|
||||
if filepath.IsAbs(target) && filepath.Clean(target) == wanted {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
Reference in New Issue
Block a user