On Sat, Dec 11, 2021 at 01:47:41PM +0000, Visa Hankala wrote:
> This adds EVFILT_EXCEPT handler for ttys to let kqueue-based poll(2)
> detect POLLHUP when pollfd.event == 0.
>
> filt_ttywrite(), and also filt_ptcwrite(), appear to lack HUP detection.
> Has this been intentional?
>
> The poll(2) emulation would need the HUP bits if feature similarity to
> ttpoll(), and ptcpoll(), is wanted.
>
> The patch also extends the scope of spltty() to cover the checking of
> t_cflag and t_state, and modification of kn, in the event filters.
Here is an updated patch. The changes to previous version are:
* Add HUP condition checking to both filt_ttywrite() and
filt_ptcwrite(). However, it is restricted to poll(2) and select(2)
to avoid possible ill effects with kevent(2).
* Flags EV_EOF and __EV_HUP are now cleared when the EOF or HUP
condition ends. The clearing is necessary with files that can revive.
I have committed the spltty() change to filt_ttyread().
OK?
Index: kern/tty.c
===================================================================
RCS file: src/sys/kern/tty.c,v
retrieving revision 1.172
diff -u -p -r1.172 tty.c
--- kern/tty.c 14 Dec 2021 15:32:20 -0000 1.172
+++ kern/tty.c 14 Dec 2021 16:56:56 -0000
@@ -78,6 +78,7 @@ int filt_ttyread(struct knote *kn, long
void filt_ttyrdetach(struct knote *kn);
int filt_ttywrite(struct knote *kn, long hint);
void filt_ttywdetach(struct knote *kn);
+int filt_ttyexcept(struct knote *kn, long hint);
void ttystats_init(struct itty **, int *, size_t *);
int ttywait_nsec(struct tty *, uint64_t);
int ttysleep_nsec(struct tty *, void *, int, char *, uint64_t);
@@ -1110,6 +1111,13 @@ const struct filterops ttywrite_filtops
.f_event = filt_ttywrite,
};
+const struct filterops ttyexcept_filtops = {
+ .f_flags = FILTEROP_ISFD,
+ .f_attach = NULL,
+ .f_detach = filt_ttyrdetach,
+ .f_event = filt_ttyexcept,
+};
+
int
ttkqfilter(dev_t dev, struct knote *kn)
{
@@ -1126,6 +1134,18 @@ ttkqfilter(dev_t dev, struct knote *kn)
klist = &tp->t_wsel.si_note;
kn->kn_fop = &ttywrite_filtops;
break;
+ case EVFILT_EXCEPT:
+ if (kn->kn_flags & __EV_SELECT) {
+ /* Prevent triggering exceptfds. */
+ return (EPERM);
+ }
+ if ((kn->kn_flags & __EV_POLL) == 0) {
+ /* Disallow usage through kevent(2). */
+ return (EINVAL);
+ }
+ klist = &tp->t_rsel.si_note;
+ kn->kn_fop = &ttyexcept_filtops;
+ break;
default:
return (EINVAL);
}
@@ -1164,6 +1184,8 @@ filt_ttyread(struct knote *kn, long hint
if (kn->kn_flags & __EV_POLL)
kn->kn_flags |= __EV_HUP;
active = 1;
+ } else {
+ kn->kn_flags &= ~(EV_EOF | __EV_HUP);
}
splx(s);
return (active);
@@ -1184,13 +1206,45 @@ int
filt_ttywrite(struct knote *kn, long hint)
{
struct tty *tp = kn->kn_hook;
- int canwrite, s;
+ int active, s;
s = spltty();
kn->kn_data = tp->t_outq.c_cn - tp->t_outq.c_cc;
- canwrite = (tp->t_outq.c_cc <= tp->t_lowat);
+ active = (tp->t_outq.c_cc <= tp->t_lowat);
+
+ /* Write-side HUP condition is only for poll(2) and select(2). */
+ if (kn->kn_flags & (__EV_POLL | __EV_SELECT)) {
+ if (!ISSET(tp->t_cflag, CLOCAL) &&
+ !ISSET(tp->t_state, TS_CARR_ON)) {
+ kn->kn_flags |= __EV_HUP;
+ active = 1;
+ } else {
+ kn->kn_flags &= ~__EV_HUP;
+ }
+ }
+ splx(s);
+ return (active);
+}
+
+int
+filt_ttyexcept(struct knote *kn, long hint)
+{
+ struct tty *tp = kn->kn_hook;
+ int active = 0;
+ int s;
+
+ s = spltty();
+ if (kn->kn_flags & __EV_POLL) {
+ if (!ISSET(tp->t_cflag, CLOCAL) &&
+ !ISSET(tp->t_state, TS_CARR_ON)) {
+ kn->kn_flags |= __EV_HUP;
+ active = 1;
+ } else {
+ kn->kn_flags &= ~__EV_HUP;
+ }
+ }
splx(s);
- return (canwrite);
+ return (active);
}
static int
Index: kern/tty_pty.c
===================================================================
RCS file: src/sys/kern/tty_pty.c,v
retrieving revision 1.111
diff -u -p -r1.111 tty_pty.c
--- kern/tty_pty.c 13 Dec 2021 14:56:55 -0000 1.111
+++ kern/tty_pty.c 14 Dec 2021 16:56:56 -0000
@@ -667,6 +667,7 @@ filt_ptcread(struct knote *kn, long hint
{
struct pt_softc *pti = (struct pt_softc *)kn->kn_hook;
struct tty *tp;
+ int active;
tp = pti->pt_tty;
kn->kn_data = 0;
@@ -678,15 +679,18 @@ filt_ptcread(struct knote *kn, long hint
((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))
kn->kn_data++;
}
+ active = (kn->kn_data > 0);
if (!ISSET(tp->t_state, TS_CARR_ON)) {
kn->kn_flags |= EV_EOF;
if (kn->kn_flags & __EV_POLL)
kn->kn_flags |= __EV_HUP;
- return (1);
+ active = 1;
+ } else {
+ kn->kn_flags &= ~(EV_EOF | __EV_HUP);
}
- return (kn->kn_data > 0);
+ return (active);
}
void
@@ -705,6 +709,7 @@ filt_ptcwrite(struct knote *kn, long hin
{
struct pt_softc *pti = (struct pt_softc *)kn->kn_hook;
struct tty *tp;
+ int active;
tp = pti->pt_tty;
kn->kn_data = 0;
@@ -718,8 +723,19 @@ filt_ptcwrite(struct knote *kn, long hin
kn->kn_data = tp->t_canq.c_cn -
(tp->t_rawq.c_cc + tp->t_canq.c_cc);
}
+ active = (kn->kn_data > 0);
- return (kn->kn_data > 0);
+ /* Write-side HUP condition is only for poll(2) and select(2). */
+ if (kn->kn_flags & (__EV_POLL | __EV_SELECT)) {
+ if (!ISSET(tp->t_state, TS_CARR_ON)) {
+ kn->kn_flags |= __EV_HUP;
+ active = 1;
+ } else {
+ kn->kn_flags &= ~__EV_HUP;
+ }
+ }
+
+ return (active);
}
int
@@ -745,6 +761,8 @@ filt_ptcexcept(struct knote *kn, long hi
if (!ISSET(tp->t_state, TS_CARR_ON)) {
kn->kn_flags |= __EV_HUP;
active = 1;
+ } else {
+ kn->kn_flags &= ~__EV_HUP;
}
}