Files
wehub-resource-sync ead81af521
CI / go (push) Waiting to run
CI / build (darwin, macos-14) (push) Waiting to run
CI / build (windows, windows-2025) (push) Waiting to run
Deploy to GitHub Pages / deploy (push) Failing after 0s
chore: import upstream snapshot with attribution
2026-07-13 12:31:13 +08:00

45 lines
917 B
Go

//go:build windows
package ipc
import (
"context"
"encoding/csv"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"time"
)
func processAlive(pid int) (bool, error) {
if pid <= 0 {
return false, nil
}
if pid == os.Getpid() {
return true, nil
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
out, err := exec.CommandContext(ctx, "tasklist", "/FI", fmt.Sprintf("PID eq %d", pid), "/FO", "CSV", "/NH").Output()
if err != nil {
return false, fmt.Errorf("probe process liveness: %w", err)
}
r := csv.NewReader(strings.NewReader(string(out)))
r.FieldsPerRecord = -1
records, err := r.ReadAll()
if err != nil {
return false, fmt.Errorf("parse tasklist output: %w", err)
}
pidStr := strconv.Itoa(pid)
for _, record := range records {
if len(record) > 1 {
if strings.Trim(record[1], ` "`) == pidStr {
return true, nil
}
}
}
return false, nil
}