xref: /dragonfly/sys/kern/tty_pty.c (revision 4a65f651)
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)tty_pty.c	8.4 (Berkeley) 2/20/95
34  * $FreeBSD: src/sys/kern/tty_pty.c,v 1.74.2.4 2002/02/20 19:58:13 dillon Exp $
35  * $DragonFly: src/sys/kern/tty_pty.c,v 1.21 2008/08/13 10:29:38 swildner Exp $
36  */
37 
38 /*
39  * Pseudo-teletype Driver
40  * (Actually two drivers, requiring two dev_ops structures)
41  */
42 #include "use_pty.h"		/* XXX */
43 #include "opt_compat.h"
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
48 #include <sys/ioctl_compat.h>
49 #endif
50 #include <sys/proc.h>
51 #include <sys/priv.h>
52 #include <sys/tty.h>
53 #include <sys/conf.h>
54 #include <sys/fcntl.h>
55 #include <sys/poll.h>
56 #include <sys/kernel.h>
57 #include <sys/vnode.h>
58 #include <sys/signalvar.h>
59 #include <sys/malloc.h>
60 #include <sys/device.h>
61 #include <sys/thread2.h>
62 #include <sys/devfs.h>
63 
64 DEVFS_DECLARE_CLONE_BITMAP(pty);
65 DEVFS_DECLARE_CLONE_BITMAP(pts);
66 
67 MALLOC_DEFINE(M_PTY, "ptys", "pty data structures");
68 
69 static void ptsstart (struct tty *tp);
70 static void ptsstop (struct tty *tp, int rw);
71 static void ptcwakeup (struct tty *tp, int flag);
72 static void ptyinit (int n);
73 
74 static	d_open_t	ptsopen;
75 static	d_close_t	ptsclose;
76 static	d_read_t	ptsread;
77 static	d_write_t	ptswrite;
78 static	d_ioctl_t	ptyioctl;
79 static	d_open_t	ptcopen;
80 static	d_close_t	ptcclose;
81 static	d_read_t	ptcread;
82 static	d_write_t	ptcwrite;
83 static	d_poll_t	ptcpoll;
84 #if 0
85 static	d_clone_t 	ptyclone;
86 #endif
87 
88 #define	CDEV_MAJOR_S	5
89 static struct dev_ops pts_ops = {
90 	{ "pts", CDEV_MAJOR_S, D_TTY | D_KQFILTER },
91 	.d_open =	ptsopen,
92 	.d_close =	ptsclose,
93 	.d_read =	ptsread,
94 	.d_write =	ptswrite,
95 	.d_ioctl =	ptyioctl,
96 	.d_poll =	ttypoll,
97 	.d_kqfilter =	ttykqfilter,
98 	.d_revoke =	ttyrevoke
99 };
100 
101 #define	CDEV_MAJOR_C	6
102 static struct dev_ops ptc_ops = {
103 	{ "ptc", CDEV_MAJOR_C, D_TTY | D_KQFILTER | D_MASTER },
104 	.d_open =	ptcopen,
105 	.d_close =	ptcclose,
106 	.d_read =	ptcread,
107 	.d_write =	ptcwrite,
108 	.d_ioctl =	ptyioctl,
109 	.d_poll =	ptcpoll,
110 	.d_kqfilter =	ttykqfilter,
111 	.d_revoke =	ttyrevoke
112 };
113 
114 #define BUFSIZ 100		/* Chunk size iomoved to/from user */
115 
116 struct	pt_ioctl {
117 	int	pt_flags;
118 	struct	selinfo pt_selr, pt_selw;
119 	u_char	pt_send;
120 	u_char	pt_ucntl;
121 	struct tty pt_tty;
122 	cdev_t	devs, devc;
123 	struct	prison *pt_prison;
124 	short	ref_count;
125 };
126 
127 #define	PF_PKT		0x08		/* packet mode */
128 #define	PF_STOPPED	0x10		/* user told stopped */
129 #define	PF_REMOTE	0x20		/* remote and flow controlled input */
130 #define	PF_NOSTOP	0x40
131 #define PF_UCNTL	0x80		/* user control mode */
132 
133 /*
134  * This function creates and initializes a pts/ptc pair
135  *
136  * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
137  * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
138  *
139  * XXX: define and add mapping of upper minor bits to allow more
140  *      than 256 ptys.
141  */
142 static void
143 ptyinit(int n)
144 {
145 	cdev_t devs, devc;
146 	char *names = "pqrsPQRS";
147 	struct pt_ioctl *pt;
148 
149 	/* For now we only map the lower 8 bits of the minor */
150 	if (n & ~0xff)
151 		return;
152 
153 	pt = kmalloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO);
154 	pt->devs = devs = make_dev(&pts_ops, n,
155 	    0, 0, 0666, "tty%c%r", names[n / 32], n % 32);
156 	pt->devc = devc = make_dev(&ptc_ops, n,
157 	    0, 0, 0666, "pty%c%r", names[n / 32], n % 32);
158 
159 	devs->si_drv1 = devc->si_drv1 = pt;
160 	devs->si_tty = devc->si_tty = &pt->pt_tty;
161 	devs->si_flags |= SI_OVERRIDE;	/* uid, gid, perms from dev */
162 	devc->si_flags |= SI_OVERRIDE;	/* uid, gid, perms from dev */
163 	pt->pt_tty.t_dev = devs;
164 	ttyregister(&pt->pt_tty);
165 }
166 
167 #if 0
168 static int
169 ptyclone(struct dev_clone_args *ap)
170 {
171 	int unit;
172 	struct pt_ioctl *pt;
173 
174 	unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(pty), 256);
175 
176 	if (unit < 0)
177 		return 1;
178 
179 	pt = kmalloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO);
180 	pt->ref_count++;
181 	pt->devc = ap->a_dev = make_only_dev(&ptc_ops, unit, ap->a_cred->cr_ruid, 0, 0600, "ptm/%d", unit);
182 	pt->devs = make_dev(&pts_ops, unit, ap->a_cred->cr_ruid, 0, 0600, "pts/%d", unit);
183 
184 	//reference_dev(pt->devc);
185 	//reference_dev(pt->devs);
186 
187 	pt->devs->si_drv1 = pt->devc->si_drv1 = pt;
188 	pt->devs->si_tty = pt->devc->si_tty = &pt->pt_tty;
189 	pt->pt_tty.t_dev = pt->devs;
190 	ttyregister(&pt->pt_tty);
191 
192 	return 0;
193 }
194 #endif
195 
196 /*ARGSUSED*/
197 static	int
198 ptsopen(struct dev_open_args *ap)
199 {
200 	cdev_t dev = ap->a_head.a_dev;
201 	struct tty *tp;
202 	int error;
203 	struct pt_ioctl *pti;
204 
205 	if (!dev->si_drv1)
206 		ptyinit(minor(dev));
207 	if (!dev->si_drv1)
208 		return(ENXIO);
209 	pti = dev->si_drv1;
210 	tp = dev->si_tty;
211 	if ((tp->t_state & TS_ISOPEN) == 0) {
212 		ttychars(tp);		/* Set up default chars */
213 		tp->t_iflag = TTYDEF_IFLAG;
214 		tp->t_oflag = TTYDEF_OFLAG;
215 		tp->t_lflag = TTYDEF_LFLAG;
216 		tp->t_cflag = TTYDEF_CFLAG;
217 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
218 	} else if ((tp->t_state & TS_XCLUDE) && priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) {
219 		return (EBUSY);
220 	} else if (pti->pt_prison != ap->a_cred->cr_prison) {
221 		return (EBUSY);
222 	}
223 	if (tp->t_oproc)			/* Ctrlr still around. */
224 		(void)(*linesw[tp->t_line].l_modem)(tp, 1);
225 	while ((tp->t_state & TS_CARR_ON) == 0) {
226 		if (ap->a_oflags & FNONBLOCK)
227 			break;
228 		error = ttysleep(tp, TSA_CARR_ON(tp), PCATCH, "ptsopn", 0);
229 		if (error)
230 			return (error);
231 	}
232 	error = (*linesw[tp->t_line].l_open)(dev, tp);
233 	if (error == 0)
234 		ptcwakeup(tp, FREAD|FWRITE);
235 
236 #if 0
237 	/* unix98 pty stuff */
238 	if ((!error) && (!memcmp(dev->si_name, "pts/", 4))) {
239 		((struct pt_ioctl *)dev->si_drv1)->ref_count++;
240 		//reference_dev(dev);
241 		//reference_dev(((struct pt_ioctl *)dev->si_drv1)->devc);
242 		//devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(pts), dev->si_uminor-300);
243 	}
244 #endif
245 
246 	return (error);
247 }
248 
249 static	int
250 ptsclose(struct dev_close_args *ap)
251 {
252 	cdev_t dev = ap->a_head.a_dev;
253 	struct tty *tp;
254 	int err;
255 #if 0
256 	/* unix98 pty stuff */
257 	if (!memcmp(dev->si_name, "pts/", 4)) {
258 		if (--((struct pt_ioctl *)dev->si_drv1)->ref_count == 0) {
259 			kfree(dev->si_drv1, M_PTY);
260 			devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(pty), dev->si_uminor);
261 			destroy_dev(dev);
262 		}
263 	}
264 #endif
265 	tp = dev->si_tty;
266 	err = (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
267 	ptsstop(tp, FREAD|FWRITE);
268 	(void) ttyclose(tp);
269 	return (err);
270 }
271 
272 static	int
273 ptsread(struct dev_read_args *ap)
274 {
275 	cdev_t dev = ap->a_head.a_dev;
276 	struct proc *p = curproc;
277 	struct tty *tp = dev->si_tty;
278 	struct pt_ioctl *pti = dev->si_drv1;
279 	struct lwp *lp;
280 
281 	int error = 0;
282 
283 	lp = curthread->td_lwp;
284 
285 again:
286 	if (pti->pt_flags & PF_REMOTE) {
287 		while (isbackground(p, tp)) {
288 			if (SIGISMEMBER(p->p_sigignore, SIGTTIN) ||
289 			    SIGISMEMBER(lp->lwp_sigmask, SIGTTIN) ||
290 			    p->p_pgrp->pg_jobc == 0 || p->p_flag & P_PPWAIT)
291 				return (EIO);
292 			pgsignal(p->p_pgrp, SIGTTIN, 1);
293 			error = ttysleep(tp, &lbolt, PCATCH, "ptsbg", 0);
294 			if (error)
295 				return (error);
296 		}
297 		if (tp->t_canq.c_cc == 0) {
298 			if (ap->a_ioflag & IO_NDELAY)
299 				return (EWOULDBLOCK);
300 			error = ttysleep(tp, TSA_PTS_READ(tp), PCATCH,
301 					 "ptsin", 0);
302 			if (error)
303 				return (error);
304 			goto again;
305 		}
306 		while (tp->t_canq.c_cc > 1 && ap->a_uio->uio_resid > 0)
307 			if (ureadc(clist_getc(&tp->t_canq), ap->a_uio) < 0) {
308 				error = EFAULT;
309 				break;
310 			}
311 		if (tp->t_canq.c_cc == 1)
312 			clist_getc(&tp->t_canq);
313 		if (tp->t_canq.c_cc)
314 			return (error);
315 	} else
316 		if (tp->t_oproc)
317 			error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, ap->a_ioflag);
318 	ptcwakeup(tp, FWRITE);
319 	return (error);
320 }
321 
322 /*
323  * Write to pseudo-tty.
324  * Wakeups of controlling tty will happen
325  * indirectly, when tty driver calls ptsstart.
326  */
327 static	int
328 ptswrite(struct dev_write_args *ap)
329 {
330 	cdev_t dev = ap->a_head.a_dev;
331 	struct tty *tp;
332 
333 	tp = dev->si_tty;
334 	if (tp->t_oproc == 0)
335 		return (EIO);
336 	return ((*linesw[tp->t_line].l_write)(tp, ap->a_uio, ap->a_ioflag));
337 }
338 
339 /*
340  * Start output on pseudo-tty.
341  * Wake up process selecting or sleeping for input from controlling tty.
342  */
343 static void
344 ptsstart(struct tty *tp)
345 {
346 	struct pt_ioctl *pti = tp->t_dev->si_drv1;
347 
348 	if (tp->t_state & TS_TTSTOP)
349 		return;
350 	if (pti->pt_flags & PF_STOPPED) {
351 		pti->pt_flags &= ~PF_STOPPED;
352 		pti->pt_send = TIOCPKT_START;
353 	}
354 	ptcwakeup(tp, FREAD);
355 }
356 
357 static void
358 ptcwakeup(struct tty *tp, int flag)
359 {
360 	struct pt_ioctl *pti = tp->t_dev->si_drv1;
361 
362 	if (flag & FREAD) {
363 		selwakeup(&pti->pt_selr);
364 		wakeup(TSA_PTC_READ(tp));
365 	}
366 	if (flag & FWRITE) {
367 		selwakeup(&pti->pt_selw);
368 		wakeup(TSA_PTC_WRITE(tp));
369 	}
370 }
371 
372 static	int
373 ptcopen(struct dev_open_args *ap)
374 {
375 	cdev_t dev = ap->a_head.a_dev;
376 	struct tty *tp;
377 	struct pt_ioctl *pti;
378 
379 	if (!dev->si_drv1)
380 		ptyinit(minor(dev));
381 	if (!dev->si_drv1)
382 		return(ENXIO);
383 	pti = dev->si_drv1;
384 	if (pti->pt_prison && pti->pt_prison != ap->a_cred->cr_prison)
385 		return(EBUSY);
386 	tp = dev->si_tty;
387 	if (tp->t_oproc)
388 		return (EIO);
389 	tp->t_oproc = ptsstart;
390 	tp->t_stop = ptsstop;
391 	(void)(*linesw[tp->t_line].l_modem)(tp, 1);
392 	tp->t_lflag &= ~EXTPROC;
393 	pti->pt_prison = ap->a_cred->cr_prison;
394 	pti->pt_flags = 0;
395 	pti->pt_send = 0;
396 	pti->pt_ucntl = 0;
397 
398 	pti->devs->si_uid = ap->a_cred->cr_uid;
399 	pti->devs->si_gid = 0;
400 	pti->devs->si_perms = 0600;
401 	pti->devc->si_uid = ap->a_cred->cr_uid;
402 	pti->devc->si_gid = 0;
403 	pti->devc->si_perms = 0600;
404 
405 	return (0);
406 }
407 
408 static	int
409 ptcclose(struct dev_close_args *ap)
410 {
411 	cdev_t dev = ap->a_head.a_dev;
412 	struct tty *tp;
413 	struct pt_ioctl *pti;
414 
415 	tp = dev->si_tty;
416 	(void)(*linesw[tp->t_line].l_modem)(tp, 0);
417 
418 	/*
419 	 * XXX MDMBUF makes no sense for ptys but would inhibit the above
420 	 * l_modem().  CLOCAL makes sense but isn't supported.   Special
421 	 * l_modem()s that ignore carrier drop make no sense for ptys but
422 	 * may be in use because other parts of the line discipline make
423 	 * sense for ptys.  Recover by doing everything that a normal
424 	 * ttymodem() would have done except for sending a SIGHUP.
425 	 */
426 	if (tp->t_state & TS_ISOPEN) {
427 		tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
428 		tp->t_state |= TS_ZOMBIE;
429 		ttyflush(tp, FREAD | FWRITE);
430 	}
431 	tp->t_oproc = 0;		/* mark closed */
432 
433 	pti = dev->si_drv1;
434 	pti->pt_prison = NULL;
435 	pti->devs->si_uid = 0;
436 	pti->devs->si_gid = 0;
437 	pti->devs->si_perms = 0666;
438 	pti->devc->si_uid = 0;
439 	pti->devc->si_gid = 0;
440 	pti->devc->si_perms = 0666;
441 
442 #if 0
443 	if (!memcmp(dev->si_name, "ptm/", 4)) {
444 		((struct pt_ioctl *)dev->si_drv1)->devc = NULL;
445 		if (--((struct pt_ioctl *)dev->si_drv1)->ref_count == 0) {
446 			kfree(dev->si_drv1, M_PTY);
447 			devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(pty), dev->si_uminor);
448 		}
449 		//release_dev(dev);
450 		//release_dev(((struct pt_ioctl *)dev->si_drv1)->devs);
451 	}
452 #endif
453 	return (0);
454 }
455 
456 static	int
457 ptcread(struct dev_read_args *ap)
458 {
459 	cdev_t dev = ap->a_head.a_dev;
460 	struct tty *tp = dev->si_tty;
461 	struct pt_ioctl *pti = dev->si_drv1;
462 	char buf[BUFSIZ];
463 	int error = 0, cc;
464 
465 	/*
466 	 * We want to block until the slave
467 	 * is open, and there's something to read;
468 	 * but if we lost the slave or we're NBIO,
469 	 * then return the appropriate error instead.
470 	 */
471 	for (;;) {
472 		if (tp->t_state&TS_ISOPEN) {
473 			if (pti->pt_flags&PF_PKT && pti->pt_send) {
474 				error = ureadc((int)pti->pt_send, ap->a_uio);
475 				if (error)
476 					return (error);
477 				if (pti->pt_send & TIOCPKT_IOCTL) {
478 					cc = (int)szmin(ap->a_uio->uio_resid,
479 							sizeof(tp->t_termios));
480 					uiomove((caddr_t)&tp->t_termios, cc,
481 						ap->a_uio);
482 				}
483 				pti->pt_send = 0;
484 				return (0);
485 			}
486 			if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
487 				error = ureadc((int)pti->pt_ucntl, ap->a_uio);
488 				if (error)
489 					return (error);
490 				pti->pt_ucntl = 0;
491 				return (0);
492 			}
493 			if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
494 				break;
495 		}
496 		if ((tp->t_state & TS_CONNECTED) == 0)
497 			return (0);	/* EOF */
498 		if (ap->a_ioflag & IO_NDELAY)
499 			return (EWOULDBLOCK);
500 		error = tsleep(TSA_PTC_READ(tp), PCATCH, "ptcin", 0);
501 		if (error)
502 			return (error);
503 	}
504 	if (pti->pt_flags & (PF_PKT|PF_UCNTL))
505 		error = ureadc(0, ap->a_uio);
506 	while (ap->a_uio->uio_resid > 0 && error == 0) {
507 		cc = q_to_b(&tp->t_outq, buf,
508 			    (int)szmin(ap->a_uio->uio_resid, BUFSIZ));
509 		if (cc <= 0)
510 			break;
511 		error = uiomove(buf, (size_t)cc, ap->a_uio);
512 	}
513 	ttwwakeup(tp);
514 	return (error);
515 }
516 
517 static	void
518 ptsstop(struct tty *tp, int flush)
519 {
520 	struct pt_ioctl *pti = tp->t_dev->si_drv1;
521 	int flag;
522 
523 	/* note: FLUSHREAD and FLUSHWRITE already ok */
524 	if (flush == 0) {
525 		flush = TIOCPKT_STOP;
526 		pti->pt_flags |= PF_STOPPED;
527 	} else
528 		pti->pt_flags &= ~PF_STOPPED;
529 	pti->pt_send |= flush;
530 	/* change of perspective */
531 	flag = 0;
532 	if (flush & FREAD)
533 		flag |= FWRITE;
534 	if (flush & FWRITE)
535 		flag |= FREAD;
536 	ptcwakeup(tp, flag);
537 }
538 
539 static	int
540 ptcpoll(struct dev_poll_args *ap)
541 {
542 	cdev_t dev = ap->a_head.a_dev;
543 	struct tty *tp = dev->si_tty;
544 	struct pt_ioctl *pti = dev->si_drv1;
545 	int revents = 0;
546 
547 	if ((tp->t_state & TS_CONNECTED) == 0) {
548 		ap->a_events = seltrue(dev, ap->a_events) | POLLHUP;
549 		return(0);
550 	}
551 
552 	/*
553 	 * Need to block timeouts (ttrstart).
554 	 */
555 	crit_enter();
556 
557 	if (ap->a_events & (POLLIN | POLLRDNORM))
558 		if ((tp->t_state & TS_ISOPEN) &&
559 		    ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
560 		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
561 		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
562 			revents |= ap->a_events & (POLLIN | POLLRDNORM);
563 
564 	if (ap->a_events & (POLLOUT | POLLWRNORM))
565 		if (tp->t_state & TS_ISOPEN &&
566 		    ((pti->pt_flags & PF_REMOTE) ?
567 		     (tp->t_canq.c_cc == 0) :
568 		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
569 		      (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON)))))
570 			revents |= ap->a_events & (POLLOUT | POLLWRNORM);
571 
572 	if (ap->a_events & POLLHUP)
573 		if ((tp->t_state & TS_CARR_ON) == 0)
574 			revents |= POLLHUP;
575 
576 	if (revents == 0) {
577 		if (ap->a_events & (POLLIN | POLLRDNORM))
578 			selrecord(curthread, &pti->pt_selr);
579 
580 		if (ap->a_events & (POLLOUT | POLLWRNORM))
581 			selrecord(curthread, &pti->pt_selw);
582 	}
583 	crit_exit();
584 
585 	ap->a_events = revents;
586 	return (0);
587 }
588 
589 static	int
590 ptcwrite(struct dev_write_args *ap)
591 {
592 	cdev_t dev = ap->a_head.a_dev;
593 	struct tty *tp = dev->si_tty;
594 	u_char *cp = 0;
595 	int cc = 0;
596 	u_char locbuf[BUFSIZ];
597 	int cnt = 0;
598 	struct pt_ioctl *pti = dev->si_drv1;
599 	int error = 0;
600 
601 again:
602 	if ((tp->t_state&TS_ISOPEN) == 0)
603 		goto block;
604 	if (pti->pt_flags & PF_REMOTE) {
605 		if (tp->t_canq.c_cc)
606 			goto block;
607 		while ((ap->a_uio->uio_resid > 0 || cc > 0) &&
608 		       tp->t_canq.c_cc < TTYHOG - 1) {
609 			if (cc == 0) {
610 				cc = (int)szmin(ap->a_uio->uio_resid, BUFSIZ);
611 				cc = imin(cc, TTYHOG - 1 - tp->t_canq.c_cc);
612 				cp = locbuf;
613 				error = uiomove(cp, (size_t)cc, ap->a_uio);
614 				if (error)
615 					return (error);
616 				/* check again for safety */
617 				if ((tp->t_state & TS_ISOPEN) == 0) {
618 					/* adjust as usual */
619 					ap->a_uio->uio_resid += cc;
620 					return (EIO);
621 				}
622 			}
623 			if (cc > 0) {
624 				cc = b_to_q((char *)cp, cc, &tp->t_canq);
625 				/*
626 				 * XXX we don't guarantee that the canq size
627 				 * is >= TTYHOG, so the above b_to_q() may
628 				 * leave some bytes uncopied.  However, space
629 				 * is guaranteed for the null terminator if
630 				 * we don't fail here since (TTYHOG - 1) is
631 				 * not a multiple of CBSIZE.
632 				 */
633 				if (cc > 0)
634 					break;
635 			}
636 		}
637 		/* adjust for data copied in but not written */
638 		ap->a_uio->uio_resid += cc;
639 		clist_putc(0, &tp->t_canq);
640 		ttwakeup(tp);
641 		wakeup(TSA_PTS_READ(tp));
642 		return (0);
643 	}
644 	while (ap->a_uio->uio_resid > 0 || cc > 0) {
645 		if (cc == 0) {
646 			cc = (int)szmin(ap->a_uio->uio_resid, BUFSIZ);
647 			cp = locbuf;
648 			error = uiomove(cp, (size_t)cc, ap->a_uio);
649 			if (error)
650 				return (error);
651 			/* check again for safety */
652 			if ((tp->t_state & TS_ISOPEN) == 0) {
653 				/* adjust for data copied in but not written */
654 				ap->a_uio->uio_resid += cc;
655 				return (EIO);
656 			}
657 		}
658 		while (cc > 0) {
659 			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
660 			   (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) {
661 				wakeup(TSA_HUP_OR_INPUT(tp));
662 				goto block;
663 			}
664 			(*linesw[tp->t_line].l_rint)(*cp++, tp);
665 			cnt++;
666 			cc--;
667 		}
668 		cc = 0;
669 	}
670 	return (0);
671 block:
672 	/*
673 	 * Come here to wait for slave to open, for space
674 	 * in outq, or space in rawq, or an empty canq.
675 	 */
676 	if ((tp->t_state & TS_CONNECTED) == 0) {
677 		/* adjust for data copied in but not written */
678 		ap->a_uio->uio_resid += cc;
679 		return (EIO);
680 	}
681 	if (ap->a_ioflag & IO_NDELAY) {
682 		/* adjust for data copied in but not written */
683 		ap->a_uio->uio_resid += cc;
684 		if (cnt == 0)
685 			return (EWOULDBLOCK);
686 		return (0);
687 	}
688 	error = tsleep(TSA_PTC_WRITE(tp), PCATCH, "ptcout", 0);
689 	if (error) {
690 		/* adjust for data copied in but not written */
691 		ap->a_uio->uio_resid += cc;
692 		return (error);
693 	}
694 	goto again;
695 }
696 
697 /*ARGSUSED*/
698 static	int
699 ptyioctl(struct dev_ioctl_args *ap)
700 {
701 	cdev_t dev = ap->a_head.a_dev;
702 	struct tty *tp = dev->si_tty;
703 	struct pt_ioctl *pti = dev->si_drv1;
704 	u_char *cc = tp->t_cc;
705 	int stop, error;
706 
707 	if (dev_dflags(dev) & D_MASTER) {
708 		switch (ap->a_cmd) {
709 
710 		case TIOCGPGRP:
711 			/*
712 			 * We avoid calling ttioctl on the controller since,
713 			 * in that case, tp must be the controlling terminal.
714 			 */
715 			*(int *)ap->a_data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
716 			return (0);
717 
718 		case TIOCPKT:
719 			if (*(int *)ap->a_data) {
720 				if (pti->pt_flags & PF_UCNTL)
721 					return (EINVAL);
722 				pti->pt_flags |= PF_PKT;
723 			} else
724 				pti->pt_flags &= ~PF_PKT;
725 			return (0);
726 
727 		case TIOCUCNTL:
728 			if (*(int *)ap->a_data) {
729 				if (pti->pt_flags & PF_PKT)
730 					return (EINVAL);
731 				pti->pt_flags |= PF_UCNTL;
732 			} else
733 				pti->pt_flags &= ~PF_UCNTL;
734 			return (0);
735 
736 		case TIOCREMOTE:
737 			if (*(int *)ap->a_data)
738 				pti->pt_flags |= PF_REMOTE;
739 			else
740 				pti->pt_flags &= ~PF_REMOTE;
741 			ttyflush(tp, FREAD|FWRITE);
742 			return (0);
743 		}
744 
745 		/*
746 		 * The rest of the ioctls shouldn't be called until
747 		 * the slave is open.
748 		 */
749 		if ((tp->t_state & TS_ISOPEN) == 0)
750 			return (EAGAIN);
751 
752 		switch (ap->a_cmd) {
753 #ifdef COMPAT_43
754 		case TIOCSETP:
755 		case TIOCSETN:
756 #endif
757 		case TIOCSETD:
758 		case TIOCSETA:
759 		case TIOCSETAW:
760 		case TIOCSETAF:
761 			/*
762 			 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
763 			 * ttywflush(tp) will hang if there are characters in
764 			 * the outq.
765 			 */
766 			ndflush(&tp->t_outq, tp->t_outq.c_cc);
767 			break;
768 
769 		case TIOCSIG:
770 			if (*(unsigned int *)ap->a_data >= NSIG ||
771 			    *(unsigned int *)ap->a_data == 0)
772 				return(EINVAL);
773 			if ((tp->t_lflag&NOFLSH) == 0)
774 				ttyflush(tp, FREAD|FWRITE);
775 			pgsignal(tp->t_pgrp, *(unsigned int *)ap->a_data, 1);
776 			if ((*(unsigned int *)ap->a_data == SIGINFO) &&
777 			    ((tp->t_lflag&NOKERNINFO) == 0))
778 				ttyinfo(tp);
779 			return(0);
780 		}
781 	}
782 	if (ap->a_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 *)ap->a_data) {
789 			if (pti->pt_flags & PF_PKT) {
790 				pti->pt_send |= TIOCPKT_IOCTL;
791 				ptcwakeup(tp, FREAD);
792 			}
793 			tp->t_lflag |= EXTPROC;
794 		} else {
795 			if ((tp->t_lflag & EXTPROC) &&
796 			    (pti->pt_flags & PF_PKT)) {
797 				pti->pt_send |= TIOCPKT_IOCTL;
798 				ptcwakeup(tp, FREAD);
799 			}
800 			tp->t_lflag &= ~EXTPROC;
801 		}
802 		return(0);
803 	}
804 	error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
805 					      ap->a_fflag, ap->a_cred);
806 	if (error == ENOIOCTL)
807 		 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
808 	if (error == ENOIOCTL) {
809 		if (pti->pt_flags & PF_UCNTL &&
810 		    (ap->a_cmd & ~0xff) == UIOCCMD(0)) {
811 			if (ap->a_cmd & 0xff) {
812 				pti->pt_ucntl = (u_char)ap->a_cmd;
813 				ptcwakeup(tp, FREAD);
814 			}
815 			return (0);
816 		}
817 		error = ENOTTY;
818 	}
819 	/*
820 	 * If external processing and packet mode send ioctl packet.
821 	 */
822 	if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
823 		switch(ap->a_cmd) {
824 		case TIOCSETA:
825 		case TIOCSETAW:
826 		case TIOCSETAF:
827 #ifdef COMPAT_43
828 		case TIOCSETP:
829 		case TIOCSETN:
830 #endif
831 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
832 		case TIOCSETC:
833 		case TIOCSLTC:
834 		case TIOCLBIS:
835 		case TIOCLBIC:
836 		case TIOCLSET:
837 #endif
838 			pti->pt_send |= TIOCPKT_IOCTL;
839 			ptcwakeup(tp, FREAD);
840 		default:
841 			break;
842 		}
843 	}
844 	stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
845 		&& CCEQ(cc[VSTART], CTRL('q'));
846 	if (pti->pt_flags & PF_NOSTOP) {
847 		if (stop) {
848 			pti->pt_send &= ~TIOCPKT_NOSTOP;
849 			pti->pt_send |= TIOCPKT_DOSTOP;
850 			pti->pt_flags &= ~PF_NOSTOP;
851 			ptcwakeup(tp, FREAD);
852 		}
853 	} else {
854 		if (!stop) {
855 			pti->pt_send &= ~TIOCPKT_DOSTOP;
856 			pti->pt_send |= TIOCPKT_NOSTOP;
857 			pti->pt_flags |= PF_NOSTOP;
858 			ptcwakeup(tp, FREAD);
859 		}
860 	}
861 	return (error);
862 }
863 
864 
865 static void ptc_drvinit (void *unused);
866 
867 static void
868 ptc_drvinit(void *unused)
869 {
870 	int i;
871 
872 	devfs_clone_bitmap_init(&DEVFS_CLONE_BITMAP(pty));
873 	devfs_clone_bitmap_init(&DEVFS_CLONE_BITMAP(pts));
874 
875 #if 0
876 	/* Unix98 pty stuff, leave out for now */
877 	make_dev(&ptc_ops, 0, 0, 0, 0666, "ptmx");
878 	devfs_clone_handler_add("ptmx", ptyclone);
879 #endif
880 	for (i = 0; i < 256; i++) {
881 		ptyinit(i);
882 	}
883 }
884 
885 SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)
886