aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/internal/testsuite/proc.go
diff options
context:
space:
mode:
Diffstat (limited to 'test/internal/testsuite/proc.go')
-rw-r--r--test/internal/testsuite/proc.go23
1 files changed, 13 insertions, 10 deletions
diff --git a/test/internal/testsuite/proc.go b/test/internal/testsuite/proc.go
index f2ad8857..e7ef1aed 100644
--- a/test/internal/testsuite/proc.go
+++ b/test/internal/testsuite/proc.go
@@ -10,6 +10,8 @@ import (
"strings"
"syscall"
"unsafe"
+
+ "hakurei.app/fhs"
)
// Stat represents status information read from /proc/pid/stat.
@@ -144,13 +146,9 @@ type Stat struct {
CGuestTime int
}
-// fhsProc points to a virtual kernel file system exposing the process list and
-// other functionality.
-const fhsProc = "/proc/"
-
// Executable is like [os.Executable], but for the process referred to by s.
func (s *Stat) Executable() (string, error) {
- path, err := os.Readlink(filepath.Join(fhsProc, strconv.Itoa(s.PID), "exe"))
+ path, err := os.Readlink(filepath.Join(fhs.Proc, strconv.Itoa(s.PID), "exe"))
// When the executable has been deleted then Readlink returns a
// path appended with " (deleted)".
@@ -159,7 +157,7 @@ func (s *Stat) Executable() (string, error) {
// Stat populates stat with the proc filesystem entry referred to by s.
func (s *Stat) Stat(stat *syscall.Stat_t) (err error) {
- err = syscall.Stat(filepath.Join(fhsProc, strconv.Itoa(s.PID)), stat)
+ err = syscall.Stat(filepath.Join(fhs.Proc, strconv.Itoa(s.PID)), stat)
if err != nil {
err = os.NewSyscallError("stat", err)
}
@@ -168,7 +166,7 @@ func (s *Stat) Stat(stat *syscall.Stat_t) (err error) {
// Args reads arguments of the process referred to by s.
func (s *Stat) Args() ([]string, error) {
- p, err := os.ReadFile(filepath.Join(fhsProc, strconv.Itoa(s.PID), "cmdline"))
+ p, err := os.ReadFile(filepath.Join(fhs.Proc, strconv.Itoa(s.PID), "cmdline"))
if err != nil {
return nil, err
}
@@ -286,6 +284,11 @@ type StatScanner struct {
err error
}
+// IsNotExist returns whether an error is [os.ErrNotExist] or ESRCH.
+func IsNotExist(err error) bool {
+ return errors.Is(err, os.ErrNotExist) || errors.Is(err, syscall.ESRCH)
+}
+
// Scan reads a process status information entry. It returns false if an
// unrecoverable error is encountered, after which Scan no longer scans new
// entries.
@@ -295,7 +298,7 @@ func (s *StatScanner) Scan() bool {
}
if s.wrapped = s.i == len(s.dents); s.wrapped {
- if s.dents, s.err = os.ReadDir(fhsProc); s.err != nil {
+ if s.dents, s.err = os.ReadDir(fhs.Proc); s.err != nil {
return false
}
s.i = 0
@@ -318,9 +321,9 @@ func (s *StatScanner) Scan() bool {
}
var p []byte
- p, err = os.ReadFile(filepath.Join(fhsProc, dent.Name(), "stat"))
+ p, err = os.ReadFile(filepath.Join(fhs.Proc, dent.Name(), "stat"))
if err != nil {
- if errors.Is(err, os.ErrNotExist) || errors.Is(err, syscall.ESRCH) {
+ if IsNotExist(err) {
continue
}
s.err = err