Home | History | Annotate | Line # | Download | only in kern
tty_pty.c revision 1.56.2.8
      1 /*	$NetBSD: tty_pty.c,v 1.56.2.8 2002/09/29 09:59:15 jdolecek 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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)tty_pty.c	8.4 (Berkeley) 2/20/95
     36  */
     37 
     38 /*
     39  * Pseudo-teletype Driver
     40  * (Actually two drivers, requiring two entries in 'cdevsw')
     41  */
     42 
     43 #include <sys/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: tty_pty.c,v 1.56.2.8 2002/09/29 09:59:15 jdolecek Exp $");
     45 
     46 #include "opt_compat_sunos.h"
     47 
     48 #include <sys/param.h>
     49 #include <sys/systm.h>
     50 #include <sys/ioctl.h>
     51 #include <sys/proc.h>
     52 #include <sys/tty.h>
     53 #include <sys/file.h>
     54 #include <sys/uio.h>
     55 #include <sys/kernel.h>
     56 #include <sys/vnode.h>
     57 #include <sys/signalvar.h>
     58 #include <sys/uio.h>
     59 #include <sys/conf.h>
     60 #include <sys/poll.h>
     61 #include <sys/malloc.h>
     62 
     63 #define	DEFAULT_NPTYS		16	/* default number of initial ptys */
     64 #define DEFAULT_MAXPTYS		992	/* default maximum number of ptys */
     65 
     66 /* Macros to clear/set/test flags. */
     67 #define	SET(t, f)	(t) |= (f)
     68 #define	CLR(t, f)	(t) &= ~((unsigned)(f))
     69 #define	ISSET(t, f)	((t) & (f))
     70 
     71 #define BUFSIZ 100		/* Chunk size iomoved to/from user */
     72 
     73 /*
     74  * pts == /dev/tty[pqrs]?
     75  * ptc == /dev/pty[pqrs]?
     76  */
     77 struct	pt_softc {
     78 	struct	tty *pt_tty;
     79 	int	pt_flags;
     80 	struct	selinfo pt_selr, pt_selw;
     81 	u_char	pt_send;
     82 	u_char	pt_ucntl;
     83 };
     84 
     85 static struct pt_softc **pt_softc = NULL;	/* pty array */
     86 static int npty = 0;			/* for pstat -t */
     87 static int maxptys = DEFAULT_MAXPTYS;	/* maximum number of ptys (sysctable) */
     88 static struct simplelock pt_softc_mutex = SIMPLELOCK_INITIALIZER;
     89 
     90 #define	PF_PKT		0x08		/* packet mode */
     91 #define	PF_STOPPED	0x10		/* user told stopped */
     92 #define	PF_REMOTE	0x20		/* remote and flow controlled input */
     93 #define	PF_NOSTOP	0x40
     94 #define PF_UCNTL	0x80		/* user control mode */
     95 
     96 void	ptyattach __P((int));
     97 void	ptcwakeup __P((struct tty *, int));
     98 int	ptcopen __P((dev_t, int, int, struct proc *));
     99 struct tty *ptytty __P((dev_t));
    100 void	ptsstart __P((struct tty *));
    101 int	pty_maxptys __P((int, int));
    102 
    103 static struct pt_softc **ptyarralloc __P((int));
    104 static int check_pty __P((dev_t));
    105 
    106 /*
    107  * Allocate and zero array of nelem elements.
    108  */
    109 static struct pt_softc **
    110 ptyarralloc(nelem)
    111 	int nelem;
    112 {
    113 	struct pt_softc **pt;
    114 	nelem += 10;
    115 	pt = malloc(nelem * sizeof(struct pt_softc *), M_DEVBUF, M_WAITOK);
    116 	memset(pt, '\0', nelem * sizeof(struct pt_softc *));
    117 	return pt;
    118 }
    119 
    120 /*
    121  * Check if the minor is correct and ensure necessary structures
    122  * are properly allocated.
    123  */
    124 static int
    125 check_pty(dev)
    126 	dev_t dev;
    127 {
    128 	struct pt_softc *pti;
    129 
    130 	if (minor(dev) >= npty) {
    131 		struct pt_softc **newpt;
    132 		int newnpty;
    133 
    134 		/* check if the requested pty can be granted */
    135 		if (minor(dev) >= maxptys) {
    136 	    limit_reached:
    137 			tablefull("pty", "increase kern.maxptys");
    138 			return (ENXIO);
    139 		}
    140 
    141 		/*
    142 		 * Now grab the pty array mutex - we need to ensure
    143 		 * that the pty array is consistent while copying it's
    144 		 * content to newly allocated, larger space; we also
    145 		 * need to be safe against pty_maxptys().
    146 		 */
    147 		simple_lock(&pt_softc_mutex);
    148 
    149 		do {
    150 			for(newnpty = npty; newnpty <= minor(dev);
    151 				newnpty *= 2);
    152 
    153 			if (newnpty > maxptys)
    154 				newnpty = maxptys;
    155 
    156 			simple_unlock(&pt_softc_mutex);
    157 			newpt = ptyarralloc(newnpty);
    158 			simple_lock(&pt_softc_mutex);
    159 
    160 			if (maxptys == npty) {
    161 				simple_unlock(&pt_softc_mutex);
    162 				goto limit_reached;
    163 			}
    164 		} while(newnpty > maxptys);
    165 
    166 		/*
    167 		 * If the pty array was not enlarged while we were waiting
    168 		 * for mutex, copy current contents of pt_softc[] to newly
    169 		 * allocated array and start using the new bigger array.
    170 		 */
    171 		if (minor(dev) >= npty) {
    172 			memcpy(newpt, pt_softc, npty*sizeof(struct pt_softc *));
    173 			free(pt_softc, M_DEVBUF);
    174 
    175 			pt_softc = newpt;
    176 			npty = newnpty;
    177 		} else {
    178 			/* was enlarged when waited fot lock, free new space */
    179 			free(newpt, M_DEVBUF);
    180 		}
    181 
    182 		simple_unlock(&pt_softc_mutex);
    183 	}
    184 
    185 	/*
    186 	 * If the entry is not yet allocated, allocate one. The mutex is
    187 	 * needed so that the state of pt_softc[] array is consistant
    188 	 * in case it has been longened above.
    189 	 */
    190 	if (!pt_softc[minor(dev)]) {
    191 		MALLOC(pti, struct pt_softc *, sizeof(struct pt_softc),
    192 			M_DEVBUF, M_WAITOK);
    193 		memset(pti, 0, sizeof(struct pt_softc));
    194 
    195 	 	pti->pt_tty = ttymalloc();
    196 
    197 		simple_lock(&pt_softc_mutex);
    198 
    199 		/*
    200 		 * Check the entry again - it might have been
    201 		 * added while we were waiting for mutex.
    202 		 */
    203 		if (!pt_softc[minor(dev)]) {
    204 			tty_attach(pti->pt_tty);
    205 			pt_softc[minor(dev)] = pti;
    206 		} else {
    207 			ttyfree(pti->pt_tty);
    208 			FREE(pti, M_DEVBUF);
    209 		}
    210 
    211 		simple_unlock(&pt_softc_mutex);
    212 	}
    213 
    214 	return (0);
    215 }
    216 
    217 /*
    218  * Set maxpty in thread-safe way. Returns 0 in case of error, otherwise
    219  * new value of maxptys.
    220  */
    221 int
    222 pty_maxptys(newmax, set)
    223 	int newmax, set;
    224 {
    225 	if (!set)
    226 		return (maxptys);
    227 
    228 	/* the value cannot be set to value lower than current number of ptys */
    229 	if (newmax < npty)
    230 		return (0);
    231 
    232 	/* can proceed immediatelly if bigger than current maximum */
    233 	if (newmax > maxptys) {
    234 		maxptys = newmax;
    235 		return (maxptys);
    236 	}
    237 
    238 	/*
    239 	 * We have to grab the pt_softc lock, so that we would pick correct
    240 	 * value of npty (might be modified in check_pty()).
    241 	 */
    242 	simple_lock(&pt_softc_mutex);
    243 
    244 	if (newmax > npty)
    245 		maxptys = newmax;
    246 
    247 	simple_unlock(&pt_softc_mutex);
    248 
    249 	return (maxptys);
    250 }
    251 
    252 /*
    253  * Establish n (or default if n is 1) ptys in the system.
    254  */
    255 void
    256 ptyattach(n)
    257 	int n;
    258 {
    259 	/* maybe should allow 0 => none? */
    260 	if (n <= 1)
    261 		n = DEFAULT_NPTYS;
    262 	pt_softc = ptyarralloc(n);
    263 	npty = n;
    264 }
    265 
    266 /*ARGSUSED*/
    267 int
    268 ptsopen(dev, flag, devtype, p)
    269 	dev_t dev;
    270 	int flag, devtype;
    271 	struct proc *p;
    272 {
    273 	struct pt_softc *pti;
    274 	struct tty *tp;
    275 	int error;
    276 
    277 	if ((error = check_pty(dev)))
    278 		return (error);
    279 
    280 	pti = pt_softc[minor(dev)];
    281 	tp = pti->pt_tty;
    282 
    283 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
    284 		ttychars(tp);		/* Set up default chars */
    285 		tp->t_iflag = TTYDEF_IFLAG;
    286 		tp->t_oflag = TTYDEF_OFLAG;
    287 		tp->t_lflag = TTYDEF_LFLAG;
    288 		tp->t_cflag = TTYDEF_CFLAG;
    289 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    290 		ttsetwater(tp);		/* would be done in xxparam() */
    291 	} else if (ISSET(tp->t_state, TS_XCLUDE) && p->p_ucred->cr_uid != 0)
    292 		return (EBUSY);
    293 	if (tp->t_oproc)			/* Ctrlr still around. */
    294 		SET(tp->t_state, TS_CARR_ON);
    295 	if (!ISSET(flag, O_NONBLOCK))
    296 		while (!ISSET(tp->t_state, TS_CARR_ON)) {
    297 			tp->t_wopen++;
    298 			error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
    299 			    ttopen, 0);
    300 			tp->t_wopen--;
    301 			if (error)
    302 				return (error);
    303 		}
    304 	error = (*tp->t_linesw->l_open)(dev, tp);
    305 	ptcwakeup(tp, FREAD|FWRITE);
    306 	return (error);
    307 }
    308 
    309 int
    310 ptsclose(dev, flag, mode, p)
    311 	dev_t dev;
    312 	int flag, mode;
    313 	struct proc *p;
    314 {
    315 	struct pt_softc *pti = pt_softc[minor(dev)];
    316 	struct tty *tp = pti->pt_tty;
    317 	int error;
    318 
    319 	error = (*tp->t_linesw->l_close)(tp, flag);
    320 	error |= ttyclose(tp);
    321 	ptcwakeup(tp, FREAD|FWRITE);
    322 	return (error);
    323 }
    324 
    325 int
    326 ptsread(dev, uio, flag)
    327 	dev_t dev;
    328 	struct uio *uio;
    329 	int flag;
    330 {
    331 	struct proc *p = curproc;
    332 	struct pt_softc *pti = pt_softc[minor(dev)];
    333 	struct tty *tp = pti->pt_tty;
    334 	int error = 0;
    335 
    336 again:
    337 	if (pti->pt_flags & PF_REMOTE) {
    338 		while (isbackground(p, tp)) {
    339 			if (sigismasked(p, SIGTTIN) ||
    340 			    p->p_pgrp->pg_jobc == 0 ||
    341 			    p->p_flag & P_PPWAIT)
    342 				return (EIO);
    343 			pgsignal(p->p_pgrp, SIGTTIN, 1);
    344 			error = ttysleep(tp, (caddr_t)&lbolt,
    345 					 TTIPRI | PCATCH, ttybg, 0);
    346 			if (error)
    347 				return (error);
    348 		}
    349 		if (tp->t_canq.c_cc == 0) {
    350 			if (flag & IO_NDELAY)
    351 				return (EWOULDBLOCK);
    352 			error = ttysleep(tp, (caddr_t)&tp->t_canq,
    353 					 TTIPRI | PCATCH, ttyin, 0);
    354 			if (error)
    355 				return (error);
    356 			goto again;
    357 		}
    358 		while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
    359 			if (ureadc(getc(&tp->t_canq), uio) < 0) {
    360 				error = EFAULT;
    361 				break;
    362 			}
    363 		if (tp->t_canq.c_cc == 1)
    364 			(void) getc(&tp->t_canq);
    365 		if (tp->t_canq.c_cc)
    366 			return (error);
    367 	} else
    368 		if (tp->t_oproc)
    369 			error = (*tp->t_linesw->l_read)(tp, uio, flag);
    370 	ptcwakeup(tp, FWRITE);
    371 	return (error);
    372 }
    373 
    374 /*
    375  * Write to pseudo-tty.
    376  * Wakeups of controlling tty will happen
    377  * indirectly, when tty driver calls ptsstart.
    378  */
    379 int
    380 ptswrite(dev, uio, flag)
    381 	dev_t dev;
    382 	struct uio *uio;
    383 	int flag;
    384 {
    385 	struct pt_softc *pti = pt_softc[minor(dev)];
    386 	struct tty *tp = pti->pt_tty;
    387 
    388 	if (tp->t_oproc == 0)
    389 		return (EIO);
    390 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    391 }
    392 
    393 /*
    394  * Poll pseudo-tty.
    395  */
    396 int
    397 ptspoll(dev, events, p)
    398 	dev_t dev;
    399 	int events;
    400 	struct proc *p;
    401 {
    402 	struct pt_softc *pti = pt_softc[minor(dev)];
    403 	struct tty *tp = pti->pt_tty;
    404 
    405 	if (tp->t_oproc == 0)
    406 		return (EIO);
    407 
    408 	return ((*tp->t_linesw->l_poll)(tp, events, p));
    409 }
    410 
    411 /*
    412  * Start output on pseudo-tty.
    413  * Wake up process polling or sleeping for input from controlling tty.
    414  */
    415 void
    416 ptsstart(tp)
    417 	struct tty *tp;
    418 {
    419 	struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
    420 
    421 	if (ISSET(tp->t_state, TS_TTSTOP))
    422 		return;
    423 	if (pti->pt_flags & PF_STOPPED) {
    424 		pti->pt_flags &= ~PF_STOPPED;
    425 		pti->pt_send = TIOCPKT_START;
    426 	}
    427 	ptcwakeup(tp, FREAD);
    428 }
    429 
    430 void
    431 ptsstop(tp, flush)
    432 	struct tty *tp;
    433 	int flush;
    434 {
    435 	struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
    436 	int flag;
    437 
    438 	/* note: FLUSHREAD and FLUSHWRITE already ok */
    439 	if (flush == 0) {
    440 		flush = TIOCPKT_STOP;
    441 		pti->pt_flags |= PF_STOPPED;
    442 	} else
    443 		pti->pt_flags &= ~PF_STOPPED;
    444 	pti->pt_send |= flush;
    445 	/* change of perspective */
    446 	flag = 0;
    447 	if (flush & FREAD)
    448 		flag |= FWRITE;
    449 	if (flush & FWRITE)
    450 		flag |= FREAD;
    451 	ptcwakeup(tp, flag);
    452 }
    453 
    454 void
    455 ptcwakeup(tp, flag)
    456 	struct tty *tp;
    457 	int flag;
    458 {
    459 	struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
    460 
    461 	if (flag & FREAD) {
    462 		selnotify(&pti->pt_selr, 0);
    463 		wakeup((caddr_t)&tp->t_outq.c_cf);
    464 	}
    465 	if (flag & FWRITE) {
    466 		selnotify(&pti->pt_selw, 0);
    467 		wakeup((caddr_t)&tp->t_rawq.c_cf);
    468 	}
    469 }
    470 
    471 /*ARGSUSED*/
    472 int
    473 ptcopen(dev, flag, devtype, p)
    474 	dev_t dev;
    475 	int flag, devtype;
    476 	struct proc *p;
    477 {
    478 	struct pt_softc *pti;
    479 	struct tty *tp;
    480 	int error;
    481 
    482 	if ((error = check_pty(dev)))
    483 		return (error);
    484 
    485 	pti = pt_softc[minor(dev)];
    486 	tp = pti->pt_tty;
    487 
    488 	if (tp->t_oproc)
    489 		return (EIO);
    490 	tp->t_oproc = ptsstart;
    491 	(void)(*tp->t_linesw->l_modem)(tp, 1);
    492 	CLR(tp->t_lflag, EXTPROC);
    493 	pti->pt_flags = 0;
    494 	pti->pt_send = 0;
    495 	pti->pt_ucntl = 0;
    496 	return (0);
    497 }
    498 
    499 /*ARGSUSED*/
    500 int
    501 ptcclose(dev, flag, devtype, p)
    502 	dev_t dev;
    503 	int flag, devtype;
    504 	struct proc *p;
    505 {
    506 	struct pt_softc *pti = pt_softc[minor(dev)];
    507 	struct tty *tp = pti->pt_tty;
    508 
    509 	(void)(*tp->t_linesw->l_modem)(tp, 0);
    510 	CLR(tp->t_state, TS_CARR_ON);
    511 	tp->t_oproc = 0;		/* mark closed */
    512 	return (0);
    513 }
    514 
    515 int
    516 ptcread(dev, uio, flag)
    517 	dev_t dev;
    518 	struct uio *uio;
    519 	int flag;
    520 {
    521 	struct pt_softc *pti = pt_softc[minor(dev)];
    522 	struct tty *tp = pti->pt_tty;
    523 	char buf[BUFSIZ];
    524 	int error = 0, cc;
    525 
    526 	/*
    527 	 * We want to block until the slave
    528 	 * is open, and there's something to read;
    529 	 * but if we lost the slave or we're NBIO,
    530 	 * then return the appropriate error instead.
    531 	 */
    532 	for (;;) {
    533 		if (ISSET(tp->t_state, TS_ISOPEN)) {
    534 			if (pti->pt_flags&PF_PKT && pti->pt_send) {
    535 				error = ureadc((int)pti->pt_send, uio);
    536 				if (error)
    537 					return (error);
    538 				if (pti->pt_send & TIOCPKT_IOCTL) {
    539 					cc = min(uio->uio_resid,
    540 						sizeof(tp->t_termios));
    541 					uiomove((caddr_t) &tp->t_termios,
    542 						cc, uio);
    543 				}
    544 				pti->pt_send = 0;
    545 				return (0);
    546 			}
    547 			if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
    548 				error = ureadc((int)pti->pt_ucntl, uio);
    549 				if (error)
    550 					return (error);
    551 				pti->pt_ucntl = 0;
    552 				return (0);
    553 			}
    554 			if (tp->t_outq.c_cc && !ISSET(tp->t_state, TS_TTSTOP))
    555 				break;
    556 		}
    557 		if (!ISSET(tp->t_state, TS_CARR_ON))
    558 			return (0);	/* EOF */
    559 		if (flag & IO_NDELAY)
    560 			return (EWOULDBLOCK);
    561 		error = tsleep((caddr_t)&tp->t_outq.c_cf, TTIPRI | PCATCH,
    562 			       ttyin, 0);
    563 		if (error)
    564 			return (error);
    565 	}
    566 	if (pti->pt_flags & (PF_PKT|PF_UCNTL))
    567 		error = ureadc(0, uio);
    568 	while (uio->uio_resid > 0 && error == 0) {
    569 		cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
    570 		if (cc <= 0)
    571 			break;
    572 		error = uiomove(buf, cc, uio);
    573 	}
    574 	if (tp->t_outq.c_cc <= tp->t_lowat) {
    575 		if (ISSET(tp->t_state, TS_ASLEEP)) {
    576 			CLR(tp->t_state, TS_ASLEEP);
    577 			wakeup((caddr_t)&tp->t_outq);
    578 		}
    579 		selnotify(&tp->t_wsel, 0);
    580 	}
    581 	return (error);
    582 }
    583 
    584 
    585 int
    586 ptcwrite(dev, uio, flag)
    587 	dev_t dev;
    588 	struct uio *uio;
    589 	int flag;
    590 {
    591 	struct pt_softc *pti = pt_softc[minor(dev)];
    592 	struct tty *tp = pti->pt_tty;
    593 	u_char *cp = NULL;
    594 	int cc = 0;
    595 	u_char locbuf[BUFSIZ];
    596 	int cnt = 0;
    597 	int error = 0;
    598 
    599 again:
    600 	if (!ISSET(tp->t_state, TS_ISOPEN))
    601 		goto block;
    602 	if (pti->pt_flags & PF_REMOTE) {
    603 		if (tp->t_canq.c_cc)
    604 			goto block;
    605 		while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) {
    606 			if (cc == 0) {
    607 				cc = min(uio->uio_resid, BUFSIZ);
    608 				cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
    609 				cp = locbuf;
    610 				error = uiomove((caddr_t)cp, cc, uio);
    611 				if (error)
    612 					return (error);
    613 				/* check again for safety */
    614 				if (!ISSET(tp->t_state, TS_ISOPEN))
    615 					return (EIO);
    616 			}
    617 			if (cc)
    618 				(void) b_to_q((char *)cp, cc, &tp->t_canq);
    619 			cc = 0;
    620 		}
    621 		(void) putc(0, &tp->t_canq);
    622 		ttwakeup(tp);
    623 		wakeup((caddr_t)&tp->t_canq);
    624 		return (0);
    625 	}
    626 	while (uio->uio_resid > 0) {
    627 		if (cc == 0) {
    628 			cc = min(uio->uio_resid, BUFSIZ);
    629 			cp = locbuf;
    630 			error = uiomove((caddr_t)cp, cc, uio);
    631 			if (error)
    632 				return (error);
    633 			/* check again for safety */
    634 			if (!ISSET(tp->t_state, TS_ISOPEN))
    635 				return (EIO);
    636 		}
    637 		while (cc > 0) {
    638 			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
    639 			   (tp->t_canq.c_cc > 0 || !ISSET(tp->t_lflag, ICANON))) {
    640 				wakeup((caddr_t)&tp->t_rawq);
    641 				goto block;
    642 			}
    643 			(*tp->t_linesw->l_rint)(*cp++, tp);
    644 			cnt++;
    645 			cc--;
    646 		}
    647 		cc = 0;
    648 	}
    649 	return (0);
    650 block:
    651 	/*
    652 	 * Come here to wait for slave to open, for space
    653 	 * in outq, or space in rawq.
    654 	 */
    655 	if (!ISSET(tp->t_state, TS_CARR_ON))
    656 		return (EIO);
    657 	if (flag & IO_NDELAY) {
    658 		/* adjust for data copied in but not written */
    659 		uio->uio_resid += cc;
    660 		if (cnt == 0)
    661 			return (EWOULDBLOCK);
    662 		return (0);
    663 	}
    664 	error = tsleep((caddr_t)&tp->t_rawq.c_cf, TTOPRI | PCATCH,
    665 		       ttyout, 0);
    666 	if (error) {
    667 		/* adjust for data copied in but not written */
    668 		uio->uio_resid += cc;
    669 		return (error);
    670 	}
    671 	goto again;
    672 }
    673 
    674 int
    675 ptcpoll(dev, events, p)
    676 	dev_t dev;
    677 	int events;
    678 	struct proc *p;
    679 {
    680 	struct pt_softc *pti = pt_softc[minor(dev)];
    681 	struct tty *tp = pti->pt_tty;
    682 	int revents = 0;
    683 	int s = splsoftclock();
    684 
    685 	if (events & (POLLIN | POLLRDNORM))
    686 		if (ISSET(tp->t_state, TS_ISOPEN) &&
    687 		    ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
    688 		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
    689 		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
    690 			revents |= events & (POLLIN | POLLRDNORM);
    691 
    692 	if (events & (POLLOUT | POLLWRNORM))
    693 		if (ISSET(tp->t_state, TS_ISOPEN) &&
    694 		    ((pti->pt_flags & PF_REMOTE) ?
    695 		     (tp->t_canq.c_cc == 0) :
    696 		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
    697 		      (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))))
    698 			revents |= events & (POLLOUT | POLLWRNORM);
    699 
    700 	if (events & POLLHUP)
    701 		if (!ISSET(tp->t_state, TS_CARR_ON))
    702 			revents |= POLLHUP;
    703 
    704 	if (revents == 0) {
    705 		if (events & (POLLIN | POLLHUP | POLLRDNORM))
    706 			selrecord(p, &pti->pt_selr);
    707 
    708 		if (events & (POLLOUT | POLLWRNORM))
    709 			selrecord(p, &pti->pt_selw);
    710 	}
    711 
    712 	splx(s);
    713 	return (revents);
    714 }
    715 
    716 static void
    717 filt_ptcrdetach(struct knote *kn)
    718 {
    719 	struct pt_softc *pti;
    720 	int		s;
    721 
    722 	pti = kn->kn_hook;
    723 	s = spltty();
    724 	SLIST_REMOVE(&pti->pt_selr.si_klist, kn, knote, kn_selnext);
    725 	splx(s);
    726 }
    727 
    728 static int
    729 filt_ptcread(struct knote *kn, long hint)
    730 {
    731 	struct pt_softc *pti;
    732 	struct tty	*tp;
    733 	int canread;
    734 
    735 	pti = kn->kn_hook;
    736 	tp = pti->pt_tty;
    737 
    738 	canread = (ISSET(tp->t_state, TS_ISOPEN) &&
    739 		    ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
    740 		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
    741 		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)));
    742 
    743 	if (canread) {
    744 		/*
    745 		 * c_cc is number of characters after output post-processing;
    746 		 * the amount of data actually read(2) depends on
    747 		 * setting of input flags for the terminal.
    748 		 */
    749 		kn->kn_data = tp->t_outq.c_cc;
    750 		if (((pti->pt_flags & PF_PKT) && pti->pt_send) ||
    751 		    ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))
    752 			kn->kn_data++;
    753 	}
    754 
    755 	return (canread);
    756 }
    757 
    758 static void
    759 filt_ptcwdetach(struct knote *kn)
    760 {
    761 	struct pt_softc *pti;
    762 	int		s;
    763 
    764 	pti = kn->kn_hook;
    765 	s = spltty();
    766 	SLIST_REMOVE(&pti->pt_selw.si_klist, kn, knote, kn_selnext);
    767 	splx(s);
    768 }
    769 
    770 static int
    771 filt_ptcwrite(struct knote *kn, long hint)
    772 {
    773 	struct pt_softc *pti;
    774 	struct tty	*tp;
    775 	int canwrite;
    776 	int nwrite;
    777 
    778 	pti = kn->kn_hook;
    779 	tp = pti->pt_tty;
    780 
    781 	canwrite = (ISSET(tp->t_state, TS_ISOPEN) &&
    782 		    ((pti->pt_flags & PF_REMOTE) ?
    783 		     (tp->t_canq.c_cc == 0) :
    784 		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
    785 		      (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))));
    786 
    787 	if (canwrite) {
    788 		if (pti->pt_flags & PF_REMOTE)
    789 			nwrite = tp->t_canq.c_cn;
    790 		else {
    791 			/* this is guaranteed to be > 0 due to above check */
    792 			nwrite = tp->t_canq.c_cn
    793 				- (tp->t_rawq.c_cc + tp->t_canq.c_cc);
    794 		}
    795 		kn->kn_data = nwrite;
    796 	}
    797 
    798 	return (canwrite);
    799 }
    800 
    801 static const struct filterops ptcread_filtops =
    802 	{ 1, NULL, filt_ptcrdetach, filt_ptcread };
    803 static const struct filterops ptcwrite_filtops =
    804 	{ 1, NULL, filt_ptcwdetach, filt_ptcwrite };
    805 
    806 int
    807 ptckqfilter(dev_t dev, struct knote *kn)
    808 {
    809 	struct pt_softc *pti = pt_softc[minor(dev)];
    810 	struct klist	*klist;
    811 	int		s;
    812 
    813 	switch (kn->kn_filter) {
    814 	case EVFILT_READ:
    815 		klist = &pti->pt_selr.si_klist;
    816 		kn->kn_fop = &ptcread_filtops;
    817 		break;
    818 	case EVFILT_WRITE:
    819 		klist = &pti->pt_selw.si_klist;
    820 		kn->kn_fop = &ptcwrite_filtops;
    821 		break;
    822 	default:
    823 		return (1);
    824 	}
    825 
    826 	kn->kn_hook = pti;
    827 
    828 	s = spltty();
    829 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    830 	splx(s);
    831 
    832 	return (0);
    833 }
    834 
    835 struct tty *
    836 ptytty(dev)
    837 	dev_t dev;
    838 {
    839 	struct pt_softc *pti = pt_softc[minor(dev)];
    840 	struct tty *tp = pti->pt_tty;
    841 
    842 	return (tp);
    843 }
    844 
    845 /*ARGSUSED*/
    846 int
    847 ptyioctl(dev, cmd, data, flag, p)
    848 	dev_t dev;
    849 	u_long cmd;
    850 	caddr_t data;
    851 	int flag;
    852 	struct proc *p;
    853 {
    854 	struct pt_softc *pti = pt_softc[minor(dev)];
    855 	struct tty *tp = pti->pt_tty;
    856 	u_char *cc = tp->t_cc;
    857 	int stop, error, sig;
    858 
    859 	/*
    860 	 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
    861 	 * ttywflush(tp) will hang if there are characters in the outq.
    862 	 */
    863 	if (cmd == TIOCEXT) {
    864 		/*
    865 		 * When the EXTPROC bit is being toggled, we need
    866 		 * to send an TIOCPKT_IOCTL if the packet driver
    867 		 * is turned on.
    868 		 */
    869 		if (*(int *)data) {
    870 			if (pti->pt_flags & PF_PKT) {
    871 				pti->pt_send |= TIOCPKT_IOCTL;
    872 				ptcwakeup(tp, FREAD);
    873 			}
    874 			SET(tp->t_lflag, EXTPROC);
    875 		} else {
    876 			if (ISSET(tp->t_lflag, EXTPROC) &&
    877 			    (pti->pt_flags & PF_PKT)) {
    878 				pti->pt_send |= TIOCPKT_IOCTL;
    879 				ptcwakeup(tp, FREAD);
    880 			}
    881 			CLR(tp->t_lflag, EXTPROC);
    882 		}
    883 		return(0);
    884 	} else
    885 	if (cdevsw[major(dev)].d_open == ptcopen)
    886 		switch (cmd) {
    887 
    888 		case TIOCGPGRP:
    889 			/*
    890 			 * We avoid calling ttioctl on the controller since,
    891 			 * in that case, tp must be the controlling terminal.
    892 			 */
    893 			*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
    894 			return (0);
    895 
    896 		case TIOCPKT:
    897 			if (*(int *)data) {
    898 				if (pti->pt_flags & PF_UCNTL)
    899 					return (EINVAL);
    900 				pti->pt_flags |= PF_PKT;
    901 			} else
    902 				pti->pt_flags &= ~PF_PKT;
    903 			return (0);
    904 
    905 		case TIOCUCNTL:
    906 			if (*(int *)data) {
    907 				if (pti->pt_flags & PF_PKT)
    908 					return (EINVAL);
    909 				pti->pt_flags |= PF_UCNTL;
    910 			} else
    911 				pti->pt_flags &= ~PF_UCNTL;
    912 			return (0);
    913 
    914 		case TIOCREMOTE:
    915 			if (*(int *)data)
    916 				pti->pt_flags |= PF_REMOTE;
    917 			else
    918 				pti->pt_flags &= ~PF_REMOTE;
    919 			ttyflush(tp, FREAD|FWRITE);
    920 			return (0);
    921 
    922 #ifdef COMPAT_OLDTTY
    923 		case TIOCSETP:
    924 		case TIOCSETN:
    925 #endif
    926 		case TIOCSETD:
    927 		case TIOCSETA:
    928 		case TIOCSETAW:
    929 		case TIOCSETAF:
    930 			ndflush(&tp->t_outq, tp->t_outq.c_cc);
    931 			break;
    932 
    933 		case TIOCSIG:
    934 			sig = (int)(long)*(caddr_t *)data;
    935 			if (sig <= 0 || sig >= NSIG)
    936 				return (EINVAL);
    937 			if (!ISSET(tp->t_lflag, NOFLSH))
    938 				ttyflush(tp, FREAD|FWRITE);
    939 			if ((sig == SIGINFO) &&
    940 			    (!ISSET(tp->t_lflag, NOKERNINFO)))
    941 				ttyinfo(tp);
    942 			pgsignal(tp->t_pgrp, sig, 1);
    943 			return(0);
    944 		}
    945 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
    946 	if (error == EPASSTHROUGH)
    947 		 error = ttioctl(tp, cmd, data, flag, p);
    948 	if (error == EPASSTHROUGH) {
    949 		if (pti->pt_flags & PF_UCNTL &&
    950 		    (cmd & ~0xff) == UIOCCMD(0)) {
    951 			if (cmd & 0xff) {
    952 				pti->pt_ucntl = (u_char)cmd;
    953 				ptcwakeup(tp, FREAD);
    954 			}
    955 			return (0);
    956 		}
    957 	}
    958 	/*
    959 	 * If external processing and packet mode send ioctl packet.
    960 	 */
    961 	if (ISSET(tp->t_lflag, EXTPROC) && (pti->pt_flags & PF_PKT)) {
    962 		switch(cmd) {
    963 		case TIOCSETA:
    964 		case TIOCSETAW:
    965 		case TIOCSETAF:
    966 #ifdef COMPAT_OLDTTY
    967 		case TIOCSETP:
    968 		case TIOCSETN:
    969 		case TIOCSETC:
    970 		case TIOCSLTC:
    971 		case TIOCLBIS:
    972 		case TIOCLBIC:
    973 		case TIOCLSET:
    974 #endif
    975 			pti->pt_send |= TIOCPKT_IOCTL;
    976 			ptcwakeup(tp, FREAD);
    977 		default:
    978 			break;
    979 		}
    980 	}
    981 	stop = ISSET(tp->t_iflag, IXON) && CCEQ(cc[VSTOP], CTRL('s'))
    982 		&& CCEQ(cc[VSTART], CTRL('q'));
    983 	if (pti->pt_flags & PF_NOSTOP) {
    984 		if (stop) {
    985 			pti->pt_send &= ~TIOCPKT_NOSTOP;
    986 			pti->pt_send |= TIOCPKT_DOSTOP;
    987 			pti->pt_flags &= ~PF_NOSTOP;
    988 			ptcwakeup(tp, FREAD);
    989 		}
    990 	} else {
    991 		if (!stop) {
    992 			pti->pt_send &= ~TIOCPKT_DOSTOP;
    993 			pti->pt_send |= TIOCPKT_NOSTOP;
    994 			pti->pt_flags |= PF_NOSTOP;
    995 			ptcwakeup(tp, FREAD);
    996 		}
    997 	}
    998 	return (error);
    999 }
   1000