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