This repository has been archived by the owner on Feb 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
system_unix.go
235 lines (209 loc) · 5.73 KB
/
system_unix.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package sandbox
import (
"context"
"sync/atomic"
"time"
"github.com/stealthrocket/wasi-go"
"golang.org/x/sys/unix"
)
// system contains the platform-specific state and implementation of the sandbox
// System type.
type system struct {
pollfds []unix.PollFd
kill [2]atomic.Int32
}
type timeout struct {
duration time.Duration
subindex int
}
func (s *System) PollOneOff(ctx context.Context, subscriptions []wasi.Subscription, events []wasi.Event) (int, wasi.Errno) {
if len(subscriptions) == 0 || len(events) < len(subscriptions) {
return 0, wasi.EINVAL
}
s.pollfds = append(s.pollfds[:0], unix.PollFd{
Fd: s.kill[0].Load(),
Events: unix.POLLIN | unix.POLLHUP,
})
var unixEpoch, now time.Time
if s.time != nil {
unixEpoch, now = time.Unix(0, 0), s.time()
}
timeout := timeout{duration: -1, subindex: -1}
setTimeout := func(i int, d time.Duration) {
if d < 0 {
d = 0
}
if timeout.duration < 0 || d < timeout.duration {
timeout.subindex = i
timeout.duration = d
}
}
events = events[:len(subscriptions)]
for i := range events {
events[i] = wasi.Event{}
}
numEvents := 0
for i, sub := range subscriptions {
var pollEvent int16 = unix.POLLPRI | unix.POLLIN | unix.POLLHUP
switch sub.EventType {
case wasi.FDWriteEvent:
pollEvent = unix.POLLOUT
fallthrough
case wasi.FDReadEvent:
fd := sub.GetFDReadWrite().FD
f, _, errno := s.files.LookupFD(fd, wasi.PollFDReadWriteRight)
if errno != wasi.ESUCCESS {
events[i] = makePollError(sub, errno)
numEvents++
continue
}
s.pollfds = append(s.pollfds, unix.PollFd{
Fd: int32(f.Fd()),
Events: pollEvent,
})
case wasi.ClockEvent:
clock := sub.GetClock()
var epoch time.Time
switch clock.ID {
case wasi.Realtime:
epoch = unixEpoch
case wasi.Monotonic:
epoch = s.epoch
}
if epoch.IsZero() {
events[i] = makePollError(sub, wasi.ENOTSUP)
numEvents++
continue
}
duration := time.Duration(clock.Timeout)
if clock.Precision > 0 {
duration += time.Duration(clock.Precision)
duration -= 1
}
if (clock.Flags & wasi.Abstime) != 0 {
deadline := epoch.Add(duration)
setTimeout(i, deadline.Sub(now))
} else {
setTimeout(i, duration)
}
}
}
// We set the timeout to zero when we already produced events due to
// invalid subscriptions; this is useful to still make progress on I/O
// completion.
var deadline time.Time
if numEvents > 0 {
timeout.duration = 0
}
if timeout.duration > 0 {
deadline = time.Now().Add(timeout.duration)
}
// This loops until either the deadline is reached or at least one event is
// reported.
for {
var timeoutMillis int
switch {
case timeout.duration == 0:
timeoutMillis = 0
case timeout.duration < 0:
timeoutMillis = -1
case !deadline.IsZero():
timeoutMillis = int(time.Until(deadline).Round(time.Millisecond).Milliseconds())
}
_, err := unix.Poll(s.pollfds, timeoutMillis)
if err != nil && err != unix.EINTR {
return 0, wasi.MakeErrno(err)
}
// poll(2) may cause spurious kill up, so we verify that the system
// has indeed been killed instead of relying on reading the events
// reported on the first pollfd.
if s.kill[1].Load() < 0 {
// If the kill fd was notified it means the system was killed,
// terminate.
_ = s.ProcRaise(ctx, wasi.SIGKILL)
}
if timeout.subindex >= 0 && deadline.Before(time.Now()) {
events[timeout.subindex] = makePollEvent(subscriptions[timeout.subindex])
}
j := 1
for i, sub := range subscriptions {
if events[i].EventType != 0 {
continue
}
switch sub.EventType {
case wasi.FDReadEvent, wasi.FDWriteEvent:
pf := &s.pollfds[j]
j++
if pf.Revents == 0 {
continue
}
// Linux never reports POLLHUP for disconnected sockets,
// so there is no reliable mechanism to set wasi.Hanghup.
// We optimize for portability here and just report that
// the file descriptor is ready for reading or writing,
// and let the application deal with the conditions it
// sees from the following calles to read/write/etc...
events[i] = makePollEvent(sub)
}
}
// A 1:1 correspondance between the subscription and events arrays is
// used to track the completion of events, including the completion of
// invalid subscriptions, clock events, and I/O notifications coming
// from poll(2).
//
// We use zero as the marker on events for subscriptions that have not
// been fulfilled, but because the zero event type is used to represent
// clock subscriptions, we mark completed events with the event type+1.
//
// The event type is finally restored to its correct value in the loop
// below when we pack all completed events at the front of the output
// buffer.
n := 0
for _, e := range events {
if e.EventType != 0 {
e.EventType--
events[n] = e
n++
}
}
if n > 0 {
return n, wasi.ESUCCESS
}
}
}
func makePollEvent(sub wasi.Subscription) wasi.Event {
return wasi.Event{
UserData: sub.UserData,
EventType: sub.EventType + 1,
}
}
func makePollError(sub wasi.Subscription, errno wasi.Errno) wasi.Event {
return wasi.Event{
UserData: sub.UserData,
EventType: sub.EventType + 1,
Errno: errno,
}
}
// Kill may be called asynchronously to cancel all blocking operations on
// the system, causing calls such as PollOneOff to unblock and return an
// error indicating that the system is shutting down.
func (s *System) Kill() {
if fd := s.kill[1].Swap(-1); fd >= 0 {
closeTraceError(int(fd))
}
}
func (s *System) init() error {
var fds [2]int
if err := pipe(&fds); err != nil {
return err
}
s.kill[0].Store(int32(fds[0]))
s.kill[1].Store(int32(fds[1]))
return nil
}
func (s *System) close() {
closePair(&[2]int{
int(s.kill[0].Swap(-1)),
int(s.kill[1].Swap(-1)),
})
}