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