| 1 | //go:build !windows |
| 2 | |
| 3 | package main |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "errors" |
| 8 | "os" |
| 9 | "syscall" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | func waitForRemoteWindowOwnerExit(ctx context.Context, pid int) bool { |
| 14 | ticker := time.NewTicker(250 * time.Millisecond) |
| 15 | defer ticker.Stop() |
| 16 | for { |
| 17 | if !remoteWindowOwnerAlive(pid) { |
| 18 | return true |
| 19 | } |
| 20 | select { |
| 21 | case <-ctx.Done(): |
| 22 | return false |
| 23 | case <-ticker.C: |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | func remoteWindowOwnerAlive(pid int) bool { |
| 29 | if pid <= 0 { |
| 30 | return false |
| 31 | } |
| 32 | proc, err := os.FindProcess(pid) |
| 33 | if err != nil { |
| 34 | return false |
| 35 | } |
| 36 | err = proc.Signal(syscall.Signal(0)) |
| 37 | return err == nil || errors.Is(err, syscall.EPERM) |
| 38 | } |
| 39 |