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