-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathsignal_windows.go
89 lines (84 loc) · 1.89 KB
/
signal_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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//go:build windows
// +build windows
package global
import (
"errors"
"fmt"
"net"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"github.com/Microsoft/go-winio"
log "github.com/sirupsen/logrus"
)
var validTasks = map[string]func(){
"dumpstack": dumpStack,
}
// SetupMainSignalHandler is for main to use at last
func SetupMainSignalHandler() <-chan struct{} {
mainOnce.Do(func() {
// for stack trace collecting on windows
pipeName := fmt.Sprintf(`\\.\pipe\go-cqhttp-%d`, os.Getpid())
pipe, err := winio.ListenPipe(pipeName, &winio.PipeConfig{})
if err != nil {
log.Errorf("创建 named pipe 失败. 将无法使用 dumpstack 功能: %v", err)
} else {
maxTaskLen := 0
for t := range validTasks {
if l := len(t); l > maxTaskLen {
maxTaskLen = l
}
}
go func() {
for {
c, err := pipe.Accept()
if err != nil {
if errors.Is(err, net.ErrClosed) || strings.Contains(err.Error(), "closed") {
return
}
log.Errorf("accept named pipe 失败: %v", err)
continue
}
go func() {
defer c.Close()
_ = c.SetReadDeadline(time.Now().Add(5 * time.Second))
buf := make([]byte, maxTaskLen)
n, err := c.Read(buf)
if err != nil {
log.Errorf("读取 named pipe 失败: %v", err)
return
}
cmd := string(buf[:n])
if task, ok := validTasks[cmd]; ok {
task()
return
}
log.Warnf("named pipe 读取到未知指令: %q", cmd)
}()
}
}()
}
// setup the main stop channel
mainStopCh = make(chan struct{})
mc := make(chan os.Signal, 2)
closeOnce := sync.Once{}
signal.Notify(mc, os.Interrupt, syscall.SIGTERM)
go func() {
for {
switch <-mc {
case os.Interrupt, syscall.SIGTERM:
closeOnce.Do(func() {
close(mainStopCh)
if pipe != nil {
_ = pipe.Close()
}
})
}
}
}()
})
return mainStopCh
}