pty.c revision 66067
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 66067 2000-09-19 10:28:44Z 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
5749536SphkMALLOC_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));
6264880Sphkstatic dev_t ptyinit __P((int n));
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,
8947625Sphk	/* flags */	D_TTY,
9047625Sphk	/* bmaj */	-1
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,
10747625Sphk	/* flags */	D_TTY,
10847625Sphk	/* bmaj */	-1
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
1291541Srgrimes/*
13049536Sphk * This function creates and initializes a pts/ptc pair
1311541Srgrimes *
13249536Sphk * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
13349536Sphk * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
13449536Sphk *
13549536Sphk * XXX: define and add mapping of upper minor bits to allow more
13649536Sphk *      than 256 ptys.
1371541Srgrimes */
13864880Sphkstatic dev_t
13949536Sphkptyinit(n)
1401541Srgrimes	int n;
1411541Srgrimes{
14249536Sphk	dev_t devs, devc;
14349536Sphk	char *names = "pqrsPQRS";
14449536Sphk	struct pt_ioctl *pt;
1451541Srgrimes
14649536Sphk	/* For now we only map the lower 8 bits of the minor */
14749536Sphk	if (n & ~0xff)
14864880Sphk		return (NODEV);
14949536Sphk
15050092Sjulian	pt = malloc(sizeof(*pt), M_PTY, M_WAITOK);
15150092Sjulian	bzero(pt, sizeof(*pt));
15250092Sjulian	pt->devs = devs = make_dev(&pts_cdevsw, n,
15366067Sphk	    UID_ROOT, GID_WHEEL, 0666, "tty%c%r", names[n / 32], n % 32);
15450092Sjulian	pt->devc = devc = make_dev(&ptc_cdevsw, n,
15566067Sphk	    UID_ROOT, GID_WHEEL, 0666, "pty%c%r", names[n / 32], n % 32);
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		ptyinit(minor(dev));
17649536Sphk	if (!dev->si_drv1)
17749536Sphk		return(ENXIO);
17857070Srwatson	pti = dev->si_drv1;
17950652Sphk	tp = dev->si_tty;
1801541Srgrimes	if ((tp->t_state & TS_ISOPEN) == 0) {
1811541Srgrimes		ttychars(tp);		/* Set up default chars */
1821541Srgrimes		tp->t_iflag = TTYDEF_IFLAG;
1831541Srgrimes		tp->t_oflag = TTYDEF_OFLAG;
1841541Srgrimes		tp->t_lflag = TTYDEF_LFLAG;
1851541Srgrimes		tp->t_cflag = TTYDEF_CFLAG;
1861541Srgrimes		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
18757070Srwatson	} else if (tp->t_state & TS_XCLUDE && suser(p)) {
1881541Srgrimes		return (EBUSY);
18957070Srwatson	} else if (pti->pt_prison != p->p_prison) {
19057070Srwatson		return (EBUSY);
19157070Srwatson	}
1921541Srgrimes	if (tp->t_oproc)			/* Ctrlr still around. */
1939824Sbde		(void)(*linesw[tp->t_line].l_modem)(tp, 1);
1941541Srgrimes	while ((tp->t_state & TS_CARR_ON) == 0) {
1951541Srgrimes		if (flag&FNONBLOCK)
1961541Srgrimes			break;
1979639Sbde		error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH,
1989624Sbde				 "ptsopn", 0);
1993308Sphk		if (error)
2001541Srgrimes			return (error);
2011541Srgrimes	}
2021541Srgrimes	error = (*linesw[tp->t_line].l_open)(dev, tp);
2037724Sache	if (error == 0)
2047724Sache		ptcwakeup(tp, FREAD|FWRITE);
2051541Srgrimes	return (error);
2061541Srgrimes}
2071541Srgrimes
20812675Sjulianstatic	int
2091541Srgrimesptsclose(dev, flag, mode, p)
2101541Srgrimes	dev_t dev;
2111541Srgrimes	int flag, mode;
2121541Srgrimes	struct proc *p;
2131541Srgrimes{
2141541Srgrimes	register struct tty *tp;
2151541Srgrimes	int err;
2161541Srgrimes
21750652Sphk	tp = dev->si_tty;
2181541Srgrimes	err = (*linesw[tp->t_line].l_close)(tp, flag);
2197730Sache	ptsstop(tp, FREAD|FWRITE);
2207724Sache	(void) ttyclose(tp);
2211541Srgrimes	return (err);
2221541Srgrimes}
2231541Srgrimes
22412675Sjulianstatic	int
2251541Srgrimesptsread(dev, uio, flag)
2261541Srgrimes	dev_t dev;
2271541Srgrimes	struct uio *uio;
2281541Srgrimes	int flag;
2291541Srgrimes{
2301541Srgrimes	struct proc *p = curproc;
23150652Sphk	register struct tty *tp = dev->si_tty;
23249536Sphk	register struct pt_ioctl *pti = dev->si_drv1;
2331541Srgrimes	int error = 0;
2341541Srgrimes
2351541Srgrimesagain:
2361541Srgrimes	if (pti->pt_flags & PF_REMOTE) {
2371541Srgrimes		while (isbackground(p, tp)) {
23851791Smarcel			if (SIGISMEMBER(p->p_sigignore, SIGTTIN) ||
23951791Smarcel			    SIGISMEMBER(p->p_sigmask, SIGTTIN) ||
24051791Smarcel			    p->p_pgrp->pg_jobc == 0 || p->p_flag & P_PPWAIT)
2411541Srgrimes				return (EIO);
2421541Srgrimes			pgsignal(p->p_pgrp, SIGTTIN, 1);
2439624Sbde			error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ptsbg",
2449624Sbde					 0);
2453308Sphk			if (error)
2461541Srgrimes				return (error);
2471541Srgrimes		}
2481541Srgrimes		if (tp->t_canq.c_cc == 0) {
2491541Srgrimes			if (flag & IO_NDELAY)
2501541Srgrimes				return (EWOULDBLOCK);
2519639Sbde			error = ttysleep(tp, TSA_PTS_READ(tp), TTIPRI | PCATCH,
2529624Sbde					 "ptsin", 0);
2533308Sphk			if (error)
2541541Srgrimes				return (error);
2551541Srgrimes			goto again;
2561541Srgrimes		}
2571541Srgrimes		while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
2581541Srgrimes			if (ureadc(getc(&tp->t_canq), uio) < 0) {
2591541Srgrimes				error = EFAULT;
2601541Srgrimes				break;
2611541Srgrimes			}
2621541Srgrimes		if (tp->t_canq.c_cc == 1)
2631541Srgrimes			(void) getc(&tp->t_canq);
2641541Srgrimes		if (tp->t_canq.c_cc)
2651541Srgrimes			return (error);
2661541Srgrimes	} else
2671541Srgrimes		if (tp->t_oproc)
2681541Srgrimes			error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
2691541Srgrimes	ptcwakeup(tp, FWRITE);
2701541Srgrimes	return (error);
2711541Srgrimes}
2721541Srgrimes
2731541Srgrimes/*
2741541Srgrimes * Write to pseudo-tty.
2751541Srgrimes * Wakeups of controlling tty will happen
2761541Srgrimes * indirectly, when tty driver calls ptsstart.
2771541Srgrimes */
27812675Sjulianstatic	int
2791541Srgrimesptswrite(dev, uio, flag)
2801541Srgrimes	dev_t dev;
2811541Srgrimes	struct uio *uio;
2821541Srgrimes	int flag;
2831541Srgrimes{
2841541Srgrimes	register struct tty *tp;
2851541Srgrimes
28650652Sphk	tp = dev->si_tty;
2871541Srgrimes	if (tp->t_oproc == 0)
2881541Srgrimes		return (EIO);
2891541Srgrimes	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
2901541Srgrimes}
2911541Srgrimes
2921541Srgrimes/*
2931541Srgrimes * Start output on pseudo-tty.
2941541Srgrimes * Wake up process selecting or sleeping for input from controlling tty.
2951541Srgrimes */
29612819Sphkstatic void
2971541Srgrimesptsstart(tp)
2981541Srgrimes	struct tty *tp;
2991541Srgrimes{
30049536Sphk	register struct pt_ioctl *pti = tp->t_dev->si_drv1;
3011541Srgrimes
3021541Srgrimes	if (tp->t_state & TS_TTSTOP)
3031541Srgrimes		return;
3041541Srgrimes	if (pti->pt_flags & PF_STOPPED) {
3051541Srgrimes		pti->pt_flags &= ~PF_STOPPED;
3061541Srgrimes		pti->pt_send = TIOCPKT_START;
3071541Srgrimes	}
3081541Srgrimes	ptcwakeup(tp, FREAD);
3091541Srgrimes}
3101541Srgrimes
31112819Sphkstatic void
3121541Srgrimesptcwakeup(tp, flag)
3131541Srgrimes	struct tty *tp;
3141541Srgrimes	int flag;
3151541Srgrimes{
31649536Sphk	struct pt_ioctl *pti = tp->t_dev->si_drv1;
3171541Srgrimes
3181541Srgrimes	if (flag & FREAD) {
3191541Srgrimes		selwakeup(&pti->pt_selr);
3209639Sbde		wakeup(TSA_PTC_READ(tp));
3211541Srgrimes	}
3221541Srgrimes	if (flag & FWRITE) {
3231541Srgrimes		selwakeup(&pti->pt_selw);
3249639Sbde		wakeup(TSA_PTC_WRITE(tp));
3251541Srgrimes	}
3261541Srgrimes}
3271541Srgrimes
32812675Sjulianstatic	int
3291541Srgrimesptcopen(dev, flag, devtype, p)
3301541Srgrimes	dev_t dev;
3311541Srgrimes	int flag, devtype;
3321541Srgrimes	struct proc *p;
3331541Srgrimes{
3341541Srgrimes	register struct tty *tp;
3351541Srgrimes	struct pt_ioctl *pti;
3361541Srgrimes
33749536Sphk	if (!dev->si_drv1)
33849536Sphk		ptyinit(minor(dev));
33949536Sphk	if (!dev->si_drv1)
34049536Sphk		return(ENXIO);
34150652Sphk	tp = dev->si_tty;
3421541Srgrimes	if (tp->t_oproc)
3431541Srgrimes		return (EIO);
34459818Sache	tp->t_timeout = -1;
3451541Srgrimes	tp->t_oproc = ptsstart;
34651654Sphk	tp->t_stop = ptsstop;
3471541Srgrimes	(void)(*linesw[tp->t_line].l_modem)(tp, 1);
3481541Srgrimes	tp->t_lflag &= ~EXTPROC;
34949536Sphk	pti = dev->si_drv1;
35057070Srwatson	pti->pt_prison = p->p_prison;
3511541Srgrimes	pti->pt_flags = 0;
3521541Srgrimes	pti->pt_send = 0;
3531541Srgrimes	pti->pt_ucntl = 0;
3541541Srgrimes	return (0);
3551541Srgrimes}
3561541Srgrimes
35712675Sjulianstatic	int
35810624Sbdeptcclose(dev, flags, fmt, p)
3591541Srgrimes	dev_t dev;
36010624Sbde	int flags;
36110624Sbde	int fmt;
36210624Sbde	struct proc *p;
3631541Srgrimes{
3641541Srgrimes	register struct tty *tp;
3651541Srgrimes
36650652Sphk	tp = dev->si_tty;
3671541Srgrimes	(void)(*linesw[tp->t_line].l_modem)(tp, 0);
3689824Sbde
3699824Sbde	/*
3709824Sbde	 * XXX MDMBUF makes no sense for ptys but would inhibit the above
3719824Sbde	 * l_modem().  CLOCAL makes sense but isn't supported.   Special
3729824Sbde	 * l_modem()s that ignore carrier drop make no sense for ptys but
3739824Sbde	 * may be in use because other parts of the line discipline make
3749824Sbde	 * sense for ptys.  Recover by doing everything that a normal
3759824Sbde	 * ttymodem() would have done except for sending a SIGHUP.
3769824Sbde	 */
3779850Sbde	if (tp->t_state & TS_ISOPEN) {
3789850Sbde		tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
3799850Sbde		tp->t_state |= TS_ZOMBIE;
3809850Sbde		ttyflush(tp, FREAD | FWRITE);
3819850Sbde	}
3829824Sbde
3831541Srgrimes	tp->t_oproc = 0;		/* mark closed */
3841541Srgrimes	return (0);
3851541Srgrimes}
3861541Srgrimes
38712675Sjulianstatic	int
3881541Srgrimesptcread(dev, uio, flag)
3891541Srgrimes	dev_t dev;
3901541Srgrimes	struct uio *uio;
3911541Srgrimes	int flag;
3921541Srgrimes{
39350652Sphk	register struct tty *tp = dev->si_tty;
39449536Sphk	struct pt_ioctl *pti = dev->si_drv1;
3951541Srgrimes	char buf[BUFSIZ];
3961541Srgrimes	int error = 0, cc;
3971541Srgrimes
3981541Srgrimes	/*
3991541Srgrimes	 * We want to block until the slave
4001541Srgrimes	 * is open, and there's something to read;
4011541Srgrimes	 * but if we lost the slave or we're NBIO,
4021541Srgrimes	 * then return the appropriate error instead.
4031541Srgrimes	 */
4041541Srgrimes	for (;;) {
4051541Srgrimes		if (tp->t_state&TS_ISOPEN) {
4061541Srgrimes			if (pti->pt_flags&PF_PKT && pti->pt_send) {
4071541Srgrimes				error = ureadc((int)pti->pt_send, uio);
4081541Srgrimes				if (error)
4091541Srgrimes					return (error);
4101541Srgrimes				if (pti->pt_send & TIOCPKT_IOCTL) {
4111541Srgrimes					cc = min(uio->uio_resid,
4121541Srgrimes						sizeof(tp->t_termios));
4132807Sbde					uiomove((caddr_t)&tp->t_termios, cc,
4142807Sbde						uio);
4151541Srgrimes				}
4161541Srgrimes				pti->pt_send = 0;
4171541Srgrimes				return (0);
4181541Srgrimes			}
4191541Srgrimes			if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
4201541Srgrimes				error = ureadc((int)pti->pt_ucntl, uio);
4211541Srgrimes				if (error)
4221541Srgrimes					return (error);
4231541Srgrimes				pti->pt_ucntl = 0;
4241541Srgrimes				return (0);
4251541Srgrimes			}
4261541Srgrimes			if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
4271541Srgrimes				break;
4281541Srgrimes		}
4299824Sbde		if ((tp->t_state & TS_CONNECTED) == 0)
4301541Srgrimes			return (0);	/* EOF */
4311541Srgrimes		if (flag & IO_NDELAY)
4321541Srgrimes			return (EWOULDBLOCK);
4339639Sbde		error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0);
4343308Sphk		if (error)
4351541Srgrimes			return (error);
4361541Srgrimes	}
4371541Srgrimes	if (pti->pt_flags & (PF_PKT|PF_UCNTL))
4381541Srgrimes		error = ureadc(0, uio);
4391541Srgrimes	while (uio->uio_resid > 0 && error == 0) {
4401541Srgrimes		cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
4411541Srgrimes		if (cc <= 0)
4421541Srgrimes			break;
4431541Srgrimes		error = uiomove(buf, cc, uio);
4441541Srgrimes	}
4459626Sbde	ttwwakeup(tp);
4461541Srgrimes	return (error);
4471541Srgrimes}
4481541Srgrimes
44912675Sjulianstatic	void
4501541Srgrimesptsstop(tp, flush)
4511541Srgrimes	register struct tty *tp;
4521541Srgrimes	int flush;
4531541Srgrimes{
45449536Sphk	struct pt_ioctl *pti = tp->t_dev->si_drv1;
4551541Srgrimes	int flag;
4561541Srgrimes
4571541Srgrimes	/* note: FLUSHREAD and FLUSHWRITE already ok */
4581541Srgrimes	if (flush == 0) {
4591541Srgrimes		flush = TIOCPKT_STOP;
4601541Srgrimes		pti->pt_flags |= PF_STOPPED;
4611541Srgrimes	} else
4621541Srgrimes		pti->pt_flags &= ~PF_STOPPED;
4631541Srgrimes	pti->pt_send |= flush;
4641541Srgrimes	/* change of perspective */
4651541Srgrimes	flag = 0;
4661541Srgrimes	if (flush & FREAD)
4671541Srgrimes		flag |= FWRITE;
4681541Srgrimes	if (flush & FWRITE)
4691541Srgrimes		flag |= FREAD;
4701541Srgrimes	ptcwakeup(tp, flag);
4711541Srgrimes}
4721541Srgrimes
47312675Sjulianstatic	int
47429354Speterptcpoll(dev, events, p)
4751541Srgrimes	dev_t dev;
47629354Speter	int events;
4771541Srgrimes	struct proc *p;
4781541Srgrimes{
47950652Sphk	register struct tty *tp = dev->si_tty;
48049536Sphk	struct pt_ioctl *pti = dev->si_drv1;
48129354Speter	int revents = 0;
4821541Srgrimes	int s;
4831541Srgrimes
4849824Sbde	if ((tp->t_state & TS_CONNECTED) == 0)
48529354Speter		return (seltrue(dev, events, p) | POLLHUP);
4861541Srgrimes
48729354Speter	/*
48829354Speter	 * Need to block timeouts (ttrstart).
48929354Speter	 */
49029354Speter	s = spltty();
4911541Srgrimes
49229354Speter	if (events & (POLLIN | POLLRDNORM))
49329354Speter		if ((tp->t_state & TS_ISOPEN) &&
49429354Speter		    ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
49529354Speter		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
49629354Speter		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
49729354Speter			revents |= events & (POLLIN | POLLRDNORM);
4981541Srgrimes
49929354Speter	if (events & (POLLOUT | POLLWRNORM))
50029354Speter		if (tp->t_state & TS_ISOPEN &&
50129354Speter		    ((pti->pt_flags & PF_REMOTE) ?
50229354Speter		     (tp->t_canq.c_cc == 0) :
50329354Speter		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
50429354Speter		      (tp->t_canq.c_cc == 0 && (tp->t_iflag & ICANON)))))
50529354Speter			revents |= events & (POLLOUT | POLLWRNORM);
5061541Srgrimes
50729354Speter	if (events & POLLHUP)
50829354Speter		if ((tp->t_state & TS_CARR_ON) == 0)
50929354Speter			revents |= POLLHUP;
5101541Srgrimes
51129354Speter	if (revents == 0) {
51229354Speter		if (events & (POLLIN | POLLRDNORM))
51329354Speter			selrecord(p, &pti->pt_selr);
51429354Speter
51529354Speter		if (events & (POLLOUT | POLLWRNORM))
51629354Speter			selrecord(p, &pti->pt_selw);
5171541Srgrimes	}
51829354Speter	splx(s);
51929354Speter
52029354Speter	return (revents);
5211541Srgrimes}
5221541Srgrimes
52312675Sjulianstatic	int
5241541Srgrimesptcwrite(dev, uio, flag)
5251541Srgrimes	dev_t dev;
5261541Srgrimes	register struct uio *uio;
5271541Srgrimes	int flag;
5281541Srgrimes{
52950652Sphk	register struct tty *tp = dev->si_tty;
5301549Srgrimes	register u_char *cp = 0;
5311541Srgrimes	register int cc = 0;
5321541Srgrimes	u_char locbuf[BUFSIZ];
5331541Srgrimes	int cnt = 0;
53449536Sphk	struct pt_ioctl *pti = dev->si_drv1;
5351541Srgrimes	int error = 0;
5361541Srgrimes
5371541Srgrimesagain:
5381541Srgrimes	if ((tp->t_state&TS_ISOPEN) == 0)
5391541Srgrimes		goto block;
5401541Srgrimes	if (pti->pt_flags & PF_REMOTE) {
5411541Srgrimes		if (tp->t_canq.c_cc)
5421541Srgrimes			goto block;
54311789Sbde		while ((uio->uio_resid > 0 || cc > 0) &&
54411789Sbde		       tp->t_canq.c_cc < TTYHOG - 1) {
5451541Srgrimes			if (cc == 0) {
5461541Srgrimes				cc = min(uio->uio_resid, BUFSIZ);
5471541Srgrimes				cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
5481541Srgrimes				cp = locbuf;
5491541Srgrimes				error = uiomove((caddr_t)cp, cc, uio);
5501541Srgrimes				if (error)
5511541Srgrimes					return (error);
5521541Srgrimes				/* check again for safety */
55311789Sbde				if ((tp->t_state & TS_ISOPEN) == 0) {
55411789Sbde					/* adjust as usual */
55511789Sbde					uio->uio_resid += cc;
5561541Srgrimes					return (EIO);
55711789Sbde				}
5581541Srgrimes			}
55915199Sbde			if (cc > 0) {
56015199Sbde				cc = b_to_q((char *)cp, cc, &tp->t_canq);
56115199Sbde				/*
56215199Sbde				 * XXX we don't guarantee that the canq size
56315199Sbde				 * is >= TTYHOG, so the above b_to_q() may
56415199Sbde				 * leave some bytes uncopied.  However, space
56515199Sbde				 * is guaranteed for the null terminator if
56615199Sbde				 * we don't fail here since (TTYHOG - 1) is
56715199Sbde				 * not a multiple of CBSIZE.
56815199Sbde				 */
56915199Sbde				if (cc > 0)
57015199Sbde					break;
57115199Sbde			}
5721541Srgrimes		}
57311789Sbde		/* adjust for data copied in but not written */
57411789Sbde		uio->uio_resid += cc;
5751541Srgrimes		(void) putc(0, &tp->t_canq);
5761541Srgrimes		ttwakeup(tp);
5779639Sbde		wakeup(TSA_PTS_READ(tp));
5781541Srgrimes		return (0);
5791541Srgrimes	}
58011789Sbde	while (uio->uio_resid > 0 || cc > 0) {
5811541Srgrimes		if (cc == 0) {
5821541Srgrimes			cc = min(uio->uio_resid, BUFSIZ);
5831541Srgrimes			cp = locbuf;
5841541Srgrimes			error = uiomove((caddr_t)cp, cc, uio);
5851541Srgrimes			if (error)
5861541Srgrimes				return (error);
5871541Srgrimes			/* check again for safety */
58811789Sbde			if ((tp->t_state & TS_ISOPEN) == 0) {
58911789Sbde				/* adjust for data copied in but not written */
59011789Sbde				uio->uio_resid += cc;
5911541Srgrimes				return (EIO);
59211789Sbde			}
5931541Srgrimes		}
5941541Srgrimes		while (cc > 0) {
5951541Srgrimes			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
5961541Srgrimes			   (tp->t_canq.c_cc > 0 || !(tp->t_iflag&ICANON))) {
5979824Sbde				wakeup(TSA_HUP_OR_INPUT(tp));
5981541Srgrimes				goto block;
5991541Srgrimes			}
6001541Srgrimes			(*linesw[tp->t_line].l_rint)(*cp++, tp);
6011541Srgrimes			cnt++;
6021541Srgrimes			cc--;
6031541Srgrimes		}
6041541Srgrimes		cc = 0;
6051541Srgrimes	}
6061541Srgrimes	return (0);
6071541Srgrimesblock:
6081541Srgrimes	/*
6091541Srgrimes	 * Come here to wait for slave to open, for space
61015199Sbde	 * in outq, or space in rawq, or an empty canq.
6111541Srgrimes	 */
61211789Sbde	if ((tp->t_state & TS_CONNECTED) == 0) {
61311789Sbde		/* adjust for data copied in but not written */
61411789Sbde		uio->uio_resid += cc;
6151541Srgrimes		return (EIO);
61611789Sbde	}
6171541Srgrimes	if (flag & IO_NDELAY) {
6181541Srgrimes		/* adjust for data copied in but not written */
6191541Srgrimes		uio->uio_resid += cc;
6201541Srgrimes		if (cnt == 0)
6211541Srgrimes			return (EWOULDBLOCK);
6221541Srgrimes		return (0);
6231541Srgrimes	}
6249639Sbde	error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0);
6253308Sphk	if (error) {
6261541Srgrimes		/* adjust for data copied in but not written */
6271541Srgrimes		uio->uio_resid += cc;
6281541Srgrimes		return (error);
6291541Srgrimes	}
6301541Srgrimes	goto again;
6311541Srgrimes}
6321541Srgrimes
6331541Srgrimes/*ARGSUSED*/
63412675Sjulianstatic	int
6351541Srgrimesptyioctl(dev, cmd, data, flag, p)
6361541Srgrimes	dev_t dev;
63736735Sdfr	u_long cmd;
6381541Srgrimes	caddr_t data;
6391541Srgrimes	int flag;
6401541Srgrimes	struct proc *p;
6411541Srgrimes{
64250652Sphk	register struct tty *tp = dev->si_tty;
64349536Sphk	register struct pt_ioctl *pti = dev->si_drv1;
6441541Srgrimes	register u_char *cc = tp->t_cc;
6451541Srgrimes	int stop, error;
6461541Srgrimes
64747301Sluoqi	if (devsw(dev)->d_open == ptcopen) {
6481541Srgrimes		switch (cmd) {
6491541Srgrimes
6501541Srgrimes		case TIOCGPGRP:
6511541Srgrimes			/*
65233826Sbde			 * We avoid calling ttioctl on the controller since,
6531541Srgrimes			 * in that case, tp must be the controlling terminal.
6541541Srgrimes			 */
6551541Srgrimes			*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
6561541Srgrimes			return (0);
6571541Srgrimes
6581541Srgrimes		case TIOCPKT:
6591541Srgrimes			if (*(int *)data) {
6601541Srgrimes				if (pti->pt_flags & PF_UCNTL)
6611541Srgrimes					return (EINVAL);
6621541Srgrimes				pti->pt_flags |= PF_PKT;
6631541Srgrimes			} else
6641541Srgrimes				pti->pt_flags &= ~PF_PKT;
6651541Srgrimes			return (0);
6661541Srgrimes
6671541Srgrimes		case TIOCUCNTL:
6681541Srgrimes			if (*(int *)data) {
6691541Srgrimes				if (pti->pt_flags & PF_PKT)
6701541Srgrimes					return (EINVAL);
6711541Srgrimes				pti->pt_flags |= PF_UCNTL;
6721541Srgrimes			} else
6731541Srgrimes				pti->pt_flags &= ~PF_UCNTL;
6741541Srgrimes			return (0);
6751541Srgrimes
6761541Srgrimes		case TIOCREMOTE:
6771541Srgrimes			if (*(int *)data)
6781541Srgrimes				pti->pt_flags |= PF_REMOTE;
6791541Srgrimes			else
6801541Srgrimes				pti->pt_flags &= ~PF_REMOTE;
6811541Srgrimes			ttyflush(tp, FREAD|FWRITE);
6821541Srgrimes			return (0);
68347203Sluoqi		}
6841541Srgrimes
68547203Sluoqi		/*
68647203Sluoqi		 * The rest of the ioctls shouldn't be called until
68747301Sluoqi		 * the slave is open.
68847203Sluoqi		 */
68947203Sluoqi		if ((tp->t_state & TS_ISOPEN) == 0)
69047301Sluoqi			return (EAGAIN);
69147203Sluoqi
69247203Sluoqi		switch (cmd) {
6931541Srgrimes#ifdef COMPAT_43
6948876Srgrimes		case TIOCSETP:
6951541Srgrimes		case TIOCSETN:
6961541Srgrimes#endif
6971541Srgrimes		case TIOCSETD:
6981541Srgrimes		case TIOCSETA:
6991541Srgrimes		case TIOCSETAW:
7009858Sache		case TIOCSETAF:
70147301Sluoqi			/*
70247301Sluoqi			 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
70347301Sluoqi			 * ttywflush(tp) will hang if there are characters in
70447301Sluoqi			 * the outq.
70547301Sluoqi			 */
7061541Srgrimes			ndflush(&tp->t_outq, tp->t_outq.c_cc);
7071541Srgrimes			break;
7081541Srgrimes
7091541Srgrimes		case TIOCSIG:
71027770Sjmg			if (*(unsigned int *)data >= NSIG ||
71127770Sjmg			    *(unsigned int *)data == 0)
7121541Srgrimes				return(EINVAL);
7131541Srgrimes			if ((tp->t_lflag&NOFLSH) == 0)
7141541Srgrimes				ttyflush(tp, FREAD|FWRITE);
7151541Srgrimes			pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
7161541Srgrimes			if ((*(unsigned int *)data == SIGINFO) &&
7171541Srgrimes			    ((tp->t_lflag&NOKERNINFO) == 0))
7181541Srgrimes				ttyinfo(tp);
7191541Srgrimes			return(0);
7201541Srgrimes		}
72147203Sluoqi	}
72247301Sluoqi	if (cmd == TIOCEXT) {
72347301Sluoqi		/*
72447301Sluoqi		 * When the EXTPROC bit is being toggled, we need
72547301Sluoqi		 * to send an TIOCPKT_IOCTL if the packet driver
72647301Sluoqi		 * is turned on.
72747301Sluoqi		 */
72847301Sluoqi		if (*(int *)data) {
72947301Sluoqi			if (pti->pt_flags & PF_PKT) {
73047301Sluoqi				pti->pt_send |= TIOCPKT_IOCTL;
73147301Sluoqi				ptcwakeup(tp, FREAD);
73247301Sluoqi			}
73347301Sluoqi			tp->t_lflag |= EXTPROC;
73447301Sluoqi		} else {
73547301Sluoqi			if ((tp->t_lflag & EXTPROC) &&
73647301Sluoqi			    (pti->pt_flags & PF_PKT)) {
73747301Sluoqi				pti->pt_send |= TIOCPKT_IOCTL;
73847301Sluoqi				ptcwakeup(tp, FREAD);
73947301Sluoqi			}
74047301Sluoqi			tp->t_lflag &= ~EXTPROC;
74147301Sluoqi		}
74247301Sluoqi		return(0);
74347301Sluoqi	}
7441541Srgrimes	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
74531577Sbde	if (error == ENOIOCTL)
7461541Srgrimes		 error = ttioctl(tp, cmd, data, flag);
74731577Sbde	if (error == ENOIOCTL) {
7481541Srgrimes		if (pti->pt_flags & PF_UCNTL &&
7491541Srgrimes		    (cmd & ~0xff) == UIOCCMD(0)) {
7501541Srgrimes			if (cmd & 0xff) {
7511541Srgrimes				pti->pt_ucntl = (u_char)cmd;
7521541Srgrimes				ptcwakeup(tp, FREAD);
7531541Srgrimes			}
7541541Srgrimes			return (0);
7551541Srgrimes		}
7561541Srgrimes		error = ENOTTY;
7571541Srgrimes	}
7581541Srgrimes	/*
7591541Srgrimes	 * If external processing and packet mode send ioctl packet.
7601541Srgrimes	 */
7611541Srgrimes	if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
7621541Srgrimes		switch(cmd) {
7631541Srgrimes		case TIOCSETA:
7641541Srgrimes		case TIOCSETAW:
7651541Srgrimes		case TIOCSETAF:
7661541Srgrimes#ifdef COMPAT_43
7671541Srgrimes		case TIOCSETP:
7681541Srgrimes		case TIOCSETN:
7691541Srgrimes#endif
7701541Srgrimes#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
7711541Srgrimes		case TIOCSETC:
7721541Srgrimes		case TIOCSLTC:
7731541Srgrimes		case TIOCLBIS:
7741541Srgrimes		case TIOCLBIC:
7751541Srgrimes		case TIOCLSET:
7761541Srgrimes#endif
7771541Srgrimes			pti->pt_send |= TIOCPKT_IOCTL;
7781541Srgrimes			ptcwakeup(tp, FREAD);
7791541Srgrimes		default:
7801541Srgrimes			break;
7811541Srgrimes		}
7821541Srgrimes	}
7838876Srgrimes	stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
7841541Srgrimes		&& CCEQ(cc[VSTART], CTRL('q'));
7851541Srgrimes	if (pti->pt_flags & PF_NOSTOP) {
7861541Srgrimes		if (stop) {
7871541Srgrimes			pti->pt_send &= ~TIOCPKT_NOSTOP;
7881541Srgrimes			pti->pt_send |= TIOCPKT_DOSTOP;
7891541Srgrimes			pti->pt_flags &= ~PF_NOSTOP;
7901541Srgrimes			ptcwakeup(tp, FREAD);
7911541Srgrimes		}
7921541Srgrimes	} else {
7931541Srgrimes		if (!stop) {
7941541Srgrimes			pti->pt_send &= ~TIOCPKT_DOSTOP;
7951541Srgrimes			pti->pt_send |= TIOCPKT_NOSTOP;
7961541Srgrimes			pti->pt_flags |= PF_NOSTOP;
7971541Srgrimes			ptcwakeup(tp, FREAD);
7981541Srgrimes		}
7991541Srgrimes	}
8001541Srgrimes	return (error);
8011541Srgrimes}
80212517Sjulian
80312517Sjulian
80429506Sbdestatic void ptc_drvinit __P((void *unused));
80549536Sphk
80664880Sphkstatic void pty_clone __P((void *arg, char *name, int namelen, dev_t *dev));
80764880Sphk
80812675Sjulianstatic void
80964880Sphkpty_clone(arg, name, namelen, dev)
81064880Sphk	void *arg;
81164880Sphk	char *name;
81264880Sphk	int namelen;
81364880Sphk	dev_t *dev;
81464880Sphk{
81564880Sphk	int u;
81664880Sphk
81764880Sphk	if (*dev != NODEV)
81864880Sphk		return;
81964880Sphk	if (bcmp(name, "pty", 3) != 0)
82064880Sphk		return;
82164880Sphk	if (name[5] != '\0')
82264880Sphk		return;
82364880Sphk	switch (name[3]) {
82464880Sphk	case 'p': u =   0; break;
82564880Sphk	case 'q': u =  32; break;
82664880Sphk	case 'r': u =  64; break;
82764880Sphk	case 's': u =  96; break;
82864880Sphk	case 'P': u = 128; break;
82964880Sphk	case 'Q': u = 160; break;
83064880Sphk	case 'R': u = 192; break;
83164880Sphk	case 'S': u = 224; break;
83264880Sphk	default: return;
83364880Sphk	}
83464880Sphk	if (name[4] >= '0' && name[4] <= '9')
83564880Sphk		u += name[4] - '0';
83664880Sphk	else if (name[4] >= 'a' && name[4] <= 'v')
83764880Sphk		u += name[4] - 'a' + 10;
83864880Sphk	else
83964880Sphk		return;
84064880Sphk	*dev = ptyinit(u);
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