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