xref: /netbsd-src/sys/kern/tty_pty.c (revision 37b34d511dea595d3ba03a661cf3b775038ea5f8)
1 /*	$NetBSD: tty_pty.c,v 1.64 2002/09/22 18:13:38 jdolecek Exp $	*/
2 
3 /*
4  * Copyright (c) 1982, 1986, 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)tty_pty.c	8.4 (Berkeley) 2/20/95
36  */
37 
38 /*
39  * Pseudo-teletype Driver
40  * (Actually two drivers, requiring two entries in 'cdevsw')
41  */
42 
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: tty_pty.c,v 1.64 2002/09/22 18:13:38 jdolecek Exp $");
45 
46 #include "opt_compat_sunos.h"
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/ioctl.h>
51 #include <sys/proc.h>
52 #include <sys/tty.h>
53 #include <sys/file.h>
54 #include <sys/uio.h>
55 #include <sys/kernel.h>
56 #include <sys/vnode.h>
57 #include <sys/signalvar.h>
58 #include <sys/uio.h>
59 #include <sys/conf.h>
60 #include <sys/poll.h>
61 #include <sys/malloc.h>
62 
63 #define	DEFAULT_NPTYS		16	/* default number of initial ptys */
64 #define DEFAULT_MAXPTYS		992	/* default maximum number of ptys */
65 
66 /* Macros to clear/set/test flags. */
67 #define	SET(t, f)	(t) |= (f)
68 #define	CLR(t, f)	(t) &= ~((unsigned)(f))
69 #define	ISSET(t, f)	((t) & (f))
70 
71 #define BUFSIZ 100		/* Chunk size iomoved to/from user */
72 
73 /*
74  * pts == /dev/tty[pqrs]?
75  * ptc == /dev/pty[pqrs]?
76  */
77 struct	pt_softc {
78 	struct	tty *pt_tty;
79 	int	pt_flags;
80 	struct	selinfo pt_selr, pt_selw;
81 	u_char	pt_send;
82 	u_char	pt_ucntl;
83 };
84 
85 static struct pt_softc **pt_softc = NULL;	/* pty array */
86 static int npty = 0;			/* for pstat -t */
87 static int maxptys = DEFAULT_MAXPTYS;	/* maximum number of ptys (sysctable) */
88 static struct simplelock pt_softc_mutex = SIMPLELOCK_INITIALIZER;
89 
90 #define	PF_PKT		0x08		/* packet mode */
91 #define	PF_STOPPED	0x10		/* user told stopped */
92 #define	PF_REMOTE	0x20		/* remote and flow controlled input */
93 #define	PF_NOSTOP	0x40
94 #define PF_UCNTL	0x80		/* user control mode */
95 
96 void	ptyattach __P((int));
97 void	ptcwakeup __P((struct tty *, int));
98 void	ptsstart __P((struct tty *));
99 int	pty_maxptys __P((int, int));
100 
101 static struct pt_softc **ptyarralloc __P((int));
102 static int check_pty __P((dev_t));
103 
104 dev_type_open(ptcopen);
105 dev_type_close(ptcclose);
106 dev_type_read(ptcread);
107 dev_type_write(ptcwrite);
108 dev_type_poll(ptcpoll);
109 
110 dev_type_open(ptsopen);
111 dev_type_close(ptsclose);
112 dev_type_read(ptsread);
113 dev_type_write(ptswrite);
114 dev_type_stop(ptsstop);
115 dev_type_poll(ptspoll);
116 
117 dev_type_ioctl(ptyioctl);
118 dev_type_tty(ptytty);
119 
120 const struct cdevsw ptc_cdevsw = {
121 	ptcopen, ptcclose, ptcread, ptcwrite, ptyioctl,
122 	nullstop, ptytty, ptcpoll, nommap, D_TTY
123 };
124 
125 const struct cdevsw pts_cdevsw = {
126 	ptsopen, ptsclose, ptsread, ptswrite, ptyioctl,
127 	ptsstop, ptytty, ptspoll, nommap, D_TTY
128 };
129 
130 #if defined(pmax)
131 const struct cdevsw ptc_ultrix_cdevsw = {
132 	ptcopen, ptcclose, ptcread, ptcwrite, ptyioctl,
133 	nullstop, ptytty, ptcpoll, nommap, D_TTY
134 };
135 
136 const struct cdevsw pts_ultrix_cdevsw = {
137 	ptsopen, ptsclose, ptsread, ptswrite, ptyioctl,
138 	ptsstop, ptytty, ptspoll, nommap, D_TTY
139 };
140 #endif /* defined(pmax) */
141 
142 /*
143  * Allocate and zero array of nelem elements.
144  */
145 static struct pt_softc **
146 ptyarralloc(nelem)
147 	int nelem;
148 {
149 	struct pt_softc **pt;
150 	nelem += 10;
151 	pt = malloc(nelem * sizeof(struct pt_softc *), M_DEVBUF, M_WAITOK);
152 	memset(pt, '\0', nelem * sizeof(struct pt_softc *));
153 	return pt;
154 }
155 
156 /*
157  * Check if the minor is correct and ensure necessary structures
158  * are properly allocated.
159  */
160 static int
161 check_pty(dev)
162 	dev_t dev;
163 {
164 	struct pt_softc *pti;
165 
166 	if (minor(dev) >= npty) {
167 		struct pt_softc **newpt;
168 		int newnpty;
169 
170 		/* check if the requested pty can be granted */
171 		if (minor(dev) >= maxptys) {
172 	    limit_reached:
173 			tablefull("pty", "increase kern.maxptys");
174 			return (ENXIO);
175 		}
176 
177 		/*
178 		 * Now grab the pty array mutex - we need to ensure
179 		 * that the pty array is consistent while copying it's
180 		 * content to newly allocated, larger space; we also
181 		 * need to be safe against pty_maxptys().
182 		 */
183 		simple_lock(&pt_softc_mutex);
184 
185 		do {
186 			for(newnpty = npty; newnpty <= minor(dev);
187 				newnpty *= 2);
188 
189 			if (newnpty > maxptys)
190 				newnpty = maxptys;
191 
192 			simple_unlock(&pt_softc_mutex);
193 			newpt = ptyarralloc(newnpty);
194 			simple_lock(&pt_softc_mutex);
195 
196 			if (maxptys == npty) {
197 				simple_unlock(&pt_softc_mutex);
198 				goto limit_reached;
199 			}
200 		} while(newnpty > maxptys);
201 
202 		/*
203 		 * If the pty array was not enlarged while we were waiting
204 		 * for mutex, copy current contents of pt_softc[] to newly
205 		 * allocated array and start using the new bigger array.
206 		 */
207 		if (minor(dev) >= npty) {
208 			memcpy(newpt, pt_softc, npty*sizeof(struct pt_softc *));
209 			free(pt_softc, M_DEVBUF);
210 
211 			pt_softc = newpt;
212 			npty = newnpty;
213 		} else {
214 			/* was enlarged when waited fot lock, free new space */
215 			free(newpt, M_DEVBUF);
216 		}
217 
218 		simple_unlock(&pt_softc_mutex);
219 	}
220 
221 	/*
222 	 * If the entry is not yet allocated, allocate one. The mutex is
223 	 * needed so that the state of pt_softc[] array is consistant
224 	 * in case it has been longened above.
225 	 */
226 	if (!pt_softc[minor(dev)]) {
227 		MALLOC(pti, struct pt_softc *, sizeof(struct pt_softc),
228 			M_DEVBUF, M_WAITOK);
229 		memset(pti, 0, sizeof(struct pt_softc));
230 
231 	 	pti->pt_tty = ttymalloc();
232 
233 		simple_lock(&pt_softc_mutex);
234 
235 		/*
236 		 * Check the entry again - it might have been
237 		 * added while we were waiting for mutex.
238 		 */
239 		if (!pt_softc[minor(dev)]) {
240 			tty_attach(pti->pt_tty);
241 			pt_softc[minor(dev)] = pti;
242 		} else {
243 			ttyfree(pti->pt_tty);
244 			FREE(pti, M_DEVBUF);
245 		}
246 
247 		simple_unlock(&pt_softc_mutex);
248 	}
249 
250 	return (0);
251 }
252 
253 /*
254  * Set maxpty in thread-safe way. Returns 0 in case of error, otherwise
255  * new value of maxptys.
256  */
257 int
258 pty_maxptys(newmax, set)
259 	int newmax, set;
260 {
261 	if (!set)
262 		return (maxptys);
263 
264 	/* the value cannot be set to value lower than current number of ptys */
265 	if (newmax < npty)
266 		return (0);
267 
268 	/* can proceed immediatelly if bigger than current maximum */
269 	if (newmax > maxptys) {
270 		maxptys = newmax;
271 		return (maxptys);
272 	}
273 
274 	/*
275 	 * We have to grab the pt_softc lock, so that we would pick correct
276 	 * value of npty (might be modified in check_pty()).
277 	 */
278 	simple_lock(&pt_softc_mutex);
279 
280 	if (newmax > npty)
281 		maxptys = newmax;
282 
283 	simple_unlock(&pt_softc_mutex);
284 
285 	return (maxptys);
286 }
287 
288 /*
289  * Establish n (or default if n is 1) ptys in the system.
290  */
291 void
292 ptyattach(n)
293 	int n;
294 {
295 	/* maybe should allow 0 => none? */
296 	if (n <= 1)
297 		n = DEFAULT_NPTYS;
298 	pt_softc = ptyarralloc(n);
299 	npty = n;
300 }
301 
302 /*ARGSUSED*/
303 int
304 ptsopen(dev, flag, devtype, p)
305 	dev_t dev;
306 	int flag, devtype;
307 	struct proc *p;
308 {
309 	struct pt_softc *pti;
310 	struct tty *tp;
311 	int error;
312 
313 	if ((error = check_pty(dev)))
314 		return (error);
315 
316 	pti = pt_softc[minor(dev)];
317 	tp = pti->pt_tty;
318 
319 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
320 		ttychars(tp);		/* Set up default chars */
321 		tp->t_iflag = TTYDEF_IFLAG;
322 		tp->t_oflag = TTYDEF_OFLAG;
323 		tp->t_lflag = TTYDEF_LFLAG;
324 		tp->t_cflag = TTYDEF_CFLAG;
325 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
326 		ttsetwater(tp);		/* would be done in xxparam() */
327 	} else if (ISSET(tp->t_state, TS_XCLUDE) && p->p_ucred->cr_uid != 0)
328 		return (EBUSY);
329 	if (tp->t_oproc)			/* Ctrlr still around. */
330 		SET(tp->t_state, TS_CARR_ON);
331 	if (!ISSET(flag, O_NONBLOCK))
332 		while (!ISSET(tp->t_state, TS_CARR_ON)) {
333 			tp->t_wopen++;
334 			error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
335 			    ttopen, 0);
336 			tp->t_wopen--;
337 			if (error)
338 				return (error);
339 		}
340 	error = (*tp->t_linesw->l_open)(dev, tp);
341 	ptcwakeup(tp, FREAD|FWRITE);
342 	return (error);
343 }
344 
345 int
346 ptsclose(dev, flag, mode, p)
347 	dev_t dev;
348 	int flag, mode;
349 	struct proc *p;
350 {
351 	struct pt_softc *pti = pt_softc[minor(dev)];
352 	struct tty *tp = pti->pt_tty;
353 	int error;
354 
355 	error = (*tp->t_linesw->l_close)(tp, flag);
356 	error |= ttyclose(tp);
357 	ptcwakeup(tp, FREAD|FWRITE);
358 	return (error);
359 }
360 
361 int
362 ptsread(dev, uio, flag)
363 	dev_t dev;
364 	struct uio *uio;
365 	int flag;
366 {
367 	struct proc *p = curproc;
368 	struct pt_softc *pti = pt_softc[minor(dev)];
369 	struct tty *tp = pti->pt_tty;
370 	int error = 0;
371 
372 again:
373 	if (pti->pt_flags & PF_REMOTE) {
374 		while (isbackground(p, tp)) {
375 			if (sigismasked(p, SIGTTIN) ||
376 			    p->p_pgrp->pg_jobc == 0 ||
377 			    p->p_flag & P_PPWAIT)
378 				return (EIO);
379 			pgsignal(p->p_pgrp, SIGTTIN, 1);
380 			error = ttysleep(tp, (caddr_t)&lbolt,
381 					 TTIPRI | PCATCH, ttybg, 0);
382 			if (error)
383 				return (error);
384 		}
385 		if (tp->t_canq.c_cc == 0) {
386 			if (flag & IO_NDELAY)
387 				return (EWOULDBLOCK);
388 			error = ttysleep(tp, (caddr_t)&tp->t_canq,
389 					 TTIPRI | PCATCH, ttyin, 0);
390 			if (error)
391 				return (error);
392 			goto again;
393 		}
394 		while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
395 			if (ureadc(getc(&tp->t_canq), uio) < 0) {
396 				error = EFAULT;
397 				break;
398 			}
399 		if (tp->t_canq.c_cc == 1)
400 			(void) getc(&tp->t_canq);
401 		if (tp->t_canq.c_cc)
402 			return (error);
403 	} else
404 		if (tp->t_oproc)
405 			error = (*tp->t_linesw->l_read)(tp, uio, flag);
406 	ptcwakeup(tp, FWRITE);
407 	return (error);
408 }
409 
410 /*
411  * Write to pseudo-tty.
412  * Wakeups of controlling tty will happen
413  * indirectly, when tty driver calls ptsstart.
414  */
415 int
416 ptswrite(dev, uio, flag)
417 	dev_t dev;
418 	struct uio *uio;
419 	int flag;
420 {
421 	struct pt_softc *pti = pt_softc[minor(dev)];
422 	struct tty *tp = pti->pt_tty;
423 
424 	if (tp->t_oproc == 0)
425 		return (EIO);
426 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
427 }
428 
429 /*
430  * Poll pseudo-tty.
431  */
432 int
433 ptspoll(dev, events, p)
434 	dev_t dev;
435 	int events;
436 	struct proc *p;
437 {
438 	struct pt_softc *pti = pt_softc[minor(dev)];
439 	struct tty *tp = pti->pt_tty;
440 
441 	if (tp->t_oproc == 0)
442 		return (EIO);
443 
444 	return ((*tp->t_linesw->l_poll)(tp, events, p));
445 }
446 
447 /*
448  * Start output on pseudo-tty.
449  * Wake up process polling or sleeping for input from controlling tty.
450  */
451 void
452 ptsstart(tp)
453 	struct tty *tp;
454 {
455 	struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
456 
457 	if (ISSET(tp->t_state, TS_TTSTOP))
458 		return;
459 	if (pti->pt_flags & PF_STOPPED) {
460 		pti->pt_flags &= ~PF_STOPPED;
461 		pti->pt_send = TIOCPKT_START;
462 	}
463 	ptcwakeup(tp, FREAD);
464 }
465 
466 void
467 ptsstop(tp, flush)
468 	struct tty *tp;
469 	int flush;
470 {
471 	struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
472 	int flag;
473 
474 	/* note: FLUSHREAD and FLUSHWRITE already ok */
475 	if (flush == 0) {
476 		flush = TIOCPKT_STOP;
477 		pti->pt_flags |= PF_STOPPED;
478 	} else
479 		pti->pt_flags &= ~PF_STOPPED;
480 	pti->pt_send |= flush;
481 	/* change of perspective */
482 	flag = 0;
483 	if (flush & FREAD)
484 		flag |= FWRITE;
485 	if (flush & FWRITE)
486 		flag |= FREAD;
487 	ptcwakeup(tp, flag);
488 }
489 
490 void
491 ptcwakeup(tp, flag)
492 	struct tty *tp;
493 	int flag;
494 {
495 	struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
496 
497 	if (flag & FREAD) {
498 		selwakeup(&pti->pt_selr);
499 		wakeup((caddr_t)&tp->t_outq.c_cf);
500 	}
501 	if (flag & FWRITE) {
502 		selwakeup(&pti->pt_selw);
503 		wakeup((caddr_t)&tp->t_rawq.c_cf);
504 	}
505 }
506 
507 /*ARGSUSED*/
508 int
509 ptcopen(dev, flag, devtype, p)
510 	dev_t dev;
511 	int flag, devtype;
512 	struct proc *p;
513 {
514 	struct pt_softc *pti;
515 	struct tty *tp;
516 	int error;
517 
518 	if ((error = check_pty(dev)))
519 		return (error);
520 
521 	pti = pt_softc[minor(dev)];
522 	tp = pti->pt_tty;
523 
524 	if (tp->t_oproc)
525 		return (EIO);
526 	tp->t_oproc = ptsstart;
527 	(void)(*tp->t_linesw->l_modem)(tp, 1);
528 	CLR(tp->t_lflag, EXTPROC);
529 	pti->pt_flags = 0;
530 	pti->pt_send = 0;
531 	pti->pt_ucntl = 0;
532 	return (0);
533 }
534 
535 /*ARGSUSED*/
536 int
537 ptcclose(dev, flag, devtype, p)
538 	dev_t dev;
539 	int flag, devtype;
540 	struct proc *p;
541 {
542 	struct pt_softc *pti = pt_softc[minor(dev)];
543 	struct tty *tp = pti->pt_tty;
544 
545 	(void)(*tp->t_linesw->l_modem)(tp, 0);
546 	CLR(tp->t_state, TS_CARR_ON);
547 	tp->t_oproc = 0;		/* mark closed */
548 	return (0);
549 }
550 
551 int
552 ptcread(dev, uio, flag)
553 	dev_t dev;
554 	struct uio *uio;
555 	int flag;
556 {
557 	struct pt_softc *pti = pt_softc[minor(dev)];
558 	struct tty *tp = pti->pt_tty;
559 	char buf[BUFSIZ];
560 	int error = 0, cc;
561 
562 	/*
563 	 * We want to block until the slave
564 	 * is open, and there's something to read;
565 	 * but if we lost the slave or we're NBIO,
566 	 * then return the appropriate error instead.
567 	 */
568 	for (;;) {
569 		if (ISSET(tp->t_state, TS_ISOPEN)) {
570 			if (pti->pt_flags&PF_PKT && pti->pt_send) {
571 				error = ureadc((int)pti->pt_send, uio);
572 				if (error)
573 					return (error);
574 				if (pti->pt_send & TIOCPKT_IOCTL) {
575 					cc = min(uio->uio_resid,
576 						sizeof(tp->t_termios));
577 					uiomove((caddr_t) &tp->t_termios,
578 						cc, uio);
579 				}
580 				pti->pt_send = 0;
581 				return (0);
582 			}
583 			if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
584 				error = ureadc((int)pti->pt_ucntl, uio);
585 				if (error)
586 					return (error);
587 				pti->pt_ucntl = 0;
588 				return (0);
589 			}
590 			if (tp->t_outq.c_cc && !ISSET(tp->t_state, TS_TTSTOP))
591 				break;
592 		}
593 		if (!ISSET(tp->t_state, TS_CARR_ON))
594 			return (0);	/* EOF */
595 		if (flag & IO_NDELAY)
596 			return (EWOULDBLOCK);
597 		error = tsleep((caddr_t)&tp->t_outq.c_cf, TTIPRI | PCATCH,
598 			       ttyin, 0);
599 		if (error)
600 			return (error);
601 	}
602 	if (pti->pt_flags & (PF_PKT|PF_UCNTL))
603 		error = ureadc(0, uio);
604 	while (uio->uio_resid > 0 && error == 0) {
605 		cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
606 		if (cc <= 0)
607 			break;
608 		error = uiomove(buf, cc, uio);
609 	}
610 	if (tp->t_outq.c_cc <= tp->t_lowat) {
611 		if (ISSET(tp->t_state, TS_ASLEEP)) {
612 			CLR(tp->t_state, TS_ASLEEP);
613 			wakeup((caddr_t)&tp->t_outq);
614 		}
615 		selwakeup(&tp->t_wsel);
616 	}
617 	return (error);
618 }
619 
620 
621 int
622 ptcwrite(dev, uio, flag)
623 	dev_t dev;
624 	struct uio *uio;
625 	int flag;
626 {
627 	struct pt_softc *pti = pt_softc[minor(dev)];
628 	struct tty *tp = pti->pt_tty;
629 	u_char *cp = NULL;
630 	int cc = 0;
631 	u_char locbuf[BUFSIZ];
632 	int cnt = 0;
633 	int error = 0;
634 
635 again:
636 	if (!ISSET(tp->t_state, TS_ISOPEN))
637 		goto block;
638 	if (pti->pt_flags & PF_REMOTE) {
639 		if (tp->t_canq.c_cc)
640 			goto block;
641 		while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) {
642 			if (cc == 0) {
643 				cc = min(uio->uio_resid, BUFSIZ);
644 				cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
645 				cp = locbuf;
646 				error = uiomove((caddr_t)cp, cc, uio);
647 				if (error)
648 					return (error);
649 				/* check again for safety */
650 				if (!ISSET(tp->t_state, TS_ISOPEN))
651 					return (EIO);
652 			}
653 			if (cc)
654 				(void) b_to_q((char *)cp, cc, &tp->t_canq);
655 			cc = 0;
656 		}
657 		(void) putc(0, &tp->t_canq);
658 		ttwakeup(tp);
659 		wakeup((caddr_t)&tp->t_canq);
660 		return (0);
661 	}
662 	while (uio->uio_resid > 0) {
663 		if (cc == 0) {
664 			cc = min(uio->uio_resid, BUFSIZ);
665 			cp = locbuf;
666 			error = uiomove((caddr_t)cp, cc, uio);
667 			if (error)
668 				return (error);
669 			/* check again for safety */
670 			if (!ISSET(tp->t_state, TS_ISOPEN))
671 				return (EIO);
672 		}
673 		while (cc > 0) {
674 			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
675 			   (tp->t_canq.c_cc > 0 || !ISSET(tp->t_lflag, ICANON))) {
676 				wakeup((caddr_t)&tp->t_rawq);
677 				goto block;
678 			}
679 			(*tp->t_linesw->l_rint)(*cp++, tp);
680 			cnt++;
681 			cc--;
682 		}
683 		cc = 0;
684 	}
685 	return (0);
686 block:
687 	/*
688 	 * Come here to wait for slave to open, for space
689 	 * in outq, or space in rawq.
690 	 */
691 	if (!ISSET(tp->t_state, TS_CARR_ON))
692 		return (EIO);
693 	if (flag & IO_NDELAY) {
694 		/* adjust for data copied in but not written */
695 		uio->uio_resid += cc;
696 		if (cnt == 0)
697 			return (EWOULDBLOCK);
698 		return (0);
699 	}
700 	error = tsleep((caddr_t)&tp->t_rawq.c_cf, TTOPRI | PCATCH,
701 		       ttyout, 0);
702 	if (error) {
703 		/* adjust for data copied in but not written */
704 		uio->uio_resid += cc;
705 		return (error);
706 	}
707 	goto again;
708 }
709 
710 int
711 ptcpoll(dev, events, p)
712 	dev_t dev;
713 	int events;
714 	struct proc *p;
715 {
716 	struct pt_softc *pti = pt_softc[minor(dev)];
717 	struct tty *tp = pti->pt_tty;
718 	int revents = 0;
719 	int s = splsoftclock();
720 
721 	if (events & (POLLIN | POLLRDNORM))
722 		if (ISSET(tp->t_state, TS_ISOPEN) &&
723 		    ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
724 		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
725 		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
726 			revents |= events & (POLLIN | POLLRDNORM);
727 
728 	if (events & (POLLOUT | POLLWRNORM))
729 		if (ISSET(tp->t_state, TS_ISOPEN) &&
730 		    ((pti->pt_flags & PF_REMOTE) ?
731 		     (tp->t_canq.c_cc == 0) :
732 		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
733 		      (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))))
734 			revents |= events & (POLLOUT | POLLWRNORM);
735 
736 	if (events & POLLHUP)
737 		if (!ISSET(tp->t_state, TS_CARR_ON))
738 			revents |= POLLHUP;
739 
740 	if (revents == 0) {
741 		if (events & (POLLIN | POLLHUP | POLLRDNORM))
742 			selrecord(p, &pti->pt_selr);
743 
744 		if (events & (POLLOUT | POLLWRNORM))
745 			selrecord(p, &pti->pt_selw);
746 	}
747 
748 	splx(s);
749 	return (revents);
750 }
751 
752 
753 struct tty *
754 ptytty(dev)
755 	dev_t dev;
756 {
757 	struct pt_softc *pti = pt_softc[minor(dev)];
758 	struct tty *tp = pti->pt_tty;
759 
760 	return (tp);
761 }
762 
763 /*ARGSUSED*/
764 int
765 ptyioctl(dev, cmd, data, flag, p)
766 	dev_t dev;
767 	u_long cmd;
768 	caddr_t data;
769 	int flag;
770 	struct proc *p;
771 {
772 	struct pt_softc *pti = pt_softc[minor(dev)];
773 	struct tty *tp = pti->pt_tty;
774 	const struct cdevsw *cdev;
775 	u_char *cc = tp->t_cc;
776 	int stop, error, sig;
777 
778 	cdev = cdevsw_lookup(dev);
779 	/*
780 	 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
781 	 * ttywflush(tp) will hang if there are characters in the outq.
782 	 */
783 	if (cmd == TIOCEXT) {
784 		/*
785 		 * When the EXTPROC bit is being toggled, we need
786 		 * to send an TIOCPKT_IOCTL if the packet driver
787 		 * is turned on.
788 		 */
789 		if (*(int *)data) {
790 			if (pti->pt_flags & PF_PKT) {
791 				pti->pt_send |= TIOCPKT_IOCTL;
792 				ptcwakeup(tp, FREAD);
793 			}
794 			SET(tp->t_lflag, EXTPROC);
795 		} else {
796 			if (ISSET(tp->t_lflag, EXTPROC) &&
797 			    (pti->pt_flags & PF_PKT)) {
798 				pti->pt_send |= TIOCPKT_IOCTL;
799 				ptcwakeup(tp, FREAD);
800 			}
801 			CLR(tp->t_lflag, EXTPROC);
802 		}
803 		return(0);
804 	} else
805 	if (cdev != NULL && cdev->d_open == ptcopen)
806 		switch (cmd) {
807 
808 		case TIOCGPGRP:
809 			/*
810 			 * We avoid calling ttioctl on the controller since,
811 			 * in that case, tp must be the controlling terminal.
812 			 */
813 			*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
814 			return (0);
815 
816 		case TIOCPKT:
817 			if (*(int *)data) {
818 				if (pti->pt_flags & PF_UCNTL)
819 					return (EINVAL);
820 				pti->pt_flags |= PF_PKT;
821 			} else
822 				pti->pt_flags &= ~PF_PKT;
823 			return (0);
824 
825 		case TIOCUCNTL:
826 			if (*(int *)data) {
827 				if (pti->pt_flags & PF_PKT)
828 					return (EINVAL);
829 				pti->pt_flags |= PF_UCNTL;
830 			} else
831 				pti->pt_flags &= ~PF_UCNTL;
832 			return (0);
833 
834 		case TIOCREMOTE:
835 			if (*(int *)data)
836 				pti->pt_flags |= PF_REMOTE;
837 			else
838 				pti->pt_flags &= ~PF_REMOTE;
839 			ttyflush(tp, FREAD|FWRITE);
840 			return (0);
841 
842 #ifdef COMPAT_OLDTTY
843 		case TIOCSETP:
844 		case TIOCSETN:
845 #endif
846 		case TIOCSETD:
847 		case TIOCSETA:
848 		case TIOCSETAW:
849 		case TIOCSETAF:
850 			ndflush(&tp->t_outq, tp->t_outq.c_cc);
851 			break;
852 
853 		case TIOCSIG:
854 			sig = (int)(long)*(caddr_t *)data;
855 			if (sig <= 0 || sig >= NSIG)
856 				return (EINVAL);
857 			if (!ISSET(tp->t_lflag, NOFLSH))
858 				ttyflush(tp, FREAD|FWRITE);
859 			if ((sig == SIGINFO) &&
860 			    (!ISSET(tp->t_lflag, NOKERNINFO)))
861 				ttyinfo(tp);
862 			pgsignal(tp->t_pgrp, sig, 1);
863 			return(0);
864 		}
865 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
866 	if (error == EPASSTHROUGH)
867 		 error = ttioctl(tp, cmd, data, flag, p);
868 	if (error == EPASSTHROUGH) {
869 		if (pti->pt_flags & PF_UCNTL &&
870 		    (cmd & ~0xff) == UIOCCMD(0)) {
871 			if (cmd & 0xff) {
872 				pti->pt_ucntl = (u_char)cmd;
873 				ptcwakeup(tp, FREAD);
874 			}
875 			return (0);
876 		}
877 	}
878 	/*
879 	 * If external processing and packet mode send ioctl packet.
880 	 */
881 	if (ISSET(tp->t_lflag, EXTPROC) && (pti->pt_flags & PF_PKT)) {
882 		switch(cmd) {
883 		case TIOCSETA:
884 		case TIOCSETAW:
885 		case TIOCSETAF:
886 #ifdef COMPAT_OLDTTY
887 		case TIOCSETP:
888 		case TIOCSETN:
889 		case TIOCSETC:
890 		case TIOCSLTC:
891 		case TIOCLBIS:
892 		case TIOCLBIC:
893 		case TIOCLSET:
894 #endif
895 			pti->pt_send |= TIOCPKT_IOCTL;
896 			ptcwakeup(tp, FREAD);
897 		default:
898 			break;
899 		}
900 	}
901 	stop = ISSET(tp->t_iflag, IXON) && CCEQ(cc[VSTOP], CTRL('s'))
902 		&& CCEQ(cc[VSTART], CTRL('q'));
903 	if (pti->pt_flags & PF_NOSTOP) {
904 		if (stop) {
905 			pti->pt_send &= ~TIOCPKT_NOSTOP;
906 			pti->pt_send |= TIOCPKT_DOSTOP;
907 			pti->pt_flags &= ~PF_NOSTOP;
908 			ptcwakeup(tp, FREAD);
909 		}
910 	} else {
911 		if (!stop) {
912 			pti->pt_send &= ~TIOCPKT_DOSTOP;
913 			pti->pt_send |= TIOCPKT_NOSTOP;
914 			pti->pt_flags |= PF_NOSTOP;
915 			ptcwakeup(tp, FREAD);
916 		}
917 	}
918 	return (error);
919 }
920