Home | History | Annotate | Line # | Download | only in kern
tty_pty.c revision 1.106
      1 /*	$NetBSD: tty_pty.c,v 1.106 2007/12/22 02:21:29 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.106 2007/12/22 02:21:29 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 	mutex_spin_enter(&tty_lock);
    323 	pti = pt_softc[ptn];
    324 	tp = pti->pt_tty;
    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 		mutex_spin_exit(&tty_lock);
    336 		return (EBUSY);
    337 	}
    338 	if (tp->t_oproc)			/* Ctrlr still around. */
    339 		SET(tp->t_state, TS_CARR_ON);
    340 	if (!ISSET(flag, O_NONBLOCK)) {
    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 	}
    351 	mutex_spin_exit(&tty_lock);
    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, c;
    381 
    382 again:
    383 	if (pti->pt_flags & PF_REMOTE) {
    384 		mutex_spin_enter(&tty_lock);
    385 		while (isbackground(p, tp)) {	/* XXXSMP */
    386 			if (sigismasked(curlwp, SIGTTIN) ||
    387 			    p->p_pgrp->pg_jobc == 0 ||
    388 			    p->p_flag & PS_PPWAIT) {
    389 				mutex_spin_exit(&tty_lock);
    390 				return (EIO);
    391 			}
    392 			ttysig(tp, TTYSIG_PG1, SIGTTIN);
    393 			error = ttysleep(tp, &lbolt, true, 0);
    394 			if (error) {
    395 				mutex_spin_exit(&tty_lock);
    396 				return (error);
    397 			}
    398 		}
    399 		if (tp->t_canq.c_cc == 0) {
    400 			if (flag & IO_NDELAY) {
    401 				mutex_spin_exit(&tty_lock);
    402 				return (EWOULDBLOCK);
    403 			}
    404 			error = ttysleep(tp, &tp->t_canq.c_cv, true, 0);
    405 			mutex_spin_exit(&tty_lock);
    406 			if (error)
    407 				return (error);
    408 			goto again;
    409 		}
    410 		while(error == 0 && tp->t_canq.c_cc > 1 && uio->uio_resid > 0) {
    411 			c = getc(&tp->t_canq);
    412 			mutex_spin_exit(&tty_lock);
    413 			error = ureadc(c, uio);
    414 			mutex_spin_enter(&tty_lock);
    415 			/* Re-check terminal state here? */
    416 		}
    417 		if (tp->t_canq.c_cc == 1)
    418 			(void) getc(&tp->t_canq);
    419 		cc = tp->t_canq.c_cc;
    420 		mutex_spin_exit(&tty_lock);
    421 		if (cc)
    422 			return (error);
    423 	} else if (tp->t_oproc)
    424 		error = (*tp->t_linesw->l_read)(tp, uio, flag);
    425 	ptcwakeup(tp, FWRITE);
    426 	return (error);
    427 }
    428 
    429 /*
    430  * Write to pseudo-tty.
    431  * Wakeups of controlling tty will happen
    432  * indirectly, when tty driver calls ptsstart.
    433  */
    434 int
    435 ptswrite(dev, uio, flag)
    436 	dev_t dev;
    437 	struct uio *uio;
    438 	int flag;
    439 {
    440 	struct pt_softc *pti = pt_softc[minor(dev)];
    441 	struct tty *tp = pti->pt_tty;
    442 
    443 	if (tp->t_oproc == 0)
    444 		return (EIO);
    445 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    446 }
    447 
    448 /*
    449  * Poll pseudo-tty.
    450  */
    451 int
    452 ptspoll(dev, events, l)
    453 	dev_t dev;
    454 	int events;
    455 	struct lwp *l;
    456 {
    457 	struct pt_softc *pti = pt_softc[minor(dev)];
    458 	struct tty *tp = pti->pt_tty;
    459 
    460 	if (tp->t_oproc == 0)
    461 		return (POLLHUP);
    462 
    463 	return ((*tp->t_linesw->l_poll)(tp, events, l));
    464 }
    465 
    466 /*
    467  * Start output on pseudo-tty.
    468  * Wake up process polling or sleeping for input from controlling tty.
    469  */
    470 void
    471 ptsstart(tp)
    472 	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, NOTE_SUBMIT);
    486 	cv_broadcast(&tp->t_outq.c_cvf);
    487 }
    488 
    489 /*
    490  * Stop output.
    491  */
    492 void
    493 ptsstop(tp, flush)
    494 	struct tty *tp;
    495 	int flush;
    496 {
    497 	struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
    498 
    499 	KASSERT(mutex_owned(&tty_lock));
    500 
    501 	/* note: FLUSHREAD and FLUSHWRITE already ok */
    502 	if (flush == 0) {
    503 		flush = TIOCPKT_STOP;
    504 		pti->pt_flags |= PF_STOPPED;
    505 	} else
    506 		pti->pt_flags &= ~PF_STOPPED;
    507 	pti->pt_send |= flush;
    508 
    509 	/* change of perspective */
    510 	if (flush & FREAD) {
    511 		selnotify(&pti->pt_selw, NOTE_SUBMIT);
    512 		cv_broadcast(&tp->t_rawq.c_cvf);
    513 	}
    514 	if (flush & FWRITE) {
    515 		selnotify(&pti->pt_selr, NOTE_SUBMIT);
    516 		cv_broadcast(&tp->t_outq.c_cvf);
    517 	}
    518 }
    519 
    520 void
    521 ptcwakeup(tp, flag)
    522 	struct tty *tp;
    523 	int flag;
    524 {
    525 	struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
    526 
    527 	mutex_spin_enter(&tty_lock);
    528 	if (flag & FREAD) {
    529 		selnotify(&pti->pt_selr, NOTE_SUBMIT);
    530 		cv_broadcast(&tp->t_outq.c_cvf);
    531 	}
    532 	if (flag & FWRITE) {
    533 		selnotify(&pti->pt_selw, NOTE_SUBMIT);
    534 		cv_broadcast(&tp->t_rawq.c_cvf);
    535 	}
    536 	mutex_spin_exit(&tty_lock);
    537 }
    538 
    539 /*ARGSUSED*/
    540 int
    541 ptcopen(dev_t dev, int flag, int devtype, struct lwp *l)
    542 {
    543 	struct pt_softc *pti;
    544 	struct tty *tp;
    545 	int error;
    546 	int ptn = minor(dev);
    547 
    548 	if ((error = pty_check(ptn)) != 0)
    549 		return (error);
    550 
    551 	pti = pt_softc[ptn];
    552 	tp = pti->pt_tty;
    553 
    554 	mutex_spin_enter(&tty_lock);
    555 	if (tp->t_oproc) {
    556 		mutex_spin_exit(&tty_lock);
    557 		return (EIO);
    558 	}
    559 	tp->t_oproc = ptsstart;
    560 	mutex_spin_exit(&tty_lock);
    561 	(void)(*tp->t_linesw->l_modem)(tp, 1);
    562 	CLR(tp->t_lflag, EXTPROC);
    563 	pti->pt_flags = 0;
    564 	pti->pt_send = 0;
    565 	pti->pt_ucntl = 0;
    566 	return (0);
    567 }
    568 
    569 /*ARGSUSED*/
    570 int
    571 ptcclose(dev_t dev, int flag, int devtype, struct lwp *l)
    572 {
    573 	struct pt_softc *pti = pt_softc[minor(dev)];
    574 	struct tty *tp = pti->pt_tty;
    575 
    576 	(void)(*tp->t_linesw->l_modem)(tp, 0);
    577 	mutex_spin_enter(&tty_lock);
    578 	CLR(tp->t_state, TS_CARR_ON);
    579 	tp->t_oproc = 0;		/* mark closed */
    580 	mutex_spin_exit(&tty_lock);
    581 	return (0);
    582 }
    583 
    584 int
    585 ptcread(dev, uio, flag)
    586 	dev_t dev;
    587 	struct uio *uio;
    588 	int flag;
    589 {
    590 	struct pt_softc *pti = pt_softc[minor(dev)];
    591 	struct tty *tp = pti->pt_tty;
    592 	u_char bf[BUFSIZ];
    593 	int error = 0, cc;
    594 
    595 	/*
    596 	 * We want to block until the slave
    597 	 * is open, and there's something to read;
    598 	 * but if we lost the slave or we're NBIO,
    599 	 * then return the appropriate error instead.
    600 	 */
    601 	mutex_spin_enter(&tty_lock);
    602 	for (;;) {
    603 		if (ISSET(tp->t_state, TS_ISOPEN)) {
    604 			if (pti->pt_flags & PF_PKT && pti->pt_send) {
    605 				mutex_spin_exit(&tty_lock);
    606 				error = ureadc((int)pti->pt_send, uio);
    607 				if (error)
    608 					return (error);
    609 				/*
    610 				 * Since we don't have the tty locked, there's
    611 				 * a risk of messing up `t_termios'. This is
    612 				 * relevant only if the tty got closed and then
    613 				 * opened again while we were out uiomoving.
    614 				 */
    615 				if (pti->pt_send & TIOCPKT_IOCTL) {
    616 					cc = min(uio->uio_resid,
    617 						sizeof(tp->t_termios));
    618 					uiomove((void *) &tp->t_termios,
    619 						cc, uio);
    620 				}
    621 				pti->pt_send = 0;
    622 				return (0);
    623 			}
    624 			if (pti->pt_flags & PF_UCNTL && pti->pt_ucntl) {
    625 				mutex_spin_exit(&tty_lock);
    626 				error = ureadc((int)pti->pt_ucntl, uio);
    627 				if (error)
    628 					return (error);
    629 				pti->pt_ucntl = 0;
    630 				return (0);
    631 			}
    632 			if (tp->t_outq.c_cc && !ISSET(tp->t_state, TS_TTSTOP))
    633 				break;
    634 		}
    635 		if (!ISSET(tp->t_state, TS_CARR_ON)) {
    636 			error = 0;	/* EOF */
    637 			goto out;
    638 		}
    639 		if (flag & IO_NDELAY) {
    640 			error = EWOULDBLOCK;
    641 			goto out;
    642 		}
    643 		error = cv_wait_sig(&tp->t_outq.c_cvf, &tty_lock);
    644 		if (error)
    645 			goto out;
    646 	}
    647 
    648 	if (pti->pt_flags & (PF_PKT|PF_UCNTL)) {
    649 		mutex_spin_exit(&tty_lock);
    650 		error = ureadc(0, uio);
    651 		mutex_spin_enter(&tty_lock);
    652 		if (error == 0 && !ISSET(tp->t_state, TS_ISOPEN))
    653 			error = EIO;
    654 	}
    655 	while (uio->uio_resid > 0 && error == 0) {
    656 		cc = q_to_b(&tp->t_outq, bf, min(uio->uio_resid, BUFSIZ));
    657 		if (cc <= 0)
    658 			break;
    659 		mutex_spin_exit(&tty_lock);
    660 		error = uiomove(bf, cc, uio);
    661 		mutex_spin_enter(&tty_lock);
    662 		if (error == 0 && !ISSET(tp->t_state, TS_ISOPEN))
    663 			error = EIO;
    664 	}
    665 	ttypull(tp);
    666 out:
    667 	mutex_spin_exit(&tty_lock);
    668 	return (error);
    669 }
    670 
    671 
    672 int
    673 ptcwrite(dev, uio, flag)
    674 	dev_t dev;
    675 	struct uio *uio;
    676 	int flag;
    677 {
    678 	struct pt_softc *pti = pt_softc[minor(dev)];
    679 	struct tty *tp = pti->pt_tty;
    680 	u_char *cp = NULL;
    681 	int cc = 0;
    682 	u_char locbuf[BUFSIZ];
    683 	int cnt = 0;
    684 	int error = 0;
    685 
    686 again:
    687 	mutex_spin_enter(&tty_lock);
    688 	if (!ISSET(tp->t_state, TS_ISOPEN))
    689 		goto block;
    690 	if (pti->pt_flags & PF_REMOTE) {
    691 		if (tp->t_canq.c_cc)
    692 			goto block;
    693 		while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) {
    694 			if (cc == 0) {
    695 				cc = min(uio->uio_resid, BUFSIZ);
    696 				cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
    697 				cp = locbuf;
    698 				mutex_spin_exit(&tty_lock);
    699 				error = uiomove((void *)cp, cc, uio);
    700 				if (error)
    701 					return (error);
    702 				mutex_spin_enter(&tty_lock);
    703 				/* check again for safety */
    704 				if (!ISSET(tp->t_state, TS_ISOPEN)) {
    705 					/*
    706 					 * adjust for data copied in but not
    707 					 * written
    708 					 */
    709 					uio->uio_resid += cc;
    710 					error = EIO;
    711 					goto out;
    712 				}
    713 			}
    714 			if (cc)
    715 				(void) b_to_q(cp, cc, &tp->t_canq);
    716 			cc = 0;
    717 		}
    718 		(void) putc(0, &tp->t_canq);
    719 		ttwakeup(tp);
    720 		clwakeup(&tp->t_canq);
    721 		error = 0;
    722 		goto out;
    723 	}
    724 	while (uio->uio_resid > 0) {
    725 		if (cc == 0) {
    726 			cc = min(uio->uio_resid, BUFSIZ);
    727 			cp = locbuf;
    728 			mutex_spin_exit(&tty_lock);
    729 			error = uiomove((void *)cp, cc, uio);
    730 			if (error)
    731 				return (error);
    732 			mutex_spin_enter(&tty_lock);
    733 			/* check again for safety */
    734 			if (!ISSET(tp->t_state, TS_ISOPEN)) {
    735 				/* adjust for data copied in but not written */
    736 				uio->uio_resid += cc;
    737 				error = EIO;
    738 				goto out;
    739 			}
    740 		}
    741 		while (cc > 0) {
    742 			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
    743 			   (tp->t_canq.c_cc > 0 || !ISSET(tp->t_lflag, ICANON))) {
    744 				clwakeup(&tp->t_rawq);
    745 				goto block;
    746 			}
    747 			/* XXX - should change l_rint to be called with lock
    748 			 *	 see also tty.c:ttyinput_wlock()
    749 			 */
    750 			mutex_spin_exit(&tty_lock);
    751 			(*tp->t_linesw->l_rint)(*cp++, tp);
    752 			mutex_spin_enter(&tty_lock);
    753 			cnt++;
    754 			cc--;
    755 		}
    756 		cc = 0;
    757 	}
    758 	error = 0;
    759 	goto out;
    760 
    761 block:
    762 	/*
    763 	 * Come here to wait for slave to open, for space
    764 	 * in outq, or space in rawq.
    765 	 */
    766 	if (!ISSET(tp->t_state, TS_CARR_ON)) {
    767 		/* adjust for data copied in but not written */
    768 		uio->uio_resid += cc;
    769 		error = EIO;
    770 		goto out;
    771 	}
    772 	if (flag & IO_NDELAY) {
    773 		/* adjust for data copied in but not written */
    774 		uio->uio_resid += cc;
    775 		error = cnt == 0 ? EWOULDBLOCK : 0;
    776 		goto out;
    777 	}
    778 	error = cv_wait_sig(&tp->t_rawq.c_cv, &tty_lock);
    779 	mutex_spin_exit(&tty_lock);
    780 	if (error) {
    781 		/* adjust for data copied in but not written */
    782 		uio->uio_resid += cc;
    783 		return (error);
    784 	}
    785 	goto again;
    786 
    787 out:
    788 	mutex_spin_exit(&tty_lock);
    789 	return (error);
    790 }
    791 
    792 int
    793 ptcpoll(dev, events, l)
    794 	dev_t dev;
    795 	int events;
    796 	struct lwp *l;
    797 {
    798 	struct pt_softc *pti = pt_softc[minor(dev)];
    799 	struct tty *tp = pti->pt_tty;
    800 	int revents = 0;
    801 
    802 	mutex_spin_enter(&tty_lock);
    803 
    804 	if (events & (POLLIN | POLLRDNORM))
    805 		if (ISSET(tp->t_state, TS_ISOPEN) &&
    806 		    ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
    807 		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
    808 		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
    809 			revents |= events & (POLLIN | POLLRDNORM);
    810 
    811 	if (events & (POLLOUT | POLLWRNORM))
    812 		if (ISSET(tp->t_state, TS_ISOPEN) &&
    813 		    ((pti->pt_flags & PF_REMOTE) ?
    814 		     (tp->t_canq.c_cc == 0) :
    815 		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
    816 		      (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))))
    817 			revents |= events & (POLLOUT | POLLWRNORM);
    818 
    819 	if (events & POLLHUP)
    820 		if (!ISSET(tp->t_state, TS_CARR_ON))
    821 			revents |= POLLHUP;
    822 
    823 	if (revents == 0) {
    824 		if (events & (POLLIN | POLLHUP | POLLRDNORM))
    825 			selrecord(l, &pti->pt_selr);
    826 
    827 		if (events & (POLLOUT | POLLWRNORM))
    828 			selrecord(l, &pti->pt_selw);
    829 	}
    830 
    831 	mutex_spin_exit(&tty_lock);
    832 
    833 	return (revents);
    834 }
    835 
    836 static void
    837 filt_ptcrdetach(struct knote *kn)
    838 {
    839 	struct pt_softc *pti;
    840 	struct tty	*tp;
    841 
    842 	pti = kn->kn_hook;
    843 	tp = pti->pt_tty;
    844 
    845 	mutex_spin_enter(&tty_lock);
    846 	SLIST_REMOVE(&pti->pt_selr.sel_klist, kn, knote, kn_selnext);
    847 	mutex_spin_exit(&tty_lock);
    848 }
    849 
    850 static int
    851 filt_ptcread(struct knote *kn, long hint)
    852 {
    853 	struct pt_softc *pti;
    854 	struct tty	*tp;
    855 	int canread;
    856 
    857 	pti = kn->kn_hook;
    858 	tp = pti->pt_tty;
    859 
    860 	mutex_spin_enter(&tty_lock);
    861 
    862 	canread = (ISSET(tp->t_state, TS_ISOPEN) &&
    863 		    ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
    864 		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
    865 		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)));
    866 
    867 	if (canread) {
    868 		/*
    869 		 * c_cc is number of characters after output post-processing;
    870 		 * the amount of data actually read(2) depends on
    871 		 * setting of input flags for the terminal.
    872 		 */
    873 		kn->kn_data = tp->t_outq.c_cc;
    874 		if (((pti->pt_flags & PF_PKT) && pti->pt_send) ||
    875 		    ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))
    876 			kn->kn_data++;
    877 	}
    878 
    879 	mutex_spin_exit(&tty_lock);
    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 	mutex_spin_enter(&tty_lock);
    910 
    911 	canwrite = (ISSET(tp->t_state, TS_ISOPEN) &&
    912 		    ((pti->pt_flags & PF_REMOTE) ?
    913 		     (tp->t_canq.c_cc == 0) :
    914 		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
    915 		      (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))));
    916 
    917 	if (canwrite) {
    918 		if (pti->pt_flags & PF_REMOTE)
    919 			nwrite = tp->t_canq.c_cn;
    920 		else {
    921 			/* this is guaranteed to be > 0 due to above check */
    922 			nwrite = tp->t_canq.c_cn
    923 				- (tp->t_rawq.c_cc + tp->t_canq.c_cc);
    924 		}
    925 		kn->kn_data = nwrite;
    926 	}
    927 
    928 	mutex_spin_exit(&tty_lock);
    929 
    930 	return (canwrite);
    931 }
    932 
    933 static const struct filterops ptcread_filtops =
    934 	{ 1, NULL, filt_ptcrdetach, filt_ptcread };
    935 static const struct filterops ptcwrite_filtops =
    936 	{ 1, NULL, filt_ptcwdetach, filt_ptcwrite };
    937 
    938 int
    939 ptckqfilter(dev_t dev, struct knote *kn)
    940 {
    941 	struct pt_softc *pti = pt_softc[minor(dev)];
    942 	struct klist	*klist;
    943 
    944 	switch (kn->kn_filter) {
    945 	case EVFILT_READ:
    946 		klist = &pti->pt_selr.sel_klist;
    947 		kn->kn_fop = &ptcread_filtops;
    948 		break;
    949 	case EVFILT_WRITE:
    950 		klist = &pti->pt_selw.sel_klist;
    951 		kn->kn_fop = &ptcwrite_filtops;
    952 		break;
    953 	default:
    954 		return (EINVAL);
    955 	}
    956 
    957 	kn->kn_hook = pti;
    958 
    959 	mutex_spin_enter(&tty_lock);
    960 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    961 	mutex_spin_exit(&tty_lock);
    962 
    963 	return (0);
    964 }
    965 
    966 struct tty *
    967 ptytty(dev)
    968 	dev_t dev;
    969 {
    970 	struct pt_softc *pti = pt_softc[minor(dev)];
    971 	struct tty *tp = pti->pt_tty;
    972 
    973 	return (tp);
    974 }
    975 
    976 /*ARGSUSED*/
    977 int
    978 ptyioctl(dev, cmd, data, flag, l)
    979 	dev_t dev;
    980 	u_long cmd;
    981 	void *data;
    982 	int flag;
    983 	struct lwp *l;
    984 {
    985 	struct pt_softc *pti = pt_softc[minor(dev)];
    986 	struct tty *tp = pti->pt_tty;
    987 	const struct cdevsw *cdev;
    988 	u_char *cc = tp->t_cc;
    989 	int stop, error, sig;
    990 
    991 	/*
    992 	 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
    993 	 * ttywflush(tp) will hang if there are characters in the outq.
    994 	 */
    995 	if (cmd == TIOCEXT) {
    996 		/*
    997 		 * When the EXTPROC bit is being toggled, we need
    998 		 * to send an TIOCPKT_IOCTL if the packet driver
    999 		 * is turned on.
   1000 		 */
   1001 		if (*(int *)data) {
   1002 			if (pti->pt_flags & PF_PKT) {
   1003 				pti->pt_send |= TIOCPKT_IOCTL;
   1004 				ptcwakeup(tp, FREAD);
   1005 			}
   1006 			SET(tp->t_lflag, EXTPROC);
   1007 		} else {
   1008 			if (ISSET(tp->t_lflag, EXTPROC) &&
   1009 			    (pti->pt_flags & PF_PKT)) {
   1010 				pti->pt_send |= TIOCPKT_IOCTL;
   1011 				ptcwakeup(tp, FREAD);
   1012 			}
   1013 			CLR(tp->t_lflag, EXTPROC);
   1014 		}
   1015 		return(0);
   1016 	}
   1017 
   1018 #ifndef NO_DEV_PTM
   1019 	/* Allow getting the name from either the master or the slave */
   1020 	if (cmd == TIOCPTSNAME)
   1021 		return pty_fill_ptmget(l, dev, -1, -1, data);
   1022 #endif
   1023 
   1024 	cdev = cdevsw_lookup(dev);
   1025 	if (cdev != NULL && cdev->d_open == ptcopen)
   1026 		switch (cmd) {
   1027 #ifndef NO_DEV_PTM
   1028 		case TIOCGRANTPT:
   1029 			return pty_grant_slave(l, dev);
   1030 #endif
   1031 
   1032 		case TIOCGPGRP:
   1033 			/*
   1034 			 * We avoid calling ttioctl on the controller since,
   1035 			 * in that case, tp must be the controlling terminal.
   1036 			 */
   1037 			*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
   1038 			return (0);
   1039 
   1040 		case TIOCPKT:
   1041 			if (*(int *)data) {
   1042 				if (pti->pt_flags & PF_UCNTL)
   1043 					return (EINVAL);
   1044 				pti->pt_flags |= PF_PKT;
   1045 			} else
   1046 				pti->pt_flags &= ~PF_PKT;
   1047 			return (0);
   1048 
   1049 		case TIOCUCNTL:
   1050 			if (*(int *)data) {
   1051 				if (pti->pt_flags & PF_PKT)
   1052 					return (EINVAL);
   1053 				pti->pt_flags |= PF_UCNTL;
   1054 			} else
   1055 				pti->pt_flags &= ~PF_UCNTL;
   1056 			return (0);
   1057 
   1058 		case TIOCREMOTE:
   1059 			if (*(int *)data)
   1060 				pti->pt_flags |= PF_REMOTE;
   1061 			else
   1062 				pti->pt_flags &= ~PF_REMOTE;
   1063 			mutex_spin_enter(&tty_lock);
   1064 			ttyflush(tp, FREAD|FWRITE);
   1065 			mutex_spin_exit(&tty_lock);
   1066 			return (0);
   1067 
   1068 #ifdef COMPAT_OLDTTY
   1069 		case TIOCSETP:
   1070 		case TIOCSETN:
   1071 #endif
   1072 		case TIOCSETD:
   1073 		case TIOCSETA:
   1074 		case TIOCSETAW:
   1075 		case TIOCSETAF:
   1076 			mutex_spin_enter(&tty_lock);
   1077 			ndflush(&tp->t_outq, tp->t_outq.c_cc);
   1078 			mutex_spin_exit(&tty_lock);
   1079 			break;
   1080 
   1081 		case TIOCSIG:
   1082 			sig = (int)(long)*(void **)data;
   1083 			if (sig <= 0 || sig >= NSIG)
   1084 				return (EINVAL);
   1085 			mutex_spin_enter(&tty_lock);
   1086 			if (!ISSET(tp->t_lflag, NOFLSH))
   1087 				ttyflush(tp, FREAD|FWRITE);
   1088 			if ((sig == SIGINFO) &&
   1089 			    (!ISSET(tp->t_lflag, NOKERNINFO)))
   1090 				ttyinfo(tp, 1);
   1091 			ttysig(tp, TTYSIG_PG1, sig);
   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 #ifdef COMPAT_OLDTTY
   1118 		case TIOCSETP:
   1119 		case TIOCSETN:
   1120 		case TIOCSETC:
   1121 		case TIOCSLTC:
   1122 		case TIOCLBIS:
   1123 		case TIOCLBIC:
   1124 		case TIOCLSET:
   1125 #endif
   1126 			pti->pt_send |= TIOCPKT_IOCTL;
   1127 			ptcwakeup(tp, FREAD);
   1128 		default:
   1129 			break;
   1130 		}
   1131 	}
   1132 	stop = ISSET(tp->t_iflag, IXON) && CCEQ(cc[VSTOP], CTRL('s'))
   1133 		&& CCEQ(cc[VSTART], CTRL('q'));
   1134 	if (pti->pt_flags & PF_NOSTOP) {
   1135 		if (stop) {
   1136 			pti->pt_send &= ~TIOCPKT_NOSTOP;
   1137 			pti->pt_send |= TIOCPKT_DOSTOP;
   1138 			pti->pt_flags &= ~PF_NOSTOP;
   1139 			ptcwakeup(tp, FREAD);
   1140 		}
   1141 	} else {
   1142 		if (!stop) {
   1143 			pti->pt_send &= ~TIOCPKT_DOSTOP;
   1144 			pti->pt_send |= TIOCPKT_NOSTOP;
   1145 			pti->pt_flags |= PF_NOSTOP;
   1146 			ptcwakeup(tp, FREAD);
   1147 		}
   1148 	}
   1149 	return (error);
   1150 }
   1151