Home | History | Annotate | Line # | Download | only in kern
tty_pty.c revision 1.38
      1 /*	$NetBSD: tty_pty.c,v 1.38 1996/09/07 12:41:03 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.2 (Berkeley) 9/23/93
     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 		SET(tp->t_state, TS_WOPEN);
    136 		ttychars(tp);		/* Set up default chars */
    137 		tp->t_iflag = TTYDEF_IFLAG;
    138 		tp->t_oflag = TTYDEF_OFLAG;
    139 		tp->t_lflag = TTYDEF_LFLAG;
    140 		tp->t_cflag = TTYDEF_CFLAG;
    141 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    142 		ttsetwater(tp);		/* would be done in xxparam() */
    143 	} else if (ISSET(tp->t_state, TS_XCLUDE) && p->p_ucred->cr_uid != 0)
    144 		return (EBUSY);
    145 	if (tp->t_oproc)			/* Ctrlr still around. */
    146 		SET(tp->t_state, TS_CARR_ON);
    147 	while (!ISSET(tp->t_state, TS_CARR_ON)) {
    148 		SET(tp->t_state, TS_WOPEN);
    149 		if (flag&FNONBLOCK)
    150 			break;
    151 		error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH,
    152 				 ttopen, 0);
    153 		if (error)
    154 			return (error);
    155 	}
    156 	error = (*linesw[tp->t_line].l_open)(dev, tp);
    157 	ptcwakeup(tp, FREAD|FWRITE);
    158 	return (error);
    159 }
    160 
    161 int
    162 ptsclose(dev, flag, mode, p)
    163 	dev_t dev;
    164 	int flag, mode;
    165 	struct proc *p;
    166 {
    167 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    168 	register struct tty *tp = pti->pt_tty;
    169 	int error;
    170 
    171 	error = (*linesw[tp->t_line].l_close)(tp, flag);
    172 	error |= ttyclose(tp);
    173 	ptcwakeup(tp, FREAD|FWRITE);
    174 	return (error);
    175 }
    176 
    177 int
    178 ptsread(dev, uio, flag)
    179 	dev_t dev;
    180 	struct uio *uio;
    181 	int flag;
    182 {
    183 	struct proc *p = curproc;
    184 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    185 	register struct tty *tp = pti->pt_tty;
    186 	int error = 0;
    187 
    188 again:
    189 	if (pti->pt_flags & PF_REMOTE) {
    190 		while (isbackground(p, tp)) {
    191 			if ((p->p_sigignore & sigmask(SIGTTIN)) ||
    192 			    (p->p_sigmask & sigmask(SIGTTIN)) ||
    193 			    p->p_pgrp->pg_jobc == 0 ||
    194 			    p->p_flag & P_PPWAIT)
    195 				return (EIO);
    196 			pgsignal(p->p_pgrp, SIGTTIN, 1);
    197 			error = ttysleep(tp, (caddr_t)&lbolt,
    198 					 TTIPRI | PCATCH, ttybg, 0);
    199 			if (error)
    200 				return (error);
    201 		}
    202 		if (tp->t_canq.c_cc == 0) {
    203 			if (flag & IO_NDELAY)
    204 				return (EWOULDBLOCK);
    205 			error = ttysleep(tp, (caddr_t)&tp->t_canq,
    206 					 TTIPRI | PCATCH, ttyin, 0);
    207 			if (error)
    208 				return (error);
    209 			goto again;
    210 		}
    211 		while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
    212 			if (ureadc(getc(&tp->t_canq), uio) < 0) {
    213 				error = EFAULT;
    214 				break;
    215 			}
    216 		if (tp->t_canq.c_cc == 1)
    217 			(void) getc(&tp->t_canq);
    218 		if (tp->t_canq.c_cc)
    219 			return (error);
    220 	} else
    221 		if (tp->t_oproc)
    222 			error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
    223 	ptcwakeup(tp, FWRITE);
    224 	return (error);
    225 }
    226 
    227 /*
    228  * Write to pseudo-tty.
    229  * Wakeups of controlling tty will happen
    230  * indirectly, when tty driver calls ptsstart.
    231  */
    232 int
    233 ptswrite(dev, uio, flag)
    234 	dev_t dev;
    235 	struct uio *uio;
    236 	int flag;
    237 {
    238 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    239 	register struct tty *tp = pti->pt_tty;
    240 
    241 	if (tp->t_oproc == 0)
    242 		return (EIO);
    243 	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
    244 }
    245 
    246 /*
    247  * Start output on pseudo-tty.
    248  * Wake up process polling or sleeping for input from controlling tty.
    249  */
    250 void
    251 ptsstart(tp)
    252 	struct tty *tp;
    253 {
    254 	register struct pt_softc *pti = &pt_softc[minor(tp->t_dev)];
    255 
    256 	if (ISSET(tp->t_state, TS_TTSTOP))
    257 		return;
    258 	if (pti->pt_flags & PF_STOPPED) {
    259 		pti->pt_flags &= ~PF_STOPPED;
    260 		pti->pt_send = TIOCPKT_START;
    261 	}
    262 	ptcwakeup(tp, FREAD);
    263 }
    264 
    265 void
    266 ptsstop(tp, flush)
    267 	register struct tty *tp;
    268 	int flush;
    269 {
    270 	struct pt_softc *pti = &pt_softc[minor(tp->t_dev)];
    271 	int flag;
    272 
    273 	/* note: FLUSHREAD and FLUSHWRITE already ok */
    274 	if (flush == 0) {
    275 		flush = TIOCPKT_STOP;
    276 		pti->pt_flags |= PF_STOPPED;
    277 	} else
    278 		pti->pt_flags &= ~PF_STOPPED;
    279 	pti->pt_send |= flush;
    280 	/* change of perspective */
    281 	flag = 0;
    282 	if (flush & FREAD)
    283 		flag |= FWRITE;
    284 	if (flush & FWRITE)
    285 		flag |= FREAD;
    286 	ptcwakeup(tp, flag);
    287 }
    288 
    289 void
    290 ptcwakeup(tp, flag)
    291 	struct tty *tp;
    292 	int flag;
    293 {
    294 	struct pt_softc *pti = &pt_softc[minor(tp->t_dev)];
    295 
    296 	if (flag & FREAD) {
    297 		selwakeup(&pti->pt_selr);
    298 		wakeup((caddr_t)&tp->t_outq.c_cf);
    299 	}
    300 	if (flag & FWRITE) {
    301 		selwakeup(&pti->pt_selw);
    302 		wakeup((caddr_t)&tp->t_rawq.c_cf);
    303 	}
    304 }
    305 
    306 int ptcopen __P((dev_t, int, int, struct proc *));
    307 
    308 /*ARGSUSED*/
    309 int
    310 ptcopen(dev, flag, devtype, p)
    311 	dev_t dev;
    312 	int flag, devtype;
    313 	struct proc *p;
    314 {
    315 	struct pt_softc *pti;
    316 	register struct tty *tp;
    317 
    318 	if (minor(dev) >= npty)
    319 		return (ENXIO);
    320 	pti = &pt_softc[minor(dev)];
    321 	if (!pti->pt_tty) {
    322 		tp = pti->pt_tty = ttymalloc();
    323 		tty_attach(tp);
    324 	}
    325 	else
    326 		tp = pti->pt_tty;
    327 	if (tp->t_oproc)
    328 		return (EIO);
    329 	tp->t_oproc = ptsstart;
    330 	(void)(*linesw[tp->t_line].l_modem)(tp, 1);
    331 	CLR(tp->t_lflag, EXTPROC);
    332 	pti->pt_flags = 0;
    333 	pti->pt_send = 0;
    334 	pti->pt_ucntl = 0;
    335 	return (0);
    336 }
    337 
    338 /*ARGSUSED*/
    339 int
    340 ptcclose(dev, flag, devtype, p)
    341 	dev_t dev;
    342 	int flag, devtype;
    343 	struct proc *p;
    344 {
    345 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    346 	register struct tty *tp = pti->pt_tty;
    347 
    348 	(void)(*linesw[tp->t_line].l_modem)(tp, 0);
    349 	CLR(tp->t_state, TS_CARR_ON);
    350 	tp->t_oproc = 0;		/* mark closed */
    351 	return (0);
    352 }
    353 
    354 int
    355 ptcread(dev, uio, flag)
    356 	dev_t dev;
    357 	struct uio *uio;
    358 	int flag;
    359 {
    360 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    361 	register struct tty *tp = pti->pt_tty;
    362 	char buf[BUFSIZ];
    363 	int error = 0, cc;
    364 
    365 	/*
    366 	 * We want to block until the slave
    367 	 * is open, and there's something to read;
    368 	 * but if we lost the slave or we're NBIO,
    369 	 * then return the appropriate error instead.
    370 	 */
    371 	for (;;) {
    372 		if (ISSET(tp->t_state, TS_ISOPEN)) {
    373 			if (pti->pt_flags&PF_PKT && pti->pt_send) {
    374 				error = ureadc((int)pti->pt_send, uio);
    375 				if (error)
    376 					return (error);
    377 				if (pti->pt_send & TIOCPKT_IOCTL) {
    378 					cc = min(uio->uio_resid,
    379 						sizeof(tp->t_termios));
    380 					uiomove((caddr_t) &tp->t_termios,
    381 						cc, uio);
    382 				}
    383 				pti->pt_send = 0;
    384 				return (0);
    385 			}
    386 			if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
    387 				error = ureadc((int)pti->pt_ucntl, uio);
    388 				if (error)
    389 					return (error);
    390 				pti->pt_ucntl = 0;
    391 				return (0);
    392 			}
    393 			if (tp->t_outq.c_cc && !ISSET(tp->t_state, TS_TTSTOP))
    394 				break;
    395 		}
    396 		if (!ISSET(tp->t_state, TS_CARR_ON))
    397 			return (0);	/* EOF */
    398 		if (flag & IO_NDELAY)
    399 			return (EWOULDBLOCK);
    400 		error = tsleep((caddr_t)&tp->t_outq.c_cf, TTIPRI | PCATCH,
    401 			       ttyin, 0);
    402 		if (error)
    403 			return (error);
    404 	}
    405 	if (pti->pt_flags & (PF_PKT|PF_UCNTL))
    406 		error = ureadc(0, uio);
    407 	while (uio->uio_resid > 0 && error == 0) {
    408 		cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
    409 		if (cc <= 0)
    410 			break;
    411 		error = uiomove(buf, cc, uio);
    412 	}
    413 	if (tp->t_outq.c_cc <= tp->t_lowat) {
    414 		if (ISSET(tp->t_state, TS_ASLEEP)) {
    415 			CLR(tp->t_state, TS_ASLEEP);
    416 			wakeup((caddr_t)&tp->t_outq);
    417 		}
    418 		selwakeup(&tp->t_wsel);
    419 	}
    420 	return (error);
    421 }
    422 
    423 
    424 int
    425 ptcwrite(dev, uio, flag)
    426 	dev_t dev;
    427 	register struct uio *uio;
    428 	int flag;
    429 {
    430 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    431 	register struct tty *tp = pti->pt_tty;
    432 	register u_char *cp = NULL;
    433 	register int cc = 0;
    434 	u_char locbuf[BUFSIZ];
    435 	int cnt = 0;
    436 	int error = 0;
    437 
    438 again:
    439 	if (!ISSET(tp->t_state, TS_ISOPEN))
    440 		goto block;
    441 	if (pti->pt_flags & PF_REMOTE) {
    442 		if (tp->t_canq.c_cc)
    443 			goto block;
    444 		while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) {
    445 			if (cc == 0) {
    446 				cc = min(uio->uio_resid, BUFSIZ);
    447 				cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
    448 				cp = locbuf;
    449 				error = uiomove((caddr_t)cp, cc, uio);
    450 				if (error)
    451 					return (error);
    452 				/* check again for safety */
    453 				if (!ISSET(tp->t_state, TS_ISOPEN))
    454 					return (EIO);
    455 			}
    456 			if (cc)
    457 				(void) b_to_q((char *)cp, cc, &tp->t_canq);
    458 			cc = 0;
    459 		}
    460 		(void) putc(0, &tp->t_canq);
    461 		ttwakeup(tp);
    462 		wakeup((caddr_t)&tp->t_canq);
    463 		return (0);
    464 	}
    465 	while (uio->uio_resid > 0) {
    466 		if (cc == 0) {
    467 			cc = min(uio->uio_resid, BUFSIZ);
    468 			cp = locbuf;
    469 			error = uiomove((caddr_t)cp, cc, uio);
    470 			if (error)
    471 				return (error);
    472 			/* check again for safety */
    473 			if (!ISSET(tp->t_state, TS_ISOPEN))
    474 				return (EIO);
    475 		}
    476 		while (cc > 0) {
    477 			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
    478 			   (tp->t_canq.c_cc > 0 || !ISSET(tp->t_iflag, ICANON))) {
    479 				wakeup((caddr_t)&tp->t_rawq);
    480 				goto block;
    481 			}
    482 			(*linesw[tp->t_line].l_rint)(*cp++, tp);
    483 			cnt++;
    484 			cc--;
    485 		}
    486 		cc = 0;
    487 	}
    488 	return (0);
    489 block:
    490 	/*
    491 	 * Come here to wait for slave to open, for space
    492 	 * in outq, or space in rawq.
    493 	 */
    494 	if (!ISSET(tp->t_state, TS_CARR_ON))
    495 		return (EIO);
    496 	if (flag & IO_NDELAY) {
    497 		/* adjust for data copied in but not written */
    498 		uio->uio_resid += cc;
    499 		if (cnt == 0)
    500 			return (EWOULDBLOCK);
    501 		return (0);
    502 	}
    503 	error = tsleep((caddr_t)&tp->t_rawq.c_cf, TTOPRI | PCATCH,
    504 		       ttyout, 0);
    505 	if (error) {
    506 		/* adjust for data copied in but not written */
    507 		uio->uio_resid += cc;
    508 		return (error);
    509 	}
    510 	goto again;
    511 }
    512 
    513 int
    514 ptcpoll(dev, events, p)
    515 	dev_t dev;
    516 	int events;
    517 	struct proc *p;
    518 {
    519 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    520 	register struct tty *tp = pti->pt_tty;
    521 	int revents = 0;
    522 	int s = splsoftclock();
    523 
    524 	if (events & (POLLIN | POLLRDNORM))
    525 		if (ISSET(tp->t_state, TS_ISOPEN) &&
    526 		    ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
    527 		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
    528 		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
    529 			revents |= events & (POLLIN | POLLRDNORM);
    530 
    531 	if (events & (POLLOUT | POLLWRNORM))
    532 		if (ISSET(tp->t_state, TS_ISOPEN) &&
    533 		    ((pti->pt_flags & PF_REMOTE) ?
    534 		     (tp->t_canq.c_cc == 0) :
    535 		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
    536 		      (tp->t_canq.c_cc == 0 && ISSET(tp->t_iflag, ICANON)))))
    537 			revents |= events & (POLLOUT | POLLWRNORM);
    538 
    539 	if (events & POLLHUP)
    540 		if (!ISSET(tp->t_state, TS_CARR_ON))
    541 			revents |= POLLHUP;
    542 
    543 	if (revents == 0) {
    544 		if (events & (POLLIN | POLLHUP | POLLRDNORM))
    545 			selrecord(p, &pti->pt_selr);
    546 
    547 		if (events & (POLLOUT | POLLWRNORM))
    548 			selrecord(p, &pti->pt_selw);
    549 	}
    550 
    551 	splx(s);
    552 	return (revents);
    553 }
    554 
    555 
    556 struct tty *
    557 ptytty(dev)
    558 	dev_t dev;
    559 {
    560 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    561 	register struct tty *tp = pti->pt_tty;
    562 
    563 	return (tp);
    564 }
    565 
    566 /*ARGSUSED*/
    567 int
    568 ptyioctl(dev, cmd, data, flag, p)
    569 	dev_t dev;
    570 	u_long cmd;
    571 	caddr_t data;
    572 	int flag;
    573 	struct proc *p;
    574 {
    575 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    576 	register struct tty *tp = pti->pt_tty;
    577 	register u_char *cc = tp->t_cc;
    578 	int stop, error;
    579 
    580 	/*
    581 	 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
    582 	 * ttywflush(tp) will hang if there are characters in the outq.
    583 	 */
    584 	if (cmd == TIOCEXT) {
    585 		/*
    586 		 * When the EXTPROC bit is being toggled, we need
    587 		 * to send an TIOCPKT_IOCTL if the packet driver
    588 		 * is turned on.
    589 		 */
    590 		if (*(int *)data) {
    591 			if (pti->pt_flags & PF_PKT) {
    592 				pti->pt_send |= TIOCPKT_IOCTL;
    593 				ptcwakeup(tp, FREAD);
    594 			}
    595 			SET(tp->t_lflag, EXTPROC);
    596 		} else {
    597 			if (ISSET(tp->t_state, EXTPROC) &&
    598 			    (pti->pt_flags & PF_PKT)) {
    599 				pti->pt_send |= TIOCPKT_IOCTL;
    600 				ptcwakeup(tp, FREAD);
    601 			}
    602 			CLR(tp->t_lflag, EXTPROC);
    603 		}
    604 		return(0);
    605 	} else
    606 	if (cdevsw[major(dev)].d_open == ptcopen)
    607 		switch (cmd) {
    608 
    609 		case TIOCGPGRP:
    610 #ifdef COMPAT_SUNOS
    611 			{
    612 			/*
    613 			 * I'm not sure about SunOS TIOCGPGRP semantics
    614 			 * on PTYs, but it's something like this:
    615 			 */
    616 			extern struct emul emul_sunos;
    617 			if (p->p_emul == &emul_sunos && tp->t_pgrp == 0)
    618 				return (EIO);
    619 			*(int *)data = tp->t_pgrp->pg_id;
    620 			return (0);
    621 			}
    622 #endif
    623 			/*
    624 			 * We avoid calling ttioctl on the controller since,
    625 			 * in that case, tp must be the controlling terminal.
    626 			 */
    627 			*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
    628 			return (0);
    629 
    630 		case TIOCPKT:
    631 			if (*(int *)data) {
    632 				if (pti->pt_flags & PF_UCNTL)
    633 					return (EINVAL);
    634 				pti->pt_flags |= PF_PKT;
    635 			} else
    636 				pti->pt_flags &= ~PF_PKT;
    637 			return (0);
    638 
    639 		case TIOCUCNTL:
    640 			if (*(int *)data) {
    641 				if (pti->pt_flags & PF_PKT)
    642 					return (EINVAL);
    643 				pti->pt_flags |= PF_UCNTL;
    644 			} else
    645 				pti->pt_flags &= ~PF_UCNTL;
    646 			return (0);
    647 
    648 		case TIOCREMOTE:
    649 			if (*(int *)data)
    650 				pti->pt_flags |= PF_REMOTE;
    651 			else
    652 				pti->pt_flags &= ~PF_REMOTE;
    653 			ttyflush(tp, FREAD|FWRITE);
    654 			return (0);
    655 
    656 #ifdef COMPAT_OLDTTY
    657 		case TIOCSETP:
    658 		case TIOCSETN:
    659 #endif
    660 		case TIOCSETD:
    661 		case TIOCSETA:
    662 		case TIOCSETAW:
    663 		case TIOCSETAF:
    664 			ndflush(&tp->t_outq, tp->t_outq.c_cc);
    665 			break;
    666 
    667 		case TIOCSIG:
    668 			if (*(unsigned int *)data >= NSIG)
    669 				return(EINVAL);
    670 			if (!ISSET(tp->t_lflag, NOFLSH))
    671 				ttyflush(tp, FREAD|FWRITE);
    672 			pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
    673 			if ((*(unsigned int *)data == SIGINFO) &&
    674 			    (!ISSET(tp->t_lflag, NOKERNINFO)))
    675 				ttyinfo(tp);
    676 			return(0);
    677 		}
    678 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
    679 	if (error < 0)
    680 		 error = ttioctl(tp, cmd, data, flag, p);
    681 	if (error < 0) {
    682 		if (pti->pt_flags & PF_UCNTL &&
    683 		    (cmd & ~0xff) == UIOCCMD(0)) {
    684 			if (cmd & 0xff) {
    685 				pti->pt_ucntl = (u_char)cmd;
    686 				ptcwakeup(tp, FREAD);
    687 			}
    688 			return (0);
    689 		}
    690 		error = ENOTTY;
    691 	}
    692 	/*
    693 	 * If external processing and packet mode send ioctl packet.
    694 	 */
    695 	if (ISSET(tp->t_lflag, EXTPROC) && (pti->pt_flags & PF_PKT)) {
    696 		switch(cmd) {
    697 		case TIOCSETA:
    698 		case TIOCSETAW:
    699 		case TIOCSETAF:
    700 #ifdef COMPAT_OLDTTY
    701 		case TIOCSETP:
    702 		case TIOCSETN:
    703 		case TIOCSETC:
    704 		case TIOCSLTC:
    705 		case TIOCLBIS:
    706 		case TIOCLBIC:
    707 		case TIOCLSET:
    708 #endif
    709 			pti->pt_send |= TIOCPKT_IOCTL;
    710 			ptcwakeup(tp, FREAD);
    711 		default:
    712 			break;
    713 		}
    714 	}
    715 	stop = ISSET(tp->t_iflag, IXON) && CCEQ(cc[VSTOP], CTRL('s'))
    716 		&& CCEQ(cc[VSTART], CTRL('q'));
    717 	if (pti->pt_flags & PF_NOSTOP) {
    718 		if (stop) {
    719 			pti->pt_send &= ~TIOCPKT_NOSTOP;
    720 			pti->pt_send |= TIOCPKT_DOSTOP;
    721 			pti->pt_flags &= ~PF_NOSTOP;
    722 			ptcwakeup(tp, FREAD);
    723 		}
    724 	} else {
    725 		if (!stop) {
    726 			pti->pt_send &= ~TIOCPKT_DOSTOP;
    727 			pti->pt_send |= TIOCPKT_NOSTOP;
    728 			pti->pt_flags |= PF_NOSTOP;
    729 			ptcwakeup(tp, FREAD);
    730 		}
    731 	}
    732 	return (error);
    733 }
    734