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