Bug 25866

Summary: [patch] more than 256 ptys, up to 1302 ptys.
Product: Base System Reporter: Cyrille Lefevre <clefevre>
Component: kernAssignee: John Baldwin <jhb>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: Unspecified   
Hardware: Any   
OS: Any   
Attachments:
Description Flags
file.diff none

Description Cyrille Lefevre 2001-03-17 02:50:00 UTC
	for years, it is not possible to have more than 256 opened ptys
	at a time. this patch deals w/ minor numbers greater than 255 and
	allow up to 1302 ptys to be opened using the following pty names :

	/dev/[pt]ty[p-uw-zP-Z][0-9a-zA-Z]
		      ^^ v cannot be used bcoz it clash w/ the syscons driver.

	same assertion about xyz (Cronyx/Sigma) and R (Rocketport), but
	I suggest to move them to something else since their names aren't
	used internaly by the kernel. something such as efg and h ?

How-To-Repeat: 
	exec sh
	ptyclassnames=pqrstuwxyzPQRSTUVWXYZ
	ptyinstancenames=0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
	nptyclass=${#ptyclassnames}
	nptyinstance=${#ptyinstancenames}
	npty=$(($nptyclass*$nptyinstance))
	ofiles=$(($npty*3))
	cat << \EOF > testpty.c
#include <sys/types.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <libutil.h>
#include <stdio.h>
#include <stdlib.h>

int main ()
{
	int i, l = 0;
	int m [npty], s [npty];
	char n [npty][11];

	for (i = 0; i < npty; i++)
		if (openpty (&m [i], &s [i], n [i],
			     (struct termios *)0, (struct winsize *)0) < 0) {
			perror ("openpty");
			fprintf (stderr, "last pty = %d\n", l);
			break;
		} else
			printf ("%d = %s\n", l = i, n [i]);
	for (i = 0; i <= l; i++)
		close (m [i]), close (s [i]);
	return (0);
}
EOF
	make testpty CFLAGS=-Dnpty=$npty LDFLAGS=-lutil
	sysctl -w kern.maxfiles=$ofiles
	sysctl -w kern.maxfilesperproc=$ofiles
	ulimit -n $ofiles
	./testpty > /dev/null
	# should give the following results less the currently opened ptys.
	openpty: No such file or directory
	last pty = 1302
Comment 1 Poul-Henning Kamp freebsd_committer freebsd_triage 2001-03-23 17:45:57 UTC
Please check out the ptynumber.patch at http://phk.freebsd.dk/patch

20010322        ptynumber.patch
 
	This is a proof-of-concept patch to change the naming of
	ptys from the letter encoding to a simple ttyp%u/ptyp%u
	like scheme.
 
	The chosen naming with the 'p' in it allows for coexistence
	of kernels and libutils which don't match up to the first
	10 ptys.

	I'm sure a number of ports may break as result of this
	because they don't use libutil to get ptys.  Please use
	this patch to fix those ports to use libutil::openpty()
	sooner rather than latter.

	I am not partial to any particular naming, there are good
	arguments for "/dev/pty/[tp]ty%d" or similar, but I cannot
	foresee the amount of breakage that might result in.
 
	There is no planned commit of this patch yet, it is provided
	for research/impactanalysis/testing only at this time.

	If anybody wants to adopt this patch, they are more than
	welcome and should contact me by email.

Poul-Henning


--
Poul-Henning Kamp       | UNIX since Zilog Zeus 3.20
phk@FreeBSD.ORG         | TCP/IP since RFC 956
FreeBSD committer       | BSD since 4.3-tahoe
Never attribute to malice what can adequately be explained by incompetence.
Comment 2 Cyrille Lefevre 2001-04-04 05:10:40 UTC
the following patch correct a typo in MAKEDEV.

Index: /usr/src/etc/MAKEDEV
===================================================================
RCS file: /home/ncvs/src/etc/MAKEDEV,v
retrieving revision 1.243.2.28
diff -u -r1.243.2.28 MAKEDEV
--- /usr/src/etc/MAKEDEV	2001/03/27 22:14:53	1.243.2.28
+++ /usr/src/etc/MAKEDEV	2001/04/03 10:39:38
@@ -231,6 +231,19 @@
   echo $(((($1 >> 8) << 16) | ($1 % 256)))
 }
 
+# Convert pty (class,instance) to minor number
+# Handle pty minor numbers greater than 256
+ptyminor()
+{
+	echo $(( (( ($1 + $2) &~ 255) << 8) | (($1 + $2) & 255) ))
+}
+
+# *MUST* be the same as in sys/kern/tty_pty.c and lib/libutil/pty.c
+ptyclassnames=pqrstuwxyzPQRSTUVWXYZ
+ptyinstancenames=0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
+nptyclass=${#ptyclassnames}
+nptyinstance=${#ptyinstancenames}
+
 # Raw partition for disks
 dkrawpart=2
 
@@ -707,34 +720,26 @@
 	done	
 	;;
 pty*)
-	class=`expr $i : 'pty\(.*\)'`
-	case $class in
-	0) offset=0 name=p;;
-	1) offset=32 name=q;;
-	2) offset=64 name=r;;
-	3) offset=96 name=s;;
-# Note that xterm (at least) only look at p-s.
-	4) offset=128 name=P;;
-	5) offset=160 name=Q;;
-	6) offset=192 name=R;;
-	7) offset=224 name=S;;
-	# This still leaves [tuTU].
-	*) echo bad unit for pty in: $i;;
-	esac
 	umask 0
-	case $class in
-	0|1|2|3|4|5|6|7)
+	class=`expr $i : 'pty\(.*\)'`
+	if [ -n "$class" -a "$class" -lt $nptyclass ]; then
+		name=$(echo $ptyclassnames |
+		       dd bs=1 skip=$class count=1 2>/dev/null)
+		offset=$(($class * $nptyinstance))
 		i=0
-		while [ $i -lt 32 ]; do
+		while [ $i -lt $nptyinstance ]; do
 #			This was an awk substr() before.
-			c=$(echo 0123456789abcdefghijklmnopqrstuv |
+			c=$(echo $ptyinstancenames |
 			    dd bs=1 skip=$i count=1 2>/dev/null)
-			mknod tty$name$c c 5 $(($offset + $i))
-			mknod pty$name$c c 6 $(($offset + $i))
+			minor=$(ptyminor $offset $i)
+			rm -f tty$name$c pty$name$c
+			mknod tty$name$c c 5 $minor
+			mknod pty$name$c c 6 $minor
 			i=$(($i + 1))
 		done
-		;;
-	esac
+	else
+		echo bad unit for pty in: $i
+	fi
 	umask 77
 	;;
 
Cyrille.
--
home: mailto:clefevre@poboxes.com   UNIX is user-friendly; it's just particular
work: mailto:Cyrille.Lefevre@edf.fr   about who it chooses to be friends with.
Comment 3 K. Macy freebsd_committer freebsd_triage 2007-11-16 08:48:53 UTC
State Changed
From-To: open->analyzed


I believe jhb has recently committed something to this effect. If all 
are satisfied he should close this. 


Comment 4 K. Macy freebsd_committer freebsd_triage 2007-11-16 08:48:53 UTC
Responsible Changed
From-To: freebsd-bugs->jhb


I believe jhb has recently committed something to this effect. If all  
are satisfied he should close this.
Comment 5 John Baldwin freebsd_committer freebsd_triage 2008-01-15 20:26:32 UTC
State Changed
From-To: analyzed->closed

6.x and later now allow for 512 ptys.  More pty's can be added by simply 
expanding the 'names' array in sys/kern/tty_pty.c and the corresponding 
constant in src/lib/libc/stdlib/grantpt.c.