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