xref: /netbsd/sys/kern/tty_pty.c (revision c4a72b64)
1 /*	$NetBSD: tty_pty.c,v 1.66 2002/11/26 18:44:35 christos 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.66 2002/11/26 18:44:35 christos 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 dev_type_kqfilter(ptckqfilter);
110 
111 dev_type_open(ptsopen);
112 dev_type_close(ptsclose);
113 dev_type_read(ptsread);
114 dev_type_write(ptswrite);
115 dev_type_stop(ptsstop);
116 dev_type_poll(ptspoll);
117 
118 dev_type_ioctl(ptyioctl);
119 dev_type_tty(ptytty);
120 
121 const struct cdevsw ptc_cdevsw = {
122 	ptcopen, ptcclose, ptcread, ptcwrite, ptyioctl,
123 	nullstop, ptytty, ptcpoll, nommap, ptckqfilter, D_TTY
124 };
125 
126 const struct cdevsw pts_cdevsw = {
127 	ptsopen, ptsclose, ptsread, ptswrite, ptyioctl,
128 	ptsstop, ptytty, ptspoll, nommap, ttykqfilter, D_TTY
129 };
130 
131 #if defined(pmax)
132 const struct cdevsw ptc_ultrix_cdevsw = {
133 	ptcopen, ptcclose, ptcread, ptcwrite, ptyioctl,
134 	nullstop, ptytty, ptcpoll, nommap, ptckqfilter, D_TTY
135 };
136 
137 const struct cdevsw pts_ultrix_cdevsw = {
138 	ptsopen, ptsclose, ptsread, ptswrite, ptyioctl,
139 	ptsstop, ptytty, ptspoll, nommap, ttykqfilter, D_TTY
140 };
141 #endif /* defined(pmax) */
142 
143 /*
144  * Allocate and zero array of nelem elements.
145  */
146 static struct pt_softc **
147 ptyarralloc(nelem)
148 	int nelem;
149 {
150 	struct pt_softc **pt;
151 	nelem += 10;
152 	pt = malloc(nelem * sizeof(struct pt_softc *), M_DEVBUF, M_WAITOK);
153 	memset(pt, '\0', nelem * sizeof(struct pt_softc *));
154 	return pt;
155 }
156 
157 /*
158  * Check if the minor is correct and ensure necessary structures
159  * are properly allocated.
160  */
161 static int
162 check_pty(dev)
163 	dev_t dev;
164 {
165 	struct pt_softc *pti;
166 
167 	if (minor(dev) >= npty) {
168 		struct pt_softc **newpt;
169 		int newnpty;
170 
171 		/* check if the requested pty can be granted */
172 		if (minor(dev) >= maxptys) {
173 	    limit_reached:
174 			tablefull("pty", "increase kern.maxptys");
175 			return (ENXIO);
176 		}
177 
178 		/*
179 		 * Now grab the pty array mutex - we need to ensure
180 		 * that the pty array is consistent while copying it's
181 		 * content to newly allocated, larger space; we also
182 		 * need to be safe against pty_maxptys().
183 		 */
184 		simple_lock(&pt_softc_mutex);
185 
186 		do {
187 			for(newnpty = npty; newnpty <= minor(dev);
188 				newnpty *= 2);
189 
190 			if (newnpty > maxptys)
191 				newnpty = maxptys;
192 
193 			simple_unlock(&pt_softc_mutex);
194 			newpt = ptyarralloc(newnpty);
195 			simple_lock(&pt_softc_mutex);
196 
197 			if (maxptys == npty) {
198 				simple_unlock(&pt_softc_mutex);
199 				goto limit_reached;
200 			}
201 		} while(newnpty > maxptys);
202 
203 		/*
204 		 * If the pty array was not enlarged while we were waiting
205 		 * for mutex, copy current contents of pt_softc[] to newly
206 		 * allocated array and start using the new bigger array.
207 		 */
208 		if (minor(dev) >= npty) {
209 			memcpy(newpt, pt_softc, npty*sizeof(struct pt_softc *));
210 			free(pt_softc, M_DEVBUF);
211 
212 			pt_softc = newpt;
213 			npty = newnpty;
214 		} else {
215 			/* was enlarged when waited fot lock, free new space */
216 			free(newpt, M_DEVBUF);
217 		}
218 
219 		simple_unlock(&pt_softc_mutex);
220 	}
221 
222 	/*
223 	 * If the entry is not yet allocated, allocate one. The mutex is
224 	 * needed so that the state of pt_softc[] array is consistant
225 	 * in case it has been longened above.
226 	 */
227 	if (!pt_softc[minor(dev)]) {
228 		MALLOC(pti, struct pt_softc *, sizeof(struct pt_softc),
229 			M_DEVBUF, M_WAITOK);
230 		memset(pti, 0, sizeof(struct pt_softc));
231 
232 	 	pti->pt_tty = ttymalloc();
233 
234 		simple_lock(&pt_softc_mutex);
235 
236 		/*
237 		 * Check the entry again - it might have been
238 		 * added while we were waiting for mutex.
239 		 */
240 		if (!pt_softc[minor(dev)]) {
241 			tty_attach(pti->pt_tty);
242 			pt_softc[minor(dev)] = pti;
243 		} else {
244 			ttyfree(pti->pt_tty);
245 			FREE(pti, M_DEVBUF);
246 		}
247 
248 		simple_unlock(&pt_softc_mutex);
249 	}
250 
251 	return (0);
252 }
253 
254 /*
255  * Set maxpty in thread-safe way. Returns 0 in case of error, otherwise
256  * new value of maxptys.
257  */
258 int
259 pty_maxptys(newmax, set)
260 	int newmax, set;
261 {
262 	if (!set)
263 		return (maxptys);
264 
265 	/* the value cannot be set to value lower than current number of ptys */
266 	if (newmax < npty)
267 		return (0);
268 
269 	/* can proceed immediatelly if bigger than current maximum */
270 	if (newmax > maxptys) {
271 		maxptys = newmax;
272 		return (maxptys);
273 	}
274 
275 	/*
276 	 * We have to grab the pt_softc lock, so that we would pick correct
277 	 * value of npty (might be modified in check_pty()).
278 	 */
279 	simple_lock(&pt_softc_mutex);
280 
281 	if (newmax > npty)
282 		maxptys = newmax;
283 
284 	simple_unlock(&pt_softc_mutex);
285 
286 	return (maxptys);
287 }
288 
289 /*
290  * Establish n (or default if n is 1) ptys in the system.
291  */
292 void
293 ptyattach(n)
294 	int n;
295 {
296 	/* maybe should allow 0 => none? */
297 	if (n <= 1)
298 		n = DEFAULT_NPTYS;
299 	pt_softc = ptyarralloc(n);
300 	npty = n;
301 }
302 
303 /*ARGSUSED*/
304 int
305 ptsopen(dev, flag, devtype, p)
306 	dev_t dev;
307 	int flag, devtype;
308 	struct proc *p;
309 {
310 	struct pt_softc *pti;
311 	struct tty *tp;
312 	int error;
313 
314 	if ((error = check_pty(dev)))
315 		return (error);
316 
317 	pti = pt_softc[minor(dev)];
318 	tp = pti->pt_tty;
319 
320 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
321 		ttychars(tp);		/* Set up default chars */
322 		tp->t_iflag = TTYDEF_IFLAG;
323 		tp->t_oflag = TTYDEF_OFLAG;
324 		tp->t_lflag = TTYDEF_LFLAG;
325 		tp->t_cflag = TTYDEF_CFLAG;
326 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
327 		ttsetwater(tp);		/* would be done in xxparam() */
328 	} else if (ISSET(tp->t_state, TS_XCLUDE) && p->p_ucred->cr_uid != 0)
329 		return (EBUSY);
330 	if (tp->t_oproc)			/* Ctrlr still around. */
331 		SET(tp->t_state, TS_CARR_ON);
332 	if (!ISSET(flag, O_NONBLOCK))
333 		while (!ISSET(tp->t_state, TS_CARR_ON)) {
334 			tp->t_wopen++;
335 			error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
336 			    ttopen, 0);
337 			tp->t_wopen--;
338 			if (error)
339 				return (error);
340 		}
341 	error = (*tp->t_linesw->l_open)(dev, tp);
342 	ptcwakeup(tp, FREAD|FWRITE);
343 	return (error);
344 }
345 
346 int
347 ptsclose(dev, flag, mode, p)
348 	dev_t dev;
349 	int flag, mode;
350 	struct proc *p;
351 {
352 	struct pt_softc *pti = pt_softc[minor(dev)];
353 	struct tty *tp = pti->pt_tty;
354 	int error;
355 
356 	error = (*tp->t_linesw->l_close)(tp, flag);
357 	error |= ttyclose(tp);
358 	ptcwakeup(tp, FREAD|FWRITE);
359 	return (error);
360 }
361 
362 int
363 ptsread(dev, uio, flag)
364 	dev_t dev;
365 	struct uio *uio;
366 	int flag;
367 {
368 	struct proc *p = curproc;
369 	struct pt_softc *pti = pt_softc[minor(dev)];
370 	struct tty *tp = pti->pt_tty;
371 	int error = 0;
372 
373 again:
374 	if (pti->pt_flags & PF_REMOTE) {
375 		while (isbackground(p, tp)) {
376 			if (sigismasked(p, SIGTTIN) ||
377 			    p->p_pgrp->pg_jobc == 0 ||
378 			    p->p_flag & P_PPWAIT)
379 				return (EIO);
380 			pgsignal(p->p_pgrp, SIGTTIN, 1);
381 			error = ttysleep(tp, (caddr_t)&lbolt,
382 					 TTIPRI | PCATCH, ttybg, 0);
383 			if (error)
384 				return (error);
385 		}
386 		if (tp->t_canq.c_cc == 0) {
387 			if (flag & IO_NDELAY)
388 				return (EWOULDBLOCK);
389 			error = ttysleep(tp, (caddr_t)&tp->t_canq,
390 					 TTIPRI | PCATCH, ttyin, 0);
391 			if (error)
392 				return (error);
393 			goto again;
394 		}
395 		while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
396 			if (ureadc(getc(&tp->t_canq), uio) < 0) {
397 				error = EFAULT;
398 				break;
399 			}
400 		if (tp->t_canq.c_cc == 1)
401 			(void) getc(&tp->t_canq);
402 		if (tp->t_canq.c_cc)
403 			return (error);
404 	} else
405 		if (tp->t_oproc)
406 			error = (*tp->t_linesw->l_read)(tp, uio, flag);
407 	ptcwakeup(tp, FWRITE);
408 	return (error);
409 }
410 
411 /*
412  * Write to pseudo-tty.
413  * Wakeups of controlling tty will happen
414  * indirectly, when tty driver calls ptsstart.
415  */
416 int
417 ptswrite(dev, uio, flag)
418 	dev_t dev;
419 	struct uio *uio;
420 	int flag;
421 {
422 	struct pt_softc *pti = pt_softc[minor(dev)];
423 	struct tty *tp = pti->pt_tty;
424 
425 	if (tp->t_oproc == 0)
426 		return (EIO);
427 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
428 }
429 
430 /*
431  * Poll pseudo-tty.
432  */
433 int
434 ptspoll(dev, events, p)
435 	dev_t dev;
436 	int events;
437 	struct proc *p;
438 {
439 	struct pt_softc *pti = pt_softc[minor(dev)];
440 	struct tty *tp = pti->pt_tty;
441 
442 	if (tp->t_oproc == 0)
443 		return (EIO);
444 
445 	return ((*tp->t_linesw->l_poll)(tp, events, p));
446 }
447 
448 /*
449  * Start output on pseudo-tty.
450  * Wake up process polling or sleeping for input from controlling tty.
451  */
452 void
453 ptsstart(tp)
454 	struct tty *tp;
455 {
456 	struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
457 
458 	if (ISSET(tp->t_state, TS_TTSTOP))
459 		return;
460 	if (pti->pt_flags & PF_STOPPED) {
461 		pti->pt_flags &= ~PF_STOPPED;
462 		pti->pt_send = TIOCPKT_START;
463 	}
464 	ptcwakeup(tp, FREAD);
465 }
466 
467 void
468 ptsstop(tp, flush)
469 	struct tty *tp;
470 	int flush;
471 {
472 	struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
473 	int flag;
474 
475 	/* note: FLUSHREAD and FLUSHWRITE already ok */
476 	if (flush == 0) {
477 		flush = TIOCPKT_STOP;
478 		pti->pt_flags |= PF_STOPPED;
479 	} else
480 		pti->pt_flags &= ~PF_STOPPED;
481 	pti->pt_send |= flush;
482 	/* change of perspective */
483 	flag = 0;
484 	if (flush & FREAD)
485 		flag |= FWRITE;
486 	if (flush & FWRITE)
487 		flag |= FREAD;
488 	ptcwakeup(tp, flag);
489 }
490 
491 void
492 ptcwakeup(tp, flag)
493 	struct tty *tp;
494 	int flag;
495 {
496 	struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
497 
498 	if (flag & FREAD) {
499 		selnotify(&pti->pt_selr, 0);
500 		wakeup((caddr_t)&tp->t_outq.c_cf);
501 	}
502 	if (flag & FWRITE) {
503 		selnotify(&pti->pt_selw, 0);
504 		wakeup((caddr_t)&tp->t_rawq.c_cf);
505 	}
506 }
507 
508 /*ARGSUSED*/
509 int
510 ptcopen(dev, flag, devtype, p)
511 	dev_t dev;
512 	int flag, devtype;
513 	struct proc *p;
514 {
515 	struct pt_softc *pti;
516 	struct tty *tp;
517 	int error;
518 
519 	if ((error = check_pty(dev)))
520 		return (error);
521 
522 	pti = pt_softc[minor(dev)];
523 	tp = pti->pt_tty;
524 
525 	if (tp->t_oproc)
526 		return (EIO);
527 	tp->t_oproc = ptsstart;
528 	(void)(*tp->t_linesw->l_modem)(tp, 1);
529 	CLR(tp->t_lflag, EXTPROC);
530 	pti->pt_flags = 0;
531 	pti->pt_send = 0;
532 	pti->pt_ucntl = 0;
533 	return (0);
534 }
535 
536 /*ARGSUSED*/
537 int
538 ptcclose(dev, flag, devtype, p)
539 	dev_t dev;
540 	int flag, devtype;
541 	struct proc *p;
542 {
543 	struct pt_softc *pti = pt_softc[minor(dev)];
544 	struct tty *tp = pti->pt_tty;
545 
546 	(void)(*tp->t_linesw->l_modem)(tp, 0);
547 	CLR(tp->t_state, TS_CARR_ON);
548 	tp->t_oproc = 0;		/* mark closed */
549 	return (0);
550 }
551 
552 int
553 ptcread(dev, uio, flag)
554 	dev_t dev;
555 	struct uio *uio;
556 	int flag;
557 {
558 	struct pt_softc *pti = pt_softc[minor(dev)];
559 	struct tty *tp = pti->pt_tty;
560 	char buf[BUFSIZ];
561 	int error = 0, cc;
562 
563 	/*
564 	 * We want to block until the slave
565 	 * is open, and there's something to read;
566 	 * but if we lost the slave or we're NBIO,
567 	 * then return the appropriate error instead.
568 	 */
569 	for (;;) {
570 		if (ISSET(tp->t_state, TS_ISOPEN)) {
571 			if (pti->pt_flags&PF_PKT && pti->pt_send) {
572 				error = ureadc((int)pti->pt_send, uio);
573 				if (error)
574 					return (error);
575 				if (pti->pt_send & TIOCPKT_IOCTL) {
576 					cc = min(uio->uio_resid,
577 						sizeof(tp->t_termios));
578 					uiomove((caddr_t) &tp->t_termios,
579 						cc, uio);
580 				}
581 				pti->pt_send = 0;
582 				return (0);
583 			}
584 			if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
585 				error = ureadc((int)pti->pt_ucntl, uio);
586 				if (error)
587 					return (error);
588 				pti->pt_ucntl = 0;
589 				return (0);
590 			}
591 			if (tp->t_outq.c_cc && !ISSET(tp->t_state, TS_TTSTOP))
592 				break;
593 		}
594 		if (!ISSET(tp->t_state, TS_CARR_ON))
595 			return (0);	/* EOF */
596 		if (flag & IO_NDELAY)
597 			return (EWOULDBLOCK);
598 		error = tsleep((caddr_t)&tp->t_outq.c_cf, TTIPRI | PCATCH,
599 			       ttyin, 0);
600 		if (error)
601 			return (error);
602 	}
603 	if (pti->pt_flags & (PF_PKT|PF_UCNTL))
604 		error = ureadc(0, uio);
605 	while (uio->uio_resid > 0 && error == 0) {
606 		cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
607 		if (cc <= 0)
608 			break;
609 		error = uiomove(buf, cc, uio);
610 	}
611 	if (tp->t_outq.c_cc <= tp->t_lowat) {
612 		if (ISSET(tp->t_state, TS_ASLEEP)) {
613 			CLR(tp->t_state, TS_ASLEEP);
614 			wakeup((caddr_t)&tp->t_outq);
615 		}
616 		selnotify(&tp->t_wsel, 0);
617 	}
618 	return (error);
619 }
620 
621 
622 int
623 ptcwrite(dev, uio, flag)
624 	dev_t dev;
625 	struct uio *uio;
626 	int flag;
627 {
628 	struct pt_softc *pti = pt_softc[minor(dev)];
629 	struct tty *tp = pti->pt_tty;
630 	u_char *cp = NULL;
631 	int cc = 0;
632 	u_char locbuf[BUFSIZ];
633 	int cnt = 0;
634 	int error = 0;
635 
636 again:
637 	if (!ISSET(tp->t_state, TS_ISOPEN))
638 		goto block;
639 	if (pti->pt_flags & PF_REMOTE) {
640 		if (tp->t_canq.c_cc)
641 			goto block;
642 		while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) {
643 			if (cc == 0) {
644 				cc = min(uio->uio_resid, BUFSIZ);
645 				cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
646 				cp = locbuf;
647 				error = uiomove((caddr_t)cp, cc, uio);
648 				if (error)
649 					return (error);
650 				/* check again for safety */
651 				if (!ISSET(tp->t_state, TS_ISOPEN))
652 					return (EIO);
653 			}
654 			if (cc)
655 				(void) b_to_q((char *)cp, cc, &tp->t_canq);
656 			cc = 0;
657 		}
658 		(void) putc(0, &tp->t_canq);
659 		ttwakeup(tp);
660 		wakeup((caddr_t)&tp->t_canq);
661 		return (0);
662 	}
663 	while (uio->uio_resid > 0) {
664 		if (cc == 0) {
665 			cc = min(uio->uio_resid, BUFSIZ);
666 			cp = locbuf;
667 			error = uiomove((caddr_t)cp, cc, uio);
668 			if (error)
669 				return (error);
670 			/* check again for safety */
671 			if (!ISSET(tp->t_state, TS_ISOPEN))
672 				return (EIO);
673 		}
674 		while (cc > 0) {
675 			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
676 			   (tp->t_canq.c_cc > 0 || !ISSET(tp->t_lflag, ICANON))) {
677 				wakeup((caddr_t)&tp->t_rawq);
678 				goto block;
679 			}
680 			(*tp->t_linesw->l_rint)(*cp++, tp);
681 			cnt++;
682 			cc--;
683 		}
684 		cc = 0;
685 	}
686 	return (0);
687 block:
688 	/*
689 	 * Come here to wait for slave to open, for space
690 	 * in outq, or space in rawq.
691 	 */
692 	if (!ISSET(tp->t_state, TS_CARR_ON))
693 		return (EIO);
694 	if (flag & IO_NDELAY) {
695 		/* adjust for data copied in but not written */
696 		uio->uio_resid += cc;
697 		if (cnt == 0)
698 			return (EWOULDBLOCK);
699 		return (0);
700 	}
701 	error = tsleep((caddr_t)&tp->t_rawq.c_cf, TTOPRI | PCATCH,
702 		       ttyout, 0);
703 	if (error) {
704 		/* adjust for data copied in but not written */
705 		uio->uio_resid += cc;
706 		return (error);
707 	}
708 	goto again;
709 }
710 
711 int
712 ptcpoll(dev, events, p)
713 	dev_t dev;
714 	int events;
715 	struct proc *p;
716 {
717 	struct pt_softc *pti = pt_softc[minor(dev)];
718 	struct tty *tp = pti->pt_tty;
719 	int revents = 0;
720 	int s = splsoftclock();
721 
722 	if (events & (POLLIN | POLLRDNORM))
723 		if (ISSET(tp->t_state, TS_ISOPEN) &&
724 		    ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
725 		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
726 		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
727 			revents |= events & (POLLIN | POLLRDNORM);
728 
729 	if (events & (POLLOUT | POLLWRNORM))
730 		if (ISSET(tp->t_state, TS_ISOPEN) &&
731 		    ((pti->pt_flags & PF_REMOTE) ?
732 		     (tp->t_canq.c_cc == 0) :
733 		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
734 		      (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))))
735 			revents |= events & (POLLOUT | POLLWRNORM);
736 
737 	if (events & POLLHUP)
738 		if (!ISSET(tp->t_state, TS_CARR_ON))
739 			revents |= POLLHUP;
740 
741 	if (revents == 0) {
742 		if (events & (POLLIN | POLLHUP | POLLRDNORM))
743 			selrecord(p, &pti->pt_selr);
744 
745 		if (events & (POLLOUT | POLLWRNORM))
746 			selrecord(p, &pti->pt_selw);
747 	}
748 
749 	splx(s);
750 	return (revents);
751 }
752 
753 static void
754 filt_ptcrdetach(struct knote *kn)
755 {
756 	struct pt_softc *pti;
757 	int		s;
758 
759 	pti = kn->kn_hook;
760 	s = spltty();
761 	SLIST_REMOVE(&pti->pt_selr.sel_klist, kn, knote, kn_selnext);
762 	splx(s);
763 }
764 
765 static int
766 filt_ptcread(struct knote *kn, long hint)
767 {
768 	struct pt_softc *pti;
769 	struct tty	*tp;
770 	int canread;
771 
772 	pti = kn->kn_hook;
773 	tp = pti->pt_tty;
774 
775 	canread = (ISSET(tp->t_state, TS_ISOPEN) &&
776 		    ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
777 		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
778 		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)));
779 
780 	if (canread) {
781 		/*
782 		 * c_cc is number of characters after output post-processing;
783 		 * the amount of data actually read(2) depends on
784 		 * setting of input flags for the terminal.
785 		 */
786 		kn->kn_data = tp->t_outq.c_cc;
787 		if (((pti->pt_flags & PF_PKT) && pti->pt_send) ||
788 		    ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))
789 			kn->kn_data++;
790 	}
791 
792 	return (canread);
793 }
794 
795 static void
796 filt_ptcwdetach(struct knote *kn)
797 {
798 	struct pt_softc *pti;
799 	int		s;
800 
801 	pti = kn->kn_hook;
802 	s = spltty();
803 	SLIST_REMOVE(&pti->pt_selw.sel_klist, kn, knote, kn_selnext);
804 	splx(s);
805 }
806 
807 static int
808 filt_ptcwrite(struct knote *kn, long hint)
809 {
810 	struct pt_softc *pti;
811 	struct tty	*tp;
812 	int canwrite;
813 	int nwrite;
814 
815 	pti = kn->kn_hook;
816 	tp = pti->pt_tty;
817 
818 	canwrite = (ISSET(tp->t_state, TS_ISOPEN) &&
819 		    ((pti->pt_flags & PF_REMOTE) ?
820 		     (tp->t_canq.c_cc == 0) :
821 		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
822 		      (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))));
823 
824 	if (canwrite) {
825 		if (pti->pt_flags & PF_REMOTE)
826 			nwrite = tp->t_canq.c_cn;
827 		else {
828 			/* this is guaranteed to be > 0 due to above check */
829 			nwrite = tp->t_canq.c_cn
830 				- (tp->t_rawq.c_cc + tp->t_canq.c_cc);
831 		}
832 		kn->kn_data = nwrite;
833 	}
834 
835 	return (canwrite);
836 }
837 
838 static const struct filterops ptcread_filtops =
839 	{ 1, NULL, filt_ptcrdetach, filt_ptcread };
840 static const struct filterops ptcwrite_filtops =
841 	{ 1, NULL, filt_ptcwdetach, filt_ptcwrite };
842 
843 int
844 ptckqfilter(dev_t dev, struct knote *kn)
845 {
846 	struct pt_softc *pti = pt_softc[minor(dev)];
847 	struct klist	*klist;
848 	int		s;
849 
850 	switch (kn->kn_filter) {
851 	case EVFILT_READ:
852 		klist = &pti->pt_selr.sel_klist;
853 		kn->kn_fop = &ptcread_filtops;
854 		break;
855 	case EVFILT_WRITE:
856 		klist = &pti->pt_selw.sel_klist;
857 		kn->kn_fop = &ptcwrite_filtops;
858 		break;
859 	default:
860 		return (1);
861 	}
862 
863 	kn->kn_hook = pti;
864 
865 	s = spltty();
866 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
867 	splx(s);
868 
869 	return (0);
870 }
871 
872 struct tty *
873 ptytty(dev)
874 	dev_t dev;
875 {
876 	struct pt_softc *pti = pt_softc[minor(dev)];
877 	struct tty *tp = pti->pt_tty;
878 
879 	return (tp);
880 }
881 
882 /*ARGSUSED*/
883 int
884 ptyioctl(dev, cmd, data, flag, p)
885 	dev_t dev;
886 	u_long cmd;
887 	caddr_t data;
888 	int flag;
889 	struct proc *p;
890 {
891 	struct pt_softc *pti = pt_softc[minor(dev)];
892 	struct tty *tp = pti->pt_tty;
893 	const struct cdevsw *cdev;
894 	u_char *cc = tp->t_cc;
895 	int stop, error, sig;
896 
897 	cdev = cdevsw_lookup(dev);
898 	/*
899 	 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
900 	 * ttywflush(tp) will hang if there are characters in the outq.
901 	 */
902 	if (cmd == TIOCEXT) {
903 		/*
904 		 * When the EXTPROC bit is being toggled, we need
905 		 * to send an TIOCPKT_IOCTL if the packet driver
906 		 * is turned on.
907 		 */
908 		if (*(int *)data) {
909 			if (pti->pt_flags & PF_PKT) {
910 				pti->pt_send |= TIOCPKT_IOCTL;
911 				ptcwakeup(tp, FREAD);
912 			}
913 			SET(tp->t_lflag, EXTPROC);
914 		} else {
915 			if (ISSET(tp->t_lflag, EXTPROC) &&
916 			    (pti->pt_flags & PF_PKT)) {
917 				pti->pt_send |= TIOCPKT_IOCTL;
918 				ptcwakeup(tp, FREAD);
919 			}
920 			CLR(tp->t_lflag, EXTPROC);
921 		}
922 		return(0);
923 	} else
924 	if (cdev != NULL && cdev->d_open == ptcopen)
925 		switch (cmd) {
926 
927 		case TIOCGPGRP:
928 			/*
929 			 * We avoid calling ttioctl on the controller since,
930 			 * in that case, tp must be the controlling terminal.
931 			 */
932 			*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
933 			return (0);
934 
935 		case TIOCPKT:
936 			if (*(int *)data) {
937 				if (pti->pt_flags & PF_UCNTL)
938 					return (EINVAL);
939 				pti->pt_flags |= PF_PKT;
940 			} else
941 				pti->pt_flags &= ~PF_PKT;
942 			return (0);
943 
944 		case TIOCUCNTL:
945 			if (*(int *)data) {
946 				if (pti->pt_flags & PF_PKT)
947 					return (EINVAL);
948 				pti->pt_flags |= PF_UCNTL;
949 			} else
950 				pti->pt_flags &= ~PF_UCNTL;
951 			return (0);
952 
953 		case TIOCREMOTE:
954 			if (*(int *)data)
955 				pti->pt_flags |= PF_REMOTE;
956 			else
957 				pti->pt_flags &= ~PF_REMOTE;
958 			ttyflush(tp, FREAD|FWRITE);
959 			return (0);
960 
961 #ifdef COMPAT_OLDTTY
962 		case TIOCSETP:
963 		case TIOCSETN:
964 #endif
965 		case TIOCSETD:
966 		case TIOCSETA:
967 		case TIOCSETAW:
968 		case TIOCSETAF:
969 			ndflush(&tp->t_outq, tp->t_outq.c_cc);
970 			break;
971 
972 		case TIOCSIG:
973 			sig = (int)(long)*(caddr_t *)data;
974 			if (sig <= 0 || sig >= NSIG)
975 				return (EINVAL);
976 			if (!ISSET(tp->t_lflag, NOFLSH))
977 				ttyflush(tp, FREAD|FWRITE);
978 			if ((sig == SIGINFO) &&
979 			    (!ISSET(tp->t_lflag, NOKERNINFO)))
980 				ttyinfo(tp);
981 			pgsignal(tp->t_pgrp, sig, 1);
982 			return(0);
983 		}
984 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
985 	if (error == EPASSTHROUGH)
986 		 error = ttioctl(tp, cmd, data, flag, p);
987 	if (error == EPASSTHROUGH) {
988 		if (pti->pt_flags & PF_UCNTL &&
989 		    (cmd & ~0xff) == UIOCCMD(0)) {
990 			if (cmd & 0xff) {
991 				pti->pt_ucntl = (u_char)cmd;
992 				ptcwakeup(tp, FREAD);
993 			}
994 			return (0);
995 		}
996 	}
997 	/*
998 	 * If external processing and packet mode send ioctl packet.
999 	 */
1000 	if (ISSET(tp->t_lflag, EXTPROC) && (pti->pt_flags & PF_PKT)) {
1001 		switch(cmd) {
1002 		case TIOCSETA:
1003 		case TIOCSETAW:
1004 		case TIOCSETAF:
1005 #ifdef COMPAT_OLDTTY
1006 		case TIOCSETP:
1007 		case TIOCSETN:
1008 		case TIOCSETC:
1009 		case TIOCSLTC:
1010 		case TIOCLBIS:
1011 		case TIOCLBIC:
1012 		case TIOCLSET:
1013 #endif
1014 			pti->pt_send |= TIOCPKT_IOCTL;
1015 			ptcwakeup(tp, FREAD);
1016 		default:
1017 			break;
1018 		}
1019 	}
1020 	stop = ISSET(tp->t_iflag, IXON) && CCEQ(cc[VSTOP], CTRL('s'))
1021 		&& CCEQ(cc[VSTART], CTRL('q'));
1022 	if (pti->pt_flags & PF_NOSTOP) {
1023 		if (stop) {
1024 			pti->pt_send &= ~TIOCPKT_NOSTOP;
1025 			pti->pt_send |= TIOCPKT_DOSTOP;
1026 			pti->pt_flags &= ~PF_NOSTOP;
1027 			ptcwakeup(tp, FREAD);
1028 		}
1029 	} else {
1030 		if (!stop) {
1031 			pti->pt_send &= ~TIOCPKT_DOSTOP;
1032 			pti->pt_send |= TIOCPKT_NOSTOP;
1033 			pti->pt_flags |= PF_NOSTOP;
1034 			ptcwakeup(tp, FREAD);
1035 		}
1036 	}
1037 	return (error);
1038 }
1039