Home | History | Annotate | Line # | Download | only in kern
tty_pty.c revision 1.12.2.2
      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.2 1993/09/27 04:08:57 deraadt 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 	if ((pt_ioctl[minor(dev)].pt_flags & PF_COPEN) == 0) {
    155 		ttyfree(tp);
    156 		pt_tty[minor(dev)] = (struct tty *)NULL;
    157 	}
    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 	tp->t_session = 0;
    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 #endif
    727