Merge pull request #9336 from thundergolfer:jonathon/dev-pts-eof-handling

PiperOrigin-RevId: 563199056
This commit is contained in:
gVisor bot
2023-09-06 13:29:19 -07:00
3 changed files with 59 additions and 2 deletions
+51
View File
@@ -82,3 +82,54 @@ func TestEchoDeadlock(t *testing.T) {
t.Fatalf("written and read strings do not match: got %q, want %q", outStr, inStr)
}
}
func TestEndOfFileHandling(t *testing.T) {
ctx := contexttest.Context(t)
termios := linux.DefaultReplicaTermios
ld := newLineDiscipline(termios, nil)
// EOF with non-empty read buffer.
inBytes := []byte("hello, tty")
inBytes = append(inBytes, termios.ControlCharacters[linux.VEOF])
outBytes := make([]byte, 32)
dst := usermem.BytesIOSequence(outBytes)
// Write to the input queue.
nw, err := ld.inputQueueWrite(ctx, usermem.BytesIOSequence(inBytes))
if err != nil {
t.Fatalf("error writing to input queue: %v", err)
}
if nw != int64(len(inBytes)) {
t.Fatalf("wrote wrong length: got %d, want %d", nw, len(inBytes))
}
// Read from the input queue.
nr, err := ld.inputQueueRead(ctx, dst)
if err != nil {
t.Fatalf("error reading from input queue: %v", err)
}
if nr != int64(len(inBytes)-1) {
t.Fatalf("read wrong length: got %d, want %d", nr, len(inBytes)-1)
}
// EOF with empty read buffer.
inBytes = []byte{termios.ControlCharacters[linux.VEOF]}
outBytes = make([]byte, 32)
dst = usermem.BytesIOSequence(outBytes)
// Write to the input queue.
nw, err = ld.inputQueueWrite(ctx, usermem.BytesIOSequence(inBytes))
if err != nil {
t.Fatalf("error writing to input queue: %v", err)
}
if nw != int64(len(inBytes)) {
t.Fatalf("wrote wrong length: got %d, want %d", nw, len(inBytes))
}
// Read from the input queue.
nr, err = ld.inputQueueRead(ctx, dst)
if err != nil {
t.Fatalf("error reading from input queue: %v", err)
}
if nr != 0 {
t.Fatalf("read length should be zero: got %d", nr)
}
}
+7 -1
View File
@@ -198,6 +198,7 @@ func (l *lineDiscipline) inputQueueReadSize(t *kernel.Task, io usermem.IO, args
func (l *lineDiscipline) inputQueueRead(ctx context.Context, dst usermem.IOSequence) (int64, error) {
l.termiosMu.RLock()
n, pushed, notifyEcho, err := l.inQueue.read(ctx, dst, l)
isCanon := l.termios.LEnabled(linux.ICANON)
l.termiosMu.RUnlock()
if err != nil {
return 0, err
@@ -212,9 +213,14 @@ func (l *lineDiscipline) inputQueueRead(ctx context.Context, dst usermem.IOSeque
l.replicaWaiter.Notify(waiter.ReadableEvents)
}
return n, nil
} else if notifyEcho {
}
if notifyEcho {
l.masterWaiter.Notify(waiter.ReadableEvents)
}
if !pushed && isCanon {
return 0, nil // EOF
}
return 0, linuxerr.ErrWouldBlock
}
+1 -1
View File
@@ -36,7 +36,7 @@ const waitBufMaxBytes = 131072
// full, at which point they are written to the wait buffer. Bytes are
// processed (i.e. undergo termios transformations) as they are added to the
// read buffer. The read buffer is readable when its length is nonzero and
// readable is true.
// readable is true, or when its length is zero and readable is true (EOF).
//
// +stateify savable
type queue struct {