From brooks@minya.sea.one-eyed-alien.net  Mon Mar 27 18:16:44 2000
Return-Path: <brooks@minya.sea.one-eyed-alien.net>
Received: from minya.sea.one-eyed-alien.net (minya.sea.one-eyed-alien.net [216.39.156.226])
	by hub.freebsd.org (Postfix) with ESMTP id 92B4037BCC2
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 27 Mar 2000 18:16:04 -0800 (PST)
	(envelope-from brooks@minya.sea.one-eyed-alien.net)
Received: (from brooks@localhost)
	by minya.sea.one-eyed-alien.net (8.9.3/8.9.3) id SAA00582;
	Mon, 27 Mar 2000 18:17:19 -0800 (PST)
	(envelope-from brooks)
Message-Id: <200003280217.SAA00582@minya.sea.one-eyed-alien.net>
Date: Mon, 27 Mar 2000 18:17:19 -0800 (PST)
From: brooks@one-eyed-alien.net
Sender: brooks@minya.sea.one-eyed-alien.net
To: FreeBSD-gnats-submit@freebsd.org
Subject: "almost clone" patch for device snp
X-Send-Pr-Version: 3.2

>Number:         17629
>Category:       kern
>Synopsis:       "almost clone" patch for device snp
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    green
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Mon Mar 27 18:20:01 PST 2000
>Closed-Date:    Tue May 16 18:31:54 PDT 2000
>Last-Modified:  Tue May 16 18:35:51 PDT 2000
>Originator:     Brooks Davis
>Release:        FreeBSD 5.0-CURRENT i386
>Organization:
The Aerospace Corporation
>Environment:

FreeBSD minya 5.0-CURRENT FreeBSD 5.0-CURRENT #15: Mon Mar 27 17:45:47 PST 2000     root@minya:/usr/src/sys/compile/MINYA  i386

>Description:

The following patch modifies the snp device to grow automagicaly like
bpf, tun, and pty do.  I had some time on my hands so I looked up phk's
message, "Kernel hacker tasks seek intrested hackers", and this one
looked easy, useful, and somewhat informative so I did it.  The patch
is mostly based on rev 1.53 of bpf.c which did the same thing to bpf.

>How-To-Repeat:
>Fix:

The patch is below.  LINT should also be modified changing:

pseudo-device	snp	3	#Snoop device - to look at pty/vty/etc..

to:

pseudo-device	snp		#Snoop device - to look at pty/vty/etc..


Index: tty_snoop.c
===================================================================
RCS file: /home/ncvs/src/sys/kern/tty_snoop.c,v
retrieving revision 1.45
diff -u -r1.45 tty_snoop.c
--- tty_snoop.c	1999/11/18 06:39:47	1.45
+++ tty_snoop.c	2000/03/28 01:30:32
@@ -32,6 +32,8 @@
 #include <sys/snoop.h>
 #include <sys/vnode.h>
 
+MALLOC_DEFINE(M_SNP, "snp", "Snoop Interface");
+
 static	d_open_t	snpopen;
 static	d_close_t	snpclose;
 static	d_read_t	snpread;
@@ -62,8 +64,6 @@
 #define MIN(a,b) (((a)<(b))?(a):(b))
 #endif
 
-static struct snoop snoopsw[NSNP];
-
 static struct tty	*snpdevtotty __P((dev_t dev));
 static int		snp_detach __P((struct snoop *snp));
 
@@ -90,8 +90,8 @@
 	struct uio     *uio;
 	int             flag;
 {
-	int             unit = minor(dev), len, i, error;
-	struct snoop   *snp = &snoopsw[unit];
+	int		len, i, error;
+	struct snoop   *snp = dev->si_drv1;
 	struct tty     *tp;
 	char		c[SNP_INPUT_BUF];
 
@@ -131,8 +131,8 @@
 	struct uio     *uio;
 	int             flag;
 {
-	int             unit = minor(dev), s;
-	struct snoop   *snp = &snoopsw[unit];
+	int		s;
+	struct snoop   *snp = dev->si_drv1;
 	int             len, n, nblen, error = 0;
 	caddr_t         from;
 	char           *nbuf;
@@ -174,9 +174,9 @@
 	if (((nblen / 2) >= SNOOP_MINLEN) && (nblen / 2) >= snp->snp_len) {
 		while (((nblen / 2) >= snp->snp_len) && ((nblen / 2) >= SNOOP_MINLEN))
 			nblen = nblen / 2;
-		if ((nbuf = malloc(nblen, M_TTYS, M_NOWAIT)) != NULL) {
+		if ((nbuf = malloc(nblen, M_SNP, M_NOWAIT)) != NULL) {
 			bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
-			free(snp->snp_buf, M_TTYS);
+			free(snp->snp_buf, M_SNP);
 			snp->snp_buf = nbuf;
 			snp->snp_blen = nblen;
 			snp->snp_base = 0;
@@ -213,12 +213,6 @@
 	if (n == 0)
 		return 0;
 
-#ifdef DIAGNOSTIC
-	if (!(snp->snp_flags & SNOOP_OPEN)) {
-		printf("Snoop: data coming to closed device.\n");
-		return 0;
-	}
-#endif
 	if (snp->snp_flags & SNOOP_DOWN) {
 		printf("Snoop: more data to down interface.\n");
 		return 0;
@@ -246,9 +240,9 @@
 			nblen = snp->snp_blen * 2;
 			s_free = nblen - (snp->snp_len + snp->snp_base);
 		}
-		if ((n <= s_free) && (nbuf = malloc(nblen, M_TTYS, M_NOWAIT))) {
+		if ((n <= s_free) && (nbuf = malloc(nblen, M_SNP, M_NOWAIT))) {
 			bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
-			free(snp->snp_buf, M_TTYS);
+			free(snp->snp_buf, M_SNP);
 			snp->snp_buf = nbuf;
 			snp->snp_blen = nblen;
 			snp->snp_base = 0;
@@ -291,26 +285,22 @@
 	struct proc    *p;
 {
 	struct snoop   *snp;
-	register int    unit, error;
+	register int    error;
 
 	if ((error = suser(p)) != 0)
 		return (error);
 
-	if ((unit = minor(dev)) >= NSNP)
-		return (ENXIO);
+	snp = dev->si_drv1;
 
-	snp = &snoopsw[unit];
+	if (snp)
+		return (EBUSY);
 
-	if (snp->snp_flags & SNOOP_OPEN)
-		return (ENXIO);
+	make_dev(&snp_cdevsw, minor(dev), 0, 0, 0600, "bpf%d", lminor(dev));
+	MALLOC(snp, struct snoop *, sizeof(*snp), M_SNP, M_WAITOK);
+	bzero(snp, sizeof(*snp));
+	dev->si_drv1 = snp;
 
-	/*
-	 * We intentionally do not OR flags with SNOOP_OPEN,but set them so
-	 * all previous settings (especially SNOOP_OFLOW) will be cleared.
-	 */
-	snp->snp_flags = SNOOP_OPEN;
-
-	snp->snp_buf = malloc(SNOOP_MINLEN, M_TTYS, M_WAITOK);
+	snp->snp_buf = malloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
 	snp->snp_blen = SNOOP_MINLEN;
 	snp->snp_base = 0;
 	snp->snp_len = 0;
@@ -367,14 +357,15 @@
 	int             fmt;
 	struct proc    *p;
 {
-	register int    unit = minor(dev);
-	struct snoop   *snp = &snoopsw[unit];
+	int		error;
+	struct snoop   *snp = dev->si_drv1;
 
-	snp->snp_blen = 0;
-	free(snp->snp_buf, M_TTYS);
-	snp->snp_flags &= ~SNOOP_OPEN;
+	free(snp->snp_buf, M_SNP);
+	error = snp_detach(snp);
+	FREE(snp, M_SNP);
+	dev->si_drv1 = 0;
 
-	return (snp_detach(snp));
+	return (error);
 }
 
 int
@@ -382,8 +373,8 @@
 	struct snoop	*snp;
 {
 	snp->snp_blen = SNOOP_MINLEN;
-	free(snp->snp_buf, M_TTYS);
-	snp->snp_buf = malloc(SNOOP_MINLEN, M_TTYS, M_WAITOK);
+	free(snp->snp_buf, M_SNP);
+	snp->snp_buf = malloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
 	snp->snp_flags |= SNOOP_DOWN;
 
 	return (snp_detach(snp));
@@ -398,9 +389,9 @@
 	int             flags;
 	struct proc    *p;
 {
-	int             unit = minor(dev), s;
+	int             s;
 	dev_t		tdev;
-	struct snoop   *snp = &snoopsw[unit];
+	struct snoop   *snp = dev->si_drv1;
 	struct tty     *tp, *tpo;
 
 	switch (cmd) {
@@ -489,8 +480,7 @@
 	int             events;
 	struct proc    *p;
 {
-	int             unit = minor(dev);
-	struct snoop   *snp = &snoopsw[unit];
+	struct snoop   *snp = dev->si_drv1;
 	int		revents = 0;
 
 
@@ -514,10 +504,8 @@
 snp_drvinit(unused)
 	void *unused;
 {
-	int	i;
 
-	for (i = 0; i < NSNP; i++) 
-		make_dev(&snp_cdevsw, i, 0, 0, 0600, "snp%d", i);
+	cdevsw_add(&snp_cdevsw);
 }
 
 SYSINIT(snpdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,snp_drvinit,NULL)

>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->sheldonh 
Responsible-Changed-By: sheldonh 
Responsible-Changed-When: Wed Mar 29 04:48:21 PST 2000 
Responsible-Changed-Why:  
After chatting to phk (who's kinda busy right now), I'm going to try 
my hand at this one. 
State-Changed-From-To: open->feedback 
State-Changed-By: green 
State-Changed-When: Sat Apr 1 23:03:08 PST 2000 
State-Changed-Why:  
This should be in feedback in Responsible: set to me because I made the accident 
of actually committing this functionality with nearly the same patch, and did 
not notice this PR.  I hope you don't mind, Sheldon.  Thanks for the PR, 
Brooks!  I apologize for not seeing your PR, but don't be discourage at all: 
you did a good job, and I certainly wouldn't mind seeing more (^_^) 


Responsible-Changed-From-To: sheldonh->green 
Responsible-Changed-By: green 
Responsible-Changed-When: Sat Apr 1 23:03:08 PST 2000 
Responsible-Changed-Why:  
I inadvertently committed nearly the same code, so I should be responsible 
for this, I suppose.  I hope you don't mind, Sheldon! 

From: Brooks Davis <brooks@one-eyed-alien.net>
To: green@FreeBSD.org
Cc: freebsd-gnats-submit@FreeBSD.org
Subject: kern/17629
Date: Tue, 16 May 2000 12:48:53 -0700

 I was looking at the list of PRs I had opened that were still around and
 noticed that this was still in feedback.  Feel free to close it.  I'm
 happy with the current situation.
 
 -- Brooks
 
 -- 
 Any statement of the form "X is the one, true Y" is FALSE.
 
State-Changed-From-To: feedback->closed 
State-Changed-By: green 
State-Changed-When: Tue May 16 18:31:54 PDT 2000 
State-Changed-Why:  
Since the submitter is happy, and a MFC is quite unlikely, it's safe to 
close this :) 
>Unformatted:
