Home | History | Annotate | Line # | Download | only in kern
tty_pty.c revision 1.69
      1 /*	$NetBSD: tty_pty.c,v 1.69 2003/06/28 14:21:57 darrenr 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 <sys/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: tty_pty.c,v 1.69 2003/06/28 14:21:57 darrenr Exp $");
     45 
     46 #include "opt_compat_sunos.h"
     47 
     48 #include <sys/param.h>
     49 #include <sys/systm.h>
     50 #include <sys/ioctl.h>
     51 #include <sys/proc.h>
     52 #include <sys/tty.h>
     53 #include <sys/file.h>
     54 #include <sys/uio.h>
     55 #include <sys/kernel.h>
     56 #include <sys/vnode.h>
     57 #include <sys/signalvar.h>
     58 #include <sys/uio.h>
     59 #include <sys/conf.h>
     60 #include <sys/poll.h>
     61 #include <sys/malloc.h>
     62 
     63 #define	DEFAULT_NPTYS		16	/* default number of initial ptys */
     64 #define DEFAULT_MAXPTYS		992	/* default maximum number of ptys */
     65 
     66 /* Macros to clear/set/test flags. */
     67 #define	SET(t, f)	(t) |= (f)
     68 #define	CLR(t, f)	(t) &= ~((unsigned)(f))
     69 #define	ISSET(t, f)	((t) & (f))
     70 
     71 #define BUFSIZ 100		/* Chunk size iomoved to/from user */
     72 
     73 /*
     74  * pts == /dev/tty[pqrs]?
     75  * ptc == /dev/pty[pqrs]?
     76  */
     77 struct	pt_softc {
     78 	struct	tty *pt_tty;
     79 	int	pt_flags;
     80 	struct	selinfo pt_selr, pt_selw;
     81 	u_char	pt_send;
     82 	u_char	pt_ucntl;
     83 };
     84 
     85 static struct pt_softc **pt_softc = NULL;	/* pty array */
     86 static int npty = 0;			/* for pstat -t */
     87 static int maxptys = DEFAULT_MAXPTYS;	/* maximum number of ptys (sysctable) */
     88 static struct simplelock pt_softc_mutex = SIMPLELOCK_INITIALIZER;
     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 void	ptsstart __P((struct tty *));
     99 int	pty_maxptys __P((int, int));
    100 
    101 static struct pt_softc **ptyarralloc __P((int));
    102 static int check_pty __P((dev_t));
    103 
    104 dev_type_open(ptcopen);
    105 dev_type_close(ptcclose);
    106 dev_type_read(ptcread);
    107 dev_type_write(ptcwrite);
    108 dev_type_poll(ptcpoll);
    109 dev_type_kqfilter(ptckqfilter);
    110 
    111 dev_type_open(ptsopen);
    112 dev_type_close(ptsclose);
    113 dev_type_read(ptsread);
    114 dev_type_write(ptswrite);
    115 dev_type_stop(ptsstop);
    116 dev_type_poll(ptspoll);
    117 
    118 dev_type_ioctl(ptyioctl);
    119 dev_type_tty(ptytty);
    120 
    121 const struct cdevsw ptc_cdevsw = {
    122 	ptcopen, ptcclose, ptcread, ptcwrite, ptyioctl,
    123 	nullstop, ptytty, ptcpoll, nommap, ptckqfilter, D_TTY
    124 };
    125 
    126 const struct cdevsw pts_cdevsw = {
    127 	ptsopen, ptsclose, ptsread, ptswrite, ptyioctl,
    128 	ptsstop, ptytty, ptspoll, nommap, ttykqfilter, D_TTY
    129 };
    130 
    131 #if defined(pmax)
    132 const struct cdevsw ptc_ultrix_cdevsw = {
    133 	ptcopen, ptcclose, ptcread, ptcwrite, ptyioctl,
    134 	nullstop, ptytty, ptcpoll, nommap, ptckqfilter, D_TTY
    135 };
    136 
    137 const struct cdevsw pts_ultrix_cdevsw = {
    138 	ptsopen, ptsclose, ptsread, ptswrite, ptyioctl,
    139 	ptsstop, ptytty, ptspoll, nommap, ttykqfilter, D_TTY
    140 };
    141 #endif /* defined(pmax) */
    142 
    143 /*
    144  * Allocate and zero array of nelem elements.
    145  */
    146 static struct pt_softc **
    147 ptyarralloc(nelem)
    148 	int nelem;
    149 {
    150 	struct pt_softc **pt;
    151 	nelem += 10;
    152 	pt = malloc(nelem * sizeof(struct pt_softc *), M_DEVBUF, M_WAITOK);
    153 	memset(pt, '\0', nelem * sizeof(struct pt_softc *));
    154 	return pt;
    155 }
    156 
    157 /*
    158  * Check if the minor is correct and ensure necessary structures
    159  * are properly allocated.
    160  */
    161 static int
    162 check_pty(dev)
    163 	dev_t dev;
    164 {
    165 	struct pt_softc *pti;
    166 
    167 	if (minor(dev) >= npty) {
    168 		struct pt_softc **newpt;
    169 		int newnpty;
    170 
    171 		/* check if the requested pty can be granted */
    172 		if (minor(dev) >= maxptys) {
    173 	    limit_reached:
    174 			tablefull("pty", "increase kern.maxptys");
    175 			return (ENXIO);
    176 		}
    177 
    178 		/*
    179 		 * Now grab the pty array mutex - we need to ensure
    180 		 * that the pty array is consistent while copying it's
    181 		 * content to newly allocated, larger space; we also
    182 		 * need to be safe against pty_maxptys().
    183 		 */
    184 		simple_lock(&pt_softc_mutex);
    185 
    186 		do {
    187 			for(newnpty = npty; newnpty <= minor(dev);
    188 				newnpty *= 2);
    189 
    190 			if (newnpty > maxptys)
    191 				newnpty = maxptys;
    192 
    193 			simple_unlock(&pt_softc_mutex);
    194 			newpt = ptyarralloc(newnpty);
    195 			simple_lock(&pt_softc_mutex);
    196 
    197 			if (maxptys == npty) {
    198 				simple_unlock(&pt_softc_mutex);
    199 				goto limit_reached;
    200 			}
    201 		} while(newnpty > maxptys);
    202 
    203 		/*
    204 		 * If the pty array was not enlarged while we were waiting
    205 		 * for mutex, copy current contents of pt_softc[] to newly
    206 		 * allocated array and start using the new bigger array.
    207 		 */
    208 		if (minor(dev) >= npty) {
    209 			memcpy(newpt, pt_softc, npty*sizeof(struct pt_softc *));
    210 			free(pt_softc, M_DEVBUF);
    211 
    212 			pt_softc = newpt;
    213 			npty = newnpty;
    214 		} else {
    215 			/* was enlarged when waited fot lock, free new space */
    216 			free(newpt, M_DEVBUF);
    217 		}
    218 
    219 		simple_unlock(&pt_softc_mutex);
    220 	}
    221 
    222 	/*
    223 	 * If the entry is not yet allocated, allocate one. The mutex is
    224 	 * needed so that the state of pt_softc[] array is consistant
    225 	 * in case it has been longened above.
    226 	 */
    227 	if (!pt_softc[minor(dev)]) {
    228 		MALLOC(pti, struct pt_softc *, sizeof(struct pt_softc),
    229 			M_DEVBUF, M_WAITOK);
    230 		memset(pti, 0, sizeof(struct pt_softc));
    231 
    232 	 	pti->pt_tty = ttymalloc();
    233 
    234 		simple_lock(&pt_softc_mutex);
    235 
    236 		/*
    237 		 * Check the entry again - it might have been
    238 		 * added while we were waiting for mutex.
    239 		 */
    240 		if (!pt_softc[minor(dev)]) {
    241 			tty_attach(pti->pt_tty);
    242 			pt_softc[minor(dev)] = pti;
    243 		} else {
    244 			ttyfree(pti->pt_tty);
    245 			FREE(pti, M_DEVBUF);
    246 		}
    247 
    248 		simple_unlock(&pt_softc_mutex);
    249 	}
    250 
    251 	return (0);
    252 }
    253 
    254 /*
    255  * Set maxpty in thread-safe way. Returns 0 in case of error, otherwise
    256  * new value of maxptys.
    257  */
    258 int
    259 pty_maxptys(newmax, set)
    260 	int newmax, set;
    261 {
    262 	if (!set)
    263 		return (maxptys);
    264 
    265 	/* the value cannot be set to value lower than current number of ptys */
    266 	if (newmax < npty)
    267 		return (0);
    268 
    269 	/* can proceed immediatelly if bigger than current maximum */
    270 	if (newmax > maxptys) {
    271 		maxptys = newmax;
    272 		return (maxptys);
    273 	}
    274 
    275 	/*
    276 	 * We have to grab the pt_softc lock, so that we would pick correct
    277 	 * value of npty (might be modified in check_pty()).
    278 	 */
    279 	simple_lock(&pt_softc_mutex);
    280 
    281 	if (newmax > npty)
    282 		maxptys = newmax;
    283 
    284 	simple_unlock(&pt_softc_mutex);
    285 
    286 	return (maxptys);
    287 }
    288 
    289 /*
    290  * Establish n (or default if n is 1) ptys in the system.
    291  */
    292 void
    293 ptyattach(n)
    294 	int n;
    295 {
    296 	/* maybe should allow 0 => none? */
    297 	if (n <= 1)
    298 		n = DEFAULT_NPTYS;
    299 	pt_softc = ptyarralloc(n);
    300 	npty = n;
    301 }
    302 
    303 /*ARGSUSED*/
    304 int
    305 ptsopen(dev, flag, devtype, l)
    306 	dev_t dev;
    307 	int flag, devtype;
    308 	struct lwp *l;
    309 {
    310 	struct proc *p = l->l_proc;
    311 	struct pt_softc *pti;
    312 	struct tty *tp;
    313 	int error;
    314 
    315 	if ((error = check_pty(dev)))
    316 		return (error);
    317 
    318 	pti = pt_softc[minor(dev)];
    319 	tp = pti->pt_tty;
    320 
    321 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
    322 		ttychars(tp);		/* Set up default chars */
    323 		tp->t_iflag = TTYDEF_IFLAG;
    324 		tp->t_oflag = TTYDEF_OFLAG;
    325 		tp->t_lflag = TTYDEF_LFLAG;
    326 		tp->t_cflag = TTYDEF_CFLAG;
    327 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    328 		ttsetwater(tp);		/* would be done in xxparam() */
    329 	} else if (ISSET(tp->t_state, TS_XCLUDE) && p->p_ucred->cr_uid != 0)
    330 		return (EBUSY);
    331 	if (tp->t_oproc)			/* Ctrlr still around. */
    332 		SET(tp->t_state, TS_CARR_ON);
    333 	if (!ISSET(flag, O_NONBLOCK)) {
    334 		TTY_LOCK(tp);
    335 		while (!ISSET(tp->t_state, TS_CARR_ON)) {
    336 			tp->t_wopen++;
    337 			error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
    338 			    ttopen, 0);
    339 			tp->t_wopen--;
    340 			if (error) {
    341 				TTY_UNLOCK(tp);
    342 				return (error);
    343 			}
    344 		}
    345 		TTY_UNLOCK(tp);
    346 	}
    347 	error = (*tp->t_linesw->l_open)(dev, tp);
    348 	ptcwakeup(tp, FREAD|FWRITE);
    349 	return (error);
    350 }
    351 
    352 int
    353 ptsclose(dev, flag, mode, l)
    354 	dev_t dev;
    355 	int flag, mode;
    356 	struct lwp *l;
    357 {
    358 	struct pt_softc *pti = pt_softc[minor(dev)];
    359 	struct tty *tp = pti->pt_tty;
    360 	int error;
    361 
    362 	error = (*tp->t_linesw->l_close)(tp, flag);
    363 	error |= ttyclose(tp);
    364 	ptcwakeup(tp, FREAD|FWRITE);
    365 	return (error);
    366 }
    367 
    368 int
    369 ptsread(dev, uio, flag)
    370 	dev_t dev;
    371 	struct uio *uio;
    372 	int flag;
    373 {
    374 	struct proc *p = curproc;
    375 	struct pt_softc *pti = pt_softc[minor(dev)];
    376 	struct tty *tp = pti->pt_tty;
    377 	int error = 0;
    378 	int cc;
    379 
    380 again:
    381 	if (pti->pt_flags & PF_REMOTE) {
    382 		while (isbackground(p, tp)) {
    383 			if (sigismasked(p, SIGTTIN) ||
    384 			    p->p_pgrp->pg_jobc == 0 ||
    385 			    p->p_flag & P_PPWAIT)
    386 				return (EIO);
    387 			pgsignal(p->p_pgrp, SIGTTIN, 1);
    388 			TTY_LOCK(tp);
    389 			error = ttysleep(tp, (caddr_t)&lbolt,
    390 					 TTIPRI | PCATCH | PNORELOCK, ttybg, 0);
    391 			if (error)
    392 				return (error);
    393 		}
    394 		TTY_LOCK(tp);
    395 		if (tp->t_canq.c_cc == 0) {
    396 			if (flag & IO_NDELAY) {
    397 				TTY_UNLOCK(tp);
    398 				return (EWOULDBLOCK);
    399 			}
    400 			error = ttysleep(tp, (caddr_t)&tp->t_canq,
    401 					 TTIPRI | PCATCH | PNORELOCK, ttyin, 0);
    402 			if (error)
    403 				return (error);
    404 			goto again;
    405 		}
    406 		while(error == 0 && tp->t_canq.c_cc > 1 && uio->uio_resid > 0) {
    407 			TTY_UNLOCK(tp);
    408 			error = ureadc(getc(&tp->t_canq), uio);
    409 			TTY_LOCK(tp);
    410 			/* Re-check terminal state here? */
    411 		}
    412 		if (tp->t_canq.c_cc == 1)
    413 			(void) getc(&tp->t_canq);
    414 		cc = tp->t_canq.c_cc;
    415 		TTY_UNLOCK(tp);
    416 		if (cc)
    417 			return (error);
    418 	} else
    419 		if (tp->t_oproc)
    420 			error = (*tp->t_linesw->l_read)(tp, uio, flag);
    421 	ptcwakeup(tp, FWRITE);
    422 	return (error);
    423 }
    424 
    425 /*
    426  * Write to pseudo-tty.
    427  * Wakeups of controlling tty will happen
    428  * indirectly, when tty driver calls ptsstart.
    429  */
    430 int
    431 ptswrite(dev, uio, flag)
    432 	dev_t dev;
    433 	struct uio *uio;
    434 	int flag;
    435 {
    436 	struct pt_softc *pti = pt_softc[minor(dev)];
    437 	struct tty *tp = pti->pt_tty;
    438 
    439 	if (tp->t_oproc == 0)
    440 		return (EIO);
    441 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    442 }
    443 
    444 /*
    445  * Poll pseudo-tty.
    446  */
    447 int
    448 ptspoll(dev, events, l)
    449 	dev_t dev;
    450 	int events;
    451 	struct lwp *l;
    452 {
    453 	struct pt_softc *pti = pt_softc[minor(dev)];
    454 	struct tty *tp = pti->pt_tty;
    455 
    456 	if (tp->t_oproc == 0)
    457 		return (EIO);
    458 
    459 	return ((*tp->t_linesw->l_poll)(tp, events, l));
    460 }
    461 
    462 /*
    463  * Start output on pseudo-tty.
    464  * Wake up process polling or sleeping for input from controlling tty.
    465  * Called with tty slock held.
    466  */
    467 void
    468 ptsstart(tp)
    469 	struct tty *tp;
    470 {
    471 	struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
    472 
    473 	if (ISSET(tp->t_state, TS_TTSTOP))
    474 		return;
    475 	if (pti->pt_flags & PF_STOPPED) {
    476 		pti->pt_flags &= ~PF_STOPPED;
    477 		pti->pt_send = TIOCPKT_START;
    478 	}
    479 
    480 	selnotify(&pti->pt_selr, 0);
    481 	wakeup((caddr_t)&tp->t_outq.c_cf);
    482 }
    483 
    484 /*
    485  * Stop output.
    486  * Called with tty slock held.
    487  */
    488 void
    489 ptsstop(tp, flush)
    490 	struct tty *tp;
    491 	int flush;
    492 {
    493 	struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
    494 
    495 	/* note: FLUSHREAD and FLUSHWRITE already ok */
    496 	if (flush == 0) {
    497 		flush = TIOCPKT_STOP;
    498 		pti->pt_flags |= PF_STOPPED;
    499 	} else
    500 		pti->pt_flags &= ~PF_STOPPED;
    501 	pti->pt_send |= flush;
    502 
    503 	/* change of perspective */
    504 	if (flush & FREAD) {
    505 		selnotify(&pti->pt_selw, 0);
    506 		wakeup((caddr_t)&tp->t_rawq.c_cf);
    507 	}
    508 	if (flush & FWRITE) {
    509 		selnotify(&pti->pt_selr, 0);
    510 		wakeup((caddr_t)&tp->t_outq.c_cf);
    511 	}
    512 }
    513 
    514 void
    515 ptcwakeup(tp, flag)
    516 	struct tty *tp;
    517 	int flag;
    518 {
    519 	struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
    520 
    521 	TTY_LOCK(tp);
    522 	if (flag & FREAD) {
    523 		selnotify(&pti->pt_selr, 0);
    524 		wakeup((caddr_t)&tp->t_outq.c_cf);
    525 	}
    526 	if (flag & FWRITE) {
    527 		selnotify(&pti->pt_selw, 0);
    528 		wakeup((caddr_t)&tp->t_rawq.c_cf);
    529 	}
    530 	TTY_UNLOCK(tp);
    531 }
    532 
    533 /*ARGSUSED*/
    534 int
    535 ptcopen(dev, flag, devtype, l)
    536 	dev_t dev;
    537 	int flag, devtype;
    538 	struct lwp *l;
    539 {
    540 	struct pt_softc *pti;
    541 	struct tty *tp;
    542 	int error;
    543 
    544 	if ((error = check_pty(dev)))
    545 		return (error);
    546 
    547 	pti = pt_softc[minor(dev)];
    548 	tp = pti->pt_tty;
    549 
    550 	if (tp->t_oproc)
    551 		return (EIO);
    552 	tp->t_oproc = ptsstart;
    553 	(void)(*tp->t_linesw->l_modem)(tp, 1);
    554 	CLR(tp->t_lflag, EXTPROC);
    555 	pti->pt_flags = 0;
    556 	pti->pt_send = 0;
    557 	pti->pt_ucntl = 0;
    558 	return (0);
    559 }
    560 
    561 /*ARGSUSED*/
    562 int
    563 ptcclose(dev, flag, devtype, l)
    564 	dev_t dev;
    565 	int flag, devtype;
    566 	struct lwp *l;
    567 {
    568 	struct pt_softc *pti = pt_softc[minor(dev)];
    569 	struct tty *tp = pti->pt_tty;
    570 
    571 	(void)(*tp->t_linesw->l_modem)(tp, 0);
    572 	CLR(tp->t_state, TS_CARR_ON);
    573 	tp->t_oproc = 0;		/* mark closed */
    574 	return (0);
    575 }
    576 
    577 int
    578 ptcread(dev, uio, flag)
    579 	dev_t dev;
    580 	struct uio *uio;
    581 	int flag;
    582 {
    583 	struct pt_softc *pti = pt_softc[minor(dev)];
    584 	struct tty *tp = pti->pt_tty;
    585 	u_char buf[BUFSIZ];
    586 	int error = 0, cc;
    587 
    588 	/*
    589 	 * We want to block until the slave
    590 	 * is open, and there's something to read;
    591 	 * but if we lost the slave or we're NBIO,
    592 	 * then return the appropriate error instead.
    593 	 */
    594 	TTY_LOCK(tp);
    595 	for (;;) {
    596 		if (ISSET(tp->t_state, TS_ISOPEN)) {
    597 			if (pti->pt_flags&PF_PKT && pti->pt_send) {
    598 				TTY_UNLOCK(tp);
    599 				error = ureadc((int)pti->pt_send, uio);
    600 				if (error)
    601 					return (error);
    602 				/*
    603 				 * Since we don't have the tty locked, there's
    604 				 * a risk of messing up `t_termios'. This is
    605 				 * relevant only if the tty got closed and then
    606 				 * opened again while we were out uiomoving.
    607 				 */
    608 				if (pti->pt_send & TIOCPKT_IOCTL) {
    609 					cc = min(uio->uio_resid,
    610 						sizeof(tp->t_termios));
    611 					uiomove((caddr_t) &tp->t_termios,
    612 						cc, uio);
    613 				}
    614 				pti->pt_send = 0;
    615 				return (0);
    616 			}
    617 			if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
    618 				TTY_UNLOCK(tp);
    619 				error = ureadc((int)pti->pt_ucntl, uio);
    620 				if (error)
    621 					return (error);
    622 				pti->pt_ucntl = 0;
    623 				return (0);
    624 			}
    625 			if (tp->t_outq.c_cc && !ISSET(tp->t_state, TS_TTSTOP))
    626 				break;
    627 		}
    628 		if (!ISSET(tp->t_state, TS_CARR_ON)) {
    629 			error = 0;	/* EOF */
    630 			goto out;
    631 		}
    632 		if (flag & IO_NDELAY) {
    633 			error = EWOULDBLOCK;
    634 			goto out;
    635 		}
    636 		error = ltsleep((caddr_t)&tp->t_outq.c_cf, TTIPRI | PCATCH,
    637 				ttyin, 0, &tp->t_slock);
    638 		if (error)
    639 			goto out;
    640 	}
    641 
    642 	if (pti->pt_flags & (PF_PKT|PF_UCNTL)) {
    643 		TTY_UNLOCK(tp);
    644 		error = ureadc(0, uio);
    645 		TTY_LOCK(tp);
    646 		if (error == 0 && !ISSET(tp->t_state, TS_ISOPEN))
    647 			error = EIO;
    648 	}
    649 	while (uio->uio_resid > 0 && error == 0) {
    650 		cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
    651 		if (cc <= 0)
    652 			break;
    653 		TTY_UNLOCK(tp);
    654 		error = uiomove(buf, cc, uio);
    655 		TTY_LOCK(tp);
    656 		if (error == 0 && !ISSET(tp->t_state, TS_ISOPEN))
    657 			error = EIO;
    658 	}
    659 
    660 	if (tp->t_outq.c_cc <= tp->t_lowat) {
    661 		if (ISSET(tp->t_state, TS_ASLEEP)) {
    662 			CLR(tp->t_state, TS_ASLEEP);
    663 			wakeup((caddr_t)&tp->t_outq);
    664 		}
    665 		selnotify(&tp->t_wsel, 0);
    666 	}
    667 out:
    668 	TTY_UNLOCK(tp);
    669 	return (error);
    670 }
    671 
    672 
    673 int
    674 ptcwrite(dev, uio, flag)
    675 	dev_t dev;
    676 	struct uio *uio;
    677 	int flag;
    678 {
    679 	struct pt_softc *pti = pt_softc[minor(dev)];
    680 	struct tty *tp = pti->pt_tty;
    681 	u_char *cp = NULL;
    682 	int cc = 0;
    683 	u_char locbuf[BUFSIZ];
    684 	int cnt = 0;
    685 	int error = 0;
    686 
    687 again:
    688 	TTY_LOCK(tp);
    689 	if (!ISSET(tp->t_state, TS_ISOPEN))
    690 		goto block;
    691 	if (pti->pt_flags & PF_REMOTE) {
    692 		if (tp->t_canq.c_cc)
    693 			goto block;
    694 		while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) {
    695 			if (cc == 0) {
    696 				cc = min(uio->uio_resid, BUFSIZ);
    697 				cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
    698 				cp = locbuf;
    699 				TTY_UNLOCK(tp);
    700 				error = uiomove((caddr_t)cp, cc, uio);
    701 				if (error)
    702 					return (error);
    703 				TTY_LOCK(tp);
    704 				/* check again for safety */
    705 				if (!ISSET(tp->t_state, TS_ISOPEN)) {
    706 					error = EIO;
    707 					goto out;
    708 				}
    709 			}
    710 			if (cc)
    711 				(void) b_to_q(cp, cc, &tp->t_canq);
    712 			cc = 0;
    713 		}
    714 		(void) putc(0, &tp->t_canq);
    715 		ttwakeup(tp);
    716 		wakeup((caddr_t)&tp->t_canq);
    717 		error = 0;
    718 		goto out;
    719 	}
    720 	while (uio->uio_resid > 0) {
    721 		if (cc == 0) {
    722 			cc = min(uio->uio_resid, BUFSIZ);
    723 			cp = locbuf;
    724 			TTY_UNLOCK(tp);
    725 			error = uiomove((caddr_t)cp, cc, uio);
    726 			if (error)
    727 				return (error);
    728 			TTY_LOCK(tp);
    729 			/* check again for safety */
    730 			if (!ISSET(tp->t_state, TS_ISOPEN)) {
    731 				error = EIO;
    732 				goto out;
    733 			}
    734 		}
    735 		while (cc > 0) {
    736 			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
    737 			   (tp->t_canq.c_cc > 0 || !ISSET(tp->t_lflag, ICANON))) {
    738 				wakeup((caddr_t)&tp->t_rawq);
    739 				goto block;
    740 			}
    741 			/* XXX - should change l_rint to be called with lock
    742 			 *	 see also tty.c:ttyinput_wlock()
    743 			 */
    744 			TTY_UNLOCK(tp);
    745 			(*tp->t_linesw->l_rint)(*cp++, tp);
    746 			TTY_LOCK(tp);
    747 			cnt++;
    748 			cc--;
    749 		}
    750 		cc = 0;
    751 	}
    752 	error = 0;
    753 	goto out;
    754 
    755 block:
    756 	/*
    757 	 * Come here to wait for slave to open, for space
    758 	 * in outq, or space in rawq.
    759 	 */
    760 	if (!ISSET(tp->t_state, TS_CARR_ON)) {
    761 		error = EIO;
    762 		goto out;
    763 	}
    764 	if (flag & IO_NDELAY) {
    765 		/* adjust for data copied in but not written */
    766 		uio->uio_resid += cc;
    767 		error = cnt == 0 ? EWOULDBLOCK : 0;
    768 		goto out;
    769 	}
    770 	error = ltsleep((caddr_t)&tp->t_rawq.c_cf, TTOPRI | PCATCH | PNORELOCK,
    771 		       ttyout, 0, &tp->t_slock);
    772 	if (error) {
    773 		/* adjust for data copied in but not written */
    774 		uio->uio_resid += cc;
    775 		return (error);
    776 	}
    777 	goto again;
    778 
    779 out:
    780 	TTY_UNLOCK(tp);
    781 	return (error);
    782 }
    783 
    784 int
    785 ptcpoll(dev, events, l)
    786 	dev_t dev;
    787 	int events;
    788 	struct lwp *l;
    789 {
    790 	struct pt_softc *pti = pt_softc[minor(dev)];
    791 	struct tty *tp = pti->pt_tty;
    792 	int revents = 0;
    793 	int s = splsoftclock();
    794 
    795 	if (events & (POLLIN | POLLRDNORM))
    796 		if (ISSET(tp->t_state, TS_ISOPEN) &&
    797 		    ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
    798 		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
    799 		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
    800 			revents |= events & (POLLIN | POLLRDNORM);
    801 
    802 	if (events & (POLLOUT | POLLWRNORM))
    803 		if (ISSET(tp->t_state, TS_ISOPEN) &&
    804 		    ((pti->pt_flags & PF_REMOTE) ?
    805 		     (tp->t_canq.c_cc == 0) :
    806 		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
    807 		      (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))))
    808 			revents |= events & (POLLOUT | POLLWRNORM);
    809 
    810 	if (events & POLLHUP)
    811 		if (!ISSET(tp->t_state, TS_CARR_ON))
    812 			revents |= POLLHUP;
    813 
    814 	if (revents == 0) {
    815 		if (events & (POLLIN | POLLHUP | POLLRDNORM))
    816 			selrecord(l, &pti->pt_selr);
    817 
    818 		if (events & (POLLOUT | POLLWRNORM))
    819 			selrecord(l, &pti->pt_selw);
    820 	}
    821 
    822 	splx(s);
    823 	return (revents);
    824 }
    825 
    826 static void
    827 filt_ptcrdetach(struct knote *kn)
    828 {
    829 	struct pt_softc *pti;
    830 	int		s;
    831 
    832 	pti = kn->kn_hook;
    833 	s = spltty();
    834 	SLIST_REMOVE(&pti->pt_selr.sel_klist, kn, knote, kn_selnext);
    835 	splx(s);
    836 }
    837 
    838 static int
    839 filt_ptcread(struct knote *kn, long hint)
    840 {
    841 	struct pt_softc *pti;
    842 	struct tty	*tp;
    843 	int canread;
    844 
    845 	pti = kn->kn_hook;
    846 	tp = pti->pt_tty;
    847 
    848 	canread = (ISSET(tp->t_state, TS_ISOPEN) &&
    849 		    ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
    850 		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
    851 		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)));
    852 
    853 	if (canread) {
    854 		/*
    855 		 * c_cc is number of characters after output post-processing;
    856 		 * the amount of data actually read(2) depends on
    857 		 * setting of input flags for the terminal.
    858 		 */
    859 		kn->kn_data = tp->t_outq.c_cc;
    860 		if (((pti->pt_flags & PF_PKT) && pti->pt_send) ||
    861 		    ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))
    862 			kn->kn_data++;
    863 	}
    864 
    865 	return (canread);
    866 }
    867 
    868 static void
    869 filt_ptcwdetach(struct knote *kn)
    870 {
    871 	struct pt_softc *pti;
    872 	int		s;
    873 
    874 	pti = kn->kn_hook;
    875 	s = spltty();
    876 	SLIST_REMOVE(&pti->pt_selw.sel_klist, kn, knote, kn_selnext);
    877 	splx(s);
    878 }
    879 
    880 static int
    881 filt_ptcwrite(struct knote *kn, long hint)
    882 {
    883 	struct pt_softc *pti;
    884 	struct tty	*tp;
    885 	int canwrite;
    886 	int nwrite;
    887 
    888 	pti = kn->kn_hook;
    889 	tp = pti->pt_tty;
    890 
    891 	canwrite = (ISSET(tp->t_state, TS_ISOPEN) &&
    892 		    ((pti->pt_flags & PF_REMOTE) ?
    893 		     (tp->t_canq.c_cc == 0) :
    894 		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
    895 		      (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))));
    896 
    897 	if (canwrite) {
    898 		if (pti->pt_flags & PF_REMOTE)
    899 			nwrite = tp->t_canq.c_cn;
    900 		else {
    901 			/* this is guaranteed to be > 0 due to above check */
    902 			nwrite = tp->t_canq.c_cn
    903 				- (tp->t_rawq.c_cc + tp->t_canq.c_cc);
    904 		}
    905 		kn->kn_data = nwrite;
    906 	}
    907 
    908 	return (canwrite);
    909 }
    910 
    911 static const struct filterops ptcread_filtops =
    912 	{ 1, NULL, filt_ptcrdetach, filt_ptcread };
    913 static const struct filterops ptcwrite_filtops =
    914 	{ 1, NULL, filt_ptcwdetach, filt_ptcwrite };
    915 
    916 int
    917 ptckqfilter(dev_t dev, struct knote *kn)
    918 {
    919 	struct pt_softc *pti = pt_softc[minor(dev)];
    920 	struct klist	*klist;
    921 	int		s;
    922 
    923 	switch (kn->kn_filter) {
    924 	case EVFILT_READ:
    925 		klist = &pti->pt_selr.sel_klist;
    926 		kn->kn_fop = &ptcread_filtops;
    927 		break;
    928 	case EVFILT_WRITE:
    929 		klist = &pti->pt_selw.sel_klist;
    930 		kn->kn_fop = &ptcwrite_filtops;
    931 		break;
    932 	default:
    933 		return (1);
    934 	}
    935 
    936 	kn->kn_hook = pti;
    937 
    938 	s = spltty();
    939 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    940 	splx(s);
    941 
    942 	return (0);
    943 }
    944 
    945 struct tty *
    946 ptytty(dev)
    947 	dev_t dev;
    948 {
    949 	struct pt_softc *pti = pt_softc[minor(dev)];
    950 	struct tty *tp = pti->pt_tty;
    951 
    952 	return (tp);
    953 }
    954 
    955 /*ARGSUSED*/
    956 int
    957 ptyioctl(dev, cmd, data, flag, l)
    958 	dev_t dev;
    959 	u_long cmd;
    960 	caddr_t data;
    961 	int flag;
    962 	struct lwp *l;
    963 {
    964 	struct pt_softc *pti = pt_softc[minor(dev)];
    965 	struct tty *tp = pti->pt_tty;
    966 	const struct cdevsw *cdev;
    967 	u_char *cc = tp->t_cc;
    968 	int stop, error, sig;
    969 
    970 	cdev = cdevsw_lookup(dev);
    971 	/*
    972 	 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
    973 	 * ttywflush(tp) will hang if there are characters in the outq.
    974 	 */
    975 	if (cmd == TIOCEXT) {
    976 		/*
    977 		 * When the EXTPROC bit is being toggled, we need
    978 		 * to send an TIOCPKT_IOCTL if the packet driver
    979 		 * is turned on.
    980 		 */
    981 		if (*(int *)data) {
    982 			if (pti->pt_flags & PF_PKT) {
    983 				pti->pt_send |= TIOCPKT_IOCTL;
    984 				ptcwakeup(tp, FREAD);
    985 			}
    986 			SET(tp->t_lflag, EXTPROC);
    987 		} else {
    988 			if (ISSET(tp->t_lflag, EXTPROC) &&
    989 			    (pti->pt_flags & PF_PKT)) {
    990 				pti->pt_send |= TIOCPKT_IOCTL;
    991 				ptcwakeup(tp, FREAD);
    992 			}
    993 			CLR(tp->t_lflag, EXTPROC);
    994 		}
    995 		return(0);
    996 	} else
    997 	if (cdev != NULL && cdev->d_open == ptcopen)
    998 		switch (cmd) {
    999 
   1000 		case TIOCGPGRP:
   1001 			/*
   1002 			 * We avoid calling ttioctl on the controller since,
   1003 			 * in that case, tp must be the controlling terminal.
   1004 			 */
   1005 			*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
   1006 			return (0);
   1007 
   1008 		case TIOCPKT:
   1009 			if (*(int *)data) {
   1010 				if (pti->pt_flags & PF_UCNTL)
   1011 					return (EINVAL);
   1012 				pti->pt_flags |= PF_PKT;
   1013 			} else
   1014 				pti->pt_flags &= ~PF_PKT;
   1015 			return (0);
   1016 
   1017 		case TIOCUCNTL:
   1018 			if (*(int *)data) {
   1019 				if (pti->pt_flags & PF_PKT)
   1020 					return (EINVAL);
   1021 				pti->pt_flags |= PF_UCNTL;
   1022 			} else
   1023 				pti->pt_flags &= ~PF_UCNTL;
   1024 			return (0);
   1025 
   1026 		case TIOCREMOTE:
   1027 			if (*(int *)data)
   1028 				pti->pt_flags |= PF_REMOTE;
   1029 			else
   1030 				pti->pt_flags &= ~PF_REMOTE;
   1031 			TTY_LOCK(tp);
   1032 			ttyflush(tp, FREAD|FWRITE);
   1033 			TTY_UNLOCK(tp);
   1034 			return (0);
   1035 
   1036 #ifdef COMPAT_OLDTTY
   1037 		case TIOCSETP:
   1038 		case TIOCSETN:
   1039 #endif
   1040 		case TIOCSETD:
   1041 		case TIOCSETA:
   1042 		case TIOCSETAW:
   1043 		case TIOCSETAF:
   1044 			TTY_LOCK(tp);
   1045 			ndflush(&tp->t_outq, tp->t_outq.c_cc);
   1046 			TTY_UNLOCK(tp);
   1047 			break;
   1048 
   1049 		case TIOCSIG:
   1050 			sig = (int)(long)*(caddr_t *)data;
   1051 			if (sig <= 0 || sig >= NSIG)
   1052 				return (EINVAL);
   1053 			TTY_LOCK(tp);
   1054 			if (!ISSET(tp->t_lflag, NOFLSH))
   1055 				ttyflush(tp, FREAD|FWRITE);
   1056 			if ((sig == SIGINFO) &&
   1057 			    (!ISSET(tp->t_lflag, NOKERNINFO)))
   1058 				ttyinfo(tp);
   1059 			TTY_UNLOCK(tp);
   1060 			pgsignal(tp->t_pgrp, sig, 1);
   1061 			return(0);
   1062 		}
   1063 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
   1064 	if (error == EPASSTHROUGH)
   1065 		 error = ttioctl(tp, cmd, data, flag, l);
   1066 	if (error == EPASSTHROUGH) {
   1067 		if (pti->pt_flags & PF_UCNTL &&
   1068 		    (cmd & ~0xff) == UIOCCMD(0)) {
   1069 			if (cmd & 0xff) {
   1070 				pti->pt_ucntl = (u_char)cmd;
   1071 				ptcwakeup(tp, FREAD);
   1072 			}
   1073 			return (0);
   1074 		}
   1075 	}
   1076 	/*
   1077 	 * If external processing and packet mode send ioctl packet.
   1078 	 */
   1079 	if (ISSET(tp->t_lflag, EXTPROC) && (pti->pt_flags & PF_PKT)) {
   1080 		switch(cmd) {
   1081 		case TIOCSETA:
   1082 		case TIOCSETAW:
   1083 		case TIOCSETAF:
   1084 #ifdef COMPAT_OLDTTY
   1085 		case TIOCSETP:
   1086 		case TIOCSETN:
   1087 		case TIOCSETC:
   1088 		case TIOCSLTC:
   1089 		case TIOCLBIS:
   1090 		case TIOCLBIC:
   1091 		case TIOCLSET:
   1092 #endif
   1093 			pti->pt_send |= TIOCPKT_IOCTL;
   1094 			ptcwakeup(tp, FREAD);
   1095 		default:
   1096 			break;
   1097 		}
   1098 	}
   1099 	stop = ISSET(tp->t_iflag, IXON) && CCEQ(cc[VSTOP], CTRL('s'))
   1100 		&& CCEQ(cc[VSTART], CTRL('q'));
   1101 	if (pti->pt_flags & PF_NOSTOP) {
   1102 		if (stop) {
   1103 			pti->pt_send &= ~TIOCPKT_NOSTOP;
   1104 			pti->pt_send |= TIOCPKT_DOSTOP;
   1105 			pti->pt_flags &= ~PF_NOSTOP;
   1106 			ptcwakeup(tp, FREAD);
   1107 		}
   1108 	} else {
   1109 		if (!stop) {
   1110 			pti->pt_send &= ~TIOCPKT_DOSTOP;
   1111 			pti->pt_send |= TIOCPKT_NOSTOP;
   1112 			pti->pt_flags |= PF_NOSTOP;
   1113 			ptcwakeup(tp, FREAD);
   1114 		}
   1115 	}
   1116 	return (error);
   1117 }
   1118