pty.c revision 92723
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 92723 2002-03-19 21:25:46Z alfred $
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>
4491140Stanimura#include <sys/lock.h>
4591140Stanimura#include <sys/mutex.h>
4691140Stanimura#include <sys/sx.h>
4724207Sbde#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
4824207Sbde#include <sys/ioctl_compat.h>
4924207Sbde#endif
501541Srgrimes#include <sys/proc.h>
511541Srgrimes#include <sys/tty.h>
521541Srgrimes#include <sys/conf.h>
5324131Sbde#include <sys/fcntl.h>
5429354Speter#include <sys/poll.h>
551541Srgrimes#include <sys/kernel.h>
561541Srgrimes#include <sys/vnode.h>
573308Sphk#include <sys/signalvar.h>
5849536Sphk#include <sys/malloc.h>
591541Srgrimes
6069774Sphkstatic MALLOC_DEFINE(M_PTY, "ptys", "pty data structures");
6112517Sjulian
6292723Salfredstatic void ptsstart(struct tty *tp);
6392723Salfredstatic void ptsstop(struct tty *tp, int rw);
6492723Salfredstatic void ptcwakeup(struct tty *tp, int flag);
6592723Salfredstatic dev_t ptyinit(dev_t cdev);
6611789Sbde
6712675Sjulianstatic	d_open_t	ptsopen;
6812675Sjulianstatic	d_close_t	ptsclose;
6912675Sjulianstatic	d_read_t	ptsread;
7012675Sjulianstatic	d_write_t	ptswrite;
7112675Sjulianstatic	d_ioctl_t	ptyioctl;
7212675Sjulianstatic	d_open_t	ptcopen;
7312675Sjulianstatic	d_close_t	ptcclose;
7412675Sjulianstatic	d_read_t	ptcread;
7512675Sjulianstatic	d_write_t	ptcwrite;
7629354Speterstatic	d_poll_t	ptcpoll;
7712675Sjulian
7838485Sbde#define	CDEV_MAJOR_S	5
7947625Sphkstatic struct cdevsw pts_cdevsw = {
8047625Sphk	/* open */	ptsopen,
8147625Sphk	/* close */	ptsclose,
8247625Sphk	/* read */	ptsread,
8347625Sphk	/* write */	ptswrite,
8447625Sphk	/* ioctl */	ptyioctl,
8551654Sphk	/* poll */	ttypoll,
8647625Sphk	/* mmap */	nommap,
8747625Sphk	/* strategy */	nostrategy,
8847625Sphk	/* name */	"pts",
8947625Sphk	/* maj */	CDEV_MAJOR_S,
9047625Sphk	/* dump */	nodump,
9147625Sphk	/* psize */	nopsize,
9272521Sjlemon	/* flags */	D_TTY | D_KQFILTER,
9372521Sjlemon	/* kqfilter */	ttykqfilter,
9438485Sbde};
9512675Sjulian
9638485Sbde#define	CDEV_MAJOR_C	6
9747625Sphkstatic struct cdevsw ptc_cdevsw = {
9847625Sphk	/* open */	ptcopen,
9947625Sphk	/* close */	ptcclose,
10047625Sphk	/* read */	ptcread,
10147625Sphk	/* write */	ptcwrite,
10247625Sphk	/* ioctl */	ptyioctl,
10347625Sphk	/* poll */	ptcpoll,
10447625Sphk	/* mmap */	nommap,
10547625Sphk	/* strategy */	nostrategy,
10647625Sphk	/* name */	"ptc",
10747625Sphk	/* maj */	CDEV_MAJOR_C,
10847625Sphk	/* dump */	nodump,
10947625Sphk	/* psize */	nopsize,
11072521Sjlemon	/* flags */	D_TTY | D_KQFILTER,
11172521Sjlemon	/* kqfilter */	ttykqfilter,
11238485Sbde};
11312675Sjulian
1141541Srgrimes#define BUFSIZ 100		/* Chunk size iomoved to/from user */
1151541Srgrimes
11649536Sphkstruct	pt_ioctl {
1171541Srgrimes	int	pt_flags;
1181541Srgrimes	struct	selinfo pt_selr, pt_selw;
1191541Srgrimes	u_char	pt_send;
1201541Srgrimes	u_char	pt_ucntl;
12149536Sphk	struct tty pt_tty;
12250092Sjulian	dev_t	devs, devc;
12357070Srwatson	struct	prison *pt_prison;
12449536Sphk};
1251541Srgrimes
1261541Srgrimes#define	PF_PKT		0x08		/* packet mode */
1271541Srgrimes#define	PF_STOPPED	0x10		/* user told stopped */
1281541Srgrimes#define	PF_REMOTE	0x20		/* remote and flow controlled input */
1291541Srgrimes#define	PF_NOSTOP	0x40
1301541Srgrimes#define PF_UCNTL	0x80		/* user control mode */
1311541Srgrimes
13277176Sphkstatic char *names = "pqrsPQRS";
1331541Srgrimes/*
13449536Sphk * This function creates and initializes a pts/ptc pair
1351541Srgrimes *
13649536Sphk * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
13749536Sphk * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
13849536Sphk *
13949536Sphk * XXX: define and add mapping of upper minor bits to allow more
14049536Sphk *      than 256 ptys.
1411541Srgrimes */
14264880Sphkstatic dev_t
14377176Sphkptyinit(dev_t devc)
1441541Srgrimes{
14577176Sphk	dev_t devs;
14649536Sphk	struct pt_ioctl *pt;
14777176Sphk	int n;
1481541Srgrimes
14977176Sphk	n = minor(devc);
15049536Sphk	/* For now we only map the lower 8 bits of the minor */
15149536Sphk	if (n & ~0xff)
15264880Sphk		return (NODEV);
15349536Sphk
15478405Sbrian	devc->si_flags &= ~SI_CHEAPCLONE;
15578405Sbrian
15669781Sdwmalone	pt = malloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO);
15750092Sjulian	pt->devs = devs = make_dev(&pts_cdevsw, n,
15866067Sphk	    UID_ROOT, GID_WHEEL, 0666, "tty%c%r", names[n / 32], n % 32);
15977176Sphk	pt->devc = devc;
16049536Sphk
16149536Sphk	devs->si_drv1 = devc->si_drv1 = pt;
16250652Sphk	devs->si_tty = devc->si_tty = &pt->pt_tty;
16377360Sphk	pt->pt_tty.t_dev = devs;
16449540Sphk	ttyregister(&pt->pt_tty);
16564880Sphk	return (devc);
16616322Sgpalmer}
1671541Srgrimes
1681541Srgrimes/*ARGSUSED*/
16912675Sjulianstatic	int
17083366Sjulianptsopen(dev, flag, devtype, td)
1711541Srgrimes	dev_t dev;
1721541Srgrimes	int flag, devtype;
17383366Sjulian	struct thread *td;
1741541Srgrimes{
1751541Srgrimes	register struct tty *tp;
1761541Srgrimes	int error;
17757070Srwatson	struct pt_ioctl *pti;
1781541Srgrimes
17949536Sphk	if (!dev->si_drv1)
18049536Sphk		return(ENXIO);
18157070Srwatson	pti = dev->si_drv1;
18250652Sphk	tp = dev->si_tty;
1831541Srgrimes	if ((tp->t_state & TS_ISOPEN) == 0) {
1841541Srgrimes		ttychars(tp);		/* Set up default chars */
1851541Srgrimes		tp->t_iflag = TTYDEF_IFLAG;
1861541Srgrimes		tp->t_oflag = TTYDEF_OFLAG;
1871541Srgrimes		tp->t_lflag = TTYDEF_LFLAG;
1881541Srgrimes		tp->t_cflag = TTYDEF_CFLAG;
1891541Srgrimes		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
19091406Sjhb	} else if (tp->t_state & TS_XCLUDE && suser_xxx(td->td_ucred, NULL, 0)) {
1911541Srgrimes		return (EBUSY);
19291406Sjhb	} else if (pti->pt_prison != td->td_ucred->cr_prison) {
19357070Srwatson		return (EBUSY);
19457070Srwatson	}
1951541Srgrimes	if (tp->t_oproc)			/* Ctrlr still around. */
1969824Sbde		(void)(*linesw[tp->t_line].l_modem)(tp, 1);
1971541Srgrimes	while ((tp->t_state & TS_CARR_ON) == 0) {
1981541Srgrimes		if (flag&FNONBLOCK)
1991541Srgrimes			break;
2009639Sbde		error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH,
2019624Sbde				 "ptsopn", 0);
2023308Sphk		if (error)
2031541Srgrimes			return (error);
2041541Srgrimes	}
2051541Srgrimes	error = (*linesw[tp->t_line].l_open)(dev, tp);
2067724Sache	if (error == 0)
2077724Sache		ptcwakeup(tp, FREAD|FWRITE);
2081541Srgrimes	return (error);
2091541Srgrimes}
2101541Srgrimes
21112675Sjulianstatic	int
21283366Sjulianptsclose(dev, flag, mode, td)
2131541Srgrimes	dev_t dev;
2141541Srgrimes	int flag, mode;
21583366Sjulian	struct thread *td;
2161541Srgrimes{
2171541Srgrimes	register struct tty *tp;
2181541Srgrimes	int err;
2191541Srgrimes
22050652Sphk	tp = dev->si_tty;
2211541Srgrimes	err = (*linesw[tp->t_line].l_close)(tp, flag);
2227730Sache	ptsstop(tp, FREAD|FWRITE);
2237724Sache	(void) ttyclose(tp);
2241541Srgrimes	return (err);
2251541Srgrimes}
2261541Srgrimes
22712675Sjulianstatic	int
2281541Srgrimesptsread(dev, uio, flag)
2291541Srgrimes	dev_t dev;
2301541Srgrimes	struct uio *uio;
2311541Srgrimes	int flag;
2321541Srgrimes{
23383366Sjulian	struct thread *td = curthread;
23483366Sjulian	struct proc *p = td->td_proc;
23550652Sphk	register struct tty *tp = dev->si_tty;
23649536Sphk	register struct pt_ioctl *pti = dev->si_drv1;
2371541Srgrimes	int error = 0;
2381541Srgrimes
2391541Srgrimesagain:
2401541Srgrimes	if (pti->pt_flags & PF_REMOTE) {
2411541Srgrimes		while (isbackground(p, tp)) {
24291140Stanimura			PGRPSESS_SLOCK();
24391140Stanimura			PROC_LOCK(p);
24451791Smarcel			if (SIGISMEMBER(p->p_sigignore, SIGTTIN) ||
24551791Smarcel			    SIGISMEMBER(p->p_sigmask, SIGTTIN) ||
24691140Stanimura			    p->p_pgrp->pg_jobc == 0 || p->p_flag & P_PPWAIT) {
24791140Stanimura				PROC_UNLOCK(p);
24891565Stanimura				PGRPSESS_SUNLOCK();
2491541Srgrimes				return (EIO);
25091140Stanimura			}
25191140Stanimura			PROC_UNLOCK(p);
25291140Stanimura			PGRP_LOCK(p->p_pgrp);
25391140Stanimura			PGRPSESS_SUNLOCK();
2541541Srgrimes			pgsignal(p->p_pgrp, SIGTTIN, 1);
25591140Stanimura			PGRP_UNLOCK(p->p_pgrp);
2569624Sbde			error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ptsbg",
2579624Sbde					 0);
2583308Sphk			if (error)
2591541Srgrimes				return (error);
2601541Srgrimes		}
2611541Srgrimes		if (tp->t_canq.c_cc == 0) {
2621541Srgrimes			if (flag & IO_NDELAY)
2631541Srgrimes				return (EWOULDBLOCK);
2649639Sbde			error = ttysleep(tp, TSA_PTS_READ(tp), TTIPRI | PCATCH,
2659624Sbde					 "ptsin", 0);
2663308Sphk			if (error)
2671541Srgrimes				return (error);
2681541Srgrimes			goto again;
2691541Srgrimes		}
2701541Srgrimes		while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
2711541Srgrimes			if (ureadc(getc(&tp->t_canq), uio) < 0) {
2721541Srgrimes				error = EFAULT;
2731541Srgrimes				break;
2741541Srgrimes			}
2751541Srgrimes		if (tp->t_canq.c_cc == 1)
2761541Srgrimes			(void) getc(&tp->t_canq);
2771541Srgrimes		if (tp->t_canq.c_cc)
2781541Srgrimes			return (error);
2791541Srgrimes	} else
2801541Srgrimes		if (tp->t_oproc)
2811541Srgrimes			error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
2821541Srgrimes	ptcwakeup(tp, FWRITE);
2831541Srgrimes	return (error);
2841541Srgrimes}
2851541Srgrimes
2861541Srgrimes/*
2871541Srgrimes * Write to pseudo-tty.
2881541Srgrimes * Wakeups of controlling tty will happen
2891541Srgrimes * indirectly, when tty driver calls ptsstart.
2901541Srgrimes */
29112675Sjulianstatic	int
2921541Srgrimesptswrite(dev, uio, flag)
2931541Srgrimes	dev_t dev;
2941541Srgrimes	struct uio *uio;
2951541Srgrimes	int flag;
2961541Srgrimes{
2971541Srgrimes	register struct tty *tp;
2981541Srgrimes
29950652Sphk	tp = dev->si_tty;
3001541Srgrimes	if (tp->t_oproc == 0)
3011541Srgrimes		return (EIO);
3021541Srgrimes	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
3031541Srgrimes}
3041541Srgrimes
3051541Srgrimes/*
3061541Srgrimes * Start output on pseudo-tty.
3071541Srgrimes * Wake up process selecting or sleeping for input from controlling tty.
3081541Srgrimes */
30912819Sphkstatic void
3101541Srgrimesptsstart(tp)
3111541Srgrimes	struct tty *tp;
3121541Srgrimes{
31349536Sphk	register struct pt_ioctl *pti = tp->t_dev->si_drv1;
3141541Srgrimes
3151541Srgrimes	if (tp->t_state & TS_TTSTOP)
3161541Srgrimes		return;
3171541Srgrimes	if (pti->pt_flags & PF_STOPPED) {
3181541Srgrimes		pti->pt_flags &= ~PF_STOPPED;
3191541Srgrimes		pti->pt_send = TIOCPKT_START;
3201541Srgrimes	}
3211541Srgrimes	ptcwakeup(tp, FREAD);
3221541Srgrimes}
3231541Srgrimes
32412819Sphkstatic void
3251541Srgrimesptcwakeup(tp, flag)
3261541Srgrimes	struct tty *tp;
3271541Srgrimes	int flag;
3281541Srgrimes{
32949536Sphk	struct pt_ioctl *pti = tp->t_dev->si_drv1;
3301541Srgrimes
3311541Srgrimes	if (flag & FREAD) {
3321541Srgrimes		selwakeup(&pti->pt_selr);
3339639Sbde		wakeup(TSA_PTC_READ(tp));
3341541Srgrimes	}
3351541Srgrimes	if (flag & FWRITE) {
3361541Srgrimes		selwakeup(&pti->pt_selw);
3379639Sbde		wakeup(TSA_PTC_WRITE(tp));
3381541Srgrimes	}
3391541Srgrimes}
3401541Srgrimes
34112675Sjulianstatic	int
34283366Sjulianptcopen(dev, flag, devtype, td)
3431541Srgrimes	dev_t dev;
3441541Srgrimes	int flag, devtype;
34583366Sjulian	struct thread *td;
3461541Srgrimes{
3471541Srgrimes	register struct tty *tp;
3481541Srgrimes	struct pt_ioctl *pti;
3491541Srgrimes
35049536Sphk	if (!dev->si_drv1)
35177176Sphk		ptyinit(dev);
35249536Sphk	if (!dev->si_drv1)
35349536Sphk		return(ENXIO);
35450652Sphk	tp = dev->si_tty;
3551541Srgrimes	if (tp->t_oproc)
3561541Srgrimes		return (EIO);
35759818Sache	tp->t_timeout = -1;
3581541Srgrimes	tp->t_oproc = ptsstart;
35951654Sphk	tp->t_stop = ptsstop;
3601541Srgrimes	(void)(*linesw[tp->t_line].l_modem)(tp, 1);
3611541Srgrimes	tp->t_lflag &= ~EXTPROC;
36249536Sphk	pti = dev->si_drv1;
36391406Sjhb	pti->pt_prison = td->td_ucred->cr_prison;
3641541Srgrimes	pti->pt_flags = 0;
3651541Srgrimes	pti->pt_send = 0;
3661541Srgrimes	pti->pt_ucntl = 0;
3671541Srgrimes	return (0);
3681541Srgrimes}
3691541Srgrimes
37012675Sjulianstatic	int
37183366Sjulianptcclose(dev, flags, fmt, td)
3721541Srgrimes	dev_t dev;
37310624Sbde	int flags;
37410624Sbde	int fmt;
37583366Sjulian	struct thread *td;
3761541Srgrimes{
3771541Srgrimes	register struct tty *tp;
3781541Srgrimes
37950652Sphk	tp = dev->si_tty;
3801541Srgrimes	(void)(*linesw[tp->t_line].l_modem)(tp, 0);
3819824Sbde
3829824Sbde	/*
3839824Sbde	 * XXX MDMBUF makes no sense for ptys but would inhibit the above
3849824Sbde	 * l_modem().  CLOCAL makes sense but isn't supported.   Special
3859824Sbde	 * l_modem()s that ignore carrier drop make no sense for ptys but
3869824Sbde	 * may be in use because other parts of the line discipline make
3879824Sbde	 * sense for ptys.  Recover by doing everything that a normal
3889824Sbde	 * ttymodem() would have done except for sending a SIGHUP.
3899824Sbde	 */
3909850Sbde	if (tp->t_state & TS_ISOPEN) {
3919850Sbde		tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
3929850Sbde		tp->t_state |= TS_ZOMBIE;
3939850Sbde		ttyflush(tp, FREAD | FWRITE);
3949850Sbde	}
3959824Sbde
3961541Srgrimes	tp->t_oproc = 0;		/* mark closed */
3971541Srgrimes	return (0);
3981541Srgrimes}
3991541Srgrimes
40012675Sjulianstatic	int
4011541Srgrimesptcread(dev, uio, flag)
4021541Srgrimes	dev_t dev;
4031541Srgrimes	struct uio *uio;
4041541Srgrimes	int flag;
4051541Srgrimes{
40650652Sphk	register struct tty *tp = dev->si_tty;
40749536Sphk	struct pt_ioctl *pti = dev->si_drv1;
4081541Srgrimes	char buf[BUFSIZ];
4091541Srgrimes	int error = 0, cc;
4101541Srgrimes
4111541Srgrimes	/*
4121541Srgrimes	 * We want to block until the slave
4131541Srgrimes	 * is open, and there's something to read;
4141541Srgrimes	 * but if we lost the slave or we're NBIO,
4151541Srgrimes	 * then return the appropriate error instead.
4161541Srgrimes	 */
4171541Srgrimes	for (;;) {
4181541Srgrimes		if (tp->t_state&TS_ISOPEN) {
4191541Srgrimes			if (pti->pt_flags&PF_PKT && pti->pt_send) {
4201541Srgrimes				error = ureadc((int)pti->pt_send, uio);
4211541Srgrimes				if (error)
4221541Srgrimes					return (error);
4231541Srgrimes				if (pti->pt_send & TIOCPKT_IOCTL) {
4241541Srgrimes					cc = min(uio->uio_resid,
4251541Srgrimes						sizeof(tp->t_termios));
4262807Sbde					uiomove((caddr_t)&tp->t_termios, cc,
4272807Sbde						uio);
4281541Srgrimes				}
4291541Srgrimes				pti->pt_send = 0;
4301541Srgrimes				return (0);
4311541Srgrimes			}
4321541Srgrimes			if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
4331541Srgrimes				error = ureadc((int)pti->pt_ucntl, uio);
4341541Srgrimes				if (error)
4351541Srgrimes					return (error);
4361541Srgrimes				pti->pt_ucntl = 0;
4371541Srgrimes				return (0);
4381541Srgrimes			}
4391541Srgrimes			if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
4401541Srgrimes				break;
4411541Srgrimes		}
4429824Sbde		if ((tp->t_state & TS_CONNECTED) == 0)
4431541Srgrimes			return (0);	/* EOF */
4441541Srgrimes		if (flag & IO_NDELAY)
4451541Srgrimes			return (EWOULDBLOCK);
4469639Sbde		error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0);
4473308Sphk		if (error)
4481541Srgrimes			return (error);
4491541Srgrimes	}
4501541Srgrimes	if (pti->pt_flags & (PF_PKT|PF_UCNTL))
4511541Srgrimes		error = ureadc(0, uio);
4521541Srgrimes	while (uio->uio_resid > 0 && error == 0) {
4531541Srgrimes		cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
4541541Srgrimes		if (cc <= 0)
4551541Srgrimes			break;
4561541Srgrimes		error = uiomove(buf, cc, uio);
4571541Srgrimes	}
4589626Sbde	ttwwakeup(tp);
4591541Srgrimes	return (error);
4601541Srgrimes}
4611541Srgrimes
46212675Sjulianstatic	void
4631541Srgrimesptsstop(tp, flush)
4641541Srgrimes	register struct tty *tp;
4651541Srgrimes	int flush;
4661541Srgrimes{
46749536Sphk	struct pt_ioctl *pti = tp->t_dev->si_drv1;
4681541Srgrimes	int flag;
4691541Srgrimes
4701541Srgrimes	/* note: FLUSHREAD and FLUSHWRITE already ok */
4711541Srgrimes	if (flush == 0) {
4721541Srgrimes		flush = TIOCPKT_STOP;
4731541Srgrimes		pti->pt_flags |= PF_STOPPED;
4741541Srgrimes	} else
4751541Srgrimes		pti->pt_flags &= ~PF_STOPPED;
4761541Srgrimes	pti->pt_send |= flush;
4771541Srgrimes	/* change of perspective */
4781541Srgrimes	flag = 0;
4791541Srgrimes	if (flush & FREAD)
4801541Srgrimes		flag |= FWRITE;
4811541Srgrimes	if (flush & FWRITE)
4821541Srgrimes		flag |= FREAD;
4831541Srgrimes	ptcwakeup(tp, flag);
4841541Srgrimes}
4851541Srgrimes
48612675Sjulianstatic	int
48783366Sjulianptcpoll(dev, events, td)
4881541Srgrimes	dev_t dev;
48929354Speter	int events;
49083366Sjulian	struct thread *td;
4911541Srgrimes{
49250652Sphk	register struct tty *tp = dev->si_tty;
49349536Sphk	struct pt_ioctl *pti = dev->si_drv1;
49429354Speter	int revents = 0;
4951541Srgrimes	int s;
4961541Srgrimes
4979824Sbde	if ((tp->t_state & TS_CONNECTED) == 0)
49883366Sjulian		return (seltrue(dev, events, td) | POLLHUP);
4991541Srgrimes
50029354Speter	/*
50129354Speter	 * Need to block timeouts (ttrstart).
50229354Speter	 */
50329354Speter	s = spltty();
5041541Srgrimes
50529354Speter	if (events & (POLLIN | POLLRDNORM))
50629354Speter		if ((tp->t_state & TS_ISOPEN) &&
50729354Speter		    ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
50829354Speter		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
50929354Speter		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
51029354Speter			revents |= events & (POLLIN | POLLRDNORM);
5111541Srgrimes
51229354Speter	if (events & (POLLOUT | POLLWRNORM))
51329354Speter		if (tp->t_state & TS_ISOPEN &&
51429354Speter		    ((pti->pt_flags & PF_REMOTE) ?
51529354Speter		     (tp->t_canq.c_cc == 0) :
51629354Speter		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
51790831Sdillon		      (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON)))))
51829354Speter			revents |= events & (POLLOUT | POLLWRNORM);
5191541Srgrimes
52029354Speter	if (events & POLLHUP)
52129354Speter		if ((tp->t_state & TS_CARR_ON) == 0)
52229354Speter			revents |= POLLHUP;
5231541Srgrimes
52429354Speter	if (revents == 0) {
52529354Speter		if (events & (POLLIN | POLLRDNORM))
52683803Sjhb			selrecord(td, &pti->pt_selr);
52729354Speter
52829354Speter		if (events & (POLLOUT | POLLWRNORM))
52983803Sjhb			selrecord(td, &pti->pt_selw);
5301541Srgrimes	}
53129354Speter	splx(s);
53229354Speter
53329354Speter	return (revents);
5341541Srgrimes}
5351541Srgrimes
53612675Sjulianstatic	int
5371541Srgrimesptcwrite(dev, uio, flag)
5381541Srgrimes	dev_t dev;
5391541Srgrimes	register struct uio *uio;
5401541Srgrimes	int flag;
5411541Srgrimes{
54250652Sphk	register struct tty *tp = dev->si_tty;
5431549Srgrimes	register u_char *cp = 0;
5441541Srgrimes	register int cc = 0;
5451541Srgrimes	u_char locbuf[BUFSIZ];
5461541Srgrimes	int cnt = 0;
54749536Sphk	struct pt_ioctl *pti = dev->si_drv1;
5481541Srgrimes	int error = 0;
5491541Srgrimes
5501541Srgrimesagain:
5511541Srgrimes	if ((tp->t_state&TS_ISOPEN) == 0)
5521541Srgrimes		goto block;
5531541Srgrimes	if (pti->pt_flags & PF_REMOTE) {
5541541Srgrimes		if (tp->t_canq.c_cc)
5551541Srgrimes			goto block;
55611789Sbde		while ((uio->uio_resid > 0 || cc > 0) &&
55711789Sbde		       tp->t_canq.c_cc < TTYHOG - 1) {
5581541Srgrimes			if (cc == 0) {
5591541Srgrimes				cc = min(uio->uio_resid, BUFSIZ);
5601541Srgrimes				cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
5611541Srgrimes				cp = locbuf;
5621541Srgrimes				error = uiomove((caddr_t)cp, cc, uio);
5631541Srgrimes				if (error)
5641541Srgrimes					return (error);
5651541Srgrimes				/* check again for safety */
56611789Sbde				if ((tp->t_state & TS_ISOPEN) == 0) {
56711789Sbde					/* adjust as usual */
56811789Sbde					uio->uio_resid += cc;
5691541Srgrimes					return (EIO);
57011789Sbde				}
5711541Srgrimes			}
57215199Sbde			if (cc > 0) {
57315199Sbde				cc = b_to_q((char *)cp, cc, &tp->t_canq);
57415199Sbde				/*
57515199Sbde				 * XXX we don't guarantee that the canq size
57615199Sbde				 * is >= TTYHOG, so the above b_to_q() may
57715199Sbde				 * leave some bytes uncopied.  However, space
57815199Sbde				 * is guaranteed for the null terminator if
57915199Sbde				 * we don't fail here since (TTYHOG - 1) is
58015199Sbde				 * not a multiple of CBSIZE.
58115199Sbde				 */
58215199Sbde				if (cc > 0)
58315199Sbde					break;
58415199Sbde			}
5851541Srgrimes		}
58611789Sbde		/* adjust for data copied in but not written */
58711789Sbde		uio->uio_resid += cc;
5881541Srgrimes		(void) putc(0, &tp->t_canq);
5891541Srgrimes		ttwakeup(tp);
5909639Sbde		wakeup(TSA_PTS_READ(tp));
5911541Srgrimes		return (0);
5921541Srgrimes	}
59311789Sbde	while (uio->uio_resid > 0 || cc > 0) {
5941541Srgrimes		if (cc == 0) {
5951541Srgrimes			cc = min(uio->uio_resid, BUFSIZ);
5961541Srgrimes			cp = locbuf;
5971541Srgrimes			error = uiomove((caddr_t)cp, cc, uio);
5981541Srgrimes			if (error)
5991541Srgrimes				return (error);
6001541Srgrimes			/* check again for safety */
60111789Sbde			if ((tp->t_state & TS_ISOPEN) == 0) {
60211789Sbde				/* adjust for data copied in but not written */
60311789Sbde				uio->uio_resid += cc;
6041541Srgrimes				return (EIO);
60511789Sbde			}
6061541Srgrimes		}
6071541Srgrimes		while (cc > 0) {
6081541Srgrimes			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
60990831Sdillon			   (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) {
6109824Sbde				wakeup(TSA_HUP_OR_INPUT(tp));
6111541Srgrimes				goto block;
6121541Srgrimes			}
6131541Srgrimes			(*linesw[tp->t_line].l_rint)(*cp++, tp);
6141541Srgrimes			cnt++;
6151541Srgrimes			cc--;
6161541Srgrimes		}
6171541Srgrimes		cc = 0;
6181541Srgrimes	}
6191541Srgrimes	return (0);
6201541Srgrimesblock:
6211541Srgrimes	/*
6221541Srgrimes	 * Come here to wait for slave to open, for space
62315199Sbde	 * in outq, or space in rawq, or an empty canq.
6241541Srgrimes	 */
62511789Sbde	if ((tp->t_state & TS_CONNECTED) == 0) {
62611789Sbde		/* adjust for data copied in but not written */
62711789Sbde		uio->uio_resid += cc;
6281541Srgrimes		return (EIO);
62911789Sbde	}
6301541Srgrimes	if (flag & IO_NDELAY) {
6311541Srgrimes		/* adjust for data copied in but not written */
6321541Srgrimes		uio->uio_resid += cc;
6331541Srgrimes		if (cnt == 0)
6341541Srgrimes			return (EWOULDBLOCK);
6351541Srgrimes		return (0);
6361541Srgrimes	}
6379639Sbde	error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0);
6383308Sphk	if (error) {
6391541Srgrimes		/* adjust for data copied in but not written */
6401541Srgrimes		uio->uio_resid += cc;
6411541Srgrimes		return (error);
6421541Srgrimes	}
6431541Srgrimes	goto again;
6441541Srgrimes}
6451541Srgrimes
6461541Srgrimes/*ARGSUSED*/
64712675Sjulianstatic	int
64883366Sjulianptyioctl(dev, cmd, data, flag, td)
6491541Srgrimes	dev_t dev;
65036735Sdfr	u_long cmd;
6511541Srgrimes	caddr_t data;
6521541Srgrimes	int flag;
65383366Sjulian	struct thread *td;
6541541Srgrimes{
65550652Sphk	register struct tty *tp = dev->si_tty;
65649536Sphk	register struct pt_ioctl *pti = dev->si_drv1;
6571541Srgrimes	register u_char *cc = tp->t_cc;
6581541Srgrimes	int stop, error;
6591541Srgrimes
66047301Sluoqi	if (devsw(dev)->d_open == ptcopen) {
6611541Srgrimes		switch (cmd) {
6621541Srgrimes
6631541Srgrimes		case TIOCGPGRP:
6641541Srgrimes			/*
66533826Sbde			 * We avoid calling ttioctl on the controller since,
6661541Srgrimes			 * in that case, tp must be the controlling terminal.
6671541Srgrimes			 */
6681541Srgrimes			*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
6691541Srgrimes			return (0);
6701541Srgrimes
6711541Srgrimes		case TIOCPKT:
6721541Srgrimes			if (*(int *)data) {
6731541Srgrimes				if (pti->pt_flags & PF_UCNTL)
6741541Srgrimes					return (EINVAL);
6751541Srgrimes				pti->pt_flags |= PF_PKT;
6761541Srgrimes			} else
6771541Srgrimes				pti->pt_flags &= ~PF_PKT;
6781541Srgrimes			return (0);
6791541Srgrimes
6801541Srgrimes		case TIOCUCNTL:
6811541Srgrimes			if (*(int *)data) {
6821541Srgrimes				if (pti->pt_flags & PF_PKT)
6831541Srgrimes					return (EINVAL);
6841541Srgrimes				pti->pt_flags |= PF_UCNTL;
6851541Srgrimes			} else
6861541Srgrimes				pti->pt_flags &= ~PF_UCNTL;
6871541Srgrimes			return (0);
6881541Srgrimes
6891541Srgrimes		case TIOCREMOTE:
6901541Srgrimes			if (*(int *)data)
6911541Srgrimes				pti->pt_flags |= PF_REMOTE;
6921541Srgrimes			else
6931541Srgrimes				pti->pt_flags &= ~PF_REMOTE;
6941541Srgrimes			ttyflush(tp, FREAD|FWRITE);
6951541Srgrimes			return (0);
69647203Sluoqi		}
6971541Srgrimes
69847203Sluoqi		/*
69947203Sluoqi		 * The rest of the ioctls shouldn't be called until
70047301Sluoqi		 * the slave is open.
70147203Sluoqi		 */
70247203Sluoqi		if ((tp->t_state & TS_ISOPEN) == 0)
70347301Sluoqi			return (EAGAIN);
70447203Sluoqi
70547203Sluoqi		switch (cmd) {
7061541Srgrimes#ifdef COMPAT_43
7078876Srgrimes		case TIOCSETP:
7081541Srgrimes		case TIOCSETN:
7091541Srgrimes#endif
7101541Srgrimes		case TIOCSETD:
7111541Srgrimes		case TIOCSETA:
7121541Srgrimes		case TIOCSETAW:
7139858Sache		case TIOCSETAF:
71447301Sluoqi			/*
71547301Sluoqi			 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
71647301Sluoqi			 * ttywflush(tp) will hang if there are characters in
71747301Sluoqi			 * the outq.
71847301Sluoqi			 */
7191541Srgrimes			ndflush(&tp->t_outq, tp->t_outq.c_cc);
7201541Srgrimes			break;
7211541Srgrimes
7221541Srgrimes		case TIOCSIG:
72327770Sjmg			if (*(unsigned int *)data >= NSIG ||
72427770Sjmg			    *(unsigned int *)data == 0)
7251541Srgrimes				return(EINVAL);
7261541Srgrimes			if ((tp->t_lflag&NOFLSH) == 0)
7271541Srgrimes				ttyflush(tp, FREAD|FWRITE);
72891140Stanimura			if (tp->t_pgrp != NULL) {
72991140Stanimura				PGRP_LOCK(tp->t_pgrp);
73091140Stanimura				pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
73191140Stanimura				PGRP_UNLOCK(tp->t_pgrp);
73291140Stanimura			}
7331541Srgrimes			if ((*(unsigned int *)data == SIGINFO) &&
7341541Srgrimes			    ((tp->t_lflag&NOKERNINFO) == 0))
7351541Srgrimes				ttyinfo(tp);
7361541Srgrimes			return(0);
7371541Srgrimes		}
73847203Sluoqi	}
73947301Sluoqi	if (cmd == TIOCEXT) {
74047301Sluoqi		/*
74147301Sluoqi		 * When the EXTPROC bit is being toggled, we need
74247301Sluoqi		 * to send an TIOCPKT_IOCTL if the packet driver
74347301Sluoqi		 * is turned on.
74447301Sluoqi		 */
74547301Sluoqi		if (*(int *)data) {
74647301Sluoqi			if (pti->pt_flags & PF_PKT) {
74747301Sluoqi				pti->pt_send |= TIOCPKT_IOCTL;
74847301Sluoqi				ptcwakeup(tp, FREAD);
74947301Sluoqi			}
75047301Sluoqi			tp->t_lflag |= EXTPROC;
75147301Sluoqi		} else {
75247301Sluoqi			if ((tp->t_lflag & EXTPROC) &&
75347301Sluoqi			    (pti->pt_flags & PF_PKT)) {
75447301Sluoqi				pti->pt_send |= TIOCPKT_IOCTL;
75547301Sluoqi				ptcwakeup(tp, FREAD);
75647301Sluoqi			}
75747301Sluoqi			tp->t_lflag &= ~EXTPROC;
75847301Sluoqi		}
75947301Sluoqi		return(0);
76047301Sluoqi	}
76183366Sjulian	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, td);
76231577Sbde	if (error == ENOIOCTL)
7631541Srgrimes		 error = ttioctl(tp, cmd, data, flag);
76431577Sbde	if (error == ENOIOCTL) {
7651541Srgrimes		if (pti->pt_flags & PF_UCNTL &&
7661541Srgrimes		    (cmd & ~0xff) == UIOCCMD(0)) {
7671541Srgrimes			if (cmd & 0xff) {
7681541Srgrimes				pti->pt_ucntl = (u_char)cmd;
7691541Srgrimes				ptcwakeup(tp, FREAD);
7701541Srgrimes			}
7711541Srgrimes			return (0);
7721541Srgrimes		}
7731541Srgrimes		error = ENOTTY;
7741541Srgrimes	}
7751541Srgrimes	/*
7761541Srgrimes	 * If external processing and packet mode send ioctl packet.
7771541Srgrimes	 */
7781541Srgrimes	if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
7791541Srgrimes		switch(cmd) {
7801541Srgrimes		case TIOCSETA:
7811541Srgrimes		case TIOCSETAW:
7821541Srgrimes		case TIOCSETAF:
7831541Srgrimes#ifdef COMPAT_43
7841541Srgrimes		case TIOCSETP:
7851541Srgrimes		case TIOCSETN:
7861541Srgrimes#endif
7871541Srgrimes#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
7881541Srgrimes		case TIOCSETC:
7891541Srgrimes		case TIOCSLTC:
7901541Srgrimes		case TIOCLBIS:
7911541Srgrimes		case TIOCLBIC:
7921541Srgrimes		case TIOCLSET:
7931541Srgrimes#endif
7941541Srgrimes			pti->pt_send |= TIOCPKT_IOCTL;
7951541Srgrimes			ptcwakeup(tp, FREAD);
7961541Srgrimes		default:
7971541Srgrimes			break;
7981541Srgrimes		}
7991541Srgrimes	}
8008876Srgrimes	stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
8011541Srgrimes		&& CCEQ(cc[VSTART], CTRL('q'));
8021541Srgrimes	if (pti->pt_flags & PF_NOSTOP) {
8031541Srgrimes		if (stop) {
8041541Srgrimes			pti->pt_send &= ~TIOCPKT_NOSTOP;
8051541Srgrimes			pti->pt_send |= TIOCPKT_DOSTOP;
8061541Srgrimes			pti->pt_flags &= ~PF_NOSTOP;
8071541Srgrimes			ptcwakeup(tp, FREAD);
8081541Srgrimes		}
8091541Srgrimes	} else {
8101541Srgrimes		if (!stop) {
8111541Srgrimes			pti->pt_send &= ~TIOCPKT_DOSTOP;
8121541Srgrimes			pti->pt_send |= TIOCPKT_NOSTOP;
8131541Srgrimes			pti->pt_flags |= PF_NOSTOP;
8141541Srgrimes			ptcwakeup(tp, FREAD);
8151541Srgrimes		}
8161541Srgrimes	}
8171541Srgrimes	return (error);
8181541Srgrimes}
81912517Sjulian
82012517Sjulian
82192723Salfredstatic void ptc_drvinit(void *unused);
82249536Sphk
82392723Salfredstatic void pty_clone(void *arg, char *name, int namelen, dev_t *dev);
82464880Sphk
82512675Sjulianstatic void
82664880Sphkpty_clone(arg, name, namelen, dev)
82764880Sphk	void *arg;
82864880Sphk	char *name;
82964880Sphk	int namelen;
83064880Sphk	dev_t *dev;
83164880Sphk{
83264880Sphk	int u;
83364880Sphk
83464880Sphk	if (*dev != NODEV)
83564880Sphk		return;
83664880Sphk	if (bcmp(name, "pty", 3) != 0)
83764880Sphk		return;
83864880Sphk	if (name[5] != '\0')
83964880Sphk		return;
84064880Sphk	switch (name[3]) {
84164880Sphk	case 'p': u =   0; break;
84264880Sphk	case 'q': u =  32; break;
84364880Sphk	case 'r': u =  64; break;
84464880Sphk	case 's': u =  96; break;
84564880Sphk	case 'P': u = 128; break;
84664880Sphk	case 'Q': u = 160; break;
84764880Sphk	case 'R': u = 192; break;
84864880Sphk	case 'S': u = 224; break;
84964880Sphk	default: return;
85064880Sphk	}
85164880Sphk	if (name[4] >= '0' && name[4] <= '9')
85264880Sphk		u += name[4] - '0';
85364880Sphk	else if (name[4] >= 'a' && name[4] <= 'v')
85464880Sphk		u += name[4] - 'a' + 10;
85564880Sphk	else
85664880Sphk		return;
85777176Sphk	*dev = make_dev(&ptc_cdevsw, u,
85877176Sphk	    UID_ROOT, GID_WHEEL, 0666, "pty%c%r", names[u / 32], u % 32);
85977176Sphk	(*dev)->si_flags |= SI_CHEAPCLONE;
86064880Sphk	return;
86164880Sphk}
86264880Sphk
86364880Sphkstatic void
86429506Sbdeptc_drvinit(unused)
86529506Sbde	void *unused;
86612517Sjulian{
86765374Sphk	EVENTHANDLER_REGISTER(dev_clone, pty_clone, 0, 1000);
86853483Sjkh	cdevsw_add(&pts_cdevsw);
86953483Sjkh	cdevsw_add(&ptc_cdevsw);
87012517Sjulian}
87112517Sjulian
87212517SjulianSYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)
873