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