diff --git a/pkg/command/process_identity_darwin.go b/pkg/command/process_identity_darwin.go index 17d0657dab..8c04b44d36 100644 --- a/pkg/command/process_identity_darwin.go +++ b/pkg/command/process_identity_darwin.go @@ -94,11 +94,26 @@ func darwinProcessGroupMemberMatches( pgid, wantSession int, identity string, ) (matches, leaderMismatch bool, err error) { - processGroupID := int(process.Eproc.Pgid) - if processGroupID != pgid { + if int(process.Eproc.Pgid) != pgid { return false, false, nil } pid := int(process.Proc.P_pid) + isLeader := pid == pgid + if isLeader && !darwinLeaderStartMatches(process, identity) { + return false, true, nil + } + // kill(-pgid, sig) returns EPERM for a zombie-only group on darwin. + if darwinProcessIsZombie(process) { + return false, false, nil + } + return darwinSessionMatches(pid, isLeader, wantSession) +} + +func darwinSessionMatches( + pid int, + isLeader bool, + wantSession int, +) (matches, leaderMismatch bool, err error) { sessionID, err := darwinSessionID(pid) if errors.Is(err, syscall.ESRCH) { return false, false, nil @@ -106,14 +121,8 @@ func darwinProcessGroupMemberMatches( if err != nil { return false, false, err } - if pid == pgid { - leaderIdentity, err := processTreeIdentity(pid) - if err != nil { - return false, false, err - } - if leaderIdentity != "" && leaderIdentity != identity { - return false, true, nil - } + if isLeader && sessionID != wantSession { + return false, true, nil } return sessionID == wantSession, false, nil } @@ -121,3 +130,43 @@ func darwinProcessGroupMemberMatches( func darwinSessionID(pid int) (int, error) { return unix.Getsid(pid) } + +// x/sys/unix does not export SZOMB. +const darwinProcessStateZombie = 5 + +func darwinProcessIsZombie(process unix.KinfoProc) bool { + return process.Proc.P_stat == darwinProcessStateZombie +} + +func darwinLeaderStartMatches(process unix.KinfoProc, identity string) bool { + parts := strings.Split(identity, ":") + if len(parts) != 3 { + return false + } + return parts[1] == strconv.FormatInt(int64(process.Proc.P_starttime.Sec), 10) && + parts[2] == strconv.FormatInt(int64(process.Proc.P_starttime.Usec), 10) +} + +func processIsLive(pid int) (bool, error) { + process, err := unix.SysctlKinfoProc("kern.proc.pid", pid) + if err != nil { + if processLookupConfirmsExit(pid, err) { + return false, nil + } + return false, err + } + return !darwinProcessIsZombie(*process), nil +} + +func processGroupHasLivingMembers(pgid int) (bool, error) { + processes, err := unix.SysctlKinfoProcSlice("kern.proc.all") + if err != nil { + return false, err + } + for _, process := range processes { + if int(process.Eproc.Pgid) == pgid && !darwinProcessIsZombie(process) { + return true, nil + } + } + return false, nil +} diff --git a/pkg/command/process_identity_darwin_test.go b/pkg/command/process_identity_darwin_test.go new file mode 100644 index 0000000000..27be95cd52 --- /dev/null +++ b/pkg/command/process_identity_darwin_test.go @@ -0,0 +1,155 @@ +//go:build darwin + +package command + +import ( + "os" + "os/exec" + "path/filepath" + "strconv" + "strings" + "syscall" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "golang.org/x/sys/unix" +) + +const ( + darwinTestPollTimeout = 5 * time.Second + darwinTestPollInterval = 10 * time.Millisecond +) + +func startGatedGroupLeader(t *testing.T, script string) (pid int, release func(), arg string) { + t.Helper() + arg = filepath.Join(t.TempDir(), "ready") + // #nosec G204 -- the fixed shell script and all paths are test-controlled. + cmd := exec.Command("sh", "-c", script, "sh", arg) + cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} + stdin, err := cmd.StdinPipe() + require.NoError(t, err) + require.NoError(t, cmd.Start()) + pid = cmd.Process.Pid + t.Cleanup(func() { + _ = syscall.Kill(-pid, syscall.SIGKILL) + _ = cmd.Wait() + }) + return pid, func() { require.NoError(t, stdin.Close()) }, arg +} + +func waitForZombie(t *testing.T, pid int) { + t.Helper() + require.Eventually(t, func() bool { + proc, err := unix.SysctlKinfoProc("kern.proc.pid", pid) + return err == nil && int(proc.Proc.P_pid) == pid && darwinProcessIsZombie(*proc) + }, darwinTestPollTimeout, darwinTestPollInterval, "process %d did not become a zombie", pid) +} + +func startZombieGroupLeader(t *testing.T) (pid int, identity string) { + t.Helper() + pid, release, _ := startGatedGroupLeader(t, `read _`) + identity = processTreeIdentityForTest(t, pid) + release() + waitForZombie(t, pid) + return pid, identity +} + +func TestDarwinZombieLeaderIsNotLiveGroupMember(t *testing.T) { + pid, identity := startZombieGroupLeader(t) + + live, err := processIsLive(pid) + require.NoError(t, err) + assert.False(t, live, "zombie leader reported live") + + living, err := processGroupHasLivingMembers(pid) + require.NoError(t, err) + assert.False(t, living, "zombie-only group reported living members") + + matches, err := processGroupMatchesIdentity(pid, identity) + require.NoError(t, err) + assert.False(t, matches, "zombie-only group matched saved identity") +} + +func TestDarwinKillWorkerTreeOnZombieOnlyGroupSucceeds(t *testing.T) { + pid, identity := startZombieGroupLeader(t) + + require.NoError(t, killWorkerTreeWithIdentity(pid, "detached-task", identity)) + waitForZombie(t, pid) +} + +func TestDarwinZombieLeaderWithLiveChildKeepsGroupMatched(t *testing.T) { + pid, release, readyFile := startGatedGroupLeader(t, `sleep 30 & echo $! > "$1"; read _`) + childPID := waitForChildPID(t, readyFile) + childGroup, err := unix.Getpgid(childPID) + require.NoError(t, err) + require.Equal(t, pid, childGroup, "child must share the leader's process group") + identity := processTreeIdentityForTest(t, pid) + + release() + waitForZombie(t, pid) + childLive, err := processIsLive(childPID) + require.NoError(t, err) + require.True(t, childLive, "child must outlive the zombie leader") + + matches, err := processGroupMatchesIdentity(pid, identity) + require.NoError(t, err) + assert.True(t, matches, "live child must keep the group matched") + living, err := processGroupHasLivingMembers(pid) + require.NoError(t, err) + assert.True(t, living, "live child must count as a living member") + + require.NoError(t, killWorkerTreeWithIdentity(pid, "detached-task", identity)) + require.Eventually(t, func() bool { + live, err := processIsLive(childPID) + return err == nil && !live + }, darwinTestPollTimeout, darwinTestPollInterval, "child %d survived termination", childPID) +} + +func TestDarwinZombieLeaderWithLiveChildRejectsStaleIdentity(t *testing.T) { + pid, release, readyFile := startGatedGroupLeader(t, `sleep 30 & echo $! > "$1"; read _`) + childPID := waitForChildPID(t, readyFile) + identity := processTreeIdentityForTest(t, pid) + stale := identity[:strings.LastIndex(identity, ":")] + ":0" + + release() + waitForZombie(t, pid) + + matches, err := processGroupMatchesIdentity(pid, stale) + require.NoError(t, err) + assert.False(t, matches, "stale identity matched a group") + require.NoError(t, killWorkerTreeWithIdentity(pid, "detached-task", stale)) + childLive, err := processIsLive(childPID) + require.NoError(t, err) + assert.True(t, childLive, "stale identity must not terminate the child") +} + +func waitForChildPID(t *testing.T, readyFile string) int { + t.Helper() + var childPID int + require.Eventually(t, func() bool { + // #nosec G304 -- this test reads only its own temporary readiness file. + data, err := os.ReadFile(readyFile) + if err != nil || !strings.HasSuffix(string(data), "\n") { + return false + } + childPID, err = strconv.Atoi(strings.TrimSpace(string(data))) + return err == nil + }, darwinTestPollTimeout, darwinTestPollInterval, "child did not report its PID") + return childPID +} + +func TestDarwinProcessGroupHasLivingMembersSeesLiveChild(t *testing.T) { + cmd := exec.Command("sleep", "30") + cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} + require.NoError(t, cmd.Start()) + t.Cleanup(func() { + _ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) + _ = cmd.Wait() + }) + + living, err := processGroupHasLivingMembers(cmd.Process.Pid) + require.NoError(t, err) + assert.True(t, living) +} diff --git a/pkg/command/process_identity_linux.go b/pkg/command/process_identity_linux.go index 2ec7e6b6ea..148f1cf11f 100644 --- a/pkg/command/process_identity_linux.go +++ b/pkg/command/process_identity_linux.go @@ -263,3 +263,64 @@ func linuxProcessStatFields(stat []byte) ([]string, bool) { } return strings.Fields(statText[closingParen+2:]), true } + +func processIsLive(pid int) (bool, error) { + fields, ok, err := linuxProcessStat(pid) + if err != nil || !ok { + return false, err + } + if len(fields) == 0 { + return false, fmt.Errorf("read process state for process %d", pid) + } + return !linuxProcessStateIsExited(fields[0]), nil +} + +type linuxStatReader func(pid int) ([]string, bool, error) + +func processGroupHasLivingMembers(pgid int) (bool, error) { + if pgid <= 0 { + return false, fmt.Errorf("invalid process group %d", pgid) + } + entries, err := os.ReadDir("/proc") + if err != nil { + return false, err + } + pids := make([]int, 0, len(entries)) + for _, entry := range entries { + if pid, err := strconv.Atoi(entry.Name()); err == nil { + pids = append(pids, pid) + } + } + return linuxLivingGroupMembers(pids, pgid, linuxProcessStat) +} + +func linuxLivingGroupMembers(pids []int, pgid int, readStat linuxStatReader) (bool, error) { + var skipped error + for _, pid := range pids { + member, err := linuxProcessIsLiveGroupMember(pid, pgid, readStat) + if err != nil { + if !linuxCanSkipProcessStatError(pid, pgid, err) { + return false, err + } + skipped = errors.Join(skipped, fmt.Errorf("inspect process %d: %w", pid, err)) + continue + } + if member { + return true, nil + } + } + return linuxProcessGroupMatchResult(pgid, false, skipped) +} + +func linuxProcessIsLiveGroupMember(pid, pgid int, readStat linuxStatReader) (bool, error) { + fields, ok, err := readStat(pid) + if err != nil || !ok || len(fields) == 0 { + return false, err + } + processGroup, _, ok := linuxProcessGroupIDs(fields) + return ok && processGroup == pgid && !linuxProcessStateIsExited(fields[0]), nil +} + +func linuxProcessStateIsExited(state string) bool { + return state == "Z" || state == "X" +} diff --git a/pkg/command/process_identity_linux_test.go b/pkg/command/process_identity_linux_test.go index 564f59ba46..271b6d7282 100644 --- a/pkg/command/process_identity_linux_test.go +++ b/pkg/command/process_identity_linux_test.go @@ -5,8 +5,12 @@ package command import ( "os" "os/exec" + "strconv" "syscall" "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) const processStatOpenOperation = "open" @@ -149,3 +153,99 @@ func TestProcessTreeIdentityForMissingProcess(t *testing.T) { t.Fatalf("processTreeIdentity for missing PID = %q, want empty identity", identity) } } + +func fakeLinuxStats(stats map[int][]string, errs map[int]error) linuxStatReader { + return func(pid int) ([]string, bool, error) { + if err, ok := errs[pid]; ok { + return nil, false, err + } + fields, ok := stats[pid] + return fields, ok, nil + } +} + +const absentLinuxGroup = int(^uint32(0) >> 1) + +type livingGroupCase struct { + name string + pgid int + pids []int + stats map[int][]string + errs map[int]error + want bool + wantErr bool +} + +func linuxLivingGroupCases(liveGroup int) []livingGroupCase { + const absent = absentLinuxGroup + denied := &os.PathError{Op: processStatOpenOperation, Err: os.ErrPermission} + member := func(pgid int, state string) []string { + return []string{state, "1", strconv.Itoa(pgid), strconv.Itoa(pgid)} + } + return []livingGroupCase{ + { + name: "readable live member wins over unreadable unrelated entry", + pgid: liveGroup, + pids: []int{10, 11}, + stats: map[int][]string{11: member(liveGroup, "S")}, + errs: map[int]error{10: denied}, + want: true, + }, + { + name: "zombie-only group has no living members", + pgid: absent, + pids: []int{absent}, + stats: map[int][]string{absent: member(absent, "Z")}, + }, + {name: "vanished process is ignored", pgid: absent, pids: []int{10}}, + { + name: "unreadable unrelated entry with absent group is complete", + pgid: absent, + pids: []int{10}, + errs: map[int]error{10: denied}, + }, + { + name: "unreadable unrelated entry with existing group fails closed", + pgid: liveGroup, + pids: []int{10}, + errs: map[int]error{10: denied}, + wantErr: true, + }, + { + name: "unreadable leader fails closed", + pgid: absent, + pids: []int{absent}, + errs: map[int]error{absent: denied}, + wantErr: true, + }, + { + name: "unexpected stat error fails closed", + pgid: absent, + pids: []int{10}, + errs: map[int]error{10: syscall.EIO}, + wantErr: true, + }, + } +} + +func TestLinuxLivingGroupMembers(t *testing.T) { + for _, tc := range linuxLivingGroupCases(startLinuxProcessGroupHelper(t)) { + t.Run(tc.name, func(t *testing.T) { + got, err := linuxLivingGroupMembers(tc.pids, tc.pgid, fakeLinuxStats(tc.stats, tc.errs)) + if tc.wantErr { + require.Error(t, err) + assert.False(t, got) + return + } + require.NoError(t, err) + assert.Equal(t, tc.want, got) + }) + } +} + +func TestProcessGroupHasLivingMembersRejectsNonpositiveGroup(t *testing.T) { + for _, pgid := range []int{0, -1} { + _, err := processGroupHasLivingMembers(pgid) + require.Error(t, err) + } +} diff --git a/pkg/command/process_identity_unix.go b/pkg/command/process_identity_unix.go index 5298036421..cfb4801df6 100644 --- a/pkg/command/process_identity_unix.go +++ b/pkg/command/process_identity_unix.go @@ -13,3 +13,11 @@ func processTreeIdentity(_ int) (string, error) { func processGroupMatchesIdentity(_ int, _ string) (bool, error) { return false, errors.New("process tree identity is unsupported on this platform") } + +func processIsLive(_ int) (bool, error) { + return false, errors.New("process liveness is unsupported on this platform") +} + +func processGroupHasLivingMembers(_ int) (bool, error) { + return false, errors.New("process group liveness is unsupported on this platform") +} diff --git a/pkg/command/process_supported.go b/pkg/command/process_supported.go index f5350ed875..8ddeced62b 100644 --- a/pkg/command/process_supported.go +++ b/pkg/command/process_supported.go @@ -114,16 +114,6 @@ func waitForUnidentifiedGroupExit(pid int, identity string) error { } } -// A worker whose leader exited may still leave members behind. -func processGroupExists(pid int) bool { - err := syscall.Kill(-pid, 0) - if isProcessGone(err) { - return false - } - // EPERM means the group exists but belongs to another user. - return true -} - func cleanupExitedProcessRef(ref ProcessRef) error { if ref.TreeKind == ProcessTreeLegacyPID { if ref.Identity == "" { @@ -149,7 +139,11 @@ func terminateUnidentifiedExitedGroup(pid int) error { if pid <= 0 { return fmt.Errorf("invalid worker PID %d", pid) } - if processGroupExists(pid) { + live, err := processGroupHasLivingMembers(pid) + if err != nil { + return fmt.Errorf("check worker group %d: %w", pid, err) + } + if live { return fmt.Errorf( "worker group %d still has processes and no saved identity identifies them", pid, @@ -240,8 +234,18 @@ func killWorkerTreeWithIdentity(parsedPid int, treeName, identity string) error if groupMatches { return signalProcessTree(-parsedPid, false, identity) } + live, err := processIsLive(parsedPid) + if err != nil { + return fmt.Errorf("check worker leader %d for worker %s: %w", parsedPid, treeName, err) + } + if !live { + return nil + } currentIdentity, err := processTreeIdentity(parsedPid) if err != nil { + if isProcessGone(err) { + return nil + } return fmt.Errorf("read process tree identity for worker %d: %w", parsedPid, err) } // The saved group is empty, so a different identity at this PID is a reused process. @@ -299,13 +303,21 @@ func isProcessGone(err error) bool { return err != nil && errors.Is(err, syscall.ESRCH) } +type signalOutcome int + +const ( + signalSent signalOutcome = iota + targetGone +) + func signalProcessTree(target int, graceful bool, identity string) error { - if err := syscall.Kill(target, syscall.SIGTERM); err != nil { - if isProcessGone(err) { - return nil // already exited - } + outcome, err := sendProcessSignal(target, syscall.SIGTERM, identity) + if err != nil { return fmt.Errorf("send SIGTERM to process target %d: %w", target, err) } + if outcome == targetGone { + return nil + } if graceful { time.Sleep(2 * time.Second) } @@ -316,13 +328,65 @@ func signalProcessTree(target int, graceful bool, identity string) error { if !matches { return nil } - err = syscall.Kill(target, syscall.SIGKILL) - if err != nil && !isProcessGone(err) { + if _, err := sendProcessSignal(target, syscall.SIGKILL, identity); err != nil { return fmt.Errorf("send SIGKILL to process target %d: %w", target, err) } return nil } +func sendProcessSignal( + target int, + sig syscall.Signal, + identity string, +) (signalOutcome, error) { + return classifySignalResult( + target, + syscall.Kill(target, sig), + identity, + processGroupMatchesIdentity, + ) +} + +func classifySignalResult( + target int, + killErr error, + identity string, + matcher func(int, string) (bool, error), +) (signalOutcome, error) { + switch { + case killErr == nil: + return signalSent, nil + case isProcessGone(killErr): + return targetGone, nil + case target < 0 && errors.Is(killErr, syscall.EPERM): + if err := confirmGroupEPERM(-target, identity, killErr, matcher); err != nil { + return signalSent, err + } + return targetGone, nil + default: + return signalSent, killErr + } +} + +func confirmGroupEPERM( + pgid int, + identity string, + killErr error, + matcher func(int, string) (bool, error), +) error { + if identity == "" { + return killErr + } + matches, err := matcher(pgid, identity) + if err != nil { + return fmt.Errorf("verify process group %d after EPERM: %w: %w", pgid, killErr, err) + } + if matches { + return fmt.Errorf("process group %d still has matching processes: %w", pgid, killErr) + } + return nil +} + func signalTargetMatchesIdentity(target int, identity string) (bool, error) { if identity == "" { return false, errors.New("process identity is required before SIGKILL") diff --git a/pkg/command/process_test.go b/pkg/command/process_test.go index 83a24f76d7..ab8948a3c9 100644 --- a/pkg/command/process_test.go +++ b/pkg/command/process_test.go @@ -3,6 +3,7 @@ package command import ( + "errors" "os" "os/exec" "path/filepath" @@ -13,6 +14,8 @@ import ( "time" ) +const testSavedIdentity = "1:2:3" + func TestKillTerminatesRunningProcess(t *testing.T) { cmd := exec.Command("sleep", "30") if err := cmd.Start(); err != nil { @@ -285,13 +288,7 @@ func TestTerminateUnidentifiedProcessGroup(t *testing.T) { if err != nil { t.Fatalf("terminateProcessRef: %v", err) } - deadline := time.Now().Add(5 * time.Second) - for processGroupExists(pid) && time.Now().Before(deadline) { - time.Sleep(50 * time.Millisecond) - } - if processGroupExists(pid) { - t.Fatalf("worker group %d still has processes after termination", pid) - } + waitForNoLivingGroupMembers(t, pid) } func TestCleanupExitedIdentitylessGroupReportsSurvivingDescendants(t *testing.T) { @@ -314,24 +311,205 @@ func TestCleanupExitedIdentitylessGroupReportsSurvivingDescendants(t *testing.T) } } -// startSigtermIgnoringGroup starts a group leader that outlives SIGTERM. The -// child is reaped as soon as it exits, the way a supervising process does, so -// an unreaped zombie does not keep the group looking populated. func startSigtermIgnoringGroup(t *testing.T) *exec.Cmd { t.Helper() - cmd := exec.Command("sh", "-c", `trap "" TERM; sleep 30`) + ready := filepath.Join(t.TempDir(), "ready") + // #nosec G204 -- the fixed shell script and all paths are test-controlled. + cmd := exec.Command("sh", "-c", `trap '' TERM; printf 'ready\n' > "$1"; sleep 30`, "sh", ready) cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} if err := cmd.Start(); err != nil { t.Fatalf("start SIGTERM-ignoring group: %v", err) } - reaped := make(chan struct{}) - go func() { - _ = cmd.Wait() - close(reaped) - }() t.Cleanup(func() { - _ = cmd.Process.Kill() - <-reaped + _ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) + _ = cmd.Wait() }) - return cmd + deadline := time.Now().Add(5 * time.Second) + for { + // #nosec G304 -- this test reads only its own temporary readiness file. + data, err := os.ReadFile(ready) + if err == nil && string(data) == "ready\n" { + return cmd + } + if time.Now().After(deadline) { + t.Fatalf("SIGTERM-ignoring group did not report readiness: %v", err) + } + time.Sleep(10 * time.Millisecond) + } +} + +func waitForNoLivingGroupMembers(t *testing.T, pgid int) { + t.Helper() + deadline := time.Now().Add(5 * time.Second) + for { + live, err := processGroupHasLivingMembers(pgid) + if err != nil { + t.Fatalf("check living members of group %d: %v", pgid, err) + } + if !live { + return + } + if time.Now().After(deadline) { + t.Fatalf("worker group %d still has living members after termination", pgid) + } + time.Sleep(50 * time.Millisecond) + } +} + +func TestKillWorkerTreeWithStaleIdentityLeavesLiveLeaderRunning(t *testing.T) { + pid := startUnixTestProcess(t, &syscall.SysProcAttr{Setpgid: true}).Process.Pid + identity := processTreeIdentityForTest(t, pid) + stale := identity[:strings.LastIndex(identity, ":")] + ":0" + if err := killWorkerTreeWithIdentity(pid, "detached-task", stale); err != nil { + t.Fatalf("killWorkerTreeWithIdentity with stale identity: %v", err) + } + if running, err := isRunning(strconv.Itoa(pid)); err != nil || !running { + t.Fatalf("live leader with stale identity: running=%t err=%v, want running", running, err) + } +} + +func TestConfirmGroupEPERMKeepsLiveOrUnverifiedGroupErrors(t *testing.T) { + matchErr := errors.New("enumerate processes") + tests := []struct { + name string + identity string + matcher func(int, string) (bool, error) + wantNil bool + wantErrs []error + }{ + { + name: "live matching member keeps EPERM", + identity: testSavedIdentity, + matcher: func(int, string) (bool, error) { return true, nil }, + wantErrs: []error{syscall.EPERM}, + }, + { + name: "failed verification keeps EPERM and verification error", + identity: testSavedIdentity, + matcher: func(int, string) (bool, error) { return false, matchErr }, + wantErrs: []error{syscall.EPERM, matchErr}, + }, + { + name: "no matching member confirms completion", + identity: testSavedIdentity, + matcher: func(int, string) (bool, error) { return false, nil }, + wantNil: true, + }, + { + name: "missing identity never confirms completion", + identity: "", + matcher: func(int, string) (bool, error) { return false, nil }, + wantErrs: []error{syscall.EPERM}, + }, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + err := confirmGroupEPERM(1234, tc.identity, syscall.EPERM, tc.matcher) + if tc.wantNil { + if err != nil { + t.Fatalf("confirmGroupEPERM = %v, want nil", err) + } + return + } + if err == nil { + t.Fatal("confirmGroupEPERM = nil, want error") + } + for _, want := range tc.wantErrs { + if !errors.Is(err, want) { + t.Fatalf("confirmGroupEPERM = %v, want errors.Is %v", err, want) + } + } + }) + } +} + +var ( + errSignalUnexpected = errors.New("unexpected") + errSignalVerify = errors.New("enumerate processes") +) + +type groupMatcher func(int, string) (bool, error) + +func matchesLiveMember(int, string) (bool, error) { return true, nil } +func matchesNoMember(int, string) (bool, error) { return false, nil } +func matchFails(int, string) (bool, error) { return false, errSignalVerify } + +type signalResultCase struct { + name string + target int + killErr error + identity string + matcher groupMatcher + want signalOutcome + wantErrs []error +} + +func groupCase(name string, killErr error, matcher groupMatcher) signalResultCase { + return signalResultCase{ + name: name, + target: -42, + killErr: killErr, + identity: testSavedIdentity, + matcher: matcher, + } +} + +func (c signalResultCase) onPositiveTarget() signalResultCase { + c.target = 42 + return c +} + +func (c signalResultCase) expect(want signalOutcome, wantErrs ...error) signalResultCase { + c.want = want + c.wantErrs = wantErrs + return c +} + +func signalResultCases() []signalResultCase { + return []signalResultCase{ + {name: "signal delivered", target: 42, want: signalSent}, + groupCase("positive target gone", syscall.ESRCH, nil). + onPositiveTarget().expect(targetGone), + groupCase("group gone", syscall.ESRCH, nil).expect(targetGone), + groupCase("group EPERM without live member is gone", syscall.EPERM, matchesNoMember). + expect(targetGone), + groupCase("group EPERM with live member keeps error", syscall.EPERM, matchesLiveMember). + expect(signalSent, syscall.EPERM), + groupCase("group EPERM with failed verification keeps both", syscall.EPERM, matchFails). + expect(signalSent, syscall.EPERM, errSignalVerify), + groupCase("positive target EPERM is never confirmed", syscall.EPERM, matchesNoMember). + onPositiveTarget().expect(signalSent, syscall.EPERM), + groupCase("unexpected error is returned", errSignalUnexpected, matchesNoMember). + expect(signalSent, errSignalUnexpected), + } +} + +func TestClassifySignalResult(t *testing.T) { + for _, tc := range signalResultCases() { + t.Run(tc.name, func(t *testing.T) { + got, err := classifySignalResult(tc.target, tc.killErr, tc.identity, tc.matcher) + if got != tc.want { + t.Fatalf("outcome = %v, want %v", got, tc.want) + } + if len(tc.wantErrs) == 0 && err != nil { + t.Fatalf("err = %v, want nil", err) + } + for _, want := range tc.wantErrs { + if !errors.Is(err, want) { + t.Fatalf("err = %v, want errors.Is %v", err, want) + } + } + }) + } +} + +func TestSignalProcessTreeReturnsImmediatelyWhenTargetAlreadyGone(t *testing.T) { + const absentPID = int(^uint32(0) >> 1) + start := time.Now() + if err := signalProcessTree(absentPID, true, testSavedIdentity); err != nil { + t.Fatalf("signalProcessTree for exited process: %v", err) + } + if elapsed := time.Since(start); elapsed >= time.Second { + t.Fatalf("signalProcessTree took %v for an already-exited target", elapsed) + } }