[comp.unix.sysv386] TIOCNOTTY under SCO System V/386

stockett@jeanluc.UUCP (Jeffrey M. Stockett) (06/21/91)

I'm trying to port over some code from a BSD machine to SCO System V/386,
and I have run into a problem with the following lines:

    if ((s = open("/dev/tty", O_RDWR)) >= 0) {
	ioctl(s, TIOCNOTTY, 0);
	close(s);
    }

I have grepped for TIOCNOTTY in /usr/include/*.h and /usr/include/sys/*.h,
and it is not to be found.  Does anyone know why it is missing, and
what would be an appropriate substitute for it under SCO System V/386?
(This is the only line that causes a problem.  If I define it to some
bogus value, the rest of the code compiles fine.)

Thanks,



Jeffrey M. Stockett

Internet:	stockett@titan.tsd.arlut.utexas.edu
UUCP:		..cs.utexas.edu!ut-emx!jeanluc!stockett

tim@dell.co.uk (Tim Wright) (06/21/91)

In <54@jeanluc.UUCP> stockett@jeanluc.UUCP (Jeffrey M. Stockett) writes:

>I'm trying to port over some code from a BSD machine to SCO System V/386,
>and I have run into a problem with the following lines:

>    if ((s = open("/dev/tty", O_RDWR)) >= 0) {
>	ioctl(s, TIOCNOTTY, 0);
>	close(s);
>    }

>I have grepped for TIOCNOTTY in /usr/include/*.h and /usr/include/sys/*.h,
>and it is not to be found.  Does anyone know why it is missing, and
>what would be an appropriate substitute for it under SCO System V/386?
>(This is the only line that causes a problem.  If I define it to some
>bogus value, the rest of the code compiles fine.)

Yup,
under most System V's it translates simply to
setpgrp();
If you're POSIX compatible, you may prefer to use
setsid();

Tim
-- 
Tim Wright, Dell Computer Corp., Bracknell    |  Domain: tim@dell.co.uk
Berkshire, UK, RG12 1RW. Tel: +44-344-860456  |  Uucp: ...!ukc!delluk!tim
Smoke me a Kipper, I'll be back for breakfast - Red Dwarf

chip@chinacat.unicom.com (Chip Rosenthal) (06/22/91)

In article <54@jeanluc.UUCP> stockett@jeanluc.UUCP (Jeffrey M. Stockett) writes:
>I'm trying to port over some code from a BSD machine to SCO System V/386,
>and I have run into a problem with [TIOCNOTTY]

Following code is from Richard Steven's daemon_start() routine.  This
is all explained in his book `UNIX Network Programming'.  It's a good
book and Richard is a great guy - I think y'all should buy two or three
copies.  Sources are on UUNET in (I think) ~/networking/netprog.tar.Z.

	/*
	 * First child process.
	 *
	 * Disassociate from controlling terminal and process group.
	 * Ensure the process can't reacquire a new controlling terminal.
	 */

#ifdef	SIGTSTP		/* BSD */

	if (setpgrp(0, getpid()) == -1)
		err_sys("can't change process group");

	if ( (fd = open("/dev/tty", O_RDWR)) >= 0) {
		ioctl(fd, TIOCNOTTY, (char *)NULL); /* lose controlling tty */
		close(fd);
	}

#else	/* System V */

	if (setpgrp() == -1)
		err_sys("can't change process group");

	signal(SIGHUP, SIG_IGN);	/* immune from pgrp leader death */

	if ( (childpid = fork()) < 0)
		err_sys("can't fork second child");
	else if (childpid > 0)
		exit(0);	/* first child */

	/* second child */
#endif

-- 
Chip Rosenthal  512-482-8260  |  Closed user interfaces for open systems?
Unicom Systems Development    |  No, thank you.
<chip@chinacat.Unicom.COM>    |  Boycott Lotus Development Corp.

nerd@percival.rain.com (Michael Galassi) (06/22/91)

stockett@jeanluc.UUCP (Jeffrey M. Stockett) writes:

>I'm trying to port over some code from a BSD machine to SCO System V/386,
>and I have run into a problem with the following lines:

>    if ((s = open("/dev/tty", O_RDWR)) >= 0) {
>	ioctl(s, TIOCNOTTY, 0);
>	close(s);
>    }

Stab in the dark, looks like the debug code in rlogind.  If this is
the case, #undef DEBUG, the TIOCNOTTY is not a good thing to have
occur to the controlling terminal of an interactive session.

If I'm wrong, here is the code to replace the above fragment:

	(void) setpgrp();

This will dissociate you from the controlling terminal and will make
the next terminal you open the controlling terminal.  There are other
side effects to this call, read the man page and know how these may
affect you.

FYI, this is the relevant section from the BSD man page (tty(4)).

    A process can remove the association it has with its con-
    trolling terminal by opening the file /dev/tty and issuing
    an

         ioctl(f, TIOCNOTTY, 0);

And, here is the relevant section from the Xenix man page (setpgrp(S))

    There are many ramifications to be considered before
    invoking setpgrp. When a process is made a process group
    leader with setpgrp, the terminal that controlled the
    process that issued the setpgrp statement is lost as the
    controlling terminal for the new process group.  The new
    process group takes as its controlling terminal the next
    terminal it opens that is not already open.

I assume that the functionality of setpgrp is identical in sco xenix
and sco unix.

Cheers,
-m
-- 
Michael Galassi				| nerd@percival.rain.com
MS-DOS:  The ultimate PC virus.		| ...!tektronix!percy!nerd

bill@ssbn.WLK.COM (Bill Kennedy) (06/22/91)

> stockett@jeanluc.UUCP (Jeffrey M. Stockett) writes:
>>I'm trying to port over some code from a BSD machine to SCO System V/386,
>>and I have run into a problem with [TIOCNOTTY]
chip@chinacat.unicom.com (Chip Rosenthal) follows up:
>
>Following code is from Richard Steven's daemon_start() routine.  This
>is all explained in his book `UNIX Network Programming'.  It's a good
>book and Richard is a great guy - I think y'all should buy two or three
>copies.  Sources are on UUNET in (I think) ~/networking/netprog.tar.Z.

I second the motion regarding the book.  It has been invaluable as a
source of examples in various techniques and I have used the material
from netprog.tar.Z.  Buy the book, work with the programs.  You'll know
a lot more about this stuff than you thought you wanted to.

In the daemon_start example there is a subtle trap.  Back in the days of
the kerosene ethernet you could decide, fairly easily, what OS you were
running with a simple #ifdef such as in the example below.  My favorite
for distinguishing between System V and BSD was #ifdef FIONREAD.  That
is no longer enough.  The cross fertilization between the OS' is such that
if you're using Interactive TCP/IP FIONREAD is, indeed, defined.  The
example that Chip posted contains another such puddle.
[ ... example code deleted except for what I'm talking about ... ]

>#ifdef	SIGTSTP		/* BSD */

Sorry, not any more, since Interactive implemented job control you've got
it.  Maybe there's something else that's still BSD unique but not SIGSTP.
[ ... ]

>#else	/* System V */

Possibly equally lethal if the #ifdef isn't 100% kosher.  Richard says clearly
that his examples are for Berkeley and System V and makes no claim to work
with anything else but this is going to break in a hybridized OS.  How come I
know that?  :-)

My point is not to criticize the article or the example but rather to warn
that the technique may be flawed unless your using the same OS' that the
author did.  Since it's all sample and experimental code I just deleted those
pieces that didn't apply in my environment (ISC 3.2.2 with their TCP/IP) and
ditched the #ifdef's that sent me down the wrong chute.
-- 
Bill Kennedy  internet  bill@ssbn.WLK.COM or ssbn!bill@attmail.COM
              uucp      {att,cs.utexas.edu,pyramid!daver}!ssbn.wlk.com!bill