Page MenuHomeFreeBSD

D61973.1795593923.diff
No OneTemporary

Authored By
Unknown
Size
7 KB
Referenced Files
None
Subscribers
None

D61973.1795593923.diff

diff --git a/sys/dev/snp/snp.c b/sys/dev/snp/snp.c
--- a/sys/dev/snp/snp.c
+++ b/sys/dev/snp/snp.c
@@ -51,12 +51,14 @@
"tty snoop registration", MTX_DEF);
#define SNP_LOCK() mtx_lock(&snp_register_lock)
#define SNP_UNLOCK() mtx_unlock(&snp_register_lock)
+#define SNP_LOCK_ASSERT() mtx_assert(&snp_register_lock, MA_OWNED)
#else
static struct sx snp_register_lock;
SX_SYSINIT(snp_register_lock, &snp_register_lock,
"tty snoop registration");
#define SNP_LOCK() sx_xlock(&snp_register_lock)
#define SNP_UNLOCK() sx_xunlock(&snp_register_lock)
+#define SNP_LOCK_ASSERT() sx_assert(&snp_register_lock, SA_XLOCKED)
#endif
#define SNPGTYY_32DEV _IOR('T', 89, uint32_t)
@@ -92,9 +94,11 @@
};
static th_getc_capture_t snp_getc_capture;
+static th_close_t snp_close;
static struct ttyhook snp_hook = {
.th_getc_capture = snp_getc_capture,
+ .th_close = snp_close,
};
/*
@@ -109,21 +113,68 @@
struct ttyoutq snp_outq; /* (t) Output queue. */
struct cv snp_outwait; /* (t) Output wait queue. */
struct selinfo snp_outpoll; /* (t) Output polling. */
+ u_int snp_busy; /* (t) Users of snp_tty. */
};
+/*
+ * Unregistering the hook might free the TTY. A thread that uses the
+ * TTY after dropping snp_register_lock holds a use reference, and
+ * snp_detach() waits for all of them.
+ */
+static struct tty *
+snp_tty_acquire(struct snp_softc *ss)
+{
+ struct tty *tp;
+
+ SNP_LOCK();
+ tp = ss->snp_tty;
+ if (tp != NULL) {
+ tty_lock(tp);
+ ss->snp_busy++;
+ tty_unlock(tp);
+ }
+ SNP_UNLOCK();
+ return (tp);
+}
+
+static void
+snp_tty_release(struct snp_softc *ss, struct tty *tp)
+{
+ tty_lock(tp);
+ MPASS(ss->snp_busy > 0);
+ if (--ss->snp_busy == 0)
+ wakeup(&ss->snp_busy);
+ tty_unlock(tp);
+}
+
+static void
+snp_detach(struct snp_softc *ss)
+{
+ struct tty *tp;
+
+ SNP_LOCK_ASSERT();
+ tp = ss->snp_tty;
+ if (tp == NULL)
+ return;
+
+ /* Make the readers waiting for data leave, and wait for all users. */
+ tty_lock(tp);
+ ss->snp_tty = NULL;
+ cv_broadcast(&ss->snp_outwait);
+ while (ss->snp_busy > 0)
+ msleep(&ss->snp_busy, tty_getlock(tp), 0, "snpdtc", 0);
+ ttyoutq_free(&ss->snp_outq);
+ ttyhook_unregister(tp);
+}
+
static void
snp_dtor(void *data)
{
struct snp_softc *ss = data;
- struct tty *tp;
- tp = ss->snp_tty;
- if (tp != NULL) {
- tty_lock(tp);
- ttyoutq_free(&ss->snp_outq);
- ttyhook_unregister(tp);
- ss->snp_tty = NULL;
- }
+ SNP_LOCK();
+ snp_detach(ss);
+ SNP_UNLOCK();
seldrain(&ss->snp_outpoll);
cv_destroy(&ss->snp_outwait);
@@ -164,9 +215,13 @@
if (error != 0)
return (error);
- tp = ss->snp_tty;
- if (tp == NULL || tty_gone(tp))
+ tp = snp_tty_acquire(ss);
+ if (tp == NULL)
return (EIO);
+ if (tty_gone(tp)) {
+ snp_tty_release(ss, tp);
+ return (EIO);
+ }
tty_lock(tp);
for (;;) {
@@ -179,6 +234,11 @@
error = EWOULDBLOCK;
break;
}
+ /* snp_detach() is waiting for us. */
+ if (ss->snp_tty != tp) {
+ error = EIO;
+ break;
+ }
error = cv_wait_sig(&ss->snp_outwait, tty_getlock(tp));
if (error != 0)
break;
@@ -188,6 +248,7 @@
}
}
tty_unlock(tp);
+ snp_tty_release(ss, tp);
return (error);
}
@@ -204,23 +265,31 @@
if (error != 0)
return (error);
- tp = ss->snp_tty;
- if (tp == NULL || tty_gone(tp))
+ tp = snp_tty_acquire(ss);
+ if (tp == NULL)
return (EIO);
+ if (tty_gone(tp)) {
+ snp_tty_release(ss, tp);
+ return (EIO);
+ }
while (uio->uio_resid > 0) {
/* Read new data. */
len = imin(uio->uio_resid, sizeof in);
error = uiomove(in, len, uio);
if (error != 0)
- return (error);
+ break;
tty_lock(tp);
- /* Driver could have abandoned the TTY in the mean time. */
- if (tty_gone(tp)) {
+ /*
+ * Driver could have abandoned the TTY in the mean time,
+ * or snp_detach() is waiting for us.
+ */
+ if (tty_gone(tp) || ss->snp_tty != tp) {
tty_unlock(tp);
- return (ENXIO);
+ error = ENXIO;
+ break;
}
/*
@@ -233,8 +302,9 @@
tty_unlock(tp);
}
+ snp_tty_release(ss, tp);
- return (0);
+ return (error);
}
static int
@@ -256,10 +326,7 @@
tp = ss->snp_tty;
if (tp != NULL) {
if (*(int *)data == -1) {
- tty_lock(tp);
- ss->snp_tty = NULL;
- ttyoutq_free(&ss->snp_outq);
- ttyhook_unregister(tp);
+ snp_detach(ss);
error = 0;
} else {
error = EBUSY;
@@ -272,31 +339,38 @@
*/
error = ttyhook_register(&ss->snp_tty, td->td_proc,
*(int *)data, &snp_hook, ss);
- SNP_UNLOCK();
- if (error != 0)
+ if (error != 0) {
+ SNP_UNLOCK();
return (error);
+ }
/* Now that went okay, allocate a buffer for the queue. */
tp = ss->snp_tty;
tty_lock(tp);
ttyoutq_setsize(&ss->snp_outq, tp, SNP_OUTPUT_BUFSIZE);
tty_unlock(tp);
+ SNP_UNLOCK();
return (0);
case SNPGTTY:
/* Obtain device number of associated TTY. */
+ SNP_LOCK();
if (ss->snp_tty == NULL)
*(dev_t *)data = NODEV;
else
*(dev_t *)data = tty_udev(ss->snp_tty);
+ SNP_UNLOCK();
return (0);
case SNPGTYY_32DEV:
+ SNP_LOCK();
if (ss->snp_tty == NULL)
*(uint32_t *)data = -1;
else
*(uint32_t *)data = tty_udev(ss->snp_tty); /* trunc */
+ SNP_UNLOCK();
return (0);
case FIONREAD:
+ SNP_LOCK();
tp = ss->snp_tty;
if (tp != NULL) {
tty_lock(tp);
@@ -308,6 +382,7 @@
} else {
*(int *)data = SNP_DETACH;
}
+ SNP_UNLOCK();
return (0);
default:
return (ENOTTY);
@@ -326,19 +401,21 @@
(POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
revents = 0;
-
- if (events & (POLLIN | POLLRDNORM)) {
- tp = ss->snp_tty;
- if (tp != NULL) {
- tty_lock(tp);
- if (ttyoutq_bytesused(&ss->snp_outq) > 0)
- revents |= events & (POLLIN | POLLRDNORM);
- tty_unlock(tp);
- }
+ SNP_LOCK();
+ tp = ss->snp_tty;
+ if (tp != NULL) {
+ tty_lock(tp);
+ if (tty_gone(tp))
+ revents |= POLLHUP;
}
-
+ if ((events & (POLLIN | POLLRDNORM)) != 0 && tp != NULL &&
+ ttyoutq_bytesused(&ss->snp_outq) > 0)
+ revents |= events & (POLLIN | POLLRDNORM);
if (revents == 0)
selrecord(td, &ss->snp_outpoll);
+ if (tp != NULL)
+ tty_unlock(tp);
+ SNP_UNLOCK();
return (revents);
}
@@ -376,6 +453,16 @@
selwakeup(&ss->snp_outpoll);
}
+static void
+snp_close(struct tty *tp)
+{
+ struct snp_softc *ss = ttyhook_softc(tp);
+
+ /* Let the readers and the pollers notice a gone TTY. */
+ cv_broadcast(&ss->snp_outwait);
+ selwakeup(&ss->snp_outpoll);
+}
+
static moduledata_t snp_mod = {
"snp",
snp_modevent,
diff --git a/sys/kern/tty.c b/sys/kern/tty.c
--- a/sys/kern/tty.c
+++ b/sys/kern/tty.c
@@ -1442,6 +1442,11 @@
cv_broadcast(&tp->t_dcdwait);
tp->t_flags |= TF_GONE;
+
+ /* Let the hook notice that the TTY is gone. */
+ if (ttyhook_hashook(tp, close))
+ ttyhook_close(tp);
+
tty_rel_free(tp, false);
}
diff --git a/sys/sys/ttyhook.h b/sys/sys/ttyhook.h
--- a/sys/sys/ttyhook.h
+++ b/sys/sys/ttyhook.h
@@ -63,6 +63,7 @@
th_getc_capture_t *th_getc_capture;
th_getc_poll_t *th_getc_poll;
+ /* Last close of the TTY, or the TTY is gone. */
th_close_t *th_close;
};
diff --git a/usr.sbin/watch/watch.c b/usr.sbin/watch/watch.c
--- a/usr.sbin/watch/watch.c
+++ b/usr.sbin/watch/watch.c
@@ -403,8 +403,11 @@
} else
cleanup(-1);
break;
- case SNP_DETACH:
case SNP_TTYCLOSE:
+ /* snp(4) keeps a gone tty attached until detached. */
+ detach_snp();
+ /* FALLTHROUGH */
+ case SNP_DETACH:
if (opt_reconn_close)
attach_snp();
else if (opt_interactive && !opt_no_switch) {

File Metadata

Mime Type
text/plain
Expires
Wed, Nov 25, 8:05 AM (8 h, 21 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41985231
Default Alt Text
D61973.1795593923.diff (7 KB)

Event Timeline