Skip to content

Commit

Permalink
test: fix unpriviliged process runner test
Browse files Browse the repository at this point in the history
Don't try cgroups/OOM scoe if not specified in the runner spec.

Signed-off-by: Andrey Smirnov <andrey.smirnov@siderolabs.com>
  • Loading branch information
smira committed Nov 8, 2024
1 parent 2001167 commit 333737f
Showing 1 changed file with 31 additions and 27 deletions.
58 changes: 31 additions & 27 deletions internal/app/machined/pkg/system/runner/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,41 +311,45 @@ func (p *processRunner) build() (commandWrapper, error) {
//
//nolint:gocyclo
func applyProperties(p *processRunner, pid int) error {
path := cgroup.Path(p.opts.CgroupPath)
if p.opts.CgroupPath != "" {
path := cgroup.Path(p.opts.CgroupPath)

if cgroups.Mode() == cgroups.Unified {
cgv2, err := cgroup2.Load(path)
if err != nil {
return fmt.Errorf("failed to load cgroup %s: %w", path, err)
}
if cgroups.Mode() == cgroups.Unified {
cgv2, err := cgroup2.Load(path)
if err != nil {
return fmt.Errorf("failed to load cgroup %s: %w", path, err)
}

// No such process error can happen in case the process is terminated before this code runs
if err := cgv2.AddProc(uint64(pid)); err != nil {
pathError, ok := err.(*fs.PathError)
if !ok || pathError.Err != syscall.ESRCH {
return fmt.Errorf("failed to move process %s to cgroup: %w", p, err)
// No such process error can happen in case the process is terminated before this code runs
if err := cgv2.AddProc(uint64(pid)); err != nil {
pathError, ok := err.(*fs.PathError)
if !ok || pathError.Err != syscall.ESRCH {
return fmt.Errorf("failed to move process %s to cgroup: %w", p, err)
}
}
} else {
cgv1, err := cgroup1.Load(cgroup1.StaticPath(path))
if err != nil {
return fmt.Errorf("failed to load cgroup %s: %w", path, err)
}
}
} else {
cgv1, err := cgroup1.Load(cgroup1.StaticPath(path))
if err != nil {
return fmt.Errorf("failed to load cgroup %s: %w", path, err)
}

if err := cgv1.Add(cgroup1.Process{
Pid: pid,
}); err != nil {
pathError, ok := err.(*fs.PathError)
if !ok || pathError.Err != syscall.ESRCH {
return fmt.Errorf("failed to move process %s to cgroup: %w", p, err)
if err := cgv1.Add(cgroup1.Process{
Pid: pid,
}); err != nil {
pathError, ok := err.(*fs.PathError)
if !ok || pathError.Err != syscall.ESRCH {
return fmt.Errorf("failed to move process %s to cgroup: %w", p, err)
}
}
}
}

if err := sys.AdjustOOMScore(pid, p.opts.OOMScoreAdj); err != nil {
pathError, ok := err.(*fs.PathError)
if !ok || pathError.Err != syscall.ENOENT {
return fmt.Errorf("failed to change OOMScoreAdj of process %s to %d: %w", p, p.opts.OOMScoreAdj, err)
if p.opts.OOMScoreAdj != 0 {
if err := sys.AdjustOOMScore(pid, p.opts.OOMScoreAdj); err != nil {
pathError, ok := err.(*fs.PathError)
if !ok || pathError.Err != syscall.ENOENT {
return fmt.Errorf("failed to change OOMScoreAdj of process %s to %d: %w", p, p.opts.OOMScoreAdj, err)
}
}
}

Expand Down

0 comments on commit 333737f

Please sign in to comment.