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