Subject: unexpected behaviour of grantpt(3)
To: None <tech-kern@NetBSD.org>
From: Klaus Heinz <k.heinz.feb.sechs@onlinehome.de>
List: tech-kern
Date: 02/18/2006 15:57:26
Hi,

today I was trying to understand why devel/p5-IO-Tty does not work correctly
on my new NetBSD 3.0 system.

The supplied test suite uses openpty(3) to get FDs for master and slave tty,
then applies grantpt(), unlockpt() and ptsname() to the master FD.

Using the master FD works but Perl is not able to access the slave FD,
the error I see is EBADF. Reading through the man pages for those functions
and comparing them to their counterpart on Linux (where this all works
with openpt()) pointed me to the NOTES section of our grantpt(3):

  NOTES
     Setting the group to tty and revoking accesses by other file descriptors
     are NetBSD extensions.  Calling grantpt() is equivalent to:
		ioctl(fildes, TIOCGRANTPT, 0);

If I understand this correctly, doing grantpt() after openpty()
invalidates the FD for the slave we received from openpty().

Consequently, acquiring a new slave FD (after grantpt()) works perfectly
for this Perl module.

Can anyone (Christos?) explain why NetBSD's grantpt() (or rather
TIOCGRANTPT) implements this extension?
Are there any other systems which act the same?

I could not find any previous discussion about this.

After my research, I think the IO-Tty authors could not possibly have
expected such behaviour in their code.

ciao
     Klaus