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