PERFORCE change 148020 for review

Ed Schouten ed at FreeBSD.org
Thu Aug 21 18:11:31 UTC 2008


http://perforce.freebsd.org/chv.cgi?CH=148020

Change 148020 by ed at ed_flippo on 2008/08/21 18:10:59

	Finish the ttyhook interface that at least simulating user input
	works like it should. Also change the TTY layer to test for
	hooks before deallocating TTY's.

Affected files ...

.. //depot/projects/mpsafetty/sys/dev/snp/snp.c#5 edit
.. //depot/projects/mpsafetty/sys/kern/tty.c#26 edit

Differences ...

==== //depot/projects/mpsafetty/sys/dev/snp/snp.c#5 (text+ko) ====

@@ -36,6 +36,7 @@
 #include <sys/sx.h>
 #include <sys/systm.h>
 #include <sys/tty.h>
+#include <sys/uio.h>
 
 static struct cdev	*snp_dev;
 static struct sx	snp_register_lock;
@@ -69,6 +70,13 @@
 snp_dtor(void *data)
 {
 	struct snp_softc *ss = data;
+	struct tty *tp;
+
+	tp = ss->snp_tty;
+	if (tp != NULL) {
+		tty_lock(tp);
+		ttyhook_unregister(tp);
+	}
 
 	free(ss, M_SNP);
 }
@@ -95,8 +103,51 @@
 static int
 snp_write(struct cdev *dev, struct uio *uio, int flag)
 {
+	struct snp_softc *ss;
+	struct tty *tp;
+	int error, len, i;
+	char in[8];
+
+	error = devfs_get_cdevpriv((void **)&ss);
+	if (error != 0)
+		return (error);
+	
+	tp = ss->snp_tty;
+	if (tp == NULL)
+		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);
 
-	return (EIO);
+		tty_lock(tp);
+
+		/* Driver could have abandoned the TTY in the mean time. */
+		if (tty_gone(tp)) {
+			tty_unlock(tp);
+			return (ENXIO);
+		}
+
+		/*
+		 * Deliver data to the TTY. Ignore errors for now,
+		 * because we shouldn't bail out when we're running
+		 * close to the watermarks.
+		 */
+		if (ttydisc_can_bypass(tp)) {
+			ttydisc_rint_bypass(tp, in, len);
+		} else {
+			for (i = 0; i < len; i++)
+				ttydisc_rint(tp, in[i], 0);
+		}
+
+		ttydisc_rint_done(tp);
+		tty_unlock(tp);
+	}
+
+	return (0);
 }
 
 static int

==== //depot/projects/mpsafetty/sys/kern/tty.c#26 (text+ko) ====

@@ -931,7 +931,7 @@
 	tty_lock_assert(tp, MA_OWNED);
 
 	if (tp->t_sessioncnt != 0 ||
-	    (tp->t_flags & (TF_GONE|TF_OPENED)) != TF_GONE) {
+	    (tp->t_flags & (TF_GONE|TF_OPENED|TF_HOOK)) != TF_GONE) {
 		/* TTY is still in use. */
 		tty_unlock(tp);
 		return;
@@ -1710,6 +1710,7 @@
 
 	tp->t_flags |= TF_HOOK;
 	tp->t_hook = th;
+	*rtp = tp;
 	error = 0;
 
 done3:	tty_unlock(tp);
@@ -1722,7 +1723,15 @@
 ttyhook_unregister(struct tty *tp)
 {
 
+	tty_lock_assert(tp, MA_OWNED);
 	MPASS(tp->t_flags & TF_HOOK);
+
+	/* Disconnect the hook */
+	tp->t_flags &= ~TF_HOOK;
+	tp->t_hook = NULL;
+
+	/* Maybe deallocate the TTY as well */
+	tty_rel_free(tp);
 }
 
 #include "opt_ddb.h"


More information about the p4-projects mailing list