From 73ceacd236480105b53325613209a01c6ebd78b7 Mon Sep 17 00:00:00 2001 From: Simon Zou Date: Thu, 6 Mar 2025 16:41:51 +0000 Subject: [PATCH] ListProcesses through PID when cgroup is not found in Linux (#25198) * ListProcesses through PID when cgroup is not found * add changelog entry * update the ListByPid for windows --- .changelog/25198.txt | 3 +++ drivers/shared/executor/executor_basic.go | 2 +- drivers/shared/executor/executor_universal_linux.go | 8 +++++++- drivers/shared/executor/procstats/list_default.go | 4 ++-- drivers/shared/executor/procstats/list_windows.go | 4 ++-- 5 files changed, 15 insertions(+), 6 deletions(-) create mode 100644 .changelog/25198.txt diff --git a/.changelog/25198.txt b/.changelog/25198.txt new file mode 100644 index 000000000..f57f2df2e --- /dev/null +++ b/.changelog/25198.txt @@ -0,0 +1,3 @@ +```release-note:improvement +metrics: Fix the process lookup for raw_exec when running rootless +``` diff --git a/drivers/shared/executor/executor_basic.go b/drivers/shared/executor/executor_basic.go index e96cdcff7..b22183cf4 100644 --- a/drivers/shared/executor/executor_basic.go +++ b/drivers/shared/executor/executor_basic.go @@ -38,7 +38,7 @@ func withNetworkIsolation(f func() error, _ *drivers.NetworkIsolationSpec) error func setCmdUser(*exec.Cmd, string) error { return nil } func (e *UniversalExecutor) ListProcesses() set.Collection[int] { - return procstats.List(e.childCmd.Process.Pid) + return procstats.ListByPid(e.childCmd.Process.Pid) } func (e *UniversalExecutor) setSubCmdCgroup(*exec.Cmd, string) (func(), error) { diff --git a/drivers/shared/executor/executor_universal_linux.go b/drivers/shared/executor/executor_universal_linux.go index b399b557e..53d042a6e 100644 --- a/drivers/shared/executor/executor_universal_linux.go +++ b/drivers/shared/executor/executor_universal_linux.go @@ -103,7 +103,13 @@ func (e *UniversalExecutor) setSubCmdCgroup(cmd *exec.Cmd, cgroup string) (func( } func (e *UniversalExecutor) ListProcesses() set.Collection[procstats.ProcessID] { - return procstats.List(e.command) + switch cgroupslib.GetMode() { + case cgroupslib.OFF: + // cgroup is unavailable, could possibly due to rootless nomad client + return procstats.ListByPid(e.childCmd.Process.Pid) + default: + return procstats.List(e.command) + } } func (e *UniversalExecutor) statCG(cgroup string) (int, func(), error) { diff --git a/drivers/shared/executor/procstats/list_default.go b/drivers/shared/executor/procstats/list_default.go index b6df7d386..0efd62bf0 100644 --- a/drivers/shared/executor/procstats/list_default.go +++ b/drivers/shared/executor/procstats/list_default.go @@ -1,7 +1,7 @@ // Copyright (c) HashiCorp, Inc. // SPDX-License-Identifier: MPL-2.0 -//go:build !linux && !windows +//go:build !windows package procstats @@ -15,7 +15,7 @@ import ( ) // List the process tree starting at the given executorPID -func List(executorPID int) set.Collection[ProcessID] { +func ListByPid(executorPID int) set.Collection[ProcessID] { result := set.New[ProcessID](10) ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) diff --git a/drivers/shared/executor/procstats/list_windows.go b/drivers/shared/executor/procstats/list_windows.go index f32c8f257..721aabbc7 100644 --- a/drivers/shared/executor/procstats/list_windows.go +++ b/drivers/shared/executor/procstats/list_windows.go @@ -10,7 +10,7 @@ import ( "github.com/mitchellh/go-ps" ) -// List will scan the process table and return a set of the process family +// ListByPid will scan the process table and return a set of the process family // tree starting with executorPID as the root. // // The implementation here specifically avoids using more than one system @@ -25,7 +25,7 @@ import ( // See https://github.com/hashicorp/nomad/issues/20042 as an example of what // happens when you use syscalls to work your way from the root down to its // descendants. -func List(executorPID int) set.Collection[ProcessID] { +func ListByPid(executorPID int) set.Collection[ProcessID] { procs := list(executorPID, ps.Processes) return procs }