Home | History | Annotate | Line # | Download | only in kern
tty_pty.c revision 1.109.2.2
      1 /*	$NetBSD: tty_pty.c,v 1.109.2.2 2009/06/20 07:20:31 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.109.2.2 2009/06/20 07:20:31 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(int newmax, int set)
    277 {
    278 	if (!set)
    279 		return (maxptys);
    280 
    281 	/*
    282 	 * We have to grab the pt_softc lock, so that we would pick correct
    283 	 * value of npty (might be modified in pty_check()).
    284 	 */
    285 	mutex_enter(&pt_softc_mutex);
    286 
    287 	/*
    288 	 * The value cannot be set to value lower than the highest pty
    289 	 * number ever allocated.
    290 	 */
    291 	if (newmax >= npty)
    292 		maxptys = newmax;
    293 	else
    294 		newmax = 0;
    295 
    296 	mutex_exit(&pt_softc_mutex);
    297 
    298 	return newmax;
    299 }
    300 
    301 /*
    302  * Establish n (or default if n is 1) ptys in the system.
    303  */
    304 void
    305 ptyattach(int n)
    306 {
    307 
    308 	mutex_init(&pt_softc_mutex, MUTEX_DEFAULT, IPL_NONE);
    309 
    310 	/* maybe should allow 0 => none? */
    311 	if (n <= 1)
    312 		n = DEFAULT_NPTYS;
    313 	pt_softc = ptyarralloc(n);
    314 	npty = n;
    315 #ifndef NO_DEV_PTM
    316 	ptmattach(1);
    317 #endif
    318 }
    319 
    320 /*ARGSUSED*/
    321 int
    322 ptsopen(dev_t dev, int flag, int devtype, struct lwp *l)
    323 {
    324 	struct pt_softc *pti;
    325 	struct tty *tp;
    326 	int error;
    327 	int ptn = minor(dev);
    328 
    329 	if ((error = pty_check(ptn)) != 0)
    330 		return (error);
    331 
    332 	mutex_spin_enter(&tty_lock);
    333 	pti = pt_softc[ptn];
    334 	tp = pti->pt_tty;
    335 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
    336 		ttychars(tp);		/* Set up default chars */
    337 		tp->t_iflag = TTYDEF_IFLAG;
    338 		tp->t_oflag = TTYDEF_OFLAG;
    339 		tp->t_lflag = TTYDEF_LFLAG;
    340 		tp->t_cflag = TTYDEF_CFLAG;
    341 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    342 		ttsetwater(tp);		/* would be done in xxparam() */
    343 	} else if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN,
    344 	    tp) != 0) {
    345 		mutex_spin_exit(&tty_lock);
    346 		return (EBUSY);
    347 	}
    348 	if (tp->t_oproc)			/* Ctrlr still around. */
    349 		SET(tp->t_state, TS_CARR_ON);
    350 	if (!ISSET(flag, O_NONBLOCK)) {
    351 		while (!ISSET(tp->t_state, TS_CARR_ON)) {
    352 			tp->t_wopen++;
    353 			error = ttysleep(tp, &tp->t_rawcv, true, 0);
    354 			tp->t_wopen--;
    355 			if (error) {
    356 				mutex_spin_exit(&tty_lock);
    357 				return (error);
    358 			}
    359 		}
    360 	}
    361 	mutex_spin_exit(&tty_lock);
    362 	error = (*tp->t_linesw->l_open)(dev, tp);
    363 	ptcwakeup(tp, FREAD|FWRITE);
    364 	return (error);
    365 }
    366 
    367 int
    368 ptsclose(dev_t dev, int flag, int mode, struct lwp *l)
    369 {
    370 	struct pt_softc *pti = pt_softc[minor(dev)];
    371 	struct tty *tp = pti->pt_tty;
    372 	int error;
    373 
    374 	error = (*tp->t_linesw->l_close)(tp, flag);
    375 	error |= ttyclose(tp);
    376 	ptcwakeup(tp, FREAD|FWRITE);
    377 	return (error);
    378 }
    379 
    380 int
    381 ptsread(dev_t dev, struct uio *uio, int flag)
    382 {
    383 	struct proc *p = curproc;
    384 	struct pt_softc *pti = pt_softc[minor(dev)];
    385 	struct tty *tp = pti->pt_tty;
    386 	int error = 0;
    387 	int cc, c;
    388 
    389 again:
    390 	if (pti->pt_flags & PF_REMOTE) {
    391 		mutex_spin_enter(&tty_lock);
    392 		while (isbackground(p, tp)) {	/* XXXSMP */
    393 			if (sigismasked(curlwp, SIGTTIN) ||
    394 			    p->p_pgrp->pg_jobc == 0 ||
    395 			    p->p_lflag & PL_PPWAIT) {
    396 				mutex_spin_exit(&tty_lock);
    397 				return (EIO);
    398 			}
    399 			ttysig(tp, TTYSIG_PG1, SIGTTIN);
    400 			error = ttysleep(tp, &lbolt, true, 0);
    401 			if (error) {
    402 				mutex_spin_exit(&tty_lock);
    403 				return (error);
    404 			}
    405 		}
    406 		if (tp->t_canq.c_cc == 0) {
    407 			if (flag & IO_NDELAY) {
    408 				mutex_spin_exit(&tty_lock);
    409 				return (EWOULDBLOCK);
    410 			}
    411 			error = ttysleep(tp, &tp->t_cancv, true, 0);
    412 			mutex_spin_exit(&tty_lock);
    413 			if (error)
    414 				return (error);
    415 			goto again;
    416 		}
    417 		while(error == 0 && tp->t_canq.c_cc > 1 && uio->uio_resid > 0) {
    418 			c = getc(&tp->t_canq);
    419 			mutex_spin_exit(&tty_lock);
    420 			error = ureadc(c, uio);
    421 			mutex_spin_enter(&tty_lock);
    422 			/* Re-check terminal state here? */
    423 		}
    424 		if (tp->t_canq.c_cc == 1)
    425 			(void) getc(&tp->t_canq);
    426 		cc = tp->t_canq.c_cc;
    427 		mutex_spin_exit(&tty_lock);
    428 		if (cc)
    429 			return (error);
    430 	} else if (tp->t_oproc)
    431 		error = (*tp->t_linesw->l_read)(tp, uio, flag);
    432 	ptcwakeup(tp, FWRITE);
    433 	return (error);
    434 }
    435 
    436 /*
    437  * Write to pseudo-tty.
    438  * Wakeups of controlling tty will happen
    439  * indirectly, when tty driver calls ptsstart.
    440  */
    441 int
    442 ptswrite(dev_t dev, struct uio *uio, int flag)
    443 {
    444 	struct pt_softc *pti = pt_softc[minor(dev)];
    445 	struct tty *tp = pti->pt_tty;
    446 
    447 	if (tp->t_oproc == 0)
    448 		return (EIO);
    449 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    450 }
    451 
    452 /*
    453  * Poll pseudo-tty.
    454  */
    455 int
    456 ptspoll(dev_t dev, int events, struct lwp *l)
    457 {
    458 	struct pt_softc *pti = pt_softc[minor(dev)];
    459 	struct tty *tp = pti->pt_tty;
    460 
    461 	if (tp->t_oproc == 0)
    462 		return (POLLHUP);
    463 
    464 	return ((*tp->t_linesw->l_poll)(tp, events, l));
    465 }
    466 
    467 /*
    468  * Start output on pseudo-tty.
    469  * Wake up process polling or sleeping for input from controlling tty.
    470  */
    471 void
    472 ptsstart(struct tty *tp)
    473 {
    474 	struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
    475 
    476 	KASSERT(mutex_owned(&tty_lock));
    477 
    478 	if (ISSET(tp->t_state, TS_TTSTOP))
    479 		return;
    480 	if (pti->pt_flags & PF_STOPPED) {
    481 		pti->pt_flags &= ~PF_STOPPED;
    482 		pti->pt_send = TIOCPKT_START;
    483 	}
    484 
    485 	selnotify(&pti->pt_selr, 0, NOTE_SUBMIT);
    486 	cv_broadcast(&tp->t_outcvf);
    487 }
    488 
    489 /*
    490  * Stop output.
    491  */
    492 void
    493 ptsstop(struct tty *tp, int flush)
    494 {
    495 	struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
    496 
    497 	KASSERT(mutex_owned(&tty_lock));
    498 
    499 	/* note: FLUSHREAD and FLUSHWRITE already ok */
    500 	if (flush == 0) {
    501 		flush = TIOCPKT_STOP;
    502 		pti->pt_flags |= PF_STOPPED;
    503 	} else
    504 		pti->pt_flags &= ~PF_STOPPED;
    505 	pti->pt_send |= flush;
    506 
    507 	/* change of perspective */
    508 	if (flush & FREAD) {
    509 		selnotify(&pti->pt_selw, 0, NOTE_SUBMIT);
    510 		cv_broadcast(&tp->t_rawcvf);
    511 	}
    512 	if (flush & FWRITE) {
    513 		selnotify(&pti->pt_selr, 0, NOTE_SUBMIT);
    514 		cv_broadcast(&tp->t_outcvf);
    515 	}
    516 }
    517 
    518 void
    519 ptcwakeup(struct tty *tp, int flag)
    520 {
    521 	struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
    522 
    523 	mutex_spin_enter(&tty_lock);
    524 	if (flag & FREAD) {
    525 		selnotify(&pti->pt_selr, 0, NOTE_SUBMIT);
    526 		cv_broadcast(&tp->t_outcvf);
    527 	}
    528 	if (flag & FWRITE) {
    529 		selnotify(&pti->pt_selw, 0, NOTE_SUBMIT);
    530 		cv_broadcast(&tp->t_rawcvf);
    531 	}
    532 	mutex_spin_exit(&tty_lock);
    533 }
    534 
    535 /*ARGSUSED*/
    536 int
    537 ptcopen(dev_t dev, int flag, int devtype, struct lwp *l)
    538 {
    539 	struct pt_softc *pti;
    540 	struct tty *tp;
    541 	int error;
    542 	int ptn = minor(dev);
    543 
    544 	if ((error = pty_check(ptn)) != 0)
    545 		return (error);
    546 
    547 	pti = pt_softc[ptn];
    548 	tp = pti->pt_tty;
    549 
    550 	mutex_spin_enter(&tty_lock);
    551 	if (tp->t_oproc) {
    552 		mutex_spin_exit(&tty_lock);
    553 		return (EIO);
    554 	}
    555 	tp->t_oproc = ptsstart;
    556 	mutex_spin_exit(&tty_lock);
    557 	(void)(*tp->t_linesw->l_modem)(tp, 1);
    558 	CLR(tp->t_lflag, EXTPROC);
    559 	pti->pt_flags = 0;
    560 	pti->pt_send = 0;
    561 	pti->pt_ucntl = 0;
    562 	return (0);
    563 }
    564 
    565 /*ARGSUSED*/
    566 int
    567 ptcclose(dev_t dev, int flag, int devtype, struct lwp *l)
    568 {
    569 	struct pt_softc *pti = pt_softc[minor(dev)];
    570 	struct tty *tp = pti->pt_tty;
    571 
    572 	(void)(*tp->t_linesw->l_modem)(tp, 0);
    573 	mutex_spin_enter(&tty_lock);
    574 	CLR(tp->t_state, TS_CARR_ON);
    575 	tp->t_oproc = 0;		/* mark closed */
    576 	mutex_spin_exit(&tty_lock);
    577 	return (0);
    578 }
    579 
    580 int
    581 ptcread(dev_t dev, struct uio *uio, int flag)
    582 {
    583 	struct pt_softc *pti = pt_softc[minor(dev)];
    584 	struct tty *tp = pti->pt_tty;
    585 	u_char bf[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 	mutex_spin_enter(&tty_lock);
    595 	for (;;) {
    596 		if (ISSET(tp->t_state, TS_ISOPEN)) {
    597 			if (pti->pt_flags & PF_PKT && pti->pt_send) {
    598 				mutex_spin_exit(&tty_lock);
    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((void *) &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 				mutex_spin_exit(&tty_lock);
    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 = cv_wait_sig(&tp->t_outcvf, &tty_lock);
    637 		if (error)
    638 			goto out;
    639 	}
    640 
    641 	if (pti->pt_flags & (PF_PKT|PF_UCNTL)) {
    642 		mutex_spin_exit(&tty_lock);
    643 		error = ureadc(0, uio);
    644 		mutex_spin_enter(&tty_lock);
    645 		if (error == 0 && !ISSET(tp->t_state, TS_ISOPEN))
    646 			error = EIO;
    647 	}
    648 	while (uio->uio_resid > 0 && error == 0) {
    649 		cc = q_to_b(&tp->t_outq, bf, min(uio->uio_resid, BUFSIZ));
    650 		if (cc <= 0)
    651 			break;
    652 		mutex_spin_exit(&tty_lock);
    653 		error = uiomove(bf, cc, uio);
    654 		mutex_spin_enter(&tty_lock);
    655 		if (error == 0 && !ISSET(tp->t_state, TS_ISOPEN))
    656 			error = EIO;
    657 	}
    658 	ttypull(tp);
    659 out:
    660 	mutex_spin_exit(&tty_lock);
    661 	return (error);
    662 }
    663 
    664 
    665 int
    666 ptcwrite(dev_t dev, struct uio *uio, int flag)
    667 {
    668 	struct pt_softc *pti = pt_softc[minor(dev)];
    669 	struct tty *tp = pti->pt_tty;
    670 	u_char *cp = NULL;
    671 	int cc = 0;
    672 	u_char locbuf[BUFSIZ];
    673 	int cnt = 0;
    674 	int error = 0;
    675 
    676 again:
    677 	mutex_spin_enter(&tty_lock);
    678 	if (!ISSET(tp->t_state, TS_ISOPEN))
    679 		goto block;
    680 	if (pti->pt_flags & PF_REMOTE) {
    681 		if (tp->t_canq.c_cc)
    682 			goto block;
    683 		while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) {
    684 			if (cc == 0) {
    685 				cc = min(uio->uio_resid, BUFSIZ);
    686 				cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
    687 				cp = locbuf;
    688 				mutex_spin_exit(&tty_lock);
    689 				error = uiomove((void *)cp, cc, uio);
    690 				if (error)
    691 					return (error);
    692 				mutex_spin_enter(&tty_lock);
    693 				/* check again for safety */
    694 				if (!ISSET(tp->t_state, TS_ISOPEN)) {
    695 					/*
    696 					 * adjust for data copied in but not
    697 					 * written
    698 					 */
    699 					uio->uio_resid += cc;
    700 					error = EIO;
    701 					goto out;
    702 				}
    703 			}
    704 			if (cc)
    705 				(void) b_to_q(cp, cc, &tp->t_canq);
    706 			cc = 0;
    707 		}
    708 		(void) putc(0, &tp->t_canq);
    709 		ttwakeup(tp);
    710 		cv_broadcast(&tp->t_cancv);
    711 		error = 0;
    712 		goto out;
    713 	}
    714 	while (uio->uio_resid > 0) {
    715 		if (cc == 0) {
    716 			cc = min(uio->uio_resid, BUFSIZ);
    717 			cp = locbuf;
    718 			mutex_spin_exit(&tty_lock);
    719 			error = uiomove((void *)cp, cc, uio);
    720 			if (error)
    721 				return (error);
    722 			mutex_spin_enter(&tty_lock);
    723 			/* check again for safety */
    724 			if (!ISSET(tp->t_state, TS_ISOPEN)) {
    725 				/* adjust for data copied in but not written */
    726 				uio->uio_resid += cc;
    727 				error = EIO;
    728 				goto out;
    729 			}
    730 		}
    731 		while (cc > 0) {
    732 			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
    733 			   (tp->t_canq.c_cc > 0 || !ISSET(tp->t_lflag, ICANON))) {
    734 				cv_broadcast(&tp->t_rawcv);
    735 				goto block;
    736 			}
    737 			/* XXX - should change l_rint to be called with lock
    738 			 *	 see also tty.c:ttyinput_wlock()
    739 			 */
    740 			mutex_spin_exit(&tty_lock);
    741 			(*tp->t_linesw->l_rint)(*cp++, tp);
    742 			mutex_spin_enter(&tty_lock);
    743 			cnt++;
    744 			cc--;
    745 		}
    746 		cc = 0;
    747 	}
    748 	error = 0;
    749 	goto out;
    750 
    751 block:
    752 	/*
    753 	 * Come here to wait for slave to open, for space
    754 	 * in outq, or space in rawq.
    755 	 */
    756 	if (!ISSET(tp->t_state, TS_CARR_ON)) {
    757 		/* adjust for data copied in but not written */
    758 		uio->uio_resid += cc;
    759 		error = EIO;
    760 		goto out;
    761 	}
    762 	if (flag & IO_NDELAY) {
    763 		/* adjust for data copied in but not written */
    764 		uio->uio_resid += cc;
    765 		error = cnt == 0 ? EWOULDBLOCK : 0;
    766 		goto out;
    767 	}
    768 	error = cv_wait_sig(&tp->t_rawcvf, &tty_lock);
    769 	mutex_spin_exit(&tty_lock);
    770 	if (error) {
    771 		/* adjust for data copied in but not written */
    772 		uio->uio_resid += cc;
    773 		return (error);
    774 	}
    775 	goto again;
    776 
    777 out:
    778 	mutex_spin_exit(&tty_lock);
    779 	return (error);
    780 }
    781 
    782 int
    783 ptcpoll(dev_t dev, int events, struct lwp *l)
    784 {
    785 	struct pt_softc *pti = pt_softc[minor(dev)];
    786 	struct tty *tp = pti->pt_tty;
    787 	int revents = 0;
    788 
    789 	mutex_spin_enter(&tty_lock);
    790 
    791 	if (events & (POLLIN | POLLRDNORM))
    792 		if (ISSET(tp->t_state, TS_ISOPEN) &&
    793 		    ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
    794 		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
    795 		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
    796 			revents |= events & (POLLIN | POLLRDNORM);
    797 
    798 	if (events & (POLLOUT | POLLWRNORM))
    799 		if (ISSET(tp->t_state, TS_ISOPEN) &&
    800 		    ((pti->pt_flags & PF_REMOTE) ?
    801 		     (tp->t_canq.c_cc == 0) :
    802 		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
    803 		      (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))))
    804 			revents |= events & (POLLOUT | POLLWRNORM);
    805 
    806 	if (events & POLLHUP)
    807 		if (!ISSET(tp->t_state, TS_CARR_ON))
    808 			revents |= POLLHUP;
    809 
    810 	if (revents == 0) {
    811 		if (events & (POLLIN | POLLHUP | POLLRDNORM))
    812 			selrecord(l, &pti->pt_selr);
    813 
    814 		if (events & (POLLOUT | POLLWRNORM))
    815 			selrecord(l, &pti->pt_selw);
    816 	}
    817 
    818 	mutex_spin_exit(&tty_lock);
    819 
    820 	return (revents);
    821 }
    822 
    823 static void
    824 filt_ptcrdetach(struct knote *kn)
    825 {
    826 	struct pt_softc *pti;
    827 	struct tty	*tp;
    828 
    829 	pti = kn->kn_hook;
    830 	tp = pti->pt_tty;
    831 
    832 	mutex_spin_enter(&tty_lock);
    833 	SLIST_REMOVE(&pti->pt_selr.sel_klist, kn, knote, kn_selnext);
    834 	mutex_spin_exit(&tty_lock);
    835 }
    836 
    837 static int
    838 filt_ptcread(struct knote *kn, long hint)
    839 {
    840 	struct pt_softc *pti;
    841 	struct tty	*tp;
    842 	int canread;
    843 
    844 	pti = kn->kn_hook;
    845 	tp = pti->pt_tty;
    846 
    847 	if ((hint & NOTE_SUBMIT) == 0) {
    848 		mutex_spin_enter(&tty_lock);
    849 	}
    850 
    851 	canread = (ISSET(tp->t_state, TS_ISOPEN) &&
    852 		    ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
    853 		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
    854 		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)));
    855 
    856 	if (canread) {
    857 		/*
    858 		 * c_cc is number of characters after output post-processing;
    859 		 * the amount of data actually read(2) depends on
    860 		 * setting of input flags for the terminal.
    861 		 */
    862 		kn->kn_data = tp->t_outq.c_cc;
    863 		if (((pti->pt_flags & PF_PKT) && pti->pt_send) ||
    864 		    ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))
    865 			kn->kn_data++;
    866 	}
    867 
    868 	if ((hint & NOTE_SUBMIT) == 0) {
    869 		mutex_spin_exit(&tty_lock);
    870 	}
    871 
    872 	return (canread);
    873 }
    874 
    875 static void
    876 filt_ptcwdetach(struct knote *kn)
    877 {
    878 	struct pt_softc *pti;
    879 	struct tty	*tp;
    880 
    881 	pti = kn->kn_hook;
    882 	tp = pti->pt_tty;
    883 
    884 	mutex_spin_enter(&tty_lock);
    885 	SLIST_REMOVE(&pti->pt_selw.sel_klist, kn, knote, kn_selnext);
    886 	mutex_spin_exit(&tty_lock);
    887 }
    888 
    889 static int
    890 filt_ptcwrite(struct knote *kn, long hint)
    891 {
    892 	struct pt_softc *pti;
    893 	struct tty	*tp;
    894 	int canwrite;
    895 	int nwrite;
    896 
    897 	pti = kn->kn_hook;
    898 	tp = pti->pt_tty;
    899 
    900 	if ((hint & NOTE_SUBMIT) == 0) {
    901 		mutex_spin_enter(&tty_lock);
    902 	}
    903 
    904 	canwrite = (ISSET(tp->t_state, TS_ISOPEN) &&
    905 		    ((pti->pt_flags & PF_REMOTE) ?
    906 		     (tp->t_canq.c_cc == 0) :
    907 		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
    908 		      (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))));
    909 
    910 	if (canwrite) {
    911 		if (pti->pt_flags & PF_REMOTE)
    912 			nwrite = tp->t_canq.c_cn;
    913 		else {
    914 			/* this is guaranteed to be > 0 due to above check */
    915 			nwrite = tp->t_canq.c_cn
    916 				- (tp->t_rawq.c_cc + tp->t_canq.c_cc);
    917 		}
    918 		kn->kn_data = nwrite;
    919 	}
    920 
    921 	if ((hint & NOTE_SUBMIT) == 0) {
    922 		mutex_spin_exit(&tty_lock);
    923 	}
    924 
    925 	return (canwrite);
    926 }
    927 
    928 static const struct filterops ptcread_filtops =
    929 	{ 1, NULL, filt_ptcrdetach, filt_ptcread };
    930 static const struct filterops ptcwrite_filtops =
    931 	{ 1, NULL, filt_ptcwdetach, filt_ptcwrite };
    932 
    933 int
    934 ptckqfilter(dev_t dev, struct knote *kn)
    935 {
    936 	struct pt_softc *pti = pt_softc[minor(dev)];
    937 	struct klist	*klist;
    938 
    939 	switch (kn->kn_filter) {
    940 	case EVFILT_READ:
    941 		klist = &pti->pt_selr.sel_klist;
    942 		kn->kn_fop = &ptcread_filtops;
    943 		break;
    944 	case EVFILT_WRITE:
    945 		klist = &pti->pt_selw.sel_klist;
    946 		kn->kn_fop = &ptcwrite_filtops;
    947 		break;
    948 	default:
    949 		return (EINVAL);
    950 	}
    951 
    952 	kn->kn_hook = pti;
    953 
    954 	mutex_spin_enter(&tty_lock);
    955 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    956 	mutex_spin_exit(&tty_lock);
    957 
    958 	return (0);
    959 }
    960 
    961 struct tty *
    962 ptytty(dev_t dev)
    963 {
    964 	struct pt_softc *pti = pt_softc[minor(dev)];
    965 	struct tty *tp = pti->pt_tty;
    966 
    967 	return (tp);
    968 }
    969 
    970 /*ARGSUSED*/
    971 int
    972 ptyioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    973 {
    974 	struct pt_softc *pti = pt_softc[minor(dev)];
    975 	struct tty *tp = pti->pt_tty;
    976 	const struct cdevsw *cdev;
    977 	u_char *cc = tp->t_cc;
    978 	int stop, error, sig;
    979 
    980 	/*
    981 	 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
    982 	 * ttywflush(tp) will hang if there are characters in the outq.
    983 	 */
    984 	if (cmd == TIOCEXT) {
    985 		/*
    986 		 * When the EXTPROC bit is being toggled, we need
    987 		 * to send an TIOCPKT_IOCTL if the packet driver
    988 		 * is turned on.
    989 		 */
    990 		if (*(int *)data) {
    991 			if (pti->pt_flags & PF_PKT) {
    992 				pti->pt_send |= TIOCPKT_IOCTL;
    993 				ptcwakeup(tp, FREAD);
    994 			}
    995 			SET(tp->t_lflag, EXTPROC);
    996 		} else {
    997 			if (ISSET(tp->t_lflag, EXTPROC) &&
    998 			    (pti->pt_flags & PF_PKT)) {
    999 				pti->pt_send |= TIOCPKT_IOCTL;
   1000 				ptcwakeup(tp, FREAD);
   1001 			}
   1002 			CLR(tp->t_lflag, EXTPROC);
   1003 		}
   1004 		return(0);
   1005 	}
   1006 
   1007 #ifndef NO_DEV_PTM
   1008 	/* Allow getting the name from either the master or the slave */
   1009 	if (cmd == TIOCPTSNAME)
   1010 		return pty_fill_ptmget(l, dev, -1, -1, data);
   1011 #endif
   1012 
   1013 	cdev = cdevsw_lookup(dev);
   1014 	if (cdev != NULL && cdev->d_open == ptcopen)
   1015 		switch (cmd) {
   1016 #ifndef NO_DEV_PTM
   1017 		case TIOCGRANTPT:
   1018 			return pty_grant_slave(l, dev);
   1019 #endif
   1020 
   1021 		case TIOCGPGRP:
   1022 			/*
   1023 			 * We avoid calling ttioctl on the controller since,
   1024 			 * in that case, tp must be the controlling terminal.
   1025 			 */
   1026 			*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
   1027 			return (0);
   1028 
   1029 		case TIOCPKT:
   1030 			if (*(int *)data) {
   1031 				if (pti->pt_flags & PF_UCNTL)
   1032 					return (EINVAL);
   1033 				pti->pt_flags |= PF_PKT;
   1034 			} else
   1035 				pti->pt_flags &= ~PF_PKT;
   1036 			return (0);
   1037 
   1038 		case TIOCUCNTL:
   1039 			if (*(int *)data) {
   1040 				if (pti->pt_flags & PF_PKT)
   1041 					return (EINVAL);
   1042 				pti->pt_flags |= PF_UCNTL;
   1043 			} else
   1044 				pti->pt_flags &= ~PF_UCNTL;
   1045 			return (0);
   1046 
   1047 		case TIOCREMOTE:
   1048 			if (*(int *)data)
   1049 				pti->pt_flags |= PF_REMOTE;
   1050 			else
   1051 				pti->pt_flags &= ~PF_REMOTE;
   1052 			mutex_spin_enter(&tty_lock);
   1053 			ttyflush(tp, FREAD|FWRITE);
   1054 			mutex_spin_exit(&tty_lock);
   1055 			return (0);
   1056 
   1057 		case TIOCSETP:
   1058 		case TIOCSETN:
   1059 		case TIOCSETD:
   1060 		case TIOCSETA:
   1061 		case TIOCSETAW:
   1062 		case TIOCSETAF:
   1063 			mutex_spin_enter(&tty_lock);
   1064 			ndflush(&tp->t_outq, tp->t_outq.c_cc);
   1065 			mutex_spin_exit(&tty_lock);
   1066 			break;
   1067 
   1068 		case TIOCSIG:
   1069 			sig = (int)(long)*(void **)data;
   1070 			if (sig <= 0 || sig >= NSIG)
   1071 				return (EINVAL);
   1072 			mutex_spin_enter(&tty_lock);
   1073 			if (!ISSET(tp->t_lflag, NOFLSH))
   1074 				ttyflush(tp, FREAD|FWRITE);
   1075 			tp->t_state |= TS_SIGINFO;
   1076 			ttysig(tp, TTYSIG_PG1, sig);
   1077 			mutex_spin_exit(&tty_lock);
   1078 			return (0);
   1079 
   1080 		case FIONREAD:
   1081 			mutex_spin_enter(&tty_lock);
   1082 			*(int *)data = tp->t_outq.c_cc;
   1083 			mutex_spin_exit(&tty_lock);
   1084 			return (0);
   1085 		}
   1086 
   1087 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
   1088 	if (error == EPASSTHROUGH)
   1089 		 error = ttioctl(tp, cmd, data, flag, l);
   1090 	if (error == EPASSTHROUGH) {
   1091 		if (pti->pt_flags & PF_UCNTL &&
   1092 		    (cmd & ~0xff) == UIOCCMD(0)) {
   1093 			if (cmd & 0xff) {
   1094 				pti->pt_ucntl = (u_char)cmd;
   1095 				ptcwakeup(tp, FREAD);
   1096 			}
   1097 			return (0);
   1098 		}
   1099 	}
   1100 	/*
   1101 	 * If external processing and packet mode send ioctl packet.
   1102 	 */
   1103 	if (ISSET(tp->t_lflag, EXTPROC) && (pti->pt_flags & PF_PKT)) {
   1104 		switch(cmd) {
   1105 		case TIOCSETA:
   1106 		case TIOCSETAW:
   1107 		case TIOCSETAF:
   1108 		case TIOCSETP:
   1109 		case TIOCSETN:
   1110 		case TIOCSETC:
   1111 		case TIOCSLTC:
   1112 		case TIOCLBIS:
   1113 		case TIOCLBIC:
   1114 		case TIOCLSET:
   1115 			pti->pt_send |= TIOCPKT_IOCTL;
   1116 			ptcwakeup(tp, FREAD);
   1117 		default:
   1118 			break;
   1119 		}
   1120 	}
   1121 	stop = ISSET(tp->t_iflag, IXON) && CCEQ(cc[VSTOP], CTRL('s'))
   1122 		&& CCEQ(cc[VSTART], CTRL('q'));
   1123 	if (pti->pt_flags & PF_NOSTOP) {
   1124 		if (stop) {
   1125 			pti->pt_send &= ~TIOCPKT_NOSTOP;
   1126 			pti->pt_send |= TIOCPKT_DOSTOP;
   1127 			pti->pt_flags &= ~PF_NOSTOP;
   1128 			ptcwakeup(tp, FREAD);
   1129 		}
   1130 	} else {
   1131 		if (!stop) {
   1132 			pti->pt_send &= ~TIOCPKT_DOSTOP;
   1133 			pti->pt_send |= TIOCPKT_NOSTOP;
   1134 			pti->pt_flags |= PF_NOSTOP;
   1135 			ptcwakeup(tp, FREAD);
   1136 		}
   1137 	}
   1138 	return (error);
   1139 }
   1140