/
killall_windows.go
68 lines (57 loc) 路 1.49 KB
/
killall_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package nwin
import (
"log"
"os"
"syscall"
ps "github.com/mitchellh/go-ps"
)
func KillAll(pathsToKill []string) error {
killMap := make(map[string]struct{})
for _, ptk := range pathsToKill {
killMap[ptk] = struct{}{}
}
pidsToKill := []int{}
processes, err := ps.Processes()
if err != nil {
return err
}
handleProc := func(process ps.Process) {
handle, err := syscall.OpenProcess(syscall.PROCESS_QUERY_INFORMATION, false, uint32(process.Pid()))
if err != nil {
if errno, ok := err.(syscall.Errno); ok && errno == syscall.ERROR_ACCESS_DENIED {
// ignore
return
}
log.Printf("Couldn't open process (pid %d): %s", process.Pid(), err.Error())
} else {
defer syscall.Close(handle)
fullName, err := GetModuleFileName(handle)
if err != nil {
log.Printf("Couldn't get module file name (pid %d): %s", process.Pid(), err.Error())
} else {
if _, ok := killMap[fullName]; ok {
log.Printf("Should kill %d", process.Pid())
pidsToKill = append(pidsToKill, process.Pid())
}
}
}
}
for _, process := range processes {
handleProc(process)
}
log.Printf("%d processes to kill", len(pidsToKill))
for _, pidToKill := range pidsToKill {
func() {
p, err := os.FindProcess(pidToKill)
if err != nil {
// oh well
log.Printf("PID %d vanished", pidToKill)
return
}
log.Printf("Killing %d...", pidToKill)
// not even going to bother with the error code - if it works, great! if it doesn't, oh well
p.Kill()
}()
}
return nil
}