summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/socket/rpcinet/notifier/notifier.go
blob: 82b75d6ddd63166accd4bf6a44778e68d3680702 (plain)
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
// Copyright 2018 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package notifier implements an FD notifier implementation over RPC.
package notifier

import (
	"fmt"
	"syscall"

	"golang.org/x/sys/unix"
	"gvisor.dev/gvisor/pkg/sentry/socket/rpcinet/conn"
	pb "gvisor.dev/gvisor/pkg/sentry/socket/rpcinet/syscall_rpc_go_proto"
	"gvisor.dev/gvisor/pkg/sync"
	"gvisor.dev/gvisor/pkg/waiter"
)

type fdInfo struct {
	queue   *waiter.Queue
	waiting bool
}

// Notifier holds all the state necessary to issue notifications when IO events
// occur in the observed FDs.
type Notifier struct {
	// rpcConn is the connection that is used for sending RPCs.
	rpcConn *conn.RPCConnection

	// epFD is the epoll file descriptor used to register for io
	// notifications.
	epFD uint32

	// mu protects fdMap.
	mu sync.Mutex

	// fdMap maps file descriptors to their notification queues and waiting
	// status.
	fdMap map[uint32]*fdInfo
}

// NewRPCNotifier creates a new notifier object.
func NewRPCNotifier(cn *conn.RPCConnection) (*Notifier, error) {
	id, c := cn.NewRequest(pb.SyscallRequest{Args: &pb.SyscallRequest_EpollCreate1{&pb.EpollCreate1Request{}}}, false /* ignoreResult */)
	<-c

	res := cn.Request(id).Result.(*pb.SyscallResponse_EpollCreate1).EpollCreate1.Result
	if e, ok := res.(*pb.EpollCreate1Response_ErrorNumber); ok {
		return nil, syscall.Errno(e.ErrorNumber)
	}

	w := &Notifier{
		rpcConn: cn,
		epFD:    res.(*pb.EpollCreate1Response_Fd).Fd,
		fdMap:   make(map[uint32]*fdInfo),
	}

	go w.waitAndNotify() // S/R-FIXME(b/77962828)

	return w, nil
}

// waitFD waits on mask for fd. The fdMap mutex must be hold.
func (n *Notifier) waitFD(fd uint32, fi *fdInfo, mask waiter.EventMask) error {
	if !fi.waiting && mask == 0 {
		return nil
	}

	e := pb.EpollEvent{
		Events: mask.ToLinux() | unix.EPOLLET,
		Fd:     fd,
	}

	switch {
	case !fi.waiting && mask != 0:
		id, c := n.rpcConn.NewRequest(pb.SyscallRequest{Args: &pb.SyscallRequest_EpollCtl{&pb.EpollCtlRequest{Epfd: n.epFD, Op: syscall.EPOLL_CTL_ADD, Fd: fd, Event: &e}}}, false /* ignoreResult */)
		<-c

		e := n.rpcConn.Request(id).Result.(*pb.SyscallResponse_EpollCtl).EpollCtl.ErrorNumber
		if e != 0 {
			return syscall.Errno(e)
		}

		fi.waiting = true
	case fi.waiting && mask == 0:
		id, c := n.rpcConn.NewRequest(pb.SyscallRequest{Args: &pb.SyscallRequest_EpollCtl{&pb.EpollCtlRequest{Epfd: n.epFD, Op: syscall.EPOLL_CTL_DEL, Fd: fd}}}, false /* ignoreResult */)
		<-c
		n.rpcConn.Request(id)

		fi.waiting = false
	case fi.waiting && mask != 0:
		id, c := n.rpcConn.NewRequest(pb.SyscallRequest{Args: &pb.SyscallRequest_EpollCtl{&pb.EpollCtlRequest{Epfd: n.epFD, Op: syscall.EPOLL_CTL_MOD, Fd: fd, Event: &e}}}, false /* ignoreResult */)
		<-c

		e := n.rpcConn.Request(id).Result.(*pb.SyscallResponse_EpollCtl).EpollCtl.ErrorNumber
		if e != 0 {
			return syscall.Errno(e)
		}
	}

	return nil
}

// addFD adds an FD to the list of FDs observed by n.
func (n *Notifier) addFD(fd uint32, queue *waiter.Queue) {
	n.mu.Lock()
	defer n.mu.Unlock()

	// Panic if we're already notifying on this FD.
	if _, ok := n.fdMap[fd]; ok {
		panic(fmt.Sprintf("File descriptor %d added twice", fd))
	}

	// We have nothing to wait for at the moment. Just add it to the map.
	n.fdMap[fd] = &fdInfo{queue: queue}
}

// updateFD updates the set of events the FD needs to be notified on.
func (n *Notifier) updateFD(fd uint32) error {
	n.mu.Lock()
	defer n.mu.Unlock()

	if fi, ok := n.fdMap[fd]; ok {
		return n.waitFD(fd, fi, fi.queue.Events())
	}

	return nil
}

// RemoveFD removes an FD from the list of FDs observed by n.
func (n *Notifier) removeFD(fd uint32) {
	n.mu.Lock()
	defer n.mu.Unlock()

	// Remove from map, then from epoll object.
	n.waitFD(fd, n.fdMap[fd], 0)
	delete(n.fdMap, fd)
}

// hasFD returns true if the FD is in the list of observed FDs.
func (n *Notifier) hasFD(fd uint32) bool {
	n.mu.Lock()
	defer n.mu.Unlock()

	_, ok := n.fdMap[fd]
	return ok
}

// waitAndNotify loops waiting for io event notifications from the epoll
// object. Once notifications arrive, they are dispatched to the
// registered queue.
func (n *Notifier) waitAndNotify() error {
	for {
		id, c := n.rpcConn.NewRequest(pb.SyscallRequest{Args: &pb.SyscallRequest_EpollWait{&pb.EpollWaitRequest{Fd: n.epFD, NumEvents: 100, Msec: -1}}}, false /* ignoreResult */)
		<-c

		res := n.rpcConn.Request(id).Result.(*pb.SyscallResponse_EpollWait).EpollWait.Result
		if e, ok := res.(*pb.EpollWaitResponse_ErrorNumber); ok {
			err := syscall.Errno(e.ErrorNumber)
			// NOTE(magi): I don't think epoll_wait can return EAGAIN but I'm being
			// conseratively careful here since exiting the notification thread
			// would be really bad.
			if err == syscall.EINTR || err == syscall.EAGAIN {
				continue
			}
			return err
		}

		n.mu.Lock()
		for _, e := range res.(*pb.EpollWaitResponse_Events).Events.Events {
			if fi, ok := n.fdMap[e.Fd]; ok {
				fi.queue.Notify(waiter.EventMaskFromLinux(e.Events))
			}
		}
		n.mu.Unlock()
	}
}

// AddFD adds an FD to the list of observed FDs.
func (n *Notifier) AddFD(fd uint32, queue *waiter.Queue) error {
	n.addFD(fd, queue)
	return nil
}

// UpdateFD updates the set of events the FD needs to be notified on.
func (n *Notifier) UpdateFD(fd uint32) error {
	return n.updateFD(fd)
}

// RemoveFD removes an FD from the list of observed FDs.
func (n *Notifier) RemoveFD(fd uint32) {
	n.removeFD(fd)
}

// HasFD returns true if the FD is in the list of observed FDs.
//
// This should only be used by tests to assert that FDs are correctly
// registered.
func (n *Notifier) HasFD(fd uint32) bool {
	return n.hasFD(fd)
}

// NonBlockingPoll polls the given fd in non-blocking fashion. It is used just
// to query the FD's current state; this method will block on the RPC response
// although the syscall is non-blocking.
func (n *Notifier) NonBlockingPoll(fd uint32, mask waiter.EventMask) waiter.EventMask {
	for {
		id, c := n.rpcConn.NewRequest(pb.SyscallRequest{Args: &pb.SyscallRequest_Poll{&pb.PollRequest{Fd: fd, Events: mask.ToLinux()}}}, false /* ignoreResult */)
		<-c

		res := n.rpcConn.Request(id).Result.(*pb.SyscallResponse_Poll).Poll.Result
		if e, ok := res.(*pb.PollResponse_ErrorNumber); ok {
			if syscall.Errno(e.ErrorNumber) == syscall.EINTR {
				continue
			}
			return mask
		}

		return waiter.EventMaskFromLinux(res.(*pb.PollResponse_Events).Events)
	}
}