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