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