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