Subject: O_ASYNC on ttys
To: None <tech-kern@netbsd.org>
From: der Mouse <mouse@Rodents.Montreal.QC.CA>
List: tech-kern
Date: 07/07/2001 05:38:32
I have a program that wants to be woken up at 240Hz.  Since I'm doing
this on a SPARC, whose clock interrupts at only 100Hz, I connected a
loopback connector to one of the serial lines, set it to 2400bps, and
then started dumping characters at it.  Of course, they came back at
240Hz, just as I wanted.

But then I had the problem of turning these received characters into
useful interrupts.  I really wanted them to be signals.  So I went to
set the tty line's process group to mine and fcntl F_SETFL to turn on
O_ASYNC.

Then I ran into a limitation: it seems that TIOCSPGRP refuses to
function unless the tty is the process's controlling tty.  Presumably
this is to stop people from changing the process group on other
people's ttys, but it does make it impossible for even semi-ordinary
programs to use O_ASYNC on random serial lines - and you can't just
make the tty your control tty briefly, because (a) you can't do that at
all unless you're a session leader and (b) if you're a session leader,
you then have trouble going back to the old control tty (to get
keyboard signals) without yanking it away from the session it belongs
to.

I couldn't see any harm in permitting TIOCSPGRP on ttys that aren't
your control tty, provided they aren't anyone else's either, so I
simply disabled the control-tty test for ttys that don't belong to a
session:

diff -c -r src/sys/kern/tty.c src+/sys/kern/tty.c
*** src/sys/kern/tty.c	Sat Jul  7 03:18:52 2001
--- src+/sys/kern/tty.c	Sat Jul  7 05:19:09 2001
***************
*** 0 ****
--- 1,11 ----
+ --- OLD/sys/kern/tty.c	Thu Jan  1 00:00:00 1970
+ +++ NEW/sys/kern/tty.c	Thu Jan  1 00:00:00 1970
+ @@ -987,7 +987,7 @@
+  	case TIOCSPGRP: {		/* set pgrp of tty */
+  		register struct pgrp *pgrp = pgfind(*(int *)data);
+  
+ -		if (!isctty(p, tp))
+ +		if (tp->t_session && !isctty(p, tp))
+  			return (ENOTTY);
+  		else if (pgrp == NULL)
+  			return (EINVAL);

Now, something equivalent may have already been done; I don't have a
-current machine set up.  But if not, well, if I've missed something,
I'd appreciate someone indicating what; if not, I offer this for
consideration.

/~\ The ASCII				der Mouse
\ / Ribbon Campaign
 X  Against HTML	       mouse@rodents.montreal.qc.ca
/ \ Email!	     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B