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