xref: /netbsd-src/sys/kern/tty_pty.c (revision 08c81a9c2dc8c7300e893321eb65c0925d60871c)
1 /*	$NetBSD: tty_pty.c,v 1.63 2002/09/06 13:18:43 gehenna 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.63 2002/09/06 13:18:43 gehenna 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 
230 	 	pti->pt_tty = ttymalloc();
231 
232 		simple_lock(&pt_softc_mutex);
233 
234 		/*
235 		 * Check the entry again - it might have been
236 		 * added while we were waiting for mutex.
237 		 */
238 		if (!pt_softc[minor(dev)]) {
239 			tty_attach(pti->pt_tty);
240 			pt_softc[minor(dev)] = pti;
241 		} else {
242 			ttyfree(pti->pt_tty);
243 			FREE(pti, M_DEVBUF);
244 		}
245 
246 		simple_unlock(&pt_softc_mutex);
247 	}
248 
249 	return (0);
250 }
251 
252 /*
253  * Set maxpty in thread-safe way. Returns 0 in case of error, otherwise
254  * new value of maxptys.
255  */
256 int
257 pty_maxptys(newmax, set)
258 	int newmax, set;
259 {
260 	if (!set)
261 		return (maxptys);
262 
263 	/* the value cannot be set to value lower than current number of ptys */
264 	if (newmax < npty)
265 		return (0);
266 
267 	/* can proceed immediatelly if bigger than current maximum */
268 	if (newmax > maxptys) {
269 		maxptys = newmax;
270 		return (maxptys);
271 	}
272 
273 	/*
274 	 * We have to grab the pt_softc lock, so that we would pick correct
275 	 * value of npty (might be modified in check_pty()).
276 	 */
277 	simple_lock(&pt_softc_mutex);
278 
279 	if (newmax > npty)
280 		maxptys = newmax;
281 
282 	simple_unlock(&pt_softc_mutex);
283 
284 	return (maxptys);
285 }
286 
287 /*
288  * Establish n (or default if n is 1) ptys in the system.
289  */
290 void
291 ptyattach(n)
292 	int n;
293 {
294 	/* maybe should allow 0 => none? */
295 	if (n <= 1)
296 		n = DEFAULT_NPTYS;
297 	pt_softc = ptyarralloc(n);
298 	npty = n;
299 }
300 
301 /*ARGSUSED*/
302 int
303 ptsopen(dev, flag, devtype, p)
304 	dev_t dev;
305 	int flag, devtype;
306 	struct proc *p;
307 {
308 	struct pt_softc *pti;
309 	struct tty *tp;
310 	int error;
311 
312 	if ((error = check_pty(dev)))
313 		return (error);
314 
315 	pti = pt_softc[minor(dev)];
316 	tp = pti->pt_tty;
317 
318 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
319 		ttychars(tp);		/* Set up default chars */
320 		tp->t_iflag = TTYDEF_IFLAG;
321 		tp->t_oflag = TTYDEF_OFLAG;
322 		tp->t_lflag = TTYDEF_LFLAG;
323 		tp->t_cflag = TTYDEF_CFLAG;
324 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
325 		ttsetwater(tp);		/* would be done in xxparam() */
326 	} else if (ISSET(tp->t_state, TS_XCLUDE) && p->p_ucred->cr_uid != 0)
327 		return (EBUSY);
328 	if (tp->t_oproc)			/* Ctrlr still around. */
329 		SET(tp->t_state, TS_CARR_ON);
330 	if (!ISSET(flag, O_NONBLOCK))
331 		while (!ISSET(tp->t_state, TS_CARR_ON)) {
332 			tp->t_wopen++;
333 			error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
334 			    ttopen, 0);
335 			tp->t_wopen--;
336 			if (error)
337 				return (error);
338 		}
339 	error = (*tp->t_linesw->l_open)(dev, tp);
340 	ptcwakeup(tp, FREAD|FWRITE);
341 	return (error);
342 }
343 
344 int
345 ptsclose(dev, flag, mode, p)
346 	dev_t dev;
347 	int flag, mode;
348 	struct proc *p;
349 {
350 	struct pt_softc *pti = pt_softc[minor(dev)];
351 	struct tty *tp = pti->pt_tty;
352 	int error;
353 
354 	error = (*tp->t_linesw->l_close)(tp, flag);
355 	error |= ttyclose(tp);
356 	ptcwakeup(tp, FREAD|FWRITE);
357 	return (error);
358 }
359 
360 int
361 ptsread(dev, uio, flag)
362 	dev_t dev;
363 	struct uio *uio;
364 	int flag;
365 {
366 	struct proc *p = curproc;
367 	struct pt_softc *pti = pt_softc[minor(dev)];
368 	struct tty *tp = pti->pt_tty;
369 	int error = 0;
370 
371 again:
372 	if (pti->pt_flags & PF_REMOTE) {
373 		while (isbackground(p, tp)) {
374 			if (sigismasked(p, SIGTTIN) ||
375 			    p->p_pgrp->pg_jobc == 0 ||
376 			    p->p_flag & P_PPWAIT)
377 				return (EIO);
378 			pgsignal(p->p_pgrp, SIGTTIN, 1);
379 			error = ttysleep(tp, (caddr_t)&lbolt,
380 					 TTIPRI | PCATCH, ttybg, 0);
381 			if (error)
382 				return (error);
383 		}
384 		if (tp->t_canq.c_cc == 0) {
385 			if (flag & IO_NDELAY)
386 				return (EWOULDBLOCK);
387 			error = ttysleep(tp, (caddr_t)&tp->t_canq,
388 					 TTIPRI | PCATCH, ttyin, 0);
389 			if (error)
390 				return (error);
391 			goto again;
392 		}
393 		while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
394 			if (ureadc(getc(&tp->t_canq), uio) < 0) {
395 				error = EFAULT;
396 				break;
397 			}
398 		if (tp->t_canq.c_cc == 1)
399 			(void) getc(&tp->t_canq);
400 		if (tp->t_canq.c_cc)
401 			return (error);
402 	} else
403 		if (tp->t_oproc)
404 			error = (*tp->t_linesw->l_read)(tp, uio, flag);
405 	ptcwakeup(tp, FWRITE);
406 	return (error);
407 }
408 
409 /*
410  * Write to pseudo-tty.
411  * Wakeups of controlling tty will happen
412  * indirectly, when tty driver calls ptsstart.
413  */
414 int
415 ptswrite(dev, uio, flag)
416 	dev_t dev;
417 	struct uio *uio;
418 	int flag;
419 {
420 	struct pt_softc *pti = pt_softc[minor(dev)];
421 	struct tty *tp = pti->pt_tty;
422 
423 	if (tp->t_oproc == 0)
424 		return (EIO);
425 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
426 }
427 
428 /*
429  * Poll pseudo-tty.
430  */
431 int
432 ptspoll(dev, events, p)
433 	dev_t dev;
434 	int events;
435 	struct proc *p;
436 {
437 	struct pt_softc *pti = pt_softc[minor(dev)];
438 	struct tty *tp = pti->pt_tty;
439 
440 	if (tp->t_oproc == 0)
441 		return (EIO);
442 
443 	return ((*tp->t_linesw->l_poll)(tp, events, p));
444 }
445 
446 /*
447  * Start output on pseudo-tty.
448  * Wake up process polling or sleeping for input from controlling tty.
449  */
450 void
451 ptsstart(tp)
452 	struct tty *tp;
453 {
454 	struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
455 
456 	if (ISSET(tp->t_state, TS_TTSTOP))
457 		return;
458 	if (pti->pt_flags & PF_STOPPED) {
459 		pti->pt_flags &= ~PF_STOPPED;
460 		pti->pt_send = TIOCPKT_START;
461 	}
462 	ptcwakeup(tp, FREAD);
463 }
464 
465 void
466 ptsstop(tp, flush)
467 	struct tty *tp;
468 	int flush;
469 {
470 	struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
471 	int flag;
472 
473 	/* note: FLUSHREAD and FLUSHWRITE already ok */
474 	if (flush == 0) {
475 		flush = TIOCPKT_STOP;
476 		pti->pt_flags |= PF_STOPPED;
477 	} else
478 		pti->pt_flags &= ~PF_STOPPED;
479 	pti->pt_send |= flush;
480 	/* change of perspective */
481 	flag = 0;
482 	if (flush & FREAD)
483 		flag |= FWRITE;
484 	if (flush & FWRITE)
485 		flag |= FREAD;
486 	ptcwakeup(tp, flag);
487 }
488 
489 void
490 ptcwakeup(tp, flag)
491 	struct tty *tp;
492 	int flag;
493 {
494 	struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
495 
496 	if (flag & FREAD) {
497 		selwakeup(&pti->pt_selr);
498 		wakeup((caddr_t)&tp->t_outq.c_cf);
499 	}
500 	if (flag & FWRITE) {
501 		selwakeup(&pti->pt_selw);
502 		wakeup((caddr_t)&tp->t_rawq.c_cf);
503 	}
504 }
505 
506 /*ARGSUSED*/
507 int
508 ptcopen(dev, flag, devtype, p)
509 	dev_t dev;
510 	int flag, devtype;
511 	struct proc *p;
512 {
513 	struct pt_softc *pti;
514 	struct tty *tp;
515 	int error;
516 
517 	if ((error = check_pty(dev)))
518 		return (error);
519 
520 	pti = pt_softc[minor(dev)];
521 	tp = pti->pt_tty;
522 
523 	if (tp->t_oproc)
524 		return (EIO);
525 	tp->t_oproc = ptsstart;
526 	(void)(*tp->t_linesw->l_modem)(tp, 1);
527 	CLR(tp->t_lflag, EXTPROC);
528 	pti->pt_flags = 0;
529 	pti->pt_send = 0;
530 	pti->pt_ucntl = 0;
531 	return (0);
532 }
533 
534 /*ARGSUSED*/
535 int
536 ptcclose(dev, flag, devtype, p)
537 	dev_t dev;
538 	int flag, devtype;
539 	struct proc *p;
540 {
541 	struct pt_softc *pti = pt_softc[minor(dev)];
542 	struct tty *tp = pti->pt_tty;
543 
544 	(void)(*tp->t_linesw->l_modem)(tp, 0);
545 	CLR(tp->t_state, TS_CARR_ON);
546 	tp->t_oproc = 0;		/* mark closed */
547 	return (0);
548 }
549 
550 int
551 ptcread(dev, uio, flag)
552 	dev_t dev;
553 	struct uio *uio;
554 	int flag;
555 {
556 	struct pt_softc *pti = pt_softc[minor(dev)];
557 	struct tty *tp = pti->pt_tty;
558 	char buf[BUFSIZ];
559 	int error = 0, cc;
560 
561 	/*
562 	 * We want to block until the slave
563 	 * is open, and there's something to read;
564 	 * but if we lost the slave or we're NBIO,
565 	 * then return the appropriate error instead.
566 	 */
567 	for (;;) {
568 		if (ISSET(tp->t_state, TS_ISOPEN)) {
569 			if (pti->pt_flags&PF_PKT && pti->pt_send) {
570 				error = ureadc((int)pti->pt_send, uio);
571 				if (error)
572 					return (error);
573 				if (pti->pt_send & TIOCPKT_IOCTL) {
574 					cc = min(uio->uio_resid,
575 						sizeof(tp->t_termios));
576 					uiomove((caddr_t) &tp->t_termios,
577 						cc, uio);
578 				}
579 				pti->pt_send = 0;
580 				return (0);
581 			}
582 			if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
583 				error = ureadc((int)pti->pt_ucntl, uio);
584 				if (error)
585 					return (error);
586 				pti->pt_ucntl = 0;
587 				return (0);
588 			}
589 			if (tp->t_outq.c_cc && !ISSET(tp->t_state, TS_TTSTOP))
590 				break;
591 		}
592 		if (!ISSET(tp->t_state, TS_CARR_ON))
593 			return (0);	/* EOF */
594 		if (flag & IO_NDELAY)
595 			return (EWOULDBLOCK);
596 		error = tsleep((caddr_t)&tp->t_outq.c_cf, TTIPRI | PCATCH,
597 			       ttyin, 0);
598 		if (error)
599 			return (error);
600 	}
601 	if (pti->pt_flags & (PF_PKT|PF_UCNTL))
602 		error = ureadc(0, uio);
603 	while (uio->uio_resid > 0 && error == 0) {
604 		cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
605 		if (cc <= 0)
606 			break;
607 		error = uiomove(buf, cc, uio);
608 	}
609 	if (tp->t_outq.c_cc <= tp->t_lowat) {
610 		if (ISSET(tp->t_state, TS_ASLEEP)) {
611 			CLR(tp->t_state, TS_ASLEEP);
612 			wakeup((caddr_t)&tp->t_outq);
613 		}
614 		selwakeup(&tp->t_wsel);
615 	}
616 	return (error);
617 }
618 
619 
620 int
621 ptcwrite(dev, uio, flag)
622 	dev_t dev;
623 	struct uio *uio;
624 	int flag;
625 {
626 	struct pt_softc *pti = pt_softc[minor(dev)];
627 	struct tty *tp = pti->pt_tty;
628 	u_char *cp = NULL;
629 	int cc = 0;
630 	u_char locbuf[BUFSIZ];
631 	int cnt = 0;
632 	int error = 0;
633 
634 again:
635 	if (!ISSET(tp->t_state, TS_ISOPEN))
636 		goto block;
637 	if (pti->pt_flags & PF_REMOTE) {
638 		if (tp->t_canq.c_cc)
639 			goto block;
640 		while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) {
641 			if (cc == 0) {
642 				cc = min(uio->uio_resid, BUFSIZ);
643 				cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
644 				cp = locbuf;
645 				error = uiomove((caddr_t)cp, cc, uio);
646 				if (error)
647 					return (error);
648 				/* check again for safety */
649 				if (!ISSET(tp->t_state, TS_ISOPEN))
650 					return (EIO);
651 			}
652 			if (cc)
653 				(void) b_to_q((char *)cp, cc, &tp->t_canq);
654 			cc = 0;
655 		}
656 		(void) putc(0, &tp->t_canq);
657 		ttwakeup(tp);
658 		wakeup((caddr_t)&tp->t_canq);
659 		return (0);
660 	}
661 	while (uio->uio_resid > 0) {
662 		if (cc == 0) {
663 			cc = min(uio->uio_resid, BUFSIZ);
664 			cp = locbuf;
665 			error = uiomove((caddr_t)cp, cc, uio);
666 			if (error)
667 				return (error);
668 			/* check again for safety */
669 			if (!ISSET(tp->t_state, TS_ISOPEN))
670 				return (EIO);
671 		}
672 		while (cc > 0) {
673 			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
674 			   (tp->t_canq.c_cc > 0 || !ISSET(tp->t_lflag, ICANON))) {
675 				wakeup((caddr_t)&tp->t_rawq);
676 				goto block;
677 			}
678 			(*tp->t_linesw->l_rint)(*cp++, tp);
679 			cnt++;
680 			cc--;
681 		}
682 		cc = 0;
683 	}
684 	return (0);
685 block:
686 	/*
687 	 * Come here to wait for slave to open, for space
688 	 * in outq, or space in rawq.
689 	 */
690 	if (!ISSET(tp->t_state, TS_CARR_ON))
691 		return (EIO);
692 	if (flag & IO_NDELAY) {
693 		/* adjust for data copied in but not written */
694 		uio->uio_resid += cc;
695 		if (cnt == 0)
696 			return (EWOULDBLOCK);
697 		return (0);
698 	}
699 	error = tsleep((caddr_t)&tp->t_rawq.c_cf, TTOPRI | PCATCH,
700 		       ttyout, 0);
701 	if (error) {
702 		/* adjust for data copied in but not written */
703 		uio->uio_resid += cc;
704 		return (error);
705 	}
706 	goto again;
707 }
708 
709 int
710 ptcpoll(dev, events, p)
711 	dev_t dev;
712 	int events;
713 	struct proc *p;
714 {
715 	struct pt_softc *pti = pt_softc[minor(dev)];
716 	struct tty *tp = pti->pt_tty;
717 	int revents = 0;
718 	int s = splsoftclock();
719 
720 	if (events & (POLLIN | POLLRDNORM))
721 		if (ISSET(tp->t_state, TS_ISOPEN) &&
722 		    ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
723 		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
724 		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
725 			revents |= events & (POLLIN | POLLRDNORM);
726 
727 	if (events & (POLLOUT | POLLWRNORM))
728 		if (ISSET(tp->t_state, TS_ISOPEN) &&
729 		    ((pti->pt_flags & PF_REMOTE) ?
730 		     (tp->t_canq.c_cc == 0) :
731 		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
732 		      (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))))
733 			revents |= events & (POLLOUT | POLLWRNORM);
734 
735 	if (events & POLLHUP)
736 		if (!ISSET(tp->t_state, TS_CARR_ON))
737 			revents |= POLLHUP;
738 
739 	if (revents == 0) {
740 		if (events & (POLLIN | POLLHUP | POLLRDNORM))
741 			selrecord(p, &pti->pt_selr);
742 
743 		if (events & (POLLOUT | POLLWRNORM))
744 			selrecord(p, &pti->pt_selw);
745 	}
746 
747 	splx(s);
748 	return (revents);
749 }
750 
751 
752 struct tty *
753 ptytty(dev)
754 	dev_t dev;
755 {
756 	struct pt_softc *pti = pt_softc[minor(dev)];
757 	struct tty *tp = pti->pt_tty;
758 
759 	return (tp);
760 }
761 
762 /*ARGSUSED*/
763 int
764 ptyioctl(dev, cmd, data, flag, p)
765 	dev_t dev;
766 	u_long cmd;
767 	caddr_t data;
768 	int flag;
769 	struct proc *p;
770 {
771 	struct pt_softc *pti = pt_softc[minor(dev)];
772 	struct tty *tp = pti->pt_tty;
773 	const struct cdevsw *cdev;
774 	u_char *cc = tp->t_cc;
775 	int stop, error, sig;
776 
777 	cdev = cdevsw_lookup(dev);
778 	/*
779 	 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
780 	 * ttywflush(tp) will hang if there are characters in the outq.
781 	 */
782 	if (cmd == TIOCEXT) {
783 		/*
784 		 * When the EXTPROC bit is being toggled, we need
785 		 * to send an TIOCPKT_IOCTL if the packet driver
786 		 * is turned on.
787 		 */
788 		if (*(int *)data) {
789 			if (pti->pt_flags & PF_PKT) {
790 				pti->pt_send |= TIOCPKT_IOCTL;
791 				ptcwakeup(tp, FREAD);
792 			}
793 			SET(tp->t_lflag, EXTPROC);
794 		} else {
795 			if (ISSET(tp->t_lflag, EXTPROC) &&
796 			    (pti->pt_flags & PF_PKT)) {
797 				pti->pt_send |= TIOCPKT_IOCTL;
798 				ptcwakeup(tp, FREAD);
799 			}
800 			CLR(tp->t_lflag, EXTPROC);
801 		}
802 		return(0);
803 	} else
804 	if (cdev != NULL && cdev->d_open == ptcopen)
805 		switch (cmd) {
806 
807 		case TIOCGPGRP:
808 			/*
809 			 * We avoid calling ttioctl on the controller since,
810 			 * in that case, tp must be the controlling terminal.
811 			 */
812 			*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
813 			return (0);
814 
815 		case TIOCPKT:
816 			if (*(int *)data) {
817 				if (pti->pt_flags & PF_UCNTL)
818 					return (EINVAL);
819 				pti->pt_flags |= PF_PKT;
820 			} else
821 				pti->pt_flags &= ~PF_PKT;
822 			return (0);
823 
824 		case TIOCUCNTL:
825 			if (*(int *)data) {
826 				if (pti->pt_flags & PF_PKT)
827 					return (EINVAL);
828 				pti->pt_flags |= PF_UCNTL;
829 			} else
830 				pti->pt_flags &= ~PF_UCNTL;
831 			return (0);
832 
833 		case TIOCREMOTE:
834 			if (*(int *)data)
835 				pti->pt_flags |= PF_REMOTE;
836 			else
837 				pti->pt_flags &= ~PF_REMOTE;
838 			ttyflush(tp, FREAD|FWRITE);
839 			return (0);
840 
841 #ifdef COMPAT_OLDTTY
842 		case TIOCSETP:
843 		case TIOCSETN:
844 #endif
845 		case TIOCSETD:
846 		case TIOCSETA:
847 		case TIOCSETAW:
848 		case TIOCSETAF:
849 			ndflush(&tp->t_outq, tp->t_outq.c_cc);
850 			break;
851 
852 		case TIOCSIG:
853 			sig = (int)(long)*(caddr_t *)data;
854 			if (sig <= 0 || sig >= NSIG)
855 				return (EINVAL);
856 			if (!ISSET(tp->t_lflag, NOFLSH))
857 				ttyflush(tp, FREAD|FWRITE);
858 			if ((sig == SIGINFO) &&
859 			    (!ISSET(tp->t_lflag, NOKERNINFO)))
860 				ttyinfo(tp);
861 			pgsignal(tp->t_pgrp, sig, 1);
862 			return(0);
863 		}
864 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
865 	if (error == EPASSTHROUGH)
866 		 error = ttioctl(tp, cmd, data, flag, p);
867 	if (error == EPASSTHROUGH) {
868 		if (pti->pt_flags & PF_UCNTL &&
869 		    (cmd & ~0xff) == UIOCCMD(0)) {
870 			if (cmd & 0xff) {
871 				pti->pt_ucntl = (u_char)cmd;
872 				ptcwakeup(tp, FREAD);
873 			}
874 			return (0);
875 		}
876 	}
877 	/*
878 	 * If external processing and packet mode send ioctl packet.
879 	 */
880 	if (ISSET(tp->t_lflag, EXTPROC) && (pti->pt_flags & PF_PKT)) {
881 		switch(cmd) {
882 		case TIOCSETA:
883 		case TIOCSETAW:
884 		case TIOCSETAF:
885 #ifdef COMPAT_OLDTTY
886 		case TIOCSETP:
887 		case TIOCSETN:
888 		case TIOCSETC:
889 		case TIOCSLTC:
890 		case TIOCLBIS:
891 		case TIOCLBIC:
892 		case TIOCLSET:
893 #endif
894 			pti->pt_send |= TIOCPKT_IOCTL;
895 			ptcwakeup(tp, FREAD);
896 		default:
897 			break;
898 		}
899 	}
900 	stop = ISSET(tp->t_iflag, IXON) && CCEQ(cc[VSTOP], CTRL('s'))
901 		&& CCEQ(cc[VSTART], CTRL('q'));
902 	if (pti->pt_flags & PF_NOSTOP) {
903 		if (stop) {
904 			pti->pt_send &= ~TIOCPKT_NOSTOP;
905 			pti->pt_send |= TIOCPKT_DOSTOP;
906 			pti->pt_flags &= ~PF_NOSTOP;
907 			ptcwakeup(tp, FREAD);
908 		}
909 	} else {
910 		if (!stop) {
911 			pti->pt_send &= ~TIOCPKT_DOSTOP;
912 			pti->pt_send |= TIOCPKT_NOSTOP;
913 			pti->pt_flags |= PF_NOSTOP;
914 			ptcwakeup(tp, FREAD);
915 		}
916 	}
917 	return (error);
918 }
919