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