xref: /netbsd-src/sys/kern/tty_pty.c (revision d48f14661dda8638fee055ba15d35bdfb29b9fa8)
1 /*	$NetBSD: tty_pty.c,v 1.90 2006/06/03 18:18:26 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. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)tty_pty.c	8.4 (Berkeley) 2/20/95
32  */
33 
34 /*
35  * Pseudo-teletype Driver
36  * (Actually two drivers, requiring two entries in 'cdevsw')
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: tty_pty.c,v 1.90 2006/06/03 18:18:26 christos Exp $");
41 
42 #include "opt_compat_sunos.h"
43 #include "opt_ptm.h"
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/ioctl.h>
48 #include <sys/proc.h>
49 #include <sys/tty.h>
50 #include <sys/stat.h>
51 #include <sys/file.h>
52 #include <sys/uio.h>
53 #include <sys/kernel.h>
54 #include <sys/vnode.h>
55 #include <sys/namei.h>
56 #include <sys/signalvar.h>
57 #include <sys/uio.h>
58 #include <sys/filedesc.h>
59 #include <sys/conf.h>
60 #include <sys/poll.h>
61 #include <sys/malloc.h>
62 #include <sys/pty.h>
63 #include <sys/kauth.h>
64 
65 #define	DEFAULT_NPTYS		16	/* default number of initial ptys */
66 #define DEFAULT_MAXPTYS		992	/* default maximum number of ptys */
67 
68 #define BUFSIZ 100		/* Chunk size iomoved to/from user */
69 
70 struct	pt_softc {
71 	struct	tty *pt_tty;
72 	int	pt_flags;
73 	struct	selinfo pt_selr, pt_selw;
74 	u_char	pt_send;
75 	u_char	pt_ucntl;
76 };
77 
78 static struct pt_softc **pt_softc = NULL;	/* pty array */
79 static int maxptys = DEFAULT_MAXPTYS;	/* maximum number of ptys (sysctable) */
80 struct simplelock pt_softc_mutex = SIMPLELOCK_INITIALIZER;
81 int npty = 0;			/* for pstat -t */
82 
83 #define	PF_PKT		0x08		/* packet mode */
84 #define	PF_STOPPED	0x10		/* user told stopped */
85 #define	PF_REMOTE	0x20		/* remote and flow controlled input */
86 #define	PF_NOSTOP	0x40
87 #define PF_UCNTL	0x80		/* user control mode */
88 
89 void	ptyattach(int);
90 void	ptcwakeup(struct tty *, int);
91 void	ptsstart(struct tty *);
92 int	pty_maxptys(int, int);
93 
94 static struct pt_softc **ptyarralloc(int);
95 
96 dev_type_open(ptcopen);
97 dev_type_close(ptcclose);
98 dev_type_read(ptcread);
99 dev_type_write(ptcwrite);
100 dev_type_poll(ptcpoll);
101 dev_type_kqfilter(ptckqfilter);
102 
103 dev_type_open(ptsopen);
104 dev_type_close(ptsclose);
105 dev_type_read(ptsread);
106 dev_type_write(ptswrite);
107 dev_type_stop(ptsstop);
108 dev_type_poll(ptspoll);
109 
110 dev_type_ioctl(ptyioctl);
111 dev_type_tty(ptytty);
112 
113 const struct cdevsw ptc_cdevsw = {
114 	ptcopen, ptcclose, ptcread, ptcwrite, ptyioctl,
115 	nullstop, ptytty, ptcpoll, nommap, ptckqfilter, D_TTY
116 };
117 
118 const struct cdevsw pts_cdevsw = {
119 	ptsopen, ptsclose, ptsread, ptswrite, ptyioctl,
120 	ptsstop, ptytty, ptspoll, nommap, ttykqfilter, D_TTY
121 };
122 
123 #if defined(pmax)
124 const struct cdevsw ptc_ultrix_cdevsw = {
125 	ptcopen, ptcclose, ptcread, ptcwrite, ptyioctl,
126 	nullstop, ptytty, ptcpoll, nommap, ptckqfilter, D_TTY
127 };
128 
129 const struct cdevsw pts_ultrix_cdevsw = {
130 	ptsopen, ptsclose, ptsread, ptswrite, ptyioctl,
131 	ptsstop, ptytty, ptspoll, nommap, ttykqfilter, D_TTY
132 };
133 #endif /* defined(pmax) */
134 
135 /*
136  * Check if a pty is free to use.
137  */
138 int
139 pty_isfree(int minor, int lock)
140 {
141 	struct pt_softc *pt = pt_softc[minor];
142 	if (lock)
143 		simple_lock(&pt_softc_mutex);
144 	minor = pt == NULL || pt->pt_tty == NULL ||
145 	    pt->pt_tty->t_oproc == NULL;
146 	if (lock)
147 		simple_unlock(&pt_softc_mutex);
148 	return minor;
149 }
150 
151 /*
152  * Allocate and zero array of nelem elements.
153  */
154 static struct pt_softc **
155 ptyarralloc(nelem)
156 	int nelem;
157 {
158 	struct pt_softc **pt;
159 	nelem += 10;
160 	pt = malloc(nelem * sizeof *pt, M_DEVBUF, M_WAITOK | M_ZERO);
161 	return pt;
162 }
163 
164 /*
165  * Check if the minor is correct and ensure necessary structures
166  * are properly allocated.
167  */
168 int
169 pty_check(int ptn)
170 {
171 	struct pt_softc *pti;
172 
173 	if (ptn >= npty) {
174 		struct pt_softc **newpt, **oldpt;
175 		int newnpty;
176 
177 		/* check if the requested pty can be granted */
178 		if (ptn >= maxptys) {
179 	    limit_reached:
180 			tablefull("pty", "increase kern.maxptys");
181 			return (ENXIO);
182 		}
183 
184 		/* Allocate a larger pty array */
185 		for (newnpty = npty; newnpty <= ptn;)
186 			newnpty *= 2;
187 		if (newnpty > maxptys)
188 			newnpty = maxptys;
189 		newpt = ptyarralloc(newnpty);
190 
191 		/*
192 		 * Now grab the pty array mutex - we need to ensure
193 		 * that the pty array is consistent while copying it's
194 		 * content to newly allocated, larger space; we also
195 		 * need to be safe against pty_maxptys().
196 		 */
197 		simple_lock(&pt_softc_mutex);
198 
199 		if (newnpty >= maxptys) {
200 			/* limit cut away beneath us... */
201 			newnpty = maxptys;
202 			if (ptn >= newnpty) {
203 				simple_unlock(&pt_softc_mutex);
204 				free(newpt, M_DEVBUF);
205 				goto limit_reached;
206 			}
207 		}
208 
209 		/*
210 		 * If the pty array was not enlarged while we were waiting
211 		 * for mutex, copy current contents of pt_softc[] to newly
212 		 * allocated array and start using the new bigger array.
213 		 */
214 		if (newnpty > npty) {
215 			memcpy(newpt, pt_softc, npty*sizeof(struct pt_softc *));
216 			oldpt = pt_softc;
217 			pt_softc = newpt;
218 			npty = newnpty;
219 		} else {
220 			/* was enlarged when waited for lock, free new space */
221 			oldpt = newpt;
222 		}
223 
224 		simple_unlock(&pt_softc_mutex);
225 		free(oldpt, M_DEVBUF);
226 	}
227 
228 	/*
229 	 * If the entry is not yet allocated, allocate one. The mutex is
230 	 * needed so that the state of pt_softc[] array is consistant
231 	 * in case it has been lengthened above.
232 	 */
233 	if (!pt_softc[ptn]) {
234 		MALLOC(pti, struct pt_softc *, sizeof(struct pt_softc),
235 			M_DEVBUF, M_WAITOK);
236 		memset(pti, 0, sizeof(struct pt_softc));
237 
238 	 	pti->pt_tty = ttymalloc();
239 
240 		simple_lock(&pt_softc_mutex);
241 
242 		/*
243 		 * Check the entry again - it might have been
244 		 * added while we were waiting for mutex.
245 		 */
246 		if (!pt_softc[ptn]) {
247 			tty_attach(pti->pt_tty);
248 			pt_softc[ptn] = pti;
249 		} else {
250 			ttyfree(pti->pt_tty);
251 			free(pti, M_DEVBUF);
252 		}
253 
254 		simple_unlock(&pt_softc_mutex);
255 	}
256 
257 	return (0);
258 }
259 
260 /*
261  * Set maxpty in thread-safe way. Returns 0 in case of error, otherwise
262  * new value of maxptys.
263  */
264 int
265 pty_maxptys(newmax, set)
266 	int newmax, set;
267 {
268 	if (!set)
269 		return (maxptys);
270 
271 	/*
272 	 * We have to grab the pt_softc lock, so that we would pick correct
273 	 * value of npty (might be modified in pty_check()).
274 	 */
275 	simple_lock(&pt_softc_mutex);
276 
277 	/*
278 	 * The value cannot be set to value lower than the highest pty
279 	 * number ever allocated.
280 	 */
281 	if (newmax >= npty)
282 		maxptys = newmax;
283 	else
284 		newmax = 0;
285 
286 	simple_unlock(&pt_softc_mutex);
287 
288 	return newmax;
289 }
290 
291 /*
292  * Establish n (or default if n is 1) ptys in the system.
293  */
294 void
295 ptyattach(n)
296 	int n;
297 {
298 	/* maybe should allow 0 => none? */
299 	if (n <= 1)
300 		n = DEFAULT_NPTYS;
301 	pt_softc = ptyarralloc(n);
302 	npty = n;
303 #ifndef NO_DEV_PTM
304 	ptmattach(1);
305 #endif
306 }
307 
308 /*ARGSUSED*/
309 int
310 ptsopen(dev, flag, devtype, l)
311 	dev_t dev;
312 	int flag, devtype;
313 	struct lwp *l;
314 {
315 	struct proc *p = l->l_proc;
316 	struct pt_softc *pti;
317 	struct tty *tp;
318 	int error;
319 	int ptn = minor(dev);
320 	int s;
321 
322 	if ((error = pty_check(ptn)) != 0)
323 		return (error);
324 
325 	pti = pt_softc[ptn];
326 	tp = pti->pt_tty;
327 
328 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
329 		ttychars(tp);		/* Set up default chars */
330 		tp->t_iflag = TTYDEF_IFLAG;
331 		tp->t_oflag = TTYDEF_OFLAG;
332 		tp->t_lflag = TTYDEF_LFLAG;
333 		tp->t_cflag = TTYDEF_CFLAG;
334 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
335 		ttsetwater(tp);		/* would be done in xxparam() */
336 	} else if (ISSET(tp->t_state, TS_XCLUDE) && kauth_cred_geteuid(p->p_cred) != 0)
337 		return (EBUSY);
338 	if (tp->t_oproc)			/* Ctrlr still around. */
339 		SET(tp->t_state, TS_CARR_ON);
340 
341 	if (!ISSET(flag, O_NONBLOCK)) {
342 		s = spltty();
343 		TTY_LOCK(tp);
344 		while (!ISSET(tp->t_state, TS_CARR_ON)) {
345 			tp->t_wopen++;
346 			error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
347 			    ttopen, 0);
348 			tp->t_wopen--;
349 			if (error) {
350 				TTY_UNLOCK(tp);
351 				splx(s);
352 				return (error);
353 			}
354 		}
355 		TTY_UNLOCK(tp);
356 		splx(s);
357 	}
358 	error = (*tp->t_linesw->l_open)(dev, tp);
359 	ptcwakeup(tp, FREAD|FWRITE);
360 	return (error);
361 }
362 
363 int
364 ptsclose(dev, flag, mode, l)
365 	dev_t dev;
366 	int flag, mode;
367 	struct lwp *l;
368 {
369 	struct pt_softc *pti = pt_softc[minor(dev)];
370 	struct tty *tp = pti->pt_tty;
371 	int error;
372 
373 	error = (*tp->t_linesw->l_close)(tp, flag);
374 	error |= ttyclose(tp);
375 	ptcwakeup(tp, FREAD|FWRITE);
376 	return (error);
377 }
378 
379 int
380 ptsread(dev, uio, flag)
381 	dev_t dev;
382 	struct uio *uio;
383 	int flag;
384 {
385 	struct proc *p = curproc;
386 	struct pt_softc *pti = pt_softc[minor(dev)];
387 	struct tty *tp = pti->pt_tty;
388 	int error = 0;
389 	int cc;
390 	int s;
391 
392 again:
393 	if (pti->pt_flags & PF_REMOTE) {
394 		while (isbackground(p, tp)) {
395 			if (sigismasked(p, SIGTTIN) ||
396 			    p->p_pgrp->pg_jobc == 0 ||
397 			    p->p_flag & P_PPWAIT)
398 				return (EIO);
399 			pgsignal(p->p_pgrp, SIGTTIN, 1);
400 			s = spltty();
401 			TTY_LOCK(tp);
402 			error = ttysleep(tp, (caddr_t)&lbolt,
403 					 TTIPRI | PCATCH | PNORELOCK, ttybg, 0);
404 			splx(s);
405 			if (error)
406 				return (error);
407 		}
408 		s = spltty();
409 		TTY_LOCK(tp);
410 		if (tp->t_canq.c_cc == 0) {
411 			if (flag & IO_NDELAY) {
412 				TTY_UNLOCK(tp);
413 				splx(s);
414 				return (EWOULDBLOCK);
415 			}
416 			error = ttysleep(tp, (caddr_t)&tp->t_canq,
417 					 TTIPRI | PCATCH | PNORELOCK, ttyin, 0);
418 			splx(s);
419 			if (error)
420 				return (error);
421 			goto again;
422 		}
423 		while(error == 0 && tp->t_canq.c_cc > 1 && uio->uio_resid > 0) {
424 			TTY_UNLOCK(tp);
425 			splx(s);
426 			error = ureadc(getc(&tp->t_canq), uio);
427 			s = spltty();
428 			TTY_LOCK(tp);
429 			/* Re-check terminal state here? */
430 		}
431 		if (tp->t_canq.c_cc == 1)
432 			(void) getc(&tp->t_canq);
433 		cc = tp->t_canq.c_cc;
434 		TTY_UNLOCK(tp);
435 		splx(s);
436 		if (cc)
437 			return (error);
438 	} else
439 		if (tp->t_oproc)
440 			error = (*tp->t_linesw->l_read)(tp, uio, flag);
441 	ptcwakeup(tp, FWRITE);
442 	return (error);
443 }
444 
445 /*
446  * Write to pseudo-tty.
447  * Wakeups of controlling tty will happen
448  * indirectly, when tty driver calls ptsstart.
449  */
450 int
451 ptswrite(dev, uio, flag)
452 	dev_t dev;
453 	struct uio *uio;
454 	int flag;
455 {
456 	struct pt_softc *pti = pt_softc[minor(dev)];
457 	struct tty *tp = pti->pt_tty;
458 
459 	if (tp->t_oproc == 0)
460 		return (EIO);
461 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
462 }
463 
464 /*
465  * Poll pseudo-tty.
466  */
467 int
468 ptspoll(dev, events, l)
469 	dev_t dev;
470 	int events;
471 	struct lwp *l;
472 {
473 	struct pt_softc *pti = pt_softc[minor(dev)];
474 	struct tty *tp = pti->pt_tty;
475 
476 	if (tp->t_oproc == 0)
477 		return (POLLHUP);
478 
479 	return ((*tp->t_linesw->l_poll)(tp, events, l));
480 }
481 
482 /*
483  * Start output on pseudo-tty.
484  * Wake up process polling or sleeping for input from controlling tty.
485  * Called with tty slock held.
486  */
487 void
488 ptsstart(tp)
489 	struct tty *tp;
490 {
491 	struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
492 
493 	if (ISSET(tp->t_state, TS_TTSTOP))
494 		return;
495 	if (pti->pt_flags & PF_STOPPED) {
496 		pti->pt_flags &= ~PF_STOPPED;
497 		pti->pt_send = TIOCPKT_START;
498 	}
499 
500 	selnotify(&pti->pt_selr, NOTE_SUBMIT);
501 	wakeup((caddr_t)&tp->t_outq.c_cf);
502 }
503 
504 /*
505  * Stop output.
506  * Called with tty slock held.
507  */
508 void
509 ptsstop(tp, flush)
510 	struct tty *tp;
511 	int flush;
512 {
513 	struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
514 
515 	/* note: FLUSHREAD and FLUSHWRITE already ok */
516 	if (flush == 0) {
517 		flush = TIOCPKT_STOP;
518 		pti->pt_flags |= PF_STOPPED;
519 	} else
520 		pti->pt_flags &= ~PF_STOPPED;
521 	pti->pt_send |= flush;
522 
523 	/* change of perspective */
524 	if (flush & FREAD) {
525 		selnotify(&pti->pt_selw, NOTE_SUBMIT);
526 		wakeup((caddr_t)&tp->t_rawq.c_cf);
527 	}
528 	if (flush & FWRITE) {
529 		selnotify(&pti->pt_selr, NOTE_SUBMIT);
530 		wakeup((caddr_t)&tp->t_outq.c_cf);
531 	}
532 }
533 
534 void
535 ptcwakeup(tp, flag)
536 	struct tty *tp;
537 	int flag;
538 {
539 	struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
540 	int s;
541 
542 	s = spltty();
543 	TTY_LOCK(tp);
544 	if (flag & FREAD) {
545 		selnotify(&pti->pt_selr, NOTE_SUBMIT);
546 		wakeup((caddr_t)&tp->t_outq.c_cf);
547 	}
548 	if (flag & FWRITE) {
549 		selnotify(&pti->pt_selw, NOTE_SUBMIT);
550 		wakeup((caddr_t)&tp->t_rawq.c_cf);
551 	}
552 	TTY_UNLOCK(tp);
553 	splx(s);
554 }
555 
556 /*ARGSUSED*/
557 int
558 ptcopen(dev, flag, devtype, l)
559 	dev_t dev;
560 	int flag, devtype;
561 	struct lwp *l;
562 {
563 	struct pt_softc *pti;
564 	struct tty *tp;
565 	int error;
566 	int ptn = minor(dev);
567 	int s;
568 
569 	if ((error = pty_check(ptn)) != 0)
570 		return (error);
571 
572 	pti = pt_softc[ptn];
573 	tp = pti->pt_tty;
574 
575 	s = spltty();
576 	TTY_LOCK(tp);
577 	if (tp->t_oproc) {
578 		TTY_UNLOCK(tp);
579 		splx(s);
580 		return (EIO);
581 	}
582 	tp->t_oproc = ptsstart;
583 	TTY_UNLOCK(tp);
584 	splx(s);
585 	(void)(*tp->t_linesw->l_modem)(tp, 1);
586 	CLR(tp->t_lflag, EXTPROC);
587 	pti->pt_flags = 0;
588 	pti->pt_send = 0;
589 	pti->pt_ucntl = 0;
590 	return (0);
591 }
592 
593 /*ARGSUSED*/
594 int
595 ptcclose(dev, flag, devtype, l)
596 	dev_t dev;
597 	int flag, devtype;
598 	struct lwp *l;
599 {
600 	struct pt_softc *pti = pt_softc[minor(dev)];
601 	struct tty *tp = pti->pt_tty;
602 	int s;
603 
604 	(void)(*tp->t_linesw->l_modem)(tp, 0);
605 	CLR(tp->t_state, TS_CARR_ON);
606 	s = spltty();
607 	TTY_LOCK(tp);
608 	tp->t_oproc = 0;		/* mark closed */
609 	TTY_UNLOCK(tp);
610 	splx(s);
611 	return (0);
612 }
613 
614 int
615 ptcread(dev, uio, flag)
616 	dev_t dev;
617 	struct uio *uio;
618 	int flag;
619 {
620 	struct pt_softc *pti = pt_softc[minor(dev)];
621 	struct tty *tp = pti->pt_tty;
622 	u_char bf[BUFSIZ];
623 	int error = 0, cc;
624 	int s;
625 
626 	/*
627 	 * We want to block until the slave
628 	 * is open, and there's something to read;
629 	 * but if we lost the slave or we're NBIO,
630 	 * then return the appropriate error instead.
631 	 */
632 	s = spltty();
633 	TTY_LOCK(tp);
634 	for (;;) {
635 		if (ISSET(tp->t_state, TS_ISOPEN)) {
636 			if (pti->pt_flags & PF_PKT && pti->pt_send) {
637 				TTY_UNLOCK(tp);
638 				splx(s);
639 				error = ureadc((int)pti->pt_send, uio);
640 				if (error)
641 					return (error);
642 				/*
643 				 * Since we don't have the tty locked, there's
644 				 * a risk of messing up `t_termios'. This is
645 				 * relevant only if the tty got closed and then
646 				 * opened again while we were out uiomoving.
647 				 */
648 				if (pti->pt_send & TIOCPKT_IOCTL) {
649 					cc = min(uio->uio_resid,
650 						sizeof(tp->t_termios));
651 					uiomove((caddr_t) &tp->t_termios,
652 						cc, uio);
653 				}
654 				pti->pt_send = 0;
655 				return (0);
656 			}
657 			if (pti->pt_flags & PF_UCNTL && pti->pt_ucntl) {
658 				TTY_UNLOCK(tp);
659 				splx(s);
660 				error = ureadc((int)pti->pt_ucntl, uio);
661 				if (error)
662 					return (error);
663 				pti->pt_ucntl = 0;
664 				return (0);
665 			}
666 			if (tp->t_outq.c_cc && !ISSET(tp->t_state, TS_TTSTOP))
667 				break;
668 		}
669 		if (!ISSET(tp->t_state, TS_CARR_ON)) {
670 			error = 0;	/* EOF */
671 			goto out;
672 		}
673 		if (flag & IO_NDELAY) {
674 			error = EWOULDBLOCK;
675 			goto out;
676 		}
677 		error = ltsleep((caddr_t)&tp->t_outq.c_cf, TTIPRI | PCATCH,
678 				ttyin, 0, &tp->t_slock);
679 		if (error)
680 			goto out;
681 	}
682 
683 	if (pti->pt_flags & (PF_PKT|PF_UCNTL)) {
684 		TTY_UNLOCK(tp);
685 		splx(s);
686 		error = ureadc(0, uio);
687 		s = spltty();
688 		TTY_LOCK(tp);
689 		if (error == 0 && !ISSET(tp->t_state, TS_ISOPEN))
690 			error = EIO;
691 	}
692 	while (uio->uio_resid > 0 && error == 0) {
693 		cc = q_to_b(&tp->t_outq, bf, min(uio->uio_resid, BUFSIZ));
694 		if (cc <= 0)
695 			break;
696 		TTY_UNLOCK(tp);
697 		splx(s);
698 		error = uiomove(bf, cc, uio);
699 		s = spltty();
700 		TTY_LOCK(tp);
701 		if (error == 0 && !ISSET(tp->t_state, TS_ISOPEN))
702 			error = EIO;
703 	}
704 
705 	if (tp->t_outq.c_cc <= tp->t_lowat) {
706 		if (ISSET(tp->t_state, TS_ASLEEP)) {
707 			CLR(tp->t_state, TS_ASLEEP);
708 			wakeup((caddr_t)&tp->t_outq);
709 		}
710 		selnotify(&tp->t_wsel, NOTE_SUBMIT);
711 	}
712 out:
713 	TTY_UNLOCK(tp);
714 	splx(s);
715 	return (error);
716 }
717 
718 
719 int
720 ptcwrite(dev, uio, flag)
721 	dev_t dev;
722 	struct uio *uio;
723 	int flag;
724 {
725 	struct pt_softc *pti = pt_softc[minor(dev)];
726 	struct tty *tp = pti->pt_tty;
727 	u_char *cp = NULL;
728 	int cc = 0;
729 	u_char locbuf[BUFSIZ];
730 	int cnt = 0;
731 	int error = 0;
732 	int s;
733 
734 again:
735 	s = spltty();
736 	TTY_LOCK(tp);
737 	if (!ISSET(tp->t_state, TS_ISOPEN))
738 		goto block;
739 	if (pti->pt_flags & PF_REMOTE) {
740 		if (tp->t_canq.c_cc)
741 			goto block;
742 		while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) {
743 			if (cc == 0) {
744 				cc = min(uio->uio_resid, BUFSIZ);
745 				cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
746 				cp = locbuf;
747 				TTY_UNLOCK(tp);
748 				splx(s);
749 				error = uiomove((caddr_t)cp, cc, uio);
750 				if (error)
751 					return (error);
752 				s = spltty();
753 				TTY_LOCK(tp);
754 				/* check again for safety */
755 				if (!ISSET(tp->t_state, TS_ISOPEN)) {
756 					error = EIO;
757 					goto out;
758 				}
759 			}
760 			if (cc)
761 				(void) b_to_q(cp, cc, &tp->t_canq);
762 			cc = 0;
763 		}
764 		(void) putc(0, &tp->t_canq);
765 		ttwakeup(tp);
766 		wakeup((caddr_t)&tp->t_canq);
767 		error = 0;
768 		goto out;
769 	}
770 	while (uio->uio_resid > 0) {
771 		if (cc == 0) {
772 			cc = min(uio->uio_resid, BUFSIZ);
773 			cp = locbuf;
774 			TTY_UNLOCK(tp);
775 			splx(s);
776 			error = uiomove((caddr_t)cp, cc, uio);
777 			if (error)
778 				return (error);
779 			s = spltty();
780 			TTY_LOCK(tp);
781 			/* check again for safety */
782 			if (!ISSET(tp->t_state, TS_ISOPEN)) {
783 				error = EIO;
784 				goto out;
785 			}
786 		}
787 		while (cc > 0) {
788 			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
789 			   (tp->t_canq.c_cc > 0 || !ISSET(tp->t_lflag, ICANON))) {
790 				wakeup((caddr_t)&tp->t_rawq);
791 				goto block;
792 			}
793 			/* XXX - should change l_rint to be called with lock
794 			 *	 see also tty.c:ttyinput_wlock()
795 			 */
796 			TTY_UNLOCK(tp);
797 			splx(s);
798 			(*tp->t_linesw->l_rint)(*cp++, tp);
799 			s = spltty();
800 			TTY_LOCK(tp);
801 			cnt++;
802 			cc--;
803 		}
804 		cc = 0;
805 	}
806 	error = 0;
807 	goto out;
808 
809 block:
810 	/*
811 	 * Come here to wait for slave to open, for space
812 	 * in outq, or space in rawq.
813 	 */
814 	if (!ISSET(tp->t_state, TS_CARR_ON)) {
815 		error = EIO;
816 		goto out;
817 	}
818 	if (flag & IO_NDELAY) {
819 		/* adjust for data copied in but not written */
820 		uio->uio_resid += cc;
821 		error = cnt == 0 ? EWOULDBLOCK : 0;
822 		goto out;
823 	}
824 	error = ltsleep((caddr_t)&tp->t_rawq.c_cf, TTOPRI | PCATCH | PNORELOCK,
825 		       ttyout, 0, &tp->t_slock);
826 	splx(s);
827 	if (error) {
828 		/* adjust for data copied in but not written */
829 		uio->uio_resid += cc;
830 		return (error);
831 	}
832 	goto again;
833 
834 out:
835 	TTY_UNLOCK(tp);
836 	splx(s);
837 	return (error);
838 }
839 
840 int
841 ptcpoll(dev, events, l)
842 	dev_t dev;
843 	int events;
844 	struct lwp *l;
845 {
846 	struct pt_softc *pti = pt_softc[minor(dev)];
847 	struct tty *tp = pti->pt_tty;
848 	int revents = 0;
849 	int s = splsoftclock();
850 
851 	if (events & (POLLIN | POLLRDNORM))
852 		if (ISSET(tp->t_state, TS_ISOPEN) &&
853 		    ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
854 		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
855 		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
856 			revents |= events & (POLLIN | POLLRDNORM);
857 
858 	if (events & (POLLOUT | POLLWRNORM))
859 		if (ISSET(tp->t_state, TS_ISOPEN) &&
860 		    ((pti->pt_flags & PF_REMOTE) ?
861 		     (tp->t_canq.c_cc == 0) :
862 		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
863 		      (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))))
864 			revents |= events & (POLLOUT | POLLWRNORM);
865 
866 	if (events & POLLHUP)
867 		if (!ISSET(tp->t_state, TS_CARR_ON))
868 			revents |= POLLHUP;
869 
870 	if (revents == 0) {
871 		if (events & (POLLIN | POLLHUP | POLLRDNORM))
872 			selrecord(l, &pti->pt_selr);
873 
874 		if (events & (POLLOUT | POLLWRNORM))
875 			selrecord(l, &pti->pt_selw);
876 	}
877 
878 	splx(s);
879 	return (revents);
880 }
881 
882 static void
883 filt_ptcrdetach(struct knote *kn)
884 {
885 	struct pt_softc *pti;
886 	int		s;
887 
888 	pti = kn->kn_hook;
889 	s = spltty();
890 	SLIST_REMOVE(&pti->pt_selr.sel_klist, kn, knote, kn_selnext);
891 	splx(s);
892 }
893 
894 static int
895 filt_ptcread(struct knote *kn, long hint)
896 {
897 	struct pt_softc *pti;
898 	struct tty	*tp;
899 	int canread;
900 
901 	pti = kn->kn_hook;
902 	tp = pti->pt_tty;
903 
904 	canread = (ISSET(tp->t_state, TS_ISOPEN) &&
905 		    ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
906 		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
907 		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)));
908 
909 	if (canread) {
910 		/*
911 		 * c_cc is number of characters after output post-processing;
912 		 * the amount of data actually read(2) depends on
913 		 * setting of input flags for the terminal.
914 		 */
915 		kn->kn_data = tp->t_outq.c_cc;
916 		if (((pti->pt_flags & PF_PKT) && pti->pt_send) ||
917 		    ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))
918 			kn->kn_data++;
919 	}
920 
921 	return (canread);
922 }
923 
924 static void
925 filt_ptcwdetach(struct knote *kn)
926 {
927 	struct pt_softc *pti;
928 	int		s;
929 
930 	pti = kn->kn_hook;
931 	s = spltty();
932 	SLIST_REMOVE(&pti->pt_selw.sel_klist, kn, knote, kn_selnext);
933 	splx(s);
934 }
935 
936 static int
937 filt_ptcwrite(struct knote *kn, long hint)
938 {
939 	struct pt_softc *pti;
940 	struct tty	*tp;
941 	int canwrite;
942 	int nwrite;
943 
944 	pti = kn->kn_hook;
945 	tp = pti->pt_tty;
946 
947 	canwrite = (ISSET(tp->t_state, TS_ISOPEN) &&
948 		    ((pti->pt_flags & PF_REMOTE) ?
949 		     (tp->t_canq.c_cc == 0) :
950 		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
951 		      (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))));
952 
953 	if (canwrite) {
954 		if (pti->pt_flags & PF_REMOTE)
955 			nwrite = tp->t_canq.c_cn;
956 		else {
957 			/* this is guaranteed to be > 0 due to above check */
958 			nwrite = tp->t_canq.c_cn
959 				- (tp->t_rawq.c_cc + tp->t_canq.c_cc);
960 		}
961 		kn->kn_data = nwrite;
962 	}
963 
964 	return (canwrite);
965 }
966 
967 static const struct filterops ptcread_filtops =
968 	{ 1, NULL, filt_ptcrdetach, filt_ptcread };
969 static const struct filterops ptcwrite_filtops =
970 	{ 1, NULL, filt_ptcwdetach, filt_ptcwrite };
971 
972 int
973 ptckqfilter(dev_t dev, struct knote *kn)
974 {
975 	struct pt_softc *pti = pt_softc[minor(dev)];
976 	struct klist	*klist;
977 	int		s;
978 
979 	switch (kn->kn_filter) {
980 	case EVFILT_READ:
981 		klist = &pti->pt_selr.sel_klist;
982 		kn->kn_fop = &ptcread_filtops;
983 		break;
984 	case EVFILT_WRITE:
985 		klist = &pti->pt_selw.sel_klist;
986 		kn->kn_fop = &ptcwrite_filtops;
987 		break;
988 	default:
989 		return (1);
990 	}
991 
992 	kn->kn_hook = pti;
993 
994 	s = spltty();
995 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
996 	splx(s);
997 
998 	return (0);
999 }
1000 
1001 struct tty *
1002 ptytty(dev)
1003 	dev_t dev;
1004 {
1005 	struct pt_softc *pti = pt_softc[minor(dev)];
1006 	struct tty *tp = pti->pt_tty;
1007 
1008 	return (tp);
1009 }
1010 
1011 /*ARGSUSED*/
1012 int
1013 ptyioctl(dev, cmd, data, flag, l)
1014 	dev_t dev;
1015 	u_long cmd;
1016 	caddr_t data;
1017 	int flag;
1018 	struct lwp *l;
1019 {
1020 	struct pt_softc *pti = pt_softc[minor(dev)];
1021 	struct tty *tp = pti->pt_tty;
1022 	const struct cdevsw *cdev;
1023 	u_char *cc = tp->t_cc;
1024 	int stop, error, sig;
1025 	int s;
1026 
1027 	/*
1028 	 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
1029 	 * ttywflush(tp) will hang if there are characters in the outq.
1030 	 */
1031 	if (cmd == TIOCEXT) {
1032 		/*
1033 		 * When the EXTPROC bit is being toggled, we need
1034 		 * to send an TIOCPKT_IOCTL if the packet driver
1035 		 * is turned on.
1036 		 */
1037 		if (*(int *)data) {
1038 			if (pti->pt_flags & PF_PKT) {
1039 				pti->pt_send |= TIOCPKT_IOCTL;
1040 				ptcwakeup(tp, FREAD);
1041 			}
1042 			SET(tp->t_lflag, EXTPROC);
1043 		} else {
1044 			if (ISSET(tp->t_lflag, EXTPROC) &&
1045 			    (pti->pt_flags & PF_PKT)) {
1046 				pti->pt_send |= TIOCPKT_IOCTL;
1047 				ptcwakeup(tp, FREAD);
1048 			}
1049 			CLR(tp->t_lflag, EXTPROC);
1050 		}
1051 		return(0);
1052 	}
1053 
1054 #ifndef NO_DEV_PTM
1055 	/* Allow getting the name from either the master or the slave */
1056 	if (cmd == TIOCPTSNAME)
1057 		return pty_fill_ptmget(l, dev, -1, -1, data);
1058 #endif
1059 
1060 	cdev = cdevsw_lookup(dev);
1061 	if (cdev != NULL && cdev->d_open == ptcopen)
1062 		switch (cmd) {
1063 #ifndef NO_DEV_PTM
1064 		case TIOCGRANTPT:
1065 			return pty_grant_slave(l, dev);
1066 #endif
1067 
1068 		case TIOCGPGRP:
1069 			/*
1070 			 * We avoid calling ttioctl on the controller since,
1071 			 * in that case, tp must be the controlling terminal.
1072 			 */
1073 			*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
1074 			return (0);
1075 
1076 		case TIOCPKT:
1077 			if (*(int *)data) {
1078 				if (pti->pt_flags & PF_UCNTL)
1079 					return (EINVAL);
1080 				pti->pt_flags |= PF_PKT;
1081 			} else
1082 				pti->pt_flags &= ~PF_PKT;
1083 			return (0);
1084 
1085 		case TIOCUCNTL:
1086 			if (*(int *)data) {
1087 				if (pti->pt_flags & PF_PKT)
1088 					return (EINVAL);
1089 				pti->pt_flags |= PF_UCNTL;
1090 			} else
1091 				pti->pt_flags &= ~PF_UCNTL;
1092 			return (0);
1093 
1094 		case TIOCREMOTE:
1095 			if (*(int *)data)
1096 				pti->pt_flags |= PF_REMOTE;
1097 			else
1098 				pti->pt_flags &= ~PF_REMOTE;
1099 			s = spltty();
1100 			TTY_LOCK(tp);
1101 			ttyflush(tp, FREAD|FWRITE);
1102 			TTY_UNLOCK(tp);
1103 			splx(s);
1104 			return (0);
1105 
1106 #ifdef COMPAT_OLDTTY
1107 		case TIOCSETP:
1108 		case TIOCSETN:
1109 #endif
1110 		case TIOCSETD:
1111 		case TIOCSETA:
1112 		case TIOCSETAW:
1113 		case TIOCSETAF:
1114 			TTY_LOCK(tp);
1115 			ndflush(&tp->t_outq, tp->t_outq.c_cc);
1116 			TTY_UNLOCK(tp);
1117 			break;
1118 
1119 		case TIOCSIG:
1120 			sig = (int)(long)*(caddr_t *)data;
1121 			if (sig <= 0 || sig >= NSIG)
1122 				return (EINVAL);
1123 			TTY_LOCK(tp);
1124 			if (!ISSET(tp->t_lflag, NOFLSH))
1125 				ttyflush(tp, FREAD|FWRITE);
1126 			if ((sig == SIGINFO) &&
1127 			    (!ISSET(tp->t_lflag, NOKERNINFO)))
1128 				ttyinfo(tp, 1);
1129 			TTY_UNLOCK(tp);
1130 			pgsignal(tp->t_pgrp, sig, 1);
1131 			return(0);
1132 		}
1133 
1134 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
1135 	if (error == EPASSTHROUGH)
1136 		 error = ttioctl(tp, cmd, data, flag, l);
1137 	if (error == EPASSTHROUGH) {
1138 		if (pti->pt_flags & PF_UCNTL &&
1139 		    (cmd & ~0xff) == UIOCCMD(0)) {
1140 			if (cmd & 0xff) {
1141 				pti->pt_ucntl = (u_char)cmd;
1142 				ptcwakeup(tp, FREAD);
1143 			}
1144 			return (0);
1145 		}
1146 	}
1147 	/*
1148 	 * If external processing and packet mode send ioctl packet.
1149 	 */
1150 	if (ISSET(tp->t_lflag, EXTPROC) && (pti->pt_flags & PF_PKT)) {
1151 		switch(cmd) {
1152 		case TIOCSETA:
1153 		case TIOCSETAW:
1154 		case TIOCSETAF:
1155 #ifdef COMPAT_OLDTTY
1156 		case TIOCSETP:
1157 		case TIOCSETN:
1158 		case TIOCSETC:
1159 		case TIOCSLTC:
1160 		case TIOCLBIS:
1161 		case TIOCLBIC:
1162 		case TIOCLSET:
1163 #endif
1164 			pti->pt_send |= TIOCPKT_IOCTL;
1165 			ptcwakeup(tp, FREAD);
1166 		default:
1167 			break;
1168 		}
1169 	}
1170 	stop = ISSET(tp->t_iflag, IXON) && CCEQ(cc[VSTOP], CTRL('s'))
1171 		&& CCEQ(cc[VSTART], CTRL('q'));
1172 	if (pti->pt_flags & PF_NOSTOP) {
1173 		if (stop) {
1174 			pti->pt_send &= ~TIOCPKT_NOSTOP;
1175 			pti->pt_send |= TIOCPKT_DOSTOP;
1176 			pti->pt_flags &= ~PF_NOSTOP;
1177 			ptcwakeup(tp, FREAD);
1178 		}
1179 	} else {
1180 		if (!stop) {
1181 			pti->pt_send &= ~TIOCPKT_DOSTOP;
1182 			pti->pt_send |= TIOCPKT_NOSTOP;
1183 			pti->pt_flags |= PF_NOSTOP;
1184 			ptcwakeup(tp, FREAD);
1185 		}
1186 	}
1187 	return (error);
1188 }
1189