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