pty.c revision 9824
11541Srgrimes/*
21541Srgrimes * Copyright (c) 1982, 1986, 1989, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 3. All advertising materials mentioning features or use of this software
141541Srgrimes *    must display the following acknowledgement:
151541Srgrimes *	This product includes software developed by the University of
161541Srgrimes *	California, Berkeley and its contributors.
171541Srgrimes * 4. Neither the name of the University nor the names of its contributors
181541Srgrimes *    may be used to endorse or promote products derived from this software
191541Srgrimes *    without specific prior written permission.
201541Srgrimes *
211541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311541Srgrimes * SUCH DAMAGE.
321541Srgrimes *
331541Srgrimes *	@(#)tty_pty.c	8.2 (Berkeley) 9/23/93
349824Sbde * $Id: tty_pty.c,v 1.15 1995/07/22 16:45:08 bde Exp $
351541Srgrimes */
361541Srgrimes
371541Srgrimes/*
381541Srgrimes * Pseudo-teletype Driver
391541Srgrimes * (Actually two drivers, requiring two entries in 'cdevsw')
401541Srgrimes */
411541Srgrimes#include "pty.h"		/* XXX */
421541Srgrimes
431541Srgrimes#include <sys/param.h>
441541Srgrimes#include <sys/systm.h>
451541Srgrimes#include <sys/ioctl.h>
461541Srgrimes#include <sys/proc.h>
471541Srgrimes#include <sys/tty.h>
481541Srgrimes#include <sys/conf.h>
491541Srgrimes#include <sys/file.h>
501541Srgrimes#include <sys/uio.h>
511541Srgrimes#include <sys/kernel.h>
521541Srgrimes#include <sys/vnode.h>
533308Sphk#include <sys/signalvar.h>
541541Srgrimes
551541Srgrimes#if NPTY == 1
561541Srgrimes#undef NPTY
571541Srgrimes#define	NPTY	32		/* crude XXX */
581541Srgrimes#endif
591541Srgrimes
601541Srgrimes#define BUFSIZ 100		/* Chunk size iomoved to/from user */
611541Srgrimes
621541Srgrimes/*
631541Srgrimes * pts == /dev/tty[pqrs]?
641541Srgrimes * ptc == /dev/pty[pqrs]?
651541Srgrimes */
661541Srgrimesstruct	tty pt_tty[NPTY];	/* XXX */
671541Srgrimesstruct	pt_ioctl {
681541Srgrimes	int	pt_flags;
691541Srgrimes	struct	selinfo pt_selr, pt_selw;
701541Srgrimes	u_char	pt_send;
711541Srgrimes	u_char	pt_ucntl;
721541Srgrimes} pt_ioctl[NPTY];		/* XXX */
731541Srgrimesint	npty = NPTY;		/* for pstat -t */
741541Srgrimes
751541Srgrimes#define	PF_PKT		0x08		/* packet mode */
761541Srgrimes#define	PF_STOPPED	0x10		/* user told stopped */
771541Srgrimes#define	PF_REMOTE	0x20		/* remote and flow controlled input */
781541Srgrimes#define	PF_NOSTOP	0x40
791541Srgrimes#define PF_UCNTL	0x80		/* user control mode */
801541Srgrimes
811549Srgrimesvoid	ptsstop		__P((struct tty *, int));
821549Srgrimesvoid	ptcwakeup	__P((struct tty *, int));
831541Srgrimes
841541Srgrimes/*
851541Srgrimes * Establish n (or default if n is 1) ptys in the system.
861541Srgrimes *
871541Srgrimes * XXX cdevsw & pstat require the array `pty[]' to be an array
881541Srgrimes */
891541Srgrimesvoid
901541Srgrimesptyattach(n)
911541Srgrimes	int n;
921541Srgrimes{
931541Srgrimes#ifdef notyet
941541Srgrimes	char *mem;
951541Srgrimes	register u_long ntb;
961541Srgrimes#define	DEFAULT_NPTY	32
971541Srgrimes
981541Srgrimes	/* maybe should allow 0 => none? */
991541Srgrimes	if (n <= 1)
1001541Srgrimes		n = DEFAULT_NPTY;
1011541Srgrimes	ntb = n * sizeof(struct tty);
1021541Srgrimes	mem = malloc(ntb + ALIGNBYTES + n * sizeof(struct pt_ioctl),
1031541Srgrimes	    M_DEVBUF, M_WAITOK);
1041541Srgrimes	pt_tty = (struct tty *)mem;
1051541Srgrimes	mem = (char *)ALIGN(mem + ntb);
1061541Srgrimes	pt_ioctl = (struct pt_ioctl *)mem;
1071541Srgrimes	npty = n;
1081541Srgrimes#endif
1091541Srgrimes}
1101541Srgrimes
1111541Srgrimes/*ARGSUSED*/
1121549Srgrimesint
1131541Srgrimesptsopen(dev, flag, devtype, p)
1141541Srgrimes	dev_t dev;
1151541Srgrimes	int flag, devtype;
1161541Srgrimes	struct proc *p;
1171541Srgrimes{
1181541Srgrimes	register struct tty *tp;
1191541Srgrimes	int error;
1201541Srgrimes
1211541Srgrimes	if (minor(dev) >= npty)
1221541Srgrimes		return (ENXIO);
1231541Srgrimes	tp = &pt_tty[minor(dev)];
1241541Srgrimes	if ((tp->t_state & TS_ISOPEN) == 0) {
1251541Srgrimes		ttychars(tp);		/* Set up default chars */
1261541Srgrimes		tp->t_iflag = TTYDEF_IFLAG;
1271541Srgrimes		tp->t_oflag = TTYDEF_OFLAG;
1281541Srgrimes		tp->t_lflag = TTYDEF_LFLAG;
1291541Srgrimes		tp->t_cflag = TTYDEF_CFLAG;
1301541Srgrimes		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
1311541Srgrimes		ttsetwater(tp);		/* would be done in xxparam() */
1321541Srgrimes	} else if (tp->t_state&TS_XCLUDE && p->p_ucred->cr_uid != 0)
1331541Srgrimes		return (EBUSY);
1341541Srgrimes	if (tp->t_oproc)			/* Ctrlr still around. */
1359824Sbde		(void)(*linesw[tp->t_line].l_modem)(tp, 1);
1361541Srgrimes	while ((tp->t_state & TS_CARR_ON) == 0) {
1371541Srgrimes		if (flag&FNONBLOCK)
1381541Srgrimes			break;
1399639Sbde		error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH,
1409624Sbde				 "ptsopn", 0);
1413308Sphk		if (error)
1421541Srgrimes			return (error);
1431541Srgrimes	}
1441541Srgrimes	error = (*linesw[tp->t_line].l_open)(dev, tp);
1457724Sache	if (error == 0)
1467724Sache		ptcwakeup(tp, FREAD|FWRITE);
1471541Srgrimes	return (error);
1481541Srgrimes}
1491541Srgrimes
1501549Srgrimesint
1511541Srgrimesptsclose(dev, flag, mode, p)
1521541Srgrimes	dev_t dev;
1531541Srgrimes	int flag, mode;
1541541Srgrimes	struct proc *p;
1551541Srgrimes{
1561541Srgrimes	register struct tty *tp;
1571541Srgrimes	int err;
1581541Srgrimes
1591541Srgrimes	tp = &pt_tty[minor(dev)];
1601541Srgrimes	err = (*linesw[tp->t_line].l_close)(tp, flag);
1617730Sache	ptsstop(tp, FREAD|FWRITE);
1627724Sache	(void) ttyclose(tp);
1631541Srgrimes	return (err);
1641541Srgrimes}
1651541Srgrimes
1661549Srgrimesint
1671541Srgrimesptsread(dev, uio, flag)
1681541Srgrimes	dev_t dev;
1691541Srgrimes	struct uio *uio;
1701541Srgrimes	int flag;
1711541Srgrimes{
1721541Srgrimes	struct proc *p = curproc;
1731541Srgrimes	register struct tty *tp = &pt_tty[minor(dev)];
1741541Srgrimes	register struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
1751541Srgrimes	int error = 0;
1761541Srgrimes
1771541Srgrimesagain:
1781541Srgrimes	if (pti->pt_flags & PF_REMOTE) {
1791541Srgrimes		while (isbackground(p, tp)) {
1801541Srgrimes			if ((p->p_sigignore & sigmask(SIGTTIN)) ||
1811541Srgrimes			    (p->p_sigmask & sigmask(SIGTTIN)) ||
1821541Srgrimes			    p->p_pgrp->pg_jobc == 0 ||
1831541Srgrimes			    p->p_flag & P_PPWAIT)
1841541Srgrimes				return (EIO);
1851541Srgrimes			pgsignal(p->p_pgrp, SIGTTIN, 1);
1869624Sbde			error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ptsbg",
1879624Sbde					 0);
1883308Sphk			if (error)
1891541Srgrimes				return (error);
1901541Srgrimes		}
1911541Srgrimes		if (tp->t_canq.c_cc == 0) {
1921541Srgrimes			if (flag & IO_NDELAY)
1931541Srgrimes				return (EWOULDBLOCK);
1949639Sbde			error = ttysleep(tp, TSA_PTS_READ(tp), TTIPRI | PCATCH,
1959624Sbde					 "ptsin", 0);
1963308Sphk			if (error)
1971541Srgrimes				return (error);
1981541Srgrimes			goto again;
1991541Srgrimes		}
2001541Srgrimes		while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
2011541Srgrimes			if (ureadc(getc(&tp->t_canq), uio) < 0) {
2021541Srgrimes				error = EFAULT;
2031541Srgrimes				break;
2041541Srgrimes			}
2051541Srgrimes		if (tp->t_canq.c_cc == 1)
2061541Srgrimes			(void) getc(&tp->t_canq);
2071541Srgrimes		if (tp->t_canq.c_cc)
2081541Srgrimes			return (error);
2091541Srgrimes	} else
2101541Srgrimes		if (tp->t_oproc)
2111541Srgrimes			error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
2121541Srgrimes	ptcwakeup(tp, FWRITE);
2131541Srgrimes	return (error);
2141541Srgrimes}
2151541Srgrimes
2161541Srgrimes/*
2171541Srgrimes * Write to pseudo-tty.
2181541Srgrimes * Wakeups of controlling tty will happen
2191541Srgrimes * indirectly, when tty driver calls ptsstart.
2201541Srgrimes */
2211549Srgrimesint
2221541Srgrimesptswrite(dev, uio, flag)
2231541Srgrimes	dev_t dev;
2241541Srgrimes	struct uio *uio;
2251541Srgrimes	int flag;
2261541Srgrimes{
2271541Srgrimes	register struct tty *tp;
2281541Srgrimes
2291541Srgrimes	tp = &pt_tty[minor(dev)];
2301541Srgrimes	if (tp->t_oproc == 0)
2311541Srgrimes		return (EIO);
2321541Srgrimes	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
2331541Srgrimes}
2341541Srgrimes
2351541Srgrimes/*
2361541Srgrimes * Start output on pseudo-tty.
2371541Srgrimes * Wake up process selecting or sleeping for input from controlling tty.
2381541Srgrimes */
2391541Srgrimesvoid
2401541Srgrimesptsstart(tp)
2411541Srgrimes	struct tty *tp;
2421541Srgrimes{
2431541Srgrimes	register struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
2441541Srgrimes
2451541Srgrimes	if (tp->t_state & TS_TTSTOP)
2461541Srgrimes		return;
2471541Srgrimes	if (pti->pt_flags & PF_STOPPED) {
2481541Srgrimes		pti->pt_flags &= ~PF_STOPPED;
2491541Srgrimes		pti->pt_send = TIOCPKT_START;
2501541Srgrimes	}
2511541Srgrimes	ptcwakeup(tp, FREAD);
2521541Srgrimes}
2531541Srgrimes
2541549Srgrimesvoid
2551541Srgrimesptcwakeup(tp, flag)
2561541Srgrimes	struct tty *tp;
2571541Srgrimes	int flag;
2581541Srgrimes{
2591541Srgrimes	struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
2601541Srgrimes
2611541Srgrimes	if (flag & FREAD) {
2621541Srgrimes		selwakeup(&pti->pt_selr);
2639639Sbde		wakeup(TSA_PTC_READ(tp));
2641541Srgrimes	}
2651541Srgrimes	if (flag & FWRITE) {
2661541Srgrimes		selwakeup(&pti->pt_selw);
2679639Sbde		wakeup(TSA_PTC_WRITE(tp));
2681541Srgrimes	}
2691541Srgrimes}
2701541Srgrimes
2711549Srgrimesint
2721541Srgrimesptcopen(dev, flag, devtype, p)
2731541Srgrimes	dev_t dev;
2741541Srgrimes	int flag, devtype;
2751541Srgrimes	struct proc *p;
2761541Srgrimes{
2771541Srgrimes	register struct tty *tp;
2781541Srgrimes	struct pt_ioctl *pti;
2791541Srgrimes
2801541Srgrimes	if (minor(dev) >= npty)
2811541Srgrimes		return (ENXIO);
2821541Srgrimes	tp = &pt_tty[minor(dev)];
2831541Srgrimes	if (tp->t_oproc)
2841541Srgrimes		return (EIO);
2851541Srgrimes	tp->t_oproc = ptsstart;
2861541Srgrimes#ifdef sun4c
2871541Srgrimes	tp->t_stop = ptsstop;
2881541Srgrimes#endif
2891541Srgrimes	(void)(*linesw[tp->t_line].l_modem)(tp, 1);
2901541Srgrimes	tp->t_lflag &= ~EXTPROC;
2911541Srgrimes	pti = &pt_ioctl[minor(dev)];
2921541Srgrimes	pti->pt_flags = 0;
2931541Srgrimes	pti->pt_send = 0;
2941541Srgrimes	pti->pt_ucntl = 0;
2951541Srgrimes	return (0);
2961541Srgrimes}
2971541Srgrimes
2981549Srgrimesint
2991541Srgrimesptcclose(dev)
3001541Srgrimes	dev_t dev;
3011541Srgrimes{
3021541Srgrimes	register struct tty *tp;
3031541Srgrimes
3041541Srgrimes	tp = &pt_tty[minor(dev)];
3051541Srgrimes	(void)(*linesw[tp->t_line].l_modem)(tp, 0);
3069824Sbde
3079824Sbde	/*
3089824Sbde	 * XXX MDMBUF makes no sense for ptys but would inhibit the above
3099824Sbde	 * l_modem().  CLOCAL makes sense but isn't supported.   Special
3109824Sbde	 * l_modem()s that ignore carrier drop make no sense for ptys but
3119824Sbde	 * may be in use because other parts of the line discipline make
3129824Sbde	 * sense for ptys.  Recover by doing everything that a normal
3139824Sbde	 * ttymodem() would have done except for sending a SIGHUP.
3149824Sbde	 */
3159824Sbde	tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
3169824Sbde	tp->t_state |= TS_ZOMBIE;
3179824Sbde	ttyflush(tp, FREAD | FWRITE);
3189824Sbde
3191541Srgrimes	tp->t_oproc = 0;		/* mark closed */
3201541Srgrimes	tp->t_session = 0;
3211541Srgrimes	return (0);
3221541Srgrimes}
3231541Srgrimes
3241549Srgrimesint
3251541Srgrimesptcread(dev, uio, flag)
3261541Srgrimes	dev_t dev;
3271541Srgrimes	struct uio *uio;
3281541Srgrimes	int flag;
3291541Srgrimes{
3301541Srgrimes	register struct tty *tp = &pt_tty[minor(dev)];
3311541Srgrimes	struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
3321541Srgrimes	char buf[BUFSIZ];
3331541Srgrimes	int error = 0, cc;
3341541Srgrimes
3351541Srgrimes	/*
3361541Srgrimes	 * We want to block until the slave
3371541Srgrimes	 * is open, and there's something to read;
3381541Srgrimes	 * but if we lost the slave or we're NBIO,
3391541Srgrimes	 * then return the appropriate error instead.
3401541Srgrimes	 */
3411541Srgrimes	for (;;) {
3421541Srgrimes		if (tp->t_state&TS_ISOPEN) {
3431541Srgrimes			if (pti->pt_flags&PF_PKT && pti->pt_send) {
3441541Srgrimes				error = ureadc((int)pti->pt_send, uio);
3451541Srgrimes				if (error)
3461541Srgrimes					return (error);
3471541Srgrimes				if (pti->pt_send & TIOCPKT_IOCTL) {
3481541Srgrimes					cc = min(uio->uio_resid,
3491541Srgrimes						sizeof(tp->t_termios));
3502807Sbde					uiomove((caddr_t)&tp->t_termios, cc,
3512807Sbde						uio);
3521541Srgrimes				}
3531541Srgrimes				pti->pt_send = 0;
3541541Srgrimes				return (0);
3551541Srgrimes			}
3561541Srgrimes			if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
3571541Srgrimes				error = ureadc((int)pti->pt_ucntl, uio);
3581541Srgrimes				if (error)
3591541Srgrimes					return (error);
3601541Srgrimes				pti->pt_ucntl = 0;
3611541Srgrimes				return (0);
3621541Srgrimes			}
3631541Srgrimes			if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
3641541Srgrimes				break;
3651541Srgrimes		}
3669824Sbde		if ((tp->t_state & TS_CONNECTED) == 0)
3671541Srgrimes			return (0);	/* EOF */
3681541Srgrimes		if (flag & IO_NDELAY)
3691541Srgrimes			return (EWOULDBLOCK);
3709639Sbde		error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0);
3713308Sphk		if (error)
3721541Srgrimes			return (error);
3731541Srgrimes	}
3741541Srgrimes	if (pti->pt_flags & (PF_PKT|PF_UCNTL))
3751541Srgrimes		error = ureadc(0, uio);
3761541Srgrimes	while (uio->uio_resid > 0 && error == 0) {
3771541Srgrimes		cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
3781541Srgrimes		if (cc <= 0)
3791541Srgrimes			break;
3801541Srgrimes		error = uiomove(buf, cc, uio);
3811541Srgrimes	}
3829626Sbde	ttwwakeup(tp);
3831541Srgrimes	return (error);
3841541Srgrimes}
3851541Srgrimes
3861541Srgrimesvoid
3871541Srgrimesptsstop(tp, flush)
3881541Srgrimes	register struct tty *tp;
3891541Srgrimes	int flush;
3901541Srgrimes{
3911541Srgrimes	struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
3921541Srgrimes	int flag;
3931541Srgrimes
3941541Srgrimes	/* note: FLUSHREAD and FLUSHWRITE already ok */
3951541Srgrimes	if (flush == 0) {
3961541Srgrimes		flush = TIOCPKT_STOP;
3971541Srgrimes		pti->pt_flags |= PF_STOPPED;
3981541Srgrimes	} else
3991541Srgrimes		pti->pt_flags &= ~PF_STOPPED;
4001541Srgrimes	pti->pt_send |= flush;
4011541Srgrimes	/* change of perspective */
4021541Srgrimes	flag = 0;
4031541Srgrimes	if (flush & FREAD)
4041541Srgrimes		flag |= FWRITE;
4051541Srgrimes	if (flush & FWRITE)
4061541Srgrimes		flag |= FREAD;
4071541Srgrimes	ptcwakeup(tp, flag);
4081541Srgrimes}
4091541Srgrimes
4101549Srgrimesint
4111541Srgrimesptcselect(dev, rw, p)
4121541Srgrimes	dev_t dev;
4131541Srgrimes	int rw;
4141541Srgrimes	struct proc *p;
4151541Srgrimes{
4161541Srgrimes	register struct tty *tp = &pt_tty[minor(dev)];
4171541Srgrimes	struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
4181541Srgrimes	int s;
4191541Srgrimes
4209824Sbde	if ((tp->t_state & TS_CONNECTED) == 0)
4211541Srgrimes		return (1);
4221541Srgrimes	switch (rw) {
4231541Srgrimes
4241541Srgrimes	case FREAD:
4251541Srgrimes		/*
4261541Srgrimes		 * Need to block timeouts (ttrstart).
4271541Srgrimes		 */
4281541Srgrimes		s = spltty();
4291541Srgrimes		if ((tp->t_state&TS_ISOPEN) &&
4301541Srgrimes		     tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) {
4311541Srgrimes			splx(s);
4321541Srgrimes			return (1);
4331541Srgrimes		}
4341541Srgrimes		splx(s);
4351541Srgrimes		/* FALLTHROUGH */
4361541Srgrimes
4371541Srgrimes	case 0:					/* exceptional */
4381541Srgrimes		if ((tp->t_state&TS_ISOPEN) &&
4393308Sphk		    ((pti->pt_flags&PF_PKT && pti->pt_send) ||
4403308Sphk		     (pti->pt_flags&PF_UCNTL && pti->pt_ucntl)))
4411541Srgrimes			return (1);
4421541Srgrimes		selrecord(p, &pti->pt_selr);
4431541Srgrimes		break;
4441541Srgrimes
4451541Srgrimes
4461541Srgrimes	case FWRITE:
4471541Srgrimes		if (tp->t_state&TS_ISOPEN) {
4481541Srgrimes			if (pti->pt_flags & PF_REMOTE) {
4491541Srgrimes			    if (tp->t_canq.c_cc == 0)
4501541Srgrimes				return (1);
4511541Srgrimes			} else {
4521541Srgrimes			    if (tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2)
4531541Srgrimes				    return (1);
4541541Srgrimes			    if (tp->t_canq.c_cc == 0 && (tp->t_iflag&ICANON))
4551541Srgrimes				    return (1);
4561541Srgrimes			}
4571541Srgrimes		}
4581541Srgrimes		selrecord(p, &pti->pt_selw);
4591541Srgrimes		break;
4601541Srgrimes
4611541Srgrimes	}
4621541Srgrimes	return (0);
4631541Srgrimes}
4641541Srgrimes
4651549Srgrimesint
4661541Srgrimesptcwrite(dev, uio, flag)
4671541Srgrimes	dev_t dev;
4681541Srgrimes	register struct uio *uio;
4691541Srgrimes	int flag;
4701541Srgrimes{
4711541Srgrimes	register struct tty *tp = &pt_tty[minor(dev)];
4721549Srgrimes	register u_char *cp = 0;
4731541Srgrimes	register int cc = 0;
4741541Srgrimes	u_char locbuf[BUFSIZ];
4751541Srgrimes	int cnt = 0;
4761541Srgrimes	struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
4771541Srgrimes	int error = 0;
4781541Srgrimes
4791541Srgrimesagain:
4801541Srgrimes	if ((tp->t_state&TS_ISOPEN) == 0)
4811541Srgrimes		goto block;
4821541Srgrimes	if (pti->pt_flags & PF_REMOTE) {
4831541Srgrimes		if (tp->t_canq.c_cc)
4841541Srgrimes			goto block;
4851541Srgrimes		while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) {
4861541Srgrimes			if (cc == 0) {
4871541Srgrimes				cc = min(uio->uio_resid, BUFSIZ);
4881541Srgrimes				cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
4891541Srgrimes				cp = locbuf;
4901541Srgrimes				error = uiomove((caddr_t)cp, cc, uio);
4911541Srgrimes				if (error)
4921541Srgrimes					return (error);
4931541Srgrimes				/* check again for safety */
4941541Srgrimes				if ((tp->t_state&TS_ISOPEN) == 0)
4951541Srgrimes					return (EIO);
4961541Srgrimes			}
4971541Srgrimes			if (cc)
4981541Srgrimes				(void) b_to_q((char *)cp, cc, &tp->t_canq);
4991541Srgrimes			cc = 0;
5001541Srgrimes		}
5011541Srgrimes		(void) putc(0, &tp->t_canq);
5021541Srgrimes		ttwakeup(tp);
5039639Sbde		wakeup(TSA_PTS_READ(tp));
5041541Srgrimes		return (0);
5051541Srgrimes	}
5061541Srgrimes	while (uio->uio_resid > 0) {
5071541Srgrimes		if (cc == 0) {
5081541Srgrimes			cc = min(uio->uio_resid, BUFSIZ);
5091541Srgrimes			cp = locbuf;
5101541Srgrimes			error = uiomove((caddr_t)cp, cc, uio);
5111541Srgrimes			if (error)
5121541Srgrimes				return (error);
5131541Srgrimes			/* check again for safety */
5141541Srgrimes			if ((tp->t_state&TS_ISOPEN) == 0)
5151541Srgrimes				return (EIO);
5161541Srgrimes		}
5171541Srgrimes		while (cc > 0) {
5181541Srgrimes			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
5191541Srgrimes			   (tp->t_canq.c_cc > 0 || !(tp->t_iflag&ICANON))) {
5209824Sbde				wakeup(TSA_HUP_OR_INPUT(tp));
5211541Srgrimes				goto block;
5221541Srgrimes			}
5231541Srgrimes			(*linesw[tp->t_line].l_rint)(*cp++, tp);
5241541Srgrimes			cnt++;
5251541Srgrimes			cc--;
5261541Srgrimes		}
5271541Srgrimes		cc = 0;
5281541Srgrimes	}
5291541Srgrimes	return (0);
5301541Srgrimesblock:
5311541Srgrimes	/*
5321541Srgrimes	 * Come here to wait for slave to open, for space
5331541Srgrimes	 * in outq, or space in rawq.
5341541Srgrimes	 */
5359824Sbde	if ((tp->t_state & TS_CONNECTED) == 0)
5361541Srgrimes		return (EIO);
5371541Srgrimes	if (flag & IO_NDELAY) {
5381541Srgrimes		/* adjust for data copied in but not written */
5391541Srgrimes		uio->uio_resid += cc;
5401541Srgrimes		if (cnt == 0)
5411541Srgrimes			return (EWOULDBLOCK);
5421541Srgrimes		return (0);
5431541Srgrimes	}
5449639Sbde	error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0);
5453308Sphk	if (error) {
5461541Srgrimes		/* adjust for data copied in but not written */
5471541Srgrimes		uio->uio_resid += cc;
5481541Srgrimes		return (error);
5491541Srgrimes	}
5501541Srgrimes	goto again;
5511541Srgrimes}
5521541Srgrimes
5536712Spststruct tty *
5546712Spstptydevtotty(dev)
5556712Spst	dev_t		dev;
5566712Spst{
5576712Spst	if (minor(dev) >= npty)
5586712Spst		return (NULL);
5596712Spst
5606712Spst	return &pt_tty[minor(dev)];
5616712Spst}
5626712Spst
5631541Srgrimes/*ARGSUSED*/
5641549Srgrimesint
5651541Srgrimesptyioctl(dev, cmd, data, flag, p)
5661541Srgrimes	dev_t dev;
5671541Srgrimes	int cmd;
5681541Srgrimes	caddr_t data;
5691541Srgrimes	int flag;
5701541Srgrimes	struct proc *p;
5711541Srgrimes{
5721541Srgrimes	register struct tty *tp = &pt_tty[minor(dev)];
5731541Srgrimes	register struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
5741541Srgrimes	register u_char *cc = tp->t_cc;
5751541Srgrimes	int stop, error;
5761541Srgrimes
5771541Srgrimes	/*
5781541Srgrimes	 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
5791541Srgrimes	 * ttywflush(tp) will hang if there are characters in the outq.
5801541Srgrimes	 */
5811541Srgrimes	if (cmd == TIOCEXT) {
5821541Srgrimes		/*
5831541Srgrimes		 * When the EXTPROC bit is being toggled, we need
5841541Srgrimes		 * to send an TIOCPKT_IOCTL if the packet driver
5851541Srgrimes		 * is turned on.
5861541Srgrimes		 */
5871541Srgrimes		if (*(int *)data) {
5881541Srgrimes			if (pti->pt_flags & PF_PKT) {
5891541Srgrimes				pti->pt_send |= TIOCPKT_IOCTL;
5901541Srgrimes				ptcwakeup(tp, FREAD);
5911541Srgrimes			}
5921541Srgrimes			tp->t_lflag |= EXTPROC;
5931541Srgrimes		} else {
5941541Srgrimes			if ((tp->t_state & EXTPROC) &&
5951541Srgrimes			    (pti->pt_flags & PF_PKT)) {
5961541Srgrimes				pti->pt_send |= TIOCPKT_IOCTL;
5971541Srgrimes				ptcwakeup(tp, FREAD);
5981541Srgrimes			}
5991541Srgrimes			tp->t_lflag &= ~EXTPROC;
6001541Srgrimes		}
6011541Srgrimes		return(0);
6021541Srgrimes	} else
6031541Srgrimes	if (cdevsw[major(dev)].d_open == ptcopen)
6041541Srgrimes		switch (cmd) {
6051541Srgrimes
6061541Srgrimes		case TIOCGPGRP:
6071541Srgrimes			/*
6081541Srgrimes			 * We aviod calling ttioctl on the controller since,
6091541Srgrimes			 * in that case, tp must be the controlling terminal.
6101541Srgrimes			 */
6111541Srgrimes			*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
6121541Srgrimes			return (0);
6131541Srgrimes
6141541Srgrimes		case TIOCPKT:
6151541Srgrimes			if (*(int *)data) {
6161541Srgrimes				if (pti->pt_flags & PF_UCNTL)
6171541Srgrimes					return (EINVAL);
6181541Srgrimes				pti->pt_flags |= PF_PKT;
6191541Srgrimes			} else
6201541Srgrimes				pti->pt_flags &= ~PF_PKT;
6211541Srgrimes			return (0);
6221541Srgrimes
6231541Srgrimes		case TIOCUCNTL:
6241541Srgrimes			if (*(int *)data) {
6251541Srgrimes				if (pti->pt_flags & PF_PKT)
6261541Srgrimes					return (EINVAL);
6271541Srgrimes				pti->pt_flags |= PF_UCNTL;
6281541Srgrimes			} else
6291541Srgrimes				pti->pt_flags &= ~PF_UCNTL;
6301541Srgrimes			return (0);
6311541Srgrimes
6321541Srgrimes		case TIOCREMOTE:
6331541Srgrimes			if (*(int *)data)
6341541Srgrimes				pti->pt_flags |= PF_REMOTE;
6351541Srgrimes			else
6361541Srgrimes				pti->pt_flags &= ~PF_REMOTE;
6371541Srgrimes			ttyflush(tp, FREAD|FWRITE);
6381541Srgrimes			return (0);
6391541Srgrimes
6401541Srgrimes#ifdef COMPAT_43
6418876Srgrimes		case TIOCSETP:
6421541Srgrimes		case TIOCSETN:
6431541Srgrimes#endif
6441541Srgrimes		case TIOCSETD:
6451541Srgrimes		case TIOCSETA:
6461541Srgrimes		case TIOCSETAW:
6471541Srgrimes		case TIOCSETAF:
6481541Srgrimes			ndflush(&tp->t_outq, tp->t_outq.c_cc);
6491541Srgrimes			break;
6501541Srgrimes
6511541Srgrimes		case TIOCSIG:
6521541Srgrimes			if (*(unsigned int *)data >= NSIG)
6531541Srgrimes				return(EINVAL);
6541541Srgrimes			if ((tp->t_lflag&NOFLSH) == 0)
6551541Srgrimes				ttyflush(tp, FREAD|FWRITE);
6561541Srgrimes			pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
6571541Srgrimes			if ((*(unsigned int *)data == SIGINFO) &&
6581541Srgrimes			    ((tp->t_lflag&NOKERNINFO) == 0))
6591541Srgrimes				ttyinfo(tp);
6601541Srgrimes			return(0);
6611541Srgrimes		}
6621541Srgrimes	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
6631541Srgrimes	if (error < 0)
6641541Srgrimes		 error = ttioctl(tp, cmd, data, flag);
6651541Srgrimes	if (error < 0) {
6661541Srgrimes		if (pti->pt_flags & PF_UCNTL &&
6671541Srgrimes		    (cmd & ~0xff) == UIOCCMD(0)) {
6681541Srgrimes			if (cmd & 0xff) {
6691541Srgrimes				pti->pt_ucntl = (u_char)cmd;
6701541Srgrimes				ptcwakeup(tp, FREAD);
6711541Srgrimes			}
6721541Srgrimes			return (0);
6731541Srgrimes		}
6741541Srgrimes		error = ENOTTY;
6751541Srgrimes	}
6761541Srgrimes	/*
6771541Srgrimes	 * If external processing and packet mode send ioctl packet.
6781541Srgrimes	 */
6791541Srgrimes	if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
6801541Srgrimes		switch(cmd) {
6811541Srgrimes		case TIOCSETA:
6821541Srgrimes		case TIOCSETAW:
6831541Srgrimes		case TIOCSETAF:
6841541Srgrimes#ifdef COMPAT_43
6851541Srgrimes		case TIOCSETP:
6861541Srgrimes		case TIOCSETN:
6871541Srgrimes#endif
6881541Srgrimes#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
6891541Srgrimes		case TIOCSETC:
6901541Srgrimes		case TIOCSLTC:
6911541Srgrimes		case TIOCLBIS:
6921541Srgrimes		case TIOCLBIC:
6931541Srgrimes		case TIOCLSET:
6941541Srgrimes#endif
6951541Srgrimes			pti->pt_send |= TIOCPKT_IOCTL;
6961541Srgrimes			ptcwakeup(tp, FREAD);
6971541Srgrimes		default:
6981541Srgrimes			break;
6991541Srgrimes		}
7001541Srgrimes	}
7018876Srgrimes	stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
7021541Srgrimes		&& CCEQ(cc[VSTART], CTRL('q'));
7031541Srgrimes	if (pti->pt_flags & PF_NOSTOP) {
7041541Srgrimes		if (stop) {
7051541Srgrimes			pti->pt_send &= ~TIOCPKT_NOSTOP;
7061541Srgrimes			pti->pt_send |= TIOCPKT_DOSTOP;
7071541Srgrimes			pti->pt_flags &= ~PF_NOSTOP;
7081541Srgrimes			ptcwakeup(tp, FREAD);
7091541Srgrimes		}
7101541Srgrimes	} else {
7111541Srgrimes		if (!stop) {
7121541Srgrimes			pti->pt_send &= ~TIOCPKT_DOSTOP;
7131541Srgrimes			pti->pt_send |= TIOCPKT_NOSTOP;
7141541Srgrimes			pti->pt_flags |= PF_NOSTOP;
7151541Srgrimes			ptcwakeup(tp, FREAD);
7161541Srgrimes		}
7171541Srgrimes	}
7181541Srgrimes	return (error);
7191541Srgrimes}
720