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