Home | History | Annotate | Line # | Download | only in kern
tty_pty.c revision 1.12.2.5
      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.5 1993/11/12 16:10:48 cgd 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 
    314 /* XXX -hv- 6.Oct.92 this prevents the "hanging console bug" with X11 */
    315 	if (constty==tp)
    316 		constty = 0;
    317 
    318 	pt_ioctl[minor(dev)].pt_flags &= ~PF_COPEN;
    319 	if ((pt_ioctl[minor(dev)].pt_flags & PF_SOPEN) == 0) {
    320 		ttyfree(tp);
    321 		pt_tty[minor(dev)] = (struct tty *)NULL;
    322 	}
    323 	return (0);
    324 }
    325 
    326 int
    327 ptcread(dev, uio, flag)
    328 	dev_t dev;
    329 	struct uio *uio;
    330 	int flag;
    331 {
    332 	register struct tty *tp = pt_tty[minor(dev)];
    333 	struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
    334 	u_char buf[BUFSIZ];
    335 	int error = 0, cc;
    336 
    337 	/*
    338 	 * We want to block until the slave
    339 	 * is open, and there's something to read;
    340 	 * but if we lost the slave or we're NBIO,
    341 	 * then return the appropriate error instead.
    342 	 */
    343 	for (;;) {
    344 		if (tp->t_state&TS_ISOPEN) {
    345 			if (pti->pt_flags&PF_PKT && pti->pt_send) {
    346 				error = ureadc((int)pti->pt_send, uio);
    347 				if (error)
    348 					return (error);
    349 				if (pti->pt_send & TIOCPKT_IOCTL) {
    350 					cc = MIN(uio->uio_resid,
    351 						sizeof(tp->t_termios));
    352 					uiomove((caddr_t)&tp->t_termios, cc,
    353 						uio);
    354 				}
    355 				pti->pt_send = 0;
    356 				return (0);
    357 			}
    358 			if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
    359 				error = ureadc((int)pti->pt_ucntl, uio);
    360 				if (error)
    361 					return (error);
    362 				pti->pt_ucntl = 0;
    363 				return (0);
    364 			}
    365 			if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
    366 				break;
    367 		}
    368 		if ((tp->t_state&TS_CARR_ON) == 0)
    369 			return (0);	/* EOF */
    370 		if (flag & IO_NDELAY)
    371 			return (EWOULDBLOCK);
    372 		if (error = tsleep((caddr_t)&tp->t_outq.c_cl, TTIPRI | PCATCH,
    373 		    ttyin, 0))
    374 			return (error);
    375 	}
    376 	if (pti->pt_flags & (PF_PKT|PF_UCNTL))
    377 		error = ureadc(0, uio);
    378 	while (uio->uio_resid > 0 && error == 0) {
    379 		cc = q_to_b(&tp->t_outq, buf, MIN(uio->uio_resid, BUFSIZ));
    380 		if (cc <= 0)
    381 			break;
    382 		error = uiomove(buf, cc, uio);
    383 	}
    384 	if (tp->t_outq.c_cc <= tp->t_lowat) {
    385 		if (tp->t_state&TS_ASLEEP) {
    386 			tp->t_state &= ~TS_ASLEEP;
    387 			wakeup((caddr_t)&tp->t_outq);
    388 		}
    389 		selwakeup(&tp->t_wsel);
    390 	}
    391 	return (error);
    392 }
    393 
    394 void
    395 ptsstop(tp, flush)
    396 	register struct tty *tp;
    397 	int flush;
    398 {
    399 	struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
    400 	int flag;
    401 
    402 	/* note: FLUSHREAD and FLUSHWRITE already ok */
    403 	if (flush == 0) {
    404 		flush = TIOCPKT_STOP;
    405 		pti->pt_flags |= PF_STOPPED;
    406 	} else
    407 		pti->pt_flags &= ~PF_STOPPED;
    408 	pti->pt_send |= flush;
    409 	/* change of perspective */
    410 	flag = 0;
    411 	if (flush & FREAD)
    412 		flag |= FWRITE;
    413 	if (flush & FWRITE)
    414 		flag |= FREAD;
    415 	ptcwakeup(tp, flag);
    416 }
    417 
    418 int
    419 ptcselect(dev, rw, p)
    420 	dev_t dev;
    421 	int rw;
    422 	struct proc *p;
    423 {
    424 	register struct tty *tp = pt_tty[minor(dev)];
    425 	struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
    426 	int s;
    427 
    428 	if ((tp->t_state&TS_CARR_ON) == 0)
    429 		return (1);
    430 	switch (rw) {
    431 
    432 	case FREAD:
    433 		/*
    434 		 * Need to block timeouts (ttrstart).
    435 		 */
    436 		s = spltty();
    437 		if ((tp->t_state&TS_ISOPEN) &&
    438 		     tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) {
    439 			splx(s);
    440 			return (1);
    441 		}
    442 		splx(s);
    443 		/* FALLTHROUGH */
    444 
    445 	case 0:					/* exceptional */
    446 		if ((tp->t_state&TS_ISOPEN) &&
    447 		    (pti->pt_flags&PF_PKT && pti->pt_send ||
    448 		     pti->pt_flags&PF_UCNTL && pti->pt_ucntl))
    449 			return (1);
    450 		selrecord(p, &pti->pt_selr);
    451 		break;
    452 
    453 
    454 	case FWRITE:
    455 		if (tp->t_state&TS_ISOPEN) {
    456 			if (pti->pt_flags & PF_REMOTE) {
    457 			    if (tp->t_canq.c_cc == 0)
    458 				return (1);
    459 			} else {
    460 			    if (tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2)
    461 				    return (1);
    462 			    if (tp->t_canq.c_cc == 0 && (tp->t_iflag&ICANON))
    463 				    return (1);
    464 			}
    465 		}
    466 		selrecord(p, &pti->pt_selw);
    467 		break;
    468 
    469 	}
    470 	return (0);
    471 }
    472 
    473 int
    474 ptcwrite(dev, uio, flag)
    475 	dev_t dev;
    476 	register struct uio *uio;
    477 	int flag;
    478 {
    479 	register struct tty *tp = pt_tty[minor(dev)];
    480 	register u_char *cp;
    481 	register int cc = 0;
    482 	u_char locbuf[BUFSIZ];
    483 	int cnt = 0;
    484 	struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
    485 	int error = 0;
    486 
    487 again:
    488 	if ((tp->t_state&TS_ISOPEN) == 0)
    489 		goto block;
    490 	if (pti->pt_flags & PF_REMOTE) {
    491 		if (tp->t_canq.c_cc)
    492 			goto block;
    493 		while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) {
    494 			if (cc == 0) {
    495 				cc = min(uio->uio_resid, BUFSIZ);
    496 				cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
    497 				cp = locbuf;
    498 				error = uiomove((caddr_t)cp, cc, uio);
    499 				if (error)
    500 					return (error);
    501 				/* check again for safety */
    502 				if ((tp->t_state&TS_ISOPEN) == 0)
    503 					return (EIO);
    504 			}
    505 			if (cc)
    506 				(void) b_to_q(cp, cc, &tp->t_canq);
    507 			cc = 0;
    508 		}
    509 		(void) putc(0, &tp->t_canq);
    510 		ttwakeup(tp);
    511 		wakeup((caddr_t)&tp->t_canq);
    512 		return (0);
    513 	}
    514 	while (uio->uio_resid > 0) {
    515 		if (cc == 0) {
    516 			cc = min(uio->uio_resid, BUFSIZ);
    517 			cp = locbuf;
    518 			error = uiomove((caddr_t)cp, cc, uio);
    519 			if (error)
    520 				return (error);
    521 			/* check again for safety */
    522 			if ((tp->t_state&TS_ISOPEN) == 0)
    523 				return (EIO);
    524 		}
    525 		while (cc > 0) {
    526 			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
    527 			   (tp->t_canq.c_cc > 0 || !(tp->t_iflag&ICANON))) {
    528 				wakeup((caddr_t)&tp->t_rawq);
    529 				goto block;
    530 			}
    531 			(*linesw[tp->t_line].l_rint)(*cp++, tp);
    532 			cnt++;
    533 			cc--;
    534 		}
    535 		cc = 0;
    536 	}
    537 	return (0);
    538 block:
    539 	/*
    540 	 * Come here to wait for slave to open, for space
    541 	 * in outq, or space in rawq.
    542 	 */
    543 	if ((tp->t_state&TS_CARR_ON) == 0)
    544 		return (EIO);
    545 	if (flag & IO_NDELAY) {
    546 		/* adjust for data copied in but not written */
    547 		uio->uio_resid += cc;
    548 		if (cnt == 0)
    549 			return (EWOULDBLOCK);
    550 		return (0);
    551 	}
    552 	if (error = tsleep((caddr_t)&tp->t_rawq.c_cf, TTOPRI | PCATCH,
    553 	    ttyout, 0)) {
    554 		/* adjust for data copied in but not written */
    555 		uio->uio_resid += cc;
    556 		return (error);
    557 	}
    558 	goto again;
    559 }
    560 
    561 /*ARGSUSED*/
    562 int
    563 ptyioctl(dev, cmd, data, flag)
    564 	caddr_t data;
    565 	int cmd, flag;
    566 	dev_t dev;
    567 {
    568 	register struct tty *tp = pt_tty[minor(dev)];
    569 	register struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
    570 	register u_char *cc = tp->t_cc;
    571 	int stop, error;
    572 
    573 	/*
    574 	 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
    575 	 * ttywflush(tp) will hang if there are characters in the outq.
    576 	 */
    577 	if (cmd == TIOCEXT) {
    578 		/*
    579 		 * When the EXTPROC bit is being toggled, we need
    580 		 * to send an TIOCPKT_IOCTL if the packet driver
    581 		 * is turned on.
    582 		 */
    583 		if (*(int *)data) {
    584 			if (pti->pt_flags & PF_PKT) {
    585 				pti->pt_send |= TIOCPKT_IOCTL;
    586 				ptcwakeup(tp, FREAD);
    587 			}
    588 			tp->t_lflag |= EXTPROC;
    589 		} else {
    590 			if ((tp->t_state & EXTPROC) &&
    591 			    (pti->pt_flags & PF_PKT)) {
    592 				pti->pt_send |= TIOCPKT_IOCTL;
    593 				ptcwakeup(tp, FREAD);
    594 			}
    595 			tp->t_lflag &= ~EXTPROC;
    596 		}
    597 		return(0);
    598 	} else
    599 	if (cdevsw[major(dev)].d_open == ptcopen)
    600 		switch (cmd) {
    601 
    602 		case TIOCGPGRP:
    603 			/*
    604 			 * We aviod calling ttioctl on the controller since,
    605 			 * in that case, tp must be the controlling terminal.
    606 			 */
    607 			*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
    608 			return (0);
    609 
    610 		case TIOCPKT:
    611 			if (*(int *)data) {
    612 				if (pti->pt_flags & PF_UCNTL)
    613 					return (EINVAL);
    614 				pti->pt_flags |= PF_PKT;
    615 			} else
    616 				pti->pt_flags &= ~PF_PKT;
    617 			return (0);
    618 
    619 		case TIOCUCNTL:
    620 			if (*(int *)data) {
    621 				if (pti->pt_flags & PF_PKT)
    622 					return (EINVAL);
    623 				pti->pt_flags |= PF_UCNTL;
    624 			} else
    625 				pti->pt_flags &= ~PF_UCNTL;
    626 			return (0);
    627 
    628 		case TIOCREMOTE:
    629 			if (*(int *)data)
    630 				pti->pt_flags |= PF_REMOTE;
    631 			else
    632 				pti->pt_flags &= ~PF_REMOTE;
    633 			ttyflush(tp, FREAD|FWRITE);
    634 			return (0);
    635 
    636 #ifdef COMPAT_43
    637 	/* wkt */
    638 		case TIOCSETP:
    639 		case TIOCSETN:
    640 #endif
    641 		case TIOCSETD:
    642 		case TIOCSETA:
    643 		case TIOCSETAW:
    644 		case TIOCSETAF:
    645 			flushq(&tp->t_outq);
    646 			break;
    647 
    648 		case TIOCSIG:
    649 			if (*(unsigned int *)data >= NSIG)
    650 				return(EINVAL);
    651 			if ((tp->t_lflag&NOFLSH) == 0)
    652 				ttyflush(tp, FREAD|FWRITE);
    653 			pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
    654 			if ((*(unsigned int *)data == SIGINFO) &&
    655 			    ((tp->t_lflag&NOKERNINFO) == 0))
    656 				ttyinfo(tp);
    657 			return(0);
    658 		}
    659 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag);
    660 	if (error < 0)
    661 		 error = ttioctl(tp, cmd, data, flag);
    662 	/*
    663 	 * Since we use the tty queues internally,
    664 	 * pty's can't be switched to disciplines which overwrite
    665 	 * the queues.  We can't tell anything about the discipline
    666 	 * from here...
    667 	 */
    668 	if (linesw[tp->t_line].l_rint != ttyinput) {
    669 		(*linesw[tp->t_line].l_close)(tp, flag);
    670 		tp->t_line = TTYDISC;
    671 		(void)(*linesw[tp->t_line].l_open)(dev, tp);
    672 		error = ENOTTY;
    673 	}
    674 	if (error < 0) {
    675 		if (pti->pt_flags & PF_UCNTL &&
    676 		    (cmd & ~0xff) == UIOCCMD(0)) {
    677 			if (cmd & 0xff) {
    678 				pti->pt_ucntl = (u_char)cmd;
    679 				ptcwakeup(tp, FREAD);
    680 			}
    681 			return (0);
    682 		}
    683 		error = ENOTTY;
    684 	}
    685 	/*
    686 	 * If external processing and packet mode send ioctl packet.
    687 	 */
    688 	if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
    689 		switch(cmd) {
    690 		case TIOCSETA:
    691 		case TIOCSETAW:
    692 		case TIOCSETAF:
    693 #ifdef	COMPAT_43
    694 	/* wkt */
    695 		case TIOCSETP:
    696 		case TIOCSETN:
    697 		case TIOCSETC:
    698 		case TIOCSLTC:
    699 		case TIOCLBIS:
    700 		case TIOCLBIC:
    701 		case TIOCLSET:
    702 #endif
    703 			pti->pt_send |= TIOCPKT_IOCTL;
    704 		default:
    705 			break;
    706 		}
    707 	}
    708 	stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
    709 		&& CCEQ(cc[VSTART], CTRL('q'));
    710 	if (pti->pt_flags & PF_NOSTOP) {
    711 		if (stop) {
    712 			pti->pt_send &= ~TIOCPKT_NOSTOP;
    713 			pti->pt_send |= TIOCPKT_DOSTOP;
    714 			pti->pt_flags &= ~PF_NOSTOP;
    715 			ptcwakeup(tp, FREAD);
    716 		}
    717 	} else {
    718 		if (!stop) {
    719 			pti->pt_send &= ~TIOCPKT_DOSTOP;
    720 			pti->pt_send |= TIOCPKT_NOSTOP;
    721 			pti->pt_flags |= PF_NOSTOP;
    722 			ptcwakeup(tp, FREAD);
    723 		}
    724 	}
    725 	return (error);
    726 }
    727 #endif
    728