From e7ca2a51a89a8ff2c9f5adfdfa5b51be1b3faeb3 Mon Sep 17 00:00:00 2001
From: Bhasker Hariharan <bhaskerh@google.com>
Date: Wed, 24 Mar 2021 12:08:24 -0700
Subject: Add POLLRDNORM/POLLWRNORM support.

On Linux these are meant to be equivalent to POLLIN/POLLOUT. Rather
than hack these on in sys_poll etc it felt cleaner to just cleanup
the call sites to notify for both events. This is what linux does
as well.

Fixes #5544

PiperOrigin-RevId: 364859977
---
 pkg/sentry/fsimpl/devpts/line_discipline.go | 16 ++++++++--------
 pkg/sentry/fsimpl/devpts/queue.go           |  4 ++--
 pkg/sentry/fsimpl/eventfd/eventfd.go        |  8 ++++----
 pkg/sentry/fsimpl/eventfd/eventfd_test.go   |  2 +-
 pkg/sentry/fsimpl/fuse/connection.go        |  2 +-
 pkg/sentry/fsimpl/fuse/dev.go               |  4 ++--
 pkg/sentry/fsimpl/fuse/dev_test.go          |  2 +-
 pkg/sentry/fsimpl/fuse/fusefs.go            |  2 +-
 pkg/sentry/fsimpl/host/socket.go            |  4 ++--
 pkg/sentry/fsimpl/signalfd/signalfd.go      |  4 ++--
 pkg/sentry/fsimpl/timerfd/timerfd.go        |  4 ++--
 11 files changed, 26 insertions(+), 26 deletions(-)

(limited to 'pkg/sentry/fsimpl')

diff --git a/pkg/sentry/fsimpl/devpts/line_discipline.go b/pkg/sentry/fsimpl/devpts/line_discipline.go
index ae95fdd08..e94a5bac3 100644
--- a/pkg/sentry/fsimpl/devpts/line_discipline.go
+++ b/pkg/sentry/fsimpl/devpts/line_discipline.go
@@ -141,7 +141,7 @@ func (l *lineDiscipline) setTermios(task *kernel.Task, args arch.SyscallArgument
 		l.inQueue.pushWaitBufLocked(l)
 		l.inQueue.readable = true
 		l.inQueue.mu.Unlock()
-		l.replicaWaiter.Notify(waiter.EventIn)
+		l.replicaWaiter.Notify(waiter.ReadableEvents)
 	}
 
 	return 0, err
@@ -185,9 +185,9 @@ func (l *lineDiscipline) inputQueueRead(ctx context.Context, dst usermem.IOSeque
 		return 0, err
 	}
 	if n > 0 {
-		l.masterWaiter.Notify(waiter.EventOut)
+		l.masterWaiter.Notify(waiter.WritableEvents)
 		if pushed {
-			l.replicaWaiter.Notify(waiter.EventIn)
+			l.replicaWaiter.Notify(waiter.ReadableEvents)
 		}
 		return n, nil
 	}
@@ -202,7 +202,7 @@ func (l *lineDiscipline) inputQueueWrite(ctx context.Context, src usermem.IOSequ
 		return 0, err
 	}
 	if n > 0 {
-		l.replicaWaiter.Notify(waiter.EventIn)
+		l.replicaWaiter.Notify(waiter.ReadableEvents)
 		return n, nil
 	}
 	return 0, syserror.ErrWouldBlock
@@ -220,9 +220,9 @@ func (l *lineDiscipline) outputQueueRead(ctx context.Context, dst usermem.IOSequ
 		return 0, err
 	}
 	if n > 0 {
-		l.replicaWaiter.Notify(waiter.EventOut)
+		l.replicaWaiter.Notify(waiter.WritableEvents)
 		if pushed {
-			l.masterWaiter.Notify(waiter.EventIn)
+			l.masterWaiter.Notify(waiter.ReadableEvents)
 		}
 		return n, nil
 	}
@@ -237,7 +237,7 @@ func (l *lineDiscipline) outputQueueWrite(ctx context.Context, src usermem.IOSeq
 		return 0, err
 	}
 	if n > 0 {
-		l.masterWaiter.Notify(waiter.EventIn)
+		l.masterWaiter.Notify(waiter.ReadableEvents)
 		return n, nil
 	}
 	return 0, syserror.ErrWouldBlock
@@ -397,7 +397,7 @@ func (*inputQueueTransformer) transform(l *lineDiscipline, q *queue, buf []byte)
 		// Anything written to the readBuf will have to be echoed.
 		if l.termios.LEnabled(linux.ECHO) {
 			l.outQueue.writeBytes(cBytes, l)
-			l.masterWaiter.Notify(waiter.EventIn)
+			l.masterWaiter.Notify(waiter.ReadableEvents)
 		}
 
 		// If we finish a line, make it available for reading.
diff --git a/pkg/sentry/fsimpl/devpts/queue.go b/pkg/sentry/fsimpl/devpts/queue.go
index 55bff3e60..47b0f1599 100644
--- a/pkg/sentry/fsimpl/devpts/queue.go
+++ b/pkg/sentry/fsimpl/devpts/queue.go
@@ -69,7 +69,7 @@ func (q *queue) readReadiness(t *linux.KernelTermios) waiter.EventMask {
 	q.mu.Lock()
 	defer q.mu.Unlock()
 	if len(q.readBuf) > 0 && q.readable {
-		return waiter.EventIn
+		return waiter.ReadableEvents
 	}
 	return waiter.EventMask(0)
 }
@@ -79,7 +79,7 @@ func (q *queue) writeReadiness(t *linux.KernelTermios) waiter.EventMask {
 	q.mu.Lock()
 	defer q.mu.Unlock()
 	if q.waitBufLen < waitBufMaxBytes {
-		return waiter.EventOut
+		return waiter.WritableEvents
 	}
 	return waiter.EventMask(0)
 }
diff --git a/pkg/sentry/fsimpl/eventfd/eventfd.go b/pkg/sentry/fsimpl/eventfd/eventfd.go
index 7f810f720..30bd05357 100644
--- a/pkg/sentry/fsimpl/eventfd/eventfd.go
+++ b/pkg/sentry/fsimpl/eventfd/eventfd.go
@@ -185,7 +185,7 @@ func (efd *EventFileDescription) read(ctx context.Context, dst usermem.IOSequenc
 	// Notify writers. We do this even if we were already writable because
 	// it is possible that a writer is waiting to write the maximum value
 	// to the event.
-	efd.queue.Notify(waiter.EventOut)
+	efd.queue.Notify(waiter.WritableEvents)
 
 	var buf [8]byte
 	usermem.ByteOrder.PutUint64(buf[:], val)
@@ -238,7 +238,7 @@ func (efd *EventFileDescription) Signal(val uint64) error {
 	efd.mu.Unlock()
 
 	// Always trigger a notification.
-	efd.queue.Notify(waiter.EventIn)
+	efd.queue.Notify(waiter.ReadableEvents)
 
 	return nil
 }
@@ -254,11 +254,11 @@ func (efd *EventFileDescription) Readiness(mask waiter.EventMask) waiter.EventMa
 
 	ready := waiter.EventMask(0)
 	if efd.val > 0 {
-		ready |= waiter.EventIn
+		ready |= waiter.ReadableEvents
 	}
 
 	if efd.val < math.MaxUint64-1 {
-		ready |= waiter.EventOut
+		ready |= waiter.WritableEvents
 	}
 
 	return mask & ready
diff --git a/pkg/sentry/fsimpl/eventfd/eventfd_test.go b/pkg/sentry/fsimpl/eventfd/eventfd_test.go
index 49916fa81..85718f813 100644
--- a/pkg/sentry/fsimpl/eventfd/eventfd_test.go
+++ b/pkg/sentry/fsimpl/eventfd/eventfd_test.go
@@ -49,7 +49,7 @@ func TestEventFD(t *testing.T) {
 
 		// Register a callback for a write event.
 		w, ch := waiter.NewChannelEntry(nil)
-		eventfd.EventRegister(&w, waiter.EventIn)
+		eventfd.EventRegister(&w, waiter.ReadableEvents)
 		defer eventfd.EventUnregister(&w)
 
 		data := []byte("00000124")
diff --git a/pkg/sentry/fsimpl/fuse/connection.go b/pkg/sentry/fsimpl/fuse/connection.go
index 34d25a61e..077bf9307 100644
--- a/pkg/sentry/fsimpl/fuse/connection.go
+++ b/pkg/sentry/fsimpl/fuse/connection.go
@@ -316,7 +316,7 @@ func (conn *connection) callFutureLocked(t *kernel.Task, r *Request) (*futureRes
 	conn.fd.completions[r.id] = fut
 
 	// Signal the readers that there is something to read.
-	conn.fd.waitQueue.Notify(waiter.EventIn)
+	conn.fd.waitQueue.Notify(waiter.ReadableEvents)
 
 	return fut, nil
 }
diff --git a/pkg/sentry/fsimpl/fuse/dev.go b/pkg/sentry/fsimpl/fuse/dev.go
index 1eeb95216..5d2bae14e 100644
--- a/pkg/sentry/fsimpl/fuse/dev.go
+++ b/pkg/sentry/fsimpl/fuse/dev.go
@@ -368,10 +368,10 @@ func (fd *DeviceFD) readinessLocked(mask waiter.EventMask) waiter.EventMask {
 	}
 
 	// FD is always writable.
-	ready |= waiter.EventOut
+	ready |= waiter.WritableEvents
 	if !fd.queue.Empty() {
 		// Have reqs available, FD is readable.
-		ready |= waiter.EventIn
+		ready |= waiter.ReadableEvents
 	}
 
 	return ready & mask
diff --git a/pkg/sentry/fsimpl/fuse/dev_test.go b/pkg/sentry/fsimpl/fuse/dev_test.go
index bb2d0d31a..04250d796 100644
--- a/pkg/sentry/fsimpl/fuse/dev_test.go
+++ b/pkg/sentry/fsimpl/fuse/dev_test.go
@@ -180,7 +180,7 @@ func ReadTest(serverTask *kernel.Task, fd *vfs.FileDescription, inIOseq usermem.
 
 	// Register for notifications.
 	w, ch := waiter.NewChannelEntry(nil)
-	dev.EventRegister(&w, waiter.EventIn)
+	dev.EventRegister(&w, waiter.ReadableEvents)
 	for {
 		// Issue the request and break out if it completes with anything other than
 		// "would block".
diff --git a/pkg/sentry/fsimpl/fuse/fusefs.go b/pkg/sentry/fsimpl/fuse/fusefs.go
index fef857afb..167c899e2 100644
--- a/pkg/sentry/fsimpl/fuse/fusefs.go
+++ b/pkg/sentry/fsimpl/fuse/fusefs.go
@@ -286,7 +286,7 @@ func (fs *filesystem) Release(ctx context.Context) {
 	fs.umounted = true
 	fs.conn.Abort(ctx)
 	// Notify all the waiters on this fd.
-	fs.conn.fd.waitQueue.Notify(waiter.EventIn)
+	fs.conn.fd.waitQueue.Notify(waiter.ReadableEvents)
 
 	fs.conn.fd.mu.Unlock()
 
diff --git a/pkg/sentry/fsimpl/host/socket.go b/pkg/sentry/fsimpl/host/socket.go
index 056f910aa..60e237ac7 100644
--- a/pkg/sentry/fsimpl/host/socket.go
+++ b/pkg/sentry/fsimpl/host/socket.go
@@ -192,7 +192,7 @@ func (c *ConnectedEndpoint) Writable() bool {
 	c.mu.RLock()
 	defer c.mu.RUnlock()
 
-	return fdnotifier.NonBlockingPoll(int32(c.fd), waiter.EventOut)&waiter.EventOut != 0
+	return fdnotifier.NonBlockingPoll(int32(c.fd), waiter.WritableEvents)&waiter.WritableEvents != 0
 }
 
 // Passcred implements transport.ConnectedEndpoint.Passcred.
@@ -282,7 +282,7 @@ func (c *ConnectedEndpoint) Readable() bool {
 	c.mu.RLock()
 	defer c.mu.RUnlock()
 
-	return fdnotifier.NonBlockingPoll(int32(c.fd), waiter.EventIn)&waiter.EventIn != 0
+	return fdnotifier.NonBlockingPoll(int32(c.fd), waiter.ReadableEvents)&waiter.ReadableEvents != 0
 }
 
 // SendQueuedSize implements transport.Receiver.SendQueuedSize.
diff --git a/pkg/sentry/fsimpl/signalfd/signalfd.go b/pkg/sentry/fsimpl/signalfd/signalfd.go
index 246bd87bc..a7f5928b7 100644
--- a/pkg/sentry/fsimpl/signalfd/signalfd.go
+++ b/pkg/sentry/fsimpl/signalfd/signalfd.go
@@ -117,8 +117,8 @@ func (sfd *SignalFileDescription) Read(ctx context.Context, dst usermem.IOSequen
 func (sfd *SignalFileDescription) Readiness(mask waiter.EventMask) waiter.EventMask {
 	sfd.mu.Lock()
 	defer sfd.mu.Unlock()
-	if mask&waiter.EventIn != 0 && sfd.target.PendingSignals()&sfd.mask != 0 {
-		return waiter.EventIn // Pending signals.
+	if mask&waiter.ReadableEvents != 0 && sfd.target.PendingSignals()&sfd.mask != 0 {
+		return waiter.ReadableEvents // Pending signals.
 	}
 	return 0
 }
diff --git a/pkg/sentry/fsimpl/timerfd/timerfd.go b/pkg/sentry/fsimpl/timerfd/timerfd.go
index 8853c8ad2..64d33c3a8 100644
--- a/pkg/sentry/fsimpl/timerfd/timerfd.go
+++ b/pkg/sentry/fsimpl/timerfd/timerfd.go
@@ -105,7 +105,7 @@ func (tfd *TimerFileDescription) SetTime(s ktime.Setting) (ktime.Time, ktime.Set
 func (tfd *TimerFileDescription) Readiness(mask waiter.EventMask) waiter.EventMask {
 	var ready waiter.EventMask
 	if atomic.LoadUint64(&tfd.val) != 0 {
-		ready |= waiter.EventIn
+		ready |= waiter.ReadableEvents
 	}
 	return ready
 }
@@ -138,7 +138,7 @@ func (tfd *TimerFileDescription) Release(context.Context) {
 // Notify implements ktime.TimerListener.Notify.
 func (tfd *TimerFileDescription) Notify(exp uint64, setting ktime.Setting) (ktime.Setting, bool) {
 	atomic.AddUint64(&tfd.val, exp)
-	tfd.events.Notify(waiter.EventIn)
+	tfd.events.Notify(waiter.ReadableEvents)
 	return ktime.Setting{}, false
 }
 
-- 
cgit v1.2.3