-
Notifications
You must be signed in to change notification settings - Fork 0
fix(slopmachine): bound verification process cleanup #124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| //go:build darwin | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "time" | ||
|
|
||
| "golang.org/x/sys/unix" | ||
| ) | ||
|
|
||
| func waitForExit(ctx context.Context, pid int) error { | ||
| queue, err := unix.Kqueue() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer unix.Close(queue) | ||
| change := unix.Kevent_t{ | ||
| Ident: uint64(pid), | ||
| Filter: unix.EVFILT_PROC, | ||
| Flags: unix.EV_ADD | unix.EV_ENABLE | unix.EV_ONESHOT, | ||
| Fflags: unix.NOTE_EXIT, | ||
| } | ||
| if _, err := unix.Kevent(queue, []unix.Kevent_t{change}, nil, nil); err != nil { | ||
| if errors.Is(err, unix.ESRCH) { | ||
| return nil | ||
| } | ||
| return err | ||
| } | ||
| events := make([]unix.Kevent_t, 1) | ||
| for { | ||
| timeout := unix.NsecToTimespec((10 * time.Millisecond).Nanoseconds()) | ||
| count, err := unix.Kevent(queue, nil, events, &timeout) | ||
| if errors.Is(err, unix.EINTR) { | ||
| continue | ||
| } | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if count != 0 { | ||
| return nil | ||
| } | ||
| select { | ||
| case <-ctx.Done(): | ||
| return ctx.Err() | ||
| default: | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func ignoreCleanupErrorAfterExit(leaderPID int, err error) bool { | ||
| if !errors.Is(err, unix.EPERM) { | ||
| return false | ||
| } | ||
| for range 10 { | ||
| safe, pending := cleanupStateAfterExit(leaderPID) | ||
| if safe { | ||
| return true | ||
| } | ||
| if !pending { | ||
| return false | ||
| } | ||
| time.Sleep(time.Millisecond) | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func cleanupStateAfterExit(leaderPID int) (safe, pending bool) { | ||
| processes, queryErr := unix.SysctlKinfoProcSlice("kern.proc.pgrp", leaderPID) | ||
| if queryErr != nil { | ||
| return false, false | ||
| } | ||
| if containsOnlyZombieLeader(processes, leaderPID) { | ||
| return true, false | ||
| } | ||
| if len(processes) == 1 && int(processes[0].Proc.P_pid) == leaderPID { | ||
| return false, true | ||
| } | ||
| if len(processes) != 0 { | ||
| return false, false | ||
| } | ||
| leader, queryErr := unix.SysctlKinfoProc("kern.proc.pid", leaderPID) | ||
| if queryErr != nil { | ||
| return false, false | ||
| } | ||
| if isZombieLeader(leader, leaderPID) { | ||
| return true, false | ||
| } | ||
| return false, int(leader.Proc.P_pid) == leaderPID | ||
| } | ||
|
|
||
| func containsOnlyZombieLeader(processes []unix.KinfoProc, leaderPID int) bool { | ||
| return len(processes) == 1 && | ||
| isZombieLeader(&processes[0], leaderPID) | ||
| } | ||
|
|
||
| func isZombieLeader(process *unix.KinfoProc, leaderPID int) bool { | ||
| // Darwin's SZOMB value is 5 in sys/proc.h but is not exported by x/sys. | ||
| const zombieState = 5 | ||
| return process != nil && | ||
| int(process.Proc.P_pid) == leaderPID && | ||
| process.Proc.P_stat == zombieState | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "os/exec" | ||
| "syscall" | ||
| "testing" | ||
|
|
||
| "golang.org/x/sys/unix" | ||
| ) | ||
|
|
||
| func TestCleanupDoesNotIgnoreLiveLeader(t *testing.T) { | ||
| command := exec.Command("sleep", "30") | ||
| command.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} | ||
| if err := command.Start(); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer func() { | ||
| _ = command.Process.Kill() | ||
| _ = command.Wait() | ||
| }() | ||
| pid := command.Process.Pid | ||
| if safe, pending := cleanupStateAfterExit(pid); safe || !pending { | ||
| t.Fatalf("live leader cleanup state = (%t, %t)", safe, pending) | ||
| } | ||
| if ignoreCleanupErrorAfterExit(pid, unix.EPERM) { | ||
| t.Fatal("permission error ignored for a live process group") | ||
| } | ||
| } | ||
|
|
||
| func verificationProcessZombie(pid int) bool { | ||
| process, err := unix.SysctlKinfoProc("kern.proc.pid", pid) | ||
| return err == nil && isZombieLeader(process, pid) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| //go:build linux | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "time" | ||
|
|
||
| "golang.org/x/sys/unix" | ||
| ) | ||
|
|
||
| func waitForExit(ctx context.Context, pid int) error { | ||
| ticker := time.NewTicker(10 * time.Millisecond) | ||
| defer ticker.Stop() | ||
| for { | ||
| var info unix.Siginfo | ||
| err := unix.Waitid(unix.P_PID, pid, &info, unix.WEXITED|unix.WNOHANG|unix.WNOWAIT, nil) | ||
| if errors.Is(err, unix.EINTR) { | ||
| continue | ||
| } | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if info.Signo != 0 { | ||
| return nil | ||
| } | ||
| select { | ||
| case <-ctx.Done(): | ||
| return ctx.Err() | ||
| case <-ticker.C: | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func ignoreCleanupErrorAfterExit(_ int, _ error) bool { | ||
| return false | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "strings" | ||
| ) | ||
|
|
||
| func verificationProcessZombie(pid int) bool { | ||
| data, err := os.ReadFile(fmt.Sprintf("/proc/%d/stat", pid)) | ||
| if err != nil { | ||
| return false | ||
| } | ||
| // The parenthesized command name may itself contain spaces and ')'. | ||
| end := strings.LastIndexByte(string(data), ')') | ||
| if end < 0 { | ||
| return false | ||
| } | ||
| fields := strings.Fields(string(data)[end+1:]) | ||
| return len(fields) > 0 && fields[0] == "Z" | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.