pty.c revision 77176
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 *
3314511Shsu *	@(#)tty_pty.c	8.4 (Berkeley) 2/20/95
3450477Speter * $FreeBSD: head/sys/kern/tty_pty.c 77176 2001-05-25 13:23:42Z phk $
351541Srgrimes */
361541Srgrimes
371541Srgrimes/*
381541Srgrimes * Pseudo-teletype Driver
391541Srgrimes * (Actually two drivers, requiring two entries in 'cdevsw')
401541Srgrimes */
4131778Seivind#include "opt_compat.h"
421541Srgrimes#include <sys/param.h>
431541Srgrimes#include <sys/systm.h>
4424207Sbde#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
4524207Sbde#include <sys/ioctl_compat.h>
4624207Sbde#endif
471541Srgrimes#include <sys/proc.h>
481541Srgrimes#include <sys/tty.h>
491541Srgrimes#include <sys/conf.h>
5024131Sbde#include <sys/fcntl.h>
5129354Speter#include <sys/poll.h>
521541Srgrimes#include <sys/kernel.h>
531541Srgrimes#include <sys/vnode.h>
543308Sphk#include <sys/signalvar.h>
5549536Sphk#include <sys/malloc.h>
561541Srgrimes
5769774Sphkstatic MALLOC_DEFINE(M_PTY, "ptys", "pty data structures");
5812517Sjulian
5912819Sphkstatic void ptsstart __P((struct tty *tp));
6051654Sphkstatic void ptsstop __P((struct tty *tp, int rw));
6112819Sphkstatic void ptcwakeup __P((struct tty *tp, int flag));
6277176Sphkstatic dev_t ptyinit __P((dev_t cdev));
6311789Sbde
6412675Sjulianstatic	d_open_t	ptsopen;
6512675Sjulianstatic	d_close_t	ptsclose;
6612675Sjulianstatic	d_read_t	ptsread;
6712675Sjulianstatic	d_write_t	ptswrite;
6812675Sjulianstatic	d_ioctl_t	ptyioctl;
6912675Sjulianstatic	d_open_t	ptcopen;
7012675Sjulianstatic	d_close_t	ptcclose;
7112675Sjulianstatic	d_read_t	ptcread;
7212675Sjulianstatic	d_write_t	ptcwrite;
7329354Speterstatic	d_poll_t	ptcpoll;
7412675Sjulian
7538485Sbde#define	CDEV_MAJOR_S	5
7647625Sphkstatic struct cdevsw pts_cdevsw = {
7747625Sphk	/* open */	ptsopen,
7847625Sphk	/* close */	ptsclose,
7947625Sphk	/* read */	ptsread,
8047625Sphk	/* write */	ptswrite,
8147625Sphk	/* ioctl */	ptyioctl,
8251654Sphk	/* poll */	ttypoll,
8347625Sphk	/* mmap */	nommap,
8447625Sphk	/* strategy */	nostrategy,
8547625Sphk	/* name */	"pts",
8647625Sphk	/* maj */	CDEV_MAJOR_S,
8747625Sphk	/* dump */	nodump,
8847625Sphk	/* psize */	nopsize,
8972521Sjlemon	/* flags */	D_TTY | D_KQFILTER,
9072521Sjlemon	/* kqfilter */	ttykqfilter,
9138485Sbde};
9212675Sjulian
9338485Sbde#define	CDEV_MAJOR_C	6
9447625Sphkstatic struct cdevsw ptc_cdevsw = {
9547625Sphk	/* open */	ptcopen,
9647625Sphk	/* close */	ptcclose,
9747625Sphk	/* read */	ptcread,
9847625Sphk	/* write */	ptcwrite,
9947625Sphk	/* ioctl */	ptyioctl,
10047625Sphk	/* poll */	ptcpoll,
10147625Sphk	/* mmap */	nommap,
10247625Sphk	/* strategy */	nostrategy,
10347625Sphk	/* name */	"ptc",
10447625Sphk	/* maj */	CDEV_MAJOR_C,
10547625Sphk	/* dump */	nodump,
10647625Sphk	/* psize */	nopsize,
10772521Sjlemon	/* flags */	D_TTY | D_KQFILTER,
10872521Sjlemon	/* kqfilter */	ttykqfilter,
10938485Sbde};
11012675Sjulian
1111541Srgrimes#define BUFSIZ 100		/* Chunk size iomoved to/from user */
1121541Srgrimes
11349536Sphkstruct	pt_ioctl {
1141541Srgrimes	int	pt_flags;
1151541Srgrimes	struct	selinfo pt_selr, pt_selw;
1161541Srgrimes	u_char	pt_send;
1171541Srgrimes	u_char	pt_ucntl;
11849536Sphk	struct tty pt_tty;
11950092Sjulian	dev_t	devs, devc;
12057070Srwatson	struct	prison *pt_prison;
12149536Sphk};
1221541Srgrimes
1231541Srgrimes#define	PF_PKT		0x08		/* packet mode */
1241541Srgrimes#define	PF_STOPPED	0x10		/* user told stopped */
1251541Srgrimes#define	PF_REMOTE	0x20		/* remote and flow controlled input */
1261541Srgrimes#define	PF_NOSTOP	0x40
1271541Srgrimes#define PF_UCNTL	0x80		/* user control mode */
1281541Srgrimes
12977176Sphkstatic char *names = "pqrsPQRS";
1301541Srgrimes/*
13149536Sphk * This function creates and initializes a pts/ptc pair
1321541Srgrimes *
13349536Sphk * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
13449536Sphk * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
13549536Sphk *
13649536Sphk * XXX: define and add mapping of upper minor bits to allow more
13749536Sphk *      than 256 ptys.
1381541Srgrimes */
13964880Sphkstatic dev_t
14077176Sphkptyinit(dev_t devc)
1411541Srgrimes{
14277176Sphk	dev_t devs;
14349536Sphk	struct pt_ioctl *pt;
14477176Sphk	int n;
1451541Srgrimes
14677176Sphk	devc->si_flags &= ~SI_CHEAPCLONE;
14777176Sphk	n = minor(devc);
14849536Sphk	/* For now we only map the lower 8 bits of the minor */
14949536Sphk	if (n & ~0xff)
15064880Sphk		return (NODEV);
15149536Sphk
15269781Sdwmalone	pt = malloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO);
15350092Sjulian	pt->devs = devs = make_dev(&pts_cdevsw, n,
15466067Sphk	    UID_ROOT, GID_WHEEL, 0666, "tty%c%r", names[n / 32], n % 32);
15577176Sphk	pt->devc = devc;
15649536Sphk
15749536Sphk	devs->si_drv1 = devc->si_drv1 = pt;
15850652Sphk	devs->si_tty = devc->si_tty = &pt->pt_tty;
15949540Sphk	ttyregister(&pt->pt_tty);
16064880Sphk	return (devc);
16116322Sgpalmer}
1621541Srgrimes
1631541Srgrimes/*ARGSUSED*/
16412675Sjulianstatic	int
1651541Srgrimesptsopen(dev, flag, devtype, p)
1661541Srgrimes	dev_t dev;
1671541Srgrimes	int flag, devtype;
1681541Srgrimes	struct proc *p;
1691541Srgrimes{
1701541Srgrimes	register struct tty *tp;
1711541Srgrimes	int error;
17257070Srwatson	struct pt_ioctl *pti;
1731541Srgrimes
17449536Sphk	if (!dev->si_drv1)
17549536Sphk		return(ENXIO);
17657070Srwatson	pti = dev->si_drv1;
17750652Sphk	tp = dev->si_tty;
1781541Srgrimes	if ((tp->t_state & TS_ISOPEN) == 0) {
1791541Srgrimes		ttychars(tp);		/* Set up default chars */
1801541Srgrimes		tp->t_iflag = TTYDEF_IFLAG;
1811541Srgrimes		tp->t_oflag = TTYDEF_OFLAG;
1821541Srgrimes		tp->t_lflag = TTYDEF_LFLAG;
1831541Srgrimes		tp->t_cflag = TTYDEF_CFLAG;
1841541Srgrimes		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
18557070Srwatson	} else if (tp->t_state & TS_XCLUDE && suser(p)) {
1861541Srgrimes		return (EBUSY);
18772786Srwatson	} else if (pti->pt_prison != p->p_ucred->cr_prison) {
18857070Srwatson		return (EBUSY);
18957070Srwatson	}
1901541Srgrimes	if (tp->t_oproc)			/* Ctrlr still around. */
1919824Sbde		(void)(*linesw[tp->t_line].l_modem)(tp, 1);
1921541Srgrimes	while ((tp->t_state & TS_CARR_ON) == 0) {
1931541Srgrimes		if (flag&FNONBLOCK)
1941541Srgrimes			break;
1959639Sbde		error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH,
1969624Sbde				 "ptsopn", 0);
1973308Sphk		if (error)
1981541Srgrimes			return (error);
1991541Srgrimes	}
2001541Srgrimes	error = (*linesw[tp->t_line].l_open)(dev, tp);
2017724Sache	if (error == 0)
2027724Sache		ptcwakeup(tp, FREAD|FWRITE);
2031541Srgrimes	return (error);
2041541Srgrimes}
2051541Srgrimes
20612675Sjulianstatic	int
2071541Srgrimesptsclose(dev, flag, mode, p)
2081541Srgrimes	dev_t dev;
2091541Srgrimes	int flag, mode;
2101541Srgrimes	struct proc *p;
2111541Srgrimes{
2121541Srgrimes	register struct tty *tp;
2131541Srgrimes	int err;
2141541Srgrimes
21550652Sphk	tp = dev->si_tty;
2161541Srgrimes	err = (*linesw[tp->t_line].l_close)(tp, flag);
2177730Sache	ptsstop(tp, FREAD|FWRITE);
2187724Sache	(void) ttyclose(tp);
2191541Srgrimes	return (err);
2201541Srgrimes}
2211541Srgrimes
22212675Sjulianstatic	int
2231541Srgrimesptsread(dev, uio, flag)
2241541Srgrimes	dev_t dev;
2251541Srgrimes	struct uio *uio;
2261541Srgrimes	int flag;
2271541Srgrimes{
2281541Srgrimes	struct proc *p = curproc;
22950652Sphk	register struct tty *tp = dev->si_tty;
23049536Sphk	register struct pt_ioctl *pti = dev->si_drv1;
2311541Srgrimes	int error = 0;
2321541Srgrimes
2331541Srgrimesagain:
2341541Srgrimes	if (pti->pt_flags & PF_REMOTE) {
2351541Srgrimes		while (isbackground(p, tp)) {
23651791Smarcel			if (SIGISMEMBER(p->p_sigignore, SIGTTIN) ||
23751791Smarcel			    SIGISMEMBER(p->p_sigmask, SIGTTIN) ||
23851791Smarcel			    p->p_pgrp->pg_jobc == 0 || p->p_flag & P_PPWAIT)
2391541Srgrimes				return (EIO);
2401541Srgrimes			pgsignal(p->p_pgrp, SIGTTIN, 1);
2419624Sbde			error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ptsbg",
2429624Sbde					 0);
2433308Sphk			if (error)
2441541Srgrimes				return (error);
2451541Srgrimes		}
2461541Srgrimes		if (tp->t_canq.c_cc == 0) {
2471541Srgrimes			if (flag & IO_NDELAY)
2481541Srgrimes				return (EWOULDBLOCK);
2499639Sbde			error = ttysleep(tp, TSA_PTS_READ(tp), TTIPRI | PCATCH,
2509624Sbde					 "ptsin", 0);
2513308Sphk			if (error)
2521541Srgrimes				return (error);
2531541Srgrimes			goto again;
2541541Srgrimes		}
2551541Srgrimes		while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
2561541Srgrimes			if (ureadc(getc(&tp->t_canq), uio) < 0) {
2571541Srgrimes				error = EFAULT;
2581541Srgrimes				break;
2591541Srgrimes			}
2601541Srgrimes		if (tp->t_canq.c_cc == 1)
2611541Srgrimes			(void) getc(&tp->t_canq);
2621541Srgrimes		if (tp->t_canq.c_cc)
2631541Srgrimes			return (error);
2641541Srgrimes	} else
2651541Srgrimes		if (tp->t_oproc)
2661541Srgrimes			error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
2671541Srgrimes	ptcwakeup(tp, FWRITE);
2681541Srgrimes	return (error);
2691541Srgrimes}
2701541Srgrimes
2711541Srgrimes/*
2721541Srgrimes * Write to pseudo-tty.
2731541Srgrimes * Wakeups of controlling tty will happen
2741541Srgrimes * indirectly, when tty driver calls ptsstart.
2751541Srgrimes */
27612675Sjulianstatic	int
2771541Srgrimesptswrite(dev, uio, flag)
2781541Srgrimes	dev_t dev;
2791541Srgrimes	struct uio *uio;
2801541Srgrimes	int flag;
2811541Srgrimes{
2821541Srgrimes	register struct tty *tp;
2831541Srgrimes
28450652Sphk	tp = dev->si_tty;
2851541Srgrimes	if (tp->t_oproc == 0)
2861541Srgrimes		return (EIO);
2871541Srgrimes	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
2881541Srgrimes}
2891541Srgrimes
2901541Srgrimes/*
2911541Srgrimes * Start output on pseudo-tty.
2921541Srgrimes * Wake up process selecting or sleeping for input from controlling tty.
2931541Srgrimes */
29412819Sphkstatic void
2951541Srgrimesptsstart(tp)
2961541Srgrimes	struct tty *tp;
2971541Srgrimes{
29849536Sphk	register struct pt_ioctl *pti = tp->t_dev->si_drv1;
2991541Srgrimes
3001541Srgrimes	if (tp->t_state & TS_TTSTOP)
3011541Srgrimes		return;
3021541Srgrimes	if (pti->pt_flags & PF_STOPPED) {
3031541Srgrimes		pti->pt_flags &= ~PF_STOPPED;
3041541Srgrimes		pti->pt_send = TIOCPKT_START;
3051541Srgrimes	}
3061541Srgrimes	ptcwakeup(tp, FREAD);
3071541Srgrimes}
3081541Srgrimes
30912819Sphkstatic void
3101541Srgrimesptcwakeup(tp, flag)
3111541Srgrimes	struct tty *tp;
3121541Srgrimes	int flag;
3131541Srgrimes{
31449536Sphk	struct pt_ioctl *pti = tp->t_dev->si_drv1;
3151541Srgrimes
3161541Srgrimes	if (flag & FREAD) {
3171541Srgrimes		selwakeup(&pti->pt_selr);
3189639Sbde		wakeup(TSA_PTC_READ(tp));
3191541Srgrimes	}
3201541Srgrimes	if (flag & FWRITE) {
3211541Srgrimes		selwakeup(&pti->pt_selw);
3229639Sbde		wakeup(TSA_PTC_WRITE(tp));
3231541Srgrimes	}
3241541Srgrimes}
3251541Srgrimes
32612675Sjulianstatic	int
3271541Srgrimesptcopen(dev, flag, devtype, p)
3281541Srgrimes	dev_t dev;
3291541Srgrimes	int flag, devtype;
3301541Srgrimes	struct proc *p;
3311541Srgrimes{
3321541Srgrimes	register struct tty *tp;
3331541Srgrimes	struct pt_ioctl *pti;
3341541Srgrimes
33549536Sphk	if (!dev->si_drv1)
33677176Sphk		ptyinit(dev);
33749536Sphk	if (!dev->si_drv1)
33849536Sphk		return(ENXIO);
33950652Sphk	tp = dev->si_tty;
3401541Srgrimes	if (tp->t_oproc)
3411541Srgrimes		return (EIO);
34259818Sache	tp->t_timeout = -1;
3431541Srgrimes	tp->t_oproc = ptsstart;
34451654Sphk	tp->t_stop = ptsstop;
3451541Srgrimes	(void)(*linesw[tp->t_line].l_modem)(tp, 1);
3461541Srgrimes	tp->t_lflag &= ~EXTPROC;
34749536Sphk	pti = dev->si_drv1;
34872786Srwatson	pti->pt_prison = p->p_ucred->cr_prison;
3491541Srgrimes	pti->pt_flags = 0;
3501541Srgrimes	pti->pt_send = 0;
3511541Srgrimes	pti->pt_ucntl = 0;
3521541Srgrimes	return (0);
3531541Srgrimes}
3541541Srgrimes
35512675Sjulianstatic	int
35610624Sbdeptcclose(dev, flags, fmt, p)
3571541Srgrimes	dev_t dev;
35810624Sbde	int flags;
35910624Sbde	int fmt;
36010624Sbde	struct proc *p;
3611541Srgrimes{
3621541Srgrimes	register struct tty *tp;
3631541Srgrimes
36450652Sphk	tp = dev->si_tty;
3651541Srgrimes	(void)(*linesw[tp->t_line].l_modem)(tp, 0);
3669824Sbde
3679824Sbde	/*
3689824Sbde	 * XXX MDMBUF makes no sense for ptys but would inhibit the above
3699824Sbde	 * l_modem().  CLOCAL makes sense but isn't supported.   Special
3709824Sbde	 * l_modem()s that ignore carrier drop make no sense for ptys but
3719824Sbde	 * may be in use because other parts of the line discipline make
3729824Sbde	 * sense for ptys.  Recover by doing everything that a normal
3739824Sbde	 * ttymodem() would have done except for sending a SIGHUP.
3749824Sbde	 */
3759850Sbde	if (tp->t_state & TS_ISOPEN) {
3769850Sbde		tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
3779850Sbde		tp->t_state |= TS_ZOMBIE;
3789850Sbde		ttyflush(tp, FREAD | FWRITE);
3799850Sbde	}
3809824Sbde
3811541Srgrimes	tp->t_oproc = 0;		/* mark closed */
3821541Srgrimes	return (0);
3831541Srgrimes}
3841541Srgrimes
38512675Sjulianstatic	int
3861541Srgrimesptcread(dev, uio, flag)
3871541Srgrimes	dev_t dev;
3881541Srgrimes	struct uio *uio;
3891541Srgrimes	int flag;
3901541Srgrimes{
39150652Sphk	register struct tty *tp = dev->si_tty;
39249536Sphk	struct pt_ioctl *pti = dev->si_drv1;
3931541Srgrimes	char buf[BUFSIZ];
3941541Srgrimes	int error = 0, cc;
3951541Srgrimes
3961541Srgrimes	/*
3971541Srgrimes	 * We want to block until the slave
3981541Srgrimes	 * is open, and there's something to read;
3991541Srgrimes	 * but if we lost the slave or we're NBIO,
4001541Srgrimes	 * then return the appropriate error instead.
4011541Srgrimes	 */
4021541Srgrimes	for (;;) {
4031541Srgrimes		if (tp->t_state&TS_ISOPEN) {
4041541Srgrimes			if (pti->pt_flags&PF_PKT && pti->pt_send) {
4051541Srgrimes				error = ureadc((int)pti->pt_send, uio);
4061541Srgrimes				if (error)
4071541Srgrimes					return (error);
4081541Srgrimes				if (pti->pt_send & TIOCPKT_IOCTL) {
4091541Srgrimes					cc = min(uio->uio_resid,
4101541Srgrimes						sizeof(tp->t_termios));
4112807Sbde					uiomove((caddr_t)&tp->t_termios, cc,
4122807Sbde						uio);
4131541Srgrimes				}
4141541Srgrimes				pti->pt_send = 0;
4151541Srgrimes				return (0);
4161541Srgrimes			}
4171541Srgrimes			if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
4181541Srgrimes				error = ureadc((int)pti->pt_ucntl, uio);
4191541Srgrimes				if (error)
4201541Srgrimes					return (error);
4211541Srgrimes				pti->pt_ucntl = 0;
4221541Srgrimes				return (0);
4231541Srgrimes			}
4241541Srgrimes			if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
4251541Srgrimes				break;
4261541Srgrimes		}
4279824Sbde		if ((tp->t_state & TS_CONNECTED) == 0)
4281541Srgrimes			return (0);	/* EOF */
4291541Srgrimes		if (flag & IO_NDELAY)
4301541Srgrimes			return (EWOULDBLOCK);
4319639Sbde		error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0);
4323308Sphk		if (error)
4331541Srgrimes			return (error);
4341541Srgrimes	}
4351541Srgrimes	if (pti->pt_flags & (PF_PKT|PF_UCNTL))
4361541Srgrimes		error = ureadc(0, uio);
4371541Srgrimes	while (uio->uio_resid > 0 && error == 0) {
4381541Srgrimes		cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
4391541Srgrimes		if (cc <= 0)
4401541Srgrimes			break;
4411541Srgrimes		error = uiomove(buf, cc, uio);
4421541Srgrimes	}
4439626Sbde	ttwwakeup(tp);
4441541Srgrimes	return (error);
4451541Srgrimes}
4461541Srgrimes
44712675Sjulianstatic	void
4481541Srgrimesptsstop(tp, flush)
4491541Srgrimes	register struct tty *tp;
4501541Srgrimes	int flush;
4511541Srgrimes{
45249536Sphk	struct pt_ioctl *pti = tp->t_dev->si_drv1;
4531541Srgrimes	int flag;
4541541Srgrimes
4551541Srgrimes	/* note: FLUSHREAD and FLUSHWRITE already ok */
4561541Srgrimes	if (flush == 0) {
4571541Srgrimes		flush = TIOCPKT_STOP;
4581541Srgrimes		pti->pt_flags |= PF_STOPPED;
4591541Srgrimes	} else
4601541Srgrimes		pti->pt_flags &= ~PF_STOPPED;
4611541Srgrimes	pti->pt_send |= flush;
4621541Srgrimes	/* change of perspective */
4631541Srgrimes	flag = 0;
4641541Srgrimes	if (flush & FREAD)
4651541Srgrimes		flag |= FWRITE;
4661541Srgrimes	if (flush & FWRITE)
4671541Srgrimes		flag |= FREAD;
4681541Srgrimes	ptcwakeup(tp, flag);
4691541Srgrimes}
4701541Srgrimes
47112675Sjulianstatic	int
47229354Speterptcpoll(dev, events, p)
4731541Srgrimes	dev_t dev;
47429354Speter	int events;
4751541Srgrimes	struct proc *p;
4761541Srgrimes{
47750652Sphk	register struct tty *tp = dev->si_tty;
47849536Sphk	struct pt_ioctl *pti = dev->si_drv1;
47929354Speter	int revents = 0;
4801541Srgrimes	int s;
4811541Srgrimes
4829824Sbde	if ((tp->t_state & TS_CONNECTED) == 0)
48329354Speter		return (seltrue(dev, events, p) | POLLHUP);
4841541Srgrimes
48529354Speter	/*
48629354Speter	 * Need to block timeouts (ttrstart).
48729354Speter	 */
48829354Speter	s = spltty();
4891541Srgrimes
49029354Speter	if (events & (POLLIN | POLLRDNORM))
49129354Speter		if ((tp->t_state & TS_ISOPEN) &&
49229354Speter		    ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
49329354Speter		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
49429354Speter		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
49529354Speter			revents |= events & (POLLIN | POLLRDNORM);
4961541Srgrimes
49729354Speter	if (events & (POLLOUT | POLLWRNORM))
49829354Speter		if (tp->t_state & TS_ISOPEN &&
49929354Speter		    ((pti->pt_flags & PF_REMOTE) ?
50029354Speter		     (tp->t_canq.c_cc == 0) :
50129354Speter		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
50229354Speter		      (tp->t_canq.c_cc == 0 && (tp->t_iflag & ICANON)))))
50329354Speter			revents |= events & (POLLOUT | POLLWRNORM);
5041541Srgrimes
50529354Speter	if (events & POLLHUP)
50629354Speter		if ((tp->t_state & TS_CARR_ON) == 0)
50729354Speter			revents |= POLLHUP;
5081541Srgrimes
50929354Speter	if (revents == 0) {
51029354Speter		if (events & (POLLIN | POLLRDNORM))
51129354Speter			selrecord(p, &pti->pt_selr);
51229354Speter
51329354Speter		if (events & (POLLOUT | POLLWRNORM))
51429354Speter			selrecord(p, &pti->pt_selw);
5151541Srgrimes	}
51629354Speter	splx(s);
51729354Speter
51829354Speter	return (revents);
5191541Srgrimes}
5201541Srgrimes
52112675Sjulianstatic	int
5221541Srgrimesptcwrite(dev, uio, flag)
5231541Srgrimes	dev_t dev;
5241541Srgrimes	register struct uio *uio;
5251541Srgrimes	int flag;
5261541Srgrimes{
52750652Sphk	register struct tty *tp = dev->si_tty;
5281549Srgrimes	register u_char *cp = 0;
5291541Srgrimes	register int cc = 0;
5301541Srgrimes	u_char locbuf[BUFSIZ];
5311541Srgrimes	int cnt = 0;
53249536Sphk	struct pt_ioctl *pti = dev->si_drv1;
5331541Srgrimes	int error = 0;
5341541Srgrimes
5351541Srgrimesagain:
5361541Srgrimes	if ((tp->t_state&TS_ISOPEN) == 0)
5371541Srgrimes		goto block;
5381541Srgrimes	if (pti->pt_flags & PF_REMOTE) {
5391541Srgrimes		if (tp->t_canq.c_cc)
5401541Srgrimes			goto block;
54111789Sbde		while ((uio->uio_resid > 0 || cc > 0) &&
54211789Sbde		       tp->t_canq.c_cc < TTYHOG - 1) {
5431541Srgrimes			if (cc == 0) {
5441541Srgrimes				cc = min(uio->uio_resid, BUFSIZ);
5451541Srgrimes				cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
5461541Srgrimes				cp = locbuf;
5471541Srgrimes				error = uiomove((caddr_t)cp, cc, uio);
5481541Srgrimes				if (error)
5491541Srgrimes					return (error);
5501541Srgrimes				/* check again for safety */
55111789Sbde				if ((tp->t_state & TS_ISOPEN) == 0) {
55211789Sbde					/* adjust as usual */
55311789Sbde					uio->uio_resid += cc;
5541541Srgrimes					return (EIO);
55511789Sbde				}
5561541Srgrimes			}
55715199Sbde			if (cc > 0) {
55815199Sbde				cc = b_to_q((char *)cp, cc, &tp->t_canq);
55915199Sbde				/*
56015199Sbde				 * XXX we don't guarantee that the canq size
56115199Sbde				 * is >= TTYHOG, so the above b_to_q() may
56215199Sbde				 * leave some bytes uncopied.  However, space
56315199Sbde				 * is guaranteed for the null terminator if
56415199Sbde				 * we don't fail here since (TTYHOG - 1) is
56515199Sbde				 * not a multiple of CBSIZE.
56615199Sbde				 */
56715199Sbde				if (cc > 0)
56815199Sbde					break;
56915199Sbde			}
5701541Srgrimes		}
57111789Sbde		/* adjust for data copied in but not written */
57211789Sbde		uio->uio_resid += cc;
5731541Srgrimes		(void) putc(0, &tp->t_canq);
5741541Srgrimes		ttwakeup(tp);
5759639Sbde		wakeup(TSA_PTS_READ(tp));
5761541Srgrimes		return (0);
5771541Srgrimes	}
57811789Sbde	while (uio->uio_resid > 0 || cc > 0) {
5791541Srgrimes		if (cc == 0) {
5801541Srgrimes			cc = min(uio->uio_resid, BUFSIZ);
5811541Srgrimes			cp = locbuf;
5821541Srgrimes			error = uiomove((caddr_t)cp, cc, uio);
5831541Srgrimes			if (error)
5841541Srgrimes				return (error);
5851541Srgrimes			/* check again for safety */
58611789Sbde			if ((tp->t_state & TS_ISOPEN) == 0) {
58711789Sbde				/* adjust for data copied in but not written */
58811789Sbde				uio->uio_resid += cc;
5891541Srgrimes				return (EIO);
59011789Sbde			}
5911541Srgrimes		}
5921541Srgrimes		while (cc > 0) {
5931541Srgrimes			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
5941541Srgrimes			   (tp->t_canq.c_cc > 0 || !(tp->t_iflag&ICANON))) {
5959824Sbde				wakeup(TSA_HUP_OR_INPUT(tp));
5961541Srgrimes				goto block;
5971541Srgrimes			}
5981541Srgrimes			(*linesw[tp->t_line].l_rint)(*cp++, tp);
5991541Srgrimes			cnt++;
6001541Srgrimes			cc--;
6011541Srgrimes		}
6021541Srgrimes		cc = 0;
6031541Srgrimes	}
6041541Srgrimes	return (0);
6051541Srgrimesblock:
6061541Srgrimes	/*
6071541Srgrimes	 * Come here to wait for slave to open, for space
60815199Sbde	 * in outq, or space in rawq, or an empty canq.
6091541Srgrimes	 */
61011789Sbde	if ((tp->t_state & TS_CONNECTED) == 0) {
61111789Sbde		/* adjust for data copied in but not written */
61211789Sbde		uio->uio_resid += cc;
6131541Srgrimes		return (EIO);
61411789Sbde	}
6151541Srgrimes	if (flag & IO_NDELAY) {
6161541Srgrimes		/* adjust for data copied in but not written */
6171541Srgrimes		uio->uio_resid += cc;
6181541Srgrimes		if (cnt == 0)
6191541Srgrimes			return (EWOULDBLOCK);
6201541Srgrimes		return (0);
6211541Srgrimes	}
6229639Sbde	error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0);
6233308Sphk	if (error) {
6241541Srgrimes		/* adjust for data copied in but not written */
6251541Srgrimes		uio->uio_resid += cc;
6261541Srgrimes		return (error);
6271541Srgrimes	}
6281541Srgrimes	goto again;
6291541Srgrimes}
6301541Srgrimes
6311541Srgrimes/*ARGSUSED*/
63212675Sjulianstatic	int
6331541Srgrimesptyioctl(dev, cmd, data, flag, p)
6341541Srgrimes	dev_t dev;
63536735Sdfr	u_long cmd;
6361541Srgrimes	caddr_t data;
6371541Srgrimes	int flag;
6381541Srgrimes	struct proc *p;
6391541Srgrimes{
64050652Sphk	register struct tty *tp = dev->si_tty;
64149536Sphk	register struct pt_ioctl *pti = dev->si_drv1;
6421541Srgrimes	register u_char *cc = tp->t_cc;
6431541Srgrimes	int stop, error;
6441541Srgrimes
64547301Sluoqi	if (devsw(dev)->d_open == ptcopen) {
6461541Srgrimes		switch (cmd) {
6471541Srgrimes
6481541Srgrimes		case TIOCGPGRP:
6491541Srgrimes			/*
65033826Sbde			 * We avoid calling ttioctl on the controller since,
6511541Srgrimes			 * in that case, tp must be the controlling terminal.
6521541Srgrimes			 */
6531541Srgrimes			*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
6541541Srgrimes			return (0);
6551541Srgrimes
6561541Srgrimes		case TIOCPKT:
6571541Srgrimes			if (*(int *)data) {
6581541Srgrimes				if (pti->pt_flags & PF_UCNTL)
6591541Srgrimes					return (EINVAL);
6601541Srgrimes				pti->pt_flags |= PF_PKT;
6611541Srgrimes			} else
6621541Srgrimes				pti->pt_flags &= ~PF_PKT;
6631541Srgrimes			return (0);
6641541Srgrimes
6651541Srgrimes		case TIOCUCNTL:
6661541Srgrimes			if (*(int *)data) {
6671541Srgrimes				if (pti->pt_flags & PF_PKT)
6681541Srgrimes					return (EINVAL);
6691541Srgrimes				pti->pt_flags |= PF_UCNTL;
6701541Srgrimes			} else
6711541Srgrimes				pti->pt_flags &= ~PF_UCNTL;
6721541Srgrimes			return (0);
6731541Srgrimes
6741541Srgrimes		case TIOCREMOTE:
6751541Srgrimes			if (*(int *)data)
6761541Srgrimes				pti->pt_flags |= PF_REMOTE;
6771541Srgrimes			else
6781541Srgrimes				pti->pt_flags &= ~PF_REMOTE;
6791541Srgrimes			ttyflush(tp, FREAD|FWRITE);
6801541Srgrimes			return (0);
68147203Sluoqi		}
6821541Srgrimes
68347203Sluoqi		/*
68447203Sluoqi		 * The rest of the ioctls shouldn't be called until
68547301Sluoqi		 * the slave is open.
68647203Sluoqi		 */
68747203Sluoqi		if ((tp->t_state & TS_ISOPEN) == 0)
68847301Sluoqi			return (EAGAIN);
68947203Sluoqi
69047203Sluoqi		switch (cmd) {
6911541Srgrimes#ifdef COMPAT_43
6928876Srgrimes		case TIOCSETP:
6931541Srgrimes		case TIOCSETN:
6941541Srgrimes#endif
6951541Srgrimes		case TIOCSETD:
6961541Srgrimes		case TIOCSETA:
6971541Srgrimes		case TIOCSETAW:
6989858Sache		case TIOCSETAF:
69947301Sluoqi			/*
70047301Sluoqi			 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
70147301Sluoqi			 * ttywflush(tp) will hang if there are characters in
70247301Sluoqi			 * the outq.
70347301Sluoqi			 */
7041541Srgrimes			ndflush(&tp->t_outq, tp->t_outq.c_cc);
7051541Srgrimes			break;
7061541Srgrimes
7071541Srgrimes		case TIOCSIG:
70827770Sjmg			if (*(unsigned int *)data >= NSIG ||
70927770Sjmg			    *(unsigned int *)data == 0)
7101541Srgrimes				return(EINVAL);
7111541Srgrimes			if ((tp->t_lflag&NOFLSH) == 0)
7121541Srgrimes				ttyflush(tp, FREAD|FWRITE);
7131541Srgrimes			pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
7141541Srgrimes			if ((*(unsigned int *)data == SIGINFO) &&
7151541Srgrimes			    ((tp->t_lflag&NOKERNINFO) == 0))
7161541Srgrimes				ttyinfo(tp);
7171541Srgrimes			return(0);
7181541Srgrimes		}
71947203Sluoqi	}
72047301Sluoqi	if (cmd == TIOCEXT) {
72147301Sluoqi		/*
72247301Sluoqi		 * When the EXTPROC bit is being toggled, we need
72347301Sluoqi		 * to send an TIOCPKT_IOCTL if the packet driver
72447301Sluoqi		 * is turned on.
72547301Sluoqi		 */
72647301Sluoqi		if (*(int *)data) {
72747301Sluoqi			if (pti->pt_flags & PF_PKT) {
72847301Sluoqi				pti->pt_send |= TIOCPKT_IOCTL;
72947301Sluoqi				ptcwakeup(tp, FREAD);
73047301Sluoqi			}
73147301Sluoqi			tp->t_lflag |= EXTPROC;
73247301Sluoqi		} else {
73347301Sluoqi			if ((tp->t_lflag & EXTPROC) &&
73447301Sluoqi			    (pti->pt_flags & PF_PKT)) {
73547301Sluoqi				pti->pt_send |= TIOCPKT_IOCTL;
73647301Sluoqi				ptcwakeup(tp, FREAD);
73747301Sluoqi			}
73847301Sluoqi			tp->t_lflag &= ~EXTPROC;
73947301Sluoqi		}
74047301Sluoqi		return(0);
74147301Sluoqi	}
7421541Srgrimes	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
74331577Sbde	if (error == ENOIOCTL)
7441541Srgrimes		 error = ttioctl(tp, cmd, data, flag);
74531577Sbde	if (error == ENOIOCTL) {
7461541Srgrimes		if (pti->pt_flags & PF_UCNTL &&
7471541Srgrimes		    (cmd & ~0xff) == UIOCCMD(0)) {
7481541Srgrimes			if (cmd & 0xff) {
7491541Srgrimes				pti->pt_ucntl = (u_char)cmd;
7501541Srgrimes				ptcwakeup(tp, FREAD);
7511541Srgrimes			}
7521541Srgrimes			return (0);
7531541Srgrimes		}
7541541Srgrimes		error = ENOTTY;
7551541Srgrimes	}
7561541Srgrimes	/*
7571541Srgrimes	 * If external processing and packet mode send ioctl packet.
7581541Srgrimes	 */
7591541Srgrimes	if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
7601541Srgrimes		switch(cmd) {
7611541Srgrimes		case TIOCSETA:
7621541Srgrimes		case TIOCSETAW:
7631541Srgrimes		case TIOCSETAF:
7641541Srgrimes#ifdef COMPAT_43
7651541Srgrimes		case TIOCSETP:
7661541Srgrimes		case TIOCSETN:
7671541Srgrimes#endif
7681541Srgrimes#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
7691541Srgrimes		case TIOCSETC:
7701541Srgrimes		case TIOCSLTC:
7711541Srgrimes		case TIOCLBIS:
7721541Srgrimes		case TIOCLBIC:
7731541Srgrimes		case TIOCLSET:
7741541Srgrimes#endif
7751541Srgrimes			pti->pt_send |= TIOCPKT_IOCTL;
7761541Srgrimes			ptcwakeup(tp, FREAD);
7771541Srgrimes		default:
7781541Srgrimes			break;
7791541Srgrimes		}
7801541Srgrimes	}
7818876Srgrimes	stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
7821541Srgrimes		&& CCEQ(cc[VSTART], CTRL('q'));
7831541Srgrimes	if (pti->pt_flags & PF_NOSTOP) {
7841541Srgrimes		if (stop) {
7851541Srgrimes			pti->pt_send &= ~TIOCPKT_NOSTOP;
7861541Srgrimes			pti->pt_send |= TIOCPKT_DOSTOP;
7871541Srgrimes			pti->pt_flags &= ~PF_NOSTOP;
7881541Srgrimes			ptcwakeup(tp, FREAD);
7891541Srgrimes		}
7901541Srgrimes	} else {
7911541Srgrimes		if (!stop) {
7921541Srgrimes			pti->pt_send &= ~TIOCPKT_DOSTOP;
7931541Srgrimes			pti->pt_send |= TIOCPKT_NOSTOP;
7941541Srgrimes			pti->pt_flags |= PF_NOSTOP;
7951541Srgrimes			ptcwakeup(tp, FREAD);
7961541Srgrimes		}
7971541Srgrimes	}
7981541Srgrimes	return (error);
7991541Srgrimes}
80012517Sjulian
80112517Sjulian
80229506Sbdestatic void ptc_drvinit __P((void *unused));
80349536Sphk
80464880Sphkstatic void pty_clone __P((void *arg, char *name, int namelen, dev_t *dev));
80564880Sphk
80612675Sjulianstatic void
80764880Sphkpty_clone(arg, name, namelen, dev)
80864880Sphk	void *arg;
80964880Sphk	char *name;
81064880Sphk	int namelen;
81164880Sphk	dev_t *dev;
81264880Sphk{
81364880Sphk	int u;
81464880Sphk
81564880Sphk	if (*dev != NODEV)
81664880Sphk		return;
81764880Sphk	if (bcmp(name, "pty", 3) != 0)
81864880Sphk		return;
81964880Sphk	if (name[5] != '\0')
82064880Sphk		return;
82164880Sphk	switch (name[3]) {
82264880Sphk	case 'p': u =   0; break;
82364880Sphk	case 'q': u =  32; break;
82464880Sphk	case 'r': u =  64; break;
82564880Sphk	case 's': u =  96; break;
82664880Sphk	case 'P': u = 128; break;
82764880Sphk	case 'Q': u = 160; break;
82864880Sphk	case 'R': u = 192; break;
82964880Sphk	case 'S': u = 224; break;
83064880Sphk	default: return;
83164880Sphk	}
83264880Sphk	if (name[4] >= '0' && name[4] <= '9')
83364880Sphk		u += name[4] - '0';
83464880Sphk	else if (name[4] >= 'a' && name[4] <= 'v')
83564880Sphk		u += name[4] - 'a' + 10;
83664880Sphk	else
83764880Sphk		return;
83877176Sphk	*dev = make_dev(&ptc_cdevsw, u,
83977176Sphk	    UID_ROOT, GID_WHEEL, 0666, "pty%c%r", names[u / 32], u % 32);
84077176Sphk	(*dev)->si_flags |= SI_CHEAPCLONE;
84164880Sphk	return;
84264880Sphk}
84364880Sphk
84464880Sphkstatic void
84529506Sbdeptc_drvinit(unused)
84629506Sbde	void *unused;
84712517Sjulian{
84865374Sphk	EVENTHANDLER_REGISTER(dev_clone, pty_clone, 0, 1000);
84953483Sjkh	cdevsw_add(&pts_cdevsw);
85053483Sjkh	cdevsw_add(&ptc_cdevsw);
85112517Sjulian}
85212517Sjulian
85312517SjulianSYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)
854