Home | History | Annotate | Line # | Download | only in kern
tty_pty.c revision 1.40
      1  1.40   mycroft /*	$NetBSD: tty_pty.c,v 1.40 1998/03/21 04:02:47 mycroft Exp $	*/
      2  1.23       cgd 
      3   1.1       cgd /*
      4  1.22       cgd  * Copyright (c) 1982, 1986, 1989, 1993
      5  1.22       cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  *
     35  1.39      fvdl  *	@(#)tty_pty.c	8.4 (Berkeley) 2/20/95
     36   1.1       cgd  */
     37   1.1       cgd 
     38   1.1       cgd /*
     39   1.1       cgd  * Pseudo-teletype Driver
     40   1.1       cgd  * (Actually two drivers, requiring two entries in 'cdevsw')
     41   1.1       cgd  */
     42  1.22       cgd #include "pty.h"		/* XXX */
     43   1.1       cgd 
     44  1.16   mycroft #include <sys/param.h>
     45  1.16   mycroft #include <sys/systm.h>
     46  1.16   mycroft #include <sys/ioctl.h>
     47  1.22       cgd #include <sys/proc.h>
     48  1.16   mycroft #include <sys/tty.h>
     49  1.16   mycroft #include <sys/file.h>
     50  1.16   mycroft #include <sys/uio.h>
     51  1.16   mycroft #include <sys/kernel.h>
     52  1.16   mycroft #include <sys/vnode.h>
     53  1.31  christos #include <sys/signalvar.h>
     54  1.31  christos #include <sys/uio.h>
     55  1.33  christos #include <sys/conf.h>
     56  1.38   mycroft #include <sys/poll.h>
     57  1.31  christos 
     58  1.31  christos 
     59   1.1       cgd 
     60   1.1       cgd #if NPTY == 1
     61   1.1       cgd #undef NPTY
     62   1.1       cgd #define	NPTY	32		/* crude XXX */
     63   1.1       cgd #endif
     64   1.1       cgd 
     65  1.37   mycroft /* Macros to clear/set/test flags. */
     66  1.37   mycroft #define	SET(t, f)	(t) |= (f)
     67  1.37   mycroft #define	CLR(t, f)	(t) &= ~((unsigned)(f))
     68  1.37   mycroft #define	ISSET(t, f)	((t) & (f))
     69  1.37   mycroft 
     70   1.1       cgd #define BUFSIZ 100		/* Chunk size iomoved to/from user */
     71   1.1       cgd 
     72   1.1       cgd /*
     73   1.1       cgd  * pts == /dev/tty[pqrs]?
     74   1.1       cgd  * ptc == /dev/pty[pqrs]?
     75   1.1       cgd  */
     76  1.27   mycroft struct	pt_softc {
     77  1.27   mycroft 	struct	tty *pt_tty;
     78   1.1       cgd 	int	pt_flags;
     79  1.22       cgd 	struct	selinfo pt_selr, pt_selw;
     80   1.1       cgd 	u_char	pt_send;
     81   1.1       cgd 	u_char	pt_ucntl;
     82  1.27   mycroft } pt_softc[NPTY];		/* XXX */
     83   1.1       cgd int	npty = NPTY;		/* for pstat -t */
     84   1.1       cgd 
     85   1.1       cgd #define	PF_PKT		0x08		/* packet mode */
     86   1.1       cgd #define	PF_STOPPED	0x10		/* user told stopped */
     87   1.1       cgd #define	PF_REMOTE	0x20		/* remote and flow controlled input */
     88   1.1       cgd #define	PF_NOSTOP	0x40
     89   1.1       cgd #define PF_UCNTL	0x80		/* user control mode */
     90   1.1       cgd 
     91  1.31  christos void	ptyattach __P((int));
     92  1.31  christos void	ptcwakeup __P((struct tty *, int));
     93  1.31  christos struct tty *ptytty __P((dev_t));
     94  1.31  christos void	ptsstart __P((struct tty *));
     95  1.15   deraadt 
     96  1.22       cgd /*
     97  1.22       cgd  * Establish n (or default if n is 1) ptys in the system.
     98  1.22       cgd  */
     99  1.15   deraadt void
    100  1.15   deraadt ptyattach(n)
    101  1.15   deraadt 	int n;
    102  1.15   deraadt {
    103  1.22       cgd #ifdef notyet
    104  1.22       cgd #define	DEFAULT_NPTY	32
    105  1.22       cgd 
    106  1.22       cgd 	/* maybe should allow 0 => none? */
    107  1.22       cgd 	if (n <= 1)
    108  1.22       cgd 		n = DEFAULT_NPTY;
    109  1.27   mycroft 	pt_softc = malloc(n * sizeof(struct pt_softc), M_DEVBUF, M_WAITOK);
    110  1.22       cgd 	npty = n;
    111  1.22       cgd #endif
    112  1.15   deraadt }
    113   1.7    andrew 
    114   1.1       cgd /*ARGSUSED*/
    115  1.31  christos int
    116   1.1       cgd ptsopen(dev, flag, devtype, p)
    117   1.1       cgd 	dev_t dev;
    118   1.7    andrew 	int flag, devtype;
    119   1.1       cgd 	struct proc *p;
    120   1.1       cgd {
    121  1.27   mycroft 	struct pt_softc *pti;
    122   1.1       cgd 	register struct tty *tp;
    123   1.1       cgd 	int error;
    124   1.1       cgd 
    125  1.22       cgd 	if (minor(dev) >= npty)
    126   1.1       cgd 		return (ENXIO);
    127  1.27   mycroft 	pti = &pt_softc[minor(dev)];
    128  1.34       mrg 	if (!pti->pt_tty) {
    129  1.27   mycroft 		tp = pti->pt_tty = ttymalloc();
    130  1.34       mrg 		tty_attach(tp);
    131  1.34       mrg 	}
    132  1.28   mycroft 	else
    133  1.27   mycroft 		tp = pti->pt_tty;
    134  1.37   mycroft 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
    135   1.1       cgd 		ttychars(tp);		/* Set up default chars */
    136   1.1       cgd 		tp->t_iflag = TTYDEF_IFLAG;
    137   1.1       cgd 		tp->t_oflag = TTYDEF_OFLAG;
    138   1.1       cgd 		tp->t_lflag = TTYDEF_LFLAG;
    139   1.1       cgd 		tp->t_cflag = TTYDEF_CFLAG;
    140   1.1       cgd 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    141   1.1       cgd 		ttsetwater(tp);		/* would be done in xxparam() */
    142  1.37   mycroft 	} else if (ISSET(tp->t_state, TS_XCLUDE) && p->p_ucred->cr_uid != 0)
    143   1.1       cgd 		return (EBUSY);
    144   1.1       cgd 	if (tp->t_oproc)			/* Ctrlr still around. */
    145  1.37   mycroft 		SET(tp->t_state, TS_CARR_ON);
    146  1.40   mycroft 	if (!ISSET(flag, O_NONBLOCK))
    147  1.40   mycroft 		while (!ISSET(tp->t_state, TS_CARR_ON)) {
    148  1.40   mycroft 			tp->t_wopen++;
    149  1.40   mycroft 			error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
    150  1.40   mycroft 			    ttopen, 0);
    151  1.40   mycroft 			tp->t_wopen--;
    152  1.40   mycroft 			if (error)
    153  1.40   mycroft 				return (error);
    154  1.40   mycroft 		}
    155  1.22       cgd 	error = (*linesw[tp->t_line].l_open)(dev, tp);
    156   1.1       cgd 	ptcwakeup(tp, FREAD|FWRITE);
    157  1.22       cgd 	return (error);
    158   1.1       cgd }
    159   1.1       cgd 
    160  1.31  christos int
    161   1.1       cgd ptsclose(dev, flag, mode, p)
    162   1.1       cgd 	dev_t dev;
    163   1.1       cgd 	int flag, mode;
    164   1.1       cgd 	struct proc *p;
    165   1.1       cgd {
    166  1.27   mycroft 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    167  1.27   mycroft 	register struct tty *tp = pti->pt_tty;
    168  1.31  christos 	int error;
    169   1.1       cgd 
    170  1.31  christos 	error = (*linesw[tp->t_line].l_close)(tp, flag);
    171  1.31  christos 	error |= ttyclose(tp);
    172   1.1       cgd 	ptcwakeup(tp, FREAD|FWRITE);
    173  1.31  christos 	return (error);
    174   1.1       cgd }
    175   1.1       cgd 
    176  1.31  christos int
    177   1.1       cgd ptsread(dev, uio, flag)
    178   1.1       cgd 	dev_t dev;
    179   1.1       cgd 	struct uio *uio;
    180   1.7    andrew 	int flag;
    181   1.1       cgd {
    182   1.1       cgd 	struct proc *p = curproc;
    183  1.27   mycroft 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    184  1.27   mycroft 	register struct tty *tp = pti->pt_tty;
    185   1.1       cgd 	int error = 0;
    186   1.1       cgd 
    187   1.1       cgd again:
    188   1.1       cgd 	if (pti->pt_flags & PF_REMOTE) {
    189   1.1       cgd 		while (isbackground(p, tp)) {
    190   1.1       cgd 			if ((p->p_sigignore & sigmask(SIGTTIN)) ||
    191   1.1       cgd 			    (p->p_sigmask & sigmask(SIGTTIN)) ||
    192   1.1       cgd 			    p->p_pgrp->pg_jobc == 0 ||
    193  1.22       cgd 			    p->p_flag & P_PPWAIT)
    194   1.1       cgd 				return (EIO);
    195   1.1       cgd 			pgsignal(p->p_pgrp, SIGTTIN, 1);
    196  1.35        pk 			error = ttysleep(tp, (caddr_t)&lbolt,
    197  1.31  christos 					 TTIPRI | PCATCH, ttybg, 0);
    198  1.31  christos 			if (error)
    199   1.1       cgd 				return (error);
    200   1.1       cgd 		}
    201   1.8   mycroft 		if (tp->t_canq.c_cc == 0) {
    202   1.1       cgd 			if (flag & IO_NDELAY)
    203   1.1       cgd 				return (EWOULDBLOCK);
    204  1.31  christos 			error = ttysleep(tp, (caddr_t)&tp->t_canq,
    205  1.31  christos 					 TTIPRI | PCATCH, ttyin, 0);
    206  1.31  christos 			if (error)
    207   1.1       cgd 				return (error);
    208   1.1       cgd 			goto again;
    209   1.1       cgd 		}
    210   1.8   mycroft 		while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
    211   1.8   mycroft 			if (ureadc(getc(&tp->t_canq), uio) < 0) {
    212   1.1       cgd 				error = EFAULT;
    213   1.1       cgd 				break;
    214   1.1       cgd 			}
    215   1.8   mycroft 		if (tp->t_canq.c_cc == 1)
    216   1.8   mycroft 			(void) getc(&tp->t_canq);
    217   1.8   mycroft 		if (tp->t_canq.c_cc)
    218   1.1       cgd 			return (error);
    219   1.1       cgd 	} else
    220   1.1       cgd 		if (tp->t_oproc)
    221   1.1       cgd 			error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
    222   1.1       cgd 	ptcwakeup(tp, FWRITE);
    223   1.1       cgd 	return (error);
    224   1.1       cgd }
    225   1.1       cgd 
    226   1.1       cgd /*
    227   1.1       cgd  * Write to pseudo-tty.
    228   1.1       cgd  * Wakeups of controlling tty will happen
    229   1.1       cgd  * indirectly, when tty driver calls ptsstart.
    230   1.1       cgd  */
    231  1.31  christos int
    232   1.1       cgd ptswrite(dev, uio, flag)
    233   1.1       cgd 	dev_t dev;
    234   1.1       cgd 	struct uio *uio;
    235   1.7    andrew 	int flag;
    236   1.1       cgd {
    237  1.27   mycroft 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    238  1.27   mycroft 	register struct tty *tp = pti->pt_tty;
    239   1.1       cgd 
    240   1.1       cgd 	if (tp->t_oproc == 0)
    241   1.1       cgd 		return (EIO);
    242   1.1       cgd 	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
    243   1.1       cgd }
    244   1.1       cgd 
    245   1.1       cgd /*
    246   1.1       cgd  * Start output on pseudo-tty.
    247  1.38   mycroft  * Wake up process polling or sleeping for input from controlling tty.
    248   1.1       cgd  */
    249  1.12   deraadt void
    250   1.1       cgd ptsstart(tp)
    251   1.1       cgd 	struct tty *tp;
    252   1.1       cgd {
    253  1.27   mycroft 	register struct pt_softc *pti = &pt_softc[minor(tp->t_dev)];
    254   1.1       cgd 
    255  1.37   mycroft 	if (ISSET(tp->t_state, TS_TTSTOP))
    256  1.12   deraadt 		return;
    257   1.1       cgd 	if (pti->pt_flags & PF_STOPPED) {
    258   1.1       cgd 		pti->pt_flags &= ~PF_STOPPED;
    259   1.1       cgd 		pti->pt_send = TIOCPKT_START;
    260   1.1       cgd 	}
    261   1.1       cgd 	ptcwakeup(tp, FREAD);
    262   1.1       cgd }
    263   1.1       cgd 
    264  1.36   mycroft void
    265  1.31  christos ptsstop(tp, flush)
    266  1.31  christos 	register struct tty *tp;
    267  1.31  christos 	int flush;
    268  1.31  christos {
    269  1.31  christos 	struct pt_softc *pti = &pt_softc[minor(tp->t_dev)];
    270  1.31  christos 	int flag;
    271  1.31  christos 
    272  1.31  christos 	/* note: FLUSHREAD and FLUSHWRITE already ok */
    273  1.31  christos 	if (flush == 0) {
    274  1.31  christos 		flush = TIOCPKT_STOP;
    275  1.31  christos 		pti->pt_flags |= PF_STOPPED;
    276  1.31  christos 	} else
    277  1.31  christos 		pti->pt_flags &= ~PF_STOPPED;
    278  1.31  christos 	pti->pt_send |= flush;
    279  1.31  christos 	/* change of perspective */
    280  1.31  christos 	flag = 0;
    281  1.31  christos 	if (flush & FREAD)
    282  1.31  christos 		flag |= FWRITE;
    283  1.31  christos 	if (flush & FWRITE)
    284  1.31  christos 		flag |= FREAD;
    285  1.31  christos 	ptcwakeup(tp, flag);
    286  1.31  christos }
    287  1.31  christos 
    288  1.31  christos void
    289   1.1       cgd ptcwakeup(tp, flag)
    290   1.1       cgd 	struct tty *tp;
    291   1.7    andrew 	int flag;
    292   1.1       cgd {
    293  1.27   mycroft 	struct pt_softc *pti = &pt_softc[minor(tp->t_dev)];
    294   1.1       cgd 
    295   1.1       cgd 	if (flag & FREAD) {
    296   1.4       cgd 		selwakeup(&pti->pt_selr);
    297  1.22       cgd 		wakeup((caddr_t)&tp->t_outq.c_cf);
    298   1.1       cgd 	}
    299   1.1       cgd 	if (flag & FWRITE) {
    300   1.4       cgd 		selwakeup(&pti->pt_selw);
    301   1.8   mycroft 		wakeup((caddr_t)&tp->t_rawq.c_cf);
    302   1.1       cgd 	}
    303   1.1       cgd }
    304   1.1       cgd 
    305  1.26   mycroft int ptcopen __P((dev_t, int, int, struct proc *));
    306  1.25   mycroft 
    307   1.1       cgd /*ARGSUSED*/
    308  1.25   mycroft int
    309  1.26   mycroft ptcopen(dev, flag, devtype, p)
    310   1.1       cgd 	dev_t dev;
    311   1.1       cgd 	int flag, devtype;
    312   1.1       cgd 	struct proc *p;
    313   1.1       cgd {
    314  1.27   mycroft 	struct pt_softc *pti;
    315   1.1       cgd 	register struct tty *tp;
    316   1.1       cgd 
    317  1.22       cgd 	if (minor(dev) >= npty)
    318   1.1       cgd 		return (ENXIO);
    319  1.27   mycroft 	pti = &pt_softc[minor(dev)];
    320  1.34       mrg 	if (!pti->pt_tty) {
    321  1.27   mycroft 		tp = pti->pt_tty = ttymalloc();
    322  1.34       mrg 		tty_attach(tp);
    323  1.34       mrg 	}
    324  1.28   mycroft 	else
    325  1.27   mycroft 		tp = pti->pt_tty;
    326   1.1       cgd 	if (tp->t_oproc)
    327   1.1       cgd 		return (EIO);
    328   1.1       cgd 	tp->t_oproc = ptsstart;
    329   1.1       cgd 	(void)(*linesw[tp->t_line].l_modem)(tp, 1);
    330  1.37   mycroft 	CLR(tp->t_lflag, EXTPROC);
    331  1.22       cgd 	pti->pt_flags = 0;
    332   1.1       cgd 	pti->pt_send = 0;
    333   1.1       cgd 	pti->pt_ucntl = 0;
    334   1.1       cgd 	return (0);
    335   1.1       cgd }
    336   1.1       cgd 
    337  1.31  christos /*ARGSUSED*/
    338  1.25   mycroft int
    339  1.31  christos ptcclose(dev, flag, devtype, p)
    340   1.1       cgd 	dev_t dev;
    341  1.31  christos 	int flag, devtype;
    342  1.31  christos 	struct proc *p;
    343   1.1       cgd {
    344  1.27   mycroft 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    345  1.27   mycroft 	register struct tty *tp = pti->pt_tty;
    346   1.1       cgd 
    347   1.1       cgd 	(void)(*linesw[tp->t_line].l_modem)(tp, 0);
    348  1.37   mycroft 	CLR(tp->t_state, TS_CARR_ON);
    349   1.1       cgd 	tp->t_oproc = 0;		/* mark closed */
    350   1.2       cgd 	return (0);
    351   1.1       cgd }
    352   1.1       cgd 
    353  1.31  christos int
    354   1.1       cgd ptcread(dev, uio, flag)
    355   1.1       cgd 	dev_t dev;
    356   1.1       cgd 	struct uio *uio;
    357   1.7    andrew 	int flag;
    358   1.1       cgd {
    359  1.27   mycroft 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    360  1.27   mycroft 	register struct tty *tp = pti->pt_tty;
    361  1.22       cgd 	char buf[BUFSIZ];
    362   1.1       cgd 	int error = 0, cc;
    363   1.1       cgd 
    364   1.1       cgd 	/*
    365   1.1       cgd 	 * We want to block until the slave
    366   1.1       cgd 	 * is open, and there's something to read;
    367   1.1       cgd 	 * but if we lost the slave or we're NBIO,
    368   1.1       cgd 	 * then return the appropriate error instead.
    369   1.1       cgd 	 */
    370   1.1       cgd 	for (;;) {
    371  1.37   mycroft 		if (ISSET(tp->t_state, TS_ISOPEN)) {
    372   1.1       cgd 			if (pti->pt_flags&PF_PKT && pti->pt_send) {
    373   1.1       cgd 				error = ureadc((int)pti->pt_send, uio);
    374   1.1       cgd 				if (error)
    375   1.1       cgd 					return (error);
    376   1.1       cgd 				if (pti->pt_send & TIOCPKT_IOCTL) {
    377  1.22       cgd 					cc = min(uio->uio_resid,
    378   1.1       cgd 						sizeof(tp->t_termios));
    379  1.31  christos 					uiomove((caddr_t) &tp->t_termios,
    380  1.31  christos 						cc, uio);
    381   1.1       cgd 				}
    382   1.1       cgd 				pti->pt_send = 0;
    383   1.1       cgd 				return (0);
    384   1.1       cgd 			}
    385   1.1       cgd 			if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
    386   1.1       cgd 				error = ureadc((int)pti->pt_ucntl, uio);
    387   1.1       cgd 				if (error)
    388   1.1       cgd 					return (error);
    389   1.1       cgd 				pti->pt_ucntl = 0;
    390   1.1       cgd 				return (0);
    391   1.1       cgd 			}
    392  1.37   mycroft 			if (tp->t_outq.c_cc && !ISSET(tp->t_state, TS_TTSTOP))
    393   1.1       cgd 				break;
    394   1.1       cgd 		}
    395  1.37   mycroft 		if (!ISSET(tp->t_state, TS_CARR_ON))
    396   1.1       cgd 			return (0);	/* EOF */
    397   1.1       cgd 		if (flag & IO_NDELAY)
    398   1.1       cgd 			return (EWOULDBLOCK);
    399  1.31  christos 		error = tsleep((caddr_t)&tp->t_outq.c_cf, TTIPRI | PCATCH,
    400  1.31  christos 			       ttyin, 0);
    401  1.31  christos 		if (error)
    402   1.1       cgd 			return (error);
    403   1.1       cgd 	}
    404   1.1       cgd 	if (pti->pt_flags & (PF_PKT|PF_UCNTL))
    405   1.1       cgd 		error = ureadc(0, uio);
    406   1.1       cgd 	while (uio->uio_resid > 0 && error == 0) {
    407  1.22       cgd 		cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
    408   1.1       cgd 		if (cc <= 0)
    409   1.1       cgd 			break;
    410   1.1       cgd 		error = uiomove(buf, cc, uio);
    411   1.1       cgd 	}
    412   1.8   mycroft 	if (tp->t_outq.c_cc <= tp->t_lowat) {
    413  1.37   mycroft 		if (ISSET(tp->t_state, TS_ASLEEP)) {
    414  1.37   mycroft 			CLR(tp->t_state, TS_ASLEEP);
    415   1.8   mycroft 			wakeup((caddr_t)&tp->t_outq);
    416   1.1       cgd 		}
    417   1.4       cgd 		selwakeup(&tp->t_wsel);
    418   1.1       cgd 	}
    419   1.1       cgd 	return (error);
    420   1.1       cgd }
    421   1.1       cgd 
    422   1.1       cgd 
    423  1.31  christos int
    424   1.1       cgd ptcwrite(dev, uio, flag)
    425   1.1       cgd 	dev_t dev;
    426   1.1       cgd 	register struct uio *uio;
    427   1.7    andrew 	int flag;
    428   1.1       cgd {
    429  1.27   mycroft 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    430  1.27   mycroft 	register struct tty *tp = pti->pt_tty;
    431  1.31  christos 	register u_char *cp = NULL;
    432   1.1       cgd 	register int cc = 0;
    433   1.1       cgd 	u_char locbuf[BUFSIZ];
    434   1.1       cgd 	int cnt = 0;
    435   1.1       cgd 	int error = 0;
    436   1.1       cgd 
    437   1.1       cgd again:
    438  1.37   mycroft 	if (!ISSET(tp->t_state, TS_ISOPEN))
    439   1.1       cgd 		goto block;
    440   1.1       cgd 	if (pti->pt_flags & PF_REMOTE) {
    441   1.8   mycroft 		if (tp->t_canq.c_cc)
    442   1.1       cgd 			goto block;
    443   1.8   mycroft 		while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) {
    444   1.1       cgd 			if (cc == 0) {
    445   1.1       cgd 				cc = min(uio->uio_resid, BUFSIZ);
    446   1.8   mycroft 				cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
    447   1.1       cgd 				cp = locbuf;
    448   1.1       cgd 				error = uiomove((caddr_t)cp, cc, uio);
    449   1.1       cgd 				if (error)
    450   1.1       cgd 					return (error);
    451   1.1       cgd 				/* check again for safety */
    452  1.37   mycroft 				if (!ISSET(tp->t_state, TS_ISOPEN))
    453   1.1       cgd 					return (EIO);
    454   1.1       cgd 			}
    455   1.1       cgd 			if (cc)
    456  1.22       cgd 				(void) b_to_q((char *)cp, cc, &tp->t_canq);
    457   1.1       cgd 			cc = 0;
    458   1.1       cgd 		}
    459   1.8   mycroft 		(void) putc(0, &tp->t_canq);
    460   1.1       cgd 		ttwakeup(tp);
    461   1.8   mycroft 		wakeup((caddr_t)&tp->t_canq);
    462   1.1       cgd 		return (0);
    463   1.1       cgd 	}
    464   1.1       cgd 	while (uio->uio_resid > 0) {
    465   1.1       cgd 		if (cc == 0) {
    466   1.1       cgd 			cc = min(uio->uio_resid, BUFSIZ);
    467   1.1       cgd 			cp = locbuf;
    468   1.1       cgd 			error = uiomove((caddr_t)cp, cc, uio);
    469   1.1       cgd 			if (error)
    470   1.1       cgd 				return (error);
    471   1.1       cgd 			/* check again for safety */
    472  1.37   mycroft 			if (!ISSET(tp->t_state, TS_ISOPEN))
    473   1.1       cgd 				return (EIO);
    474   1.1       cgd 		}
    475   1.1       cgd 		while (cc > 0) {
    476   1.8   mycroft 			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
    477  1.37   mycroft 			   (tp->t_canq.c_cc > 0 || !ISSET(tp->t_iflag, ICANON))) {
    478   1.8   mycroft 				wakeup((caddr_t)&tp->t_rawq);
    479   1.1       cgd 				goto block;
    480   1.1       cgd 			}
    481   1.1       cgd 			(*linesw[tp->t_line].l_rint)(*cp++, tp);
    482   1.1       cgd 			cnt++;
    483   1.1       cgd 			cc--;
    484   1.1       cgd 		}
    485   1.1       cgd 		cc = 0;
    486   1.1       cgd 	}
    487   1.1       cgd 	return (0);
    488   1.1       cgd block:
    489   1.1       cgd 	/*
    490   1.1       cgd 	 * Come here to wait for slave to open, for space
    491   1.1       cgd 	 * in outq, or space in rawq.
    492   1.1       cgd 	 */
    493  1.37   mycroft 	if (!ISSET(tp->t_state, TS_CARR_ON))
    494   1.1       cgd 		return (EIO);
    495   1.1       cgd 	if (flag & IO_NDELAY) {
    496   1.1       cgd 		/* adjust for data copied in but not written */
    497   1.1       cgd 		uio->uio_resid += cc;
    498   1.1       cgd 		if (cnt == 0)
    499   1.1       cgd 			return (EWOULDBLOCK);
    500   1.1       cgd 		return (0);
    501   1.1       cgd 	}
    502  1.31  christos 	error = tsleep((caddr_t)&tp->t_rawq.c_cf, TTOPRI | PCATCH,
    503  1.31  christos 		       ttyout, 0);
    504  1.31  christos 	if (error) {
    505   1.1       cgd 		/* adjust for data copied in but not written */
    506   1.1       cgd 		uio->uio_resid += cc;
    507   1.1       cgd 		return (error);
    508   1.1       cgd 	}
    509   1.1       cgd 	goto again;
    510  1.29   mycroft }
    511  1.29   mycroft 
    512  1.31  christos int
    513  1.38   mycroft ptcpoll(dev, events, p)
    514  1.31  christos 	dev_t dev;
    515  1.38   mycroft 	int events;
    516  1.31  christos 	struct proc *p;
    517  1.31  christos {
    518  1.31  christos 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    519  1.31  christos 	register struct tty *tp = pti->pt_tty;
    520  1.38   mycroft 	int revents = 0;
    521  1.38   mycroft 	int s = splsoftclock();
    522  1.31  christos 
    523  1.38   mycroft 	if (events & (POLLIN | POLLRDNORM))
    524  1.37   mycroft 		if (ISSET(tp->t_state, TS_ISOPEN) &&
    525  1.38   mycroft 		    ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
    526  1.38   mycroft 		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
    527  1.38   mycroft 		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
    528  1.38   mycroft 			revents |= events & (POLLIN | POLLRDNORM);
    529  1.31  christos 
    530  1.38   mycroft 	if (events & (POLLOUT | POLLWRNORM))
    531  1.37   mycroft 		if (ISSET(tp->t_state, TS_ISOPEN) &&
    532  1.38   mycroft 		    ((pti->pt_flags & PF_REMOTE) ?
    533  1.38   mycroft 		     (tp->t_canq.c_cc == 0) :
    534  1.38   mycroft 		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
    535  1.38   mycroft 		      (tp->t_canq.c_cc == 0 && ISSET(tp->t_iflag, ICANON)))))
    536  1.38   mycroft 			revents |= events & (POLLOUT | POLLWRNORM);
    537  1.31  christos 
    538  1.38   mycroft 	if (events & POLLHUP)
    539  1.38   mycroft 		if (!ISSET(tp->t_state, TS_CARR_ON))
    540  1.38   mycroft 			revents |= POLLHUP;
    541  1.31  christos 
    542  1.38   mycroft 	if (revents == 0) {
    543  1.38   mycroft 		if (events & (POLLIN | POLLHUP | POLLRDNORM))
    544  1.38   mycroft 			selrecord(p, &pti->pt_selr);
    545  1.31  christos 
    546  1.38   mycroft 		if (events & (POLLOUT | POLLWRNORM))
    547  1.38   mycroft 			selrecord(p, &pti->pt_selw);
    548  1.31  christos 	}
    549  1.38   mycroft 
    550  1.38   mycroft 	splx(s);
    551  1.38   mycroft 	return (revents);
    552  1.31  christos }
    553  1.31  christos 
    554  1.31  christos 
    555  1.29   mycroft struct tty *
    556  1.29   mycroft ptytty(dev)
    557  1.29   mycroft 	dev_t dev;
    558  1.29   mycroft {
    559  1.29   mycroft 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    560  1.29   mycroft 	register struct tty *tp = pti->pt_tty;
    561  1.29   mycroft 
    562  1.29   mycroft 	return (tp);
    563   1.1       cgd }
    564   1.1       cgd 
    565   1.1       cgd /*ARGSUSED*/
    566  1.31  christos int
    567  1.18   mycroft ptyioctl(dev, cmd, data, flag, p)
    568  1.18   mycroft 	dev_t dev;
    569  1.24       cgd 	u_long cmd;
    570   1.1       cgd 	caddr_t data;
    571  1.18   mycroft 	int flag;
    572  1.18   mycroft 	struct proc *p;
    573   1.1       cgd {
    574  1.27   mycroft 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    575  1.27   mycroft 	register struct tty *tp = pti->pt_tty;
    576   1.1       cgd 	register u_char *cc = tp->t_cc;
    577   1.1       cgd 	int stop, error;
    578   1.1       cgd 
    579   1.1       cgd 	/*
    580   1.1       cgd 	 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
    581   1.1       cgd 	 * ttywflush(tp) will hang if there are characters in the outq.
    582   1.1       cgd 	 */
    583   1.1       cgd 	if (cmd == TIOCEXT) {
    584   1.1       cgd 		/*
    585   1.1       cgd 		 * When the EXTPROC bit is being toggled, we need
    586   1.1       cgd 		 * to send an TIOCPKT_IOCTL if the packet driver
    587   1.1       cgd 		 * is turned on.
    588   1.1       cgd 		 */
    589   1.1       cgd 		if (*(int *)data) {
    590   1.1       cgd 			if (pti->pt_flags & PF_PKT) {
    591   1.1       cgd 				pti->pt_send |= TIOCPKT_IOCTL;
    592   1.1       cgd 				ptcwakeup(tp, FREAD);
    593   1.1       cgd 			}
    594  1.37   mycroft 			SET(tp->t_lflag, EXTPROC);
    595   1.1       cgd 		} else {
    596  1.39      fvdl 			if (ISSET(tp->t_lflag, EXTPROC) &&
    597   1.1       cgd 			    (pti->pt_flags & PF_PKT)) {
    598   1.1       cgd 				pti->pt_send |= TIOCPKT_IOCTL;
    599   1.1       cgd 				ptcwakeup(tp, FREAD);
    600   1.1       cgd 			}
    601  1.37   mycroft 			CLR(tp->t_lflag, EXTPROC);
    602   1.1       cgd 		}
    603   1.1       cgd 		return(0);
    604   1.1       cgd 	} else
    605   1.1       cgd 	if (cdevsw[major(dev)].d_open == ptcopen)
    606   1.1       cgd 		switch (cmd) {
    607   1.1       cgd 
    608   1.1       cgd 		case TIOCGPGRP:
    609  1.35        pk #ifdef COMPAT_SUNOS
    610  1.35        pk 			{
    611   1.1       cgd 			/*
    612  1.35        pk 			 * I'm not sure about SunOS TIOCGPGRP semantics
    613  1.35        pk 			 * on PTYs, but it's something like this:
    614  1.35        pk 			 */
    615  1.35        pk 			extern struct emul emul_sunos;
    616  1.35        pk 			if (p->p_emul == &emul_sunos && tp->t_pgrp == 0)
    617  1.35        pk 				return (EIO);
    618  1.35        pk 			*(int *)data = tp->t_pgrp->pg_id;
    619  1.35        pk 			return (0);
    620  1.35        pk 			}
    621  1.35        pk #endif
    622  1.35        pk 			/*
    623  1.35        pk 			 * We avoid calling ttioctl on the controller since,
    624   1.1       cgd 			 * in that case, tp must be the controlling terminal.
    625   1.1       cgd 			 */
    626   1.1       cgd 			*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
    627   1.1       cgd 			return (0);
    628   1.1       cgd 
    629   1.1       cgd 		case TIOCPKT:
    630   1.1       cgd 			if (*(int *)data) {
    631   1.1       cgd 				if (pti->pt_flags & PF_UCNTL)
    632   1.1       cgd 					return (EINVAL);
    633   1.1       cgd 				pti->pt_flags |= PF_PKT;
    634   1.1       cgd 			} else
    635   1.1       cgd 				pti->pt_flags &= ~PF_PKT;
    636   1.1       cgd 			return (0);
    637   1.1       cgd 
    638   1.1       cgd 		case TIOCUCNTL:
    639   1.1       cgd 			if (*(int *)data) {
    640   1.1       cgd 				if (pti->pt_flags & PF_PKT)
    641   1.1       cgd 					return (EINVAL);
    642   1.1       cgd 				pti->pt_flags |= PF_UCNTL;
    643   1.1       cgd 			} else
    644   1.1       cgd 				pti->pt_flags &= ~PF_UCNTL;
    645   1.1       cgd 			return (0);
    646   1.1       cgd 
    647   1.1       cgd 		case TIOCREMOTE:
    648   1.1       cgd 			if (*(int *)data)
    649   1.1       cgd 				pti->pt_flags |= PF_REMOTE;
    650   1.1       cgd 			else
    651   1.1       cgd 				pti->pt_flags &= ~PF_REMOTE;
    652   1.1       cgd 			ttyflush(tp, FREAD|FWRITE);
    653   1.1       cgd 			return (0);
    654   1.1       cgd 
    655  1.32  christos #ifdef COMPAT_OLDTTY
    656  1.35        pk 		case TIOCSETP:
    657   1.1       cgd 		case TIOCSETN:
    658   1.2       cgd #endif
    659   1.1       cgd 		case TIOCSETD:
    660   1.1       cgd 		case TIOCSETA:
    661   1.1       cgd 		case TIOCSETAW:
    662   1.1       cgd 		case TIOCSETAF:
    663  1.21       cgd 			ndflush(&tp->t_outq, tp->t_outq.c_cc);
    664   1.1       cgd 			break;
    665   1.1       cgd 
    666   1.1       cgd 		case TIOCSIG:
    667   1.1       cgd 			if (*(unsigned int *)data >= NSIG)
    668   1.1       cgd 				return(EINVAL);
    669  1.37   mycroft 			if (!ISSET(tp->t_lflag, NOFLSH))
    670   1.1       cgd 				ttyflush(tp, FREAD|FWRITE);
    671   1.1       cgd 			pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
    672   1.1       cgd 			if ((*(unsigned int *)data == SIGINFO) &&
    673  1.37   mycroft 			    (!ISSET(tp->t_lflag, NOKERNINFO)))
    674   1.1       cgd 				ttyinfo(tp);
    675   1.1       cgd 			return(0);
    676   1.1       cgd 		}
    677  1.18   mycroft 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
    678   1.1       cgd 	if (error < 0)
    679  1.18   mycroft 		 error = ttioctl(tp, cmd, data, flag, p);
    680   1.1       cgd 	if (error < 0) {
    681   1.1       cgd 		if (pti->pt_flags & PF_UCNTL &&
    682   1.1       cgd 		    (cmd & ~0xff) == UIOCCMD(0)) {
    683   1.1       cgd 			if (cmd & 0xff) {
    684   1.1       cgd 				pti->pt_ucntl = (u_char)cmd;
    685   1.1       cgd 				ptcwakeup(tp, FREAD);
    686   1.1       cgd 			}
    687   1.1       cgd 			return (0);
    688   1.1       cgd 		}
    689   1.1       cgd 		error = ENOTTY;
    690   1.1       cgd 	}
    691   1.1       cgd 	/*
    692   1.1       cgd 	 * If external processing and packet mode send ioctl packet.
    693   1.1       cgd 	 */
    694  1.37   mycroft 	if (ISSET(tp->t_lflag, EXTPROC) && (pti->pt_flags & PF_PKT)) {
    695   1.1       cgd 		switch(cmd) {
    696   1.1       cgd 		case TIOCSETA:
    697   1.1       cgd 		case TIOCSETAW:
    698   1.1       cgd 		case TIOCSETAF:
    699  1.32  christos #ifdef COMPAT_OLDTTY
    700   1.1       cgd 		case TIOCSETP:
    701   1.1       cgd 		case TIOCSETN:
    702   1.1       cgd 		case TIOCSETC:
    703   1.1       cgd 		case TIOCSLTC:
    704   1.1       cgd 		case TIOCLBIS:
    705   1.1       cgd 		case TIOCLBIC:
    706   1.1       cgd 		case TIOCLSET:
    707   1.1       cgd #endif
    708   1.1       cgd 			pti->pt_send |= TIOCPKT_IOCTL;
    709  1.22       cgd 			ptcwakeup(tp, FREAD);
    710   1.1       cgd 		default:
    711   1.1       cgd 			break;
    712   1.1       cgd 		}
    713   1.1       cgd 	}
    714  1.37   mycroft 	stop = ISSET(tp->t_iflag, IXON) && CCEQ(cc[VSTOP], CTRL('s'))
    715   1.1       cgd 		&& CCEQ(cc[VSTART], CTRL('q'));
    716   1.1       cgd 	if (pti->pt_flags & PF_NOSTOP) {
    717   1.1       cgd 		if (stop) {
    718   1.1       cgd 			pti->pt_send &= ~TIOCPKT_NOSTOP;
    719   1.1       cgd 			pti->pt_send |= TIOCPKT_DOSTOP;
    720   1.1       cgd 			pti->pt_flags &= ~PF_NOSTOP;
    721   1.1       cgd 			ptcwakeup(tp, FREAD);
    722   1.1       cgd 		}
    723   1.1       cgd 	} else {
    724   1.1       cgd 		if (!stop) {
    725   1.1       cgd 			pti->pt_send &= ~TIOCPKT_DOSTOP;
    726   1.1       cgd 			pti->pt_send |= TIOCPKT_NOSTOP;
    727   1.1       cgd 			pti->pt_flags |= PF_NOSTOP;
    728   1.1       cgd 			ptcwakeup(tp, FREAD);
    729   1.1       cgd 		}
    730   1.1       cgd 	}
    731   1.1       cgd 	return (error);
    732   1.1       cgd }
    733