Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 21 Aug 2008 19:08:29 GMT
From:      Ed Schouten <ed@FreeBSD.org>
To:        Perforce Change Reviews <perforce@FreeBSD.org>
Subject:   PERFORCE change 148027 for review
Message-ID:  <200808211908.m7LJ8TKr090338@repoman.freebsd.org>

next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=148027

Change 148027 by ed@ed_flippo on 2008/08/21 19:08:00

	Already make snp(4) somewhat work. It still needs polishing to
	make things like select()/FIONREAD work. When that's done,
	snp(4) should almost be ready for usage.

Affected files ...

.. //depot/projects/mpsafetty/sys/dev/snp/snp.c#7 edit

Differences ...

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

@@ -29,6 +29,7 @@
 
 #include <sys/param.h>
 #include <sys/conf.h>
+#include <sys/fcntl.h>
 #include <sys/kernel.h>
 #include <sys/malloc.h>
 #include <sys/module.h>
@@ -78,6 +79,7 @@
 struct snp_softc {
 	struct tty	*snp_tty;	/* (r) TTY we're snooping. */
 	struct ttyoutq	snp_outq;	/* (t) Output queue. */
+	struct cv	snp_outwait;	/* (t) Output wait queue. */
 };
 
 static void
@@ -98,6 +100,7 @@
 		ttyhook_unregister(tp);
 	}
 
+	cv_destroy(&ss->snp_outwait);
 	free(ss, M_SNP);
 }
 
@@ -113,6 +116,7 @@
 	/* Allocate per-snoop data. */
 	ss = malloc(sizeof(struct snp_softc), M_SNP, M_WAITOK|M_ZERO);
 	ttyoutq_init(&ss->snp_outq);
+	cv_init(&ss->snp_outwait, "snp out");
 
 	devfs_set_cdevpriv(ss, snp_dtor);
 
@@ -122,8 +126,43 @@
 static int
 snp_read(struct cdev *dev, struct uio *uio, int flag)
 {
+	int error, oresid = uio->uio_resid;
+	struct snp_softc *ss;
+	struct tty *tp;
+
+	while (uio->uio_resid == 0)
+		return (0);
 
-	return (EIO);
+	error = devfs_get_cdevpriv((void **)&ss);
+	if (error != 0)
+		return (error);
+	
+	tp = ss->snp_tty;
+	if (tp == NULL || tty_gone(tp))
+		return (EIO);
+
+	tty_lock(tp);
+	for (;;) {
+		error = ttyoutq_read_uio(&ss->snp_outq, tp, uio);
+		if (error != 0 || uio->uio_resid != oresid)
+			break;
+
+		/* Wait for more data. */
+		if (flag & O_NONBLOCK) {
+			error = EWOULDBLOCK;
+			break;
+		}
+		error = cv_wait_sig(&ss->snp_outwait, tp->t_mtx);
+		if (error != 0)
+			break;
+		if (tty_gone(tp)) {
+			error = EIO;
+			break;
+		}
+	}
+	tty_unlock(tp);
+
+	return (error);
 }
 
 static int
@@ -139,7 +178,7 @@
 		return (error);
 	
 	tp = ss->snp_tty;
-	if (tp == NULL)
+	if (tp == NULL || tty_gone(tp))
 		return (EIO);
 
 	while (uio->uio_resid > 0) {
@@ -246,8 +285,8 @@
 {
 	struct snp_softc *ss = ttyhook_softc(tp);
 
-	printf("Added %zu bytes\n", len);
 	ttyoutq_write(&ss->snp_outq, buf, len);
+	cv_broadcast(&ss->snp_outwait);
 }
 
 static moduledata_t snp_mod = {



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200808211908.m7LJ8TKr090338>