pty.c revision 83803
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 83803 2001-09-21 22:22:25Z jhb $
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	n = minor(devc);
14749536Sphk	/* For now we only map the lower 8 bits of the minor */
14849536Sphk	if (n & ~0xff)
14964880Sphk		return (NODEV);
15049536Sphk
15178405Sbrian	devc->si_flags &= ~SI_CHEAPCLONE;
15278405Sbrian
15369781Sdwmalone	pt = malloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO);
15450092Sjulian	pt->devs = devs = make_dev(&pts_cdevsw, n,
15566067Sphk	    UID_ROOT, GID_WHEEL, 0666, "tty%c%r", names[n / 32], n % 32);
15677176Sphk	pt->devc = devc;
15749536Sphk
15849536Sphk	devs->si_drv1 = devc->si_drv1 = pt;
15950652Sphk	devs->si_tty = devc->si_tty = &pt->pt_tty;
16077360Sphk	pt->pt_tty.t_dev = devs;
16149540Sphk	ttyregister(&pt->pt_tty);
16264880Sphk	return (devc);
16316322Sgpalmer}
1641541Srgrimes
1651541Srgrimes/*ARGSUSED*/
16612675Sjulianstatic	int
16783366Sjulianptsopen(dev, flag, devtype, td)
1681541Srgrimes	dev_t dev;
1691541Srgrimes	int flag, devtype;
17083366Sjulian	struct thread *td;
1711541Srgrimes{
17283366Sjulian	struct proc *p = td->td_proc;
1731541Srgrimes	register struct tty *tp;
1741541Srgrimes	int error;
17557070Srwatson	struct pt_ioctl *pti;
1761541Srgrimes
17749536Sphk	if (!dev->si_drv1)
17849536Sphk		return(ENXIO);
17957070Srwatson	pti = dev->si_drv1;
18050652Sphk	tp = dev->si_tty;
1811541Srgrimes	if ((tp->t_state & TS_ISOPEN) == 0) {
1821541Srgrimes		ttychars(tp);		/* Set up default chars */
1831541Srgrimes		tp->t_iflag = TTYDEF_IFLAG;
1841541Srgrimes		tp->t_oflag = TTYDEF_OFLAG;
1851541Srgrimes		tp->t_lflag = TTYDEF_LFLAG;
1861541Srgrimes		tp->t_cflag = TTYDEF_CFLAG;
1871541Srgrimes		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
18857070Srwatson	} else if (tp->t_state & TS_XCLUDE && suser(p)) {
1891541Srgrimes		return (EBUSY);
19072786Srwatson	} else if (pti->pt_prison != p->p_ucred->cr_prison) {
19157070Srwatson		return (EBUSY);
19257070Srwatson	}
1931541Srgrimes	if (tp->t_oproc)			/* Ctrlr still around. */
1949824Sbde		(void)(*linesw[tp->t_line].l_modem)(tp, 1);
1951541Srgrimes	while ((tp->t_state & TS_CARR_ON) == 0) {
1961541Srgrimes		if (flag&FNONBLOCK)
1971541Srgrimes			break;
1989639Sbde		error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH,
1999624Sbde				 "ptsopn", 0);
2003308Sphk		if (error)
2011541Srgrimes			return (error);
2021541Srgrimes	}
2031541Srgrimes	error = (*linesw[tp->t_line].l_open)(dev, tp);
2047724Sache	if (error == 0)
2057724Sache		ptcwakeup(tp, FREAD|FWRITE);
2061541Srgrimes	return (error);
2071541Srgrimes}
2081541Srgrimes
20912675Sjulianstatic	int
21083366Sjulianptsclose(dev, flag, mode, td)
2111541Srgrimes	dev_t dev;
2121541Srgrimes	int flag, mode;
21383366Sjulian	struct thread *td;
2141541Srgrimes{
2151541Srgrimes	register struct tty *tp;
2161541Srgrimes	int err;
2171541Srgrimes
21850652Sphk	tp = dev->si_tty;
2191541Srgrimes	err = (*linesw[tp->t_line].l_close)(tp, flag);
2207730Sache	ptsstop(tp, FREAD|FWRITE);
2217724Sache	(void) ttyclose(tp);
2221541Srgrimes	return (err);
2231541Srgrimes}
2241541Srgrimes
22512675Sjulianstatic	int
2261541Srgrimesptsread(dev, uio, flag)
2271541Srgrimes	dev_t dev;
2281541Srgrimes	struct uio *uio;
2291541Srgrimes	int flag;
2301541Srgrimes{
23183366Sjulian	struct thread *td = curthread;
23283366Sjulian	struct proc *p = td->td_proc;
23350652Sphk	register struct tty *tp = dev->si_tty;
23449536Sphk	register struct pt_ioctl *pti = dev->si_drv1;
2351541Srgrimes	int error = 0;
2361541Srgrimes
2371541Srgrimesagain:
2381541Srgrimes	if (pti->pt_flags & PF_REMOTE) {
2391541Srgrimes		while (isbackground(p, tp)) {
24051791Smarcel			if (SIGISMEMBER(p->p_sigignore, SIGTTIN) ||
24151791Smarcel			    SIGISMEMBER(p->p_sigmask, SIGTTIN) ||
24251791Smarcel			    p->p_pgrp->pg_jobc == 0 || p->p_flag & P_PPWAIT)
2431541Srgrimes				return (EIO);
2441541Srgrimes			pgsignal(p->p_pgrp, SIGTTIN, 1);
2459624Sbde			error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ptsbg",
2469624Sbde					 0);
2473308Sphk			if (error)
2481541Srgrimes				return (error);
2491541Srgrimes		}
2501541Srgrimes		if (tp->t_canq.c_cc == 0) {
2511541Srgrimes			if (flag & IO_NDELAY)
2521541Srgrimes				return (EWOULDBLOCK);
2539639Sbde			error = ttysleep(tp, TSA_PTS_READ(tp), TTIPRI | PCATCH,
2549624Sbde					 "ptsin", 0);
2553308Sphk			if (error)
2561541Srgrimes				return (error);
2571541Srgrimes			goto again;
2581541Srgrimes		}
2591541Srgrimes		while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
2601541Srgrimes			if (ureadc(getc(&tp->t_canq), uio) < 0) {
2611541Srgrimes				error = EFAULT;
2621541Srgrimes				break;
2631541Srgrimes			}
2641541Srgrimes		if (tp->t_canq.c_cc == 1)
2651541Srgrimes			(void) getc(&tp->t_canq);
2661541Srgrimes		if (tp->t_canq.c_cc)
2671541Srgrimes			return (error);
2681541Srgrimes	} else
2691541Srgrimes		if (tp->t_oproc)
2701541Srgrimes			error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
2711541Srgrimes	ptcwakeup(tp, FWRITE);
2721541Srgrimes	return (error);
2731541Srgrimes}
2741541Srgrimes
2751541Srgrimes/*
2761541Srgrimes * Write to pseudo-tty.
2771541Srgrimes * Wakeups of controlling tty will happen
2781541Srgrimes * indirectly, when tty driver calls ptsstart.
2791541Srgrimes */
28012675Sjulianstatic	int
2811541Srgrimesptswrite(dev, uio, flag)
2821541Srgrimes	dev_t dev;
2831541Srgrimes	struct uio *uio;
2841541Srgrimes	int flag;
2851541Srgrimes{
2861541Srgrimes	register struct tty *tp;
2871541Srgrimes
28850652Sphk	tp = dev->si_tty;
2891541Srgrimes	if (tp->t_oproc == 0)
2901541Srgrimes		return (EIO);
2911541Srgrimes	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
2921541Srgrimes}
2931541Srgrimes
2941541Srgrimes/*
2951541Srgrimes * Start output on pseudo-tty.
2961541Srgrimes * Wake up process selecting or sleeping for input from controlling tty.
2971541Srgrimes */
29812819Sphkstatic void
2991541Srgrimesptsstart(tp)
3001541Srgrimes	struct tty *tp;
3011541Srgrimes{
30249536Sphk	register struct pt_ioctl *pti = tp->t_dev->si_drv1;
3031541Srgrimes
3041541Srgrimes	if (tp->t_state & TS_TTSTOP)
3051541Srgrimes		return;
3061541Srgrimes	if (pti->pt_flags & PF_STOPPED) {
3071541Srgrimes		pti->pt_flags &= ~PF_STOPPED;
3081541Srgrimes		pti->pt_send = TIOCPKT_START;
3091541Srgrimes	}
3101541Srgrimes	ptcwakeup(tp, FREAD);
3111541Srgrimes}
3121541Srgrimes
31312819Sphkstatic void
3141541Srgrimesptcwakeup(tp, flag)
3151541Srgrimes	struct tty *tp;
3161541Srgrimes	int flag;
3171541Srgrimes{
31849536Sphk	struct pt_ioctl *pti = tp->t_dev->si_drv1;
3191541Srgrimes
3201541Srgrimes	if (flag & FREAD) {
3211541Srgrimes		selwakeup(&pti->pt_selr);
3229639Sbde		wakeup(TSA_PTC_READ(tp));
3231541Srgrimes	}
3241541Srgrimes	if (flag & FWRITE) {
3251541Srgrimes		selwakeup(&pti->pt_selw);
3269639Sbde		wakeup(TSA_PTC_WRITE(tp));
3271541Srgrimes	}
3281541Srgrimes}
3291541Srgrimes
33012675Sjulianstatic	int
33183366Sjulianptcopen(dev, flag, devtype, td)
3321541Srgrimes	dev_t dev;
3331541Srgrimes	int flag, devtype;
33483366Sjulian	struct thread *td;
3351541Srgrimes{
33683366Sjulian	struct proc *p = td->td_proc;
3371541Srgrimes	register struct tty *tp;
3381541Srgrimes	struct pt_ioctl *pti;
3391541Srgrimes
34049536Sphk	if (!dev->si_drv1)
34177176Sphk		ptyinit(dev);
34249536Sphk	if (!dev->si_drv1)
34349536Sphk		return(ENXIO);
34450652Sphk	tp = dev->si_tty;
3451541Srgrimes	if (tp->t_oproc)
3461541Srgrimes		return (EIO);
34759818Sache	tp->t_timeout = -1;
3481541Srgrimes	tp->t_oproc = ptsstart;
34951654Sphk	tp->t_stop = ptsstop;
3501541Srgrimes	(void)(*linesw[tp->t_line].l_modem)(tp, 1);
3511541Srgrimes	tp->t_lflag &= ~EXTPROC;
35249536Sphk	pti = dev->si_drv1;
35372786Srwatson	pti->pt_prison = p->p_ucred->cr_prison;
3541541Srgrimes	pti->pt_flags = 0;
3551541Srgrimes	pti->pt_send = 0;
3561541Srgrimes	pti->pt_ucntl = 0;
3571541Srgrimes	return (0);
3581541Srgrimes}
3591541Srgrimes
36012675Sjulianstatic	int
36183366Sjulianptcclose(dev, flags, fmt, td)
3621541Srgrimes	dev_t dev;
36310624Sbde	int flags;
36410624Sbde	int fmt;
36583366Sjulian	struct thread *td;
3661541Srgrimes{
3671541Srgrimes	register struct tty *tp;
3681541Srgrimes
36950652Sphk	tp = dev->si_tty;
3701541Srgrimes	(void)(*linesw[tp->t_line].l_modem)(tp, 0);
3719824Sbde
3729824Sbde	/*
3739824Sbde	 * XXX MDMBUF makes no sense for ptys but would inhibit the above
3749824Sbde	 * l_modem().  CLOCAL makes sense but isn't supported.   Special
3759824Sbde	 * l_modem()s that ignore carrier drop make no sense for ptys but
3769824Sbde	 * may be in use because other parts of the line discipline make
3779824Sbde	 * sense for ptys.  Recover by doing everything that a normal
3789824Sbde	 * ttymodem() would have done except for sending a SIGHUP.
3799824Sbde	 */
3809850Sbde	if (tp->t_state & TS_ISOPEN) {
3819850Sbde		tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
3829850Sbde		tp->t_state |= TS_ZOMBIE;
3839850Sbde		ttyflush(tp, FREAD | FWRITE);
3849850Sbde	}
3859824Sbde
3861541Srgrimes	tp->t_oproc = 0;		/* mark closed */
3871541Srgrimes	return (0);
3881541Srgrimes}
3891541Srgrimes
39012675Sjulianstatic	int
3911541Srgrimesptcread(dev, uio, flag)
3921541Srgrimes	dev_t dev;
3931541Srgrimes	struct uio *uio;
3941541Srgrimes	int flag;
3951541Srgrimes{
39650652Sphk	register struct tty *tp = dev->si_tty;
39749536Sphk	struct pt_ioctl *pti = dev->si_drv1;
3981541Srgrimes	char buf[BUFSIZ];
3991541Srgrimes	int error = 0, cc;
4001541Srgrimes
4011541Srgrimes	/*
4021541Srgrimes	 * We want to block until the slave
4031541Srgrimes	 * is open, and there's something to read;
4041541Srgrimes	 * but if we lost the slave or we're NBIO,
4051541Srgrimes	 * then return the appropriate error instead.
4061541Srgrimes	 */
4071541Srgrimes	for (;;) {
4081541Srgrimes		if (tp->t_state&TS_ISOPEN) {
4091541Srgrimes			if (pti->pt_flags&PF_PKT && pti->pt_send) {
4101541Srgrimes				error = ureadc((int)pti->pt_send, uio);
4111541Srgrimes				if (error)
4121541Srgrimes					return (error);
4131541Srgrimes				if (pti->pt_send & TIOCPKT_IOCTL) {
4141541Srgrimes					cc = min(uio->uio_resid,
4151541Srgrimes						sizeof(tp->t_termios));
4162807Sbde					uiomove((caddr_t)&tp->t_termios, cc,
4172807Sbde						uio);
4181541Srgrimes				}
4191541Srgrimes				pti->pt_send = 0;
4201541Srgrimes				return (0);
4211541Srgrimes			}
4221541Srgrimes			if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
4231541Srgrimes				error = ureadc((int)pti->pt_ucntl, uio);
4241541Srgrimes				if (error)
4251541Srgrimes					return (error);
4261541Srgrimes				pti->pt_ucntl = 0;
4271541Srgrimes				return (0);
4281541Srgrimes			}
4291541Srgrimes			if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
4301541Srgrimes				break;
4311541Srgrimes		}
4329824Sbde		if ((tp->t_state & TS_CONNECTED) == 0)
4331541Srgrimes			return (0);	/* EOF */
4341541Srgrimes		if (flag & IO_NDELAY)
4351541Srgrimes			return (EWOULDBLOCK);
4369639Sbde		error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0);
4373308Sphk		if (error)
4381541Srgrimes			return (error);
4391541Srgrimes	}
4401541Srgrimes	if (pti->pt_flags & (PF_PKT|PF_UCNTL))
4411541Srgrimes		error = ureadc(0, uio);
4421541Srgrimes	while (uio->uio_resid > 0 && error == 0) {
4431541Srgrimes		cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
4441541Srgrimes		if (cc <= 0)
4451541Srgrimes			break;
4461541Srgrimes		error = uiomove(buf, cc, uio);
4471541Srgrimes	}
4489626Sbde	ttwwakeup(tp);
4491541Srgrimes	return (error);
4501541Srgrimes}
4511541Srgrimes
45212675Sjulianstatic	void
4531541Srgrimesptsstop(tp, flush)
4541541Srgrimes	register struct tty *tp;
4551541Srgrimes	int flush;
4561541Srgrimes{
45749536Sphk	struct pt_ioctl *pti = tp->t_dev->si_drv1;
4581541Srgrimes	int flag;
4591541Srgrimes
4601541Srgrimes	/* note: FLUSHREAD and FLUSHWRITE already ok */
4611541Srgrimes	if (flush == 0) {
4621541Srgrimes		flush = TIOCPKT_STOP;
4631541Srgrimes		pti->pt_flags |= PF_STOPPED;
4641541Srgrimes	} else
4651541Srgrimes		pti->pt_flags &= ~PF_STOPPED;
4661541Srgrimes	pti->pt_send |= flush;
4671541Srgrimes	/* change of perspective */
4681541Srgrimes	flag = 0;
4691541Srgrimes	if (flush & FREAD)
4701541Srgrimes		flag |= FWRITE;
4711541Srgrimes	if (flush & FWRITE)
4721541Srgrimes		flag |= FREAD;
4731541Srgrimes	ptcwakeup(tp, flag);
4741541Srgrimes}
4751541Srgrimes
47612675Sjulianstatic	int
47783366Sjulianptcpoll(dev, events, td)
4781541Srgrimes	dev_t dev;
47929354Speter	int events;
48083366Sjulian	struct thread *td;
4811541Srgrimes{
48250652Sphk	register struct tty *tp = dev->si_tty;
48349536Sphk	struct pt_ioctl *pti = dev->si_drv1;
48429354Speter	int revents = 0;
4851541Srgrimes	int s;
4861541Srgrimes
4879824Sbde	if ((tp->t_state & TS_CONNECTED) == 0)
48883366Sjulian		return (seltrue(dev, events, td) | POLLHUP);
4891541Srgrimes
49029354Speter	/*
49129354Speter	 * Need to block timeouts (ttrstart).
49229354Speter	 */
49329354Speter	s = spltty();
4941541Srgrimes
49529354Speter	if (events & (POLLIN | POLLRDNORM))
49629354Speter		if ((tp->t_state & TS_ISOPEN) &&
49729354Speter		    ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
49829354Speter		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
49929354Speter		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
50029354Speter			revents |= events & (POLLIN | POLLRDNORM);
5011541Srgrimes
50229354Speter	if (events & (POLLOUT | POLLWRNORM))
50329354Speter		if (tp->t_state & TS_ISOPEN &&
50429354Speter		    ((pti->pt_flags & PF_REMOTE) ?
50529354Speter		     (tp->t_canq.c_cc == 0) :
50629354Speter		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
50729354Speter		      (tp->t_canq.c_cc == 0 && (tp->t_iflag & ICANON)))))
50829354Speter			revents |= events & (POLLOUT | POLLWRNORM);
5091541Srgrimes
51029354Speter	if (events & POLLHUP)
51129354Speter		if ((tp->t_state & TS_CARR_ON) == 0)
51229354Speter			revents |= POLLHUP;
5131541Srgrimes
51429354Speter	if (revents == 0) {
51529354Speter		if (events & (POLLIN | POLLRDNORM))
51683803Sjhb			selrecord(td, &pti->pt_selr);
51729354Speter
51829354Speter		if (events & (POLLOUT | POLLWRNORM))
51983803Sjhb			selrecord(td, &pti->pt_selw);
5201541Srgrimes	}
52129354Speter	splx(s);
52229354Speter
52329354Speter	return (revents);
5241541Srgrimes}
5251541Srgrimes
52612675Sjulianstatic	int
5271541Srgrimesptcwrite(dev, uio, flag)
5281541Srgrimes	dev_t dev;
5291541Srgrimes	register struct uio *uio;
5301541Srgrimes	int flag;
5311541Srgrimes{
53250652Sphk	register struct tty *tp = dev->si_tty;
5331549Srgrimes	register u_char *cp = 0;
5341541Srgrimes	register int cc = 0;
5351541Srgrimes	u_char locbuf[BUFSIZ];
5361541Srgrimes	int cnt = 0;
53749536Sphk	struct pt_ioctl *pti = dev->si_drv1;
5381541Srgrimes	int error = 0;
5391541Srgrimes
5401541Srgrimesagain:
5411541Srgrimes	if ((tp->t_state&TS_ISOPEN) == 0)
5421541Srgrimes		goto block;
5431541Srgrimes	if (pti->pt_flags & PF_REMOTE) {
5441541Srgrimes		if (tp->t_canq.c_cc)
5451541Srgrimes			goto block;
54611789Sbde		while ((uio->uio_resid > 0 || cc > 0) &&
54711789Sbde		       tp->t_canq.c_cc < TTYHOG - 1) {
5481541Srgrimes			if (cc == 0) {
5491541Srgrimes				cc = min(uio->uio_resid, BUFSIZ);
5501541Srgrimes				cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
5511541Srgrimes				cp = locbuf;
5521541Srgrimes				error = uiomove((caddr_t)cp, cc, uio);
5531541Srgrimes				if (error)
5541541Srgrimes					return (error);
5551541Srgrimes				/* check again for safety */
55611789Sbde				if ((tp->t_state & TS_ISOPEN) == 0) {
55711789Sbde					/* adjust as usual */
55811789Sbde					uio->uio_resid += cc;
5591541Srgrimes					return (EIO);
56011789Sbde				}
5611541Srgrimes			}
56215199Sbde			if (cc > 0) {
56315199Sbde				cc = b_to_q((char *)cp, cc, &tp->t_canq);
56415199Sbde				/*
56515199Sbde				 * XXX we don't guarantee that the canq size
56615199Sbde				 * is >= TTYHOG, so the above b_to_q() may
56715199Sbde				 * leave some bytes uncopied.  However, space
56815199Sbde				 * is guaranteed for the null terminator if
56915199Sbde				 * we don't fail here since (TTYHOG - 1) is
57015199Sbde				 * not a multiple of CBSIZE.
57115199Sbde				 */
57215199Sbde				if (cc > 0)
57315199Sbde					break;
57415199Sbde			}
5751541Srgrimes		}
57611789Sbde		/* adjust for data copied in but not written */
57711789Sbde		uio->uio_resid += cc;
5781541Srgrimes		(void) putc(0, &tp->t_canq);
5791541Srgrimes		ttwakeup(tp);
5809639Sbde		wakeup(TSA_PTS_READ(tp));
5811541Srgrimes		return (0);
5821541Srgrimes	}
58311789Sbde	while (uio->uio_resid > 0 || cc > 0) {
5841541Srgrimes		if (cc == 0) {
5851541Srgrimes			cc = min(uio->uio_resid, BUFSIZ);
5861541Srgrimes			cp = locbuf;
5871541Srgrimes			error = uiomove((caddr_t)cp, cc, uio);
5881541Srgrimes			if (error)
5891541Srgrimes				return (error);
5901541Srgrimes			/* check again for safety */
59111789Sbde			if ((tp->t_state & TS_ISOPEN) == 0) {
59211789Sbde				/* adjust for data copied in but not written */
59311789Sbde				uio->uio_resid += cc;
5941541Srgrimes				return (EIO);
59511789Sbde			}
5961541Srgrimes		}
5971541Srgrimes		while (cc > 0) {
5981541Srgrimes			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
5991541Srgrimes			   (tp->t_canq.c_cc > 0 || !(tp->t_iflag&ICANON))) {
6009824Sbde				wakeup(TSA_HUP_OR_INPUT(tp));
6011541Srgrimes				goto block;
6021541Srgrimes			}
6031541Srgrimes			(*linesw[tp->t_line].l_rint)(*cp++, tp);
6041541Srgrimes			cnt++;
6051541Srgrimes			cc--;
6061541Srgrimes		}
6071541Srgrimes		cc = 0;
6081541Srgrimes	}
6091541Srgrimes	return (0);
6101541Srgrimesblock:
6111541Srgrimes	/*
6121541Srgrimes	 * Come here to wait for slave to open, for space
61315199Sbde	 * in outq, or space in rawq, or an empty canq.
6141541Srgrimes	 */
61511789Sbde	if ((tp->t_state & TS_CONNECTED) == 0) {
61611789Sbde		/* adjust for data copied in but not written */
61711789Sbde		uio->uio_resid += cc;
6181541Srgrimes		return (EIO);
61911789Sbde	}
6201541Srgrimes	if (flag & IO_NDELAY) {
6211541Srgrimes		/* adjust for data copied in but not written */
6221541Srgrimes		uio->uio_resid += cc;
6231541Srgrimes		if (cnt == 0)
6241541Srgrimes			return (EWOULDBLOCK);
6251541Srgrimes		return (0);
6261541Srgrimes	}
6279639Sbde	error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0);
6283308Sphk	if (error) {
6291541Srgrimes		/* adjust for data copied in but not written */
6301541Srgrimes		uio->uio_resid += cc;
6311541Srgrimes		return (error);
6321541Srgrimes	}
6331541Srgrimes	goto again;
6341541Srgrimes}
6351541Srgrimes
6361541Srgrimes/*ARGSUSED*/
63712675Sjulianstatic	int
63883366Sjulianptyioctl(dev, cmd, data, flag, td)
6391541Srgrimes	dev_t dev;
64036735Sdfr	u_long cmd;
6411541Srgrimes	caddr_t data;
6421541Srgrimes	int flag;
64383366Sjulian	struct thread *td;
6441541Srgrimes{
64550652Sphk	register struct tty *tp = dev->si_tty;
64649536Sphk	register struct pt_ioctl *pti = dev->si_drv1;
6471541Srgrimes	register u_char *cc = tp->t_cc;
6481541Srgrimes	int stop, error;
6491541Srgrimes
65047301Sluoqi	if (devsw(dev)->d_open == ptcopen) {
6511541Srgrimes		switch (cmd) {
6521541Srgrimes
6531541Srgrimes		case TIOCGPGRP:
6541541Srgrimes			/*
65533826Sbde			 * We avoid calling ttioctl on the controller since,
6561541Srgrimes			 * in that case, tp must be the controlling terminal.
6571541Srgrimes			 */
6581541Srgrimes			*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
6591541Srgrimes			return (0);
6601541Srgrimes
6611541Srgrimes		case TIOCPKT:
6621541Srgrimes			if (*(int *)data) {
6631541Srgrimes				if (pti->pt_flags & PF_UCNTL)
6641541Srgrimes					return (EINVAL);
6651541Srgrimes				pti->pt_flags |= PF_PKT;
6661541Srgrimes			} else
6671541Srgrimes				pti->pt_flags &= ~PF_PKT;
6681541Srgrimes			return (0);
6691541Srgrimes
6701541Srgrimes		case TIOCUCNTL:
6711541Srgrimes			if (*(int *)data) {
6721541Srgrimes				if (pti->pt_flags & PF_PKT)
6731541Srgrimes					return (EINVAL);
6741541Srgrimes				pti->pt_flags |= PF_UCNTL;
6751541Srgrimes			} else
6761541Srgrimes				pti->pt_flags &= ~PF_UCNTL;
6771541Srgrimes			return (0);
6781541Srgrimes
6791541Srgrimes		case TIOCREMOTE:
6801541Srgrimes			if (*(int *)data)
6811541Srgrimes				pti->pt_flags |= PF_REMOTE;
6821541Srgrimes			else
6831541Srgrimes				pti->pt_flags &= ~PF_REMOTE;
6841541Srgrimes			ttyflush(tp, FREAD|FWRITE);
6851541Srgrimes			return (0);
68647203Sluoqi		}
6871541Srgrimes
68847203Sluoqi		/*
68947203Sluoqi		 * The rest of the ioctls shouldn't be called until
69047301Sluoqi		 * the slave is open.
69147203Sluoqi		 */
69247203Sluoqi		if ((tp->t_state & TS_ISOPEN) == 0)
69347301Sluoqi			return (EAGAIN);
69447203Sluoqi
69547203Sluoqi		switch (cmd) {
6961541Srgrimes#ifdef COMPAT_43
6978876Srgrimes		case TIOCSETP:
6981541Srgrimes		case TIOCSETN:
6991541Srgrimes#endif
7001541Srgrimes		case TIOCSETD:
7011541Srgrimes		case TIOCSETA:
7021541Srgrimes		case TIOCSETAW:
7039858Sache		case TIOCSETAF:
70447301Sluoqi			/*
70547301Sluoqi			 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
70647301Sluoqi			 * ttywflush(tp) will hang if there are characters in
70747301Sluoqi			 * the outq.
70847301Sluoqi			 */
7091541Srgrimes			ndflush(&tp->t_outq, tp->t_outq.c_cc);
7101541Srgrimes			break;
7111541Srgrimes
7121541Srgrimes		case TIOCSIG:
71327770Sjmg			if (*(unsigned int *)data >= NSIG ||
71427770Sjmg			    *(unsigned int *)data == 0)
7151541Srgrimes				return(EINVAL);
7161541Srgrimes			if ((tp->t_lflag&NOFLSH) == 0)
7171541Srgrimes				ttyflush(tp, FREAD|FWRITE);
7181541Srgrimes			pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
7191541Srgrimes			if ((*(unsigned int *)data == SIGINFO) &&
7201541Srgrimes			    ((tp->t_lflag&NOKERNINFO) == 0))
7211541Srgrimes				ttyinfo(tp);
7221541Srgrimes			return(0);
7231541Srgrimes		}
72447203Sluoqi	}
72547301Sluoqi	if (cmd == TIOCEXT) {
72647301Sluoqi		/*
72747301Sluoqi		 * When the EXTPROC bit is being toggled, we need
72847301Sluoqi		 * to send an TIOCPKT_IOCTL if the packet driver
72947301Sluoqi		 * is turned on.
73047301Sluoqi		 */
73147301Sluoqi		if (*(int *)data) {
73247301Sluoqi			if (pti->pt_flags & PF_PKT) {
73347301Sluoqi				pti->pt_send |= TIOCPKT_IOCTL;
73447301Sluoqi				ptcwakeup(tp, FREAD);
73547301Sluoqi			}
73647301Sluoqi			tp->t_lflag |= EXTPROC;
73747301Sluoqi		} else {
73847301Sluoqi			if ((tp->t_lflag & EXTPROC) &&
73947301Sluoqi			    (pti->pt_flags & PF_PKT)) {
74047301Sluoqi				pti->pt_send |= TIOCPKT_IOCTL;
74147301Sluoqi				ptcwakeup(tp, FREAD);
74247301Sluoqi			}
74347301Sluoqi			tp->t_lflag &= ~EXTPROC;
74447301Sluoqi		}
74547301Sluoqi		return(0);
74647301Sluoqi	}
74783366Sjulian	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, td);
74831577Sbde	if (error == ENOIOCTL)
7491541Srgrimes		 error = ttioctl(tp, cmd, data, flag);
75031577Sbde	if (error == ENOIOCTL) {
7511541Srgrimes		if (pti->pt_flags & PF_UCNTL &&
7521541Srgrimes		    (cmd & ~0xff) == UIOCCMD(0)) {
7531541Srgrimes			if (cmd & 0xff) {
7541541Srgrimes				pti->pt_ucntl = (u_char)cmd;
7551541Srgrimes				ptcwakeup(tp, FREAD);
7561541Srgrimes			}
7571541Srgrimes			return (0);
7581541Srgrimes		}
7591541Srgrimes		error = ENOTTY;
7601541Srgrimes	}
7611541Srgrimes	/*
7621541Srgrimes	 * If external processing and packet mode send ioctl packet.
7631541Srgrimes	 */
7641541Srgrimes	if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
7651541Srgrimes		switch(cmd) {
7661541Srgrimes		case TIOCSETA:
7671541Srgrimes		case TIOCSETAW:
7681541Srgrimes		case TIOCSETAF:
7691541Srgrimes#ifdef COMPAT_43
7701541Srgrimes		case TIOCSETP:
7711541Srgrimes		case TIOCSETN:
7721541Srgrimes#endif
7731541Srgrimes#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
7741541Srgrimes		case TIOCSETC:
7751541Srgrimes		case TIOCSLTC:
7761541Srgrimes		case TIOCLBIS:
7771541Srgrimes		case TIOCLBIC:
7781541Srgrimes		case TIOCLSET:
7791541Srgrimes#endif
7801541Srgrimes			pti->pt_send |= TIOCPKT_IOCTL;
7811541Srgrimes			ptcwakeup(tp, FREAD);
7821541Srgrimes		default:
7831541Srgrimes			break;
7841541Srgrimes		}
7851541Srgrimes	}
7868876Srgrimes	stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
7871541Srgrimes		&& CCEQ(cc[VSTART], CTRL('q'));
7881541Srgrimes	if (pti->pt_flags & PF_NOSTOP) {
7891541Srgrimes		if (stop) {
7901541Srgrimes			pti->pt_send &= ~TIOCPKT_NOSTOP;
7911541Srgrimes			pti->pt_send |= TIOCPKT_DOSTOP;
7921541Srgrimes			pti->pt_flags &= ~PF_NOSTOP;
7931541Srgrimes			ptcwakeup(tp, FREAD);
7941541Srgrimes		}
7951541Srgrimes	} else {
7961541Srgrimes		if (!stop) {
7971541Srgrimes			pti->pt_send &= ~TIOCPKT_DOSTOP;
7981541Srgrimes			pti->pt_send |= TIOCPKT_NOSTOP;
7991541Srgrimes			pti->pt_flags |= PF_NOSTOP;
8001541Srgrimes			ptcwakeup(tp, FREAD);
8011541Srgrimes		}
8021541Srgrimes	}
8031541Srgrimes	return (error);
8041541Srgrimes}
80512517Sjulian
80612517Sjulian
80729506Sbdestatic void ptc_drvinit __P((void *unused));
80849536Sphk
80964880Sphkstatic void pty_clone __P((void *arg, char *name, int namelen, dev_t *dev));
81064880Sphk
81112675Sjulianstatic void
81264880Sphkpty_clone(arg, name, namelen, dev)
81364880Sphk	void *arg;
81464880Sphk	char *name;
81564880Sphk	int namelen;
81664880Sphk	dev_t *dev;
81764880Sphk{
81864880Sphk	int u;
81964880Sphk
82064880Sphk	if (*dev != NODEV)
82164880Sphk		return;
82264880Sphk	if (bcmp(name, "pty", 3) != 0)
82364880Sphk		return;
82464880Sphk	if (name[5] != '\0')
82564880Sphk		return;
82664880Sphk	switch (name[3]) {
82764880Sphk	case 'p': u =   0; break;
82864880Sphk	case 'q': u =  32; break;
82964880Sphk	case 'r': u =  64; break;
83064880Sphk	case 's': u =  96; break;
83164880Sphk	case 'P': u = 128; break;
83264880Sphk	case 'Q': u = 160; break;
83364880Sphk	case 'R': u = 192; break;
83464880Sphk	case 'S': u = 224; break;
83564880Sphk	default: return;
83664880Sphk	}
83764880Sphk	if (name[4] >= '0' && name[4] <= '9')
83864880Sphk		u += name[4] - '0';
83964880Sphk	else if (name[4] >= 'a' && name[4] <= 'v')
84064880Sphk		u += name[4] - 'a' + 10;
84164880Sphk	else
84264880Sphk		return;
84377176Sphk	*dev = make_dev(&ptc_cdevsw, u,
84477176Sphk	    UID_ROOT, GID_WHEEL, 0666, "pty%c%r", names[u / 32], u % 32);
84577176Sphk	(*dev)->si_flags |= SI_CHEAPCLONE;
84664880Sphk	return;
84764880Sphk}
84864880Sphk
84964880Sphkstatic void
85029506Sbdeptc_drvinit(unused)
85129506Sbde	void *unused;
85212517Sjulian{
85365374Sphk	EVENTHANDLER_REGISTER(dev_clone, pty_clone, 0, 1000);
85453483Sjkh	cdevsw_add(&pts_cdevsw);
85553483Sjkh	cdevsw_add(&ptc_cdevsw);
85612517Sjulian}
85712517Sjulian
85812517SjulianSYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)
859