xref: /csrg-svn/sys/kern/tty_pty.c (revision 39557)
123390Smckusick /*
238007Sbostic  * Copyright (c) 1982, 1986, 1989 The Regents of the University of California.
338007Sbostic  * All rights reserved.
423390Smckusick  *
538007Sbostic  * Redistribution and use in source and binary forms are permitted
638007Sbostic  * provided that the above copyright notice and this paragraph are
738007Sbostic  * duplicated in all such forms and that any documentation,
838007Sbostic  * advertising materials, and other materials related to such
938007Sbostic  * distribution and use acknowledge that the software was developed
1038007Sbostic  * by the University of California, Berkeley.  The name of the
1138007Sbostic  * University may not be used to endorse or promote products derived
1238007Sbostic  * from this software without specific prior written permission.
1338007Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1438007Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1538007Sbostic  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1638007Sbostic  *
17*39557Smarc  *	@(#)tty_pty.c	7.9 (Berkeley) 11/20/89
1823390Smckusick  */
192283Stoy 
202281Stoy /*
212281Stoy  * Pseudo-teletype Driver
222281Stoy  * (Actually two drivers, requiring two entries in 'cdevsw')
232281Stoy  */
242314Stoy #include "pty.h"
252314Stoy 
263206Swnj #if NPTY > 0
2717096Sbloom #include "param.h"
2817096Sbloom #include "systm.h"
2917096Sbloom #include "ioctl.h"
3017096Sbloom #include "tty.h"
3117096Sbloom #include "user.h"
3217096Sbloom #include "conf.h"
3317096Sbloom #include "file.h"
3417096Sbloom #include "proc.h"
3517096Sbloom #include "uio.h"
3617096Sbloom #include "kernel.h"
3737728Smckusick #include "vnode.h"
386239Sroot 
397475Ssam #if NPTY == 1
4018651Sbloom #undef NPTY
416239Sroot #define	NPTY	32		/* crude XXX */
427475Ssam #endif
432281Stoy 
4416788Ssam #define BUFSIZ 100		/* Chunk size iomoved to/from user */
454484Swnj 
462281Stoy /*
4718651Sbloom  * pts == /dev/tty[pqrs]?
4818651Sbloom  * ptc == /dev/pty[pqrs]?
492281Stoy  */
504484Swnj struct	tty pt_tty[NPTY];
514484Swnj struct	pt_ioctl {
525427Swnj 	int	pt_flags;
535427Swnj 	struct	proc *pt_selr, *pt_selw;
5418651Sbloom 	u_char	pt_send;
5518651Sbloom 	u_char	pt_ucntl;
564484Swnj } pt_ioctl[NPTY];
5718651Sbloom int	npty = NPTY;		/* for pstat -t */
582281Stoy 
5937590Smarc int ptydebug = 0;
6037590Smarc 
6135811Smarc #define	PF_RCOLL	0x01
6235811Smarc #define	PF_WCOLL	0x02
6335811Smarc #define	PF_NBIO		0x04
6435811Smarc #define	PF_PKT		0x08		/* packet mode */
6535811Smarc #define	PF_STOPPED	0x10		/* user told stopped */
6635811Smarc #define	PF_REMOTE	0x20		/* remote and flow controlled input */
6735811Smarc #define	PF_NOSTOP	0x40
6835811Smarc #define PF_UCNTL	0x80		/* user control mode */
692281Stoy 
702281Stoy /*ARGSUSED*/
712281Stoy ptsopen(dev, flag)
725396Sroot 	dev_t dev;
734484Swnj {
742281Stoy 	register struct tty *tp;
7518651Sbloom 	int error;
762281Stoy 
7718651Sbloom #ifdef lint
7818651Sbloom 	npty = npty;
7918651Sbloom #endif
808563Sroot 	if (minor(dev) >= NPTY)
818563Sroot 		return (ENXIO);
822281Stoy 	tp = &pt_tty[minor(dev)];
835408Swnj 	if ((tp->t_state & TS_ISOPEN) == 0) {
842427Swnj 		ttychars(tp);		/* Set up default chars */
8535811Smarc 		tp->t_iflag = TTYDEF_IFLAG;
8635811Smarc 		tp->t_oflag = TTYDEF_OFLAG;
8735811Smarc 		tp->t_lflag = TTYDEF_LFLAG;
8835811Smarc 		tp->t_cflag = TTYDEF_CFLAG;
8935811Smarc 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
9035811Smarc 		ttsetwater(tp);		/* would be done in xxparam() */
918563Sroot 	} else if (tp->t_state&TS_XCLUDE && u.u_uid != 0)
928563Sroot 		return (EBUSY);
934484Swnj 	if (tp->t_oproc)			/* Ctrlr still around. */
945408Swnj 		tp->t_state |= TS_CARR_ON;
955408Swnj 	while ((tp->t_state & TS_CARR_ON) == 0) {
965408Swnj 		tp->t_state |= TS_WOPEN;
9737590Smarc 		if (flag&FNDELAY)
9837590Smarc 			break;
992281Stoy 		sleep((caddr_t)&tp->t_rawq, TTIPRI);
1002281Stoy 	}
10137590Smarc 	error = (*linesw[tp->t_line].l_open)(dev, tp, flag);
10218651Sbloom 	ptcwakeup(tp, FREAD|FWRITE);
10318651Sbloom 	return (error);
1042281Stoy }
1052281Stoy 
1062281Stoy ptsclose(dev)
1075396Sroot 	dev_t dev;
1085408Swnj {
1092281Stoy 	register struct tty *tp;
1102281Stoy 
1112281Stoy 	tp = &pt_tty[minor(dev)];
1122281Stoy 	(*linesw[tp->t_line].l_close)(tp);
1136299Swnj 	ttyclose(tp);
11418651Sbloom 	ptcwakeup(tp, FREAD|FWRITE);
1152281Stoy }
1162281Stoy 
11737590Smarc ptsread(dev, uio, flag)
1185396Sroot 	dev_t dev;
1197823Sroot 	struct uio *uio;
1204484Swnj {
1215894Swnj 	register struct tty *tp = &pt_tty[minor(dev)];
1225894Swnj 	register struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
1238521Sroot 	int error = 0;
1242281Stoy 
1255894Swnj again:
1265894Swnj 	if (pti->pt_flags & PF_REMOTE) {
127*39557Smarc 		while (isbackground(u.u_procp, tp)) {
12818651Sbloom 			if ((u.u_procp->p_sigignore & sigmask(SIGTTIN)) ||
12918651Sbloom 			    (u.u_procp->p_sigmask & sigmask(SIGTTIN)) ||
130*39557Smarc 			    u.u_procp->p_pgrp->pg_jobc == 0 ||
1315894Swnj 			    u.u_procp->p_flag&SVFORK)
1328521Sroot 				return (EIO);
13335811Smarc 			pgsignal(u.u_procp->p_pgrp, SIGTTIN);
1345894Swnj 			sleep((caddr_t)&lbolt, TTIPRI);
1355408Swnj 		}
13618651Sbloom 		if (tp->t_canq.c_cc == 0) {
13737728Smckusick 			if (flag & IO_NDELAY)
1388521Sroot 				return (EWOULDBLOCK);
13918651Sbloom 			sleep((caddr_t)&tp->t_canq, TTIPRI);
1405894Swnj 			goto again;
1415894Swnj 		}
14218651Sbloom 		while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
14318651Sbloom 			if (ureadc(getc(&tp->t_canq), uio) < 0) {
1448521Sroot 				error = EFAULT;
1457823Sroot 				break;
1467823Sroot 			}
14718651Sbloom 		if (tp->t_canq.c_cc == 1)
14818651Sbloom 			(void) getc(&tp->t_canq);
14918651Sbloom 		if (tp->t_canq.c_cc)
1508521Sroot 			return (error);
1515894Swnj 	} else
1525894Swnj 		if (tp->t_oproc)
15337590Smarc 			error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
15418651Sbloom 	ptcwakeup(tp, FWRITE);
1558521Sroot 	return (error);
1562281Stoy }
1572281Stoy 
1585408Swnj /*
1595408Swnj  * Write to pseudo-tty.
1605408Swnj  * Wakeups of controlling tty will happen
1615408Swnj  * indirectly, when tty driver calls ptsstart.
1625408Swnj  */
16337590Smarc ptswrite(dev, uio, flag)
1645396Sroot 	dev_t dev;
1657823Sroot 	struct uio *uio;
1664484Swnj {
16735811Smarc 	register struct tty *tp;
1682281Stoy 
16935811Smarc 	tp = &pt_tty[minor(dev)];
1708521Sroot 	if (tp->t_oproc == 0)
1718521Sroot 		return (EIO);
17237590Smarc 	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
1732281Stoy }
1742281Stoy 
1755408Swnj /*
1765408Swnj  * Start output on pseudo-tty.
1775408Swnj  * Wake up process selecting or sleeping for input from controlling tty.
1785408Swnj  */
1792281Stoy ptsstart(tp)
1804484Swnj 	struct tty *tp;
1814484Swnj {
1825574Swnj 	register struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
1834484Swnj 
1845408Swnj 	if (tp->t_state & TS_TTSTOP)
1852281Stoy 		return;
1865574Swnj 	if (pti->pt_flags & PF_STOPPED) {
1875574Swnj 		pti->pt_flags &= ~PF_STOPPED;
1885574Swnj 		pti->pt_send = TIOCPKT_START;
1895574Swnj 	}
19018651Sbloom 	ptcwakeup(tp, FREAD);
1915430Swnj }
1925430Swnj 
19318651Sbloom ptcwakeup(tp, flag)
1945430Swnj 	struct tty *tp;
1955430Swnj {
1965430Swnj 	struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
1975430Swnj 
19818651Sbloom 	if (flag & FREAD) {
19918651Sbloom 		if (pti->pt_selr) {
20018651Sbloom 			selwakeup(pti->pt_selr, pti->pt_flags & PF_RCOLL);
20118651Sbloom 			pti->pt_selr = 0;
20218651Sbloom 			pti->pt_flags &= ~PF_RCOLL;
20318651Sbloom 		}
20418651Sbloom 		wakeup((caddr_t)&tp->t_outq.c_cf);
2054484Swnj 	}
20618651Sbloom 	if (flag & FWRITE) {
20718651Sbloom 		if (pti->pt_selw) {
20818651Sbloom 			selwakeup(pti->pt_selw, pti->pt_flags & PF_WCOLL);
20918651Sbloom 			pti->pt_selw = 0;
21018651Sbloom 			pti->pt_flags &= ~PF_WCOLL;
21118651Sbloom 		}
21237590Smarc if (ptydebug) printf("WAKEUP c_cf %d\n", u.u_procp->p_pid);
21318651Sbloom 		wakeup((caddr_t)&tp->t_rawq.c_cf);
21418651Sbloom 	}
2152281Stoy }
2162281Stoy 
2172281Stoy /*ARGSUSED*/
2182281Stoy ptcopen(dev, flag)
2194484Swnj 	dev_t dev;
2204484Swnj 	int flag;
2214484Swnj {
2222281Stoy 	register struct tty *tp;
2235427Swnj 	struct pt_ioctl *pti;
2242281Stoy 
2258563Sroot 	if (minor(dev) >= NPTY)
2268563Sroot 		return (ENXIO);
2272281Stoy 	tp = &pt_tty[minor(dev)];
2288563Sroot 	if (tp->t_oproc)
2298563Sroot 		return (EIO);
2304484Swnj 	tp->t_oproc = ptsstart;
23125390Skarels 	(void)(*linesw[tp->t_line].l_modem)(tp, 1);
2325427Swnj 	pti = &pt_ioctl[minor(dev)];
2335427Swnj 	pti->pt_flags = 0;
2345427Swnj 	pti->pt_send = 0;
23518651Sbloom 	pti->pt_ucntl = 0;
2368563Sroot 	return (0);
2372281Stoy }
2382281Stoy 
2392281Stoy ptcclose(dev)
2404484Swnj 	dev_t dev;
2414484Swnj {
2422281Stoy 	register struct tty *tp;
2432281Stoy 
2442281Stoy 	tp = &pt_tty[minor(dev)];
24525390Skarels 	(void)(*linesw[tp->t_line].l_modem)(tp, 0);
24632326Skarels 	tp->t_state &= ~TS_CARR_ON;
2474484Swnj 	tp->t_oproc = 0;		/* mark closed */
24839497Smarc 	tp->t_session = 0;
2492281Stoy }
2502281Stoy 
25137590Smarc ptcread(dev, uio, flag)
2525427Swnj 	dev_t dev;
2537823Sroot 	struct uio *uio;
2544484Swnj {
2558521Sroot 	register struct tty *tp = &pt_tty[minor(dev)];
25618651Sbloom 	struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
25716788Ssam 	char buf[BUFSIZ];
25816788Ssam 	int error = 0, cc;
2592281Stoy 
26018651Sbloom 	/*
26118651Sbloom 	 * We want to block until the slave
26218651Sbloom 	 * is open, and there's something to read;
26318651Sbloom 	 * but if we lost the slave or we're NBIO,
26418651Sbloom 	 * then return the appropriate error instead.
26518651Sbloom 	 */
26618651Sbloom 	for (;;) {
26718651Sbloom 		if (tp->t_state&TS_ISOPEN) {
26818651Sbloom 			if (pti->pt_flags&PF_PKT && pti->pt_send) {
26926358Skarels 				error = ureadc((int)pti->pt_send, uio);
27018651Sbloom 				if (error)
27118651Sbloom 					return (error);
27218651Sbloom 				pti->pt_send = 0;
27318651Sbloom 				return (0);
27418651Sbloom 			}
27518651Sbloom 			if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
27626358Skarels 				error = ureadc((int)pti->pt_ucntl, uio);
27718651Sbloom 				if (error)
27818651Sbloom 					return (error);
27918651Sbloom 				pti->pt_ucntl = 0;
28018651Sbloom 				return (0);
28118651Sbloom 			}
28218651Sbloom 			if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
28318651Sbloom 				break;
2845427Swnj 		}
28516788Ssam 		if ((tp->t_state&TS_CARR_ON) == 0)
28637590Smarc 			return (0);	/* EOF */
28737728Smckusick 		if (flag & IO_NDELAY)
2888521Sroot 			return (EWOULDBLOCK);
28937590Smarc if (ptydebug) printf("SLEEP(1) c_cf %d\n", u.u_procp->p_pid);
2902281Stoy 		sleep((caddr_t)&tp->t_outq.c_cf, TTIPRI);
2915411Swnj 	}
29235811Smarc 	if (pti->pt_flags & (PF_PKT|PF_UCNTL))
29318651Sbloom 		error = ureadc(0, uio);
29416788Ssam 	while (uio->uio_resid > 0 && error == 0) {
29516788Ssam 		cc = q_to_b(&tp->t_outq, buf, MIN(uio->uio_resid, BUFSIZ));
29616788Ssam 		if (cc <= 0)
2977823Sroot 			break;
29837728Smckusick 		error = uiomove(buf, cc, uio);
29916788Ssam 	}
30035811Smarc 	if (tp->t_outq.c_cc <= tp->t_lowat) {
30135811Smarc 		if (tp->t_state&TS_ASLEEP) {
30235811Smarc 			tp->t_state &= ~TS_ASLEEP;
30335811Smarc 			wakeup((caddr_t)&tp->t_outq);
30435811Smarc 		}
30535811Smarc 		if (tp->t_wsel) {
30635811Smarc 			selwakeup(tp->t_wsel, tp->t_state & TS_WCOLL);
30735811Smarc 			tp->t_wsel = 0;
30835811Smarc 			tp->t_state &= ~TS_WCOLL;
30935811Smarc 		}
31035811Smarc 	}
3118521Sroot 	return (error);
3122281Stoy }
3132281Stoy 
3145427Swnj ptsstop(tp, flush)
3155427Swnj 	register struct tty *tp;
3165427Swnj 	int flush;
3175427Swnj {
3185427Swnj 	struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
31918651Sbloom 	int flag;
3205427Swnj 
3215574Swnj 	/* note: FLUSHREAD and FLUSHWRITE already ok */
3225574Swnj 	if (flush == 0) {
3235574Swnj 		flush = TIOCPKT_STOP;
3245574Swnj 		pti->pt_flags |= PF_STOPPED;
32518651Sbloom 	} else
3265574Swnj 		pti->pt_flags &= ~PF_STOPPED;
3276119Swnj 	pti->pt_send |= flush;
32818651Sbloom 	/* change of perspective */
32918651Sbloom 	flag = 0;
33018651Sbloom 	if (flush & FREAD)
33118651Sbloom 		flag |= FWRITE;
33218651Sbloom 	if (flush & FWRITE)
33318651Sbloom 		flag |= FREAD;
33423633Sbloom 	ptcwakeup(tp, flag);
3355427Swnj }
3365427Swnj 
3375408Swnj ptcselect(dev, rw)
3384484Swnj 	dev_t dev;
3395408Swnj 	int rw;
3404484Swnj {
3414484Swnj 	register struct tty *tp = &pt_tty[minor(dev)];
3425894Swnj 	struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
3434484Swnj 	struct proc *p;
3445430Swnj 	int s;
3454484Swnj 
34618651Sbloom 	if ((tp->t_state&TS_CARR_ON) == 0)
3474484Swnj 		return (1);
3485408Swnj 	switch (rw) {
3495408Swnj 
3505408Swnj 	case FREAD:
35125945Skarels 		/*
35225945Skarels 		 * Need to block timeouts (ttrstart).
35325945Skarels 		 */
35425945Skarels 		s = spltty();
35518651Sbloom 		if ((tp->t_state&TS_ISOPEN) &&
35625436Skarels 		     tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) {
35725436Skarels 			splx(s);
35825436Skarels 			return (1);
35925436Skarels 		}
36025945Skarels 		splx(s);
36125436Skarels 		/* FALLTHROUGH */
36225436Skarels 
36325436Skarels 	case 0:					/* exceptional */
36425436Skarels 		if ((tp->t_state&TS_ISOPEN) &&
36518651Sbloom 		    (pti->pt_flags&PF_PKT && pti->pt_send ||
36625945Skarels 		     pti->pt_flags&PF_UCNTL && pti->pt_ucntl))
3675408Swnj 			return (1);
3685427Swnj 		if ((p = pti->pt_selr) && p->p_wchan == (caddr_t)&selwait)
3695427Swnj 			pti->pt_flags |= PF_RCOLL;
3705408Swnj 		else
3715427Swnj 			pti->pt_selr = u.u_procp;
3725430Swnj 		break;
3735408Swnj 
37425436Skarels 
3755408Swnj 	case FWRITE:
37625945Skarels 		if (tp->t_state&TS_ISOPEN) {
37725945Skarels 			if (pti->pt_flags & PF_REMOTE) {
37825945Skarels 			    if (tp->t_canq.c_cc == 0)
37925945Skarels 				return (1);
38025945Skarels 			} else {
38125945Skarels 			    if (tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2)
38225945Skarels 				    return (1);
38335811Smarc 			    if (tp->t_canq.c_cc == 0 && (tp->t_iflag&ICANON))
38425945Skarels 				    return (1);
38525945Skarels 			}
3865430Swnj 		}
3875427Swnj 		if ((p = pti->pt_selw) && p->p_wchan == (caddr_t)&selwait)
3885427Swnj 			pti->pt_flags |= PF_WCOLL;
3895408Swnj 		else
3905427Swnj 			pti->pt_selw = u.u_procp;
3915430Swnj 		break;
39225436Skarels 
3935408Swnj 	}
3945430Swnj 	return (0);
3955396Sroot }
3964484Swnj 
39737590Smarc ptcwrite(dev, uio, flag)
3985408Swnj 	dev_t dev;
39918651Sbloom 	register struct uio *uio;
4004484Swnj {
4018521Sroot 	register struct tty *tp = &pt_tty[minor(dev)];
40218651Sbloom 	register struct iovec *iov;
40318651Sbloom 	register char *cp;
40418651Sbloom 	register int cc = 0;
4052281Stoy 	char locbuf[BUFSIZ];
4065408Swnj 	int cnt = 0;
4075894Swnj 	struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
4088521Sroot 	int error = 0;
4092281Stoy 
41018651Sbloom again:
41118651Sbloom 	if ((tp->t_state&TS_ISOPEN) == 0)
41218651Sbloom 		goto block;
41323633Sbloom 	if (pti->pt_flags & PF_REMOTE) {
41418651Sbloom 		if (tp->t_canq.c_cc)
41518651Sbloom 			goto block;
41623633Sbloom 		while (uio->uio_iovcnt > 0 && tp->t_canq.c_cc < TTYHOG - 1) {
41723633Sbloom 			iov = uio->uio_iov;
41823633Sbloom 			if (iov->iov_len == 0) {
41923633Sbloom 				uio->uio_iovcnt--;
42023633Sbloom 				uio->uio_iov++;
42123633Sbloom 				continue;
42223633Sbloom 			}
42323633Sbloom 			if (cc == 0) {
42423633Sbloom 				cc = MIN(iov->iov_len, BUFSIZ);
42523633Sbloom 				cc = MIN(cc, TTYHOG - 1 - tp->t_canq.c_cc);
42623633Sbloom 				cp = locbuf;
42737728Smckusick 				error = uiomove(cp, cc, uio);
42823633Sbloom 				if (error)
42923633Sbloom 					return (error);
43023633Sbloom 				/* check again for safety */
43123633Sbloom 				if ((tp->t_state&TS_ISOPEN) == 0)
43223633Sbloom 					return (EIO);
43323633Sbloom 			}
43423633Sbloom 			if (cc)
43523633Sbloom 				(void) b_to_q(cp, cc, &tp->t_canq);
43623633Sbloom 			cc = 0;
4377823Sroot 		}
43818651Sbloom 		(void) putc(0, &tp->t_canq);
43918651Sbloom 		ttwakeup(tp);
44018651Sbloom 		wakeup((caddr_t)&tp->t_canq);
44118651Sbloom 		return (0);
44218651Sbloom 	}
44318651Sbloom 	while (uio->uio_iovcnt > 0) {
44418651Sbloom 		iov = uio->uio_iov;
44518651Sbloom 		if (cc == 0) {
44618651Sbloom 			if (iov->iov_len == 0) {
44718651Sbloom 				uio->uio_iovcnt--;
44818651Sbloom 				uio->uio_iov++;
44918651Sbloom 				continue;
4505894Swnj 			}
45118651Sbloom 			cc = MIN(iov->iov_len, BUFSIZ);
45218651Sbloom 			cp = locbuf;
45337728Smckusick 			error = uiomove(cp, cc, uio);
45418651Sbloom 			if (error)
45518651Sbloom 				return (error);
45618651Sbloom 			/* check again for safety */
45718651Sbloom 			if ((tp->t_state&TS_ISOPEN) == 0)
45818651Sbloom 				return (EIO);
4595894Swnj 		}
46023633Sbloom 		while (cc > 0) {
46123633Sbloom 			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
46235811Smarc 			   (tp->t_canq.c_cc > 0 || !(tp->t_iflag&ICANON))) {
46323633Sbloom 				wakeup((caddr_t)&tp->t_rawq);
46423633Sbloom 				goto block;
46523633Sbloom 			}
46637590Smarc 			(*linesw[tp->t_line].l_rint)(*cp++&0377, tp);
4675408Swnj 			cnt++;
46823633Sbloom 			cc--;
4692281Stoy 		}
47018651Sbloom 		cc = 0;
47118651Sbloom 	}
47218651Sbloom 	return (0);
47318651Sbloom block:
47418651Sbloom 	/*
47523633Sbloom 	 * Come here to wait for slave to open, for space
47623633Sbloom 	 * in outq, or space in rawq.
47718651Sbloom 	 */
47818651Sbloom 	if ((tp->t_state&TS_CARR_ON) == 0)
47918651Sbloom 		return (EIO);
48037728Smckusick 	if ((pti->pt_flags & PF_NBIO) || (flag & IO_NDELAY)) {
48118651Sbloom 		iov->iov_base -= cc;
48218651Sbloom 		iov->iov_len += cc;
48318651Sbloom 		uio->uio_resid += cc;
48418651Sbloom 		uio->uio_offset -= cc;
48523633Sbloom 		if (cnt == 0)
48623633Sbloom 			return (EWOULDBLOCK);
48718651Sbloom 		return (0);
48818651Sbloom 	}
48937590Smarc if (ptydebug) printf("SLEEP(2) c_cf %d\n", u.u_procp->p_pid);
49018651Sbloom 	sleep((caddr_t)&tp->t_rawq.c_cf, TTOPRI);
49118651Sbloom 	goto again;
4922281Stoy }
4932281Stoy 
4942281Stoy /*ARGSUSED*/
4957626Ssam ptyioctl(dev, cmd, data, flag)
4967626Ssam 	caddr_t data;
4974484Swnj 	dev_t dev;
4984484Swnj {
4996119Swnj 	register struct tty *tp = &pt_tty[minor(dev)];
5006119Swnj 	register struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
50135811Smarc 	register u_char *cc = tp->t_cc;
50218651Sbloom 	int stop, error;
50325390Skarels 	extern ttyinput();
5042281Stoy 
50525390Skarels 	/*
50625390Skarels 	 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
50725390Skarels 	 * ttywflush(tp) will hang if there are characters in the outq.
50825390Skarels 	 */
50935811Smarc 	if (cdevsw[major(dev)].d_open == ptcopen)
5107626Ssam 		switch (cmd) {
5117626Ssam 
5127626Ssam 		case TIOCPKT:
51318651Sbloom 			if (*(int *)data) {
51418651Sbloom 				if (pti->pt_flags & PF_UCNTL)
51518651Sbloom 					return (EINVAL);
5165427Swnj 				pti->pt_flags |= PF_PKT;
51718651Sbloom 			} else
5185427Swnj 				pti->pt_flags &= ~PF_PKT;
5198563Sroot 			return (0);
5207626Ssam 
52118651Sbloom 		case TIOCUCNTL:
52218651Sbloom 			if (*(int *)data) {
52318651Sbloom 				if (pti->pt_flags & PF_PKT)
52418651Sbloom 					return (EINVAL);
52518651Sbloom 				pti->pt_flags |= PF_UCNTL;
52618651Sbloom 			} else
52718651Sbloom 				pti->pt_flags &= ~PF_UCNTL;
52818651Sbloom 			return (0);
52918651Sbloom 
5307626Ssam 		case TIOCREMOTE:
5317626Ssam 			if (*(int *)data)
5325894Swnj 				pti->pt_flags |= PF_REMOTE;
5335894Swnj 			else
5345894Swnj 				pti->pt_flags &= ~PF_REMOTE;
53512753Ssam 			ttyflush(tp, FREAD|FWRITE);
5368563Sroot 			return (0);
5377626Ssam 
5387626Ssam 		case FIONBIO:
5397626Ssam 			if (*(int *)data)
5405427Swnj 				pti->pt_flags |= PF_NBIO;
5415411Swnj 			else
5425427Swnj 				pti->pt_flags &= ~PF_NBIO;
5438563Sroot 			return (0);
5447626Ssam 
54535811Smarc 		case TIOCSETP:
54625390Skarels 		case TIOCSETN:
54725390Skarels 		case TIOCSETD:
54835811Smarc 		case TIOCSETA:
54935811Smarc 		case TIOCSETAW:
55035811Smarc 		case TIOCSETAF:
55139497Smarc 		case JUNK_TIOCSETAS:
55239497Smarc 		case JUNK_TIOCSETAWS:
55339497Smarc 		case JUNK_TIOCSETAFS:
5547626Ssam 			while (getc(&tp->t_outq) >= 0)
5557626Ssam 				;
5567626Ssam 			break;
5575411Swnj 		}
55835811Smarc 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag);
55935811Smarc 	if (error < 0)
56035811Smarc 		 error = ttioctl(tp, cmd, data, flag);
56125390Skarels 	/*
56225390Skarels 	 * Since we use the tty queues internally,
56325390Skarels 	 * pty's can't be switched to disciplines which overwrite
56425390Skarels 	 * the queues.  We can't tell anything about the discipline
56525390Skarels 	 * from here...
56625390Skarels 	 */
56725390Skarels 	if (linesw[tp->t_line].l_rint != ttyinput) {
56825390Skarels 		(*linesw[tp->t_line].l_close)(tp);
56925390Skarels 		tp->t_line = 0;
57037590Smarc 		(void)(*linesw[tp->t_line].l_open)(dev, tp, flag);
57125390Skarels 		error = ENOTTY;
57225390Skarels 	}
57318651Sbloom 	if (error < 0) {
57418651Sbloom 		if (pti->pt_flags & PF_UCNTL &&
57528285Skarels 		    (cmd & ~0xff) == UIOCCMD(0)) {
57618651Sbloom 			if (cmd & 0xff) {
57718651Sbloom 				pti->pt_ucntl = (u_char)cmd;
57818651Sbloom 				ptcwakeup(tp, FREAD);
57918651Sbloom 			}
58018651Sbloom 			return (0);
58118651Sbloom 		}
5828563Sroot 		error = ENOTTY;
58318651Sbloom 	}
58435811Smarc 	stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
58535811Smarc 		&& CCEQ(cc[VSTART], CTRL('q'));
5866119Swnj 	if (pti->pt_flags & PF_NOSTOP) {
5876119Swnj 		if (stop) {
58825478Skarels 			pti->pt_send &= ~TIOCPKT_NOSTOP;
5896119Swnj 			pti->pt_send |= TIOCPKT_DOSTOP;
5906119Swnj 			pti->pt_flags &= ~PF_NOSTOP;
59118651Sbloom 			ptcwakeup(tp, FREAD);
5926119Swnj 		}
5936119Swnj 	} else {
59418651Sbloom 		if (!stop) {
5956119Swnj 			pti->pt_send &= ~TIOCPKT_DOSTOP;
5966119Swnj 			pti->pt_send |= TIOCPKT_NOSTOP;
5976119Swnj 			pti->pt_flags |= PF_NOSTOP;
59818651Sbloom 			ptcwakeup(tp, FREAD);
5996119Swnj 		}
6006119Swnj 	}
6018563Sroot 	return (error);
6022281Stoy }
6032313Stoy #endif
604