Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 27 Mar 2000 18:17:19 -0800 (PST)
From:      brooks@one-eyed-alien.net
To:        FreeBSD-gnats-submit@freebsd.org
Subject:   kern/17629: "almost clone" patch for device snp
Message-ID:  <200003280217.SAA00582@minya.sea.one-eyed-alien.net>

next in thread | raw e-mail | index | archive | help

>Number:         17629
>Category:       kern
>Synopsis:       "almost clone" patch for device snp
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Mon Mar 27 18:20:01 PST 2000
>Closed-Date:
>Last-Modified:
>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:
>Unformatted:


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message




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