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