Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 85d0635

Browse files
committed
Improve env marker and code conventions
1 parent 8f2fa52 commit 85d0635

File tree

1 file changed

+32
-30
lines changed

1 file changed

+32
-30
lines changed

agent/reaper/reaper.go

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
// to indicated that the process is a child as opposed to the reaper.
1414
// Since we are forkexec'ing we need to be able to differentiate between
1515
// the two to avoid fork bombing ourselves.
16-
const agentEnvMark = "CODER_REAPER_AGENT"
16+
const agentEnvMark = "CODER_DO_NOT_REAP"
1717

1818
// IsChild returns true if we're the forked process.
1919
func IsChild() bool {
@@ -34,41 +34,43 @@ func IsInitProcess() bool {
3434
func ForkReap(pids reap.PidCh) error {
3535
// Check if the process is the parent or the child.
3636
// If it's the child we want to skip attempting to reap.
37-
if !IsChild() {
38-
go reap.ReapChildren(pids, nil, nil, nil)
37+
if IsChild() {
38+
return nil
39+
}
40+
41+
go reap.ReapChildren(pids, nil, nil, nil)
3942

40-
args := os.Args
41-
// This is simply done to help identify the real agent process
42-
// when viewing in something like 'ps'.
43-
args = append(args, "#Agent")
43+
args := os.Args
44+
// This is simply done to help identify the real agent process
45+
// when viewing in something like 'ps'.
46+
args = append(args, "#Agent")
4447

45-
pwd, err := os.Getwd()
46-
if err != nil {
47-
return xerrors.Errorf("get wd: %w", err)
48-
}
48+
pwd, err := os.Getwd()
49+
if err != nil {
50+
return xerrors.Errorf("get wd: %w", err)
51+
}
4952

50-
pattrs := &syscall.ProcAttr{
51-
Dir: pwd,
52-
// Add our marker for identifying the child process.
53-
Env: append(os.Environ(), fmt.Sprintf("%s=true", agentEnvMark)),
54-
Sys: &syscall.SysProcAttr{
55-
Setsid: true,
56-
},
57-
Files: []uintptr{
58-
uintptr(syscall.Stdin),
59-
uintptr(syscall.Stdout),
60-
uintptr(syscall.Stderr),
61-
},
62-
}
53+
pattrs := &syscall.ProcAttr{
54+
Dir: pwd,
55+
// Add our marker for identifying the child process.
56+
Env: append(os.Environ(), fmt.Sprintf("%s=true", agentEnvMark)),
57+
Sys: &syscall.SysProcAttr{
58+
Setsid: true,
59+
},
60+
Files: []uintptr{
61+
uintptr(syscall.Stdin),
62+
uintptr(syscall.Stdout),
63+
uintptr(syscall.Stderr),
64+
},
65+
}
6366

64-
pid, _ := syscall.ForkExec(args[0], args, pattrs)
67+
//#nosec G204
68+
pid, _ := syscall.ForkExec(args[0], args, pattrs)
6569

66-
var wstatus syscall.WaitStatus
70+
var wstatus syscall.WaitStatus
71+
_, err = syscall.Wait4(pid, &wstatus, 0, nil)
72+
for xerrors.Is(err, syscall.EINTR) {
6773
_, err = syscall.Wait4(pid, &wstatus, 0, nil)
68-
for xerrors.Is(err, syscall.EINTR) {
69-
_, err = syscall.Wait4(pid, &wstatus, 0, nil)
70-
}
71-
return nil
7274
}
7375

7476
return nil

0 commit comments

Comments
 (0)