-
Notifications
You must be signed in to change notification settings - Fork 0
/
winsignal.go
91 lines (78 loc) · 1.85 KB
/
winsignal.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
90
91
package winsignal
import (
"os"
"log"
"strconv"
"io/ioutil"
"github.com/fsnotify/fsnotify"
)
type WinSignal int
const (
SIGABRT = WinSignal(1)
SIGCHLD = WinSignal(3)
SIGCLD = WinSignal(4)
SIGINT = WinSignal(5)
SIGKILL = WinSignal(6)
SIGQUIT = WinSignal(7)
SIGSTOP = WinSignal(8)
SIGTERM = WinSignal(9)
SIGWINCH = WinSignal(10)
)
var shmfile = ".shm"
func Wait(sigs ...WinSignal) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
panic(err)
}
defer watcher.Close()
if f, err := os.Create(shmfile); err != nil {
panic(err)
} else {
f.Close()
}
if err = watcher.Add(shmfile); err != nil {
panic(err)
}
skip := false
for {
if skip {
break
}
select {
case event := <-watcher.Events:
if event.Op&fsnotify.Write == fsnotify.Write {
if sig := readSignalFrom(shmfile); inSignals(sig, sigs) {
skip = true
}
} else if event.Op&fsnotify.Remove == fsnotify.Remove ||
event.Op&fsnotify.Rename == fsnotify.Rename {
skip = true
}
case err := <-watcher.Errors:
skip = true
log.Println("winsignal error:", err)
}
}
watcher.Remove(shmfile)
os.Remove(shmfile)
}
func Send(s WinSignal) {
i := strconv.FormatInt(int64(s), 10)
ioutil.WriteFile(shmfile, []byte(i), 0666)
}
func readSignalFrom(f string) (s WinSignal) {
buf, err := ioutil.ReadFile(f)
if err != nil {
return
}
i, _ := strconv.ParseInt(string(buf), 10, 32)
return WinSignal(int(i))
}
func inSignals(s WinSignal, sigs []WinSignal) bool {
for i, _ := range sigs {
if sigs[i] == s {
return true
}
}
return false
}