[comp.unix.questions] Which pseudo-tty's are in use now?

jerryp@tekgen.TEK.COM (Jerry Peek) (09/12/87)

I've written a program that monitors usage of the ports on our VAX 780 (4.3BSD).
It warns us when most of the available ports -- dataswitch and network
(rlogin, telnet) -- are in use.  (In other words, it warns us when people won't
be able to log in on a certain type of dataswitch or network port.)

To do that, it reads through /etc/utmp, and compares the list of current users
to the list of available ports in /etc/ttys.

The program works great for standard terminals, but it isn't accurate for
pseudo-terminals.  That's because programs like script(1) use pseudo-tty's,
but they don't make entries in /etc/utmp.

How can I find *all* the pty's in use now, not just the ones listed in the
utmp file?

The "Advanced 4.3BSD IPC Tutorial" writeup (in the Berkeley manuals) shows
some code that starts at the first pty and tries to open(2) each one
until the open succeeds:

	...

	for (i = 0; i < 16; i++) {
		line[sizeof("/dev/ptyp")-1] = "0123456789abcdef"[i];
		master = open(line, O_RDWR);
		if (master > 0) {
			gotpty = YES;
			break;
		}
	}
	if (gotpty == NO)
		<complain that all network ports are in use>;

I'm not much of an expert on this (obviously!), but this method makes me say
"ecccch".  Does anyone know of a more "elegant" way to find out which pty's
are in use, without trying to open them all first?  Thanks.

--Jerry Peek
uucp-style:   {allegra,decvax,hplabs,ihnp4,ucbvax}!tektronix!tekgen!jerryp
Domain-style: jerryp@tekgen.TEK.COM
Phone:        +1 315 445-9379