Home | History | Annotate | Line # | Download | only in kern
tty_pty.c revision 1.54
      1 /*	$NetBSD: tty_pty.c,v 1.54 2000/11/21 03:53:27 enami 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.4 (Berkeley) 2/20/95
     36  */
     37 
     38 /*
     39  * Pseudo-teletype Driver
     40  * (Actually two drivers, requiring two entries in 'cdevsw')
     41  */
     42 
     43 #include "opt_compat_sunos.h"
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/ioctl.h>
     48 #include <sys/proc.h>
     49 #include <sys/tty.h>
     50 #include <sys/file.h>
     51 #include <sys/uio.h>
     52 #include <sys/kernel.h>
     53 #include <sys/vnode.h>
     54 #include <sys/signalvar.h>
     55 #include <sys/uio.h>
     56 #include <sys/conf.h>
     57 #include <sys/poll.h>
     58 #include <sys/malloc.h>
     59 
     60 #define	DEFAULT_NPTYS		16	/* default number of initial ptys */
     61 #define DEFAULT_MAXPTYS		256	/* default maximum number of ptys */
     62 
     63 /* Macros to clear/set/test flags. */
     64 #define	SET(t, f)	(t) |= (f)
     65 #define	CLR(t, f)	(t) &= ~((unsigned)(f))
     66 #define	ISSET(t, f)	((t) & (f))
     67 
     68 #define BUFSIZ 100		/* Chunk size iomoved to/from user */
     69 
     70 /*
     71  * pts == /dev/tty[pqrs]?
     72  * ptc == /dev/pty[pqrs]?
     73  */
     74 struct	pt_softc {
     75 	struct	tty *pt_tty;
     76 	int	pt_flags;
     77 	struct	selinfo pt_selr, pt_selw;
     78 	u_char	pt_send;
     79 	u_char	pt_ucntl;
     80 };
     81 
     82 static struct pt_softc **pt_softc = NULL;	/* pty array */
     83 static int npty = 0;			/* for pstat -t */
     84 static int maxptys = DEFAULT_MAXPTYS;	/* maximum number of ptys (sysctable) */
     85 
     86 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
     87 static struct simplelock pt_softc_mutex = SIMPLELOCK_INITIALIZER;
     88 #endif
     89 
     90 #define	PF_PKT		0x08		/* packet mode */
     91 #define	PF_STOPPED	0x10		/* user told stopped */
     92 #define	PF_REMOTE	0x20		/* remote and flow controlled input */
     93 #define	PF_NOSTOP	0x40
     94 #define PF_UCNTL	0x80		/* user control mode */
     95 
     96 void	ptyattach __P((int));
     97 void	ptcwakeup __P((struct tty *, int));
     98 int	ptcopen __P((dev_t, int, int, struct proc *));
     99 struct tty *ptytty __P((dev_t));
    100 void	ptsstart __P((struct tty *));
    101 int	pty_maxptys __P((int, int));
    102 
    103 static struct pt_softc **ptyarralloc __P((int));
    104 static int check_pty __P((dev_t));
    105 
    106 /*
    107  * Allocate and zero array of nelem elements.
    108  */
    109 static struct pt_softc **
    110 ptyarralloc(nelem)
    111 	int nelem;
    112 {
    113 	struct pt_softc **pt;
    114 	nelem += 10;
    115 	pt = malloc(nelem * sizeof(struct pt_softc *), M_DEVBUF, M_WAITOK);
    116 	memset(pt, '\0', nelem * sizeof(struct pt_softc *));
    117 	return pt;
    118 }
    119 
    120 /*
    121  * Check if the minor is correct and ensure necessary structures
    122  * are properly allocated.
    123  */
    124 static int
    125 check_pty(dev)
    126 	dev_t dev;
    127 {
    128 	struct pt_softc *pti;
    129 
    130 	if (minor(dev) >= npty) {
    131 		struct pt_softc **newpt;
    132 		int newnpty;
    133 
    134 		/* check if the requested pty can be granted */
    135 		if (minor(dev) >= maxptys) {
    136 	    limit_reached:
    137 			tablefull("pty", "increase kern.maxptys");
    138 			return (ENXIO);
    139 		}
    140 
    141 		/*
    142 		 * Now grab the pty array mutex - we need to ensure
    143 		 * that the pty array is consistent while copying it's
    144 		 * content to newly allocated, larger space; we also
    145 		 * need to be safe against pty_maxptys().
    146 		 */
    147 		simple_lock(&pt_softc_mutex);
    148 
    149 		do {
    150 			for(newnpty = npty; newnpty <= minor(dev);
    151 				newnpty *= 2);
    152 
    153 			if (newnpty > maxptys)
    154 				newnpty = maxptys;
    155 
    156 			simple_unlock(&pt_softc_mutex);
    157 			newpt = ptyarralloc(newnpty);
    158 			simple_lock(&pt_softc_mutex);
    159 
    160 			if (maxptys == npty) {
    161 				simple_unlock(&pt_softc_mutex);
    162 				goto limit_reached;
    163 			}
    164 		} while(newnpty > maxptys);
    165 
    166 		/*
    167 		 * If the pty array was not enlarged while we were waiting
    168 		 * for mutex, copy current contents of pt_softc[] to newly
    169 		 * allocated array and start using the new bigger array.
    170 		 */
    171 		if (minor(dev) >= npty) {
    172 			memcpy(newpt, pt_softc, npty*sizeof(struct pt_softc *));
    173 			free(pt_softc, M_DEVBUF);
    174 
    175 			pt_softc = newpt;
    176 			npty = newnpty;
    177 		} else {
    178 			/* was enlarged when waited fot lock, free new space */
    179 			free(newpt, M_DEVBUF);
    180 		}
    181 
    182 		simple_unlock(&pt_softc_mutex);
    183 	}
    184 
    185 	/*
    186 	 * If the entry is not yet allocated, allocate one. The mutex is
    187 	 * needed so that the state of pt_softc[] array is consistant
    188 	 * in case it has been longened above.
    189 	 */
    190 	if (!pt_softc[minor(dev)]) {
    191 		MALLOC(pti, struct pt_softc *, sizeof(struct pt_softc),
    192 			M_DEVBUF, M_WAITOK);
    193 
    194 	 	pti->pt_tty = ttymalloc();
    195 
    196 		simple_lock(&pt_softc_mutex);
    197 
    198 		/*
    199 		 * Check the entry again - it might have been
    200 		 * added while we were waiting for mutex.
    201 		 */
    202 		if (!pt_softc[minor(dev)]) {
    203 			tty_attach(pti->pt_tty);
    204 			pt_softc[minor(dev)] = pti;
    205 		} else {
    206 			ttyfree(pti->pt_tty);
    207 			FREE(pti, M_DEVBUF);
    208 		}
    209 
    210 		simple_unlock(&pt_softc_mutex);
    211 	}
    212 
    213 	return (0);
    214 }
    215 
    216 /*
    217  * Set maxpty in thread-safe way. Returns 0 in case of error, otherwise
    218  * new value of maxptys.
    219  */
    220 int
    221 pty_maxptys(newmax, set)
    222 	int newmax, set;
    223 {
    224 	if (!set)
    225 		return (maxptys);
    226 
    227 	/* the value cannot be set to value lower than current number of ptys */
    228 	if (newmax < npty)
    229 		return (0);
    230 
    231 	/* can proceed immediatelly if bigger than current maximum */
    232 	if (newmax > maxptys) {
    233 		maxptys = newmax;
    234 		return (maxptys);
    235 	}
    236 
    237 	/*
    238 	 * We have to grab the pt_softc lock, so that we would pick correct
    239 	 * value of npty (might be modified in check_pty()).
    240 	 */
    241 	simple_lock(&pt_softc_mutex);
    242 
    243 	if (newmax > npty)
    244 		maxptys = newmax;
    245 
    246 	simple_unlock(&pt_softc_mutex);
    247 
    248 	return (maxptys);
    249 }
    250 
    251 /*
    252  * Establish n (or default if n is 1) ptys in the system.
    253  */
    254 void
    255 ptyattach(n)
    256 	int n;
    257 {
    258 	/* maybe should allow 0 => none? */
    259 	if (n <= 1)
    260 		n = DEFAULT_NPTYS;
    261 	pt_softc = ptyarralloc(n);
    262 	npty = n;
    263 }
    264 
    265 /*ARGSUSED*/
    266 int
    267 ptsopen(dev, flag, devtype, p)
    268 	dev_t dev;
    269 	int flag, devtype;
    270 	struct proc *p;
    271 {
    272 	struct pt_softc *pti;
    273 	struct tty *tp;
    274 	int error;
    275 
    276 	if ((error = check_pty(dev)))
    277 		return (error);
    278 
    279 	pti = pt_softc[minor(dev)];
    280 	tp = pti->pt_tty;
    281 
    282 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
    283 		ttychars(tp);		/* Set up default chars */
    284 		tp->t_iflag = TTYDEF_IFLAG;
    285 		tp->t_oflag = TTYDEF_OFLAG;
    286 		tp->t_lflag = TTYDEF_LFLAG;
    287 		tp->t_cflag = TTYDEF_CFLAG;
    288 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    289 		ttsetwater(tp);		/* would be done in xxparam() */
    290 	} else if (ISSET(tp->t_state, TS_XCLUDE) && p->p_ucred->cr_uid != 0)
    291 		return (EBUSY);
    292 	if (tp->t_oproc)			/* Ctrlr still around. */
    293 		SET(tp->t_state, TS_CARR_ON);
    294 	if (!ISSET(flag, O_NONBLOCK))
    295 		while (!ISSET(tp->t_state, TS_CARR_ON)) {
    296 			tp->t_wopen++;
    297 			error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
    298 			    ttopen, 0);
    299 			tp->t_wopen--;
    300 			if (error)
    301 				return (error);
    302 		}
    303 	error = (*tp->t_linesw->l_open)(dev, tp);
    304 	ptcwakeup(tp, FREAD|FWRITE);
    305 	return (error);
    306 }
    307 
    308 int
    309 ptsclose(dev, flag, mode, p)
    310 	dev_t dev;
    311 	int flag, mode;
    312 	struct proc *p;
    313 {
    314 	struct pt_softc *pti = pt_softc[minor(dev)];
    315 	struct tty *tp = pti->pt_tty;
    316 	int error;
    317 
    318 	error = (*tp->t_linesw->l_close)(tp, flag);
    319 	error |= ttyclose(tp);
    320 	ptcwakeup(tp, FREAD|FWRITE);
    321 	return (error);
    322 }
    323 
    324 int
    325 ptsread(dev, uio, flag)
    326 	dev_t dev;
    327 	struct uio *uio;
    328 	int flag;
    329 {
    330 	struct proc *p = curproc;
    331 	struct pt_softc *pti = pt_softc[minor(dev)];
    332 	struct tty *tp = pti->pt_tty;
    333 	int error = 0;
    334 
    335 again:
    336 	if (pti->pt_flags & PF_REMOTE) {
    337 		while (isbackground(p, tp)) {
    338 			if (sigismasked(p, SIGTTIN) ||
    339 			    p->p_pgrp->pg_jobc == 0 ||
    340 			    p->p_flag & P_PPWAIT)
    341 				return (EIO);
    342 			pgsignal(p->p_pgrp, SIGTTIN, 1);
    343 			error = ttysleep(tp, (caddr_t)&lbolt,
    344 					 TTIPRI | PCATCH, ttybg, 0);
    345 			if (error)
    346 				return (error);
    347 		}
    348 		if (tp->t_canq.c_cc == 0) {
    349 			if (flag & IO_NDELAY)
    350 				return (EWOULDBLOCK);
    351 			error = ttysleep(tp, (caddr_t)&tp->t_canq,
    352 					 TTIPRI | PCATCH, ttyin, 0);
    353 			if (error)
    354 				return (error);
    355 			goto again;
    356 		}
    357 		while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
    358 			if (ureadc(getc(&tp->t_canq), uio) < 0) {
    359 				error = EFAULT;
    360 				break;
    361 			}
    362 		if (tp->t_canq.c_cc == 1)
    363 			(void) getc(&tp->t_canq);
    364 		if (tp->t_canq.c_cc)
    365 			return (error);
    366 	} else
    367 		if (tp->t_oproc)
    368 			error = (*tp->t_linesw->l_read)(tp, uio, flag);
    369 	ptcwakeup(tp, FWRITE);
    370 	return (error);
    371 }
    372 
    373 /*
    374  * Write to pseudo-tty.
    375  * Wakeups of controlling tty will happen
    376  * indirectly, when tty driver calls ptsstart.
    377  */
    378 int
    379 ptswrite(dev, uio, flag)
    380 	dev_t dev;
    381 	struct uio *uio;
    382 	int flag;
    383 {
    384 	struct pt_softc *pti = pt_softc[minor(dev)];
    385 	struct tty *tp = pti->pt_tty;
    386 
    387 	if (tp->t_oproc == 0)
    388 		return (EIO);
    389 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    390 }
    391 
    392 /*
    393  * Start output on pseudo-tty.
    394  * Wake up process polling or sleeping for input from controlling tty.
    395  */
    396 void
    397 ptsstart(tp)
    398 	struct tty *tp;
    399 {
    400 	struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
    401 
    402 	if (ISSET(tp->t_state, TS_TTSTOP))
    403 		return;
    404 	if (pti->pt_flags & PF_STOPPED) {
    405 		pti->pt_flags &= ~PF_STOPPED;
    406 		pti->pt_send = TIOCPKT_START;
    407 	}
    408 	ptcwakeup(tp, FREAD);
    409 }
    410 
    411 void
    412 ptsstop(tp, flush)
    413 	struct tty *tp;
    414 	int flush;
    415 {
    416 	struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
    417 	int flag;
    418 
    419 	/* note: FLUSHREAD and FLUSHWRITE already ok */
    420 	if (flush == 0) {
    421 		flush = TIOCPKT_STOP;
    422 		pti->pt_flags |= PF_STOPPED;
    423 	} else
    424 		pti->pt_flags &= ~PF_STOPPED;
    425 	pti->pt_send |= flush;
    426 	/* change of perspective */
    427 	flag = 0;
    428 	if (flush & FREAD)
    429 		flag |= FWRITE;
    430 	if (flush & FWRITE)
    431 		flag |= FREAD;
    432 	ptcwakeup(tp, flag);
    433 }
    434 
    435 void
    436 ptcwakeup(tp, flag)
    437 	struct tty *tp;
    438 	int flag;
    439 {
    440 	struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
    441 
    442 	if (flag & FREAD) {
    443 		selwakeup(&pti->pt_selr);
    444 		wakeup((caddr_t)&tp->t_outq.c_cf);
    445 	}
    446 	if (flag & FWRITE) {
    447 		selwakeup(&pti->pt_selw);
    448 		wakeup((caddr_t)&tp->t_rawq.c_cf);
    449 	}
    450 }
    451 
    452 /*ARGSUSED*/
    453 int
    454 ptcopen(dev, flag, devtype, p)
    455 	dev_t dev;
    456 	int flag, devtype;
    457 	struct proc *p;
    458 {
    459 	struct pt_softc *pti;
    460 	struct tty *tp;
    461 	int error;
    462 
    463 	if ((error = check_pty(dev)))
    464 		return (error);
    465 
    466 	pti = pt_softc[minor(dev)];
    467 	tp = pti->pt_tty;
    468 
    469 	if (tp->t_oproc)
    470 		return (EIO);
    471 	tp->t_oproc = ptsstart;
    472 	(void)(*tp->t_linesw->l_modem)(tp, 1);
    473 	CLR(tp->t_lflag, EXTPROC);
    474 	pti->pt_flags = 0;
    475 	pti->pt_send = 0;
    476 	pti->pt_ucntl = 0;
    477 	return (0);
    478 }
    479 
    480 /*ARGSUSED*/
    481 int
    482 ptcclose(dev, flag, devtype, p)
    483 	dev_t dev;
    484 	int flag, devtype;
    485 	struct proc *p;
    486 {
    487 	struct pt_softc *pti = pt_softc[minor(dev)];
    488 	struct tty *tp = pti->pt_tty;
    489 
    490 	(void)(*tp->t_linesw->l_modem)(tp, 0);
    491 	CLR(tp->t_state, TS_CARR_ON);
    492 	tp->t_oproc = 0;		/* mark closed */
    493 	return (0);
    494 }
    495 
    496 int
    497 ptcread(dev, uio, flag)
    498 	dev_t dev;
    499 	struct uio *uio;
    500 	int flag;
    501 {
    502 	struct pt_softc *pti = pt_softc[minor(dev)];
    503 	struct tty *tp = pti->pt_tty;
    504 	char buf[BUFSIZ];
    505 	int error = 0, cc;
    506 
    507 	/*
    508 	 * We want to block until the slave
    509 	 * is open, and there's something to read;
    510 	 * but if we lost the slave or we're NBIO,
    511 	 * then return the appropriate error instead.
    512 	 */
    513 	for (;;) {
    514 		if (ISSET(tp->t_state, TS_ISOPEN)) {
    515 			if (pti->pt_flags&PF_PKT && pti->pt_send) {
    516 				error = ureadc((int)pti->pt_send, uio);
    517 				if (error)
    518 					return (error);
    519 				if (pti->pt_send & TIOCPKT_IOCTL) {
    520 					cc = min(uio->uio_resid,
    521 						sizeof(tp->t_termios));
    522 					uiomove((caddr_t) &tp->t_termios,
    523 						cc, uio);
    524 				}
    525 				pti->pt_send = 0;
    526 				return (0);
    527 			}
    528 			if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
    529 				error = ureadc((int)pti->pt_ucntl, uio);
    530 				if (error)
    531 					return (error);
    532 				pti->pt_ucntl = 0;
    533 				return (0);
    534 			}
    535 			if (tp->t_outq.c_cc && !ISSET(tp->t_state, TS_TTSTOP))
    536 				break;
    537 		}
    538 		if (!ISSET(tp->t_state, TS_CARR_ON))
    539 			return (0);	/* EOF */
    540 		if (flag & IO_NDELAY)
    541 			return (EWOULDBLOCK);
    542 		error = tsleep((caddr_t)&tp->t_outq.c_cf, TTIPRI | PCATCH,
    543 			       ttyin, 0);
    544 		if (error)
    545 			return (error);
    546 	}
    547 	if (pti->pt_flags & (PF_PKT|PF_UCNTL))
    548 		error = ureadc(0, uio);
    549 	while (uio->uio_resid > 0 && error == 0) {
    550 		cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
    551 		if (cc <= 0)
    552 			break;
    553 		error = uiomove(buf, cc, uio);
    554 	}
    555 	if (tp->t_outq.c_cc <= tp->t_lowat) {
    556 		if (ISSET(tp->t_state, TS_ASLEEP)) {
    557 			CLR(tp->t_state, TS_ASLEEP);
    558 			wakeup((caddr_t)&tp->t_outq);
    559 		}
    560 		selwakeup(&tp->t_wsel);
    561 	}
    562 	return (error);
    563 }
    564 
    565 
    566 int
    567 ptcwrite(dev, uio, flag)
    568 	dev_t dev;
    569 	struct uio *uio;
    570 	int flag;
    571 {
    572 	struct pt_softc *pti = pt_softc[minor(dev)];
    573 	struct tty *tp = pti->pt_tty;
    574 	u_char *cp = NULL;
    575 	int cc = 0;
    576 	u_char locbuf[BUFSIZ];
    577 	int cnt = 0;
    578 	int error = 0;
    579 
    580 again:
    581 	if (!ISSET(tp->t_state, TS_ISOPEN))
    582 		goto block;
    583 	if (pti->pt_flags & PF_REMOTE) {
    584 		if (tp->t_canq.c_cc)
    585 			goto block;
    586 		while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) {
    587 			if (cc == 0) {
    588 				cc = min(uio->uio_resid, BUFSIZ);
    589 				cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
    590 				cp = locbuf;
    591 				error = uiomove((caddr_t)cp, cc, uio);
    592 				if (error)
    593 					return (error);
    594 				/* check again for safety */
    595 				if (!ISSET(tp->t_state, TS_ISOPEN))
    596 					return (EIO);
    597 			}
    598 			if (cc)
    599 				(void) b_to_q((char *)cp, cc, &tp->t_canq);
    600 			cc = 0;
    601 		}
    602 		(void) putc(0, &tp->t_canq);
    603 		ttwakeup(tp);
    604 		wakeup((caddr_t)&tp->t_canq);
    605 		return (0);
    606 	}
    607 	while (uio->uio_resid > 0) {
    608 		if (cc == 0) {
    609 			cc = min(uio->uio_resid, BUFSIZ);
    610 			cp = locbuf;
    611 			error = uiomove((caddr_t)cp, cc, uio);
    612 			if (error)
    613 				return (error);
    614 			/* check again for safety */
    615 			if (!ISSET(tp->t_state, TS_ISOPEN))
    616 				return (EIO);
    617 		}
    618 		while (cc > 0) {
    619 			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
    620 			   (tp->t_canq.c_cc > 0 || !ISSET(tp->t_iflag, ICANON))) {
    621 				wakeup((caddr_t)&tp->t_rawq);
    622 				goto block;
    623 			}
    624 			(*tp->t_linesw->l_rint)(*cp++, tp);
    625 			cnt++;
    626 			cc--;
    627 		}
    628 		cc = 0;
    629 	}
    630 	return (0);
    631 block:
    632 	/*
    633 	 * Come here to wait for slave to open, for space
    634 	 * in outq, or space in rawq.
    635 	 */
    636 	if (!ISSET(tp->t_state, TS_CARR_ON))
    637 		return (EIO);
    638 	if (flag & IO_NDELAY) {
    639 		/* adjust for data copied in but not written */
    640 		uio->uio_resid += cc;
    641 		if (cnt == 0)
    642 			return (EWOULDBLOCK);
    643 		return (0);
    644 	}
    645 	error = tsleep((caddr_t)&tp->t_rawq.c_cf, TTOPRI | PCATCH,
    646 		       ttyout, 0);
    647 	if (error) {
    648 		/* adjust for data copied in but not written */
    649 		uio->uio_resid += cc;
    650 		return (error);
    651 	}
    652 	goto again;
    653 }
    654 
    655 int
    656 ptcpoll(dev, events, p)
    657 	dev_t dev;
    658 	int events;
    659 	struct proc *p;
    660 {
    661 	struct pt_softc *pti = pt_softc[minor(dev)];
    662 	struct tty *tp = pti->pt_tty;
    663 	int revents = 0;
    664 	int s = splsoftclock();
    665 
    666 	if (events & (POLLIN | POLLRDNORM))
    667 		if (ISSET(tp->t_state, TS_ISOPEN) &&
    668 		    ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
    669 		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
    670 		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
    671 			revents |= events & (POLLIN | POLLRDNORM);
    672 
    673 	if (events & (POLLOUT | POLLWRNORM))
    674 		if (ISSET(tp->t_state, TS_ISOPEN) &&
    675 		    ((pti->pt_flags & PF_REMOTE) ?
    676 		     (tp->t_canq.c_cc == 0) :
    677 		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
    678 		      (tp->t_canq.c_cc == 0 && ISSET(tp->t_iflag, ICANON)))))
    679 			revents |= events & (POLLOUT | POLLWRNORM);
    680 
    681 	if (events & POLLHUP)
    682 		if (!ISSET(tp->t_state, TS_CARR_ON))
    683 			revents |= POLLHUP;
    684 
    685 	if (revents == 0) {
    686 		if (events & (POLLIN | POLLHUP | POLLRDNORM))
    687 			selrecord(p, &pti->pt_selr);
    688 
    689 		if (events & (POLLOUT | POLLWRNORM))
    690 			selrecord(p, &pti->pt_selw);
    691 	}
    692 
    693 	splx(s);
    694 	return (revents);
    695 }
    696 
    697 
    698 struct tty *
    699 ptytty(dev)
    700 	dev_t dev;
    701 {
    702 	struct pt_softc *pti = pt_softc[minor(dev)];
    703 	struct tty *tp = pti->pt_tty;
    704 
    705 	return (tp);
    706 }
    707 
    708 /*ARGSUSED*/
    709 int
    710 ptyioctl(dev, cmd, data, flag, p)
    711 	dev_t dev;
    712 	u_long cmd;
    713 	caddr_t data;
    714 	int flag;
    715 	struct proc *p;
    716 {
    717 	struct pt_softc *pti = pt_softc[minor(dev)];
    718 	struct tty *tp = pti->pt_tty;
    719 	u_char *cc = tp->t_cc;
    720 	int stop, error, sig;
    721 
    722 	/*
    723 	 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
    724 	 * ttywflush(tp) will hang if there are characters in the outq.
    725 	 */
    726 	if (cmd == TIOCEXT) {
    727 		/*
    728 		 * When the EXTPROC bit is being toggled, we need
    729 		 * to send an TIOCPKT_IOCTL if the packet driver
    730 		 * is turned on.
    731 		 */
    732 		if (*(int *)data) {
    733 			if (pti->pt_flags & PF_PKT) {
    734 				pti->pt_send |= TIOCPKT_IOCTL;
    735 				ptcwakeup(tp, FREAD);
    736 			}
    737 			SET(tp->t_lflag, EXTPROC);
    738 		} else {
    739 			if (ISSET(tp->t_lflag, EXTPROC) &&
    740 			    (pti->pt_flags & PF_PKT)) {
    741 				pti->pt_send |= TIOCPKT_IOCTL;
    742 				ptcwakeup(tp, FREAD);
    743 			}
    744 			CLR(tp->t_lflag, EXTPROC);
    745 		}
    746 		return(0);
    747 	} else
    748 	if (cdevsw[major(dev)].d_open == ptcopen)
    749 		switch (cmd) {
    750 
    751 		case TIOCGPGRP:
    752 #ifdef COMPAT_SUNOS
    753 			{
    754 			/*
    755 			 * I'm not sure about SunOS TIOCGPGRP semantics
    756 			 * on PTYs, but it's something like this:
    757 			 */
    758 			extern struct emul emul_sunos;
    759 			if (p->p_emul == &emul_sunos && tp->t_pgrp == 0)
    760 				return (EIO);
    761 			*(int *)data = tp->t_pgrp->pg_id;
    762 			return (0);
    763 			}
    764 #endif
    765 			/*
    766 			 * We avoid calling ttioctl on the controller since,
    767 			 * in that case, tp must be the controlling terminal.
    768 			 */
    769 			*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
    770 			return (0);
    771 
    772 		case TIOCPKT:
    773 			if (*(int *)data) {
    774 				if (pti->pt_flags & PF_UCNTL)
    775 					return (EINVAL);
    776 				pti->pt_flags |= PF_PKT;
    777 			} else
    778 				pti->pt_flags &= ~PF_PKT;
    779 			return (0);
    780 
    781 		case TIOCUCNTL:
    782 			if (*(int *)data) {
    783 				if (pti->pt_flags & PF_PKT)
    784 					return (EINVAL);
    785 				pti->pt_flags |= PF_UCNTL;
    786 			} else
    787 				pti->pt_flags &= ~PF_UCNTL;
    788 			return (0);
    789 
    790 		case TIOCREMOTE:
    791 			if (*(int *)data)
    792 				pti->pt_flags |= PF_REMOTE;
    793 			else
    794 				pti->pt_flags &= ~PF_REMOTE;
    795 			ttyflush(tp, FREAD|FWRITE);
    796 			return (0);
    797 
    798 #ifdef COMPAT_OLDTTY
    799 		case TIOCSETP:
    800 		case TIOCSETN:
    801 #endif
    802 		case TIOCSETD:
    803 		case TIOCSETA:
    804 		case TIOCSETAW:
    805 		case TIOCSETAF:
    806 			ndflush(&tp->t_outq, tp->t_outq.c_cc);
    807 			break;
    808 
    809 		case TIOCSIG:
    810 			sig = (int)(long)*(caddr_t *)data;
    811 			if (sig <= 0 || sig >= NSIG)
    812 				return (EINVAL);
    813 			if (!ISSET(tp->t_lflag, NOFLSH))
    814 				ttyflush(tp, FREAD|FWRITE);
    815 			pgsignal(tp->t_pgrp, sig, 1);
    816 			if ((sig == SIGINFO) &&
    817 			    (!ISSET(tp->t_lflag, NOKERNINFO)))
    818 				ttyinfo(tp);
    819 			return(0);
    820 		}
    821 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
    822 	if (error < 0)
    823 		 error = ttioctl(tp, cmd, data, flag, p);
    824 	if (error < 0) {
    825 		if (pti->pt_flags & PF_UCNTL &&
    826 		    (cmd & ~0xff) == UIOCCMD(0)) {
    827 			if (cmd & 0xff) {
    828 				pti->pt_ucntl = (u_char)cmd;
    829 				ptcwakeup(tp, FREAD);
    830 			}
    831 			return (0);
    832 		}
    833 		error = ENOTTY;
    834 	}
    835 	/*
    836 	 * If external processing and packet mode send ioctl packet.
    837 	 */
    838 	if (ISSET(tp->t_lflag, EXTPROC) && (pti->pt_flags & PF_PKT)) {
    839 		switch(cmd) {
    840 		case TIOCSETA:
    841 		case TIOCSETAW:
    842 		case TIOCSETAF:
    843 #ifdef COMPAT_OLDTTY
    844 		case TIOCSETP:
    845 		case TIOCSETN:
    846 		case TIOCSETC:
    847 		case TIOCSLTC:
    848 		case TIOCLBIS:
    849 		case TIOCLBIC:
    850 		case TIOCLSET:
    851 #endif
    852 			pti->pt_send |= TIOCPKT_IOCTL;
    853 			ptcwakeup(tp, FREAD);
    854 		default:
    855 			break;
    856 		}
    857 	}
    858 	stop = ISSET(tp->t_iflag, IXON) && CCEQ(cc[VSTOP], CTRL('s'))
    859 		&& CCEQ(cc[VSTART], CTRL('q'));
    860 	if (pti->pt_flags & PF_NOSTOP) {
    861 		if (stop) {
    862 			pti->pt_send &= ~TIOCPKT_NOSTOP;
    863 			pti->pt_send |= TIOCPKT_DOSTOP;
    864 			pti->pt_flags &= ~PF_NOSTOP;
    865 			ptcwakeup(tp, FREAD);
    866 		}
    867 	} else {
    868 		if (!stop) {
    869 			pti->pt_send &= ~TIOCPKT_DOSTOP;
    870 			pti->pt_send |= TIOCPKT_NOSTOP;
    871 			pti->pt_flags |= PF_NOSTOP;
    872 			ptcwakeup(tp, FREAD);
    873 		}
    874 	}
    875 	return (error);
    876 }
    877