Module Name:    src
Committed By:   mgorny
Date:           Fri Feb 15 18:57:15 UTC 2019

Modified Files:
        src/sys/kern: tty_pty.c
        src/tests/kernel/kqueue/read: t_ttypty.c

Log Message:
Fix reporting EOF via kevent and add a test case

Fix the kernel pty driver to report closed slave via master's kevent
EVFILT_READ.  This behavior matches the behavior for pipes, is
consistent with how FreeBSD implements it and is relied upon by LLDB's
main loop implementation.

Includes feedback by kre and kamil (from tech-kern), commit approved
by kamil.


To generate a diff of this commit:
cvs rdiff -u -r1.144 -r1.145 src/sys/kern/tty_pty.c
cvs rdiff -u -r1.2 -r1.3 src/tests/kernel/kqueue/read/t_ttypty.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/kern/tty_pty.c
diff -u src/sys/kern/tty_pty.c:1.144 src/sys/kern/tty_pty.c:1.145
--- src/sys/kern/tty_pty.c:1.144	Mon Sep  3 16:29:35 2018
+++ src/sys/kern/tty_pty.c	Fri Feb 15 18:57:15 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: tty_pty.c,v 1.144 2018/09/03 16:29:35 riastradh Exp $	*/
+/*	$NetBSD: tty_pty.c,v 1.145 2019/02/15 18:57:15 mgorny Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1989, 1993
@@ -37,7 +37,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: tty_pty.c,v 1.144 2018/09/03 16:29:35 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: tty_pty.c,v 1.145 2019/02/15 18:57:15 mgorny Exp $");
 
 #include "opt_ptm.h"
 
@@ -938,6 +938,10 @@ filt_ptcread(struct knote *kn, long hint
 		    ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))
 			kn->kn_data++;
 	}
+	if (!ISSET(tp->t_state, TS_CARR_ON)) {
+		kn->kn_flags |= EV_EOF;
+		canread = 1;
+	}
 
 	if ((hint & NOTE_SUBMIT) == 0) {
 		mutex_spin_exit(&tty_lock);

Index: src/tests/kernel/kqueue/read/t_ttypty.c
diff -u src/tests/kernel/kqueue/read/t_ttypty.c:1.2 src/tests/kernel/kqueue/read/t_ttypty.c:1.3
--- src/tests/kernel/kqueue/read/t_ttypty.c:1.2	Fri Jan 13 21:30:41 2017
+++ src/tests/kernel/kqueue/read/t_ttypty.c	Fri Feb 15 18:57:15 2019
@@ -1,7 +1,7 @@
-/* $NetBSD: t_ttypty.c,v 1.2 2017/01/13 21:30:41 christos Exp $ */
+/* $NetBSD: t_ttypty.c,v 1.3 2019/02/15 18:57:15 mgorny Exp $ */
 
 /*-
- * Copyright (c) 2002, 2008 The NetBSD Foundation, Inc.
+ * Copyright (c) 2002, 2008, 2019 The NetBSD Foundation, Inc.
  * All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
@@ -30,11 +30,12 @@
  */
 
 #include <sys/cdefs.h>
-__COPYRIGHT("@(#) Copyright (c) 2008\
+__COPYRIGHT("@(#) Copyright (c) 2008, 2019\
  The NetBSD Foundation, inc. All rights reserved.");
-__RCSID("$NetBSD: t_ttypty.c,v 1.2 2017/01/13 21:30:41 christos Exp $");
+__RCSID("$NetBSD: t_ttypty.c,v 1.3 2019/02/15 18:57:15 mgorny Exp $");
 
 #include <sys/event.h>
+#include <sys/time.h>
 #include <sys/wait.h>
 
 #include <poll.h>
@@ -135,10 +136,48 @@ ATF_TC_BODY(slave, tc)
 	h_check(false);
 }
 
+ATF_TC(closed_slave);
+ATF_TC_HEAD(closed_slave, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+		"Checks EVFILT_READ reporting for slave tty being closed");
+}
+ATF_TC_BODY(closed_slave, tc)
+{
+	char slavetty[1024];
+	struct kevent event[1];
+	int amaster, aslave;
+	int kq, n;
+	struct timespec timeout = {5, 0};
+
+	RL(openpty(&amaster, &aslave, slavetty, NULL, NULL));
+
+	(void)printf("tty: openpty master %d slave %d tty '%s'\n",
+		amaster, aslave, slavetty);
+
+	RL(kq = kqueue());
+
+	EV_SET(&event[0], amaster, EVFILT_READ, EV_ADD|EV_ENABLE, 0, 0, 0);
+	RL(kevent(kq, event, 1, NULL, 0, NULL));
+
+	RL(close(aslave));
+
+	RL(n = kevent(kq, NULL, 0, event, 1, &timeout));
+
+	(void)printf("kevent num %d filt %d flags: %#x, fflags: %#x, "
+	    "data: %" PRId64 "\n", n, event[0].filter, event[0].flags,
+	    event[0].fflags, event[0].data);
+
+	ATF_REQUIRE_EQ(n, 1);
+	ATF_REQUIRE_EQ(event[0].filter, EVFILT_READ);
+	ATF_REQUIRE_EQ(event[0].flags & EV_EOF, EV_EOF);
+}
+
 ATF_TP_ADD_TCS(tp)
 {
 	ATF_TP_ADD_TC(tp, master);
 	ATF_TP_ADD_TC(tp, slave);
+	ATF_TP_ADD_TC(tp, closed_slave);
 
 	return atf_no_error();
 }

Reply via email to