Home | History | Annotate | Line # | Download | only in kern
tty_pty.c revision 1.33
      1 /*	$NetBSD: tty_pty.c,v 1.33 1996/03/30 22:24:45 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)tty_pty.c	8.2 (Berkeley) 9/23/93
     36  */
     37 
     38 /*
     39  * Pseudo-teletype Driver
     40  * (Actually two drivers, requiring two entries in 'cdevsw')
     41  */
     42 #include "pty.h"		/* XXX */
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/ioctl.h>
     47 #include <sys/proc.h>
     48 #include <sys/tty.h>
     49 #include <sys/file.h>
     50 #include <sys/uio.h>
     51 #include <sys/kernel.h>
     52 #include <sys/vnode.h>
     53 #include <sys/signalvar.h>
     54 #include <sys/uio.h>
     55 #include <sys/conf.h>
     56 
     57 
     58 
     59 #if NPTY == 1
     60 #undef NPTY
     61 #define	NPTY	32		/* crude XXX */
     62 #endif
     63 
     64 #define BUFSIZ 100		/* Chunk size iomoved to/from user */
     65 
     66 /*
     67  * pts == /dev/tty[pqrs]?
     68  * ptc == /dev/pty[pqrs]?
     69  */
     70 struct	pt_softc {
     71 	struct	tty *pt_tty;
     72 	int	pt_flags;
     73 	struct	selinfo pt_selr, pt_selw;
     74 	u_char	pt_send;
     75 	u_char	pt_ucntl;
     76 } pt_softc[NPTY];		/* XXX */
     77 int	npty = NPTY;		/* for pstat -t */
     78 
     79 #define	PF_PKT		0x08		/* packet mode */
     80 #define	PF_STOPPED	0x10		/* user told stopped */
     81 #define	PF_REMOTE	0x20		/* remote and flow controlled input */
     82 #define	PF_NOSTOP	0x40
     83 #define PF_UCNTL	0x80		/* user control mode */
     84 
     85 void	ptyattach __P((int));
     86 void	ptcwakeup __P((struct tty *, int));
     87 struct tty *ptytty __P((dev_t));
     88 void	ptsstart __P((struct tty *));
     89 
     90 /*
     91  * Establish n (or default if n is 1) ptys in the system.
     92  */
     93 void
     94 ptyattach(n)
     95 	int n;
     96 {
     97 #ifdef notyet
     98 #define	DEFAULT_NPTY	32
     99 
    100 	/* maybe should allow 0 => none? */
    101 	if (n <= 1)
    102 		n = DEFAULT_NPTY;
    103 	pt_softc = malloc(n * sizeof(struct pt_softc), M_DEVBUF, M_WAITOK);
    104 	npty = n;
    105 #endif
    106 }
    107 
    108 /*ARGSUSED*/
    109 int
    110 ptsopen(dev, flag, devtype, p)
    111 	dev_t dev;
    112 	int flag, devtype;
    113 	struct proc *p;
    114 {
    115 	struct pt_softc *pti;
    116 	register struct tty *tp;
    117 	int error;
    118 
    119 	if (minor(dev) >= npty)
    120 		return (ENXIO);
    121 	pti = &pt_softc[minor(dev)];
    122 	if (!pti->pt_tty)
    123 		tp = pti->pt_tty = ttymalloc();
    124 	else
    125 		tp = pti->pt_tty;
    126 	if ((tp->t_state & TS_ISOPEN) == 0) {
    127 		tp->t_state |= TS_WOPEN;
    128 		ttychars(tp);		/* Set up default chars */
    129 		tp->t_iflag = TTYDEF_IFLAG;
    130 		tp->t_oflag = TTYDEF_OFLAG;
    131 		tp->t_lflag = TTYDEF_LFLAG;
    132 		tp->t_cflag = TTYDEF_CFLAG;
    133 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    134 		ttsetwater(tp);		/* would be done in xxparam() */
    135 	} else if (tp->t_state&TS_XCLUDE && p->p_ucred->cr_uid != 0)
    136 		return (EBUSY);
    137 	if (tp->t_oproc)			/* Ctrlr still around. */
    138 		tp->t_state |= TS_CARR_ON;
    139 	while ((tp->t_state & TS_CARR_ON) == 0) {
    140 		tp->t_state |= TS_WOPEN;
    141 		if (flag&FNONBLOCK)
    142 			break;
    143 		error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH,
    144 				 ttopen, 0);
    145 		if (error)
    146 			return (error);
    147 	}
    148 	error = (*linesw[tp->t_line].l_open)(dev, tp);
    149 	ptcwakeup(tp, FREAD|FWRITE);
    150 	return (error);
    151 }
    152 
    153 int
    154 ptsclose(dev, flag, mode, p)
    155 	dev_t dev;
    156 	int flag, mode;
    157 	struct proc *p;
    158 {
    159 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    160 	register struct tty *tp = pti->pt_tty;
    161 	int error;
    162 
    163 	error = (*linesw[tp->t_line].l_close)(tp, flag);
    164 	error |= ttyclose(tp);
    165 	ptcwakeup(tp, FREAD|FWRITE);
    166 	return (error);
    167 }
    168 
    169 int
    170 ptsread(dev, uio, flag)
    171 	dev_t dev;
    172 	struct uio *uio;
    173 	int flag;
    174 {
    175 	struct proc *p = curproc;
    176 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    177 	register struct tty *tp = pti->pt_tty;
    178 	int error = 0;
    179 
    180 again:
    181 	if (pti->pt_flags & PF_REMOTE) {
    182 		while (isbackground(p, tp)) {
    183 			if ((p->p_sigignore & sigmask(SIGTTIN)) ||
    184 			    (p->p_sigmask & sigmask(SIGTTIN)) ||
    185 			    p->p_pgrp->pg_jobc == 0 ||
    186 			    p->p_flag & P_PPWAIT)
    187 				return (EIO);
    188 			pgsignal(p->p_pgrp, SIGTTIN, 1);
    189 			error = ttysleep(tp, (caddr_t)&lbolt,
    190 					 TTIPRI | PCATCH, ttybg, 0);
    191 			if (error)
    192 				return (error);
    193 		}
    194 		if (tp->t_canq.c_cc == 0) {
    195 			if (flag & IO_NDELAY)
    196 				return (EWOULDBLOCK);
    197 			error = ttysleep(tp, (caddr_t)&tp->t_canq,
    198 					 TTIPRI | PCATCH, ttyin, 0);
    199 			if (error)
    200 				return (error);
    201 			goto again;
    202 		}
    203 		while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
    204 			if (ureadc(getc(&tp->t_canq), uio) < 0) {
    205 				error = EFAULT;
    206 				break;
    207 			}
    208 		if (tp->t_canq.c_cc == 1)
    209 			(void) getc(&tp->t_canq);
    210 		if (tp->t_canq.c_cc)
    211 			return (error);
    212 	} else
    213 		if (tp->t_oproc)
    214 			error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
    215 	ptcwakeup(tp, FWRITE);
    216 	return (error);
    217 }
    218 
    219 /*
    220  * Write to pseudo-tty.
    221  * Wakeups of controlling tty will happen
    222  * indirectly, when tty driver calls ptsstart.
    223  */
    224 int
    225 ptswrite(dev, uio, flag)
    226 	dev_t dev;
    227 	struct uio *uio;
    228 	int flag;
    229 {
    230 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    231 	register struct tty *tp = pti->pt_tty;
    232 
    233 	if (tp->t_oproc == 0)
    234 		return (EIO);
    235 	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
    236 }
    237 
    238 /*
    239  * Start output on pseudo-tty.
    240  * Wake up process selecting or sleeping for input from controlling tty.
    241  */
    242 void
    243 ptsstart(tp)
    244 	struct tty *tp;
    245 {
    246 	register struct pt_softc *pti = &pt_softc[minor(tp->t_dev)];
    247 
    248 	if (tp->t_state & TS_TTSTOP)
    249 		return;
    250 	if (pti->pt_flags & PF_STOPPED) {
    251 		pti->pt_flags &= ~PF_STOPPED;
    252 		pti->pt_send = TIOCPKT_START;
    253 	}
    254 	ptcwakeup(tp, FREAD);
    255 }
    256 
    257 int
    258 ptsstop(tp, flush)
    259 	register struct tty *tp;
    260 	int flush;
    261 {
    262 	struct pt_softc *pti = &pt_softc[minor(tp->t_dev)];
    263 	int flag;
    264 
    265 	/* note: FLUSHREAD and FLUSHWRITE already ok */
    266 	if (flush == 0) {
    267 		flush = TIOCPKT_STOP;
    268 		pti->pt_flags |= PF_STOPPED;
    269 	} else
    270 		pti->pt_flags &= ~PF_STOPPED;
    271 	pti->pt_send |= flush;
    272 	/* change of perspective */
    273 	flag = 0;
    274 	if (flush & FREAD)
    275 		flag |= FWRITE;
    276 	if (flush & FWRITE)
    277 		flag |= FREAD;
    278 	ptcwakeup(tp, flag);
    279 	return 0;
    280 }
    281 
    282 void
    283 ptcwakeup(tp, flag)
    284 	struct tty *tp;
    285 	int flag;
    286 {
    287 	struct pt_softc *pti = &pt_softc[minor(tp->t_dev)];
    288 
    289 	if (flag & FREAD) {
    290 		selwakeup(&pti->pt_selr);
    291 		wakeup((caddr_t)&tp->t_outq.c_cf);
    292 	}
    293 	if (flag & FWRITE) {
    294 		selwakeup(&pti->pt_selw);
    295 		wakeup((caddr_t)&tp->t_rawq.c_cf);
    296 	}
    297 }
    298 
    299 int ptcopen __P((dev_t, int, int, struct proc *));
    300 
    301 /*ARGSUSED*/
    302 int
    303 ptcopen(dev, flag, devtype, p)
    304 	dev_t dev;
    305 	int flag, devtype;
    306 	struct proc *p;
    307 {
    308 	struct pt_softc *pti;
    309 	register struct tty *tp;
    310 
    311 	if (minor(dev) >= npty)
    312 		return (ENXIO);
    313 	pti = &pt_softc[minor(dev)];
    314 	if (!pti->pt_tty)
    315 		tp = pti->pt_tty = ttymalloc();
    316 	else
    317 		tp = pti->pt_tty;
    318 	if (tp->t_oproc)
    319 		return (EIO);
    320 	tp->t_oproc = ptsstart;
    321 	(void)(*linesw[tp->t_line].l_modem)(tp, 1);
    322 	tp->t_lflag &= ~EXTPROC;
    323 	pti->pt_flags = 0;
    324 	pti->pt_send = 0;
    325 	pti->pt_ucntl = 0;
    326 	return (0);
    327 }
    328 
    329 /*ARGSUSED*/
    330 int
    331 ptcclose(dev, flag, devtype, p)
    332 	dev_t dev;
    333 	int flag, devtype;
    334 	struct proc *p;
    335 {
    336 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    337 	register struct tty *tp = pti->pt_tty;
    338 
    339 	(void)(*linesw[tp->t_line].l_modem)(tp, 0);
    340 	tp->t_state &= ~TS_CARR_ON;
    341 	tp->t_oproc = 0;		/* mark closed */
    342 	return (0);
    343 }
    344 
    345 int
    346 ptcread(dev, uio, flag)
    347 	dev_t dev;
    348 	struct uio *uio;
    349 	int flag;
    350 {
    351 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    352 	register struct tty *tp = pti->pt_tty;
    353 	char buf[BUFSIZ];
    354 	int error = 0, cc;
    355 
    356 	/*
    357 	 * We want to block until the slave
    358 	 * is open, and there's something to read;
    359 	 * but if we lost the slave or we're NBIO,
    360 	 * then return the appropriate error instead.
    361 	 */
    362 	for (;;) {
    363 		if (tp->t_state&TS_ISOPEN) {
    364 			if (pti->pt_flags&PF_PKT && pti->pt_send) {
    365 				error = ureadc((int)pti->pt_send, uio);
    366 				if (error)
    367 					return (error);
    368 				if (pti->pt_send & TIOCPKT_IOCTL) {
    369 					cc = min(uio->uio_resid,
    370 						sizeof(tp->t_termios));
    371 					uiomove((caddr_t) &tp->t_termios,
    372 						cc, uio);
    373 				}
    374 				pti->pt_send = 0;
    375 				return (0);
    376 			}
    377 			if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
    378 				error = ureadc((int)pti->pt_ucntl, uio);
    379 				if (error)
    380 					return (error);
    381 				pti->pt_ucntl = 0;
    382 				return (0);
    383 			}
    384 			if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
    385 				break;
    386 		}
    387 		if ((tp->t_state&TS_CARR_ON) == 0)
    388 			return (0);	/* EOF */
    389 		if (flag & IO_NDELAY)
    390 			return (EWOULDBLOCK);
    391 		error = tsleep((caddr_t)&tp->t_outq.c_cf, TTIPRI | PCATCH,
    392 			       ttyin, 0);
    393 		if (error)
    394 			return (error);
    395 	}
    396 	if (pti->pt_flags & (PF_PKT|PF_UCNTL))
    397 		error = ureadc(0, uio);
    398 	while (uio->uio_resid > 0 && error == 0) {
    399 		cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
    400 		if (cc <= 0)
    401 			break;
    402 		error = uiomove(buf, cc, uio);
    403 	}
    404 	if (tp->t_outq.c_cc <= tp->t_lowat) {
    405 		if (tp->t_state&TS_ASLEEP) {
    406 			tp->t_state &= ~TS_ASLEEP;
    407 			wakeup((caddr_t)&tp->t_outq);
    408 		}
    409 		selwakeup(&tp->t_wsel);
    410 	}
    411 	return (error);
    412 }
    413 
    414 
    415 int
    416 ptcwrite(dev, uio, flag)
    417 	dev_t dev;
    418 	register struct uio *uio;
    419 	int flag;
    420 {
    421 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    422 	register struct tty *tp = pti->pt_tty;
    423 	register u_char *cp = NULL;
    424 	register int cc = 0;
    425 	u_char locbuf[BUFSIZ];
    426 	int cnt = 0;
    427 	int error = 0;
    428 
    429 again:
    430 	if ((tp->t_state&TS_ISOPEN) == 0)
    431 		goto block;
    432 	if (pti->pt_flags & PF_REMOTE) {
    433 		if (tp->t_canq.c_cc)
    434 			goto block;
    435 		while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) {
    436 			if (cc == 0) {
    437 				cc = min(uio->uio_resid, BUFSIZ);
    438 				cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
    439 				cp = locbuf;
    440 				error = uiomove((caddr_t)cp, cc, uio);
    441 				if (error)
    442 					return (error);
    443 				/* check again for safety */
    444 				if ((tp->t_state&TS_ISOPEN) == 0)
    445 					return (EIO);
    446 			}
    447 			if (cc)
    448 				(void) b_to_q((char *)cp, cc, &tp->t_canq);
    449 			cc = 0;
    450 		}
    451 		(void) putc(0, &tp->t_canq);
    452 		ttwakeup(tp);
    453 		wakeup((caddr_t)&tp->t_canq);
    454 		return (0);
    455 	}
    456 	while (uio->uio_resid > 0) {
    457 		if (cc == 0) {
    458 			cc = min(uio->uio_resid, BUFSIZ);
    459 			cp = locbuf;
    460 			error = uiomove((caddr_t)cp, cc, uio);
    461 			if (error)
    462 				return (error);
    463 			/* check again for safety */
    464 			if ((tp->t_state&TS_ISOPEN) == 0)
    465 				return (EIO);
    466 		}
    467 		while (cc > 0) {
    468 			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
    469 			   (tp->t_canq.c_cc > 0 || !(tp->t_iflag&ICANON))) {
    470 				wakeup((caddr_t)&tp->t_rawq);
    471 				goto block;
    472 			}
    473 			(*linesw[tp->t_line].l_rint)(*cp++, tp);
    474 			cnt++;
    475 			cc--;
    476 		}
    477 		cc = 0;
    478 	}
    479 	return (0);
    480 block:
    481 	/*
    482 	 * Come here to wait for slave to open, for space
    483 	 * in outq, or space in rawq.
    484 	 */
    485 	if ((tp->t_state&TS_CARR_ON) == 0)
    486 		return (EIO);
    487 	if (flag & IO_NDELAY) {
    488 		/* adjust for data copied in but not written */
    489 		uio->uio_resid += cc;
    490 		if (cnt == 0)
    491 			return (EWOULDBLOCK);
    492 		return (0);
    493 	}
    494 	error = tsleep((caddr_t)&tp->t_rawq.c_cf, TTOPRI | PCATCH,
    495 		       ttyout, 0);
    496 	if (error) {
    497 		/* adjust for data copied in but not written */
    498 		uio->uio_resid += cc;
    499 		return (error);
    500 	}
    501 	goto again;
    502 }
    503 
    504 int
    505 ptcselect(dev, rw, p)
    506 	dev_t dev;
    507 	int rw;
    508 	struct proc *p;
    509 {
    510 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    511 	register struct tty *tp = pti->pt_tty;
    512 	int s;
    513 
    514 	if ((tp->t_state&TS_CARR_ON) == 0)
    515 		return (1);
    516 	switch (rw) {
    517 
    518 	case FREAD:
    519 		/*
    520 		 * Need to block timeouts (ttrstart).
    521 		 */
    522 		s = spltty();
    523 		if ((tp->t_state&TS_ISOPEN) &&
    524 		     tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) {
    525 			splx(s);
    526 			return (1);
    527 		}
    528 		splx(s);
    529 		/* FALLTHROUGH */
    530 
    531 	case 0:					/* exceptional */
    532 		if ((tp->t_state&TS_ISOPEN) &&
    533 		    (((pti->pt_flags & PF_PKT) && pti->pt_send) ||
    534 		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
    535 			return (1);
    536 		selrecord(p, &pti->pt_selr);
    537 		break;
    538 
    539 
    540 	case FWRITE:
    541 		if (tp->t_state&TS_ISOPEN) {
    542 			if (pti->pt_flags & PF_REMOTE) {
    543 			    if (tp->t_canq.c_cc == 0)
    544 				return (1);
    545 			} else {
    546 			    if (tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2)
    547 				    return (1);
    548 			    if (tp->t_canq.c_cc == 0 && (tp->t_iflag&ICANON))
    549 				    return (1);
    550 			}
    551 		}
    552 		selrecord(p, &pti->pt_selw);
    553 		break;
    554 
    555 	}
    556 	return (0);
    557 }
    558 
    559 
    560 struct tty *
    561 ptytty(dev)
    562 	dev_t dev;
    563 {
    564 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    565 	register struct tty *tp = pti->pt_tty;
    566 
    567 	return (tp);
    568 }
    569 
    570 /*ARGSUSED*/
    571 int
    572 ptyioctl(dev, cmd, data, flag, p)
    573 	dev_t dev;
    574 	u_long cmd;
    575 	caddr_t data;
    576 	int flag;
    577 	struct proc *p;
    578 {
    579 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    580 	register struct tty *tp = pti->pt_tty;
    581 	register u_char *cc = tp->t_cc;
    582 	int stop, error;
    583 
    584 	/*
    585 	 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
    586 	 * ttywflush(tp) will hang if there are characters in the outq.
    587 	 */
    588 	if (cmd == TIOCEXT) {
    589 		/*
    590 		 * When the EXTPROC bit is being toggled, we need
    591 		 * to send an TIOCPKT_IOCTL if the packet driver
    592 		 * is turned on.
    593 		 */
    594 		if (*(int *)data) {
    595 			if (pti->pt_flags & PF_PKT) {
    596 				pti->pt_send |= TIOCPKT_IOCTL;
    597 				ptcwakeup(tp, FREAD);
    598 			}
    599 			tp->t_lflag |= EXTPROC;
    600 		} else {
    601 			if ((tp->t_state & EXTPROC) &&
    602 			    (pti->pt_flags & PF_PKT)) {
    603 				pti->pt_send |= TIOCPKT_IOCTL;
    604 				ptcwakeup(tp, FREAD);
    605 			}
    606 			tp->t_lflag &= ~EXTPROC;
    607 		}
    608 		return(0);
    609 	} else
    610 	if (cdevsw[major(dev)].d_open == ptcopen)
    611 		switch (cmd) {
    612 
    613 		case TIOCGPGRP:
    614 			/*
    615 			 * We aviod calling ttioctl on the controller since,
    616 			 * in that case, tp must be the controlling terminal.
    617 			 */
    618 			*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
    619 			return (0);
    620 
    621 		case TIOCPKT:
    622 			if (*(int *)data) {
    623 				if (pti->pt_flags & PF_UCNTL)
    624 					return (EINVAL);
    625 				pti->pt_flags |= PF_PKT;
    626 			} else
    627 				pti->pt_flags &= ~PF_PKT;
    628 			return (0);
    629 
    630 		case TIOCUCNTL:
    631 			if (*(int *)data) {
    632 				if (pti->pt_flags & PF_PKT)
    633 					return (EINVAL);
    634 				pti->pt_flags |= PF_UCNTL;
    635 			} else
    636 				pti->pt_flags &= ~PF_UCNTL;
    637 			return (0);
    638 
    639 		case TIOCREMOTE:
    640 			if (*(int *)data)
    641 				pti->pt_flags |= PF_REMOTE;
    642 			else
    643 				pti->pt_flags &= ~PF_REMOTE;
    644 			ttyflush(tp, FREAD|FWRITE);
    645 			return (0);
    646 
    647 #ifdef COMPAT_OLDTTY
    648 		case TIOCSETP:
    649 		case TIOCSETN:
    650 #endif
    651 		case TIOCSETD:
    652 		case TIOCSETA:
    653 		case TIOCSETAW:
    654 		case TIOCSETAF:
    655 			ndflush(&tp->t_outq, tp->t_outq.c_cc);
    656 			break;
    657 
    658 		case TIOCSIG:
    659 			if (*(unsigned int *)data >= NSIG)
    660 				return(EINVAL);
    661 			if ((tp->t_lflag&NOFLSH) == 0)
    662 				ttyflush(tp, FREAD|FWRITE);
    663 			pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
    664 			if ((*(unsigned int *)data == SIGINFO) &&
    665 			    ((tp->t_lflag&NOKERNINFO) == 0))
    666 				ttyinfo(tp);
    667 			return(0);
    668 		}
    669 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
    670 	if (error < 0)
    671 		 error = ttioctl(tp, cmd, data, flag, p);
    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_OLDTTY
    692 		case TIOCSETP:
    693 		case TIOCSETN:
    694 		case TIOCSETC:
    695 		case TIOCSLTC:
    696 		case TIOCLBIS:
    697 		case TIOCLBIC:
    698 		case TIOCLSET:
    699 #endif
    700 			pti->pt_send |= TIOCPKT_IOCTL;
    701 			ptcwakeup(tp, FREAD);
    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