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