pty.c revision 29354
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.45 1997/09/02 20:05:55 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/poll.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_poll_t	ptcpoll;
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	  ttpoll,	nommap,		NULL,	"pts",	NULL,	-1 };
86
87static struct cdevsw ptc_cdevsw =
88	{ ptcopen,	ptcclose,	ptcread,	ptcwrite,	/*6*/
89	  ptyioctl,	nullstop,	nullreset,	ptydevtotty,/* ptyp */
90	  ptcpoll,	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
463ptcpoll(dev, events, p)
464	dev_t dev;
465	int events;
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 revents = 0;
471	int s;
472
473	if ((tp->t_state & TS_CONNECTED) == 0)
474		return (seltrue(dev, events, p) | POLLHUP);
475
476	/*
477	 * Need to block timeouts (ttrstart).
478	 */
479	s = spltty();
480
481	if (events & (POLLIN | POLLRDNORM))
482		if ((tp->t_state & TS_ISOPEN) &&
483		    ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
484		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
485		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
486			revents |= events & (POLLIN | POLLRDNORM);
487
488	if (events & (POLLOUT | POLLWRNORM))
489		if (tp->t_state & TS_ISOPEN &&
490		    ((pti->pt_flags & PF_REMOTE) ?
491		     (tp->t_canq.c_cc == 0) :
492		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
493		      (tp->t_canq.c_cc == 0 && (tp->t_iflag & ICANON)))))
494			revents |= events & (POLLOUT | POLLWRNORM);
495
496	if (events & POLLHUP)
497		if ((tp->t_state & TS_CARR_ON) == 0)
498			revents |= POLLHUP;
499
500	if (revents == 0) {
501		if (events & (POLLIN | POLLRDNORM))
502			selrecord(p, &pti->pt_selr);
503
504		if (events & (POLLOUT | POLLWRNORM))
505			selrecord(p, &pti->pt_selw);
506	}
507	splx(s);
508
509	return (revents);
510}
511
512static	int
513ptcwrite(dev, uio, flag)
514	dev_t dev;
515	register struct uio *uio;
516	int flag;
517{
518	register struct tty *tp = &pt_tty[minor(dev)];
519	register u_char *cp = 0;
520	register int cc = 0;
521	u_char locbuf[BUFSIZ];
522	int cnt = 0;
523	struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
524	int error = 0;
525
526again:
527	if ((tp->t_state&TS_ISOPEN) == 0)
528		goto block;
529	if (pti->pt_flags & PF_REMOTE) {
530		if (tp->t_canq.c_cc)
531			goto block;
532		while ((uio->uio_resid > 0 || cc > 0) &&
533		       tp->t_canq.c_cc < TTYHOG - 1) {
534			if (cc == 0) {
535				cc = min(uio->uio_resid, BUFSIZ);
536				cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
537				cp = locbuf;
538				error = uiomove((caddr_t)cp, cc, uio);
539				if (error)
540					return (error);
541				/* check again for safety */
542				if ((tp->t_state & TS_ISOPEN) == 0) {
543					/* adjust as usual */
544					uio->uio_resid += cc;
545					return (EIO);
546				}
547			}
548			if (cc > 0) {
549				cc = b_to_q((char *)cp, cc, &tp->t_canq);
550				/*
551				 * XXX we don't guarantee that the canq size
552				 * is >= TTYHOG, so the above b_to_q() may
553				 * leave some bytes uncopied.  However, space
554				 * is guaranteed for the null terminator if
555				 * we don't fail here since (TTYHOG - 1) is
556				 * not a multiple of CBSIZE.
557				 */
558				if (cc > 0)
559					break;
560			}
561		}
562		/* adjust for data copied in but not written */
563		uio->uio_resid += cc;
564		(void) putc(0, &tp->t_canq);
565		ttwakeup(tp);
566		wakeup(TSA_PTS_READ(tp));
567		return (0);
568	}
569	while (uio->uio_resid > 0 || cc > 0) {
570		if (cc == 0) {
571			cc = min(uio->uio_resid, BUFSIZ);
572			cp = locbuf;
573			error = uiomove((caddr_t)cp, cc, uio);
574			if (error)
575				return (error);
576			/* check again for safety */
577			if ((tp->t_state & TS_ISOPEN) == 0) {
578				/* adjust for data copied in but not written */
579				uio->uio_resid += cc;
580				return (EIO);
581			}
582		}
583		while (cc > 0) {
584			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
585			   (tp->t_canq.c_cc > 0 || !(tp->t_iflag&ICANON))) {
586				wakeup(TSA_HUP_OR_INPUT(tp));
587				goto block;
588			}
589			(*linesw[tp->t_line].l_rint)(*cp++, tp);
590			cnt++;
591			cc--;
592		}
593		cc = 0;
594	}
595	return (0);
596block:
597	/*
598	 * Come here to wait for slave to open, for space
599	 * in outq, or space in rawq, or an empty canq.
600	 */
601	if ((tp->t_state & TS_CONNECTED) == 0) {
602		/* adjust for data copied in but not written */
603		uio->uio_resid += cc;
604		return (EIO);
605	}
606	if (flag & IO_NDELAY) {
607		/* adjust for data copied in but not written */
608		uio->uio_resid += cc;
609		if (cnt == 0)
610			return (EWOULDBLOCK);
611		return (0);
612	}
613	error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0);
614	if (error) {
615		/* adjust for data copied in but not written */
616		uio->uio_resid += cc;
617		return (error);
618	}
619	goto again;
620}
621
622static	struct tty *
623ptydevtotty(dev)
624	dev_t		dev;
625{
626	if (minor(dev) >= npty)
627		return (NULL);
628
629	return &pt_tty[minor(dev)];
630}
631
632/*ARGSUSED*/
633static	int
634ptyioctl(dev, cmd, data, flag, p)
635	dev_t dev;
636	int cmd;
637	caddr_t data;
638	int flag;
639	struct proc *p;
640{
641	register struct tty *tp = &pt_tty[minor(dev)];
642	register struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
643	register u_char *cc = tp->t_cc;
644	int stop, error;
645
646	/*
647	 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
648	 * ttywflush(tp) will hang if there are characters in the outq.
649	 */
650	if (cmd == TIOCEXT) {
651		/*
652		 * When the EXTPROC bit is being toggled, we need
653		 * to send an TIOCPKT_IOCTL if the packet driver
654		 * is turned on.
655		 */
656		if (*(int *)data) {
657			if (pti->pt_flags & PF_PKT) {
658				pti->pt_send |= TIOCPKT_IOCTL;
659				ptcwakeup(tp, FREAD);
660			}
661			tp->t_lflag |= EXTPROC;
662		} else {
663			if ((tp->t_lflag & EXTPROC) &&
664			    (pti->pt_flags & PF_PKT)) {
665				pti->pt_send |= TIOCPKT_IOCTL;
666				ptcwakeup(tp, FREAD);
667			}
668			tp->t_lflag &= ~EXTPROC;
669		}
670		return(0);
671	} else
672	if (cdevsw[major(dev)]->d_open == ptcopen)
673		switch (cmd) {
674
675		case TIOCGPGRP:
676			/*
677			 * We aviod calling ttioctl on the controller since,
678			 * in that case, tp must be the controlling terminal.
679			 */
680			*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
681			return (0);
682
683		case TIOCPKT:
684			if (*(int *)data) {
685				if (pti->pt_flags & PF_UCNTL)
686					return (EINVAL);
687				pti->pt_flags |= PF_PKT;
688			} else
689				pti->pt_flags &= ~PF_PKT;
690			return (0);
691
692		case TIOCUCNTL:
693			if (*(int *)data) {
694				if (pti->pt_flags & PF_PKT)
695					return (EINVAL);
696				pti->pt_flags |= PF_UCNTL;
697			} else
698				pti->pt_flags &= ~PF_UCNTL;
699			return (0);
700
701		case TIOCREMOTE:
702			if (*(int *)data)
703				pti->pt_flags |= PF_REMOTE;
704			else
705				pti->pt_flags &= ~PF_REMOTE;
706			ttyflush(tp, FREAD|FWRITE);
707			return (0);
708
709#ifdef COMPAT_43
710		case TIOCSETP:
711		case TIOCSETN:
712#endif
713		case TIOCSETD:
714		case TIOCSETA:
715		case TIOCSETAW:
716		case TIOCSETAF:
717			ndflush(&tp->t_outq, tp->t_outq.c_cc);
718			break;
719
720		case TIOCSIG:
721			if (*(unsigned int *)data >= NSIG ||
722			    *(unsigned int *)data == 0)
723				return(EINVAL);
724			if ((tp->t_lflag&NOFLSH) == 0)
725				ttyflush(tp, FREAD|FWRITE);
726			pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
727			if ((*(unsigned int *)data == SIGINFO) &&
728			    ((tp->t_lflag&NOKERNINFO) == 0))
729				ttyinfo(tp);
730			return(0);
731		}
732	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
733	if (error < 0)
734		 error = ttioctl(tp, cmd, data, flag);
735	if (error < 0) {
736		if (pti->pt_flags & PF_UCNTL &&
737		    (cmd & ~0xff) == UIOCCMD(0)) {
738			if (cmd & 0xff) {
739				pti->pt_ucntl = (u_char)cmd;
740				ptcwakeup(tp, FREAD);
741			}
742			return (0);
743		}
744		error = ENOTTY;
745	}
746	/*
747	 * If external processing and packet mode send ioctl packet.
748	 */
749	if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
750		switch(cmd) {
751		case TIOCSETA:
752		case TIOCSETAW:
753		case TIOCSETAF:
754#ifdef COMPAT_43
755		case TIOCSETP:
756		case TIOCSETN:
757#endif
758#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
759		case TIOCSETC:
760		case TIOCSLTC:
761		case TIOCLBIS:
762		case TIOCLBIC:
763		case TIOCLSET:
764#endif
765			pti->pt_send |= TIOCPKT_IOCTL;
766			ptcwakeup(tp, FREAD);
767		default:
768			break;
769		}
770	}
771	stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
772		&& CCEQ(cc[VSTART], CTRL('q'));
773	if (pti->pt_flags & PF_NOSTOP) {
774		if (stop) {
775			pti->pt_send &= ~TIOCPKT_NOSTOP;
776			pti->pt_send |= TIOCPKT_DOSTOP;
777			pti->pt_flags &= ~PF_NOSTOP;
778			ptcwakeup(tp, FREAD);
779		}
780	} else {
781		if (!stop) {
782			pti->pt_send &= ~TIOCPKT_DOSTOP;
783			pti->pt_send |= TIOCPKT_NOSTOP;
784			pti->pt_flags |= PF_NOSTOP;
785			ptcwakeup(tp, FREAD);
786		}
787	}
788	return (error);
789}
790
791static ptc_devsw_installed = 0;
792
793static void
794ptc_drvinit(void *unused)
795{
796#ifdef DEVFS
797	int i,j,k;
798#endif
799	dev_t dev;
800
801	if( ! ptc_devsw_installed ) {
802		dev = makedev(CDEV_MAJOR_S, 0);
803		cdevsw_add(&dev, &pts_cdevsw, NULL);
804		dev = makedev(CDEV_MAJOR_C, 0);
805		cdevsw_add(&dev, &ptc_cdevsw, NULL);
806		ptc_devsw_installed = 1;
807#ifdef DEVFS
808		for ( i = 0 ; i<NPTY ; i++ ) {
809			j = i / 32;
810			k = i % 32;
811			devfs_token_pts[i] =
812				devfs_add_devswf(&pts_cdevsw,i,
813						DV_CHR,0,0,0666,
814						"tty%c%n",jnames[j],k);
815			devfs_token_ptc[i] =
816				devfs_add_devswf(&ptc_cdevsw,i,
817						DV_CHR,0,0,0666,
818						"pty%c%n",jnames[j],k);
819		}
820#endif
821    	}
822}
823
824SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)
825