Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!rutgers!sri-spam!ames!ucbcad!ucbvax!decvax!tektronix!uw-beaver!cornell!rochester!pt.cs.cmu.edu!andrew.cmu.edu!postman# From: postman#@andrew.cmu.edu.UUCP Newsgroups: comp.unix.wizards Subject: Re: how do I make a process release its terminal? Message-ID: Date: Fri, 30-Jan-87 17:46:50 EST Article-I.D.: andrew.MS.V3.20.zs01.80021e08.leola.ibm032.3110.2 Posted: Fri Jan 30 17:46:50 1987 Date-Received: Sat, 31-Jan-87 19:42:07 EST Organization: Carnegie-Mellon University Lines: 52 ReSent-Date: Fri, 30 Jan 87 17:48:43 est ReSent-From: postman#@andrew.cmu.edu ReSent-To:nntp-xmit#@andrew.cmu.edu Return-path: To: outnews#ext.nn.comp.unix.wizards@andrew.cmu.edu, m5d@bobkat.UUCP (Mike McNally (dlsh)), paul@vixie.UUCP In-Reply-To: <471@bobkat.UUCP> I had a lot of fun trying to figure out process groups and control terminals under BSD 4.2. There was this bug where I couldn't open /dev/tty in a certain process (ENXIO error). So after spending a year looking for it in my code, I got access to kernel sources. Here is what I found. The kernel's idea of a process without a control terminal is one who's process group is zero. It also has a pointer to the device, if this is NULL, you don't have a control terminal. The documentation doesn't help here, either you read the source or you lose. It says that if you have no control terminal and you open another tty, it will become your control terminal. What really happens is that if your process group is 0 and you open a tty, it becomes your control terminal. If your process group is non-zero, and that pointer is NULL, /dev/tty remains broken. Getting rid of a control terminal is pretty easy. The following code (strictly BSD 4.2) will do it: #include #include #include void NukeControlTerminal() { int fd; if ((fd = open("/dev/tty", O_RDWR)) >= 0) { ioctl(fd, TIOCNOTTY, NULL); close(fd); } else /* Insert real error handling here... */ printf("Couldn't open control terminal, errno = %d\n", errno); } My bug was caused by setting the process group and then trying to open /dev/tty. Since I really didn't have a control terminal (i.e. pointer was NULL), it returned EXNIO. But when I opened another tty, it did not become /dev/tty (because my process group was not 0). There are a number of other problems you can get into by changing the process group of a terminal (or of your process), and then trying to do an ioctl on it. For example where you wish to fork a child, and have a terminal in the child's process group but not in yours. If you do the process group switching in the wrong order, you will not be able to ioctl the terminal into the target process group... Sincerely, Zalman Stern Information Technology Center Carnegie Mellon University zs01@andrew.cmu.edu