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