Home | History | Annotate | Line # | Download | only in kern
tty_pty.c revision 1.40
      1 /*	$NetBSD: tty_pty.c,v 1.40 1998/03/21 04:02:47 mycroft Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)tty_pty.c	8.4 (Berkeley) 2/20/95
     36  */
     37 
     38 /*
     39  * Pseudo-teletype Driver
     40  * (Actually two drivers, requiring two entries in 'cdevsw')
     41  */
     42 #include "pty.h"		/* XXX */
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/ioctl.h>
     47 #include <sys/proc.h>
     48 #include <sys/tty.h>
     49 #include <sys/file.h>
     50 #include <sys/uio.h>
     51 #include <sys/kernel.h>
     52 #include <sys/vnode.h>
     53 #include <sys/signalvar.h>
     54 #include <sys/uio.h>
     55 #include <sys/conf.h>
     56 #include <sys/poll.h>
     57 
     58 
     59 
     60 #if NPTY == 1
     61 #undef NPTY
     62 #define	NPTY	32		/* crude XXX */
     63 #endif
     64 
     65 /* Macros to clear/set/test flags. */
     66 #define	SET(t, f)	(t) |= (f)
     67 #define	CLR(t, f)	(t) &= ~((unsigned)(f))
     68 #define	ISSET(t, f)	((t) & (f))
     69 
     70 #define BUFSIZ 100		/* Chunk size iomoved to/from user */
     71 
     72 /*
     73  * pts == /dev/tty[pqrs]?
     74  * ptc == /dev/pty[pqrs]?
     75  */
     76 struct	pt_softc {
     77 	struct	tty *pt_tty;
     78 	int	pt_flags;
     79 	struct	selinfo pt_selr, pt_selw;
     80 	u_char	pt_send;
     81 	u_char	pt_ucntl;
     82 } pt_softc[NPTY];		/* XXX */
     83 int	npty = NPTY;		/* for pstat -t */
     84 
     85 #define	PF_PKT		0x08		/* packet mode */
     86 #define	PF_STOPPED	0x10		/* user told stopped */
     87 #define	PF_REMOTE	0x20		/* remote and flow controlled input */
     88 #define	PF_NOSTOP	0x40
     89 #define PF_UCNTL	0x80		/* user control mode */
     90 
     91 void	ptyattach __P((int));
     92 void	ptcwakeup __P((struct tty *, int));
     93 struct tty *ptytty __P((dev_t));
     94 void	ptsstart __P((struct tty *));
     95 
     96 /*
     97  * Establish n (or default if n is 1) ptys in the system.
     98  */
     99 void
    100 ptyattach(n)
    101 	int n;
    102 {
    103 #ifdef notyet
    104 #define	DEFAULT_NPTY	32
    105 
    106 	/* maybe should allow 0 => none? */
    107 	if (n <= 1)
    108 		n = DEFAULT_NPTY;
    109 	pt_softc = malloc(n * sizeof(struct pt_softc), M_DEVBUF, M_WAITOK);
    110 	npty = n;
    111 #endif
    112 }
    113 
    114 /*ARGSUSED*/
    115 int
    116 ptsopen(dev, flag, devtype, p)
    117 	dev_t dev;
    118 	int flag, devtype;
    119 	struct proc *p;
    120 {
    121 	struct pt_softc *pti;
    122 	register struct tty *tp;
    123 	int error;
    124 
    125 	if (minor(dev) >= npty)
    126 		return (ENXIO);
    127 	pti = &pt_softc[minor(dev)];
    128 	if (!pti->pt_tty) {
    129 		tp = pti->pt_tty = ttymalloc();
    130 		tty_attach(tp);
    131 	}
    132 	else
    133 		tp = pti->pt_tty;
    134 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
    135 		ttychars(tp);		/* Set up default chars */
    136 		tp->t_iflag = TTYDEF_IFLAG;
    137 		tp->t_oflag = TTYDEF_OFLAG;
    138 		tp->t_lflag = TTYDEF_LFLAG;
    139 		tp->t_cflag = TTYDEF_CFLAG;
    140 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    141 		ttsetwater(tp);		/* would be done in xxparam() */
    142 	} else if (ISSET(tp->t_state, TS_XCLUDE) && p->p_ucred->cr_uid != 0)
    143 		return (EBUSY);
    144 	if (tp->t_oproc)			/* Ctrlr still around. */
    145 		SET(tp->t_state, TS_CARR_ON);
    146 	if (!ISSET(flag, O_NONBLOCK))
    147 		while (!ISSET(tp->t_state, TS_CARR_ON)) {
    148 			tp->t_wopen++;
    149 			error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
    150 			    ttopen, 0);
    151 			tp->t_wopen--;
    152 			if (error)
    153 				return (error);
    154 		}
    155 	error = (*linesw[tp->t_line].l_open)(dev, tp);
    156 	ptcwakeup(tp, FREAD|FWRITE);
    157 	return (error);
    158 }
    159 
    160 int
    161 ptsclose(dev, flag, mode, p)
    162 	dev_t dev;
    163 	int flag, mode;
    164 	struct proc *p;
    165 {
    166 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    167 	register struct tty *tp = pti->pt_tty;
    168 	int error;
    169 
    170 	error = (*linesw[tp->t_line].l_close)(tp, flag);
    171 	error |= ttyclose(tp);
    172 	ptcwakeup(tp, FREAD|FWRITE);
    173 	return (error);
    174 }
    175 
    176 int
    177 ptsread(dev, uio, flag)
    178 	dev_t dev;
    179 	struct uio *uio;
    180 	int flag;
    181 {
    182 	struct proc *p = curproc;
    183 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    184 	register struct tty *tp = pti->pt_tty;
    185 	int error = 0;
    186 
    187 again:
    188 	if (pti->pt_flags & PF_REMOTE) {
    189 		while (isbackground(p, tp)) {
    190 			if ((p->p_sigignore & sigmask(SIGTTIN)) ||
    191 			    (p->p_sigmask & sigmask(SIGTTIN)) ||
    192 			    p->p_pgrp->pg_jobc == 0 ||
    193 			    p->p_flag & P_PPWAIT)
    194 				return (EIO);
    195 			pgsignal(p->p_pgrp, SIGTTIN, 1);
    196 			error = ttysleep(tp, (caddr_t)&lbolt,
    197 					 TTIPRI | PCATCH, ttybg, 0);
    198 			if (error)
    199 				return (error);
    200 		}
    201 		if (tp->t_canq.c_cc == 0) {
    202 			if (flag & IO_NDELAY)
    203 				return (EWOULDBLOCK);
    204 			error = ttysleep(tp, (caddr_t)&tp->t_canq,
    205 					 TTIPRI | PCATCH, ttyin, 0);
    206 			if (error)
    207 				return (error);
    208 			goto again;
    209 		}
    210 		while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
    211 			if (ureadc(getc(&tp->t_canq), uio) < 0) {
    212 				error = EFAULT;
    213 				break;
    214 			}
    215 		if (tp->t_canq.c_cc == 1)
    216 			(void) getc(&tp->t_canq);
    217 		if (tp->t_canq.c_cc)
    218 			return (error);
    219 	} else
    220 		if (tp->t_oproc)
    221 			error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
    222 	ptcwakeup(tp, FWRITE);
    223 	return (error);
    224 }
    225 
    226 /*
    227  * Write to pseudo-tty.
    228  * Wakeups of controlling tty will happen
    229  * indirectly, when tty driver calls ptsstart.
    230  */
    231 int
    232 ptswrite(dev, uio, flag)
    233 	dev_t dev;
    234 	struct uio *uio;
    235 	int flag;
    236 {
    237 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    238 	register struct tty *tp = pti->pt_tty;
    239 
    240 	if (tp->t_oproc == 0)
    241 		return (EIO);
    242 	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
    243 }
    244 
    245 /*
    246  * Start output on pseudo-tty.
    247  * Wake up process polling or sleeping for input from controlling tty.
    248  */
    249 void
    250 ptsstart(tp)
    251 	struct tty *tp;
    252 {
    253 	register struct pt_softc *pti = &pt_softc[minor(tp->t_dev)];
    254 
    255 	if (ISSET(tp->t_state, TS_TTSTOP))
    256 		return;
    257 	if (pti->pt_flags & PF_STOPPED) {
    258 		pti->pt_flags &= ~PF_STOPPED;
    259 		pti->pt_send = TIOCPKT_START;
    260 	}
    261 	ptcwakeup(tp, FREAD);
    262 }
    263 
    264 void
    265 ptsstop(tp, flush)
    266 	register struct tty *tp;
    267 	int flush;
    268 {
    269 	struct pt_softc *pti = &pt_softc[minor(tp->t_dev)];
    270 	int flag;
    271 
    272 	/* note: FLUSHREAD and FLUSHWRITE already ok */
    273 	if (flush == 0) {
    274 		flush = TIOCPKT_STOP;
    275 		pti->pt_flags |= PF_STOPPED;
    276 	} else
    277 		pti->pt_flags &= ~PF_STOPPED;
    278 	pti->pt_send |= flush;
    279 	/* change of perspective */
    280 	flag = 0;
    281 	if (flush & FREAD)
    282 		flag |= FWRITE;
    283 	if (flush & FWRITE)
    284 		flag |= FREAD;
    285 	ptcwakeup(tp, flag);
    286 }
    287 
    288 void
    289 ptcwakeup(tp, flag)
    290 	struct tty *tp;
    291 	int flag;
    292 {
    293 	struct pt_softc *pti = &pt_softc[minor(tp->t_dev)];
    294 
    295 	if (flag & FREAD) {
    296 		selwakeup(&pti->pt_selr);
    297 		wakeup((caddr_t)&tp->t_outq.c_cf);
    298 	}
    299 	if (flag & FWRITE) {
    300 		selwakeup(&pti->pt_selw);
    301 		wakeup((caddr_t)&tp->t_rawq.c_cf);
    302 	}
    303 }
    304 
    305 int ptcopen __P((dev_t, int, int, struct proc *));
    306 
    307 /*ARGSUSED*/
    308 int
    309 ptcopen(dev, flag, devtype, p)
    310 	dev_t dev;
    311 	int flag, devtype;
    312 	struct proc *p;
    313 {
    314 	struct pt_softc *pti;
    315 	register struct tty *tp;
    316 
    317 	if (minor(dev) >= npty)
    318 		return (ENXIO);
    319 	pti = &pt_softc[minor(dev)];
    320 	if (!pti->pt_tty) {
    321 		tp = pti->pt_tty = ttymalloc();
    322 		tty_attach(tp);
    323 	}
    324 	else
    325 		tp = pti->pt_tty;
    326 	if (tp->t_oproc)
    327 		return (EIO);
    328 	tp->t_oproc = ptsstart;
    329 	(void)(*linesw[tp->t_line].l_modem)(tp, 1);
    330 	CLR(tp->t_lflag, EXTPROC);
    331 	pti->pt_flags = 0;
    332 	pti->pt_send = 0;
    333 	pti->pt_ucntl = 0;
    334 	return (0);
    335 }
    336 
    337 /*ARGSUSED*/
    338 int
    339 ptcclose(dev, flag, devtype, p)
    340 	dev_t dev;
    341 	int flag, devtype;
    342 	struct proc *p;
    343 {
    344 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    345 	register struct tty *tp = pti->pt_tty;
    346 
    347 	(void)(*linesw[tp->t_line].l_modem)(tp, 0);
    348 	CLR(tp->t_state, TS_CARR_ON);
    349 	tp->t_oproc = 0;		/* mark closed */
    350 	return (0);
    351 }
    352 
    353 int
    354 ptcread(dev, uio, flag)
    355 	dev_t dev;
    356 	struct uio *uio;
    357 	int flag;
    358 {
    359 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    360 	register struct tty *tp = pti->pt_tty;
    361 	char buf[BUFSIZ];
    362 	int error = 0, cc;
    363 
    364 	/*
    365 	 * We want to block until the slave
    366 	 * is open, and there's something to read;
    367 	 * but if we lost the slave or we're NBIO,
    368 	 * then return the appropriate error instead.
    369 	 */
    370 	for (;;) {
    371 		if (ISSET(tp->t_state, TS_ISOPEN)) {
    372 			if (pti->pt_flags&PF_PKT && pti->pt_send) {
    373 				error = ureadc((int)pti->pt_send, uio);
    374 				if (error)
    375 					return (error);
    376 				if (pti->pt_send & TIOCPKT_IOCTL) {
    377 					cc = min(uio->uio_resid,
    378 						sizeof(tp->t_termios));
    379 					uiomove((caddr_t) &tp->t_termios,
    380 						cc, uio);
    381 				}
    382 				pti->pt_send = 0;
    383 				return (0);
    384 			}
    385 			if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
    386 				error = ureadc((int)pti->pt_ucntl, uio);
    387 				if (error)
    388 					return (error);
    389 				pti->pt_ucntl = 0;
    390 				return (0);
    391 			}
    392 			if (tp->t_outq.c_cc && !ISSET(tp->t_state, TS_TTSTOP))
    393 				break;
    394 		}
    395 		if (!ISSET(tp->t_state, TS_CARR_ON))
    396 			return (0);	/* EOF */
    397 		if (flag & IO_NDELAY)
    398 			return (EWOULDBLOCK);
    399 		error = tsleep((caddr_t)&tp->t_outq.c_cf, TTIPRI | PCATCH,
    400 			       ttyin, 0);
    401 		if (error)
    402 			return (error);
    403 	}
    404 	if (pti->pt_flags & (PF_PKT|PF_UCNTL))
    405 		error = ureadc(0, uio);
    406 	while (uio->uio_resid > 0 && error == 0) {
    407 		cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
    408 		if (cc <= 0)
    409 			break;
    410 		error = uiomove(buf, cc, uio);
    411 	}
    412 	if (tp->t_outq.c_cc <= tp->t_lowat) {
    413 		if (ISSET(tp->t_state, TS_ASLEEP)) {
    414 			CLR(tp->t_state, TS_ASLEEP);
    415 			wakeup((caddr_t)&tp->t_outq);
    416 		}
    417 		selwakeup(&tp->t_wsel);
    418 	}
    419 	return (error);
    420 }
    421 
    422 
    423 int
    424 ptcwrite(dev, uio, flag)
    425 	dev_t dev;
    426 	register struct uio *uio;
    427 	int flag;
    428 {
    429 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    430 	register struct tty *tp = pti->pt_tty;
    431 	register u_char *cp = NULL;
    432 	register int cc = 0;
    433 	u_char locbuf[BUFSIZ];
    434 	int cnt = 0;
    435 	int error = 0;
    436 
    437 again:
    438 	if (!ISSET(tp->t_state, TS_ISOPEN))
    439 		goto block;
    440 	if (pti->pt_flags & PF_REMOTE) {
    441 		if (tp->t_canq.c_cc)
    442 			goto block;
    443 		while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) {
    444 			if (cc == 0) {
    445 				cc = min(uio->uio_resid, BUFSIZ);
    446 				cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
    447 				cp = locbuf;
    448 				error = uiomove((caddr_t)cp, cc, uio);
    449 				if (error)
    450 					return (error);
    451 				/* check again for safety */
    452 				if (!ISSET(tp->t_state, TS_ISOPEN))
    453 					return (EIO);
    454 			}
    455 			if (cc)
    456 				(void) b_to_q((char *)cp, cc, &tp->t_canq);
    457 			cc = 0;
    458 		}
    459 		(void) putc(0, &tp->t_canq);
    460 		ttwakeup(tp);
    461 		wakeup((caddr_t)&tp->t_canq);
    462 		return (0);
    463 	}
    464 	while (uio->uio_resid > 0) {
    465 		if (cc == 0) {
    466 			cc = min(uio->uio_resid, BUFSIZ);
    467 			cp = locbuf;
    468 			error = uiomove((caddr_t)cp, cc, uio);
    469 			if (error)
    470 				return (error);
    471 			/* check again for safety */
    472 			if (!ISSET(tp->t_state, TS_ISOPEN))
    473 				return (EIO);
    474 		}
    475 		while (cc > 0) {
    476 			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
    477 			   (tp->t_canq.c_cc > 0 || !ISSET(tp->t_iflag, ICANON))) {
    478 				wakeup((caddr_t)&tp->t_rawq);
    479 				goto block;
    480 			}
    481 			(*linesw[tp->t_line].l_rint)(*cp++, tp);
    482 			cnt++;
    483 			cc--;
    484 		}
    485 		cc = 0;
    486 	}
    487 	return (0);
    488 block:
    489 	/*
    490 	 * Come here to wait for slave to open, for space
    491 	 * in outq, or space in rawq.
    492 	 */
    493 	if (!ISSET(tp->t_state, TS_CARR_ON))
    494 		return (EIO);
    495 	if (flag & IO_NDELAY) {
    496 		/* adjust for data copied in but not written */
    497 		uio->uio_resid += cc;
    498 		if (cnt == 0)
    499 			return (EWOULDBLOCK);
    500 		return (0);
    501 	}
    502 	error = tsleep((caddr_t)&tp->t_rawq.c_cf, TTOPRI | PCATCH,
    503 		       ttyout, 0);
    504 	if (error) {
    505 		/* adjust for data copied in but not written */
    506 		uio->uio_resid += cc;
    507 		return (error);
    508 	}
    509 	goto again;
    510 }
    511 
    512 int
    513 ptcpoll(dev, events, p)
    514 	dev_t dev;
    515 	int events;
    516 	struct proc *p;
    517 {
    518 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    519 	register struct tty *tp = pti->pt_tty;
    520 	int revents = 0;
    521 	int s = splsoftclock();
    522 
    523 	if (events & (POLLIN | POLLRDNORM))
    524 		if (ISSET(tp->t_state, TS_ISOPEN) &&
    525 		    ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
    526 		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
    527 		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
    528 			revents |= events & (POLLIN | POLLRDNORM);
    529 
    530 	if (events & (POLLOUT | POLLWRNORM))
    531 		if (ISSET(tp->t_state, TS_ISOPEN) &&
    532 		    ((pti->pt_flags & PF_REMOTE) ?
    533 		     (tp->t_canq.c_cc == 0) :
    534 		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
    535 		      (tp->t_canq.c_cc == 0 && ISSET(tp->t_iflag, ICANON)))))
    536 			revents |= events & (POLLOUT | POLLWRNORM);
    537 
    538 	if (events & POLLHUP)
    539 		if (!ISSET(tp->t_state, TS_CARR_ON))
    540 			revents |= POLLHUP;
    541 
    542 	if (revents == 0) {
    543 		if (events & (POLLIN | POLLHUP | POLLRDNORM))
    544 			selrecord(p, &pti->pt_selr);
    545 
    546 		if (events & (POLLOUT | POLLWRNORM))
    547 			selrecord(p, &pti->pt_selw);
    548 	}
    549 
    550 	splx(s);
    551 	return (revents);
    552 }
    553 
    554 
    555 struct tty *
    556 ptytty(dev)
    557 	dev_t dev;
    558 {
    559 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    560 	register struct tty *tp = pti->pt_tty;
    561 
    562 	return (tp);
    563 }
    564 
    565 /*ARGSUSED*/
    566 int
    567 ptyioctl(dev, cmd, data, flag, p)
    568 	dev_t dev;
    569 	u_long cmd;
    570 	caddr_t data;
    571 	int flag;
    572 	struct proc *p;
    573 {
    574 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    575 	register struct tty *tp = pti->pt_tty;
    576 	register u_char *cc = tp->t_cc;
    577 	int stop, error;
    578 
    579 	/*
    580 	 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
    581 	 * ttywflush(tp) will hang if there are characters in the outq.
    582 	 */
    583 	if (cmd == TIOCEXT) {
    584 		/*
    585 		 * When the EXTPROC bit is being toggled, we need
    586 		 * to send an TIOCPKT_IOCTL if the packet driver
    587 		 * is turned on.
    588 		 */
    589 		if (*(int *)data) {
    590 			if (pti->pt_flags & PF_PKT) {
    591 				pti->pt_send |= TIOCPKT_IOCTL;
    592 				ptcwakeup(tp, FREAD);
    593 			}
    594 			SET(tp->t_lflag, EXTPROC);
    595 		} else {
    596 			if (ISSET(tp->t_lflag, EXTPROC) &&
    597 			    (pti->pt_flags & PF_PKT)) {
    598 				pti->pt_send |= TIOCPKT_IOCTL;
    599 				ptcwakeup(tp, FREAD);
    600 			}
    601 			CLR(tp->t_lflag, EXTPROC);
    602 		}
    603 		return(0);
    604 	} else
    605 	if (cdevsw[major(dev)].d_open == ptcopen)
    606 		switch (cmd) {
    607 
    608 		case TIOCGPGRP:
    609 #ifdef COMPAT_SUNOS
    610 			{
    611 			/*
    612 			 * I'm not sure about SunOS TIOCGPGRP semantics
    613 			 * on PTYs, but it's something like this:
    614 			 */
    615 			extern struct emul emul_sunos;
    616 			if (p->p_emul == &emul_sunos && tp->t_pgrp == 0)
    617 				return (EIO);
    618 			*(int *)data = tp->t_pgrp->pg_id;
    619 			return (0);
    620 			}
    621 #endif
    622 			/*
    623 			 * We avoid calling ttioctl on the controller since,
    624 			 * in that case, tp must be the controlling terminal.
    625 			 */
    626 			*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
    627 			return (0);
    628 
    629 		case TIOCPKT:
    630 			if (*(int *)data) {
    631 				if (pti->pt_flags & PF_UCNTL)
    632 					return (EINVAL);
    633 				pti->pt_flags |= PF_PKT;
    634 			} else
    635 				pti->pt_flags &= ~PF_PKT;
    636 			return (0);
    637 
    638 		case TIOCUCNTL:
    639 			if (*(int *)data) {
    640 				if (pti->pt_flags & PF_PKT)
    641 					return (EINVAL);
    642 				pti->pt_flags |= PF_UCNTL;
    643 			} else
    644 				pti->pt_flags &= ~PF_UCNTL;
    645 			return (0);
    646 
    647 		case TIOCREMOTE:
    648 			if (*(int *)data)
    649 				pti->pt_flags |= PF_REMOTE;
    650 			else
    651 				pti->pt_flags &= ~PF_REMOTE;
    652 			ttyflush(tp, FREAD|FWRITE);
    653 			return (0);
    654 
    655 #ifdef COMPAT_OLDTTY
    656 		case TIOCSETP:
    657 		case TIOCSETN:
    658 #endif
    659 		case TIOCSETD:
    660 		case TIOCSETA:
    661 		case TIOCSETAW:
    662 		case TIOCSETAF:
    663 			ndflush(&tp->t_outq, tp->t_outq.c_cc);
    664 			break;
    665 
    666 		case TIOCSIG:
    667 			if (*(unsigned int *)data >= NSIG)
    668 				return(EINVAL);
    669 			if (!ISSET(tp->t_lflag, NOFLSH))
    670 				ttyflush(tp, FREAD|FWRITE);
    671 			pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
    672 			if ((*(unsigned int *)data == SIGINFO) &&
    673 			    (!ISSET(tp->t_lflag, NOKERNINFO)))
    674 				ttyinfo(tp);
    675 			return(0);
    676 		}
    677 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
    678 	if (error < 0)
    679 		 error = ttioctl(tp, cmd, data, flag, p);
    680 	if (error < 0) {
    681 		if (pti->pt_flags & PF_UCNTL &&
    682 		    (cmd & ~0xff) == UIOCCMD(0)) {
    683 			if (cmd & 0xff) {
    684 				pti->pt_ucntl = (u_char)cmd;
    685 				ptcwakeup(tp, FREAD);
    686 			}
    687 			return (0);
    688 		}
    689 		error = ENOTTY;
    690 	}
    691 	/*
    692 	 * If external processing and packet mode send ioctl packet.
    693 	 */
    694 	if (ISSET(tp->t_lflag, EXTPROC) && (pti->pt_flags & PF_PKT)) {
    695 		switch(cmd) {
    696 		case TIOCSETA:
    697 		case TIOCSETAW:
    698 		case TIOCSETAF:
    699 #ifdef COMPAT_OLDTTY
    700 		case TIOCSETP:
    701 		case TIOCSETN:
    702 		case TIOCSETC:
    703 		case TIOCSLTC:
    704 		case TIOCLBIS:
    705 		case TIOCLBIC:
    706 		case TIOCLSET:
    707 #endif
    708 			pti->pt_send |= TIOCPKT_IOCTL;
    709 			ptcwakeup(tp, FREAD);
    710 		default:
    711 			break;
    712 		}
    713 	}
    714 	stop = ISSET(tp->t_iflag, IXON) && CCEQ(cc[VSTOP], CTRL('s'))
    715 		&& CCEQ(cc[VSTART], CTRL('q'));
    716 	if (pti->pt_flags & PF_NOSTOP) {
    717 		if (stop) {
    718 			pti->pt_send &= ~TIOCPKT_NOSTOP;
    719 			pti->pt_send |= TIOCPKT_DOSTOP;
    720 			pti->pt_flags &= ~PF_NOSTOP;
    721 			ptcwakeup(tp, FREAD);
    722 		}
    723 	} else {
    724 		if (!stop) {
    725 			pti->pt_send &= ~TIOCPKT_DOSTOP;
    726 			pti->pt_send |= TIOCPKT_NOSTOP;
    727 			pti->pt_flags |= PF_NOSTOP;
    728 			ptcwakeup(tp, FREAD);
    729 		}
    730 	}
    731 	return (error);
    732 }
    733