pty.c revision 53483
1/*
2 * Copyright (c) 1982, 1986, 1989, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 *	@(#)tty_pty.c	8.4 (Berkeley) 2/20/95
34 * $FreeBSD: head/sys/kern/tty_pty.c 53483 1999-11-21 02:54:54Z jkh $
35 */
36
37/*
38 * Pseudo-teletype Driver
39 * (Actually two drivers, requiring two entries in 'cdevsw')
40 */
41#include "pty.h"		/* XXX */
42#include "opt_compat.h"
43#include <sys/param.h>
44#include <sys/systm.h>
45#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
46#include <sys/ioctl_compat.h>
47#endif
48#include <sys/proc.h>
49#include <sys/tty.h>
50#include <sys/conf.h>
51#include <sys/fcntl.h>
52#include <sys/poll.h>
53#include <sys/kernel.h>
54#include <sys/vnode.h>
55#include <sys/signalvar.h>
56#include <sys/malloc.h>
57
58MALLOC_DEFINE(M_PTY, "ptys", "pty data structures");
59
60static void ptsstart __P((struct tty *tp));
61static void ptsstop __P((struct tty *tp, int rw));
62static void ptcwakeup __P((struct tty *tp, int flag));
63static void ptyinit __P((int n));
64
65static	d_open_t	ptsopen;
66static	d_close_t	ptsclose;
67static	d_read_t	ptsread;
68static	d_write_t	ptswrite;
69static	d_ioctl_t	ptyioctl;
70static	d_open_t	ptcopen;
71static	d_close_t	ptcclose;
72static	d_read_t	ptcread;
73static	d_write_t	ptcwrite;
74static	d_poll_t	ptcpoll;
75
76#define	CDEV_MAJOR_S	5
77static struct cdevsw pts_cdevsw = {
78	/* open */	ptsopen,
79	/* close */	ptsclose,
80	/* read */	ptsread,
81	/* write */	ptswrite,
82	/* ioctl */	ptyioctl,
83	/* poll */	ttypoll,
84	/* mmap */	nommap,
85	/* strategy */	nostrategy,
86	/* name */	"pts",
87	/* maj */	CDEV_MAJOR_S,
88	/* dump */	nodump,
89	/* psize */	nopsize,
90	/* flags */	D_TTY,
91	/* bmaj */	-1
92};
93
94#define	CDEV_MAJOR_C	6
95static struct cdevsw ptc_cdevsw = {
96	/* open */	ptcopen,
97	/* close */	ptcclose,
98	/* read */	ptcread,
99	/* write */	ptcwrite,
100	/* ioctl */	ptyioctl,
101	/* poll */	ptcpoll,
102	/* mmap */	nommap,
103	/* strategy */	nostrategy,
104	/* name */	"ptc",
105	/* maj */	CDEV_MAJOR_C,
106	/* dump */	nodump,
107	/* psize */	nopsize,
108	/* flags */	D_TTY,
109	/* bmaj */	-1
110};
111
112#define BUFSIZ 100		/* Chunk size iomoved to/from user */
113
114struct	pt_ioctl {
115	int	pt_flags;
116	struct	selinfo pt_selr, pt_selw;
117	u_char	pt_send;
118	u_char	pt_ucntl;
119	struct tty pt_tty;
120	dev_t	devs, devc;
121};
122
123#define	PF_PKT		0x08		/* packet mode */
124#define	PF_STOPPED	0x10		/* user told stopped */
125#define	PF_REMOTE	0x20		/* remote and flow controlled input */
126#define	PF_NOSTOP	0x40
127#define PF_UCNTL	0x80		/* user control mode */
128
129/*
130 * This function creates and initializes a pts/ptc pair
131 *
132 * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
133 * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
134 *
135 * XXX: define and add mapping of upper minor bits to allow more
136 *      than 256 ptys.
137 */
138static void
139ptyinit(n)
140	int n;
141{
142	dev_t devs, devc;
143	char *names = "pqrsPQRS";
144	struct pt_ioctl *pt;
145
146	/* For now we only map the lower 8 bits of the minor */
147	if (n & ~0xff)
148		return;
149
150	pt = malloc(sizeof(*pt), M_PTY, M_WAITOK);
151	bzero(pt, sizeof(*pt));
152	pt->devs = devs = make_dev(&pts_cdevsw, n,
153	    0, 0, 0666, "tty%c%r", names[n / 32], n % 32);
154	pt->devc = devc = make_dev(&ptc_cdevsw, n,
155	    0, 0, 0666, "pty%c%r", names[n / 32], n % 32);
156
157	devs->si_drv1 = devc->si_drv1 = pt;
158	devs->si_tty = devc->si_tty = &pt->pt_tty;
159	ttyregister(&pt->pt_tty);
160}
161
162/*ARGSUSED*/
163static	int
164ptsopen(dev, flag, devtype, p)
165	dev_t dev;
166	int flag, devtype;
167	struct proc *p;
168{
169	register struct tty *tp;
170	int error;
171	int minr;
172	dev_t nextdev;
173
174	/*
175	 * XXX: Gross hack for DEVFS:
176	 * If we openned this device, ensure we have the
177	 * next one too, so people can open it.
178	 */
179	minr = lminor(dev);
180	if (minr < 255) {
181		nextdev = makedev(major(dev), minr + 1);
182		if (!nextdev->si_drv1) {
183			ptyinit(minr + 1);
184		}
185	}
186	if (!dev->si_drv1)
187		ptyinit(minor(dev));
188	if (!dev->si_drv1)
189		return(ENXIO);
190	tp = dev->si_tty;
191	if ((tp->t_state & TS_ISOPEN) == 0) {
192		ttychars(tp);		/* Set up default chars */
193		tp->t_iflag = TTYDEF_IFLAG;
194		tp->t_oflag = TTYDEF_OFLAG;
195		tp->t_lflag = TTYDEF_LFLAG;
196		tp->t_cflag = TTYDEF_CFLAG;
197		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
198	} else if (tp->t_state & TS_XCLUDE && suser(p))
199		return (EBUSY);
200	if (tp->t_oproc)			/* Ctrlr still around. */
201		(void)(*linesw[tp->t_line].l_modem)(tp, 1);
202	while ((tp->t_state & TS_CARR_ON) == 0) {
203		if (flag&FNONBLOCK)
204			break;
205		error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH,
206				 "ptsopn", 0);
207		if (error)
208			return (error);
209	}
210	error = (*linesw[tp->t_line].l_open)(dev, tp);
211	if (error == 0)
212		ptcwakeup(tp, FREAD|FWRITE);
213	return (error);
214}
215
216static	int
217ptsclose(dev, flag, mode, p)
218	dev_t dev;
219	int flag, mode;
220	struct proc *p;
221{
222	register struct tty *tp;
223	int err;
224
225	tp = dev->si_tty;
226	err = (*linesw[tp->t_line].l_close)(tp, flag);
227	ptsstop(tp, FREAD|FWRITE);
228	(void) ttyclose(tp);
229	return (err);
230}
231
232static	int
233ptsread(dev, uio, flag)
234	dev_t dev;
235	struct uio *uio;
236	int flag;
237{
238	struct proc *p = curproc;
239	register struct tty *tp = dev->si_tty;
240	register struct pt_ioctl *pti = dev->si_drv1;
241	int error = 0;
242
243again:
244	if (pti->pt_flags & PF_REMOTE) {
245		while (isbackground(p, tp)) {
246			if (SIGISMEMBER(p->p_sigignore, SIGTTIN) ||
247			    SIGISMEMBER(p->p_sigmask, SIGTTIN) ||
248			    p->p_pgrp->pg_jobc == 0 || p->p_flag & P_PPWAIT)
249				return (EIO);
250			pgsignal(p->p_pgrp, SIGTTIN, 1);
251			error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ptsbg",
252					 0);
253			if (error)
254				return (error);
255		}
256		if (tp->t_canq.c_cc == 0) {
257			if (flag & IO_NDELAY)
258				return (EWOULDBLOCK);
259			error = ttysleep(tp, TSA_PTS_READ(tp), TTIPRI | PCATCH,
260					 "ptsin", 0);
261			if (error)
262				return (error);
263			goto again;
264		}
265		while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
266			if (ureadc(getc(&tp->t_canq), uio) < 0) {
267				error = EFAULT;
268				break;
269			}
270		if (tp->t_canq.c_cc == 1)
271			(void) getc(&tp->t_canq);
272		if (tp->t_canq.c_cc)
273			return (error);
274	} else
275		if (tp->t_oproc)
276			error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
277	ptcwakeup(tp, FWRITE);
278	return (error);
279}
280
281/*
282 * Write to pseudo-tty.
283 * Wakeups of controlling tty will happen
284 * indirectly, when tty driver calls ptsstart.
285 */
286static	int
287ptswrite(dev, uio, flag)
288	dev_t dev;
289	struct uio *uio;
290	int flag;
291{
292	register struct tty *tp;
293
294	tp = dev->si_tty;
295	if (tp->t_oproc == 0)
296		return (EIO);
297	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
298}
299
300/*
301 * Start output on pseudo-tty.
302 * Wake up process selecting or sleeping for input from controlling tty.
303 */
304static void
305ptsstart(tp)
306	struct tty *tp;
307{
308	register struct pt_ioctl *pti = tp->t_dev->si_drv1;
309
310	if (tp->t_state & TS_TTSTOP)
311		return;
312	if (pti->pt_flags & PF_STOPPED) {
313		pti->pt_flags &= ~PF_STOPPED;
314		pti->pt_send = TIOCPKT_START;
315	}
316	ptcwakeup(tp, FREAD);
317}
318
319static void
320ptcwakeup(tp, flag)
321	struct tty *tp;
322	int flag;
323{
324	struct pt_ioctl *pti = tp->t_dev->si_drv1;
325
326	if (flag & FREAD) {
327		selwakeup(&pti->pt_selr);
328		wakeup(TSA_PTC_READ(tp));
329	}
330	if (flag & FWRITE) {
331		selwakeup(&pti->pt_selw);
332		wakeup(TSA_PTC_WRITE(tp));
333	}
334}
335
336static	int
337ptcopen(dev, flag, devtype, p)
338	dev_t dev;
339	int flag, devtype;
340	struct proc *p;
341{
342	register struct tty *tp;
343	struct pt_ioctl *pti;
344
345	if (!dev->si_drv1)
346		ptyinit(minor(dev));
347	if (!dev->si_drv1)
348		return(ENXIO);
349	tp = dev->si_tty;
350	if (tp->t_oproc)
351		return (EIO);
352	tp->t_oproc = ptsstart;
353	tp->t_stop = ptsstop;
354	(void)(*linesw[tp->t_line].l_modem)(tp, 1);
355	tp->t_lflag &= ~EXTPROC;
356	pti = dev->si_drv1;
357	pti->pt_flags = 0;
358	pti->pt_send = 0;
359	pti->pt_ucntl = 0;
360	return (0);
361}
362
363static	int
364ptcclose(dev, flags, fmt, p)
365	dev_t dev;
366	int flags;
367	int fmt;
368	struct proc *p;
369{
370	register struct tty *tp;
371
372	tp = dev->si_tty;
373	(void)(*linesw[tp->t_line].l_modem)(tp, 0);
374
375	/*
376	 * XXX MDMBUF makes no sense for ptys but would inhibit the above
377	 * l_modem().  CLOCAL makes sense but isn't supported.   Special
378	 * l_modem()s that ignore carrier drop make no sense for ptys but
379	 * may be in use because other parts of the line discipline make
380	 * sense for ptys.  Recover by doing everything that a normal
381	 * ttymodem() would have done except for sending a SIGHUP.
382	 */
383	if (tp->t_state & TS_ISOPEN) {
384		tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
385		tp->t_state |= TS_ZOMBIE;
386		ttyflush(tp, FREAD | FWRITE);
387	}
388
389	tp->t_oproc = 0;		/* mark closed */
390	return (0);
391}
392
393static	int
394ptcread(dev, uio, flag)
395	dev_t dev;
396	struct uio *uio;
397	int flag;
398{
399	register struct tty *tp = dev->si_tty;
400	struct pt_ioctl *pti = dev->si_drv1;
401	char buf[BUFSIZ];
402	int error = 0, cc;
403
404	/*
405	 * We want to block until the slave
406	 * is open, and there's something to read;
407	 * but if we lost the slave or we're NBIO,
408	 * then return the appropriate error instead.
409	 */
410	for (;;) {
411		if (tp->t_state&TS_ISOPEN) {
412			if (pti->pt_flags&PF_PKT && pti->pt_send) {
413				error = ureadc((int)pti->pt_send, uio);
414				if (error)
415					return (error);
416				if (pti->pt_send & TIOCPKT_IOCTL) {
417					cc = min(uio->uio_resid,
418						sizeof(tp->t_termios));
419					uiomove((caddr_t)&tp->t_termios, cc,
420						uio);
421				}
422				pti->pt_send = 0;
423				return (0);
424			}
425			if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
426				error = ureadc((int)pti->pt_ucntl, uio);
427				if (error)
428					return (error);
429				pti->pt_ucntl = 0;
430				return (0);
431			}
432			if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
433				break;
434		}
435		if ((tp->t_state & TS_CONNECTED) == 0)
436			return (0);	/* EOF */
437		if (flag & IO_NDELAY)
438			return (EWOULDBLOCK);
439		error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0);
440		if (error)
441			return (error);
442	}
443	if (pti->pt_flags & (PF_PKT|PF_UCNTL))
444		error = ureadc(0, uio);
445	while (uio->uio_resid > 0 && error == 0) {
446		cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
447		if (cc <= 0)
448			break;
449		error = uiomove(buf, cc, uio);
450	}
451	ttwwakeup(tp);
452	return (error);
453}
454
455static	void
456ptsstop(tp, flush)
457	register struct tty *tp;
458	int flush;
459{
460	struct pt_ioctl *pti = tp->t_dev->si_drv1;
461	int flag;
462
463	/* note: FLUSHREAD and FLUSHWRITE already ok */
464	if (flush == 0) {
465		flush = TIOCPKT_STOP;
466		pti->pt_flags |= PF_STOPPED;
467	} else
468		pti->pt_flags &= ~PF_STOPPED;
469	pti->pt_send |= flush;
470	/* change of perspective */
471	flag = 0;
472	if (flush & FREAD)
473		flag |= FWRITE;
474	if (flush & FWRITE)
475		flag |= FREAD;
476	ptcwakeup(tp, flag);
477}
478
479static	int
480ptcpoll(dev, events, p)
481	dev_t dev;
482	int events;
483	struct proc *p;
484{
485	register struct tty *tp = dev->si_tty;
486	struct pt_ioctl *pti = dev->si_drv1;
487	int revents = 0;
488	int s;
489
490	if ((tp->t_state & TS_CONNECTED) == 0)
491		return (seltrue(dev, events, p) | POLLHUP);
492
493	/*
494	 * Need to block timeouts (ttrstart).
495	 */
496	s = spltty();
497
498	if (events & (POLLIN | POLLRDNORM))
499		if ((tp->t_state & TS_ISOPEN) &&
500		    ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
501		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
502		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
503			revents |= events & (POLLIN | POLLRDNORM);
504
505	if (events & (POLLOUT | POLLWRNORM))
506		if (tp->t_state & TS_ISOPEN &&
507		    ((pti->pt_flags & PF_REMOTE) ?
508		     (tp->t_canq.c_cc == 0) :
509		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
510		      (tp->t_canq.c_cc == 0 && (tp->t_iflag & ICANON)))))
511			revents |= events & (POLLOUT | POLLWRNORM);
512
513	if (events & POLLHUP)
514		if ((tp->t_state & TS_CARR_ON) == 0)
515			revents |= POLLHUP;
516
517	if (revents == 0) {
518		if (events & (POLLIN | POLLRDNORM))
519			selrecord(p, &pti->pt_selr);
520
521		if (events & (POLLOUT | POLLWRNORM))
522			selrecord(p, &pti->pt_selw);
523	}
524	splx(s);
525
526	return (revents);
527}
528
529static	int
530ptcwrite(dev, uio, flag)
531	dev_t dev;
532	register struct uio *uio;
533	int flag;
534{
535	register struct tty *tp = dev->si_tty;
536	register u_char *cp = 0;
537	register int cc = 0;
538	u_char locbuf[BUFSIZ];
539	int cnt = 0;
540	struct pt_ioctl *pti = dev->si_drv1;
541	int error = 0;
542
543again:
544	if ((tp->t_state&TS_ISOPEN) == 0)
545		goto block;
546	if (pti->pt_flags & PF_REMOTE) {
547		if (tp->t_canq.c_cc)
548			goto block;
549		while ((uio->uio_resid > 0 || cc > 0) &&
550		       tp->t_canq.c_cc < TTYHOG - 1) {
551			if (cc == 0) {
552				cc = min(uio->uio_resid, BUFSIZ);
553				cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
554				cp = locbuf;
555				error = uiomove((caddr_t)cp, cc, uio);
556				if (error)
557					return (error);
558				/* check again for safety */
559				if ((tp->t_state & TS_ISOPEN) == 0) {
560					/* adjust as usual */
561					uio->uio_resid += cc;
562					return (EIO);
563				}
564			}
565			if (cc > 0) {
566				cc = b_to_q((char *)cp, cc, &tp->t_canq);
567				/*
568				 * XXX we don't guarantee that the canq size
569				 * is >= TTYHOG, so the above b_to_q() may
570				 * leave some bytes uncopied.  However, space
571				 * is guaranteed for the null terminator if
572				 * we don't fail here since (TTYHOG - 1) is
573				 * not a multiple of CBSIZE.
574				 */
575				if (cc > 0)
576					break;
577			}
578		}
579		/* adjust for data copied in but not written */
580		uio->uio_resid += cc;
581		(void) putc(0, &tp->t_canq);
582		ttwakeup(tp);
583		wakeup(TSA_PTS_READ(tp));
584		return (0);
585	}
586	while (uio->uio_resid > 0 || cc > 0) {
587		if (cc == 0) {
588			cc = min(uio->uio_resid, BUFSIZ);
589			cp = locbuf;
590			error = uiomove((caddr_t)cp, cc, uio);
591			if (error)
592				return (error);
593			/* check again for safety */
594			if ((tp->t_state & TS_ISOPEN) == 0) {
595				/* adjust for data copied in but not written */
596				uio->uio_resid += cc;
597				return (EIO);
598			}
599		}
600		while (cc > 0) {
601			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
602			   (tp->t_canq.c_cc > 0 || !(tp->t_iflag&ICANON))) {
603				wakeup(TSA_HUP_OR_INPUT(tp));
604				goto block;
605			}
606			(*linesw[tp->t_line].l_rint)(*cp++, tp);
607			cnt++;
608			cc--;
609		}
610		cc = 0;
611	}
612	return (0);
613block:
614	/*
615	 * Come here to wait for slave to open, for space
616	 * in outq, or space in rawq, or an empty canq.
617	 */
618	if ((tp->t_state & TS_CONNECTED) == 0) {
619		/* adjust for data copied in but not written */
620		uio->uio_resid += cc;
621		return (EIO);
622	}
623	if (flag & IO_NDELAY) {
624		/* adjust for data copied in but not written */
625		uio->uio_resid += cc;
626		if (cnt == 0)
627			return (EWOULDBLOCK);
628		return (0);
629	}
630	error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0);
631	if (error) {
632		/* adjust for data copied in but not written */
633		uio->uio_resid += cc;
634		return (error);
635	}
636	goto again;
637}
638
639/*ARGSUSED*/
640static	int
641ptyioctl(dev, cmd, data, flag, p)
642	dev_t dev;
643	u_long cmd;
644	caddr_t data;
645	int flag;
646	struct proc *p;
647{
648	register struct tty *tp = dev->si_tty;
649	register struct pt_ioctl *pti = dev->si_drv1;
650	register u_char *cc = tp->t_cc;
651	int stop, error;
652
653	if (devsw(dev)->d_open == ptcopen) {
654		switch (cmd) {
655
656		case TIOCGPGRP:
657			/*
658			 * We avoid calling ttioctl on the controller since,
659			 * in that case, tp must be the controlling terminal.
660			 */
661			*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
662			return (0);
663
664		case TIOCPKT:
665			if (*(int *)data) {
666				if (pti->pt_flags & PF_UCNTL)
667					return (EINVAL);
668				pti->pt_flags |= PF_PKT;
669			} else
670				pti->pt_flags &= ~PF_PKT;
671			return (0);
672
673		case TIOCUCNTL:
674			if (*(int *)data) {
675				if (pti->pt_flags & PF_PKT)
676					return (EINVAL);
677				pti->pt_flags |= PF_UCNTL;
678			} else
679				pti->pt_flags &= ~PF_UCNTL;
680			return (0);
681
682		case TIOCREMOTE:
683			if (*(int *)data)
684				pti->pt_flags |= PF_REMOTE;
685			else
686				pti->pt_flags &= ~PF_REMOTE;
687			ttyflush(tp, FREAD|FWRITE);
688			return (0);
689		}
690
691		/*
692		 * The rest of the ioctls shouldn't be called until
693		 * the slave is open.
694		 */
695		if ((tp->t_state & TS_ISOPEN) == 0)
696			return (EAGAIN);
697
698		switch (cmd) {
699#ifdef COMPAT_43
700		case TIOCSETP:
701		case TIOCSETN:
702#endif
703		case TIOCSETD:
704		case TIOCSETA:
705		case TIOCSETAW:
706		case TIOCSETAF:
707			/*
708			 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
709			 * ttywflush(tp) will hang if there are characters in
710			 * the outq.
711			 */
712			ndflush(&tp->t_outq, tp->t_outq.c_cc);
713			break;
714
715		case TIOCSIG:
716			if (*(unsigned int *)data >= NSIG ||
717			    *(unsigned int *)data == 0)
718				return(EINVAL);
719			if ((tp->t_lflag&NOFLSH) == 0)
720				ttyflush(tp, FREAD|FWRITE);
721			pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
722			if ((*(unsigned int *)data == SIGINFO) &&
723			    ((tp->t_lflag&NOKERNINFO) == 0))
724				ttyinfo(tp);
725			return(0);
726		}
727	}
728	if (cmd == TIOCEXT) {
729		/*
730		 * When the EXTPROC bit is being toggled, we need
731		 * to send an TIOCPKT_IOCTL if the packet driver
732		 * is turned on.
733		 */
734		if (*(int *)data) {
735			if (pti->pt_flags & PF_PKT) {
736				pti->pt_send |= TIOCPKT_IOCTL;
737				ptcwakeup(tp, FREAD);
738			}
739			tp->t_lflag |= EXTPROC;
740		} else {
741			if ((tp->t_lflag & EXTPROC) &&
742			    (pti->pt_flags & PF_PKT)) {
743				pti->pt_send |= TIOCPKT_IOCTL;
744				ptcwakeup(tp, FREAD);
745			}
746			tp->t_lflag &= ~EXTPROC;
747		}
748		return(0);
749	}
750	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
751	if (error == ENOIOCTL)
752		 error = ttioctl(tp, cmd, data, flag);
753	if (error == ENOIOCTL) {
754		if (pti->pt_flags & PF_UCNTL &&
755		    (cmd & ~0xff) == UIOCCMD(0)) {
756			if (cmd & 0xff) {
757				pti->pt_ucntl = (u_char)cmd;
758				ptcwakeup(tp, FREAD);
759			}
760			return (0);
761		}
762		error = ENOTTY;
763	}
764	/*
765	 * If external processing and packet mode send ioctl packet.
766	 */
767	if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
768		switch(cmd) {
769		case TIOCSETA:
770		case TIOCSETAW:
771		case TIOCSETAF:
772#ifdef COMPAT_43
773		case TIOCSETP:
774		case TIOCSETN:
775#endif
776#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
777		case TIOCSETC:
778		case TIOCSLTC:
779		case TIOCLBIS:
780		case TIOCLBIC:
781		case TIOCLSET:
782#endif
783			pti->pt_send |= TIOCPKT_IOCTL;
784			ptcwakeup(tp, FREAD);
785		default:
786			break;
787		}
788	}
789	stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
790		&& CCEQ(cc[VSTART], CTRL('q'));
791	if (pti->pt_flags & PF_NOSTOP) {
792		if (stop) {
793			pti->pt_send &= ~TIOCPKT_NOSTOP;
794			pti->pt_send |= TIOCPKT_DOSTOP;
795			pti->pt_flags &= ~PF_NOSTOP;
796			ptcwakeup(tp, FREAD);
797		}
798	} else {
799		if (!stop) {
800			pti->pt_send &= ~TIOCPKT_DOSTOP;
801			pti->pt_send |= TIOCPKT_NOSTOP;
802			pti->pt_flags |= PF_NOSTOP;
803			ptcwakeup(tp, FREAD);
804		}
805	}
806	return (error);
807}
808
809
810static void ptc_drvinit __P((void *unused));
811
812static void
813ptc_drvinit(unused)
814	void *unused;
815{
816	cdevsw_add(&pts_cdevsw);
817	cdevsw_add(&ptc_cdevsw);
818	/* XXX: Gross hack for DEVFS */
819	ptyinit(0);
820}
821
822SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)
823