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