Skip to content

Commit 5076cf0

Browse files
committed
pr changes
1 parent 471ea8d commit 5076cf0

File tree

6 files changed

+55
-40
lines changed

6 files changed

+55
-40
lines changed

agent/agentexec/cli_unix.go renamed to agent/agentexec/cli_linux.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ func CLI() error {
2525
// We lock the OS thread here to avoid a race conditino where the nice priority
2626
// we get is on a different thread from the one we set it on.
2727
runtime.LockOSThread()
28+
// Nop on success but we do it anyway in case of an error.
29+
defer runtime.UnlockOSThread()
2830

2931
var (
3032
fs = flag.NewFlagSet("agent-exec", flag.ExitOnError)
@@ -42,18 +44,12 @@ func CLI() error {
4244
return xerrors.Errorf("parse flags: %w", err)
4345
}
4446

45-
if runtime.GOOS != "linux" {
46-
return xerrors.Errorf("agent-exec is only supported on Linux")
47-
}
48-
4947
// Get everything after "coder agent-exec --"
5048
args := execArgs(os.Args)
5149
if len(args) == 0 {
5250
return xerrors.Errorf("no exec command provided %+v", os.Args)
5351
}
5452

55-
pid := os.Getpid()
56-
5753
if *nice == unset {
5854
// If an explicit nice score isn't set, we use the default.
5955
*nice, err = defaultNiceScore()
@@ -75,7 +71,7 @@ func CLI() error {
7571
return xerrors.Errorf("set nice score: %w", err)
7672
}
7773

78-
err = writeOOMScoreAdj(pid, *oom)
74+
err = writeOOMScoreAdj(*oom)
7975
if err != nil {
8076
return xerrors.Errorf("set oom score: %w", err)
8177
}
@@ -104,7 +100,7 @@ func defaultNiceScore() (int, error) {
104100
}
105101

106102
func defaultOOMScore() (int, error) {
107-
score, err := oomScoreAdj(os.Getpid())
103+
score, err := oomScoreAdj()
108104
if err != nil {
109105
return 0, xerrors.Errorf("get oom score: %w", err)
110106
}
@@ -126,16 +122,16 @@ func defaultOOMScore() (int, error) {
126122
return 998, nil
127123
}
128124

129-
func oomScoreAdj(pid int) (int, error) {
130-
scoreStr, err := os.ReadFile(fmt.Sprintf("/proc/%d/oom_score_adj", pid))
125+
func oomScoreAdj() (int, error) {
126+
scoreStr, err := os.ReadFile("/proc/self/oom_score_adj")
131127
if err != nil {
132128
return 0, xerrors.Errorf("read oom_score_adj: %w", err)
133129
}
134130
return strconv.Atoi(strings.TrimSpace(string(scoreStr)))
135131
}
136132

137-
func writeOOMScoreAdj(pid int, score int) error {
138-
return os.WriteFile(fmt.Sprintf("/proc/%d/oom_score_adj", pid), []byte(fmt.Sprintf("%d", score)), 0o600)
133+
func writeOOMScoreAdj(score int) error {
134+
return os.WriteFile("/proc/self/oom_score_adj", []byte(fmt.Sprintf("%d", score)), 0o600)
139135
}
140136

141137
// execArgs returns the arguments to pass to syscall.Exec after the "--" delimiter.
File renamed without changes.

agent/agentexec/cli_other.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ package agentexec
66
import "golang.org/x/xerrors"
77

88
func CLI() error {
9-
return xerrors.Errorf("agent-exec is only supported on Linux")
9+
return xerrors.New("agent-exec is only supported on Linux")
1010
}

agent/agentexec/cmdtest/main.go renamed to agent/agentexec/cmdtest/main_linux.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//go:build linux
2+
// +build linux
3+
14
package main
25

36
import (

agent/agentexec/main_linux_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package agentexec_test
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"os/exec"
7+
"path/filepath"
8+
"testing"
9+
)
10+
11+
var TestBin string
12+
13+
func TestMain(m *testing.M) {
14+
code := func() int {
15+
// We generate a unique directory per test invocation to avoid collisions between two
16+
// processes attempting to create the same temp file.
17+
dir := genDir()
18+
defer os.RemoveAll(dir)
19+
TestBin = buildBinary(dir)
20+
return m.Run()
21+
}()
22+
23+
os.Exit(code)
24+
}
25+
26+
func buildBinary(dir string) string {
27+
path := filepath.Join(dir, "agent-test")
28+
out, err := exec.Command("go", "build", "-o", path, "./cmdtest").CombinedOutput()
29+
mustf(err, "build binary: %s", out)
30+
return path
31+
}
32+
33+
func mustf(err error, msg string, args ...any) {
34+
if err != nil {
35+
panic(fmt.Sprintf(msg, args...))
36+
}
37+
}
38+
39+
func genDir() string {
40+
dir, err := os.MkdirTemp(os.TempDir(), "agentexec")
41+
mustf(err, "create temp dir: %v", err)
42+
return dir
43+
}

agent/agentexec/main_test.go

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)