pty.c revision 130077
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 * 4. Neither the name of the University nor the names of its contributors
141541Srgrimes *    may be used to endorse or promote products derived from this software
151541Srgrimes *    without specific prior written permission.
161541Srgrimes *
171541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271541Srgrimes * SUCH DAMAGE.
281541Srgrimes *
2914511Shsu *	@(#)tty_pty.c	8.4 (Berkeley) 2/20/95
301541Srgrimes */
311541Srgrimes
32116182Sobrien#include <sys/cdefs.h>
33116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/tty_pty.c 130077 2004-06-04 16:02:56Z phk $");
34116182Sobrien
351541Srgrimes/*
361541Srgrimes * Pseudo-teletype Driver
371541Srgrimes * (Actually two drivers, requiring two entries in 'cdevsw')
381541Srgrimes */
3931778Seivind#include "opt_compat.h"
40111899Sdas#include "opt_tty.h"
411541Srgrimes#include <sys/param.h>
421541Srgrimes#include <sys/systm.h>
4391140Stanimura#include <sys/lock.h>
4491140Stanimura#include <sys/mutex.h>
4591140Stanimura#include <sys/sx.h>
4624207Sbde#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
4724207Sbde#include <sys/ioctl_compat.h>
4824207Sbde#endif
491541Srgrimes#include <sys/proc.h>
501541Srgrimes#include <sys/tty.h>
511541Srgrimes#include <sys/conf.h>
5224131Sbde#include <sys/fcntl.h>
5329354Speter#include <sys/poll.h>
541541Srgrimes#include <sys/kernel.h>
551541Srgrimes#include <sys/vnode.h>
563308Sphk#include <sys/signalvar.h>
5749536Sphk#include <sys/malloc.h>
581541Srgrimes
5969774Sphkstatic MALLOC_DEFINE(M_PTY, "ptys", "pty data structures");
6012517Sjulian
6192723Salfredstatic void ptsstart(struct tty *tp);
6292723Salfredstatic void ptsstop(struct tty *tp, int rw);
6392723Salfredstatic void ptcwakeup(struct tty *tp, int flag);
6492723Salfredstatic dev_t ptyinit(dev_t cdev);
6511789Sbde
6612675Sjulianstatic	d_open_t	ptsopen;
6712675Sjulianstatic	d_close_t	ptsclose;
6812675Sjulianstatic	d_read_t	ptsread;
6912675Sjulianstatic	d_write_t	ptswrite;
7012675Sjulianstatic	d_ioctl_t	ptyioctl;
7112675Sjulianstatic	d_open_t	ptcopen;
7212675Sjulianstatic	d_close_t	ptcclose;
7312675Sjulianstatic	d_read_t	ptcread;
7412675Sjulianstatic	d_write_t	ptcwrite;
7529354Speterstatic	d_poll_t	ptcpoll;
7612675Sjulian
7738485Sbde#define	CDEV_MAJOR_S	5
7847625Sphkstatic struct cdevsw pts_cdevsw = {
79126080Sphk	.d_version =	D_VERSION,
80111815Sphk	.d_open =	ptsopen,
81111815Sphk	.d_close =	ptsclose,
82111815Sphk	.d_read =	ptsread,
83111815Sphk	.d_write =	ptswrite,
84111815Sphk	.d_ioctl =	ptyioctl,
85111815Sphk	.d_name =	"pts",
86111815Sphk	.d_maj =	CDEV_MAJOR_S,
87126080Sphk	.d_flags =	D_TTY | D_NEEDGIANT,
8838485Sbde};
8912675Sjulian
9038485Sbde#define	CDEV_MAJOR_C	6
9147625Sphkstatic struct cdevsw ptc_cdevsw = {
92126080Sphk	.d_version =	D_VERSION,
93111815Sphk	.d_open =	ptcopen,
94111815Sphk	.d_close =	ptcclose,
95111815Sphk	.d_read =	ptcread,
96111815Sphk	.d_write =	ptcwrite,
97111815Sphk	.d_ioctl =	ptyioctl,
98111815Sphk	.d_poll =	ptcpoll,
99111815Sphk	.d_name =	"ptc",
100111815Sphk	.d_maj =	CDEV_MAJOR_C,
101126080Sphk	.d_flags =	D_TTY | D_NEEDGIANT,
10238485Sbde};
10312675Sjulian
1041541Srgrimes#define BUFSIZ 100		/* Chunk size iomoved to/from user */
1051541Srgrimes
10649536Sphkstruct	pt_ioctl {
1071541Srgrimes	int	pt_flags;
1081541Srgrimes	struct	selinfo pt_selr, pt_selw;
1091541Srgrimes	u_char	pt_send;
1101541Srgrimes	u_char	pt_ucntl;
111130054Sphk	struct tty *pt_tty;
11250092Sjulian	dev_t	devs, devc;
11357070Srwatson	struct	prison *pt_prison;
11449536Sphk};
1151541Srgrimes
1161541Srgrimes#define	PF_PKT		0x08		/* packet mode */
1171541Srgrimes#define	PF_STOPPED	0x10		/* user told stopped */
1181541Srgrimes#define	PF_REMOTE	0x20		/* remote and flow controlled input */
1191541Srgrimes#define	PF_NOSTOP	0x40
1201541Srgrimes#define PF_UCNTL	0x80		/* user control mode */
1211541Srgrimes
12277176Sphkstatic char *names = "pqrsPQRS";
1231541Srgrimes/*
12449536Sphk * This function creates and initializes a pts/ptc pair
1251541Srgrimes *
12649536Sphk * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
12749536Sphk * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
12849536Sphk *
129111742Sdes * XXX: define and add mapping of upper minor bits to allow more
13049536Sphk *      than 256 ptys.
1311541Srgrimes */
13264880Sphkstatic dev_t
13377176Sphkptyinit(dev_t devc)
1341541Srgrimes{
13577176Sphk	dev_t devs;
13649536Sphk	struct pt_ioctl *pt;
13777176Sphk	int n;
1381541Srgrimes
13977176Sphk	n = minor(devc);
14049536Sphk	/* For now we only map the lower 8 bits of the minor */
14149536Sphk	if (n & ~0xff)
14264880Sphk		return (NODEV);
14349536Sphk
14478405Sbrian	devc->si_flags &= ~SI_CHEAPCLONE;
14578405Sbrian
146111119Simp	pt = malloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO);
14750092Sjulian	pt->devs = devs = make_dev(&pts_cdevsw, n,
14866067Sphk	    UID_ROOT, GID_WHEEL, 0666, "tty%c%r", names[n / 32], n % 32);
14977176Sphk	pt->devc = devc;
15049536Sphk
151130054Sphk	pt->pt_tty = ttymalloc(pt->pt_tty);
15249536Sphk	devs->si_drv1 = devc->si_drv1 = pt;
153130054Sphk	devs->si_tty = devc->si_tty = pt->pt_tty;
154130054Sphk	pt->pt_tty->t_dev = devs;
15564880Sphk	return (devc);
15616322Sgpalmer}
1571541Srgrimes
1581541Srgrimes/*ARGSUSED*/
15912675Sjulianstatic	int
16083366Sjulianptsopen(dev, flag, devtype, td)
1611541Srgrimes	dev_t dev;
1621541Srgrimes	int flag, devtype;
16383366Sjulian	struct thread *td;
1641541Srgrimes{
165111742Sdes	struct tty *tp;
1661541Srgrimes	int error;
16757070Srwatson	struct pt_ioctl *pti;
1681541Srgrimes
16949536Sphk	if (!dev->si_drv1)
170111742Sdes		return(ENXIO);
17157070Srwatson	pti = dev->si_drv1;
17250652Sphk	tp = dev->si_tty;
1731541Srgrimes	if ((tp->t_state & TS_ISOPEN) == 0) {
1741541Srgrimes		ttychars(tp);		/* Set up default chars */
1751541Srgrimes		tp->t_iflag = TTYDEF_IFLAG;
1761541Srgrimes		tp->t_oflag = TTYDEF_OFLAG;
1771541Srgrimes		tp->t_lflag = TTYDEF_LFLAG;
1781541Srgrimes		tp->t_cflag = TTYDEF_CFLAG;
1791541Srgrimes		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
180125839Srwatson	} else if (tp->t_state & TS_XCLUDE && suser(td))
1811541Srgrimes		return (EBUSY);
182125839Srwatson	else if (pti->pt_prison != td->td_ucred->cr_prison)
18357070Srwatson		return (EBUSY);
1841541Srgrimes	if (tp->t_oproc)			/* Ctrlr still around. */
185130077Sphk		(void)ttyld_modem(tp, 1);
1861541Srgrimes	while ((tp->t_state & TS_CARR_ON) == 0) {
1871541Srgrimes		if (flag&FNONBLOCK)
1881541Srgrimes			break;
1899639Sbde		error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH,
1909624Sbde				 "ptsopn", 0);
1913308Sphk		if (error)
1921541Srgrimes			return (error);
1931541Srgrimes	}
194130077Sphk	error = ttyld_open(tp, dev);
1957724Sache	if (error == 0)
1967724Sache		ptcwakeup(tp, FREAD|FWRITE);
1971541Srgrimes	return (error);
1981541Srgrimes}
1991541Srgrimes
20012675Sjulianstatic	int
20183366Sjulianptsclose(dev, flag, mode, td)
2021541Srgrimes	dev_t dev;
2031541Srgrimes	int flag, mode;
20483366Sjulian	struct thread *td;
2051541Srgrimes{
206111742Sdes	struct tty *tp;
2071541Srgrimes	int err;
2081541Srgrimes
20950652Sphk	tp = dev->si_tty;
210130077Sphk	err = ttyld_close(tp, flag);
2117724Sache	(void) ttyclose(tp);
2121541Srgrimes	return (err);
2131541Srgrimes}
2141541Srgrimes
21512675Sjulianstatic	int
2161541Srgrimesptsread(dev, uio, flag)
2171541Srgrimes	dev_t dev;
2181541Srgrimes	struct uio *uio;
2191541Srgrimes	int flag;
2201541Srgrimes{
22183366Sjulian	struct thread *td = curthread;
22283366Sjulian	struct proc *p = td->td_proc;
223111742Sdes	struct tty *tp = dev->si_tty;
224111742Sdes	struct pt_ioctl *pti = dev->si_drv1;
22594860Sjhb	struct pgrp *pg;
2261541Srgrimes	int error = 0;
2271541Srgrimes
2281541Srgrimesagain:
2291541Srgrimes	if (pti->pt_flags & PF_REMOTE) {
2301541Srgrimes		while (isbackground(p, tp)) {
23194860Sjhb			sx_slock(&proctree_lock);
23291140Stanimura			PROC_LOCK(p);
233114983Sjhb			if (SIGISMEMBER(p->p_sigacts->ps_sigignore, SIGTTIN) ||
234112888Sjeff			    SIGISMEMBER(td->td_sigmask, SIGTTIN) ||
23591140Stanimura			    p->p_pgrp->pg_jobc == 0 || p->p_flag & P_PPWAIT) {
23691140Stanimura				PROC_UNLOCK(p);
23794860Sjhb				sx_sunlock(&proctree_lock);
2381541Srgrimes				return (EIO);
23991140Stanimura			}
24094860Sjhb			pg = p->p_pgrp;
24191140Stanimura			PROC_UNLOCK(p);
24294860Sjhb			PGRP_LOCK(pg);
24394860Sjhb			sx_sunlock(&proctree_lock);
24494860Sjhb			pgsignal(pg, SIGTTIN, 1);
24594860Sjhb			PGRP_UNLOCK(pg);
2469624Sbde			error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ptsbg",
2479624Sbde					 0);
2483308Sphk			if (error)
2491541Srgrimes				return (error);
2501541Srgrimes		}
2511541Srgrimes		if (tp->t_canq.c_cc == 0) {
2521541Srgrimes			if (flag & IO_NDELAY)
2531541Srgrimes				return (EWOULDBLOCK);
2549639Sbde			error = ttysleep(tp, TSA_PTS_READ(tp), TTIPRI | PCATCH,
2559624Sbde					 "ptsin", 0);
2563308Sphk			if (error)
2571541Srgrimes				return (error);
2581541Srgrimes			goto again;
2591541Srgrimes		}
2601541Srgrimes		while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
2611541Srgrimes			if (ureadc(getc(&tp->t_canq), uio) < 0) {
2621541Srgrimes				error = EFAULT;
2631541Srgrimes				break;
2641541Srgrimes			}
2651541Srgrimes		if (tp->t_canq.c_cc == 1)
2661541Srgrimes			(void) getc(&tp->t_canq);
2671541Srgrimes		if (tp->t_canq.c_cc)
2681541Srgrimes			return (error);
2691541Srgrimes	} else
2701541Srgrimes		if (tp->t_oproc)
271130077Sphk			error = ttyld_read(tp, uio, flag);
2721541Srgrimes	ptcwakeup(tp, FWRITE);
2731541Srgrimes	return (error);
2741541Srgrimes}
2751541Srgrimes
2761541Srgrimes/*
2771541Srgrimes * Write to pseudo-tty.
2781541Srgrimes * Wakeups of controlling tty will happen
2791541Srgrimes * indirectly, when tty driver calls ptsstart.
2801541Srgrimes */
28112675Sjulianstatic	int
2821541Srgrimesptswrite(dev, uio, flag)
2831541Srgrimes	dev_t dev;
2841541Srgrimes	struct uio *uio;
2851541Srgrimes	int flag;
2861541Srgrimes{
287111742Sdes	struct tty *tp;
2881541Srgrimes
28950652Sphk	tp = dev->si_tty;
2901541Srgrimes	if (tp->t_oproc == 0)
2911541Srgrimes		return (EIO);
292130077Sphk	return (ttyld_write(tp, uio, flag));
2931541Srgrimes}
2941541Srgrimes
2951541Srgrimes/*
2961541Srgrimes * Start output on pseudo-tty.
2971541Srgrimes * Wake up process selecting or sleeping for input from controlling tty.
2981541Srgrimes */
29912819Sphkstatic void
3001541Srgrimesptsstart(tp)
3011541Srgrimes	struct tty *tp;
3021541Srgrimes{
303111742Sdes	struct pt_ioctl *pti = tp->t_dev->si_drv1;
3041541Srgrimes
3051541Srgrimes	if (tp->t_state & TS_TTSTOP)
3061541Srgrimes		return;
3071541Srgrimes	if (pti->pt_flags & PF_STOPPED) {
3081541Srgrimes		pti->pt_flags &= ~PF_STOPPED;
3091541Srgrimes		pti->pt_send = TIOCPKT_START;
3101541Srgrimes	}
3111541Srgrimes	ptcwakeup(tp, FREAD);
3121541Srgrimes}
3131541Srgrimes
31412819Sphkstatic void
3151541Srgrimesptcwakeup(tp, flag)
3161541Srgrimes	struct tty *tp;
3171541Srgrimes	int flag;
3181541Srgrimes{
31949536Sphk	struct pt_ioctl *pti = tp->t_dev->si_drv1;
3201541Srgrimes
3211541Srgrimes	if (flag & FREAD) {
322122352Stanimura		selwakeuppri(&pti->pt_selr, TTIPRI);
3239639Sbde		wakeup(TSA_PTC_READ(tp));
3241541Srgrimes	}
3251541Srgrimes	if (flag & FWRITE) {
326122352Stanimura		selwakeuppri(&pti->pt_selw, TTOPRI);
3279639Sbde		wakeup(TSA_PTC_WRITE(tp));
3281541Srgrimes	}
3291541Srgrimes}
3301541Srgrimes
33112675Sjulianstatic	int
33283366Sjulianptcopen(dev, flag, devtype, td)
3331541Srgrimes	dev_t dev;
3341541Srgrimes	int flag, devtype;
33583366Sjulian	struct thread *td;
3361541Srgrimes{
337111742Sdes	struct tty *tp;
3381541Srgrimes	struct pt_ioctl *pti;
3391541Srgrimes
34049536Sphk	if (!dev->si_drv1)
34177176Sphk		ptyinit(dev);
34249536Sphk	if (!dev->si_drv1)
343111742Sdes		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;
350130077Sphk	(void)ttyld_modem(tp, 1);
3511541Srgrimes	tp->t_lflag &= ~EXTPROC;
35249536Sphk	pti = dev->si_drv1;
35391406Sjhb	pti->pt_prison = td->td_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{
367111742Sdes	struct tty *tp;
3681541Srgrimes
36950652Sphk	tp = dev->si_tty;
370130077Sphk	(void)ttyld_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{
396111742Sdes	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));
416111741Sdes					uiomove(&tp->t_termios, cc, uio);
4171541Srgrimes				}
4181541Srgrimes				pti->pt_send = 0;
4191541Srgrimes				return (0);
4201541Srgrimes			}
4211541Srgrimes			if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
4221541Srgrimes				error = ureadc((int)pti->pt_ucntl, uio);
4231541Srgrimes				if (error)
4241541Srgrimes					return (error);
4251541Srgrimes				pti->pt_ucntl = 0;
4261541Srgrimes				return (0);
4271541Srgrimes			}
4281541Srgrimes			if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
4291541Srgrimes				break;
4301541Srgrimes		}
4319824Sbde		if ((tp->t_state & TS_CONNECTED) == 0)
4321541Srgrimes			return (0);	/* EOF */
4331541Srgrimes		if (flag & IO_NDELAY)
4341541Srgrimes			return (EWOULDBLOCK);
4359639Sbde		error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0);
4363308Sphk		if (error)
4371541Srgrimes			return (error);
4381541Srgrimes	}
4391541Srgrimes	if (pti->pt_flags & (PF_PKT|PF_UCNTL))
4401541Srgrimes		error = ureadc(0, uio);
4411541Srgrimes	while (uio->uio_resid > 0 && error == 0) {
4421541Srgrimes		cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
4431541Srgrimes		if (cc <= 0)
4441541Srgrimes			break;
4451541Srgrimes		error = uiomove(buf, cc, uio);
4461541Srgrimes	}
4479626Sbde	ttwwakeup(tp);
4481541Srgrimes	return (error);
4491541Srgrimes}
4501541Srgrimes
45112675Sjulianstatic	void
4521541Srgrimesptsstop(tp, flush)
453111742Sdes	struct tty *tp;
4541541Srgrimes	int flush;
4551541Srgrimes{
45649536Sphk	struct pt_ioctl *pti = tp->t_dev->si_drv1;
4571541Srgrimes	int flag;
4581541Srgrimes
4591541Srgrimes	/* note: FLUSHREAD and FLUSHWRITE already ok */
4601541Srgrimes	if (flush == 0) {
4611541Srgrimes		flush = TIOCPKT_STOP;
4621541Srgrimes		pti->pt_flags |= PF_STOPPED;
4631541Srgrimes	} else
4641541Srgrimes		pti->pt_flags &= ~PF_STOPPED;
4651541Srgrimes	pti->pt_send |= flush;
4661541Srgrimes	/* change of perspective */
4671541Srgrimes	flag = 0;
4681541Srgrimes	if (flush & FREAD)
4691541Srgrimes		flag |= FWRITE;
4701541Srgrimes	if (flush & FWRITE)
4711541Srgrimes		flag |= FREAD;
4721541Srgrimes	ptcwakeup(tp, flag);
4731541Srgrimes}
4741541Srgrimes
47512675Sjulianstatic	int
47683366Sjulianptcpoll(dev, events, td)
4771541Srgrimes	dev_t dev;
47829354Speter	int events;
47983366Sjulian	struct thread *td;
4801541Srgrimes{
481111742Sdes	struct tty *tp = dev->si_tty;
48249536Sphk	struct pt_ioctl *pti = dev->si_drv1;
48329354Speter	int revents = 0;
4841541Srgrimes	int s;
4851541Srgrimes
4869824Sbde	if ((tp->t_state & TS_CONNECTED) == 0)
487120513Sphk		return (events &
488120513Sphk		   (POLLHUP | POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM));
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) ?
505111742Sdes		     (tp->t_canq.c_cc == 0) :
50629354Speter		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
50790831Sdillon		      (tp->t_canq.c_cc == 0 && (tp->t_lflag & 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
518111742Sdes		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;
529111742Sdes	struct uio *uio;
5301541Srgrimes	int flag;
5311541Srgrimes{
532111742Sdes	struct tty *tp = dev->si_tty;
533111742Sdes	u_char *cp = 0;
534111742Sdes	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;
552111741Sdes				error = uiomove(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;
587111741Sdes			error = uiomove(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 &&
59990831Sdillon			   (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) {
6009824Sbde				wakeup(TSA_HUP_OR_INPUT(tp));
6011541Srgrimes				goto block;
6021541Srgrimes			}
603130077Sphk			ttyld_rint(tp, *cp++);
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{
645111742Sdes	struct tty *tp = dev->si_tty;
646111742Sdes	struct pt_ioctl *pti = dev->si_drv1;
647111742Sdes	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		/*
689111742Sdes		 * 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);
71891140Stanimura			if (tp->t_pgrp != NULL) {
71991140Stanimura				PGRP_LOCK(tp->t_pgrp);
72091140Stanimura				pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
72191140Stanimura				PGRP_UNLOCK(tp->t_pgrp);
72291140Stanimura			}
7231541Srgrimes			if ((*(unsigned int *)data == SIGINFO) &&
7241541Srgrimes			    ((tp->t_lflag&NOKERNINFO) == 0))
7251541Srgrimes				ttyinfo(tp);
7261541Srgrimes			return(0);
7271541Srgrimes		}
72847203Sluoqi	}
72947301Sluoqi	if (cmd == TIOCEXT) {
73047301Sluoqi		/*
73147301Sluoqi		 * When the EXTPROC bit is being toggled, we need
73247301Sluoqi		 * to send an TIOCPKT_IOCTL if the packet driver
73347301Sluoqi		 * is turned on.
73447301Sluoqi		 */
73547301Sluoqi		if (*(int *)data) {
73647301Sluoqi			if (pti->pt_flags & PF_PKT) {
73747301Sluoqi				pti->pt_send |= TIOCPKT_IOCTL;
73847301Sluoqi				ptcwakeup(tp, FREAD);
73947301Sluoqi			}
74047301Sluoqi			tp->t_lflag |= EXTPROC;
74147301Sluoqi		} else {
74247301Sluoqi			if ((tp->t_lflag & EXTPROC) &&
74347301Sluoqi			    (pti->pt_flags & PF_PKT)) {
74447301Sluoqi				pti->pt_send |= TIOCPKT_IOCTL;
74547301Sluoqi				ptcwakeup(tp, FREAD);
74647301Sluoqi			}
74747301Sluoqi			tp->t_lflag &= ~EXTPROC;
74847301Sluoqi		}
74947301Sluoqi		return(0);
75047301Sluoqi	}
751130054Sphk	error = ttyioctl(dev, cmd, data, flag, td);
752130054Sphk	if (error == ENOTTY) {
7531541Srgrimes		if (pti->pt_flags & PF_UCNTL &&
7541541Srgrimes		    (cmd & ~0xff) == UIOCCMD(0)) {
7551541Srgrimes			if (cmd & 0xff) {
7561541Srgrimes				pti->pt_ucntl = (u_char)cmd;
7571541Srgrimes				ptcwakeup(tp, FREAD);
7581541Srgrimes			}
7591541Srgrimes			return (0);
7601541Srgrimes		}
7611541Srgrimes		error = ENOTTY;
7621541Srgrimes	}
7631541Srgrimes	/*
7641541Srgrimes	 * If external processing and packet mode send ioctl packet.
7651541Srgrimes	 */
7661541Srgrimes	if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
7671541Srgrimes		switch(cmd) {
7681541Srgrimes		case TIOCSETA:
7691541Srgrimes		case TIOCSETAW:
7701541Srgrimes		case TIOCSETAF:
7711541Srgrimes#ifdef COMPAT_43
7721541Srgrimes		case TIOCSETP:
7731541Srgrimes		case TIOCSETN:
7741541Srgrimes#endif
7751541Srgrimes#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
7761541Srgrimes		case TIOCSETC:
7771541Srgrimes		case TIOCSLTC:
7781541Srgrimes		case TIOCLBIS:
7791541Srgrimes		case TIOCLBIC:
7801541Srgrimes		case TIOCLSET:
7811541Srgrimes#endif
7821541Srgrimes			pti->pt_send |= TIOCPKT_IOCTL;
7831541Srgrimes			ptcwakeup(tp, FREAD);
784115463Sphk			break;
7851541Srgrimes		default:
7861541Srgrimes			break;
7871541Srgrimes		}
7881541Srgrimes	}
7898876Srgrimes	stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
7901541Srgrimes		&& CCEQ(cc[VSTART], CTRL('q'));
7911541Srgrimes	if (pti->pt_flags & PF_NOSTOP) {
7921541Srgrimes		if (stop) {
7931541Srgrimes			pti->pt_send &= ~TIOCPKT_NOSTOP;
7941541Srgrimes			pti->pt_send |= TIOCPKT_DOSTOP;
7951541Srgrimes			pti->pt_flags &= ~PF_NOSTOP;
7961541Srgrimes			ptcwakeup(tp, FREAD);
7971541Srgrimes		}
7981541Srgrimes	} else {
7991541Srgrimes		if (!stop) {
8001541Srgrimes			pti->pt_send &= ~TIOCPKT_DOSTOP;
8011541Srgrimes			pti->pt_send |= TIOCPKT_NOSTOP;
8021541Srgrimes			pti->pt_flags |= PF_NOSTOP;
8031541Srgrimes			ptcwakeup(tp, FREAD);
8041541Srgrimes		}
8051541Srgrimes	}
8061541Srgrimes	return (error);
8071541Srgrimes}
80812517Sjulian
80912517Sjulian
81092723Salfredstatic void ptc_drvinit(void *unused);
81149536Sphk
81292723Salfredstatic void pty_clone(void *arg, char *name, int namelen, dev_t *dev);
81364880Sphk
81412675Sjulianstatic void
81564880Sphkpty_clone(arg, name, namelen, dev)
81664880Sphk	void *arg;
81764880Sphk	char *name;
81864880Sphk	int namelen;
81964880Sphk	dev_t *dev;
82064880Sphk{
82164880Sphk	int u;
82264880Sphk
82364880Sphk	if (*dev != NODEV)
82464880Sphk		return;
82564880Sphk	if (bcmp(name, "pty", 3) != 0)
82664880Sphk		return;
82764880Sphk	if (name[5] != '\0')
82864880Sphk		return;
82964880Sphk	switch (name[3]) {
83064880Sphk	case 'p': u =   0; break;
83164880Sphk	case 'q': u =  32; break;
83264880Sphk	case 'r': u =  64; break;
83364880Sphk	case 's': u =  96; break;
83464880Sphk	case 'P': u = 128; break;
83564880Sphk	case 'Q': u = 160; break;
83664880Sphk	case 'R': u = 192; break;
83764880Sphk	case 'S': u = 224; break;
83864880Sphk	default: return;
83964880Sphk	}
84064880Sphk	if (name[4] >= '0' && name[4] <= '9')
84164880Sphk		u += name[4] - '0';
84264880Sphk	else if (name[4] >= 'a' && name[4] <= 'v')
84364880Sphk		u += name[4] - 'a' + 10;
84464880Sphk	else
84564880Sphk		return;
84677176Sphk	*dev = make_dev(&ptc_cdevsw, u,
84777176Sphk	    UID_ROOT, GID_WHEEL, 0666, "pty%c%r", names[u / 32], u % 32);
84877176Sphk	(*dev)->si_flags |= SI_CHEAPCLONE;
84964880Sphk	return;
85064880Sphk}
85164880Sphk
85264880Sphkstatic void
85329506Sbdeptc_drvinit(unused)
85429506Sbde	void *unused;
85512517Sjulian{
856108363Sphk
85765374Sphk	EVENTHANDLER_REGISTER(dev_clone, pty_clone, 0, 1000);
85812517Sjulian}
85912517Sjulian
86012517SjulianSYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)
861