Home | History | Annotate | Line # | Download | only in kern
tty_pty.c revision 1.112.2.3
      1  1.112.2.3     skrll /*	$NetBSD: tty_pty.c,v 1.112.2.3 2009/04/28 07:37:01 skrll 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.72       agc  * 3. Neither the name of the University nor the names of its contributors
     16        1.1       cgd  *    may be used to endorse or promote products derived from this software
     17        1.1       cgd  *    without specific prior written permission.
     18        1.1       cgd  *
     19        1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20        1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21        1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22        1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23        1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24        1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25        1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26        1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27        1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28        1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29        1.1       cgd  * SUCH DAMAGE.
     30        1.1       cgd  *
     31       1.39      fvdl  *	@(#)tty_pty.c	8.4 (Berkeley) 2/20/95
     32        1.1       cgd  */
     33        1.1       cgd 
     34        1.1       cgd /*
     35        1.1       cgd  * Pseudo-teletype Driver
     36        1.1       cgd  * (Actually two drivers, requiring two entries in 'cdevsw')
     37        1.1       cgd  */
     38       1.57     lukem 
     39       1.57     lukem #include <sys/cdefs.h>
     40  1.112.2.3     skrll __KERNEL_RCSID(0, "$NetBSD: tty_pty.c,v 1.112.2.3 2009/04/28 07:37:01 skrll Exp $");
     41       1.41   thorpej 
     42       1.80  christos #include "opt_ptm.h"
     43       1.41   thorpej 
     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.112.2.1     skrll #include <sys/ioctl_compat.h>
     48       1.22       cgd #include <sys/proc.h>
     49       1.16   mycroft #include <sys/tty.h>
     50       1.77  christos #include <sys/stat.h>
     51       1.16   mycroft #include <sys/file.h>
     52       1.16   mycroft #include <sys/kernel.h>
     53       1.16   mycroft #include <sys/vnode.h>
     54       1.77  christos #include <sys/namei.h>
     55       1.31  christos #include <sys/signalvar.h>
     56       1.31  christos #include <sys/uio.h>
     57       1.77  christos #include <sys/filedesc.h>
     58       1.33  christos #include <sys/conf.h>
     59       1.38   mycroft #include <sys/poll.h>
     60       1.81  christos #include <sys/pty.h>
     61       1.89      elad #include <sys/kauth.h>
     62       1.31  christos 
     63       1.47  jdolecek #define	DEFAULT_NPTYS		16	/* default number of initial ptys */
     64       1.58       tls #define DEFAULT_MAXPTYS		992	/* default maximum number of ptys */
     65        1.1       cgd 
     66        1.1       cgd #define BUFSIZ 100		/* Chunk size iomoved to/from user */
     67        1.1       cgd 
     68       1.27   mycroft struct	pt_softc {
     69       1.27   mycroft 	struct	tty *pt_tty;
     70        1.1       cgd 	int	pt_flags;
     71       1.22       cgd 	struct	selinfo pt_selr, pt_selw;
     72        1.1       cgd 	u_char	pt_send;
     73        1.1       cgd 	u_char	pt_ucntl;
     74       1.47  jdolecek };
     75       1.47  jdolecek 
     76       1.48  jdolecek static struct pt_softc **pt_softc = NULL;	/* pty array */
     77       1.48  jdolecek static int maxptys = DEFAULT_MAXPTYS;	/* maximum number of ptys (sysctable) */
     78      1.100        ad kmutex_t pt_softc_mutex;
     79       1.81  christos int npty = 0;			/* for pstat -t */
     80        1.1       cgd 
     81        1.1       cgd #define	PF_PKT		0x08		/* packet mode */
     82        1.1       cgd #define	PF_STOPPED	0x10		/* user told stopped */
     83        1.1       cgd #define	PF_REMOTE	0x20		/* remote and flow controlled input */
     84        1.1       cgd #define	PF_NOSTOP	0x40
     85        1.1       cgd #define PF_UCNTL	0x80		/* user control mode */
     86        1.1       cgd 
     87       1.76  junyoung void	ptyattach(int);
     88       1.76  junyoung void	ptcwakeup(struct tty *, int);
     89       1.76  junyoung void	ptsstart(struct tty *);
     90       1.76  junyoung int	pty_maxptys(int, int);
     91       1.15   deraadt 
     92       1.76  junyoung static struct pt_softc **ptyarralloc(int);
     93       1.77  christos 
     94       1.63   gehenna dev_type_open(ptcopen);
     95       1.63   gehenna dev_type_close(ptcclose);
     96       1.63   gehenna dev_type_read(ptcread);
     97       1.63   gehenna dev_type_write(ptcwrite);
     98       1.63   gehenna dev_type_poll(ptcpoll);
     99       1.65  jdolecek dev_type_kqfilter(ptckqfilter);
    100       1.63   gehenna 
    101       1.63   gehenna dev_type_open(ptsopen);
    102       1.63   gehenna dev_type_close(ptsclose);
    103       1.63   gehenna dev_type_read(ptsread);
    104       1.63   gehenna dev_type_write(ptswrite);
    105       1.63   gehenna dev_type_stop(ptsstop);
    106       1.63   gehenna dev_type_poll(ptspoll);
    107       1.63   gehenna 
    108       1.63   gehenna dev_type_ioctl(ptyioctl);
    109       1.63   gehenna dev_type_tty(ptytty);
    110       1.63   gehenna 
    111       1.63   gehenna const struct cdevsw ptc_cdevsw = {
    112       1.63   gehenna 	ptcopen, ptcclose, ptcread, ptcwrite, ptyioctl,
    113       1.65  jdolecek 	nullstop, ptytty, ptcpoll, nommap, ptckqfilter, D_TTY
    114       1.63   gehenna };
    115       1.63   gehenna 
    116       1.63   gehenna const struct cdevsw pts_cdevsw = {
    117       1.63   gehenna 	ptsopen, ptsclose, ptsread, ptswrite, ptyioctl,
    118       1.65  jdolecek 	ptsstop, ptytty, ptspoll, nommap, ttykqfilter, D_TTY
    119       1.63   gehenna };
    120       1.63   gehenna 
    121       1.63   gehenna #if defined(pmax)
    122       1.63   gehenna const struct cdevsw ptc_ultrix_cdevsw = {
    123       1.63   gehenna 	ptcopen, ptcclose, ptcread, ptcwrite, ptyioctl,
    124       1.65  jdolecek 	nullstop, ptytty, ptcpoll, nommap, ptckqfilter, D_TTY
    125       1.63   gehenna };
    126       1.63   gehenna 
    127       1.63   gehenna const struct cdevsw pts_ultrix_cdevsw = {
    128       1.63   gehenna 	ptsopen, ptsclose, ptsread, ptswrite, ptyioctl,
    129       1.65  jdolecek 	ptsstop, ptytty, ptspoll, nommap, ttykqfilter, D_TTY
    130       1.63   gehenna };
    131       1.63   gehenna #endif /* defined(pmax) */
    132       1.63   gehenna 
    133       1.47  jdolecek /*
    134       1.81  christos  * Check if a pty is free to use.
    135       1.81  christos  */
    136       1.81  christos int
    137       1.81  christos pty_isfree(int minor, int lock)
    138       1.81  christos {
    139       1.81  christos 	struct pt_softc *pt = pt_softc[minor];
    140       1.81  christos 	if (lock)
    141      1.100        ad 		mutex_enter(&pt_softc_mutex);
    142       1.81  christos 	minor = pt == NULL || pt->pt_tty == NULL ||
    143       1.81  christos 	    pt->pt_tty->t_oproc == NULL;
    144       1.81  christos 	if (lock)
    145      1.100        ad 		mutex_exit(&pt_softc_mutex);
    146       1.81  christos 	return minor;
    147       1.81  christos }
    148       1.81  christos 
    149       1.81  christos /*
    150       1.48  jdolecek  * Allocate and zero array of nelem elements.
    151       1.47  jdolecek  */
    152       1.47  jdolecek static struct pt_softc **
    153  1.112.2.2     skrll ptyarralloc(int nelem)
    154       1.47  jdolecek {
    155       1.47  jdolecek 	struct pt_softc **pt;
    156       1.47  jdolecek 	nelem += 10;
    157  1.112.2.2     skrll 	pt = kmem_zalloc(nelem * sizeof(*pt), KM_SLEEP);
    158       1.47  jdolecek 	return pt;
    159       1.47  jdolecek }
    160       1.47  jdolecek 
    161  1.112.2.2     skrll static void
    162  1.112.2.2     skrll ptyarrfree(struct pt_softc **pt, int nelem)
    163  1.112.2.2     skrll {
    164  1.112.2.2     skrll 
    165  1.112.2.2     skrll 	nelem += 10;
    166  1.112.2.2     skrll 	kmem_free(pt, nelem * sizeof(*pt));
    167  1.112.2.2     skrll }
    168  1.112.2.2     skrll 
    169       1.47  jdolecek /*
    170       1.47  jdolecek  * Check if the minor is correct and ensure necessary structures
    171       1.47  jdolecek  * are properly allocated.
    172       1.47  jdolecek  */
    173       1.81  christos int
    174       1.81  christos pty_check(int ptn)
    175       1.47  jdolecek {
    176       1.47  jdolecek 	struct pt_softc *pti;
    177       1.47  jdolecek 
    178       1.71       dsl 	if (ptn >= npty) {
    179       1.71       dsl 		struct pt_softc **newpt, **oldpt;
    180       1.48  jdolecek 		int newnpty;
    181  1.112.2.2     skrll 		int oldnpty;
    182       1.47  jdolecek 
    183       1.48  jdolecek 		/* check if the requested pty can be granted */
    184       1.71       dsl 		if (ptn >= maxptys) {
    185       1.48  jdolecek 	    limit_reached:
    186       1.48  jdolecek 			tablefull("pty", "increase kern.maxptys");
    187       1.48  jdolecek 			return (ENXIO);
    188       1.48  jdolecek 		}
    189       1.47  jdolecek 
    190       1.71       dsl 		/* Allocate a larger pty array */
    191       1.71       dsl 		for (newnpty = npty; newnpty <= ptn;)
    192       1.71       dsl 			newnpty *= 2;
    193       1.71       dsl 		if (newnpty > maxptys)
    194       1.71       dsl 			newnpty = maxptys;
    195       1.71       dsl 		newpt = ptyarralloc(newnpty);
    196       1.71       dsl 
    197       1.47  jdolecek 		/*
    198       1.48  jdolecek 		 * Now grab the pty array mutex - we need to ensure
    199       1.47  jdolecek 		 * that the pty array is consistent while copying it's
    200       1.48  jdolecek 		 * content to newly allocated, larger space; we also
    201       1.48  jdolecek 		 * need to be safe against pty_maxptys().
    202       1.47  jdolecek 		 */
    203      1.100        ad 		mutex_enter(&pt_softc_mutex);
    204       1.48  jdolecek 
    205       1.71       dsl 		if (newnpty >= maxptys) {
    206       1.71       dsl 			/* limit cut away beneath us... */
    207  1.112.2.2     skrll 			if (ptn >= maxptys) {
    208      1.100        ad 				mutex_exit(&pt_softc_mutex);
    209  1.112.2.2     skrll 				ptyarrfree(newpt, newnpty);
    210       1.48  jdolecek 				goto limit_reached;
    211       1.48  jdolecek 			}
    212  1.112.2.2     skrll 			newnpty = maxptys;
    213       1.71       dsl 		}
    214       1.47  jdolecek 
    215       1.47  jdolecek 		/*
    216       1.48  jdolecek 		 * If the pty array was not enlarged while we were waiting
    217       1.48  jdolecek 		 * for mutex, copy current contents of pt_softc[] to newly
    218       1.48  jdolecek 		 * allocated array and start using the new bigger array.
    219       1.47  jdolecek 		 */
    220       1.71       dsl 		if (newnpty > npty) {
    221       1.47  jdolecek 			memcpy(newpt, pt_softc, npty*sizeof(struct pt_softc *));
    222       1.71       dsl 			oldpt = pt_softc;
    223  1.112.2.2     skrll 			oldnpty = npty;
    224       1.47  jdolecek 			pt_softc = newpt;
    225       1.47  jdolecek 			npty = newnpty;
    226       1.47  jdolecek 		} else {
    227       1.71       dsl 			/* was enlarged when waited for lock, free new space */
    228       1.71       dsl 			oldpt = newpt;
    229  1.112.2.2     skrll 			oldnpty = newnpty;
    230       1.47  jdolecek 		}
    231       1.48  jdolecek 
    232      1.100        ad 		mutex_exit(&pt_softc_mutex);
    233  1.112.2.2     skrll 		ptyarrfree(oldpt, oldnpty);
    234       1.47  jdolecek 	}
    235       1.71       dsl 
    236       1.47  jdolecek 	/*
    237       1.48  jdolecek 	 * If the entry is not yet allocated, allocate one. The mutex is
    238       1.48  jdolecek 	 * needed so that the state of pt_softc[] array is consistant
    239       1.71       dsl 	 * in case it has been lengthened above.
    240       1.47  jdolecek 	 */
    241       1.71       dsl 	if (!pt_softc[ptn]) {
    242  1.112.2.2     skrll 		pti = kmem_zalloc(sizeof(*pti), KM_SLEEP);
    243       1.48  jdolecek 
    244      1.108     rmind 		selinit(&pti->pt_selr);
    245      1.108     rmind 		selinit(&pti->pt_selw);
    246      1.108     rmind 		pti->pt_tty = ttymalloc();
    247       1.48  jdolecek 
    248      1.100        ad 		mutex_enter(&pt_softc_mutex);
    249       1.47  jdolecek 
    250       1.47  jdolecek 		/*
    251       1.48  jdolecek 		 * Check the entry again - it might have been
    252       1.48  jdolecek 		 * added while we were waiting for mutex.
    253       1.47  jdolecek 		 */
    254      1.108     rmind 		if (pt_softc[ptn]) {
    255      1.108     rmind 			mutex_exit(&pt_softc_mutex);
    256       1.48  jdolecek 			ttyfree(pti->pt_tty);
    257      1.108     rmind 			seldestroy(&pti->pt_selr);
    258      1.108     rmind 			seldestroy(&pti->pt_selw);
    259  1.112.2.2     skrll 			kmem_free(pti, sizeof(*pti));
    260      1.108     rmind 			return (0);
    261       1.47  jdolecek 		}
    262      1.108     rmind 		tty_attach(pti->pt_tty);
    263      1.108     rmind 		pt_softc[ptn] = pti;
    264       1.48  jdolecek 
    265      1.100        ad 		mutex_exit(&pt_softc_mutex);
    266       1.48  jdolecek 	}
    267       1.48  jdolecek 
    268       1.48  jdolecek 	return (0);
    269       1.48  jdolecek }
    270       1.48  jdolecek 
    271       1.48  jdolecek /*
    272       1.48  jdolecek  * Set maxpty in thread-safe way. Returns 0 in case of error, otherwise
    273       1.48  jdolecek  * new value of maxptys.
    274       1.48  jdolecek  */
    275       1.48  jdolecek int
    276  1.112.2.3     skrll pty_maxptys(int newmax, int set)
    277       1.48  jdolecek {
    278       1.48  jdolecek 	if (!set)
    279       1.48  jdolecek 		return (maxptys);
    280       1.48  jdolecek 
    281       1.48  jdolecek 	/*
    282       1.48  jdolecek 	 * We have to grab the pt_softc lock, so that we would pick correct
    283       1.81  christos 	 * value of npty (might be modified in pty_check()).
    284       1.48  jdolecek 	 */
    285      1.100        ad 	mutex_enter(&pt_softc_mutex);
    286       1.48  jdolecek 
    287       1.71       dsl 	/*
    288       1.71       dsl 	 * The value cannot be set to value lower than the highest pty
    289       1.71       dsl 	 * number ever allocated.
    290       1.71       dsl 	 */
    291       1.71       dsl 	if (newmax >= npty)
    292       1.48  jdolecek 		maxptys = newmax;
    293       1.71       dsl 	else
    294       1.71       dsl 		newmax = 0;
    295       1.47  jdolecek 
    296      1.100        ad 	mutex_exit(&pt_softc_mutex);
    297       1.48  jdolecek 
    298       1.71       dsl 	return newmax;
    299       1.47  jdolecek }
    300       1.47  jdolecek 
    301       1.22       cgd /*
    302       1.22       cgd  * Establish n (or default if n is 1) ptys in the system.
    303       1.22       cgd  */
    304       1.15   deraadt void
    305  1.112.2.3     skrll ptyattach(int n)
    306       1.15   deraadt {
    307      1.100        ad 
    308      1.100        ad 	mutex_init(&pt_softc_mutex, MUTEX_DEFAULT, IPL_NONE);
    309      1.100        ad 
    310       1.22       cgd 	/* maybe should allow 0 => none? */
    311       1.22       cgd 	if (n <= 1)
    312       1.47  jdolecek 		n = DEFAULT_NPTYS;
    313       1.47  jdolecek 	pt_softc = ptyarralloc(n);
    314       1.22       cgd 	npty = n;
    315       1.80  christos #ifndef NO_DEV_PTM
    316       1.80  christos 	ptmattach(1);
    317       1.80  christos #endif
    318       1.15   deraadt }
    319        1.7    andrew 
    320        1.1       cgd /*ARGSUSED*/
    321       1.31  christos int
    322       1.96      yamt ptsopen(dev_t dev, int flag, int devtype, struct lwp *l)
    323        1.1       cgd {
    324       1.27   mycroft 	struct pt_softc *pti;
    325       1.43  augustss 	struct tty *tp;
    326        1.1       cgd 	int error;
    327       1.71       dsl 	int ptn = minor(dev);
    328        1.1       cgd 
    329       1.81  christos 	if ((error = pty_check(ptn)) != 0)
    330       1.47  jdolecek 		return (error);
    331       1.47  jdolecek 
    332      1.104        ad 	mutex_spin_enter(&tty_lock);
    333       1.71       dsl 	pti = pt_softc[ptn];
    334       1.47  jdolecek 	tp = pti->pt_tty;
    335       1.37   mycroft 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
    336        1.1       cgd 		ttychars(tp);		/* Set up default chars */
    337        1.1       cgd 		tp->t_iflag = TTYDEF_IFLAG;
    338        1.1       cgd 		tp->t_oflag = TTYDEF_OFLAG;
    339        1.1       cgd 		tp->t_lflag = TTYDEF_LFLAG;
    340        1.1       cgd 		tp->t_cflag = TTYDEF_CFLAG;
    341        1.1       cgd 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    342        1.1       cgd 		ttsetwater(tp);		/* would be done in xxparam() */
    343       1.97      elad 	} else if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN,
    344      1.104        ad 	    tp) != 0) {
    345      1.104        ad 		mutex_spin_exit(&tty_lock);
    346        1.1       cgd 		return (EBUSY);
    347      1.104        ad 	}
    348        1.1       cgd 	if (tp->t_oproc)			/* Ctrlr still around. */
    349       1.37   mycroft 		SET(tp->t_state, TS_CARR_ON);
    350       1.68        pk 	if (!ISSET(flag, O_NONBLOCK)) {
    351       1.40   mycroft 		while (!ISSET(tp->t_state, TS_CARR_ON)) {
    352       1.40   mycroft 			tp->t_wopen++;
    353      1.110        ad 			error = ttysleep(tp, &tp->t_rawcv, true, 0);
    354       1.40   mycroft 			tp->t_wopen--;
    355       1.68        pk 			if (error) {
    356      1.102        ad 				mutex_spin_exit(&tty_lock);
    357       1.40   mycroft 				return (error);
    358       1.68        pk 			}
    359       1.40   mycroft 		}
    360       1.68        pk 	}
    361      1.104        ad 	mutex_spin_exit(&tty_lock);
    362       1.50       eeh 	error = (*tp->t_linesw->l_open)(dev, tp);
    363        1.1       cgd 	ptcwakeup(tp, FREAD|FWRITE);
    364       1.22       cgd 	return (error);
    365        1.1       cgd }
    366        1.1       cgd 
    367       1.31  christos int
    368       1.96      yamt ptsclose(dev_t dev, int flag, int mode, struct lwp *l)
    369        1.1       cgd {
    370       1.47  jdolecek 	struct pt_softc *pti = pt_softc[minor(dev)];
    371       1.43  augustss 	struct tty *tp = pti->pt_tty;
    372       1.31  christos 	int error;
    373        1.1       cgd 
    374       1.50       eeh 	error = (*tp->t_linesw->l_close)(tp, flag);
    375       1.31  christos 	error |= ttyclose(tp);
    376        1.1       cgd 	ptcwakeup(tp, FREAD|FWRITE);
    377       1.31  christos 	return (error);
    378        1.1       cgd }
    379        1.1       cgd 
    380       1.31  christos int
    381  1.112.2.3     skrll ptsread(dev_t dev, struct uio *uio, int flag)
    382        1.1       cgd {
    383        1.1       cgd 	struct proc *p = curproc;
    384       1.47  jdolecek 	struct pt_softc *pti = pt_softc[minor(dev)];
    385       1.43  augustss 	struct tty *tp = pti->pt_tty;
    386        1.1       cgd 	int error = 0;
    387      1.104        ad 	int cc, c;
    388        1.1       cgd 
    389        1.1       cgd again:
    390        1.1       cgd 	if (pti->pt_flags & PF_REMOTE) {
    391      1.104        ad 		mutex_spin_enter(&tty_lock);
    392       1.98        ad 		while (isbackground(p, tp)) {	/* XXXSMP */
    393       1.98        ad 			if (sigismasked(curlwp, SIGTTIN) ||
    394        1.1       cgd 			    p->p_pgrp->pg_jobc == 0 ||
    395      1.111        ad 			    p->p_lflag & PL_PPWAIT) {
    396      1.104        ad 				mutex_spin_exit(&tty_lock);
    397        1.1       cgd 				return (EIO);
    398      1.104        ad 			}
    399      1.102        ad 			ttysig(tp, TTYSIG_PG1, SIGTTIN);
    400      1.102        ad 			error = ttysleep(tp, &lbolt, true, 0);
    401      1.104        ad 			if (error) {
    402      1.104        ad 				mutex_spin_exit(&tty_lock);
    403        1.1       cgd 				return (error);
    404      1.104        ad 			}
    405        1.1       cgd 		}
    406        1.8   mycroft 		if (tp->t_canq.c_cc == 0) {
    407       1.68        pk 			if (flag & IO_NDELAY) {
    408      1.102        ad 				mutex_spin_exit(&tty_lock);
    409        1.1       cgd 				return (EWOULDBLOCK);
    410       1.68        pk 			}
    411      1.110        ad 			error = ttysleep(tp, &tp->t_cancv, true, 0);
    412      1.102        ad 			mutex_spin_exit(&tty_lock);
    413       1.31  christos 			if (error)
    414        1.1       cgd 				return (error);
    415        1.1       cgd 			goto again;
    416        1.1       cgd 		}
    417       1.68        pk 		while(error == 0 && tp->t_canq.c_cc > 1 && uio->uio_resid > 0) {
    418      1.104        ad 			c = getc(&tp->t_canq);
    419      1.102        ad 			mutex_spin_exit(&tty_lock);
    420      1.104        ad 			error = ureadc(c, uio);
    421      1.102        ad 			mutex_spin_enter(&tty_lock);
    422       1.68        pk 			/* Re-check terminal state here? */
    423       1.68        pk 		}
    424        1.8   mycroft 		if (tp->t_canq.c_cc == 1)
    425        1.8   mycroft 			(void) getc(&tp->t_canq);
    426       1.68        pk 		cc = tp->t_canq.c_cc;
    427      1.102        ad 		mutex_spin_exit(&tty_lock);
    428       1.68        pk 		if (cc)
    429        1.1       cgd 			return (error);
    430      1.104        ad 	} else if (tp->t_oproc)
    431      1.104        ad 		error = (*tp->t_linesw->l_read)(tp, uio, flag);
    432        1.1       cgd 	ptcwakeup(tp, FWRITE);
    433        1.1       cgd 	return (error);
    434        1.1       cgd }
    435        1.1       cgd 
    436        1.1       cgd /*
    437        1.1       cgd  * Write to pseudo-tty.
    438        1.1       cgd  * Wakeups of controlling tty will happen
    439        1.1       cgd  * indirectly, when tty driver calls ptsstart.
    440        1.1       cgd  */
    441       1.31  christos int
    442  1.112.2.3     skrll ptswrite(dev_t dev, struct uio *uio, int flag)
    443        1.1       cgd {
    444       1.47  jdolecek 	struct pt_softc *pti = pt_softc[minor(dev)];
    445       1.43  augustss 	struct tty *tp = pti->pt_tty;
    446        1.1       cgd 
    447        1.1       cgd 	if (tp->t_oproc == 0)
    448        1.1       cgd 		return (EIO);
    449       1.50       eeh 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    450       1.56       scw }
    451       1.56       scw 
    452       1.56       scw /*
    453       1.56       scw  * Poll pseudo-tty.
    454       1.56       scw  */
    455       1.56       scw int
    456  1.112.2.3     skrll ptspoll(dev_t dev, int events, struct lwp *l)
    457       1.56       scw {
    458       1.56       scw 	struct pt_softc *pti = pt_softc[minor(dev)];
    459       1.56       scw 	struct tty *tp = pti->pt_tty;
    460       1.56       scw 
    461       1.56       scw 	if (tp->t_oproc == 0)
    462       1.85        ws 		return (POLLHUP);
    463       1.83     perry 
    464       1.86  christos 	return ((*tp->t_linesw->l_poll)(tp, events, l));
    465        1.1       cgd }
    466        1.1       cgd 
    467        1.1       cgd /*
    468        1.1       cgd  * Start output on pseudo-tty.
    469       1.38   mycroft  * Wake up process polling or sleeping for input from controlling tty.
    470        1.1       cgd  */
    471       1.12   deraadt void
    472  1.112.2.3     skrll ptsstart(struct tty *tp)
    473        1.1       cgd {
    474       1.47  jdolecek 	struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
    475        1.1       cgd 
    476      1.104        ad 	KASSERT(mutex_owned(&tty_lock));
    477      1.104        ad 
    478       1.37   mycroft 	if (ISSET(tp->t_state, TS_TTSTOP))
    479       1.12   deraadt 		return;
    480        1.1       cgd 	if (pti->pt_flags & PF_STOPPED) {
    481        1.1       cgd 		pti->pt_flags &= ~PF_STOPPED;
    482        1.1       cgd 		pti->pt_send = TIOCPKT_START;
    483        1.1       cgd 	}
    484       1.68        pk 
    485      1.108     rmind 	selnotify(&pti->pt_selr, 0, NOTE_SUBMIT);
    486      1.110        ad 	cv_broadcast(&tp->t_outcvf);
    487        1.1       cgd }
    488        1.1       cgd 
    489       1.68        pk /*
    490       1.68        pk  * Stop output.
    491       1.68        pk  */
    492       1.36   mycroft void
    493  1.112.2.3     skrll ptsstop(struct tty *tp, int flush)
    494       1.31  christos {
    495       1.47  jdolecek 	struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
    496       1.31  christos 
    497      1.104        ad 	KASSERT(mutex_owned(&tty_lock));
    498      1.104        ad 
    499       1.31  christos 	/* note: FLUSHREAD and FLUSHWRITE already ok */
    500       1.31  christos 	if (flush == 0) {
    501       1.31  christos 		flush = TIOCPKT_STOP;
    502       1.31  christos 		pti->pt_flags |= PF_STOPPED;
    503       1.31  christos 	} else
    504       1.31  christos 		pti->pt_flags &= ~PF_STOPPED;
    505       1.31  christos 	pti->pt_send |= flush;
    506       1.68        pk 
    507       1.31  christos 	/* change of perspective */
    508       1.68        pk 	if (flush & FREAD) {
    509      1.108     rmind 		selnotify(&pti->pt_selw, 0, NOTE_SUBMIT);
    510      1.110        ad 		cv_broadcast(&tp->t_rawcvf);
    511       1.68        pk 	}
    512       1.68        pk 	if (flush & FWRITE) {
    513      1.108     rmind 		selnotify(&pti->pt_selr, 0, NOTE_SUBMIT);
    514      1.110        ad 		cv_broadcast(&tp->t_outcvf);
    515       1.68        pk 	}
    516       1.31  christos }
    517       1.31  christos 
    518       1.31  christos void
    519  1.112.2.3     skrll ptcwakeup(struct tty *tp, int flag)
    520        1.1       cgd {
    521       1.47  jdolecek 	struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
    522        1.1       cgd 
    523      1.102        ad 	mutex_spin_enter(&tty_lock);
    524        1.1       cgd 	if (flag & FREAD) {
    525      1.108     rmind 		selnotify(&pti->pt_selr, 0, NOTE_SUBMIT);
    526      1.110        ad 		cv_broadcast(&tp->t_outcvf);
    527        1.1       cgd 	}
    528        1.1       cgd 	if (flag & FWRITE) {
    529      1.108     rmind 		selnotify(&pti->pt_selw, 0, NOTE_SUBMIT);
    530      1.110        ad 		cv_broadcast(&tp->t_rawcvf);
    531        1.1       cgd 	}
    532      1.102        ad 	mutex_spin_exit(&tty_lock);
    533        1.1       cgd }
    534        1.1       cgd 
    535        1.1       cgd /*ARGSUSED*/
    536       1.25   mycroft int
    537       1.96      yamt ptcopen(dev_t dev, int flag, int devtype, struct lwp *l)
    538        1.1       cgd {
    539       1.27   mycroft 	struct pt_softc *pti;
    540       1.43  augustss 	struct tty *tp;
    541       1.47  jdolecek 	int error;
    542       1.71       dsl 	int ptn = minor(dev);
    543       1.47  jdolecek 
    544       1.81  christos 	if ((error = pty_check(ptn)) != 0)
    545       1.47  jdolecek 		return (error);
    546       1.47  jdolecek 
    547       1.71       dsl 	pti = pt_softc[ptn];
    548       1.47  jdolecek 	tp = pti->pt_tty;
    549        1.1       cgd 
    550      1.102        ad 	mutex_spin_enter(&tty_lock);
    551       1.71       dsl 	if (tp->t_oproc) {
    552      1.102        ad 		mutex_spin_exit(&tty_lock);
    553        1.1       cgd 		return (EIO);
    554       1.71       dsl 	}
    555        1.1       cgd 	tp->t_oproc = ptsstart;
    556      1.102        ad 	mutex_spin_exit(&tty_lock);
    557       1.50       eeh 	(void)(*tp->t_linesw->l_modem)(tp, 1);
    558       1.37   mycroft 	CLR(tp->t_lflag, EXTPROC);
    559       1.22       cgd 	pti->pt_flags = 0;
    560        1.1       cgd 	pti->pt_send = 0;
    561        1.1       cgd 	pti->pt_ucntl = 0;
    562        1.1       cgd 	return (0);
    563        1.1       cgd }
    564        1.1       cgd 
    565       1.31  christos /*ARGSUSED*/
    566       1.25   mycroft int
    567       1.96      yamt ptcclose(dev_t dev, int flag, int devtype, struct lwp *l)
    568        1.1       cgd {
    569       1.47  jdolecek 	struct pt_softc *pti = pt_softc[minor(dev)];
    570       1.43  augustss 	struct tty *tp = pti->pt_tty;
    571        1.1       cgd 
    572       1.50       eeh 	(void)(*tp->t_linesw->l_modem)(tp, 0);
    573      1.104        ad 	mutex_spin_enter(&tty_lock);
    574       1.37   mycroft 	CLR(tp->t_state, TS_CARR_ON);
    575        1.1       cgd 	tp->t_oproc = 0;		/* mark closed */
    576      1.102        ad 	mutex_spin_exit(&tty_lock);
    577        1.2       cgd 	return (0);
    578        1.1       cgd }
    579        1.1       cgd 
    580       1.31  christos int
    581  1.112.2.3     skrll ptcread(dev_t dev, struct uio *uio, int flag)
    582        1.1       cgd {
    583       1.47  jdolecek 	struct pt_softc *pti = pt_softc[minor(dev)];
    584       1.43  augustss 	struct tty *tp = pti->pt_tty;
    585       1.84  christos 	u_char bf[BUFSIZ];
    586        1.1       cgd 	int error = 0, cc;
    587        1.1       cgd 
    588        1.1       cgd 	/*
    589        1.1       cgd 	 * We want to block until the slave
    590        1.1       cgd 	 * is open, and there's something to read;
    591        1.1       cgd 	 * but if we lost the slave or we're NBIO,
    592        1.1       cgd 	 * then return the appropriate error instead.
    593        1.1       cgd 	 */
    594      1.102        ad 	mutex_spin_enter(&tty_lock);
    595        1.1       cgd 	for (;;) {
    596       1.37   mycroft 		if (ISSET(tp->t_state, TS_ISOPEN)) {
    597       1.71       dsl 			if (pti->pt_flags & PF_PKT && pti->pt_send) {
    598      1.102        ad 				mutex_spin_exit(&tty_lock);
    599        1.1       cgd 				error = ureadc((int)pti->pt_send, uio);
    600        1.1       cgd 				if (error)
    601        1.1       cgd 					return (error);
    602       1.68        pk 				/*
    603       1.68        pk 				 * Since we don't have the tty locked, there's
    604       1.68        pk 				 * a risk of messing up `t_termios'. This is
    605       1.68        pk 				 * relevant only if the tty got closed and then
    606       1.68        pk 				 * opened again while we were out uiomoving.
    607       1.68        pk 				 */
    608        1.1       cgd 				if (pti->pt_send & TIOCPKT_IOCTL) {
    609       1.22       cgd 					cc = min(uio->uio_resid,
    610        1.1       cgd 						sizeof(tp->t_termios));
    611       1.99  christos 					uiomove((void *) &tp->t_termios,
    612       1.31  christos 						cc, uio);
    613        1.1       cgd 				}
    614        1.1       cgd 				pti->pt_send = 0;
    615        1.1       cgd 				return (0);
    616        1.1       cgd 			}
    617       1.71       dsl 			if (pti->pt_flags & PF_UCNTL && pti->pt_ucntl) {
    618      1.102        ad 				mutex_spin_exit(&tty_lock);
    619        1.1       cgd 				error = ureadc((int)pti->pt_ucntl, uio);
    620        1.1       cgd 				if (error)
    621        1.1       cgd 					return (error);
    622        1.1       cgd 				pti->pt_ucntl = 0;
    623        1.1       cgd 				return (0);
    624        1.1       cgd 			}
    625       1.37   mycroft 			if (tp->t_outq.c_cc && !ISSET(tp->t_state, TS_TTSTOP))
    626        1.1       cgd 				break;
    627        1.1       cgd 		}
    628       1.68        pk 		if (!ISSET(tp->t_state, TS_CARR_ON)) {
    629       1.68        pk 			error = 0;	/* EOF */
    630       1.68        pk 			goto out;
    631       1.68        pk 		}
    632       1.68        pk 		if (flag & IO_NDELAY) {
    633       1.68        pk 			error = EWOULDBLOCK;
    634       1.68        pk 			goto out;
    635       1.68        pk 		}
    636      1.110        ad 		error = cv_wait_sig(&tp->t_outcvf, &tty_lock);
    637       1.31  christos 		if (error)
    638       1.68        pk 			goto out;
    639        1.1       cgd 	}
    640       1.68        pk 
    641       1.68        pk 	if (pti->pt_flags & (PF_PKT|PF_UCNTL)) {
    642      1.102        ad 		mutex_spin_exit(&tty_lock);
    643        1.1       cgd 		error = ureadc(0, uio);
    644      1.102        ad 		mutex_spin_enter(&tty_lock);
    645       1.68        pk 		if (error == 0 && !ISSET(tp->t_state, TS_ISOPEN))
    646       1.68        pk 			error = EIO;
    647       1.68        pk 	}
    648        1.1       cgd 	while (uio->uio_resid > 0 && error == 0) {
    649       1.84  christos 		cc = q_to_b(&tp->t_outq, bf, min(uio->uio_resid, BUFSIZ));
    650        1.1       cgd 		if (cc <= 0)
    651        1.1       cgd 			break;
    652      1.102        ad 		mutex_spin_exit(&tty_lock);
    653       1.84  christos 		error = uiomove(bf, cc, uio);
    654      1.102        ad 		mutex_spin_enter(&tty_lock);
    655       1.68        pk 		if (error == 0 && !ISSET(tp->t_state, TS_ISOPEN))
    656       1.68        pk 			error = EIO;
    657        1.1       cgd 	}
    658      1.103        ad 	ttypull(tp);
    659       1.68        pk out:
    660      1.102        ad 	mutex_spin_exit(&tty_lock);
    661        1.1       cgd 	return (error);
    662        1.1       cgd }
    663        1.1       cgd 
    664        1.1       cgd 
    665       1.31  christos int
    666  1.112.2.3     skrll ptcwrite(dev_t dev, struct uio *uio, int flag)
    667        1.1       cgd {
    668       1.47  jdolecek 	struct pt_softc *pti = pt_softc[minor(dev)];
    669       1.43  augustss 	struct tty *tp = pti->pt_tty;
    670       1.43  augustss 	u_char *cp = NULL;
    671       1.43  augustss 	int cc = 0;
    672        1.1       cgd 	u_char locbuf[BUFSIZ];
    673        1.1       cgd 	int cnt = 0;
    674        1.1       cgd 	int error = 0;
    675        1.1       cgd 
    676        1.1       cgd again:
    677      1.102        ad 	mutex_spin_enter(&tty_lock);
    678       1.37   mycroft 	if (!ISSET(tp->t_state, TS_ISOPEN))
    679        1.1       cgd 		goto block;
    680        1.1       cgd 	if (pti->pt_flags & PF_REMOTE) {
    681        1.8   mycroft 		if (tp->t_canq.c_cc)
    682        1.1       cgd 			goto block;
    683        1.8   mycroft 		while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) {
    684        1.1       cgd 			if (cc == 0) {
    685        1.1       cgd 				cc = min(uio->uio_resid, BUFSIZ);
    686        1.8   mycroft 				cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
    687        1.1       cgd 				cp = locbuf;
    688      1.102        ad 				mutex_spin_exit(&tty_lock);
    689       1.99  christos 				error = uiomove((void *)cp, cc, uio);
    690        1.1       cgd 				if (error)
    691        1.1       cgd 					return (error);
    692      1.102        ad 				mutex_spin_enter(&tty_lock);
    693        1.1       cgd 				/* check again for safety */
    694       1.68        pk 				if (!ISSET(tp->t_state, TS_ISOPEN)) {
    695       1.93  christos 					/*
    696       1.93  christos 					 * adjust for data copied in but not
    697       1.93  christos 					 * written
    698       1.93  christos 					 */
    699       1.93  christos 					uio->uio_resid += cc;
    700       1.68        pk 					error = EIO;
    701       1.68        pk 					goto out;
    702       1.68        pk 				}
    703        1.1       cgd 			}
    704        1.1       cgd 			if (cc)
    705       1.67    simonb 				(void) b_to_q(cp, cc, &tp->t_canq);
    706        1.1       cgd 			cc = 0;
    707        1.1       cgd 		}
    708        1.8   mycroft 		(void) putc(0, &tp->t_canq);
    709        1.1       cgd 		ttwakeup(tp);
    710      1.110        ad 		cv_broadcast(&tp->t_cancv);
    711       1.68        pk 		error = 0;
    712       1.68        pk 		goto out;
    713        1.1       cgd 	}
    714        1.1       cgd 	while (uio->uio_resid > 0) {
    715        1.1       cgd 		if (cc == 0) {
    716        1.1       cgd 			cc = min(uio->uio_resid, BUFSIZ);
    717        1.1       cgd 			cp = locbuf;
    718      1.102        ad 			mutex_spin_exit(&tty_lock);
    719       1.99  christos 			error = uiomove((void *)cp, cc, uio);
    720        1.1       cgd 			if (error)
    721        1.1       cgd 				return (error);
    722      1.102        ad 			mutex_spin_enter(&tty_lock);
    723        1.1       cgd 			/* check again for safety */
    724       1.68        pk 			if (!ISSET(tp->t_state, TS_ISOPEN)) {
    725       1.92  christos 				/* adjust for data copied in but not written */
    726       1.92  christos 				uio->uio_resid += cc;
    727       1.68        pk 				error = EIO;
    728       1.68        pk 				goto out;
    729       1.68        pk 			}
    730        1.1       cgd 		}
    731        1.1       cgd 		while (cc > 0) {
    732        1.8   mycroft 			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
    733       1.59  christos 			   (tp->t_canq.c_cc > 0 || !ISSET(tp->t_lflag, ICANON))) {
    734      1.110        ad 				cv_broadcast(&tp->t_rawcv);
    735        1.1       cgd 				goto block;
    736        1.1       cgd 			}
    737       1.68        pk 			/* XXX - should change l_rint to be called with lock
    738       1.68        pk 			 *	 see also tty.c:ttyinput_wlock()
    739       1.68        pk 			 */
    740      1.102        ad 			mutex_spin_exit(&tty_lock);
    741       1.50       eeh 			(*tp->t_linesw->l_rint)(*cp++, tp);
    742      1.102        ad 			mutex_spin_enter(&tty_lock);
    743        1.1       cgd 			cnt++;
    744        1.1       cgd 			cc--;
    745        1.1       cgd 		}
    746        1.1       cgd 		cc = 0;
    747        1.1       cgd 	}
    748       1.68        pk 	error = 0;
    749       1.68        pk 	goto out;
    750       1.68        pk 
    751        1.1       cgd block:
    752        1.1       cgd 	/*
    753        1.1       cgd 	 * Come here to wait for slave to open, for space
    754        1.1       cgd 	 * in outq, or space in rawq.
    755        1.1       cgd 	 */
    756       1.68        pk 	if (!ISSET(tp->t_state, TS_CARR_ON)) {
    757       1.92  christos 		/* adjust for data copied in but not written */
    758       1.92  christos 		uio->uio_resid += cc;
    759       1.68        pk 		error = EIO;
    760       1.68        pk 		goto out;
    761       1.68        pk 	}
    762        1.1       cgd 	if (flag & IO_NDELAY) {
    763        1.1       cgd 		/* adjust for data copied in but not written */
    764        1.1       cgd 		uio->uio_resid += cc;
    765       1.68        pk 		error = cnt == 0 ? EWOULDBLOCK : 0;
    766       1.68        pk 		goto out;
    767        1.1       cgd 	}
    768      1.110        ad 	error = cv_wait_sig(&tp->t_rawcv, &tty_lock);
    769      1.102        ad 	mutex_spin_exit(&tty_lock);
    770       1.31  christos 	if (error) {
    771        1.1       cgd 		/* adjust for data copied in but not written */
    772        1.1       cgd 		uio->uio_resid += cc;
    773        1.1       cgd 		return (error);
    774        1.1       cgd 	}
    775        1.1       cgd 	goto again;
    776       1.68        pk 
    777       1.68        pk out:
    778      1.102        ad 	mutex_spin_exit(&tty_lock);
    779       1.68        pk 	return (error);
    780       1.29   mycroft }
    781       1.29   mycroft 
    782       1.31  christos int
    783  1.112.2.3     skrll ptcpoll(dev_t dev, int events, struct lwp *l)
    784       1.31  christos {
    785       1.47  jdolecek 	struct pt_softc *pti = pt_softc[minor(dev)];
    786       1.43  augustss 	struct tty *tp = pti->pt_tty;
    787       1.38   mycroft 	int revents = 0;
    788      1.102        ad 
    789      1.102        ad 	mutex_spin_enter(&tty_lock);
    790       1.31  christos 
    791       1.38   mycroft 	if (events & (POLLIN | POLLRDNORM))
    792       1.37   mycroft 		if (ISSET(tp->t_state, TS_ISOPEN) &&
    793       1.38   mycroft 		    ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
    794       1.38   mycroft 		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
    795       1.38   mycroft 		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
    796       1.38   mycroft 			revents |= events & (POLLIN | POLLRDNORM);
    797       1.31  christos 
    798       1.38   mycroft 	if (events & (POLLOUT | POLLWRNORM))
    799       1.37   mycroft 		if (ISSET(tp->t_state, TS_ISOPEN) &&
    800       1.38   mycroft 		    ((pti->pt_flags & PF_REMOTE) ?
    801       1.38   mycroft 		     (tp->t_canq.c_cc == 0) :
    802       1.38   mycroft 		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
    803       1.59  christos 		      (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))))
    804       1.38   mycroft 			revents |= events & (POLLOUT | POLLWRNORM);
    805       1.31  christos 
    806       1.38   mycroft 	if (events & POLLHUP)
    807       1.38   mycroft 		if (!ISSET(tp->t_state, TS_CARR_ON))
    808       1.38   mycroft 			revents |= POLLHUP;
    809       1.31  christos 
    810       1.38   mycroft 	if (revents == 0) {
    811       1.38   mycroft 		if (events & (POLLIN | POLLHUP | POLLRDNORM))
    812       1.86  christos 			selrecord(l, &pti->pt_selr);
    813       1.31  christos 
    814       1.38   mycroft 		if (events & (POLLOUT | POLLWRNORM))
    815       1.86  christos 			selrecord(l, &pti->pt_selw);
    816       1.31  christos 	}
    817       1.38   mycroft 
    818      1.102        ad 	mutex_spin_exit(&tty_lock);
    819      1.102        ad 
    820       1.38   mycroft 	return (revents);
    821       1.31  christos }
    822       1.31  christos 
    823       1.65  jdolecek static void
    824       1.65  jdolecek filt_ptcrdetach(struct knote *kn)
    825       1.65  jdolecek {
    826       1.65  jdolecek 	struct pt_softc *pti;
    827      1.102        ad 	struct tty	*tp;
    828       1.65  jdolecek 
    829       1.65  jdolecek 	pti = kn->kn_hook;
    830      1.102        ad 	tp = pti->pt_tty;
    831      1.102        ad 
    832      1.102        ad 	mutex_spin_enter(&tty_lock);
    833       1.66  christos 	SLIST_REMOVE(&pti->pt_selr.sel_klist, kn, knote, kn_selnext);
    834      1.102        ad 	mutex_spin_exit(&tty_lock);
    835       1.65  jdolecek }
    836       1.65  jdolecek 
    837       1.65  jdolecek static int
    838       1.96      yamt filt_ptcread(struct knote *kn, long hint)
    839       1.65  jdolecek {
    840       1.65  jdolecek 	struct pt_softc *pti;
    841       1.65  jdolecek 	struct tty	*tp;
    842       1.65  jdolecek 	int canread;
    843       1.65  jdolecek 
    844       1.65  jdolecek 	pti = kn->kn_hook;
    845       1.65  jdolecek 	tp = pti->pt_tty;
    846       1.65  jdolecek 
    847      1.107        ad 	if ((hint & NOTE_SUBMIT) == 0) {
    848      1.107        ad 		mutex_spin_enter(&tty_lock);
    849      1.107        ad 	}
    850      1.102        ad 
    851       1.65  jdolecek 	canread = (ISSET(tp->t_state, TS_ISOPEN) &&
    852       1.65  jdolecek 		    ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
    853       1.65  jdolecek 		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
    854       1.65  jdolecek 		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)));
    855       1.65  jdolecek 
    856       1.65  jdolecek 	if (canread) {
    857       1.65  jdolecek 		/*
    858       1.65  jdolecek 		 * c_cc is number of characters after output post-processing;
    859       1.83     perry 		 * the amount of data actually read(2) depends on
    860       1.65  jdolecek 		 * setting of input flags for the terminal.
    861       1.65  jdolecek 		 */
    862       1.65  jdolecek 		kn->kn_data = tp->t_outq.c_cc;
    863       1.65  jdolecek 		if (((pti->pt_flags & PF_PKT) && pti->pt_send) ||
    864       1.65  jdolecek 		    ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))
    865       1.65  jdolecek 			kn->kn_data++;
    866       1.65  jdolecek 	}
    867       1.65  jdolecek 
    868      1.107        ad 	if ((hint & NOTE_SUBMIT) == 0) {
    869      1.107        ad 		mutex_spin_exit(&tty_lock);
    870      1.107        ad 	}
    871      1.102        ad 
    872       1.65  jdolecek 	return (canread);
    873       1.65  jdolecek }
    874       1.65  jdolecek 
    875       1.65  jdolecek static void
    876       1.65  jdolecek filt_ptcwdetach(struct knote *kn)
    877       1.65  jdolecek {
    878       1.65  jdolecek 	struct pt_softc *pti;
    879      1.102        ad 	struct tty	*tp;
    880       1.65  jdolecek 
    881       1.65  jdolecek 	pti = kn->kn_hook;
    882      1.102        ad 	tp = pti->pt_tty;
    883      1.102        ad 
    884      1.102        ad 	mutex_spin_enter(&tty_lock);
    885       1.66  christos 	SLIST_REMOVE(&pti->pt_selw.sel_klist, kn, knote, kn_selnext);
    886      1.102        ad 	mutex_spin_exit(&tty_lock);
    887       1.65  jdolecek }
    888       1.65  jdolecek 
    889       1.65  jdolecek static int
    890       1.96      yamt filt_ptcwrite(struct knote *kn, long hint)
    891       1.65  jdolecek {
    892       1.65  jdolecek 	struct pt_softc *pti;
    893       1.65  jdolecek 	struct tty	*tp;
    894       1.65  jdolecek 	int canwrite;
    895       1.65  jdolecek 	int nwrite;
    896       1.65  jdolecek 
    897       1.65  jdolecek 	pti = kn->kn_hook;
    898       1.65  jdolecek 	tp = pti->pt_tty;
    899       1.65  jdolecek 
    900      1.107        ad 	if ((hint & NOTE_SUBMIT) == 0) {
    901      1.107        ad 		mutex_spin_enter(&tty_lock);
    902      1.107        ad 	}
    903      1.102        ad 
    904       1.65  jdolecek 	canwrite = (ISSET(tp->t_state, TS_ISOPEN) &&
    905       1.65  jdolecek 		    ((pti->pt_flags & PF_REMOTE) ?
    906       1.65  jdolecek 		     (tp->t_canq.c_cc == 0) :
    907       1.65  jdolecek 		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
    908       1.65  jdolecek 		      (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))));
    909       1.65  jdolecek 
    910       1.65  jdolecek 	if (canwrite) {
    911       1.65  jdolecek 		if (pti->pt_flags & PF_REMOTE)
    912       1.65  jdolecek 			nwrite = tp->t_canq.c_cn;
    913       1.65  jdolecek 		else {
    914       1.65  jdolecek 			/* this is guaranteed to be > 0 due to above check */
    915       1.65  jdolecek 			nwrite = tp->t_canq.c_cn
    916       1.65  jdolecek 				- (tp->t_rawq.c_cc + tp->t_canq.c_cc);
    917       1.65  jdolecek 		}
    918       1.65  jdolecek 		kn->kn_data = nwrite;
    919       1.65  jdolecek 	}
    920       1.65  jdolecek 
    921      1.107        ad 	if ((hint & NOTE_SUBMIT) == 0) {
    922      1.107        ad 		mutex_spin_exit(&tty_lock);
    923      1.107        ad 	}
    924      1.102        ad 
    925       1.65  jdolecek 	return (canwrite);
    926       1.65  jdolecek }
    927       1.65  jdolecek 
    928       1.65  jdolecek static const struct filterops ptcread_filtops =
    929       1.65  jdolecek 	{ 1, NULL, filt_ptcrdetach, filt_ptcread };
    930       1.65  jdolecek static const struct filterops ptcwrite_filtops =
    931       1.65  jdolecek 	{ 1, NULL, filt_ptcwdetach, filt_ptcwrite };
    932       1.65  jdolecek 
    933       1.65  jdolecek int
    934       1.65  jdolecek ptckqfilter(dev_t dev, struct knote *kn)
    935       1.65  jdolecek {
    936       1.65  jdolecek 	struct pt_softc *pti = pt_softc[minor(dev)];
    937       1.65  jdolecek 	struct klist	*klist;
    938       1.65  jdolecek 
    939       1.65  jdolecek 	switch (kn->kn_filter) {
    940       1.65  jdolecek 	case EVFILT_READ:
    941       1.66  christos 		klist = &pti->pt_selr.sel_klist;
    942       1.65  jdolecek 		kn->kn_fop = &ptcread_filtops;
    943       1.65  jdolecek 		break;
    944       1.65  jdolecek 	case EVFILT_WRITE:
    945       1.66  christos 		klist = &pti->pt_selw.sel_klist;
    946       1.65  jdolecek 		kn->kn_fop = &ptcwrite_filtops;
    947       1.65  jdolecek 		break;
    948       1.65  jdolecek 	default:
    949      1.105     pooka 		return (EINVAL);
    950       1.65  jdolecek 	}
    951       1.65  jdolecek 
    952       1.65  jdolecek 	kn->kn_hook = pti;
    953       1.65  jdolecek 
    954      1.102        ad 	mutex_spin_enter(&tty_lock);
    955       1.65  jdolecek 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    956      1.102        ad 	mutex_spin_exit(&tty_lock);
    957       1.65  jdolecek 
    958       1.65  jdolecek 	return (0);
    959       1.65  jdolecek }
    960       1.31  christos 
    961       1.29   mycroft struct tty *
    962  1.112.2.3     skrll ptytty(dev_t dev)
    963       1.29   mycroft {
    964       1.47  jdolecek 	struct pt_softc *pti = pt_softc[minor(dev)];
    965       1.43  augustss 	struct tty *tp = pti->pt_tty;
    966       1.29   mycroft 
    967       1.29   mycroft 	return (tp);
    968        1.1       cgd }
    969        1.1       cgd 
    970        1.1       cgd /*ARGSUSED*/
    971       1.31  christos int
    972  1.112.2.3     skrll ptyioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    973        1.1       cgd {
    974       1.47  jdolecek 	struct pt_softc *pti = pt_softc[minor(dev)];
    975       1.43  augustss 	struct tty *tp = pti->pt_tty;
    976       1.63   gehenna 	const struct cdevsw *cdev;
    977       1.43  augustss 	u_char *cc = tp->t_cc;
    978       1.44      fvdl 	int stop, error, sig;
    979        1.1       cgd 
    980        1.1       cgd 	/*
    981        1.1       cgd 	 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
    982        1.1       cgd 	 * ttywflush(tp) will hang if there are characters in the outq.
    983        1.1       cgd 	 */
    984        1.1       cgd 	if (cmd == TIOCEXT) {
    985        1.1       cgd 		/*
    986        1.1       cgd 		 * When the EXTPROC bit is being toggled, we need
    987        1.1       cgd 		 * to send an TIOCPKT_IOCTL if the packet driver
    988        1.1       cgd 		 * is turned on.
    989        1.1       cgd 		 */
    990        1.1       cgd 		if (*(int *)data) {
    991        1.1       cgd 			if (pti->pt_flags & PF_PKT) {
    992        1.1       cgd 				pti->pt_send |= TIOCPKT_IOCTL;
    993        1.1       cgd 				ptcwakeup(tp, FREAD);
    994        1.1       cgd 			}
    995       1.37   mycroft 			SET(tp->t_lflag, EXTPROC);
    996        1.1       cgd 		} else {
    997       1.39      fvdl 			if (ISSET(tp->t_lflag, EXTPROC) &&
    998        1.1       cgd 			    (pti->pt_flags & PF_PKT)) {
    999        1.1       cgd 				pti->pt_send |= TIOCPKT_IOCTL;
   1000        1.1       cgd 				ptcwakeup(tp, FREAD);
   1001        1.1       cgd 			}
   1002       1.37   mycroft 			CLR(tp->t_lflag, EXTPROC);
   1003        1.1       cgd 		}
   1004        1.1       cgd 		return(0);
   1005       1.71       dsl 	}
   1006       1.71       dsl 
   1007       1.81  christos #ifndef NO_DEV_PTM
   1008       1.81  christos 	/* Allow getting the name from either the master or the slave */
   1009       1.81  christos 	if (cmd == TIOCPTSNAME)
   1010       1.88  christos 		return pty_fill_ptmget(l, dev, -1, -1, data);
   1011       1.81  christos #endif
   1012       1.81  christos 
   1013       1.81  christos 	cdev = cdevsw_lookup(dev);
   1014       1.63   gehenna 	if (cdev != NULL && cdev->d_open == ptcopen)
   1015        1.1       cgd 		switch (cmd) {
   1016       1.80  christos #ifndef NO_DEV_PTM
   1017       1.77  christos 		case TIOCGRANTPT:
   1018       1.86  christos 			return pty_grant_slave(l, dev);
   1019       1.77  christos #endif
   1020        1.1       cgd 
   1021        1.1       cgd 		case TIOCGPGRP:
   1022       1.35        pk 			/*
   1023       1.35        pk 			 * We avoid calling ttioctl on the controller since,
   1024        1.1       cgd 			 * in that case, tp must be the controlling terminal.
   1025        1.1       cgd 			 */
   1026        1.1       cgd 			*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
   1027        1.1       cgd 			return (0);
   1028        1.1       cgd 
   1029        1.1       cgd 		case TIOCPKT:
   1030        1.1       cgd 			if (*(int *)data) {
   1031        1.1       cgd 				if (pti->pt_flags & PF_UCNTL)
   1032        1.1       cgd 					return (EINVAL);
   1033        1.1       cgd 				pti->pt_flags |= PF_PKT;
   1034        1.1       cgd 			} else
   1035        1.1       cgd 				pti->pt_flags &= ~PF_PKT;
   1036        1.1       cgd 			return (0);
   1037        1.1       cgd 
   1038        1.1       cgd 		case TIOCUCNTL:
   1039        1.1       cgd 			if (*(int *)data) {
   1040        1.1       cgd 				if (pti->pt_flags & PF_PKT)
   1041        1.1       cgd 					return (EINVAL);
   1042        1.1       cgd 				pti->pt_flags |= PF_UCNTL;
   1043        1.1       cgd 			} else
   1044        1.1       cgd 				pti->pt_flags &= ~PF_UCNTL;
   1045        1.1       cgd 			return (0);
   1046        1.1       cgd 
   1047        1.1       cgd 		case TIOCREMOTE:
   1048        1.1       cgd 			if (*(int *)data)
   1049        1.1       cgd 				pti->pt_flags |= PF_REMOTE;
   1050        1.1       cgd 			else
   1051        1.1       cgd 				pti->pt_flags &= ~PF_REMOTE;
   1052      1.102        ad 			mutex_spin_enter(&tty_lock);
   1053        1.1       cgd 			ttyflush(tp, FREAD|FWRITE);
   1054      1.102        ad 			mutex_spin_exit(&tty_lock);
   1055        1.1       cgd 			return (0);
   1056        1.1       cgd 
   1057       1.35        pk 		case TIOCSETP:
   1058        1.1       cgd 		case TIOCSETN:
   1059        1.1       cgd 		case TIOCSETD:
   1060        1.1       cgd 		case TIOCSETA:
   1061        1.1       cgd 		case TIOCSETAW:
   1062        1.1       cgd 		case TIOCSETAF:
   1063      1.102        ad 			mutex_spin_enter(&tty_lock);
   1064       1.21       cgd 			ndflush(&tp->t_outq, tp->t_outq.c_cc);
   1065      1.102        ad 			mutex_spin_exit(&tty_lock);
   1066        1.1       cgd 			break;
   1067        1.1       cgd 
   1068        1.1       cgd 		case TIOCSIG:
   1069       1.99  christos 			sig = (int)(long)*(void **)data;
   1070       1.44      fvdl 			if (sig <= 0 || sig >= NSIG)
   1071       1.44      fvdl 				return (EINVAL);
   1072      1.102        ad 			mutex_spin_enter(&tty_lock);
   1073       1.37   mycroft 			if (!ISSET(tp->t_lflag, NOFLSH))
   1074        1.1       cgd 				ttyflush(tp, FREAD|FWRITE);
   1075      1.109        ad 			tp->t_state |= TS_SIGINFO;
   1076      1.102        ad 			ttysig(tp, TTYSIG_PG1, sig);
   1077      1.102        ad 			mutex_spin_exit(&tty_lock);
   1078      1.112  drochner 			return (0);
   1079      1.112  drochner 
   1080      1.112  drochner 		case FIONREAD:
   1081      1.112  drochner 			mutex_spin_enter(&tty_lock);
   1082      1.112  drochner 			*(int *)data = tp->t_outq.c_cc;
   1083      1.112  drochner 			mutex_spin_exit(&tty_lock);
   1084      1.112  drochner 			return (0);
   1085        1.1       cgd 		}
   1086       1.71       dsl 
   1087       1.86  christos 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
   1088       1.61    atatat 	if (error == EPASSTHROUGH)
   1089       1.86  christos 		 error = ttioctl(tp, cmd, data, flag, l);
   1090       1.61    atatat 	if (error == EPASSTHROUGH) {
   1091        1.1       cgd 		if (pti->pt_flags & PF_UCNTL &&
   1092        1.1       cgd 		    (cmd & ~0xff) == UIOCCMD(0)) {
   1093        1.1       cgd 			if (cmd & 0xff) {
   1094        1.1       cgd 				pti->pt_ucntl = (u_char)cmd;
   1095        1.1       cgd 				ptcwakeup(tp, FREAD);
   1096        1.1       cgd 			}
   1097        1.1       cgd 			return (0);
   1098        1.1       cgd 		}
   1099        1.1       cgd 	}
   1100        1.1       cgd 	/*
   1101        1.1       cgd 	 * If external processing and packet mode send ioctl packet.
   1102        1.1       cgd 	 */
   1103       1.37   mycroft 	if (ISSET(tp->t_lflag, EXTPROC) && (pti->pt_flags & PF_PKT)) {
   1104        1.1       cgd 		switch(cmd) {
   1105        1.1       cgd 		case TIOCSETA:
   1106        1.1       cgd 		case TIOCSETAW:
   1107        1.1       cgd 		case TIOCSETAF:
   1108        1.1       cgd 		case TIOCSETP:
   1109        1.1       cgd 		case TIOCSETN:
   1110        1.1       cgd 		case TIOCSETC:
   1111        1.1       cgd 		case TIOCSLTC:
   1112        1.1       cgd 		case TIOCLBIS:
   1113        1.1       cgd 		case TIOCLBIC:
   1114        1.1       cgd 		case TIOCLSET:
   1115        1.1       cgd 			pti->pt_send |= TIOCPKT_IOCTL;
   1116       1.22       cgd 			ptcwakeup(tp, FREAD);
   1117        1.1       cgd 		default:
   1118        1.1       cgd 			break;
   1119        1.1       cgd 		}
   1120        1.1       cgd 	}
   1121       1.37   mycroft 	stop = ISSET(tp->t_iflag, IXON) && CCEQ(cc[VSTOP], CTRL('s'))
   1122        1.1       cgd 		&& CCEQ(cc[VSTART], CTRL('q'));
   1123        1.1       cgd 	if (pti->pt_flags & PF_NOSTOP) {
   1124        1.1       cgd 		if (stop) {
   1125        1.1       cgd 			pti->pt_send &= ~TIOCPKT_NOSTOP;
   1126        1.1       cgd 			pti->pt_send |= TIOCPKT_DOSTOP;
   1127        1.1       cgd 			pti->pt_flags &= ~PF_NOSTOP;
   1128        1.1       cgd 			ptcwakeup(tp, FREAD);
   1129        1.1       cgd 		}
   1130        1.1       cgd 	} else {
   1131        1.1       cgd 		if (!stop) {
   1132        1.1       cgd 			pti->pt_send &= ~TIOCPKT_DOSTOP;
   1133        1.1       cgd 			pti->pt_send |= TIOCPKT_NOSTOP;
   1134        1.1       cgd 			pti->pt_flags |= PF_NOSTOP;
   1135        1.1       cgd 			ptcwakeup(tp, FREAD);
   1136        1.1       cgd 		}
   1137        1.1       cgd 	}
   1138        1.1       cgd 	return (error);
   1139        1.1       cgd }
   1140