Home | History | Annotate | Line # | Download | only in kern
tty_pty.c revision 1.55.2.11
      1   1.55.2.8   nathanw /*	$NetBSD: tty_pty.c,v 1.55.2.11 2002/12/11 06:43:10 thorpej 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.55.2.3   nathanw 
     43   1.55.2.3   nathanw #include <sys/cdefs.h>
     44   1.55.2.8   nathanw __KERNEL_RCSID(0, "$NetBSD: tty_pty.c,v 1.55.2.11 2002/12/11 06:43:10 thorpej Exp $");
     45       1.41   thorpej 
     46       1.41   thorpej #include "opt_compat_sunos.h"
     47       1.41   thorpej 
     48       1.16   mycroft #include <sys/param.h>
     49       1.16   mycroft #include <sys/systm.h>
     50       1.16   mycroft #include <sys/ioctl.h>
     51       1.22       cgd #include <sys/proc.h>
     52       1.16   mycroft #include <sys/tty.h>
     53       1.16   mycroft #include <sys/file.h>
     54       1.16   mycroft #include <sys/uio.h>
     55       1.16   mycroft #include <sys/kernel.h>
     56       1.16   mycroft #include <sys/vnode.h>
     57       1.31  christos #include <sys/signalvar.h>
     58       1.31  christos #include <sys/uio.h>
     59       1.33  christos #include <sys/conf.h>
     60       1.38   mycroft #include <sys/poll.h>
     61       1.47  jdolecek #include <sys/malloc.h>
     62       1.31  christos 
     63       1.47  jdolecek #define	DEFAULT_NPTYS		16	/* default number of initial ptys */
     64   1.55.2.4   nathanw #define DEFAULT_MAXPTYS		992	/* default maximum number of ptys */
     65        1.1       cgd 
     66       1.37   mycroft /* Macros to clear/set/test flags. */
     67       1.37   mycroft #define	SET(t, f)	(t) |= (f)
     68       1.37   mycroft #define	CLR(t, f)	(t) &= ~((unsigned)(f))
     69       1.37   mycroft #define	ISSET(t, f)	((t) & (f))
     70       1.37   mycroft 
     71        1.1       cgd #define BUFSIZ 100		/* Chunk size iomoved to/from user */
     72        1.1       cgd 
     73        1.1       cgd /*
     74        1.1       cgd  * pts == /dev/tty[pqrs]?
     75        1.1       cgd  * ptc == /dev/pty[pqrs]?
     76        1.1       cgd  */
     77       1.27   mycroft struct	pt_softc {
     78       1.27   mycroft 	struct	tty *pt_tty;
     79        1.1       cgd 	int	pt_flags;
     80       1.22       cgd 	struct	selinfo pt_selr, pt_selw;
     81        1.1       cgd 	u_char	pt_send;
     82        1.1       cgd 	u_char	pt_ucntl;
     83       1.47  jdolecek };
     84       1.47  jdolecek 
     85       1.48  jdolecek static struct pt_softc **pt_softc = NULL;	/* pty array */
     86       1.48  jdolecek static int npty = 0;			/* for pstat -t */
     87       1.48  jdolecek static int maxptys = DEFAULT_MAXPTYS;	/* maximum number of ptys (sysctable) */
     88       1.48  jdolecek static struct simplelock pt_softc_mutex = SIMPLELOCK_INITIALIZER;
     89        1.1       cgd 
     90        1.1       cgd #define	PF_PKT		0x08		/* packet mode */
     91        1.1       cgd #define	PF_STOPPED	0x10		/* user told stopped */
     92        1.1       cgd #define	PF_REMOTE	0x20		/* remote and flow controlled input */
     93        1.1       cgd #define	PF_NOSTOP	0x40
     94        1.1       cgd #define PF_UCNTL	0x80		/* user control mode */
     95        1.1       cgd 
     96       1.31  christos void	ptyattach __P((int));
     97       1.31  christos void	ptcwakeup __P((struct tty *, int));
     98       1.31  christos void	ptsstart __P((struct tty *));
     99       1.48  jdolecek int	pty_maxptys __P((int, int));
    100       1.15   deraadt 
    101       1.47  jdolecek static struct pt_softc **ptyarralloc __P((int));
    102       1.47  jdolecek static int check_pty __P((dev_t));
    103       1.47  jdolecek 
    104   1.55.2.8   nathanw dev_type_open(ptcopen);
    105   1.55.2.8   nathanw dev_type_close(ptcclose);
    106   1.55.2.8   nathanw dev_type_read(ptcread);
    107   1.55.2.8   nathanw dev_type_write(ptcwrite);
    108   1.55.2.8   nathanw dev_type_poll(ptcpoll);
    109  1.55.2.10   nathanw dev_type_kqfilter(ptckqfilter);
    110   1.55.2.8   nathanw 
    111   1.55.2.8   nathanw dev_type_open(ptsopen);
    112   1.55.2.8   nathanw dev_type_close(ptsclose);
    113   1.55.2.8   nathanw dev_type_read(ptsread);
    114   1.55.2.8   nathanw dev_type_write(ptswrite);
    115   1.55.2.8   nathanw dev_type_stop(ptsstop);
    116   1.55.2.8   nathanw dev_type_poll(ptspoll);
    117   1.55.2.8   nathanw 
    118   1.55.2.8   nathanw dev_type_ioctl(ptyioctl);
    119   1.55.2.8   nathanw dev_type_tty(ptytty);
    120   1.55.2.8   nathanw 
    121   1.55.2.8   nathanw const struct cdevsw ptc_cdevsw = {
    122   1.55.2.8   nathanw 	ptcopen, ptcclose, ptcread, ptcwrite, ptyioctl,
    123  1.55.2.10   nathanw 	nullstop, ptytty, ptcpoll, nommap, ptckqfilter, D_TTY
    124   1.55.2.8   nathanw };
    125   1.55.2.8   nathanw 
    126   1.55.2.8   nathanw const struct cdevsw pts_cdevsw = {
    127   1.55.2.8   nathanw 	ptsopen, ptsclose, ptsread, ptswrite, ptyioctl,
    128  1.55.2.10   nathanw 	ptsstop, ptytty, ptspoll, nommap, ttykqfilter, D_TTY
    129   1.55.2.8   nathanw };
    130   1.55.2.8   nathanw 
    131   1.55.2.8   nathanw #if defined(pmax)
    132   1.55.2.8   nathanw const struct cdevsw ptc_ultrix_cdevsw = {
    133   1.55.2.8   nathanw 	ptcopen, ptcclose, ptcread, ptcwrite, ptyioctl,
    134  1.55.2.10   nathanw 	nullstop, ptytty, ptcpoll, nommap, ptckqfilter, D_TTY
    135   1.55.2.8   nathanw };
    136   1.55.2.8   nathanw 
    137   1.55.2.8   nathanw const struct cdevsw pts_ultrix_cdevsw = {
    138   1.55.2.8   nathanw 	ptsopen, ptsclose, ptsread, ptswrite, ptyioctl,
    139  1.55.2.10   nathanw 	ptsstop, ptytty, ptspoll, nommap, ttykqfilter, D_TTY
    140   1.55.2.8   nathanw };
    141   1.55.2.8   nathanw #endif /* defined(pmax) */
    142   1.55.2.8   nathanw 
    143       1.47  jdolecek /*
    144       1.48  jdolecek  * Allocate and zero array of nelem elements.
    145       1.47  jdolecek  */
    146       1.47  jdolecek static struct pt_softc **
    147       1.47  jdolecek ptyarralloc(nelem)
    148       1.47  jdolecek 	int nelem;
    149       1.47  jdolecek {
    150       1.47  jdolecek 	struct pt_softc **pt;
    151       1.47  jdolecek 	nelem += 10;
    152       1.47  jdolecek 	pt = malloc(nelem * sizeof(struct pt_softc *), M_DEVBUF, M_WAITOK);
    153       1.47  jdolecek 	memset(pt, '\0', nelem * sizeof(struct pt_softc *));
    154       1.47  jdolecek 	return pt;
    155       1.47  jdolecek }
    156       1.47  jdolecek 
    157       1.47  jdolecek /*
    158       1.47  jdolecek  * Check if the minor is correct and ensure necessary structures
    159       1.47  jdolecek  * are properly allocated.
    160       1.47  jdolecek  */
    161       1.47  jdolecek static int
    162       1.47  jdolecek check_pty(dev)
    163       1.47  jdolecek 	dev_t dev;
    164       1.47  jdolecek {
    165       1.47  jdolecek 	struct pt_softc *pti;
    166       1.47  jdolecek 
    167       1.47  jdolecek 	if (minor(dev) >= npty) {
    168       1.47  jdolecek 		struct pt_softc **newpt;
    169       1.48  jdolecek 		int newnpty;
    170       1.47  jdolecek 
    171       1.48  jdolecek 		/* check if the requested pty can be granted */
    172       1.48  jdolecek 		if (minor(dev) >= maxptys) {
    173       1.48  jdolecek 	    limit_reached:
    174       1.48  jdolecek 			tablefull("pty", "increase kern.maxptys");
    175       1.48  jdolecek 			return (ENXIO);
    176       1.48  jdolecek 		}
    177       1.47  jdolecek 
    178       1.47  jdolecek 		/*
    179       1.48  jdolecek 		 * Now grab the pty array mutex - we need to ensure
    180       1.47  jdolecek 		 * that the pty array is consistent while copying it's
    181       1.48  jdolecek 		 * content to newly allocated, larger space; we also
    182       1.48  jdolecek 		 * need to be safe against pty_maxptys().
    183       1.47  jdolecek 		 */
    184       1.48  jdolecek 		simple_lock(&pt_softc_mutex);
    185       1.48  jdolecek 
    186       1.48  jdolecek 		do {
    187       1.48  jdolecek 			for(newnpty = npty; newnpty <= minor(dev);
    188       1.48  jdolecek 				newnpty *= 2);
    189       1.48  jdolecek 
    190       1.48  jdolecek 			if (newnpty > maxptys)
    191       1.48  jdolecek 				newnpty = maxptys;
    192       1.48  jdolecek 
    193       1.48  jdolecek 			simple_unlock(&pt_softc_mutex);
    194       1.48  jdolecek 			newpt = ptyarralloc(newnpty);
    195       1.48  jdolecek 			simple_lock(&pt_softc_mutex);
    196       1.48  jdolecek 
    197       1.48  jdolecek 			if (maxptys == npty) {
    198       1.54     enami 				simple_unlock(&pt_softc_mutex);
    199       1.48  jdolecek 				goto limit_reached;
    200       1.48  jdolecek 			}
    201       1.48  jdolecek 		} while(newnpty > maxptys);
    202       1.47  jdolecek 
    203       1.47  jdolecek 		/*
    204       1.48  jdolecek 		 * If the pty array was not enlarged while we were waiting
    205       1.48  jdolecek 		 * for mutex, copy current contents of pt_softc[] to newly
    206       1.48  jdolecek 		 * allocated array and start using the new bigger array.
    207       1.47  jdolecek 		 */
    208       1.47  jdolecek 		if (minor(dev) >= npty) {
    209       1.47  jdolecek 			memcpy(newpt, pt_softc, npty*sizeof(struct pt_softc *));
    210       1.47  jdolecek 			free(pt_softc, M_DEVBUF);
    211       1.47  jdolecek 
    212       1.47  jdolecek 			pt_softc = newpt;
    213       1.47  jdolecek 			npty = newnpty;
    214       1.47  jdolecek 		} else {
    215       1.48  jdolecek 			/* was enlarged when waited fot lock, free new space */
    216       1.47  jdolecek 			free(newpt, M_DEVBUF);
    217       1.47  jdolecek 		}
    218       1.48  jdolecek 
    219       1.48  jdolecek 		simple_unlock(&pt_softc_mutex);
    220       1.47  jdolecek 	}
    221       1.47  jdolecek 
    222       1.47  jdolecek 	/*
    223       1.48  jdolecek 	 * If the entry is not yet allocated, allocate one. The mutex is
    224       1.48  jdolecek 	 * needed so that the state of pt_softc[] array is consistant
    225       1.48  jdolecek 	 * in case it has been longened above.
    226       1.47  jdolecek 	 */
    227       1.47  jdolecek 	if (!pt_softc[minor(dev)]) {
    228       1.48  jdolecek 		MALLOC(pti, struct pt_softc *, sizeof(struct pt_softc),
    229       1.48  jdolecek 			M_DEVBUF, M_WAITOK);
    230   1.55.2.9   nathanw 		memset(pti, 0, sizeof(struct pt_softc));
    231       1.48  jdolecek 
    232       1.48  jdolecek 	 	pti->pt_tty = ttymalloc();
    233       1.48  jdolecek 
    234       1.48  jdolecek 		simple_lock(&pt_softc_mutex);
    235       1.47  jdolecek 
    236       1.47  jdolecek 		/*
    237       1.48  jdolecek 		 * Check the entry again - it might have been
    238       1.48  jdolecek 		 * added while we were waiting for mutex.
    239       1.47  jdolecek 		 */
    240       1.47  jdolecek 		if (!pt_softc[minor(dev)]) {
    241       1.47  jdolecek 			tty_attach(pti->pt_tty);
    242       1.47  jdolecek 			pt_softc[minor(dev)] = pti;
    243       1.48  jdolecek 		} else {
    244       1.48  jdolecek 			ttyfree(pti->pt_tty);
    245       1.48  jdolecek 			FREE(pti, M_DEVBUF);
    246       1.47  jdolecek 		}
    247       1.48  jdolecek 
    248       1.48  jdolecek 		simple_unlock(&pt_softc_mutex);
    249       1.48  jdolecek 	}
    250       1.48  jdolecek 
    251       1.48  jdolecek 	return (0);
    252       1.48  jdolecek }
    253       1.48  jdolecek 
    254       1.48  jdolecek /*
    255       1.48  jdolecek  * Set maxpty in thread-safe way. Returns 0 in case of error, otherwise
    256       1.48  jdolecek  * new value of maxptys.
    257       1.48  jdolecek  */
    258       1.48  jdolecek int
    259       1.48  jdolecek pty_maxptys(newmax, set)
    260       1.48  jdolecek 	int newmax, set;
    261       1.48  jdolecek {
    262       1.48  jdolecek 	if (!set)
    263       1.48  jdolecek 		return (maxptys);
    264       1.48  jdolecek 
    265       1.48  jdolecek 	/* the value cannot be set to value lower than current number of ptys */
    266       1.48  jdolecek 	if (newmax < npty)
    267       1.48  jdolecek 		return (0);
    268       1.48  jdolecek 
    269       1.48  jdolecek 	/* can proceed immediatelly if bigger than current maximum */
    270       1.48  jdolecek 	if (newmax > maxptys) {
    271       1.48  jdolecek 		maxptys = newmax;
    272       1.48  jdolecek 		return (maxptys);
    273       1.47  jdolecek 	}
    274       1.47  jdolecek 
    275       1.48  jdolecek 	/*
    276       1.48  jdolecek 	 * We have to grab the pt_softc lock, so that we would pick correct
    277       1.48  jdolecek 	 * value of npty (might be modified in check_pty()).
    278       1.48  jdolecek 	 */
    279       1.48  jdolecek 	simple_lock(&pt_softc_mutex);
    280       1.48  jdolecek 
    281       1.48  jdolecek 	if (newmax > npty)
    282       1.48  jdolecek 		maxptys = newmax;
    283       1.47  jdolecek 
    284       1.48  jdolecek 	simple_unlock(&pt_softc_mutex);
    285       1.48  jdolecek 
    286       1.48  jdolecek 	return (maxptys);
    287       1.47  jdolecek }
    288       1.47  jdolecek 
    289       1.22       cgd /*
    290       1.22       cgd  * Establish n (or default if n is 1) ptys in the system.
    291       1.22       cgd  */
    292       1.15   deraadt void
    293       1.15   deraadt ptyattach(n)
    294       1.15   deraadt 	int n;
    295       1.15   deraadt {
    296       1.22       cgd 	/* maybe should allow 0 => none? */
    297       1.22       cgd 	if (n <= 1)
    298       1.47  jdolecek 		n = DEFAULT_NPTYS;
    299       1.47  jdolecek 	pt_softc = ptyarralloc(n);
    300       1.22       cgd 	npty = n;
    301       1.15   deraadt }
    302        1.7    andrew 
    303        1.1       cgd /*ARGSUSED*/
    304       1.31  christos int
    305        1.1       cgd ptsopen(dev, flag, devtype, p)
    306        1.1       cgd 	dev_t dev;
    307        1.7    andrew 	int flag, devtype;
    308        1.1       cgd 	struct proc *p;
    309        1.1       cgd {
    310       1.27   mycroft 	struct pt_softc *pti;
    311       1.43  augustss 	struct tty *tp;
    312        1.1       cgd 	int error;
    313        1.1       cgd 
    314       1.47  jdolecek 	if ((error = check_pty(dev)))
    315       1.47  jdolecek 		return (error);
    316       1.47  jdolecek 
    317       1.47  jdolecek 	pti = pt_softc[minor(dev)];
    318       1.47  jdolecek 	tp = pti->pt_tty;
    319       1.47  jdolecek 
    320       1.37   mycroft 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
    321        1.1       cgd 		ttychars(tp);		/* Set up default chars */
    322        1.1       cgd 		tp->t_iflag = TTYDEF_IFLAG;
    323        1.1       cgd 		tp->t_oflag = TTYDEF_OFLAG;
    324        1.1       cgd 		tp->t_lflag = TTYDEF_LFLAG;
    325        1.1       cgd 		tp->t_cflag = TTYDEF_CFLAG;
    326        1.1       cgd 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    327        1.1       cgd 		ttsetwater(tp);		/* would be done in xxparam() */
    328       1.37   mycroft 	} else if (ISSET(tp->t_state, TS_XCLUDE) && p->p_ucred->cr_uid != 0)
    329        1.1       cgd 		return (EBUSY);
    330        1.1       cgd 	if (tp->t_oproc)			/* Ctrlr still around. */
    331       1.37   mycroft 		SET(tp->t_state, TS_CARR_ON);
    332       1.40   mycroft 	if (!ISSET(flag, O_NONBLOCK))
    333       1.40   mycroft 		while (!ISSET(tp->t_state, TS_CARR_ON)) {
    334       1.40   mycroft 			tp->t_wopen++;
    335       1.40   mycroft 			error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
    336       1.40   mycroft 			    ttopen, 0);
    337       1.40   mycroft 			tp->t_wopen--;
    338       1.40   mycroft 			if (error)
    339       1.40   mycroft 				return (error);
    340       1.40   mycroft 		}
    341       1.50       eeh 	error = (*tp->t_linesw->l_open)(dev, tp);
    342        1.1       cgd 	ptcwakeup(tp, FREAD|FWRITE);
    343       1.22       cgd 	return (error);
    344        1.1       cgd }
    345        1.1       cgd 
    346       1.31  christos int
    347        1.1       cgd ptsclose(dev, flag, mode, p)
    348        1.1       cgd 	dev_t dev;
    349        1.1       cgd 	int flag, mode;
    350        1.1       cgd 	struct proc *p;
    351        1.1       cgd {
    352       1.47  jdolecek 	struct pt_softc *pti = pt_softc[minor(dev)];
    353       1.43  augustss 	struct tty *tp = pti->pt_tty;
    354       1.31  christos 	int error;
    355        1.1       cgd 
    356       1.50       eeh 	error = (*tp->t_linesw->l_close)(tp, flag);
    357       1.31  christos 	error |= ttyclose(tp);
    358        1.1       cgd 	ptcwakeup(tp, FREAD|FWRITE);
    359       1.31  christos 	return (error);
    360        1.1       cgd }
    361        1.1       cgd 
    362       1.31  christos int
    363        1.1       cgd ptsread(dev, uio, flag)
    364        1.1       cgd 	dev_t dev;
    365        1.1       cgd 	struct uio *uio;
    366        1.7    andrew 	int flag;
    367        1.1       cgd {
    368   1.55.2.6   nathanw 	struct proc *p = curproc;
    369       1.47  jdolecek 	struct pt_softc *pti = pt_softc[minor(dev)];
    370       1.43  augustss 	struct tty *tp = pti->pt_tty;
    371        1.1       cgd 	int error = 0;
    372        1.1       cgd 
    373        1.1       cgd again:
    374        1.1       cgd 	if (pti->pt_flags & PF_REMOTE) {
    375        1.1       cgd 		while (isbackground(p, tp)) {
    376       1.51  jdolecek 			if (sigismasked(p, SIGTTIN) ||
    377        1.1       cgd 			    p->p_pgrp->pg_jobc == 0 ||
    378       1.22       cgd 			    p->p_flag & P_PPWAIT)
    379        1.1       cgd 				return (EIO);
    380        1.1       cgd 			pgsignal(p->p_pgrp, SIGTTIN, 1);
    381       1.35        pk 			error = ttysleep(tp, (caddr_t)&lbolt,
    382       1.31  christos 					 TTIPRI | PCATCH, ttybg, 0);
    383       1.31  christos 			if (error)
    384        1.1       cgd 				return (error);
    385        1.1       cgd 		}
    386        1.8   mycroft 		if (tp->t_canq.c_cc == 0) {
    387        1.1       cgd 			if (flag & IO_NDELAY)
    388        1.1       cgd 				return (EWOULDBLOCK);
    389       1.31  christos 			error = ttysleep(tp, (caddr_t)&tp->t_canq,
    390       1.31  christos 					 TTIPRI | PCATCH, ttyin, 0);
    391       1.31  christos 			if (error)
    392        1.1       cgd 				return (error);
    393        1.1       cgd 			goto again;
    394        1.1       cgd 		}
    395        1.8   mycroft 		while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
    396        1.8   mycroft 			if (ureadc(getc(&tp->t_canq), uio) < 0) {
    397        1.1       cgd 				error = EFAULT;
    398        1.1       cgd 				break;
    399        1.1       cgd 			}
    400        1.8   mycroft 		if (tp->t_canq.c_cc == 1)
    401        1.8   mycroft 			(void) getc(&tp->t_canq);
    402        1.8   mycroft 		if (tp->t_canq.c_cc)
    403        1.1       cgd 			return (error);
    404        1.1       cgd 	} else
    405        1.1       cgd 		if (tp->t_oproc)
    406       1.50       eeh 			error = (*tp->t_linesw->l_read)(tp, uio, flag);
    407        1.1       cgd 	ptcwakeup(tp, FWRITE);
    408        1.1       cgd 	return (error);
    409        1.1       cgd }
    410        1.1       cgd 
    411        1.1       cgd /*
    412        1.1       cgd  * Write to pseudo-tty.
    413        1.1       cgd  * Wakeups of controlling tty will happen
    414        1.1       cgd  * indirectly, when tty driver calls ptsstart.
    415        1.1       cgd  */
    416       1.31  christos int
    417        1.1       cgd ptswrite(dev, uio, flag)
    418        1.1       cgd 	dev_t dev;
    419        1.1       cgd 	struct uio *uio;
    420        1.7    andrew 	int flag;
    421        1.1       cgd {
    422       1.47  jdolecek 	struct pt_softc *pti = pt_softc[minor(dev)];
    423       1.43  augustss 	struct tty *tp = pti->pt_tty;
    424        1.1       cgd 
    425        1.1       cgd 	if (tp->t_oproc == 0)
    426        1.1       cgd 		return (EIO);
    427       1.50       eeh 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    428   1.55.2.2   nathanw }
    429   1.55.2.2   nathanw 
    430   1.55.2.2   nathanw /*
    431   1.55.2.2   nathanw  * Poll pseudo-tty.
    432   1.55.2.2   nathanw  */
    433   1.55.2.2   nathanw int
    434   1.55.2.2   nathanw ptspoll(dev, events, p)
    435   1.55.2.2   nathanw 	dev_t dev;
    436   1.55.2.2   nathanw 	int events;
    437   1.55.2.2   nathanw 	struct proc *p;
    438   1.55.2.2   nathanw {
    439   1.55.2.2   nathanw 	struct pt_softc *pti = pt_softc[minor(dev)];
    440   1.55.2.2   nathanw 	struct tty *tp = pti->pt_tty;
    441   1.55.2.2   nathanw 
    442   1.55.2.2   nathanw 	if (tp->t_oproc == 0)
    443   1.55.2.2   nathanw 		return (EIO);
    444   1.55.2.2   nathanw 
    445   1.55.2.2   nathanw 	return ((*tp->t_linesw->l_poll)(tp, events, p));
    446        1.1       cgd }
    447        1.1       cgd 
    448        1.1       cgd /*
    449        1.1       cgd  * Start output on pseudo-tty.
    450       1.38   mycroft  * Wake up process polling or sleeping for input from controlling tty.
    451        1.1       cgd  */
    452       1.12   deraadt void
    453        1.1       cgd ptsstart(tp)
    454        1.1       cgd 	struct tty *tp;
    455        1.1       cgd {
    456       1.47  jdolecek 	struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
    457        1.1       cgd 
    458       1.37   mycroft 	if (ISSET(tp->t_state, TS_TTSTOP))
    459       1.12   deraadt 		return;
    460        1.1       cgd 	if (pti->pt_flags & PF_STOPPED) {
    461        1.1       cgd 		pti->pt_flags &= ~PF_STOPPED;
    462        1.1       cgd 		pti->pt_send = TIOCPKT_START;
    463        1.1       cgd 	}
    464        1.1       cgd 	ptcwakeup(tp, FREAD);
    465        1.1       cgd }
    466        1.1       cgd 
    467       1.36   mycroft void
    468       1.31  christos ptsstop(tp, flush)
    469       1.43  augustss 	struct tty *tp;
    470       1.31  christos 	int flush;
    471       1.31  christos {
    472       1.47  jdolecek 	struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
    473       1.31  christos 	int flag;
    474       1.31  christos 
    475       1.31  christos 	/* note: FLUSHREAD and FLUSHWRITE already ok */
    476       1.31  christos 	if (flush == 0) {
    477       1.31  christos 		flush = TIOCPKT_STOP;
    478       1.31  christos 		pti->pt_flags |= PF_STOPPED;
    479       1.31  christos 	} else
    480       1.31  christos 		pti->pt_flags &= ~PF_STOPPED;
    481       1.31  christos 	pti->pt_send |= flush;
    482       1.31  christos 	/* change of perspective */
    483       1.31  christos 	flag = 0;
    484       1.31  christos 	if (flush & FREAD)
    485       1.31  christos 		flag |= FWRITE;
    486       1.31  christos 	if (flush & FWRITE)
    487       1.31  christos 		flag |= FREAD;
    488       1.31  christos 	ptcwakeup(tp, flag);
    489       1.31  christos }
    490       1.31  christos 
    491       1.31  christos void
    492        1.1       cgd ptcwakeup(tp, flag)
    493        1.1       cgd 	struct tty *tp;
    494        1.7    andrew 	int flag;
    495        1.1       cgd {
    496       1.47  jdolecek 	struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
    497        1.1       cgd 
    498        1.1       cgd 	if (flag & FREAD) {
    499  1.55.2.10   nathanw 		selnotify(&pti->pt_selr, 0);
    500       1.22       cgd 		wakeup((caddr_t)&tp->t_outq.c_cf);
    501        1.1       cgd 	}
    502        1.1       cgd 	if (flag & FWRITE) {
    503  1.55.2.10   nathanw 		selnotify(&pti->pt_selw, 0);
    504        1.8   mycroft 		wakeup((caddr_t)&tp->t_rawq.c_cf);
    505        1.1       cgd 	}
    506        1.1       cgd }
    507        1.1       cgd 
    508        1.1       cgd /*ARGSUSED*/
    509       1.25   mycroft int
    510       1.26   mycroft ptcopen(dev, flag, devtype, p)
    511        1.1       cgd 	dev_t dev;
    512        1.1       cgd 	int flag, devtype;
    513        1.1       cgd 	struct proc *p;
    514        1.1       cgd {
    515       1.27   mycroft 	struct pt_softc *pti;
    516       1.43  augustss 	struct tty *tp;
    517       1.47  jdolecek 	int error;
    518       1.47  jdolecek 
    519       1.47  jdolecek 	if ((error = check_pty(dev)))
    520       1.47  jdolecek 		return (error);
    521       1.47  jdolecek 
    522       1.47  jdolecek 	pti = pt_softc[minor(dev)];
    523       1.47  jdolecek 	tp = pti->pt_tty;
    524        1.1       cgd 
    525        1.1       cgd 	if (tp->t_oproc)
    526        1.1       cgd 		return (EIO);
    527        1.1       cgd 	tp->t_oproc = ptsstart;
    528       1.50       eeh 	(void)(*tp->t_linesw->l_modem)(tp, 1);
    529       1.37   mycroft 	CLR(tp->t_lflag, EXTPROC);
    530       1.22       cgd 	pti->pt_flags = 0;
    531        1.1       cgd 	pti->pt_send = 0;
    532        1.1       cgd 	pti->pt_ucntl = 0;
    533        1.1       cgd 	return (0);
    534        1.1       cgd }
    535        1.1       cgd 
    536       1.31  christos /*ARGSUSED*/
    537       1.25   mycroft int
    538       1.31  christos ptcclose(dev, flag, devtype, p)
    539        1.1       cgd 	dev_t dev;
    540       1.31  christos 	int flag, devtype;
    541       1.31  christos 	struct proc *p;
    542        1.1       cgd {
    543       1.47  jdolecek 	struct pt_softc *pti = pt_softc[minor(dev)];
    544       1.43  augustss 	struct tty *tp = pti->pt_tty;
    545        1.1       cgd 
    546       1.50       eeh 	(void)(*tp->t_linesw->l_modem)(tp, 0);
    547       1.37   mycroft 	CLR(tp->t_state, TS_CARR_ON);
    548        1.1       cgd 	tp->t_oproc = 0;		/* mark closed */
    549        1.2       cgd 	return (0);
    550        1.1       cgd }
    551        1.1       cgd 
    552       1.31  christos int
    553        1.1       cgd ptcread(dev, uio, flag)
    554        1.1       cgd 	dev_t dev;
    555        1.1       cgd 	struct uio *uio;
    556        1.7    andrew 	int flag;
    557        1.1       cgd {
    558       1.47  jdolecek 	struct pt_softc *pti = pt_softc[minor(dev)];
    559       1.43  augustss 	struct tty *tp = pti->pt_tty;
    560       1.22       cgd 	char buf[BUFSIZ];
    561        1.1       cgd 	int error = 0, cc;
    562        1.1       cgd 
    563        1.1       cgd 	/*
    564        1.1       cgd 	 * We want to block until the slave
    565        1.1       cgd 	 * is open, and there's something to read;
    566        1.1       cgd 	 * but if we lost the slave or we're NBIO,
    567        1.1       cgd 	 * then return the appropriate error instead.
    568        1.1       cgd 	 */
    569        1.1       cgd 	for (;;) {
    570       1.37   mycroft 		if (ISSET(tp->t_state, TS_ISOPEN)) {
    571        1.1       cgd 			if (pti->pt_flags&PF_PKT && pti->pt_send) {
    572        1.1       cgd 				error = ureadc((int)pti->pt_send, uio);
    573        1.1       cgd 				if (error)
    574        1.1       cgd 					return (error);
    575        1.1       cgd 				if (pti->pt_send & TIOCPKT_IOCTL) {
    576       1.22       cgd 					cc = min(uio->uio_resid,
    577        1.1       cgd 						sizeof(tp->t_termios));
    578       1.31  christos 					uiomove((caddr_t) &tp->t_termios,
    579       1.31  christos 						cc, uio);
    580        1.1       cgd 				}
    581        1.1       cgd 				pti->pt_send = 0;
    582        1.1       cgd 				return (0);
    583        1.1       cgd 			}
    584        1.1       cgd 			if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
    585        1.1       cgd 				error = ureadc((int)pti->pt_ucntl, uio);
    586        1.1       cgd 				if (error)
    587        1.1       cgd 					return (error);
    588        1.1       cgd 				pti->pt_ucntl = 0;
    589        1.1       cgd 				return (0);
    590        1.1       cgd 			}
    591       1.37   mycroft 			if (tp->t_outq.c_cc && !ISSET(tp->t_state, TS_TTSTOP))
    592        1.1       cgd 				break;
    593        1.1       cgd 		}
    594       1.37   mycroft 		if (!ISSET(tp->t_state, TS_CARR_ON))
    595        1.1       cgd 			return (0);	/* EOF */
    596        1.1       cgd 		if (flag & IO_NDELAY)
    597        1.1       cgd 			return (EWOULDBLOCK);
    598       1.31  christos 		error = tsleep((caddr_t)&tp->t_outq.c_cf, TTIPRI | PCATCH,
    599       1.31  christos 			       ttyin, 0);
    600       1.31  christos 		if (error)
    601        1.1       cgd 			return (error);
    602        1.1       cgd 	}
    603        1.1       cgd 	if (pti->pt_flags & (PF_PKT|PF_UCNTL))
    604        1.1       cgd 		error = ureadc(0, uio);
    605        1.1       cgd 	while (uio->uio_resid > 0 && error == 0) {
    606       1.22       cgd 		cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
    607        1.1       cgd 		if (cc <= 0)
    608        1.1       cgd 			break;
    609        1.1       cgd 		error = uiomove(buf, cc, uio);
    610        1.1       cgd 	}
    611        1.8   mycroft 	if (tp->t_outq.c_cc <= tp->t_lowat) {
    612       1.37   mycroft 		if (ISSET(tp->t_state, TS_ASLEEP)) {
    613       1.37   mycroft 			CLR(tp->t_state, TS_ASLEEP);
    614        1.8   mycroft 			wakeup((caddr_t)&tp->t_outq);
    615        1.1       cgd 		}
    616  1.55.2.10   nathanw 		selnotify(&tp->t_wsel, 0);
    617        1.1       cgd 	}
    618        1.1       cgd 	return (error);
    619        1.1       cgd }
    620        1.1       cgd 
    621        1.1       cgd 
    622       1.31  christos int
    623        1.1       cgd ptcwrite(dev, uio, flag)
    624        1.1       cgd 	dev_t dev;
    625       1.43  augustss 	struct uio *uio;
    626        1.7    andrew 	int flag;
    627        1.1       cgd {
    628       1.47  jdolecek 	struct pt_softc *pti = pt_softc[minor(dev)];
    629       1.43  augustss 	struct tty *tp = pti->pt_tty;
    630       1.43  augustss 	u_char *cp = NULL;
    631       1.43  augustss 	int cc = 0;
    632        1.1       cgd 	u_char locbuf[BUFSIZ];
    633        1.1       cgd 	int cnt = 0;
    634        1.1       cgd 	int error = 0;
    635        1.1       cgd 
    636        1.1       cgd again:
    637       1.37   mycroft 	if (!ISSET(tp->t_state, TS_ISOPEN))
    638        1.1       cgd 		goto block;
    639        1.1       cgd 	if (pti->pt_flags & PF_REMOTE) {
    640        1.8   mycroft 		if (tp->t_canq.c_cc)
    641        1.1       cgd 			goto block;
    642        1.8   mycroft 		while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) {
    643        1.1       cgd 			if (cc == 0) {
    644        1.1       cgd 				cc = min(uio->uio_resid, BUFSIZ);
    645        1.8   mycroft 				cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
    646        1.1       cgd 				cp = locbuf;
    647        1.1       cgd 				error = uiomove((caddr_t)cp, cc, uio);
    648        1.1       cgd 				if (error)
    649        1.1       cgd 					return (error);
    650        1.1       cgd 				/* check again for safety */
    651       1.37   mycroft 				if (!ISSET(tp->t_state, TS_ISOPEN))
    652        1.1       cgd 					return (EIO);
    653        1.1       cgd 			}
    654        1.1       cgd 			if (cc)
    655       1.22       cgd 				(void) b_to_q((char *)cp, cc, &tp->t_canq);
    656        1.1       cgd 			cc = 0;
    657        1.1       cgd 		}
    658        1.8   mycroft 		(void) putc(0, &tp->t_canq);
    659        1.1       cgd 		ttwakeup(tp);
    660        1.8   mycroft 		wakeup((caddr_t)&tp->t_canq);
    661        1.1       cgd 		return (0);
    662        1.1       cgd 	}
    663        1.1       cgd 	while (uio->uio_resid > 0) {
    664        1.1       cgd 		if (cc == 0) {
    665        1.1       cgd 			cc = min(uio->uio_resid, BUFSIZ);
    666        1.1       cgd 			cp = locbuf;
    667        1.1       cgd 			error = uiomove((caddr_t)cp, cc, uio);
    668        1.1       cgd 			if (error)
    669        1.1       cgd 				return (error);
    670        1.1       cgd 			/* check again for safety */
    671       1.37   mycroft 			if (!ISSET(tp->t_state, TS_ISOPEN))
    672        1.1       cgd 				return (EIO);
    673        1.1       cgd 		}
    674        1.1       cgd 		while (cc > 0) {
    675        1.8   mycroft 			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
    676   1.55.2.4   nathanw 			   (tp->t_canq.c_cc > 0 || !ISSET(tp->t_lflag, ICANON))) {
    677        1.8   mycroft 				wakeup((caddr_t)&tp->t_rawq);
    678        1.1       cgd 				goto block;
    679        1.1       cgd 			}
    680       1.50       eeh 			(*tp->t_linesw->l_rint)(*cp++, tp);
    681        1.1       cgd 			cnt++;
    682        1.1       cgd 			cc--;
    683        1.1       cgd 		}
    684        1.1       cgd 		cc = 0;
    685        1.1       cgd 	}
    686        1.1       cgd 	return (0);
    687        1.1       cgd block:
    688        1.1       cgd 	/*
    689        1.1       cgd 	 * Come here to wait for slave to open, for space
    690        1.1       cgd 	 * in outq, or space in rawq.
    691        1.1       cgd 	 */
    692       1.37   mycroft 	if (!ISSET(tp->t_state, TS_CARR_ON))
    693        1.1       cgd 		return (EIO);
    694        1.1       cgd 	if (flag & IO_NDELAY) {
    695        1.1       cgd 		/* adjust for data copied in but not written */
    696        1.1       cgd 		uio->uio_resid += cc;
    697        1.1       cgd 		if (cnt == 0)
    698        1.1       cgd 			return (EWOULDBLOCK);
    699        1.1       cgd 		return (0);
    700        1.1       cgd 	}
    701       1.31  christos 	error = tsleep((caddr_t)&tp->t_rawq.c_cf, TTOPRI | PCATCH,
    702       1.31  christos 		       ttyout, 0);
    703       1.31  christos 	if (error) {
    704        1.1       cgd 		/* adjust for data copied in but not written */
    705        1.1       cgd 		uio->uio_resid += cc;
    706        1.1       cgd 		return (error);
    707        1.1       cgd 	}
    708        1.1       cgd 	goto again;
    709       1.29   mycroft }
    710       1.29   mycroft 
    711       1.31  christos int
    712       1.38   mycroft ptcpoll(dev, events, p)
    713       1.31  christos 	dev_t dev;
    714       1.38   mycroft 	int events;
    715       1.31  christos 	struct proc *p;
    716       1.31  christos {
    717       1.47  jdolecek 	struct pt_softc *pti = pt_softc[minor(dev)];
    718       1.43  augustss 	struct tty *tp = pti->pt_tty;
    719       1.38   mycroft 	int revents = 0;
    720       1.38   mycroft 	int s = splsoftclock();
    721       1.31  christos 
    722       1.38   mycroft 	if (events & (POLLIN | POLLRDNORM))
    723       1.37   mycroft 		if (ISSET(tp->t_state, TS_ISOPEN) &&
    724       1.38   mycroft 		    ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
    725       1.38   mycroft 		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
    726       1.38   mycroft 		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
    727       1.38   mycroft 			revents |= events & (POLLIN | POLLRDNORM);
    728       1.31  christos 
    729       1.38   mycroft 	if (events & (POLLOUT | POLLWRNORM))
    730       1.37   mycroft 		if (ISSET(tp->t_state, TS_ISOPEN) &&
    731       1.38   mycroft 		    ((pti->pt_flags & PF_REMOTE) ?
    732       1.38   mycroft 		     (tp->t_canq.c_cc == 0) :
    733       1.38   mycroft 		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
    734   1.55.2.4   nathanw 		      (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))))
    735       1.38   mycroft 			revents |= events & (POLLOUT | POLLWRNORM);
    736       1.31  christos 
    737       1.38   mycroft 	if (events & POLLHUP)
    738       1.38   mycroft 		if (!ISSET(tp->t_state, TS_CARR_ON))
    739       1.38   mycroft 			revents |= POLLHUP;
    740       1.31  christos 
    741       1.38   mycroft 	if (revents == 0) {
    742       1.38   mycroft 		if (events & (POLLIN | POLLHUP | POLLRDNORM))
    743       1.38   mycroft 			selrecord(p, &pti->pt_selr);
    744       1.31  christos 
    745       1.38   mycroft 		if (events & (POLLOUT | POLLWRNORM))
    746       1.38   mycroft 			selrecord(p, &pti->pt_selw);
    747       1.31  christos 	}
    748       1.38   mycroft 
    749       1.38   mycroft 	splx(s);
    750       1.38   mycroft 	return (revents);
    751       1.31  christos }
    752       1.31  christos 
    753  1.55.2.10   nathanw static void
    754  1.55.2.10   nathanw filt_ptcrdetach(struct knote *kn)
    755  1.55.2.10   nathanw {
    756  1.55.2.10   nathanw 	struct pt_softc *pti;
    757  1.55.2.10   nathanw 	int		s;
    758  1.55.2.10   nathanw 
    759  1.55.2.10   nathanw 	pti = kn->kn_hook;
    760  1.55.2.10   nathanw 	s = spltty();
    761  1.55.2.11   thorpej 	SLIST_REMOVE(&pti->pt_selr.sel_klist, kn, knote, kn_selnext);
    762  1.55.2.10   nathanw 	splx(s);
    763  1.55.2.10   nathanw }
    764  1.55.2.10   nathanw 
    765  1.55.2.10   nathanw static int
    766  1.55.2.10   nathanw filt_ptcread(struct knote *kn, long hint)
    767  1.55.2.10   nathanw {
    768  1.55.2.10   nathanw 	struct pt_softc *pti;
    769  1.55.2.10   nathanw 	struct tty	*tp;
    770  1.55.2.10   nathanw 	int canread;
    771  1.55.2.10   nathanw 
    772  1.55.2.10   nathanw 	pti = kn->kn_hook;
    773  1.55.2.10   nathanw 	tp = pti->pt_tty;
    774  1.55.2.10   nathanw 
    775  1.55.2.10   nathanw 	canread = (ISSET(tp->t_state, TS_ISOPEN) &&
    776  1.55.2.10   nathanw 		    ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
    777  1.55.2.10   nathanw 		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
    778  1.55.2.10   nathanw 		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)));
    779  1.55.2.10   nathanw 
    780  1.55.2.10   nathanw 	if (canread) {
    781  1.55.2.10   nathanw 		/*
    782  1.55.2.10   nathanw 		 * c_cc is number of characters after output post-processing;
    783  1.55.2.10   nathanw 		 * the amount of data actually read(2) depends on
    784  1.55.2.10   nathanw 		 * setting of input flags for the terminal.
    785  1.55.2.10   nathanw 		 */
    786  1.55.2.10   nathanw 		kn->kn_data = tp->t_outq.c_cc;
    787  1.55.2.10   nathanw 		if (((pti->pt_flags & PF_PKT) && pti->pt_send) ||
    788  1.55.2.10   nathanw 		    ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))
    789  1.55.2.10   nathanw 			kn->kn_data++;
    790  1.55.2.10   nathanw 	}
    791  1.55.2.10   nathanw 
    792  1.55.2.10   nathanw 	return (canread);
    793  1.55.2.10   nathanw }
    794  1.55.2.10   nathanw 
    795  1.55.2.10   nathanw static void
    796  1.55.2.10   nathanw filt_ptcwdetach(struct knote *kn)
    797  1.55.2.10   nathanw {
    798  1.55.2.10   nathanw 	struct pt_softc *pti;
    799  1.55.2.10   nathanw 	int		s;
    800  1.55.2.10   nathanw 
    801  1.55.2.10   nathanw 	pti = kn->kn_hook;
    802  1.55.2.10   nathanw 	s = spltty();
    803  1.55.2.11   thorpej 	SLIST_REMOVE(&pti->pt_selw.sel_klist, kn, knote, kn_selnext);
    804  1.55.2.10   nathanw 	splx(s);
    805  1.55.2.10   nathanw }
    806  1.55.2.10   nathanw 
    807  1.55.2.10   nathanw static int
    808  1.55.2.10   nathanw filt_ptcwrite(struct knote *kn, long hint)
    809  1.55.2.10   nathanw {
    810  1.55.2.10   nathanw 	struct pt_softc *pti;
    811  1.55.2.10   nathanw 	struct tty	*tp;
    812  1.55.2.10   nathanw 	int canwrite;
    813  1.55.2.10   nathanw 	int nwrite;
    814  1.55.2.10   nathanw 
    815  1.55.2.10   nathanw 	pti = kn->kn_hook;
    816  1.55.2.10   nathanw 	tp = pti->pt_tty;
    817  1.55.2.10   nathanw 
    818  1.55.2.10   nathanw 	canwrite = (ISSET(tp->t_state, TS_ISOPEN) &&
    819  1.55.2.10   nathanw 		    ((pti->pt_flags & PF_REMOTE) ?
    820  1.55.2.10   nathanw 		     (tp->t_canq.c_cc == 0) :
    821  1.55.2.10   nathanw 		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
    822  1.55.2.10   nathanw 		      (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))));
    823  1.55.2.10   nathanw 
    824  1.55.2.10   nathanw 	if (canwrite) {
    825  1.55.2.10   nathanw 		if (pti->pt_flags & PF_REMOTE)
    826  1.55.2.10   nathanw 			nwrite = tp->t_canq.c_cn;
    827  1.55.2.10   nathanw 		else {
    828  1.55.2.10   nathanw 			/* this is guaranteed to be > 0 due to above check */
    829  1.55.2.10   nathanw 			nwrite = tp->t_canq.c_cn
    830  1.55.2.10   nathanw 				- (tp->t_rawq.c_cc + tp->t_canq.c_cc);
    831  1.55.2.10   nathanw 		}
    832  1.55.2.10   nathanw 		kn->kn_data = nwrite;
    833  1.55.2.10   nathanw 	}
    834  1.55.2.10   nathanw 
    835  1.55.2.10   nathanw 	return (canwrite);
    836  1.55.2.10   nathanw }
    837  1.55.2.10   nathanw 
    838  1.55.2.10   nathanw static const struct filterops ptcread_filtops =
    839  1.55.2.10   nathanw 	{ 1, NULL, filt_ptcrdetach, filt_ptcread };
    840  1.55.2.10   nathanw static const struct filterops ptcwrite_filtops =
    841  1.55.2.10   nathanw 	{ 1, NULL, filt_ptcwdetach, filt_ptcwrite };
    842  1.55.2.10   nathanw 
    843  1.55.2.10   nathanw int
    844  1.55.2.10   nathanw ptckqfilter(dev_t dev, struct knote *kn)
    845  1.55.2.10   nathanw {
    846  1.55.2.10   nathanw 	struct pt_softc *pti = pt_softc[minor(dev)];
    847  1.55.2.10   nathanw 	struct klist	*klist;
    848  1.55.2.10   nathanw 	int		s;
    849  1.55.2.10   nathanw 
    850  1.55.2.10   nathanw 	switch (kn->kn_filter) {
    851  1.55.2.10   nathanw 	case EVFILT_READ:
    852  1.55.2.11   thorpej 		klist = &pti->pt_selr.sel_klist;
    853  1.55.2.10   nathanw 		kn->kn_fop = &ptcread_filtops;
    854  1.55.2.10   nathanw 		break;
    855  1.55.2.10   nathanw 	case EVFILT_WRITE:
    856  1.55.2.11   thorpej 		klist = &pti->pt_selw.sel_klist;
    857  1.55.2.10   nathanw 		kn->kn_fop = &ptcwrite_filtops;
    858  1.55.2.10   nathanw 		break;
    859  1.55.2.10   nathanw 	default:
    860  1.55.2.10   nathanw 		return (1);
    861  1.55.2.10   nathanw 	}
    862  1.55.2.10   nathanw 
    863  1.55.2.10   nathanw 	kn->kn_hook = pti;
    864  1.55.2.10   nathanw 
    865  1.55.2.10   nathanw 	s = spltty();
    866  1.55.2.10   nathanw 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    867  1.55.2.10   nathanw 	splx(s);
    868  1.55.2.10   nathanw 
    869  1.55.2.10   nathanw 	return (0);
    870  1.55.2.10   nathanw }
    871       1.31  christos 
    872       1.29   mycroft struct tty *
    873       1.29   mycroft ptytty(dev)
    874       1.29   mycroft 	dev_t dev;
    875       1.29   mycroft {
    876       1.47  jdolecek 	struct pt_softc *pti = pt_softc[minor(dev)];
    877       1.43  augustss 	struct tty *tp = pti->pt_tty;
    878       1.29   mycroft 
    879       1.29   mycroft 	return (tp);
    880        1.1       cgd }
    881        1.1       cgd 
    882        1.1       cgd /*ARGSUSED*/
    883       1.31  christos int
    884       1.18   mycroft ptyioctl(dev, cmd, data, flag, p)
    885       1.18   mycroft 	dev_t dev;
    886       1.24       cgd 	u_long cmd;
    887        1.1       cgd 	caddr_t data;
    888       1.18   mycroft 	int flag;
    889       1.18   mycroft 	struct proc *p;
    890        1.1       cgd {
    891       1.47  jdolecek 	struct pt_softc *pti = pt_softc[minor(dev)];
    892       1.43  augustss 	struct tty *tp = pti->pt_tty;
    893   1.55.2.8   nathanw 	const struct cdevsw *cdev;
    894       1.43  augustss 	u_char *cc = tp->t_cc;
    895       1.44      fvdl 	int stop, error, sig;
    896        1.1       cgd 
    897   1.55.2.8   nathanw 	cdev = cdevsw_lookup(dev);
    898        1.1       cgd 	/*
    899        1.1       cgd 	 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
    900        1.1       cgd 	 * ttywflush(tp) will hang if there are characters in the outq.
    901        1.1       cgd 	 */
    902        1.1       cgd 	if (cmd == TIOCEXT) {
    903        1.1       cgd 		/*
    904        1.1       cgd 		 * When the EXTPROC bit is being toggled, we need
    905        1.1       cgd 		 * to send an TIOCPKT_IOCTL if the packet driver
    906        1.1       cgd 		 * is turned on.
    907        1.1       cgd 		 */
    908        1.1       cgd 		if (*(int *)data) {
    909        1.1       cgd 			if (pti->pt_flags & PF_PKT) {
    910        1.1       cgd 				pti->pt_send |= TIOCPKT_IOCTL;
    911        1.1       cgd 				ptcwakeup(tp, FREAD);
    912        1.1       cgd 			}
    913       1.37   mycroft 			SET(tp->t_lflag, EXTPROC);
    914        1.1       cgd 		} else {
    915       1.39      fvdl 			if (ISSET(tp->t_lflag, EXTPROC) &&
    916        1.1       cgd 			    (pti->pt_flags & PF_PKT)) {
    917        1.1       cgd 				pti->pt_send |= TIOCPKT_IOCTL;
    918        1.1       cgd 				ptcwakeup(tp, FREAD);
    919        1.1       cgd 			}
    920       1.37   mycroft 			CLR(tp->t_lflag, EXTPROC);
    921        1.1       cgd 		}
    922        1.1       cgd 		return(0);
    923        1.1       cgd 	} else
    924   1.55.2.8   nathanw 	if (cdev != NULL && cdev->d_open == ptcopen)
    925        1.1       cgd 		switch (cmd) {
    926        1.1       cgd 
    927        1.1       cgd 		case TIOCGPGRP:
    928       1.35        pk 			/*
    929       1.35        pk 			 * We avoid calling ttioctl on the controller since,
    930        1.1       cgd 			 * in that case, tp must be the controlling terminal.
    931        1.1       cgd 			 */
    932        1.1       cgd 			*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
    933        1.1       cgd 			return (0);
    934        1.1       cgd 
    935        1.1       cgd 		case TIOCPKT:
    936        1.1       cgd 			if (*(int *)data) {
    937        1.1       cgd 				if (pti->pt_flags & PF_UCNTL)
    938        1.1       cgd 					return (EINVAL);
    939        1.1       cgd 				pti->pt_flags |= PF_PKT;
    940        1.1       cgd 			} else
    941        1.1       cgd 				pti->pt_flags &= ~PF_PKT;
    942        1.1       cgd 			return (0);
    943        1.1       cgd 
    944        1.1       cgd 		case TIOCUCNTL:
    945        1.1       cgd 			if (*(int *)data) {
    946        1.1       cgd 				if (pti->pt_flags & PF_PKT)
    947        1.1       cgd 					return (EINVAL);
    948        1.1       cgd 				pti->pt_flags |= PF_UCNTL;
    949        1.1       cgd 			} else
    950        1.1       cgd 				pti->pt_flags &= ~PF_UCNTL;
    951        1.1       cgd 			return (0);
    952        1.1       cgd 
    953        1.1       cgd 		case TIOCREMOTE:
    954        1.1       cgd 			if (*(int *)data)
    955        1.1       cgd 				pti->pt_flags |= PF_REMOTE;
    956        1.1       cgd 			else
    957        1.1       cgd 				pti->pt_flags &= ~PF_REMOTE;
    958        1.1       cgd 			ttyflush(tp, FREAD|FWRITE);
    959        1.1       cgd 			return (0);
    960        1.1       cgd 
    961       1.32  christos #ifdef COMPAT_OLDTTY
    962       1.35        pk 		case TIOCSETP:
    963        1.1       cgd 		case TIOCSETN:
    964        1.2       cgd #endif
    965        1.1       cgd 		case TIOCSETD:
    966        1.1       cgd 		case TIOCSETA:
    967        1.1       cgd 		case TIOCSETAW:
    968        1.1       cgd 		case TIOCSETAF:
    969       1.21       cgd 			ndflush(&tp->t_outq, tp->t_outq.c_cc);
    970        1.1       cgd 			break;
    971        1.1       cgd 
    972        1.1       cgd 		case TIOCSIG:
    973       1.46       mrg 			sig = (int)(long)*(caddr_t *)data;
    974       1.44      fvdl 			if (sig <= 0 || sig >= NSIG)
    975       1.44      fvdl 				return (EINVAL);
    976       1.37   mycroft 			if (!ISSET(tp->t_lflag, NOFLSH))
    977        1.1       cgd 				ttyflush(tp, FREAD|FWRITE);
    978       1.44      fvdl 			if ((sig == SIGINFO) &&
    979       1.37   mycroft 			    (!ISSET(tp->t_lflag, NOKERNINFO)))
    980        1.1       cgd 				ttyinfo(tp);
    981   1.55.2.5   nathanw 			pgsignal(tp->t_pgrp, sig, 1);
    982        1.1       cgd 			return(0);
    983        1.1       cgd 		}
    984       1.50       eeh 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
    985   1.55.2.5   nathanw 	if (error == EPASSTHROUGH)
    986       1.18   mycroft 		 error = ttioctl(tp, cmd, data, flag, p);
    987   1.55.2.5   nathanw 	if (error == EPASSTHROUGH) {
    988        1.1       cgd 		if (pti->pt_flags & PF_UCNTL &&
    989        1.1       cgd 		    (cmd & ~0xff) == UIOCCMD(0)) {
    990        1.1       cgd 			if (cmd & 0xff) {
    991        1.1       cgd 				pti->pt_ucntl = (u_char)cmd;
    992        1.1       cgd 				ptcwakeup(tp, FREAD);
    993        1.1       cgd 			}
    994        1.1       cgd 			return (0);
    995        1.1       cgd 		}
    996        1.1       cgd 	}
    997        1.1       cgd 	/*
    998        1.1       cgd 	 * If external processing and packet mode send ioctl packet.
    999        1.1       cgd 	 */
   1000       1.37   mycroft 	if (ISSET(tp->t_lflag, EXTPROC) && (pti->pt_flags & PF_PKT)) {
   1001        1.1       cgd 		switch(cmd) {
   1002        1.1       cgd 		case TIOCSETA:
   1003        1.1       cgd 		case TIOCSETAW:
   1004        1.1       cgd 		case TIOCSETAF:
   1005       1.32  christos #ifdef COMPAT_OLDTTY
   1006        1.1       cgd 		case TIOCSETP:
   1007        1.1       cgd 		case TIOCSETN:
   1008        1.1       cgd 		case TIOCSETC:
   1009        1.1       cgd 		case TIOCSLTC:
   1010        1.1       cgd 		case TIOCLBIS:
   1011        1.1       cgd 		case TIOCLBIC:
   1012        1.1       cgd 		case TIOCLSET:
   1013        1.1       cgd #endif
   1014        1.1       cgd 			pti->pt_send |= TIOCPKT_IOCTL;
   1015       1.22       cgd 			ptcwakeup(tp, FREAD);
   1016        1.1       cgd 		default:
   1017        1.1       cgd 			break;
   1018        1.1       cgd 		}
   1019        1.1       cgd 	}
   1020       1.37   mycroft 	stop = ISSET(tp->t_iflag, IXON) && CCEQ(cc[VSTOP], CTRL('s'))
   1021        1.1       cgd 		&& CCEQ(cc[VSTART], CTRL('q'));
   1022        1.1       cgd 	if (pti->pt_flags & PF_NOSTOP) {
   1023        1.1       cgd 		if (stop) {
   1024        1.1       cgd 			pti->pt_send &= ~TIOCPKT_NOSTOP;
   1025        1.1       cgd 			pti->pt_send |= TIOCPKT_DOSTOP;
   1026        1.1       cgd 			pti->pt_flags &= ~PF_NOSTOP;
   1027        1.1       cgd 			ptcwakeup(tp, FREAD);
   1028        1.1       cgd 		}
   1029        1.1       cgd 	} else {
   1030        1.1       cgd 		if (!stop) {
   1031        1.1       cgd 			pti->pt_send &= ~TIOCPKT_DOSTOP;
   1032        1.1       cgd 			pti->pt_send |= TIOCPKT_NOSTOP;
   1033        1.1       cgd 			pti->pt_flags |= PF_NOSTOP;
   1034        1.1       cgd 			ptcwakeup(tp, FREAD);
   1035        1.1       cgd 		}
   1036        1.1       cgd 	}
   1037        1.1       cgd 	return (error);
   1038        1.1       cgd }
   1039