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