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