...
1
2
3
4
5
6
7
8 package main
9
10
29 import "C"
30
31 import (
32 "bytes"
33 "errors"
34 "fmt"
35 "io"
36 "log"
37 "net"
38 "os"
39 "os/exec"
40 "sync"
41 "syscall"
42 "time"
43 )
44
45 func init() {
46 register("EINTR", EINTR)
47 register("Block", Block)
48 }
49
50
51
52 func EINTR() {
53 if errno := C.clearRestart(C.int(syscall.SIGURG)); errno != 0 {
54 log.Fatal(syscall.Errno(errno))
55 }
56 if errno := C.clearRestart(C.int(syscall.SIGWINCH)); errno != 0 {
57 log.Fatal(syscall.Errno(errno))
58 }
59 if errno := C.clearRestart(C.int(syscall.SIGCHLD)); errno != 0 {
60 log.Fatal(syscall.Errno(errno))
61 }
62
63 var wg sync.WaitGroup
64 testPipe(&wg)
65 testNet(&wg)
66 testExec(&wg)
67 wg.Wait()
68 fmt.Println("OK")
69 }
70
71
72
73
74
75 func spin() (float64, []byte) {
76 stop := time.Now().Add(time.Millisecond)
77 r1 := 0.0
78 r2 := make([]byte, 200)
79 for time.Now().Before(stop) {
80 for i := 1; i < 1e6; i++ {
81 r1 += r1 / float64(i)
82 r2 = append(r2, bytes.Repeat([]byte{byte(i)}, 100)...)
83 r2 = r2[100:]
84 }
85 }
86 return r1, r2
87 }
88
89
90 func winch() {
91 ticker := time.NewTicker(100 * time.Microsecond)
92 defer ticker.Stop()
93 pid := syscall.Getpid()
94 for n := 10; n > 0; n-- {
95 syscall.Kill(pid, syscall.SIGWINCH)
96 <-ticker.C
97 }
98 }
99
100
101 func sendSomeSignals() {
102 done := make(chan struct{})
103 go func() {
104 spin()
105 close(done)
106 }()
107 winch()
108 <-done
109 }
110
111
112 func testPipe(wg *sync.WaitGroup) {
113 r, w, err := os.Pipe()
114 if err != nil {
115 log.Fatal(err)
116 }
117 if err := syscall.SetNonblock(int(r.Fd()), false); err != nil {
118 log.Fatal(err)
119 }
120 if err := syscall.SetNonblock(int(w.Fd()), false); err != nil {
121 log.Fatal(err)
122 }
123 wg.Add(2)
124 go func() {
125 defer wg.Done()
126 defer w.Close()
127
128
129
130 sendSomeSignals()
131
132
133
134
135 if _, err := w.Write(bytes.Repeat([]byte{0}, 2<<20)); err != nil {
136 log.Fatal(err)
137 }
138 }()
139 go func() {
140 defer wg.Done()
141 defer r.Close()
142 b := make([]byte, 1<<20)
143
144
145 if _, err := io.ReadFull(r, b); err != nil {
146 log.Fatal(err)
147 }
148
149
150
151 sendSomeSignals()
152 if _, err := io.ReadFull(r, b); err != nil {
153 log.Fatal(err)
154 }
155 }()
156 }
157
158
159 func testNet(wg *sync.WaitGroup) {
160 ln, err := net.Listen("tcp4", "127.0.0.1:0")
161 if err != nil {
162 if errors.Is(err, syscall.EAFNOSUPPORT) || errors.Is(err, syscall.EPROTONOSUPPORT) {
163 return
164 }
165 log.Fatal(err)
166 }
167 wg.Add(2)
168 go func() {
169 defer wg.Done()
170 defer ln.Close()
171 c, err := ln.Accept()
172 if err != nil {
173 log.Fatal(err)
174 }
175 defer c.Close()
176 cf, err := c.(*net.TCPConn).File()
177 if err != nil {
178 log.Fatal(err)
179 }
180 defer cf.Close()
181 if err := syscall.SetNonblock(int(cf.Fd()), false); err != nil {
182 log.Fatal(err)
183 }
184
185 sendSomeSignals()
186 if _, err := cf.Write(bytes.Repeat([]byte{0}, 2<<20)); err != nil {
187 log.Fatal(err)
188 }
189 }()
190 go func() {
191 defer wg.Done()
192 sendSomeSignals()
193 c, err := net.Dial("tcp", ln.Addr().String())
194 if err != nil {
195 log.Fatal(err)
196 }
197 defer c.Close()
198 cf, err := c.(*net.TCPConn).File()
199 if err != nil {
200 log.Fatal(err)
201 }
202 defer cf.Close()
203 if err := syscall.SetNonblock(int(cf.Fd()), false); err != nil {
204 log.Fatal(err)
205 }
206
207 b := make([]byte, 1<<20)
208 if _, err := io.ReadFull(cf, b); err != nil {
209 log.Fatal(err)
210 }
211 sendSomeSignals()
212 if _, err := io.ReadFull(cf, b); err != nil {
213 log.Fatal(err)
214 }
215 }()
216 }
217
218 func testExec(wg *sync.WaitGroup) {
219 wg.Add(1)
220 go func() {
221 defer wg.Done()
222 cmd := exec.Command(os.Args[0], "Block")
223 stdin, err := cmd.StdinPipe()
224 if err != nil {
225 log.Fatal(err)
226 }
227 cmd.Stderr = new(bytes.Buffer)
228 cmd.Stdout = cmd.Stderr
229 if err := cmd.Start(); err != nil {
230 log.Fatal(err)
231 }
232
233 go func() {
234 sendSomeSignals()
235 stdin.Close()
236 }()
237
238 if err := cmd.Wait(); err != nil {
239 log.Fatalf("%v:\n%s", err, cmd.Stdout)
240 }
241 }()
242 }
243
244
245 func Block() {
246 io.Copy(io.Discard, os.Stdin)
247 }
248
View as plain text