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